animation.h
1 /*************************************************************************/
2 /* animation.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_H
30 #define ANIMATION_H
31 
32 #include "resource.h"
36 class Animation : public Resource {
37 
38  OBJ_TYPE( Animation, Resource );
39  RES_BASE_EXTENSION("anm");
40 public:
41 
42  enum LoopMode {
43 
44  LOOP_NONE,
45  LOOP_ENABLED,
46  LOOP_WRAP
47  };
48 
49  enum TrackType {
53  };
54 
55  enum InterpolationType {
56  INTERPOLATION_NEAREST,
57  INTERPOLATION_LINEAR,
58  INTERPOLATION_CUBIC
59  };
60 
61 private:
62 
63  struct Track {
64 
65  TrackType type;
66  InterpolationType interpolation;
67  NodePath path; // path to something
68  Track() { interpolation=INTERPOLATION_LINEAR; }
69  virtual ~Track() {}
70  };
71 
72  struct Key {
73 
74  float transition;
75  float time; // time in secs
76  Key() { transition=1; }
77  };
78 
79  // transform key holds either Vector3 or Quaternion
80  template<class T>
81  struct TKey : public Key {
82 
83  float time;
84  T value;
85  };
86 
87 
88  struct TransformKey {
89 
90  Vector3 loc;
91  Quat rot;
92  Vector3 scale;
93  };
94 
95  /* TRANSFORM TRACK */
96 
97  struct TransformTrack : public Track {
98 
99  Vector< TKey<TransformKey> > transforms;
100 
101  TransformTrack() { type=TYPE_TRANSFORM; }
102  };
103 
104  /* PROPERTY VALUE TRACK */
105 
106  struct ValueTrack : public Track {
107 
108  bool continuous;
109  Vector< TKey<Variant> > values;
110 
111  ValueTrack() { type=TYPE_VALUE; continuous=true; }
112  };
113 
114 
115  /* METHOD TRACK */
116 
117  struct MethodKey : public Key {
118 
119  StringName method;
120  Vector<Variant> params;
121  };
122 
123  struct MethodTrack : public Track {
124 
125  Vector< MethodKey > methods;
126  MethodTrack() { type=TYPE_METHOD; }
127  };
128 
129  Vector<Track*> tracks;
130 
131  /*
132  template<class T>
133  int _insert_pos(float p_time, T& p_keys);*/
134 
135  template<class T>
136  void _clear(T& p_keys);
137 
138  template<class T, class V>
139  int _insert(float p_time, T& p_keys, const V& p_value);
140 
141  template<class K>
142  inline int _find( const Vector<K>& p_keys, float p_time) const;
143 
144  _FORCE_INLINE_ Animation::TransformKey _interpolate( const Animation::TransformKey& p_a, const Animation::TransformKey& p_b, float p_c) const;
145 
146  _FORCE_INLINE_ Vector3 _interpolate( const Vector3& p_a, const Vector3& p_b, float p_c) const;
147  _FORCE_INLINE_ Quat _interpolate( const Quat& p_a, const Quat& p_b, float p_c) const;
148  _FORCE_INLINE_ Variant _interpolate( const Variant& p_a, const Variant& p_b, float p_c) const;
149  _FORCE_INLINE_ float _interpolate( const float& p_a, const float& p_b, float p_c) const;
150 
151  _FORCE_INLINE_ Animation::TransformKey _cubic_interpolate( const Animation::TransformKey& p_pre_a, const Animation::TransformKey& p_a, const Animation::TransformKey& p_b, const Animation::TransformKey& p_post_b,float p_c) const;
152  _FORCE_INLINE_ Vector3 _cubic_interpolate( const Vector3& p_pre_a,const Vector3& p_a, const Vector3& p_b,const Vector3& p_post_b, float p_c) const;
153  _FORCE_INLINE_ Quat _cubic_interpolate( const Quat& p_pre_a,const Quat& p_a, const Quat& p_b,const Quat& p_post_b, float p_c) const;
154  _FORCE_INLINE_ Variant _cubic_interpolate( const Variant& p_pre_a,const Variant& p_a, const Variant& p_b, const Variant& p_post_b,float p_c) const;
155  _FORCE_INLINE_ float _cubic_interpolate( const float& p_pre_a,const float& p_a, const float& p_b, const float& p_post_b, float p_c) const;
156 
157  template<class T>
158  _FORCE_INLINE_ T _interpolate( const Vector< TKey<T> >& p_keys, float p_time, InterpolationType p_interp,bool *p_ok) const;
159 
160  _FORCE_INLINE_ void _value_track_get_key_indices_in_range(const ValueTrack * vt, float from_time, float to_time,List<int> *p_indices) const;
161  _FORCE_INLINE_ void _method_track_get_key_indices_in_range(const MethodTrack * mt, float from_time, float to_time,List<int> *p_indices) const;
162 
163  float length;
164  float step;
165  bool loop;
166 
167 // bind helpers
168 private:
169 
170  Array _transform_track_interpolate(int p_track, float p_time) const {
171  Vector3 loc;
172  Quat rot;
173  Vector3 scale;
174  transform_track_interpolate(p_track,p_time,&loc,&rot,&scale);
175  Array ret;
176  ret.push_back(loc);
177  ret.push_back(rot);
178  ret.push_back(scale);
179  return ret;
180  }
181 
182  DVector<int> _value_track_get_key_indices(int p_track, float p_time, float p_delta) const {
183 
184  List<int> idxs;
185  value_track_get_key_indices(p_track,p_time,p_delta,&idxs);
186  DVector<int> idxr;
187 
188  for (List<int>::Element *E=idxs.front();E;E=E->next()) {
189 
190  idxr.push_back(E->get());
191  }
192  return idxr;
193  }
194  DVector<int> _method_track_get_key_indices(int p_track, float p_time, float p_delta) const {
195 
196  List<int> idxs;
197  method_track_get_key_indices(p_track,p_time,p_delta,&idxs);
198  DVector<int> idxr;
199 
200  for (List<int>::Element *E=idxs.front();E;E=E->next()) {
201 
202  idxr.push_back(E->get());
203  }
204  return idxr;
205  }
206 
207  bool _transform_track_optimize_key(const TKey<TransformKey> &t0,const TKey<TransformKey> &t1, const TKey<TransformKey> &t2, float p_alowed_linear_err,float p_alowed_angular_err,float p_max_optimizable_angle,const Vector3& p_norm);
208  void _transform_track_optimize(int p_idx, float p_allowed_err=0.05, float p_alowed_angular_err=0.01,float p_max_optimizable_angle=Math_PI*0.125);
209 
210 protected:
211 
212  bool _set(const StringName& p_name, const Variant& p_value);
213  bool _get(const StringName& p_name,Variant &r_ret) const;
214  void _get_property_list( List<PropertyInfo> *p_list) const;
215 
216  static void _bind_methods();
217 
218 public:
219 
220  int add_track(TrackType p_type,int p_at_pos=-1);
221  void remove_track(int p_track);
222 
223  int get_track_count() const;
224  TrackType track_get_type(int p_track) const;
225 
226  void track_set_path(int p_track,const NodePath& p_path);
227  NodePath track_get_path(int p_track) const;
228  int find_track(const NodePath& p_path) const;
229  // transform
230 
231 
232  void track_move_up(int p_track);
233  void track_move_down(int p_track);
234 
235  int transform_track_insert_key(int p_track, float p_time, const Vector3 p_loc, const Quat& p_rot=Quat(), const Vector3& p_scale=Vector3());
236  void track_insert_key(int p_track, float p_time, const Variant& p_key, float p_transition=1);
237  void track_set_key_transition(int p_track, int p_key_idx,float p_transition);
238  void track_set_key_value(int p_track, int p_key_idx,const Variant& p_value);
239  int track_find_key(int p_track, float p_time, bool p_exact=false) const;
240  void track_remove_key(int p_track, int p_idx);
241  void track_remove_key_at_pos(int p_track, float p_pos);
242  int track_get_key_count(int p_track) const;
243  Variant track_get_key_value(int p_track, int p_key_idx) const;
244  float track_get_key_time(int p_track, int p_key_idx) const;
245  float track_get_key_transition(int p_track, int p_key_idx) const;
246 
247  Error transform_track_get_key(int p_track, int p_key, Vector3* r_loc, Quat* r_rot, Vector3* r_scale) const;
248  void track_set_interpolation_type(int p_track,InterpolationType p_interp);
249  InterpolationType track_get_interpolation_type(int p_track) const;
250 
251 
252  Error transform_track_interpolate(int p_track, float p_time, Vector3 * r_loc, Quat *r_rot, Vector3 *r_scale) const;
253 
254  Variant value_track_interpolate(int p_track, float p_time) const;
255  void value_track_get_key_indices(int p_track, float p_time, float p_delta,List<int> *p_indices) const;
256  void value_track_set_continuous(int p_track, bool p_continuous);
257  bool value_track_is_continuous(int p_track) const;
258 
259  void method_track_get_key_indices(int p_track, float p_time, float p_delta,List<int> *p_indices) const;
260  Vector<Variant> method_track_get_params(int p_track,int p_key_idx) const;
261  StringName method_track_get_name(int p_track,int p_key_idx) const;
262 
263 
264  void set_length(float p_length);
265  float get_length() const;
266 
267  void set_loop(bool p_enabled);
268  bool has_loop() const;
269 
270  void set_step(float p_step);
271  float get_step() const;
272 
273  void clear();
274 
275  void optimize(float p_allowed_linear_err=0.05,float p_allowed_angular_err=0.01,float p_max_optimizable_angle=Math_PI*0.125);
276 
277  Animation();
278  ~Animation();
279 
280 };
281 
282 VARIANT_ENUM_CAST( Animation::TrackType );
283 VARIANT_ENUM_CAST( Animation::InterpolationType );
284 
285 #endif
Definition: animation.h:36
Definition: array.h:38
Call any method on a specific node.
Definition: animation.h:52
TrackType
Definition: animation.h:49
Definition: variant.h:74
Definition: path_db.h:41
Definition: quat.h:40
_FORCE_INLINE_ const Element * front() const
Definition: list.h:189
Definition: string_db.h:48
Definition: vector3.h:38
Definition: resource.h:89
Transform a node or a bone.
Definition: animation.h:51
Set a value in a property, can be interpolated.
Definition: animation.h:50
Definition: vector.h:43