Browse Source

Fix TabContainer crashes

volzhs 4 years ago
parent
commit
8e10e33762
1 changed files with 11 additions and 9 deletions
  1. 11 9
      scene/gui/tab_container.cpp

+ 11 - 9
scene/gui/tab_container.cpp

@@ -352,29 +352,31 @@ void TabContainer::_notification(int p_what) {
 			// Draw unselected tabs in back
 			// Draw unselected tabs in back
 			int x = 0;
 			int x = 0;
 			int x_current = 0;
 			int x_current = 0;
+			int index = 0;
 			for (int i = 0; i < tab_widths.size(); i++) {
 			for (int i = 0; i < tab_widths.size(); i++) {
-				if (get_tab_hidden(i)) {
+				index = i + first_tab_cache;
+				if (get_tab_hidden(index)) {
 					continue;
 					continue;
 				}
 				}
 
 
 				int tab_width = tab_widths[i];
 				int tab_width = tab_widths[i];
-				if (get_tab_disabled(i + first_tab_cache)) {
-					_draw_tab(tab_disabled, font_color_disabled, i, tabs_ofs_cache + x);
-				} else if (i + first_tab_cache == current) {
+				if (get_tab_disabled(index)) {
+					_draw_tab(tab_disabled, font_color_disabled, index, tabs_ofs_cache + x);
+				} else if (index == current) {
 					x_current = x;
 					x_current = x;
 				} else {
 				} else {
-					_draw_tab(tab_bg, font_color_bg, i, tabs_ofs_cache + x);
+					_draw_tab(tab_bg, font_color_bg, index, tabs_ofs_cache + x);
 				}
 				}
 
 
 				x += tab_width;
 				x += tab_width;
-				last_tab_cache = i + first_tab_cache;
+				last_tab_cache = index;
 			}
 			}
 
 
 			// Draw the tab area.
 			// Draw the tab area.
 			panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
 			panel->draw(canvas, Rect2(0, header_height, size.width, size.height - header_height));
 
 
-			// Draw selected tab in front. Need to check tabs.size() in case of no contents at all.
-			if (tabs.size() > 0) {
+			// Draw selected tab in front. only draw selected tab when it's in visible range.
+			if (tabs.size() > 0 && current - first_tab_cache < tab_widths.size() && current >= first_tab_cache) {
 				_draw_tab(tab_fg, font_color_fg, current, tabs_ofs_cache + x_current);
 				_draw_tab(tab_fg, font_color_fg, current, tabs_ofs_cache + x_current);
 			}
 			}
 
 
@@ -427,7 +429,7 @@ void TabContainer::_draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, in
 	p_tab_style->draw(canvas, tab_rect);
 	p_tab_style->draw(canvas, tab_rect);
 
 
 	// Draw the tab contents.
 	// Draw the tab contents.
-	Control *control = Object::cast_to<Control>(tabs[p_index + first_tab_cache]);
+	Control *control = Object::cast_to<Control>(tabs[p_index]);
 	String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
 	String text = control->has_meta("_tab_name") ? String(tr(String(control->get_meta("_tab_name")))) : String(tr(control->get_name()));
 
 
 	int x_content = tab_rect.position.x + p_tab_style->get_margin(MARGIN_LEFT);
 	int x_content = tab_rect.position.x + p_tab_style->get_margin(MARGIN_LEFT);