animation_tree_player.cpp 55 KB

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