raylib UI: fix scrolling click behavior (#35609)

see look how nice using base classes are
pull/35610/head
Shane Smiskol 2 months ago committed by GitHub
parent 0218ae82ed
commit 4e094bc740
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 13
      system/ui/lib/list_view.py
  2. 14
      system/ui/lib/toggle.py

@ -52,6 +52,10 @@ class ToggleAction(ItemAction):
self.toggle = Toggle(initial_state=initial_state)
self.state = initial_state
def set_touch_valid_callback(self, touch_callback: Callable[[], bool]) -> None:
super().set_touch_valid_callback(touch_callback)
self.toggle.set_touch_valid_callback(touch_callback)
def _render(self, rect: rl.Rectangle) -> bool:
self.toggle.set_enabled(self.enabled)
self.toggle.render(rl.Rectangle(rect.x, rect.y + (rect.height - TOGGLE_HEIGHT) / 2, self._rect.width, TOGGLE_HEIGHT))
@ -163,7 +167,7 @@ class MultipleButtonAction(ItemAction):
# Check button state
mouse_pos = rl.get_mouse_position()
is_hovered = rl.check_collision_point_rec(mouse_pos, button_rect)
is_pressed = is_hovered and rl.is_mouse_button_down(rl.MouseButton.MOUSE_BUTTON_LEFT)
is_pressed = is_hovered and rl.is_mouse_button_down(rl.MouseButton.MOUSE_BUTTON_LEFT) and self._is_pressed
is_selected = i == self.selected_button
# Button colors
@ -184,7 +188,7 @@ class MultipleButtonAction(ItemAction):
rl.draw_text_ex(self._font, text, rl.Vector2(text_x, text_y), 40, 0, rl.Color(228, 228, 228, 255))
# Handle click
if is_hovered and rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT):
if is_hovered and rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT) and self._is_pressed:
clicked = i
if clicked >= 0:
@ -216,6 +220,11 @@ class ListItem(Widget):
self._prev_description: str | None = None
self._description_height: float = 0
def set_touch_valid_callback(self, touch_callback: Callable[[], bool]) -> None:
super().set_touch_valid_callback(touch_callback)
if self.action_item:
self.action_item.set_touch_valid_callback(touch_callback)
def set_parent_rect(self, parent_rect: rl.Rectangle):
super().set_parent_rect(parent_rect)
self._rect.width = parent_rect.width

@ -23,16 +23,12 @@ class Toggle(Widget):
def set_rect(self, rect: rl.Rectangle):
self._rect = rl.Rectangle(rect.x, rect.y, WIDTH, HEIGHT)
def handle_input(self):
def _handle_mouse_release(self, mouse_pos: rl.Vector2):
if not self._enabled:
return 0
return
if rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT):
if rl.check_collision_point_rec(rl.get_mouse_position(), self._rect):
self._state = not self._state
self._target = 1.0 if self._state else 0.0
return 1
return 0
self._state = not self._state
self._target = 1.0 if self._state else 0.0
def get_state(self):
return self._state
@ -72,7 +68,5 @@ class Toggle(Widget):
knob_y = self._rect.y + HEIGHT / 2
rl.draw_circle(int(knob_x), int(knob_y), HEIGHT / 2, knob_color)
return self.handle_input()
def _blend_color(self, c1, c2, t):
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)

Loading…
Cancel
Save