|
32 | 32 |
|
33 | 33 | #include "editor/editor_string_names.h"
|
34 | 34 | #include "editor/themes/editor_scale.h"
|
| 35 | +#include "scene/gui/aspect_ratio_container.h" |
| 36 | +#include "scene/gui/color_rect.h" |
35 | 37 | #include "scene/gui/label.h"
|
36 | 38 | #include "scene/gui/texture_rect.h"
|
37 | 39 | #include "scene/resources/animated_texture.h"
|
@@ -61,11 +63,25 @@ void TexturePreview::_notification(int p_what) {
|
61 | 63 | metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font);
|
62 | 64 | }
|
63 | 65 |
|
| 66 | + bg_rect->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor))); |
64 | 67 | checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard")));
|
| 68 | + cached_outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor)); |
65 | 69 | } break;
|
66 | 70 | }
|
67 | 71 | }
|
68 | 72 |
|
| 73 | +void TexturePreview::_draw_outline() { |
| 74 | + const float outline_width = Math::round(EDSCALE); |
| 75 | + const Rect2 outline_rect = Rect2(Vector2(), texture_display->get_size()).grow(outline_width * 0.5); |
| 76 | + texture_display->draw_rect(outline_rect, cached_outline_color, false, outline_width); |
| 77 | +} |
| 78 | + |
| 79 | +void TexturePreview::_update_texture_display_ratio() { |
| 80 | + if (texture_display->get_texture().is_valid()) { |
| 81 | + centering_container->set_ratio(texture_display->get_texture()->get_size().aspect()); |
| 82 | + } |
| 83 | +} |
| 84 | + |
69 | 85 | void TexturePreview::_update_metadata_label_text() {
|
70 | 86 | const Ref<Texture2D> texture = texture_display->get_texture();
|
71 | 87 |
|
@@ -124,25 +140,49 @@ void TexturePreview::_update_metadata_label_text() {
|
124 | 140 | }
|
125 | 141 |
|
126 | 142 | TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
|
| 143 | + set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); |
| 144 | + |
| 145 | + bg_rect = memnew(ColorRect); |
| 146 | + |
| 147 | + add_child(bg_rect); |
| 148 | + |
| 149 | + margin_container = memnew(MarginContainer); |
| 150 | + const float outline_width = Math::round(EDSCALE); |
| 151 | + margin_container->add_theme_constant_override("margin_right", outline_width); |
| 152 | + margin_container->add_theme_constant_override("margin_top", outline_width); |
| 153 | + margin_container->add_theme_constant_override("margin_left", outline_width); |
| 154 | + margin_container->add_theme_constant_override("margin_bottom", outline_width); |
| 155 | + add_child(margin_container); |
| 156 | + |
| 157 | + centering_container = memnew(AspectRatioContainer); |
| 158 | + margin_container->add_child(centering_container); |
| 159 | + |
127 | 160 | checkerboard = memnew(TextureRect);
|
| 161 | + checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
128 | 162 | checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
|
129 | 163 | checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
|
130 |
| - checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); |
131 |
| - add_child(checkerboard); |
| 164 | + centering_container->add_child(checkerboard); |
132 | 165 |
|
133 | 166 | texture_display = memnew(TextureRect);
|
134 | 167 | texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
|
135 | 168 | texture_display->set_texture(p_texture);
|
136 |
| - texture_display->set_anchors_preset(TextureRect::PRESET_FULL_RECT); |
137 |
| - texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); |
138 | 169 | texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
|
139 |
| - add_child(texture_display); |
| 170 | + centering_container->add_child(texture_display); |
| 171 | + |
| 172 | + texture_display->connect(SceneStringName(draw), callable_mp(this, &TexturePreview::_draw_outline)); |
| 173 | + |
| 174 | + if (p_texture.is_valid()) { |
| 175 | + _update_texture_display_ratio(); |
| 176 | + p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_texture_display_ratio)); |
| 177 | + } |
140 | 178 |
|
141 | 179 | if (p_show_metadata) {
|
142 | 180 | metadata_label = memnew(Label);
|
143 | 181 |
|
144 |
| - _update_metadata_label_text(); |
145 |
| - p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text)); |
| 182 | + if (p_texture.is_valid()) { |
| 183 | + _update_metadata_label_text(); |
| 184 | + p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text)); |
| 185 | + } |
146 | 186 |
|
147 | 187 | // It's okay that these colors are static since the grid color is static too.
|
148 | 188 | metadata_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
|
|
0 commit comments