Fix crash when pressing up on an empty PopupMenu

This commit is contained in:
Michael Alexsander 2022-08-27 18:26:31 -03:00 committed by Relintai
parent 92bc8ed3be
commit 7fd1e3b8ac

View File

@ -243,93 +243,95 @@ void PopupMenu::_scroll(float p_factor, const Point2 &p_over) {
void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) { void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) {
ERR_FAIL_COND(p_event.is_null()); ERR_FAIL_COND(p_event.is_null());
if (p_event->is_action("ui_down") && p_event->is_pressed()) { if (!items.empty()) {
int search_from = mouse_over + 1; if (p_event->is_action("ui_down") && p_event->is_pressed()) {
if (search_from >= items.size()) { int search_from = mouse_over + 1;
search_from = 0; if (search_from >= items.size()) {
} search_from = 0;
bool match_found = false;
for (int i = search_from; i < items.size(); i++) {
if (i < 0 || i >= items.size()) {
continue;
} }
if (!items[i].separator && !items[i].disabled) { bool match_found = false;
mouse_over = i; for (int i = search_from; i < items.size(); i++) {
emit_signal("id_focused", i); if (i < 0 || i >= items.size()) {
update(); continue;
accept_event(); }
match_found = true;
break;
}
}
if (!match_found) {
// If the last item is not selectable, try re-searching from the start.
for (int i = 0; i < search_from; i++) {
if (!items[i].separator && !items[i].disabled) { if (!items[i].separator && !items[i].disabled) {
mouse_over = i; mouse_over = i;
emit_signal("id_focused", i); emit_signal("id_focused", i);
update(); update();
accept_event(); accept_event();
match_found = true;
break; break;
} }
} }
}
} else if (p_event->is_action("ui_up") && p_event->is_pressed()) {
int search_from = mouse_over - 1;
if (search_from < 0) {
search_from = items.size() - 1;
}
bool match_found = false; if (!match_found) {
for (int i = search_from; i >= 0; i--) { // If the last item is not selectable, try re-searching from the start.
if (i >= items.size()) { for (int i = 0; i < search_from; i++) {
continue; if (!items[i].separator && !items[i].disabled) {
mouse_over = i;
emit_signal("id_focused", i);
update();
accept_event();
break;
}
}
}
} else if (p_event->is_action("ui_up") && p_event->is_pressed()) {
int search_from = mouse_over - 1;
if (search_from < 0) {
search_from = items.size() - 1;
} }
if (!items[i].separator && !items[i].disabled) { bool match_found = false;
mouse_over = i; for (int i = search_from; i >= 0; i--) {
emit_signal("id_focused", i); if (i >= items.size()) {
update(); continue;
accept_event(); }
match_found = true;
break;
}
}
if (!match_found) {
// If the first item is not selectable, try re-searching from the end.
for (int i = items.size() - 1; i >= search_from; i--) {
if (!items[i].separator && !items[i].disabled) { if (!items[i].separator && !items[i].disabled) {
mouse_over = i; mouse_over = i;
emit_signal("id_focused", i); emit_signal("id_focused", i);
update(); update();
accept_event(); accept_event();
match_found = true;
break; break;
} }
} }
}
} else if (p_event->is_action("ui_left") && p_event->is_pressed()) { if (!match_found) {
Node *n = get_parent(); // If the first item is not selectable, try re-searching from the end.
if (n && Object::cast_to<PopupMenu>(n)) { for (int i = items.size() - 1; i >= search_from; i--) {
hide(); if (!items[i].separator && !items[i].disabled) {
accept_event(); mouse_over = i;
} emit_signal("id_focused", i);
} else if (p_event->is_action("ui_right") && p_event->is_pressed()) { update();
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu != "" && submenu_over != mouse_over) { accept_event();
_activate_submenu(mouse_over, true); break;
accept_event(); }
} }
} else if (p_event->is_action("ui_accept") && p_event->is_pressed()) { }
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) { } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
if (items[mouse_over].submenu != "" && submenu_over != mouse_over) { Node *n = get_parent();
if (n && Object::cast_to<PopupMenu>(n)) {
hide();
accept_event();
}
} else if (p_event->is_action("ui_right") && p_event->is_pressed()) {
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator && items[mouse_over].submenu != "" && submenu_over != mouse_over) {
_activate_submenu(mouse_over, true); _activate_submenu(mouse_over, true);
} else { accept_event();
activate_item(mouse_over); }
} else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
if (mouse_over >= 0 && mouse_over < items.size() && !items[mouse_over].separator) {
if (items[mouse_over].submenu != "" && submenu_over != mouse_over) {
_activate_submenu(mouse_over, true);
} else {
activate_item(mouse_over);
}
accept_event();
} }
accept_event();
} }
} }