texture_rect.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*************************************************************************/
  2. /* texture_rect.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_rect.h"
  31. #include "servers/visual_server.h"
  32. void TextureRect::_notification(int p_what) {
  33. if (p_what == NOTIFICATION_DRAW) {
  34. if (texture.is_null())
  35. return;
  36. switch (stretch_mode) {
  37. case STRETCH_SCALE_ON_EXPAND: {
  38. Size2 s = expand ? get_size() : texture->get_size();
  39. draw_texture_rect(texture, Rect2(Point2(), s), false);
  40. } break;
  41. case STRETCH_SCALE: {
  42. draw_texture_rect(texture, Rect2(Point2(), get_size()), false);
  43. } break;
  44. case STRETCH_TILE: {
  45. draw_texture_rect(texture, Rect2(Point2(), get_size()), true);
  46. } break;
  47. case STRETCH_KEEP: {
  48. draw_texture_rect(texture, Rect2(Point2(), texture->get_size()), false);
  49. } break;
  50. case STRETCH_KEEP_CENTERED: {
  51. Vector2 ofs = (get_size() - texture->get_size()) / 2;
  52. draw_texture_rect(texture, Rect2(ofs, texture->get_size()), false);
  53. } break;
  54. case STRETCH_KEEP_ASPECT_CENTERED:
  55. case STRETCH_KEEP_ASPECT: {
  56. Size2 size = get_size();
  57. int tex_width = texture->get_width() * size.height / texture->get_height();
  58. int tex_height = size.height;
  59. if (tex_width > size.width) {
  60. tex_width = size.width;
  61. tex_height = texture->get_height() * tex_width / texture->get_width();
  62. }
  63. int ofs_x = 0;
  64. int ofs_y = 0;
  65. if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
  66. ofs_x += (size.width - tex_width) / 2;
  67. ofs_y += (size.height - tex_height) / 2;
  68. }
  69. draw_texture_rect(texture, Rect2(ofs_x, ofs_y, tex_width, tex_height));
  70. } break;
  71. case STRETCH_KEEP_ASPECT_COVERED: {
  72. Size2 size = get_size();
  73. Size2 tex_size = texture->get_size();
  74. Size2 scaleSize(size.width / tex_size.width, size.height / tex_size.height);
  75. float scale = scaleSize.width > scaleSize.height ? scaleSize.width : scaleSize.height;
  76. Size2 scaledTexSize = tex_size * scale;
  77. Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f;
  78. draw_texture_rect_region(texture, Rect2(Point2(), size), Rect2(ofs, size / scale));
  79. } break;
  80. }
  81. }
  82. }
  83. Size2 TextureRect::get_minimum_size() const {
  84. if (!expand && !texture.is_null())
  85. return texture->get_size();
  86. else
  87. return Size2();
  88. }
  89. void TextureRect::_bind_methods() {
  90. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
  91. ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
  92. ClassDB::bind_method(D_METHOD("set_expand", "enable"), &TextureRect::set_expand);
  93. ClassDB::bind_method(D_METHOD("has_expand"), &TextureRect::has_expand);
  94. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
  95. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
  96. ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  97. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
  98. ADD_PROPERTYNO(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode");
  99. BIND_ENUM_CONSTANT(STRETCH_SCALE_ON_EXPAND);
  100. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  101. BIND_ENUM_CONSTANT(STRETCH_TILE);
  102. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  103. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  104. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  105. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  106. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  107. }
  108. void TextureRect::set_texture(const Ref<Texture> &p_tex) {
  109. texture = p_tex;
  110. update();
  111. /*
  112. if (texture.is_valid())
  113. texture->set_flags(texture->get_flags()&(~Texture::FLAG_REPEAT)); //remove repeat from texture, it looks bad in sprites
  114. */
  115. minimum_size_changed();
  116. }
  117. Ref<Texture> TextureRect::get_texture() const {
  118. return texture;
  119. }
  120. void TextureRect::set_expand(bool p_expand) {
  121. expand = p_expand;
  122. update();
  123. minimum_size_changed();
  124. }
  125. bool TextureRect::has_expand() const {
  126. return expand;
  127. }
  128. void TextureRect::set_stretch_mode(StretchMode p_mode) {
  129. stretch_mode = p_mode;
  130. update();
  131. }
  132. TextureRect::StretchMode TextureRect::get_stretch_mode() const {
  133. return stretch_mode;
  134. }
  135. TextureRect::TextureRect() {
  136. expand = false;
  137. set_mouse_filter(MOUSE_FILTER_PASS);
  138. stretch_mode = STRETCH_SCALE_ON_EXPAND;
  139. }
  140. TextureRect::~TextureRect() {
  141. }