From decd5482237714f29edd38fee2717262f5812427 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Tue, 3 Aug 2021 16:52:12 +0300 Subject: Cache EditorResourcePicker's allowed types --- editor/editor_resource_picker.cpp | 40 +++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'editor/editor_resource_picker.cpp') diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp index 9977f5f42c..f3965fe7de 100644 --- a/editor/editor_resource_picker.cpp +++ b/editor/editor_resource_picker.cpp @@ -36,6 +36,12 @@ #include "editor_settings.h" #include "filesystem_dock.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)); @@ -462,17 +468,31 @@ void EditorResourcePicker::_get_allowed_types(bool p_with_convert, Set * String base = allowed_types[i].strip_edges(); p_vector->insert(base); - List inheriters; + // 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 (const StringName &subtype_name : allowed_subtypes) { + p_vector->insert(subtype_name); + } + } else { + List allowed_subtypes; - ClassDB::get_inheriters_from_class(base, &inheriters); - for (const StringName &E : inheriters) { - p_vector->insert(E); - } + List inheriters; + ClassDB::get_inheriters_from_class(base, &inheriters); + for (const StringName &subtype_name : inheriters) { + p_vector->insert(subtype_name); + allowed_subtypes.push_back(subtype_name); + } - for (const StringName &E : global_classes) { - if (EditorNode::get_editor_data().script_class_is_parent(E, base)) { - p_vector->insert(E); + for (const StringName &subtype_name : global_classes) { + if (EditorNode::get_editor_data().script_class_is_parent(subtype_name, base)) { + p_vector->insert(subtype_name); + allowed_subtypes.push_back(subtype_name); + } } + + // Store the subtypes of the base type in the cache for future use. + allowed_types_cache[base] = allowed_subtypes; } if (p_with_convert) { @@ -706,6 +726,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); } } -- cgit v1.2.3