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