animation_tree_player.cpp 55 KB

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