From 1da50698fcb4bc8350c89c6777b5960bc2cc758a Mon Sep 17 00:00:00 2001 From: Michael Alexsander Date: Fri, 19 Aug 2022 14:19:39 -0300 Subject: [PATCH] Make `Menu/OptionButton` item auto-highlight behave better --- scene/gui/base_button.cpp | 6 ++++++ scene/gui/base_button.h | 2 ++ scene/gui/menu_button.cpp | 4 +--- scene/gui/option_button.cpp | 3 +-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/scene/gui/base_button.cpp b/scene/gui/base_button.cpp index 776623f7ced..47f9e42edd8 100644 --- a/scene/gui/base_button.cpp +++ b/scene/gui/base_button.cpp @@ -64,6 +64,8 @@ void BaseButton::gui_input(const Ref &p_event) { bool button_masked = mouse_button.is_valid() && (mouse_button_to_mask(mouse_button->get_button_index()) & button_mask) != MouseButton::NONE; if (button_masked || ui_accept) { + was_mouse_pressed = button_masked; + on_action_event(p_event); return; } @@ -417,6 +419,10 @@ bool BaseButton::_is_focus_owner_in_shortcut_context() const { return ctx_node && vp_focus && (ctx_node == vp_focus || ctx_node->is_ancestor_of(vp_focus)); } +bool BaseButton::_was_pressed_by_mouse() const { + return was_mouse_pressed; +} + void BaseButton::_bind_methods() { ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed); ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed); diff --git a/scene/gui/base_button.h b/scene/gui/base_button.h index 7cf8de64321..4f124d4c89b 100644 --- a/scene/gui/base_button.h +++ b/scene/gui/base_button.h @@ -49,6 +49,7 @@ private: MouseButton button_mask = MouseButton::MASK_LEFT; bool toggle_mode = false; bool shortcut_in_tooltip = true; + bool was_mouse_pressed = false; bool keep_pressed_outside = false; Ref shortcut; ObjectID shortcut_context; @@ -81,6 +82,7 @@ protected: void _notification(int p_what); bool _is_focus_owner_in_shortcut_context() const; + bool _was_pressed_by_mouse() const; GDVIRTUAL0(_pressed) GDVIRTUAL1(_toggled, bool) diff --git a/scene/gui/menu_button.cpp b/scene/gui/menu_button.cpp index 0252f25888c..ab611079f78 100644 --- a/scene/gui/menu_button.cpp +++ b/scene/gui/menu_button.cpp @@ -99,9 +99,7 @@ void MenuButton::pressed() { popup->set_parent_rect(Rect2(Point2(gp - popup->get_position()), size)); // If not triggered by the mouse, start the popup with its first item selected. - if (popup->get_item_count() > 0 && - ((get_action_mode() == ActionMode::ACTION_MODE_BUTTON_PRESS && Input::get_singleton()->is_action_just_pressed("ui_accept")) || - (get_action_mode() == ActionMode::ACTION_MODE_BUTTON_RELEASE && Input::get_singleton()->is_action_just_released("ui_accept")))) { + if (popup->get_item_count() > 0 && !_was_pressed_by_mouse()) { popup->set_current_index(0); } diff --git a/scene/gui/option_button.cpp b/scene/gui/option_button.cpp index b26410e3185..67c7b0f5f46 100644 --- a/scene/gui/option_button.cpp +++ b/scene/gui/option_button.cpp @@ -204,8 +204,7 @@ void OptionButton::pressed() { // If not triggered by the mouse, start the popup with the checked item selected. if (popup->get_item_count() > 0) { - if ((get_action_mode() == ActionMode::ACTION_MODE_BUTTON_PRESS && Input::get_singleton()->is_action_just_pressed("ui_accept")) || - (get_action_mode() == ActionMode::ACTION_MODE_BUTTON_RELEASE && Input::get_singleton()->is_action_just_released("ui_accept"))) { + if (!_was_pressed_by_mouse()) { popup->set_current_index(current > -1 ? current : 0); } else { popup->scroll_to_item(current > -1 ? current : 0);