texture_editor_plugin.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. void TexturePreview::_update_metadata_label_text() {
  55. const Ref<Texture2D> texture = texture_display->get_texture();
  56. String format;
  57. if (Object::cast_to<ImageTexture>(*texture)) {
  58. format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format());
  59. } else if (Object::cast_to<CompressedTexture2D>(*texture)) {
  60. format = Image::get_format_name(Object::cast_to<CompressedTexture2D>(*texture)->get_format());
  61. } else {
  62. format = texture->get_class();
  63. }
  64. const Ref<Image> image = texture->get_image();
  65. if (image.is_valid()) {
  66. const int mipmaps = image->get_mipmap_count();
  67. // Avoid signed integer overflow that could occur with huge texture sizes by casting everything to uint64_t.
  68. uint64_t memory = uint64_t(image->get_width()) * uint64_t(image->get_height()) * uint64_t(Image::get_format_pixel_size(image->get_format()));
  69. // Handle VRAM-compressed formats that are stored with 4 bpp.
  70. memory >>= Image::get_format_pixel_rshift(image->get_format());
  71. float mipmaps_multiplier = 1.0;
  72. float mipmap_increase = 0.25;
  73. for (int i = 0; i < mipmaps; i++) {
  74. // Each mip adds 25% memory usage of the previous one.
  75. // With a complete mipmap chain, memory usage increases by ~33%.
  76. mipmaps_multiplier += mipmap_increase;
  77. mipmap_increase *= 0.25;
  78. }
  79. memory *= mipmaps_multiplier;
  80. if (mipmaps >= 1) {
  81. metadata_label->set_text(
  82. vformat(String::utf8("%d×%d %s\n") + TTR("%s Mipmaps") + "\n" + TTR("Memory: %s"),
  83. texture->get_width(),
  84. texture->get_height(),
  85. format,
  86. mipmaps,
  87. String::humanize_size(memory)));
  88. } else {
  89. // "No Mipmaps" is easier to distinguish than "0 Mipmaps",
  90. // especially since 0, 6, and 8 look quite close with the default code font.
  91. metadata_label->set_text(
  92. vformat(String::utf8("%d×%d %s\n") + TTR("No Mipmaps") + "\n" + TTR("Memory: %s"),
  93. texture->get_width(),
  94. texture->get_height(),
  95. format,
  96. String::humanize_size(memory)));
  97. }
  98. } else {
  99. metadata_label->set_text(
  100. vformat(String::utf8("%d×%d %s"),
  101. texture->get_width(),
  102. texture->get_height(),
  103. format));
  104. }
  105. }
  106. TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
  107. checkerboard = memnew(TextureRect);
  108. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  109. checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
  110. checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
  111. add_child(checkerboard);
  112. texture_display = memnew(TextureRect);
  113. texture_display->set_texture(p_texture);
  114. texture_display->set_anchors_preset(TextureRect::PRESET_FULL_RECT);
  115. texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  116. texture_display->set_ignore_texture_size(true);
  117. add_child(texture_display);
  118. if (p_show_metadata) {
  119. metadata_label = memnew(Label);
  120. _update_metadata_label_text();
  121. p_texture->connect("changed", callable_mp(this, &TexturePreview::_update_metadata_label_text));
  122. // It's okay that these colors are static since the grid color is static too.
  123. metadata_label->add_theme_color_override("font_color", Color::named("white"));
  124. metadata_label->add_theme_color_override("font_shadow_color", Color::named("black"));
  125. metadata_label->add_theme_font_size_override("font_size", 14 * EDSCALE);
  126. metadata_label->add_theme_color_override("font_outline_color", Color::named("black"));
  127. metadata_label->add_theme_constant_override("outline_size", 8 * EDSCALE);
  128. metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);
  129. metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);
  130. add_child(metadata_label);
  131. }
  132. }
  133. bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
  134. 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;
  135. }
  136. void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
  137. Ref<Texture> texture(Object::cast_to<Texture>(p_object));
  138. add_custom_control(memnew(TexturePreview(texture, true)));
  139. }
  140. TextureEditorPlugin::TextureEditorPlugin() {
  141. Ref<EditorInspectorPluginTexture> plugin;
  142. plugin.instantiate();
  143. add_inspector_plugin(plugin);
  144. }