animation_tree_player.cpp 55 KB

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