animation_blend_tree.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**************************************************************************/
  2. /* animation_blend_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_BLEND_TREE_H
  31. #define ANIMATION_BLEND_TREE_H
  32. #include "scene/animation/animation_tree.h"
  33. class AnimationNodeAnimation : public AnimationRootNode {
  34. GDCLASS(AnimationNodeAnimation, AnimationRootNode);
  35. StringName animation;
  36. bool advance_on_start = false;
  37. bool use_custom_timeline = false;
  38. double timeline_length = 1.0;
  39. Animation::LoopMode loop_mode = Animation::LOOP_NONE;
  40. bool stretch_time_scale = true;
  41. double start_offset = 0.0;
  42. uint64_t last_version = 0;
  43. bool skip = false;
  44. public:
  45. enum PlayMode {
  46. PLAY_MODE_FORWARD,
  47. PLAY_MODE_BACKWARD
  48. };
  49. void get_parameter_list(List<PropertyInfo> *r_list) const override;
  50. virtual NodeTimeInfo get_node_time_info() const override; // Wrapper of get_parameter().
  51. static Vector<String> (*get_editable_animation_list)();
  52. virtual String get_caption() const override;
  53. virtual NodeTimeInfo process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  54. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  55. void set_animation(const StringName &p_name);
  56. StringName get_animation() const;
  57. void set_play_mode(PlayMode p_play_mode);
  58. PlayMode get_play_mode() const;
  59. void set_backward(bool p_backward);
  60. bool is_backward() const;
  61. void set_advance_on_start(bool p_advance_on_start);
  62. bool is_advance_on_start() const;
  63. void set_use_custom_timeline(bool p_use_custom_timeline);
  64. bool is_using_custom_timeline() const;
  65. void set_timeline_length(double p_length);
  66. double get_timeline_length() const;
  67. void set_stretch_time_scale(bool p_strech_time_scale);
  68. bool is_stretching_time_scale() const;
  69. void set_start_offset(double p_offset);
  70. double get_start_offset() const;
  71. void set_loop_mode(Animation::LoopMode p_loop_mode);
  72. Animation::LoopMode get_loop_mode() const;
  73. AnimationNodeAnimation();
  74. protected:
  75. void _validate_property(PropertyInfo &p_property) const;
  76. static void _bind_methods();
  77. private:
  78. PlayMode play_mode = PLAY_MODE_FORWARD;
  79. bool backward = false; // Only used by pingpong animation.
  80. };
  81. VARIANT_ENUM_CAST(AnimationNodeAnimation::PlayMode)
  82. class AnimationNodeSync : public AnimationNode {
  83. GDCLASS(AnimationNodeSync, AnimationNode);
  84. protected:
  85. bool sync = false;
  86. static void _bind_methods();
  87. public:
  88. void set_use_sync(bool p_sync);
  89. bool is_using_sync() const;
  90. AnimationNodeSync();
  91. };
  92. class AnimationNodeOneShot : public AnimationNodeSync {
  93. GDCLASS(AnimationNodeOneShot, AnimationNodeSync);
  94. public:
  95. enum OneShotRequest {
  96. ONE_SHOT_REQUEST_NONE,
  97. ONE_SHOT_REQUEST_FIRE,
  98. ONE_SHOT_REQUEST_ABORT,
  99. ONE_SHOT_REQUEST_FADE_OUT,
  100. };
  101. enum MixMode {
  102. MIX_MODE_BLEND,
  103. MIX_MODE_ADD
  104. };
  105. private:
  106. double fade_in = 0.0;
  107. Ref<Curve> fade_in_curve;
  108. double fade_out = 0.0;
  109. Ref<Curve> fade_out_curve;
  110. bool auto_restart = false;
  111. double auto_restart_delay = 1.0;
  112. double auto_restart_random_delay = 0.0;
  113. MixMode mix = MIX_MODE_BLEND;
  114. bool break_loop_at_end = false;
  115. StringName request = PNAME("request");
  116. StringName active = PNAME("active");
  117. StringName internal_active = PNAME("internal_active");
  118. StringName fade_in_remaining = "fade_in_remaining";
  119. StringName fade_out_remaining = "fade_out_remaining";
  120. StringName time_to_restart = "time_to_restart";
  121. protected:
  122. static void _bind_methods();
  123. public:
  124. virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
  125. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  126. virtual bool is_parameter_read_only(const StringName &p_parameter) const override;
  127. virtual String get_caption() const override;
  128. void set_fade_in_time(double p_time);
  129. double get_fade_in_time() const;
  130. void set_fade_in_curve(const Ref<Curve> &p_curve);
  131. Ref<Curve> get_fade_in_curve() const;
  132. void set_fade_out_time(double p_time);
  133. double get_fade_out_time() const;
  134. void set_fade_out_curve(const Ref<Curve> &p_curve);
  135. Ref<Curve> get_fade_out_curve() const;
  136. void set_auto_restart_enabled(bool p_enabled);
  137. void set_auto_restart_delay(double p_time);
  138. void set_auto_restart_random_delay(double p_time);
  139. bool is_auto_restart_enabled() const;
  140. double get_auto_restart_delay() const;
  141. double get_auto_restart_random_delay() const;
  142. void set_mix_mode(MixMode p_mix);
  143. MixMode get_mix_mode() const;
  144. void set_break_loop_at_end(bool p_enable);
  145. bool is_loop_broken_at_end() const;
  146. virtual bool has_filter() const override;
  147. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  148. AnimationNodeOneShot();
  149. };
  150. VARIANT_ENUM_CAST(AnimationNodeOneShot::OneShotRequest)
  151. VARIANT_ENUM_CAST(AnimationNodeOneShot::MixMode)
  152. class AnimationNodeAdd2 : public AnimationNodeSync {
  153. GDCLASS(AnimationNodeAdd2, AnimationNodeSync);
  154. StringName add_amount = PNAME("add_amount");
  155. public:
  156. void get_parameter_list(List<PropertyInfo> *r_list) const override;
  157. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  158. virtual String get_caption() const override;
  159. virtual bool has_filter() const override;
  160. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  161. AnimationNodeAdd2();
  162. };
  163. class AnimationNodeAdd3 : public AnimationNodeSync {
  164. GDCLASS(AnimationNodeAdd3, AnimationNodeSync);
  165. StringName add_amount = PNAME("add_amount");
  166. public:
  167. void get_parameter_list(List<PropertyInfo> *r_list) const override;
  168. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  169. virtual String get_caption() const override;
  170. virtual bool has_filter() const override;
  171. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  172. AnimationNodeAdd3();
  173. };
  174. class AnimationNodeBlend2 : public AnimationNodeSync {
  175. GDCLASS(AnimationNodeBlend2, AnimationNodeSync);
  176. StringName blend_amount = PNAME("blend_amount");
  177. public:
  178. virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
  179. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  180. virtual String get_caption() const override;
  181. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  182. virtual bool has_filter() const override;
  183. AnimationNodeBlend2();
  184. };
  185. class AnimationNodeBlend3 : public AnimationNodeSync {
  186. GDCLASS(AnimationNodeBlend3, AnimationNodeSync);
  187. StringName blend_amount = PNAME("blend_amount");
  188. public:
  189. virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
  190. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  191. virtual String get_caption() const override;
  192. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  193. AnimationNodeBlend3();
  194. };
  195. class AnimationNodeSub2 : public AnimationNodeSync {
  196. GDCLASS(AnimationNodeSub2, AnimationNodeSync);
  197. StringName sub_amount = PNAME("sub_amount");
  198. public:
  199. void get_parameter_list(List<PropertyInfo> *r_list) const override;
  200. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  201. virtual String get_caption() const override;
  202. virtual bool has_filter() const override;
  203. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  204. AnimationNodeSub2();
  205. };
  206. class AnimationNodeTimeScale : public AnimationNode {
  207. GDCLASS(AnimationNodeTimeScale, AnimationNode);
  208. StringName scale = PNAME("scale");
  209. public:
  210. virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
  211. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  212. virtual String get_caption() const override;
  213. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  214. AnimationNodeTimeScale();
  215. };
  216. class AnimationNodeTimeSeek : public AnimationNode {
  217. GDCLASS(AnimationNodeTimeSeek, AnimationNode);
  218. StringName seek_pos_request = PNAME("seek_request");
  219. bool explicit_elapse = true;
  220. protected:
  221. static void _bind_methods();
  222. public:
  223. virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
  224. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  225. virtual String get_caption() const override;
  226. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  227. void set_explicit_elapse(bool p_enable);
  228. bool is_explicit_elapse() const;
  229. AnimationNodeTimeSeek();
  230. };
  231. class AnimationNodeTransition : public AnimationNodeSync {
  232. GDCLASS(AnimationNodeTransition, AnimationNodeSync);
  233. struct InputData {
  234. bool auto_advance = false;
  235. bool break_loop_at_end = false;
  236. bool reset = true;
  237. };
  238. Vector<InputData> input_data;
  239. StringName prev_xfading = "prev_xfading";
  240. StringName prev_index = "prev_index";
  241. StringName current_index = PNAME("current_index");
  242. StringName current_state = PNAME("current_state");
  243. StringName transition_request = PNAME("transition_request");
  244. StringName prev_frame_current = "pf_current";
  245. StringName prev_frame_current_idx = "pf_current_idx";
  246. double xfade_time = 0.0;
  247. Ref<Curve> xfade_curve;
  248. bool allow_transition_to_self = false;
  249. bool pending_update = false;
  250. protected:
  251. bool _get(const StringName &p_path, Variant &r_ret) const;
  252. bool _set(const StringName &p_path, const Variant &p_value);
  253. static void _bind_methods();
  254. void _get_property_list(List<PropertyInfo> *p_list) const;
  255. public:
  256. virtual void get_parameter_list(List<PropertyInfo> *r_list) const override;
  257. virtual Variant get_parameter_default_value(const StringName &p_parameter) const override;
  258. virtual bool is_parameter_read_only(const StringName &p_parameter) const override;
  259. virtual String get_caption() const override;
  260. void set_input_count(int p_inputs);
  261. virtual bool add_input(const String &p_name) override;
  262. virtual void remove_input(int p_index) override;
  263. virtual bool set_input_name(int p_input, const String &p_name) override;
  264. void set_input_as_auto_advance(int p_input, bool p_enable);
  265. bool is_input_set_as_auto_advance(int p_input) const;
  266. void set_input_break_loop_at_end(int p_input, bool p_enable);
  267. bool is_input_loop_broken_at_end(int p_input) const;
  268. void set_input_reset(int p_input, bool p_enable);
  269. bool is_input_reset(int p_input) const;
  270. void set_xfade_time(double p_fade);
  271. double get_xfade_time() const;
  272. void set_xfade_curve(const Ref<Curve> &p_curve);
  273. Ref<Curve> get_xfade_curve() const;
  274. void set_allow_transition_to_self(bool p_enable);
  275. bool is_allow_transition_to_self() const;
  276. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  277. AnimationNodeTransition();
  278. };
  279. class AnimationNodeOutput : public AnimationNode {
  280. GDCLASS(AnimationNodeOutput, AnimationNode);
  281. public:
  282. virtual String get_caption() const override;
  283. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  284. AnimationNodeOutput();
  285. };
  286. /////
  287. class AnimationNodeBlendTree : public AnimationRootNode {
  288. GDCLASS(AnimationNodeBlendTree, AnimationRootNode);
  289. struct Node {
  290. Ref<AnimationNode> node;
  291. Vector2 position;
  292. Vector<StringName> connections;
  293. };
  294. RBMap<StringName, Node, StringName::AlphCompare> nodes;
  295. Vector2 graph_offset;
  296. void _node_changed(const StringName &p_node);
  297. void _initialize_node_tree();
  298. protected:
  299. static void _bind_methods();
  300. bool _set(const StringName &p_name, const Variant &p_value);
  301. bool _get(const StringName &p_name, Variant &r_ret) const;
  302. void _get_property_list(List<PropertyInfo> *p_list) const;
  303. virtual void _tree_changed() override;
  304. virtual void _animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) override;
  305. virtual void _animation_node_removed(const ObjectID &p_oid, const StringName &p_node) override;
  306. virtual void reset_state() override;
  307. public:
  308. enum ConnectionError {
  309. CONNECTION_OK,
  310. CONNECTION_ERROR_NO_INPUT,
  311. CONNECTION_ERROR_NO_INPUT_INDEX,
  312. CONNECTION_ERROR_NO_OUTPUT,
  313. CONNECTION_ERROR_SAME_NODE,
  314. CONNECTION_ERROR_CONNECTION_EXISTS,
  315. //no need to check for cycles due to tree topology
  316. };
  317. void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2());
  318. Ref<AnimationNode> get_node(const StringName &p_name) const;
  319. void remove_node(const StringName &p_name);
  320. void rename_node(const StringName &p_name, const StringName &p_new_name);
  321. bool has_node(const StringName &p_name) const;
  322. StringName get_node_name(const Ref<AnimationNode> &p_node) const;
  323. Vector<StringName> get_node_connection_array(const StringName &p_name) const;
  324. void set_node_position(const StringName &p_node, const Vector2 &p_position);
  325. Vector2 get_node_position(const StringName &p_node) const;
  326. virtual void get_child_nodes(List<ChildNode> *r_child_nodes) override;
  327. void connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node);
  328. void disconnect_node(const StringName &p_node, int p_input_index);
  329. struct NodeConnection {
  330. StringName input_node;
  331. int input_index = 0;
  332. StringName output_node;
  333. };
  334. ConnectionError can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const;
  335. void get_node_connections(List<NodeConnection> *r_connections) const;
  336. virtual String get_caption() const override;
  337. virtual NodeTimeInfo _process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only = false) override;
  338. void get_node_list(List<StringName> *r_list);
  339. void set_graph_offset(const Vector2 &p_graph_offset);
  340. Vector2 get_graph_offset() const;
  341. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name) const override;
  342. #ifdef TOOLS_ENABLED
  343. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  344. #endif
  345. AnimationNodeBlendTree();
  346. ~AnimationNodeBlendTree();
  347. };
  348. VARIANT_ENUM_CAST(AnimationNodeBlendTree::ConnectionError)
  349. #endif // ANIMATION_BLEND_TREE_H