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_scale.h" |
34 | #include "scene/gui/label.h" |
35 | #include "scene/gui/texture_rect.h" |
36 | #include "scene/resources/image_texture.h" |
37 | |
38 | void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) { |
39 | texture_rect->set_texture(ImageTexture::create_from_image(p_bitmap->convert_to_image())); |
40 | size_label->set_text(vformat(U"%s×%s" , p_bitmap->get_size().width, p_bitmap->get_size().height)); |
41 | } |
42 | |
43 | BitMapEditor::BitMapEditor() { |
44 | texture_rect = memnew(TextureRect); |
45 | texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED); |
46 | texture_rect->set_texture_filter(TEXTURE_FILTER_NEAREST); |
47 | texture_rect->set_custom_minimum_size(Size2(0, 250) * EDSCALE); |
48 | add_child(texture_rect); |
49 | |
50 | size_label = memnew(Label); |
51 | size_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT); |
52 | add_child(size_label); |
53 | |
54 | // Reduce extra padding on top and bottom of size label. |
55 | Ref<StyleBoxEmpty> stylebox; |
56 | stylebox.instantiate(); |
57 | stylebox->set_content_margin(SIDE_RIGHT, 4 * EDSCALE); |
58 | size_label->add_theme_style_override("normal" , stylebox); |
59 | } |
60 | |
61 | /////////////////////// |
62 | |
63 | bool EditorInspectorPluginBitMap::can_handle(Object *p_object) { |
64 | return Object::cast_to<BitMap>(p_object) != nullptr; |
65 | } |
66 | |
67 | void EditorInspectorPluginBitMap::parse_begin(Object *p_object) { |
68 | BitMap *bitmap = Object::cast_to<BitMap>(p_object); |
69 | if (!bitmap) { |
70 | return; |
71 | } |
72 | Ref<BitMap> bm(bitmap); |
73 | |
74 | BitMapEditor *editor = memnew(BitMapEditor); |
75 | editor->setup(bm); |
76 | add_custom_control(editor); |
77 | } |
78 | |
79 | /////////////////////// |
80 | |
81 | BitMapEditorPlugin::BitMapEditorPlugin() { |
82 | Ref<EditorInspectorPluginBitMap> plugin; |
83 | plugin.instantiate(); |
84 | add_inspector_plugin(plugin); |
85 | } |
86 | |