animation.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*************************************************************************/
  2. /* animation.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. #include "resource.h"
  32. /**
  33. @author Juan Linietsky <[email protected]>
  34. */
  35. class Animation : public Resource {
  36. OBJ_TYPE( Animation, Resource );
  37. RES_BASE_EXTENSION("anm");
  38. public:
  39. enum LoopMode {
  40. LOOP_NONE,
  41. LOOP_ENABLED,
  42. LOOP_WRAP
  43. };
  44. enum TrackType {
  45. TYPE_VALUE, ///< Set a value in a property, can be interpolated.
  46. TYPE_TRANSFORM, ///< Transform a node or a bone.
  47. TYPE_METHOD, ///< Call any method on a specific node.
  48. };
  49. enum InterpolationType {
  50. INTERPOLATION_NEAREST,
  51. INTERPOLATION_LINEAR,
  52. INTERPOLATION_CUBIC
  53. };
  54. private:
  55. struct Track {
  56. TrackType type;
  57. InterpolationType interpolation;
  58. NodePath path; // path to something
  59. Track() { interpolation=INTERPOLATION_LINEAR; }
  60. virtual ~Track() {}
  61. };
  62. struct Key {
  63. float transition;
  64. float time; // time in secs
  65. Key() { transition=1; }
  66. };
  67. // transform key holds either Vector3 or Quaternion
  68. template<class T>
  69. struct TKey : public Key {
  70. float time;
  71. T value;
  72. };
  73. struct TransformKey {
  74. Vector3 loc;
  75. Quat rot;
  76. Vector3 scale;
  77. };
  78. /* TRANSFORM TRACK */
  79. struct TransformTrack : public Track {
  80. Vector< TKey<TransformKey> > transforms;
  81. TransformTrack() { type=TYPE_TRANSFORM; }
  82. };
  83. /* PROPERTY VALUE TRACK */
  84. struct ValueTrack : public Track {
  85. bool continuous;
  86. Vector< TKey<Variant> > values;
  87. ValueTrack() { type=TYPE_VALUE; continuous=true; }
  88. };
  89. /* METHOD TRACK */
  90. struct MethodKey : public Key {
  91. StringName method;
  92. Vector<Variant> params;
  93. };
  94. struct MethodTrack : public Track {
  95. Vector< MethodKey > methods;
  96. MethodTrack() { type=TYPE_METHOD; }
  97. };
  98. Vector<Track*> tracks;
  99. /*
  100. template<class T>
  101. int _insert_pos(float p_time, T& p_keys);*/
  102. template<class T>
  103. void _clear(T& p_keys);
  104. template<class T, class V>
  105. int _insert(float p_time, T& p_keys, const V& p_value);
  106. template<class K>
  107. inline int _find( const Vector<K>& p_keys, float p_time) const;
  108. _FORCE_INLINE_ Animation::TransformKey _interpolate( const Animation::TransformKey& p_a, const Animation::TransformKey& p_b, float p_c) const;
  109. _FORCE_INLINE_ Vector3 _interpolate( const Vector3& p_a, const Vector3& p_b, float p_c) const;
  110. _FORCE_INLINE_ Quat _interpolate( const Quat& p_a, const Quat& p_b, float p_c) const;
  111. _FORCE_INLINE_ Variant _interpolate( const Variant& p_a, const Variant& p_b, float p_c) const;
  112. _FORCE_INLINE_ float _interpolate( const float& p_a, const float& p_b, float p_c) const;
  113. _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;
  114. _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;
  115. _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;
  116. _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;
  117. _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;
  118. template<class T>
  119. _FORCE_INLINE_ T _interpolate( const Vector< TKey<T> >& p_keys, float p_time, InterpolationType p_interp,bool *p_ok) const;
  120. _FORCE_INLINE_ void _value_track_get_key_indices_in_range(const ValueTrack * vt, float from_time, float to_time,List<int> *p_indices) const;
  121. _FORCE_INLINE_ void _method_track_get_key_indices_in_range(const MethodTrack * mt, float from_time, float to_time,List<int> *p_indices) const;
  122. float length;
  123. float step;
  124. bool loop;
  125. // bind helpers
  126. private:
  127. Array _transform_track_interpolate(int p_track, float p_time) const {
  128. Vector3 loc;
  129. Quat rot;
  130. Vector3 scale;
  131. transform_track_interpolate(p_track,p_time,&loc,&rot,&scale);
  132. Array ret;
  133. ret.push_back(loc);
  134. ret.push_back(rot);
  135. ret.push_back(scale);
  136. return ret;
  137. }
  138. DVector<int> _value_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  139. List<int> idxs;
  140. value_track_get_key_indices(p_track,p_time,p_delta,&idxs);
  141. DVector<int> idxr;
  142. for (List<int>::Element *E=idxs.front();E;E=E->next()) {
  143. idxr.push_back(E->get());
  144. }
  145. return idxr;
  146. }
  147. DVector<int> _method_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  148. List<int> idxs;
  149. method_track_get_key_indices(p_track,p_time,p_delta,&idxs);
  150. DVector<int> idxr;
  151. for (List<int>::Element *E=idxs.front();E;E=E->next()) {
  152. idxr.push_back(E->get());
  153. }
  154. return idxr;
  155. }
  156. void _transform_track_optimize(int p_idx, float p_allowed_err=0.05, float p_alowed_angular_err=0.01);
  157. protected:
  158. bool _set(const StringName& p_name, const Variant& p_value);
  159. bool _get(const StringName& p_name,Variant &r_ret) const;
  160. void _get_property_list( List<PropertyInfo> *p_list) const;
  161. static void _bind_methods();
  162. public:
  163. int add_track(TrackType p_type,int p_at_pos=-1);
  164. void remove_track(int p_track);
  165. int get_track_count() const;
  166. TrackType track_get_type(int p_track) const;
  167. void track_set_path(int p_track,const NodePath& p_path);
  168. NodePath track_get_path(int p_track) const;
  169. int find_track(const NodePath& p_path) const;
  170. // transform
  171. void track_move_up(int p_track);
  172. void track_move_down(int p_track);
  173. 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());
  174. void track_insert_key(int p_track, float p_time, const Variant& p_key, float p_transition=1);
  175. void track_set_key_transition(int p_track, int p_key_idx,float p_transition);
  176. void track_set_key_value(int p_track, int p_key_idx,const Variant& p_value);
  177. int track_find_key(int p_track, float p_time, bool p_exact=false) const;
  178. void track_remove_key(int p_track, int p_idx);
  179. void track_remove_key_at_pos(int p_track, float p_pos);
  180. int track_get_key_count(int p_track) const;
  181. Variant track_get_key_value(int p_track, int p_key_idx) const;
  182. float track_get_key_time(int p_track, int p_key_idx) const;
  183. float track_get_key_transition(int p_track, int p_key_idx) const;
  184. Error transform_track_get_key(int p_track, int p_key, Vector3* r_loc, Quat* r_rot, Vector3* r_scale) const;
  185. void track_set_interpolation_type(int p_track,InterpolationType p_interp);
  186. InterpolationType track_get_interpolation_type(int p_track) const;
  187. Error transform_track_interpolate(int p_track, float p_time, Vector3 * r_loc, Quat *r_rot, Vector3 *r_scale) const;
  188. Variant value_track_interpolate(int p_track, float p_time) const;
  189. void value_track_get_key_indices(int p_track, float p_time, float p_delta,List<int> *p_indices) const;
  190. void value_track_set_continuous(int p_track, bool p_continuous);
  191. bool value_track_is_continuous(int p_track) const;
  192. void method_track_get_key_indices(int p_track, float p_time, float p_delta,List<int> *p_indices) const;
  193. Vector<Variant> method_track_get_params(int p_track,int p_key_idx) const;
  194. StringName method_track_get_name(int p_track,int p_key_idx) const;
  195. void set_length(float p_length);
  196. float get_length() const;
  197. void set_loop(bool p_enabled);
  198. bool has_loop() const;
  199. void set_step(float p_step);
  200. float get_step() const;
  201. void clear();
  202. void optimize(float p_allowed_linear_err=0.05,float p_allowed_angular_err=0.01);
  203. Animation();
  204. ~Animation();
  205. };
  206. VARIANT_ENUM_CAST( Animation::TrackType );
  207. VARIANT_ENUM_CAST( Animation::InterpolationType );
  208. #endif