bit_map_editor_plugin.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_scale.h"
  32. void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) {
  33. texture_rect->set_texture(ImageTexture::create_from_image(p_bitmap->convert_to_image()));
  34. size_label->set_text(vformat(String::utf8("%s×%s"), p_bitmap->get_size().width, p_bitmap->get_size().height));
  35. }
  36. BitMapEditor::BitMapEditor() {
  37. texture_rect = memnew(TextureRect);
  38. texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  39. texture_rect->set_texture_filter(TEXTURE_FILTER_NEAREST);
  40. texture_rect->set_custom_minimum_size(Size2(0, 250) * EDSCALE);
  41. add_child(texture_rect);
  42. size_label = memnew(Label);
  43. size_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  44. add_child(size_label);
  45. // Reduce extra padding on top and bottom of size label.
  46. Ref<StyleBoxEmpty> stylebox;
  47. stylebox.instantiate();
  48. stylebox->set_default_margin(SIDE_RIGHT, 4 * EDSCALE);
  49. size_label->add_theme_style_override("normal", stylebox);
  50. }
  51. ///////////////////////
  52. bool EditorInspectorPluginBitMap::can_handle(Object *p_object) {
  53. return Object::cast_to<BitMap>(p_object) != nullptr;
  54. }
  55. void EditorInspectorPluginBitMap::parse_begin(Object *p_object) {
  56. BitMap *bitmap = Object::cast_to<BitMap>(p_object);
  57. if (!bitmap) {
  58. return;
  59. }
  60. Ref<BitMap> bm(bitmap);
  61. BitMapEditor *editor = memnew(BitMapEditor);
  62. editor->setup(bm);
  63. add_custom_control(editor);
  64. }
  65. ///////////////////////
  66. BitMapEditorPlugin::BitMapEditorPlugin() {
  67. Ref<EditorInspectorPluginBitMap> plugin;
  68. plugin.instantiate();
  69. add_inspector_plugin(plugin);
  70. }