Skip to content

Commit

Permalink
Show texture preview "transparent background" in editor plugin only b…
Browse files Browse the repository at this point in the history
…ehind actual texture
  • Loading branch information
arkology committed Dec 24, 2024
1 parent 19e003b commit fbc48d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
52 changes: 44 additions & 8 deletions editor/plugins/texture_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,18 @@ 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)));
borders_rect->set_border_color(get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor)));

checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard")));
} break;
}
}

void TexturePreview::_update_centering_container_ratio() {
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 +131,48 @@ void TexturePreview::_update_metadata_label_text() {
}

TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
checkerboard = memnew(TextureRect);
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);
set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);

bg_rect = memnew(ColorRect);
bg_rect->set_anchors_preset(PRESET_FULL_RECT);
bg_rect->set_clip_contents(true);
add_child(bg_rect);

MarginContainer *margin_container = memnew(MarginContainer);
margin_container->set_anchors_preset(PRESET_FULL_RECT);
margin_container->add_theme_constant_override("margin_right", 1);
margin_container->add_theme_constant_override("margin_top", 1);
margin_container->add_theme_constant_override("margin_left", 1);
margin_container->add_theme_constant_override("margin_bottom", 1);
add_child(margin_container);

centering_container = memnew(AspectRatioContainer);
centering_container->set_anchors_preset(PRESET_FULL_RECT);
margin_container->add_child(centering_container);

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);

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_draw_behind_parent(true);
texture_display->add_child(checkerboard);

borders_rect = memnew(ReferenceRect);
borders_rect->set_border_width(2);
borders_rect->set_anchors_preset(PRESET_FULL_RECT);
borders_rect->set_draw_behind_parent(true);
checkerboard->add_child(borders_rect);

_update_centering_container_ratio();
p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_centering_container_ratio));

if (p_show_metadata) {
metadata_label = memnew(Label);
Expand Down
7 changes: 7 additions & 0 deletions editor/plugins/texture_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@

#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/gui/reference_rect.h"
#include "scene/resources/texture.h"

class TextureRect;
Expand All @@ -44,9 +47,13 @@ class TexturePreview : public MarginContainer {
private:
TextureRect *texture_display = nullptr;

AspectRatioContainer *centering_container = nullptr;
ColorRect *bg_rect = nullptr;
TextureRect *checkerboard = nullptr;
ReferenceRect *borders_rect = nullptr;
Label *metadata_label = nullptr;

void _update_centering_container_ratio();
void _update_metadata_label_text();

protected:
Expand Down

0 comments on commit fbc48d1

Please sign in to comment.