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