animation.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*************************************************************************/
  2. /* animation.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/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. TYPE_BEZIER, ///< Bezier curve
  45. TYPE_AUDIO,
  46. TYPE_ANIMATION,
  47. };
  48. enum InterpolationType {
  49. INTERPOLATION_NEAREST,
  50. INTERPOLATION_LINEAR,
  51. INTERPOLATION_CUBIC
  52. };
  53. enum UpdateMode {
  54. UPDATE_CONTINUOUS,
  55. UPDATE_DISCRETE,
  56. UPDATE_TRIGGER,
  57. UPDATE_CAPTURE,
  58. };
  59. private:
  60. struct Track {
  61. TrackType type;
  62. InterpolationType interpolation;
  63. bool loop_wrap;
  64. NodePath path; // path to something
  65. bool imported;
  66. bool enabled;
  67. Track() {
  68. interpolation = INTERPOLATION_LINEAR;
  69. imported = false;
  70. loop_wrap = true;
  71. enabled = true;
  72. }
  73. virtual ~Track() {}
  74. };
  75. struct Key {
  76. float transition;
  77. float time; // time in secs
  78. Key() { transition = 1; }
  79. };
  80. // transform key holds either Vector3 or Quaternion
  81. template <class T>
  82. struct TKey : public Key {
  83. float time;
  84. T value;
  85. };
  86. struct TransformKey {
  87. Vector3 loc;
  88. Quat rot;
  89. Vector3 scale;
  90. };
  91. /* TRANSFORM TRACK */
  92. struct TransformTrack : public Track {
  93. Vector<TKey<TransformKey> > transforms;
  94. TransformTrack() { type = TYPE_TRANSFORM; }
  95. };
  96. /* PROPERTY VALUE TRACK */
  97. struct ValueTrack : public Track {
  98. UpdateMode update_mode;
  99. bool update_on_seek;
  100. Vector<TKey<Variant> > values;
  101. ValueTrack() {
  102. type = TYPE_VALUE;
  103. update_mode = UPDATE_CONTINUOUS;
  104. }
  105. };
  106. /* METHOD TRACK */
  107. struct MethodKey : public Key {
  108. StringName method;
  109. Vector<Variant> params;
  110. };
  111. struct MethodTrack : public Track {
  112. Vector<MethodKey> methods;
  113. MethodTrack() { type = TYPE_METHOD; }
  114. };
  115. /* BEZIER TRACK */
  116. struct BezierKey {
  117. Vector2 in_handle; //relative (x always <0)
  118. Vector2 out_handle; //relative (x always >0)
  119. float value;
  120. };
  121. struct BezierTrack : public Track {
  122. Vector<TKey<BezierKey> > values;
  123. BezierTrack() {
  124. type = TYPE_BEZIER;
  125. }
  126. };
  127. /* AUDIO TRACK */
  128. struct AudioKey {
  129. RES stream;
  130. float start_offset; //offset from start
  131. float end_offset; //offset from end, if 0 then full length or infinite
  132. AudioKey() {
  133. start_offset = 0;
  134. end_offset = 0;
  135. }
  136. };
  137. struct AudioTrack : public Track {
  138. Vector<TKey<AudioKey> > values;
  139. AudioTrack() {
  140. type = TYPE_AUDIO;
  141. }
  142. };
  143. /* AUDIO TRACK */
  144. struct AnimationTrack : public Track {
  145. Vector<TKey<StringName> > values;
  146. AnimationTrack() {
  147. type = TYPE_ANIMATION;
  148. }
  149. };
  150. Vector<Track *> tracks;
  151. /*
  152. template<class T>
  153. int _insert_pos(float p_time, T& p_keys);*/
  154. template <class T>
  155. void _clear(T &p_keys);
  156. template <class T, class V>
  157. int _insert(float p_time, T &p_keys, const V &p_value);
  158. template <class K>
  159. inline int _find(const Vector<K> &p_keys, float p_time) const;
  160. _FORCE_INLINE_ Animation::TransformKey _interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, float p_c) const;
  161. _FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const;
  162. _FORCE_INLINE_ Quat _interpolate(const Quat &p_a, const Quat &p_b, float p_c) const;
  163. _FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, float p_c) const;
  164. _FORCE_INLINE_ float _interpolate(const float &p_a, const float &p_b, float p_c) const;
  165. _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;
  166. _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;
  167. _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;
  168. _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;
  169. _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;
  170. template <class T>
  171. _FORCE_INLINE_ T _interpolate(const Vector<TKey<T> > &p_keys, float p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const;
  172. template <class T>
  173. _FORCE_INLINE_ void _track_get_key_indices_in_range(const Vector<T> &p_array, float from_time, float to_time, List<int> *p_indices) const;
  174. _FORCE_INLINE_ void _value_track_get_key_indices_in_range(const ValueTrack *vt, float from_time, float to_time, List<int> *p_indices) const;
  175. _FORCE_INLINE_ void _method_track_get_key_indices_in_range(const MethodTrack *mt, float from_time, float to_time, List<int> *p_indices) const;
  176. float length;
  177. float step;
  178. bool loop;
  179. // bind helpers
  180. private:
  181. Array _transform_track_interpolate(int p_track, float p_time) const {
  182. Vector3 loc;
  183. Quat rot;
  184. Vector3 scale;
  185. transform_track_interpolate(p_track, p_time, &loc, &rot, &scale);
  186. Array ret;
  187. ret.push_back(loc);
  188. ret.push_back(rot);
  189. ret.push_back(scale);
  190. return ret;
  191. }
  192. PoolVector<int> _value_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  193. List<int> idxs;
  194. value_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  195. PoolVector<int> idxr;
  196. for (List<int>::Element *E = idxs.front(); E; E = E->next()) {
  197. idxr.push_back(E->get());
  198. }
  199. return idxr;
  200. }
  201. PoolVector<int> _method_track_get_key_indices(int p_track, float p_time, float p_delta) const {
  202. List<int> idxs;
  203. method_track_get_key_indices(p_track, p_time, p_delta, &idxs);
  204. PoolVector<int> idxr;
  205. for (List<int>::Element *E = idxs.front(); E; E = E->next()) {
  206. idxr.push_back(E->get());
  207. }
  208. return idxr;
  209. }
  210. 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);
  211. 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);
  212. protected:
  213. bool _set(const StringName &p_name, const Variant &p_value);
  214. bool _get(const StringName &p_name, Variant &r_ret) const;
  215. void _get_property_list(List<PropertyInfo> *p_list) const;
  216. static void _bind_methods();
  217. public:
  218. int add_track(TrackType p_type, int p_at_pos = -1);
  219. void remove_track(int p_track);
  220. int get_track_count() const;
  221. TrackType track_get_type(int p_track) const;
  222. void track_set_path(int p_track, const NodePath &p_path);
  223. NodePath track_get_path(int p_track) const;
  224. int find_track(const NodePath &p_path) const;
  225. // transform
  226. void track_move_up(int p_track);
  227. void track_move_down(int p_track);
  228. void track_swap(int p_track, int p_with_track);
  229. void track_set_imported(int p_track, bool p_imported);
  230. bool track_is_imported(int p_track) const;
  231. void track_set_enabled(int p_track, bool p_enabled);
  232. bool track_is_enabled(int p_track) const;
  233. void track_insert_key(int p_track, float p_time, const Variant &p_key, float p_transition = 1);
  234. void track_set_key_transition(int p_track, int p_key_idx, float p_transition);
  235. void track_set_key_value(int p_track, int p_key_idx, const Variant &p_value);
  236. int track_find_key(int p_track, float p_time, bool p_exact = false) const;
  237. void track_remove_key(int p_track, int p_idx);
  238. void track_remove_key_at_position(int p_track, float p_pos);
  239. int track_get_key_count(int p_track) const;
  240. Variant track_get_key_value(int p_track, int p_key_idx) const;
  241. float track_get_key_time(int p_track, int p_key_idx) const;
  242. float track_get_key_transition(int p_track, int p_key_idx) const;
  243. 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());
  244. Error transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  245. void track_set_interpolation_type(int p_track, InterpolationType p_interp);
  246. InterpolationType track_get_interpolation_type(int p_track) const;
  247. int bezier_track_insert_key(int p_track, float p_time, float p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle);
  248. void bezier_track_set_key_value(int p_track, int p_index, float p_value);
  249. void bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle);
  250. void bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle);
  251. float bezier_track_get_key_value(int p_track, int p_index) const;
  252. Vector2 bezier_track_get_key_in_handle(int p_track, int p_index) const;
  253. Vector2 bezier_track_get_key_out_handle(int p_track, int p_index) const;
  254. float bezier_track_interpolate(int p_track, float p_time) const;
  255. int audio_track_insert_key(int p_track, float p_time, const RES &p_stream, float p_start_offset = 0, float p_end_offset = 0);
  256. void audio_track_set_key_stream(int p_track, int p_key, const RES &p_stream);
  257. void audio_track_set_key_start_offset(int p_track, int p_key, float p_offset);
  258. void audio_track_set_key_end_offset(int p_track, int p_key, float p_offset);
  259. RES audio_track_get_key_stream(int p_track, int p_key) const;
  260. float audio_track_get_key_start_offset(int p_track, int p_key) const;
  261. float audio_track_get_key_end_offset(int p_track, int p_key) const;
  262. int animation_track_insert_key(int p_track, float p_time, const StringName &p_animation);
  263. void animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation);
  264. StringName animation_track_get_key_animation(int p_track, int p_key) const;
  265. void track_set_interpolation_loop_wrap(int p_track, bool p_enable);
  266. bool track_get_interpolation_loop_wrap(int p_track) const;
  267. Error transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
  268. Variant value_track_interpolate(int p_track, float p_time) const;
  269. void value_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  270. void value_track_set_update_mode(int p_track, UpdateMode p_mode);
  271. UpdateMode value_track_get_update_mode(int p_track) const;
  272. void method_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  273. Vector<Variant> method_track_get_params(int p_track, int p_key_idx) const;
  274. StringName method_track_get_name(int p_track, int p_key_idx) const;
  275. void copy_track(int p_track, Ref<Animation> p_to_animation);
  276. void track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
  277. void set_length(float p_length);
  278. float get_length() const;
  279. void set_loop(bool p_enabled);
  280. bool has_loop() const;
  281. void set_step(float p_step);
  282. float get_step() const;
  283. void clear();
  284. 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);
  285. Animation();
  286. ~Animation();
  287. };
  288. VARIANT_ENUM_CAST(Animation::TrackType);
  289. VARIANT_ENUM_CAST(Animation::InterpolationType);
  290. VARIANT_ENUM_CAST(Animation::UpdateMode);
  291. #endif