Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/debugger/editor_debugger_inspector.cpp
20831 views
1
/**************************************************************************/
2
/* editor_debugger_inspector.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_inspector.h"
32
33
#include "core/debugger/debugger_marshalls.h"
34
#include "core/io/marshalls.h"
35
#include "core/io/resource_loader.h"
36
#include "editor/docks/inspector_dock.h"
37
#include "editor/editor_node.h"
38
#include "editor/editor_undo_redo_manager.h"
39
#include "scene/debugger/scene_debugger_object.h"
40
41
bool EditorDebuggerRemoteObjects::_set(const StringName &p_name, const Variant &p_value) {
42
return _set_impl(p_name, p_value, "");
43
}
44
45
bool EditorDebuggerRemoteObjects::_set_impl(const StringName &p_name, const Variant &p_value, const String &p_field) {
46
String name = p_name;
47
if (!prop_values.has(name) || String(name).begins_with("Constants/")) {
48
return false;
49
}
50
51
// Change it back to the real name when fetching.
52
if (name == "Script") {
53
name = "script";
54
} else if (name.begins_with("Metadata/")) {
55
name = name.replace_first("Metadata/", "metadata/");
56
}
57
58
Dictionary &values = prop_values[p_name];
59
Dictionary old_values = values.duplicate();
60
for (const uint64_t key : values.keys()) {
61
values.set(key, p_value);
62
}
63
64
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
65
const int size = remote_object_ids.size();
66
ur->create_action(size == 1 ? vformat(TTR("Set %s"), name) : vformat(TTR("Set %s on %d objects"), name, size), UndoRedo::MERGE_ENDS);
67
68
ur->add_do_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, values, p_field);
69
ur->add_undo_method(this, SNAME("emit_signal"), SNAME("values_edited"), name, old_values, p_field);
70
ur->commit_action();
71
72
return true;
73
}
74
75
bool EditorDebuggerRemoteObjects::_get(const StringName &p_name, Variant &r_ret) const {
76
String name = p_name;
77
if (!prop_values.has(name)) {
78
return false;
79
}
80
81
// Change it back to the real name when fetching.
82
if (name == "Script") {
83
name = "script";
84
} else if (name.begins_with("Metadata/")) {
85
name = name.replace_first("Metadata/", "metadata/");
86
}
87
88
r_ret = prop_values[p_name][remote_object_ids[0]];
89
return true;
90
}
91
92
void EditorDebuggerRemoteObjects::_get_property_list(List<PropertyInfo> *p_list) const {
93
p_list->clear(); // Sorry, don't want any categories.
94
for (const PropertyInfo &prop : prop_list) {
95
p_list->push_back(prop);
96
}
97
}
98
99
void EditorDebuggerRemoteObjects::set_property_field(const StringName &p_property, const Variant &p_value, const String &p_field) {
100
// Ignore the field with arrays and dictionaries, as they are passed whole when edited.
101
Variant::Type type = p_value.get_type();
102
if (type == Variant::ARRAY || type == Variant::DICTIONARY) {
103
_set_impl(p_property, p_value, "");
104
} else {
105
_set_impl(p_property, p_value, p_field);
106
}
107
}
108
109
String EditorDebuggerRemoteObjects::get_title() {
110
if (!remote_object_ids.is_empty() && ObjectID(remote_object_ids[0].operator uint64_t()).is_valid()) {
111
const int size = remote_object_ids.size();
112
return size == 1 ? vformat(TTR("Remote %s: %d"), type_name, remote_object_ids[0]) : vformat(TTR("Remote %s (%d Selected)"), type_name, size);
113
}
114
115
return "<null>";
116
}
117
118
Variant EditorDebuggerRemoteObjects::get_variant(const StringName &p_name) {
119
Variant var;
120
_get(p_name, var);
121
return var;
122
}
123
124
void EditorDebuggerRemoteObjects::_bind_methods() {
125
ClassDB::bind_method(D_METHOD("get_title"), &EditorDebuggerRemoteObjects::get_title);
126
ClassDB::bind_method("_hide_script_from_inspector", &EditorDebuggerRemoteObjects::_hide_script_from_inspector);
127
ClassDB::bind_method("_hide_metadata_from_inspector", &EditorDebuggerRemoteObjects::_hide_metadata_from_inspector);
128
129
ADD_SIGNAL(MethodInfo("values_edited", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::DICTIONARY, "values", PROPERTY_HINT_DICTIONARY_TYPE, "uint64_t;Variant"), PropertyInfo(Variant::STRING, "field")));
130
}
131
132
/// EditorDebuggerInspector
133
134
EditorDebuggerInspector::EditorDebuggerInspector() {
135
variables = memnew(EditorDebuggerRemoteObjects);
136
}
137
138
EditorDebuggerInspector::~EditorDebuggerInspector() {
139
clear_cache();
140
memdelete(variables);
141
}
142
143
void EditorDebuggerInspector::_bind_methods() {
144
ADD_SIGNAL(MethodInfo("object_selected", PropertyInfo(Variant::INT, "id")));
145
ADD_SIGNAL(MethodInfo("objects_edited", PropertyInfo(Variant::ARRAY, "ids"), PropertyInfo(Variant::STRING, "property"), PropertyInfo("value"), PropertyInfo(Variant::STRING, "field")));
146
ADD_SIGNAL(MethodInfo("object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
147
}
148
149
void EditorDebuggerInspector::_notification(int p_what) {
150
switch (p_what) {
151
case NOTIFICATION_POSTINITIALIZE: {
152
connect("object_id_selected", callable_mp(this, &EditorDebuggerInspector::_object_selected));
153
} break;
154
155
case NOTIFICATION_ENTER_TREE: {
156
variables->remote_object_ids.append(0);
157
edit(variables);
158
} break;
159
}
160
}
161
162
void EditorDebuggerInspector::_objects_edited(const String &p_prop, const TypedDictionary<uint64_t, Variant> &p_values, const String &p_field) {
163
emit_signal(SNAME("objects_edited"), p_prop, p_values, p_field);
164
}
165
166
void EditorDebuggerInspector::_object_selected(ObjectID p_object) {
167
emit_signal(SNAME("object_selected"), p_object);
168
}
169
170
EditorDebuggerRemoteObjects *EditorDebuggerInspector::set_objects(const Array &p_arr) {
171
ERR_FAIL_COND_V(p_arr.is_empty(), nullptr);
172
173
TypedArray<uint64_t> ids;
174
LocalVector<SceneDebuggerObject> objects;
175
for (const Array arr : p_arr) {
176
SceneDebuggerObject obj;
177
obj.deserialize(arr);
178
if (obj.id.is_valid()) {
179
ids.push_back((uint64_t)obj.id);
180
objects.push_back(obj);
181
}
182
}
183
ERR_FAIL_COND_V(ids.is_empty(), nullptr);
184
185
// Sorting is necessary, as selected nodes in the remote tree are ordered by index.
186
ids.sort();
187
188
EditorDebuggerRemoteObjects *remote_objects = nullptr;
189
for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
190
if (robjs->remote_object_ids == ids) {
191
remote_objects = robjs;
192
break;
193
}
194
}
195
196
if (!remote_objects) {
197
remote_objects = memnew(EditorDebuggerRemoteObjects);
198
remote_objects->remote_object_ids = ids;
199
remote_objects->remote_object_ids.make_read_only();
200
remote_objects->connect("values_edited", callable_mp(this, &EditorDebuggerInspector::_objects_edited));
201
remote_objects_list.push_back(remote_objects);
202
}
203
204
StringName class_name = objects[0].class_name;
205
if (class_name != SNAME("Object")) {
206
// Search for the common class between all selected objects.
207
bool check_type_again = true;
208
while (check_type_again) {
209
check_type_again = false;
210
211
if (class_name == SNAME("Object") || class_name == StringName()) {
212
// All objects inherit from Object, so no need to continue checking.
213
class_name = SNAME("Object");
214
break;
215
}
216
217
// Check that all objects inherit from type_name.
218
for (const SceneDebuggerObject &obj : objects) {
219
if (obj.class_name == class_name || ClassDB::is_parent_class(obj.class_name, class_name)) {
220
continue; // class_name is the same or a parent of the object's class.
221
}
222
223
// class_name is not a parent of the node's class, so check again with the parent class.
224
class_name = ClassDB::get_parent_class(class_name);
225
check_type_again = true;
226
break;
227
}
228
}
229
}
230
remote_objects->type_name = class_name;
231
232
// Search for properties that are present in all selected objects.
233
struct UsageData {
234
int qty = 0;
235
SceneDebuggerObject::SceneDebuggerProperty prop;
236
TypedDictionary<uint64_t, Variant> values;
237
};
238
HashMap<String, UsageData> usage;
239
int nc = 0;
240
for (const SceneDebuggerObject &obj : objects) {
241
for (const SceneDebuggerObject::SceneDebuggerProperty &prop : obj.properties) {
242
PropertyInfo pinfo = prop.first;
243
// Rename those variables, so they don't conflict with the ones from the resource itself.
244
if (pinfo.name == "script") {
245
pinfo.name = "Script";
246
} else if (pinfo.name.begins_with("metadata/")) {
247
pinfo.name = pinfo.name.replace_first("metadata/", "Metadata/");
248
}
249
250
if (!usage.has(pinfo.name)) {
251
UsageData usage_dt;
252
usage_dt.prop = prop;
253
usage_dt.prop.first.name = pinfo.name;
254
usage_dt.values[obj.id] = prop.second;
255
usage[pinfo.name] = usage_dt;
256
}
257
258
// Make sure only properties with the same exact PropertyInfo data will appear.
259
if (usage[pinfo.name].prop.first == pinfo) {
260
usage[pinfo.name].qty++;
261
usage[pinfo.name].values[obj.id] = prop.second;
262
}
263
}
264
265
nc++;
266
}
267
for (HashMap<String, UsageData>::Iterator E = usage.begin(); E;) {
268
HashMap<String, UsageData>::Iterator next = E;
269
++next;
270
271
UsageData usage_dt = E->value;
272
if (nc != usage_dt.qty) {
273
// Doesn't appear on all of them, remove it.
274
usage.erase(E->key);
275
}
276
277
E = next;
278
}
279
280
int old_prop_size = remote_objects->prop_list.size();
281
282
remote_objects->prop_list.clear();
283
int new_props_added = 0;
284
HashSet<String> changed;
285
for (KeyValue<String, UsageData> &KV : usage) {
286
const PropertyInfo &pinfo = KV.value.prop.first;
287
Variant var = KV.value.values[remote_objects->remote_object_ids[0]];
288
289
if (pinfo.type == Variant::OBJECT && var.is_string()) {
290
String path = var;
291
if (path.contains("::")) {
292
// Built-in resource.
293
String base_path = path.get_slice("::", 0);
294
Ref<Resource> dependency = ResourceLoader::load(base_path);
295
if (dependency.is_valid()) {
296
remote_dependencies.insert(dependency);
297
}
298
}
299
var = ResourceLoader::load(path);
300
KV.value.values[remote_objects->remote_object_ids[0]] = var;
301
}
302
303
// Always add the property, since props may have been added or removed.
304
remote_objects->prop_list.push_back(pinfo);
305
306
if (!remote_objects->prop_values.has(pinfo.name)) {
307
new_props_added++;
308
} else if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, remote_objects->prop_values[pinfo.name], var))) {
309
changed.insert(pinfo.name);
310
}
311
312
remote_objects->prop_values[pinfo.name] = KV.value.values;
313
}
314
315
if (old_prop_size == remote_objects->prop_list.size() && new_props_added == 0) {
316
// Only some may have changed, if so, then update those, if they exist.
317
for (const String &E : changed) {
318
emit_signal(SNAME("object_property_updated"), remote_objects->get_instance_id(), E);
319
}
320
} else {
321
// Full update, because props were added or removed.
322
remote_objects->update();
323
}
324
325
return remote_objects;
326
}
327
328
void EditorDebuggerInspector::clear_remote_inspector() {
329
if (remote_objects_list.is_empty()) {
330
return;
331
}
332
333
const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
334
// Check if the inspector holds remote items, and take it out if so.
335
if (Object::cast_to<EditorDebuggerRemoteObjects>(obj)) {
336
EditorNode::get_singleton()->push_item(nullptr);
337
}
338
}
339
340
void EditorDebuggerInspector::clear_cache() {
341
clear_remote_inspector();
342
343
for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
344
memdelete(robjs);
345
}
346
remote_objects_list.clear();
347
348
remote_dependencies.clear();
349
}
350
351
void EditorDebuggerInspector::invalidate_selection_from_cache(const TypedArray<uint64_t> &p_ids) {
352
for (EditorDebuggerRemoteObjects *robjs : remote_objects_list) {
353
if (robjs->remote_object_ids == p_ids) {
354
const Object *obj = InspectorDock::get_inspector_singleton()->get_edited_object();
355
if (obj == robjs) {
356
EditorNode::get_singleton()->push_item(nullptr);
357
}
358
359
remote_objects_list.erase(robjs);
360
memdelete(robjs);
361
break;
362
}
363
}
364
}
365
366
void EditorDebuggerInspector::add_stack_variable(const Array &p_array, int p_offset) {
367
DebuggerMarshalls::ScriptStackVariable var;
368
var.deserialize(p_array);
369
String n = var.name;
370
Variant v = var.value;
371
372
PropertyHint h = PROPERTY_HINT_NONE;
373
String hs;
374
375
if (var.var_type == Variant::OBJECT && v) {
376
v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
377
h = PROPERTY_HINT_OBJECT_ID;
378
hs = "Object";
379
}
380
String type;
381
switch (var.type) {
382
case 0:
383
type = "Locals/";
384
break;
385
case 1:
386
type = "Members/";
387
break;
388
case 2:
389
type = "Globals/";
390
break;
391
case 3:
392
type = "Evaluated/";
393
break;
394
default:
395
type = "Unknown/";
396
}
397
398
PropertyInfo pinfo;
399
// Encode special characters to avoid issues with expressions in Evaluator.
400
// Dots are skipped by uri_encode(), but uri_decode() process them correctly when replaced with "%2E".
401
pinfo.name = type + n.uri_encode().replace(".", "%2E");
402
pinfo.type = v.get_type();
403
pinfo.hint = h;
404
pinfo.hint_string = hs;
405
406
if ((p_offset == -1) || variables->prop_list.is_empty()) {
407
variables->prop_list.push_back(pinfo);
408
} else {
409
List<PropertyInfo>::Element *current = variables->prop_list.front();
410
for (int i = 0; i < p_offset; i++) {
411
current = current->next();
412
}
413
variables->prop_list.insert_before(current, pinfo);
414
}
415
variables->prop_values[pinfo.name][0] = v;
416
variables->update();
417
edit(variables);
418
}
419
420
void EditorDebuggerInspector::clear_stack_variables() {
421
variables->clear();
422
variables->update();
423
}
424
425
String EditorDebuggerInspector::get_stack_variable(const String &p_var) {
426
for (KeyValue<StringName, TypedDictionary<uint64_t, Variant>> &E : variables->prop_values) {
427
String v = E.key.operator String();
428
if (v.get_slicec('/', 1) == p_var) {
429
return variables->get_variant(v);
430
}
431
}
432
return String();
433
}
434
435