Skip to content

Commit 4e094bc

Browse files
authored
raylib UI: fix scrolling click behavior (commaai#35609)
see look how nice using base classes are
1 parent 0218ae8 commit 4e094bc

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

system/ui/lib/list_view.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ def __init__(self, initial_state: bool = False, width: int = TOGGLE_WIDTH, enabl
5252
self.toggle = Toggle(initial_state=initial_state)
5353
self.state = initial_state
5454

55+
def set_touch_valid_callback(self, touch_callback: Callable[[], bool]) -> None:
56+
super().set_touch_valid_callback(touch_callback)
57+
self.toggle.set_touch_valid_callback(touch_callback)
58+
5559
def _render(self, rect: rl.Rectangle) -> bool:
5660
self.toggle.set_enabled(self.enabled)
5761
self.toggle.render(rl.Rectangle(rect.x, rect.y + (rect.height - TOGGLE_HEIGHT) / 2, self._rect.width, TOGGLE_HEIGHT))
@@ -163,7 +167,7 @@ def _render(self, rect: rl.Rectangle) -> bool:
163167
# Check button state
164168
mouse_pos = rl.get_mouse_position()
165169
is_hovered = rl.check_collision_point_rec(mouse_pos, button_rect)
166-
is_pressed = is_hovered and rl.is_mouse_button_down(rl.MouseButton.MOUSE_BUTTON_LEFT)
170+
is_pressed = is_hovered and rl.is_mouse_button_down(rl.MouseButton.MOUSE_BUTTON_LEFT) and self._is_pressed
167171
is_selected = i == self.selected_button
168172

169173
# Button colors
@@ -184,7 +188,7 @@ def _render(self, rect: rl.Rectangle) -> bool:
184188
rl.draw_text_ex(self._font, text, rl.Vector2(text_x, text_y), 40, 0, rl.Color(228, 228, 228, 255))
185189

186190
# Handle click
187-
if is_hovered and rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT):
191+
if is_hovered and rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT) and self._is_pressed:
188192
clicked = i
189193

190194
if clicked >= 0:
@@ -216,6 +220,11 @@ def __init__(self, title: str = "", icon: str | None = None, description: str |
216220
self._prev_description: str | None = None
217221
self._description_height: float = 0
218222

223+
def set_touch_valid_callback(self, touch_callback: Callable[[], bool]) -> None:
224+
super().set_touch_valid_callback(touch_callback)
225+
if self.action_item:
226+
self.action_item.set_touch_valid_callback(touch_callback)
227+
219228
def set_parent_rect(self, parent_rect: rl.Rectangle):
220229
super().set_parent_rect(parent_rect)
221230
self._rect.width = parent_rect.width

system/ui/lib/toggle.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ def __init__(self, initial_state=False):
2323
def set_rect(self, rect: rl.Rectangle):
2424
self._rect = rl.Rectangle(rect.x, rect.y, WIDTH, HEIGHT)
2525

26-
def handle_input(self):
26+
def _handle_mouse_release(self, mouse_pos: rl.Vector2):
2727
if not self._enabled:
28-
return 0
28+
return
2929

30-
if rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT):
31-
if rl.check_collision_point_rec(rl.get_mouse_position(), self._rect):
32-
self._state = not self._state
33-
self._target = 1.0 if self._state else 0.0
34-
return 1
35-
return 0
30+
self._state = not self._state
31+
self._target = 1.0 if self._state else 0.0
3632

3733
def get_state(self):
3834
return self._state
@@ -72,7 +68,5 @@ def _render(self, rect: rl.Rectangle):
7268
knob_y = self._rect.y + HEIGHT / 2
7369
rl.draw_circle(int(knob_x), int(knob_y), HEIGHT / 2, knob_color)
7470

75-
return self.handle_input()
76-
7771
def _blend_color(self, c1, c2, t):
7872
return rl.Color(int(c1.r + (c2.r - c1.r) * t), int(c1.g + (c2.g - c1.g) * t), int(c1.b + (c2.b - c1.b) * t), 255)

0 commit comments

Comments
 (0)