From ca8bc72acf343e846dac44dc9cfb37211d088252 Mon Sep 17 00:00:00 2001 From: crwusiz Date: Sat, 13 Dec 2025 18:41:42 +0900 Subject: [PATCH] UI: refactor constant declarations and remove redundancy UI_CONFIG FONT_SIZES COLORS --- selfdrive/ui/mici/onroad/hud_renderer.py | 19 +++----- selfdrive/ui/onroad/hud_renderer.py | 57 +++++++++++------------- 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/selfdrive/ui/mici/onroad/hud_renderer.py b/selfdrive/ui/mici/onroad/hud_renderer.py index 7f489ccf9813ad..669dab4b456cb6 100644 --- a/selfdrive/ui/mici/onroad/hud_renderer.py +++ b/selfdrive/ui/mici/onroad/hud_renderer.py @@ -16,7 +16,6 @@ SET_SPEED_NA = 255 KM_TO_MILE = 0.621371 CRUISE_DISABLED_CHAR = '–' - SET_SPEED_PERSISTENCE = 2.5 # seconds @@ -34,10 +33,6 @@ class Colors: WHITE_TRANSLUCENT = rl.Color(255, 255, 255, 200) -FONT_SIZES = FontSizes() -COLORS = Colors() - - class TurnIntent(Widget): FADE_IN_ANGLE = 30 # degrees @@ -249,7 +244,7 @@ def _draw_set_speed(self, rect: rl.Rectangle) -> None: self._font_display, set_speed_text, rl.Vector2(x + 13 + 4, y + 3 - 8 - 3 + 4), - FONT_SIZES.set_speed, + FontSizes.set_speed, 0, set_speed_color, ) @@ -258,8 +253,8 @@ def _draw_set_speed(self, rect: rl.Rectangle) -> None: rl.draw_text_ex( self._font_semi_bold, max_text, - rl.Vector2(x + 25, y + FONT_SIZES.set_speed - 7 + 4), - FONT_SIZES.max_speed, + rl.Vector2(x + 25, y + FontSizes.set_speed - 7 + 4), + FontSizes.max_speed, 0, max_color, ) @@ -267,11 +262,11 @@ def _draw_set_speed(self, rect: rl.Rectangle) -> None: def _draw_current_speed(self, rect: rl.Rectangle) -> None: """Draw the current vehicle speed and unit.""" speed_text = str(round(self.speed)) - speed_text_size = measure_text_cached(self._font_bold, speed_text, FONT_SIZES.current_speed) + speed_text_size = measure_text_cached(self._font_bold, speed_text, FontSizes.current_speed) speed_pos = rl.Vector2(rect.x + rect.width / 2 - speed_text_size.x / 2, 180 - speed_text_size.y / 2) - rl.draw_text_ex(self._font_bold, speed_text, speed_pos, FONT_SIZES.current_speed, 0, COLORS.WHITE) + rl.draw_text_ex(self._font_bold, speed_text, speed_pos, FontSizes.current_speed, 0, Colors.WHITE) unit_text = tr("km/h") if ui_state.is_metric else tr("mph") - unit_text_size = measure_text_cached(self._font_medium, unit_text, FONT_SIZES.speed_unit) + unit_text_size = measure_text_cached(self._font_medium, unit_text, FontSizes.speed_unit) unit_pos = rl.Vector2(rect.x + rect.width / 2 - unit_text_size.x / 2, 290 - unit_text_size.y / 2) - rl.draw_text_ex(self._font_medium, unit_text, unit_pos, FONT_SIZES.speed_unit, 0, COLORS.WHITE_TRANSLUCENT) + rl.draw_text_ex(self._font_medium, unit_text, unit_pos, FontSizes.speed_unit, 0, Colors.WHITE_TRANSLUCENT) diff --git a/selfdrive/ui/onroad/hud_renderer.py b/selfdrive/ui/onroad/hud_renderer.py index 79f150deea03d2..4c059e268559dc 100644 --- a/selfdrive/ui/onroad/hud_renderer.py +++ b/selfdrive/ui/onroad/hud_renderer.py @@ -51,11 +51,6 @@ class Colors: HEADER_GRADIENT_END = rl.BLANK -UI_CONFIG = UIConfig() -FONT_SIZES = FontSizes() -COLORS = Colors() - - class HudRenderer(Widget): def __init__(self): super().__init__() @@ -70,7 +65,7 @@ def __init__(self): self._font_bold: rl.Font = gui_app.font(FontWeight.BOLD) self._font_medium: rl.Font = gui_app.font(FontWeight.MEDIUM) - self._exp_button: ExpButton = ExpButton(UI_CONFIG.button_size, UI_CONFIG.wheel_icon_size) + self._exp_button: ExpButton = ExpButton(UIConfig.button_size, UIConfig.wheel_icon_size) def _update_state(self) -> None: """Update HUD state based on car state and controls state.""" @@ -107,9 +102,9 @@ def _render(self, rect: rl.Rectangle) -> None: int(rect.x), int(rect.y), int(rect.width), - UI_CONFIG.header_height, - COLORS.HEADER_GRADIENT_START, - COLORS.HEADER_GRADIENT_END, + UIConfig.header_height, + Colors.HEADER_GRADIENT_START, + Colors.HEADER_GRADIENT_END, ) if self.is_cruise_available: @@ -117,52 +112,52 @@ def _render(self, rect: rl.Rectangle) -> None: self._draw_current_speed(rect) - button_x = rect.x + rect.width - UI_CONFIG.border_size - UI_CONFIG.button_size - button_y = rect.y + UI_CONFIG.border_size - self._exp_button.render(rl.Rectangle(button_x, button_y, UI_CONFIG.button_size, UI_CONFIG.button_size)) + button_x = rect.x + rect.width - UIConfig.border_size - UIConfig.button_size + button_y = rect.y + UIConfig.border_size + self._exp_button.render(rl.Rectangle(button_x, button_y, UIConfig.button_size, UIConfig.button_size)) def user_interacting(self) -> bool: return self._exp_button.is_pressed def _draw_set_speed(self, rect: rl.Rectangle) -> None: """Draw the MAX speed indicator box.""" - set_speed_width = UI_CONFIG.set_speed_width_metric if ui_state.is_metric else UI_CONFIG.set_speed_width_imperial - x = rect.x + 60 + (UI_CONFIG.set_speed_width_imperial - set_speed_width) // 2 + set_speed_width = UIConfig.set_speed_width_metric if ui_state.is_metric else UIConfig.set_speed_width_imperial + x = rect.x + 60 + (UIConfig.set_speed_width_imperial - set_speed_width) // 2 y = rect.y + 45 - set_speed_rect = rl.Rectangle(x, y, set_speed_width, UI_CONFIG.set_speed_height) - rl.draw_rectangle_rounded(set_speed_rect, 0.35, 10, COLORS.BLACK_TRANSLUCENT) - rl.draw_rectangle_rounded_lines_ex(set_speed_rect, 0.35, 10, 6, COLORS.BORDER_TRANSLUCENT) + set_speed_rect = rl.Rectangle(x, y, set_speed_width, UIConfig.set_speed_height) + rl.draw_rectangle_rounded(set_speed_rect, 0.35, 10, Colors.BLACK_TRANSLUCENT) + rl.draw_rectangle_rounded_lines_ex(set_speed_rect, 0.35, 10, 6, Colors.BORDER_TRANSLUCENT) - max_color = COLORS.GREY - set_speed_color = COLORS.DARK_GREY + max_color = Colors.GREY + set_speed_color = Colors.DARK_GREY if self.is_cruise_set: - set_speed_color = COLORS.WHITE + set_speed_color = Colors.WHITE if ui_state.status == UIStatus.ENGAGED: - max_color = COLORS.ENGAGED + max_color = Colors.ENGAGED elif ui_state.status == UIStatus.DISENGAGED: - max_color = COLORS.DISENGAGED + max_color = Colors.DISENGAGED elif ui_state.status == UIStatus.OVERRIDE: - max_color = COLORS.OVERRIDE + max_color = Colors.OVERRIDE max_text = tr("MAX") - max_text_width = measure_text_cached(self._font_semi_bold, max_text, FONT_SIZES.max_speed).x + max_text_width = measure_text_cached(self._font_semi_bold, max_text, FontSizes.max_speed).x rl.draw_text_ex( self._font_semi_bold, max_text, rl.Vector2(x + (set_speed_width - max_text_width) / 2, y + 27), - FONT_SIZES.max_speed, + FontSizes.max_speed, 0, max_color, ) set_speed_text = CRUISE_DISABLED_CHAR if not self.is_cruise_set else str(round(self.set_speed)) - speed_text_width = measure_text_cached(self._font_bold, set_speed_text, FONT_SIZES.set_speed).x + speed_text_width = measure_text_cached(self._font_bold, set_speed_text, FontSizes.set_speed).x rl.draw_text_ex( self._font_bold, set_speed_text, rl.Vector2(x + (set_speed_width - speed_text_width) / 2, y + 77), - FONT_SIZES.set_speed, + FontSizes.set_speed, 0, set_speed_color, ) @@ -170,11 +165,11 @@ def _draw_set_speed(self, rect: rl.Rectangle) -> None: def _draw_current_speed(self, rect: rl.Rectangle) -> None: """Draw the current vehicle speed and unit.""" speed_text = str(round(self.speed)) - speed_text_size = measure_text_cached(self._font_bold, speed_text, FONT_SIZES.current_speed) + speed_text_size = measure_text_cached(self._font_bold, speed_text, FontSizes.current_speed) speed_pos = rl.Vector2(rect.x + rect.width / 2 - speed_text_size.x / 2, 180 - speed_text_size.y / 2) - rl.draw_text_ex(self._font_bold, speed_text, speed_pos, FONT_SIZES.current_speed, 0, COLORS.WHITE) + rl.draw_text_ex(self._font_bold, speed_text, speed_pos, FontSizes.current_speed, 0, Colors.WHITE) unit_text = tr("km/h") if ui_state.is_metric else tr("mph") - unit_text_size = measure_text_cached(self._font_medium, unit_text, FONT_SIZES.speed_unit) + unit_text_size = measure_text_cached(self._font_medium, unit_text, FontSizes.speed_unit) unit_pos = rl.Vector2(rect.x + rect.width / 2 - unit_text_size.x / 2, 290 - unit_text_size.y / 2) - rl.draw_text_ex(self._font_medium, unit_text, unit_pos, FONT_SIZES.speed_unit, 0, COLORS.WHITE_TRANSLUCENT) + rl.draw_text_ex(self._font_medium, unit_text, unit_pos, FontSizes.speed_unit, 0, Colors.WHITE_TRANSLUCENT)