animation_player.h 11 KB

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