From 063d1a5d53e7723418a649348275fb1113483b1f Mon Sep 17 00:00:00 2001 From: kleonc <9283098+kleonc@users.noreply.github.com> Date: Wed, 4 Aug 2021 22:10:06 +0200 Subject: [PATCH] TabContainer: Fix error on removing top-level Control child, Remove _get_tab method --- scene/gui/tab_container.cpp | 69 +++++++++++++++++++------------------ scene/gui/tab_container.h | 2 +- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/scene/gui/tab_container.cpp b/scene/gui/tab_container.cpp index 22f3c1ad24..c392c5a3fe 100644 --- a/scene/gui/tab_container.cpp +++ b/scene/gui/tab_container.cpp @@ -421,7 +421,7 @@ void TabContainer::_notification(int p_what) { } void TabContainer::_draw_tab(Ref &p_tab_style, Color &p_font_color, int p_index, float p_x) { - Vector tabs = _get_tabs(); + Control *control = get_tab_control(p_index); RID canvas = get_canvas_item(); Ref font = get_font("font"); int icon_text_distance = get_constant("hseparation"); @@ -433,7 +433,6 @@ 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]); 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); @@ -475,10 +474,10 @@ void TabContainer::_repaint() { if (tabs_visible) { c->set_margin(MARGIN_TOP, _get_top_margin()); } - c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP))); - c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT))); - c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT))); - c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM))); + c->set_margin(MARGIN_TOP, c->get_margin(MARGIN_TOP) + sb->get_margin(MARGIN_TOP)); + c->set_margin(MARGIN_LEFT, c->get_margin(MARGIN_LEFT) + sb->get_margin(MARGIN_LEFT)); + c->set_margin(MARGIN_RIGHT, c->get_margin(MARGIN_RIGHT) - sb->get_margin(MARGIN_RIGHT)); + c->set_margin(MARGIN_BOTTOM, c->get_margin(MARGIN_BOTTOM) - sb->get_margin(MARGIN_BOTTOM)); } else { c->hide(); @@ -536,7 +535,7 @@ Vector TabContainer::_get_tabs() const { Vector controls; for (int i = 0; i < get_child_count(); i++) { Control *control = Object::cast_to(get_child(i)); - if (!control || control->is_toplevel_control()) { + if (!control || control->is_set_as_toplevel()) { continue; } @@ -553,10 +552,7 @@ void TabContainer::add_child_notify(Node *p_child) { Container::add_child_notify(p_child); Control *c = Object::cast_to(p_child); - if (!c) { - return; - } - if (c->is_set_as_toplevel()) { + if (!c || c->is_set_as_toplevel()) { return; } @@ -576,10 +572,10 @@ void TabContainer::add_child_notify(Node *p_child) { c->set_margin(MARGIN_TOP, _get_top_margin()); } Ref sb = get_stylebox("panel"); - c->set_margin(Margin(MARGIN_TOP), c->get_margin(Margin(MARGIN_TOP)) + sb->get_margin(Margin(MARGIN_TOP))); - c->set_margin(Margin(MARGIN_LEFT), c->get_margin(Margin(MARGIN_LEFT)) + sb->get_margin(Margin(MARGIN_LEFT))); - c->set_margin(Margin(MARGIN_RIGHT), c->get_margin(Margin(MARGIN_RIGHT)) - sb->get_margin(Margin(MARGIN_RIGHT))); - c->set_margin(Margin(MARGIN_BOTTOM), c->get_margin(Margin(MARGIN_BOTTOM)) - sb->get_margin(Margin(MARGIN_BOTTOM))); + c->set_margin(MARGIN_TOP, c->get_margin(MARGIN_TOP) + sb->get_margin(MARGIN_TOP)); + c->set_margin(MARGIN_LEFT, c->get_margin(MARGIN_LEFT) + sb->get_margin(MARGIN_LEFT)); + c->set_margin(MARGIN_RIGHT, c->get_margin(MARGIN_RIGHT) - sb->get_margin(MARGIN_RIGHT)); + c->set_margin(MARGIN_BOTTOM, c->get_margin(MARGIN_BOTTOM) - sb->get_margin(MARGIN_BOTTOM)); update(); p_child->connect("renamed", this, "_child_renamed_callback"); @@ -588,6 +584,18 @@ void TabContainer::add_child_notify(Node *p_child) { } } +void TabContainer::move_child_notify(Node *p_child) { + Container::move_child_notify(p_child); + + Control *c = Object::cast_to(p_child); + if (!c || c->is_set_as_toplevel()) { + return; + } + + _update_current_tab(); + update(); +} + int TabContainer::get_tab_count() const { return _get_tabs().size(); } @@ -631,21 +639,18 @@ Control *TabContainer::get_tab_control(int p_idx) const { } Control *TabContainer::get_current_tab_control() const { - Vector tabs = _get_tabs(); - if (current >= 0 && current < tabs.size()) { - return tabs[current]; - } else { - return nullptr; - } + return get_tab_control(current); } void TabContainer::remove_child_notify(Node *p_child) { Container::remove_child_notify(p_child); - if (!Object::cast_to(p_child)) { + Control *c = Object::cast_to(p_child); + if (!c || c->is_set_as_toplevel()) { return; } + // Defer the call because tab is not yet removed (remove_child_notify is called right before p_child is actually removed). call_deferred("_update_current_tab"); p_child->disconnect("renamed", this, "_child_renamed_callback"); @@ -856,19 +861,15 @@ bool TabContainer::is_all_tabs_in_front() const { return all_tabs_in_front; } -Control *TabContainer::_get_tab(int p_idx) const { - return get_tab_control(p_idx); -} - void TabContainer::set_tab_title(int p_tab, const String &p_title) { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND(!child); child->set_meta("_tab_name", p_title); update(); } String TabContainer::get_tab_title(int p_tab) const { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND_V(!child, ""); if (child->has_meta("_tab_name")) { return child->get_meta("_tab_name"); @@ -878,13 +879,13 @@ String TabContainer::get_tab_title(int p_tab) const { } void TabContainer::set_tab_icon(int p_tab, const Ref &p_icon) { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND(!child); child->set_meta("_tab_icon", p_icon); update(); } Ref TabContainer::get_tab_icon(int p_tab) const { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND_V(!child, Ref()); if (child->has_meta("_tab_icon")) { return child->get_meta("_tab_icon"); @@ -894,14 +895,14 @@ Ref TabContainer::get_tab_icon(int p_tab) const { } void TabContainer::set_tab_disabled(int p_tab, bool p_disabled) { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND(!child); child->set_meta("_tab_disabled", p_disabled); update(); } bool TabContainer::get_tab_disabled(int p_tab) const { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND_V(!child, false); if (child->has_meta("_tab_disabled")) { return child->get_meta("_tab_disabled"); @@ -911,7 +912,7 @@ bool TabContainer::get_tab_disabled(int p_tab) const { } void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND(!child); child->set_meta("_tab_hidden", p_hidden); update(); @@ -930,7 +931,7 @@ void TabContainer::set_tab_hidden(int p_tab, bool p_hidden) { } bool TabContainer::get_tab_hidden(int p_tab) const { - Control *child = _get_tab(p_tab); + Control *child = get_tab_control(p_tab); ERR_FAIL_COND_V(!child, false); if (child->has_meta("_tab_hidden")) { return child->get_meta("_tab_hidden"); diff --git a/scene/gui/tab_container.h b/scene/gui/tab_container.h index 4817d1cf57..efdffc5d5b 100644 --- a/scene/gui/tab_container.h +++ b/scene/gui/tab_container.h @@ -56,7 +56,6 @@ private: bool menu_hovered; int highlight_arrow; TabAlign align; - Control *_get_tab(int p_idx) const; int _get_top_margin() const; mutable ObjectID popup_obj_id; bool drag_to_rearrange_enabled; @@ -76,6 +75,7 @@ protected: void _gui_input(const Ref &p_event); void _notification(int p_what); virtual void add_child_notify(Node *p_child); + virtual void move_child_notify(Node *p_child); virtual void remove_child_notify(Node *p_child); Variant get_drag_data(const Point2 &p_point);