Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/editor_data.cpp
9821 views
1
/**************************************************************************/
2
/* editor_data.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_data.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/extension/gdextension_manager.h"
35
#include "core/io/file_access.h"
36
#include "core/io/resource_loader.h"
37
#include "editor/editor_node.h"
38
#include "editor/editor_undo_redo_manager.h"
39
#include "editor/inspector/editor_context_menu_plugin.h"
40
#include "editor/inspector/multi_node_edit.h"
41
#include "editor/plugins/editor_plugin.h"
42
#include "scene/property_utils.h"
43
#include "scene/resources/packed_scene.h"
44
45
void EditorSelectionHistory::cleanup_history() {
46
for (int i = 0; i < history.size(); i++) {
47
bool fail = false;
48
49
for (int j = 0; j < history[i].path.size(); j++) {
50
if (history[i].path[j].ref.is_valid()) {
51
// If the node is a MultiNodeEdit node, examine it and see if anything is missing from it.
52
Ref<MultiNodeEdit> multi_node_edit = history[i].path[j].ref;
53
if (multi_node_edit.is_valid()) {
54
Node *root = EditorNode::get_singleton()->get_edited_scene();
55
if (root) {
56
for (int k = 0; k < multi_node_edit->get_node_count(); k++) {
57
NodePath np = multi_node_edit->get_node(k);
58
Node *multi_node_selected_node = root->get_node_or_null(np);
59
if (!multi_node_selected_node) {
60
fail = true;
61
break;
62
}
63
}
64
} else {
65
fail = true;
66
}
67
} else {
68
// Reference is not null - object still alive.
69
continue;
70
}
71
}
72
73
if (!fail) {
74
Object *obj = ObjectDB::get_instance(history[i].path[j].object);
75
if (obj) {
76
Node *n = Object::cast_to<Node>(obj);
77
if (n && n->is_inside_tree()) {
78
// Node valid and inside tree - object still alive.
79
continue;
80
}
81
if (!n) {
82
// Node possibly still alive.
83
continue;
84
}
85
} // Else: object not valid - not alive.
86
87
fail = true;
88
}
89
90
if (fail) {
91
break;
92
}
93
}
94
95
if (fail) {
96
history.remove_at(i);
97
i--;
98
}
99
}
100
101
if (current_elem_idx >= history.size()) {
102
current_elem_idx = history.size() - 1;
103
}
104
}
105
106
void EditorSelectionHistory::add_object(ObjectID p_object, const String &p_property, bool p_inspector_only) {
107
Object *obj = ObjectDB::get_instance(p_object);
108
ERR_FAIL_NULL(obj);
109
RefCounted *r = Object::cast_to<RefCounted>(obj);
110
_Object o;
111
if (r) {
112
o.ref = Ref<RefCounted>(r);
113
}
114
o.object = p_object;
115
o.property = p_property;
116
o.inspector_only = p_inspector_only;
117
118
bool has_prev = current_elem_idx >= 0 && current_elem_idx < history.size();
119
120
if (has_prev) {
121
history.resize(current_elem_idx + 1); // Clip history to next.
122
}
123
124
HistoryElement h;
125
if (!p_property.is_empty() && has_prev) {
126
// Add a sub property.
127
HistoryElement &prev_element = history.write[current_elem_idx];
128
h = prev_element;
129
h.path.resize(h.level + 1);
130
h.path.push_back(o);
131
h.level++;
132
133
} else {
134
// Create a new history item.
135
h.path.push_back(o);
136
h.level = 0;
137
}
138
139
history.push_back(h);
140
current_elem_idx++;
141
}
142
143
void EditorSelectionHistory::replace_object(ObjectID p_old_object, ObjectID p_new_object) {
144
for (HistoryElement &element : history) {
145
for (int index = 0; index < element.path.size(); index++) {
146
if (element.path[index].object == p_old_object) {
147
element.path.write[index].object = p_new_object;
148
}
149
}
150
}
151
}
152
153
int EditorSelectionHistory::get_history_len() {
154
return history.size();
155
}
156
157
int EditorSelectionHistory::get_history_pos() {
158
return current_elem_idx;
159
}
160
161
ObjectID EditorSelectionHistory::get_history_obj(int p_obj) const {
162
ERR_FAIL_INDEX_V(p_obj, history.size(), ObjectID());
163
ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), ObjectID());
164
return history[p_obj].path[history[p_obj].level].object;
165
}
166
167
bool EditorSelectionHistory::is_at_beginning() const {
168
return current_elem_idx <= 0;
169
}
170
171
bool EditorSelectionHistory::is_at_end() const {
172
return ((current_elem_idx + 1) >= history.size());
173
}
174
175
bool EditorSelectionHistory::next() {
176
cleanup_history();
177
178
if ((current_elem_idx + 1) < history.size()) {
179
current_elem_idx++;
180
} else {
181
return false;
182
}
183
184
return true;
185
}
186
187
bool EditorSelectionHistory::previous() {
188
cleanup_history();
189
190
if (current_elem_idx > 0) {
191
current_elem_idx--;
192
} else {
193
return false;
194
}
195
196
return true;
197
}
198
199
bool EditorSelectionHistory::is_current_inspector_only() const {
200
if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
201
return false;
202
}
203
204
const HistoryElement &h = history[current_elem_idx];
205
return h.path[h.level].inspector_only;
206
}
207
208
ObjectID EditorSelectionHistory::get_current() {
209
if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
210
return ObjectID();
211
}
212
213
Object *obj = ObjectDB::get_instance(get_history_obj(current_elem_idx));
214
return obj ? obj->get_instance_id() : ObjectID();
215
}
216
217
int EditorSelectionHistory::get_path_size() const {
218
if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
219
return 0;
220
}
221
222
return history[current_elem_idx].path.size();
223
}
224
225
ObjectID EditorSelectionHistory::get_path_object(int p_index) const {
226
if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
227
return ObjectID();
228
}
229
230
ERR_FAIL_INDEX_V(p_index, history[current_elem_idx].path.size(), ObjectID());
231
232
Object *obj = ObjectDB::get_instance(history[current_elem_idx].path[p_index].object);
233
return obj ? obj->get_instance_id() : ObjectID();
234
}
235
236
String EditorSelectionHistory::get_path_property(int p_index) const {
237
if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
238
return "";
239
}
240
241
ERR_FAIL_INDEX_V(p_index, history[current_elem_idx].path.size(), "");
242
return history[current_elem_idx].path[p_index].property;
243
}
244
245
void EditorSelectionHistory::clear() {
246
history.clear();
247
current_elem_idx = -1;
248
}
249
250
EditorSelectionHistory::EditorSelectionHistory() {
251
current_elem_idx = -1;
252
}
253
254
////////////////////////////////////////////////////////////
255
256
EditorPlugin *EditorData::get_handling_main_editor(Object *p_object) {
257
// We need to iterate backwards so that we can check user-created plugins first.
258
// Otherwise, it would not be possible for plugins to handle CanvasItem and Spatial nodes.
259
for (int i = editor_plugins.size() - 1; i > -1; i--) {
260
if (editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
261
return editor_plugins[i];
262
}
263
}
264
265
return nullptr;
266
}
267
268
Vector<EditorPlugin *> EditorData::get_handling_sub_editors(Object *p_object) {
269
Vector<EditorPlugin *> sub_plugins;
270
for (int i = editor_plugins.size() - 1; i > -1; i--) {
271
if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) {
272
sub_plugins.push_back(editor_plugins[i]);
273
}
274
}
275
return sub_plugins;
276
}
277
278
EditorPlugin *EditorData::get_editor_by_name(const String &p_name) {
279
for (int i = editor_plugins.size() - 1; i > -1; i--) {
280
if (editor_plugins[i]->get_plugin_name() == p_name) {
281
return editor_plugins[i];
282
}
283
}
284
285
return nullptr;
286
}
287
288
void EditorData::copy_object_params(Object *p_object) {
289
clipboard.clear();
290
291
List<PropertyInfo> pinfo;
292
p_object->get_property_list(&pinfo);
293
294
for (const PropertyInfo &E : pinfo) {
295
if (!(E.usage & PROPERTY_USAGE_EDITOR) || E.name == "script" || E.name == "scripts" || E.name == "resource_path") {
296
continue;
297
}
298
299
PropertyData pd;
300
pd.name = E.name;
301
pd.value = p_object->get(pd.name);
302
clipboard.push_back(pd);
303
}
304
}
305
306
void EditorData::get_editor_breakpoints(List<String> *p_breakpoints) {
307
for (int i = 0; i < editor_plugins.size(); i++) {
308
editor_plugins[i]->get_breakpoints(p_breakpoints);
309
}
310
}
311
312
Dictionary EditorData::get_editor_plugin_states() const {
313
Dictionary metadata;
314
for (int i = 0; i < editor_plugins.size(); i++) {
315
Dictionary state = editor_plugins[i]->get_state();
316
if (state.is_empty()) {
317
continue;
318
}
319
metadata[editor_plugins[i]->get_plugin_name()] = state;
320
}
321
322
return metadata;
323
}
324
325
Dictionary EditorData::get_scene_editor_states(int p_idx) const {
326
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Dictionary());
327
EditedScene es = edited_scene[p_idx];
328
return es.editor_states;
329
}
330
331
void EditorData::set_editor_plugin_states(const Dictionary &p_states) {
332
if (p_states.is_empty()) {
333
for (EditorPlugin *ep : editor_plugins) {
334
ep->clear();
335
}
336
return;
337
}
338
339
for (const KeyValue<Variant, Variant> &kv : p_states) {
340
String name = kv.key;
341
int idx = -1;
342
for (int i = 0; i < editor_plugins.size(); i++) {
343
if (editor_plugins[i]->get_plugin_name() == name) {
344
idx = i;
345
break;
346
}
347
}
348
349
if (idx == -1) {
350
continue;
351
}
352
editor_plugins[idx]->set_state(kv.value);
353
}
354
}
355
356
void EditorData::notify_edited_scene_changed() {
357
for (int i = 0; i < editor_plugins.size(); i++) {
358
editor_plugins[i]->edited_scene_changed();
359
editor_plugins[i]->notify_scene_changed(get_edited_scene_root());
360
}
361
}
362
363
void EditorData::notify_resource_saved(const Ref<Resource> &p_resource) {
364
for (int i = 0; i < editor_plugins.size(); i++) {
365
editor_plugins[i]->notify_resource_saved(p_resource);
366
}
367
}
368
369
void EditorData::notify_scene_saved(const String &p_path) {
370
for (int i = 0; i < editor_plugins.size(); i++) {
371
editor_plugins[i]->notify_scene_saved(p_path);
372
}
373
}
374
375
void EditorData::clear_editor_states() {
376
for (int i = 0; i < editor_plugins.size(); i++) {
377
editor_plugins[i]->clear();
378
}
379
}
380
381
void EditorData::save_editor_external_data() {
382
for (int i = 0; i < editor_plugins.size(); i++) {
383
editor_plugins[i]->save_external_data();
384
}
385
}
386
387
void EditorData::apply_changes_in_editors() {
388
for (int i = 0; i < editor_plugins.size(); i++) {
389
editor_plugins[i]->apply_changes();
390
}
391
}
392
393
void EditorData::paste_object_params(Object *p_object) {
394
ERR_FAIL_NULL(p_object);
395
undo_redo_manager->create_action(TTR("Paste Params"));
396
for (const PropertyData &E : clipboard) {
397
String name = E.name;
398
undo_redo_manager->add_do_property(p_object, name, E.value);
399
undo_redo_manager->add_undo_property(p_object, name, p_object->get(name));
400
}
401
undo_redo_manager->commit_action();
402
}
403
404
bool EditorData::call_build() {
405
bool result = true;
406
407
for (int i = 0; i < editor_plugins.size() && result; i++) {
408
result &= editor_plugins[i]->build();
409
}
410
411
return result;
412
}
413
414
void EditorData::set_scene_as_saved(int p_idx) {
415
if (p_idx == -1) {
416
p_idx = current_edited_scene;
417
}
418
ERR_FAIL_INDEX(p_idx, edited_scene.size());
419
420
undo_redo_manager->set_history_as_saved(edited_scene[p_idx].history_id);
421
}
422
423
bool EditorData::is_scene_changed(int p_idx) {
424
if (p_idx == -1) {
425
p_idx = current_edited_scene;
426
}
427
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
428
429
uint64_t current_scene_version = undo_redo_manager->get_or_create_history(edited_scene[p_idx].history_id).undo_redo->get_version();
430
bool is_changed = edited_scene[p_idx].last_checked_version != current_scene_version;
431
edited_scene.write[p_idx].last_checked_version = current_scene_version;
432
return is_changed;
433
}
434
435
int EditorData::get_scene_history_id_from_path(const String &p_path) const {
436
for (const EditedScene &E : edited_scene) {
437
if (E.path == p_path) {
438
return E.history_id;
439
}
440
}
441
return 0;
442
}
443
444
int EditorData::get_current_edited_scene_history_id() const {
445
if (current_edited_scene != -1) {
446
return edited_scene[current_edited_scene].history_id;
447
}
448
return 0;
449
}
450
451
int EditorData::get_scene_history_id(int p_idx) const {
452
return edited_scene[p_idx].history_id;
453
}
454
455
void EditorData::add_undo_redo_inspector_hook_callback(Callable p_callable) {
456
undo_redo_callbacks.push_back(p_callable);
457
}
458
459
void EditorData::remove_undo_redo_inspector_hook_callback(Callable p_callable) {
460
undo_redo_callbacks.erase(p_callable);
461
}
462
463
const Vector<Callable> EditorData::get_undo_redo_inspector_hook_callback() {
464
return undo_redo_callbacks;
465
}
466
467
void EditorData::add_move_array_element_function(const StringName &p_class, Callable p_callable) {
468
move_element_functions.insert(p_class, p_callable);
469
}
470
471
void EditorData::remove_move_array_element_function(const StringName &p_class) {
472
move_element_functions.erase(p_class);
473
}
474
475
Callable EditorData::get_move_array_element_function(const StringName &p_class) const {
476
if (move_element_functions.has(p_class)) {
477
return move_element_functions[p_class];
478
}
479
return Callable();
480
}
481
482
void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) {
483
editor_plugins.erase(p_plugin);
484
}
485
486
void EditorData::add_editor_plugin(EditorPlugin *p_plugin) {
487
editor_plugins.push_back(p_plugin);
488
}
489
490
int EditorData::get_editor_plugin_count() const {
491
return editor_plugins.size();
492
}
493
494
EditorPlugin *EditorData::get_editor_plugin(int p_idx) {
495
ERR_FAIL_INDEX_V(p_idx, editor_plugins.size(), nullptr);
496
return editor_plugins[p_idx];
497
}
498
499
void EditorData::add_extension_editor_plugin(const StringName &p_class_name, EditorPlugin *p_plugin) {
500
ERR_FAIL_COND(extension_editor_plugins.has(p_class_name));
501
extension_editor_plugins.insert(p_class_name, p_plugin);
502
}
503
504
void EditorData::remove_extension_editor_plugin(const StringName &p_class_name) {
505
extension_editor_plugins.erase(p_class_name);
506
}
507
508
bool EditorData::has_extension_editor_plugin(const StringName &p_class_name) {
509
return extension_editor_plugins.has(p_class_name);
510
}
511
512
EditorPlugin *EditorData::get_extension_editor_plugin(const StringName &p_class_name) {
513
EditorPlugin **plugin = extension_editor_plugins.getptr(p_class_name);
514
return plugin == nullptr ? nullptr : *plugin;
515
}
516
517
void EditorData::add_custom_type(const String &p_type, const String &p_inherits, const Ref<Script> &p_script, const Ref<Texture2D> &p_icon) {
518
ERR_FAIL_COND_MSG(p_script.is_null(), "It's not a reference to a valid Script object.");
519
CustomType ct;
520
ct.name = p_type;
521
ct.icon = p_icon;
522
ct.script = p_script;
523
524
if (!custom_types.has(p_inherits)) {
525
custom_types[p_inherits] = Vector<CustomType>();
526
}
527
custom_types[p_inherits].push_back(ct);
528
}
529
530
Variant EditorData::instantiate_custom_type(const String &p_type, const String &p_inherits) {
531
if (get_custom_types().has(p_inherits)) {
532
for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) {
533
if (get_custom_types()[p_inherits][i].name == p_type) {
534
Ref<Script> script = get_custom_types()[p_inherits][i].script;
535
536
// Store in a variant to initialize the refcount if needed.
537
Variant v = ClassDB::instantiate(p_inherits);
538
ERR_FAIL_COND_V(!v, Variant());
539
Object *ob = v;
540
541
Node *n = Object::cast_to<Node>(ob);
542
if (n) {
543
n->set_name(p_type);
544
}
545
PropertyUtils::assign_custom_type_script(ob, script);
546
ob->set_script(script);
547
return v;
548
}
549
}
550
}
551
552
return Variant();
553
}
554
555
const EditorData::CustomType *EditorData::get_custom_type_by_name(const String &p_type) const {
556
for (const KeyValue<String, Vector<CustomType>> &E : custom_types) {
557
for (const CustomType &F : E.value) {
558
if (F.name == p_type) {
559
return &F;
560
}
561
}
562
}
563
return nullptr;
564
}
565
566
const EditorData::CustomType *EditorData::get_custom_type_by_path(const String &p_path) const {
567
for (const KeyValue<String, Vector<CustomType>> &E : custom_types) {
568
for (const CustomType &F : E.value) {
569
if (F.script->get_path() == p_path) {
570
return &F;
571
}
572
}
573
}
574
return nullptr;
575
}
576
577
bool EditorData::is_type_recognized(const String &p_type) const {
578
return ClassDB::class_exists(p_type) || ScriptServer::is_global_class(p_type) || get_custom_type_by_name(p_type);
579
}
580
581
void EditorData::remove_custom_type(const String &p_type) {
582
for (KeyValue<String, Vector<CustomType>> &E : custom_types) {
583
for (int i = 0; i < E.value.size(); i++) {
584
if (E.value[i].name == p_type) {
585
E.value.remove_at(i);
586
if (E.value.is_empty()) {
587
custom_types.erase(E.key);
588
}
589
return;
590
}
591
}
592
}
593
}
594
595
void EditorData::instantiate_object_properties(Object *p_object) {
596
ERR_FAIL_NULL(p_object);
597
// Check if any Object-type property should be instantiated.
598
List<PropertyInfo> pinfo;
599
p_object->get_property_list(&pinfo);
600
601
for (const PropertyInfo &pi : pinfo) {
602
if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
603
Object *prop = ClassDB::instantiate(pi.class_name);
604
p_object->set(pi.name, prop);
605
}
606
}
607
}
608
609
int EditorData::add_edited_scene(int p_at_pos) {
610
if (p_at_pos < 0) {
611
p_at_pos = edited_scene.size();
612
}
613
EditedScene es;
614
es.root = nullptr;
615
es.path = String();
616
es.file_modified_time = 0;
617
es.history_current = -1;
618
es.live_edit_root = NodePath(String("/root"));
619
es.history_id = last_created_scene++;
620
621
if (p_at_pos == edited_scene.size()) {
622
edited_scene.push_back(es);
623
} else {
624
edited_scene.insert(p_at_pos, es);
625
}
626
627
if (current_edited_scene < 0) {
628
current_edited_scene = 0;
629
}
630
return p_at_pos;
631
}
632
633
void EditorData::move_edited_scene_index(int p_idx, int p_to_idx) {
634
ERR_FAIL_INDEX(p_idx, edited_scene.size());
635
ERR_FAIL_INDEX(p_to_idx, edited_scene.size());
636
SWAP(edited_scene.write[p_idx], edited_scene.write[p_to_idx]);
637
}
638
639
void EditorData::remove_scene(int p_idx) {
640
ERR_FAIL_INDEX(p_idx, edited_scene.size());
641
if (edited_scene[p_idx].root) {
642
for (int i = 0; i < editor_plugins.size(); i++) {
643
editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_scene_file_path());
644
}
645
646
memdelete(edited_scene[p_idx].root);
647
edited_scene.write[p_idx].root = nullptr;
648
}
649
650
if (current_edited_scene > p_idx) {
651
current_edited_scene--;
652
} else if (current_edited_scene == p_idx && current_edited_scene > 0) {
653
current_edited_scene--;
654
}
655
656
if (!edited_scene[p_idx].path.is_empty()) {
657
EditorNode::get_singleton()->emit_signal("scene_closed", edited_scene[p_idx].path);
658
}
659
660
if (undo_redo_manager->has_history(edited_scene[p_idx].history_id)) { // Might not exist if scene failed to load.
661
undo_redo_manager->discard_history(edited_scene[p_idx].history_id);
662
}
663
edited_scene.remove_at(p_idx);
664
}
665
666
bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths) {
667
Ref<SceneState> ss;
668
669
if (p_node == p_root) {
670
ss = p_node->get_scene_inherited_state();
671
} else if (!p_node->get_scene_file_path().is_empty()) {
672
ss = p_node->get_scene_instance_state();
673
}
674
675
if (ss.is_valid()) {
676
String path = ss->get_path();
677
678
if (!checked_paths.has(path)) {
679
uint64_t modified_time = FileAccess::get_modified_time(path);
680
if (modified_time != ss->get_last_modified_time()) {
681
return true; //external scene changed
682
}
683
684
checked_paths.insert(path);
685
}
686
}
687
688
for (int i = 0; i < p_node->get_child_count(); i++) {
689
bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths);
690
if (found) {
691
return true;
692
}
693
}
694
695
return false;
696
}
697
698
bool EditorData::check_and_update_scene(int p_idx) {
699
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false);
700
if (!edited_scene[p_idx].root) {
701
return false;
702
}
703
704
HashSet<String> checked_scenes;
705
706
bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
707
708
if (must_reload) {
709
Ref<PackedScene> pscene;
710
pscene.instantiate();
711
712
EditorProgress ep("update_scene", TTR("Updating Scene"), 2);
713
ep.step(TTR("Storing local changes..."), 0);
714
// Pack first, so it stores diffs to previous version of saved scene.
715
Error err = pscene->pack(edited_scene[p_idx].root);
716
ERR_FAIL_COND_V(err != OK, false);
717
ep.step(TTR("Updating scene..."), 1);
718
Node *new_scene = pscene->instantiate(PackedScene::GEN_EDIT_STATE_MAIN);
719
ERR_FAIL_NULL_V(new_scene, false);
720
721
// Transfer selection.
722
List<Node *> new_selection;
723
for (const Node *E : edited_scene.write[p_idx].selection) {
724
NodePath p = edited_scene[p_idx].root->get_path_to(E);
725
Node *new_node = new_scene->get_node(p);
726
if (new_node) {
727
new_selection.push_back(new_node);
728
}
729
}
730
731
new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path());
732
Node *old_root = edited_scene[p_idx].root;
733
EditorNode::get_singleton()->set_edited_scene(new_scene);
734
memdelete(old_root);
735
edited_scene.write[p_idx].selection = new_selection;
736
737
return true;
738
}
739
740
return false;
741
}
742
743
int EditorData::get_edited_scene() const {
744
return current_edited_scene;
745
}
746
747
int EditorData::get_edited_scene_from_path(const String &p_path) const {
748
for (int i = 0; i < edited_scene.size(); i++) {
749
if (edited_scene[i].path == p_path) {
750
return i;
751
}
752
}
753
754
return -1;
755
}
756
757
void EditorData::set_edited_scene(int p_idx) {
758
ERR_FAIL_INDEX(p_idx, edited_scene.size());
759
current_edited_scene = p_idx;
760
}
761
762
Node *EditorData::get_edited_scene_root(int p_idx) {
763
if (p_idx < 0) {
764
ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr);
765
return edited_scene[current_edited_scene].root;
766
} else {
767
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr);
768
return edited_scene[p_idx].root;
769
}
770
}
771
772
void EditorData::set_edited_scene_root(Node *p_root) {
773
ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
774
edited_scene.write[current_edited_scene].root = p_root;
775
if (p_root) {
776
if (!p_root->get_scene_file_path().is_empty()) {
777
edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path();
778
} else {
779
p_root->set_scene_file_path(edited_scene[current_edited_scene].path);
780
}
781
}
782
783
if (!edited_scene[current_edited_scene].path.is_empty()) {
784
edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
785
}
786
}
787
788
int EditorData::get_edited_scene_count() const {
789
return edited_scene.size();
790
}
791
792
Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const {
793
Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>();
794
795
for (int i = 0; i < edited_scene.size(); i++) {
796
out_edited_scenes_list.push_back(edited_scene[i]);
797
}
798
799
return out_edited_scenes_list;
800
}
801
802
void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) {
803
if (p_idx == -1) {
804
p_idx = current_edited_scene;
805
}
806
ERR_FAIL_INDEX(p_idx, edited_scene.size());
807
808
edited_scene.write[p_idx].file_modified_time = p_time;
809
}
810
811
uint64_t EditorData::get_scene_modified_time(int p_idx) const {
812
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0);
813
return edited_scene[p_idx].file_modified_time;
814
}
815
816
String EditorData::get_scene_type(int p_idx) const {
817
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
818
if (!edited_scene[p_idx].root) {
819
return "";
820
}
821
return edited_scene[p_idx].root->get_class();
822
}
823
824
void EditorData::move_edited_scene_to_index(int p_idx) {
825
ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
826
ERR_FAIL_INDEX(p_idx, edited_scene.size());
827
828
EditedScene es = edited_scene[current_edited_scene];
829
edited_scene.remove_at(current_edited_scene);
830
edited_scene.insert(p_idx, es);
831
current_edited_scene = p_idx;
832
}
833
834
Ref<Script> EditorData::get_scene_root_script(int p_idx) const {
835
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>());
836
if (!edited_scene[p_idx].root) {
837
return Ref<Script>();
838
}
839
Ref<Script> s = edited_scene[p_idx].root->get_script();
840
if (s.is_null() && edited_scene[p_idx].root->get_child_count()) {
841
Node *n = edited_scene[p_idx].root->get_child(0);
842
while (s.is_null() && n && n->get_scene_file_path().is_empty()) {
843
s = n->get_script();
844
n = n->get_parent();
845
}
846
}
847
return s;
848
}
849
850
String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const {
851
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
852
if (!edited_scene[p_idx].root) {
853
return TTR("[empty]");
854
}
855
if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
856
return TTR("[unsaved]");
857
}
858
859
const String filename = edited_scene[p_idx].root->get_scene_file_path().get_file();
860
const String basename = filename.get_basename();
861
862
if (p_always_strip_extension) {
863
return basename;
864
}
865
866
// Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited).
867
for (int i = 0; i < edited_scene.size(); i++) {
868
if (i == p_idx) {
869
// Don't compare the edited scene against itself.
870
continue;
871
}
872
873
if (edited_scene[i].root && basename == edited_scene[i].root->get_scene_file_path().get_file().get_basename()) {
874
return filename;
875
}
876
}
877
878
// Else, return just the basename as there's no ambiguity.
879
return basename;
880
}
881
882
void EditorData::set_scene_path(int p_idx, const String &p_path) {
883
ERR_FAIL_INDEX(p_idx, edited_scene.size());
884
edited_scene.write[p_idx].path = p_path;
885
886
if (!edited_scene[p_idx].root) {
887
return;
888
}
889
edited_scene[p_idx].root->set_scene_file_path(p_path);
890
}
891
892
String EditorData::get_scene_path(int p_idx) const {
893
ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String());
894
895
if (edited_scene[p_idx].root) {
896
if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) {
897
edited_scene[p_idx].root->set_scene_file_path(edited_scene[p_idx].path);
898
} else {
899
return edited_scene[p_idx].root->get_scene_file_path();
900
}
901
}
902
903
return edited_scene[p_idx].path;
904
}
905
906
void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) {
907
ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
908
909
edited_scene.write[current_edited_scene].live_edit_root = p_root;
910
}
911
912
NodePath EditorData::get_edited_scene_live_edit_root() {
913
ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String());
914
915
return edited_scene[current_edited_scene].live_edit_root;
916
}
917
918
void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom) {
919
ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
920
921
EditedScene &es = edited_scene.write[current_edited_scene];
922
es.selection = p_selection->get_full_selected_node_list();
923
es.history_current = p_history->current_elem_idx;
924
es.history_stored = p_history->history;
925
es.editor_states = get_editor_plugin_states();
926
es.custom_state = p_custom;
927
}
928
929
Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history) {
930
ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
931
932
const EditedScene &es = edited_scene.write[current_edited_scene];
933
934
p_history->current_elem_idx = es.history_current;
935
p_history->history = es.history_stored;
936
937
p_selection->clear();
938
for (Node *E : es.selection) {
939
p_selection->add_node(E);
940
}
941
set_editor_plugin_states(es.editor_states);
942
943
return es.custom_state;
944
}
945
946
void EditorData::clear_edited_scenes() {
947
for (int i = 0; i < edited_scene.size(); i++) {
948
if (edited_scene[i].root) {
949
memdelete(edited_scene[i].root);
950
}
951
}
952
edited_scene.clear();
953
SceneTree::get_singleton()->set_edited_scene_root(nullptr);
954
}
955
956
void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) {
957
for (int i = 0; i < editor_plugins.size(); i++) {
958
editor_plugins[i]->set_window_layout(p_layout);
959
}
960
}
961
962
void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) {
963
for (int i = 0; i < editor_plugins.size(); i++) {
964
editor_plugins[i]->get_window_layout(p_layout);
965
}
966
}
967
968
bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) {
969
if (!ScriptServer::is_global_class(p_class)) {
970
return false;
971
}
972
973
String base = p_class;
974
while (base != p_inherits) {
975
if (ClassDB::class_exists(base)) {
976
return ClassDB::is_parent_class(base, p_inherits);
977
} else if (ScriptServer::is_global_class(base)) {
978
base = ScriptServer::get_global_class_base(base);
979
} else {
980
return false;
981
}
982
}
983
return true;
984
}
985
986
Variant EditorData::script_class_instance(const String &p_class) {
987
if (ScriptServer::is_global_class(p_class)) {
988
Ref<Script> script = script_class_load_script(p_class);
989
if (script.is_valid()) {
990
// Store in a variant to initialize the refcount if needed.
991
Variant v = ClassDB::instantiate(script->get_instance_base_type());
992
if (v) {
993
Object *obj = v;
994
PropertyUtils::assign_custom_type_script(obj, script);
995
obj->set_script(script);
996
}
997
return v;
998
}
999
}
1000
return Variant();
1001
}
1002
1003
Ref<Script> EditorData::script_class_load_script(const String &p_class) const {
1004
if (!ScriptServer::is_global_class(p_class)) {
1005
return Ref<Script>();
1006
}
1007
1008
String path = ScriptServer::get_global_class_path(p_class);
1009
return ResourceLoader::load(path, "Script");
1010
}
1011
1012
void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) {
1013
_script_class_icon_paths[p_class] = p_icon_path;
1014
}
1015
1016
String EditorData::script_class_get_icon_path(const String &p_class, bool *r_valid) const {
1017
String current = p_class;
1018
while (true) {
1019
if (!ScriptServer::is_global_class(current)) {
1020
// If the classnames chain has a native class ancestor, we're done with success.
1021
if (r_valid) {
1022
*r_valid = ClassDB::class_exists(current);
1023
}
1024
return String();
1025
}
1026
HashMap<StringName, String>::ConstIterator E = _script_class_icon_paths.find(current);
1027
if ((bool)E) {
1028
if (r_valid) {
1029
*r_valid = !E->value.is_empty();
1030
return E->value;
1031
} else if (!E->value.is_empty()) {
1032
return E->value;
1033
}
1034
}
1035
current = ScriptServer::get_global_class_base(current);
1036
}
1037
}
1038
1039
StringName EditorData::script_class_get_name(const String &p_path) const {
1040
return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName();
1041
}
1042
1043
void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) {
1044
_script_class_file_to_path[p_path] = p_class;
1045
}
1046
1047
void EditorData::script_class_save_global_classes() {
1048
List<StringName> global_classes;
1049
ScriptServer::get_global_class_list(&global_classes);
1050
Array array_classes;
1051
for (const StringName &class_name : global_classes) {
1052
Dictionary d;
1053
String *icon = _script_class_icon_paths.getptr(class_name);
1054
d["class"] = class_name;
1055
d["language"] = ScriptServer::get_global_class_language(class_name);
1056
d["path"] = ScriptServer::get_global_class_path(class_name);
1057
d["base"] = ScriptServer::get_global_class_base(class_name);
1058
d["icon"] = icon ? *icon : String();
1059
d["is_abstract"] = ScriptServer::is_global_class_abstract(class_name);
1060
d["is_tool"] = ScriptServer::is_global_class_tool(class_name);
1061
array_classes.push_back(d);
1062
}
1063
ProjectSettings::get_singleton()->store_global_class_list(array_classes);
1064
}
1065
1066
void EditorData::script_class_load_icon_paths() {
1067
script_class_clear_icon_paths();
1068
1069
#ifndef DISABLE_DEPRECATED
1070
if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) {
1071
Dictionary d = GLOBAL_GET("_global_script_class_icons");
1072
1073
for (const KeyValue<Variant, Variant> &kv : d) {
1074
String name = kv.key.operator String();
1075
_script_class_icon_paths[name] = kv.value;
1076
1077
String path = ScriptServer::get_global_class_path(name);
1078
script_class_set_name(path, name);
1079
}
1080
ProjectSettings::get_singleton()->clear("_global_script_class_icons");
1081
}
1082
#endif
1083
1084
Array script_classes = ProjectSettings::get_singleton()->get_global_class_list();
1085
for (int i = 0; i < script_classes.size(); i++) {
1086
Dictionary d = script_classes[i];
1087
if (!d.has("class") || !d.has("path") || !d.has("icon")) {
1088
continue;
1089
}
1090
1091
String name = d["class"];
1092
_script_class_icon_paths[name] = d["icon"];
1093
script_class_set_name(d["path"], name);
1094
}
1095
}
1096
1097
Ref<Texture2D> EditorData::extension_class_get_icon(const String &p_class) const {
1098
if (GDExtensionManager::get_singleton()->class_has_icon_path(p_class)) {
1099
String icon_path = GDExtensionManager::get_singleton()->class_get_icon_path(p_class);
1100
Ref<Texture2D> icon = _load_script_icon(icon_path);
1101
if (icon.is_valid()) {
1102
return icon;
1103
}
1104
}
1105
return nullptr;
1106
}
1107
1108
Ref<Texture2D> EditorData::_load_script_icon(const String &p_path) const {
1109
if (!p_path.is_empty() && ResourceLoader::exists(p_path)) {
1110
Ref<Texture2D> icon = ResourceLoader::load(p_path);
1111
if (icon.is_valid()) {
1112
return icon;
1113
}
1114
}
1115
return nullptr;
1116
}
1117
1118
Ref<Texture2D> EditorData::get_script_icon(const String &p_script_path) {
1119
// Take from the local cache, if available.
1120
{
1121
Ref<Texture2D> *icon = _script_icon_cache.getptr(p_script_path);
1122
if (icon) {
1123
// Can be an empty value if we can't resolve any icon for this script.
1124
// An empty value is still cached to avoid unnecessary attempts at resolving it again.
1125
return *icon;
1126
}
1127
}
1128
1129
// Fast path in case the whole hierarchy is made of global classes.
1130
StringName class_name = script_class_get_name(p_script_path);
1131
{
1132
if (class_name != StringName()) {
1133
bool icon_valid = false;
1134
String icon_path = script_class_get_icon_path(class_name, &icon_valid);
1135
if (icon_valid) {
1136
Ref<Texture2D> icon = _load_script_icon(icon_path);
1137
_script_icon_cache[p_script_path] = icon;
1138
return icon;
1139
}
1140
}
1141
}
1142
1143
Ref<Script> base_scr = ResourceLoader::load(p_script_path, "Script");
1144
while (base_scr.is_valid()) {
1145
// Check for scripted classes.
1146
String icon_path;
1147
StringName base_class_name = script_class_get_name(base_scr->get_path());
1148
if (base_scr->is_built_in() || base_class_name == StringName()) {
1149
icon_path = base_scr->get_class_icon_path();
1150
} else {
1151
icon_path = script_class_get_icon_path(base_class_name);
1152
}
1153
1154
Ref<Texture2D> icon = _load_script_icon(icon_path);
1155
if (icon.is_valid()) {
1156
_script_icon_cache[p_script_path] = icon;
1157
return icon;
1158
}
1159
1160
// Check for legacy custom classes defined by plugins.
1161
// TODO: Should probably be deprecated in 4.x
1162
const EditorData::CustomType *ctype = get_custom_type_by_path(base_scr->get_path());
1163
if (ctype && ctype->icon.is_valid()) {
1164
_script_icon_cache[p_script_path] = ctype->icon;
1165
return ctype->icon;
1166
}
1167
1168
// Move to the base class.
1169
base_scr = base_scr->get_base_script();
1170
}
1171
1172
// Check if the base type is an extension-defined type.
1173
Ref<Texture2D> ext_icon = extension_class_get_icon(class_name);
1174
if (ext_icon.is_valid()) {
1175
_script_icon_cache[p_script_path] = ext_icon;
1176
return ext_icon;
1177
}
1178
1179
// If no icon found, cache it as null.
1180
_script_icon_cache[p_script_path] = Ref<Texture2D>();
1181
return Ref<Texture2D>();
1182
}
1183
1184
void EditorData::clear_script_icon_cache() {
1185
_script_icon_cache.clear();
1186
}
1187
1188
EditorData::EditorData() {
1189
undo_redo_manager = memnew(EditorUndoRedoManager);
1190
script_class_load_icon_paths();
1191
}
1192
1193
EditorData::~EditorData() {
1194
memdelete(undo_redo_manager);
1195
}
1196
1197
///////////////////////////////////////////////////////////////////////////////
1198
1199
void EditorSelection::_node_removed(Node *p_node) {
1200
if (!selection.has(p_node)) {
1201
return;
1202
}
1203
1204
Object *meta = selection[p_node];
1205
if (meta) {
1206
memdelete(meta);
1207
}
1208
selection.erase(p_node);
1209
changed = true;
1210
node_list_changed = true;
1211
}
1212
1213
void EditorSelection::add_node(Node *p_node) {
1214
ERR_FAIL_NULL(p_node);
1215
ERR_FAIL_COND(!p_node->is_inside_tree());
1216
if (selection.has(p_node)) {
1217
return;
1218
}
1219
1220
changed = true;
1221
node_list_changed = true;
1222
Object *meta = nullptr;
1223
for (Object *E : editor_plugins) {
1224
meta = E->call("_get_editor_data", p_node);
1225
if (meta) {
1226
break;
1227
}
1228
}
1229
selection[p_node] = meta;
1230
1231
p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed).bind(p_node), CONNECT_ONE_SHOT);
1232
}
1233
1234
void EditorSelection::remove_node(Node *p_node) {
1235
ERR_FAIL_NULL(p_node);
1236
if (!selection.has(p_node)) {
1237
return;
1238
}
1239
1240
changed = true;
1241
node_list_changed = true;
1242
Object *meta = selection[p_node];
1243
if (meta) {
1244
memdelete(meta);
1245
}
1246
selection.erase(p_node);
1247
1248
p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed));
1249
}
1250
1251
bool EditorSelection::is_selected(Node *p_node) const {
1252
return selection.has(p_node);
1253
}
1254
1255
void EditorSelection::_bind_methods() {
1256
ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
1257
ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
1258
ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node);
1259
ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::get_selected_nodes);
1260
ClassDB::bind_method(D_METHOD("get_top_selected_nodes"), &EditorSelection::get_top_selected_nodes);
1261
#ifndef DISABLE_DEPRECATED
1262
ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::get_top_selected_nodes);
1263
#endif // DISABLE_DEPRECATED
1264
ADD_SIGNAL(MethodInfo("selection_changed"));
1265
}
1266
1267
void EditorSelection::add_editor_plugin(Object *p_object) {
1268
editor_plugins.push_back(p_object);
1269
}
1270
1271
void EditorSelection::_update_node_list() {
1272
if (!node_list_changed) {
1273
return;
1274
}
1275
1276
top_selected_node_list.clear();
1277
1278
// If the selection does not have the parent of the selected node, then add the node to the node list.
1279
// However, if the parent is already selected, then adding this node is redundant as
1280
// it is included with the parent, so skip it.
1281
for (const KeyValue<Node *, Object *> &E : selection) {
1282
Node *parent = E.key;
1283
parent = parent->get_parent();
1284
bool skip = false;
1285
while (parent) {
1286
if (selection.has(parent)) {
1287
skip = true;
1288
break;
1289
}
1290
parent = parent->get_parent();
1291
}
1292
1293
if (skip) {
1294
continue;
1295
}
1296
top_selected_node_list.push_back(E.key);
1297
}
1298
1299
node_list_changed = true;
1300
}
1301
1302
void EditorSelection::update() {
1303
_update_node_list();
1304
1305
if (!changed) {
1306
return;
1307
}
1308
changed = false;
1309
if (!emitted) {
1310
emitted = true;
1311
callable_mp(this, &EditorSelection::_emit_change).call_deferred();
1312
}
1313
}
1314
1315
void EditorSelection::_emit_change() {
1316
emit_signal(SNAME("selection_changed"));
1317
emitted = false;
1318
}
1319
1320
TypedArray<Node> EditorSelection::get_top_selected_nodes() {
1321
TypedArray<Node> ret;
1322
1323
for (const Node *E : top_selected_node_list) {
1324
ret.push_back(E);
1325
}
1326
1327
return ret;
1328
}
1329
1330
TypedArray<Node> EditorSelection::get_selected_nodes() {
1331
TypedArray<Node> ret;
1332
1333
for (const KeyValue<Node *, Object *> &E : selection) {
1334
ret.push_back(E.key);
1335
}
1336
1337
return ret;
1338
}
1339
1340
const List<Node *> &EditorSelection::get_top_selected_node_list() {
1341
if (changed) {
1342
update();
1343
} else {
1344
_update_node_list();
1345
}
1346
return top_selected_node_list;
1347
}
1348
1349
List<Node *> EditorSelection::get_full_selected_node_list() {
1350
List<Node *> node_list;
1351
for (const KeyValue<Node *, Object *> &E : selection) {
1352
node_list.push_back(E.key);
1353
}
1354
1355
return node_list;
1356
}
1357
1358
void EditorSelection::clear() {
1359
while (!selection.is_empty()) {
1360
remove_node(selection.begin()->key);
1361
}
1362
1363
changed = true;
1364
node_list_changed = true;
1365
}
1366
1367
EditorSelection::~EditorSelection() {
1368
clear();
1369
}
1370
1371