atlas_texture.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**************************************************************************/
  2. /* atlas_texture.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 "atlas_texture.h"
  31. int AtlasTexture::get_width() const {
  32. if (rounded_region.size.width == 0) {
  33. if (atlas.is_valid()) {
  34. return atlas->get_width();
  35. }
  36. return 1;
  37. } else {
  38. return rounded_region.size.width + margin.size.width;
  39. }
  40. }
  41. int AtlasTexture::get_height() const {
  42. if (rounded_region.size.height == 0) {
  43. if (atlas.is_valid()) {
  44. return atlas->get_height();
  45. }
  46. return 1;
  47. } else {
  48. return rounded_region.size.height + margin.size.height;
  49. }
  50. }
  51. RID AtlasTexture::get_rid() const {
  52. if (atlas.is_valid()) {
  53. return atlas->get_rid();
  54. }
  55. return RID();
  56. }
  57. bool AtlasTexture::has_alpha() const {
  58. if (atlas.is_valid()) {
  59. return atlas->has_alpha();
  60. }
  61. return false;
  62. }
  63. void AtlasTexture::set_atlas(const Ref<Texture2D> &p_atlas) {
  64. ERR_FAIL_COND(p_atlas == this);
  65. if (atlas == p_atlas) {
  66. return;
  67. }
  68. // Support recursive AtlasTextures.
  69. if (Ref<AtlasTexture>(atlas).is_valid()) {
  70. atlas->disconnect_changed(callable_mp((Resource *)this, &AtlasTexture::emit_changed));
  71. }
  72. atlas = p_atlas;
  73. if (Ref<AtlasTexture>(atlas).is_valid()) {
  74. atlas->connect_changed(callable_mp((Resource *)this, &AtlasTexture::emit_changed));
  75. }
  76. emit_changed();
  77. }
  78. Ref<Texture2D> AtlasTexture::get_atlas() const {
  79. return atlas;
  80. }
  81. void AtlasTexture::set_region(const Rect2 &p_region) {
  82. if (region == p_region) {
  83. return;
  84. }
  85. region = p_region;
  86. rounded_region = Rect2(p_region.position, p_region.size.floor());
  87. emit_changed();
  88. }
  89. Rect2 AtlasTexture::get_region() const {
  90. return region;
  91. }
  92. void AtlasTexture::set_margin(const Rect2 &p_margin) {
  93. if (margin == p_margin) {
  94. return;
  95. }
  96. margin = p_margin;
  97. emit_changed();
  98. }
  99. Rect2 AtlasTexture::get_margin() const {
  100. return margin;
  101. }
  102. void AtlasTexture::set_filter_clip(const bool p_enable) {
  103. filter_clip = p_enable;
  104. emit_changed();
  105. }
  106. bool AtlasTexture::has_filter_clip() const {
  107. return filter_clip;
  108. }
  109. Rect2 AtlasTexture::_get_region_rect() const {
  110. Rect2 rc = rounded_region;
  111. if (atlas.is_valid()) {
  112. if (rc.size.width == 0) {
  113. rc.size.width = atlas->get_width();
  114. }
  115. if (rc.size.height == 0) {
  116. rc.size.height = atlas->get_height();
  117. }
  118. }
  119. return rc;
  120. }
  121. void AtlasTexture::_bind_methods() {
  122. ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
  123. ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
  124. ClassDB::bind_method(D_METHOD("set_region", "region"), &AtlasTexture::set_region);
  125. ClassDB::bind_method(D_METHOD("get_region"), &AtlasTexture::get_region);
  126. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &AtlasTexture::set_margin);
  127. ClassDB::bind_method(D_METHOD("get_margin"), &AtlasTexture::get_margin);
  128. ClassDB::bind_method(D_METHOD("set_filter_clip", "enable"), &AtlasTexture::set_filter_clip);
  129. ClassDB::bind_method(D_METHOD("has_filter_clip"), &AtlasTexture::has_filter_clip);
  130. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_atlas", "get_atlas");
  131. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region", PROPERTY_HINT_NONE, "suffix:px"), "set_region", "get_region");
  132. ADD_PROPERTY(PropertyInfo(Variant::RECT2, "margin", PROPERTY_HINT_NONE, "suffix:px"), "set_margin", "get_margin");
  133. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_clip"), "set_filter_clip", "has_filter_clip");
  134. }
  135. void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  136. if (atlas.is_null()) {
  137. return;
  138. }
  139. const Rect2 rc = _get_region_rect();
  140. atlas->draw_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), rc, p_modulate, p_transpose, filter_clip);
  141. }
  142. void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  143. if (atlas.is_null()) {
  144. return;
  145. }
  146. Rect2 src_rect = Rect2(0, 0, get_width(), get_height());
  147. Rect2 dr;
  148. Rect2 src_c;
  149. if (get_rect_region(p_rect, src_rect, dr, src_c)) {
  150. atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
  151. }
  152. }
  153. void AtlasTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
  154. // This might not necessarily work well if using a rect, needs to be fixed properly.
  155. if (atlas.is_null()) {
  156. return;
  157. }
  158. Rect2 dr;
  159. Rect2 src_c;
  160. if (get_rect_region(p_rect, p_src_rect, dr, src_c)) {
  161. atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
  162. }
  163. }
  164. bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  165. if (atlas.is_null()) {
  166. return false;
  167. }
  168. Rect2 src = p_src_rect;
  169. if (src.size == Size2()) {
  170. src.size = rounded_region.size;
  171. }
  172. if (src.size == Size2() && atlas.is_valid()) {
  173. src.size = atlas->get_size();
  174. }
  175. Vector2 scale = p_rect.size / src.size;
  176. src.position += (rounded_region.position - margin.position);
  177. Rect2 src_clipped = _get_region_rect().intersection(src);
  178. if (src_clipped.size == Size2()) {
  179. return false;
  180. }
  181. Vector2 ofs = (src_clipped.position - src.position);
  182. if (scale.x < 0) {
  183. ofs.x += (src_clipped.size.x - src.size.x);
  184. }
  185. if (scale.y < 0) {
  186. ofs.y += (src_clipped.size.y - src.size.y);
  187. }
  188. r_rect = Rect2(p_rect.position + ofs * scale, src_clipped.size * scale);
  189. r_src_rect = src_clipped;
  190. return true;
  191. }
  192. bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
  193. if (atlas.is_null()) {
  194. return true;
  195. }
  196. int x = p_x + rounded_region.position.x - margin.position.x;
  197. int y = p_y + rounded_region.position.y - margin.position.y;
  198. // Margin edge may outside of atlas.
  199. if (x < 0 || x >= atlas->get_width()) {
  200. return false;
  201. }
  202. if (y < 0 || y >= atlas->get_height()) {
  203. return false;
  204. }
  205. return atlas->is_pixel_opaque(x, y);
  206. }
  207. Ref<Image> AtlasTexture::get_image() const {
  208. if (atlas.is_null()) {
  209. return Ref<Image>();
  210. }
  211. const Ref<Image> &atlas_image = atlas->get_image();
  212. if (atlas_image.is_null()) {
  213. return Ref<Image>();
  214. }
  215. return atlas_image->get_region(_get_region_rect());
  216. }