animation.h 10 KB

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