From f00bead198a9957c978f3622fee0aa45a9bfb483 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 20 May 2025 17:22:34 +0800 Subject: [PATCH] system/ui: improve button click logic for proper press-release interaction (#35289) improve button click logic for proper press,release interaction --- system/ui/lib/button.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/system/ui/lib/button.py b/system/ui/lib/button.py index 69a5da646e..4870e333df 100644 --- a/system/ui/lib/button.py +++ b/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): - result = 1 + # 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)