texture_editor_plugin.cpp 7.9 KB

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