atlas_texture.cpp 8.0 KB

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