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