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