Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache allowed types in EditorResourcePicker #84443

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions editor/editor_resource_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,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;

Vector<EditorData::CustomType> custom_resources;
if (EditorNode::get_editor_data().get_custom_types().has("Resource")) {
Expand Down Expand Up @@ -612,23 +612,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");
}
}
}
Expand Down Expand Up @@ -658,8 +664,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);
Expand Down Expand Up @@ -725,8 +731,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);

Expand Down Expand Up @@ -842,8 +848,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;
Comment on lines +851 to +852
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we force updating the allowed types even if already cached, when changing base_type?

Like _ensure_allowed_types(bool p_force) that would bypass the !allowed_types_without_convert.is_empty() early return.

I'm not too familiar with this code but wouldn't this alleviate the issue with possibly requiring an editor restart to take new allowed types into account?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Editor restart is not required, only inspector refresh. When global classes change, all resource pickers already in scene tree will have outdated information and need to be recreated. Inspector pickers are recreated with every object edit, not sure if there are some that would stay outdated (maybe in e.g. project settings).

Also base type is set once per picker, idk if there are exceptions.


StringName custom_class;
bool is_custom = false;
Expand All @@ -864,8 +870,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());
Expand All @@ -888,8 +894,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;
Expand Down
4 changes: 3 additions & 1 deletion editor/editor_resource_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -96,7 +98,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, HashSet<StringName> p_allowed_types) const;

Expand Down
Loading