texture_editor_plugin.cpp 5.8 KB

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