animation_player.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. owner(NULL),
  102. special(SP_NONE),
  103. object(NULL),
  104. accum_pass(0) {}
  105. };
  106. Map<StringName, PropertyAnim> property_anim;
  107. struct BezierAnim {
  108. Vector<StringName> bezier_property;
  109. TrackNodeCache *owner;
  110. float bezier_accum;
  111. Object *object;
  112. uint64_t accum_pass;
  113. BezierAnim() :
  114. owner(NULL),
  115. bezier_accum(0.0),
  116. object(NULL),
  117. accum_pass(0) {}
  118. };
  119. Map<StringName, BezierAnim> bezier_anim;
  120. TrackNodeCache() :
  121. id(0),
  122. node(NULL),
  123. spatial(NULL),
  124. node_2d(NULL),
  125. skeleton(NULL),
  126. bone_idx(-1),
  127. accum_pass(0),
  128. audio_playing(false),
  129. audio_start(0.0),
  130. audio_len(0.0),
  131. animation_playing(false) {}
  132. };
  133. struct TrackNodeCacheKey {
  134. uint32_t id;
  135. int bone_idx;
  136. inline bool operator<(const TrackNodeCacheKey &p_right) const {
  137. if (id < p_right.id)
  138. return true;
  139. else if (id > p_right.id)
  140. return false;
  141. else
  142. return bone_idx < p_right.bone_idx;
  143. }
  144. };
  145. Map<TrackNodeCacheKey, TrackNodeCache> node_cache_map;
  146. TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX];
  147. int cache_update_size;
  148. TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX];
  149. int cache_update_prop_size;
  150. TrackNodeCache::BezierAnim *cache_update_bezier[NODE_CACHE_UPDATE_MAX];
  151. int cache_update_bezier_size;
  152. Set<TrackNodeCache *> playing_caches;
  153. uint64_t accum_pass;
  154. float speed_scale;
  155. float default_blend_time;
  156. struct AnimationData {
  157. String name;
  158. StringName next;
  159. Vector<TrackNodeCache *> node_cache;
  160. Ref<Animation> animation;
  161. };
  162. Map<StringName, AnimationData> animation_set;
  163. struct BlendKey {
  164. StringName from;
  165. StringName to;
  166. bool operator<(const BlendKey &bk) const { return from == bk.from ? String(to) < String(bk.to) : String(from) < String(bk.from); }
  167. };
  168. Map<BlendKey, float> blend_times;
  169. struct PlaybackData {
  170. AnimationData *from;
  171. float pos;
  172. float speed_scale;
  173. PlaybackData() {
  174. pos = 0;
  175. speed_scale = 1.0;
  176. from = NULL;
  177. }
  178. };
  179. struct Blend {
  180. PlaybackData data;
  181. float blend_time;
  182. float blend_left;
  183. Blend() {
  184. blend_left = 0;
  185. blend_time = 0;
  186. }
  187. };
  188. struct Playback {
  189. List<Blend> blend;
  190. PlaybackData current;
  191. StringName assigned;
  192. bool seeked;
  193. bool started;
  194. } playback;
  195. List<StringName> queued;
  196. bool end_reached;
  197. bool end_notify;
  198. String autoplay;
  199. AnimationProcessMode animation_process_mode;
  200. bool processing;
  201. bool active;
  202. NodePath root;
  203. 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);
  204. void _ensure_node_caches(AnimationData *p_anim);
  205. void _animation_process_data(PlaybackData &cd, float p_delta, float p_blend, bool p_seeked, bool p_started);
  206. void _animation_process2(float p_delta, bool p_started);
  207. void _animation_update_transforms();
  208. void _animation_process(float p_delta);
  209. void _node_removed(Node *p_node);
  210. void _stop_playing_caches();
  211. // bind helpers
  212. PoolVector<String> _get_animation_list() const {
  213. List<StringName> animations;
  214. get_animation_list(&animations);
  215. PoolVector<String> ret;
  216. while (animations.size()) {
  217. ret.push_back(animations.front()->get());
  218. animations.pop_front();
  219. }
  220. return ret;
  221. }
  222. void _animation_changed();
  223. void _ref_anim(const Ref<Animation> &p_anim);
  224. void _unref_anim(const Ref<Animation> &p_anim);
  225. void _set_process(bool p_process, bool p_force = false);
  226. bool playing;
  227. protected:
  228. bool _set(const StringName &p_name, const Variant &p_value);
  229. bool _get(const StringName &p_name, Variant &r_ret) const;
  230. virtual void _validate_property(PropertyInfo &property) const;
  231. void _get_property_list(List<PropertyInfo> *p_list) const;
  232. void _notification(int p_what);
  233. static void _bind_methods();
  234. public:
  235. StringName find_animation(const Ref<Animation> &p_animation) const;
  236. Error add_animation(const StringName &p_name, const Ref<Animation> &p_animation);
  237. void remove_animation(const StringName &p_name);
  238. void rename_animation(const StringName &p_name, const StringName &p_new_name);
  239. bool has_animation(const StringName &p_name) const;
  240. Ref<Animation> get_animation(const StringName &p_name) const;
  241. void get_animation_list(List<StringName> *p_animations) const;
  242. void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time);
  243. float get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
  244. void animation_set_next(const StringName &p_animation, const StringName &p_next);
  245. StringName animation_get_next(const StringName &p_animation) const;
  246. void set_default_blend_time(float p_default);
  247. float get_default_blend_time() const;
  248. void play(const StringName &p_name = StringName(), float p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  249. void play_backwards(const StringName &p_name = StringName(), float p_custom_blend = -1);
  250. void queue(const StringName &p_name);
  251. PoolVector<String> get_queue();
  252. void clear_queue();
  253. void stop(bool p_reset = true);
  254. bool is_playing() const;
  255. String get_current_animation() const;
  256. void set_current_animation(const String &p_anim);
  257. String get_assigned_animation() const;
  258. void set_assigned_animation(const String &p_anim);
  259. void stop_all();
  260. void set_active(bool p_active);
  261. bool is_active() const;
  262. bool is_valid() const;
  263. void set_speed_scale(float p_speed);
  264. float get_speed_scale() const;
  265. float get_playing_speed() const;
  266. void set_autoplay(const String &p_name);
  267. String get_autoplay() const;
  268. void set_animation_process_mode(AnimationProcessMode p_mode);
  269. AnimationProcessMode get_animation_process_mode() const;
  270. void seek(float p_time, bool p_update = false);
  271. void seek_delta(float p_time, float p_delta);
  272. float get_current_animation_position() const;
  273. float get_current_animation_length() const;
  274. void advance(float p_time);
  275. void set_root(const NodePath &p_root);
  276. NodePath get_root() const;
  277. void clear_caches(); ///< must be called by hand if an animation was modified after added
  278. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  279. #ifdef TOOLS_ENABLED
  280. // These may be interesting for games, but are too dangerous for general use
  281. AnimatedValuesBackup backup_animated_values();
  282. void restore_animated_values(const AnimatedValuesBackup &p_backup);
  283. #endif
  284. AnimationPlayer();
  285. ~AnimationPlayer();
  286. };
  287. VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessMode);
  288. #endif