animated_texture.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************/
  2. /* animated_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 "animated_texture.h"
  31. void AnimatedTexture::_update_proxy() {
  32. RWLockRead r(rw_lock);
  33. float delta;
  34. if (prev_ticks == 0) {
  35. delta = 0;
  36. prev_ticks = OS::get_singleton()->get_ticks_usec();
  37. } else {
  38. uint64_t ticks = OS::get_singleton()->get_ticks_usec();
  39. delta = float(double(ticks - prev_ticks) / 1000000.0);
  40. prev_ticks = ticks;
  41. }
  42. time += delta;
  43. float speed = speed_scale == 0 ? 0 : abs(1.0 / speed_scale);
  44. int iter_max = frame_count;
  45. while (iter_max && !pause) {
  46. float frame_limit = frames[current_frame].duration * speed;
  47. if (time > frame_limit) {
  48. if (speed_scale > 0.0) {
  49. current_frame++;
  50. } else {
  51. current_frame--;
  52. }
  53. if (current_frame >= frame_count) {
  54. if (one_shot) {
  55. current_frame = frame_count - 1;
  56. } else {
  57. current_frame = 0;
  58. }
  59. } else if (current_frame < 0) {
  60. if (one_shot) {
  61. current_frame = 0;
  62. } else {
  63. current_frame = frame_count - 1;
  64. }
  65. }
  66. time -= frame_limit;
  67. } else {
  68. break;
  69. }
  70. iter_max--;
  71. }
  72. if (frames[current_frame].texture.is_valid()) {
  73. RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid());
  74. }
  75. }
  76. void AnimatedTexture::set_frames(int p_frames) {
  77. ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);
  78. RWLockWrite r(rw_lock);
  79. frame_count = p_frames;
  80. }
  81. int AnimatedTexture::get_frames() const {
  82. return frame_count;
  83. }
  84. void AnimatedTexture::set_current_frame(int p_frame) {
  85. ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count);
  86. RWLockWrite r(rw_lock);
  87. current_frame = p_frame;
  88. time = 0;
  89. }
  90. int AnimatedTexture::get_current_frame() const {
  91. return current_frame;
  92. }
  93. void AnimatedTexture::set_pause(bool p_pause) {
  94. RWLockWrite r(rw_lock);
  95. pause = p_pause;
  96. }
  97. bool AnimatedTexture::get_pause() const {
  98. return pause;
  99. }
  100. void AnimatedTexture::set_one_shot(bool p_one_shot) {
  101. RWLockWrite r(rw_lock);
  102. one_shot = p_one_shot;
  103. }
  104. bool AnimatedTexture::get_one_shot() const {
  105. return one_shot;
  106. }
  107. void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) {
  108. ERR_FAIL_COND(p_texture == this);
  109. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  110. RWLockWrite w(rw_lock);
  111. frames[p_frame].texture = p_texture;
  112. }
  113. Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const {
  114. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>());
  115. RWLockRead r(rw_lock);
  116. return frames[p_frame].texture;
  117. }
  118. void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) {
  119. ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
  120. RWLockWrite r(rw_lock);
  121. frames[p_frame].duration = p_duration;
  122. }
  123. float AnimatedTexture::get_frame_duration(int p_frame) const {
  124. ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);
  125. RWLockRead r(rw_lock);
  126. return frames[p_frame].duration;
  127. }
  128. void AnimatedTexture::set_speed_scale(float p_scale) {
  129. ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000);
  130. RWLockWrite r(rw_lock);
  131. speed_scale = p_scale;
  132. }
  133. float AnimatedTexture::get_speed_scale() const {
  134. return speed_scale;
  135. }
  136. int AnimatedTexture::get_width() const {
  137. RWLockRead r(rw_lock);
  138. if (!frames[current_frame].texture.is_valid()) {
  139. return 1;
  140. }
  141. return frames[current_frame].texture->get_width();
  142. }
  143. int AnimatedTexture::get_height() const {
  144. RWLockRead r(rw_lock);
  145. if (!frames[current_frame].texture.is_valid()) {
  146. return 1;
  147. }
  148. return frames[current_frame].texture->get_height();
  149. }
  150. RID AnimatedTexture::get_rid() const {
  151. return proxy;
  152. }
  153. bool AnimatedTexture::has_alpha() const {
  154. RWLockRead r(rw_lock);
  155. if (!frames[current_frame].texture.is_valid()) {
  156. return false;
  157. }
  158. return frames[current_frame].texture->has_alpha();
  159. }
  160. Ref<Image> AnimatedTexture::get_image() const {
  161. RWLockRead r(rw_lock);
  162. if (!frames[current_frame].texture.is_valid()) {
  163. return Ref<Image>();
  164. }
  165. return frames[current_frame].texture->get_image();
  166. }
  167. bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {
  168. RWLockRead r(rw_lock);
  169. if (frames[current_frame].texture.is_valid()) {
  170. return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);
  171. }
  172. return true;
  173. }
  174. void AnimatedTexture::_validate_property(PropertyInfo &p_property) const {
  175. String prop = p_property.name;
  176. if (prop.begins_with("frame_")) {
  177. int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
  178. if (frame >= frame_count) {
  179. p_property.usage = PROPERTY_USAGE_NONE;
  180. }
  181. }
  182. }
  183. void AnimatedTexture::_bind_methods() {
  184. ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);
  185. ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);
  186. ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame);
  187. ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame);
  188. ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause);
  189. ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause);
  190. ClassDB::bind_method(D_METHOD("set_one_shot", "one_shot"), &AnimatedTexture::set_one_shot);
  191. ClassDB::bind_method(D_METHOD("get_one_shot"), &AnimatedTexture::get_one_shot);
  192. ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &AnimatedTexture::set_speed_scale);
  193. ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedTexture::get_speed_scale);
  194. ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);
  195. ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);
  196. ClassDB::bind_method(D_METHOD("set_frame_duration", "frame", "duration"), &AnimatedTexture::set_frame_duration);
  197. ClassDB::bind_method(D_METHOD("get_frame_duration", "frame"), &AnimatedTexture::get_frame_duration);
  198. ADD_PROPERTY(PropertyInfo(Variant::INT, "frames", PROPERTY_HINT_RANGE, "1," + itos(MAX_FRAMES), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_frames", "get_frames");
  199. ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_frame", "get_current_frame");
  200. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause");
  201. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
  202. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-60,60,0.1,or_less,or_greater"), "set_speed_scale", "get_speed_scale");
  203. for (int i = 0; i < MAX_FRAMES; i++) {
  204. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "frame_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_texture", "get_frame_texture", i);
  205. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "frame_" + itos(i) + "/duration", PROPERTY_HINT_RANGE, "0.0,16.0,0.01,or_greater,suffix:s", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_duration", "get_frame_duration", i);
  206. }
  207. BIND_CONSTANT(MAX_FRAMES);
  208. }
  209. void AnimatedTexture::_finish_non_thread_safe_setup() {
  210. RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));
  211. }
  212. AnimatedTexture::AnimatedTexture() {
  213. //proxy = RS::get_singleton()->texture_create();
  214. proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
  215. proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);
  216. RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);
  217. MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &AnimatedTexture::_finish_non_thread_safe_setup));
  218. }
  219. AnimatedTexture::~AnimatedTexture() {
  220. ERR_FAIL_NULL(RenderingServer::get_singleton());
  221. RS::get_singleton()->free(proxy);
  222. RS::get_singleton()->free(proxy_ph);
  223. }