texture_editor_plugin.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "core/io/resource_loader.h"
  32. #include "core/project_settings.h"
  33. #include "editor/editor_settings.h"
  34. void TextureEditor::_gui_input(Ref<InputEvent> p_event) {
  35. }
  36. void TextureEditor::_notification(int p_what) {
  37. if (p_what == NOTIFICATION_READY) {
  38. //get_scene()->connect("node_removed",this,"_node_removed");
  39. }
  40. if (p_what == NOTIFICATION_DRAW) {
  41. Ref<Texture> checkerboard = get_icon("Checkerboard", "EditorIcons");
  42. Size2 size = get_size();
  43. draw_texture_rect(checkerboard, Rect2(Point2(), size), true);
  44. int tex_width = texture->get_width() * size.height / texture->get_height();
  45. int tex_height = size.height;
  46. if (tex_width > size.width) {
  47. tex_width = size.width;
  48. tex_height = texture->get_height() * tex_width / texture->get_width();
  49. }
  50. // Prevent the texture from being unpreviewable after the rescale, so that we can still see something
  51. if (tex_height <= 0)
  52. tex_height = 1;
  53. if (tex_width <= 0)
  54. tex_width = 1;
  55. int ofs_x = (size.width - tex_width) / 2;
  56. int ofs_y = (size.height - tex_height) / 2;
  57. if (Object::cast_to<CurveTexture>(*texture)) {
  58. // In the case of CurveTextures we know they are 1 in height, so fill the preview to see the gradient
  59. ofs_y = 0;
  60. tex_height = size.height;
  61. } else if (Object::cast_to<GradientTexture>(*texture)) {
  62. ofs_y = size.height / 4.0;
  63. tex_height = size.height / 2.0;
  64. }
  65. draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height));
  66. Ref<Font> font = get_font("font", "Label");
  67. String format;
  68. if (Object::cast_to<ImageTexture>(*texture)) {
  69. format = Image::get_format_name(Object::cast_to<ImageTexture>(*texture)->get_format());
  70. } else if (Object::cast_to<StreamTexture>(*texture)) {
  71. format = Image::get_format_name(Object::cast_to<StreamTexture>(*texture)->get_format());
  72. } else {
  73. format = texture->get_class();
  74. }
  75. String text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format;
  76. Size2 rect = font->get_string_size(text);
  77. Vector2 draw_from = size - rect + Size2(-2, font->get_ascent() - 2);
  78. if (draw_from.x < 0)
  79. draw_from.x = 0;
  80. draw_string(font, draw_from + Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
  81. draw_string(font, draw_from - Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
  82. draw_string(font, draw_from, text, Color(1, 1, 1, 1), size.width);
  83. }
  84. }
  85. void TextureEditor::_changed_callback(Object *p_changed, const char *p_prop) {
  86. if (!is_visible())
  87. return;
  88. update();
  89. }
  90. void TextureEditor::edit(Ref<Texture> p_texture) {
  91. if (!texture.is_null())
  92. texture->remove_change_receptor(this);
  93. texture = p_texture;
  94. if (!texture.is_null()) {
  95. texture->add_change_receptor(this);
  96. update();
  97. } else {
  98. hide();
  99. }
  100. }
  101. void TextureEditor::_bind_methods() {
  102. ClassDB::bind_method(D_METHOD("_gui_input"), &TextureEditor::_gui_input);
  103. }
  104. TextureEditor::TextureEditor() {
  105. set_custom_minimum_size(Size2(1, 150));
  106. }
  107. TextureEditor::~TextureEditor() {
  108. if (!texture.is_null()) {
  109. texture->remove_change_receptor(this);
  110. }
  111. }
  112. //
  113. bool EditorInspectorPluginTexture::can_handle(Object *p_object) {
  114. return Object::cast_to<ImageTexture>(p_object) != NULL || Object::cast_to<AtlasTexture>(p_object) != NULL || Object::cast_to<StreamTexture>(p_object) != NULL || Object::cast_to<LargeTexture>(p_object) != NULL || Object::cast_to<AnimatedTexture>(p_object) != NULL;
  115. }
  116. void EditorInspectorPluginTexture::parse_begin(Object *p_object) {
  117. Texture *texture = Object::cast_to<Texture>(p_object);
  118. if (!texture) {
  119. return;
  120. }
  121. Ref<Texture> m(texture);
  122. TextureEditor *editor = memnew(TextureEditor);
  123. editor->edit(m);
  124. add_custom_control(editor);
  125. }
  126. TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
  127. Ref<EditorInspectorPluginTexture> plugin;
  128. plugin.instance();
  129. add_inspector_plugin(plugin);
  130. }