animation_tree_player.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. /*************************************************************************/
  2. /* animation_tree_player.cpp */
  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. #include "animation_tree_player.h"
  30. #include "animation_player.h"
  31. #include "scene/scene_string_names.h"
  32. void AnimationTreePlayer::set_animation_process_mode(AnimationProcessMode p_mode) {
  33. if (animation_process_mode == p_mode)
  34. return;
  35. bool pr = processing;
  36. if (pr)
  37. _set_process(false);
  38. animation_process_mode = p_mode;
  39. if (pr)
  40. _set_process(true);
  41. }
  42. AnimationTreePlayer::AnimationProcessMode AnimationTreePlayer::get_animation_process_mode() const{
  43. return animation_process_mode;
  44. }
  45. void AnimationTreePlayer::_set_process(bool p_process, bool p_force)
  46. {
  47. if (processing == p_process && !p_force)
  48. return;
  49. switch (animation_process_mode) {
  50. case ANIMATION_PROCESS_FIXED: set_fixed_process(p_process && active); break;
  51. case ANIMATION_PROCESS_IDLE: set_process(p_process && active); break;
  52. }
  53. processing = p_process;
  54. }
  55. bool AnimationTreePlayer::_set(const StringName& p_name, const Variant& p_value) {
  56. if (String(p_name)=="base_path") {
  57. set_base_path(p_value);
  58. return true;
  59. }
  60. if (String(p_name)=="master_player") {
  61. set_master_player(p_value);
  62. return true;
  63. }
  64. if(String(p_name) == SceneStringNames::get_singleton()->playback_active) {
  65. set_active(p_value);
  66. return true;
  67. }
  68. if (String(p_name)!="data")
  69. return false;
  70. Dictionary data=p_value;
  71. Array nodes=data.get_valid("nodes");
  72. for(int i=0;i<nodes.size();i++) {
  73. Dictionary node = nodes[i];
  74. StringName id = node.get_valid("id");
  75. Point2 pos = node.get_valid("pos");
  76. NodeType nt=NODE_MAX;
  77. String type = node.get_valid("type");
  78. if (type=="output")
  79. nt=NODE_OUTPUT;
  80. else if (type=="animation")
  81. nt=NODE_ANIMATION;
  82. else if (type=="oneshot")
  83. nt=NODE_ONESHOT;
  84. else if (type=="mix")
  85. nt=NODE_MIX;
  86. else if (type=="blend2")
  87. nt=NODE_BLEND2;
  88. else if (type=="blend3")
  89. nt=NODE_BLEND3;
  90. else if (type=="blend4")
  91. nt=NODE_BLEND4;
  92. else if (type=="timescale")
  93. nt=NODE_TIMESCALE;
  94. else if (type=="timeseek")
  95. nt=NODE_TIMESEEK;
  96. else if (type=="transition")
  97. nt=NODE_TRANSITION;
  98. ERR_FAIL_COND_V(nt==NODE_MAX,false);
  99. if (nt!=NODE_OUTPUT)
  100. add_node(nt,id);
  101. node_set_pos(id,pos);
  102. switch(nt) {
  103. case NODE_OUTPUT: {
  104. } break;
  105. case NODE_ANIMATION: {
  106. if (node.has("from"))
  107. animation_node_set_master_animation(id,node.get_valid("from"));
  108. else
  109. animation_node_set_animation(id,node.get_valid("animation"));
  110. } break;
  111. case NODE_ONESHOT: {
  112. oneshot_node_set_fadein_time(id,node.get_valid("fade_in"));
  113. oneshot_node_set_fadeout_time(id,node.get_valid("fade_out"));
  114. oneshot_node_set_mix_mode(id,node.get_valid("mix"));
  115. oneshot_node_set_autorestart(id,node.get_valid("autorestart"));
  116. oneshot_node_set_autorestart_delay(id,node.get_valid("autorestart_delay"));
  117. oneshot_node_set_autorestart_random_delay(id,node.get_valid("autorestart_random_delay"));
  118. Array filters= node.get_valid("filter");
  119. for(int i=0;i<filters.size();i++) {
  120. oneshot_node_set_filter_path(id,filters[i],true);
  121. }
  122. } break;
  123. case NODE_MIX: {
  124. mix_node_set_amount(id,node.get_valid("mix"));
  125. } break;
  126. case NODE_BLEND2: {
  127. blend2_node_set_amount(id,node.get_valid("blend"));
  128. Array filters= node.get_valid("filter");
  129. for(int i=0;i<filters.size();i++) {
  130. blend2_node_set_filter_path(id,filters[i],true);
  131. }
  132. } break;
  133. case NODE_BLEND3: {
  134. blend3_node_set_amount(id,node.get_valid("blend"));
  135. } break;
  136. case NODE_BLEND4: {
  137. blend4_node_set_amount(id,node.get_valid("blend"));
  138. } break;
  139. case NODE_TIMESCALE: {
  140. timescale_node_set_scale(id,node.get_valid("scale"));
  141. } break;
  142. case NODE_TIMESEEK: {
  143. } break;
  144. case NODE_TRANSITION: {
  145. transition_node_set_xfade_time(id,node.get_valid("xfade"));
  146. Array transitions = node.get_valid("transitions");
  147. transition_node_set_input_count(id,transitions.size());
  148. for(int x=0;x<transitions.size();x++) {
  149. Dictionary d =transitions[x];
  150. bool aa = d.get_valid("auto_advance");
  151. transition_node_set_input_auto_advance(id,x,aa);
  152. }
  153. } break;
  154. default: {};
  155. }
  156. }
  157. Array connections = data.get_valid("connections");
  158. ERR_FAIL_COND_V(connections.size()%3,false);
  159. int cc=connections.size()/3;
  160. for(int i=0;i<cc;i++) {
  161. StringName src = connections[i*3+0];
  162. StringName dst = connections[i*3+1];
  163. int dst_in = connections[i*3+2];
  164. connect(src,dst,dst_in);
  165. }
  166. set_active(data.get_valid("active"));
  167. set_master_player(data.get_valid("master"));
  168. return true;
  169. }
  170. bool AnimationTreePlayer::_get(const StringName& p_name,Variant &r_ret) const {
  171. if (String(p_name)=="base_path") {
  172. r_ret=base_path;
  173. return true;
  174. }
  175. if (String(p_name)=="master_player") {
  176. r_ret=master;
  177. return true;
  178. }
  179. if (String(p_name) == "playback/active") {
  180. r_ret=is_active();
  181. return true;
  182. }
  183. if (String(p_name)!="data")
  184. return false;
  185. Dictionary data;
  186. Array nodes;
  187. for(Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  188. NodeBase *n = node_map[E->key()];
  189. Dictionary node;
  190. node["id"]=E->key();
  191. node["pos"]=n->pos;
  192. switch(n->type) {
  193. case NODE_OUTPUT: node["type"]= "output"; break;
  194. case NODE_ANIMATION: node["type"]= "animation"; break;
  195. case NODE_ONESHOT: node["type"]= "oneshot"; break;
  196. case NODE_MIX: node["type"]= "mix"; break;
  197. case NODE_BLEND2: node["type"]= "blend2"; break;
  198. case NODE_BLEND3: node["type"]= "blend3"; break;
  199. case NODE_BLEND4: node["type"]= "blend4"; break;
  200. case NODE_TIMESCALE: node["type"]= "timescale"; break;
  201. case NODE_TIMESEEK: node["type"]= "timeseek"; break;
  202. case NODE_TRANSITION: node["type"]= "transition"; break;
  203. default: node["type"]= ""; break;
  204. }
  205. switch(n->type) {
  206. case NODE_OUTPUT: {
  207. } break;
  208. case NODE_ANIMATION: {
  209. AnimationNode *an = static_cast<AnimationNode*>(n);
  210. if (master!=NodePath() && an->from!="") {
  211. node["from"]=an->from;
  212. } else {
  213. node["animation"]=an->animation;
  214. }
  215. } break;
  216. case NODE_ONESHOT: {
  217. OneShotNode *osn = static_cast<OneShotNode*>(n);
  218. node["fade_in"]=osn->fade_in;
  219. node["fade_out"]=osn->fade_out;
  220. node["mix"]=osn->mix;
  221. node["autorestart"]=osn->autorestart;
  222. node["autorestart_delay"]=osn->autorestart_delay;
  223. node["autorestart_random_delay"]=osn->autorestart_random_delay;
  224. Array k;
  225. List<NodePath> keys;
  226. osn->filter.get_key_list(&keys);
  227. k.resize(keys.size());
  228. int i=0;
  229. for(List<NodePath>::Element *E=keys.front();E;E=E->next()) {
  230. k[i++]=E->get();
  231. }
  232. node["filter"]=k;
  233. } break;
  234. case NODE_MIX: {
  235. MixNode *mn = static_cast<MixNode*>(n);
  236. node["mix"]=mn->amount;
  237. } break;
  238. case NODE_BLEND2: {
  239. Blend2Node *bn = static_cast<Blend2Node*>(n);
  240. node["blend"]=bn->value;
  241. Array k;
  242. List<NodePath> keys;
  243. bn->filter.get_key_list(&keys);
  244. k.resize(keys.size());
  245. int i=0;
  246. for(List<NodePath>::Element *E=keys.front();E;E=E->next()) {
  247. k[i++]=E->get();
  248. }
  249. node["filter"]=k;
  250. } break;
  251. case NODE_BLEND3: {
  252. Blend3Node *bn = static_cast<Blend3Node*>(n);
  253. node["blend"]=bn->value;
  254. } break;
  255. case NODE_BLEND4: {
  256. Blend4Node *bn = static_cast<Blend4Node*>(n);
  257. node["blend"]=bn->value;
  258. } break;
  259. case NODE_TIMESCALE: {
  260. TimeScaleNode *tsn = static_cast<TimeScaleNode*>(n);
  261. node["scale"]=tsn->scale;
  262. } break;
  263. case NODE_TIMESEEK: {
  264. } break;
  265. case NODE_TRANSITION: {
  266. TransitionNode *tn = static_cast<TransitionNode*>(n);
  267. node["xfade"]=tn->xfade;
  268. Array transitions;
  269. for(int i=0;i<tn->input_data.size();i++) {
  270. Dictionary d;
  271. d["auto_advance"]=tn->input_data[i].auto_advance;
  272. transitions.push_back(d);
  273. }
  274. node["transitions"]=transitions;
  275. } break;
  276. default: {};
  277. }
  278. nodes.push_back(node);
  279. }
  280. data["nodes"]=nodes;
  281. //connectiosn
  282. List<Connection> connections;
  283. get_connection_list(&connections);
  284. Array connections_arr;
  285. connections_arr.resize(connections.size()*3);
  286. int idx=0;
  287. for (List<Connection>::Element *E=connections.front();E;E=E->next()) {
  288. connections_arr.set(idx+0,E->get().src_node);
  289. connections_arr.set(idx+1,E->get().dst_node);
  290. connections_arr.set(idx+2,E->get().dst_input);
  291. idx+=3;
  292. }
  293. data["connections"]=connections_arr;
  294. data["active"]=active;
  295. data["master"]=master;
  296. r_ret=data;
  297. return true;
  298. }
  299. void AnimationTreePlayer::_get_property_list( List<PropertyInfo> *p_list) const {
  300. p_list->push_back( PropertyInfo(Variant::NODE_PATH,"base_path" ) );
  301. p_list->push_back( PropertyInfo(Variant::NODE_PATH,"master_player" ) );
  302. p_list->push_back( PropertyInfo(Variant::DICTIONARY,"data",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_STORAGE|PROPERTY_USAGE_NETWORK) );
  303. }
  304. void AnimationTreePlayer::advance(float p_time) {
  305. _process_animation(p_time);
  306. }
  307. void AnimationTreePlayer::_notification(int p_what) {
  308. switch(p_what) {
  309. case NOTIFICATION_ENTER_TREE: {
  310. if (!processing) {
  311. //make sure that a previous process state was not saved
  312. //only process if "processing" is set
  313. set_fixed_process(false);
  314. set_process(false);
  315. }
  316. } break;
  317. case NOTIFICATION_READY: {
  318. dirty_caches=true;
  319. if (master!=NodePath()) {
  320. _update_sources();
  321. }
  322. } break;
  323. case NOTIFICATION_PROCESS: {
  324. if (animation_process_mode==ANIMATION_PROCESS_FIXED)
  325. break;
  326. if (processing)
  327. _process_animation( get_process_delta_time() );
  328. } break;
  329. case NOTIFICATION_FIXED_PROCESS: {
  330. if (animation_process_mode==ANIMATION_PROCESS_IDLE)
  331. break;
  332. if (processing)
  333. _process_animation(get_fixed_process_delta_time());
  334. } break;
  335. }
  336. }
  337. float AnimationTreePlayer::_process_node(const StringName& p_node,AnimationNode **r_prev_anim,float p_weight, float p_time, bool p_seek,const HashMap<NodePath,bool> *p_filter, float p_reverse_weight) {
  338. ERR_FAIL_COND_V(!node_map.has(p_node), 0);
  339. NodeBase *nb=node_map[p_node];
  340. //transform to seconds...
  341. switch(nb->type) {
  342. case NODE_OUTPUT: {
  343. NodeOut *on = static_cast<NodeOut*>(nb);
  344. return _process_node(on->inputs[0].node,r_prev_anim,p_weight,p_time,p_seek);
  345. } break;
  346. case NODE_ANIMATION: {
  347. AnimationNode *an = static_cast<AnimationNode*>(nb);
  348. float rem = 0;
  349. if (!an->animation.is_null()) {
  350. // float pos = an->time;
  351. // float delta = p_time;
  352. // const Animation *a = an->animation.operator->();
  353. if (p_seek) {
  354. an->time=p_time;
  355. an->step=0;
  356. } else {
  357. an->time+=p_time;
  358. an->step=p_time;
  359. }
  360. float anim_size = an->animation->get_length();
  361. if (an->animation->has_loop()) {
  362. if (anim_size)
  363. an->time=Math::fposmod(an->time,anim_size);
  364. } else if (an->time > anim_size) {
  365. an->time=anim_size;
  366. }
  367. an->skip=true;
  368. for (List<AnimationNode::TrackRef>::Element *E=an->tref.front();E;E=E->next()) {
  369. if (p_filter && p_filter->has(an->animation->track_get_path(E->get().local_track))) {
  370. if (p_reverse_weight<0)
  371. E->get().weight=0;
  372. else
  373. E->get().weight=p_reverse_weight;
  374. } else {
  375. E->get().weight=p_weight;
  376. }
  377. if (E->get().weight>CMP_EPSILON)
  378. an->skip=false;
  379. }
  380. rem = anim_size - an->time;
  381. }
  382. if (!(*r_prev_anim))
  383. active_list=an;
  384. else
  385. (*r_prev_anim)->next=an;
  386. an->next=NULL;
  387. *r_prev_anim=an;
  388. return rem;
  389. } break;
  390. case NODE_ONESHOT: {
  391. OneShotNode *osn = static_cast<OneShotNode*>(nb);
  392. if (!osn->active) {
  393. //make it as if this node doesn't exist, pass input 0 by.
  394. return _process_node(osn->inputs[0].node,r_prev_anim,p_weight,p_time,p_seek,p_filter,p_reverse_weight);
  395. }
  396. float os_seek = p_seek;
  397. if (p_seek)
  398. osn->time=p_time;
  399. if (osn->start) {
  400. osn->time=0;
  401. os_seek = true;
  402. }
  403. float blend;
  404. if (osn->time<osn->fade_in) {
  405. if (osn->fade_in>0)
  406. blend = osn->time/osn->fade_in;
  407. else
  408. blend=0; //wtf
  409. } else if (!osn->start && osn->remaining<osn->fade_out) {
  410. if (osn->fade_out)
  411. blend=(osn->remaining/osn->fade_out);
  412. else
  413. blend=1.0;
  414. } else
  415. blend=1.0;
  416. float main_rem;
  417. float os_rem;
  418. if (!osn->filter.empty()) {
  419. main_rem = _process_node(osn->inputs[0].node,r_prev_anim,(osn->mix?p_weight:p_weight*(1.0-blend)),p_time,p_seek,&osn->filter,p_weight);
  420. os_rem = _process_node(osn->inputs[1].node,r_prev_anim,p_weight*blend,p_time,os_seek,&osn->filter,-1);
  421. } else {
  422. main_rem = _process_node(osn->inputs[0].node,r_prev_anim,(osn->mix?p_weight:p_weight*(1.0-blend)),p_time,p_seek);
  423. os_rem = _process_node(osn->inputs[1].node,r_prev_anim,p_weight*blend,p_time,os_seek);
  424. }
  425. if (osn->start) {
  426. osn->remaining=os_rem;
  427. osn->start=false;
  428. }
  429. if (!p_seek) {
  430. osn->time+=p_time;
  431. osn->remaining=os_rem;
  432. if (osn->remaining<=0)
  433. osn->active=false;
  434. }
  435. return MAX(main_rem,osn->remaining);
  436. } break;
  437. case NODE_MIX: {
  438. MixNode *mn = static_cast<MixNode*>(nb);
  439. float rem = _process_node(mn->inputs[0].node,r_prev_anim,p_weight,p_time,p_seek,p_filter,p_reverse_weight);
  440. _process_node(mn->inputs[1].node,r_prev_anim,p_weight*mn->amount,p_time,p_seek,p_filter,p_reverse_weight);
  441. return rem;
  442. } break;
  443. case NODE_BLEND2: {
  444. Blend2Node *bn = static_cast<Blend2Node*>(nb);
  445. float rem;
  446. if (!bn->filter.empty()) {
  447. rem = _process_node(bn->inputs[0].node,r_prev_anim,p_weight*(1.0-bn->value),p_time,p_seek,&bn->filter,p_weight);
  448. _process_node(bn->inputs[1].node,r_prev_anim,p_weight*bn->value,p_time,p_seek,&bn->filter,-1);
  449. } else {
  450. rem = _process_node(bn->inputs[0].node,r_prev_anim,p_weight*(1.0-bn->value),p_time,p_seek,p_filter,p_reverse_weight*(1.0-bn->value));
  451. _process_node(bn->inputs[1].node,r_prev_anim,p_weight*bn->value,p_time,p_seek,p_filter,p_reverse_weight*bn->value);
  452. }
  453. return rem;
  454. } break;
  455. case NODE_BLEND3: {
  456. Blend3Node *bn = static_cast<Blend3Node*>(nb);
  457. float rem;
  458. float blend, lower_blend, upper_blend;
  459. if (bn->value < 0) {
  460. lower_blend = -bn->value;
  461. blend = 1.0 - lower_blend;
  462. upper_blend = 0;
  463. } else {
  464. lower_blend = 0;
  465. blend = 1.0 - bn->value;
  466. upper_blend = bn->value;
  467. }
  468. rem = _process_node(bn->inputs[1].node,r_prev_anim,p_weight*blend,p_time,p_seek,p_filter,p_reverse_weight*blend);
  469. _process_node(bn->inputs[2].node,r_prev_anim,p_weight*upper_blend,p_time,p_seek,p_filter,p_reverse_weight*upper_blend);
  470. _process_node(bn->inputs[0].node,r_prev_anim,p_weight*lower_blend,p_time,p_seek,p_filter,p_reverse_weight*lower_blend);
  471. return rem;
  472. } break;
  473. case NODE_BLEND4: {
  474. Blend4Node *bn = static_cast<Blend4Node*>(nb);
  475. float rem = _process_node(bn->inputs[0].node,r_prev_anim,p_weight*(1.0-bn->value.x),p_time,p_seek,p_filter,p_reverse_weight*(1.0-bn->value.x));
  476. _process_node(bn->inputs[1].node,r_prev_anim,p_weight*bn->value.x,p_time,p_seek,p_filter,p_reverse_weight*bn->value.x);
  477. float rem2 = _process_node(bn->inputs[2].node,r_prev_anim,p_weight*(1.0-bn->value.y),p_time,p_seek,p_filter,p_reverse_weight*(1.0-bn->value.y));
  478. _process_node(bn->inputs[3].node,r_prev_anim,p_weight*bn->value.y,p_time,p_seek,p_filter,p_reverse_weight*bn->value.y);
  479. return MAX(rem,rem2);
  480. } break;
  481. case NODE_TIMESCALE: {
  482. TimeScaleNode *tsn = static_cast<TimeScaleNode*>(nb);
  483. float rem;
  484. if (p_seek)
  485. rem = _process_node(tsn->inputs[0].node,r_prev_anim,p_weight,p_time,true,p_filter,p_reverse_weight);
  486. else
  487. rem = _process_node(tsn->inputs[0].node,r_prev_anim,p_weight,p_time*tsn->scale,false,p_filter,p_reverse_weight);
  488. if (tsn->scale == 0)
  489. return INFINITY;
  490. else
  491. return rem / tsn->scale;
  492. } break;
  493. case NODE_TIMESEEK: {
  494. TimeSeekNode *tsn = static_cast<TimeSeekNode*>(nb);
  495. if (tsn->seek_pos>=0 && !p_seek) {
  496. p_time = tsn->seek_pos;
  497. p_seek = true;
  498. }
  499. tsn->seek_pos=-1;
  500. return _process_node(tsn->inputs[0].node,r_prev_anim,p_weight,p_time,p_seek);
  501. } break;
  502. case NODE_TRANSITION: {
  503. TransitionNode *tn = static_cast<TransitionNode*>(nb);
  504. if (tn->prev<0) { // process current animation, check for transition
  505. float rem = _process_node(tn->inputs[tn->current].node,r_prev_anim,p_weight,p_time,p_seek,p_filter,p_reverse_weight);
  506. if (p_seek)
  507. tn->time=p_time;
  508. else
  509. tn->time+=p_time;
  510. if (tn->input_data[tn->current].auto_advance && rem <= tn->xfade) {
  511. tn->set_current((tn->current+1) % tn->inputs.size());
  512. }
  513. return rem;
  514. } else { // cross-fading from tn->prev to tn->current
  515. float blend = tn->xfade? (tn->prev_xfading/tn->xfade) : 1;
  516. float rem;
  517. if (!p_seek && tn->switched) { //just switched, seek to start of current
  518. rem = _process_node(tn->inputs[tn->current].node,r_prev_anim,p_weight*(1.0-blend),0,true,p_filter,p_reverse_weight*(1.0-blend));
  519. } else {
  520. rem = _process_node(tn->inputs[tn->current].node,r_prev_anim,p_weight*(1.0-blend),p_time,p_seek,p_filter,p_reverse_weight*(1.0-blend));
  521. }
  522. tn->switched=false;
  523. if (p_seek) { // don't seek prev animation
  524. _process_node(tn->inputs[tn->prev].node,r_prev_anim,p_weight*blend,0,false,p_filter,p_reverse_weight*blend);
  525. tn->time=p_time;
  526. } else {
  527. _process_node(tn->inputs[tn->prev].node,r_prev_anim,p_weight*blend,p_time,false,p_filter,p_reverse_weight*blend);
  528. tn->time+=p_time;
  529. tn->prev_xfading-=p_time;
  530. if (tn->prev_xfading<0) {
  531. tn->prev=-1;
  532. }
  533. }
  534. return rem;
  535. }
  536. } break;
  537. default: {}
  538. }
  539. return 0;
  540. }
  541. void AnimationTreePlayer::_process_animation(float p_delta) {
  542. if (last_error!=CONNECT_OK)
  543. return;
  544. if (dirty_caches)
  545. _recompute_caches();
  546. active_list=NULL;
  547. AnimationNode *prev=NULL;
  548. if (reset_request) {
  549. _process_node(out_name,&prev, 1.0, 0, true);
  550. reset_request=false;
  551. } else
  552. _process_node(out_name,&prev, 1.0, p_delta);
  553. if (dirty_caches) {
  554. //some animation changed.. ignore this pass
  555. return;
  556. }
  557. //update the tracks..
  558. /* STEP 1 CLEAR TRACKS */
  559. for(TrackMap::Element *E=track_map.front();E;E=E->next()) {
  560. Track &t = E->get();
  561. t.loc.zero();
  562. t.rot=Quat();
  563. t.scale.x=0;
  564. t.scale.y=0;
  565. t.scale.z=0;
  566. Variant value = t.node->get(t.property);
  567. value.zero();
  568. t.node->set(t.property, value);
  569. }
  570. /* STEP 2 PROCESS ANIMATIONS */
  571. AnimationNode *anim_list=active_list;
  572. Quat empty_rot;
  573. while(anim_list) {
  574. if (!anim_list->animation.is_null() && !anim_list->skip) {
  575. //check if animation is meaningful
  576. Animation *a = anim_list->animation.operator->();
  577. for(List<AnimationNode::TrackRef>::Element *E=anim_list->tref.front();E;E=E->next()) {
  578. AnimationNode::TrackRef &tr = E->get();
  579. if (tr.track==NULL || tr.local_track<0 || tr.weight < CMP_EPSILON)
  580. continue;
  581. float blend=tr.weight;
  582. switch(a->track_get_type(tr.local_track)) {
  583. case Animation::TYPE_TRANSFORM: { ///< Transform a node or a bone.
  584. Vector3 loc;
  585. Quat rot;
  586. Vector3 scale;
  587. a->transform_track_interpolate(tr.local_track,anim_list->time,&loc,&rot,&scale);
  588. tr.track->loc+=loc*blend;
  589. scale.x-=1.0;
  590. scale.y-=1.0;
  591. scale.z-=1.0;
  592. tr.track->scale+=scale*blend;
  593. tr.track->rot = tr.track->rot * empty_rot.slerp(rot,blend);
  594. } break;
  595. case Animation::TYPE_VALUE: { ///< Set a value in a property, can be interpolated.
  596. if (a->value_track_is_continuous(tr.local_track)) {
  597. Variant blended, value = a->value_track_interpolate(tr.local_track,anim_list->time);
  598. Variant::blend(tr.track->node->get(tr.track->property),value,blend,blended);
  599. tr.track->node->set(tr.track->property,blended);
  600. } else {
  601. List<int> indices;
  602. a->value_track_get_key_indices(tr.local_track,anim_list->time,anim_list->step,&indices);
  603. for(List<int>::Element *E=indices.front();E;E=E->next()) {
  604. Variant value = a->track_get_key_value(tr.local_track,E->get());
  605. tr.track->node->set(tr.track->property,value);
  606. }
  607. }
  608. } break;
  609. case Animation::TYPE_METHOD: { ///< Call any method on a specific node.
  610. List<int> indices;
  611. a->method_track_get_key_indices(tr.local_track,anim_list->time,anim_list->step,&indices);
  612. for(List<int>::Element *E=indices.front();E;E=E->next()) {
  613. StringName method = a->method_track_get_name(tr.local_track,E->get());
  614. Vector<Variant> args=a->method_track_get_params(tr.local_track,E->get());
  615. args.resize(VARIANT_ARG_MAX);
  616. tr.track->node->call(method,args[0],args[1],args[2],args[3],args[4]);
  617. }
  618. } break;
  619. }
  620. }
  621. }
  622. anim_list=anim_list->next;
  623. }
  624. /* STEP 3 APPLY TRACKS */
  625. for(TrackMap::Element *E=track_map.front();E;E=E->next()) {
  626. Track &t = E->get();
  627. if (!t.node)
  628. continue;
  629. if(t.property) // value track; was applied in step 2
  630. continue;
  631. Transform xform;
  632. xform.basis=t.rot;
  633. xform.origin=t.loc;
  634. t.scale.x+=1.0;
  635. t.scale.y+=1.0;
  636. t.scale.z+=1.0;
  637. xform.basis.scale(t.scale);
  638. if (t.bone_idx>=0) {
  639. if (t.skeleton)
  640. t.skeleton->set_bone_pose(t.bone_idx,xform);
  641. } else if (t.spatial) {
  642. t.spatial->set_transform(xform);
  643. }
  644. }
  645. }
  646. void AnimationTreePlayer::add_node(NodeType p_type, const StringName& p_node) {
  647. ERR_FAIL_COND( p_type == NODE_OUTPUT );
  648. ERR_FAIL_COND( node_map.has(p_node));
  649. NodeBase *n=NULL;
  650. switch(p_type) {
  651. case NODE_ANIMATION: {
  652. n = memnew( AnimationNode );
  653. } break;
  654. case NODE_ONESHOT: {
  655. n = memnew( OneShotNode );
  656. } break;
  657. case NODE_MIX: {
  658. n = memnew( MixNode );
  659. } break;
  660. case NODE_BLEND2: {
  661. n = memnew( Blend2Node );
  662. } break;
  663. case NODE_BLEND3: {
  664. n = memnew( Blend3Node );
  665. } break;
  666. case NODE_BLEND4: {
  667. n = memnew( Blend4Node );
  668. } break;
  669. case NODE_TIMESCALE: {
  670. n = memnew( TimeScaleNode );
  671. } break;
  672. case NODE_TIMESEEK: {
  673. n = memnew( TimeSeekNode );
  674. } break;
  675. case NODE_TRANSITION: {
  676. n = memnew( TransitionNode );
  677. } break;
  678. default: {}
  679. }
  680. //n->name+=" "+itos(p_node);
  681. node_map[p_node]=n;
  682. }
  683. StringName AnimationTreePlayer::node_get_input_source(const StringName& p_node,int p_input) const {
  684. ERR_FAIL_COND_V(!node_map.has(p_node),StringName());
  685. ERR_FAIL_INDEX_V( p_input,node_map[p_node]->inputs.size(),StringName() );
  686. return node_map[p_node]->inputs[p_input].node;
  687. }
  688. int AnimationTreePlayer::node_get_input_count(const StringName& p_node) const {
  689. ERR_FAIL_COND_V(!node_map.has(p_node),-1);
  690. return node_map[p_node]->inputs.size();
  691. }
  692. #define GET_NODE( m_type, m_cast )\
  693. ERR_FAIL_COND(!node_map.has(p_node));\
  694. ERR_EXPLAIN("Invalid parameter for node type.");\
  695. ERR_FAIL_COND(node_map[p_node]->type!=m_type);\
  696. m_cast *n = static_cast<m_cast*>( node_map[p_node] );\
  697. void AnimationTreePlayer::animation_node_set_animation(const StringName& p_node,const Ref<Animation>& p_animation) {
  698. GET_NODE( NODE_ANIMATION, AnimationNode );
  699. n->animation=p_animation;
  700. dirty_caches=true;
  701. }
  702. void AnimationTreePlayer::animation_node_set_master_animation(const StringName& p_node,const String& p_master_animation) {
  703. GET_NODE( NODE_ANIMATION, AnimationNode );
  704. n->from=p_master_animation;
  705. dirty_caches=true;
  706. if (master!=NodePath())
  707. _update_sources();
  708. }
  709. void AnimationTreePlayer::oneshot_node_set_fadein_time(const StringName& p_node,float p_time) {
  710. GET_NODE( NODE_ONESHOT, OneShotNode );
  711. n->fade_in=p_time;
  712. }
  713. void AnimationTreePlayer::oneshot_node_set_fadeout_time(const StringName& p_node,float p_time) {
  714. GET_NODE( NODE_ONESHOT, OneShotNode );
  715. n->fade_out=p_time;
  716. }
  717. void AnimationTreePlayer::oneshot_node_set_mix_mode(const StringName& p_node,bool p_mix) {
  718. GET_NODE( NODE_ONESHOT, OneShotNode );
  719. n->mix=p_mix;
  720. }
  721. void AnimationTreePlayer::oneshot_node_set_autorestart(const StringName& p_node,bool p_active) {
  722. GET_NODE( NODE_ONESHOT, OneShotNode );
  723. n->autorestart=p_active;
  724. }
  725. void AnimationTreePlayer::oneshot_node_set_autorestart_delay(const StringName& p_node,float p_time) {
  726. GET_NODE( NODE_ONESHOT, OneShotNode );
  727. n->autorestart_delay=p_time;
  728. }
  729. void AnimationTreePlayer::oneshot_node_set_autorestart_random_delay(const StringName& p_node,float p_time) {
  730. GET_NODE( NODE_ONESHOT, OneShotNode );
  731. n->autorestart_random_delay=p_time;
  732. }
  733. void AnimationTreePlayer::oneshot_node_start(const StringName& p_node) {
  734. GET_NODE( NODE_ONESHOT, OneShotNode );
  735. n->active=true;
  736. n->start=true;
  737. }
  738. void AnimationTreePlayer::oneshot_node_stop(const StringName& p_node) {
  739. GET_NODE( NODE_ONESHOT, OneShotNode );
  740. n->active=false;
  741. }
  742. void AnimationTreePlayer::oneshot_node_set_filter_path(const StringName& p_node,const NodePath& p_filter,bool p_enable) {
  743. GET_NODE( NODE_ONESHOT, OneShotNode );
  744. if (p_enable)
  745. n->filter[p_filter]=true;
  746. else
  747. n->filter.erase(p_filter);
  748. }
  749. void AnimationTreePlayer::oneshot_node_set_get_filtered_paths(const StringName& p_node,List<NodePath> *r_paths) const{
  750. GET_NODE( NODE_ONESHOT, OneShotNode );
  751. n->filter.get_key_list(r_paths);
  752. }
  753. void AnimationTreePlayer::mix_node_set_amount(const StringName& p_node,float p_amount) {
  754. GET_NODE( NODE_MIX, MixNode );
  755. n->amount=p_amount;
  756. }
  757. void AnimationTreePlayer::blend2_node_set_amount(const StringName& p_node,float p_amount) {
  758. GET_NODE( NODE_BLEND2, Blend2Node );
  759. n->value=p_amount;
  760. }
  761. void AnimationTreePlayer::blend2_node_set_filter_path(const StringName& p_node,const NodePath& p_filter,bool p_enable) {
  762. GET_NODE( NODE_BLEND2, Blend2Node );
  763. if (p_enable)
  764. n->filter[p_filter]=true;
  765. else
  766. n->filter.erase(p_filter);
  767. }
  768. void AnimationTreePlayer::blend2_node_set_get_filtered_paths(const StringName& p_node,List<NodePath> *r_paths) const{
  769. GET_NODE( NODE_BLEND2, Blend2Node );
  770. n->filter.get_key_list(r_paths);
  771. }
  772. void AnimationTreePlayer::blend3_node_set_amount(const StringName& p_node,float p_amount) {
  773. GET_NODE( NODE_BLEND3, Blend3Node );
  774. n->value=p_amount;
  775. }
  776. void AnimationTreePlayer::blend4_node_set_amount(const StringName& p_node,const Vector2& p_amount) {
  777. GET_NODE( NODE_BLEND4, Blend4Node );
  778. n->value=p_amount;
  779. }
  780. void AnimationTreePlayer::timescale_node_set_scale(const StringName& p_node,float p_scale) {
  781. GET_NODE( NODE_TIMESCALE, TimeScaleNode );
  782. n->scale=p_scale;
  783. }
  784. void AnimationTreePlayer::timeseek_node_seek(const StringName& p_node,float p_pos) {
  785. GET_NODE( NODE_TIMESEEK, TimeSeekNode );
  786. n->seek_pos=p_pos;
  787. }
  788. void AnimationTreePlayer::transition_node_set_input_count(const StringName& p_node, int p_inputs) {
  789. GET_NODE( NODE_TRANSITION, TransitionNode );
  790. ERR_FAIL_COND(p_inputs<1);
  791. n->inputs.resize(p_inputs);
  792. n->input_data.resize(p_inputs);
  793. last_error=_cycle_test(out_name);
  794. }
  795. void AnimationTreePlayer::transition_node_set_input_auto_advance(const StringName& p_node, int p_input,bool p_auto_advance) {
  796. GET_NODE( NODE_TRANSITION, TransitionNode );
  797. ERR_FAIL_INDEX(p_input,n->input_data.size());
  798. n->input_data[p_input].auto_advance=p_auto_advance;
  799. }
  800. void AnimationTreePlayer::transition_node_set_xfade_time(const StringName& p_node, float p_time) {
  801. GET_NODE( NODE_TRANSITION, TransitionNode );
  802. n->xfade=p_time;
  803. }
  804. void AnimationTreePlayer::TransitionNode::set_current(int p_current) {
  805. ERR_FAIL_INDEX(p_current,inputs.size());
  806. if (current==p_current)
  807. return;
  808. prev=current;
  809. prev_xfading=xfade;
  810. prev_time=time;
  811. time=0;
  812. current=p_current;
  813. switched=true;
  814. }
  815. void AnimationTreePlayer::transition_node_set_current(const StringName& p_node, int p_current) {
  816. GET_NODE( NODE_TRANSITION, TransitionNode );
  817. n->set_current(p_current);
  818. }
  819. void AnimationTreePlayer::node_set_pos(const StringName& p_node, const Vector2& p_pos) {
  820. ERR_FAIL_COND(!node_map.has(p_node));
  821. node_map[p_node]->pos=p_pos;
  822. }
  823. AnimationTreePlayer::NodeType AnimationTreePlayer::node_get_type(const StringName& p_node) const {
  824. ERR_FAIL_COND_V(!node_map.has(p_node),NODE_OUTPUT);
  825. return node_map[p_node]->type;
  826. }
  827. Point2 AnimationTreePlayer::node_get_pos(const StringName& p_node) const {
  828. ERR_FAIL_COND_V(!node_map.has(p_node),Point2());
  829. return node_map[p_node]->pos;
  830. }
  831. #define GET_NODE_V( m_type, m_cast, m_ret )\
  832. ERR_FAIL_COND_V(!node_map.has(p_node),m_ret);\
  833. ERR_EXPLAIN("Invalid parameter for node type.");\
  834. ERR_FAIL_COND_V(node_map[p_node]->type!=m_type,m_ret);\
  835. m_cast *n = static_cast<m_cast*>( node_map[p_node] );\
  836. Ref<Animation> AnimationTreePlayer::animation_node_get_animation(const StringName& p_node) const {
  837. GET_NODE_V(NODE_ANIMATION, AnimationNode, Ref<Animation>());
  838. return n->animation;
  839. }
  840. String AnimationTreePlayer::animation_node_get_master_animation(const StringName& p_node) const {
  841. GET_NODE_V(NODE_ANIMATION, AnimationNode, String());
  842. return n->from;
  843. }
  844. float AnimationTreePlayer::oneshot_node_get_fadein_time(const StringName& p_node) const {
  845. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  846. return n->fade_in;
  847. }
  848. float AnimationTreePlayer::oneshot_node_get_fadeout_time(const StringName& p_node) const {
  849. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  850. return n->fade_out;
  851. }
  852. bool AnimationTreePlayer::oneshot_node_get_mix_mode(const StringName& p_node) const {
  853. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  854. return n->mix;
  855. }
  856. bool AnimationTreePlayer::oneshot_node_has_autorestart(const StringName& p_node) const {
  857. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  858. return n->autorestart;
  859. }
  860. float AnimationTreePlayer::oneshot_node_get_autorestart_delay(const StringName& p_node) const {
  861. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  862. return n->autorestart_delay;
  863. }
  864. float AnimationTreePlayer::oneshot_node_get_autorestart_random_delay(const StringName& p_node) const {
  865. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  866. return n->autorestart_random_delay;
  867. }
  868. bool AnimationTreePlayer::oneshot_node_is_active(const StringName& p_node) const {
  869. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  870. return n->active;
  871. }
  872. bool AnimationTreePlayer::oneshot_node_is_path_filtered(const StringName& p_node,const NodePath& p_path) const {
  873. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0 );
  874. return n->filter.has(p_path);
  875. }
  876. float AnimationTreePlayer::mix_node_get_amount(const StringName& p_node) const {
  877. GET_NODE_V(NODE_MIX, MixNode, 0 );
  878. return n->amount;
  879. }
  880. float AnimationTreePlayer::blend2_node_get_amount(const StringName& p_node) const {
  881. GET_NODE_V(NODE_BLEND2, Blend2Node, 0 );
  882. return n->value;
  883. }
  884. bool AnimationTreePlayer::blend2_node_is_path_filtered(const StringName& p_node,const NodePath& p_path) const {
  885. GET_NODE_V(NODE_BLEND2, Blend2Node, 0 );
  886. return n->filter.has(p_path);
  887. }
  888. float AnimationTreePlayer::blend3_node_get_amount(const StringName& p_node) const {
  889. GET_NODE_V(NODE_BLEND3, Blend3Node, 0 );
  890. return n->value;
  891. }
  892. Vector2 AnimationTreePlayer::blend4_node_get_amount(const StringName& p_node) const {
  893. GET_NODE_V(NODE_BLEND4, Blend4Node, Vector2() );
  894. return n->value;
  895. }
  896. float AnimationTreePlayer::timescale_node_get_scale(const StringName& p_node) const {
  897. GET_NODE_V(NODE_TIMESCALE, TimeScaleNode, 0 );
  898. return n->scale;
  899. }
  900. void AnimationTreePlayer::transition_node_delete_input(const StringName& p_node, int p_input) {
  901. GET_NODE(NODE_TRANSITION, TransitionNode);
  902. ERR_FAIL_INDEX(p_input,n->inputs.size());
  903. if (n->inputs.size()<=1)
  904. return;
  905. n->inputs.remove(p_input);
  906. n->input_data.remove(p_input);
  907. last_error=_cycle_test(out_name);
  908. }
  909. int AnimationTreePlayer::transition_node_get_input_count(const StringName& p_node) const {
  910. GET_NODE_V(NODE_TRANSITION, TransitionNode, 0 );
  911. return n->inputs.size();
  912. }
  913. bool AnimationTreePlayer::transition_node_has_input_auto_advance(const StringName& p_node, int p_input) const {
  914. GET_NODE_V(NODE_TRANSITION, TransitionNode, false );
  915. ERR_FAIL_INDEX_V(p_input,n->inputs.size(),false);
  916. return n->input_data[p_input].auto_advance;
  917. }
  918. float AnimationTreePlayer::transition_node_get_xfade_time(const StringName& p_node) const {
  919. GET_NODE_V(NODE_TRANSITION, TransitionNode, 0 );
  920. return n->xfade;
  921. }
  922. int AnimationTreePlayer::transition_node_get_current(const StringName& p_node) const {
  923. GET_NODE_V(NODE_TRANSITION, TransitionNode, -1 );
  924. return n->current;
  925. }
  926. /*misc */
  927. void AnimationTreePlayer::get_node_list(List<StringName> *p_node_list) const {
  928. for(Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  929. p_node_list->push_back( E->key() );
  930. }
  931. }
  932. void AnimationTreePlayer::remove_node(const StringName& p_node) {
  933. ERR_FAIL_COND( !node_map.has(p_node) );
  934. ERR_EXPLAIN("Node 0 (output) can't be removed.");
  935. ERR_FAIL_COND( p_node == out_name );
  936. for(Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  937. NodeBase *nb = E->get();
  938. for(int i=0;i<nb->inputs.size();i++) {
  939. if (nb->inputs[i].node==p_node)
  940. nb->inputs[i].node=StringName();
  941. }
  942. }
  943. node_map.erase(p_node);
  944. // compute last error again, just in case
  945. last_error=_cycle_test(out_name);
  946. dirty_caches=true;
  947. }
  948. AnimationTreePlayer::ConnectError AnimationTreePlayer::_cycle_test(const StringName& p_at_node) {
  949. ERR_FAIL_COND_V(!node_map.has(p_at_node), CONNECT_INCOMPLETE);
  950. NodeBase *nb = node_map[p_at_node];
  951. if (nb->cycletest)
  952. return CONNECT_CYCLE;
  953. nb->cycletest=true;
  954. for(int i=0;i<nb->inputs.size();i++) {
  955. if (nb->inputs[i].node==StringName())
  956. return CONNECT_INCOMPLETE;
  957. ConnectError _err = _cycle_test(nb->inputs[i].node);
  958. if (_err)
  959. return _err;
  960. }
  961. return CONNECT_OK;
  962. }
  963. Error AnimationTreePlayer::connect(const StringName& p_src_node,const StringName& p_dst_node, int p_dst_input) {
  964. ERR_FAIL_COND_V( !node_map.has(p_src_node) , ERR_INVALID_PARAMETER);
  965. ERR_FAIL_COND_V( !node_map.has(p_dst_node) , ERR_INVALID_PARAMETER);
  966. ERR_FAIL_COND_V( p_src_node==p_dst_node , ERR_INVALID_PARAMETER);
  967. // NodeBase *src = node_map[p_src_node];
  968. NodeBase *dst = node_map[p_dst_node];
  969. ERR_FAIL_INDEX_V( p_dst_input, dst->inputs.size(), ERR_INVALID_PARAMETER);
  970. // int oldval = dst->inputs[p_dst_input].node;
  971. for(Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  972. NodeBase *nb = E->get();
  973. for(int i=0;i<nb->inputs.size();i++) {
  974. if (nb->inputs[i].node==p_src_node)
  975. nb->inputs[i].node=StringName();
  976. }
  977. }
  978. dst->inputs[p_dst_input].node=p_src_node;
  979. for(Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  980. NodeBase *nb = E->get();
  981. nb->cycletest=false;
  982. }
  983. last_error=_cycle_test(out_name);
  984. if (last_error) {
  985. if (last_error==CONNECT_INCOMPLETE)
  986. return ERR_UNCONFIGURED;
  987. else if (last_error==CONNECT_CYCLE)
  988. return ERR_CYCLIC_LINK;
  989. }
  990. dirty_caches=true;
  991. return OK;
  992. }
  993. bool AnimationTreePlayer::is_connected(const StringName& p_src_node,const StringName& p_dst_node, int p_dst_input) const {
  994. ERR_FAIL_COND_V( !node_map.has(p_src_node) , false);
  995. ERR_FAIL_COND_V( !node_map.has(p_dst_node) , false);
  996. ERR_FAIL_COND_V( p_src_node==p_dst_node , false);
  997. NodeBase *dst = node_map[p_dst_node];
  998. return dst->inputs[p_dst_input].node==p_src_node;
  999. }
  1000. void AnimationTreePlayer::disconnect(const StringName& p_node, int p_input) {
  1001. ERR_FAIL_COND( !node_map.has(p_node));
  1002. NodeBase *dst = node_map[p_node];
  1003. ERR_FAIL_INDEX(p_input,dst->inputs.size());
  1004. dst->inputs[p_input].node=StringName();
  1005. last_error=CONNECT_INCOMPLETE;
  1006. dirty_caches=true;
  1007. }
  1008. void AnimationTreePlayer::get_connection_list( List<Connection> *p_connections) const {
  1009. for(Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  1010. NodeBase *nb = E->get();
  1011. for(int i=0;i<nb->inputs.size();i++) {
  1012. if (nb->inputs[i].node!=StringName()) {
  1013. Connection c;
  1014. c.src_node=nb->inputs[i].node;
  1015. c.dst_node=E->key();
  1016. c.dst_input=i;
  1017. p_connections->push_back(c);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. AnimationTreePlayer::Track* AnimationTreePlayer::_find_track(const NodePath& p_path) {
  1023. Node *parent=get_node(base_path);
  1024. ERR_FAIL_COND_V(!parent,NULL);
  1025. Node *child=parent->get_node(p_path);
  1026. if (!child) {
  1027. String err = "Animation track references unknown Node: '"+String(p_path)+"'.";
  1028. WARN_PRINT(err.ascii().get_data());
  1029. return NULL;
  1030. }
  1031. ObjectID id=child->get_instance_ID();
  1032. StringName property;
  1033. int bone_idx=-1;
  1034. if (p_path.get_property()) {
  1035. if (child->cast_to<Skeleton>())
  1036. bone_idx = child->cast_to<Skeleton>()->find_bone( p_path.get_property() );
  1037. if (bone_idx==-1)
  1038. property=p_path.get_property();
  1039. }
  1040. TrackKey key;
  1041. key.id=id;
  1042. key.bone_idx=bone_idx;
  1043. key.property=property;
  1044. if (!track_map.has(key)) {
  1045. Track tr;
  1046. tr.id=id;
  1047. tr.node=child;
  1048. tr.skeleton=child->cast_to<Skeleton>();
  1049. tr.spatial=child->cast_to<Spatial>();
  1050. tr.bone_idx=bone_idx;
  1051. tr.property=property;
  1052. track_map[key]=tr;
  1053. }
  1054. return &track_map[key];
  1055. }
  1056. void AnimationTreePlayer::_recompute_caches() {
  1057. track_map.clear();
  1058. _recompute_caches(out_name);
  1059. dirty_caches=false;
  1060. }
  1061. void AnimationTreePlayer::_recompute_caches(const StringName& p_node) {
  1062. ERR_FAIL_COND( !node_map.has(p_node) );
  1063. NodeBase *nb = node_map[p_node];
  1064. if (nb->type==NODE_ANIMATION) {
  1065. AnimationNode *an = static_cast<AnimationNode*>(nb);
  1066. an->tref.clear();;
  1067. if (!an->animation.is_null()) {
  1068. Ref<Animation> a = an->animation;
  1069. for(int i=0;i<an->animation->get_track_count();i++) {
  1070. Track *tr = _find_track(a->track_get_path(i));
  1071. if (!tr)
  1072. continue;
  1073. AnimationNode::TrackRef tref;
  1074. tref.local_track=i;
  1075. tref.track=tr;
  1076. tref.weight=0;
  1077. an->tref.push_back(tref);
  1078. }
  1079. }
  1080. }
  1081. for(int i=0;i<nb->inputs.size();i++) {
  1082. _recompute_caches(nb->inputs[i].node);
  1083. }
  1084. }
  1085. void AnimationTreePlayer::recompute_caches() {
  1086. dirty_caches=true;
  1087. }
  1088. /* playback */
  1089. void AnimationTreePlayer::set_active(bool p_active) {
  1090. if (active == p_active)
  1091. return;
  1092. active = p_active;
  1093. processing = active;
  1094. reset_request = p_active;
  1095. _set_process(processing, true);
  1096. }
  1097. bool AnimationTreePlayer::is_active() const {
  1098. return active;
  1099. }
  1100. AnimationTreePlayer::ConnectError AnimationTreePlayer::get_last_error() const {
  1101. return last_error;
  1102. }
  1103. void AnimationTreePlayer::reset() {
  1104. reset_request=true;
  1105. }
  1106. void AnimationTreePlayer::set_base_path(const NodePath& p_path) {
  1107. base_path=p_path;
  1108. recompute_caches();
  1109. }
  1110. NodePath AnimationTreePlayer::get_base_path() const{
  1111. return base_path;
  1112. }
  1113. void AnimationTreePlayer::set_master_player(const NodePath& p_path) {
  1114. if (p_path==master)
  1115. return;
  1116. master=p_path;
  1117. _update_sources();
  1118. recompute_caches();
  1119. }
  1120. NodePath AnimationTreePlayer::get_master_player() const{
  1121. return master;
  1122. }
  1123. DVector<String> AnimationTreePlayer::_get_node_list() {
  1124. List<StringName> nl;
  1125. get_node_list(&nl);
  1126. DVector<String> ret;
  1127. ret.resize(nl.size());
  1128. int idx=0;
  1129. for(List<StringName>::Element *E=nl.front();E;E=E->next()) {
  1130. ret.set(idx++,E->get());
  1131. }
  1132. return ret;
  1133. }
  1134. void AnimationTreePlayer::_update_sources() {
  1135. if (master==NodePath())
  1136. return;
  1137. if (!is_inside_tree())
  1138. return;
  1139. Node *m = get_node(master);
  1140. if (!m) {
  1141. master=NodePath();
  1142. ERR_FAIL_COND(!m);
  1143. }
  1144. AnimationPlayer *ap = m->cast_to<AnimationPlayer>();
  1145. if (!ap) {
  1146. master=NodePath();
  1147. ERR_FAIL_COND(!ap);
  1148. }
  1149. for (Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  1150. if (E->get()->type==NODE_ANIMATION) {
  1151. AnimationNode *an = static_cast<AnimationNode*>(E->get());
  1152. if (an->from!="") {
  1153. an->animation = ap->get_animation(an->from);
  1154. }
  1155. }
  1156. }
  1157. }
  1158. bool AnimationTreePlayer::node_exists(const StringName& p_name) const {
  1159. return (node_map.has(p_name));
  1160. }
  1161. Error AnimationTreePlayer::node_rename(const StringName& p_node,const StringName& p_new_name) {
  1162. if (p_new_name==p_node)
  1163. return OK;
  1164. ERR_FAIL_COND_V(!node_map.has(p_node),ERR_ALREADY_EXISTS);
  1165. ERR_FAIL_COND_V(node_map.has(p_new_name),ERR_ALREADY_EXISTS);
  1166. ERR_FAIL_COND_V(p_new_name==StringName(),ERR_INVALID_DATA);
  1167. ERR_FAIL_COND_V(p_node==out_name,ERR_INVALID_DATA);
  1168. ERR_FAIL_COND_V(p_new_name==out_name,ERR_INVALID_DATA);
  1169. for(Map<StringName,NodeBase*>::Element *E=node_map.front();E;E=E->next()) {
  1170. NodeBase *nb = E->get();
  1171. for(int i=0;i<nb->inputs.size();i++) {
  1172. if (nb->inputs[i].node==p_node) {
  1173. nb->inputs[i].node=p_new_name;
  1174. }
  1175. }
  1176. }
  1177. node_map[p_new_name]=node_map[p_node];
  1178. node_map.erase(p_node);
  1179. return OK;
  1180. }
  1181. void AnimationTreePlayer::_bind_methods() {
  1182. ObjectTypeDB::bind_method(_MD("add_node","type","id"),&AnimationTreePlayer::add_node);
  1183. ObjectTypeDB::bind_method(_MD("node_exists","node"),&AnimationTreePlayer::node_exists);
  1184. ObjectTypeDB::bind_method(_MD("node_rename","node","new_name"),&AnimationTreePlayer::node_rename);
  1185. ObjectTypeDB::bind_method(_MD("node_get_type","id"),&AnimationTreePlayer::node_get_type);
  1186. ObjectTypeDB::bind_method(_MD("node_get_input_count","id"),&AnimationTreePlayer::node_get_input_count);
  1187. ObjectTypeDB::bind_method(_MD("node_get_input_source","id","idx"),&AnimationTreePlayer::node_get_input_source);
  1188. ObjectTypeDB::bind_method(_MD("animation_node_set_animation","id","animation:Animation"),&AnimationTreePlayer::animation_node_set_animation);
  1189. ObjectTypeDB::bind_method(_MD("animation_node_get_animation:Animation","id"),&AnimationTreePlayer::animation_node_get_animation);
  1190. ObjectTypeDB::bind_method(_MD("animation_node_set_master_animation","id","source"),&AnimationTreePlayer::animation_node_set_master_animation);
  1191. ObjectTypeDB::bind_method(_MD("animation_node_get_master_animation","id"),&AnimationTreePlayer::animation_node_get_master_animation);
  1192. ObjectTypeDB::bind_method(_MD("oneshot_node_set_fadein_time","id","time_sec"),&AnimationTreePlayer::oneshot_node_set_fadein_time);
  1193. ObjectTypeDB::bind_method(_MD("oneshot_node_get_fadein_time","id"),&AnimationTreePlayer::oneshot_node_get_fadein_time);
  1194. ObjectTypeDB::bind_method(_MD("oneshot_node_set_fadeout_time","id","time_sec"),&AnimationTreePlayer::oneshot_node_set_fadeout_time);
  1195. ObjectTypeDB::bind_method(_MD("oneshot_node_get_fadeout_time","id"),&AnimationTreePlayer::oneshot_node_get_fadeout_time);
  1196. ObjectTypeDB::bind_method(_MD("oneshot_node_set_autorestart","id","enable"),&AnimationTreePlayer::oneshot_node_set_autorestart);
  1197. ObjectTypeDB::bind_method(_MD("oneshot_node_set_autorestart_delay","id","delay_sec"),&AnimationTreePlayer::oneshot_node_set_autorestart_delay);
  1198. ObjectTypeDB::bind_method(_MD("oneshot_node_set_autorestart_random_delay","id","rand_sec"),&AnimationTreePlayer::oneshot_node_set_autorestart_random_delay);
  1199. ObjectTypeDB::bind_method(_MD("oneshot_node_has_autorestart","id"),&AnimationTreePlayer::oneshot_node_has_autorestart);
  1200. ObjectTypeDB::bind_method(_MD("oneshot_node_get_autorestart_delay","id"),&AnimationTreePlayer::oneshot_node_get_autorestart_delay);
  1201. ObjectTypeDB::bind_method(_MD("oneshot_node_get_autorestart_random_delay","id"),&AnimationTreePlayer::oneshot_node_get_autorestart_random_delay);
  1202. ObjectTypeDB::bind_method(_MD("oneshot_node_start","id"),&AnimationTreePlayer::oneshot_node_start);
  1203. ObjectTypeDB::bind_method(_MD("oneshot_node_stop","id"),&AnimationTreePlayer::oneshot_node_stop);
  1204. ObjectTypeDB::bind_method(_MD("oneshot_node_is_active","id"),&AnimationTreePlayer::oneshot_node_is_active);
  1205. ObjectTypeDB::bind_method(_MD("oneshot_node_set_filter_path","id","path","enable"),&AnimationTreePlayer::oneshot_node_set_filter_path);
  1206. ObjectTypeDB::bind_method(_MD("mix_node_set_amount","id","ratio"),&AnimationTreePlayer::mix_node_set_amount);
  1207. ObjectTypeDB::bind_method(_MD("mix_node_get_amount","id"),&AnimationTreePlayer::mix_node_get_amount);
  1208. ObjectTypeDB::bind_method(_MD("blend2_node_set_amount","id","blend"),&AnimationTreePlayer::blend2_node_set_amount);
  1209. ObjectTypeDB::bind_method(_MD("blend2_node_get_amount","id"),&AnimationTreePlayer::blend2_node_get_amount);
  1210. ObjectTypeDB::bind_method(_MD("blend2_node_set_filter_path","id","path","enable"),&AnimationTreePlayer::blend2_node_set_filter_path);
  1211. ObjectTypeDB::bind_method(_MD("blend3_node_set_amount","id","blend"),&AnimationTreePlayer::blend3_node_set_amount);
  1212. ObjectTypeDB::bind_method(_MD("blend3_node_get_amount","id"),&AnimationTreePlayer::blend3_node_get_amount);
  1213. ObjectTypeDB::bind_method(_MD("blend4_node_set_amount","id","blend"),&AnimationTreePlayer::blend4_node_set_amount);
  1214. ObjectTypeDB::bind_method(_MD("blend4_node_get_amount","id"),&AnimationTreePlayer::blend4_node_get_amount);
  1215. ObjectTypeDB::bind_method(_MD("timescale_node_set_scale","id","scale"),&AnimationTreePlayer::timescale_node_set_scale);
  1216. ObjectTypeDB::bind_method(_MD("timescale_node_get_scale","id"),&AnimationTreePlayer::timescale_node_get_scale);
  1217. ObjectTypeDB::bind_method(_MD("timeseek_node_seek","id","pos_sec"),&AnimationTreePlayer::timeseek_node_seek);
  1218. ObjectTypeDB::bind_method(_MD("transition_node_set_input_count","id","count"),&AnimationTreePlayer::transition_node_set_input_count);
  1219. ObjectTypeDB::bind_method(_MD("transition_node_get_input_count","id"),&AnimationTreePlayer::transition_node_get_input_count);
  1220. ObjectTypeDB::bind_method(_MD("transition_node_delete_input","id","input_idx"),&AnimationTreePlayer::transition_node_delete_input);
  1221. ObjectTypeDB::bind_method(_MD("transition_node_set_input_auto_advance","id","input_idx","enable"),&AnimationTreePlayer::transition_node_set_input_auto_advance);
  1222. ObjectTypeDB::bind_method(_MD("transition_node_has_input_auto_advance","id","input_idx"),&AnimationTreePlayer::transition_node_has_input_auto_advance);
  1223. ObjectTypeDB::bind_method(_MD("transition_node_set_xfade_time","id","time_sec"),&AnimationTreePlayer::transition_node_set_xfade_time);
  1224. ObjectTypeDB::bind_method(_MD("transition_node_get_xfade_time","id"),&AnimationTreePlayer::transition_node_get_xfade_time);
  1225. ObjectTypeDB::bind_method(_MD("transition_node_set_current","id","input_idx"),&AnimationTreePlayer::transition_node_set_current);
  1226. ObjectTypeDB::bind_method(_MD("transition_node_get_current","id"),&AnimationTreePlayer::transition_node_get_current);
  1227. ObjectTypeDB::bind_method(_MD("node_set_pos","id","screen_pos"),&AnimationTreePlayer::node_set_pos);
  1228. ObjectTypeDB::bind_method(_MD("node_get_pos","id"),&AnimationTreePlayer::node_get_pos);
  1229. ObjectTypeDB::bind_method(_MD("remove_node","id"),&AnimationTreePlayer::remove_node);
  1230. ObjectTypeDB::bind_method(_MD("connect","id","dst_id","dst_input_idx"),&AnimationTreePlayer::connect);
  1231. ObjectTypeDB::bind_method(_MD("is_connected","id","dst_id","dst_input_idx"),&AnimationTreePlayer::is_connected);
  1232. ObjectTypeDB::bind_method(_MD("disconnect","id","dst_input_idx"),&AnimationTreePlayer::disconnect);
  1233. ObjectTypeDB::bind_method(_MD("set_active","enabled"),&AnimationTreePlayer::set_active);
  1234. ObjectTypeDB::bind_method(_MD("is_active"),&AnimationTreePlayer::is_active);
  1235. ObjectTypeDB::bind_method(_MD("set_base_path","path"),&AnimationTreePlayer::set_base_path);
  1236. ObjectTypeDB::bind_method(_MD("get_base_path"),&AnimationTreePlayer::get_base_path);
  1237. ObjectTypeDB::bind_method(_MD("set_master_player","nodepath"),&AnimationTreePlayer::set_master_player);
  1238. ObjectTypeDB::bind_method(_MD("get_master_player"),&AnimationTreePlayer::get_master_player);
  1239. ObjectTypeDB::bind_method(_MD("get_node_list"),&AnimationTreePlayer::_get_node_list);
  1240. ObjectTypeDB::bind_method(_MD("set_animation_process_mode","mode"),&AnimationTreePlayer::set_animation_process_mode);
  1241. ObjectTypeDB::bind_method(_MD("get_animation_process_mode"),&AnimationTreePlayer::get_animation_process_mode);
  1242. ObjectTypeDB::bind_method(_MD("advance", "delta"), &AnimationTreePlayer::advance);
  1243. ObjectTypeDB::bind_method(_MD("reset"),&AnimationTreePlayer::reset);
  1244. ObjectTypeDB::bind_method(_MD("recompute_caches"),&AnimationTreePlayer::recompute_caches);
  1245. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback/process_mode", PROPERTY_HINT_ENUM, "Fixed,Idle"), _SCS("set_animation_process_mode"), _SCS("get_animation_process_mode"));
  1246. BIND_CONSTANT( NODE_OUTPUT );
  1247. BIND_CONSTANT( NODE_ANIMATION );
  1248. BIND_CONSTANT( NODE_ONESHOT );
  1249. BIND_CONSTANT( NODE_MIX );
  1250. BIND_CONSTANT( NODE_BLEND2 );
  1251. BIND_CONSTANT( NODE_BLEND3 );
  1252. BIND_CONSTANT( NODE_BLEND4 );
  1253. BIND_CONSTANT( NODE_TIMESCALE );
  1254. BIND_CONSTANT( NODE_TIMESEEK );
  1255. BIND_CONSTANT( NODE_TRANSITION );
  1256. }
  1257. AnimationTreePlayer::AnimationTreePlayer() {
  1258. active_list=NULL;
  1259. out = memnew( NodeOut ) ;
  1260. out_name="out";
  1261. out->pos=Point2(40,40);
  1262. node_map.insert( out_name , out);
  1263. AnimationProcessMode animation_process_mode;
  1264. animation_process_mode = ANIMATION_PROCESS_IDLE;
  1265. processing = false;
  1266. active=false;
  1267. dirty_caches=true;
  1268. reset_request=true;
  1269. last_error=CONNECT_INCOMPLETE;
  1270. base_path=String("..");
  1271. }
  1272. AnimationTreePlayer::~AnimationTreePlayer() {
  1273. while(node_map.size()) {
  1274. memdelete( node_map.front()->get() );
  1275. node_map.erase( node_map.front() );
  1276. }
  1277. }