animation_player.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*************************************************************************/
  2. /* animation_player.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #ifndef ANIMATION_PLAYER_H
  31. #define ANIMATION_PLAYER_H
  32. #include "scene/2d/node_2d.h"
  33. #include "scene/3d/skeleton.h"
  34. #include "scene/3d/spatial.h"
  35. #include "scene/resources/animation.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. class AnimationPlayer : public Node {
  40. GDCLASS(AnimationPlayer, Node);
  41. OBJ_CATEGORY("Animation Nodes");
  42. public:
  43. enum AnimationProcessMode {
  44. ANIMATION_PROCESS_PHYSICS,
  45. ANIMATION_PROCESS_IDLE,
  46. };
  47. private:
  48. enum {
  49. NODE_CACHE_UPDATE_MAX = 1024,
  50. BLEND_FROM_MAX = 3
  51. };
  52. enum SpecialProperty {
  53. SP_NONE,
  54. SP_NODE2D_POS,
  55. SP_NODE2D_ROT,
  56. SP_NODE2D_SCALE,
  57. };
  58. struct TrackNodeCache {
  59. NodePath path;
  60. uint32_t id;
  61. RES resource;
  62. Node *node;
  63. Spatial *spatial;
  64. Node2D *node_2d;
  65. Skeleton *skeleton;
  66. int bone_idx;
  67. // accumulated transforms
  68. Vector3 loc_accum;
  69. Quat rot_accum;
  70. Vector3 scale_accum;
  71. uint64_t accum_pass;
  72. struct PropertyAnim {
  73. TrackNodeCache *owner;
  74. SpecialProperty special; //small optimization
  75. StringName prop;
  76. Object *object;
  77. Variant value_accum;
  78. uint64_t accum_pass;
  79. PropertyAnim() {
  80. accum_pass = 0;
  81. object = NULL;
  82. }
  83. };
  84. Map<StringName, PropertyAnim> property_anim;
  85. TrackNodeCache() {
  86. skeleton = NULL;
  87. spatial = NULL;
  88. node = NULL;
  89. accum_pass = 0;
  90. bone_idx = -1;
  91. node_2d = NULL;
  92. }
  93. };
  94. struct TrackNodeCacheKey {
  95. uint32_t id;
  96. int bone_idx;
  97. inline bool operator<(const TrackNodeCacheKey &p_right) const {
  98. if (id < p_right.id)
  99. return true;
  100. else if (id > p_right.id)
  101. return false;
  102. else
  103. return bone_idx < p_right.bone_idx;
  104. }
  105. };
  106. Map<TrackNodeCacheKey, TrackNodeCache> node_cache_map;
  107. TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX];
  108. int cache_update_size;
  109. TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX];
  110. int cache_update_prop_size;
  111. Map<Ref<Animation>, int> used_anims;
  112. uint64_t accum_pass;
  113. float speed_scale;
  114. float default_blend_time;
  115. struct AnimationData {
  116. String name;
  117. StringName next;
  118. Vector<TrackNodeCache *> node_cache;
  119. Ref<Animation> animation;
  120. };
  121. Map<StringName, AnimationData> animation_set;
  122. struct BlendKey {
  123. StringName from;
  124. StringName to;
  125. bool operator<(const BlendKey &bk) const { return from == bk.from ? String(to) < String(bk.to) : String(from) < String(bk.from); }
  126. };
  127. Map<BlendKey, float> blend_times;
  128. struct PlaybackData {
  129. AnimationData *from;
  130. float pos;
  131. float speed_scale;
  132. PlaybackData() {
  133. pos = 0;
  134. speed_scale = 1.0;
  135. from = NULL;
  136. }
  137. };
  138. struct Blend {
  139. PlaybackData data;
  140. float blend_time;
  141. float blend_left;
  142. Blend() {
  143. blend_left = 0;
  144. blend_time = 0;
  145. }
  146. };
  147. struct Playback {
  148. List<Blend> blend;
  149. PlaybackData current;
  150. StringName assigned;
  151. } playback;
  152. List<StringName> queued;
  153. bool end_notify;
  154. String autoplay;
  155. AnimationProcessMode animation_process_mode;
  156. bool processing;
  157. bool active;
  158. NodePath root;
  159. void _animation_process_animation(AnimationData *p_anim, float p_time, float p_delta, float p_interp, bool p_allow_discrete = true);
  160. void _generate_node_caches(AnimationData *p_anim);
  161. void _animation_process_data(PlaybackData &cd, float p_delta, float p_blend);
  162. void _animation_process2(float p_delta);
  163. void _animation_update_transforms();
  164. void _animation_process(float p_delta);
  165. void _node_removed(Node *p_node);
  166. // bind helpers
  167. PoolVector<String> _get_animation_list() const {
  168. List<StringName> animations;
  169. get_animation_list(&animations);
  170. PoolVector<String> ret;
  171. while (animations.size()) {
  172. ret.push_back(animations.front()->get());
  173. animations.pop_front();
  174. }
  175. return ret;
  176. }
  177. void _animation_changed();
  178. void _ref_anim(const Ref<Animation> &p_anim);
  179. void _unref_anim(const Ref<Animation> &p_anim);
  180. void _set_process(bool p_process, bool p_force = false);
  181. bool playing;
  182. protected:
  183. bool _set(const StringName &p_name, const Variant &p_value);
  184. bool _get(const StringName &p_name, Variant &r_ret) const;
  185. void _get_property_list(List<PropertyInfo> *p_list) const;
  186. void _notification(int p_what);
  187. static void _bind_methods();
  188. public:
  189. StringName find_animation(const Ref<Animation> &p_animation) const;
  190. Error add_animation(const StringName &p_name, const Ref<Animation> &p_animation);
  191. void remove_animation(const StringName &p_name);
  192. void rename_animation(const StringName &p_name, const StringName &p_new_name);
  193. bool has_animation(const StringName &p_name) const;
  194. Ref<Animation> get_animation(const StringName &p_name) const;
  195. void get_animation_list(List<StringName> *p_animations) const;
  196. void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time);
  197. float get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
  198. void animation_set_next(const StringName &p_animation, const StringName &p_next);
  199. StringName animation_get_next(const StringName &p_animation) const;
  200. void set_default_blend_time(float p_default);
  201. float get_default_blend_time() const;
  202. void play(const StringName &p_name = StringName(), float p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  203. void play_backwards(const StringName &p_name = StringName(), float p_custom_blend = -1);
  204. void queue(const StringName &p_name);
  205. void clear_queue();
  206. void stop(bool p_reset = true);
  207. bool is_playing() const;
  208. String get_current_animation() const;
  209. void set_current_animation(const String &p_anim);
  210. void stop_all();
  211. void set_active(bool p_active);
  212. bool is_active() const;
  213. bool is_valid() const;
  214. void set_speed_scale(float p_speed);
  215. float get_speed_scale() const;
  216. void set_autoplay(const String &p_name);
  217. String get_autoplay() const;
  218. void set_animation_process_mode(AnimationProcessMode p_mode);
  219. AnimationProcessMode get_animation_process_mode() const;
  220. void seek(float p_time, bool p_update = false);
  221. void seek_delta(float p_time, float p_delta);
  222. float get_current_animation_position() const;
  223. float get_current_animation_length() const;
  224. void advance(float p_time);
  225. void set_root(const NodePath &p_root);
  226. NodePath get_root() const;
  227. void clear_caches(); ///< must be called by hand if an animation was modified after added
  228. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  229. AnimationPlayer();
  230. ~AnimationPlayer();
  231. };
  232. VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessMode);
  233. #endif