animation_player.h
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 
32 
33 #include "scene/resources/animation.h"
34 #include "scene/3d/spatial.h"
35 #include "scene/3d/skeleton.h"
36 #include "scene/main/misc.h"
37 #include "scene/2d/node_2d.h"
42 class AnimationPlayer : public Node {
43  OBJ_TYPE( AnimationPlayer, Node );
44  OBJ_CATEGORY("Animation Nodes");
45 
46 public:
47 
48  enum AnimationProcessMode {
49  ANIMATION_PROCESS_FIXED,
50  ANIMATION_PROCESS_IDLE,
51  };
52 
53 private:
54 
55  enum {
56 
57  NODE_CACHE_UPDATE_MAX=1024,
58  BLEND_FROM_MAX=3
59  };
60 
61 
62  enum SpecialProperty {
63  SP_NONE,
64  SP_NODE2D_POS,
65  SP_NODE2D_ROT,
66  SP_NODE2D_SCALE,
67  };
68 
69  struct TrackNodeCache {
70 
71  NodePath path;
72  uint32_t id;
73  RES resource;
74  Node *node;
75  Spatial* spatial;
76  Node2D* node_2d;
77  Skeleton *skeleton;
78  int bone_idx;
79  // accumulated transforms
80 
81  Vector3 loc_accum;
82  Quat rot_accum;
83  Vector3 scale_accum;
84  uint64_t accum_pass;
85 
86  struct PropertyAnim {
87 
88  TrackNodeCache *owner;
89  SpecialProperty special; //small optimization
90  StringName prop;
91  Object *object;
92  Variant value_accum;
93  uint64_t accum_pass;
94  PropertyAnim() { accum_pass=0; object=NULL; }
95  };
96 
97  Map<StringName,PropertyAnim> property_anim;
98 
99 
100  TrackNodeCache() { skeleton=NULL; spatial=NULL; node=NULL; accum_pass=0; bone_idx=-1; node_2d=NULL; }
101 
102  };
103 
104  struct TrackNodeCacheKey {
105 
106  uint32_t id;
107  int bone_idx;
108 
109  inline bool operator<(const TrackNodeCacheKey& p_right) const {
110 
111  if (id<p_right.id)
112  return true;
113  else if (id>p_right.id)
114  return false;
115  else
116  return bone_idx<p_right.bone_idx;
117  }
118  };
119 
121 
122  TrackNodeCache* cache_update[NODE_CACHE_UPDATE_MAX];
123  int cache_update_size;
124  TrackNodeCache::PropertyAnim* cache_update_prop[NODE_CACHE_UPDATE_MAX];
125  int cache_update_prop_size;
126  Map<Ref<Animation>,int> used_anims;
127 
128  uint64_t accum_pass;
129  float speed_scale;
130  float default_blend_time;
131 
132 
133  struct AnimationData {
134  String name;
135  StringName next;
136  Vector<TrackNodeCache*> node_cache;
137  Ref<Animation> animation;
138 
139  };
140 
141  Map<StringName, AnimationData> animation_set;
142  struct BlendKey {
143 
144  StringName from;
145  StringName to;
146  bool operator<(const BlendKey& bk) const { return from==bk.from?to<bk.to:from<bk.from; }
147  };
148 
149 
150  Map<BlendKey, float > blend_times;
151 
152 
153  struct PlaybackData {
154 
155  AnimationData* from;
156  float pos;
157  float speed_scale;
158 
159  PlaybackData() {
160 
161  pos=0;
162  speed_scale=1.0;
163  from=NULL;
164 
165  }
166 
167  };
168 
169  struct Blend {
170 
171  PlaybackData data;
172 
173  float blend_time;
174  float blend_left;
175 
176  Blend() {
177 
178  blend_left=0;
179  blend_time=0;
180  }
181  };
182 
183  struct Playback {
184 
185  List<Blend> blend;
186  PlaybackData current;
187  StringName assigned;
188  } playback;
189 
190  List<StringName> queued;
191 
192  bool end_notify;
193 
194  String autoplay;
195  AnimationProcessMode animation_process_mode;
196  bool processing;
197  bool active;
198 
199  NodePath root;
200 
201  void _animation_process_animation(AnimationData* p_anim,float p_time, float p_delta,float p_interp, bool p_allow_discrete=true);
202 
203  void _generate_node_caches(AnimationData* p_anim);
204  void _animation_process_data(PlaybackData &cd,float p_delta,float p_blend);
205  void _animation_process2(float p_delta);
206  void _animation_update_transforms();
207  void _animation_process(float p_delta);
208 
209  void _node_removed(Node *p_node);
210 
211 // bind helpers
212  DVector<String> _get_animation_list() const {
213 
214  List<StringName> animations;
215  get_animation_list(&animations);
216  DVector<String> ret;
217  while(animations.size()) {
218 
219  ret.push_back( animations.front()->get());
220  animations.pop_front();
221  }
222  return ret;
223  }
224 
225  void _animation_changed();
226  void _ref_anim(const Ref<Animation>& p_anim);
227  void _unref_anim(const Ref<Animation>& p_anim);
228 
229 
230  void _set_process(bool p_process,bool p_force=false);
231 
232  bool playing;
233 
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  void _get_property_list( List<PropertyInfo> *p_list) const;
238  void _notification(int p_what);
239 
240  static void _bind_methods();
241 
242 public:
243 
244  StringName find_animation(const Ref<Animation>& p_animation) const;
245 
246  Error add_animation(const StringName& p_name, const Ref<Animation>& p_animation);
247  void remove_animation(const StringName& p_name);
248  void rename_animation(const StringName& p_name,const StringName& p_new_name);
249  bool has_animation(const StringName& p_name) const;
250  Ref<Animation> get_animation(const StringName& p_name) const;
251  void get_animation_list( List<StringName> * p_animations) const;
252 
253  void set_blend_time(const StringName& p_animation1, const StringName& p_animation2, float p_time);
254  float get_blend_time( const StringName& p_animation1, const StringName& p_animation2) const;
255 
256  void animation_set_next(const StringName& p_animation, const StringName& p_next);
257  StringName animation_get_next(const StringName& p_animation) const;
258 
259  void set_default_blend_time(float p_default);
260  float get_default_blend_time() const;
261 
262  void play(const StringName& p_name=StringName(),float p_custom_blend=-1,float p_custom_scale=1.0,bool p_from_end=false);
263  void play_backwards(const StringName& p_name=StringName(),float p_custom_blend=-1);
264  void queue(const StringName& p_name);
265  void clear_queue();
266  void stop(bool p_reset=true);
267  bool is_playing() const;
268  String get_current_animation() const;
269  void set_current_animation(const String& p_anim);
270  void stop_all();
271  void set_active(bool p_active);
272  bool is_active() const;
273  bool is_valid() const;
274 
275  void set_speed(float p_speed);
276  float get_speed() const;
277 
278  void set_autoplay(const String& pname);
279  String get_autoplay() const;
280 
281  void set_animation_process_mode(AnimationProcessMode p_mode);
282  AnimationProcessMode get_animation_process_mode() const;
283 
284  void seek(float p_time,bool p_update=false);
285  void seek_delta(float p_time,float p_delta);
286  float get_current_animation_pos() const;
287  float get_current_animation_length() const;
288 
289  void advance(float p_time);
290 
291  void set_root(const NodePath& p_root);
292  NodePath get_root() const;
293 
294  void clear_caches();
295 
296  void get_argument_options(const StringName& p_function,int p_idx,List<String>*r_options) const;
297 
298 
299  AnimationPlayer();
300  ~AnimationPlayer();
301 
302 };
303 
304 
305 VARIANT_ENUM_CAST( AnimationPlayer::AnimationProcessMode );
306 
307 
308 #endif
Definition: animation_player.h:86
Definition: variant.h:74
Definition: path_db.h:41
Definition: quat.h:40
void clear_caches()
must be called by hand if an animation was modified after added
Definition: animation_player.cpp:1152
Definition: node.h:42
_FORCE_INLINE_ const Element * front() const
Definition: list.h:189
Definition: string_db.h:48
Definition: vector3.h:38
Definition: map.h:41
Definition: node_2d.h:34
Definition: skeleton.h:38
Definition: dvector.h:43
Definition: ustring.h:64
Definition: object.h:317
Definition: animation_player.h:42
Definition: spatial.h:56