animation_player.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. HashMap<StringName, StringName> animation_next_set; // For auto advance.
  51. float speed_scale = 1.0;
  52. double default_blend_time = 0.0;
  53. bool is_stopping = false;
  54. struct PlaybackData {
  55. AnimationData *from = nullptr;
  56. double pos = 0.0;
  57. float speed_scale = 1.0;
  58. };
  59. struct Blend {
  60. PlaybackData data;
  61. double blend_time = 0.0;
  62. double blend_left = 0.0;
  63. };
  64. struct Playback {
  65. PlaybackData current;
  66. StringName assigned;
  67. bool seeked = false;
  68. bool started = false;
  69. List<Blend> blend;
  70. } playback;
  71. struct BlendKey {
  72. StringName from;
  73. StringName to;
  74. static uint32_t hash(const BlendKey &p_key) {
  75. return hash_one_uint64((uint64_t(p_key.from.hash()) << 32) | uint32_t(p_key.to.hash()));
  76. }
  77. bool operator==(const BlendKey &bk) const {
  78. return from == bk.from && to == bk.to;
  79. }
  80. bool operator<(const BlendKey &bk) const {
  81. if (from == bk.from) {
  82. return to < bk.to;
  83. } else {
  84. return from < bk.from;
  85. }
  86. }
  87. };
  88. HashMap<BlendKey, double, BlendKey> blend_times;
  89. List<StringName> playback_queue;
  90. ObjectID tmp_from;
  91. bool end_reached = false;
  92. bool end_notify = false;
  93. StringName autoplay;
  94. bool reset_on_save = true;
  95. bool movie_quit_on_finish = false;
  96. void _process_playback_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_started, bool p_is_current = false);
  97. void _blend_playback_data(double p_delta, bool p_started);
  98. void _stop_internal(bool p_reset, bool p_keep_state);
  99. void _check_immediately_after_start();
  100. float get_current_blend_amount();
  101. bool playing = false;
  102. protected:
  103. bool _set(const StringName &p_name, const Variant &p_value);
  104. bool _get(const StringName &p_name, Variant &r_ret) const;
  105. virtual void _validate_property(PropertyInfo &p_property) const override;
  106. void _get_property_list(List<PropertyInfo> *p_list) const;
  107. void _notification(int p_what);
  108. static void _bind_methods();
  109. // Make animation instances.
  110. virtual bool _blend_pre_process(double p_delta, int p_track_count, const HashMap<NodePath, int> &p_track_map) override;
  111. virtual void _blend_post_process() override;
  112. virtual void _animation_removed(const StringName &p_name, const StringName &p_library) override;
  113. virtual void _rename_animation(const StringName &p_from_name, const StringName &p_to_name) override;
  114. #ifndef DISABLE_DEPRECATED
  115. void _set_process_callback_bind_compat_80813(AnimationProcessCallback p_mode);
  116. AnimationProcessCallback _get_process_callback_bind_compat_80813() const;
  117. void _set_method_call_mode_bind_compat_80813(AnimationMethodCallMode p_mode);
  118. AnimationMethodCallMode _get_method_call_mode_bind_compat_80813() const;
  119. void _set_root_bind_compat_80813(const NodePath &p_root);
  120. NodePath _get_root_bind_compat_80813() const;
  121. void _seek_bind_compat_80813(double p_time, bool p_update = false);
  122. static void _bind_compatibility_methods();
  123. #endif // DISABLE_DEPRECATED
  124. public:
  125. void animation_set_next(const StringName &p_animation, const StringName &p_next);
  126. StringName animation_get_next(const StringName &p_animation) const;
  127. void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time);
  128. double get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
  129. void set_default_blend_time(double p_default);
  130. double get_default_blend_time() const;
  131. void play(const StringName &p_name = StringName(), double p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
  132. void play_backwards(const StringName &p_name = StringName(), double p_custom_blend = -1);
  133. void queue(const StringName &p_name);
  134. Vector<String> get_queue();
  135. void clear_queue();
  136. void pause();
  137. void stop(bool p_keep_state = false);
  138. bool is_playing() const;
  139. String get_current_animation() const;
  140. void set_current_animation(const String &p_animation);
  141. String get_assigned_animation() const;
  142. void set_assigned_animation(const String &p_animation);
  143. bool is_valid() const;
  144. void set_speed_scale(float p_speed);
  145. float get_speed_scale() const;
  146. float get_playing_speed() const;
  147. void set_autoplay(const String &p_name);
  148. String get_autoplay() const;
  149. void set_movie_quit_on_finish_enabled(bool p_enabled);
  150. bool is_movie_quit_on_finish_enabled() const;
  151. void seek(double p_time, bool p_update = false, bool p_update_only = false);
  152. double get_current_animation_position() const;
  153. double get_current_animation_length() const;
  154. void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  155. virtual void advance(double p_time) override;
  156. AnimationPlayer();
  157. ~AnimationPlayer();
  158. };
  159. #ifndef DISABLE_DEPRECATED
  160. VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessCallback);
  161. VARIANT_ENUM_CAST(AnimationPlayer::AnimationMethodCallMode);
  162. #endif // DISABLE_DEPRECATED
  163. #endif // ANIMATION_PLAYER_H