| 1 | /**************************************************************************/ |
| 2 | /* bit_map_editor_plugin.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "bit_map_editor_plugin.h" |
| 32 | |
| 33 | #include "editor/editor_string_names.h" |
| 34 | #include "editor/themes/editor_scale.h" |
| 35 | #include "scene/gui/aspect_ratio_container.h" |
| 36 | #include "scene/gui/label.h" |
| 37 | #include "scene/gui/margin_container.h" |
| 38 | #include "scene/gui/texture_rect.h" |
| 39 | #include "scene/resources/image_texture.h" |
| 40 | |
| 41 | void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) { |
| 42 | Ref<ImageTexture> bitmap_texture = ImageTexture::create_from_image(p_bitmap->convert_to_image()); |
| 43 | texture_rect->set_texture(bitmap_texture); |
| 44 | if (bitmap_texture.is_valid()) { |
| 45 | centering_container->set_custom_minimum_size(Size2(0, 250) * EDSCALE); |
| 46 | centering_container->set_ratio(bitmap_texture->get_size().aspect()); |
| 47 | outline_overlay->connect(SceneStringName(draw), callable_mp(this, &BitMapEditor::_draw_outline)); |
| 48 | } |
| 49 | size_label->set_text(vformat(U"%s×%s", p_bitmap->get_size().width, p_bitmap->get_size().height)); |
| 50 | } |
| 51 | |
| 52 | void BitMapEditor::_notification(int p_what) { |
| 53 | switch (p_what) { |
| 54 | case NOTIFICATION_THEME_CHANGED: { |
| 55 | cached_outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor)); |
| 56 | } break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void BitMapEditor::_draw_outline() { |
| 61 | const float outline_width = Math::round(EDSCALE); |
| 62 | const Rect2 outline_rect = Rect2(Vector2(), texture_rect->get_size()).grow(outline_width * 0.5); |
| 63 | outline_overlay->draw_rect(outline_rect, cached_outline_color, false, outline_width); |
| 64 | } |
| 65 | |
| 66 | BitMapEditor::BitMapEditor() { |
| 67 | MarginContainer *margin_container = memnew(MarginContainer); |
| 68 | const float outline_width = Math::round(EDSCALE); |
| 69 | margin_container->add_theme_constant_override("margin_right", outline_width); |
| 70 | margin_container->add_theme_constant_override("margin_top", outline_width); |
| 71 | margin_container->add_theme_constant_override("margin_left", outline_width); |
| 72 | margin_container->add_theme_constant_override("margin_bottom", outline_width); |
| 73 | add_child(margin_container); |
| 74 | |
| 75 | centering_container = memnew(AspectRatioContainer); |
| 76 | margin_container->add_child(centering_container); |
| 77 | |
| 78 | texture_rect = memnew(TextureRect); |
| 79 | texture_rect->set_texture_filter(TEXTURE_FILTER_NEAREST); |
| 80 | texture_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE); |
| 81 | centering_container->add_child(texture_rect); |
| 82 | |
| 83 | outline_overlay = memnew(Control); |
| 84 | centering_container->add_child(outline_overlay); |
| 85 | |
| 86 | size_label = memnew(Label); |
| 87 | size_label->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 88 | size_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
| 89 | add_child(size_label); |
| 90 | |
| 91 | // Reduce extra padding on top and bottom of size label. |
| 92 | Ref<StyleBoxEmpty> stylebox; |
| 93 | stylebox.instantiate(); |
| 94 | stylebox->set_content_margin(SIDE_RIGHT, 4 * EDSCALE); |
| 95 | size_label->add_theme_style_override(CoreStringName(normal), stylebox); |
| 96 | } |
| 97 | |
| 98 | /////////////////////// |
| 99 | |
| 100 | bool EditorInspectorPluginBitMap::can_handle(Object *p_object) { |
| 101 | return Object::cast_to<BitMap>(p_object) != nullptr; |
| 102 | } |
| 103 | |
| 104 | void EditorInspectorPluginBitMap::parse_begin(Object *p_object) { |
| 105 | BitMap *bitmap = Object::cast_to<BitMap>(p_object); |
| 106 | if (!bitmap) { |
| 107 | return; |
| 108 | } |
| 109 | Ref<BitMap> bm(bitmap); |
| 110 | |
| 111 | BitMapEditor *editor = memnew(BitMapEditor); |
| 112 | editor->setup(bm); |
| 113 | add_custom_control(editor); |
| 114 | } |
| 115 | |
| 116 | /////////////////////// |
| 117 | |
| 118 | BitMapEditorPlugin::BitMapEditorPlugin() { |
| 119 | Ref<EditorInspectorPluginBitMap> plugin; |
| 120 | plugin.instantiate(); |
| 121 | add_inspector_plugin(plugin); |
| 122 | } |