Skip to content

Commit 71e3fd7

Browse files
authored
system/ui: use icon for special characters (commaai#35248)
* use icon for special characters * add icon for SHIFT_DOWN_KEY
1 parent 5babe18 commit 71e3fd7

2 files changed

Lines changed: 48 additions & 12 deletions

File tree

system/ui/lib/button.py

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class TextAlignment(IntEnum):
1616
RIGHT = 2
1717

1818

19+
ICON_PADDING = 15
1920
DEFAULT_BUTTON_FONT_SIZE = 60
2021
BUTTON_ENABLED_TEXT_COLOR = rl.Color(228, 228, 228, 255)
2122
BUTTON_DISABLED_TEXT_COLOR = rl.Color(228, 228, 228, 51)
@@ -46,6 +47,7 @@ def gui_button(
4647
border_radius: int = 10, # Corner rounding in pixels
4748
text_alignment: TextAlignment = TextAlignment.CENTER,
4849
text_padding: int = 20, # Padding for left/right alignment
50+
icon = None,
4951
) -> int:
5052
result = 0
5153

@@ -68,20 +70,42 @@ def gui_button(
6870
rl.draw_rectangle_rounded(rect, roundness, 20, rl.BLACK)
6971
rl.draw_rectangle_rounded_lines_ex(rect, roundness, 20, 2, rl.WHITE)
7072

73+
# Handle icon and text positioning
7174
font = gui_app.font(font_weight)
7275
text_size = rl.measure_text_ex(font, text, font_size, 0)
7376
text_pos = rl.Vector2(0, rect.y + (rect.height - text_size.y) // 2) # Vertical centering
7477

75-
# Horizontal alignment
76-
if text_alignment == TextAlignment.LEFT:
77-
text_pos.x = rect.x + text_padding
78-
elif text_alignment == TextAlignment.CENTER:
79-
text_pos.x = rect.x + (rect.width - text_size.x) // 2
80-
elif text_alignment == TextAlignment.RIGHT:
81-
text_pos.x = rect.x + rect.width - text_size.x - text_padding
82-
83-
# Draw the button text
84-
text_color = BUTTON_ENABLED_TEXT_COLOR if is_enabled else BUTTON_DISABLED_TEXT_COLOR
85-
rl.draw_text_ex(font, text, text_pos, font_size, 0, text_color)
78+
# Draw icon if provided
79+
if icon:
80+
icon_y = rect.y + (rect.height - icon.height) / 2
81+
if text:
82+
if text_alignment == TextAlignment.LEFT:
83+
icon_x = rect.x + text_padding
84+
text_pos.x = icon_x + icon.width + ICON_PADDING
85+
elif text_alignment == TextAlignment.CENTER:
86+
total_width = icon.width + ICON_PADDING + text_size.x
87+
icon_x = rect.x + (rect.width - total_width) / 2
88+
text_pos.x = icon_x + icon.width + ICON_PADDING
89+
else: # RIGHT
90+
text_pos.x = rect.x + rect.width - text_size.x - text_padding
91+
icon_x = text_pos.x - ICON_PADDING - icon.width
92+
else:
93+
# Center icon when no text
94+
icon_x = rect.x + (rect.width - icon.width) / 2
95+
96+
rl.draw_texture_v(icon, rl.Vector2(icon_x, icon_y), rl.WHITE if is_enabled else rl.Color(255, 255, 255, 100))
97+
else:
98+
# No icon, position text normally
99+
if text_alignment == TextAlignment.LEFT:
100+
text_pos.x = rect.x + text_padding
101+
elif text_alignment == TextAlignment.CENTER:
102+
text_pos.x = rect.x + (rect.width - text_size.x) // 2
103+
elif text_alignment == TextAlignment.RIGHT:
104+
text_pos.x = rect.x + rect.width - text_size.x - text_padding
105+
106+
# Draw the button text if any
107+
if text:
108+
text_color = BUTTON_ENABLED_TEXT_COLOR if is_enabled else BUTTON_DISABLED_TEXT_COLOR
109+
rl.draw_text_ex(font, text, text_pos, font_size, 0, text_color)
86110

87111
return result

system/ui/widgets/keyboard.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def __init__(self, max_text_size: int = 255, min_text_size: int = 0, password_mo
5555

5656
self._eye_open_texture = gui_app.texture("icons/eye_open.png", 81, 54)
5757
self._eye_closed_texture = gui_app.texture("icons/eye_closed.png", 81, 54)
58+
self._key_icons = {
59+
BACKSPACE_KEY: gui_app.texture("icons/backspace.png", 60, 60),
60+
SHIFT_KEY: gui_app.texture("icons/shift.png", 60, 60),
61+
SHIFT_DOWN_KEY: gui_app.texture("icons/arrow-down.png", 60, 60),
62+
}
5863

5964
@property
6065
def text(self):
@@ -95,7 +100,14 @@ def render(self, title: str, sub_title: str):
95100
start_x += new_width
96101

97102
is_enabled = key != ENTER_KEY or len(self._input_box.text) >= self._min_text_size
98-
if gui_button(key_rect, key, is_enabled=is_enabled):
103+
result = -1
104+
if key in self._key_icons:
105+
texture = self._key_icons[key]
106+
result = gui_button(key_rect, "", icon=texture, is_enabled=is_enabled)
107+
else:
108+
result = gui_button(key_rect, key, is_enabled=is_enabled)
109+
110+
if result:
99111
if key == ENTER_KEY:
100112
return 1
101113
else:

0 commit comments

Comments
 (0)