texture_editor_plugin.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_string_names.h"
  32. #include "editor/themes/editor_scale.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/gui/texture_rect.h"
  35. #include "scene/resources/animated_texture.h"
  36. #include "scene/resources/atlas_texture.h"
  37. #include "scene/resources/compressed_texture.h"
  38. #include "scene/resources/image_texture.h"
  39. #include "scene/resources/portable_compressed_texture.h"
  40. TextureRect *TexturePreview::get_texture_display() {
  41. return texture_display;
  42. }
  43. void TexturePreview::_notification(int p_what) {
  44. switch (p_what) {
  45. case NOTIFICATION_ENTER_TREE:
  46. case NOTIFICATION_THEME_CHANGED: {
  47. if (!is_inside_tree()) {
  48. // TODO: This is a workaround because `NOTIFICATION_THEME_CHANGED`
  49. // is getting called for some reason when the `TexturePreview` is
  50. // getting destroyed, which causes `get_theme_font()` to return `nullptr`.
  51. // See https://github.com/godotengine/godot/issues/50743.
  52. break;
  53. }
  54. if (metadata_label) {
  55. Ref<Font> metadata_label_font = get_theme_font(SNAME("expression"), EditorStringName(EditorFonts));
  56. metadata_label->add_theme_font_override(SceneStringName(font), metadata_label_font);
  57. }
  58. checkerboard->set_texture(get_editor_theme_icon(SNAME("Checkerboard")));
  59. } break;
  60. }
  61. }
  62. void TexturePreview::_update_metadata_label_text() {
  63. const Ref<Texture2D> texture = texture_display->get_texture();
  64. String format;
  65. if (Object::cast_to<ImageTexture>(*texture)) {
  66. format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format());
  67. } else if (Object::cast_to<CompressedTexture2D>(*texture)) {
  68. format = Image::get_format_name(Object::cast_to<CompressedTexture2D>(*texture)->get_format());
  69. } else {
  70. format = texture->get_class();
  71. }
  72. const Ref<Image> image = texture->get_image();
  73. if (image.is_valid()) {
  74. const int mipmaps = image->get_mipmap_count();
  75. // Avoid signed integer overflow that could occur with huge texture sizes by casting everything to uint64_t.
  76. uint64_t memory = uint64_t(image->get_width()) * uint64_t(image->get_height()) * uint64_t(Image::get_format_pixel_size(image->get_format()));
  77. // Handle VRAM-compressed formats that are stored with 4 bpp.
  78. memory >>= Image::get_format_pixel_rshift(image->get_format());
  79. float mipmaps_multiplier = 1.0;
  80. float mipmap_increase = 0.25;
  81. for (int i = 0; i < mipmaps; i++) {
  82. // Each mip adds 25% memory usage of the previous one.
  83. // With a complete mipmap chain, memory usage increases by ~33%.
  84. mipmaps_multiplier += mipmap_increase;
  85. mipmap_increase *= 0.25;
  86. }
  87. memory *= mipmaps_multiplier;
  88. if (mipmaps >= 1) {
  89. metadata_label->set_text(
  90. vformat(String::utf8("%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
  91. texture->get_width(),
  92. texture->get_height(),
  93. format,
  94. mipmaps,
  95. String::humanize_size(memory)));
  96. } else {
  97. // "No Mipmaps" is easier to distinguish than "0 Mipmaps",
  98. // especially since 0, 6, and 8 look quite close with the default code font.
  99. metadata_label->set_text(
  100. vformat(String::utf8("%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
  101. texture->get_width(),
  102. texture->get_height(),
  103. format,
  104. String::humanize_size(memory)));
  105. }
  106. } else {
  107. metadata_label->set_text(
  108. vformat(String::utf8("%d×%d %s"),
  109. texture->get_width(),
  110. texture->get_height(),
  111. format));
  112. }
  113. }
  114. TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
  115. checkerboard = memnew(TextureRect);
  116. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  117. checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
  118. checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
  119. add_child(checkerboard);
  120. texture_display = memnew(TextureRect);
  121. texture_display->set_texture_filter(TEXTURE_FILTER_NEAREST_WITH_MIPMAPS);
  122. texture_display->set_texture(p_texture);
  123. texture_display->set_anchors_preset(TextureRect::PRESET_FULL_RECT);
  124. texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  125. texture_display->set_expand_mode(TextureRect::EXPAND_IGNORE_SIZE);
  126. add_child(texture_display);
  127. if (p_show_metadata) {
  128. metadata_label = memnew(Label);
  129. _update_metadata_label_text();
  130. p_texture->connect_changed(callable_mp(this, &TexturePreview::_update_metadata_label_text));
  131. // It's okay that these colors are static since the grid color is static too.
  132. metadata_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1));
  133. metadata_label->add_theme_color_override("font_shadow_color", Color(0, 0, 0));
  134. metadata_label->add_theme_font_size_override(SceneStringName(font_size), 14 * EDSCALE);
  135. metadata_label->add_theme_color_override("font_outline_color", Color(0, 0, 0));
  136. metadata_label->add_theme_constant_override("outline_size", 8 * EDSCALE);
  137. metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);
  138. metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);
  139. add_child(metadata_label);
  140. }
  141. }
  142. bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
  143. return Object::cast_to<ImageTexture>(p_object) != nullptr || Object::cast_to<AtlasTexture>(p_object) != nullptr || Object::cast_to<CompressedTexture2D>(p_object) != nullptr || Object::cast_to<PortableCompressedTexture2D>(p_object) != nullptr || Object::cast_to<AnimatedTexture>(p_object) != nullptr || Object::cast_to<Image>(p_object) != nullptr;
  144. }
  145. void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
  146. Ref<Texture> texture(Object::cast_to<Texture>(p_object));
  147. if (texture.is_null()) {
  148. Ref<Image> image(Object::cast_to<Image>(p_object));
  149. texture = ImageTexture::create_from_image(image);
  150. ERR_FAIL_COND_MSG(texture.is_null(), "Failed to create the texture from an invalid image.");
  151. }
  152. add_custom_control(memnew(TexturePreview(texture, true)));
  153. }
  154. TextureEditorPlugin::TextureEditorPlugin() {
  155. Ref<EditorInspectorPluginTexture> plugin;
  156. plugin.instantiate();
  157. add_inspector_plugin(plugin);
  158. }