From 161ee1c8fd1d0a7543439b631d84bc00fd7a9671 Mon Sep 17 00:00:00 2001 From: arkology <43543909+arkology@users.noreply.github.com> Date: Thu, 12 Dec 2024 22:29:02 +0300 Subject: [PATCH] Show "transparent background" texture only behind actual texture in `TexturePreview` class + add borders for readability --- editor/plugins/camera_3d_editor_plugin.cpp | 1 + editor/plugins/texture_editor_plugin.cpp | 58 ++++++++++++++++++++-- editor/plugins/texture_editor_plugin.h | 11 ++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/editor/plugins/camera_3d_editor_plugin.cpp b/editor/plugins/camera_3d_editor_plugin.cpp index 10ed7d1902ee..2d1e6d642cd0 100644 --- a/editor/plugins/camera_3d_editor_plugin.cpp +++ b/editor/plugins/camera_3d_editor_plugin.cpp @@ -91,6 +91,7 @@ Camera3DPreview::Camera3DPreview(Camera3D *p_camera) : TextureRect *display = get_texture_display(); display->set_texture(sub_viewport->get_texture()); sub_viewport->connect("size_changed", callable_mp((CanvasItem *)display, &CanvasItem::queue_redraw)); + sub_viewport->get_texture()->connect_changed(callable_mp((TexturePreview *)this, &Camera3DPreview::_update_texture_display_ratio)); ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Camera3DPreview::_update_sub_viewport_size)); _update_sub_viewport_size(); diff --git a/editor/plugins/texture_editor_plugin.cpp b/editor/plugins/texture_editor_plugin.cpp index d93466b5ba98..425cd7e9e87b 100644 --- a/editor/plugins/texture_editor_plugin.cpp +++ b/editor/plugins/texture_editor_plugin.cpp @@ -44,6 +44,15 @@ TextureRect *TexturePreview::get_texture_display() { return texture_display; } +void TexturePreview::set_texture_display_outline_width(int p_width) { + outline_width = p_width; + + margin_container->add_theme_constant_override("margin_right", outline_width + 1); + margin_container->add_theme_constant_override("margin_top", outline_width + 1); + margin_container->add_theme_constant_override("margin_left", outline_width + 1); + margin_container->add_theme_constant_override("margin_bottom", outline_width + 1); +} + void TexturePreview::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: @@ -61,11 +70,27 @@ void TexturePreview::_notification(int p_what) { metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font); } + bg_rect->set_color(get_theme_color(SNAME("dark_color_2"), EditorStringName(Editor))); checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard"))); } break; } } +void TexturePreview::_draw_outline() { + if (outline_width == 0) { + return; + } + const Rect2 outline_rect = Rect2(Vector2(), texture_display->get_size()).grow((float)outline_width / 2); + const Color outline_border_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor)); + texture_display->draw_rect(outline_rect, outline_border_color, false, (float)outline_width); +} + +void TexturePreview::_update_texture_display_ratio() { + if (texture_display->get_texture().is_valid()) { + centering_container->set_ratio(texture_display->get_texture()->get_size().aspect()); + } +} + void TexturePreview::_update_metadata_label_text() { const Ref texture = texture_display->get_texture(); @@ -124,19 +149,42 @@ void TexturePreview::_update_metadata_label_text() { } TexturePreview::TexturePreview(Ref p_texture, bool p_show_metadata) { + set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); + + bg_rect = memnew(ColorRect); + bg_rect->set_anchors_preset(PRESET_FULL_RECT); + add_child(bg_rect); + + margin_container = memnew(MarginContainer); + margin_container->set_anchors_preset(PRESET_FULL_RECT); + add_child(margin_container); + + centering_container = memnew(AspectRatioContainer); + centering_container->set_anchors_preset(PRESET_FULL_RECT); + margin_container->add_child(centering_container); + checkerboard = memnew(TextureRect); + checkerboard->set_anchors_preset(PRESET_FULL_RECT); + checkerboard->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE); checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED); - checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE); - add_child(checkerboard); + centering_container->add_child(checkerboard); texture_display = memnew(TextureRect); texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS); texture_display->set_texture(p_texture); - texture_display->set_anchors_preset(TextureRect::PRESET_FULL_RECT); - texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); - add_child(texture_display); + texture_display->set_anchors_preset(PRESET_FULL_RECT); + centering_container->add_child(texture_display); + + texture_display->connect(SceneStringName(draw), callable_mp(this, &TexturePreview::_draw_outline)); + + set_texture_display_outline_width(outline_width); + + if (p_texture.is_valid()) { + _update_texture_display_ratio(); + p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_texture_display_ratio)); + } if (p_show_metadata) { metadata_label = memnew(Label); diff --git a/editor/plugins/texture_editor_plugin.h b/editor/plugins/texture_editor_plugin.h index dc0140e71ee8..14ff4941c225 100644 --- a/editor/plugins/texture_editor_plugin.h +++ b/editor/plugins/texture_editor_plugin.h @@ -33,6 +33,8 @@ #include "editor/editor_inspector.h" #include "editor/plugins/editor_plugin.h" +#include "scene/gui/aspect_ratio_container.h" +#include "scene/gui/color_rect.h" #include "scene/gui/margin_container.h" #include "scene/resources/texture.h" @@ -44,16 +46,25 @@ class TexturePreview : public MarginContainer { private: TextureRect *texture_display = nullptr; + MarginContainer *margin_container = nullptr; + AspectRatioContainer *centering_container = nullptr; + ColorRect *bg_rect = nullptr; TextureRect *checkerboard = nullptr; Label *metadata_label = nullptr; + int outline_width = 1; + + void _draw_outline(); void _update_metadata_label_text(); protected: void _notification(int p_what); + void _update_texture_display_ratio(); public: TextureRect *get_texture_display(); + void set_texture_display_outline_width(int p_width); + TexturePreview(Ref p_texture, bool p_show_metadata); };