mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-06 22:22:11 +08:00
raylib UI: fix scrolling click behavior (#35609)
see look how nice using base classes are
This commit is contained in:
@@ -52,6 +52,10 @@ class ToggleAction(ItemAction):
|
|||||||
self.toggle = Toggle(initial_state=initial_state)
|
self.toggle = Toggle(initial_state=initial_state)
|
||||||
self.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:
|
def _render(self, rect: rl.Rectangle) -> bool:
|
||||||
self.toggle.set_enabled(self.enabled)
|
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))
|
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
|
# Check button state
|
||||||
mouse_pos = rl.get_mouse_position()
|
mouse_pos = rl.get_mouse_position()
|
||||||
is_hovered = rl.check_collision_point_rec(mouse_pos, button_rect)
|
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
|
is_selected = i == self.selected_button
|
||||||
|
|
||||||
# Button colors
|
# 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))
|
rl.draw_text_ex(self._font, text, rl.Vector2(text_x, text_y), 40, 0, rl.Color(228, 228, 228, 255))
|
||||||
|
|
||||||
# Handle click
|
# 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
|
clicked = i
|
||||||
|
|
||||||
if clicked >= 0:
|
if clicked >= 0:
|
||||||
@@ -216,6 +220,11 @@ class ListItem(Widget):
|
|||||||
self._prev_description: str | None = None
|
self._prev_description: str | None = None
|
||||||
self._description_height: float = 0
|
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):
|
def set_parent_rect(self, parent_rect: rl.Rectangle):
|
||||||
super().set_parent_rect(parent_rect)
|
super().set_parent_rect(parent_rect)
|
||||||
self._rect.width = parent_rect.width
|
self._rect.width = parent_rect.width
|
||||||
|
|||||||
+4
-10
@@ -23,16 +23,12 @@ class Toggle(Widget):
|
|||||||
def set_rect(self, rect: rl.Rectangle):
|
def set_rect(self, rect: rl.Rectangle):
|
||||||
self._rect = rl.Rectangle(rect.x, rect.y, WIDTH, HEIGHT)
|
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:
|
if not self._enabled:
|
||||||
return 0
|
return
|
||||||
|
|
||||||
if rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT):
|
self._state = not self._state
|
||||||
if rl.check_collision_point_rec(rl.get_mouse_position(), self._rect):
|
self._target = 1.0 if self._state else 0.0
|
||||||
self._state = not self._state
|
|
||||||
self._target = 1.0 if self._state else 0.0
|
|
||||||
return 1
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def get_state(self):
|
def get_state(self):
|
||||||
return self._state
|
return self._state
|
||||||
@@ -72,7 +68,5 @@ class Toggle(Widget):
|
|||||||
knob_y = self._rect.y + HEIGHT / 2
|
knob_y = self._rect.y + HEIGHT / 2
|
||||||
rl.draw_circle(int(knob_x), int(knob_y), HEIGHT / 2, knob_color)
|
rl.draw_circle(int(knob_x), int(knob_y), HEIGHT / 2, knob_color)
|
||||||
|
|
||||||
return self.handle_input()
|
|
||||||
|
|
||||||
def _blend_color(self, c1, c2, t):
|
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)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user