Fix some corner cases in the Menu/OptionButton item auto-highlight

This commit is contained in:
Michael Alexsander 2022-08-27 13:41:58 -03:00 committed by Relintai
parent 7fd1e3b8ac
commit bce30062a5
6 changed files with 41 additions and 11 deletions

View File

@ -317,6 +317,7 @@
<argument index="0" name="index" type="int" /> <argument index="0" name="index" type="int" />
<description> <description>
Sets the currently focused item as the given [code]index[/code]. Sets the currently focused item as the given [code]index[/code].
Passing [code]-1[/code] as the index makes so that no item is focused.
</description> </description>
</method> </method>
<method name="set_hide_on_window_lose_focus"> <method name="set_hide_on_window_lose_focus">

View File

@ -66,8 +66,9 @@ void BaseButton::_gui_input(Ref<InputEvent> p_event) {
bool button_masked = mouse_button.is_valid() && ((1 << (mouse_button->get_button_index() - 1)) & button_mask) > 0; bool button_masked = mouse_button.is_valid() && ((1 << (mouse_button->get_button_index() - 1)) & button_mask) > 0;
if (button_masked || ui_accept) { if (button_masked || ui_accept) {
was_mouse_pressed = button_masked; was_mouse_pressed = button_masked;
on_action_event(p_event); on_action_event(p_event);
was_mouse_pressed = false;
return; return;
} }

View File

@ -65,9 +65,14 @@ void MenuButton::pressed() {
popup->set_scale(get_global_transform().get_scale()); popup->set_scale(get_global_transform().get_scale());
popup->set_parent_rect(Rect2(Point2(gp - popup->get_global_position()), get_size())); popup->set_parent_rect(Rect2(Point2(gp - popup->get_global_position()), get_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 enabled item focused.
if (popup->get_item_count() > 0 && !_was_pressed_by_mouse()) { if (!_was_pressed_by_mouse()) {
popup->set_current_index(0); for (int i = 0; i < popup->get_item_count(); i++) {
if (!popup->is_item_disabled(i)) {
popup->set_current_index(i);
break;
}
}
} }
popup->popup(); popup->popup();

View File

@ -115,9 +115,18 @@ void OptionButton::pressed() {
popup->set_size(Size2(size.width, 0)); popup->set_size(Size2(size.width, 0));
popup->set_scale(get_global_transform().get_scale()); popup->set_scale(get_global_transform().get_scale());
// If not triggered by the mouse, start the popup with its first item selected. // If not triggered by the mouse, start the popup with the checked item (or the first enabled one) focused.
if (popup->get_item_count() > 0 && !_was_pressed_by_mouse()) { if (!_was_pressed_by_mouse()) {
popup->set_current_index(0); if (current > -1 && !popup->is_item_disabled(current)) {
popup->set_current_index(current);
} else {
for (int i = 0; i < popup->get_item_count(); i++) {
if (!popup->is_item_disabled(i)) {
popup->set_current_index(i);
break;
}
}
}
} }
popup->popup(); popup->popup();

View File

@ -191,9 +191,14 @@ void PopupMenu::_activate_submenu(int over, bool p_by_keyboard) {
PopupMenu *pum = Object::cast_to<PopupMenu>(pm); PopupMenu *pum = Object::cast_to<PopupMenu>(pm);
if (pum) { if (pum) {
// 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 enabled item focused.
if (pum->get_item_count() > 0 && p_by_keyboard) { if (p_by_keyboard) {
pum->set_current_index(0); for (int i = 0; i < pum->get_item_count(); i++) {
if (!pum->is_item_disabled(i)) {
pum->set_current_index(i);
break;
}
}
} }
// Setting autohide areas *must* be done after `popup()` which can move the popup (to fit it into the viewport). // Setting autohide areas *must* be done after `popup()` which can move the popup (to fit it into the viewport).
@ -1071,7 +1076,14 @@ bool PopupMenu::is_item_shortcut_disabled(int p_idx) const {
} }
void PopupMenu::set_current_index(int p_idx) { void PopupMenu::set_current_index(int p_idx) {
ERR_FAIL_INDEX(p_idx, items.size()); if (p_idx != -1) {
ERR_FAIL_INDEX(p_idx, items.size());
}
if (mouse_over == p_idx) {
return;
}
mouse_over = p_idx; mouse_over = p_idx;
update(); update();
} }

View File

@ -2080,6 +2080,8 @@ void Viewport::_gui_input_event(Ref<InputEvent> p_event) {
popup_menu->hide(); popup_menu->hide();
menu_button->pressed(); menu_button->pressed();
// As the popup wasn't triggered by a mouse click, the item focus needs to be removed manually.
menu_button->get_popup()->set_current_index(-1);
} else { } else {
over = nullptr; //nothing can be found outside the modal stack over = nullptr; //nothing can be found outside the modal stack
} }