animation_tree.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. AHashMap<NodePath, bool> filter;
  55. bool filter_enabled = false;
  56. // To propagate information from upstream for use in estimation of playback progress.
  57. // These values must be taken from the result of blend_node() or blend_input() and must be essentially read-only.
  58. // For example, if you want to change the position, you need to change the pi.time value of PlaybackInfo passed to blend_input(pi) and get the result.
  59. struct NodeTimeInfo {
  60. // Retain the previous frame values. These are stored into the AnimationTree's Map and exposing them as read-only values.
  61. double length = 0.0;
  62. double position = 0.0;
  63. double delta = 0.0;
  64. // Needs internally to estimate remain time, the previous frame values are not retained.
  65. Animation::LoopMode loop_mode = Animation::LOOP_NONE;
  66. bool will_end = false; // For breaking loop, it is true when just looped.
  67. bool is_infinity = false; // For unpredictable state machine's end.
  68. bool is_looping() {
  69. return loop_mode != Animation::LOOP_NONE;
  70. }
  71. double get_remain(bool p_break_loop = false) {
  72. if ((is_looping() && !p_break_loop) || is_infinity) {
  73. return HUGE_LENGTH;
  74. }
  75. if (is_looping() && p_break_loop && will_end) {
  76. return 0;
  77. }
  78. double remain = length - position;
  79. if (Math::is_zero_approx(remain)) {
  80. return 0;
  81. }
  82. return remain;
  83. }
  84. };
  85. // Temporary state for blending process which needs to be stored in each AnimationNodes.
  86. struct NodeState {
  87. friend AnimationNode;
  88. private:
  89. StringName base_path;
  90. public:
  91. AnimationNode *parent = nullptr;
  92. Vector<StringName> connections;
  93. Vector<real_t> track_weights;
  94. const StringName get_base_path() const {
  95. return base_path;
  96. }
  97. } node_state;
  98. // Temporary state for blending process which needs to be started in the AnimationTree, pass through the AnimationNodes, and then return to the AnimationTree.
  99. struct ProcessState {
  100. AnimationTree *tree = nullptr;
  101. const AHashMap<NodePath, int> *track_map; // TODO: Is there a better way to manage filter/tracks?
  102. bool is_testing = false;
  103. bool valid = false;
  104. String invalid_reasons;
  105. uint64_t last_pass = 0;
  106. } *process_state = nullptr;
  107. private:
  108. mutable AHashMap<StringName, int> property_cache;
  109. public:
  110. void set_node_state_base_path(const StringName p_base_path) {
  111. if (p_base_path != node_state.base_path) {
  112. node_state.base_path = p_base_path;
  113. make_cache_dirty();
  114. }
  115. }
  116. void set_node_state_base_path(const String p_base_path) {
  117. if (p_base_path != node_state.base_path) {
  118. node_state.base_path = p_base_path;
  119. make_cache_dirty();
  120. }
  121. }
  122. const StringName get_node_state_base_path() const {
  123. return node_state.get_base_path();
  124. }
  125. void make_cache_dirty() {
  126. property_cache.clear();
  127. }
  128. Array _get_filters() const;
  129. void _set_filters(const Array &p_filters);
  130. friend class AnimationNodeBlendTree;
  131. // The time information is passed from upstream to downstream by AnimationMixer::PlaybackInfo::p_playback_info until AnimationNodeAnimation processes it.
  132. // Conversely, AnimationNodeAnimation returns the processed result as NodeTimeInfo from downstream to upstream.
  133. NodeTimeInfo _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);
  134. NodeTimeInfo _pre_process(ProcessState *p_process_state, AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false);
  135. protected:
  136. StringName current_length = "current_length";
  137. StringName current_position = "current_position";
  138. StringName current_delta = "current_delta";
  139. virtual NodeTimeInfo process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false); // To organize time information. Virtualizing for especially AnimationNodeAnimation needs to take "backward" into account.
  140. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false); // Main process.
  141. void blend_animation(const StringName &p_animation, AnimationMixer::PlaybackInfo p_playback_info);
  142. NodeTimeInfo 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);
  143. NodeTimeInfo blend_input(int p_input, AnimationMixer::PlaybackInfo p_playback_info, FilterAction p_filter = FILTER_IGNORE, bool p_sync = true, bool p_test_only = false);
  144. // Bind-able methods to expose for compatibility, moreover AnimationMixer::PlaybackInfo is not exposed.
  145. 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);
  146. 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);
  147. 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);
  148. void make_invalid(const String &p_reason);
  149. AnimationTree *get_animation_tree() const;
  150. static void _bind_methods();
  151. void _validate_property(PropertyInfo &p_property) const;
  152. GDVIRTUAL0RC(Dictionary, _get_child_nodes)
  153. GDVIRTUAL0RC(Array, _get_parameter_list)
  154. GDVIRTUAL1RC(Ref<AnimationNode>, _get_child_by_name, StringName)
  155. GDVIRTUAL1RC(Variant, _get_parameter_default_value, StringName)
  156. GDVIRTUAL1RC(bool, _is_parameter_read_only, StringName)
  157. GDVIRTUAL4R(double, _process, double, bool, bool, bool)
  158. GDVIRTUAL0RC(String, _get_caption)
  159. GDVIRTUAL0RC(bool, _has_filter)
  160. public:
  161. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  162. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  163. virtual bool is_parameter_read_only(const StringName &p_parameter) const;
  164. void set_parameter(const StringName &p_name, const Variant &p_value);
  165. Variant get_parameter(const StringName &p_name) const;
  166. void set_node_time_info(const NodeTimeInfo &p_node_time_info); // Wrapper of set_parameter().
  167. virtual NodeTimeInfo get_node_time_info() const; // Wrapper of get_parameter().
  168. struct ChildNode {
  169. StringName name;
  170. Ref<AnimationNode> node;
  171. };
  172. virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
  173. virtual String get_caption() const;
  174. virtual bool add_input(const String &p_name);
  175. virtual void remove_input(int p_index);
  176. virtual bool set_input_name(int p_input, const String &p_name);
  177. virtual String get_input_name(int p_input) const;
  178. int get_input_count() const;
  179. int find_input(const String &p_name) const;
  180. void set_filter_path(const NodePath &p_path, bool p_enable);
  181. bool is_path_filtered(const NodePath &p_path) const;
  182. void set_filter_enabled(bool p_enable);
  183. bool is_filter_enabled() const;
  184. void set_deletable(bool p_closable);
  185. bool is_deletable() const;
  186. virtual bool has_filter() const;
  187. #ifdef TOOLS_ENABLED
  188. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  189. #endif
  190. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const;
  191. Ref<AnimationNode> find_node_by_path(const String &p_name) const;
  192. AnimationNode();
  193. };
  194. VARIANT_ENUM_CAST(AnimationNode::FilterAction)
  195. // Root node does not allow inputs.
  196. class AnimationRootNode : public AnimationNode {
  197. GDCLASS(AnimationRootNode, AnimationNode);
  198. protected:
  199. virtual void _tree_changed();
  200. virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
  201. virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
  202. public:
  203. AnimationRootNode() {}
  204. };
  205. class AnimationNodeStartState : public AnimationRootNode {
  206. GDCLASS(AnimationNodeStartState, AnimationRootNode);
  207. };
  208. class AnimationNodeEndState : public AnimationRootNode {
  209. GDCLASS(AnimationNodeEndState, AnimationRootNode);
  210. };
  211. class AnimationTree : public AnimationMixer {
  212. GDCLASS(AnimationTree, AnimationMixer);
  213. #ifndef DISABLE_DEPRECATED
  214. public:
  215. enum AnimationProcessCallback {
  216. ANIMATION_PROCESS_PHYSICS,
  217. ANIMATION_PROCESS_IDLE,
  218. ANIMATION_PROCESS_MANUAL,
  219. };
  220. #endif // DISABLE_DEPRECATED
  221. private:
  222. Ref<AnimationRootNode> root_animation_node;
  223. NodePath advance_expression_base_node = NodePath(String("."));
  224. AnimationNode::ProcessState process_state;
  225. uint64_t process_pass = 1;
  226. bool started = true;
  227. friend class AnimationNode;
  228. List<PropertyInfo> properties;
  229. AHashMap<StringName, AHashMap<StringName, StringName>> property_parent_map;
  230. AHashMap<ObjectID, StringName> property_reference_map;
  231. AHashMap<StringName, Pair<Variant, bool>> property_map; // Property value and read-only flag.
  232. bool properties_dirty = true;
  233. void _update_properties();
  234. void _update_properties_for_node(const String &p_base_path, Ref<AnimationNode> p_node);
  235. void _tree_changed();
  236. void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name);
  237. void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node);
  238. struct Activity {
  239. uint64_t last_pass = 0;
  240. real_t activity = 0.0;
  241. };
  242. HashMap<StringName, Vector<Activity>> input_activity_map;
  243. HashMap<StringName, Vector<Activity> *> input_activity_map_get;
  244. NodePath animation_player;
  245. void _setup_animation_player();
  246. void _animation_player_changed();
  247. bool _set(const StringName &p_name, const Variant &p_value);
  248. bool _get(const StringName &p_name, Variant &r_ret) const;
  249. void _get_property_list(List<PropertyInfo> *p_list) const;
  250. virtual void _validate_property(PropertyInfo &p_property) const override;
  251. void _notification(int p_what);
  252. static void _bind_methods();
  253. virtual void _set_active(bool p_active) override;
  254. // Make animation instances.
  255. virtual bool _blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map) override;
  256. #ifndef DISABLE_DEPRECATED
  257. void _set_process_callback_bind_compat_80813(AnimationProcessCallback p_mode);
  258. AnimationProcessCallback _get_process_callback_bind_compat_80813() const;
  259. void _set_tree_root_bind_compat_80813(const Ref<AnimationNode> &p_root);
  260. Ref<AnimationNode> _get_tree_root_bind_compat_80813() const;
  261. static void _bind_compatibility_methods();
  262. #endif // DISABLE_DEPRECATED
  263. public:
  264. void set_animation_player(const NodePath &p_path);
  265. NodePath get_animation_player() const;
  266. void set_root_animation_node(const Ref<AnimationRootNode> &p_animation_node);
  267. Ref<AnimationRootNode> get_root_animation_node() const;
  268. void set_advance_expression_base_node(const NodePath &p_path);
  269. NodePath get_advance_expression_base_node() const;
  270. PackedStringArray get_configuration_warnings() const override;
  271. bool is_state_invalid() const;
  272. String get_invalid_state_reason() const;
  273. real_t get_connection_activity(const StringName &p_path, int p_connection) const;
  274. uint64_t get_last_process_pass() const;
  275. AnimationTree();
  276. ~AnimationTree();
  277. };
  278. #ifndef DISABLE_DEPRECATED
  279. VARIANT_ENUM_CAST(AnimationTree::AnimationProcessCallback);
  280. #endif // DISABLE_DEPRECATED
  281. #endif // ANIMATION_TREE_H