1 | /**************************************************************************/ |
2 | /* texture_editor_plugin.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "texture_editor_plugin.h" |
32 | |
33 | #include "editor/editor_scale.h" |
34 | #include "editor/editor_string_names.h" |
35 | #include "scene/gui/label.h" |
36 | #include "scene/gui/texture_rect.h" |
37 | #include "scene/resources/animated_texture.h" |
38 | #include "scene/resources/atlas_texture.h" |
39 | #include "scene/resources/compressed_texture.h" |
40 | #include "scene/resources/image_texture.h" |
41 | |
42 | TextureRect *TexturePreview::get_texture_display() { |
43 | return texture_display; |
44 | } |
45 | |
46 | void TexturePreview::_notification(int p_what) { |
47 | switch (p_what) { |
48 | case NOTIFICATION_ENTER_TREE: |
49 | case NOTIFICATION_THEME_CHANGED: { |
50 | if (!is_inside_tree()) { |
51 | // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED` |
52 | // is getting called for some reason when the `TexturePreview` is |
53 | // getting destroyed, which causes `get_theme_font()` to return `nullptr`. |
54 | // See https://github.com/godotengine/godot/issues/50743. |
55 | break; |
56 | } |
57 | |
58 | if (metadata_label) { |
59 | Ref<Font> metadata_label_font = get_theme_font(SNAME("expression" ), EditorStringName(EditorFonts)); |
60 | metadata_label->add_theme_font_override("font" , metadata_label_font); |
61 | } |
62 | |
63 | checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard" ))); |
64 | } break; |
65 | } |
66 | } |
67 | |
68 | void TexturePreview::_update_metadata_label_text() { |
69 | const Ref<Texture2D> texture = texture_display->get_texture(); |
70 | |
71 | String format; |
72 | if (Object::cast_to<ImageTexture>(*texture)) { |
73 | format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format()); |
74 | } else if (Object::cast_to<CompressedTexture2D>(*texture)) { |
75 | format = Image::get_format_name(Object::cast_to<CompressedTexture2D>(*texture)->get_format()); |
76 | } else { |
77 | format = texture->get_class(); |
78 | } |
79 | |
80 | const Ref<Image> image = texture->get_image(); |
81 | if (image.is_valid()) { |
82 | const int mipmaps = image->get_mipmap_count(); |
83 | // Avoid signed integer overflow that could occur with huge texture sizes by casting everything to uint64_t. |
84 | uint64_t memory = uint64_t(image->get_width()) * uint64_t(image->get_height()) * uint64_t(Image::get_format_pixel_size(image->get_format())); |
85 | // Handle VRAM-compressed formats that are stored with 4 bpp. |
86 | memory >>= Image::get_format_pixel_rshift(image->get_format()); |
87 | |
88 | float mipmaps_multiplier = 1.0; |
89 | float mipmap_increase = 0.25; |
90 | for (int i = 0; i < mipmaps; i++) { |
91 | // Each mip adds 25% memory usage of the previous one. |
92 | // With a complete mipmap chain, memory usage increases by ~33%. |
93 | mipmaps_multiplier += mipmap_increase; |
94 | mipmap_increase *= 0.25; |
95 | } |
96 | memory *= mipmaps_multiplier; |
97 | |
98 | if (mipmaps >= 1) { |
99 | metadata_label->set_text( |
100 | vformat(String::utf8("%d×%d %s\n" ) + TTR("%s Mipmaps" ) + "\n" + TTR("Memory: %s" ), |
101 | texture->get_width(), |
102 | texture->get_height(), |
103 | format, |
104 | mipmaps, |
105 | String::humanize_size(memory))); |
106 | } else { |
107 | // "No Mipmaps" is easier to distinguish than "0 Mipmaps", |
108 | // especially since 0, 6, and 8 look quite close with the default code font. |
109 | metadata_label->set_text( |
110 | vformat(String::utf8("%d×%d %s\n" ) + TTR("No Mipmaps" ) + "\n" + TTR("Memory: %s" ), |
111 | texture->get_width(), |
112 | texture->get_height(), |
113 | format, |
114 | String::humanize_size(memory))); |
115 | } |
116 | } else { |
117 | metadata_label->set_text( |
118 | vformat(String::utf8("%d×%d %s" ), |
119 | texture->get_width(), |
120 | texture->get_height(), |
121 | format)); |
122 | } |
123 | } |
124 | |
125 | TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) { |
126 | checkerboard = memnew(TextureRect); |
127 | checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE); |
128 | checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED); |
129 | checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); |
130 | add_child(checkerboard); |
131 | |
132 | texture_display = memnew(TextureRect); |
133 | texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS); |
134 | texture_display->set_texture(p_texture); |
135 | texture_display->set_anchors_preset(TextureRect::PRESET_FULL_RECT); |
136 | texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); |
137 | texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
138 | add_child(texture_display); |
139 | |
140 | if (p_show_metadata) { |
141 | metadata_label = memnew(Label); |
142 | |
143 | _update_metadata_label_text(); |
144 | p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text)); |
145 | |
146 | // It's okay that these colors are static since the grid color is static too. |
147 | metadata_label->add_theme_color_override("font_color" , Color::named("white" )); |
148 | metadata_label->add_theme_color_override("font_shadow_color" , Color::named("black" )); |
149 | |
150 | metadata_label->add_theme_font_size_override("font_size" , 14 * EDSCALE); |
151 | metadata_label->add_theme_color_override("font_outline_color" , Color::named("black" )); |
152 | metadata_label->add_theme_constant_override("outline_size" , 8 * EDSCALE); |
153 | metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END); |
154 | metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END); |
155 | |
156 | add_child(metadata_label); |
157 | } |
158 | } |
159 | |
160 | bool EditorInspectorPluginTexture::can_handle(Object *p_object) { |
161 | return Object::cast_to<ImageTexture>(p_object) != nullptr || Object::cast_to<AtlasTexture>(p_object) != nullptr || Object::cast_to<CompressedTexture2D>(p_object) != nullptr || Object::cast_to<AnimatedTexture>(p_object) != nullptr || Object::cast_to<Image>(p_object) != nullptr; |
162 | } |
163 | |
164 | void EditorInspectorPluginTexture::parse_begin(Object *p_object) { |
165 | Ref<Texture> texture(Object::cast_to<Texture>(p_object)); |
166 | if (texture.is_null()) { |
167 | Ref<Image> image(Object::cast_to<Image>(p_object)); |
168 | texture = ImageTexture::create_from_image(image); |
169 | |
170 | ERR_FAIL_NULL_MSG(texture, "Failed to create the texture from an invalid image." ); |
171 | } |
172 | |
173 | add_custom_control(memnew(TexturePreview(texture, true))); |
174 | } |
175 | |
176 | TextureEditorPlugin::TextureEditorPlugin() { |
177 | Ref<EditorInspectorPluginTexture> plugin; |
178 | plugin.instantiate(); |
179 | add_inspector_plugin(plugin); |
180 | } |
181 | |