animation_player.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**************************************************************************/
  2. /* animation_player.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_PLAYER_H
  31. #define ANIMATION_PLAYER_H
  32. #include "animation_mixer.h"
  33. #include "scene/2d/node_2d.h"
  34. #include "scene/resources/animation.h"
  35. class AnimationPlayer : public AnimationMixer {
  36. GDCLASS(AnimationPlayer, AnimationMixer);
  37. #ifndef DISABLE_DEPRECATED
  38. public:
  39. enum AnimationProcessCallback {
  40. ANIMATION_PROCESS_PHYSICS,
  41. ANIMATION_PROCESS_IDLE,
  42. ANIMATION_PROCESS_MANUAL,
  43. };
  44. enum AnimationMethodCallMode {
  45. ANIMATION_METHOD_CALL_DEFERRED,
  46. ANIMATION_METHOD_CALL_IMMEDIATE,
  47. };
  48. #endif // DISABLE_DEPRECATED
  49. private:
  50. AHashMap<StringName, StringName> animation_next_set; // For auto advance.
  51. float speed_scale = 1.0;
  52. double default_blend_time = 0.0;
  53. bool auto_capture = true;
  54. double auto_capture_duration = -1.0;
  55. Tween::TransitionType auto_capture_transition_type = Tween::TRANS_LINEAR;
  56. Tween::EaseType auto_capture_ease_type = Tween::EASE_IN;
  57. bool is_stopping = false;
  58. struct PlaybackData {
  59. AnimationData *from = nullptr;
  60. double pos = 0.0;
  61. float speed_scale = 1.0;
  62. double start_time = 0.0;
  63. double end_time = 0.0;
  64. double get_start_time() const {
  65. if (from && (Animation::is_less_approx(start_time, 0) || Animation::is_greater_approx(start_time, from->animation->get_length()))) {
  66. return 0;
  67. }
  68. return start_time;
  69. }
  70. double get_end_time() const {
  71. if (from && (Animation::is_less_approx(end_time, 0) || Animation::is_greater_approx(end_time, from->animation->get_length()))) {
  72. return from->animation->get_length();
  73. }
  74. return end_time;
  75. }
  76. };
  77. struct Blend {
  78. PlaybackData data;
  79. double blend_time = 0.0;
  80. double blend_left = 0.0;
  81. };
  82. struct Playback {
  83. PlaybackData current;
  84. StringName assigned;
  85. bool seeked = false;
  86. bool internal_seeked = false;
  87. bool started = false;
  88. List<Blend> blend;
  89. } playback;
  90. struct BlendKey {
  91. StringName from;
  92. StringName to;
  93. static uint32_t hash(const BlendKey &p_key) {
  94. return hash_one_uint64((uint64_t(p_key.from.hash()) << 32) | uint32_t(p_key.to.hash()));
  95. }
  96. bool operator==(const BlendKey &bk) const {
  97. return from == bk.from && to == bk.to;
  98. }
  99. bool operator<(const BlendKey &bk) const {
  100. if (from == bk.from) {
  101. return StringName::AlphCompare()(to, bk.to);
  102. } else {
  103. return StringName::AlphCompare()(from, bk.from);
  104. }
  105. }
  106. };
  107. HashMap<BlendKey, double, BlendKey> blend_times;
  108. List<StringName> playback_queue;
  109. ObjectID tmp_from;
  110. bool end_reached = false;
  111. bool end_notify = false;
  112. StringName autoplay;
  113. bool reset_on_save = true;
  114. bool movie_quit_on_finish = false;
  115. void _play(const StringName &p_name, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  116. void _capture(const StringName &p_name, bool p_from_end = false, double p_duration = -1.0, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
  117. void _process_playback_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_internal_seeked, bool p_started, bool p_is_current = false);
  118. void _blend_playback_data(double p_delta, bool p_started);
  119. void _stop_internal(bool p_reset, bool p_keep_state);
  120. void _check_immediately_after_start();
  121. float get_current_blend_amount();
  122. bool playing = false;
  123. protected:
  124. bool _set(const StringName &p_name, const Variant &p_value);
  125. bool _get(const StringName &p_name, Variant &r_ret) const;
  126. virtual void _validate_property(PropertyInfo &p_property) const override;
  127. void _get_property_list(List<PropertyInfo> *p_list) const;
  128. void _notification(int p_what);
  129. static void _bind_methods();
  130. // Make animation instances.
  131. virtual bool _blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map) override;
  132. virtual void _blend_capture(double p_delta) override;
  133. virtual void _blend_post_process() override;
  134. virtual void _animation_removed(const StringName &p_name, const StringName &p_library) override;
  135. virtual void _rename_animation(const StringName &p_from_name, const StringName &p_to_name) override;
  136. #ifndef DISABLE_DEPRECATED
  137. void _set_process_callback_bind_compat_80813(AnimationProcessCallback p_mode);
  138. AnimationProcessCallback _get_process_callback_bind_compat_80813() const;
  139. void _set_method_call_mode_bind_compat_80813(AnimationMethodCallMode p_mode);
  140. AnimationMethodCallMode _get_method_call_mode_bind_compat_80813() const;
  141. void _set_root_bind_compat_80813(const NodePath &p_root);
  142. NodePath _get_root_bind_compat_80813() const;
  143. void _seek_bind_compat_80813(double p_time, bool p_update = false);
  144. void _play_compat_84906(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  145. void _play_backwards_compat_84906(const StringName &p_name = StringName(), double p_custom_blend = -1);
  146. static void _bind_compatibility_methods();
  147. #endif // DISABLE_DEPRECATED
  148. public:
  149. void animation_set_next(const StringName &p_animation, const StringName &p_next);
  150. StringName animation_get_next(const StringName &p_animation) const;
  151. void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time);
  152. double get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
  153. void set_default_blend_time(double p_default);
  154. double get_default_blend_time() const;
  155. void set_auto_capture(bool p_auto_capture);
  156. bool is_auto_capture() const;
  157. void set_auto_capture_duration(double p_auto_capture_duration);
  158. double get_auto_capture_duration() const;
  159. void set_auto_capture_transition_type(Tween::TransitionType p_auto_capture_transition_type);
  160. Tween::TransitionType get_auto_capture_transition_type() const;
  161. void set_auto_capture_ease_type(Tween::EaseType p_auto_capture_ease_type);
  162. Tween::EaseType get_auto_capture_ease_type() const;
  163. #ifdef TOOLS_ENABLED
  164. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  165. #endif
  166. void play(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  167. void play_section_with_markers(const StringName &p_name = StringName(), const StringName &p_start_marker = StringName(), const StringName &p_end_marker = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  168. void play_section(const StringName &p_name = StringName(), double p_start_time = -1, double p_end_time = -1, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  169. void play_backwards(const StringName &p_name = StringName(), double p_custom_blend = -1);
  170. void play_section_with_markers_backwards(const StringName &p_name = StringName(), const StringName &p_start_marker = StringName(), const StringName &p_end_marker = StringName(), double p_custom_blend = -1);
  171. void play_section_backwards(const StringName &p_name = StringName(), double p_start_time = -1, double p_end_time = -1, double p_custom_blend = -1);
  172. void play_with_capture(const StringName &p_name = StringName(), double p_duration = -1.0, double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false, Tween::TransitionType p_trans_type = Tween::TRANS_LINEAR, Tween::EaseType p_ease_type = Tween::EASE_IN);
  173. void queue(const StringName &p_name);
  174. Vector<String> get_queue();
  175. void clear_queue();
  176. void pause();
  177. void stop(bool p_keep_state = false);
  178. bool is_playing() const;
  179. String get_current_animation() const;
  180. void set_current_animation(const String &p_animation);
  181. String get_assigned_animation() const;
  182. void set_assigned_animation(const String &p_animation);
  183. bool is_valid() const;
  184. void set_speed_scale(float p_speed);
  185. float get_speed_scale() const;
  186. float get_playing_speed() const;
  187. void set_autoplay(const String &p_name);
  188. String get_autoplay() const;
  189. void set_movie_quit_on_finish_enabled(bool p_enabled);
  190. bool is_movie_quit_on_finish_enabled() const;
  191. void seek_internal(double p_time, bool p_update = false, bool p_update_only = false, bool p_is_internal_seek = false);
  192. void seek(double p_time, bool p_update = false, bool p_update_only = false);
  193. double get_current_animation_position() const;
  194. double get_current_animation_length() const;
  195. void set_section_with_markers(const StringName &p_start_marker = StringName(), const StringName &p_end_marker = StringName());
  196. void set_section(double p_start_time = -1, double p_end_time = -1);
  197. void reset_section();
  198. double get_section_start_time() const;
  199. double get_section_end_time() const;
  200. bool has_section() const;
  201. virtual void advance(double p_time) override;
  202. AnimationPlayer();
  203. ~AnimationPlayer();
  204. };
  205. #ifndef DISABLE_DEPRECATED
  206. VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessCallback);
  207. VARIANT_ENUM_CAST(AnimationPlayer::AnimationMethodCallMode);
  208. #endif // DISABLE_DEPRECATED
  209. #endif // ANIMATION_PLAYER_H