From b3bc5aafc5dbe6559534bb1e19093e719afbf52c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Verschelde?= Date: Fri, 12 Jun 2020 13:16:14 +0200 Subject: Object: Add usage hint to instantiate Object properties in editor Fixes #36372 as Path2D/Path3D's `curve` property no longer uses a Curve instance as default value, but instead it gets a (unique) default Curve instance when created through the editor (CreateDialog). ClassDB gets a sanity check to ensure that we don't do the same mistake for other properties in the future, but instead use the dedicated property usage hint. Fixes #36372. Fixes #36650. Supersedes #36644 and #36656. Co-authored-by: Thakee Nathees Co-authored-by: simpuid --- editor/create_dialog.cpp | 49 +++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'editor') diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index 4bd7371c21..73468f8ce0 100644 --- a/editor/create_dialog.cpp +++ b/editor/create_dialog.cpp @@ -513,30 +513,45 @@ String CreateDialog::get_selected_type() { Object *CreateDialog::instance_selected() { TreeItem *selected = search_options->get_selected(); - if (selected) { - Variant md = selected->get_metadata(0); + if (!selected) { + return nullptr; + } - String custom; - if (md.get_type() != Variant::NIL) { - custom = md; - } + Variant md = selected->get_metadata(0); + String custom; + if (md.get_type() != Variant::NIL) { + custom = md; + } - if (custom != String()) { - if (ScriptServer::is_global_class(custom)) { - Object *obj = EditorNode::get_editor_data().script_class_instance(custom); - Node *n = Object::cast_to(obj); - if (n) { - n->set_name(custom); - } - return obj; + Object *obj = nullptr; + + if (!custom.empty()) { + if (ScriptServer::is_global_class(custom)) { + obj = EditorNode::get_editor_data().script_class_instance(custom); + Node *n = Object::cast_to(obj); + if (n) { + n->set_name(custom); } - return EditorNode::get_editor_data().instance_custom_type(selected->get_text(0), custom); + obj = n; } else { - return ClassDB::instance(selected->get_text(0)); + obj = EditorNode::get_editor_data().instance_custom_type(selected->get_text(0), custom); + } + } else { + obj = ClassDB::instance(selected->get_text(0)); + } + + // Check if any Object-type property should be instantiated. + List pinfo; + obj->get_property_list(&pinfo); + + for (List::Element *E = pinfo.front(); E; E = E->next()) { + if (E->get().type == Variant::OBJECT && E->get().usage & PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT) { + Object *prop = ClassDB::instance(E->get().class_name); + obj->set(E->get().name, prop); } } - return nullptr; + return obj; } void CreateDialog::_item_selected() { -- cgit v1.2.3