Skip to content

Commit

Permalink
Show "transparent background" texture only behind actual texture in `…
Browse files Browse the repository at this point in the history
…TexturePreview` class + add borders for readability
  • Loading branch information
arkology committed Dec 26, 2024
1 parent 216b330 commit 161ee1c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
1 change: 1 addition & 0 deletions editor/plugins/camera_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
58 changes: 53 additions & 5 deletions editor/plugins/texture_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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<Texture2D> texture = texture_display->get_texture();

Expand Down Expand Up @@ -124,19 +149,42 @@ void TexturePreview::_update_metadata_label_text() {
}

TexturePreview::TexturePreview(Ref<Texture2D> 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);
Expand Down
11 changes: 11 additions & 0 deletions editor/plugins/texture_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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<Texture2D> p_texture, bool p_show_metadata);
};

Expand Down

0 comments on commit 161ee1c

Please sign in to comment.