texture_rect.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**************************************************************************/
  2. /* texture_rect.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/rendering_server.h"
  32. void TextureRect::_notification(int p_what) {
  33. switch (p_what) {
  34. case 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: {
  44. size = get_size();
  45. } break;
  46. case STRETCH_TILE: {
  47. size = get_size();
  48. tile = true;
  49. } break;
  50. case STRETCH_KEEP: {
  51. size = texture->get_size();
  52. } break;
  53. case STRETCH_KEEP_CENTERED: {
  54. offset = (get_size() - texture->get_size()) / 2;
  55. size = texture->get_size();
  56. } break;
  57. case STRETCH_KEEP_ASPECT_CENTERED:
  58. case STRETCH_KEEP_ASPECT: {
  59. size = get_size();
  60. int tex_width = texture->get_width() * size.height / texture->get_height();
  61. int tex_height = size.height;
  62. if (tex_width > size.width) {
  63. tex_width = size.width;
  64. tex_height = texture->get_height() * tex_width / texture->get_width();
  65. }
  66. if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
  67. offset.x += (size.width - tex_width) / 2;
  68. offset.y += (size.height - tex_height) / 2;
  69. }
  70. size.width = tex_width;
  71. size.height = tex_height;
  72. } break;
  73. case STRETCH_KEEP_ASPECT_COVERED: {
  74. size = get_size();
  75. Size2 tex_size = texture->get_size();
  76. Size2 scale_size(size.width / tex_size.width, size.height / tex_size.height);
  77. float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
  78. Size2 scaled_tex_size = tex_size * scale;
  79. region.position = ((scaled_tex_size - size) / scale).abs() / 2.0f;
  80. region.size = size / scale;
  81. } break;
  82. }
  83. size.width *= hflip ? -1.0f : 1.0f;
  84. size.height *= vflip ? -1.0f : 1.0f;
  85. if (region.has_area()) {
  86. draw_texture_rect_region(texture, Rect2(offset, size), region);
  87. } else {
  88. draw_texture_rect(texture, Rect2(offset, size), tile);
  89. }
  90. } break;
  91. case NOTIFICATION_RESIZED: {
  92. update_minimum_size();
  93. } break;
  94. }
  95. }
  96. Size2 TextureRect::get_minimum_size() const {
  97. if (!texture.is_null()) {
  98. switch (expand_mode) {
  99. case EXPAND_KEEP_SIZE: {
  100. return texture->get_size();
  101. } break;
  102. case EXPAND_IGNORE_SIZE: {
  103. return Size2();
  104. } break;
  105. case EXPAND_FIT_WIDTH: {
  106. return Size2(get_size().y, 0);
  107. } break;
  108. case EXPAND_FIT_WIDTH_PROPORTIONAL: {
  109. real_t ratio = real_t(texture->get_width()) / texture->get_height();
  110. return Size2(get_size().y * ratio, 0);
  111. } break;
  112. case EXPAND_FIT_HEIGHT: {
  113. return Size2(0, get_size().x);
  114. } break;
  115. case EXPAND_FIT_HEIGHT_PROPORTIONAL: {
  116. real_t ratio = real_t(texture->get_height()) / texture->get_width();
  117. return Size2(0, get_size().x * ratio);
  118. } break;
  119. }
  120. }
  121. return Size2();
  122. }
  123. void TextureRect::_bind_methods() {
  124. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
  125. ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
  126. ClassDB::bind_method(D_METHOD("set_expand_mode", "expand_mode"), &TextureRect::set_expand_mode);
  127. ClassDB::bind_method(D_METHOD("get_expand_mode"), &TextureRect::get_expand_mode);
  128. ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
  129. ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
  130. ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
  131. ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
  132. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
  133. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
  134. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  135. ADD_PROPERTY(PropertyInfo(Variant::INT, "expand_mode", PROPERTY_HINT_ENUM, "Keep Size,Ignore Size,Fit Width,Fit Width Proportional,Fit Height,Fit Height Proportional"), "set_expand_mode", "get_expand_mode");
  136. ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode");
  137. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  138. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  139. BIND_ENUM_CONSTANT(EXPAND_KEEP_SIZE);
  140. BIND_ENUM_CONSTANT(EXPAND_IGNORE_SIZE);
  141. BIND_ENUM_CONSTANT(EXPAND_FIT_WIDTH);
  142. BIND_ENUM_CONSTANT(EXPAND_FIT_WIDTH_PROPORTIONAL);
  143. BIND_ENUM_CONSTANT(EXPAND_FIT_HEIGHT);
  144. BIND_ENUM_CONSTANT(EXPAND_FIT_HEIGHT_PROPORTIONAL);
  145. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  146. BIND_ENUM_CONSTANT(STRETCH_TILE);
  147. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  148. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  149. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  150. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  151. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  152. }
  153. #ifndef DISABLE_DEPRECATED
  154. bool TextureRect::_set(const StringName &p_name, const Variant &p_value) {
  155. if ((p_name == SNAME("expand") || p_name == SNAME("ignore_texture_size")) && p_value.operator bool()) {
  156. expand_mode = EXPAND_IGNORE_SIZE;
  157. return true;
  158. }
  159. return false;
  160. }
  161. #endif
  162. void TextureRect::_texture_changed() {
  163. queue_redraw();
  164. update_minimum_size();
  165. }
  166. void TextureRect::set_texture(const Ref<Texture2D> &p_tex) {
  167. if (p_tex == texture) {
  168. return;
  169. }
  170. if (texture.is_valid()) {
  171. texture->disconnect_changed(callable_mp(this, &TextureRect::_texture_changed));
  172. }
  173. texture = p_tex;
  174. if (texture.is_valid()) {
  175. texture->connect_changed(callable_mp(this, &TextureRect::_texture_changed));
  176. }
  177. queue_redraw();
  178. update_minimum_size();
  179. }
  180. Ref<Texture2D> TextureRect::get_texture() const {
  181. return texture;
  182. }
  183. void TextureRect::set_expand_mode(ExpandMode p_mode) {
  184. if (expand_mode == p_mode) {
  185. return;
  186. }
  187. expand_mode = p_mode;
  188. queue_redraw();
  189. update_minimum_size();
  190. }
  191. TextureRect::ExpandMode TextureRect::get_expand_mode() const {
  192. return expand_mode;
  193. }
  194. void TextureRect::set_stretch_mode(StretchMode p_mode) {
  195. if (stretch_mode == p_mode) {
  196. return;
  197. }
  198. stretch_mode = p_mode;
  199. queue_redraw();
  200. }
  201. TextureRect::StretchMode TextureRect::get_stretch_mode() const {
  202. return stretch_mode;
  203. }
  204. void TextureRect::set_flip_h(bool p_flip) {
  205. if (hflip == p_flip) {
  206. return;
  207. }
  208. hflip = p_flip;
  209. queue_redraw();
  210. }
  211. bool TextureRect::is_flipped_h() const {
  212. return hflip;
  213. }
  214. void TextureRect::set_flip_v(bool p_flip) {
  215. if (vflip == p_flip) {
  216. return;
  217. }
  218. vflip = p_flip;
  219. queue_redraw();
  220. }
  221. bool TextureRect::is_flipped_v() const {
  222. return vflip;
  223. }
  224. TextureRect::TextureRect() {
  225. set_mouse_filter(MOUSE_FILTER_PASS);
  226. }
  227. TextureRect::~TextureRect() {
  228. }