animation_player.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*************************************************************************/
  2. /* animation_player.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #ifdef TOOLS_ENABLED
  37. // To save/restore animated values
  38. class AnimatedValuesBackup {
  39. struct Entry {
  40. Object *object;
  41. Vector<StringName> subpath; // Unused if bone
  42. int bone_idx; // -1 if not a bone
  43. Variant value;
  44. };
  45. Vector<Entry> entries;
  46. friend class AnimationPlayer;
  47. public:
  48. void update_skeletons();
  49. };
  50. #endif
  51. class AnimationPlayer : public Node {
  52. GDCLASS(AnimationPlayer, Node);
  53. OBJ_CATEGORY("Animation Nodes");
  54. public:
  55. enum AnimationProcessMode {
  56. ANIMATION_PROCESS_PHYSICS,
  57. ANIMATION_PROCESS_IDLE,
  58. ANIMATION_PROCESS_MANUAL,
  59. };
  60. enum AnimationMethodCallMode {
  61. ANIMATION_METHOD_CALL_DEFERRED,
  62. ANIMATION_METHOD_CALL_IMMEDIATE,
  63. };
  64. private:
  65. enum {
  66. NODE_CACHE_UPDATE_MAX = 1024,
  67. BLEND_FROM_MAX = 3
  68. };
  69. enum SpecialProperty {
  70. SP_NONE,
  71. SP_NODE2D_POS,
  72. SP_NODE2D_ROT,
  73. SP_NODE2D_SCALE,
  74. };
  75. struct TrackNodeCache {
  76. NodePath path;
  77. uint32_t id;
  78. RES resource;
  79. Node *node;
  80. Spatial *spatial;
  81. Node2D *node_2d;
  82. Skeleton *skeleton;
  83. int bone_idx;
  84. // accumulated transforms
  85. Vector3 loc_accum;
  86. Quat rot_accum;
  87. Vector3 scale_accum;
  88. uint64_t accum_pass;
  89. bool audio_playing;
  90. float audio_start;
  91. float audio_len;
  92. bool animation_playing;
  93. struct PropertyAnim {
  94. TrackNodeCache *owner;
  95. SpecialProperty special; //small optimization
  96. Vector<StringName> subpath;
  97. Object *object;
  98. Variant value_accum;
  99. uint64_t accum_pass;
  100. Variant capture;
  101. PropertyAnim() :
  102. owner(NULL),
  103. special(SP_NONE),
  104. object(NULL),
  105. accum_pass(0) {}
  106. };
  107. Map<StringName, PropertyAnim> property_anim;
  108. struct BezierAnim {
  109. Vector<StringName> bezier_property;
  110. TrackNodeCache *owner;
  111. float bezier_accum;
  112. Object *object;
  113. uint64_t accum_pass;
  114. BezierAnim() :
  115. owner(NULL),
  116. bezier_accum(0.0),
  117. object(NULL),
  118. accum_pass(0) {}
  119. };
  120. Map<StringName, BezierAnim> bezier_anim;
  121. TrackNodeCache() :
  122. id(0),
  123. node(NULL),
  124. spatial(NULL),
  125. node_2d(NULL),
  126. skeleton(NULL),
  127. bone_idx(-1),
  128. accum_pass(0),
  129. audio_playing(false),
  130. audio_start(0.0),
  131. audio_len(0.0),
  132. animation_playing(false) {}
  133. };
  134. struct TrackNodeCacheKey {
  135. uint32_t id;
  136. int bone_idx;
  137. inline bool operator<(const TrackNodeCacheKey &p_right) const {
  138. if (id < p_right.id)
  139. return true;
  140. else if (id > p_right.id)
  141. return false;
  142. else
  143. return bone_idx < p_right.bone_idx;
  144. }
  145. };
  146. Map<TrackNodeCacheKey, TrackNodeCache> node_cache_map;
  147. TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX];
  148. int cache_update_size;
  149. TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX];
  150. int cache_update_prop_size;
  151. TrackNodeCache::BezierAnim *cache_update_bezier[NODE_CACHE_UPDATE_MAX];
  152. int cache_update_bezier_size;
  153. Set<TrackNodeCache *> playing_caches;
  154. uint64_t accum_pass;
  155. float speed_scale;
  156. float default_blend_time;
  157. struct AnimationData {
  158. String name;
  159. StringName next;
  160. Vector<TrackNodeCache *> node_cache;
  161. Ref<Animation> animation;
  162. };
  163. Map<StringName, AnimationData> animation_set;
  164. struct BlendKey {
  165. StringName from;
  166. StringName to;
  167. bool operator<(const BlendKey &bk) const { return from == bk.from ? String(to) < String(bk.to) : String(from) < String(bk.from); }
  168. };
  169. Map<BlendKey, float> blend_times;
  170. struct PlaybackData {
  171. AnimationData *from;
  172. float pos;
  173. float speed_scale;
  174. PlaybackData() {
  175. pos = 0;
  176. speed_scale = 1.0;
  177. from = NULL;
  178. }
  179. };
  180. struct Blend {
  181. PlaybackData data;
  182. float blend_time;
  183. float blend_left;
  184. Blend() {
  185. blend_left = 0;
  186. blend_time = 0;
  187. }
  188. };
  189. struct Playback {
  190. List<Blend> blend;
  191. PlaybackData current;
  192. StringName assigned;
  193. bool seeked;
  194. bool started;
  195. } playback;
  196. List<StringName> queued;
  197. bool end_reached;
  198. bool end_notify;
  199. String autoplay;
  200. AnimationProcessMode animation_process_mode;
  201. AnimationMethodCallMode method_call_mode;
  202. bool processing;
  203. bool active;
  204. NodePath root;
  205. void _animation_process_animation(AnimationData *p_anim, float p_time, float p_delta, float p_interp, bool p_is_current = true, bool p_seeked = false, bool p_started = false);
  206. void _ensure_node_caches(AnimationData *p_anim);
  207. void _animation_process_data(PlaybackData &cd, float p_delta, float p_blend, bool p_seeked, bool p_started);
  208. void _animation_process2(float p_delta, bool p_started);
  209. void _animation_update_transforms();
  210. void _animation_process(float p_delta);
  211. void _node_removed(Node *p_node);
  212. void _stop_playing_caches();
  213. // bind helpers
  214. PoolVector<String> _get_animation_list() const {
  215. List<StringName> animations;
  216. get_animation_list(&animations);
  217. PoolVector<String> ret;
  218. while (animations.size()) {
  219. ret.push_back(animations.front()->get());
  220. animations.pop_front();
  221. }
  222. return ret;
  223. }
  224. void _animation_changed();
  225. void _ref_anim(const Ref<Animation> &p_anim);
  226. void _unref_anim(const Ref<Animation> &p_anim);
  227. void _set_process(bool p_process, bool p_force = false);
  228. bool playing;
  229. protected:
  230. bool _set(const StringName &p_name, const Variant &p_value);
  231. bool _get(const StringName &p_name, Variant &r_ret) const;
  232. virtual void _validate_property(PropertyInfo &property) const;
  233. void _get_property_list(List<PropertyInfo> *p_list) const;
  234. void _notification(int p_what);
  235. static void _bind_methods();
  236. public:
  237. StringName find_animation(const Ref<Animation> &p_animation) const;
  238. Error add_animation(const StringName &p_name, const Ref<Animation> &p_animation);
  239. void remove_animation(const StringName &p_name);
  240. void rename_animation(const StringName &p_name, const StringName &p_new_name);
  241. bool has_animation(const StringName &p_name) const;
  242. Ref<Animation> get_animation(const StringName &p_name) const;
  243. void get_animation_list(List<StringName> *p_animations) const;
  244. void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time);
  245. float get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
  246. void animation_set_next(const StringName &p_animation, const StringName &p_next);
  247. StringName animation_get_next(const StringName &p_animation) const;
  248. void set_default_blend_time(float p_default);
  249. float get_default_blend_time() const;
  250. void play(const StringName &p_name = StringName(), float p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  251. void play_backwards(const StringName &p_name = StringName(), float p_custom_blend = -1);
  252. void queue(const StringName &p_name);
  253. PoolVector<String> get_queue();
  254. void clear_queue();
  255. void stop(bool p_reset = true);
  256. bool is_playing() const;
  257. String get_current_animation() const;
  258. void set_current_animation(const String &p_anim);
  259. String get_assigned_animation() const;
  260. void set_assigned_animation(const String &p_anim);
  261. void stop_all();
  262. void set_active(bool p_active);
  263. bool is_active() const;
  264. bool is_valid() const;
  265. void set_speed_scale(float p_speed);
  266. float get_speed_scale() const;
  267. float get_playing_speed() const;
  268. void set_autoplay(const String &p_name);
  269. String get_autoplay() const;
  270. void set_animation_process_mode(AnimationProcessMode p_mode);
  271. AnimationProcessMode get_animation_process_mode() const;
  272. void set_method_call_mode(AnimationMethodCallMode p_mode);
  273. AnimationMethodCallMode get_method_call_mode() const;
  274. void seek(float p_time, bool p_update = false);
  275. void seek_delta(float p_time, float p_delta);
  276. float get_current_animation_position() const;
  277. float get_current_animation_length() const;
  278. void advance(float p_time);
  279. void set_root(const NodePath &p_root);
  280. NodePath get_root() const;
  281. void clear_caches(); ///< must be called by hand if an animation was modified after added
  282. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  283. #ifdef TOOLS_ENABLED
  284. // These may be interesting for games, but are too dangerous for general use
  285. AnimatedValuesBackup backup_animated_values();
  286. void restore_animated_values(const AnimatedValuesBackup &p_backup);
  287. #endif
  288. AnimationPlayer();
  289. ~AnimationPlayer();
  290. };
  291. VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessMode);
  292. VARIANT_ENUM_CAST(AnimationPlayer::AnimationMethodCallMode);
  293. #endif