texture_rect.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*************************************************************************/
  2. /* texture_rect.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/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_no_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_no_area()) {
  93. draw_texture_rect(texture, Rect2(offset, size), tile);
  94. } else {
  95. draw_texture_rect_region(texture, Rect2(offset, size), region);
  96. }
  97. } break;
  98. }
  99. }
  100. Size2 TextureRect::get_minimum_size() const {
  101. if (!ignore_texture_size && !texture.is_null()) {
  102. return texture->get_size();
  103. } else {
  104. return Size2();
  105. }
  106. }
  107. void TextureRect::_bind_methods() {
  108. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
  109. ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
  110. ClassDB::bind_method(D_METHOD("set_ignore_texture_size", "ignore"), &TextureRect::set_ignore_texture_size);
  111. ClassDB::bind_method(D_METHOD("get_ignore_texture_size"), &TextureRect::get_ignore_texture_size);
  112. ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
  113. ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
  114. ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
  115. ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
  116. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
  117. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
  118. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
  119. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_texture_size"), "set_ignore_texture_size", "get_ignore_texture_size");
  120. 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");
  121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  122. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  123. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  124. BIND_ENUM_CONSTANT(STRETCH_TILE);
  125. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  126. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  127. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  128. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  129. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  130. }
  131. void TextureRect::_texture_changed() {
  132. if (texture.is_valid()) {
  133. queue_redraw();
  134. update_minimum_size();
  135. }
  136. }
  137. void TextureRect::set_texture(const Ref<Texture2D> &p_tex) {
  138. if (p_tex == texture) {
  139. return;
  140. }
  141. if (texture.is_valid()) {
  142. texture->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureRect::_texture_changed));
  143. }
  144. texture = p_tex;
  145. if (texture.is_valid()) {
  146. texture->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureRect::_texture_changed));
  147. }
  148. queue_redraw();
  149. update_minimum_size();
  150. }
  151. Ref<Texture2D> TextureRect::get_texture() const {
  152. return texture;
  153. }
  154. void TextureRect::set_ignore_texture_size(bool p_ignore) {
  155. if (ignore_texture_size == p_ignore) {
  156. return;
  157. }
  158. ignore_texture_size = p_ignore;
  159. queue_redraw();
  160. update_minimum_size();
  161. }
  162. bool TextureRect::get_ignore_texture_size() const {
  163. return ignore_texture_size;
  164. }
  165. void TextureRect::set_stretch_mode(StretchMode p_mode) {
  166. if (stretch_mode == p_mode) {
  167. return;
  168. }
  169. stretch_mode = p_mode;
  170. queue_redraw();
  171. }
  172. TextureRect::StretchMode TextureRect::get_stretch_mode() const {
  173. return stretch_mode;
  174. }
  175. void TextureRect::set_flip_h(bool p_flip) {
  176. if (hflip == p_flip) {
  177. return;
  178. }
  179. hflip = p_flip;
  180. queue_redraw();
  181. }
  182. bool TextureRect::is_flipped_h() const {
  183. return hflip;
  184. }
  185. void TextureRect::set_flip_v(bool p_flip) {
  186. if (vflip == p_flip) {
  187. return;
  188. }
  189. vflip = p_flip;
  190. queue_redraw();
  191. }
  192. bool TextureRect::is_flipped_v() const {
  193. return vflip;
  194. }
  195. TextureRect::TextureRect() {
  196. set_mouse_filter(MOUSE_FILTER_PASS);
  197. }
  198. TextureRect::~TextureRect() {
  199. }