animation_tree_player.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*************************************************************************/
  2. /* animation_tree_player.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_TREE_PLAYER_H
  31. #define ANIMATION_TREE_PLAYER_H
  32. #include "animation_player.h"
  33. #include "scene/3d/skeleton.h"
  34. #include "scene/3d/spatial.h"
  35. #include "scene/resources/animation.h"
  36. class AnimationTreePlayer : public Node {
  37. GDCLASS(AnimationTreePlayer, Node);
  38. OBJ_CATEGORY("Animation Nodes");
  39. public:
  40. enum AnimationProcessMode {
  41. ANIMATION_PROCESS_PHYSICS,
  42. ANIMATION_PROCESS_IDLE,
  43. };
  44. enum NodeType {
  45. NODE_OUTPUT,
  46. NODE_ANIMATION,
  47. NODE_ONESHOT,
  48. NODE_MIX,
  49. NODE_BLEND2,
  50. NODE_BLEND3,
  51. NODE_BLEND4,
  52. NODE_TIMESCALE,
  53. NODE_TIMESEEK,
  54. NODE_TRANSITION,
  55. NODE_MAX,
  56. };
  57. enum ConnectError {
  58. CONNECT_OK,
  59. CONNECT_INCOMPLETE,
  60. CONNECT_CYCLE
  61. };
  62. private:
  63. enum {
  64. DISCONNECTED = -1,
  65. };
  66. struct TrackKey {
  67. uint32_t id;
  68. StringName subpath_concatenated;
  69. int bone_idx;
  70. inline bool operator<(const TrackKey &p_right) const {
  71. if (id == p_right.id) {
  72. if (bone_idx == p_right.bone_idx) {
  73. return subpath_concatenated < p_right.subpath_concatenated;
  74. } else
  75. return bone_idx < p_right.bone_idx;
  76. } else
  77. return id < p_right.id;
  78. }
  79. };
  80. struct Track {
  81. uint32_t id;
  82. Object *object;
  83. Spatial *spatial;
  84. Skeleton *skeleton;
  85. int bone_idx;
  86. Vector<StringName> subpath;
  87. Vector3 loc;
  88. Quat rot;
  89. Vector3 scale;
  90. Variant value;
  91. bool skip;
  92. };
  93. typedef Map<TrackKey, Track> TrackMap;
  94. TrackMap track_map;
  95. struct Input {
  96. StringName node;
  97. //Input() { node=-1; }
  98. };
  99. struct NodeBase {
  100. bool cycletest;
  101. NodeType type;
  102. Point2 pos;
  103. Vector<Input> inputs;
  104. NodeBase() { cycletest = false; };
  105. virtual ~NodeBase() { cycletest = false; }
  106. };
  107. struct NodeOut : public NodeBase {
  108. NodeOut() {
  109. type = NODE_OUTPUT;
  110. inputs.resize(1);
  111. }
  112. };
  113. struct AnimationNode : public NodeBase {
  114. Ref<Animation> animation;
  115. struct TrackRef {
  116. int local_track;
  117. Track *track;
  118. float weight;
  119. };
  120. uint64_t last_version;
  121. List<TrackRef> tref;
  122. AnimationNode *next;
  123. float time;
  124. float step;
  125. String from;
  126. bool skip;
  127. HashMap<NodePath, bool> filter;
  128. AnimationNode() {
  129. type = NODE_ANIMATION;
  130. next = NULL;
  131. last_version = 0;
  132. skip = false;
  133. }
  134. };
  135. struct OneShotNode : public NodeBase {
  136. bool active;
  137. bool start;
  138. float fade_in;
  139. float fade_out;
  140. bool autorestart;
  141. float autorestart_delay;
  142. float autorestart_random_delay;
  143. bool mix;
  144. float time;
  145. float remaining;
  146. float autorestart_remaining;
  147. HashMap<NodePath, bool> filter;
  148. OneShotNode() {
  149. type = NODE_ONESHOT;
  150. fade_in = 0;
  151. fade_out = 0;
  152. inputs.resize(2);
  153. autorestart = false;
  154. autorestart_delay = 1;
  155. autorestart_remaining = 0;
  156. mix = false;
  157. active = false;
  158. start = false;
  159. }
  160. };
  161. struct MixNode : public NodeBase {
  162. float amount;
  163. MixNode() {
  164. type = NODE_MIX;
  165. inputs.resize(2);
  166. }
  167. };
  168. struct Blend2Node : public NodeBase {
  169. float value;
  170. HashMap<NodePath, bool> filter;
  171. Blend2Node() {
  172. type = NODE_BLEND2;
  173. value = 0;
  174. inputs.resize(2);
  175. }
  176. };
  177. struct Blend3Node : public NodeBase {
  178. float value;
  179. Blend3Node() {
  180. type = NODE_BLEND3;
  181. value = 0;
  182. inputs.resize(3);
  183. }
  184. };
  185. struct Blend4Node : public NodeBase {
  186. Point2 value;
  187. Blend4Node() {
  188. type = NODE_BLEND4;
  189. inputs.resize(4);
  190. }
  191. };
  192. struct TimeScaleNode : public NodeBase {
  193. float scale;
  194. TimeScaleNode() {
  195. type = NODE_TIMESCALE;
  196. scale = 1;
  197. inputs.resize(1);
  198. }
  199. };
  200. struct TimeSeekNode : public NodeBase {
  201. float seek_pos;
  202. TimeSeekNode() {
  203. type = NODE_TIMESEEK;
  204. inputs.resize(1);
  205. seek_pos = -1;
  206. }
  207. };
  208. struct TransitionNode : public NodeBase {
  209. struct InputData {
  210. bool auto_advance;
  211. InputData() { auto_advance = false; }
  212. };
  213. Vector<InputData> input_data;
  214. float prev_time;
  215. float prev_xfading;
  216. int prev;
  217. bool switched;
  218. float time;
  219. int current;
  220. float xfade;
  221. TransitionNode() {
  222. type = NODE_TRANSITION;
  223. xfade = 0;
  224. inputs.resize(1);
  225. input_data.resize(1);
  226. current = 0;
  227. prev = -1;
  228. prev_time = 0;
  229. prev_xfading = 0;
  230. switched = false;
  231. }
  232. void set_current(int p_current);
  233. };
  234. void _update_sources();
  235. StringName out_name;
  236. NodeOut *out;
  237. NodePath base_path;
  238. NodePath master;
  239. ConnectError last_error;
  240. AnimationNode *active_list;
  241. AnimationProcessMode animation_process_mode;
  242. bool processing;
  243. bool active;
  244. bool dirty_caches;
  245. Map<StringName, NodeBase *> node_map;
  246. // return time left to finish animation
  247. float _process_node(const StringName &p_node, AnimationNode **r_prev_anim, float p_time, bool p_seek = false, float p_fallback_weight = 1.0, HashMap<NodePath, float> *p_weights = NULL);
  248. void _process_animation(float p_delta);
  249. bool reset_request;
  250. ConnectError _cycle_test(const StringName &p_at_node);
  251. void _clear_cycle_test();
  252. Track *_find_track(const NodePath &p_path);
  253. void _recompute_caches();
  254. void _recompute_caches(const StringName &p_node);
  255. PoolVector<String> _get_node_list();
  256. void _compute_weights(float *p_fallback_weight, HashMap<NodePath, float> *p_weights, float p_coeff, const HashMap<NodePath, bool> *p_filter = NULL, float p_filtered_coeff = 0);
  257. protected:
  258. bool _set(const StringName &p_name, const Variant &p_value);
  259. bool _get(const StringName &p_name, Variant &r_ret) const;
  260. void _get_property_list(List<PropertyInfo> *p_list) const;
  261. void _notification(int p_what);
  262. static void _bind_methods();
  263. public:
  264. void add_node(NodeType p_type, const StringName &p_node); // nodes must be >0 node 0 is built-in (exit)
  265. bool node_exists(const StringName &p_name) const;
  266. Error node_rename(const StringName &p_node, const StringName &p_new_name);
  267. int node_get_input_count(const StringName &p_node) const;
  268. StringName node_get_input_source(const StringName &p_node, int p_input) const;
  269. String get_configuration_warning() const;
  270. /* ANIMATION NODE */
  271. void animation_node_set_animation(const StringName &p_node, const Ref<Animation> &p_animation);
  272. Ref<Animation> animation_node_get_animation(const StringName &p_node) const;
  273. void animation_node_set_master_animation(const StringName &p_node, const String &p_master_animation);
  274. String animation_node_get_master_animation(const StringName &p_node) const;
  275. float animation_node_get_position(const StringName &p_node) const;
  276. void animation_node_set_filter_path(const StringName &p_node, const NodePath &p_track_path, bool p_filter);
  277. void animation_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const;
  278. bool animation_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const;
  279. /* ONE SHOT NODE */
  280. void oneshot_node_set_fadein_time(const StringName &p_node, float p_time);
  281. void oneshot_node_set_fadeout_time(const StringName &p_node, float p_time);
  282. float oneshot_node_get_fadein_time(const StringName &p_node) const;
  283. float oneshot_node_get_fadeout_time(const StringName &p_node) const;
  284. void oneshot_node_set_autorestart(const StringName &p_node, bool p_active);
  285. void oneshot_node_set_autorestart_delay(const StringName &p_node, float p_time);
  286. void oneshot_node_set_autorestart_random_delay(const StringName &p_node, float p_time);
  287. bool oneshot_node_has_autorestart(const StringName &p_node) const;
  288. float oneshot_node_get_autorestart_delay(const StringName &p_node) const;
  289. float oneshot_node_get_autorestart_random_delay(const StringName &p_node) const;
  290. void oneshot_node_set_mix_mode(const StringName &p_node, bool p_mix);
  291. bool oneshot_node_get_mix_mode(const StringName &p_node) const;
  292. void oneshot_node_start(const StringName &p_node);
  293. void oneshot_node_stop(const StringName &p_node);
  294. bool oneshot_node_is_active(const StringName &p_node) const;
  295. void oneshot_node_set_filter_path(const StringName &p_node, const NodePath &p_filter, bool p_enable);
  296. void oneshot_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const;
  297. bool oneshot_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const;
  298. /* MIX/BLEND NODES */
  299. void mix_node_set_amount(const StringName &p_node, float p_amount);
  300. float mix_node_get_amount(const StringName &p_node) const;
  301. void blend2_node_set_amount(const StringName &p_node, float p_amount);
  302. float blend2_node_get_amount(const StringName &p_node) const;
  303. void blend2_node_set_filter_path(const StringName &p_node, const NodePath &p_filter, bool p_enable);
  304. void blend2_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const;
  305. bool blend2_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const;
  306. void blend3_node_set_amount(const StringName &p_node, float p_amount);
  307. float blend3_node_get_amount(const StringName &p_node) const;
  308. void blend4_node_set_amount(const StringName &p_node, const Point2 &p_amount);
  309. Point2 blend4_node_get_amount(const StringName &p_node) const;
  310. /* TIMESCALE/TIMESEEK NODES */
  311. void timescale_node_set_scale(const StringName &p_node, float p_scale);
  312. float timescale_node_get_scale(const StringName &p_node) const;
  313. void timeseek_node_seek(const StringName &p_node, float p_pos);
  314. /* TRANSITION NODE */
  315. void transition_node_set_input_count(const StringName &p_node, int p_inputs); // used for transition node
  316. int transition_node_get_input_count(const StringName &p_node) const;
  317. void transition_node_delete_input(const StringName &p_node, int p_input); // used for transition node
  318. void transition_node_set_input_auto_advance(const StringName &p_node, int p_input, bool p_auto_advance); // used for transition node
  319. bool transition_node_has_input_auto_advance(const StringName &p_node, int p_input) const;
  320. void transition_node_set_xfade_time(const StringName &p_node, float p_time); // used for transition node
  321. float transition_node_get_xfade_time(const StringName &p_node) const;
  322. void transition_node_set_current(const StringName &p_node, int p_current);
  323. int transition_node_get_current(const StringName &p_node) const;
  324. void node_set_position(const StringName &p_node, const Vector2 &p_pos); //for display
  325. /* GETS */
  326. Point2 node_get_position(const StringName &p_node) const; //for display
  327. NodeType node_get_type(const StringName &p_node) const;
  328. void get_node_list(List<StringName> *p_node_list) const;
  329. void remove_node(const StringName &p_node);
  330. Error connect_nodes(const StringName &p_src_node, const StringName &p_dst_node, int p_dst_input);
  331. bool are_nodes_connected(const StringName &p_src_node, const StringName &p_dst_node, int p_dst_input) const;
  332. void disconnect_nodes(const StringName &p_node, int p_input);
  333. void set_base_path(const NodePath &p_path);
  334. NodePath get_base_path() const;
  335. void set_master_player(const NodePath &p_path);
  336. NodePath get_master_player() const;
  337. struct Connection {
  338. StringName src_node;
  339. StringName dst_node;
  340. int dst_input;
  341. };
  342. void get_connection_list(List<Connection> *p_connections) const;
  343. /* playback */
  344. void set_active(bool p_active);
  345. bool is_active() const;
  346. void reset();
  347. void recompute_caches();
  348. ConnectError get_last_error() const;
  349. void set_animation_process_mode(AnimationProcessMode p_mode);
  350. AnimationProcessMode get_animation_process_mode() const;
  351. void _set_process(bool p_process, bool p_force = false);
  352. void advance(float p_time);
  353. AnimationTreePlayer();
  354. ~AnimationTreePlayer();
  355. };
  356. VARIANT_ENUM_CAST(AnimationTreePlayer::NodeType);
  357. VARIANT_ENUM_CAST(AnimationTreePlayer::AnimationProcessMode);
  358. #endif // ANIMATION_TREE_PLAYER_H