system/ui: improve button click logic for proper press-release interaction (#35289)

improve button click logic for proper press,release interaction
pull/35288/head^2
Dean Lee 1 week ago committed by GitHub
parent 8752071049
commit f00bead198
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 20
      system/ui/lib/button.py

@ -41,6 +41,8 @@ BUTTON_PRESSED_BACKGROUND_COLORS = {
ButtonStyle.ACTION: rl.Color(130, 130, 130, 255),
}
_pressed_buttons: set[str] = set() # Track mouse press state globally
def gui_button(
rect: rl.Rectangle,
@ -54,6 +56,7 @@ def gui_button(
text_padding: int = 20, # Padding for left/right alignment
icon=None,
) -> int:
button_id = f"{rect.x}_{rect.y}_{rect.width}_{rect.height}"
result = 0
if button_style in (ButtonStyle.PRIMARY, ButtonStyle.DANGER) and not is_enabled:
@ -64,11 +67,24 @@ def gui_button(
# Set background color based on button type
bg_color = BUTTON_BACKGROUND_COLORS[button_style]
if is_enabled and rl.check_collision_point_rec(rl.get_mouse_position(), rect):
if rl.is_mouse_button_down(rl.MouseButton.MOUSE_BUTTON_LEFT):
mouse_over = is_enabled and rl.check_collision_point_rec(rl.get_mouse_position(), rect)
if mouse_over:
if rl.is_mouse_button_pressed(rl.MouseButton.MOUSE_BUTTON_LEFT):
# Record that this button was pressed
_pressed_buttons.add(button_id)
bg_color = BUTTON_PRESSED_BACKGROUND_COLORS[button_style]
elif rl.is_mouse_button_down(rl.MouseButton.MOUSE_BUTTON_LEFT):
bg_color = BUTTON_PRESSED_BACKGROUND_COLORS[button_style]
elif rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT):
# Check if this button was previously pressed
if button_id in _pressed_buttons:
result = 1
_pressed_buttons.remove(button_id)
# Clean up pressed state if mouse is released anywhere
if rl.is_mouse_button_released(rl.MouseButton.MOUSE_BUTTON_LEFT) and button_id in _pressed_buttons:
_pressed_buttons.remove(button_id)
# Draw the button with rounded corners
roundness = border_radius / (min(rect.width, rect.height) / 2)

Loading…
Cancel
Save