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 Object Icons #36999

Closed
Closed
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
16 changes: 5 additions & 11 deletions editor/editor_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,17 +899,11 @@ String EditorData::script_class_get_icon_path(const String &p_class) const {
return String();
}

String current = p_class;
String ret = _script_class_icon_paths[current];
while (ret.empty()) {
current = script_class_get_base(current);
if (!ScriptServer::is_global_class(current)) {
return String();
}
ret = _script_class_icon_paths.has(current) ? _script_class_icon_paths[current] : String();
if (_script_class_icon_paths.has(p_class)) {
return _script_class_icon_paths[p_class];
}

return ret;
return script_class_get_icon_path(script_class_get_base(p_class));
}

StringName EditorData::script_class_get_name(const String &p_path) const {
Expand Down Expand Up @@ -950,8 +944,8 @@ void EditorData::script_class_load_icon_paths() {
d.get_key_list(&keys);

for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
String name = E->get().operator String();
_script_class_icon_paths[name] = d[name];
String name = (String)E->get();
_script_class_icon_paths[name] = d.has(name) ? String(d[name]) : String();

String path = ScriptServer::get_global_class_path(name);
script_class_set_name(path, name);
Expand Down
37 changes: 24 additions & 13 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3760,21 +3760,32 @@ StringName EditorNode::get_object_custom_type_name(const Object *p_object) const
return StringName();
}

Ref<ImageTexture> EditorNode::_load_custom_class_icon(const String &p_path) const {
if (p_path.length()) {
Ref<Image> img = memnew(Image);
Error err = ImageLoader::load_image(p_path, img);
if (err == OK) {
Ref<ImageTexture> icon = memnew(ImageTexture);
img->resize(16 * EDSCALE, 16 * EDSCALE, Image::INTERPOLATE_LANCZOS);
icon->create_from_image(img);
return icon;
Ref<ImageTexture> EditorNode::_load_custom_class_icon(const String &p_path) {
if (p_path.length() && p_path != "Null") {
Error err;
RES res = ResourceLoader::load(p_path, "", false, &err);
if (Object::cast_to<Texture2D>(*res)) {
res = ((Texture2D*)(*res))->get_data();
ERR_FAIL_COND_V(err != OK, nullptr);
} else if (Object::cast_to<Image>(*res)) {
res = ResourceLoader::load(p_path, "", false, &err);
ERR_FAIL_COND_V(err != OK, nullptr);
} else {
ERR_FAIL_V_MSG(nullptr, "Icon is neither an Image nor a Texture");
}

Ref<Image> img = res;

img->resize(16 * EDSCALE, 16 * EDSCALE, Image::INTERPOLATE_LANCZOS);

Ref<ImageTexture> icon = memnew(ImageTexture);
icon->create_from_image(img);
return icon;
}
return nullptr;
}

Ref<Texture2D> EditorNode::get_object_icon(const Object *p_object, const String &p_fallback) const {
Ref<Texture2D> EditorNode::get_object_icon(const Object *p_object, const String &p_fallback) {
ERR_FAIL_COND_V(!p_object || !gui_base, nullptr);

Ref<Script> script = p_object->get_script();
Expand All @@ -3787,7 +3798,7 @@ Ref<Texture2D> EditorNode::get_object_icon(const Object *p_object, const String
while (base_script.is_valid()) {
StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
String icon_path = EditorNode::get_editor_data().script_class_get_icon_path(name);
Ref<ImageTexture> icon = _load_custom_class_icon(icon_path);
Ref<Texture2D> icon = _load_custom_class_icon(icon_path);
if (icon.is_valid()) {
return icon;
}
Expand Down Expand Up @@ -3822,11 +3833,11 @@ Ref<Texture2D> EditorNode::get_object_icon(const Object *p_object, const String
return nullptr;
}

Ref<Texture2D> EditorNode::get_class_icon(const String &p_class, const String &p_fallback) const {
Ref<Texture2D> EditorNode::get_class_icon(const String &p_class, const String &p_fallback) {
ERR_FAIL_COND_V_MSG(p_class.empty(), nullptr, "Class name cannot be empty.");

if (ScriptServer::is_global_class(p_class)) {
Ref<ImageTexture> icon;
Ref<Texture2D> icon;
Ref<Script> script = EditorNode::get_editor_data().script_class_load_script(p_class);
StringName name = p_class;

Expand Down
6 changes: 3 additions & 3 deletions editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ class EditorNode : public Node {

void _feature_profile_changed();
bool _is_class_editor_disabled_by_feature_profile(const StringName &p_class);
Ref<ImageTexture> _load_custom_class_icon(const String &p_path) const;
Ref<ImageTexture> _load_custom_class_icon(const String &p_path);

protected:
void _notification(int p_what);
Expand Down Expand Up @@ -773,8 +773,8 @@ class EditorNode : public Node {
Ref<Theme> get_editor_theme() const { return theme; }
Ref<Script> get_object_custom_type_base(const Object *p_object) const;
StringName get_object_custom_type_name(const Object *p_object) const;
Ref<Texture2D> get_object_icon(const Object *p_object, const String &p_fallback = "Object") const;
Ref<Texture2D> get_class_icon(const String &p_class, const String &p_fallback = "Object") const;
Copy link
Member

Choose a reason for hiding this comment

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

What is the purpose of removing const? Do these methods need to modify the EditorNode instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They use resource loader load which isn't const IIRC, but I might be able to add it back since previously I used a manual cache system.

Ref<Texture2D> get_object_icon(const Object *p_object, const String &p_fallback = "Object");
Ref<Texture2D> get_class_icon(const String &p_class, const String &p_fallback = "Object");

void show_accept(const String &p_text, const String &p_title);
void show_warning(const String &p_text, const String &p_title = TTR("Warning!"));
Expand Down