Skip to content

Commit 357cace

Browse files
committed
Optimize tree size computation and the scene tree dock filter
1 parent 5950fca commit 357cace

4 files changed

Lines changed: 27 additions & 6 deletions

File tree

editor/scene/scene_tree_editor.cpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,17 @@ void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
972972
}
973973

974974
bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_selected) {
975+
TreeItem *last_selected = nullptr;
976+
bool result = _update_filter_helper(p_parent, p_scroll_to_selected, last_selected);
977+
if (p_scroll_to_selected && last_selected) {
978+
// Scrolling to the first selected in the _update_filter call above followed by the last
979+
// selected here is enough to frame all selected items as well as possible.
980+
callable_mp(tree, &Tree::scroll_to_item).call_deferred(last_selected, false);
981+
}
982+
return result;
983+
}
984+
985+
bool SceneTreeEditor::_update_filter_helper(TreeItem *p_parent, bool p_scroll_to_selected, TreeItem *&r_last_selected) {
975986
if (!p_parent) {
976987
p_parent = tree->get_root();
977988
filter_term_warning.clear();
@@ -1026,7 +1037,8 @@ bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_select
10261037
bool keep_for_children = false;
10271038
for (TreeItem *child = p_parent->get_first_child(); child; child = child->get_next()) {
10281039
// Always keep if at least one of the children are kept.
1029-
keep_for_children = _update_filter(child, p_scroll_to_selected) || keep_for_children;
1040+
// Only scroll if we haven't already found a child to scroll to.
1041+
keep_for_children = _update_filter_helper(child, p_scroll_to_selected && !keep_for_children, r_last_selected) || keep_for_children;
10301042
}
10311043

10321044
if (!is_root) {
@@ -1099,9 +1111,13 @@ bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_select
10991111
if (editor_selection) {
11001112
Node *n = get_node(p_parent->get_metadata(0));
11011113
if (selectable) {
1102-
if (p_scroll_to_selected && n && editor_selection->is_selected(n)) {
1103-
// Needs to be deferred to account for possible root visibility change.
1104-
callable_mp(tree, &Tree::scroll_to_item).call_deferred(p_parent, false);
1114+
if (n && editor_selection->is_selected(n)) {
1115+
if (p_scroll_to_selected) {
1116+
// Needs to be deferred to account for possible root visibility change.
1117+
callable_mp(tree, &Tree::scroll_to_item).call_deferred(p_parent, false);
1118+
} else {
1119+
r_last_selected = p_parent;
1120+
}
11051121
}
11061122
} else if (n) {
11071123
editor_selection->remove_node(n);

editor/scene/scene_tree_editor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class SceneTreeEditor : public Control {
146146

147147
void _test_update_tree();
148148
bool _update_filter(TreeItem *p_parent = nullptr, bool p_scroll_to_selected = false);
149+
bool _update_filter_helper(TreeItem *p_parent, bool p_scroll_to_selected, TreeItem *&r_last_selected);
149150
bool _node_matches_class_term(const Node *p_item_node, const String &p_term);
150151
bool _item_matches_all_terms(TreeItem *p_item, const PackedStringArray &p_terms);
151152
void _tree_changed();

scene/gui/tree.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,7 +2008,8 @@ int Tree::compute_item_height(TreeItem *p_item) const {
20082008
for (int i = 0; i < columns.size(); i++) {
20092009
height = MAX(height, p_item->get_minimum_size(i).y);
20102010
}
2011-
int item_min_height = MAX(theme_cache.font->get_height(theme_cache.font_size), p_item->get_custom_minimum_height());
2011+
int font_height = cache.font_height != -1 ? cache.font_height : theme_cache.font->get_height(theme_cache.font_size);
2012+
int item_min_height = MAX(font_height, p_item->get_custom_minimum_height());
20122013
if (height < item_min_height) {
20132014
height = item_min_height;
20142015
}
@@ -5024,7 +5025,8 @@ void Tree::_notification(int p_what) {
50245025
} break;
50255026

50265027
case NOTIFICATION_DRAW: {
5027-
v_scroll->set_custom_step(theme_cache.font->get_height(theme_cache.font_size));
5028+
int font_height = cache.font_height != -1 ? cache.font_height : theme_cache.font->get_height(theme_cache.font_size);
5029+
v_scroll->set_custom_step(font_height);
50285030

50295031
update_scrollbars();
50305032
RID ci = get_canvas_item();
@@ -5120,6 +5122,7 @@ void Tree::_notification(int p_what) {
51205122
}
51215123

51225124
void Tree::_update_all() {
5125+
cache.font_height = theme_cache.font->get_height(theme_cache.font_size);
51235126
for (int i = 0; i < columns.size(); i++) {
51245127
update_column(i);
51255128
}

scene/gui/tree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ class Tree : public Control {
688688
int hover_button_index_in_column = -1;
689689

690690
bool rtl = false;
691+
int font_height = -1;
691692
} cache;
692693

693694
int _get_title_button_height() const;

0 commit comments

Comments
 (0)