Skip to content

Commit

Permalink
Add borders to BitMap in BitMapEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
arkology committed Dec 23, 2024
1 parent 216b330 commit 88802e2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
46 changes: 43 additions & 3 deletions editor/plugins/bit_map_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@

#include "bit_map_editor_plugin.h"

#include "editor/editor_string_names.h"
#include "editor/themes/editor_scale.h"
#include "scene/gui/aspect_ratio_container.h"
#include "scene/gui/label.h"
#include "scene/gui/margin_container.h"
#include "scene/gui/texture_rect.h"
#include "scene/resources/image_texture.h"

Expand All @@ -40,12 +43,49 @@ void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) {
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);

AspectRatioContainer *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);
Expand Down
6 changes: 6 additions & 0 deletions editor/plugins/bit_map_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@

#include "editor/editor_inspector.h"
#include "editor/plugins/editor_plugin.h"
#include "scene/gui/reference_rect.h"
#include "scene/resources/bit_map.h"

class TextureRect;

class BitMapEditor : public VBoxContainer {
GDCLASS(BitMapEditor, VBoxContainer);

private:
TextureRect *texture_rect = nullptr;
ReferenceRect *borders_rect = nullptr;
Label *size_label = nullptr;

protected:
void _notification(int p_what);

public:
void setup(const Ref<BitMap> &p_bitmap);

Expand Down

0 comments on commit 88802e2

Please sign in to comment.