texture_rect.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/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. Ref<AtlasTexture> p_atlas = texture;
  87. if (p_atlas.is_valid() && region.has_no_area()) {
  88. Size2 scale_size(size.width / texture->get_width(), size.height / texture->get_height());
  89. offset.width += hflip ? p_atlas->get_margin().get_position().width * scale_size.width * 2 : 0;
  90. offset.height += vflip ? p_atlas->get_margin().get_position().height * scale_size.height * 2 : 0;
  91. }
  92. size.width *= hflip ? -1.0f : 1.0f;
  93. size.height *= vflip ? -1.0f : 1.0f;
  94. if (region.has_no_area()) {
  95. draw_texture_rect(texture, Rect2(offset, size), tile);
  96. } else {
  97. draw_texture_rect_region(texture, Rect2(offset, size), region);
  98. }
  99. }
  100. }
  101. Size2 TextureRect::get_minimum_size() const {
  102. if (!expand && !texture.is_null()) {
  103. return texture->get_size();
  104. } else {
  105. return Size2();
  106. }
  107. }
  108. void TextureRect::_bind_methods() {
  109. ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
  110. ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
  111. ClassDB::bind_method(D_METHOD("set_expand", "enable"), &TextureRect::set_expand);
  112. ClassDB::bind_method(D_METHOD("has_expand"), &TextureRect::has_expand);
  113. ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
  114. ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
  115. ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
  116. ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
  117. ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
  118. ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
  119. ClassDB::bind_method(D_METHOD("_texture_changed"), &TextureRect::_texture_changed);
  120. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  121. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
  122. 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");
  123. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
  124. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
  125. BIND_ENUM_CONSTANT(STRETCH_SCALE_ON_EXPAND);
  126. BIND_ENUM_CONSTANT(STRETCH_SCALE);
  127. BIND_ENUM_CONSTANT(STRETCH_TILE);
  128. BIND_ENUM_CONSTANT(STRETCH_KEEP);
  129. BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
  130. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
  131. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
  132. BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
  133. }
  134. void TextureRect::_texture_changed() {
  135. if (texture.is_valid()) {
  136. update();
  137. minimum_size_changed();
  138. }
  139. }
  140. void TextureRect::set_texture(const Ref<Texture> &p_tex) {
  141. if (p_tex == texture) {
  142. return;
  143. }
  144. if (texture.is_valid()) {
  145. texture->disconnect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
  146. }
  147. texture = p_tex;
  148. if (texture.is_valid()) {
  149. texture->connect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
  150. }
  151. update();
  152. minimum_size_changed();
  153. }
  154. Ref<Texture> TextureRect::get_texture() const {
  155. return texture;
  156. }
  157. void TextureRect::set_expand(bool p_expand) {
  158. expand = p_expand;
  159. update();
  160. minimum_size_changed();
  161. }
  162. bool TextureRect::has_expand() const {
  163. return expand;
  164. }
  165. void TextureRect::set_stretch_mode(StretchMode p_mode) {
  166. stretch_mode = p_mode;
  167. update();
  168. }
  169. TextureRect::StretchMode TextureRect::get_stretch_mode() const {
  170. return stretch_mode;
  171. }
  172. void TextureRect::set_flip_h(bool p_flip) {
  173. hflip = p_flip;
  174. update();
  175. }
  176. bool TextureRect::is_flipped_h() const {
  177. return hflip;
  178. }
  179. void TextureRect::set_flip_v(bool p_flip) {
  180. vflip = p_flip;
  181. update();
  182. }
  183. bool TextureRect::is_flipped_v() const {
  184. return vflip;
  185. }
  186. TextureRect::TextureRect() {
  187. expand = false;
  188. hflip = false;
  189. vflip = false;
  190. set_mouse_filter(MOUSE_FILTER_PASS);
  191. stretch_mode = STRETCH_SCALE_ON_EXPAND;
  192. }
  193. TextureRect::~TextureRect() {
  194. }