animation_blend_tree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*************************************************************************/
  2. /* animation_blend_tree.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. StringName time;
  37. uint64_t last_version;
  38. bool skip;
  39. protected:
  40. void _validate_property(PropertyInfo &property) const;
  41. static void _bind_methods();
  42. public:
  43. void get_parameter_list(List<PropertyInfo> *r_list) const;
  44. static Vector<String> (*get_editable_animation_list)();
  45. virtual String get_caption() const;
  46. virtual float process(float p_time, bool p_seek);
  47. void set_animation(const StringName &p_name);
  48. StringName get_animation() const;
  49. AnimationNodeAnimation();
  50. };
  51. class AnimationNodeOneShot : public AnimationNode {
  52. GDCLASS(AnimationNodeOneShot, AnimationNode);
  53. public:
  54. enum MixMode {
  55. MIX_MODE_BLEND,
  56. MIX_MODE_ADD
  57. };
  58. private:
  59. float fade_in;
  60. float fade_out;
  61. bool autorestart;
  62. float autorestart_delay;
  63. float autorestart_random_delay;
  64. MixMode mix;
  65. bool sync;
  66. /* bool active;
  67. bool do_start;
  68. float time;
  69. float remaining;*/
  70. StringName active;
  71. StringName prev_active;
  72. StringName time;
  73. StringName remaining;
  74. StringName time_to_restart;
  75. protected:
  76. static void _bind_methods();
  77. public:
  78. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  79. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  80. virtual String get_caption() const;
  81. void set_fadein_time(float p_time);
  82. void set_fadeout_time(float p_time);
  83. float get_fadein_time() const;
  84. float get_fadeout_time() const;
  85. void set_autorestart(bool p_active);
  86. void set_autorestart_delay(float p_time);
  87. void set_autorestart_random_delay(float p_time);
  88. bool has_autorestart() const;
  89. float get_autorestart_delay() const;
  90. float get_autorestart_random_delay() const;
  91. void set_mix_mode(MixMode p_mix);
  92. MixMode get_mix_mode() const;
  93. void set_use_sync(bool p_sync);
  94. bool is_using_sync() const;
  95. virtual bool has_filter() const;
  96. virtual float process(float p_time, bool p_seek);
  97. AnimationNodeOneShot();
  98. };
  99. VARIANT_ENUM_CAST(AnimationNodeOneShot::MixMode)
  100. class AnimationNodeAdd2 : public AnimationNode {
  101. GDCLASS(AnimationNodeAdd2, AnimationNode);
  102. StringName add_amount;
  103. bool sync;
  104. protected:
  105. static void _bind_methods();
  106. public:
  107. void get_parameter_list(List<PropertyInfo> *r_list) const;
  108. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  109. virtual String get_caption() const;
  110. void set_use_sync(bool p_sync);
  111. bool is_using_sync() const;
  112. virtual bool has_filter() const;
  113. virtual float process(float p_time, bool p_seek);
  114. AnimationNodeAdd2();
  115. };
  116. class AnimationNodeAdd3 : public AnimationNode {
  117. GDCLASS(AnimationNodeAdd3, AnimationNode);
  118. StringName add_amount;
  119. bool sync;
  120. protected:
  121. static void _bind_methods();
  122. public:
  123. void get_parameter_list(List<PropertyInfo> *r_list) const;
  124. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  125. virtual String get_caption() const;
  126. void set_use_sync(bool p_sync);
  127. bool is_using_sync() const;
  128. virtual bool has_filter() const;
  129. virtual float process(float p_time, bool p_seek);
  130. AnimationNodeAdd3();
  131. };
  132. class AnimationNodeBlend2 : public AnimationNode {
  133. GDCLASS(AnimationNodeBlend2, AnimationNode);
  134. StringName blend_amount;
  135. bool sync;
  136. protected:
  137. static void _bind_methods();
  138. public:
  139. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  140. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  141. virtual String get_caption() const;
  142. virtual float process(float p_time, bool p_seek);
  143. void set_use_sync(bool p_sync);
  144. bool is_using_sync() const;
  145. virtual bool has_filter() const;
  146. AnimationNodeBlend2();
  147. };
  148. class AnimationNodeBlend3 : public AnimationNode {
  149. GDCLASS(AnimationNodeBlend3, AnimationNode);
  150. StringName blend_amount;
  151. bool sync;
  152. protected:
  153. static void _bind_methods();
  154. public:
  155. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  156. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  157. virtual String get_caption() const;
  158. void set_use_sync(bool p_sync);
  159. bool is_using_sync() const;
  160. float process(float p_time, bool p_seek);
  161. AnimationNodeBlend3();
  162. };
  163. class AnimationNodeTimeScale : public AnimationNode {
  164. GDCLASS(AnimationNodeTimeScale, AnimationNode);
  165. StringName scale;
  166. protected:
  167. static void _bind_methods();
  168. public:
  169. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  170. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  171. virtual String get_caption() const;
  172. float process(float p_time, bool p_seek);
  173. AnimationNodeTimeScale();
  174. };
  175. class AnimationNodeTimeSeek : public AnimationNode {
  176. GDCLASS(AnimationNodeTimeSeek, AnimationNode);
  177. StringName seek_pos;
  178. protected:
  179. static void _bind_methods();
  180. public:
  181. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  182. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  183. virtual String get_caption() const;
  184. float process(float p_time, bool p_seek);
  185. AnimationNodeTimeSeek();
  186. };
  187. class AnimationNodeTransition : public AnimationNode {
  188. GDCLASS(AnimationNodeTransition, AnimationNode);
  189. enum {
  190. MAX_INPUTS = 32
  191. };
  192. struct InputData {
  193. String name;
  194. bool auto_advance;
  195. InputData() { auto_advance = false; }
  196. };
  197. InputData inputs[MAX_INPUTS];
  198. int enabled_inputs;
  199. /*
  200. float prev_xfading;
  201. int prev;
  202. float time;
  203. int current;
  204. int prev_current; */
  205. StringName prev_xfading;
  206. StringName prev;
  207. StringName time;
  208. StringName current;
  209. StringName prev_current;
  210. float xfade;
  211. void _update_inputs();
  212. protected:
  213. static void _bind_methods();
  214. void _validate_property(PropertyInfo &property) const;
  215. public:
  216. virtual void get_parameter_list(List<PropertyInfo> *r_list) const;
  217. virtual Variant get_parameter_default_value(const StringName &p_parameter) const;
  218. virtual String get_caption() const;
  219. void set_enabled_inputs(int p_inputs);
  220. int get_enabled_inputs();
  221. void set_input_as_auto_advance(int p_input, bool p_enable);
  222. bool is_input_set_as_auto_advance(int p_input) const;
  223. void set_input_caption(int p_input, const String &p_name);
  224. String get_input_caption(int p_input) const;
  225. void set_cross_fade_time(float p_fade);
  226. float get_cross_fade_time() const;
  227. float process(float p_time, bool p_seek);
  228. AnimationNodeTransition();
  229. };
  230. class AnimationNodeOutput : public AnimationNode {
  231. GDCLASS(AnimationNodeOutput, AnimationNode);
  232. public:
  233. virtual String get_caption() const;
  234. virtual float process(float p_time, bool p_seek);
  235. AnimationNodeOutput();
  236. };
  237. /////
  238. class AnimationNodeBlendTree : public AnimationRootNode {
  239. GDCLASS(AnimationNodeBlendTree, AnimationRootNode);
  240. struct Node {
  241. Ref<AnimationNode> node;
  242. Vector2 position;
  243. Vector<StringName> connections;
  244. };
  245. Map<StringName, Node> nodes;
  246. Vector2 graph_offset;
  247. void _tree_changed();
  248. void _node_changed(const StringName &p_node);
  249. protected:
  250. static void _bind_methods();
  251. bool _set(const StringName &p_name, const Variant &p_value);
  252. bool _get(const StringName &p_name, Variant &r_ret) const;
  253. void _get_property_list(List<PropertyInfo> *p_list) const;
  254. public:
  255. enum ConnectionError {
  256. CONNECTION_OK,
  257. CONNECTION_ERROR_NO_INPUT,
  258. CONNECTION_ERROR_NO_INPUT_INDEX,
  259. CONNECTION_ERROR_NO_OUTPUT,
  260. CONNECTION_ERROR_SAME_NODE,
  261. CONNECTION_ERROR_CONNECTION_EXISTS,
  262. //no need to check for cycles due to tree topology
  263. };
  264. void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2());
  265. Ref<AnimationNode> get_node(const StringName &p_name) const;
  266. void remove_node(const StringName &p_name);
  267. void rename_node(const StringName &p_name, const StringName &p_new_name);
  268. bool has_node(const StringName &p_name) const;
  269. StringName get_node_name(const Ref<AnimationNode> &p_node) const;
  270. Vector<StringName> get_node_connection_array(const StringName &p_name) const;
  271. void set_node_position(const StringName &p_node, const Vector2 &p_position);
  272. Vector2 get_node_position(const StringName &p_node) const;
  273. virtual void get_child_nodes(List<ChildNode> *r_child_nodes);
  274. void connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node);
  275. void disconnect_node(const StringName &p_node, int p_input_index);
  276. struct NodeConnection {
  277. StringName input_node;
  278. int input_index;
  279. StringName output_node;
  280. };
  281. ConnectionError can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const;
  282. void get_node_connections(List<NodeConnection> *r_connections) const;
  283. virtual String get_caption() const;
  284. virtual float process(float p_time, bool p_seek);
  285. void get_node_list(List<StringName> *r_list);
  286. void set_graph_offset(const Vector2 &p_graph_offset);
  287. Vector2 get_graph_offset() const;
  288. virtual Ref<AnimationNode> get_child_by_name(const StringName &p_name);
  289. AnimationNodeBlendTree();
  290. ~AnimationNodeBlendTree();
  291. };
  292. VARIANT_ENUM_CAST(AnimationNodeBlendTree::ConnectionError)
  293. #endif // ANIMATION_BLEND_TREE_H