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