animation_player.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*************************************************************************/
  2. /* animation_player.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef ANIMATION_PLAYER_H
  30. #define ANIMATION_PLAYER_H
  31. #include "scene/resources/animation.h"
  32. #include "scene/3d/spatial.h"
  33. #include "scene/3d/skeleton.h"
  34. #include "scene/main/misc.h"
  35. #include "scene/2d/node_2d.h"
  36. /**
  37. @author Juan Linietsky <reduzio@gmail.com>
  38. */
  39. class AnimationPlayer : public Node {
  40. OBJ_TYPE( AnimationPlayer, Node );
  41. OBJ_CATEGORY("Animation Nodes");
  42. public:
  43. enum AnimationProcessMode {
  44. ANIMATION_PROCESS_FIXED,
  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() { accum_pass=0; object=NULL; }
  80. };
  81. Map<StringName,PropertyAnim> property_anim;
  82. TrackNodeCache() { skeleton=NULL; spatial=NULL; node=NULL; accum_pass=0; bone_idx=-1; node_2d=NULL; }
  83. };
  84. struct TrackNodeCacheKey {
  85. uint32_t id;
  86. int bone_idx;
  87. inline bool operator<(const TrackNodeCacheKey& p_right) const {
  88. if (id<p_right.id)
  89. return true;
  90. else if (id>p_right.id)
  91. return false;
  92. else
  93. return bone_idx<p_right.bone_idx;
  94. }
  95. };
  96. Map<TrackNodeCacheKey,TrackNodeCache> node_cache_map;
  97. TrackNodeCache* cache_update[NODE_CACHE_UPDATE_MAX];
  98. int cache_update_size;
  99. TrackNodeCache::PropertyAnim* cache_update_prop[NODE_CACHE_UPDATE_MAX];
  100. int cache_update_prop_size;
  101. Map<Ref<Animation>,int> used_anims;
  102. uint64_t accum_pass;
  103. float speed_scale;
  104. float default_blend_time;
  105. struct AnimationData {
  106. String name;
  107. StringName next;
  108. Vector<TrackNodeCache*> node_cache;
  109. Ref<Animation> animation;
  110. };
  111. Map<StringName, AnimationData> animation_set;
  112. struct BlendKey {
  113. StringName from;
  114. StringName to;
  115. bool operator<(const BlendKey& bk) const { return from==bk.from?to<bk.to:from<bk.from; }
  116. };
  117. Map<BlendKey, float > blend_times;
  118. struct PlaybackData {
  119. AnimationData* from;
  120. float pos;
  121. float speed_scale;
  122. PlaybackData() {
  123. pos=0;
  124. speed_scale=1.0;
  125. from=NULL;
  126. }
  127. };
  128. struct Blend {
  129. PlaybackData data;
  130. float blend_time;
  131. float blend_left;
  132. Blend() {
  133. blend_left=0;
  134. blend_time=0;
  135. }
  136. };
  137. struct Playback {
  138. List<Blend> blend;
  139. PlaybackData current;
  140. StringName assigned;
  141. } playback;
  142. List<StringName> queued;
  143. bool end_notify;
  144. String autoplay;
  145. AnimationProcessMode animation_process_mode;
  146. bool processing;
  147. bool active;
  148. NodePath root;
  149. void _animation_process_animation(AnimationData* p_anim,float p_time, float p_delta,float p_interp, bool p_allow_discrete=true);
  150. void _generate_node_caches(AnimationData* p_anim);
  151. void _animation_process_data(PlaybackData &cd,float p_delta,float p_blend);
  152. void _animation_process2(float p_delta);
  153. void _animation_update_transforms();
  154. void _animation_process(float p_delta);
  155. void _node_removed(Node *p_node);
  156. // bind helpers
  157. DVector<String> _get_animation_list() const {
  158. List<StringName> animations;
  159. get_animation_list(&animations);
  160. DVector<String> ret;
  161. while(animations.size()) {
  162. ret.push_back( animations.front()->get());
  163. animations.pop_front();
  164. }
  165. return ret;
  166. }
  167. void _animation_changed();
  168. void _ref_anim(const Ref<Animation>& p_anim);
  169. void _unref_anim(const Ref<Animation>& p_anim);
  170. void _set_process(bool p_process,bool p_force=false);
  171. bool playing;
  172. protected:
  173. bool _set(const StringName& p_name, const Variant& p_value);
  174. bool _get(const StringName& p_name,Variant &r_ret) const;
  175. void _get_property_list( List<PropertyInfo> *p_list) const;
  176. void _notification(int p_what);
  177. static void _bind_methods();
  178. public:
  179. StringName find_animation(const Ref<Animation>& p_animation) const;
  180. Error add_animation(const StringName& p_name, const Ref<Animation>& p_animation);
  181. void remove_animation(const StringName& p_name);
  182. void rename_animation(const StringName& p_name,const StringName& p_new_name);
  183. bool has_animation(const StringName& p_name) const;
  184. Ref<Animation> get_animation(const StringName& p_name) const;
  185. void get_animation_list( List<StringName> * p_animations) const;
  186. void set_blend_time(const StringName& p_animation1, const StringName& p_animation2, float p_time);
  187. float get_blend_time( const StringName& p_animation1, const StringName& p_animation2) const;
  188. void animation_set_next(const StringName& p_animation, const StringName& p_next);
  189. StringName animation_get_next(const StringName& p_animation) const;
  190. void set_default_blend_time(float p_default);
  191. float get_default_blend_time() const;
  192. void play(const StringName& p_name=StringName(),float p_custom_blend=-1,float p_custom_scale=1.0,bool p_from_end=false);
  193. void play_backwards(const StringName& p_name=StringName(),float p_custom_blend=-1);
  194. void queue(const StringName& p_name);
  195. void clear_queue();
  196. void stop(bool p_reset=true);
  197. bool is_playing() const;
  198. String get_current_animation() const;
  199. void set_current_animation(const String& p_anim);
  200. void stop_all();
  201. void set_active(bool p_active);
  202. bool is_active() const;
  203. bool is_valid() const;
  204. void set_speed(float p_speed);
  205. float get_speed() const;
  206. void set_autoplay(const String& pname);
  207. String get_autoplay() const;
  208. void set_animation_process_mode(AnimationProcessMode p_mode);
  209. AnimationProcessMode get_animation_process_mode() const;
  210. void seek(float p_time,bool p_update=false);
  211. void seek_delta(float p_time,float p_delta);
  212. float get_current_animation_pos() const;
  213. float get_current_animation_length() const;
  214. void advance(float p_time);
  215. void set_root(const NodePath& p_root);
  216. NodePath get_root() const;
  217. void clear_caches(); ///< must be called by hand if an animation was modified after added
  218. void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const;
  219. AnimationPlayer();
  220. ~AnimationPlayer();
  221. };
  222. VARIANT_ENUM_CAST( AnimationPlayer::AnimationProcessMode );
  223. #endif