animation_tree_player.cpp 55 KB

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