From 6a12fac44cf3bbc020689b4309841ee96ecd56c3 Mon Sep 17 00:00:00 2001 From: Mark Wilson <23439518+wlsnmrk@users.noreply.github.com> Date: Sun, 10 Sep 2023 22:13:04 -0400 Subject: [PATCH] Fix button up and down events with focus changes Adds a flag to guard button_up and button_down events based on whether button_down has been previously emitted. Buttons now emit button_up signals if they have emitted button_down and subsequently lose focus, do not emit button_up if they gain focus while ui_accept is still pressed, and do not emit multiple up/down signals if multiple ui_accept keys are pressed simultaneously. --- scene/gui/base_button.cpp | 11 +++++++++-- scene/gui/base_button.h | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index 34f50954932..85aa90cfee3 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -115,6 +115,11 @@ void BaseButton::_notification(int p_what) { } else if (status.hovering) { queue_redraw(); } + + if (status.pressed_down_with_focus) { + status.pressed_down_with_focus = false; + emit_signal(SNAME("button_up")); + } } break; case NOTIFICATION_VISIBILITY_CHANGED: @@ -147,9 +152,10 @@ void BaseButton::_toggled(bool p_pressed) { void BaseButton::on_action_event(Ref p_event) { Ref mouse_button = p_event; - if (p_event->is_pressed() && (mouse_button.is_null() || status.hovering)) { + if (!status.pressed_down_with_focus && p_event->is_pressed() && (mouse_button.is_null() || status.hovering)) { status.press_attempt = true; status.pressing_inside = true; + status.pressed_down_with_focus = true; emit_signal(SNAME("button_down")); } @@ -176,9 +182,10 @@ void BaseButton::on_action_event(Ref p_event) { } } - if (!p_event->is_pressed()) { + if (status.pressed_down_with_focus && !p_event->is_pressed()) { status.press_attempt = false; status.pressing_inside = false; + status.pressed_down_with_focus = false; emit_signal(SNAME("button_up")); } diff --git a/scene/gui/base_button.h b/scene/gui/base_button.h index 8405acb21db..e15539208b4 100644 --- a/scene/gui/base_button.h +++ b/scene/gui/base_button.h @@ -61,7 +61,7 @@ private: bool hovering = false; bool press_attempt = false; bool pressing_inside = false; - + bool pressed_down_with_focus = false; bool disabled = false; } status;