texture_editor_plugin.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*************************************************************************/
  2. /* texture_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "texture_editor_plugin.h"
  31. #include "editor/editor_scale.h"
  32. TextureRect *TexturePreview::get_texture_display() {
  33. return texture_display;
  34. }
  35. void TexturePreview::_notification(int p_what) {
  36. switch (p_what) {
  37. case NOTIFICATION_ENTER_TREE:
  38. case NOTIFICATION_THEME_CHANGED: {
  39. if (!is_inside_tree()) {
  40. // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED`
  41. // is getting called for some reason when the `TexturePreview` is
  42. // getting destroyed, which causes `get_theme_font()` to return `nullptr`.
  43. // See https://github.com/godotengine/godot/issues/50743.
  44. break;
  45. }
  46. if (metadata_label) {
  47. Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), SNAME("EditorFonts"));
  48. metadata_label->add_theme_font_override("font", metadata_label_font);
  49. }
  50. checkerboard->set_texture(get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons")));
  51. } break;
  52. }
  53. }
  54. TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
  55. checkerboard = memnew(TextureRect);
  56. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  57. checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
  58. checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
  59. add_child(checkerboard);
  60. texture_display = memnew(TextureRect);
  61. texture_display->set_texture(p_texture);
  62. texture_display->set_anchors_preset(TextureRect::PRESET_WIDE);
  63. texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  64. texture_display->set_expand(true);
  65. add_child(texture_display);
  66. if (p_show_metadata) {
  67. metadata_label = memnew(Label);
  68. String format;
  69. if (Object::cast_to<ImageTexture>(*p_texture)) {
  70. format = Image::get_format_name(Object::cast_to<ImageTexture>(*p_texture)->get_format());
  71. } else if (Object::cast_to<StreamTexture2D>(*p_texture)) {
  72. format = Image::get_format_name(Object::cast_to<StreamTexture2D>(*p_texture)->get_format());
  73. } else {
  74. format = p_texture->get_class();
  75. }
  76. metadata_label->set_text(itos(p_texture->get_width()) + "x" + itos(p_texture->get_height()) + " " + format);
  77. // It's okay that these colors are static since the grid color is static too.
  78. metadata_label->add_theme_color_override("font_color", Color::named("white"));
  79. metadata_label->add_theme_color_override("font_color_shadow", Color::named("black"));
  80. metadata_label->add_theme_font_size_override("font_size", 16 * EDSCALE);
  81. metadata_label->add_theme_color_override("font_outline_color", Color::named("black"));
  82. metadata_label->add_theme_constant_override("outline_size", 2 * EDSCALE);
  83. metadata_label->add_theme_constant_override("shadow_as_outline", 1);
  84. metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);
  85. metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);
  86. add_child(metadata_label);
  87. }
  88. }
  89. bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
  90. return Object::cast_to<ImageTexture>(p_object) != nullptr || Object::cast_to<AtlasTexture>(p_object) != nullptr || Object::cast_to<StreamTexture2D>(p_object) != nullptr || Object::cast_to<AnimatedTexture>(p_object) != nullptr;
  91. }
  92. void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
  93. Ref<Texture> texture(Object::cast_to<Texture>(p_object));
  94. add_custom_control(memnew(TexturePreview(texture, true)));
  95. }
  96. TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
  97. Ref<EditorInspectorPluginTexture> plugin;
  98. plugin.instantiate();
  99. add_inspector_plugin(plugin);
  100. }