animation_tree_player.cpp 46 KB

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