diff options
Diffstat (limited to 'editor/editor_data.cpp')
-rw-r--r-- | editor/editor_data.cpp | 218 |
1 files changed, 142 insertions, 76 deletions
diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index 4c4dacbeb5..aee9c21007 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -31,9 +31,9 @@ #include "editor_data.h" #include "core/config/project_settings.h" +#include "core/io/dir_access.h" +#include "core/io/file_access.h" #include "core/io/resource_loader.h" -#include "core/os/dir_access.h" -#include "core/os/file_access.h" #include "editor_node.h" #include "editor_settings.h" #include "scene/resources/packed_scene.h" @@ -83,7 +83,7 @@ void EditorHistory::cleanup_history() { void EditorHistory::_add_object(ObjectID p_object, const String &p_property, int p_level_change, bool p_inspector_only) { Object *obj = ObjectDB::get_instance(p_object); ERR_FAIL_COND(!obj); - Reference *r = Object::cast_to<Reference>(obj); + RefCounted *r = Object::cast_to<RefCounted>(obj); Obj o; if (r) { o.ref = REF(r); @@ -273,16 +273,6 @@ EditorPlugin *EditorData::get_editor(Object *p_object) { return nullptr; } -EditorPlugin *EditorData::get_subeditor(Object *p_object) { - for (int i = editor_plugins.size() - 1; i > -1; i--) { - if (!editor_plugins[i]->has_main_screen() && editor_plugins[i]->handles(p_object)) { - return editor_plugins[i]; - } - } - - return nullptr; -} - Vector<EditorPlugin *> EditorData::get_subeditors(Object *p_object) { Vector<EditorPlugin *> sub_plugins; for (int i = editor_plugins.size() - 1; i > -1; i--) { @@ -309,13 +299,13 @@ void EditorData::copy_object_params(Object *p_object) { List<PropertyInfo> pinfo; p_object->get_property_list(&pinfo); - for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) { - if (!(E->get().usage & PROPERTY_USAGE_EDITOR) || E->get().name == "script" || E->get().name == "scripts") { + for (const PropertyInfo &E : pinfo) { + if (!(E.usage & PROPERTY_USAGE_EDITOR) || E.name == "script" || E.name == "scripts") { continue; } PropertyData pd; - pd.name = E->get().name; + pd.name = E.name; pd.value = p_object->get(pd.name); clipboard.push_back(pd); } @@ -414,9 +404,9 @@ void EditorData::restore_editor_global_states() { void EditorData::paste_object_params(Object *p_object) { ERR_FAIL_NULL(p_object); undo_redo.create_action(TTR("Paste Params")); - for (List<PropertyData>::Element *E = clipboard.front(); E; E = E->next()) { - String name = E->get().name; - undo_redo.add_do_property(p_object, name, E->get().value); + for (const PropertyData &E : clipboard) { + String name = E.name; + undo_redo.add_do_property(p_object, name, E.value); undo_redo.add_undo_property(p_object, name, p_object->get(name)); } undo_redo.commit_action(); @@ -436,6 +426,33 @@ UndoRedo &EditorData::get_undo_redo() { return undo_redo; } +void EditorData::add_undo_redo_inspector_hook_callback(Callable p_callable) { + undo_redo_callbacks.push_back(p_callable); +} + +void EditorData::remove_undo_redo_inspector_hook_callback(Callable p_callable) { + undo_redo_callbacks.erase(p_callable); +} + +const Vector<Callable> EditorData::get_undo_redo_inspector_hook_callback() { + return undo_redo_callbacks; +} + +void EditorData::add_move_array_element_function(const StringName &p_class, Callable p_callable) { + move_element_functions.insert(p_class, p_callable); +} + +void EditorData::remove_move_array_element_function(const StringName &p_class) { + move_element_functions.erase(p_class); +} + +Callable EditorData::get_move_array_element_function(const StringName &p_class) const { + if (move_element_functions.has(p_class)) { + return move_element_functions[p_class]; + } + return Callable(); +} + void EditorData::remove_editor_plugin(EditorPlugin *p_plugin) { p_plugin->undo_redo = nullptr; editor_plugins.erase(p_plugin); @@ -468,24 +485,25 @@ void EditorData::add_custom_type(const String &p_type, const String &p_inherits, custom_types[p_inherits].push_back(ct); } -Object *EditorData::instance_custom_type(const String &p_type, const String &p_inherits) { +Variant EditorData::instance_custom_type(const String &p_type, const String &p_inherits) { if (get_custom_types().has(p_inherits)) { for (int i = 0; i < get_custom_types()[p_inherits].size(); i++) { if (get_custom_types()[p_inherits][i].name == p_type) { Ref<Script> script = get_custom_types()[p_inherits][i].script; - Object *ob = ClassDB::instance(p_inherits); - ERR_FAIL_COND_V(!ob, nullptr); - if (ob->is_class("Node")) { - ob->call("set_name", p_type); + Variant ob = ClassDB::instantiate(p_inherits); + ERR_FAIL_COND_V(!ob, Variant()); + Node *n = Object::cast_to<Node>(ob); + if (n) { + n->set_name(p_type); } - ob->set_script(script); + ((Object *)ob)->set_script(script); return ob; } } } - return nullptr; + return Variant(); } void EditorData::remove_custom_type(const String &p_type) { @@ -509,6 +527,7 @@ int EditorData::add_edited_scene(int p_at_pos) { EditedScene es; es.root = nullptr; es.path = String(); + es.file_modified_time = 0; es.history_current = -1; es.version = 0; es.live_edit_root = NodePath(String("/root")); @@ -535,7 +554,7 @@ void EditorData::remove_scene(int p_idx) { ERR_FAIL_INDEX(p_idx, edited_scene.size()); if (edited_scene[p_idx].root) { for (int i = 0; i < editor_plugins.size(); i++) { - editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_filename()); + editor_plugins[i]->notify_scene_closed(edited_scene[p_idx].root->get_scene_file_path()); } memdelete(edited_scene[p_idx].root); @@ -547,6 +566,10 @@ void EditorData::remove_scene(int p_idx) { current_edited_scene--; } + if (edited_scene[p_idx].path != String()) { + ScriptEditor::get_singleton()->close_builtin_scripts_from_scene(edited_scene[p_idx].path); + } + edited_scene.remove(p_idx); } @@ -560,7 +583,7 @@ bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, Set<String> if (p_node == p_root) { ss = p_node->get_scene_inherited_state(); - } else if (p_node->get_filename() != String()) { + } else if (p_node->get_scene_file_path() != String()) { ss = p_node->get_scene_instance_state(); } @@ -599,7 +622,7 @@ bool EditorData::check_and_update_scene(int p_idx) { if (must_reload) { Ref<PackedScene> pscene; - pscene.instance(); + pscene.instantiate(); EditorProgress ep("update_scene", TTR("Updating Scene"), 2); ep.step(TTR("Storing local changes..."), 0); @@ -607,25 +630,25 @@ bool EditorData::check_and_update_scene(int p_idx) { Error err = pscene->pack(edited_scene[p_idx].root); ERR_FAIL_COND_V(err != OK, false); ep.step(TTR("Updating scene..."), 1); - Node *new_scene = pscene->instance(PackedScene::GEN_EDIT_STATE_MAIN); + Node *new_scene = pscene->instantiate(PackedScene::GEN_EDIT_STATE_MAIN); ERR_FAIL_COND_V(!new_scene, false); //transfer selection List<Node *> new_selection; - for (List<Node *>::Element *E = edited_scene.write[p_idx].selection.front(); E; E = E->next()) { - NodePath p = edited_scene[p_idx].root->get_path_to(E->get()); + for (const Node *E : edited_scene.write[p_idx].selection) { + NodePath p = edited_scene[p_idx].root->get_path_to(E); Node *new_node = new_scene->get_node(p); if (new_node) { new_selection.push_back(new_node); } } - new_scene->set_filename(edited_scene[p_idx].root->get_filename()); + new_scene->set_scene_file_path(edited_scene[p_idx].root->get_scene_file_path()); memdelete(edited_scene[p_idx].root); edited_scene.write[p_idx].root = new_scene; - if (new_scene->get_filename() != "") { - edited_scene.write[p_idx].path = new_scene->get_filename(); + if (new_scene->get_scene_file_path() != "") { + edited_scene.write[p_idx].path = new_scene->get_scene_file_path(); } edited_scene.write[p_idx].selection = new_selection; @@ -659,12 +682,16 @@ void EditorData::set_edited_scene_root(Node *p_root) { ERR_FAIL_INDEX(current_edited_scene, edited_scene.size()); edited_scene.write[current_edited_scene].root = p_root; if (p_root) { - if (p_root->get_filename() != "") { - edited_scene.write[current_edited_scene].path = p_root->get_filename(); + if (p_root->get_scene_file_path() != "") { + edited_scene.write[current_edited_scene].path = p_root->get_scene_file_path(); } else { - p_root->set_filename(edited_scene[current_edited_scene].path); + p_root->set_scene_file_path(edited_scene[current_edited_scene].path); } } + + if (edited_scene[current_edited_scene].path != "") { + edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path); + } } int EditorData::get_edited_scene_count() const { @@ -696,6 +723,21 @@ uint64_t EditorData::get_scene_version(int p_idx) const { return edited_scene[p_idx].version; } +void EditorData::set_scene_modified_time(int p_idx, uint64_t p_time) { + if (p_idx == -1) { + p_idx = current_edited_scene; + } + + ERR_FAIL_INDEX(p_idx, edited_scene.size()); + + edited_scene.write[p_idx].file_modified_time = p_time; +} + +uint64_t EditorData::get_scene_modified_time(int p_idx) const { + ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), 0); + return edited_scene[p_idx].file_modified_time; +} + String EditorData::get_scene_type(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String()); if (!edited_scene[p_idx].root) { @@ -722,7 +764,7 @@ Ref<Script> EditorData::get_scene_root_script(int p_idx) const { Ref<Script> s = edited_scene[p_idx].root->get_script(); if (!s.is_valid() && edited_scene[p_idx].root->get_child_count()) { Node *n = edited_scene[p_idx].root->get_child(0); - while (!s.is_valid() && n && n->get_filename() == String()) { + while (!s.is_valid() && n && n->get_scene_file_path() == String()) { s = n->get_script(); n = n->get_parent(); } @@ -730,20 +772,36 @@ Ref<Script> EditorData::get_scene_root_script(int p_idx) const { return s; } -String EditorData::get_scene_title(int p_idx) const { +String EditorData::get_scene_title(int p_idx, bool p_always_strip_extension) const { ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String()); if (!edited_scene[p_idx].root) { return TTR("[empty]"); } - if (edited_scene[p_idx].root->get_filename() == "") { + if (edited_scene[p_idx].root->get_scene_file_path() == "") { return TTR("[unsaved]"); } - bool show_ext = EDITOR_DEF("interface/scene_tabs/show_extension", false); - String name = edited_scene[p_idx].root->get_filename().get_file(); - if (!show_ext) { - name = name.get_basename(); + + const String filename = edited_scene[p_idx].root->get_scene_file_path().get_file(); + const String basename = filename.get_basename(); + + if (p_always_strip_extension) { + return basename; + } + + // Return the filename including the extension if there's ambiguity (e.g. both `foo.tscn` and `foo.scn` are being edited). + for (int i = 0; i < edited_scene.size(); i++) { + if (i == p_idx) { + // Don't compare the edited scene against itself. + continue; + } + + if (edited_scene[i].root && basename == edited_scene[i].root->get_scene_file_path().get_file().get_basename()) { + return filename; + } } - return name; + + // Else, return just the basename as there's no ambiguity. + return basename; } void EditorData::set_scene_path(int p_idx, const String &p_path) { @@ -753,17 +811,17 @@ void EditorData::set_scene_path(int p_idx, const String &p_path) { if (!edited_scene[p_idx].root) { return; } - edited_scene[p_idx].root->set_filename(p_path); + edited_scene[p_idx].root->set_scene_file_path(p_path); } String EditorData::get_scene_path(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, edited_scene.size(), String()); if (edited_scene[p_idx].root) { - if (edited_scene[p_idx].root->get_filename() == "") { - edited_scene[p_idx].root->set_filename(edited_scene[p_idx].path); + if (edited_scene[p_idx].root->get_scene_file_path() == "") { + edited_scene[p_idx].root->set_scene_file_path(edited_scene[p_idx].path); } else { - return edited_scene[p_idx].root->get_filename(); + return edited_scene[p_idx].root->get_scene_file_path(); } } @@ -802,8 +860,8 @@ Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, p_history->history = es.history_stored; p_selection->clear(); - for (List<Node *>::Element *E = es.selection.front(); E; E = E->next()) { - p_selection->add_node(E->get()); + for (Node *E : es.selection) { + p_selection->add_node(E); } set_editor_states(es.editor_states); @@ -867,18 +925,18 @@ StringName EditorData::script_class_get_base(const String &p_class) const { return script->get_language()->get_global_class_name(base_script->get_path()); } -Object *EditorData::script_class_instance(const String &p_class) { +Variant EditorData::script_class_instance(const String &p_class) { if (ScriptServer::is_global_class(p_class)) { - Object *obj = ClassDB::instance(ScriptServer::get_global_class_native_base(p_class)); + Variant obj = ClassDB::instantiate(ScriptServer::get_global_class_native_base(p_class)); if (obj) { Ref<Script> script = script_class_load_script(p_class); if (script.is_valid()) { - obj->set_script(script); + ((Object *)obj)->set_script(script); } return obj; } } - return nullptr; + return Variant(); } Ref<Script> EditorData::script_class_load_script(const String &p_class) const { @@ -925,12 +983,20 @@ void EditorData::script_class_save_icon_paths() { _script_class_icon_paths.get_key_list(&keys); Dictionary d; - for (List<StringName>::Element *E = keys.front(); E; E = E->next()) { - if (ScriptServer::is_global_class(E->get())) { - d[E->get()] = _script_class_icon_paths[E->get()]; + for (const StringName &E : keys) { + if (ScriptServer::is_global_class(E)) { + d[E] = _script_class_icon_paths[E]; } } + Dictionary old; + if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) { + old = ProjectSettings::get_singleton()->get("_global_script_class_icons"); + } + if ((!old.is_empty() || d.is_empty()) && d.hash() == old.hash()) { + return; + } + if (d.is_empty()) { if (ProjectSettings::get_singleton()->has_setting("_global_script_class_icons")) { ProjectSettings::get_singleton()->clear("_global_script_class_icons"); @@ -949,8 +1015,8 @@ void EditorData::script_class_load_icon_paths() { List<Variant> keys; d.get_key_list(&keys); - for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { - String name = E->get().operator String(); + for (const Variant &E : keys) { + String name = E.operator String(); _script_class_icon_paths[name] = d[name]; String path = ScriptServer::get_global_class_path(name); @@ -991,8 +1057,8 @@ void EditorSelection::add_node(Node *p_node) { changed = true; nl_changed = true; Object *meta = nullptr; - for (List<Object *>::Element *E = editor_plugins.front(); E; E = E->next()) { - meta = E->get()->call("_get_editor_data", p_node); + for (Object *E : editor_plugins) { + meta = E->call("_get_editor_data", p_node); if (meta) { break; } @@ -1001,7 +1067,7 @@ void EditorSelection::add_node(Node *p_node) { p_node->connect("tree_exiting", callable_mp(this, &EditorSelection::_node_removed), varray(p_node), CONNECT_ONESHOT); - //emit_signal("selection_changed"); + //emit_signal(SNAME("selection_changed")); } void EditorSelection::remove_node(Node *p_node) { @@ -1019,7 +1085,7 @@ void EditorSelection::remove_node(Node *p_node) { } selection.erase(p_node); p_node->disconnect("tree_exiting", callable_mp(this, &EditorSelection::_node_removed)); - //emit_signal("selection_changed"); + //emit_signal(SNAME("selection_changed")); } bool EditorSelection::is_selected(Node *p_node) const { @@ -1029,8 +1095,8 @@ bool EditorSelection::is_selected(Node *p_node) const { Array EditorSelection::_get_transformable_selected_nodes() { Array ret; - for (List<Node *>::Element *E = selected_node_list.front(); E; E = E->next()) { - ret.push_back(E->get()); + for (const Node *E : selected_node_list) { + ret.push_back(E); } return ret; @@ -1039,8 +1105,8 @@ Array EditorSelection::_get_transformable_selected_nodes() { TypedArray<Node> EditorSelection::get_selected_nodes() { TypedArray<Node> ret; - for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) { - ret.push_back(E->key()); + for (const KeyValue<Node *, Object *> &E : selection) { + ret.push_back(E.key); } return ret; @@ -1067,8 +1133,8 @@ void EditorSelection::_update_nl() { selected_node_list.clear(); - for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) { - Node *parent = E->key(); + for (const KeyValue<Node *, Object *> &E : selection) { + Node *parent = E.key; parent = parent->get_parent(); bool skip = false; while (parent) { @@ -1082,7 +1148,7 @@ void EditorSelection::_update_nl() { if (skip) { continue; } - selected_node_list.push_back(E->key()); + selected_node_list.push_back(E.key); } nl_changed = true; @@ -1097,12 +1163,12 @@ void EditorSelection::update() { changed = false; if (!emitted) { emitted = true; - call_deferred("_emit_change"); + call_deferred(SNAME("_emit_change")); } } void EditorSelection::_emit_change() { - emit_signal("selection_changed"); + emit_signal(SNAME("selection_changed")); emitted = false; } @@ -1117,8 +1183,8 @@ List<Node *> &EditorSelection::get_selected_node_list() { List<Node *> EditorSelection::get_full_selected_node_list() { List<Node *> node_list; - for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) { - node_list.push_back(E->key()); + for (const KeyValue<Node *, Object *> &E : selection) { + node_list.push_back(E.key); } return node_list; |