From 8e10e3376258631cee705ef092ab5ea2bf61bbc5 Mon Sep 17 00:00:00 2001 From: volzhs Date: Mon, 30 Nov 2020 11:02:18 +0900 Subject: [PATCH] Fix TabContainer crashes --- scene/gui/tab_container.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index 56ed363b8b3..c6fdf0ccbe5 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -352,29 +352,31 @@ void TabContainer::_notification(int p_what) { // Draw unselected tabs in back int x = 0; int x_current = 0; + int index = 0; 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; } 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; } 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; - last_tab_cache = i + first_tab_cache; + last_tab_cache = index; } // Draw the tab area. 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); } @@ -427,7 +429,7 @@ void TabContainer::_draw_tab(Ref &p_tab_style, Color &p_font_color, in p_tab_style->draw(canvas, tab_rect); // Draw the tab contents. - Control *control = Object::cast_to(tabs[p_index + first_tab_cache]); + Control *control = Object::cast_to(tabs[p_index]); 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);