animation_tree_player.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*************************************************************************/
  2. /* animation_tree_player.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef ANIMATION_TREE_PLAYER_H
  30. #define ANIMATION_TREE_PLAYER_H
  31. #include "scene/resources/animation.h"
  32. #include "scene/3d/spatial.h"
  33. #include "scene/3d/skeleton.h"
  34. #include "scene/main/misc.h"
  35. class AnimationTreePlayer : public Node {
  36. OBJ_TYPE( AnimationTreePlayer, Node );
  37. OBJ_CATEGORY("Animation Nodes");
  38. public:
  39. enum NodeType {
  40. NODE_OUTPUT,
  41. NODE_ANIMATION,
  42. NODE_ONESHOT,
  43. NODE_MIX,
  44. NODE_BLEND2,
  45. NODE_BLEND3,
  46. NODE_BLEND4,
  47. NODE_TIMESCALE,
  48. NODE_TIMESEEK,
  49. NODE_TRANSITION,
  50. NODE_MAX,
  51. };
  52. enum ConnectError {
  53. CONNECT_OK,
  54. CONNECT_INCOMPLETE,
  55. CONNECT_CYCLE
  56. };
  57. private:
  58. enum {
  59. DISCONNECTED=-1,
  60. };
  61. struct TrackKey {
  62. uint32_t id;
  63. StringName property;
  64. int bone_idx;
  65. inline bool operator<(const TrackKey& p_right) const {
  66. if (id==p_right.id) {
  67. if (bone_idx==p_right.bone_idx) {
  68. return property<p_right.property;
  69. } else
  70. return bone_idx<p_right.bone_idx;
  71. } else
  72. return id<p_right.id;
  73. }
  74. };
  75. struct Track {
  76. uint32_t id;
  77. Node *node;
  78. Spatial* spatial;
  79. Skeleton *skeleton;
  80. int bone_idx;
  81. StringName property;
  82. Vector3 loc;
  83. Quat rot;
  84. Vector3 scale;
  85. };
  86. typedef Map<TrackKey,Track> TrackMap;
  87. TrackMap track_map;
  88. struct Input {
  89. StringName node;
  90. //Input() { node=-1; }
  91. };
  92. struct NodeBase {
  93. bool cycletest;
  94. NodeType type;
  95. Point2 pos;
  96. Vector<Input> inputs;
  97. NodeBase() { cycletest = false; };
  98. virtual ~NodeBase() { cycletest=false; }
  99. };
  100. struct NodeOut : public NodeBase {
  101. NodeOut() { type=NODE_OUTPUT; inputs.resize(1); }
  102. };
  103. struct AnimationNode : public NodeBase {
  104. Ref<Animation> animation;
  105. struct TrackRef {
  106. int local_track;
  107. Track *track;
  108. float weight;
  109. };
  110. uint64_t last_version;
  111. List<TrackRef> tref;
  112. AnimationNode *next;
  113. float time;
  114. float step;
  115. String from;
  116. bool skip;
  117. AnimationNode() { type=NODE_ANIMATION; next=NULL; last_version=0; skip=false; }
  118. };
  119. struct OneShotNode : public NodeBase {
  120. bool active;
  121. bool start;
  122. float fade_in;
  123. float fade_out;
  124. bool autorestart;
  125. float autorestart_delay;
  126. float autorestart_random_delay;
  127. bool mix;
  128. float time;
  129. float remaining;
  130. float autorestart_remaining;
  131. HashMap<NodePath,bool> filter;
  132. OneShotNode() { type=NODE_ONESHOT; fade_in=0; fade_out=0; inputs.resize(2); autorestart=false; autorestart_delay=1; autorestart_remaining=0; mix=false; active=false; start=false;}
  133. };
  134. struct MixNode : public NodeBase {
  135. float amount;
  136. MixNode() { type=NODE_MIX; inputs.resize(2); }
  137. };
  138. struct Blend2Node : public NodeBase {
  139. float value;
  140. HashMap<NodePath,bool> filter;
  141. Blend2Node() { type=NODE_BLEND2; value=0; inputs.resize(2); }
  142. };
  143. struct Blend3Node : public NodeBase {
  144. float value;
  145. Blend3Node() { type=NODE_BLEND3; value=0; inputs.resize(3); }
  146. };
  147. struct Blend4Node : public NodeBase {
  148. Point2 value;
  149. Blend4Node() { type=NODE_BLEND4; inputs.resize(4); }
  150. };
  151. struct TimeScaleNode : public NodeBase {
  152. float scale;
  153. TimeScaleNode() { type=NODE_TIMESCALE; scale=1; inputs.resize(1); }
  154. };
  155. struct TimeSeekNode : public NodeBase {
  156. float seek_pos;
  157. TimeSeekNode() { type=NODE_TIMESEEK; inputs.resize(1); seek_pos=-1; }
  158. };
  159. struct TransitionNode : public NodeBase {
  160. struct InputData {
  161. bool auto_advance;
  162. InputData() { auto_advance=false; }
  163. };
  164. Vector<InputData> input_data;
  165. float prev_time;
  166. float prev_xfading;
  167. int prev;
  168. bool switched;
  169. float time;
  170. int current;
  171. float xfade;
  172. TransitionNode() { type=NODE_TRANSITION; xfade=0; inputs.resize(1); input_data.resize(1); current=0; prev=-1; prev_time=0; prev_xfading=0; switched=false; }
  173. };
  174. void _update_sources();
  175. StringName out_name;
  176. NodeOut *out;
  177. NodePath base_path;
  178. NodePath master;
  179. ConnectError last_error;
  180. AnimationNode *active_list;
  181. bool active;
  182. bool dirty_caches;
  183. Map<StringName,NodeBase*> node_map;
  184. // return time left to finish animation
  185. float _process_node(const StringName& p_node,AnimationNode **r_prev_anim, float p_weight,float p_step, bool p_seek=false,const HashMap<NodePath,bool> *p_filter=NULL, float p_reverse_weight=0);
  186. void _process_animation();
  187. bool reset_request;
  188. ConnectError _cycle_test(const StringName &p_at_node);
  189. Track* _find_track(const NodePath& p_path);
  190. void _recompute_caches();
  191. void _recompute_caches(const StringName& p_node);
  192. DVector<String> _get_node_list();
  193. protected:
  194. bool _set(const StringName& p_name, const Variant& p_value);
  195. bool _get(const StringName& p_name,Variant &r_ret) const;
  196. void _get_property_list( List<PropertyInfo> *p_list) const;
  197. void _notification(int p_what);
  198. static void _bind_methods();
  199. public:
  200. void add_node(NodeType p_type, const StringName& p_node); // nodes must be >0 node 0 is built-in (exit)
  201. bool node_exists(const StringName& p_name) const;
  202. Error node_rename(const StringName& p_node,const StringName& p_new_name);
  203. int node_get_input_count(const StringName& p_node) const;
  204. StringName node_get_input_source(const StringName& p_node,int p_input) const;
  205. /* ANIMATION NODE */
  206. void animation_node_set_animation(const StringName& p_node,const Ref<Animation>& p_animation);
  207. Ref<Animation> animation_node_get_animation(const StringName& p_node) const;
  208. void animation_node_set_master_animation(const StringName& p_node,const String& p_master_animation);
  209. String animation_node_get_master_animation(const StringName& p_node) const;
  210. /* ONE SHOT NODE */
  211. void oneshot_node_set_fadein_time(const StringName& p_node,float p_time);
  212. void oneshot_node_set_fadeout_time(const StringName& p_node,float p_time);
  213. float oneshot_node_get_fadein_time(const StringName& p_node) const;
  214. float oneshot_node_get_fadeout_time(const StringName& p_node) const;
  215. void oneshot_node_set_autorestart(const StringName& p_node,bool p_active);
  216. void oneshot_node_set_autorestart_delay(const StringName& p_node,float p_time);
  217. void oneshot_node_set_autorestart_random_delay(const StringName& p_node,float p_time);
  218. bool oneshot_node_has_autorestart(const StringName& p_node) const;
  219. float oneshot_node_get_autorestart_delay(const StringName& p_node) const;
  220. float oneshot_node_get_autorestart_random_delay(const StringName& p_node) const;
  221. void oneshot_node_set_mix_mode(const StringName& p_node,bool p_enabled);
  222. bool oneshot_node_get_mix_mode(const StringName& p_node) const;
  223. void oneshot_node_start(const StringName& p_node);
  224. void oneshot_node_stop(const StringName& p_node);
  225. bool oneshot_node_is_active(const StringName& p_node) const;
  226. void oneshot_node_set_filter_path(const StringName& p_node,const NodePath& p_filter,bool p_enable);
  227. void oneshot_node_set_get_filtered_paths(const StringName& p_node,List<NodePath> *r_paths) const;
  228. bool oneshot_node_is_path_filtered(const StringName& p_node,const NodePath& p_path) const;
  229. /* MIX/BLEND NODES */
  230. void mix_node_set_amount(const StringName& p_node,float p_amount);
  231. float mix_node_get_amount(const StringName& p_node) const;
  232. void blend2_node_set_amount(const StringName& p_node,float p_amount);
  233. float blend2_node_get_amount(const StringName& p_node) const;
  234. void blend2_node_set_filter_path(const StringName& p_node,const NodePath& p_filter,bool p_enable);
  235. void blend2_node_set_get_filtered_paths(const StringName& p_node,List<NodePath> *r_paths) const;
  236. bool blend2_node_is_path_filtered(const StringName& p_node,const NodePath& p_path) const;
  237. void blend3_node_set_amount(const StringName& p_node,float p_amount);
  238. float blend3_node_get_amount(const StringName& p_node) const;
  239. void blend4_node_set_amount(const StringName& p_node,const Point2& p_amount);
  240. Point2 blend4_node_get_amount(const StringName& p_node) const;
  241. /* TIMESCALE/TIMESEEK NODES */
  242. void timescale_node_set_scale(const StringName& p_node,float p_scale);
  243. float timescale_node_get_scale(const StringName& p_node) const;
  244. void timeseek_node_seek(const StringName& p_node,float p_pos);
  245. /* TRANSITION NODE */
  246. void transition_node_set_input_count(const StringName& p_node, int p_inputs); // used for transition node
  247. int transition_node_get_input_count(const StringName& p_node) const;
  248. void transition_node_delete_input(const StringName& p_node, int p_input); // used for transition node
  249. void transition_node_set_input_auto_advance(const StringName& p_node, int p_input,bool p_auto_advance); // used for transition node
  250. bool transition_node_has_input_auto_advance(const StringName& p_node, int p_input) const;
  251. void transition_node_set_xfade_time(const StringName& p_node, float p_time); // used for transition node
  252. float transition_node_get_xfade_time(const StringName& p_node) const;
  253. void transition_node_set_current(const StringName& p_node, int p_current);
  254. int transition_node_get_current(const StringName& p_node) const;
  255. void node_set_pos(const StringName& p_node, const Vector2& p_pos); //for display
  256. /* GETS */
  257. Point2 node_get_pos(const StringName& p_node) const; //for display
  258. NodeType node_get_type(const StringName& p_node) const;
  259. void get_node_list(List<StringName> *p_node_list) const;
  260. void remove_node(const StringName& p_node);
  261. Error connect(const StringName& p_src_node,const StringName& p_dst_node, int p_dst_input);
  262. bool is_connected(const StringName& p_src_node,const StringName& p_dst_node, int p_input) const;
  263. void disconnect(const StringName& p_src_node, int p_input);
  264. void set_base_path(const NodePath& p_path);
  265. NodePath get_base_path() const;
  266. void set_master_player(const NodePath& p_path);
  267. NodePath get_master_player() const;
  268. struct Connection {
  269. StringName src_node;
  270. StringName dst_node;
  271. int dst_input;
  272. };
  273. void get_connection_list( List<Connection> *p_connections) const;
  274. /* playback */
  275. void set_active(bool p_active);
  276. bool is_active() const;
  277. void reset();
  278. void recompute_caches();
  279. ConnectError get_last_error() const;
  280. AnimationTreePlayer();
  281. ~AnimationTreePlayer();
  282. };
  283. VARIANT_ENUM_CAST( AnimationTreePlayer::NodeType );
  284. #endif // ANIMATION_TREE_PLAYER_H