animation_tree_player.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. /*************************************************************************/
  2. /* animation_tree_player.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_tree_player.h"
  31. #include "animation_player.h"
  32. #include "scene/scene_string_names.h"
  33. void AnimationTreePlayer::set_animation_process_mode(AnimationProcessMode p_mode) {
  34. if (animation_process_mode == p_mode) {
  35. return;
  36. }
  37. bool pr = processing;
  38. if (pr) {
  39. _set_process(false);
  40. }
  41. animation_process_mode = p_mode;
  42. if (pr) {
  43. _set_process(true);
  44. }
  45. }
  46. AnimationTreePlayer::AnimationProcessMode AnimationTreePlayer::get_animation_process_mode() const {
  47. return animation_process_mode;
  48. }
  49. void AnimationTreePlayer::_set_process(bool p_process, bool p_force) {
  50. if (processing == p_process && !p_force) {
  51. return;
  52. }
  53. switch (animation_process_mode) {
  54. case ANIMATION_PROCESS_PHYSICS:
  55. set_physics_process_internal(p_process && active);
  56. break;
  57. case ANIMATION_PROCESS_IDLE:
  58. set_process_internal(p_process && active);
  59. break;
  60. }
  61. processing = p_process;
  62. }
  63. bool AnimationTreePlayer::_set(const StringName &p_name, const Variant &p_value) {
  64. if (String(p_name) == "base_path") {
  65. set_base_path(p_value);
  66. return true;
  67. }
  68. if (String(p_name) == "master_player") {
  69. set_master_player(p_value);
  70. return true;
  71. }
  72. if (String(p_name) == SceneStringNames::get_singleton()->playback_active) {
  73. set_active(p_value);
  74. return true;
  75. }
  76. if (String(p_name) != "data") {
  77. return false;
  78. }
  79. Dictionary data = p_value;
  80. Array nodes = data.get_valid("nodes");
  81. for (int i = 0; i < nodes.size(); i++) {
  82. Dictionary node = nodes[i];
  83. StringName id = node.get_valid("id");
  84. Point2 pos = node.get_valid("position");
  85. NodeType nt = NODE_MAX;
  86. String type = node.get_valid("type");
  87. if (type == "output") {
  88. nt = NODE_OUTPUT;
  89. } else if (type == "animation") {
  90. nt = NODE_ANIMATION;
  91. } else if (type == "oneshot") {
  92. nt = NODE_ONESHOT;
  93. } else if (type == "mix") {
  94. nt = NODE_MIX;
  95. } else if (type == "blend2") {
  96. nt = NODE_BLEND2;
  97. } else if (type == "blend3") {
  98. nt = NODE_BLEND3;
  99. } else if (type == "blend4") {
  100. nt = NODE_BLEND4;
  101. } else if (type == "timescale") {
  102. nt = NODE_TIMESCALE;
  103. } else if (type == "timeseek") {
  104. nt = NODE_TIMESEEK;
  105. } else if (type == "transition") {
  106. nt = NODE_TRANSITION;
  107. }
  108. ERR_FAIL_COND_V(nt == NODE_MAX, false);
  109. if (nt != NODE_OUTPUT) {
  110. add_node(nt, id);
  111. }
  112. node_set_position(id, pos);
  113. switch (nt) {
  114. case NODE_OUTPUT: {
  115. } break;
  116. case NODE_ANIMATION: {
  117. if (node.has("from")) {
  118. animation_node_set_master_animation(id, node.get_valid("from"));
  119. } else {
  120. animation_node_set_animation(id, node.get_valid("animation"));
  121. }
  122. Array filters = node.get_valid("filter");
  123. for (int j = 0; j < filters.size(); j++) {
  124. animation_node_set_filter_path(id, filters[j], true);
  125. }
  126. } break;
  127. case NODE_ONESHOT: {
  128. oneshot_node_set_fadein_time(id, node.get_valid("fade_in"));
  129. oneshot_node_set_fadeout_time(id, node.get_valid("fade_out"));
  130. oneshot_node_set_mix_mode(id, node.get_valid("mix"));
  131. oneshot_node_set_autorestart(id, node.get_valid("autorestart"));
  132. oneshot_node_set_autorestart_delay(id, node.get_valid("autorestart_delay"));
  133. oneshot_node_set_autorestart_random_delay(id, node.get_valid("autorestart_random_delay"));
  134. Array filters = node.get_valid("filter");
  135. for (int j = 0; j < filters.size(); j++) {
  136. oneshot_node_set_filter_path(id, filters[j], true);
  137. }
  138. } break;
  139. case NODE_MIX: {
  140. mix_node_set_amount(id, node.get_valid("mix"));
  141. } break;
  142. case NODE_BLEND2: {
  143. blend2_node_set_amount(id, node.get_valid("blend"));
  144. Array filters = node.get_valid("filter");
  145. for (int j = 0; j < filters.size(); j++) {
  146. blend2_node_set_filter_path(id, filters[j], true);
  147. }
  148. } break;
  149. case NODE_BLEND3: {
  150. blend3_node_set_amount(id, node.get_valid("blend"));
  151. } break;
  152. case NODE_BLEND4: {
  153. blend4_node_set_amount(id, node.get_valid("blend"));
  154. } break;
  155. case NODE_TIMESCALE: {
  156. timescale_node_set_scale(id, node.get_valid("scale"));
  157. } break;
  158. case NODE_TIMESEEK: {
  159. } break;
  160. case NODE_TRANSITION: {
  161. transition_node_set_xfade_time(id, node.get_valid("xfade"));
  162. Array transitions = node.get_valid("transitions");
  163. transition_node_set_input_count(id, transitions.size());
  164. for (int x = 0; x < transitions.size(); x++) {
  165. Dictionary d = transitions[x];
  166. bool aa = d.get_valid("auto_advance");
  167. transition_node_set_input_auto_advance(id, x, aa);
  168. }
  169. } break;
  170. default: {
  171. };
  172. }
  173. }
  174. Array connections = data.get_valid("connections");
  175. ERR_FAIL_COND_V(connections.size() % 3, false);
  176. int cc = connections.size() / 3;
  177. for (int i = 0; i < cc; i++) {
  178. StringName src = connections[i * 3 + 0];
  179. StringName dst = connections[i * 3 + 1];
  180. int dst_in = connections[i * 3 + 2];
  181. connect_nodes(src, dst, dst_in);
  182. }
  183. set_active(data.get_valid("active"));
  184. set_master_player(data.get_valid("master"));
  185. return true;
  186. }
  187. bool AnimationTreePlayer::_get(const StringName &p_name, Variant &r_ret) const {
  188. if (String(p_name) == "base_path") {
  189. r_ret = base_path;
  190. return true;
  191. }
  192. if (String(p_name) == "master_player") {
  193. r_ret = master;
  194. return true;
  195. }
  196. if (String(p_name) == "playback/active") {
  197. r_ret = is_active();
  198. return true;
  199. }
  200. if (String(p_name) != "data") {
  201. return false;
  202. }
  203. Dictionary data;
  204. Array nodes;
  205. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  206. NodeBase *n = node_map[E->key()];
  207. Dictionary node;
  208. node["id"] = E->key();
  209. node["position"] = n->pos;
  210. switch (n->type) {
  211. case NODE_OUTPUT:
  212. node["type"] = "output";
  213. break;
  214. case NODE_ANIMATION:
  215. node["type"] = "animation";
  216. break;
  217. case NODE_ONESHOT:
  218. node["type"] = "oneshot";
  219. break;
  220. case NODE_MIX:
  221. node["type"] = "mix";
  222. break;
  223. case NODE_BLEND2:
  224. node["type"] = "blend2";
  225. break;
  226. case NODE_BLEND3:
  227. node["type"] = "blend3";
  228. break;
  229. case NODE_BLEND4:
  230. node["type"] = "blend4";
  231. break;
  232. case NODE_TIMESCALE:
  233. node["type"] = "timescale";
  234. break;
  235. case NODE_TIMESEEK:
  236. node["type"] = "timeseek";
  237. break;
  238. case NODE_TRANSITION:
  239. node["type"] = "transition";
  240. break;
  241. default:
  242. node["type"] = "";
  243. break;
  244. }
  245. switch (n->type) {
  246. case NODE_OUTPUT: {
  247. } break;
  248. case NODE_ANIMATION: {
  249. AnimationNode *an = static_cast<AnimationNode *>(n);
  250. if (master != NodePath() && an->from != "") {
  251. node["from"] = an->from;
  252. } else {
  253. node["animation"] = an->animation;
  254. }
  255. Array k;
  256. List<NodePath> keys;
  257. an->filter.get_key_list(&keys);
  258. k.resize(keys.size());
  259. int i = 0;
  260. for (List<NodePath>::Element *F = keys.front(); F; F = F->next()) {
  261. k[i++] = F->get();
  262. }
  263. node["filter"] = k;
  264. } break;
  265. case NODE_ONESHOT: {
  266. OneShotNode *osn = static_cast<OneShotNode *>(n);
  267. node["fade_in"] = osn->fade_in;
  268. node["fade_out"] = osn->fade_out;
  269. node["mix"] = osn->mix;
  270. node["autorestart"] = osn->autorestart;
  271. node["autorestart_delay"] = osn->autorestart_delay;
  272. node["autorestart_random_delay"] = osn->autorestart_random_delay;
  273. Array k;
  274. List<NodePath> keys;
  275. osn->filter.get_key_list(&keys);
  276. k.resize(keys.size());
  277. int i = 0;
  278. for (List<NodePath>::Element *F = keys.front(); F; F = F->next()) {
  279. k[i++] = F->get();
  280. }
  281. node["filter"] = k;
  282. } break;
  283. case NODE_MIX: {
  284. MixNode *mn = static_cast<MixNode *>(n);
  285. node["mix"] = mn->amount;
  286. } break;
  287. case NODE_BLEND2: {
  288. Blend2Node *bn = static_cast<Blend2Node *>(n);
  289. node["blend"] = bn->value;
  290. Array k;
  291. List<NodePath> keys;
  292. bn->filter.get_key_list(&keys);
  293. k.resize(keys.size());
  294. int i = 0;
  295. for (List<NodePath>::Element *F = keys.front(); F; F = F->next()) {
  296. k[i++] = F->get();
  297. }
  298. node["filter"] = k;
  299. } break;
  300. case NODE_BLEND3: {
  301. Blend3Node *bn = static_cast<Blend3Node *>(n);
  302. node["blend"] = bn->value;
  303. } break;
  304. case NODE_BLEND4: {
  305. Blend4Node *bn = static_cast<Blend4Node *>(n);
  306. node["blend"] = bn->value;
  307. } break;
  308. case NODE_TIMESCALE: {
  309. TimeScaleNode *tsn = static_cast<TimeScaleNode *>(n);
  310. node["scale"] = tsn->scale;
  311. } break;
  312. case NODE_TIMESEEK: {
  313. } break;
  314. case NODE_TRANSITION: {
  315. TransitionNode *tn = static_cast<TransitionNode *>(n);
  316. node["xfade"] = tn->xfade;
  317. Array transitions;
  318. for (int i = 0; i < tn->input_data.size(); i++) {
  319. Dictionary d;
  320. d["auto_advance"] = tn->input_data[i].auto_advance;
  321. transitions.push_back(d);
  322. }
  323. node["transitions"] = transitions;
  324. } break;
  325. default: {
  326. };
  327. }
  328. nodes.push_back(node);
  329. }
  330. data["nodes"] = nodes;
  331. //connectiosn
  332. List<Connection> connections;
  333. get_connection_list(&connections);
  334. Array connections_arr;
  335. connections_arr.resize(connections.size() * 3);
  336. int idx = 0;
  337. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  338. connections_arr.set(idx + 0, E->get().src_node);
  339. connections_arr.set(idx + 1, E->get().dst_node);
  340. connections_arr.set(idx + 2, E->get().dst_input);
  341. idx += 3;
  342. }
  343. data["connections"] = connections_arr;
  344. data["active"] = active;
  345. data["master"] = master;
  346. r_ret = data;
  347. return true;
  348. }
  349. void AnimationTreePlayer::_get_property_list(List<PropertyInfo> *p_list) const {
  350. p_list->push_back(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_NETWORK));
  351. }
  352. void AnimationTreePlayer::advance(float p_time) {
  353. _process_animation(p_time);
  354. }
  355. void AnimationTreePlayer::_notification(int p_what) {
  356. switch (p_what) {
  357. case NOTIFICATION_ENTER_TREE: {
  358. WARN_DEPRECATED_MSG("AnimationTreePlayer has been deprecated. Use AnimationTree instead.");
  359. if (!processing) {
  360. //make sure that a previous process state was not saved
  361. //only process if "processing" is set
  362. set_physics_process_internal(false);
  363. set_process_internal(false);
  364. }
  365. } break;
  366. case NOTIFICATION_READY: {
  367. dirty_caches = true;
  368. if (master != NodePath()) {
  369. _update_sources();
  370. }
  371. } break;
  372. case NOTIFICATION_INTERNAL_PROCESS: {
  373. if (animation_process_mode == ANIMATION_PROCESS_PHYSICS) {
  374. break;
  375. }
  376. if (processing) {
  377. _process_animation(get_process_delta_time());
  378. }
  379. } break;
  380. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  381. if (animation_process_mode == ANIMATION_PROCESS_IDLE) {
  382. break;
  383. }
  384. if (processing) {
  385. _process_animation(get_physics_process_delta_time());
  386. }
  387. } break;
  388. }
  389. }
  390. void AnimationTreePlayer::_compute_weights(float *p_fallback_weight, HashMap<NodePath, float> *p_weights, float p_coeff, const HashMap<NodePath, bool> *p_filter, float p_filtered_coeff) {
  391. if (p_filter != nullptr) {
  392. List<NodePath> key_list;
  393. p_filter->get_key_list(&key_list);
  394. for (List<NodePath>::Element *E = key_list.front(); E; E = E->next()) {
  395. if ((*p_filter)[E->get()]) {
  396. if (p_weights->has(E->get())) {
  397. (*p_weights)[E->get()] *= p_filtered_coeff;
  398. } else {
  399. p_weights->set(E->get(), *p_fallback_weight * p_filtered_coeff);
  400. }
  401. } else if (p_weights->has(E->get())) {
  402. (*p_weights)[E->get()] *= p_coeff;
  403. }
  404. }
  405. }
  406. List<NodePath> key_list;
  407. p_weights->get_key_list(&key_list);
  408. for (List<NodePath>::Element *E = key_list.front(); E; E = E->next()) {
  409. if (p_filter == nullptr || !p_filter->has(E->get())) {
  410. (*p_weights)[E->get()] *= p_coeff;
  411. }
  412. }
  413. *p_fallback_weight *= p_coeff;
  414. }
  415. float AnimationTreePlayer::_process_node(const StringName &p_node, AnimationNode **r_prev_anim, float p_time, bool p_seek, float p_fallback_weight, HashMap<NodePath, float> *p_weights) {
  416. ERR_FAIL_COND_V(!node_map.has(p_node), 0);
  417. NodeBase *nb = node_map[p_node];
  418. //transform to seconds...
  419. switch (nb->type) {
  420. case NODE_OUTPUT: {
  421. NodeOut *on = static_cast<NodeOut *>(nb);
  422. HashMap<NodePath, float> weights;
  423. return _process_node(on->inputs[0].node, r_prev_anim, p_time, p_seek, p_fallback_weight, &weights);
  424. } break;
  425. case NODE_ANIMATION: {
  426. AnimationNode *an = static_cast<AnimationNode *>(nb);
  427. float rem = 0;
  428. if (!an->animation.is_null()) {
  429. //float pos = an->time;
  430. //float delta = p_time;
  431. //const Animation *a = an->animation.operator->();
  432. if (p_seek) {
  433. an->time = p_time;
  434. an->step = 0;
  435. } else {
  436. an->time = MAX(0, an->time + p_time);
  437. an->step = p_time;
  438. }
  439. float anim_size = an->animation->get_length();
  440. if (an->animation->has_loop()) {
  441. if (anim_size) {
  442. an->time = Math::fposmod(an->time, anim_size);
  443. }
  444. } else if (an->time > anim_size) {
  445. an->time = anim_size;
  446. }
  447. an->skip = true;
  448. for (List<AnimationNode::TrackRef>::Element *E = an->tref.front(); E; E = E->next()) {
  449. NodePath track_path = an->animation->track_get_path(E->get().local_track);
  450. if (an->filter.has(track_path) && an->filter[track_path]) {
  451. E->get().weight = 0;
  452. } else {
  453. if (p_weights->has(track_path)) {
  454. float weight = (*p_weights)[track_path];
  455. E->get().weight = weight;
  456. } else {
  457. E->get().weight = p_fallback_weight;
  458. }
  459. }
  460. if (E->get().weight > CMP_EPSILON) {
  461. an->skip = false;
  462. }
  463. }
  464. rem = anim_size - an->time;
  465. }
  466. if (!(*r_prev_anim)) {
  467. active_list = an;
  468. } else {
  469. (*r_prev_anim)->next = an;
  470. }
  471. an->next = nullptr;
  472. *r_prev_anim = an;
  473. return rem;
  474. } break;
  475. case NODE_ONESHOT: {
  476. OneShotNode *osn = static_cast<OneShotNode *>(nb);
  477. if (!osn->active) {
  478. //make it as if this node doesn't exist, pass input 0 by.
  479. return _process_node(osn->inputs[0].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  480. }
  481. bool os_seek = p_seek;
  482. if (p_seek) {
  483. osn->time = p_time;
  484. }
  485. if (osn->start) {
  486. osn->time = 0;
  487. os_seek = true;
  488. }
  489. float blend;
  490. if (osn->time < osn->fade_in) {
  491. if (osn->fade_in > 0) {
  492. blend = osn->time / osn->fade_in;
  493. } else {
  494. blend = 0; //wtf
  495. }
  496. } else if (!osn->start && osn->remaining < osn->fade_out) {
  497. if (osn->fade_out) {
  498. blend = (osn->remaining / osn->fade_out);
  499. } else {
  500. blend = 1.0;
  501. }
  502. } else {
  503. blend = 1.0;
  504. }
  505. float main_rem;
  506. float os_rem;
  507. HashMap<NodePath, float> os_weights(*p_weights);
  508. float os_fallback_weight = p_fallback_weight;
  509. _compute_weights(&p_fallback_weight, p_weights, osn->mix ? 1.0 : 1.0 - blend, &osn->filter, 1.0);
  510. _compute_weights(&os_fallback_weight, &os_weights, blend, &osn->filter, 0.0);
  511. main_rem = _process_node(osn->inputs[0].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  512. os_rem = _process_node(osn->inputs[1].node, r_prev_anim, p_time, os_seek, os_fallback_weight, &os_weights);
  513. if (osn->start) {
  514. osn->remaining = os_rem;
  515. osn->start = false;
  516. }
  517. if (!p_seek) {
  518. osn->time += p_time;
  519. osn->remaining = os_rem;
  520. if (osn->remaining <= 0) {
  521. osn->active = false;
  522. }
  523. }
  524. return MAX(main_rem, osn->remaining);
  525. } break;
  526. case NODE_MIX: {
  527. MixNode *mn = static_cast<MixNode *>(nb);
  528. HashMap<NodePath, float> mn_weights(*p_weights);
  529. float mn_fallback_weight = p_fallback_weight;
  530. _compute_weights(&mn_fallback_weight, &mn_weights, mn->amount);
  531. float rem = _process_node(mn->inputs[0].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  532. _process_node(mn->inputs[1].node, r_prev_anim, p_time, p_seek, mn_fallback_weight, &mn_weights);
  533. return rem;
  534. } break;
  535. case NODE_BLEND2: {
  536. Blend2Node *bn = static_cast<Blend2Node *>(nb);
  537. HashMap<NodePath, float> bn_weights(*p_weights);
  538. float bn_fallback_weight = p_fallback_weight;
  539. _compute_weights(&p_fallback_weight, p_weights, 1.0 - bn->value, &bn->filter, 1.0);
  540. _compute_weights(&bn_fallback_weight, &bn_weights, bn->value, &bn->filter, 0.0);
  541. float rem = _process_node(bn->inputs[0].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  542. _process_node(bn->inputs[1].node, r_prev_anim, p_time, p_seek, bn_fallback_weight, &bn_weights);
  543. return rem;
  544. } break;
  545. case NODE_BLEND3: {
  546. Blend3Node *bn = static_cast<Blend3Node *>(nb);
  547. float rem;
  548. float blend, lower_blend, upper_blend;
  549. if (bn->value < 0) {
  550. lower_blend = -bn->value;
  551. blend = 1.0 - lower_blend;
  552. upper_blend = 0;
  553. } else {
  554. lower_blend = 0;
  555. blend = 1.0 - bn->value;
  556. upper_blend = bn->value;
  557. }
  558. HashMap<NodePath, float> upper_weights(*p_weights);
  559. float upper_fallback_weight = p_fallback_weight;
  560. HashMap<NodePath, float> lower_weights(*p_weights);
  561. float lower_fallback_weight = p_fallback_weight;
  562. _compute_weights(&upper_fallback_weight, &upper_weights, upper_blend);
  563. _compute_weights(&p_fallback_weight, p_weights, blend);
  564. _compute_weights(&lower_fallback_weight, &lower_weights, lower_blend);
  565. rem = _process_node(bn->inputs[1].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  566. _process_node(bn->inputs[0].node, r_prev_anim, p_time, p_seek, lower_fallback_weight, &lower_weights);
  567. _process_node(bn->inputs[2].node, r_prev_anim, p_time, p_seek, upper_fallback_weight, &upper_weights);
  568. return rem;
  569. } break;
  570. case NODE_BLEND4: {
  571. Blend4Node *bn = static_cast<Blend4Node *>(nb);
  572. HashMap<NodePath, float> weights1(*p_weights);
  573. float fallback_weight1 = p_fallback_weight;
  574. HashMap<NodePath, float> weights2(*p_weights);
  575. float fallback_weight2 = p_fallback_weight;
  576. HashMap<NodePath, float> weights3(*p_weights);
  577. float fallback_weight3 = p_fallback_weight;
  578. _compute_weights(&p_fallback_weight, p_weights, 1.0 - bn->value.x);
  579. _compute_weights(&fallback_weight1, &weights1, bn->value.x);
  580. _compute_weights(&fallback_weight2, &weights2, 1.0 - bn->value.y);
  581. _compute_weights(&fallback_weight3, &weights3, bn->value.y);
  582. float rem = _process_node(bn->inputs[0].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  583. _process_node(bn->inputs[1].node, r_prev_anim, p_time, p_seek, fallback_weight1, &weights1);
  584. float rem2 = _process_node(bn->inputs[2].node, r_prev_anim, p_time, p_seek, fallback_weight2, &weights2);
  585. _process_node(bn->inputs[3].node, r_prev_anim, p_time, p_seek, fallback_weight3, &weights3);
  586. return MAX(rem, rem2);
  587. } break;
  588. case NODE_TIMESCALE: {
  589. TimeScaleNode *tsn = static_cast<TimeScaleNode *>(nb);
  590. float rem;
  591. if (p_seek) {
  592. rem = _process_node(tsn->inputs[0].node, r_prev_anim, p_time, true, p_fallback_weight, p_weights);
  593. } else {
  594. rem = _process_node(tsn->inputs[0].node, r_prev_anim, p_time * tsn->scale, false, p_fallback_weight, p_weights);
  595. }
  596. if (tsn->scale == 0) {
  597. return Math_INF;
  598. } else {
  599. return rem / tsn->scale;
  600. }
  601. } break;
  602. case NODE_TIMESEEK: {
  603. TimeSeekNode *tsn = static_cast<TimeSeekNode *>(nb);
  604. if (tsn->seek_pos >= 0 && !p_seek) {
  605. p_time = tsn->seek_pos;
  606. p_seek = true;
  607. }
  608. tsn->seek_pos = -1;
  609. return _process_node(tsn->inputs[0].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  610. } break;
  611. case NODE_TRANSITION: {
  612. TransitionNode *tn = static_cast<TransitionNode *>(nb);
  613. HashMap<NodePath, float> prev_weights(*p_weights);
  614. float prev_fallback_weight = p_fallback_weight;
  615. if (tn->prev < 0) { // process current animation, check for transition
  616. float rem = _process_node(tn->inputs[tn->current].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  617. if (p_seek) {
  618. tn->time = p_time;
  619. } else {
  620. tn->time += p_time;
  621. }
  622. if (tn->input_data[tn->current].auto_advance && rem <= tn->xfade) {
  623. tn->set_current((tn->current + 1) % tn->inputs.size());
  624. }
  625. return rem;
  626. } else { // cross-fading from tn->prev to tn->current
  627. float blend = tn->xfade ? (tn->prev_xfading / tn->xfade) : 1;
  628. float rem;
  629. _compute_weights(&p_fallback_weight, p_weights, 1.0 - blend);
  630. _compute_weights(&prev_fallback_weight, &prev_weights, blend);
  631. if (!p_seek && tn->switched) { //just switched, seek to start of current
  632. rem = _process_node(tn->inputs[tn->current].node, r_prev_anim, 0, true, p_fallback_weight, p_weights);
  633. } else {
  634. rem = _process_node(tn->inputs[tn->current].node, r_prev_anim, p_time, p_seek, p_fallback_weight, p_weights);
  635. }
  636. tn->switched = false;
  637. if (p_seek) { // don't seek prev animation
  638. _process_node(tn->inputs[tn->prev].node, r_prev_anim, 0, false, prev_fallback_weight, &prev_weights);
  639. tn->time = p_time;
  640. } else {
  641. _process_node(tn->inputs[tn->prev].node, r_prev_anim, p_time, false, prev_fallback_weight, &prev_weights);
  642. tn->time += p_time;
  643. tn->prev_xfading -= p_time;
  644. if (tn->prev_xfading < 0) {
  645. tn->prev = -1;
  646. }
  647. }
  648. return rem;
  649. }
  650. } break;
  651. default: {
  652. }
  653. }
  654. return 0;
  655. }
  656. void AnimationTreePlayer::_process_animation(float p_delta) {
  657. if (last_error != CONNECT_OK) {
  658. return;
  659. }
  660. if (dirty_caches) {
  661. _recompute_caches();
  662. }
  663. active_list = nullptr;
  664. AnimationNode *prev = nullptr;
  665. if (reset_request) {
  666. _process_node(out_name, &prev, 0, true);
  667. reset_request = false;
  668. } else {
  669. _process_node(out_name, &prev, p_delta);
  670. }
  671. if (dirty_caches) {
  672. //some animation changed.. ignore this pass
  673. return;
  674. }
  675. //update the tracks..
  676. /* STEP 1 CLEAR TRACKS */
  677. for (TrackMap::Element *E = track_map.front(); E; E = E->next()) {
  678. Track &t = E->get();
  679. t.loc.zero();
  680. t.rot = Quat();
  681. t.scale.x = 0;
  682. t.scale.y = 0;
  683. t.scale.z = 0;
  684. t.value = t.object->get_indexed(t.subpath);
  685. t.value.zero();
  686. t.skip = false;
  687. }
  688. /* STEP 2 PROCESS ANIMATIONS */
  689. AnimationNode *anim_list = active_list;
  690. Quat empty_rot;
  691. while (anim_list) {
  692. if (!anim_list->animation.is_null() && !anim_list->skip) {
  693. //check if animation is meaningful
  694. Animation *a = anim_list->animation.operator->();
  695. for (List<AnimationNode::TrackRef>::Element *E = anim_list->tref.front(); E; E = E->next()) {
  696. AnimationNode::TrackRef &tr = E->get();
  697. if (tr.track == nullptr || tr.local_track < 0 || tr.weight < CMP_EPSILON || !a->track_is_enabled(tr.local_track)) {
  698. continue;
  699. }
  700. switch (a->track_get_type(tr.local_track)) {
  701. case Animation::TYPE_TRANSFORM: { ///< Transform a node or a bone.
  702. Vector3 loc;
  703. Quat rot;
  704. Vector3 scale;
  705. a->transform_track_interpolate(tr.local_track, anim_list->time, &loc, &rot, &scale);
  706. tr.track->loc += loc * tr.weight;
  707. scale.x -= 1.0;
  708. scale.y -= 1.0;
  709. scale.z -= 1.0;
  710. tr.track->scale += scale * tr.weight;
  711. tr.track->rot = tr.track->rot * empty_rot.slerp(rot, tr.weight);
  712. } break;
  713. case Animation::TYPE_VALUE: { ///< Set a value in a property, can be interpolated.
  714. if (a->value_track_get_update_mode(tr.local_track) == Animation::UPDATE_CONTINUOUS) {
  715. Variant value = a->value_track_interpolate(tr.local_track, anim_list->time);
  716. Variant::blend(tr.track->value, value, tr.weight, tr.track->value);
  717. } else {
  718. int index = a->track_find_key(tr.local_track, anim_list->time);
  719. tr.track->value = a->track_get_key_value(tr.local_track, index);
  720. }
  721. } break;
  722. case Animation::TYPE_METHOD: { ///< Call any method on a specific node.
  723. List<int> indices;
  724. a->method_track_get_key_indices(tr.local_track, anim_list->time, anim_list->step, &indices);
  725. for (List<int>::Element *F = indices.front(); F; F = F->next()) {
  726. StringName method = a->method_track_get_name(tr.local_track, F->get());
  727. Vector<Variant> args = a->method_track_get_params(tr.local_track, F->get());
  728. args.resize(VARIANT_ARG_MAX);
  729. tr.track->object->call(method, args[0], args[1], args[2], args[3], args[4]);
  730. }
  731. } break;
  732. default: {
  733. }
  734. }
  735. }
  736. }
  737. anim_list = anim_list->next;
  738. }
  739. /* STEP 3 APPLY TRACKS */
  740. for (TrackMap::Element *E = track_map.front(); E; E = E->next()) {
  741. Track &t = E->get();
  742. if (t.skip || !t.object) {
  743. continue;
  744. }
  745. if (t.subpath.size()) { // value track
  746. t.object->set_indexed(t.subpath, t.value);
  747. continue;
  748. }
  749. Transform xform;
  750. xform.origin = t.loc;
  751. t.scale.x += 1.0;
  752. t.scale.y += 1.0;
  753. t.scale.z += 1.0;
  754. xform.basis.set_quat_scale(t.rot, t.scale);
  755. if (t.bone_idx >= 0) {
  756. if (t.skeleton) {
  757. t.skeleton->set_bone_pose(t.bone_idx, xform);
  758. }
  759. } else if (t.spatial) {
  760. t.spatial->set_transform(xform);
  761. }
  762. }
  763. }
  764. void AnimationTreePlayer::add_node(NodeType p_type, const StringName &p_node) {
  765. ERR_FAIL_COND(p_type == NODE_OUTPUT);
  766. ERR_FAIL_COND(node_map.has(p_node));
  767. ERR_FAIL_INDEX(p_type, NODE_MAX);
  768. NodeBase *n = nullptr;
  769. switch (p_type) {
  770. case NODE_ANIMATION: {
  771. n = memnew(AnimationNode);
  772. } break;
  773. case NODE_ONESHOT: {
  774. n = memnew(OneShotNode);
  775. } break;
  776. case NODE_MIX: {
  777. n = memnew(MixNode);
  778. } break;
  779. case NODE_BLEND2: {
  780. n = memnew(Blend2Node);
  781. } break;
  782. case NODE_BLEND3: {
  783. n = memnew(Blend3Node);
  784. } break;
  785. case NODE_BLEND4: {
  786. n = memnew(Blend4Node);
  787. } break;
  788. case NODE_TIMESCALE: {
  789. n = memnew(TimeScaleNode);
  790. } break;
  791. case NODE_TIMESEEK: {
  792. n = memnew(TimeSeekNode);
  793. } break;
  794. case NODE_TRANSITION: {
  795. n = memnew(TransitionNode);
  796. } break;
  797. default: {
  798. }
  799. }
  800. //n->name+=" "+itos(p_node);
  801. node_map[p_node] = n;
  802. }
  803. StringName AnimationTreePlayer::node_get_input_source(const StringName &p_node, int p_input) const {
  804. ERR_FAIL_COND_V(!node_map.has(p_node), StringName());
  805. ERR_FAIL_INDEX_V(p_input, node_map[p_node]->inputs.size(), StringName());
  806. return node_map[p_node]->inputs[p_input].node;
  807. }
  808. int AnimationTreePlayer::node_get_input_count(const StringName &p_node) const {
  809. ERR_FAIL_COND_V(!node_map.has(p_node), -1);
  810. return node_map[p_node]->inputs.size();
  811. }
  812. #define GET_NODE(m_type, m_cast) \
  813. ERR_FAIL_COND(!node_map.has(p_node)); \
  814. ERR_FAIL_COND_MSG(node_map[p_node]->type != m_type, "Invalid parameter for node type."); \
  815. m_cast *n = static_cast<m_cast *>(node_map[p_node]);
  816. void AnimationTreePlayer::animation_node_set_animation(const StringName &p_node, const Ref<Animation> &p_animation) {
  817. GET_NODE(NODE_ANIMATION, AnimationNode);
  818. n->animation = p_animation;
  819. dirty_caches = true;
  820. }
  821. void AnimationTreePlayer::animation_node_set_master_animation(const StringName &p_node, const String &p_master_animation) {
  822. GET_NODE(NODE_ANIMATION, AnimationNode);
  823. n->from = p_master_animation;
  824. dirty_caches = true;
  825. if (master != NodePath()) {
  826. _update_sources();
  827. }
  828. }
  829. void AnimationTreePlayer::animation_node_set_filter_path(const StringName &p_node, const NodePath &p_track_path, bool p_filter) {
  830. GET_NODE(NODE_ANIMATION, AnimationNode);
  831. if (p_filter) {
  832. n->filter[p_track_path] = true;
  833. } else {
  834. n->filter.erase(p_track_path);
  835. }
  836. }
  837. void AnimationTreePlayer::animation_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const {
  838. GET_NODE(NODE_ANIMATION, AnimationNode);
  839. n->filter.get_key_list(r_paths);
  840. }
  841. void AnimationTreePlayer::oneshot_node_set_fadein_time(const StringName &p_node, float p_time) {
  842. GET_NODE(NODE_ONESHOT, OneShotNode);
  843. n->fade_in = p_time;
  844. }
  845. void AnimationTreePlayer::oneshot_node_set_fadeout_time(const StringName &p_node, float p_time) {
  846. GET_NODE(NODE_ONESHOT, OneShotNode);
  847. n->fade_out = p_time;
  848. }
  849. void AnimationTreePlayer::oneshot_node_set_mix_mode(const StringName &p_node, bool p_mix) {
  850. GET_NODE(NODE_ONESHOT, OneShotNode);
  851. n->mix = p_mix;
  852. }
  853. void AnimationTreePlayer::oneshot_node_set_autorestart(const StringName &p_node, bool p_active) {
  854. GET_NODE(NODE_ONESHOT, OneShotNode);
  855. n->autorestart = p_active;
  856. }
  857. void AnimationTreePlayer::oneshot_node_set_autorestart_delay(const StringName &p_node, float p_time) {
  858. GET_NODE(NODE_ONESHOT, OneShotNode);
  859. n->autorestart_delay = p_time;
  860. }
  861. void AnimationTreePlayer::oneshot_node_set_autorestart_random_delay(const StringName &p_node, float p_time) {
  862. GET_NODE(NODE_ONESHOT, OneShotNode);
  863. n->autorestart_random_delay = p_time;
  864. }
  865. void AnimationTreePlayer::oneshot_node_start(const StringName &p_node) {
  866. GET_NODE(NODE_ONESHOT, OneShotNode);
  867. n->active = true;
  868. n->start = true;
  869. }
  870. void AnimationTreePlayer::oneshot_node_stop(const StringName &p_node) {
  871. GET_NODE(NODE_ONESHOT, OneShotNode);
  872. n->active = false;
  873. }
  874. void AnimationTreePlayer::oneshot_node_set_filter_path(const StringName &p_node, const NodePath &p_filter, bool p_enable) {
  875. GET_NODE(NODE_ONESHOT, OneShotNode);
  876. if (p_enable) {
  877. n->filter[p_filter] = true;
  878. } else {
  879. n->filter.erase(p_filter);
  880. }
  881. }
  882. void AnimationTreePlayer::oneshot_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const {
  883. GET_NODE(NODE_ONESHOT, OneShotNode);
  884. n->filter.get_key_list(r_paths);
  885. }
  886. void AnimationTreePlayer::mix_node_set_amount(const StringName &p_node, float p_amount) {
  887. GET_NODE(NODE_MIX, MixNode);
  888. n->amount = p_amount;
  889. }
  890. void AnimationTreePlayer::blend2_node_set_amount(const StringName &p_node, float p_amount) {
  891. GET_NODE(NODE_BLEND2, Blend2Node);
  892. n->value = p_amount;
  893. }
  894. void AnimationTreePlayer::blend2_node_set_filter_path(const StringName &p_node, const NodePath &p_filter, bool p_enable) {
  895. GET_NODE(NODE_BLEND2, Blend2Node);
  896. if (p_enable) {
  897. n->filter[p_filter] = true;
  898. } else {
  899. n->filter.erase(p_filter);
  900. }
  901. }
  902. void AnimationTreePlayer::blend2_node_set_get_filtered_paths(const StringName &p_node, List<NodePath> *r_paths) const {
  903. GET_NODE(NODE_BLEND2, Blend2Node);
  904. n->filter.get_key_list(r_paths);
  905. }
  906. void AnimationTreePlayer::blend3_node_set_amount(const StringName &p_node, float p_amount) {
  907. GET_NODE(NODE_BLEND3, Blend3Node);
  908. n->value = p_amount;
  909. }
  910. void AnimationTreePlayer::blend4_node_set_amount(const StringName &p_node, const Vector2 &p_amount) {
  911. GET_NODE(NODE_BLEND4, Blend4Node);
  912. n->value = p_amount;
  913. }
  914. void AnimationTreePlayer::timescale_node_set_scale(const StringName &p_node, float p_scale) {
  915. GET_NODE(NODE_TIMESCALE, TimeScaleNode);
  916. n->scale = p_scale;
  917. }
  918. void AnimationTreePlayer::timeseek_node_seek(const StringName &p_node, float p_pos) {
  919. GET_NODE(NODE_TIMESEEK, TimeSeekNode);
  920. n->seek_pos = p_pos;
  921. }
  922. void AnimationTreePlayer::transition_node_set_input_count(const StringName &p_node, int p_inputs) {
  923. GET_NODE(NODE_TRANSITION, TransitionNode);
  924. ERR_FAIL_COND(p_inputs < 1);
  925. n->inputs.resize(p_inputs);
  926. n->input_data.resize(p_inputs);
  927. _clear_cycle_test();
  928. last_error = _cycle_test(out_name);
  929. }
  930. void AnimationTreePlayer::transition_node_set_input_auto_advance(const StringName &p_node, int p_input, bool p_auto_advance) {
  931. GET_NODE(NODE_TRANSITION, TransitionNode);
  932. ERR_FAIL_INDEX(p_input, n->input_data.size());
  933. n->input_data.write[p_input].auto_advance = p_auto_advance;
  934. }
  935. void AnimationTreePlayer::transition_node_set_xfade_time(const StringName &p_node, float p_time) {
  936. GET_NODE(NODE_TRANSITION, TransitionNode);
  937. n->xfade = p_time;
  938. }
  939. void AnimationTreePlayer::TransitionNode::set_current(int p_current) {
  940. ERR_FAIL_INDEX(p_current, inputs.size());
  941. if (current == p_current) {
  942. return;
  943. }
  944. prev = current;
  945. prev_xfading = xfade;
  946. prev_time = time;
  947. time = 0;
  948. current = p_current;
  949. switched = true;
  950. }
  951. void AnimationTreePlayer::transition_node_set_current(const StringName &p_node, int p_current) {
  952. GET_NODE(NODE_TRANSITION, TransitionNode);
  953. n->set_current(p_current);
  954. }
  955. void AnimationTreePlayer::node_set_position(const StringName &p_node, const Vector2 &p_pos) {
  956. ERR_FAIL_COND(!node_map.has(p_node));
  957. node_map[p_node]->pos = p_pos;
  958. }
  959. AnimationTreePlayer::NodeType AnimationTreePlayer::node_get_type(const StringName &p_node) const {
  960. ERR_FAIL_COND_V(!node_map.has(p_node), NODE_OUTPUT);
  961. return node_map[p_node]->type;
  962. }
  963. Point2 AnimationTreePlayer::node_get_position(const StringName &p_node) const {
  964. ERR_FAIL_COND_V(!node_map.has(p_node), Point2());
  965. return node_map[p_node]->pos;
  966. }
  967. #define GET_NODE_V(m_type, m_cast, m_ret) \
  968. ERR_FAIL_COND_V(!node_map.has(p_node), m_ret); \
  969. ERR_FAIL_COND_V_MSG(node_map[p_node]->type != m_type, m_ret, "Invalid parameter for node type."); \
  970. m_cast *n = static_cast<m_cast *>(node_map[p_node]);
  971. Ref<Animation> AnimationTreePlayer::animation_node_get_animation(const StringName &p_node) const {
  972. GET_NODE_V(NODE_ANIMATION, AnimationNode, Ref<Animation>());
  973. return n->animation;
  974. }
  975. String AnimationTreePlayer::animation_node_get_master_animation(const StringName &p_node) const {
  976. GET_NODE_V(NODE_ANIMATION, AnimationNode, String());
  977. return n->from;
  978. }
  979. float AnimationTreePlayer::animation_node_get_position(const StringName &p_node) const {
  980. GET_NODE_V(NODE_ANIMATION, AnimationNode, 0);
  981. return n->time;
  982. }
  983. bool AnimationTreePlayer::animation_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const {
  984. GET_NODE_V(NODE_ANIMATION, AnimationNode, 0);
  985. return n->filter.has(p_path);
  986. }
  987. float AnimationTreePlayer::oneshot_node_get_fadein_time(const StringName &p_node) const {
  988. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  989. return n->fade_in;
  990. }
  991. float AnimationTreePlayer::oneshot_node_get_fadeout_time(const StringName &p_node) const {
  992. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  993. return n->fade_out;
  994. }
  995. bool AnimationTreePlayer::oneshot_node_get_mix_mode(const StringName &p_node) const {
  996. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  997. return n->mix;
  998. }
  999. bool AnimationTreePlayer::oneshot_node_has_autorestart(const StringName &p_node) const {
  1000. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  1001. return n->autorestart;
  1002. }
  1003. float AnimationTreePlayer::oneshot_node_get_autorestart_delay(const StringName &p_node) const {
  1004. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  1005. return n->autorestart_delay;
  1006. }
  1007. float AnimationTreePlayer::oneshot_node_get_autorestart_random_delay(const StringName &p_node) const {
  1008. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  1009. return n->autorestart_random_delay;
  1010. }
  1011. bool AnimationTreePlayer::oneshot_node_is_active(const StringName &p_node) const {
  1012. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  1013. return n->active;
  1014. }
  1015. bool AnimationTreePlayer::oneshot_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const {
  1016. GET_NODE_V(NODE_ONESHOT, OneShotNode, 0);
  1017. return n->filter.has(p_path);
  1018. }
  1019. float AnimationTreePlayer::mix_node_get_amount(const StringName &p_node) const {
  1020. GET_NODE_V(NODE_MIX, MixNode, 0);
  1021. return n->amount;
  1022. }
  1023. float AnimationTreePlayer::blend2_node_get_amount(const StringName &p_node) const {
  1024. GET_NODE_V(NODE_BLEND2, Blend2Node, 0);
  1025. return n->value;
  1026. }
  1027. bool AnimationTreePlayer::blend2_node_is_path_filtered(const StringName &p_node, const NodePath &p_path) const {
  1028. GET_NODE_V(NODE_BLEND2, Blend2Node, 0);
  1029. return n->filter.has(p_path);
  1030. }
  1031. float AnimationTreePlayer::blend3_node_get_amount(const StringName &p_node) const {
  1032. GET_NODE_V(NODE_BLEND3, Blend3Node, 0);
  1033. return n->value;
  1034. }
  1035. Vector2 AnimationTreePlayer::blend4_node_get_amount(const StringName &p_node) const {
  1036. GET_NODE_V(NODE_BLEND4, Blend4Node, Vector2());
  1037. return n->value;
  1038. }
  1039. float AnimationTreePlayer::timescale_node_get_scale(const StringName &p_node) const {
  1040. GET_NODE_V(NODE_TIMESCALE, TimeScaleNode, 0);
  1041. return n->scale;
  1042. }
  1043. void AnimationTreePlayer::transition_node_delete_input(const StringName &p_node, int p_input) {
  1044. GET_NODE(NODE_TRANSITION, TransitionNode);
  1045. ERR_FAIL_INDEX(p_input, n->inputs.size());
  1046. if (n->inputs.size() <= 1) {
  1047. return;
  1048. }
  1049. n->inputs.remove(p_input);
  1050. n->input_data.remove(p_input);
  1051. last_error = _cycle_test(out_name);
  1052. }
  1053. int AnimationTreePlayer::transition_node_get_input_count(const StringName &p_node) const {
  1054. GET_NODE_V(NODE_TRANSITION, TransitionNode, 0);
  1055. return n->inputs.size();
  1056. }
  1057. bool AnimationTreePlayer::transition_node_has_input_auto_advance(const StringName &p_node, int p_input) const {
  1058. GET_NODE_V(NODE_TRANSITION, TransitionNode, false);
  1059. ERR_FAIL_INDEX_V(p_input, n->inputs.size(), false);
  1060. return n->input_data[p_input].auto_advance;
  1061. }
  1062. float AnimationTreePlayer::transition_node_get_xfade_time(const StringName &p_node) const {
  1063. GET_NODE_V(NODE_TRANSITION, TransitionNode, 0);
  1064. return n->xfade;
  1065. }
  1066. int AnimationTreePlayer::transition_node_get_current(const StringName &p_node) const {
  1067. GET_NODE_V(NODE_TRANSITION, TransitionNode, -1);
  1068. return n->current;
  1069. }
  1070. /*misc */
  1071. void AnimationTreePlayer::get_node_list(List<StringName> *p_node_list) const {
  1072. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  1073. p_node_list->push_back(E->key());
  1074. }
  1075. }
  1076. void AnimationTreePlayer::remove_node(const StringName &p_node) {
  1077. ERR_FAIL_COND(!node_map.has(p_node));
  1078. ERR_FAIL_COND_MSG(p_node == out_name, "Node 0 (output) can't be removed.");
  1079. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  1080. NodeBase *nb = E->get();
  1081. for (int i = 0; i < nb->inputs.size(); i++) {
  1082. if (nb->inputs[i].node == p_node) {
  1083. nb->inputs.write[i].node = StringName();
  1084. }
  1085. }
  1086. }
  1087. memdelete(node_map[p_node]);
  1088. node_map.erase(p_node);
  1089. _clear_cycle_test();
  1090. // compute last error again, just in case
  1091. last_error = _cycle_test(out_name);
  1092. dirty_caches = true;
  1093. }
  1094. AnimationTreePlayer::ConnectError AnimationTreePlayer::_cycle_test(const StringName &p_at_node) {
  1095. ERR_FAIL_COND_V(!node_map.has(p_at_node), CONNECT_INCOMPLETE);
  1096. NodeBase *nb = node_map[p_at_node];
  1097. if (nb->cycletest) {
  1098. return CONNECT_CYCLE;
  1099. }
  1100. nb->cycletest = true;
  1101. for (int i = 0; i < nb->inputs.size(); i++) {
  1102. if (nb->inputs[i].node == StringName()) {
  1103. return CONNECT_INCOMPLETE;
  1104. }
  1105. ConnectError _err = _cycle_test(nb->inputs[i].node);
  1106. if (_err) {
  1107. return _err;
  1108. }
  1109. }
  1110. return CONNECT_OK;
  1111. }
  1112. // Use this function to not alter next complete _cycle_test().
  1113. void AnimationTreePlayer::_clear_cycle_test() {
  1114. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  1115. NodeBase *nb = E->get();
  1116. nb->cycletest = false;
  1117. }
  1118. }
  1119. Error AnimationTreePlayer::connect_nodes(const StringName &p_src_node, const StringName &p_dst_node, int p_dst_input) {
  1120. ERR_FAIL_COND_V(!node_map.has(p_src_node), ERR_INVALID_PARAMETER);
  1121. ERR_FAIL_COND_V(!node_map.has(p_dst_node), ERR_INVALID_PARAMETER);
  1122. ERR_FAIL_COND_V(p_src_node == p_dst_node, ERR_INVALID_PARAMETER);
  1123. //NodeBase *src = node_map[p_src_node];
  1124. NodeBase *dst = node_map[p_dst_node];
  1125. ERR_FAIL_INDEX_V(p_dst_input, dst->inputs.size(), ERR_INVALID_PARAMETER);
  1126. //int oldval = dst->inputs[p_dst_input].node;
  1127. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  1128. NodeBase *nb = E->get();
  1129. for (int i = 0; i < nb->inputs.size(); i++) {
  1130. if (nb->inputs[i].node == p_src_node) {
  1131. nb->inputs.write[i].node = StringName();
  1132. }
  1133. }
  1134. }
  1135. dst->inputs.write[p_dst_input].node = p_src_node;
  1136. _clear_cycle_test();
  1137. last_error = _cycle_test(out_name);
  1138. if (last_error) {
  1139. if (last_error == CONNECT_INCOMPLETE) {
  1140. return ERR_UNCONFIGURED;
  1141. } else if (last_error == CONNECT_CYCLE) {
  1142. return ERR_CYCLIC_LINK;
  1143. }
  1144. }
  1145. dirty_caches = true;
  1146. return OK;
  1147. }
  1148. bool AnimationTreePlayer::are_nodes_connected(const StringName &p_src_node, const StringName &p_dst_node, int p_dst_input) const {
  1149. ERR_FAIL_COND_V(!node_map.has(p_src_node), false);
  1150. ERR_FAIL_COND_V(!node_map.has(p_dst_node), false);
  1151. ERR_FAIL_COND_V(p_src_node == p_dst_node, false);
  1152. NodeBase *dst = node_map[p_dst_node];
  1153. ERR_FAIL_INDEX_V(p_dst_input, dst->inputs.size(), false);
  1154. return dst->inputs[p_dst_input].node == p_src_node;
  1155. }
  1156. void AnimationTreePlayer::disconnect_nodes(const StringName &p_node, int p_input) {
  1157. ERR_FAIL_COND(!node_map.has(p_node));
  1158. NodeBase *dst = node_map[p_node];
  1159. ERR_FAIL_INDEX(p_input, dst->inputs.size());
  1160. dst->inputs.write[p_input].node = StringName();
  1161. last_error = CONNECT_INCOMPLETE;
  1162. dirty_caches = true;
  1163. }
  1164. void AnimationTreePlayer::get_connection_list(List<Connection> *p_connections) const {
  1165. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  1166. NodeBase *nb = E->get();
  1167. for (int i = 0; i < nb->inputs.size(); i++) {
  1168. if (nb->inputs[i].node != StringName()) {
  1169. Connection c;
  1170. c.src_node = nb->inputs[i].node;
  1171. c.dst_node = E->key();
  1172. c.dst_input = i;
  1173. p_connections->push_back(c);
  1174. }
  1175. }
  1176. }
  1177. }
  1178. AnimationTreePlayer::Track *AnimationTreePlayer::_find_track(const NodePath &p_path) {
  1179. Node *parent = get_node(base_path);
  1180. ERR_FAIL_COND_V(!parent, nullptr);
  1181. RES resource;
  1182. Vector<StringName> leftover_path;
  1183. Node *child = parent->get_node_and_resource(p_path, resource, leftover_path);
  1184. if (!child) {
  1185. String err = "Animation track references unknown Node: '" + String(p_path) + "'.";
  1186. WARN_PRINT(err.ascii().get_data());
  1187. return nullptr;
  1188. }
  1189. ObjectID id = child->get_instance_id();
  1190. int bone_idx = -1;
  1191. if (p_path.get_subname_count()) {
  1192. if (Object::cast_to<Skeleton>(child)) {
  1193. bone_idx = Object::cast_to<Skeleton>(child)->find_bone(p_path.get_subname(0));
  1194. }
  1195. }
  1196. TrackKey key;
  1197. key.id = id;
  1198. key.bone_idx = bone_idx;
  1199. key.subpath_concatenated = p_path.get_concatenated_subnames();
  1200. if (!track_map.has(key)) {
  1201. Track tr;
  1202. tr.id = id;
  1203. tr.object = resource.is_valid() ? (Object *)resource.ptr() : (Object *)child;
  1204. tr.skeleton = Object::cast_to<Skeleton>(child);
  1205. tr.spatial = Object::cast_to<Spatial>(child);
  1206. tr.bone_idx = bone_idx;
  1207. if (bone_idx == -1) {
  1208. tr.subpath = leftover_path;
  1209. }
  1210. track_map[key] = tr;
  1211. }
  1212. return &track_map[key];
  1213. }
  1214. void AnimationTreePlayer::_recompute_caches() {
  1215. track_map.clear();
  1216. _recompute_caches(out_name);
  1217. dirty_caches = false;
  1218. }
  1219. void AnimationTreePlayer::_recompute_caches(const StringName &p_node) {
  1220. ERR_FAIL_COND(!node_map.has(p_node));
  1221. NodeBase *nb = node_map[p_node];
  1222. if (nb->type == NODE_ANIMATION) {
  1223. AnimationNode *an = static_cast<AnimationNode *>(nb);
  1224. an->tref.clear();
  1225. if (!an->animation.is_null()) {
  1226. Ref<Animation> a = an->animation;
  1227. for (int i = 0; i < an->animation->get_track_count(); i++) {
  1228. Track *tr = _find_track(a->track_get_path(i));
  1229. if (!tr) {
  1230. continue;
  1231. }
  1232. AnimationNode::TrackRef tref;
  1233. tref.local_track = i;
  1234. tref.track = tr;
  1235. tref.weight = 0;
  1236. an->tref.push_back(tref);
  1237. }
  1238. }
  1239. }
  1240. for (int i = 0; i < nb->inputs.size(); i++) {
  1241. _recompute_caches(nb->inputs[i].node);
  1242. }
  1243. }
  1244. void AnimationTreePlayer::recompute_caches() {
  1245. dirty_caches = true;
  1246. }
  1247. /* playback */
  1248. void AnimationTreePlayer::set_active(bool p_active) {
  1249. if (active == p_active) {
  1250. return;
  1251. }
  1252. active = p_active;
  1253. processing = active;
  1254. reset_request = p_active;
  1255. _set_process(processing, true);
  1256. }
  1257. bool AnimationTreePlayer::is_active() const {
  1258. return active;
  1259. }
  1260. AnimationTreePlayer::ConnectError AnimationTreePlayer::get_last_error() const {
  1261. return last_error;
  1262. }
  1263. void AnimationTreePlayer::reset() {
  1264. reset_request = true;
  1265. }
  1266. void AnimationTreePlayer::set_base_path(const NodePath &p_path) {
  1267. base_path = p_path;
  1268. recompute_caches();
  1269. }
  1270. NodePath AnimationTreePlayer::get_base_path() const {
  1271. return base_path;
  1272. }
  1273. void AnimationTreePlayer::set_master_player(const NodePath &p_path) {
  1274. if (p_path == master) {
  1275. return;
  1276. }
  1277. master = p_path;
  1278. _update_sources();
  1279. recompute_caches();
  1280. }
  1281. NodePath AnimationTreePlayer::get_master_player() const {
  1282. return master;
  1283. }
  1284. PoolVector<String> AnimationTreePlayer::_get_node_list() {
  1285. List<StringName> nl;
  1286. get_node_list(&nl);
  1287. PoolVector<String> ret;
  1288. ret.resize(nl.size());
  1289. int idx = 0;
  1290. for (List<StringName>::Element *E = nl.front(); E; E = E->next()) {
  1291. ret.set(idx++, E->get());
  1292. }
  1293. return ret;
  1294. }
  1295. void AnimationTreePlayer::_update_sources() {
  1296. if (master == NodePath()) {
  1297. return;
  1298. }
  1299. if (!is_inside_tree()) {
  1300. return;
  1301. }
  1302. Node *m = get_node(master);
  1303. if (!m) {
  1304. master = NodePath();
  1305. ERR_FAIL_COND(!m);
  1306. }
  1307. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(m);
  1308. if (!ap) {
  1309. master = NodePath();
  1310. ERR_FAIL_COND(!ap);
  1311. }
  1312. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  1313. if (E->get()->type == NODE_ANIMATION) {
  1314. AnimationNode *an = static_cast<AnimationNode *>(E->get());
  1315. if (an->from != "") {
  1316. an->animation = ap->get_animation(an->from);
  1317. }
  1318. }
  1319. }
  1320. }
  1321. bool AnimationTreePlayer::node_exists(const StringName &p_name) const {
  1322. return (node_map.has(p_name));
  1323. }
  1324. Error AnimationTreePlayer::node_rename(const StringName &p_node, const StringName &p_new_name) {
  1325. if (p_new_name == p_node) {
  1326. return OK;
  1327. }
  1328. ERR_FAIL_COND_V(!node_map.has(p_node), ERR_ALREADY_EXISTS);
  1329. ERR_FAIL_COND_V(node_map.has(p_new_name), ERR_ALREADY_EXISTS);
  1330. ERR_FAIL_COND_V(p_new_name == StringName(), ERR_INVALID_DATA);
  1331. ERR_FAIL_COND_V(p_node == out_name, ERR_INVALID_DATA);
  1332. ERR_FAIL_COND_V(p_new_name == out_name, ERR_INVALID_DATA);
  1333. for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) {
  1334. NodeBase *nb = E->get();
  1335. for (int i = 0; i < nb->inputs.size(); i++) {
  1336. if (nb->inputs[i].node == p_node) {
  1337. nb->inputs.write[i].node = p_new_name;
  1338. }
  1339. }
  1340. }
  1341. node_map[p_new_name] = node_map[p_node];
  1342. node_map.erase(p_node);
  1343. return OK;
  1344. }
  1345. String AnimationTreePlayer::get_configuration_warning() const {
  1346. return TTR("This node has been deprecated. Use AnimationTree instead.");
  1347. }
  1348. void AnimationTreePlayer::_bind_methods() {
  1349. ClassDB::bind_method(D_METHOD("add_node", "type", "id"), &AnimationTreePlayer::add_node);
  1350. ClassDB::bind_method(D_METHOD("node_exists", "node"), &AnimationTreePlayer::node_exists);
  1351. ClassDB::bind_method(D_METHOD("node_rename", "node", "new_name"), &AnimationTreePlayer::node_rename);
  1352. ClassDB::bind_method(D_METHOD("node_get_type", "id"), &AnimationTreePlayer::node_get_type);
  1353. ClassDB::bind_method(D_METHOD("node_get_input_count", "id"), &AnimationTreePlayer::node_get_input_count);
  1354. ClassDB::bind_method(D_METHOD("node_get_input_source", "id", "idx"), &AnimationTreePlayer::node_get_input_source);
  1355. ClassDB::bind_method(D_METHOD("animation_node_set_animation", "id", "animation"), &AnimationTreePlayer::animation_node_set_animation);
  1356. ClassDB::bind_method(D_METHOD("animation_node_get_animation", "id"), &AnimationTreePlayer::animation_node_get_animation);
  1357. ClassDB::bind_method(D_METHOD("animation_node_set_master_animation", "id", "source"), &AnimationTreePlayer::animation_node_set_master_animation);
  1358. ClassDB::bind_method(D_METHOD("animation_node_get_master_animation", "id"), &AnimationTreePlayer::animation_node_get_master_animation);
  1359. ClassDB::bind_method(D_METHOD("animation_node_get_position", "id"), &AnimationTreePlayer::animation_node_get_position);
  1360. ClassDB::bind_method(D_METHOD("animation_node_set_filter_path", "id", "path", "enable"), &AnimationTreePlayer::animation_node_set_filter_path);
  1361. ClassDB::bind_method(D_METHOD("oneshot_node_set_fadein_time", "id", "time_sec"), &AnimationTreePlayer::oneshot_node_set_fadein_time);
  1362. ClassDB::bind_method(D_METHOD("oneshot_node_get_fadein_time", "id"), &AnimationTreePlayer::oneshot_node_get_fadein_time);
  1363. ClassDB::bind_method(D_METHOD("oneshot_node_set_fadeout_time", "id", "time_sec"), &AnimationTreePlayer::oneshot_node_set_fadeout_time);
  1364. ClassDB::bind_method(D_METHOD("oneshot_node_get_fadeout_time", "id"), &AnimationTreePlayer::oneshot_node_get_fadeout_time);
  1365. ClassDB::bind_method(D_METHOD("oneshot_node_set_autorestart", "id", "enable"), &AnimationTreePlayer::oneshot_node_set_autorestart);
  1366. ClassDB::bind_method(D_METHOD("oneshot_node_set_autorestart_delay", "id", "delay_sec"), &AnimationTreePlayer::oneshot_node_set_autorestart_delay);
  1367. ClassDB::bind_method(D_METHOD("oneshot_node_set_autorestart_random_delay", "id", "rand_sec"), &AnimationTreePlayer::oneshot_node_set_autorestart_random_delay);
  1368. ClassDB::bind_method(D_METHOD("oneshot_node_has_autorestart", "id"), &AnimationTreePlayer::oneshot_node_has_autorestart);
  1369. ClassDB::bind_method(D_METHOD("oneshot_node_get_autorestart_delay", "id"), &AnimationTreePlayer::oneshot_node_get_autorestart_delay);
  1370. ClassDB::bind_method(D_METHOD("oneshot_node_get_autorestart_random_delay", "id"), &AnimationTreePlayer::oneshot_node_get_autorestart_random_delay);
  1371. ClassDB::bind_method(D_METHOD("oneshot_node_start", "id"), &AnimationTreePlayer::oneshot_node_start);
  1372. ClassDB::bind_method(D_METHOD("oneshot_node_stop", "id"), &AnimationTreePlayer::oneshot_node_stop);
  1373. ClassDB::bind_method(D_METHOD("oneshot_node_is_active", "id"), &AnimationTreePlayer::oneshot_node_is_active);
  1374. ClassDB::bind_method(D_METHOD("oneshot_node_set_filter_path", "id", "path", "enable"), &AnimationTreePlayer::oneshot_node_set_filter_path);
  1375. ClassDB::bind_method(D_METHOD("mix_node_set_amount", "id", "ratio"), &AnimationTreePlayer::mix_node_set_amount);
  1376. ClassDB::bind_method(D_METHOD("mix_node_get_amount", "id"), &AnimationTreePlayer::mix_node_get_amount);
  1377. ClassDB::bind_method(D_METHOD("blend2_node_set_amount", "id", "blend"), &AnimationTreePlayer::blend2_node_set_amount);
  1378. ClassDB::bind_method(D_METHOD("blend2_node_get_amount", "id"), &AnimationTreePlayer::blend2_node_get_amount);
  1379. ClassDB::bind_method(D_METHOD("blend2_node_set_filter_path", "id", "path", "enable"), &AnimationTreePlayer::blend2_node_set_filter_path);
  1380. ClassDB::bind_method(D_METHOD("blend3_node_set_amount", "id", "blend"), &AnimationTreePlayer::blend3_node_set_amount);
  1381. ClassDB::bind_method(D_METHOD("blend3_node_get_amount", "id"), &AnimationTreePlayer::blend3_node_get_amount);
  1382. ClassDB::bind_method(D_METHOD("blend4_node_set_amount", "id", "blend"), &AnimationTreePlayer::blend4_node_set_amount);
  1383. ClassDB::bind_method(D_METHOD("blend4_node_get_amount", "id"), &AnimationTreePlayer::blend4_node_get_amount);
  1384. ClassDB::bind_method(D_METHOD("timescale_node_set_scale", "id", "scale"), &AnimationTreePlayer::timescale_node_set_scale);
  1385. ClassDB::bind_method(D_METHOD("timescale_node_get_scale", "id"), &AnimationTreePlayer::timescale_node_get_scale);
  1386. ClassDB::bind_method(D_METHOD("timeseek_node_seek", "id", "seconds"), &AnimationTreePlayer::timeseek_node_seek);
  1387. ClassDB::bind_method(D_METHOD("transition_node_set_input_count", "id", "count"), &AnimationTreePlayer::transition_node_set_input_count);
  1388. ClassDB::bind_method(D_METHOD("transition_node_get_input_count", "id"), &AnimationTreePlayer::transition_node_get_input_count);
  1389. ClassDB::bind_method(D_METHOD("transition_node_delete_input", "id", "input_idx"), &AnimationTreePlayer::transition_node_delete_input);
  1390. ClassDB::bind_method(D_METHOD("transition_node_set_input_auto_advance", "id", "input_idx", "enable"), &AnimationTreePlayer::transition_node_set_input_auto_advance);
  1391. ClassDB::bind_method(D_METHOD("transition_node_has_input_auto_advance", "id", "input_idx"), &AnimationTreePlayer::transition_node_has_input_auto_advance);
  1392. ClassDB::bind_method(D_METHOD("transition_node_set_xfade_time", "id", "time_sec"), &AnimationTreePlayer::transition_node_set_xfade_time);
  1393. ClassDB::bind_method(D_METHOD("transition_node_get_xfade_time", "id"), &AnimationTreePlayer::transition_node_get_xfade_time);
  1394. ClassDB::bind_method(D_METHOD("transition_node_set_current", "id", "input_idx"), &AnimationTreePlayer::transition_node_set_current);
  1395. ClassDB::bind_method(D_METHOD("transition_node_get_current", "id"), &AnimationTreePlayer::transition_node_get_current);
  1396. ClassDB::bind_method(D_METHOD("node_set_position", "id", "screen_position"), &AnimationTreePlayer::node_set_position);
  1397. ClassDB::bind_method(D_METHOD("node_get_position", "id"), &AnimationTreePlayer::node_get_position);
  1398. ClassDB::bind_method(D_METHOD("remove_node", "id"), &AnimationTreePlayer::remove_node);
  1399. ClassDB::bind_method(D_METHOD("connect_nodes", "id", "dst_id", "dst_input_idx"), &AnimationTreePlayer::connect_nodes);
  1400. ClassDB::bind_method(D_METHOD("are_nodes_connected", "id", "dst_id", "dst_input_idx"), &AnimationTreePlayer::are_nodes_connected);
  1401. ClassDB::bind_method(D_METHOD("disconnect_nodes", "id", "dst_input_idx"), &AnimationTreePlayer::disconnect_nodes);
  1402. ClassDB::bind_method(D_METHOD("set_active", "enabled"), &AnimationTreePlayer::set_active);
  1403. ClassDB::bind_method(D_METHOD("is_active"), &AnimationTreePlayer::is_active);
  1404. ClassDB::bind_method(D_METHOD("set_base_path", "path"), &AnimationTreePlayer::set_base_path);
  1405. ClassDB::bind_method(D_METHOD("get_base_path"), &AnimationTreePlayer::get_base_path);
  1406. ClassDB::bind_method(D_METHOD("set_master_player", "nodepath"), &AnimationTreePlayer::set_master_player);
  1407. ClassDB::bind_method(D_METHOD("get_master_player"), &AnimationTreePlayer::get_master_player);
  1408. ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationTreePlayer::_get_node_list);
  1409. ClassDB::bind_method(D_METHOD("set_animation_process_mode", "mode"), &AnimationTreePlayer::set_animation_process_mode);
  1410. ClassDB::bind_method(D_METHOD("get_animation_process_mode"), &AnimationTreePlayer::get_animation_process_mode);
  1411. ClassDB::bind_method(D_METHOD("advance", "delta"), &AnimationTreePlayer::advance);
  1412. ClassDB::bind_method(D_METHOD("reset"), &AnimationTreePlayer::reset);
  1413. ClassDB::bind_method(D_METHOD("recompute_caches"), &AnimationTreePlayer::recompute_caches);
  1414. ADD_GROUP("Playback", "playback_");
  1415. ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_animation_process_mode", "get_animation_process_mode");
  1416. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "master_player", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "AnimationPlayer"), "set_master_player", "get_master_player");
  1417. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "base_path"), "set_base_path", "get_base_path");
  1418. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "is_active");
  1419. BIND_ENUM_CONSTANT(NODE_OUTPUT);
  1420. BIND_ENUM_CONSTANT(NODE_ANIMATION);
  1421. BIND_ENUM_CONSTANT(NODE_ONESHOT);
  1422. BIND_ENUM_CONSTANT(NODE_MIX);
  1423. BIND_ENUM_CONSTANT(NODE_BLEND2);
  1424. BIND_ENUM_CONSTANT(NODE_BLEND3);
  1425. BIND_ENUM_CONSTANT(NODE_BLEND4);
  1426. BIND_ENUM_CONSTANT(NODE_TIMESCALE);
  1427. BIND_ENUM_CONSTANT(NODE_TIMESEEK);
  1428. BIND_ENUM_CONSTANT(NODE_TRANSITION);
  1429. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS);
  1430. BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE);
  1431. }
  1432. AnimationTreePlayer::AnimationTreePlayer() {
  1433. active_list = nullptr;
  1434. out = memnew(NodeOut);
  1435. out_name = "out";
  1436. out->pos = Point2(40, 40);
  1437. node_map.insert(out_name, out);
  1438. animation_process_mode = ANIMATION_PROCESS_IDLE;
  1439. processing = false;
  1440. active = false;
  1441. dirty_caches = true;
  1442. reset_request = true;
  1443. last_error = CONNECT_INCOMPLETE;
  1444. base_path = String("..");
  1445. }
  1446. AnimationTreePlayer::~AnimationTreePlayer() {
  1447. while (node_map.size()) {
  1448. memdelete(node_map.front()->get());
  1449. node_map.erase(node_map.front());
  1450. }
  1451. }