animation_tree.h 14 KB

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