texture_editor_plugin.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**************************************************************************/
  2. /* texture_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 "texture_editor_plugin.h"
  31. #include "editor/editor_scale.h"
  32. #include "scene/resources/dynamic_font.h"
  33. TextureRect *TexturePreview::get_texture_display() {
  34. return texture_display;
  35. }
  36. void TexturePreview::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_ENTER_TREE:
  39. case NOTIFICATION_THEME_CHANGED: {
  40. if (!is_inside_tree()) {
  41. // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED`
  42. // is getting called for some reason when the `TexturePreview` is
  43. // getting destroyed, which causes `get_theme_font()` to return `nullptr`.
  44. // See https://github.com/godotengine/godot/issues/50743.
  45. break;
  46. }
  47. if (metadata_label) {
  48. Ref<DynamicFont> metadata_label_font = get_font("expression", "EditorFonts")->duplicate();
  49. metadata_label_font->set_size(16 * EDSCALE);
  50. metadata_label_font->set_outline_size(2 * EDSCALE);
  51. metadata_label_font->set_outline_color(Color::named("black"));
  52. metadata_label->add_font_override("font", metadata_label_font);
  53. }
  54. checkerboard->set_texture(get_icon("Checkerboard", "EditorIcons"));
  55. } break;
  56. }
  57. }
  58. void TexturePreview::_update_metadata_label_text() {
  59. Ref<Texture> texture = texture_display->get_texture();
  60. String format;
  61. if (Object::cast_to<ImageTexture>(*texture)) {
  62. format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format());
  63. } else if (Object::cast_to<StreamTexture>(*texture)) {
  64. format = Image::get_format_name(Object::cast_to<StreamTexture>(*texture)->get_format());
  65. } else {
  66. format = texture->get_class();
  67. }
  68. metadata_label->set_text(itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format);
  69. }
  70. void TexturePreview::_bind_methods() {
  71. ClassDB::bind_method(D_METHOD("_update_metadata_label_text"), &TexturePreview::_update_metadata_label_text);
  72. }
  73. TexturePreview::TexturePreview(Ref<Texture> p_texture, bool p_show_metadata) {
  74. checkerboard = memnew(TextureRect);
  75. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  76. checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
  77. add_child(checkerboard);
  78. texture_display = memnew(TextureRect);
  79. texture_display->set_texture(p_texture);
  80. texture_display->set_anchors_preset(TextureRect::PRESET_WIDE);
  81. texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  82. texture_display->set_expand(true);
  83. add_child(texture_display);
  84. if (p_show_metadata) {
  85. metadata_label = memnew(Label);
  86. _update_metadata_label_text();
  87. p_texture->connect("changed", this, "_update_metadata_label_text");
  88. // It's okay that these colors are static since the grid color is static too.
  89. metadata_label->add_color_override("font_color", Color::named("white"));
  90. metadata_label->add_color_override("font_color_shadow", Color::named("black"));
  91. metadata_label->add_constant_override("shadow_as_outline", 1);
  92. metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);
  93. metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);
  94. add_child(metadata_label);
  95. }
  96. }
  97. bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
  98. return Object::cast_to<ImageTexture>(p_object) != nullptr || Object::cast_to<AtlasTexture>(p_object) != nullptr || Object::cast_to<StreamTexture>(p_object) != nullptr || Object::cast_to<LargeTexture>(p_object) != nullptr || Object::cast_to<AnimatedTexture>(p_object) != nullptr;
  99. }
  100. void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
  101. Ref<Texture> texture(Object::cast_to<Texture>(p_object));
  102. add_custom_control(memnew(TexturePreview(texture, true)));
  103. }
  104. TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
  105. Ref<EditorInspectorPluginTexture> plugin;
  106. plugin.instance();
  107. add_inspector_plugin(plugin);
  108. }