texture_editor_plugin.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. TexturePreview::TexturePreview(Ref<Texture2D> p_texture, bool p_show_metadata) {
  36. Ref<Theme> theme = EditorNode::get_singleton()->get_editor_theme();
  37. TextureRect *checkerboard = memnew(TextureRect);
  38. checkerboard->set_texture(theme->get_icon("Checkerboard", "EditorIcons"));
  39. checkerboard->set_stretch_mode(TextureRect::STRETCH_TILE);
  40. checkerboard->set_texture_repeat(CanvasItem::TEXTURE_REPEAT_ENABLED);
  41. checkerboard->set_custom_minimum_size(Size2(0.0, 256.0) * EDSCALE);
  42. add_child(checkerboard);
  43. texture_display = memnew(TextureRect);
  44. texture_display->set_texture(p_texture);
  45. texture_display->set_anchors_preset(TextureRect::PRESET_WIDE);
  46. texture_display->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  47. texture_display->set_expand(true);
  48. add_child(texture_display);
  49. if (p_show_metadata) {
  50. Label *metadata_label = memnew(Label);
  51. String format;
  52. if (Object::cast_to<ImageTexture>(*p_texture)) {
  53. format = Image::get_format_name(Object::cast_to<ImageTexture>(*p_texture)->get_format());
  54. } else if (Object::cast_to<StreamTexture2D>(*p_texture)) {
  55. format = Image::get_format_name(Object::cast_to<StreamTexture2D>(*p_texture)->get_format());
  56. } else {
  57. format = p_texture->get_class();
  58. }
  59. metadata_label->set_text(itos(p_texture->get_width()) + "x" + itos(p_texture->get_height()) + " " + format);
  60. metadata_label->add_theme_font_size_override("font_size", 16 * EDSCALE);
  61. metadata_label->add_theme_color_override("font_outline_color", Color::named("black"));
  62. metadata_label->add_theme_constant_override("outline_size", 2 * EDSCALE);
  63. Ref<Font> metadata_label_font = theme->get_font("expression", "EditorFonts");
  64. metadata_label->add_theme_font_override("font", metadata_label_font);
  65. // it's okay that these colors are static since the grid color is static too
  66. metadata_label->add_theme_color_override("font_color", Color::named("white"));
  67. metadata_label->add_theme_color_override("font_color_shadow", Color::named("black"));
  68. metadata_label->add_theme_constant_override("shadow_as_outline", 1);
  69. metadata_label->set_h_size_flags(Control::SIZE_SHRINK_END);
  70. metadata_label->set_v_size_flags(Control::SIZE_SHRINK_END);
  71. add_child(metadata_label);
  72. }
  73. }
  74. bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
  75. 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;
  76. }
  77. void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
  78. Ref<Texture> texture(Object::cast_to<Texture>(p_object));
  79. add_custom_control(memnew(TexturePreview(texture, true)));
  80. }
  81. TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
  82. Ref<EditorInspectorPluginTexture> plugin;
  83. plugin.instantiate();
  84. add_inspector_plugin(plugin);
  85. }