bit_map_editor_plugin.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #include "bit_map_editor_plugin.h"
  31. #include "editor/editor_string_names.h"
  32. #include "editor/themes/editor_scale.h"
  33. #include "scene/gui/aspect_ratio_container.h"
  34. #include "scene/gui/label.h"
  35. #include "scene/gui/margin_container.h"
  36. #include "scene/gui/texture_rect.h"
  37. #include "scene/resources/image_texture.h"
  38. void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) {
  39. Ref<ImageTexture> bitmap_texture = ImageTexture::create_from_image(p_bitmap->convert_to_image());
  40. texture_rect->set_texture(bitmap_texture);
  41. if (bitmap_texture.is_valid()) {
  42. centering_container->set_custom_minimum_size(Size2(0, 250) * EDSCALE);
  43. centering_container->set_ratio(bitmap_texture->get_size().aspect());
  44. outline_overlay->connect(SceneStringName(draw), callable_mp(this, &BitMapEditor::_draw_outline));
  45. }
  46. size_label->set_text(vformat(U"%s×%s", p_bitmap->get_size().width, p_bitmap->get_size().height));
  47. }
  48. void BitMapEditor::_notification(int p_what) {
  49. switch (p_what) {
  50. case NOTIFICATION_THEME_CHANGED: {
  51. cached_outline_color = get_theme_color(SNAME("extra_border_color_1"), EditorStringName(Editor));
  52. } break;
  53. }
  54. }
  55. void BitMapEditor::_draw_outline() {
  56. const float outline_width = Math::round(EDSCALE);
  57. const Rect2 outline_rect = Rect2(Vector2(), texture_rect->get_size()).grow(outline_width * 0.5);
  58. outline_overlay->draw_rect(outline_rect, cached_outline_color, false, outline_width);
  59. }
  60. BitMapEditor::BitMapEditor() {
  61. MarginContainer *margin_container = memnew(MarginContainer);
  62. const float outline_width = Math::round(EDSCALE);
  63. margin_container->add_theme_constant_override("margin_right", outline_width);
  64. margin_container->add_theme_constant_override("margin_top", outline_width);
  65. margin_container->add_theme_constant_override("margin_left", outline_width);
  66. margin_container->add_theme_constant_override("margin_bottom", outline_width);
  67. add_child(margin_container);
  68. centering_container = memnew(AspectRatioContainer);
  69. margin_container->add_child(centering_container);
  70. texture_rect = memnew(TextureRect);
  71. texture_rect->set_texture_filter(TEXTURE_FILTER_NEAREST);
  72. texture_rect->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  73. centering_container->add_child(texture_rect);
  74. outline_overlay = memnew(Control);
  75. centering_container->add_child(outline_overlay);
  76. size_label = memnew(Label);
  77. size_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  78. size_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  79. add_child(size_label);
  80. // Reduce extra padding on top and bottom of size label.
  81. Ref<StyleBoxEmpty> stylebox;
  82. stylebox.instantiate();
  83. stylebox->set_content_margin(SIDE_RIGHT, 4 * EDSCALE);
  84. size_label->add_theme_style_override(CoreStringName(normal), stylebox);
  85. }
  86. ///////////////////////
  87. bool EditorInspectorPluginBitMap::can_handle(Object *p_object) {
  88. return Object::cast_to<BitMap>(p_object) != nullptr;
  89. }
  90. void EditorInspectorPluginBitMap::parse_begin(Object *p_object) {
  91. BitMap *bitmap = Object::cast_to<BitMap>(p_object);
  92. if (!bitmap) {
  93. return;
  94. }
  95. Ref<BitMap> bm(bitmap);
  96. BitMapEditor *editor = memnew(BitMapEditor);
  97. editor->setup(bm);
  98. add_custom_control(editor);
  99. }
  100. ///////////////////////
  101. BitMapEditorPlugin::BitMapEditorPlugin() {
  102. Ref<EditorInspectorPluginBitMap> plugin;
  103. plugin.instantiate();
  104. add_inspector_plugin(plugin);
  105. }