bit_map_editor_plugin.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_scale.h"
  32. void BitMapEditor::setup(const Ref<BitMap> &p_bitmap) {
  33. Ref<ImageTexture> texture;
  34. texture.instance();
  35. texture->create_from_image(p_bitmap->convert_to_image());
  36. texture->set_flags(texture->get_flags() & (~VisualServer::TEXTURE_FLAG_FILTER));
  37. texture_rect->set_texture(texture);
  38. size_label->set_text(vformat(String::utf8("%s×%s"), p_bitmap->get_size().width, p_bitmap->get_size().height));
  39. }
  40. BitMapEditor::BitMapEditor() {
  41. texture_rect = memnew(TextureRect);
  42. texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  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_align(Label::ALIGN_RIGHT);
  47. add_child(size_label);
  48. // Reduce extra padding on top and bottom of size label.
  49. Ref<StyleBoxEmpty> stylebox;
  50. stylebox.instance();
  51. stylebox->set_default_margin(MARGIN_RIGHT, 4 * EDSCALE);
  52. size_label->add_style_override("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(EditorNode *p_editor) {
  70. Ref<EditorInspectorPluginBitMap> plugin;
  71. plugin.instance();
  72. add_inspector_plugin(plugin);
  73. }