From 2033510a65e36357326848f31f6416047c8d3076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anni=20Ryyn=C3=A4nen?= Date: Sun, 23 Jun 2024 07:30:40 +0300 Subject: [PATCH] Improve button behavior when multiple mouse buttons are used at the same time - To emit `pressed`, buttons require that the press was initiated while hovering. - Controls can't grab focus from a mouse click if they're not hovered. - Hovers are updated both before and after a handled mouse button event. --- scene/gui/base_button.cpp | 10 +++------- scene/main/viewport.cpp | 4 +++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index 01e3cce78b..390f153a95 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -144,7 +144,9 @@ void BaseButton::_toggled(bool p_pressed) { } void BaseButton::on_action_event(Ref p_event) { - if (p_event->is_pressed()) { + Ref mouse_button = p_event; + + if (p_event->is_pressed() && (mouse_button.is_null() || status.hovering)) { status.press_attempt = true; status.pressing_inside = true; emit_signal(SNAME("button_down")); @@ -174,12 +176,6 @@ void BaseButton::on_action_event(Ref p_event) { } if (!p_event->is_pressed()) { - Ref mouse_button = p_event; - if (mouse_button.is_valid()) { - if (!has_point(mouse_button->get_position())) { - status.hovering = false; - } - } status.press_attempt = false; status.pressing_inside = false; emit_signal(SNAME("button_up")); diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp index 4d75e06ff9..e8b942cf04 100644 --- a/scene/main/viewport.cpp +++ b/scene/main/viewport.cpp @@ -1768,7 +1768,9 @@ void Viewport::_gui_input_event(Ref p_event) { Control *control = Object::cast_to(ci); if (control) { if (control->get_focus_mode() != Control::FOCUS_NONE) { - if (control != gui.key_focus) { + // Grabbing unhovered focus can cause issues when mouse is dragged + // with another button held down. + if (control != gui.key_focus && gui.mouse_over_hierarchy.has(control)) { control->grab_focus(); } break;