bit_map_editor_plugin.cpp 3.8 KB

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