| 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 | if (edited_scene[p_idx].root == SceneTree::get_singleton()->get_edited_scene_root()) { |
| 647 | SceneTree::get_singleton()->set_edited_scene_root(nullptr); |
| 648 | } |
| 649 | memdelete(edited_scene[p_idx].root); |
| 650 | edited_scene.write[p_idx].root = nullptr; |
| 651 | } |
| 652 | |
| 653 | if (current_edited_scene > p_idx) { |
| 654 | current_edited_scene--; |
| 655 | } else if (current_edited_scene == p_idx && current_edited_scene > 0) { |
| 656 | current_edited_scene--; |
| 657 | } |
| 658 | |
| 659 | if (!edited_scene[p_idx].path.is_empty()) { |
| 660 | EditorNode::get_singleton()->emit_signal("scene_closed", edited_scene[p_idx].path); |
| 661 | } |
| 662 | |
| 663 | if (undo_redo_manager->has_history(edited_scene[p_idx].history_id)) { // Might not exist if scene failed to load. |
| 664 | undo_redo_manager->discard_history(edited_scene[p_idx].history_id); |
| 665 | } |
| 666 | edited_scene.remove_at(p_idx); |
| 667 | } |
| 668 | |
| 669 | bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths) { |
| 670 | Ref<SceneState> ss; |
| 671 | |
| 672 | if (p_node == p_root) { |
| 673 | ss = p_node->get_scene_inherited_state(); |
| 674 | } else if (p_node->is_instance()) { |
| 675 | ss = p_node->get_scene_instance_state(); |
| 676 | } |
| 677 | |
| 678 | if (ss.is_valid()) { |
| 679 | String path = ss->get_path(); |
| 680 | |
| 681 | if (!checked_paths.has(path)) { |
| 682 | uint64_t modified_time = FileAccess::get_modified_time(path); |
| 683 | if (modified_time != ss->get_last_modified_time()) { |
| 684 | return true; //external scene changed |
| 685 | } |
| 686 | |
| 687 | checked_paths.insert(path); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | for (int i = 0; i < p_node->get_child_count(); i++) { |
| 692 | bool found = _find_updated_instances(p_root, p_node->get_child(i), checked_paths); |
| 693 | if (found) { |
| 694 | return true; |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | return false; |
| 699 | } |
| 700 | |
| 701 | bool EditorData::check_and_update_scene(int p_idx) { |
| 702 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false); |
| 703 | if (!edited_scene[p_idx].root) { |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | HashSet<String> checked_scenes; |
| 708 | |
| 709 | bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes); |
| 710 | |
| 711 | if (must_reload) { |
| 712 | reload_scene_from_memory(p_idx, false); |
| 713 | |
| 714 | return true; |
| 715 | } |
| 716 | |
| 717 | return false; |
| 718 | } |
| 719 | |
| 720 | bool EditorData::reload_scene_from_memory(int p_idx, bool p_mark_unsaved) { |
| 721 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), false); |
| 722 | if (!edited_scene[p_idx].root) { |
| 723 | return false; |
| 724 | } |
| 725 | |
| 726 | Ref<PackedScene> pscene; |
| 727 | pscene.instantiate(); |
| 728 | |
| 729 | EditorProgress ep("update_scene", TTR("Updating Scene"), 2); |
| 730 | ep.step(TTR("Storing local changes..."), 0); |
| 731 | // Pack first, so it stores diffs to previous version of saved scene. |
| 732 | Error err = pscene->pack(edited_scene[p_idx].root); |
| 733 | ERR_FAIL_COND_V(err != OK, false); |
| 734 | ep.step(TTR("Updating scene..."), 1); |
| 735 | Node *new_scene = pscene->instantiate(PackedScene::GEN_EDIT_STATE_MAIN); |
| 736 | ERR_FAIL_NULL_V(new_scene, false); |
| 737 | |
| 738 | // Transfer selection. |
| 739 | List<Node *> new_selection; |
| 740 | for (const Node *E : edited_scene.write[p_idx].selection) { |
| 741 | NodePath p = edited_scene[p_idx].root->get_path_to(E); |
| 742 | Node *new_node = new_scene->get_node(p); |
| 743 | if (new_node) { |
| 744 | new_selection.push_back(new_node); |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path()); |
| 749 | Node *old_root = edited_scene[p_idx].root; |
| 750 | EditorNode::get_singleton()->set_edited_scene(new_scene); |
| 751 | memdelete(old_root); |
| 752 | edited_scene.write[p_idx].selection = new_selection; |
| 753 | |
| 754 | if (p_mark_unsaved) { |
| 755 | EditorUndoRedoManager::get_singleton()->clear_history(get_scene_history_id(p_idx)); |
| 756 | } |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | int EditorData::get_edited_scene() const { |
| 761 | return current_edited_scene; |
| 762 | } |
| 763 | |
| 764 | int EditorData::get_edited_scene_from_path(const String &p_path) const { |
| 765 | for (int i = 0; i < edited_scene.size(); i++) { |
| 766 | if (edited_scene[i].path == p_path) { |
| 767 | return i; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | return -1; |
| 772 | } |
| 773 | |
| 774 | void EditorData::set_edited_scene(int p_idx) { |
| 775 | ERR_FAIL_INDEX(p_idx, edited_scene.size()); |
| 776 | current_edited_scene = p_idx; |
| 777 | } |
| 778 | |
| 779 | Node *EditorData::get_edited_scene_root(int p_idx) { |
| 780 | if (p_idx < 0) { |
| 781 | ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), nullptr); |
| 782 | return edited_scene[current_edited_scene].root; |
| 783 | } else { |
| 784 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), nullptr); |
| 785 | return edited_scene[p_idx].root; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | void EditorData::set_edited_scene_root(Node *p_root) { |
| 790 | ERR_FAIL_INDEX(current_edited_scene, edited_scene.size()); |
| 791 | edited_scene.write[current_edited_scene].root = p_root; |
| 792 | if (p_root) { |
| 793 | if (p_root->is_instance()) { |
| 794 | edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path(); |
| 795 | } else { |
| 796 | p_root->set_scene_file_path(edited_scene[current_edited_scene].path); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | if (!edited_scene[current_edited_scene].path.is_empty()) { |
| 801 | edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | int EditorData::get_edited_scene_count() const { |
| 806 | return edited_scene.size(); |
| 807 | } |
| 808 | |
| 809 | Vector<EditorData::EditedScene> EditorData::get_edited_scenes() const { |
| 810 | Vector<EditedScene> out_edited_scenes_list = Vector<EditedScene>(); |
| 811 | |
| 812 | for (int i = 0; i < edited_scene.size(); i++) { |
| 813 | out_edited_scenes_list.push_back(edited_scene[i]); |
| 814 | } |
| 815 | |
| 816 | return out_edited_scenes_list; |
| 817 | } |
| 818 | |
| 819 | void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) { |
| 820 | if (p_idx == -1) { |
| 821 | p_idx = current_edited_scene; |
| 822 | } |
| 823 | ERR_FAIL_INDEX(p_idx, edited_scene.size()); |
| 824 | |
| 825 | edited_scene.write[p_idx].file_modified_time = p_time; |
| 826 | } |
| 827 | |
| 828 | uint64_t EditorData::get_scene_modified_time(int p_idx) const { |
| 829 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0); |
| 830 | return edited_scene[p_idx].file_modified_time; |
| 831 | } |
| 832 | |
| 833 | String EditorData::get_scene_type(int p_idx) const { |
| 834 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String()); |
| 835 | if (!edited_scene[p_idx].root) { |
| 836 | return ""; |
| 837 | } |
| 838 | return edited_scene[p_idx].root->get_class(); |
| 839 | } |
| 840 | |
| 841 | void EditorData::move_edited_scene_to_index(int p_idx) { |
| 842 | ERR_FAIL_INDEX(current_edited_scene, edited_scene.size()); |
| 843 | ERR_FAIL_INDEX(p_idx, edited_scene.size()); |
| 844 | |
| 845 | EditedScene es = edited_scene[current_edited_scene]; |
| 846 | edited_scene.remove_at(current_edited_scene); |
| 847 | edited_scene.insert(p_idx, es); |
| 848 | current_edited_scene = p_idx; |
| 849 | } |
| 850 | |
| 851 | Ref<Script> EditorData::get_scene_root_script(int p_idx) const { |
| 852 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), Ref<Script>()); |
| 853 | if (!edited_scene[p_idx].root) { |
| 854 | return Ref<Script>(); |
| 855 | } |
| 856 | Ref<Script> s = edited_scene[p_idx].root->get_script(); |
| 857 | if (s.is_null() && edited_scene[p_idx].root->get_child_count()) { |
| 858 | Node *n = edited_scene[p_idx].root->get_child(0); |
| 859 | while (s.is_null() && n && !n->is_instance()) { |
| 860 | s = n->get_script(); |
| 861 | n = n->get_parent(); |
| 862 | } |
| 863 | } |
| 864 | return s; |
| 865 | } |
| 866 | |
| 867 | String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const { |
| 868 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String()); |
| 869 | if (!edited_scene[p_idx].root) { |
| 870 | return TTR("[empty]"); |
| 871 | } |
| 872 | if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) { |
| 873 | return TTR("[unsaved]"); |
| 874 | } |
| 875 | |
| 876 | const String filename = edited_scene[p_idx].root->get_scene_file_path().get_file(); |
| 877 | const String basename = filename.get_basename(); |
| 878 | |
| 879 | if (p_always_strip_extension) { |
| 880 | return basename; |
| 881 | } |
| 882 | |
| 883 | // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited). |
| 884 | for (int i = 0; i < edited_scene.size(); i++) { |
| 885 | if (i == p_idx) { |
| 886 | // Don't compare the edited scene against itself. |
| 887 | continue; |
| 888 | } |
| 889 | |
| 890 | if (edited_scene[i].root && basename == edited_scene[i].root->get_scene_file_path().get_file().get_basename()) { |
| 891 | return filename; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | // Else, return just the basename as there's no ambiguity. |
| 896 | return basename; |
| 897 | } |
| 898 | |
| 899 | void EditorData::set_scene_path(int p_idx, const String &p_path) { |
| 900 | ERR_FAIL_INDEX(p_idx, edited_scene.size()); |
| 901 | edited_scene.write[p_idx].path = p_path; |
| 902 | |
| 903 | if (!edited_scene[p_idx].root) { |
| 904 | return; |
| 905 | } |
| 906 | edited_scene[p_idx].root->set_scene_file_path(p_path); |
| 907 | } |
| 908 | |
| 909 | String EditorData::get_scene_path(int p_idx) const { |
| 910 | ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String()); |
| 911 | |
| 912 | if (edited_scene[p_idx].root) { |
| 913 | if (edited_scene[p_idx].root->get_scene_file_path().is_empty()) { |
| 914 | edited_scene[p_idx].root->set_scene_file_path(edited_scene[p_idx].path); |
| 915 | } else { |
| 916 | return edited_scene[p_idx].root->get_scene_file_path(); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | return edited_scene[p_idx].path; |
| 921 | } |
| 922 | |
| 923 | void EditorData::set_edited_scene_live_edit_root(const NodePath &p_root) { |
| 924 | ERR_FAIL_INDEX(current_edited_scene, edited_scene.size()); |
| 925 | |
| 926 | edited_scene.write[current_edited_scene].live_edit_root = p_root; |
| 927 | } |
| 928 | |
| 929 | NodePath EditorData::get_edited_scene_live_edit_root() { |
| 930 | ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), String()); |
| 931 | |
| 932 | return edited_scene[current_edited_scene].live_edit_root; |
| 933 | } |
| 934 | |
| 935 | void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom) { |
| 936 | ERR_FAIL_INDEX(current_edited_scene, edited_scene.size()); |
| 937 | |
| 938 | EditedScene &es = edited_scene.write[current_edited_scene]; |
| 939 | es.selection = p_selection->get_full_selected_node_list(); |
| 940 | es.history_current = p_history->current_elem_idx; |
| 941 | es.history_stored = p_history->history; |
| 942 | es.editor_states = get_editor_plugin_states(); |
| 943 | es.custom_state = p_custom; |
| 944 | } |
| 945 | |
| 946 | Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history) { |
| 947 | ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary()); |
| 948 | |
| 949 | const EditedScene &es = edited_scene.write[current_edited_scene]; |
| 950 | |
| 951 | p_history->current_elem_idx = es.history_current; |
| 952 | p_history->history = es.history_stored; |
| 953 | |
| 954 | p_selection->clear(); |
| 955 | for (Node *E : es.selection) { |
| 956 | p_selection->add_node(E); |
| 957 | } |
| 958 | set_editor_plugin_states(es.editor_states); |
| 959 | |
| 960 | return es.custom_state; |
| 961 | } |
| 962 | |
| 963 | void EditorData::clear_edited_scenes() { |
| 964 | for (int i = 0; i < edited_scene.size(); i++) { |
| 965 | if (edited_scene[i].root) { |
| 966 | memdelete(edited_scene[i].root); |
| 967 | } |
| 968 | } |
| 969 | edited_scene.clear(); |
| 970 | SceneTree::get_singleton()->set_edited_scene_root(nullptr); |
| 971 | } |
| 972 | |
| 973 | void EditorData::set_plugin_window_layout(Ref<ConfigFile> p_layout) { |
| 974 | for (int i = 0; i < editor_plugins.size(); i++) { |
| 975 | editor_plugins[i]->set_window_layout(p_layout); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | void EditorData::get_plugin_window_layout(Ref<ConfigFile> p_layout) { |
| 980 | for (int i = 0; i < editor_plugins.size(); i++) { |
| 981 | editor_plugins[i]->get_window_layout(p_layout); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | bool EditorData::script_class_is_parent(const String &p_class, const String &p_inherits) { |
| 986 | if (!ScriptServer::is_global_class(p_class)) { |
| 987 | return false; |
| 988 | } |
| 989 | |
| 990 | String base = p_class; |
| 991 | while (base != p_inherits) { |
| 992 | if (ClassDB::class_exists(base)) { |
| 993 | return ClassDB::is_parent_class(base, p_inherits); |
| 994 | } else if (ScriptServer::is_global_class(base)) { |
| 995 | base = ScriptServer::get_global_class_base(base); |
| 996 | } else { |
| 997 | return false; |
| 998 | } |
| 999 | } |
| 1000 | return true; |
| 1001 | } |
| 1002 | |
| 1003 | Variant EditorData::script_class_instance(const String &p_class) { |
| 1004 | if (ScriptServer::is_global_class(p_class)) { |
| 1005 | Ref<Script> script = script_class_load_script(p_class); |
| 1006 | if (script.is_valid()) { |
| 1007 | // Store in a variant to initialize the refcount if needed. |
| 1008 | Variant v = ClassDB::instantiate(script->get_instance_base_type()); |
| 1009 | if (v) { |
| 1010 | Object *obj = v; |
| 1011 | PropertyUtils::assign_custom_type_script(obj, script); |
| 1012 | obj->set_script(script); |
| 1013 | } |
| 1014 | return v; |
| 1015 | } |
| 1016 | } |
| 1017 | return Variant(); |
| 1018 | } |
| 1019 | |
| 1020 | Ref<Script> EditorData::script_class_load_script(const String &p_class) const { |
| 1021 | if (!ScriptServer::is_global_class(p_class)) { |
| 1022 | return Ref<Script>(); |
| 1023 | } |
| 1024 | |
| 1025 | String path = ScriptServer::get_global_class_path(p_class); |
| 1026 | return ResourceLoader::load(path, "Script"); |
| 1027 | } |
| 1028 | |
| 1029 | void EditorData::script_class_set_icon_path(const String &p_class, const String &p_icon_path) { |
| 1030 | _script_class_icon_paths[p_class] = p_icon_path; |
| 1031 | } |
| 1032 | |
| 1033 | String EditorData::script_class_get_icon_path(const String &p_class, bool *r_valid) const { |
| 1034 | String current = p_class; |
| 1035 | while (true) { |
| 1036 | if (!ScriptServer::is_global_class(current)) { |
| 1037 | // If the classnames chain has a native class ancestor, we're done with success. |
| 1038 | if (r_valid) { |
| 1039 | *r_valid = ClassDB::class_exists(current); |
| 1040 | } |
| 1041 | return String(); |
| 1042 | } |
| 1043 | HashMap<StringName, String>::ConstIterator E = _script_class_icon_paths.find(current); |
| 1044 | if ((bool)E) { |
| 1045 | if (r_valid) { |
| 1046 | *r_valid = !E->value.is_empty(); |
| 1047 | return E->value; |
| 1048 | } else if (!E->value.is_empty()) { |
| 1049 | return E->value; |
| 1050 | } |
| 1051 | } |
| 1052 | current = ScriptServer::get_global_class_base(current); |
| 1053 | } |
| 1054 | } |
| 1055 | |
| 1056 | StringName EditorData::script_class_get_name(const String &p_path) const { |
| 1057 | return _script_class_file_to_path.has(p_path) ? _script_class_file_to_path[p_path] : StringName(); |
| 1058 | } |
| 1059 | |
| 1060 | void EditorData::script_class_set_name(const String &p_path, const StringName &p_class) { |
| 1061 | _script_class_file_to_path[p_path] = p_class; |
| 1062 | } |
| 1063 | |
| 1064 | void EditorData::script_class_save_global_classes() { |
| 1065 | LocalVector<StringName> global_classes; |
| 1066 | ScriptServer::get_global_class_list(global_classes); |
| 1067 | Array array_classes; |
| 1068 | for (const StringName &class_name : global_classes) { |
| 1069 | Dictionary d; |
| 1070 | String *icon = _script_class_icon_paths.getptr(class_name); |
| 1071 | d["class"] = class_name; |
| 1072 | d["language"] = ScriptServer::get_global_class_language(class_name); |
| 1073 | d["path"] = ScriptServer::get_global_class_path(class_name); |
| 1074 | d["base"] = ScriptServer::get_global_class_base(class_name); |
| 1075 | d["icon"] = icon ? *icon : String(); |
| 1076 | d["is_abstract"] = ScriptServer::is_global_class_abstract(class_name); |
| 1077 | d["is_tool"] = ScriptServer::is_global_class_tool(class_name); |
| 1078 | array_classes.push_back(d); |
| 1079 | } |
| 1080 | ProjectSettings::get_singleton()->store_global_class_list(array_classes); |
| 1081 | } |
| 1082 | |
| 1083 | void EditorData::script_class_load_icon_paths() { |
| 1084 | script_class_clear_icon_paths(); |
| 1085 | |
| 1086 | #ifndef DISABLE_DEPRECATED |
| 1087 | if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) { |
| 1088 | Dictionary d = GLOBAL_GET("_global_script_class_icons"); |
| 1089 | |
| 1090 | for (const KeyValue<Variant, Variant> &kv : d) { |
| 1091 | String name = kv.key.operator String(); |
| 1092 | _script_class_icon_paths[name] = kv.value; |
| 1093 | |
| 1094 | String path = ScriptServer::get_global_class_path(name); |
| 1095 | script_class_set_name(path, name); |
| 1096 | } |
| 1097 | ProjectSettings::get_singleton()->clear("_global_script_class_icons"); |
| 1098 | } |
| 1099 | #endif |
| 1100 | |
| 1101 | Array script_classes = ProjectSettings::get_singleton()->get_global_class_list(); |
| 1102 | for (int i = 0; i < script_classes.size(); i++) { |
| 1103 | Dictionary d = script_classes[i]; |
| 1104 | if (!d.has("class") || !d.has("path") || !d.has("icon")) { |
| 1105 | continue; |
| 1106 | } |
| 1107 | |
| 1108 | String name = d["class"]; |
| 1109 | _script_class_icon_paths[name] = d["icon"]; |
| 1110 | script_class_set_name(d["path"], name); |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | Ref<Texture2D> EditorData::extension_class_get_icon(const String &p_class) const { |
| 1115 | if (GDExtensionManager::get_singleton()->class_has_icon_path(p_class)) { |
| 1116 | String icon_path = GDExtensionManager::get_singleton()->class_get_icon_path(p_class); |
| 1117 | Ref<Texture2D> icon = _load_script_icon(icon_path); |
| 1118 | if (icon.is_valid()) { |
| 1119 | return icon; |
| 1120 | } |
| 1121 | } |
| 1122 | return nullptr; |
| 1123 | } |
| 1124 | |
| 1125 | Ref<Texture2D> EditorData::_load_script_icon(const String &p_path) const { |
| 1126 | if (!p_path.is_empty() && ResourceLoader::exists(p_path)) { |
| 1127 | Ref<Texture2D> icon = ResourceLoader::load(p_path); |
| 1128 | if (icon.is_valid()) { |
| 1129 | return icon; |
| 1130 | } |
| 1131 | } |
| 1132 | return nullptr; |
| 1133 | } |
| 1134 | |
| 1135 | Ref<Texture2D> EditorData::get_script_icon(const String &p_script_path) { |
| 1136 | // Take from the local cache, if available. |
| 1137 | { |
| 1138 | Ref<Texture2D> *icon = _script_icon_cache.getptr(p_script_path); |
| 1139 | if (icon) { |
| 1140 | // Can be an empty value if we can't resolve any icon for this script. |
| 1141 | // An empty value is still cached to avoid unnecessary attempts at resolving it again. |
| 1142 | return *icon; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | // Fast path in case the whole hierarchy is made of global classes. |
| 1147 | StringName class_name = script_class_get_name(p_script_path); |
| 1148 | { |
| 1149 | if (class_name != StringName()) { |
| 1150 | bool icon_valid = false; |
| 1151 | String icon_path = script_class_get_icon_path(class_name, &icon_valid); |
| 1152 | if (icon_valid) { |
| 1153 | Ref<Texture2D> icon = _load_script_icon(icon_path); |
| 1154 | _script_icon_cache[p_script_path] = icon; |
| 1155 | return icon; |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | Ref<Script> base_scr = ResourceLoader::load(p_script_path, "Script"); |
| 1161 | while (base_scr.is_valid()) { |
| 1162 | // Check for scripted classes. |
| 1163 | String icon_path; |
| 1164 | StringName base_class_name = script_class_get_name(base_scr->get_path()); |
| 1165 | if (base_scr->is_built_in() || base_class_name == StringName()) { |
| 1166 | icon_path = base_scr->get_class_icon_path(); |
| 1167 | } else { |
| 1168 | icon_path = script_class_get_icon_path(base_class_name); |
| 1169 | } |
| 1170 | |
| 1171 | Ref<Texture2D> icon = _load_script_icon(icon_path); |
| 1172 | if (icon.is_valid()) { |
| 1173 | _script_icon_cache[p_script_path] = icon; |
| 1174 | return icon; |
| 1175 | } |
| 1176 | |
| 1177 | // Check for legacy custom classes defined by plugins. |
| 1178 | // TODO: Should probably be deprecated in 4.x |
| 1179 | const EditorData::CustomType *ctype = get_custom_type_by_path(base_scr->get_path()); |
| 1180 | if (ctype && ctype->icon.is_valid()) { |
| 1181 | _script_icon_cache[p_script_path] = ctype->icon; |
| 1182 | return ctype->icon; |
| 1183 | } |
| 1184 | |
| 1185 | // Move to the base class. |
| 1186 | base_scr = base_scr->get_base_script(); |
| 1187 | } |
| 1188 | |
| 1189 | // Check if the base type is an extension-defined type. |
| 1190 | Ref<Texture2D> ext_icon = extension_class_get_icon(class_name); |
| 1191 | if (ext_icon.is_valid()) { |
| 1192 | _script_icon_cache[p_script_path] = ext_icon; |
| 1193 | return ext_icon; |
| 1194 | } |
| 1195 | |
| 1196 | // If no icon found, cache it as null. |
| 1197 | _script_icon_cache[p_script_path] = Ref<Texture2D>(); |
| 1198 | return Ref<Texture2D>(); |
| 1199 | } |
| 1200 | |
| 1201 | void EditorData::clear_script_icon_cache() { |
| 1202 | _script_icon_cache.clear(); |
| 1203 | } |
| 1204 | |
| 1205 | EditorData::EditorData() { |
| 1206 | undo_redo_manager = memnew(EditorUndoRedoManager); |
| 1207 | script_class_load_icon_paths(); |
| 1208 | } |
| 1209 | |
| 1210 | EditorData::~EditorData() { |
| 1211 | memdelete(undo_redo_manager); |
| 1212 | } |
| 1213 | |
| 1214 | /////////////////////////////////////////////////////////////////////////////// |
| 1215 | |
| 1216 | void EditorSelection::_node_removed(Node *p_node) { |
| 1217 | ERR_FAIL_NULL(p_node); |
| 1218 | ObjectID nid = p_node->get_instance_id(); |
| 1219 | if (!selection.has(nid)) { |
| 1220 | return; |
| 1221 | } |
| 1222 | |
| 1223 | Object *meta = selection[nid]; |
| 1224 | memdelete_notnull(meta); |
| 1225 | selection.erase(nid); |
| 1226 | changed = true; |
| 1227 | node_list_changed = true; |
| 1228 | } |
| 1229 | |
| 1230 | void EditorSelection::add_node(Node *p_node) { |
| 1231 | ERR_FAIL_NULL(p_node); |
| 1232 | ERR_FAIL_COND(!p_node->is_inside_tree()); |
| 1233 | ObjectID nid = p_node->get_instance_id(); |
| 1234 | if (selection.has(nid)) { |
| 1235 | return; |
| 1236 | } |
| 1237 | |
| 1238 | changed = true; |
| 1239 | node_list_changed = true; |
| 1240 | Object *meta = nullptr; |
| 1241 | for (Object *E : editor_plugins) { |
| 1242 | meta = E->call("_get_editor_data", p_node); |
| 1243 | if (meta) { |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | selection[nid] = meta; |
| 1248 | |
| 1249 | p_node->connect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed).bind(p_node), CONNECT_ONE_SHOT); |
| 1250 | } |
| 1251 | |
| 1252 | void EditorSelection::remove_node(Node *p_node) { |
| 1253 | ERR_FAIL_NULL(p_node); |
| 1254 | ObjectID nid = p_node->get_instance_id(); |
| 1255 | if (!selection.has(nid)) { |
| 1256 | return; |
| 1257 | } |
| 1258 | |
| 1259 | changed = true; |
| 1260 | node_list_changed = true; |
| 1261 | Object *meta = selection[nid]; |
| 1262 | memdelete_notnull(meta); |
| 1263 | selection.erase(nid); |
| 1264 | |
| 1265 | p_node->disconnect(SceneStringName(tree_exiting), callable_mp(this, &EditorSelection::_node_removed)); |
| 1266 | } |
| 1267 | |
| 1268 | bool EditorSelection::is_selected(Node *p_node) const { |
| 1269 | if (!p_node) { |
| 1270 | return false; |
| 1271 | } |
| 1272 | return selection.has(p_node->get_instance_id()); |
| 1273 | } |
| 1274 | |
| 1275 | void EditorSelection::_bind_methods() { |
| 1276 | ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear); |
| 1277 | ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node); |
| 1278 | ClassDB::bind_method(D_METHOD("remove_node", "node"), &EditorSelection::remove_node); |
| 1279 | ClassDB::bind_method(D_METHOD("get_selected_nodes"), &EditorSelection::get_selected_nodes); |
| 1280 | ClassDB::bind_method(D_METHOD("get_top_selected_nodes"), &EditorSelection::get_top_selected_nodes); |
| 1281 | #ifndef DISABLE_DEPRECATED |
| 1282 | ClassDB::bind_method(D_METHOD("get_transformable_selected_nodes"), &EditorSelection::get_top_selected_nodes); |
| 1283 | #endif // DISABLE_DEPRECATED |
| 1284 | ADD_SIGNAL(MethodInfo("selection_changed")); |
| 1285 | } |
| 1286 | |
| 1287 | void EditorSelection::add_editor_plugin(Object *p_object) { |
| 1288 | editor_plugins.push_back(p_object); |
| 1289 | } |
| 1290 | |
| 1291 | void EditorSelection::_update_node_list() { |
| 1292 | if (!node_list_changed) { |
| 1293 | return; |
| 1294 | } |
| 1295 | |
| 1296 | top_selected_node_list.clear(); |
| 1297 | |
| 1298 | // If the selection does not have the parent of the selected node, then add the node to the node list. |
| 1299 | // However, if the parent is already selected, then adding this node is redundant as |
| 1300 | // it is included with the parent, so skip it. |
| 1301 | for (const KeyValue<ObjectID, Object *> &E : selection) { |
| 1302 | Node *parent = ObjectDB::get_instance<Node>(E.key); |
| 1303 | if (!parent) { |
| 1304 | continue; |
| 1305 | } |
| 1306 | parent = parent->get_parent(); |
| 1307 | bool skip = false; |
| 1308 | while (parent) { |
| 1309 | if (selection.has(parent->get_instance_id())) { |
| 1310 | skip = true; |
| 1311 | break; |
| 1312 | } |
| 1313 | parent = parent->get_parent(); |
| 1314 | } |
| 1315 | |
| 1316 | if (skip) { |
| 1317 | continue; |
| 1318 | } |
| 1319 | top_selected_node_list.push_back(E.key); |
| 1320 | } |
| 1321 | |
| 1322 | node_list_changed = false; |
| 1323 | } |
| 1324 | |
| 1325 | void EditorSelection::update() { |
| 1326 | _update_node_list(); |
| 1327 | |
| 1328 | if (!changed) { |
| 1329 | return; |
| 1330 | } |
| 1331 | changed = false; |
| 1332 | if (!emitted) { |
| 1333 | emitted = true; |
| 1334 | callable_mp(this, &EditorSelection::_emit_change).call_deferred(); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | void EditorSelection::_emit_change() { |
| 1339 | emit_signal(SNAME("selection_changed")); |
| 1340 | emitted = false; |
| 1341 | } |
| 1342 | |
| 1343 | TypedArray<Node> EditorSelection::get_top_selected_nodes() { |
| 1344 | TypedArray<Node> ret; |
| 1345 | |
| 1346 | for (const ObjectID &nid : top_selected_node_list) { |
| 1347 | Node *node = ObjectDB::get_instance<Node>(nid); |
| 1348 | if (node) { |
| 1349 | ret.push_back(node); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
| 1356 | TypedArray<Node> EditorSelection::get_selected_nodes() { |
| 1357 | TypedArray<Node> ret; |
| 1358 | |
| 1359 | for (const KeyValue<ObjectID, Object *> &E : selection) { |
| 1360 | Node *node = ObjectDB::get_instance<Node>(E.key); |
| 1361 | if (node) { |
| 1362 | ret.push_back(node); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | return ret; |
| 1367 | } |
| 1368 | |
| 1369 | List<Node *> EditorSelection::get_top_selected_node_list() { |
| 1370 | if (changed) { |
| 1371 | update(); |
| 1372 | } else { |
| 1373 | _update_node_list(); |
| 1374 | } |
| 1375 | List<Node *> node_list; |
| 1376 | for (const ObjectID &nid : top_selected_node_list) { |
| 1377 | Node *node = ObjectDB::get_instance<Node>(nid); |
| 1378 | if (node) { |
| 1379 | node_list.push_back(node); |
| 1380 | } |
| 1381 | } |
| 1382 | return node_list; |
| 1383 | } |
| 1384 | |
| 1385 | List<Node *> EditorSelection::get_full_selected_node_list() { |
| 1386 | List<Node *> node_list; |
| 1387 | for (const KeyValue<ObjectID, Object *> &E : selection) { |
| 1388 | Node *node = ObjectDB::get_instance<Node>(E.key); |
| 1389 | if (node) { |
| 1390 | node_list.push_back(node); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | return node_list; |
| 1395 | } |
| 1396 | |
| 1397 | void EditorSelection::clear() { |
| 1398 | while (!selection.is_empty()) { |
| 1399 | Node *node = ObjectDB::get_instance<Node>(selection.begin()->key); |
| 1400 | if (node) { |
| 1401 | remove_node(node); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | changed = true; |
| 1406 | node_list_changed = true; |
| 1407 | } |
| 1408 | |
| 1409 | EditorSelection::~EditorSelection() { |
| 1410 | clear(); |
| 1411 | } |