From 7a8b11ee14f34ca97e6d5023ef15b8abf4d59cda Mon Sep 17 00:00:00 2001 From: "Andrii Doroshenko (Xrayez)" Date: Thu, 22 Oct 2020 22:02:57 +0300 Subject: [PATCH] Refactor auto-instantiation of `Object` properties in editor Auto-instantiation is used by the create dialog, but should also be used by the editor inspector. This refactors object properties auto-instantiation into a dedicated method to be reused throughout editor (and possibly scripting). --- core/object/class_db.cpp | 3 ++- editor/create_dialog.cpp | 12 +----------- editor/editor_data.cpp | 15 +++++++++++++++ editor/editor_data.h | 2 ++ editor/editor_resource_picker.cpp | 2 ++ editor/property_editor.cpp | 6 ++++++ 6 files changed, 28 insertions(+), 12 deletions(-) diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index bbd3b7b8dea1..27fc308b5830 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -1632,7 +1632,8 @@ Variant ClassDB::class_get_default_property_value(const StringName &p_class, con // Some properties may have an instantiated Object as default value, // (like Path2D's `curve` used to have), but that's not a good practice. // Instead, those properties should use PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT - // to be auto-instantiated when created in the editor. + // to be auto-instantiated when created in the editor with the following method: + // EditorNode::get_editor_data().instantiate_object_properties(obj); if (var.get_type() == Variant::OBJECT) { Object *obj = var.get_validated_object(); if (obj) { diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index 6bf14df8a178..24b745e6994f 100644 --- a/editor/create_dialog.cpp +++ b/editor/create_dialog.cpp @@ -446,17 +446,7 @@ Variant CreateDialog::instance_selected() { } else { obj = ClassDB::instantiate(selected->get_text(0)); } - - // Check if any Object-type property should be instantiated. - List pinfo; - ((Object *)obj)->get_property_list(&pinfo); - - for (const PropertyInfo &pi : pinfo) { - if (pi.type == Variant::OBJECT && pi.usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) { - Object *prop = ClassDB::instantiate(pi.class_name); - ((Object *)obj)->set(pi.name, prop); - } - } + EditorNode::get_editor_data().instantiate_object_properties(obj); return obj; } diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index 390a2998d602..f6cd990a03e9 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -520,6 +520,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 pinfo; + p_object->get_property_list(&pinfo); + + for (List::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(); diff --git a/editor/editor_data.h b/editor/editor_data.h index 976d718b8e43..8714715d7f89 100644 --- a/editor/editor_data.h +++ b/editor/editor_data.h @@ -182,6 +182,8 @@ class EditorData { void remove_custom_type(const String &p_type); const Map> &get_custom_types() const { return custom_types; } + void instantiate_object_properties(Object *p_object); + int add_edited_scene(int p_at_pos); void move_edited_scene_index(int p_idx, int p_to_idx); void remove_scene(int p_idx); diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp index 6a6634d7e589..dc97514ed37f 100644 --- a/editor/editor_resource_picker.cpp +++ b/editor/editor_resource_picker.cpp @@ -375,6 +375,8 @@ void EditorResourcePicker::_edit_menu_cbk(int p_which) { Resource *resp = Object::cast_to(obj); ERR_BREAK(!resp); + EditorNode::get_editor_data().instantiate_object_properties(obj); + edited_resource = RES(resp); emit_signal(SNAME("resource_changed"), edited_resource); _update_resource(); diff --git a/editor/property_editor.cpp b/editor/property_editor.cpp index abe1bcf9e5a3..27970e3cd90c 100644 --- a/editor/property_editor.cpp +++ b/editor/property_editor.cpp @@ -269,7 +269,9 @@ void CustomPropertyEditor::_menu_option(int p_which) { res->call("set_instance_base_type", owner->get_class()); } + EditorNode::get_editor_data().instantiate_object_properties(obj); v = obj; + emit_signal(SNAME("variant_changed")); } break; @@ -1080,7 +1082,9 @@ void CustomPropertyEditor::_type_create_selected(int p_idx) { ERR_FAIL_COND(!obj); ERR_FAIL_COND(!Object::cast_to(obj)); + EditorNode::get_editor_data().instantiate_object_properties(obj); v = obj; + emit_signal(SNAME("variant_changed")); hide(); } @@ -1270,7 +1274,9 @@ void CustomPropertyEditor::_action_pressed(int p_which) { ERR_BREAK(!obj); ERR_BREAK(!Object::cast_to(obj)); + EditorNode::get_editor_data().instantiate_object_properties(obj); v = obj; + emit_signal(SNAME("variant_changed")); hide(); }