Skip to content

Commit

Permalink
Merge pull request #64635 from YeldhamDev/menu_buttons_popup_fix
Browse files Browse the repository at this point in the history
Make `Menu/OptionButton` item auto-highlight behave better
  • Loading branch information
akien-mga authored Aug 24, 2022
2 parents 2961651 + 1da5069 commit b7d2ba1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
6 changes: 6 additions & 0 deletions scene/gui/base_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ void BaseButton::gui_input(const Ref<InputEvent> &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;
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions scene/gui/base_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class BaseButton : public Control {
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> shortcut;
ObjectID shortcut_context;
Expand Down Expand Up @@ -81,6 +82,7 @@ class BaseButton : public Control {
void _notification(int p_what);

bool _is_focus_owner_in_shortcut_context() const;
bool _was_pressed_by_mouse() const;

GDVIRTUAL0(_pressed)
GDVIRTUAL1(_toggled, bool)
Expand Down
4 changes: 1 addition & 3 deletions scene/gui/menu_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions scene/gui/option_button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b7d2ba1

Please sign in to comment.