texture_rect.cpp 9.2 KB

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