From a3309215c200dff1cb8930cbdf83dbc0cff6d491 Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 28 Feb 2022 01:57:34 +0100 Subject: [PATCH] Improve handling of custom types --- doc/classes/EditorPlugin.xml | 1 + editor/create_dialog.cpp | 4 ++-- editor/editor_data.cpp | 26 ++++++++++++++++++++++++++ editor/editor_data.h | 3 +++ editor/editor_inspector.cpp | 10 ++++++++-- editor/editor_node.cpp | 12 ++---------- editor/script_create_dialog.cpp | 12 ++++++++++-- 7 files changed, 52 insertions(+), 16 deletions(-) diff --git a/doc/classes/EditorPlugin.xml b/doc/classes/EditorPlugin.xml index fdd3807b698b..9271e384872b 100644 --- a/doc/classes/EditorPlugin.xml +++ b/doc/classes/EditorPlugin.xml @@ -406,6 +406,7 @@ When a given node or resource is selected, the base type will be instantiated (e.g. "Node3D", "Control", "Resource"), then the script will be loaded and set to this object. You can use the virtual method [method _handles] to check if your custom object is being edited by checking the script or using the [code]is[/code] keyword. During run-time, this will be a simple object with a script so this function does not need to be called then. + [b]Note:[/b] Custom types added this way are not true classes. They are just a helper to create a node with specific script. diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index 8ccfda1145cf..2d70934c9c83 100644 --- a/editor/create_dialog.cpp +++ b/editor/create_dialog.cpp @@ -665,7 +665,7 @@ void CreateDialog::_save_and_update_favorite_list() { for (int i = 0; i < favorite_list.size(); i++) { String l = favorite_list[i]; String name = l.get_slicec(' ', 0); - if (!(ClassDB::class_exists(name) || ScriptServer::is_global_class(name))) { + if (!EditorNode::get_editor_data().is_type_recognized(name)) { continue; } f->store_line(l); @@ -692,7 +692,7 @@ void CreateDialog::_load_favorites_and_history() { String l = f->get_line().strip_edges(); String name = l.get_slicec(' ', 0); - if ((ClassDB::class_exists(name) || ScriptServer::is_global_class(name)) && !_is_class_disabled_by_feature_profile(name)) { + if (EditorNode::get_editor_data().is_type_recognized(name) && !_is_class_disabled_by_feature_profile(name)) { recent->add_item(l, EditorNode::get_singleton()->get_class_icon(name, icon_fallback)); } } diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index 231ae198d207..227e1b28e2b2 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -509,6 +509,32 @@ Variant EditorData::instance_custom_type(const String &p_type, const String &p_i return Variant(); } +const EditorData::CustomType *EditorData::get_custom_type_by_name(const String &p_type) const { + for (const KeyValue> &E : custom_types) { + for (const CustomType &F : E.value) { + if (F.name == p_type) { + return &F; + } + } + } + return nullptr; +} + +const EditorData::CustomType *EditorData::get_custom_type_by_path(const String &p_path) const { + for (const KeyValue> &E : custom_types) { + for (const CustomType &F : E.value) { + if (F.script->get_path() == p_path) { + return &F; + } + } + } + return nullptr; +} + +bool EditorData::is_type_recognized(const String &p_type) const { + return ClassDB::class_exists(p_type) || ScriptServer::is_global_class(p_type) || get_custom_type_by_name(p_type); +} + void EditorData::remove_custom_type(const String &p_type) { for (KeyValue> &E : custom_types) { for (int i = 0; i < E.value.size(); i++) { diff --git a/editor/editor_data.h b/editor/editor_data.h index 1da188c546c2..4f1740d4f073 100644 --- a/editor/editor_data.h +++ b/editor/editor_data.h @@ -184,6 +184,9 @@ class EditorData { Variant instance_custom_type(const String &p_type, const String &p_inherits); void remove_custom_type(const String &p_type); const HashMap> &get_custom_types() const { return custom_types; } + const CustomType *get_custom_type_by_name(const String &p_name) const; + const CustomType *get_custom_type_by_path(const String &p_path) const; + bool is_type_recognized(const String &p_type) const; void instantiate_object_properties(Object *p_object); diff --git a/editor/editor_inspector.cpp b/editor/editor_inspector.cpp index bdf1c314f862..5bd08d1e633a 100644 --- a/editor/editor_inspector.cpp +++ b/editor/editor_inspector.cpp @@ -2738,7 +2738,7 @@ void EditorInspector::update_tree() { doc_name = p.name; // Set the category icon. - if (!ClassDB::class_exists(type) && !ScriptServer::is_global_class(type) && p.hint_string.length() && FileAccess::exists(p.hint_string)) { + if (!EditorNode::get_editor_data().is_type_recognized(type) && p.hint_string.length() && FileAccess::exists(p.hint_string)) { // If we have a category inside a script, search for the first script with a valid icon. Ref