Make `Menu/OptionButton` item auto-highlight behave better

This commit is contained in:
Michael Alexsander 2022-08-19 14:19:39 -03:00
parent 35cfaafda8
commit 1da50698fc
4 changed files with 10 additions and 5 deletions

View File

@ -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; 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) { if (button_masked || ui_accept) {
was_mouse_pressed = button_masked;
on_action_event(p_event); on_action_event(p_event);
return; 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)); 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() { void BaseButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed); ClassDB::bind_method(D_METHOD("set_pressed", "pressed"), &BaseButton::set_pressed);
ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed); ClassDB::bind_method(D_METHOD("is_pressed"), &BaseButton::is_pressed);

View File

@ -49,6 +49,7 @@ private:
MouseButton button_mask = MouseButton::MASK_LEFT; MouseButton button_mask = MouseButton::MASK_LEFT;
bool toggle_mode = false; bool toggle_mode = false;
bool shortcut_in_tooltip = true; bool shortcut_in_tooltip = true;
bool was_mouse_pressed = false;
bool keep_pressed_outside = false; bool keep_pressed_outside = false;
Ref<Shortcut> shortcut; Ref<Shortcut> shortcut;
ObjectID shortcut_context; ObjectID shortcut_context;
@ -81,6 +82,7 @@ protected:
void _notification(int p_what); void _notification(int p_what);
bool _is_focus_owner_in_shortcut_context() const; bool _is_focus_owner_in_shortcut_context() const;
bool _was_pressed_by_mouse() const;
GDVIRTUAL0(_pressed) GDVIRTUAL0(_pressed)
GDVIRTUAL1(_toggled, bool) GDVIRTUAL1(_toggled, bool)

View File

@ -99,9 +99,7 @@ void MenuButton::pressed() {
popup->set_parent_rect(Rect2(Point2(gp - popup->get_position()), size)); 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 not triggered by the mouse, start the popup with its first item selected.
if (popup->get_item_count() > 0 && if (popup->get_item_count() > 0 && !_was_pressed_by_mouse()) {
((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")))) {
popup->set_current_index(0); popup->set_current_index(0);
} }

View File

@ -204,8 +204,7 @@ void OptionButton::pressed() {
// If not triggered by the mouse, start the popup with the checked item selected. // If not triggered by the mouse, start the popup with the checked item selected.
if (popup->get_item_count() > 0) { if (popup->get_item_count() > 0) {
if ((get_action_mode() == ActionMode::ACTION_MODE_BUTTON_PRESS && Input::get_singleton()->is_action_just_pressed("ui_accept")) || if (!_was_pressed_by_mouse()) {
(get_action_mode() == ActionMode::ACTION_MODE_BUTTON_RELEASE && Input::get_singleton()->is_action_just_released("ui_accept"))) {
popup->set_current_index(current > -1 ? current : 0); popup->set_current_index(current > -1 ? current : 0);
} else { } else {
popup->scroll_to_item(current > -1 ? current : 0); popup->scroll_to_item(current > -1 ? current : 0);