texture_rect.cpp 8.7 KB

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