Skip to content

Commit be3ecae

Browse files
committed
Merge pull request godotengine#106588 from timothyqiu/tree-new-bee
Lazy create menu and slider nodes in `Tree`
2 parents 80d77d7 + 7aa9174 commit be3ecae

File tree

2 files changed

+49
-44
lines changed

2 files changed

+49
-44
lines changed

scene/gui/tree.cpp

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,12 +3197,7 @@ int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int
31973197
} break;
31983198
case TreeItem::CELL_MODE_RANGE: {
31993199
if (!c.text.is_empty()) {
3200-
popup_menu->clear();
3201-
for (int i = 0; i < c.text.get_slice_count(","); i++) {
3202-
String s = c.text.get_slicec(',', i);
3203-
popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).is_empty() ? i : s.get_slicec(':', 1).to_int());
3204-
}
3205-
3200+
_update_popup_menu(c);
32063201
popup_menu->set_size(Size2(col_width, 0));
32073202
popup_menu->set_position(get_screen_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - theme_cache.offset);
32083203
popup_menu->popup();
@@ -3332,7 +3327,7 @@ void Tree::_text_editor_popup_modal_close() {
33323327
return; // ESC pressed, app focus lost, or forced close from code.
33333328
}
33343329

3335-
if (value_editor->has_point(value_editor->get_local_mouse_position())) {
3330+
if (value_editor && value_editor->has_point(value_editor->get_local_mouse_position())) {
33363331
return;
33373332
}
33383333

@@ -3449,6 +3444,37 @@ void Tree::value_editor_changed(double p_value) {
34493444
queue_redraw();
34503445
}
34513446

3447+
void Tree::_update_popup_menu(const TreeItem::Cell &p_cell) {
3448+
if (popup_menu == nullptr) {
3449+
popup_menu = memnew(PopupMenu);
3450+
popup_menu->hide();
3451+
add_child(popup_menu, false, INTERNAL_MODE_FRONT);
3452+
popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &Tree::popup_select));
3453+
}
3454+
popup_menu->clear();
3455+
for (int i = 0; i < p_cell.text.get_slice_count(","); i++) {
3456+
String s = p_cell.text.get_slicec(',', i);
3457+
popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).is_empty() ? i : s.get_slicec(':', 1).to_int());
3458+
}
3459+
}
3460+
3461+
void Tree::_update_value_editor(const TreeItem::Cell &p_cell) {
3462+
if (value_editor == nullptr) {
3463+
value_editor = memnew(HSlider);
3464+
value_editor->set_v_size_flags(SIZE_EXPAND_FILL);
3465+
value_editor->hide();
3466+
popup_editor_vb->add_child(value_editor);
3467+
value_editor->connect(SceneStringName(value_changed), callable_mp(this, &Tree::value_editor_changed));
3468+
}
3469+
updating_value_editor = true;
3470+
value_editor->set_min(p_cell.min);
3471+
value_editor->set_max(p_cell.max);
3472+
value_editor->set_step(p_cell.step);
3473+
value_editor->set_value(p_cell.val);
3474+
value_editor->set_exp_ratio(p_cell.expr);
3475+
updating_value_editor = false;
3476+
}
3477+
34523478
void Tree::popup_select(int p_option) {
34533479
if (!popup_edited_item) {
34543480
return;
@@ -4308,12 +4334,7 @@ bool Tree::edit_selected(bool p_force_edit) {
43084334

43094335
return true;
43104336
} else if (c.mode == TreeItem::CELL_MODE_RANGE && !c.text.is_empty()) {
4311-
popup_menu->clear();
4312-
for (int i = 0; i < c.text.get_slice_count(","); i++) {
4313-
String s2 = c.text.get_slicec(',', i);
4314-
popup_menu->add_item(s2.get_slicec(':', 0), s2.get_slicec(':', 1).is_empty() ? i : s2.get_slicec(':', 1).to_int());
4315-
}
4316-
4337+
_update_popup_menu(c);
43174338
popup_menu->set_size(Size2(rect.size.width, 0));
43184339
popup_menu->set_position(get_screen_position() + rect.position + Point2i(0, rect.size.height));
43194340
popup_menu->popup();
@@ -4322,9 +4343,16 @@ bool Tree::edit_selected(bool p_force_edit) {
43224343

43234344
return true;
43244345
} else if ((c.mode == TreeItem::CELL_MODE_STRING && !c.edit_multiline) || c.mode == TreeItem::CELL_MODE_RANGE) {
4325-
Rect2 popup_rect;
4346+
int value_editor_height = 0;
4347+
if (c.mode == TreeItem::CELL_MODE_RANGE) {
4348+
_update_value_editor(c);
4349+
value_editor_height = value_editor->get_minimum_size().height;
4350+
value_editor->show();
4351+
} else if (value_editor) {
4352+
value_editor->hide();
4353+
}
43264354

4327-
int value_editor_height = c.mode == TreeItem::CELL_MODE_RANGE ? value_editor->get_minimum_size().height : 0;
4355+
Rect2 popup_rect;
43284356
// `floor()` centers vertically.
43294357
Vector2 ofs(0, Math::floor((MAX(line_editor->get_minimum_size().height, rect.size.height - value_editor_height) - rect.size.height) / 2));
43304358

@@ -4334,8 +4362,7 @@ bool Tree::edit_selected(bool p_force_edit) {
43344362
icon_ofs = _get_cell_icon_size(c).x * popup_scale + theme_cache.h_separation;
43354363
}
43364364

4337-
popup_rect.size = rect.size;
4338-
popup_rect.size.x -= icon_ofs;
4365+
popup_rect.size = rect.size + Vector2(-icon_ofs, value_editor_height);
43394366

43404367
popup_rect.position = rect.position - ofs;
43414368
popup_rect.position.x += icon_ofs;
@@ -4351,21 +4378,6 @@ bool Tree::edit_selected(bool p_force_edit) {
43514378

43524379
text_editor->hide();
43534380

4354-
if (c.mode == TreeItem::CELL_MODE_RANGE) {
4355-
popup_rect.size.y += value_editor_height;
4356-
4357-
value_editor->show();
4358-
updating_value_editor = true;
4359-
value_editor->set_min(c.min);
4360-
value_editor->set_max(c.max);
4361-
value_editor->set_step(c.step);
4362-
value_editor->set_value(c.val);
4363-
value_editor->set_exp_ratio(c.expr);
4364-
updating_value_editor = false;
4365-
} else {
4366-
value_editor->hide();
4367-
}
4368-
43694381
popup_editor->set_position(popup_rect.position);
43704382
popup_editor->set_size(popup_rect.size * popup_scale);
43714383
if (!popup_editor->is_embedded()) {
@@ -6634,14 +6646,10 @@ Tree::Tree() {
66346646

66356647
set_focus_mode(FOCUS_ALL);
66366648

6637-
popup_menu = memnew(PopupMenu);
6638-
popup_menu->hide();
6639-
add_child(popup_menu, false, INTERNAL_MODE_FRONT);
6640-
66416649
popup_editor = memnew(Popup);
66426650
add_child(popup_editor, false, INTERNAL_MODE_FRONT);
66436651

6644-
VBoxContainer *popup_editor_vb = memnew(VBoxContainer);
6652+
popup_editor_vb = memnew(VBoxContainer);
66456653
popup_editor_vb->add_theme_constant_override("separation", 0);
66466654
popup_editor_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
66476655
popup_editor->add_child(popup_editor_vb);
@@ -6656,11 +6664,6 @@ Tree::Tree() {
66566664
text_editor->hide();
66576665
popup_editor_vb->add_child(text_editor);
66586666

6659-
value_editor = memnew(HSlider);
6660-
value_editor->set_v_size_flags(SIZE_EXPAND_FILL);
6661-
value_editor->hide();
6662-
popup_editor_vb->add_child(value_editor);
6663-
66646667
h_scroll = memnew(HScrollBar);
66656668
v_scroll = memnew(VScrollBar);
66666669

@@ -6676,8 +6679,6 @@ Tree::Tree() {
66766679
line_editor->connect(SceneStringName(text_submitted), callable_mp(this, &Tree::_line_editor_submit));
66776680
text_editor->connect(SceneStringName(gui_input), callable_mp(this, &Tree::_text_editor_gui_input));
66786681
popup_editor->connect("popup_hide", callable_mp(this, &Tree::_text_editor_popup_modal_close));
6679-
popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &Tree::popup_select));
6680-
value_editor->connect(SceneStringName(value_changed), callable_mp(this, &Tree::value_editor_changed));
66816682

66826683
set_notify_transform(true);
66836684

scene/gui/tree.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "scene/gui/control.h"
3434
#include "scene/resources/text_paragraph.h"
3535

36+
class VBoxContainer;
3637
class HScrollBar;
3738
class HSlider;
3839
class LineEdit;
@@ -529,6 +530,7 @@ class Tree : public Control {
529530
bool popup_edit_committed = true;
530531
RID accessibility_scroll_element;
531532

533+
VBoxContainer *popup_editor_vb = nullptr;
532534
Popup *popup_editor = nullptr;
533535
LineEdit *line_editor = nullptr;
534536
TextEdit *text_editor = nullptr;
@@ -559,6 +561,8 @@ class Tree : public Control {
559561
void _text_editor_popup_modal_close();
560562
void _text_editor_gui_input(const Ref<InputEvent> &p_event);
561563
void value_editor_changed(double p_value);
564+
void _update_popup_menu(const TreeItem::Cell &p_cell);
565+
void _update_value_editor(const TreeItem::Cell &p_cell);
562566

563567
void popup_select(int p_option);
564568

0 commit comments

Comments
 (0)