summaryrefslogtreecommitdiff
path: root/editor/editor_data.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor/editor_data.cpp')
-rw-r--r--editor/editor_data.cpp469
1 files changed, 238 insertions, 231 deletions
diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp
index 213c3f5631..e9e3320a3d 100644
--- a/editor/editor_data.cpp
+++ b/editor/editor_data.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -31,19 +31,20 @@
#include "editor_data.h"
#include "core/config/project_settings.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 "editor/editor_node.h"
+#include "editor/editor_plugin.h"
+#include "editor/plugins/script_editor_plugin.h"
#include "scene/resources/packed_scene.h"
-void EditorHistory::cleanup_history() {
+void EditorSelectionHistory::cleanup_history() {
for (int i = 0; i < history.size(); i++) {
bool fail = false;
for (int j = 0; j < history[i].path.size(); j++) {
if (!history[i].path[j].ref.is_null()) {
+ // Reference is not null - object still alive.
continue;
}
@@ -51,127 +52,100 @@ void EditorHistory::cleanup_history() {
if (obj) {
Node *n = Object::cast_to<Node>(obj);
if (n && n->is_inside_tree()) {
+ // Node valid and inside tree - object still alive.
continue;
}
- if (!n) { // Possibly still alive
+ if (!n) {
+ // Node possibly still alive.
continue;
}
- }
-
- if (j <= history[i].level) {
- //before or equal level, complete fail
- fail = true;
- } else {
- //after level, clip
- history.write[i].path.resize(j);
- }
+ } // Else: object not valid - not alive.
+ fail = true;
break;
}
if (fail) {
- history.remove(i);
+ history.remove_at(i);
i--;
}
}
- if (current >= history.size()) {
- current = history.size() - 1;
+ if (current_elem_idx >= history.size()) {
+ current_elem_idx = history.size() - 1;
}
}
-void EditorHistory::_add_object(ObjectID p_object, const String &p_property, int p_level_change, bool p_inspector_only) {
+void EditorSelectionHistory::add_object(ObjectID p_object, const String &p_property, bool p_inspector_only) {
Object *obj = ObjectDB::get_instance(p_object);
ERR_FAIL_COND(!obj);
- Reference *r = Object::cast_to<Reference>(obj);
- Obj o;
+ RefCounted *r = Object::cast_to<RefCounted>(obj);
+ _Object o;
if (r) {
- o.ref = REF(r);
+ o.ref = Ref<RefCounted>(r);
}
o.object = p_object;
o.property = p_property;
o.inspector_only = p_inspector_only;
- History h;
-
- bool has_prev = current >= 0 && current < history.size();
+ bool has_prev = current_elem_idx >= 0 && current_elem_idx < history.size();
if (has_prev) {
- history.resize(current + 1); //clip history to next
+ history.resize(current_elem_idx + 1); // Clip history to next.
}
- if (p_property != "" && has_prev) {
- //add a sub property
- History &pr = history.write[current];
- h = pr;
+ HistoryElement h;
+ if (!p_property.is_empty() && has_prev) {
+ // Add a sub property.
+ HistoryElement &prev_element = history.write[current_elem_idx];
+ h = prev_element;
h.path.resize(h.level + 1);
h.path.push_back(o);
h.level++;
- } else if (p_level_change != -1 && has_prev) {
- //add a sub property
- History &pr = history.write[current];
- h = pr;
- ERR_FAIL_INDEX(p_level_change, h.path.size());
- h.level = p_level_change;
+
} else {
- //add a new node
+ // Create a new history item.
h.path.push_back(o);
h.level = 0;
}
history.push_back(h);
- current++;
-}
-
-void EditorHistory::add_object_inspector_only(ObjectID p_object) {
- _add_object(p_object, "", -1, true);
-}
-
-void EditorHistory::add_object(ObjectID p_object) {
- _add_object(p_object, "", -1);
-}
-
-void EditorHistory::add_object(ObjectID p_object, const String &p_subprop) {
- _add_object(p_object, p_subprop, -1);
-}
-
-void EditorHistory::add_object(ObjectID p_object, int p_relevel) {
- _add_object(p_object, "", p_relevel);
+ current_elem_idx++;
}
-int EditorHistory::get_history_len() {
+int EditorSelectionHistory::get_history_len() {
return history.size();
}
-int EditorHistory::get_history_pos() {
- return current;
+int EditorSelectionHistory::get_history_pos() {
+ return current_elem_idx;
}
-bool EditorHistory::is_history_obj_inspector_only(int p_obj) const {
+bool EditorSelectionHistory::is_history_obj_inspector_only(int p_obj) const {
ERR_FAIL_INDEX_V(p_obj, history.size(), false);
ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), false);
return history[p_obj].path[history[p_obj].level].inspector_only;
}
-ObjectID EditorHistory::get_history_obj(int p_obj) const {
+ObjectID EditorSelectionHistory::get_history_obj(int p_obj) const {
ERR_FAIL_INDEX_V(p_obj, history.size(), ObjectID());
ERR_FAIL_INDEX_V(history[p_obj].level, history[p_obj].path.size(), ObjectID());
return history[p_obj].path[history[p_obj].level].object;
}
-bool EditorHistory::is_at_beginning() const {
- return current <= 0;
+bool EditorSelectionHistory::is_at_beginning() const {
+ return current_elem_idx <= 0;
}
-bool EditorHistory::is_at_end() const {
- return ((current + 1) >= history.size());
+bool EditorSelectionHistory::is_at_end() const {
+ return ((current_elem_idx + 1) >= history.size());
}
-bool EditorHistory::next() {
+bool EditorSelectionHistory::next() {
cleanup_history();
- if ((current + 1) < history.size()) {
- current++;
+ if ((current_elem_idx + 1) < history.size()) {
+ current_elem_idx++;
} else {
return false;
}
@@ -179,11 +153,11 @@ bool EditorHistory::next() {
return true;
}
-bool EditorHistory::previous() {
+bool EditorSelectionHistory::previous() {
cleanup_history();
- if (current > 0) {
- current--;
+ if (current_elem_idx > 0) {
+ current_elem_idx--;
} else {
return false;
}
@@ -191,76 +165,63 @@ bool EditorHistory::previous() {
return true;
}
-bool EditorHistory::is_current_inspector_only() const {
- if (current < 0 || current >= history.size()) {
+bool EditorSelectionHistory::is_current_inspector_only() const {
+ if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
return false;
}
- const History &h = history[current];
+ const HistoryElement &h = history[current_elem_idx];
return h.path[h.level].inspector_only;
}
-ObjectID EditorHistory::get_current() {
- if (current < 0 || current >= history.size()) {
+ObjectID EditorSelectionHistory::get_current() {
+ if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
return ObjectID();
}
- History &h = history.write[current];
- Object *obj = ObjectDB::get_instance(h.path[h.level].object);
- if (!obj) {
- return ObjectID();
- }
-
- return obj->get_instance_id();
+ Object *obj = ObjectDB::get_instance(get_history_obj(current_elem_idx));
+ return obj ? obj->get_instance_id() : ObjectID();
}
-int EditorHistory::get_path_size() const {
- if (current < 0 || current >= history.size()) {
+int EditorSelectionHistory::get_path_size() const {
+ if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
return 0;
}
- const History &h = history[current];
- return h.path.size();
+ return history[current_elem_idx].path.size();
}
-ObjectID EditorHistory::get_path_object(int p_index) const {
- if (current < 0 || current >= history.size()) {
+ObjectID EditorSelectionHistory::get_path_object(int p_index) const {
+ if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
return ObjectID();
}
- const History &h = history[current];
-
- ERR_FAIL_INDEX_V(p_index, h.path.size(), ObjectID());
-
- Object *obj = ObjectDB::get_instance(h.path[p_index].object);
- if (!obj) {
- return ObjectID();
- }
+ ERR_FAIL_INDEX_V(p_index, history[current_elem_idx].path.size(), ObjectID());
- return obj->get_instance_id();
+ Object *obj = ObjectDB::get_instance(history[current_elem_idx].path[p_index].object);
+ return obj ? obj->get_instance_id() : ObjectID();
}
-String EditorHistory::get_path_property(int p_index) const {
- if (current < 0 || current >= history.size()) {
+String EditorSelectionHistory::get_path_property(int p_index) const {
+ if (current_elem_idx < 0 || current_elem_idx >= history.size()) {
return "";
}
- const History &h = history[current];
-
- ERR_FAIL_INDEX_V(p_index, h.path.size(), "");
-
- return h.path[p_index].property;
+ ERR_FAIL_INDEX_V(p_index, history[current_elem_idx].path.size(), "");
+ return history[current_elem_idx].path[p_index].property;
}
-void EditorHistory::clear() {
+void EditorSelectionHistory::clear() {
history.clear();
- current = -1;
+ current_elem_idx = -1;
}
-EditorHistory::EditorHistory() {
- current = -1;
+EditorSelectionHistory::EditorSelectionHistory() {
+ current_elem_idx = -1;
}
+////////////////////////////////////////////////////////////
+
EditorPlugin *EditorData::get_editor(Object *p_object) {
// We need to iterate backwards so that we can check user-created plugins first.
// Otherwise, it would not be possible for plugins to handle CanvasItem and Spatial nodes.
@@ -299,13 +260,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);
}
@@ -404,9 +365,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();
@@ -426,6 +387,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);
@@ -464,7 +452,7 @@ Variant EditorData::instance_custom_type(const String &p_type, const String &p_i
if (get_custom_types()[p_inherits][i].name == p_type) {
Ref<Script> script = get_custom_types()[p_inherits][i].script;
- Variant ob = ClassDB::instance(p_inherits);
+ Variant ob = ClassDB::instantiate(p_inherits);
ERR_FAIL_COND_V(!ob, Variant());
Node *n = Object::cast_to<Node>(ob);
if (n) {
@@ -480,12 +468,12 @@ Variant EditorData::instance_custom_type(const String &p_type, const String &p_i
}
void EditorData::remove_custom_type(const String &p_type) {
- for (Map<String, Vector<CustomType>>::Element *E = custom_types.front(); E; E = E->next()) {
- for (int i = 0; i < E->get().size(); i++) {
- if (E->get()[i].name == p_type) {
- E->get().remove(i);
- if (E->get().is_empty()) {
- custom_types.erase(E->key());
+ for (KeyValue<String, Vector<CustomType>> &E : custom_types) {
+ for (int i = 0; i < E.value.size(); i++) {
+ if (E.value[i].name == p_type) {
+ E.value.remove_at(i);
+ if (E.value.is_empty()) {
+ custom_types.erase(E.key);
}
return;
}
@@ -493,6 +481,21 @@ void EditorData::remove_custom_type(const String &p_type) {
}
}
+void EditorData::instantiate_object_properties(Object *p_object) {
+ ERR_FAIL_NULL(p_object);
+ // Check if any Object-type property should be instantiated.
+ List<PropertyInfo> pinfo;
+ p_object->get_property_list(&pinfo);
+
+ for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
+ PropertyInfo pi = E->get();
+ if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) {
+ Object *prop = ClassDB::instantiate(pi.class_name);
+ p_object->set(pi.name, prop);
+ }
+ }
+}
+
int EditorData::add_edited_scene(int p_at_pos) {
if (p_at_pos < 0) {
p_at_pos = edited_scene.size();
@@ -527,10 +530,11 @@ 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);
+ edited_scene.write[p_idx].root = nullptr;
}
if (current_edited_scene > p_idx) {
@@ -539,20 +543,19 @@ void EditorData::remove_scene(int p_idx) {
current_edited_scene--;
}
- edited_scene.remove(p_idx);
-}
+ if (!edited_scene[p_idx].path.is_empty()) {
+ ScriptEditor::get_singleton()->close_builtin_scripts_from_scene(edited_scene[p_idx].path);
+ }
-bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, Set<String> &checked_paths) {
- /*
- if (p_root!=p_node && p_node->get_owner()!=p_root && !p_root->is_editable_instance(p_node->get_owner()))
- return false;
- */
+ edited_scene.remove_at(p_idx);
+}
+bool EditorData::_find_updated_instances(Node *p_root, Node *p_node, HashSet<String> &checked_paths) {
Ref<SceneState> ss;
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().is_empty()) {
ss = p_node->get_scene_instance_state();
}
@@ -585,39 +588,39 @@ bool EditorData::check_and_update_scene(int p_idx) {
return false;
}
- Set<String> checked_scenes;
+ HashSet<String> checked_scenes;
bool must_reload = _find_updated_instances(edited_scene[p_idx].root, edited_scene[p_idx].root, checked_scenes);
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);
- //pack first, so it stores diffs to previous version of saved scene
+ // Pack first, so it stores diffs to previous version of saved scene.
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
+ // 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().is_empty()) {
+ edited_scene.write[p_idx].path = new_scene->get_scene_file_path();
}
edited_scene.write[p_idx].selection = new_selection;
@@ -634,7 +637,6 @@ int EditorData::get_edited_scene() const {
void EditorData::set_edited_scene(int p_idx) {
ERR_FAIL_INDEX(p_idx, edited_scene.size());
current_edited_scene = p_idx;
- //swap
}
Node *EditorData::get_edited_scene_root(int p_idx) {
@@ -651,14 +653,14 @@ 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().is_empty()) {
+ 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 != "") {
+ if (!edited_scene[current_edited_scene].path.is_empty()) {
edited_scene.write[current_edited_scene].file_modified_time = FileAccess::get_modified_time(edited_scene[current_edited_scene].path);
}
}
@@ -720,7 +722,7 @@ void EditorData::move_edited_scene_to_index(int p_idx) {
ERR_FAIL_INDEX(p_idx, edited_scene.size());
EditedScene es = edited_scene[current_edited_scene];
- edited_scene.remove(current_edited_scene);
+ edited_scene.remove_at(current_edited_scene);
edited_scene.insert(p_idx, es);
current_edited_scene = p_idx;
}
@@ -733,7 +735,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().is_empty()) {
s = n->get_script();
n = n->get_parent();
}
@@ -741,20 +743,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().is_empty()) {
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 name;
+
+ // 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;
+ }
+ }
+
+ // Else, return just the basename as there's no ambiguity.
+ return basename;
}
void EditorData::set_scene_path(int p_idx, const String &p_path) {
@@ -764,17 +782,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().is_empty()) {
+ 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();
}
}
@@ -793,28 +811,28 @@ NodePath EditorData::get_edited_scene_live_edit_root() {
return edited_scene[current_edited_scene].live_edit_root;
}
-void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history, const Dictionary &p_custom) {
+void EditorData::save_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history, const Dictionary &p_custom) {
ERR_FAIL_INDEX(current_edited_scene, edited_scene.size());
EditedScene &es = edited_scene.write[current_edited_scene];
es.selection = p_selection->get_full_selected_node_list();
- es.history_current = p_history->current;
+ es.history_current = p_history->current_elem_idx;
es.history_stored = p_history->history;
es.editor_states = get_editor_states();
es.custom_state = p_custom;
}
-Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorHistory *p_history) {
+Dictionary EditorData::restore_edited_scene_state(EditorSelection *p_selection, EditorSelectionHistory *p_history) {
ERR_FAIL_INDEX_V(current_edited_scene, edited_scene.size(), Dictionary());
- EditedScene &es = edited_scene.write[current_edited_scene];
+ const EditedScene &es = edited_scene.write[current_edited_scene];
- p_history->current = es.history_current;
+ p_history->current_elem_idx = es.history_current;
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);
@@ -846,17 +864,13 @@ bool EditorData::script_class_is_parent(const String &p_class, const String &p_i
if (!ScriptServer::is_global_class(p_class)) {
return false;
}
- String base = script_class_get_base(p_class);
- Ref<Script> script = script_class_load_script(p_class);
- Ref<Script> base_script = script->get_base_script();
- while (p_inherits != base) {
+ String base = p_class;
+ while (base != p_inherits) {
if (ClassDB::class_exists(base)) {
return ClassDB::is_parent_class(base, p_inherits);
} else if (ScriptServer::is_global_class(base)) {
- base = script_class_get_base(base);
- } else if (base_script.is_valid()) {
- return ClassDB::is_parent_class(base_script->get_instance_base_type(), p_inherits);
+ base = ScriptServer::get_global_class_base(base);
} else {
return false;
}
@@ -880,7 +894,7 @@ StringName EditorData::script_class_get_base(const String &p_class) const {
Variant EditorData::script_class_instance(const String &p_class) {
if (ScriptServer::is_global_class(p_class)) {
- Variant 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()) {
@@ -932,13 +946,10 @@ void EditorData::script_class_set_name(const String &p_path, const StringName &p
}
void EditorData::script_class_save_icon_paths() {
- List<StringName> keys;
- _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 KeyValue<StringName, String> &E : _script_class_icon_paths) {
+ if (ScriptServer::is_global_class(E.key)) {
+ d[E.key] = E.value;
}
}
@@ -968,8 +979,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);
@@ -980,12 +991,11 @@ void EditorData::script_class_load_icon_paths() {
EditorData::EditorData() {
current_edited_scene = -1;
-
- //load_imported_scenes_from_globals();
script_class_load_icon_paths();
}
-///////////
+///////////////////////////////////////////////////////////////////////////////
+
void EditorSelection::_node_removed(Node *p_node) {
if (!selection.has(p_node)) {
return;
@@ -997,7 +1007,7 @@ void EditorSelection::_node_removed(Node *p_node) {
}
selection.erase(p_node);
changed = true;
- nl_changed = true;
+ node_list_changed = true;
}
void EditorSelection::add_node(Node *p_node) {
@@ -1008,10 +1018,10 @@ void EditorSelection::add_node(Node *p_node) {
}
changed = true;
- nl_changed = true;
+ node_list_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;
}
@@ -1019,52 +1029,29 @@ void EditorSelection::add_node(Node *p_node) {
selection[p_node] = meta;
p_node->connect("tree_exiting", callable_mp(this, &EditorSelection::_node_removed), varray(p_node), CONNECT_ONESHOT);
-
- //emit_signal("selection_changed");
}
void EditorSelection::remove_node(Node *p_node) {
ERR_FAIL_NULL(p_node);
-
if (!selection.has(p_node)) {
return;
}
changed = true;
- nl_changed = true;
+ node_list_changed = true;
Object *meta = selection[p_node];
if (meta) {
memdelete(meta);
}
selection.erase(p_node);
+
p_node->disconnect("tree_exiting", callable_mp(this, &EditorSelection::_node_removed));
- //emit_signal("selection_changed");
}
bool EditorSelection::is_selected(Node *p_node) const {
return selection.has(p_node);
}
-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());
- }
-
- return ret;
-}
-
-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());
- }
-
- return ret;
-}
-
void EditorSelection::_bind_methods() {
ClassDB::bind_method(D_METHOD("clear"), &EditorSelection::clear);
ClassDB::bind_method(D_METHOD("add_node", "node"), &EditorSelection::add_node);
@@ -1079,15 +1066,18 @@ void EditorSelection::add_editor_plugin(Object *p_object) {
editor_plugins.push_back(p_object);
}
-void EditorSelection::_update_nl() {
- if (!nl_changed) {
+void EditorSelection::_update_node_list() {
+ if (!node_list_changed) {
return;
}
selected_node_list.clear();
- for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
- Node *parent = E->key();
+ // If the selection does not have the parent of the selected node, then add the node to the node list.
+ // However, if the parent is already selected, then adding this node is redundant as
+ // it is included with the parent, so skip it.
+ for (const KeyValue<Node *, Object *> &E : selection) {
+ Node *parent = E.key;
parent = parent->get_parent();
bool skip = false;
while (parent) {
@@ -1101,14 +1091,14 @@ void EditorSelection::_update_nl() {
if (skip) {
continue;
}
- selected_node_list.push_back(E->key());
+ selected_node_list.push_back(E.key);
}
- nl_changed = true;
+ node_list_changed = true;
}
void EditorSelection::update() {
- _update_nl();
+ _update_node_list();
if (!changed) {
return;
@@ -1116,28 +1106,48 @@ 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;
}
+Array EditorSelection::_get_transformable_selected_nodes() {
+ Array ret;
+
+ for (const Node *E : selected_node_list) {
+ ret.push_back(E);
+ }
+
+ return ret;
+}
+
+TypedArray<Node> EditorSelection::get_selected_nodes() {
+ TypedArray<Node> ret;
+
+ for (const KeyValue<Node *, Object *> &E : selection) {
+ ret.push_back(E.key);
+ }
+
+ return ret;
+}
+
List<Node *> &EditorSelection::get_selected_node_list() {
if (changed) {
update();
} else {
- _update_nl();
+ _update_node_list();
}
return 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;
@@ -1145,17 +1155,14 @@ List<Node *> EditorSelection::get_full_selected_node_list() {
void EditorSelection::clear() {
while (!selection.is_empty()) {
- remove_node(selection.front()->key());
+ remove_node(selection.begin()->key);
}
changed = true;
- nl_changed = true;
+ node_list_changed = true;
}
EditorSelection::EditorSelection() {
- emitted = false;
- changed = false;
- nl_changed = false;
}
EditorSelection::~EditorSelection() {