texture_editor_plugin.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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_settings.h"
  32. #include "globals.h"
  33. #include "io/resource_loader.h"
  34. void TextureEditor::_input_event(InputEvent p_event) {
  35. }
  36. void TextureEditor::_notification(int p_what) {
  37. if (p_what == NOTIFICATION_FIXED_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. int ofs_x = (size.width - tex_width) / 2;
  53. int ofs_y = (size.height - tex_height) / 2;
  54. draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height));
  55. Ref<Font> font = get_font("font", "Label");
  56. String format;
  57. if (texture->cast_to<ImageTexture>()) {
  58. format = Image::get_format_name(texture->cast_to<ImageTexture>()->get_format());
  59. } else {
  60. format = texture->get_type();
  61. }
  62. String text = itos(texture->get_width()) + "x" + itos(texture->get_height()) + " " + format;
  63. Size2 rect = font->get_string_size(text);
  64. Vector2 draw_from = size - rect + Size2(-2, font->get_ascent() - 2);
  65. if (draw_from.x < 0)
  66. draw_from.x = 0;
  67. draw_string(font, draw_from + Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
  68. draw_string(font, draw_from - Vector2(2, 2), text, Color(0, 0, 0, 0.5), size.width);
  69. draw_string(font, draw_from, text, Color(1, 1, 1, 1), size.width);
  70. }
  71. }
  72. void TextureEditor::edit(Ref<Texture> p_texture) {
  73. texture = p_texture;
  74. if (!texture.is_null())
  75. update();
  76. else {
  77. hide();
  78. }
  79. }
  80. void TextureEditor::_bind_methods() {
  81. ObjectTypeDB::bind_method(_MD("_input_event"), &TextureEditor::_input_event);
  82. }
  83. TextureEditor::TextureEditor() {
  84. set_custom_minimum_size(Size2(1, 150));
  85. }
  86. void TextureEditorPlugin::edit(Object *p_object) {
  87. Texture *s = p_object->cast_to<Texture>();
  88. if (!s)
  89. return;
  90. texture_editor->edit(Ref<Texture>(s));
  91. }
  92. bool TextureEditorPlugin::handles(Object *p_object) const {
  93. return p_object->is_type("Texture");
  94. }
  95. void TextureEditorPlugin::make_visible(bool p_visible) {
  96. if (p_visible) {
  97. texture_editor->show();
  98. // texture_editor->set_process(true);
  99. } else {
  100. texture_editor->hide();
  101. // texture_editor->set_process(false);
  102. }
  103. }
  104. TextureEditorPlugin::TextureEditorPlugin(EditorNode *p_node) {
  105. editor = p_node;
  106. texture_editor = memnew(TextureEditor);
  107. add_control_to_container(CONTAINER_PROPERTY_EDITOR_BOTTOM, texture_editor);
  108. texture_editor->hide();
  109. }
  110. TextureEditorPlugin::~TextureEditorPlugin() {
  111. }