From 54b02c41e6e3355ab7c43ddb4d8faea30e139967 Mon Sep 17 00:00:00 2001 From: Aaron Record Date: Mon, 23 May 2022 21:43:09 -0600 Subject: [PATCH] Allow ScrollBar params of a ScrollContainer to be modified from _ready() Cherrypicks b8610dbd3159985f007deb0424a64df386832d07 --- scene/gui/scroll_container.cpp | 111 +++++++++++++++++---------------- scene/gui/scroll_container.h | 1 + 2 files changed, 59 insertions(+), 53 deletions(-) diff --git a/scene/gui/scroll_container.cpp b/scene/gui/scroll_container.cpp index 22699f08a..f14b18d23 100644 --- a/scene/gui/scroll_container.cpp +++ b/scene/gui/scroll_container.cpp @@ -253,6 +253,62 @@ void ScrollContainer::ensure_control_visible(Control *p_control) { set_v_scroll(get_v_scroll() + (diff.y - global_rect.position.y)); } +void ScrollContainer::_update_dimensions() { + child_max_size = Size2(0, 0); + Size2 size = get_size(); + Point2 ofs; + + Ref sb = get_stylebox("bg"); + size -= sb->get_minimum_size(); + ofs += sb->get_offset(); + + if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons + size.y -= h_scroll->get_minimum_size().y; + } + + if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) { //scrolls may have been moved out for reasons + size.x -= v_scroll->get_minimum_size().x; + } + + for (int i = 0; i < get_child_count(); i++) { + Control *c = Object::cast_to(get_child(i)); + if (!c) { + continue; + } + if (c->is_set_as_toplevel()) { + continue; + } + if (c == h_scroll || c == v_scroll) { + continue; + } + Size2 minsize = c->get_combined_minimum_size(); + child_max_size.x = MAX(child_max_size.x, minsize.x); + child_max_size.y = MAX(child_max_size.y, minsize.y); + + Rect2 r = Rect2(-scroll, minsize); + if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) { + r.position.x = 0; + if (c->get_h_size_flags() & SIZE_EXPAND) { + r.size.width = MAX(size.width, minsize.width); + } else { + r.size.width = minsize.width; + } + } + if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) { + r.position.y = 0; + if (c->get_v_size_flags() & SIZE_EXPAND) { + r.size.height = MAX(size.height, minsize.height); + } else { + r.size.height = minsize.height; + } + } + r.position += ofs; + fit_child_in_rect(c, r); + } + + update(); +} + void ScrollContainer::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) { call_deferred("_update_scrollbar_position"); @@ -262,62 +318,11 @@ void ScrollContainer::_notification(int p_what) { Viewport *viewport = get_viewport(); ERR_FAIL_COND(!viewport); viewport->connect("gui_focus_changed", this, "_gui_focus_changed"); + _update_dimensions(); } if (p_what == NOTIFICATION_SORT_CHILDREN) { - child_max_size = Size2(0, 0); - Size2 size = get_size(); - Point2 ofs; - - Ref sb = get_stylebox("bg"); - size -= sb->get_minimum_size(); - ofs += sb->get_offset(); - - if (h_scroll->is_visible_in_tree() && h_scroll->get_parent() == this) { //scrolls may have been moved out for reasons - size.y -= h_scroll->get_minimum_size().y; - } - - if (v_scroll->is_visible_in_tree() && v_scroll->get_parent() == this) { //scrolls may have been moved out for reasons - size.x -= v_scroll->get_minimum_size().x; - } - - for (int i = 0; i < get_child_count(); i++) { - Control *c = Object::cast_to(get_child(i)); - if (!c) { - continue; - } - if (c->is_set_as_toplevel()) { - continue; - } - if (c == h_scroll || c == v_scroll) { - continue; - } - Size2 minsize = c->get_combined_minimum_size(); - child_max_size.x = MAX(child_max_size.x, minsize.x); - child_max_size.y = MAX(child_max_size.y, minsize.y); - - Rect2 r = Rect2(-scroll, minsize); - if (!scroll_h || (!h_scroll->is_visible_in_tree() && c->get_h_size_flags() & SIZE_EXPAND)) { - r.position.x = 0; - if (c->get_h_size_flags() & SIZE_EXPAND) { - r.size.width = MAX(size.width, minsize.width); - } else { - r.size.width = minsize.width; - } - } - if (!scroll_v || (!v_scroll->is_visible_in_tree() && c->get_v_size_flags() & SIZE_EXPAND)) { - r.position.y = 0; - if (c->get_v_size_flags() & SIZE_EXPAND) { - r.size.height = MAX(size.height, minsize.height); - } else { - r.size.height = minsize.height; - } - } - r.position += ofs; - fit_child_in_rect(c, r); - } - - update(); + _update_dimensions(); }; if (p_what == NOTIFICATION_DRAW) { diff --git a/scene/gui/scroll_container.h b/scene/gui/scroll_container.h index f1409c671..d13d7a1ba 100644 --- a/scene/gui/scroll_container.h +++ b/scene/gui/scroll_container.h @@ -70,6 +70,7 @@ protected: void _gui_input(const Ref &p_gui_input); void _gui_focus_changed(Control *p_control); + void _update_dimensions(); void _notification(int p_what); void _scroll_moved(float);