Cache EditorResourcePicker's allowed types

This commit is contained in:
Yuri Sizov 2021-08-03 16:52:12 +03:00
parent ba2e6c66cb
commit decd548223
3 changed files with 40 additions and 10 deletions

View file

@ -3797,6 +3797,8 @@ void EditorNode::unregister_editor_types() {
if (EditorPaths::get_singleton()) { if (EditorPaths::get_singleton()) {
EditorPaths::free(); EditorPaths::free();
} }
EditorResourcePicker::clear_caches();
} }
void EditorNode::stop_child_process(OS::ProcessID p_pid) { void EditorNode::stop_child_process(OS::ProcessID p_pid) {

View file

@ -36,6 +36,12 @@
#include "editor_settings.h" #include "editor_settings.h"
#include "filesystem_dock.h" #include "filesystem_dock.h"
HashMap<StringName, List<StringName>> EditorResourcePicker::allowed_types_cache;
void EditorResourcePicker::clear_caches() {
allowed_types_cache.clear();
}
void EditorResourcePicker::_update_resource() { void EditorResourcePicker::_update_resource() {
preview_rect->set_texture(Ref<Texture2D>()); preview_rect->set_texture(Ref<Texture2D>());
assign_button->set_custom_minimum_size(Size2(1, 1)); assign_button->set_custom_minimum_size(Size2(1, 1));
@ -462,17 +468,31 @@ void EditorResourcePicker::_get_allowed_types(bool p_with_convert, Set<String> *
String base = allowed_types[i].strip_edges(); String base = allowed_types[i].strip_edges();
p_vector->insert(base); p_vector->insert(base);
List<StringName> inheriters; // If we hit a familiar base type, take all the data from cache.
if (allowed_types_cache.has(base)) {
ClassDB::get_inheriters_from_class(base, &inheriters); List<StringName> allowed_subtypes = allowed_types_cache[base];
for (const StringName &E : inheriters) { for (const StringName &subtype_name : allowed_subtypes) {
p_vector->insert(E); p_vector->insert(subtype_name);
}
for (const StringName &E : global_classes) {
if (EditorNode::get_editor_data().script_class_is_parent(E, base)) {
p_vector->insert(E);
} }
} else {
List<StringName> allowed_subtypes;
List<StringName> 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 &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) { 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())); 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)); 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<String> allowed_types;
_get_allowed_types(false, &allowed_types);
} }
} }

View file

@ -40,6 +40,8 @@
class EditorResourcePicker : public HBoxContainer { class EditorResourcePicker : public HBoxContainer {
GDCLASS(EditorResourcePicker, HBoxContainer); GDCLASS(EditorResourcePicker, HBoxContainer);
static HashMap<StringName, List<StringName>> allowed_types_cache;
String base_type; String base_type;
RES edited_resource; RES edited_resource;
@ -97,6 +99,8 @@ protected:
void _notification(int p_what); void _notification(int p_what);
public: public:
static void clear_caches();
void set_base_type(const String &p_base_type); void set_base_type(const String &p_base_type);
String get_base_type() const; String get_base_type() const;
Vector<String> get_allowed_types() const; Vector<String> get_allowed_types() const;