animation_tree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**************************************************************************/
  2. /* animation_tree.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_TREE_H
  31. #define ANIMATION_TREE_H
  32. #include "animation_mixer.h"
  33. #include "scene/resources/animation.h"
  34. #define HUGE_LENGTH 31540000 // 31540000 seconds mean 1 year... is it too long? It must be longer than any Animation length and Transition xfade time to prevent time inversion for AnimationNodeStateMachine.
  35. class AnimationNodeBlendTree;
  36. class AnimationNodeStartState;
  37. class AnimationNodeEndState;
  38. class AnimationTree;
  39. class AnimationNode : public Resource {
  40. GDCLASS(AnimationNode, Resource);
  41. public:
  42. friend class AnimationTree;
  43. enum FilterAction {
  44. FILTER_IGNORE,
  45. FILTER_PASS,
  46. FILTER_STOP,
  47. FILTER_BLEND
  48. };
  49. struct Input {
  50. String name;
  51. };
  52. bool closable = false;
  53. Vector<Input> inputs;
  54. HashMap<NodePath, bool> filter;
  55. bool filter_enabled = false;
  56. // Temporary state for blending process which needs to be stored in each AnimationNodes.
  57. struct NodeState {
  58. StringName base_path;
  59. AnimationNode *parent = nullptr;
  60. Vector<StringName> connections;
  61. Vector<real_t> track_weights;
  62. } node_state;
  63. // Temporary state for blending process which needs to be started in the AnimationTree, pass through the AnimationNodes, and then return to the AnimationTree.
  64. struct ProcessState {
  65. AnimationTree *tree = nullptr;
  66. HashMap<NodePath, int> track_map; // TODO: Is there a better way to manage filter/tracks?
  67. bool is_testing = false;
  68. bool valid = false;
  69. String invalid_reasons;
  70. uint64_t last_pass = 0;
  71. } *process_state = nullptr;
  72. Array _get_filters() const;
  73. void _set_filters(const Array &p_filters);
  74. friend class AnimationNodeBlendTree;
  75. double _blend_node(Ref<AnimationNode> p_node, const StringName &p_subpath, AnimationNode *p_new_parent, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false, real_t *r_activity = nullptr);
  76. double _pre_process(ProcessState *p_process_state, AnimationMixer::PlaybackInfo p_playback_info);
  77. protected:
  78. virtual double _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false);
  79. double process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false);
  80. void blend_animation(const StringName &p_animation, AnimationMixer::PlaybackInfo p_playback_info);
  81. double blend_node(Ref<AnimationNode> p_node, const StringName &p_subpath, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
  82. double blend_input(int p_input, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
  83. // Bind-able methods to expose for compatibility, moreover AnimationMixer::PlaybackInfo is not exposed.
  84. void blend_animation_ex(const StringName &p_animation, double p_time, double p_delta, bool p_seeked, bool p_is_external_seeking, real_t p_blend, Animation::LoopedFlag p_looped_flag = Animation::LOOPED_FLAG_NONE);
  85. double blend_node_ex(const StringName &p_sub_path, Ref<AnimationNode> p_node, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
  86. double blend_input_ex(int p_input, double p_time, bool p_seek, bool p_is_external_seeking, real_t p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
  87. void make_invalid(const String &p_reason);
  88. AnimationTree *get_animation_tree() const;
  89. static void _bind_methods();
  90. void _validate_property(PropertyInfo &p_property) const;
  91. GDVIRTUAL0RC(Dictionary, _get_child_nodes)
  92. GDVIRTUAL0RC(Array, _get_parameter_list)
  93. GDVIRTUAL1RC(Ref<AnimationNode>, _get_child_by_name, StringName)
  94. GDVIRTUAL1RC(Variant, _get_parameter_default_value, StringName)
  95. GDVIRTUAL1RC(bool, _is_parameter_read_only, StringName)
  96. GDVIRTUAL4RC(double, _process, double, bool, bool, bool)
  97. GDVIRTUAL0RC(String, _get_caption)
  98. GDVIRTUAL0RC(bool, _has_filter)
  99. public:
  100. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  101. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  102. virtual bool is_parameter_read_only(const StringName &p_parameter) const;
  103. void set_parameter(const StringName &p_name, const Variant &p_value);
  104. Variant get_parameter(const StringName &p_name) const;
  105. struct ChildNode {
  106. StringName name;
  107. Ref<AnimationNode> node;
  108. };
  109. virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
  110. virtual String get_caption() const;
  111. virtual bool add_input(const String &p_name);
  112. virtual void remove_input(int p_index);
  113. virtual bool set_input_name(int p_input, const String &p_name);
  114. virtual String get_input_name(int p_input) const;
  115. int get_input_count() const;
  116. int find_input(const String &p_name) const;
  117. void set_filter_path(const NodePath &p_path, bool p_enable);
  118. bool is_path_filtered(const NodePath &p_path) const;
  119. void set_filter_enabled(bool p_enable);
  120. bool is_filter_enabled() const;
  121. void set_closable(bool p_closable);
  122. bool is_closable() const;
  123. virtual bool has_filter() const;
  124. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const;
  125. Ref<AnimationNode> find_node_by_path(const String &p_name) const;
  126. AnimationNode();
  127. };
  128. VARIANT_ENUM_CAST(AnimationNode::FilterAction)
  129. // Root node does not allow inputs.
  130. class AnimationRootNode : public AnimationNode {
  131. GDCLASS(AnimationRootNode, AnimationNode);
  132. protected:
  133. virtual void _tree_changed();
  134. virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
  135. virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
  136. public:
  137. AnimationRootNode() {}
  138. };
  139. class AnimationNodeStartState : public AnimationRootNode {
  140. GDCLASS(AnimationNodeStartState, AnimationRootNode);
  141. };
  142. class AnimationNodeEndState : public AnimationRootNode {
  143. GDCLASS(AnimationNodeEndState, AnimationRootNode);
  144. };
  145. class AnimationTree : public AnimationMixer {
  146. GDCLASS(AnimationTree, AnimationMixer);
  147. #ifndef DISABLE_DEPRECATED
  148. public:
  149. enum AnimationProcessCallback {
  150. ANIMATION_PROCESS_PHYSICS,
  151. ANIMATION_PROCESS_IDLE,
  152. ANIMATION_PROCESS_MANUAL,
  153. };
  154. #endif // DISABLE_DEPRECATED
  155. private:
  156. Ref<AnimationRootNode> root_animation_node;
  157. NodePath advance_expression_base_node = NodePath(String("."));
  158. AnimationNode::ProcessState process_state;
  159. uint64_t process_pass = 1;
  160. bool started = true;
  161. friend class AnimationNode;
  162. List<PropertyInfo> properties;
  163. HashMap<StringName, HashMap<StringName, StringName>> property_parent_map;
  164. HashMap<ObjectID, StringName> property_reference_map;
  165. HashMap<StringName, Pair<Variant, bool>> property_map; // Property value and read-only flag.
  166. bool properties_dirty = true;
  167. void _update_properties();
  168. void _update_properties_for_node(const String &p_base_path, Ref<AnimationNode> p_node);
  169. void _tree_changed();
  170. void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
  171. void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
  172. struct Activity {
  173. uint64_t last_pass = 0;
  174. real_t activity = 0.0;
  175. };
  176. HashMap<StringName, Vector<Activity>> input_activity_map;
  177. HashMap<StringName, Vector<Activity> *> input_activity_map_get;
  178. NodePath animation_player;
  179. void _setup_animation_player();
  180. void _animation_player_changed();
  181. bool _set(const StringName &p_name, const Variant &p_value);
  182. bool _get(const StringName &p_name, Variant &r_ret) const;
  183. void _get_property_list(List<PropertyInfo> *p_list) const;
  184. virtual void _validate_property(PropertyInfo &p_property) const override;
  185. void _notification(int p_what);
  186. static void _bind_methods();
  187. virtual void _set_active(bool p_active) override;
  188. // Make animation instances.
  189. virtual bool _blend_pre_process(double p_delta, int p_track_count, const HashMap<NodePath, int> &p_track_map) override;
  190. #ifndef DISABLE_DEPRECATED
  191. void _set_process_callback_bind_compat_80813(AnimationProcessCallback p_mode);
  192. AnimationProcessCallback _get_process_callback_bind_compat_80813() const;
  193. void _set_tree_root_bind_compat_80813(const Ref<AnimationNode> &p_root);
  194. Ref<AnimationNode> _get_tree_root_bind_compat_80813() const;
  195. static void _bind_compatibility_methods();
  196. #endif // DISABLE_DEPRECATED
  197. public:
  198. void set_animation_player(const NodePath &p_path);
  199. NodePath get_animation_player() const;
  200. void set_root_animation_node(const Ref<AnimationRootNode> &p_animation_node);
  201. Ref<AnimationRootNode> get_root_animation_node() const;
  202. void set_advance_expression_base_node(const NodePath &p_path);
  203. NodePath get_advance_expression_base_node() const;
  204. PackedStringArray get_configuration_warnings() const override;
  205. bool is_state_invalid() const;
  206. String get_invalid_state_reason() const;
  207. real_t get_connection_activity(const StringName &p_path, int p_connection) const;
  208. uint64_t get_last_process_pass() const;
  209. AnimationTree();
  210. ~AnimationTree();
  211. };
  212. #ifndef DISABLE_DEPRECATED
  213. VARIANT_ENUM_CAST(AnimationTree::AnimationProcessCallback);
  214. #endif // DISABLE_DEPRECATED
  215. #endif // ANIMATION_TREE_H