Debian Codegraph @

editor/debugger/editor_debugger_tree.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_debugger_tree.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 "editor_debugger_tree.h"
32
33 #include "editor/debugger/editor_debugger_node.h"
34 #include "editor/docks/scene_tree_dock.h"
35 #include "editor/editor_node.h"
36 #include "editor/editor_string_names.h"
37 #include "editor/gui/editor_file_dialog.h"
38 #include "editor/gui/editor_toaster.h"
39 #include "editor/settings/editor_settings.h"
40 #include "scene/debugger/scene_debugger.h"
41 #include "scene/gui/texture_rect.h"
42 #include "scene/resources/packed_scene.h"
43 #include "servers/display/display_server.h"
44
45 EditorDebuggerTree::EditorDebuggerTree() {
46 set_v_size_flags(SIZE_EXPAND_FILL);
47 set_allow_rmb_select(true);
48 set_select_mode(SELECT_MULTI);
49
50 // Popup
51 item_menu = memnew(PopupMenu);
52 item_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorDebuggerTree::_item_menu_id_pressed));
53 add_child(item_menu);
54
55 // File Dialog
56 file_dialog = memnew(EditorFileDialog);
57 file_dialog->connect("file_selected", callable_mp(this, &EditorDebuggerTree::_file_selected));
58 add_child(file_dialog);
59
60 accept = memnew(AcceptDialog);
61 add_child(accept);
62 }
63
64 void EditorDebuggerTree::_notification(int p_what) {
65 switch (p_what) {
66 case NOTIFICATION_POSTINITIALIZE: {
67 set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
68
69 connect("cell_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_selected));
70 connect("multi_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_selection_changed));
71 connect("nothing_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_nothing_selected));
72 connect("item_collapsed", callable_mp(this, &EditorDebuggerTree::_scene_tree_folded));
73 connect("item_mouse_selected", callable_mp(this, &EditorDebuggerTree::_scene_tree_rmb_selected));
74 } break;
75
76 case NOTIFICATION_READY: {
77 update_icon_max_width();
78 } break;
79 }
80 }
81
82 void EditorDebuggerTree::_bind_methods() {
83 ADD_SIGNAL(MethodInfo("objects_selected", PropertyInfo(Variant::ARRAY, "object_ids"), PropertyInfo(Variant::INT, "debugger")));
84 ADD_SIGNAL(MethodInfo("selection_cleared", PropertyInfo(Variant::INT, "debugger")));
85 ADD_SIGNAL(MethodInfo("save_node", PropertyInfo(Variant::INT, "object_id"), PropertyInfo(Variant::STRING, "filename"), PropertyInfo(Variant::INT, "debugger")));
86 ADD_SIGNAL(MethodInfo("open"));
87 }
88
89 void EditorDebuggerTree::_scene_tree_selected() {
90 TreeItem *item = get_selected();
91 if (!item) {
92 return;
93 }
94
95 if (!inspected_object_ids.is_empty()) {
96 inspected_object_ids.clear();
97 deselect_all();
98 item->select(0);
99 }
100
101 uint64_t id = uint64_t(item->get_metadata(0));
102 inspected_object_ids.append(id);
103
104 if (!notify_selection_queued) {
105 callable_mp(this, &EditorDebuggerTree::_notify_selection_changed).call_deferred();
106 notify_selection_queued = true;
107 }
108 }
109
110 void EditorDebuggerTree::_scene_tree_selection_changed(TreeItem *p_item, int p_column, bool p_selected) {
111 if (updating_scene_tree || !p_item) {
112 return;
113 }
114
115 uint64_t id = uint64_t(p_item->get_metadata(0));
116 if (p_selected) {
117 if (inspected_object_ids.size() == (int)EDITOR_GET("debugger/max_node_selection")) {
118 selection_surpassed_limit = true;
119 p_item->deselect(0);
120 } else if (!inspected_object_ids.has(id)) {
121 inspected_object_ids.append(id);
122 }
123 } else if (inspected_object_ids.has(id)) {
124 inspected_object_ids.erase(id);
125 }
126
127 if (!notify_selection_queued) {
128 callable_mp(this, &EditorDebuggerTree::_notify_selection_changed).call_deferred();
129 notify_selection_queued = true;
130 }
131 }
132
133 void EditorDebuggerTree::_scene_tree_nothing_selected() {
134 deselect_all();
135 inspected_object_ids.clear();
136 emit_signal(SNAME("selection_cleared"), debugger_id);
137 }
138
139 void EditorDebuggerTree::_notify_selection_changed() {
140 notify_selection_queued = false;
141
142 if (inspected_object_ids.is_empty()) {
143 emit_signal(SNAME("selection_cleared"), debugger_id);
144 } else {
145 emit_signal(SNAME("objects_selected"), inspected_object_ids.duplicate(), debugger_id);
146 }
147
148 if (selection_surpassed_limit) {
149 selection_surpassed_limit = false;
150 EditorToaster::get_singleton()->popup_str(vformat(TTR("Some remote nodes were not selected, as the configured maximum selection is %d. This can be changed at \"debugger/max_node_selection\" in the Editor Settings."), EDITOR_GET("debugger/max_node_selection")), EditorToaster::SEVERITY_WARNING);
151 }
152 }
153
154 void EditorDebuggerTree::_scene_tree_folded(Object *p_obj) {
155 if (updating_scene_tree) {
156 return;
157 }
158 TreeItem *item = Object::cast_to<TreeItem>(p_obj);
159
160 if (!item) {
161 return;
162 }
163
164 ObjectID id = ObjectID(uint64_t(item->get_metadata(0)));
165 if (unfold_cache.has(id)) {
166 unfold_cache.erase(id);
167 } else {
168 unfold_cache.insert(id);
169 }
170 }
171
172 void EditorDebuggerTree::_scene_tree_rmb_selected(const Vector2 &p_position, MouseButton p_button) {
173 if (p_button != MouseButton::RIGHT) {
174 return;
175 }
176
177 TreeItem *item = get_item_at_position(p_position);
178 if (!item) {
179 return;
180 }
181
182 item->select(0);
183
184 item_menu->clear();
185 item_menu->add_icon_item(get_editor_theme_icon(SNAME("CreateNewSceneFrom")), TTR("Save Branch as Scene..."), ITEM_MENU_SAVE_REMOTE_NODE);
186 item_menu->add_icon_item(get_editor_theme_icon(SNAME("CopyNodePath")), TTR("Copy Node Path"), ITEM_MENU_COPY_NODE_PATH);
187 item_menu->add_icon_item(get_editor_theme_icon(SNAME("Collapse")), TTR("Expand/Collapse Branch"), ITEM_MENU_EXPAND_COLLAPSE);
188 item_menu->set_position(get_screen_position() + get_local_mouse_position());
189 item_menu->reset_size();
190 item_menu->popup();
191 }
192
193 /// Populates inspect_scene_tree given data in nodes as a flat list, encoded depth first.
194 ///
195 /// Given a nodes array like [R,A,B,C,D,E] the following Tree will be generated, assuming
196 /// filter is an empty String, R and A child count are 2, B is 1 and C, D and E are 0.
197 ///
198 /// R
199 /// |-A
200 /// | |-B
201 /// | | |-C
202 /// | |
203 /// | |-D
204 /// |
205 /// |-E
206 ///
207 void EditorDebuggerTree::update_scene_tree(const SceneDebuggerTree *p_tree, int p_debugger) {
208 set_hide_root(false);
209
210 updating_scene_tree = true;
211 const String last_path = get_selected_path();
212 const String filter = SceneTreeDock::get_singleton()->get_filter();
213 LocalVector<TreeItem *> select_items;
214 bool hide_filtered_out_parents = EDITOR_GET("docks/scene_tree/hide_filtered_out_parents");
215
216 bool should_scroll = scrolling_to_item || filter != last_filter;
217 scrolling_to_item = false;
218 TreeItem *scroll_item = nullptr;
219 TypedArray<uint64_t> ids_present;
220
221 // Nodes are in a flatten list, depth first. Use a stack of parents, avoid recursion.
222 List<ParentItem> parents;
223 for (const SceneDebuggerTree::RemoteNode &node : p_tree->nodes) {
224 TreeItem *parent = nullptr;
225 Pair<TreeItem *, TreeItem *> move_from_to;
226 if (parents.size()) { // Find last parent.
227 ParentItem &p = parents.front()->get();
228 parent = p.tree_item;
229 if (!(--p.child_count)) { // If no child left, remove it.
230 parents.pop_front();
231
232 if (hide_filtered_out_parents && !filter.is_subsequence_ofn(parent->get_text(0))) {
233 if (parent == get_root()) {
234 set_hide_root(true);
235 } else {
236 move_from_to.first = parent;
237 // Find the closest ancestor that matches the filter.
238 for (const ParentItem p2 : parents) {
239 move_from_to.second = p2.tree_item;
240 if (p2.matches_filter || move_from_to.second == get_root()) {
241 break;
242 }
243 }
244
245 if (!move_from_to.second) {
246 move_from_to.second = get_root();
247 }
248 }
249 }
250 }
251 }
252
253 // Add this node.
254 TreeItem *item = create_item(parent);
255 item->set_text(0, node.name);
256 if (node.scene_file_path.is_empty()) {
257 item->set_tooltip_text(0, node.name + "\n" + TTR("Type:") + " " + node.type_name);
258 } else {
259 item->set_tooltip_text(0, node.name + "\n" + TTR("Instance:") + " " + node.scene_file_path + "\n" + TTR("Type:") + " " + node.type_name);
260 }
261 Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(node.type_name);
262 if (icon.is_valid()) {
263 item->set_icon(0, icon);
264 }
265 item->set_metadata(0, node.id);
266
267 String current_path;
268 if (parent) {
269 current_path += (String)parent->get_meta("node_path");
270
271 // Set current item as collapsed if necessary (root is never collapsed).
272 if (!unfold_cache.has(node.id)) {
273 item->set_collapsed(true);
274 }
275 }
276 item->set_meta("node_path", current_path + "/" + item->get_text(0));
277
278 // Select previously selected nodes.
279 if (debugger_id == p_debugger) { // Can use remote id.
280 if (inspected_object_ids.has(uint64_t(node.id))) {
281 ids_present.append(node.id);
282
283 if (selection_uncollapse_all) {
284 selection_uncollapse_all = false;
285
286 // Temporarily set to `false`, to allow caching the unfolds.
287 updating_scene_tree = false;
288 item->uncollapse_tree();
289 updating_scene_tree = true;
290 }
291
292 select_items.push_back(item);
293 if (should_scroll) {
294 scroll_item = item;
295 }
296 }
297 } else if (last_path == (String)item->get_meta("node_path")) { // Must use path.
298 updating_scene_tree = false; // Force emission of new selections.
299 select_items.push_back(item);
300 if (should_scroll) {
301 scroll_item = item;
302 }
303 updating_scene_tree = true;
304 }
305
306 // Add buttons.
307 const Color remote_button_color = Color(1, 1, 1, 0.8);
308 if (!node.scene_file_path.is_empty()) {
309 String node_scene_file_path = node.scene_file_path;
310 Ref<Texture2D> button_icon = get_editor_theme_icon(SNAME("InstanceOptions"));
311 String tooltip = vformat(TTR("This node has been instantiated from a PackedScene file:\n%s\nClick to open the original file in the Editor."), node_scene_file_path);
312
313 item->set_meta("scene_file_path", node_scene_file_path);
314 item->add_button(0, button_icon, BUTTON_SUBSCENE, false, tooltip);
315 item->set_button_color(0, item->get_button_count(0) - 1, remote_button_color);
316 }
317
318 if (node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_HAS_VISIBLE_METHOD) {
319 bool node_visible = node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_VISIBLE;
320 bool node_visible_in_tree = node.view_flags & SceneDebuggerTree::RemoteNode::VIEW_VISIBLE_IN_TREE;
321 Ref<Texture2D> button_icon = get_editor_theme_icon(node_visible ? SNAME("GuiVisibilityVisible") : SNAME("GuiVisibilityHidden"));
322 String tooltip = TTR("Toggle Visibility");
323
324 item->set_meta("visible", node_visible);
325 item->add_button(0, button_icon, BUTTON_VISIBILITY, false, tooltip);
326 if (ClassDB::is_parent_class(node.type_name, "CanvasItem") || ClassDB::is_parent_class(node.type_name, "Node3D")) {
327 item->set_button_color(0, item->get_button_count(0) - 1, node_visible_in_tree ? remote_button_color : Color(1, 1, 1, 0.6));
328 } else {
329 item->set_button_color(0, item->get_button_count(0) - 1, remote_button_color);
330 }
331 }
332
333 // Add in front of the parents stack if children are expected.
334 if (node.child_count) {
335 parents.push_front(ParentItem(item, node.child_count, filter.is_subsequence_ofn(item->get_text(0))));
336 } else {
337 // Apply filters.
338 while (parent) {
339 const bool had_siblings = item->get_prev() || item->get_next();
340 if (filter.is_subsequence_ofn(item->get_text(0))) {
341 break; // Filter matches, must survive.
342 }
343
344 if (select_items.has(item) || scroll_item == item) {
345 select_items.resize(select_items.size() - 1);
346 scroll_item = nullptr;
347 }
348 parent->remove_child(item);
349 memdelete(item);
350
351 if (had_siblings) {
352 break; // Parent must survive.
353 }
354
355 item = parent;
356 parent = item->get_parent();
357 // Check if parent expects more children.
358 for (ParentItem &pair : parents) {
359 if (pair.tree_item == item) {
360 parent = nullptr;
361 break; // Might have more children.
362 }
363 }
364 }
365 }
366
367 // Move all children to the ancestor that matches the filter, if picked.
368 if (move_from_to.first) {
369 TreeItem *from = move_from_to.first;
370 TypedArray<TreeItem> children = from->get_children();
371 if (!children.is_empty()) {
372 for (Variant &c : children) {
373 TreeItem *ti = Object::cast_to<TreeItem>(c);
374 from->remove_child(ti);
375 move_from_to.second->add_child(ti);
376 }
377
378 from->get_parent()->remove_child(from);
379 memdelete(from);
380 if (select_items.has(from) || scroll_item == from) {
381 select_items.erase(from);
382 scroll_item = nullptr;
383 }
384 }
385 }
386 }
387
388 inspected_object_ids = ids_present;
389
390 debugger_id = p_debugger; // Needed by hook, could be avoided if every debugger had its own tree.
391
392 for (TreeItem *item : select_items) {
393 item->select(0);
394 }
395 if (scroll_item) {
396 scroll_to_item(scroll_item, false);
397 }
398
399 if (new_session) {
400 // Some nodes may stay selected between sessions.
401 // Make sure the inspector shows them properly.
402 if (!notify_selection_queued) {
403 callable_mp(this, &EditorDebuggerTree::_notify_selection_changed).call_deferred();
404 notify_selection_queued = true;
405 }
406 new_session = false;
407 }
408
409 last_filter = filter;
410 updating_scene_tree = false;
411 }
412
413 void EditorDebuggerTree::select_nodes(const TypedArray<int64_t> &p_ids) {
414 // Manually select, as the tree control may be out-of-date for some reason (e.g. not shown yet).
415 selection_uncollapse_all = true;
416 inspected_object_ids = p_ids;
417 scrolling_to_item = true;
418
419 if (!updating_scene_tree) {
420 // Request a tree refresh.
421 EditorDebuggerNode::get_singleton()->request_remote_tree();
422 }
423 // Set the value immediately, so no update flooding happens and causes a crash.
424 updating_scene_tree = true;
425 }
426
427 void EditorDebuggerTree::clear_selection() {
428 inspected_object_ids.clear();
429
430 if (!updating_scene_tree) {
431 // Request a tree refresh.
432 EditorDebuggerNode::get_singleton()->request_remote_tree();
433 }
434 // Set the value immediately, so no update flooding happens and causes a crash.
435 updating_scene_tree = true;
436 }
437
438 Variant EditorDebuggerTree::get_drag_data(const Point2 &p_point) {
439 if (get_button_id_at_position(p_point) != -1) {
440 return Variant();
441 }
442
443 TreeItem *selected = get_selected();
444 if (!selected) {
445 return Variant();
446 }
447
448 String path = selected->get_text(0);
449 const int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
450
451 HBoxContainer *hb = memnew(HBoxContainer);
452 TextureRect *tf = memnew(TextureRect);
453 tf->set_texture(selected->get_icon(0));
454 tf->set_custom_minimum_size(Size2(icon_size, icon_size));
455 tf->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
456 tf->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
457 hb->add_child(tf);
458 Label *label = memnew(Label(path));
459 hb->add_child(label);
460 set_drag_preview(hb);
461
462 if (!selected->get_parent() || !selected->get_parent()->get_parent()) {
463 path = ".";
464 } else {
465 while (selected->get_parent()->get_parent() != get_root()) {
466 selected = selected->get_parent();
467 path = selected->get_text(0) + "/" + path;
468 }
469 }
470
471 return vformat("\"%s\"", path);
472 }
473
474 void EditorDebuggerTree::update_icon_max_width() {
475 add_theme_constant_override("icon_max_width", get_theme_constant("class_icon_size", EditorStringName(Editor)));
476 }
477
478 String EditorDebuggerTree::get_selected_path() {
479 if (!get_selected()) {
480 return "";
481 }
482 return get_selected()->get_meta("node_path");
483 }
484
485 void EditorDebuggerTree::_item_menu_id_pressed(int p_option) {
486 switch (p_option) {
487 case ITEM_MENU_SAVE_REMOTE_NODE: {
488 file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
489 file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
490
491 List<String> extensions;
492 Ref<PackedScene> sd = memnew(PackedScene);
493 ResourceSaver::get_recognized_extensions(sd, &extensions);
494 file_dialog->clear_filters();
495 for (const String &extension : extensions) {
496 file_dialog->add_filter("*." + extension, extension.to_upper());
497 }
498
499 String filename = get_selected_path().get_file() + "." + extensions.front()->get().to_lower();
500 file_dialog->set_current_path(filename);
501 file_dialog->popup_file_dialog();
502 } break;
503 case ITEM_MENU_COPY_NODE_PATH: {
504 String text = get_selected_path();
505 if (text.is_empty()) {
506 return;
507 } else if (text == "/root") {
508 text = ".";
509 } else {
510 text = text.replace("/root/", "");
511 int slash = text.find_char('/');
512 if (slash < 0) {
513 text = ".";
514 } else {
515 text = text.substr(slash + 1);
516 }
517 }
518 DisplayServer::get_singleton()->clipboard_set(text);
519 } break;
520 case ITEM_MENU_EXPAND_COLLAPSE: {
521 TreeItem *s_item = get_selected();
522
523 if (!s_item) {
524 s_item = get_root();
525 if (!s_item) {
526 break;
527 }
528 }
529
530 bool collapsed = s_item->is_any_collapsed();
531 s_item->set_collapsed_recursive(!collapsed);
532
533 ensure_cursor_is_visible();
534 }
535 }
536 }
537
538 void EditorDebuggerTree::_file_selected(const String &p_file) {
539 if (inspected_object_ids.size() != 1) {
540 accept->set_text(vformat(TTR("Saving the branch as a scene requires selecting only one node, but you have selected %d nodes."), inspected_object_ids.size()));
541 accept->popup_centered();
542 return;
543 }
544
545 emit_signal(SNAME("save_node"), inspected_object_ids[0], p_file, debugger_id);
546 }