diff options
author | Andrii Doroshenko (Xrayez) <xrayez@gmail.com> | 2020-10-22 22:02:57 +0300 |
---|---|---|
committer | Andrii Doroshenko (Xrayez) <xrayez@gmail.com> | 2021-12-28 15:50:44 +0200 |
commit | 7a8b11ee14f34ca97e6d5023ef15b8abf4d59cda (patch) | |
tree | 14ea1d867c1043c0fb2f6a1d573e14cabc847c9b /editor | |
parent | 28174d531b7128f0281fc2b88da2f4962fd3513e (diff) |
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).
Diffstat (limited to 'editor')
-rw-r--r-- | editor/create_dialog.cpp | 12 | ||||
-rw-r--r-- | editor/editor_data.cpp | 15 | ||||
-rw-r--r-- | editor/editor_data.h | 2 | ||||
-rw-r--r-- | editor/editor_resource_picker.cpp | 2 | ||||
-rw-r--r-- | editor/property_editor.cpp | 6 |
5 files changed, 26 insertions, 11 deletions
diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index 6bf14df8a1..24b745e699 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<PropertyInfo> 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 390a2998d6..f6cd990a03 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<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(); diff --git a/editor/editor_data.h b/editor/editor_data.h index 976d718b8e..8714715d7f 100644 --- a/editor/editor_data.h +++ b/editor/editor_data.h @@ -182,6 +182,8 @@ public: void remove_custom_type(const String &p_type); const Map<String, Vector<CustomType>> &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 6a6634d7e5..dc97514ed3 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<Resource>(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 abe1bcf9e5..27970e3cd9 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<Resource>(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<Resource>(obj)); + EditorNode::get_editor_data().instantiate_object_properties(obj); v = obj; + emit_signal(SNAME("variant_changed")); hide(); } |