summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-04-30 17:02:58 +0200
committerRémi Verschelde <rverschelde@gmail.com>2024-04-30 17:02:58 +0200
commitd9bf750cbf9b24103f85f43856e771fb739df4c8 (patch)
treec66a2768e8102b26f4a5cfaa2e5b4206ac15a5ee
parent64520fe6741d8ec3c55e0c9618d3fadcda949f63 (diff)
parent50eac4d67b1a20a9420069bb3133bb6ff8ec4061 (diff)
downloadredot-engine-d9bf750cbf9b24103f85f43856e771fb739df4c8.tar.gz
Merge pull request #84443 from KoBeWi/cookies_for_EditorResourcePicker
Cache allowed types in EditorResourcePicker
-rw-r--r--editor/editor_resource_picker.cpp52
-rw-r--r--editor/editor_resource_picker.h4
2 files changed, 32 insertions, 24 deletions
diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp
index 9e4bf2b7d9..e082366c44 100644
--- a/editor/editor_resource_picker.cpp
+++ b/editor/editor_resource_picker.cpp
@@ -488,8 +488,8 @@ void EditorResourcePicker::set_create_options(Object *p_menu_node) {
if (!base_type.is_empty()) {
int idx = 0;
- HashSet<StringName> allowed_types;
- _get_allowed_types(false, &allowed_types);
+ _ensure_allowed_types();
+ HashSet<StringName> allowed_types = allowed_types_without_convert;
for (const StringName &E : allowed_types) {
const String &t = E;
@@ -593,23 +593,29 @@ static void _add_allowed_type(const StringName &p_type, HashSet<StringName> *p_v
}
}
-void EditorResourcePicker::_get_allowed_types(bool p_with_convert, HashSet<StringName> *p_vector) const {
+void EditorResourcePicker::_ensure_allowed_types() const {
+ if (!allowed_types_without_convert.is_empty()) {
+ return;
+ }
+
Vector<String> allowed_types = base_type.split(",");
int size = allowed_types.size();
for (int i = 0; i < size; i++) {
- String base = allowed_types[i].strip_edges();
+ const String base = allowed_types[i].strip_edges();
+ _add_allowed_type(base, &allowed_types_without_convert);
+ }
- _add_allowed_type(base, p_vector);
+ allowed_types_with_convert = HashSet<StringName>(allowed_types_without_convert);
- if (p_with_convert) {
- if (base == "BaseMaterial3D") {
- p_vector->insert("Texture2D");
- } else if (base == "ShaderMaterial") {
- p_vector->insert("Shader");
- } else if (base == "Texture2D") {
- p_vector->insert("Image");
- }
+ for (int i = 0; i < size; i++) {
+ const String base = allowed_types[i].strip_edges();
+ if (base == "BaseMaterial3D") {
+ allowed_types_with_convert.insert("Texture2D");
+ } else if (base == "ShaderMaterial") {
+ allowed_types_with_convert.insert("Shader");
+ } else if (base == "Texture2D") {
+ allowed_types_with_convert.insert("Image");
}
}
}
@@ -645,8 +651,8 @@ bool EditorResourcePicker::_is_drop_valid(const Dictionary &p_drag_data) const {
}
}
- HashSet<StringName> allowed_types;
- _get_allowed_types(true, &allowed_types);
+ _ensure_allowed_types();
+ HashSet<StringName> allowed_types = allowed_types_with_convert;
if (res.is_valid()) {
String res_type = _get_resource_type(res);
@@ -713,8 +719,8 @@ void EditorResourcePicker::drop_data_fw(const Point2 &p_point, const Variant &p_
}
if (dropped_resource.is_valid()) {
- HashSet<StringName> allowed_types;
- _get_allowed_types(false, &allowed_types);
+ _ensure_allowed_types();
+ HashSet<StringName> allowed_types = allowed_types_without_convert;
String res_type = _get_resource_type(dropped_resource);
@@ -835,8 +841,8 @@ void EditorResourcePicker::set_base_type(const String &p_base_type) {
// There is a possibility that the new base type is conflicting with the existing value.
// Keep the value, but warn the user that there is a potential mistake.
if (!base_type.is_empty() && edited_resource.is_valid()) {
- HashSet<StringName> allowed_types;
- _get_allowed_types(true, &allowed_types);
+ _ensure_allowed_types();
+ HashSet<StringName> allowed_types = allowed_types_with_convert;
StringName custom_class;
bool is_custom = false;
@@ -857,8 +863,8 @@ String EditorResourcePicker::get_base_type() const {
}
Vector<String> EditorResourcePicker::get_allowed_types() const {
- HashSet<StringName> allowed_types;
- _get_allowed_types(false, &allowed_types);
+ _ensure_allowed_types();
+ HashSet<StringName> allowed_types = allowed_types_without_convert;
Vector<String> types;
types.resize(allowed_types.size());
@@ -881,8 +887,8 @@ void EditorResourcePicker::set_edited_resource(Ref<Resource> p_resource) {
}
if (!base_type.is_empty()) {
- HashSet<StringName> allowed_types;
- _get_allowed_types(true, &allowed_types);
+ _ensure_allowed_types();
+ HashSet<StringName> allowed_types = allowed_types_with_convert;
StringName custom_class;
bool is_custom = false;
diff --git a/editor/editor_resource_picker.h b/editor/editor_resource_picker.h
index 333d9c9915..28229e6b37 100644
--- a/editor/editor_resource_picker.h
+++ b/editor/editor_resource_picker.h
@@ -52,6 +52,8 @@ class EditorResourcePicker : public HBoxContainer {
bool dropping = false;
Vector<String> inheritors_array;
+ mutable HashSet<StringName> allowed_types_without_convert;
+ mutable HashSet<StringName> allowed_types_with_convert;
Button *assign_button = nullptr;
TextureRect *preview_rect = nullptr;
@@ -97,7 +99,7 @@ class EditorResourcePicker : public HBoxContainer {
void _button_input(const Ref<InputEvent> &p_event);
String _get_resource_type(const Ref<Resource> &p_resource) const;
- void _get_allowed_types(bool p_with_convert, HashSet<StringName> *p_vector) const;
+ void _ensure_allowed_types() const;
bool _is_drop_valid(const Dictionary &p_drag_data) const;
bool _is_type_valid(const String &p_type_name, const HashSet<StringName> &p_allowed_types) const;