texture_rect.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************/
  2. /* texture_rect.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_rect.h"
  31. #include "core/core_string_names.h"
  32. #include "servers/visual_server.h"
  33. void TextureRect::_notification(int p_what) {
  34. if (p_what == NOTIFICATION_DRAW) {
  35. if (texture.is_null()) {
  36. return;
  37. }
  38. Size2 size;
  39. Point2 offset;
  40. Rect2 region;
  41. bool tile = false;
  42. switch (stretch_mode) {
  43. case STRETCH_SCALE_ON_EXPAND: {
  44. size = expand ? get_size() : texture->get_size();
  45. } break;
  46. case STRETCH_SCALE: {
  47. size = get_size();
  48. } break;
  49. case STRETCH_TILE: {
  50. size = get_size();
  51. tile = true;
  52. } break;
  53. case STRETCH_KEEP: {
  54. size = texture->get_size();
  55. } break;
  56. case STRETCH_KEEP_CENTERED: {
  57. offset = (get_size() - texture->get_size()) / 2;
  58. size = texture->get_size();
  59. } break;
  60. case STRETCH_KEEP_ASPECT_CENTERED:
  61. case STRETCH_KEEP_ASPECT: {
  62. size = get_size();
  63. int tex_width = texture->get_width() * size.height / texture->get_height();
  64. int tex_height = size.height;
  65. if (tex_width > size.width) {
  66. tex_width = size.width;
  67. tex_height = texture->get_height() * tex_width / texture->get_width();
  68. }
  69. if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
  70. offset.x += (size.width - tex_width) / 2;
  71. offset.y += (size.height - tex_height) / 2;
  72. }
  73. size.width = tex_width;
  74. size.height = tex_height;
  75. } break;
  76. case STRETCH_KEEP_ASPECT_COVERED: {
  77. size = get_size();
  78. Size2 tex_size = texture->get_size();
  79. Size2 scale_size(size.width / tex_size.width, size.height / tex_size.height);
  80. float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
  81. Size2 scaled_tex_size = tex_size * scale;
  82. region.position = ((scaled_tex_size - size) / scale).abs() / 2.0f;
  83. region.size = size / scale;
  84. } break;
  85. }
  86. size.width *= hflip ? -1.0f : 1.0f;
  87. size.height *= vflip ? -1.0f : 1.0f;
  88. if (region.has_no_area()) {
  89. draw_texture_rect(texture, Rect2(offset, size), tile);
  90. } else {
  91. draw_texture_rect_region(texture, Rect2(offset, size), region);
  92. }
  93. }
  94. }
  95. Size2 TextureRect::get_minimum_size() const {
  96. if (!expand && !texture.is_null()) {
  97. return texture->get_size();
  98. } else {
  99. return Size2();
  100. }
  101. }
  102. void TextureRect::_bind_methods() {
  103. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
  104. ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
  105. ClassDB::bind_method(D_METHOD("set_expand", "enable"), &TextureRect::set_expand);
  106. ClassDB::bind_method(D_METHOD("has_expand"), &TextureRect::has_expand);
  107. ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
  108. ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
  109. ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
  110. ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
  111. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
  112. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
  113. ClassDB::bind_method(D_METHOD("_texture_changed"), &TextureRect::_texture_changed);
  114. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  115. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
  116. ADD_PROPERTY(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");
  117. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  118. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  119. BIND_ENUM_CONSTANT(STRETCH_SCALE_ON_EXPAND);
  120. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  121. BIND_ENUM_CONSTANT(STRETCH_TILE);
  122. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  123. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  124. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  125. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  126. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  127. }
  128. void TextureRect::_texture_changed() {
  129. if (texture.is_valid()) {
  130. update();
  131. minimum_size_changed();
  132. }
  133. }
  134. void TextureRect::set_texture(const Ref<Texture> &p_tex) {
  135. if (p_tex == texture) {
  136. return;
  137. }
  138. if (texture.is_valid()) {
  139. texture->disconnect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
  140. }
  141. texture = p_tex;
  142. if (texture.is_valid()) {
  143. texture->connect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
  144. }
  145. update();
  146. minimum_size_changed();
  147. }
  148. Ref<Texture> TextureRect::get_texture() const {
  149. return texture;
  150. }
  151. void TextureRect::set_expand(bool p_expand) {
  152. expand = p_expand;
  153. update();
  154. minimum_size_changed();
  155. }
  156. bool TextureRect::has_expand() const {
  157. return expand;
  158. }
  159. void TextureRect::set_stretch_mode(StretchMode p_mode) {
  160. stretch_mode = p_mode;
  161. update();
  162. }
  163. TextureRect::StretchMode TextureRect::get_stretch_mode() const {
  164. return stretch_mode;
  165. }
  166. void TextureRect::set_flip_h(bool p_flip) {
  167. hflip = p_flip;
  168. update();
  169. }
  170. bool TextureRect::is_flipped_h() const {
  171. return hflip;
  172. }
  173. void TextureRect::set_flip_v(bool p_flip) {
  174. vflip = p_flip;
  175. update();
  176. }
  177. bool TextureRect::is_flipped_v() const {
  178. return vflip;
  179. }
  180. TextureRect::TextureRect() {
  181. expand = false;
  182. hflip = false;
  183. vflip = false;
  184. set_mouse_filter(MOUSE_FILTER_PASS);
  185. stretch_mode = STRETCH_SCALE_ON_EXPAND;
  186. }
  187. TextureRect::~TextureRect() {
  188. }