animation.h 13 KB

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