1/**************************************************************************/
2/* scene_tree_editor.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "scene_tree_editor.h"
32
33#include "core/config/project_settings.h"
34#include "core/object/message_queue.h"
35#include "core/object/script_language.h"
36#include "editor/editor_file_system.h"
37#include "editor/editor_node.h"
38#include "editor/editor_scale.h"
39#include "editor/editor_settings.h"
40#include "editor/editor_string_names.h"
41#include "editor/editor_undo_redo_manager.h"
42#include "editor/node_dock.h"
43#include "editor/plugins/animation_player_editor_plugin.h"
44#include "editor/plugins/canvas_item_editor_plugin.h"
45#include "editor/plugins/script_editor_plugin.h"
46#include "scene/gui/flow_container.h"
47#include "scene/gui/label.h"
48#include "scene/gui/tab_container.h"
49#include "scene/gui/texture_rect.h"
50#include "scene/main/window.h"
51#include "scene/resources/packed_scene.h"
52
53Node *SceneTreeEditor::get_scene_node() {
54 ERR_FAIL_COND_V(!is_inside_tree(), nullptr);
55
56 return get_tree()->get_edited_scene_root();
57}
58
59void SceneTreeEditor::_cell_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
60 if (p_button != MouseButton::LEFT) {
61 return;
62 }
63
64 if (connect_to_script_mode) {
65 return; //don't do anything in this mode
66 }
67
68 TreeItem *item = Object::cast_to<TreeItem>(p_item);
69 ERR_FAIL_NULL(item);
70
71 NodePath np = item->get_metadata(0);
72
73 Node *n = get_node(np);
74 ERR_FAIL_NULL(n);
75
76 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
77 if (p_id == BUTTON_SUBSCENE) {
78 if (n == get_scene_node()) {
79 if (n && n->get_scene_inherited_state().is_valid()) {
80 emit_signal(SNAME("open"), n->get_scene_inherited_state()->get_path());
81 }
82 } else {
83 emit_signal(SNAME("open"), n->get_scene_file_path());
84 }
85 } else if (p_id == BUTTON_SCRIPT) {
86 Ref<Script> script_typed = n->get_script();
87 if (!script_typed.is_null()) {
88 emit_signal(SNAME("open_script"), script_typed);
89 }
90
91 } else if (p_id == BUTTON_VISIBILITY) {
92 undo_redo->create_action(TTR("Toggle Visible"));
93 _toggle_visible(n);
94 List<Node *> selection = editor_selection->get_selected_node_list();
95 if (selection.size() > 1 && selection.find(n) != nullptr) {
96 for (Node *nv : selection) {
97 ERR_FAIL_NULL(nv);
98 if (nv == n) {
99 continue;
100 }
101 _toggle_visible(nv);
102 }
103 }
104 undo_redo->commit_action();
105 } else if (p_id == BUTTON_LOCK) {
106 undo_redo->create_action(TTR("Unlock Node"));
107
108 if (n->is_class("CanvasItem") || n->is_class("Node3D")) {
109 undo_redo->add_do_method(n, "remove_meta", "_edit_lock_");
110 undo_redo->add_undo_method(n, "set_meta", "_edit_lock_", true);
111 undo_redo->add_do_method(this, "_update_tree");
112 undo_redo->add_undo_method(this, "_update_tree");
113 undo_redo->add_do_method(this, "emit_signal", "node_changed");
114 undo_redo->add_undo_method(this, "emit_signal", "node_changed");
115 }
116 undo_redo->commit_action();
117 } else if (p_id == BUTTON_PIN) {
118 if (n->is_class("AnimationPlayer")) {
119 AnimationPlayerEditor::get_singleton()->unpin();
120 _update_tree();
121 }
122
123 } else if (p_id == BUTTON_GROUP) {
124 undo_redo->create_action(TTR("Button Group"));
125
126 if (n->is_class("CanvasItem") || n->is_class("Node3D")) {
127 undo_redo->add_do_method(n, "remove_meta", "_edit_group_");
128 undo_redo->add_undo_method(n, "set_meta", "_edit_group_", true);
129 undo_redo->add_do_method(this, "_update_tree");
130 undo_redo->add_undo_method(this, "_update_tree");
131 undo_redo->add_do_method(this, "emit_signal", "node_changed");
132 undo_redo->add_undo_method(this, "emit_signal", "node_changed");
133 }
134 undo_redo->commit_action();
135 } else if (p_id == BUTTON_WARNING) {
136 String config_err = n->get_configuration_warnings_as_string();
137 if (config_err.is_empty()) {
138 return;
139 }
140
141 const PackedInt32Array boundaries = TS->string_get_word_breaks(config_err, "", 80);
142 PackedStringArray lines;
143 for (int i = 0; i < boundaries.size(); i += 2) {
144 const int start = boundaries[i];
145 const int end = boundaries[i + 1];
146 lines.append(config_err.substr(start, end - start + 1));
147 }
148
149 warning->set_text(String("\n").join(lines));
150 warning->popup_centered();
151
152 } else if (p_id == BUTTON_SIGNALS) {
153 editor_selection->clear();
154 editor_selection->add_node(n);
155
156 set_selected(n);
157
158 TabContainer *tab_container = Object::cast_to<TabContainer>(NodeDock::get_singleton()->get_parent());
159 NodeDock::get_singleton()->get_parent()->call("set_current_tab", tab_container->get_tab_idx_from_control(NodeDock::get_singleton()));
160 NodeDock::get_singleton()->show_connections();
161
162 } else if (p_id == BUTTON_GROUPS) {
163 editor_selection->clear();
164 editor_selection->add_node(n);
165
166 set_selected(n);
167
168 TabContainer *tab_container = Object::cast_to<TabContainer>(NodeDock::get_singleton()->get_parent());
169 NodeDock::get_singleton()->get_parent()->call("set_current_tab", tab_container->get_tab_idx_from_control(NodeDock::get_singleton()));
170 NodeDock::get_singleton()->show_groups();
171 } else if (p_id == BUTTON_UNIQUE) {
172 undo_redo->create_action(TTR("Disable Scene Unique Name"));
173 undo_redo->add_do_method(n, "set_unique_name_in_owner", false);
174 undo_redo->add_undo_method(n, "set_unique_name_in_owner", true);
175 undo_redo->add_do_method(this, "_update_tree");
176 undo_redo->add_undo_method(this, "_update_tree");
177 undo_redo->commit_action();
178 }
179}
180
181void SceneTreeEditor::_toggle_visible(Node *p_node) {
182 if (p_node->has_method("is_visible") && p_node->has_method("set_visible")) {
183 bool v = bool(p_node->call("is_visible"));
184 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
185 undo_redo->add_do_method(p_node, "set_visible", !v);
186 undo_redo->add_undo_method(p_node, "set_visible", v);
187 }
188}
189
190void SceneTreeEditor::_add_nodes(Node *p_node, TreeItem *p_parent) {
191 if (!p_node) {
192 return;
193 }
194
195 // only owned nodes are editable, since nodes can create their own (manually owned) child nodes,
196 // which the editor needs not to know about.
197
198 bool part_of_subscene = false;
199
200 if (!display_foreign && p_node->get_owner() != get_scene_node() && p_node != get_scene_node()) {
201 if ((show_enabled_subscene || can_open_instance) && p_node->get_owner() && (get_scene_node()->is_editable_instance(p_node->get_owner()))) {
202 part_of_subscene = true;
203 //allow
204 } else {
205 return;
206 }
207 } else {
208 part_of_subscene = p_node != get_scene_node() && get_scene_node()->get_scene_inherited_state().is_valid() && get_scene_node()->get_scene_inherited_state()->find_node_by_path(get_scene_node()->get_path_to(p_node)) >= 0;
209 }
210
211 TreeItem *item = tree->create_item(p_parent);
212
213 item->set_text(0, p_node->get_name());
214 if (can_rename && !part_of_subscene) {
215 item->set_editable(0, true);
216 }
217
218 item->set_selectable(0, true);
219 if (can_rename) {
220 bool collapsed = p_node->is_displayed_folded();
221 if (collapsed) {
222 item->set_collapsed(true);
223 }
224 }
225
226 Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(p_node, "Node");
227 item->set_icon(0, icon);
228 item->set_metadata(0, p_node->get_path());
229
230 if (connect_to_script_mode) {
231 Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
232
233 Ref<Script> scr = p_node->get_script();
234 if (!scr.is_null() && EditorNode::get_singleton()->get_object_custom_type_base(p_node) != scr) {
235 //has script
236 item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT);
237 } else {
238 //has no script (or script is a custom type)
239 _set_item_custom_color(item, get_theme_color(SNAME("disabled_font_color"), EditorStringName(Editor)));
240 item->set_selectable(0, false);
241
242 if (!scr.is_null()) { // make sure to mark the script if a custom type
243 item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT);
244 item->set_button_disabled(0, item->get_button_count(0) - 1, true);
245 }
246
247 accent.a *= 0.7;
248 }
249
250 if (marked.has(p_node)) {
251 String node_name = p_node->get_name();
252 if (connecting_signal) {
253 node_name += " " + TTR("(Connecting From)");
254 }
255 item->set_text(0, node_name);
256 _set_item_custom_color(item, accent);
257 }
258 } else if (part_of_subscene) {
259 if (valid_types.size() == 0) {
260 _set_item_custom_color(item, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
261 }
262 } else if (marked.has(p_node)) {
263 String node_name = p_node->get_name();
264 if (connecting_signal) {
265 node_name += " " + TTR("(Connecting From)");
266 }
267 item->set_text(0, node_name);
268 item->set_selectable(0, marked_selectable);
269 _set_item_custom_color(item, get_theme_color(SNAME("accent_color"), EditorStringName(Editor)));
270 } else if (!p_node->can_process()) {
271 _set_item_custom_color(item, get_theme_color(SNAME("disabled_font_color"), EditorStringName(Editor)));
272 } else if (!marked_selectable && !marked_children_selectable) {
273 Node *node = p_node;
274 while (node) {
275 if (marked.has(node)) {
276 item->set_selectable(0, false);
277 _set_item_custom_color(item, get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
278 break;
279 }
280 node = node->get_parent();
281 }
282 }
283
284 if (can_rename) { //should be can edit..
285
286 String conf_warning = p_node->get_configuration_warnings_as_string();
287 if (!conf_warning.is_empty()) {
288 const int num_warnings = p_node->get_configuration_warnings().size();
289 String warning_icon;
290 if (num_warnings == 1) {
291 warning_icon = SNAME("NodeWarning");
292 } else if (num_warnings <= 3) {
293 warning_icon = vformat("NodeWarnings%d", num_warnings);
294 } else {
295 warning_icon = SNAME("NodeWarnings4Plus");
296 }
297
298 // Improve looks on tooltip, extra spacing on non-bullet point newlines.
299 const String bullet_point = U"• ";
300 int next_newline = 0;
301 while (next_newline != -1) {
302 next_newline = conf_warning.find("\n", next_newline + 2);
303 if (conf_warning.substr(next_newline + 1, bullet_point.length()) != bullet_point) {
304 conf_warning = conf_warning.insert(next_newline + 1, " ");
305 }
306 }
307
308 String newline = (num_warnings == 1 ? "\n" : "\n\n");
309
310 item->add_button(0, get_editor_theme_icon(warning_icon), BUTTON_WARNING, false, TTR("Node configuration warning:") + newline + conf_warning);
311 }
312
313 if (p_node->is_unique_name_in_owner()) {
314 item->add_button(0, get_editor_theme_icon(SNAME("SceneUniqueName")), BUTTON_UNIQUE, false, vformat(TTR("This node can be accessed from within anywhere in the scene by preceding it with the '%s' prefix in a node path.\nClick to disable this."), UNIQUE_NODE_PREFIX));
315 }
316
317 int num_connections = p_node->get_persistent_signal_connection_count();
318 int num_groups = p_node->get_persistent_group_count();
319
320 String msg_temp;
321 if (num_connections >= 1) {
322 Array arr;
323 arr.push_back(num_connections);
324 msg_temp += TTRN("Node has one connection.", "Node has {num} connections.", num_connections).format(arr, "{num}");
325 if (num_groups >= 1) {
326 msg_temp += "\n";
327 }
328 }
329 if (num_groups >= 1) {
330 msg_temp += TTRN("Node is in this group:", "Node is in the following groups:", num_groups) + "\n";
331
332 List<GroupInfo> groups;
333 p_node->get_groups(&groups);
334 for (const GroupInfo &E : groups) {
335 if (E.persistent) {
336 msg_temp += String::utf8("• ") + String(E.name) + "\n";
337 }
338 }
339 }
340 if (num_connections >= 1 || num_groups >= 1) {
341 if (num_groups < 1) {
342 msg_temp += "\n";
343 }
344 msg_temp += TTR("Click to show signals dock.");
345 }
346
347 Ref<Texture2D> icon_temp;
348 SceneTreeEditorButton signal_temp = BUTTON_SIGNALS;
349 if (num_connections >= 1 && num_groups >= 1) {
350 icon_temp = get_editor_theme_icon(SNAME("SignalsAndGroups"));
351 } else if (num_connections >= 1) {
352 icon_temp = get_editor_theme_icon(SNAME("Signals"));
353 } else if (num_groups >= 1) {
354 icon_temp = get_editor_theme_icon(SNAME("Groups"));
355 signal_temp = BUTTON_GROUPS;
356 }
357
358 if (num_connections >= 1 || num_groups >= 1) {
359 item->add_button(0, icon_temp, signal_temp, false, msg_temp);
360 }
361 }
362
363 {
364 // Display the node name in all tooltips so that long node names can be previewed
365 // without having to rename them.
366 String tooltip = String(p_node->get_name());
367
368 if (p_node == get_scene_node() && p_node->get_scene_inherited_state().is_valid()) {
369 item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
370 tooltip += String("\n" + TTR("Inherits:") + " " + p_node->get_scene_inherited_state()->get_path());
371 } else if (p_node != get_scene_node() && !p_node->get_scene_file_path().is_empty() && can_open_instance) {
372 item->add_button(0, get_editor_theme_icon(SNAME("InstanceOptions")), BUTTON_SUBSCENE, false, TTR("Open in Editor"));
373 tooltip += String("\n" + TTR("Instance:") + " " + p_node->get_scene_file_path());
374 }
375
376 StringName custom_type = EditorNode::get_singleton()->get_object_custom_type_name(p_node);
377 tooltip += String("\n" + TTR("Type:") + " " + (custom_type != StringName() ? String(custom_type) : p_node->get_class()));
378
379 if (!p_node->get_editor_description().is_empty()) {
380 const PackedInt32Array boundaries = TS->string_get_word_breaks(p_node->get_editor_description(), "", 80);
381 tooltip += "\n";
382
383 for (int i = 0; i < boundaries.size(); i += 2) {
384 const int start = boundaries[i];
385 const int end = boundaries[i + 1];
386 tooltip += "\n" + p_node->get_editor_description().substr(start, end - start + 1).rstrip("\n");
387 }
388 }
389
390 item->set_tooltip_text(0, tooltip);
391 }
392
393 if (can_open_instance && is_scene_tree_dock) { // Show buttons only when necessary (SceneTreeDock) to avoid crashes.
394 if (!p_node->is_connected("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed))) {
395 p_node->connect("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed).bind(p_node));
396 }
397
398 Ref<Script> scr = p_node->get_script();
399 if (!scr.is_null()) {
400 String additional_notes;
401 Color button_color = Color(1, 1, 1);
402 // Can't set tooltip after adding button, need to do it before.
403 if (scr->is_tool()) {
404 additional_notes += "\n" + TTR("This script is currently running in the editor.");
405 button_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
406 }
407 if (EditorNode::get_singleton()->get_object_custom_type_base(p_node) == scr) {
408 additional_notes += "\n" + TTR("This script is a custom type.");
409 button_color.a = 0.5;
410 }
411 item->add_button(0, get_editor_theme_icon(SNAME("Script")), BUTTON_SCRIPT, false, TTR("Open Script:") + " " + scr->get_path() + additional_notes);
412 item->set_button_color(0, item->get_button_count(0) - 1, button_color);
413 }
414
415 if (p_node->is_class("CanvasItem")) {
416 if (p_node->has_meta("_edit_lock_")) {
417 item->add_button(0, get_editor_theme_icon(SNAME("Lock")), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
418 }
419
420 if (p_node->has_meta("_edit_group_")) {
421 item->add_button(0, get_editor_theme_icon(SNAME("Group")), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
422 }
423
424 bool v = p_node->call("is_visible");
425 if (v) {
426 item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityVisible")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
427 } else {
428 item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityHidden")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
429 }
430
431 if (!p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
432 p_node->connect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed).bind(p_node));
433 }
434
435 _update_visibility_color(p_node, item);
436 } else if (p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
437 bool v = p_node->call("is_visible");
438 if (v) {
439 item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityVisible")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
440 } else {
441 item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityHidden")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
442 }
443
444 if (!p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
445 p_node->connect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed).bind(p_node));
446 }
447 } else if (p_node->is_class("Node3D")) {
448 if (p_node->has_meta("_edit_lock_")) {
449 item->add_button(0, get_editor_theme_icon(SNAME("Lock")), BUTTON_LOCK, false, TTR("Node is locked.\nClick to unlock it."));
450 }
451
452 if (p_node->has_meta("_edit_group_")) {
453 item->add_button(0, get_editor_theme_icon(SNAME("Group")), BUTTON_GROUP, false, TTR("Children are not selectable.\nClick to make them selectable."));
454 }
455
456 bool v = p_node->call("is_visible");
457 if (v) {
458 item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityVisible")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
459 } else {
460 item->add_button(0, get_editor_theme_icon(SNAME("GuiVisibilityHidden")), BUTTON_VISIBILITY, false, TTR("Toggle Visibility"));
461 }
462
463 if (!p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
464 p_node->connect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed).bind(p_node));
465 }
466
467 _update_visibility_color(p_node, item);
468 } else if (p_node->is_class("AnimationPlayer")) {
469 bool is_pinned = AnimationPlayerEditor::get_singleton()->get_player() == p_node && AnimationPlayerEditor::get_singleton()->is_pinned();
470
471 if (is_pinned) {
472 item->add_button(0, get_editor_theme_icon(SNAME("Pin")), BUTTON_PIN, false, TTR("AnimationPlayer is pinned.\nClick to unpin."));
473 }
474 }
475 }
476
477 if (editor_selection) {
478 if (editor_selection->is_selected(p_node)) {
479 item->select(0);
480 }
481 }
482
483 if (selected == p_node) {
484 if (!editor_selection) {
485 item->select(0);
486 }
487 item->set_as_cursor(0);
488 }
489
490 for (int i = 0; i < p_node->get_child_count(); i++) {
491 _add_nodes(p_node->get_child(i), item);
492 }
493
494 if (valid_types.size()) {
495 bool valid = false;
496 for (const StringName &E : valid_types) {
497 if (p_node->is_class(E) ||
498 EditorNode::get_singleton()->is_object_of_custom_type(p_node, E)) {
499 valid = true;
500 break;
501 }
502 }
503
504 if (!valid) {
505 _set_item_custom_color(item, get_theme_color(SNAME("disabled_font_color"), EditorStringName(Editor)));
506 item->set_selectable(0, false);
507 }
508 }
509}
510
511void SceneTreeEditor::_node_visibility_changed(Node *p_node) {
512 if (!p_node || (p_node != get_scene_node() && !p_node->get_owner())) {
513 return;
514 }
515
516 TreeItem *item = _find(tree->get_root(), p_node->get_path());
517
518 if (!item) {
519 return;
520 }
521
522 int idx = item->get_button_by_id(0, BUTTON_VISIBILITY);
523 ERR_FAIL_COND(idx == -1);
524
525 bool node_visible = false;
526
527 if (p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
528 node_visible = p_node->call("is_visible");
529 CanvasItemEditor::get_singleton()->get_viewport_control()->queue_redraw();
530 } else if (p_node->is_class("Node3D")) {
531 node_visible = p_node->call("is_visible");
532 }
533
534 if (node_visible) {
535 item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityVisible")));
536 } else {
537 item->set_button(0, idx, get_editor_theme_icon(SNAME("GuiVisibilityHidden")));
538 }
539
540 _update_visibility_color(p_node, item);
541}
542
543void SceneTreeEditor::_update_visibility_color(Node *p_node, TreeItem *p_item) {
544 if (p_node->is_class("CanvasItem") || p_node->is_class("Node3D")) {
545 Color color(1, 1, 1, 1);
546 bool visible_on_screen = p_node->call("is_visible_in_tree");
547 if (!visible_on_screen) {
548 color.a = 0.6;
549 }
550 int idx = p_item->get_button_by_id(0, BUTTON_VISIBILITY);
551 p_item->set_button_color(0, idx, color);
552 }
553}
554
555void SceneTreeEditor::_set_item_custom_color(TreeItem *p_item, Color p_color) {
556 p_item->set_custom_color(0, p_color);
557 p_item->set_meta(SNAME("custom_color"), p_color);
558}
559
560void SceneTreeEditor::_node_script_changed(Node *p_node) {
561 if (tree_dirty) {
562 return;
563 }
564
565 MessageQueue::get_singleton()->push_call(this, "_update_tree");
566 tree_dirty = true;
567}
568
569void SceneTreeEditor::_node_removed(Node *p_node) {
570 if (EditorNode::get_singleton()->is_exiting()) {
571 return; //speed up exit
572 }
573
574 if (p_node->is_connected("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed))) {
575 p_node->disconnect("script_changed", callable_mp(this, &SceneTreeEditor::_node_script_changed));
576 }
577
578 if (p_node->is_class("Node3D") || p_node->is_class("CanvasItem") || p_node->is_class("CanvasLayer") || p_node->is_class("Window")) {
579 if (p_node->is_connected("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed))) {
580 p_node->disconnect("visibility_changed", callable_mp(this, &SceneTreeEditor::_node_visibility_changed));
581 }
582 }
583
584 if (p_node == selected) {
585 selected = nullptr;
586 emit_signal(SNAME("node_selected"));
587 }
588}
589
590void SceneTreeEditor::_node_renamed(Node *p_node) {
591 if (p_node != get_scene_node() && !get_scene_node()->is_ancestor_of(p_node)) {
592 return;
593 }
594
595 emit_signal(SNAME("node_renamed"));
596
597 if (!tree_dirty) {
598 MessageQueue::get_singleton()->push_call(this, "_update_tree");
599 tree_dirty = true;
600 }
601}
602
603void SceneTreeEditor::_update_tree(bool p_scroll_to_selected) {
604 if (!is_inside_tree()) {
605 tree_dirty = false;
606 return;
607 }
608
609 if (tree->is_editing()) {
610 return;
611 }
612
613 updating_tree = true;
614 tree->clear();
615 if (get_scene_node()) {
616 _add_nodes(get_scene_node(), nullptr);
617 last_hash = hash_djb2_one_64(0);
618 _compute_hash(get_scene_node(), last_hash);
619 }
620 updating_tree = false;
621 tree_dirty = false;
622
623 if (!filter.strip_edges().is_empty() || !show_all_nodes) {
624 _update_filter(nullptr, p_scroll_to_selected);
625 }
626}
627
628bool SceneTreeEditor::_update_filter(TreeItem *p_parent, bool p_scroll_to_selected) {
629 if (!p_parent) {
630 p_parent = tree->get_root();
631 filter_term_warning.clear();
632 }
633
634 if (!p_parent) {
635 // Tree is empty, nothing to do here.
636 return false;
637 }
638
639 bool keep_for_children = false;
640 for (TreeItem *child = p_parent->get_first_child(); child; child = child->get_next()) {
641 // Always keep if at least one of the children are kept.
642 keep_for_children = _update_filter(child, p_scroll_to_selected) || keep_for_children;
643 }
644
645 // Now find other reasons to keep this Node, too.
646 PackedStringArray terms = filter.to_lower().split_spaces();
647 bool keep = _item_matches_all_terms(p_parent, terms);
648
649 bool selectable = keep;
650 if (keep && !valid_types.is_empty()) {
651 selectable = false;
652 Node *n = get_node(p_parent->get_metadata(0));
653
654 for (const StringName &E : valid_types) {
655 if (n->is_class(E) ||
656 EditorNode::get_singleton()->is_object_of_custom_type(n, E)) {
657 selectable = true;
658 break;
659 }
660 }
661 }
662
663 if (show_all_nodes) {
664 p_parent->set_visible(keep_for_children || keep);
665 } else {
666 // Show only selectable nodes, or parents of selectable.
667 p_parent->set_visible(keep_for_children || selectable);
668 }
669
670 if (selectable) {
671 Color custom_color = p_parent->get_meta(SNAME("custom_color"), Color(0, 0, 0, 0));
672 if (custom_color == Color(0, 0, 0, 0)) {
673 p_parent->clear_custom_color(0);
674 } else {
675 p_parent->set_custom_color(0, custom_color);
676 }
677 p_parent->set_selectable(0, true);
678 } else if (keep_for_children) {
679 p_parent->set_custom_color(0, get_theme_color(SNAME("disabled_font_color"), EditorStringName(Editor)));
680 p_parent->set_selectable(0, false);
681 p_parent->deselect(0);
682 }
683
684 if (editor_selection) {
685 Node *n = get_node(p_parent->get_metadata(0));
686 if (selectable) {
687 if (p_scroll_to_selected && n && editor_selection->is_selected(n)) {
688 tree->scroll_to_item(p_parent);
689 }
690 } else {
691 if (n && p_parent->is_selected(0)) {
692 editor_selection->remove_node(n);
693 p_parent->deselect(0);
694 }
695 }
696 }
697
698 return p_parent->is_visible();
699}
700
701bool SceneTreeEditor::_item_matches_all_terms(TreeItem *p_item, PackedStringArray p_terms) {
702 if (p_terms.is_empty()) {
703 return true;
704 }
705
706 for (int i = 0; i < p_terms.size(); i++) {
707 String term = p_terms[i];
708
709 // Recognize special filter.
710 if (term.contains(":") && !term.get_slicec(':', 0).is_empty()) {
711 String parameter = term.get_slicec(':', 0);
712 String argument = term.get_slicec(':', 1);
713
714 if (parameter == "type" || parameter == "t") {
715 // Filter by Type.
716 String type = get_node(p_item->get_metadata(0))->get_class();
717 bool term_in_inherited_class = false;
718 // Every Node is is a Node, duh!
719 while (type != "Node") {
720 if (type.to_lower().contains(argument)) {
721 term_in_inherited_class = true;
722 break;
723 }
724
725 type = ClassDB::get_parent_class(type);
726 }
727 if (!term_in_inherited_class) {
728 return false;
729 }
730 } else if (parameter == "group" || parameter == "g") {
731 // Filter by Group.
732 Node *node = get_node(p_item->get_metadata(0));
733
734 if (argument.is_empty()) {
735 // When argument is empty, match all Nodes belonging to any exposed group.
736 if (node->get_persistent_group_count() == 0) {
737 return false;
738 }
739 } else {
740 List<Node::GroupInfo> group_info_list;
741 node->get_groups(&group_info_list);
742
743 bool term_in_groups = false;
744 for (int j = 0; j < group_info_list.size(); j++) {
745 if (!group_info_list[j].persistent) {
746 continue; // Ignore internal groups.
747 }
748 if (String(group_info_list[j].name).to_lower().contains(argument)) {
749 term_in_groups = true;
750 break;
751 }
752 }
753 if (!term_in_groups) {
754 return false;
755 }
756 }
757 } else if (filter_term_warning.is_empty()) {
758 filter_term_warning = vformat(TTR("\"%s\" is not a known filter."), parameter);
759 continue;
760 }
761 } else {
762 // Default.
763 if (!p_item->get_text(0).to_lower().contains(term)) {
764 return false;
765 }
766 }
767 }
768
769 return true;
770}
771
772void SceneTreeEditor::_compute_hash(Node *p_node, uint64_t &hash) {
773 hash = hash_djb2_one_64(p_node->get_instance_id(), hash);
774 if (p_node->get_parent()) {
775 hash = hash_djb2_one_64(p_node->get_parent()->get_instance_id(), hash); //so a reparent still produces a different hash
776 }
777
778 for (int i = 0; i < p_node->get_child_count(); i++) {
779 _compute_hash(p_node->get_child(i), hash);
780 }
781}
782
783void SceneTreeEditor::_test_update_tree() {
784 pending_test_update = false;
785
786 if (!is_inside_tree()) {
787 return;
788 }
789
790 if (tree_dirty) {
791 return; // don't even bother
792 }
793
794 uint64_t hash = hash_djb2_one_64(0);
795 if (get_scene_node()) {
796 _compute_hash(get_scene_node(), hash);
797 }
798 //test hash
799 if (hash == last_hash) {
800 return; // did not change
801 }
802
803 MessageQueue::get_singleton()->push_call(this, "_update_tree");
804 tree_dirty = true;
805}
806
807void SceneTreeEditor::_tree_process_mode_changed() {
808 MessageQueue::get_singleton()->push_call(this, "_update_tree");
809 tree_dirty = true;
810}
811
812void SceneTreeEditor::_tree_changed() {
813 if (EditorNode::get_singleton()->is_exiting()) {
814 return; //speed up exit
815 }
816 if (pending_test_update) {
817 return;
818 }
819 if (tree_dirty) {
820 return;
821 }
822
823 MessageQueue::get_singleton()->push_call(this, "_test_update_tree");
824 pending_test_update = true;
825}
826
827void SceneTreeEditor::_selected_changed() {
828 TreeItem *s = tree->get_selected();
829 ERR_FAIL_NULL(s);
830 NodePath np = s->get_metadata(0);
831
832 Node *n = get_node(np);
833
834 if (n == selected) {
835 return;
836 }
837
838 selected = get_node(np);
839
840 blocked++;
841 emit_signal(SNAME("node_selected"));
842 blocked--;
843}
844
845void SceneTreeEditor::_deselect_items() {
846 // Clear currently selected items in scene tree dock.
847 if (editor_selection) {
848 editor_selection->clear();
849 emit_signal(SNAME("node_changed"));
850 }
851}
852
853void SceneTreeEditor::_cell_multi_selected(Object *p_object, int p_cell, bool p_selected) {
854 TreeItem *item = Object::cast_to<TreeItem>(p_object);
855 ERR_FAIL_NULL(item);
856
857 if (!item->is_visible()) {
858 return;
859 }
860
861 NodePath np = item->get_metadata(0);
862
863 Node *n = get_node(np);
864
865 if (!n) {
866 return;
867 }
868
869 if (!editor_selection) {
870 return;
871 }
872
873 if (p_selected) {
874 editor_selection->add_node(n);
875
876 } else {
877 editor_selection->remove_node(n);
878 }
879
880 // Emitted "selected" in _selected_changed() when select single node, so select multiple node emit "changed"
881 if (editor_selection->get_selected_nodes().size() > 1) {
882 emit_signal(SNAME("node_changed"));
883 }
884}
885
886void SceneTreeEditor::_notification(int p_what) {
887 switch (p_what) {
888 case NOTIFICATION_ENTER_TREE: {
889 get_tree()->connect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
890 get_tree()->connect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
891 get_tree()->connect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
892 get_tree()->connect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
893 get_tree()->connect("node_configuration_warning_changed", callable_mp(this, &SceneTreeEditor::_warning_changed));
894
895 tree->connect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
896
897 _update_tree();
898 } break;
899
900 case NOTIFICATION_EXIT_TREE: {
901 get_tree()->disconnect("tree_changed", callable_mp(this, &SceneTreeEditor::_tree_changed));
902 get_tree()->disconnect("tree_process_mode_changed", callable_mp(this, &SceneTreeEditor::_tree_process_mode_changed));
903 get_tree()->disconnect("node_removed", callable_mp(this, &SceneTreeEditor::_node_removed));
904 get_tree()->disconnect("node_renamed", callable_mp(this, &SceneTreeEditor::_node_renamed));
905 tree->disconnect("item_collapsed", callable_mp(this, &SceneTreeEditor::_cell_collapsed));
906 get_tree()->disconnect("node_configuration_warning_changed", callable_mp(this, &SceneTreeEditor::_warning_changed));
907 } break;
908
909 case NOTIFICATION_THEME_CHANGED: {
910 tree->add_theme_constant_override("icon_max_width", get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
911
912 _update_tree();
913 } break;
914 }
915}
916
917TreeItem *SceneTreeEditor::_find(TreeItem *p_node, const NodePath &p_path) {
918 if (!p_node) {
919 return nullptr;
920 }
921
922 NodePath np = p_node->get_metadata(0);
923 if (np == p_path) {
924 return p_node;
925 }
926
927 TreeItem *children = p_node->get_first_child();
928 while (children) {
929 TreeItem *n = _find(children, p_path);
930 if (n) {
931 return n;
932 }
933 children = children->get_next();
934 }
935
936 return nullptr;
937}
938
939void SceneTreeEditor::set_selected(Node *p_node, bool p_emit_selected) {
940 ERR_FAIL_COND(blocked > 0);
941
942 if (pending_test_update) {
943 _test_update_tree();
944 }
945 if (tree_dirty) {
946 _update_tree();
947 }
948
949 if (selected == p_node) {
950 return;
951 }
952
953 TreeItem *item = p_node ? _find(tree->get_root(), p_node->get_path()) : nullptr;
954
955 if (item) {
956 if (auto_expand_selected) {
957 // Make visible when it's collapsed.
958 TreeItem *node = item->get_parent();
959 while (node && node != tree->get_root()) {
960 node->set_collapsed(false);
961 node = node->get_parent();
962 }
963 item->select(0);
964 item->set_as_cursor(0);
965 selected = p_node;
966 tree->ensure_cursor_is_visible();
967 }
968 } else {
969 if (!p_node) {
970 selected = nullptr;
971 }
972 _update_tree();
973 selected = p_node;
974 }
975
976 if (p_emit_selected) {
977 emit_signal(SNAME("node_selected"));
978 }
979}
980
981void SceneTreeEditor::_rename_node(Node *p_node, const String &p_name) {
982 TreeItem *item = _find(tree->get_root(), p_node->get_path());
983 ERR_FAIL_NULL(item);
984 String new_name = p_name.validate_node_name();
985
986 if (new_name != p_name) {
987 String text = TTR("Invalid node name, the following characters are not allowed:") + "\n" + String::get_invalid_node_name_characters();
988 if (error->is_visible()) {
989 if (!error->get_meta("invalid_character", false)) {
990 error->set_text(error->get_text() + "\n\n" + text);
991 error->set_meta("invalid_character", true);
992 }
993 } else {
994 error->set_text(text);
995 error->set_meta("invalid_character", true);
996 error->set_meta("same_unique_name", false);
997 error->popup_centered();
998 }
999 }
1000
1001 // Trim leading/trailing whitespace to prevent node names from containing accidental whitespace, which would make it more difficult to get the node via `get_node()`.
1002 new_name = new_name.strip_edges();
1003 if (new_name.is_empty()) {
1004 // If name is empty, fallback to class name.
1005 if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
1006 new_name = Node::adjust_name_casing(p_node->get_class());
1007 } else {
1008 new_name = p_node->get_class();
1009 }
1010 }
1011
1012 if (new_name == p_node->get_name()) {
1013 if (item->get_text(0).is_empty()) {
1014 item->set_text(0, new_name);
1015 }
1016 return;
1017 }
1018
1019 // We previously made sure name is not the same as current name so that it won't complain about already used unique name when not changing name.
1020 if (p_node->is_unique_name_in_owner() && get_tree()->get_edited_scene_root()->get_node_or_null("%" + new_name)) {
1021 String text = TTR("Another node already uses this unique name in the scene.");
1022 if (error->is_visible()) {
1023 if (!error->get_meta("same_unique_name", false)) {
1024 error->set_text(error->get_text() + "\n\n" + text);
1025 error->set_meta("same_unique_name", true);
1026 }
1027 } else {
1028 error->set_text(text);
1029 error->set_meta("same_unique_name", true);
1030 error->set_meta("invalid_character", false);
1031 error->popup_centered();
1032 }
1033 item->set_text(0, p_node->get_name());
1034 return;
1035 }
1036
1037 if (!is_scene_tree_dock) {
1038 p_node->set_name(new_name);
1039 item->set_metadata(0, p_node->get_path());
1040 emit_signal(SNAME("node_renamed"));
1041 } else {
1042 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1043 undo_redo->create_action("Rename Node", UndoRedo::MERGE_DISABLE, p_node);
1044
1045 emit_signal(SNAME("node_prerename"), p_node, new_name);
1046
1047 undo_redo->add_undo_method(p_node, "set_name", p_node->get_name());
1048 undo_redo->add_undo_method(item, "set_metadata", 0, p_node->get_path());
1049 undo_redo->add_undo_method(item, "set_text", 0, p_node->get_name());
1050
1051 p_node->set_name(new_name);
1052 undo_redo->add_do_method(p_node, "set_name", new_name);
1053 undo_redo->add_do_method(item, "set_metadata", 0, p_node->get_path());
1054 undo_redo->add_do_method(item, "set_text", 0, new_name);
1055
1056 undo_redo->commit_action();
1057 }
1058}
1059
1060void SceneTreeEditor::_renamed() {
1061 TreeItem *which = tree->get_edited();
1062
1063 ERR_FAIL_NULL(which);
1064 NodePath np = which->get_metadata(0);
1065 Node *n = get_node(np);
1066 ERR_FAIL_NULL(n);
1067
1068 String new_name = which->get_text(0);
1069
1070 _rename_node(n, new_name);
1071}
1072
1073Node *SceneTreeEditor::get_selected() {
1074 return selected;
1075}
1076
1077void SceneTreeEditor::set_marked(const HashSet<Node *> &p_marked, bool p_selectable, bool p_children_selectable) {
1078 if (tree_dirty) {
1079 _update_tree();
1080 }
1081 marked = p_marked;
1082 marked_selectable = p_selectable;
1083 marked_children_selectable = p_children_selectable;
1084 _update_tree();
1085}
1086
1087void SceneTreeEditor::set_marked(Node *p_marked, bool p_selectable, bool p_children_selectable) {
1088 HashSet<Node *> s;
1089 if (p_marked) {
1090 s.insert(p_marked);
1091 }
1092 set_marked(s, p_selectable, p_children_selectable);
1093}
1094
1095void SceneTreeEditor::set_filter(const String &p_filter) {
1096 filter = p_filter;
1097 _update_filter(nullptr, true);
1098}
1099
1100String SceneTreeEditor::get_filter() const {
1101 return filter;
1102}
1103
1104String SceneTreeEditor::get_filter_term_warning() {
1105 return filter_term_warning;
1106}
1107
1108void SceneTreeEditor::set_show_all_nodes(bool p_show_all_nodes) {
1109 show_all_nodes = p_show_all_nodes;
1110 _update_filter(nullptr, true);
1111}
1112
1113void SceneTreeEditor::set_as_scene_tree_dock() {
1114 is_scene_tree_dock = true;
1115}
1116
1117void SceneTreeEditor::set_display_foreign_nodes(bool p_display) {
1118 display_foreign = p_display;
1119 _update_tree();
1120}
1121
1122void SceneTreeEditor::set_valid_types(const Vector<StringName> &p_valid) {
1123 valid_types = p_valid;
1124}
1125
1126void SceneTreeEditor::set_editor_selection(EditorSelection *p_selection) {
1127 editor_selection = p_selection;
1128 tree->set_select_mode(Tree::SELECT_MULTI);
1129 tree->set_cursor_can_exit_tree(false);
1130 editor_selection->connect("selection_changed", callable_mp(this, &SceneTreeEditor::_selection_changed));
1131}
1132
1133void SceneTreeEditor::_update_selection(TreeItem *item) {
1134 ERR_FAIL_NULL(item);
1135
1136 NodePath np = item->get_metadata(0);
1137
1138 if (!has_node(np)) {
1139 return;
1140 }
1141
1142 Node *n = get_node(np);
1143
1144 if (!n) {
1145 return;
1146 }
1147
1148 if (editor_selection->is_selected(n)) {
1149 if (!item->is_selected(0)) {
1150 item->select(0);
1151 }
1152 } else {
1153 if (item->is_selected(0)) {
1154 TreeItem *previous_cursor_item = tree->get_selected();
1155 item->deselect(0);
1156 previous_cursor_item->set_as_cursor(0);
1157 }
1158 }
1159
1160 TreeItem *c = item->get_first_child();
1161
1162 while (c) {
1163 _update_selection(c);
1164 c = c->get_next();
1165 }
1166}
1167
1168void SceneTreeEditor::_selection_changed() {
1169 if (!editor_selection) {
1170 return;
1171 }
1172
1173 TreeItem *root = tree->get_root();
1174
1175 if (!root) {
1176 return;
1177 }
1178 _update_selection(root);
1179}
1180
1181void SceneTreeEditor::_cell_collapsed(Object *p_obj) {
1182 if (updating_tree) {
1183 return;
1184 }
1185 if (!can_rename) {
1186 return;
1187 }
1188
1189 TreeItem *ti = Object::cast_to<TreeItem>(p_obj);
1190 if (!ti) {
1191 return;
1192 }
1193
1194 bool collapsed = ti->is_collapsed();
1195
1196 NodePath np = ti->get_metadata(0);
1197
1198 Node *n = get_node(np);
1199 ERR_FAIL_NULL(n);
1200
1201 n->set_display_folded(collapsed);
1202}
1203
1204Variant SceneTreeEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
1205 if (!can_rename) {
1206 return Variant(); //not editable tree
1207 }
1208
1209 if (tree->get_button_id_at_position(p_point) != -1) {
1210 return Variant(); //dragging from button
1211 }
1212
1213 Vector<Node *> selected_nodes;
1214 Vector<Ref<Texture2D>> icons;
1215 TreeItem *next = tree->get_next_selected(nullptr);
1216 while (next) {
1217 NodePath np = next->get_metadata(0);
1218
1219 Node *n = get_node(np);
1220 if (n) {
1221 // Only allow selection if not part of an instantiated scene.
1222 if (!n->get_owner() || n->get_owner() == get_scene_node() || n->get_owner()->get_scene_file_path().is_empty()) {
1223 selected_nodes.push_back(n);
1224 icons.push_back(next->get_icon(0));
1225 }
1226 }
1227 next = tree->get_next_selected(next);
1228 }
1229
1230 if (selected_nodes.is_empty()) {
1231 return Variant();
1232 }
1233
1234 VBoxContainer *vb = memnew(VBoxContainer);
1235 Array objs;
1236 int list_max = 10;
1237 float opacity_step = 1.0f / list_max;
1238 float opacity_item = 1.0f;
1239 for (int i = 0; i < selected_nodes.size(); i++) {
1240 if (i < list_max) {
1241 HBoxContainer *hb = memnew(HBoxContainer);
1242 TextureRect *tf = memnew(TextureRect);
1243 int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
1244 tf->set_custom_minimum_size(Size2(icon_size, icon_size));
1245 tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
1246 tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
1247 tf->set_texture(icons[i]);
1248 hb->add_child(tf);
1249 Label *label = memnew(Label(selected_nodes[i]->get_name()));
1250 hb->add_child(label);
1251 vb->add_child(hb);
1252 hb->set_modulate(Color(1, 1, 1, opacity_item));
1253 opacity_item -= opacity_step;
1254 }
1255 NodePath p = selected_nodes[i]->get_path();
1256 objs.push_back(p);
1257 }
1258
1259 set_drag_preview(vb);
1260 Dictionary drag_data;
1261 drag_data["type"] = "nodes";
1262 drag_data["nodes"] = objs;
1263
1264 tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
1265 emit_signal(SNAME("nodes_dragged"));
1266
1267 return drag_data;
1268}
1269
1270bool SceneTreeEditor::_is_script_type(const StringName &p_type) const {
1271 return (script_types->find(p_type));
1272}
1273
1274bool SceneTreeEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
1275 if (!can_rename) {
1276 return false; //not editable tree
1277 }
1278
1279 Dictionary d = p_data;
1280 if (!d.has("type")) {
1281 return false;
1282 }
1283
1284 TreeItem *item = tree->get_item_at_position(p_point);
1285 if (!item) {
1286 return false;
1287 }
1288
1289 int section = tree->get_drop_section_at_position(p_point);
1290 if (section < -1 || (section == -1 && !item->get_parent())) {
1291 return false;
1292 }
1293
1294 if (String(d["type"]) == "files") {
1295 Vector<String> files = d["files"];
1296
1297 if (files.size() == 0) {
1298 return false; //weird
1299 }
1300
1301 if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(files[0]))) {
1302 tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
1303 return true;
1304 }
1305
1306 bool scene_drop = true;
1307 for (int i = 0; i < files.size(); i++) {
1308 String file = files[i];
1309 String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
1310 if (ftype != "PackedScene") {
1311 scene_drop = false;
1312 break;
1313 }
1314 }
1315
1316 if (scene_drop) {
1317 tree->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN | Tree::DROP_MODE_ON_ITEM);
1318 } else {
1319 if (files.size() > 1) {
1320 return false;
1321 }
1322 tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
1323 }
1324
1325 return true;
1326 }
1327
1328 if (String(d["type"]) == "script_list_element") {
1329 ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
1330 if (se) {
1331 String sp = se->get_edited_resource()->get_path();
1332 if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
1333 tree->set_drop_mode_flags(Tree::DROP_MODE_ON_ITEM);
1334 return true;
1335 }
1336 }
1337 }
1338
1339 return String(d["type"]) == "nodes" && filter.is_empty();
1340}
1341
1342void SceneTreeEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
1343 if (!can_drop_data_fw(p_point, p_data, p_from)) {
1344 return;
1345 }
1346
1347 TreeItem *item = tree->get_item_at_position(p_point);
1348 if (!item) {
1349 return;
1350 }
1351 int section = tree->get_drop_section_at_position(p_point);
1352 if (section < -1) {
1353 return;
1354 }
1355
1356 NodePath np = item->get_metadata(0);
1357 Node *n = get_node(np);
1358 if (!n) {
1359 return;
1360 }
1361
1362 Dictionary d = p_data;
1363
1364 if (String(d["type"]) == "nodes") {
1365 Array nodes = d["nodes"];
1366 emit_signal(SNAME("nodes_rearranged"), nodes, np, section);
1367 }
1368
1369 if (String(d["type"]) == "files") {
1370 Vector<String> files = d["files"];
1371
1372 String ftype = EditorFileSystem::get_singleton()->get_file_type(files[0]);
1373 if (_is_script_type(ftype)) {
1374 emit_signal(SNAME("script_dropped"), files[0], np);
1375 } else {
1376 emit_signal(SNAME("files_dropped"), files, np, section);
1377 }
1378 }
1379
1380 if (String(d["type"]) == "script_list_element") {
1381 ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(d["script_list_element"]);
1382 if (se) {
1383 String sp = se->get_edited_resource()->get_path();
1384 if (_is_script_type(EditorFileSystem::get_singleton()->get_file_type(sp))) {
1385 emit_signal(SNAME("script_dropped"), sp, np);
1386 }
1387 }
1388 }
1389}
1390
1391void SceneTreeEditor::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) {
1392 if (p_button != MouseButton::RIGHT) {
1393 return;
1394 }
1395 _rmb_select(p_pos);
1396}
1397
1398void SceneTreeEditor::_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
1399 if (p_button != MouseButton::RIGHT) {
1400 return;
1401 }
1402 emit_signal(SNAME("rmb_pressed"), tree->get_screen_position() + p_pos);
1403}
1404
1405void SceneTreeEditor::update_warning() {
1406 _warning_changed(nullptr);
1407}
1408
1409void SceneTreeEditor::_warning_changed(Node *p_for_node) {
1410 //should use a timer
1411 update_timer->start();
1412}
1413
1414void SceneTreeEditor::set_auto_expand_selected(bool p_auto, bool p_update_settings) {
1415 if (p_update_settings) {
1416 EditorSettings::get_singleton()->set("docks/scene_tree/auto_expand_to_selected", p_auto);
1417 }
1418 auto_expand_selected = p_auto;
1419}
1420
1421void SceneTreeEditor::set_connect_to_script_mode(bool p_enable) {
1422 connect_to_script_mode = p_enable;
1423 update_tree();
1424}
1425
1426void SceneTreeEditor::set_connecting_signal(bool p_enable) {
1427 connecting_signal = p_enable;
1428 update_tree();
1429}
1430
1431void SceneTreeEditor::_bind_methods() {
1432 ClassDB::bind_method(D_METHOD("_update_tree"), &SceneTreeEditor::_update_tree, DEFVAL(false)); // Still used by UndoRedo.
1433 ClassDB::bind_method("_rename_node", &SceneTreeEditor::_rename_node);
1434 ClassDB::bind_method("_test_update_tree", &SceneTreeEditor::_test_update_tree);
1435
1436 ClassDB::bind_method(D_METHOD("update_tree"), &SceneTreeEditor::update_tree);
1437
1438 ADD_SIGNAL(MethodInfo("node_selected"));
1439 ADD_SIGNAL(MethodInfo("node_renamed"));
1440 ADD_SIGNAL(MethodInfo("node_prerename"));
1441 ADD_SIGNAL(MethodInfo("node_changed"));
1442 ADD_SIGNAL(MethodInfo("nodes_dragged"));
1443 ADD_SIGNAL(MethodInfo("nodes_rearranged", PropertyInfo(Variant::ARRAY, "paths"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
1444 ADD_SIGNAL(MethodInfo("files_dropped", PropertyInfo(Variant::PACKED_STRING_ARRAY, "files"), PropertyInfo(Variant::NODE_PATH, "to_path"), PropertyInfo(Variant::INT, "type")));
1445 ADD_SIGNAL(MethodInfo("script_dropped", PropertyInfo(Variant::STRING, "file"), PropertyInfo(Variant::NODE_PATH, "to_path")));
1446 ADD_SIGNAL(MethodInfo("rmb_pressed", PropertyInfo(Variant::VECTOR2, "position")));
1447
1448 ADD_SIGNAL(MethodInfo("open"));
1449 ADD_SIGNAL(MethodInfo("open_script"));
1450}
1451
1452SceneTreeEditor::SceneTreeEditor(bool p_label, bool p_can_rename, bool p_can_open_instance) {
1453 selected = nullptr;
1454
1455 can_rename = p_can_rename;
1456 can_open_instance = p_can_open_instance;
1457 editor_selection = nullptr;
1458
1459 if (p_label) {
1460 Label *label = memnew(Label);
1461 label->set_theme_type_variation("HeaderSmall");
1462 label->set_position(Point2(10, 0));
1463 label->set_text(TTR("Scene Tree (Nodes):"));
1464
1465 add_child(label);
1466 }
1467
1468 tree = memnew(Tree);
1469 tree->set_anchor(SIDE_RIGHT, ANCHOR_END);
1470 tree->set_anchor(SIDE_BOTTOM, ANCHOR_END);
1471 tree->set_begin(Point2(0, p_label ? 18 : 0));
1472 tree->set_end(Point2(0, 0));
1473 tree->set_allow_reselect(true);
1474 tree->add_theme_constant_override("button_margin", 0);
1475
1476 add_child(tree);
1477
1478 SET_DRAG_FORWARDING_GCD(tree, SceneTreeEditor);
1479 if (p_can_rename) {
1480 tree->set_allow_rmb_select(true);
1481 tree->connect("item_mouse_selected", callable_mp(this, &SceneTreeEditor::_rmb_select));
1482 tree->connect("empty_clicked", callable_mp(this, &SceneTreeEditor::_empty_clicked));
1483 }
1484
1485 tree->connect("cell_selected", callable_mp(this, &SceneTreeEditor::_selected_changed));
1486 tree->connect("item_edited", callable_mp(this, &SceneTreeEditor::_renamed));
1487 tree->connect("multi_selected", callable_mp(this, &SceneTreeEditor::_cell_multi_selected));
1488 tree->connect("button_clicked", callable_mp(this, &SceneTreeEditor::_cell_button_pressed));
1489 tree->connect("nothing_selected", callable_mp(this, &SceneTreeEditor::_deselect_items));
1490
1491 error = memnew(AcceptDialog);
1492 add_child(error);
1493
1494 warning = memnew(AcceptDialog);
1495 add_child(warning);
1496 warning->set_title(TTR("Node Configuration Warning!"));
1497
1498 last_hash = 0;
1499 blocked = 0;
1500
1501 update_timer = memnew(Timer);
1502 update_timer->connect("timeout", callable_mp(this, &SceneTreeEditor::_update_tree).bind(false));
1503 update_timer->set_one_shot(true);
1504 update_timer->set_wait_time(0.5);
1505 add_child(update_timer);
1506
1507 script_types = memnew(List<StringName>);
1508 ClassDB::get_inheriters_from_class("Script", script_types);
1509}
1510
1511SceneTreeEditor::~SceneTreeEditor() {
1512 memdelete(script_types);
1513}
1514
1515/******** DIALOG *********/
1516
1517void SceneTreeDialog::popup_scenetree_dialog() {
1518 popup_centered_clamped(Size2(350, 700) * EDSCALE);
1519}
1520
1521void SceneTreeDialog::_show_all_nodes_changed(bool p_button_pressed) {
1522 EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", p_button_pressed);
1523 tree->set_show_all_nodes(p_button_pressed);
1524}
1525
1526void SceneTreeDialog::set_valid_types(const Vector<StringName> &p_valid) {
1527 if (p_valid.is_empty()) {
1528 return;
1529 }
1530
1531 tree->set_valid_types(p_valid);
1532
1533 HBoxContainer *hbox = memnew(HBoxContainer);
1534 content->add_child(hbox);
1535 content->move_child(hbox, 0);
1536
1537 {
1538 Label *label = memnew(Label);
1539 hbox->add_child(label);
1540 label->set_text(TTR("Allowed:"));
1541 }
1542
1543 HFlowContainer *hflow = memnew(HFlowContainer);
1544 hbox->add_child(hflow);
1545 hflow->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1546
1547 for (const StringName &type : p_valid) {
1548 HBoxContainer *hb = memnew(HBoxContainer);
1549 hflow->add_child(hb);
1550
1551 TextureRect *trect = memnew(TextureRect);
1552 hb->add_child(trect);
1553 trect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
1554 trect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
1555 trect->set_meta("type", type);
1556 valid_type_icons.push_back(trect);
1557
1558 Label *label = memnew(Label);
1559 hb->add_child(label);
1560 label->set_text(type);
1561 label->set_auto_translate(false);
1562 }
1563
1564 show_all_nodes->show();
1565}
1566
1567void SceneTreeDialog::_update_theme() {
1568 filter->set_right_icon(tree->get_editor_theme_icon(SNAME("Search")));
1569 for (TextureRect *trect : valid_type_icons) {
1570 trect->set_custom_minimum_size(Vector2(get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor)), 0));
1571 trect->set_texture(EditorNode::get_singleton()->get_class_icon(trect->get_meta("type")));
1572 }
1573}
1574
1575void SceneTreeDialog::_notification(int p_what) {
1576 switch (p_what) {
1577 case NOTIFICATION_VISIBILITY_CHANGED: {
1578 if (is_visible()) {
1579 tree->update_tree();
1580
1581 // Select the search bar by default.
1582 filter->call_deferred(SNAME("grab_focus"));
1583 }
1584 } break;
1585
1586 case NOTIFICATION_ENTER_TREE: {
1587 connect("confirmed", callable_mp(this, &SceneTreeDialog::_select));
1588 _update_theme();
1589 } break;
1590
1591 case NOTIFICATION_THEME_CHANGED: {
1592 _update_theme();
1593 } break;
1594
1595 case NOTIFICATION_EXIT_TREE: {
1596 disconnect("confirmed", callable_mp(this, &SceneTreeDialog::_select));
1597 } break;
1598 }
1599}
1600
1601void SceneTreeDialog::_cancel() {
1602 hide();
1603}
1604
1605void SceneTreeDialog::_select() {
1606 if (tree->get_selected()) {
1607 // The signal may cause another dialog to be displayed, so be sure to hide this one first.
1608 hide();
1609 emit_signal(SNAME("selected"), tree->get_selected()->get_path());
1610 }
1611}
1612
1613void SceneTreeDialog::_selected_changed() {
1614 get_ok_button()->set_disabled(!tree->get_selected());
1615}
1616
1617void SceneTreeDialog::_filter_changed(const String &p_filter) {
1618 tree->set_filter(p_filter);
1619}
1620
1621void SceneTreeDialog::_bind_methods() {
1622 ClassDB::bind_method("_cancel", &SceneTreeDialog::_cancel);
1623
1624 ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::NODE_PATH, "path")));
1625}
1626
1627SceneTreeDialog::SceneTreeDialog() {
1628 set_title(TTR("Select a Node"));
1629 content = memnew(VBoxContainer);
1630 add_child(content);
1631
1632 HBoxContainer *filter_hbc = memnew(HBoxContainer);
1633 content->add_child(filter_hbc);
1634
1635 filter = memnew(LineEdit);
1636 filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1637 filter->set_placeholder(TTR("Filter Nodes"));
1638 filter->set_clear_button_enabled(true);
1639 filter->add_theme_constant_override("minimum_character_width", 0);
1640 filter->connect("text_changed", callable_mp(this, &SceneTreeDialog::_filter_changed));
1641 filter_hbc->add_child(filter);
1642
1643 // Add 'Show All' button to HBoxContainer next to the filter, visible only when valid_types is defined.
1644 show_all_nodes = memnew(CheckButton);
1645 show_all_nodes->set_text(TTR("Show All"));
1646 show_all_nodes->connect("toggled", callable_mp(this, &SceneTreeDialog::_show_all_nodes_changed));
1647 show_all_nodes->set_h_size_flags(Control::SIZE_SHRINK_BEGIN);
1648 show_all_nodes->hide();
1649 filter_hbc->add_child(show_all_nodes);
1650
1651 tree = memnew(SceneTreeEditor(false, false, true));
1652 tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
1653 tree->get_scene_tree()->connect("item_activated", callable_mp(this, &SceneTreeDialog::_select));
1654 // Initialize button state, must be done after the tree has been created to update its 'show_all_nodes' flag.
1655 // This is also done before adding the tree to the content to avoid triggering unnecessary tree filtering.
1656 show_all_nodes->set_pressed(EditorSettings::get_singleton()->get_project_metadata("editor_metadata", "show_all_nodes_for_node_selection", false));
1657 content->add_child(tree);
1658
1659 // Disable the OK button when no node is selected.
1660 get_ok_button()->set_disabled(!tree->get_selected());
1661 tree->connect("node_selected", callable_mp(this, &SceneTreeDialog::_selected_changed));
1662}
1663
1664SceneTreeDialog::~SceneTreeDialog() {
1665}
1666