From cb66cf80f5eb66e34b417ba58fa61f603fabebd4 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Tue, 3 Aug 2021 16:21:05 +0300 Subject: [PATCH] Cache EditorResourcePicker's allowed types --- editor/editor_node.cpp | 2 ++ editor/editor_resource_picker.cpp | 42 ++++++++++++++++++++++++------- editor/editor_resource_picker.h | 4 +++ 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index c35746527a0..c2c260c5702 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -3811,6 +3811,8 @@ void EditorNode::register_editor_types() { void EditorNode::unregister_editor_types() { _init_callbacks.clear(); + + EditorResourcePicker::clear_caches(); } void EditorNode::stop_child_process() { diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp index 49463d043b2..775391885ea 100644 --- a/editor/editor_resource_picker.cpp +++ b/editor/editor_resource_picker.cpp @@ -38,6 +38,12 @@ #include "scene/main/viewport.h" #include "scene/resources/dynamic_font.h" +HashMap> EditorResourcePicker::allowed_types_cache; + +void EditorResourcePicker::clear_caches() { + allowed_types_cache.clear(); +} + void EditorResourcePicker::_update_resource() { preview_rect->set_texture(Ref()); assign_button->set_custom_minimum_size(Size2(1, 1)); @@ -464,17 +470,31 @@ void EditorResourcePicker::_get_allowed_types(bool p_with_convert, Set * String base = allowed_types[i].strip_edges(); p_vector->insert(base); - List inheriters; - - ClassDB::get_inheriters_from_class(base, &inheriters); - for (List::Element *E = inheriters.front(); E; E = E->next()) { - p_vector->insert(E->get()); - } - - for (List::Element *E = global_classes.front(); E; E = E->next()) { - if (EditorNode::get_editor_data().script_class_is_parent(E->get(), base)) { + // If we hit a familiar base type, take all the data from cache. + if (allowed_types_cache.has(base)) { + List allowed_subtypes = allowed_types_cache[base]; + for (List::Element *E = allowed_subtypes.front(); E; E = E->next()) { p_vector->insert(E->get()); } + } else { + List allowed_subtypes; + + List inheriters; + ClassDB::get_inheriters_from_class(base, &inheriters); + for (List::Element *E = inheriters.front(); E; E = E->next()) { + p_vector->insert(E->get()); + allowed_subtypes.push_back(E->get()); + } + + for (List::Element *E = global_classes.front(); E; E = E->next()) { + if (EditorNode::get_editor_data().script_class_is_parent(E->get(), base)) { + p_vector->insert(E->get()); + allowed_subtypes.push_back(E->get()); + } + } + + // Store the subtypes of the base type in the cache for future use. + allowed_types_cache[base] = allowed_subtypes; } if (p_with_convert) { @@ -710,6 +730,10 @@ void EditorResourcePicker::set_base_type(const String &p_base_type) { String class_str = (custom_class == StringName() ? edited_resource->get_class() : vformat("%s (%s)", custom_class, edited_resource->get_class())); WARN_PRINT(vformat("Value mismatch between the new base type of this EditorResourcePicker, '%s', and the type of the value it already has, '%s'.", base_type, class_str)); } + } else { + // Call the method to build the cache immediately. + Set allowed_types; + _get_allowed_types(false, &allowed_types); } } diff --git a/editor/editor_resource_picker.h b/editor/editor_resource_picker.h index e2b2e5f53dc..b39d6599986 100644 --- a/editor/editor_resource_picker.h +++ b/editor/editor_resource_picker.h @@ -40,6 +40,8 @@ class EditorResourcePicker : public HBoxContainer { GDCLASS(EditorResourcePicker, HBoxContainer); + static HashMap> allowed_types_cache; + String base_type; RES edited_resource; @@ -95,6 +97,8 @@ protected: void _notification(int p_what); public: + static void clear_caches(); + void set_base_type(const String &p_base_type); String get_base_type() const; Vector get_allowed_types() const;