animation_player.h 9.9 KB

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