소스 검색

Fix crash when pressing up on an empty `PopupMenu`

(cherry picked from commit f0d380c9fd7e285b4ea0bb1dd334ef4bed3422f5)
Michael Alexsander 3 년 전
부모
커밋
5becfce603
1개의 변경된 파일64개의 추가작업 그리고 62개의 파일을 삭제
  1. 64 62
      scene/gui/popup_menu.cpp

+ 64 - 62
scene/gui/popup_menu.cpp

@@ -223,93 +223,95 @@ void PopupMenu::_scroll(float p_factor, const Point2 &p_over) {
 void PopupMenu::_gui_input(const Ref<InputEvent> &p_event) {
 	ERR_FAIL_COND(p_event.is_null());
 
-	if (p_event->is_action("ui_down") && p_event->is_pressed()) {
-		int search_from = mouse_over + 1;
-		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.empty()) {
+		if (p_event->is_action("ui_down") && p_event->is_pressed()) {
+			int search_from = mouse_over + 1;
+			if (search_from >= items.size()) {
+				search_from = 0;
 			}
 
-			if (!items[i].separator && !items[i].disabled) {
-				mouse_over = i;
-				emit_signal("id_focused", i);
-				update();
-				accept_event();
-				match_found = true;
-				break;
-			}
-		}
+			bool match_found = false;
+			for (int i = search_from; i < items.size(); i++) {
+				if (i < 0 || i >= items.size()) {
+					continue;
+				}
 
-		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) {
 					mouse_over = i;
 					emit_signal("id_focused", i);
 					update();
 					accept_event();
+					match_found = true;
 					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;
-		for (int i = search_from; i >= 0; i--) {
-			if (i >= items.size()) {
-				continue;
+			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) {
+						mouse_over = i;
+						emit_signal("id_focused", i);
+						update();
+						accept_event();
+						break;
+					}
+				}
 			}
-
-			if (!items[i].separator && !items[i].disabled) {
-				mouse_over = i;
-				emit_signal("id_focused", i);
-				update();
-				accept_event();
-				match_found = true;
-				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 (!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--) {
+			bool match_found = false;
+			for (int i = search_from; i >= 0; i--) {
+				if (i >= items.size()) {
+					continue;
+				}
+
 				if (!items[i].separator && !items[i].disabled) {
 					mouse_over = i;
 					emit_signal("id_focused", i);
 					update();
 					accept_event();
+					match_found = true;
 					break;
 				}
 			}
-		}
-	} else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
-		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);
-			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) {
-			if (items[mouse_over].submenu != "" && submenu_over != mouse_over) {
+
+			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) {
+						mouse_over = i;
+						emit_signal("id_focused", i);
+						update();
+						accept_event();
+						break;
+					}
+				}
+			}
+		} else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
+			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);
-			} else {
-				activate_item(mouse_over);
+				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) {
+				if (items[mouse_over].submenu != "" && submenu_over != mouse_over) {
+					_activate_submenu(mouse_over);
+				} else {
+					activate_item(mouse_over);
+				}
+				accept_event();
 			}
-			accept_event();
 		}
 	}