From 64af5669c063469e73869da39b2ad7992f0dc249 Mon Sep 17 00:00:00 2001 From: arkology <43543909+arkology@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:33:22 +0300 Subject: [PATCH] Add borders to `BitMap` in `BitMapEditor` --- editor/plugins/bit_map_editor_plugin.cpp | 46 ++++++++++++++++++++++-- editor/plugins/bit_map_editor_plugin.h | 8 +++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/editor/plugins/bit_map_editor_plugin.cpp b/editor/plugins/bit_map_editor_plugin.cpp index 668ea04d6982..66bbd355b84a 100644 --- a/editor/plugins/bit_map_editor_plugin.cpp +++ b/editor/plugins/bit_map_editor_plugin.cpp @@ -30,22 +30,62 @@ #include "bit_map_editor_plugin.h" +#include "editor/editor_string_names.h" #include "editor/themes/editor_scale.h" #include "scene/gui/label.h" +#include "scene/gui/margin_container.h" #include "scene/gui/texture_rect.h" #include "scene/resources/image_texture.h" void BitMapEditor::setup(const Ref &p_bitmap) { texture_rect->set_texture(ImageTexture::create_from_image(p_bitmap->convert_to_image())); + centering_container->set_ratio(texture_rect->get_texture()->get_size().aspect()); size_label->set_text(vformat(U"%s×%s", p_bitmap->get_size().width, p_bitmap->get_size().height)); } +void BitMapEditor::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_ENTER_TREE: + case NOTIFICATION_THEME_CHANGED: { + if (!is_inside_tree()) { + // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED` + // is getting called for some reason when the `BitMapEditor` is + // getting destroyed, which causes `get_theme_font()` to return `nullptr`. + // See https://github.com/godotengine/godot/issues/50743. + break; + } + + borders_rect->set_border_color(get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor))); + + } break; + } +} + BitMapEditor::BitMapEditor() { + MarginContainer *margin_container = memnew(MarginContainer); + margin_container->set_anchors_preset(PRESET_FULL_RECT); + margin_container->set_custom_minimum_size(Size2(0, 250) * EDSCALE); + 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_rect = memnew(TextureRect); - texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); texture_rect->set_texture_filter(TEXTURE_FILTER_NEAREST); - texture_rect->set_custom_minimum_size(Size2(0, 250) * EDSCALE); - add_child(texture_rect); + texture_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); + texture_rect->set_anchors_preset(PRESET_FULL_RECT); + centering_container->add_child(texture_rect); + + 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); + texture_rect->add_child(borders_rect); size_label = memnew(Label); size_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); diff --git a/editor/plugins/bit_map_editor_plugin.h b/editor/plugins/bit_map_editor_plugin.h index 030536ab6b35..b02ca7651dc8 100644 --- a/editor/plugins/bit_map_editor_plugin.h +++ b/editor/plugins/bit_map_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/reference_rect.h" #include "scene/resources/bit_map.h" class TextureRect; @@ -40,9 +42,15 @@ class TextureRect; class BitMapEditor : public VBoxContainer { GDCLASS(BitMapEditor, VBoxContainer); +private: + AspectRatioContainer *centering_container = nullptr; TextureRect *texture_rect = nullptr; + ReferenceRect *borders_rect = nullptr; Label *size_label = nullptr; +protected: + void _notification(int p_what); + public: void setup(const Ref &p_bitmap);