animation_blend_tree.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*************************************************************************/
  2. /* animation_blend_tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_blend_tree.h"
  31. #include "scene/scene_string_names.h"
  32. void AnimationNodeAnimation::set_animation(const StringName &p_name) {
  33. animation = p_name;
  34. }
  35. StringName AnimationNodeAnimation::get_animation() const {
  36. return animation;
  37. }
  38. Vector<String> (*AnimationNodeAnimation::get_editable_animation_list)() = nullptr;
  39. void AnimationNodeAnimation::get_parameter_list(List<PropertyInfo> *r_list) const {
  40. r_list->push_back(PropertyInfo(Variant::FLOAT, time, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  41. }
  42. void AnimationNodeAnimation::_validate_property(PropertyInfo &property) const {
  43. if (property.name == "animation" && get_editable_animation_list) {
  44. Vector<String> names = get_editable_animation_list();
  45. String anims;
  46. for (int i = 0; i < names.size(); i++) {
  47. if (i > 0) {
  48. anims += ",";
  49. }
  50. anims += String(names[i]);
  51. }
  52. if (anims != String()) {
  53. property.hint = PROPERTY_HINT_ENUM;
  54. property.hint_string = anims;
  55. }
  56. }
  57. }
  58. double AnimationNodeAnimation::process(double p_time, bool p_seek) {
  59. AnimationPlayer *ap = state->player;
  60. ERR_FAIL_COND_V(!ap, 0);
  61. double time = get_parameter(this->time);
  62. if (!ap->has_animation(animation)) {
  63. AnimationNodeBlendTree *tree = Object::cast_to<AnimationNodeBlendTree>(parent);
  64. if (tree) {
  65. String name = tree->get_node_name(Ref<AnimationNodeAnimation>(this));
  66. make_invalid(vformat(RTR("On BlendTree node '%s', animation not found: '%s'"), name, animation));
  67. } else {
  68. make_invalid(vformat(RTR("Animation not found: '%s'"), animation));
  69. }
  70. return 0;
  71. }
  72. Ref<Animation> anim = ap->get_animation(animation);
  73. double step;
  74. if (p_seek) {
  75. time = p_time;
  76. step = 0;
  77. } else {
  78. time = MAX(0, time + p_time);
  79. step = p_time;
  80. }
  81. double anim_size = anim->get_length();
  82. if (anim->has_loop()) {
  83. if (anim_size) {
  84. time = Math::fposmod(time, anim_size);
  85. }
  86. } else if (time > anim_size) {
  87. time = anim_size;
  88. }
  89. blend_animation(animation, time, step, p_seek, 1.0);
  90. set_parameter(this->time, time);
  91. return anim_size - time;
  92. }
  93. String AnimationNodeAnimation::get_caption() const {
  94. return "Animation";
  95. }
  96. void AnimationNodeAnimation::_bind_methods() {
  97. ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimationNodeAnimation::set_animation);
  98. ClassDB::bind_method(D_METHOD("get_animation"), &AnimationNodeAnimation::get_animation);
  99. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
  100. }
  101. AnimationNodeAnimation::AnimationNodeAnimation() {
  102. }
  103. ////////////////////////////////////////////////////////
  104. void AnimationNodeOneShot::get_parameter_list(List<PropertyInfo> *r_list) const {
  105. r_list->push_back(PropertyInfo(Variant::BOOL, active));
  106. r_list->push_back(PropertyInfo(Variant::BOOL, prev_active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  107. r_list->push_back(PropertyInfo(Variant::FLOAT, time, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  108. r_list->push_back(PropertyInfo(Variant::FLOAT, remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  109. r_list->push_back(PropertyInfo(Variant::FLOAT, time_to_restart, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  110. }
  111. Variant AnimationNodeOneShot::get_parameter_default_value(const StringName &p_parameter) const {
  112. if (p_parameter == active || p_parameter == prev_active) {
  113. return false;
  114. } else if (p_parameter == time_to_restart) {
  115. return -1;
  116. } else {
  117. return 0.0;
  118. }
  119. }
  120. void AnimationNodeOneShot::set_fadein_time(float p_time) {
  121. fade_in = p_time;
  122. }
  123. void AnimationNodeOneShot::set_fadeout_time(float p_time) {
  124. fade_out = p_time;
  125. }
  126. float AnimationNodeOneShot::get_fadein_time() const {
  127. return fade_in;
  128. }
  129. float AnimationNodeOneShot::get_fadeout_time() const {
  130. return fade_out;
  131. }
  132. void AnimationNodeOneShot::set_autorestart(bool p_active) {
  133. autorestart = p_active;
  134. }
  135. void AnimationNodeOneShot::set_autorestart_delay(float p_time) {
  136. autorestart_delay = p_time;
  137. }
  138. void AnimationNodeOneShot::set_autorestart_random_delay(float p_time) {
  139. autorestart_random_delay = p_time;
  140. }
  141. bool AnimationNodeOneShot::has_autorestart() const {
  142. return autorestart;
  143. }
  144. float AnimationNodeOneShot::get_autorestart_delay() const {
  145. return autorestart_delay;
  146. }
  147. float AnimationNodeOneShot::get_autorestart_random_delay() const {
  148. return autorestart_random_delay;
  149. }
  150. void AnimationNodeOneShot::set_mix_mode(MixMode p_mix) {
  151. mix = p_mix;
  152. }
  153. AnimationNodeOneShot::MixMode AnimationNodeOneShot::get_mix_mode() const {
  154. return mix;
  155. }
  156. String AnimationNodeOneShot::get_caption() const {
  157. return "OneShot";
  158. }
  159. bool AnimationNodeOneShot::has_filter() const {
  160. return true;
  161. }
  162. double AnimationNodeOneShot::process(double p_time, bool p_seek) {
  163. bool active = get_parameter(this->active);
  164. bool prev_active = get_parameter(this->prev_active);
  165. double time = get_parameter(this->time);
  166. double remaining = get_parameter(this->remaining);
  167. double time_to_restart = get_parameter(this->time_to_restart);
  168. if (!active) {
  169. //make it as if this node doesn't exist, pass input 0 by.
  170. if (prev_active) {
  171. set_parameter(this->prev_active, false);
  172. }
  173. if (time_to_restart >= 0.0 && !p_seek) {
  174. time_to_restart -= p_time;
  175. if (time_to_restart < 0) {
  176. //restart
  177. set_parameter(this->active, true);
  178. active = true;
  179. }
  180. set_parameter(this->time_to_restart, time_to_restart);
  181. }
  182. if (!active) {
  183. return blend_input(0, p_time, p_seek, 1.0, FILTER_IGNORE, !sync);
  184. }
  185. }
  186. bool os_seek = p_seek;
  187. if (p_seek) {
  188. time = p_time;
  189. }
  190. bool do_start = !prev_active;
  191. if (do_start) {
  192. time = 0;
  193. os_seek = true;
  194. set_parameter(this->prev_active, true);
  195. }
  196. float blend;
  197. if (time < fade_in) {
  198. if (fade_in > 0) {
  199. blend = time / fade_in;
  200. } else {
  201. blend = 0;
  202. }
  203. } else if (!do_start && remaining <= fade_out) {
  204. if (fade_out > 0) {
  205. blend = (remaining / fade_out);
  206. } else {
  207. blend = 0;
  208. }
  209. } else {
  210. blend = 1.0;
  211. }
  212. double main_rem;
  213. if (mix == MIX_MODE_ADD) {
  214. main_rem = blend_input(0, p_time, p_seek, 1.0, FILTER_IGNORE, !sync);
  215. } else {
  216. main_rem = blend_input(0, p_time, p_seek, 1.0 - blend, FILTER_BLEND, !sync);
  217. }
  218. double os_rem = blend_input(1, os_seek ? time : p_time, os_seek, blend, FILTER_PASS, false);
  219. if (do_start) {
  220. remaining = os_rem;
  221. }
  222. if (!p_seek) {
  223. time += p_time;
  224. remaining = os_rem;
  225. if (remaining <= 0) {
  226. set_parameter(this->active, false);
  227. set_parameter(this->prev_active, false);
  228. if (autorestart) {
  229. float restart_sec = autorestart_delay + Math::randf() * autorestart_random_delay;
  230. set_parameter(this->time_to_restart, restart_sec);
  231. }
  232. }
  233. }
  234. set_parameter(this->time, time);
  235. set_parameter(this->remaining, remaining);
  236. return MAX(main_rem, remaining);
  237. }
  238. void AnimationNodeOneShot::set_use_sync(bool p_sync) {
  239. sync = p_sync;
  240. }
  241. bool AnimationNodeOneShot::is_using_sync() const {
  242. return sync;
  243. }
  244. void AnimationNodeOneShot::_bind_methods() {
  245. ClassDB::bind_method(D_METHOD("set_fadein_time", "time"), &AnimationNodeOneShot::set_fadein_time);
  246. ClassDB::bind_method(D_METHOD("get_fadein_time"), &AnimationNodeOneShot::get_fadein_time);
  247. ClassDB::bind_method(D_METHOD("set_fadeout_time", "time"), &AnimationNodeOneShot::set_fadeout_time);
  248. ClassDB::bind_method(D_METHOD("get_fadeout_time"), &AnimationNodeOneShot::get_fadeout_time);
  249. ClassDB::bind_method(D_METHOD("set_autorestart", "enable"), &AnimationNodeOneShot::set_autorestart);
  250. ClassDB::bind_method(D_METHOD("has_autorestart"), &AnimationNodeOneShot::has_autorestart);
  251. ClassDB::bind_method(D_METHOD("set_autorestart_delay", "enable"), &AnimationNodeOneShot::set_autorestart_delay);
  252. ClassDB::bind_method(D_METHOD("get_autorestart_delay"), &AnimationNodeOneShot::get_autorestart_delay);
  253. ClassDB::bind_method(D_METHOD("set_autorestart_random_delay", "enable"), &AnimationNodeOneShot::set_autorestart_random_delay);
  254. ClassDB::bind_method(D_METHOD("get_autorestart_random_delay"), &AnimationNodeOneShot::get_autorestart_random_delay);
  255. ClassDB::bind_method(D_METHOD("set_mix_mode", "mode"), &AnimationNodeOneShot::set_mix_mode);
  256. ClassDB::bind_method(D_METHOD("get_mix_mode"), &AnimationNodeOneShot::get_mix_mode);
  257. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeOneShot::set_use_sync);
  258. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeOneShot::is_using_sync);
  259. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadein_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater"), "set_fadein_time", "get_fadein_time");
  260. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadeout_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater"), "set_fadeout_time", "get_fadeout_time");
  261. ADD_GROUP("autorestart_", "Auto Restart");
  262. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autorestart"), "set_autorestart", "has_autorestart");
  263. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater"), "set_autorestart_delay", "get_autorestart_delay");
  264. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_random_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater"), "set_autorestart_random_delay", "get_autorestart_random_delay");
  265. ADD_GROUP("", "");
  266. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
  267. BIND_ENUM_CONSTANT(MIX_MODE_BLEND);
  268. BIND_ENUM_CONSTANT(MIX_MODE_ADD);
  269. }
  270. AnimationNodeOneShot::AnimationNodeOneShot() {
  271. add_input("in");
  272. add_input("shot");
  273. }
  274. ////////////////////////////////////////////////
  275. void AnimationNodeAdd2::get_parameter_list(List<PropertyInfo> *r_list) const {
  276. r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "0,1,0.01"));
  277. }
  278. Variant AnimationNodeAdd2::get_parameter_default_value(const StringName &p_parameter) const {
  279. return 0;
  280. }
  281. String AnimationNodeAdd2::get_caption() const {
  282. return "Add2";
  283. }
  284. void AnimationNodeAdd2::set_use_sync(bool p_sync) {
  285. sync = p_sync;
  286. }
  287. bool AnimationNodeAdd2::is_using_sync() const {
  288. return sync;
  289. }
  290. bool AnimationNodeAdd2::has_filter() const {
  291. return true;
  292. }
  293. double AnimationNodeAdd2::process(double p_time, bool p_seek) {
  294. double amount = get_parameter(add_amount);
  295. double rem0 = blend_input(0, p_time, p_seek, 1.0, FILTER_IGNORE, !sync);
  296. blend_input(1, p_time, p_seek, amount, FILTER_PASS, !sync);
  297. return rem0;
  298. }
  299. void AnimationNodeAdd2::_bind_methods() {
  300. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeAdd2::set_use_sync);
  301. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeAdd2::is_using_sync);
  302. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
  303. }
  304. AnimationNodeAdd2::AnimationNodeAdd2() {
  305. add_input("in");
  306. add_input("add");
  307. }
  308. ////////////////////////////////////////////////
  309. void AnimationNodeAdd3::get_parameter_list(List<PropertyInfo> *r_list) const {
  310. r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "-1,1,0.01"));
  311. }
  312. Variant AnimationNodeAdd3::get_parameter_default_value(const StringName &p_parameter) const {
  313. return 0;
  314. }
  315. String AnimationNodeAdd3::get_caption() const {
  316. return "Add3";
  317. }
  318. void AnimationNodeAdd3::set_use_sync(bool p_sync) {
  319. sync = p_sync;
  320. }
  321. bool AnimationNodeAdd3::is_using_sync() const {
  322. return sync;
  323. }
  324. bool AnimationNodeAdd3::has_filter() const {
  325. return true;
  326. }
  327. double AnimationNodeAdd3::process(double p_time, bool p_seek) {
  328. double amount = get_parameter(add_amount);
  329. blend_input(0, p_time, p_seek, MAX(0, -amount), FILTER_PASS, !sync);
  330. double rem0 = blend_input(1, p_time, p_seek, 1.0, FILTER_IGNORE, !sync);
  331. blend_input(2, p_time, p_seek, MAX(0, amount), FILTER_PASS, !sync);
  332. return rem0;
  333. }
  334. void AnimationNodeAdd3::_bind_methods() {
  335. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeAdd3::set_use_sync);
  336. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeAdd3::is_using_sync);
  337. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
  338. }
  339. AnimationNodeAdd3::AnimationNodeAdd3() {
  340. add_input("-add");
  341. add_input("in");
  342. add_input("+add");
  343. }
  344. /////////////////////////////////////////////
  345. void AnimationNodeBlend2::get_parameter_list(List<PropertyInfo> *r_list) const {
  346. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "0,1,0.01"));
  347. }
  348. Variant AnimationNodeBlend2::get_parameter_default_value(const StringName &p_parameter) const {
  349. return 0; //for blend amount
  350. }
  351. String AnimationNodeBlend2::get_caption() const {
  352. return "Blend2";
  353. }
  354. double AnimationNodeBlend2::process(double p_time, bool p_seek) {
  355. double amount = get_parameter(blend_amount);
  356. double rem0 = blend_input(0, p_time, p_seek, 1.0 - amount, FILTER_BLEND, !sync);
  357. double rem1 = blend_input(1, p_time, p_seek, amount, FILTER_PASS, !sync);
  358. return amount > 0.5 ? rem1 : rem0; //hacky but good enough
  359. }
  360. void AnimationNodeBlend2::set_use_sync(bool p_sync) {
  361. sync = p_sync;
  362. }
  363. bool AnimationNodeBlend2::is_using_sync() const {
  364. return sync;
  365. }
  366. bool AnimationNodeBlend2::has_filter() const {
  367. return true;
  368. }
  369. void AnimationNodeBlend2::_bind_methods() {
  370. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeBlend2::set_use_sync);
  371. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeBlend2::is_using_sync);
  372. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
  373. }
  374. AnimationNodeBlend2::AnimationNodeBlend2() {
  375. add_input("in");
  376. add_input("blend");
  377. }
  378. //////////////////////////////////////
  379. void AnimationNodeBlend3::get_parameter_list(List<PropertyInfo> *r_list) const {
  380. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "-1,1,0.01"));
  381. }
  382. Variant AnimationNodeBlend3::get_parameter_default_value(const StringName &p_parameter) const {
  383. return 0; //for blend amount
  384. }
  385. String AnimationNodeBlend3::get_caption() const {
  386. return "Blend3";
  387. }
  388. void AnimationNodeBlend3::set_use_sync(bool p_sync) {
  389. sync = p_sync;
  390. }
  391. bool AnimationNodeBlend3::is_using_sync() const {
  392. return sync;
  393. }
  394. double AnimationNodeBlend3::process(double p_time, bool p_seek) {
  395. double amount = get_parameter(blend_amount);
  396. double rem0 = blend_input(0, p_time, p_seek, MAX(0, -amount), FILTER_IGNORE, !sync);
  397. double rem1 = blend_input(1, p_time, p_seek, 1.0 - ABS(amount), FILTER_IGNORE, !sync);
  398. double rem2 = blend_input(2, p_time, p_seek, MAX(0, amount), FILTER_IGNORE, !sync);
  399. return amount > 0.5 ? rem2 : (amount < -0.5 ? rem0 : rem1); //hacky but good enough
  400. }
  401. void AnimationNodeBlend3::_bind_methods() {
  402. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeBlend3::set_use_sync);
  403. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeBlend3::is_using_sync);
  404. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
  405. }
  406. AnimationNodeBlend3::AnimationNodeBlend3() {
  407. blend_amount = "blend_amount";
  408. add_input("-blend");
  409. add_input("in");
  410. add_input("+blend");
  411. sync = false;
  412. }
  413. /////////////////////////////////
  414. void AnimationNodeTimeScale::get_parameter_list(List<PropertyInfo> *r_list) const {
  415. r_list->push_back(PropertyInfo(Variant::FLOAT, scale, PROPERTY_HINT_RANGE, "0,32,0.01,or_greater"));
  416. }
  417. Variant AnimationNodeTimeScale::get_parameter_default_value(const StringName &p_parameter) const {
  418. return 1.0; //initial timescale
  419. }
  420. String AnimationNodeTimeScale::get_caption() const {
  421. return "TimeScale";
  422. }
  423. double AnimationNodeTimeScale::process(double p_time, bool p_seek) {
  424. double scale = get_parameter(this->scale);
  425. if (p_seek) {
  426. return blend_input(0, p_time, true, 1.0, FILTER_IGNORE, false);
  427. } else {
  428. return blend_input(0, p_time * scale, false, 1.0, FILTER_IGNORE, false);
  429. }
  430. }
  431. void AnimationNodeTimeScale::_bind_methods() {
  432. }
  433. AnimationNodeTimeScale::AnimationNodeTimeScale() {
  434. add_input("in");
  435. }
  436. ////////////////////////////////////
  437. void AnimationNodeTimeSeek::get_parameter_list(List<PropertyInfo> *r_list) const {
  438. r_list->push_back(PropertyInfo(Variant::FLOAT, seek_pos, PROPERTY_HINT_RANGE, "-1,3600,0.01,or_greater"));
  439. }
  440. Variant AnimationNodeTimeSeek::get_parameter_default_value(const StringName &p_parameter) const {
  441. return 1.0; //initial timescale
  442. }
  443. String AnimationNodeTimeSeek::get_caption() const {
  444. return "Seek";
  445. }
  446. double AnimationNodeTimeSeek::process(double p_time, bool p_seek) {
  447. double seek_pos = get_parameter(this->seek_pos);
  448. if (p_seek) {
  449. return blend_input(0, p_time, true, 1.0, FILTER_IGNORE, false);
  450. } else if (seek_pos >= 0) {
  451. double ret = blend_input(0, seek_pos, true, 1.0, FILTER_IGNORE, false);
  452. set_parameter(this->seek_pos, -1.0); //reset
  453. return ret;
  454. } else {
  455. return blend_input(0, p_time, false, 1.0, FILTER_IGNORE, false);
  456. }
  457. }
  458. void AnimationNodeTimeSeek::_bind_methods() {
  459. }
  460. AnimationNodeTimeSeek::AnimationNodeTimeSeek() {
  461. add_input("in");
  462. }
  463. /////////////////////////////////////////////////
  464. void AnimationNodeTransition::get_parameter_list(List<PropertyInfo> *r_list) const {
  465. String anims;
  466. for (int i = 0; i < enabled_inputs; i++) {
  467. if (i > 0) {
  468. anims += ",";
  469. }
  470. anims += inputs[i].name;
  471. }
  472. r_list->push_back(PropertyInfo(Variant::INT, current, PROPERTY_HINT_ENUM, anims));
  473. r_list->push_back(PropertyInfo(Variant::INT, prev_current, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  474. r_list->push_back(PropertyInfo(Variant::INT, prev, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  475. r_list->push_back(PropertyInfo(Variant::FLOAT, time, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  476. r_list->push_back(PropertyInfo(Variant::FLOAT, prev_xfading, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  477. }
  478. Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p_parameter) const {
  479. if (p_parameter == time || p_parameter == prev_xfading) {
  480. return 0.0;
  481. } else if (p_parameter == prev || p_parameter == prev_current) {
  482. return -1;
  483. } else {
  484. return 0;
  485. }
  486. }
  487. String AnimationNodeTransition::get_caption() const {
  488. return "Transition";
  489. }
  490. void AnimationNodeTransition::_update_inputs() {
  491. while (get_input_count() < enabled_inputs) {
  492. add_input(inputs[get_input_count()].name);
  493. }
  494. while (get_input_count() > enabled_inputs) {
  495. remove_input(get_input_count() - 1);
  496. }
  497. }
  498. void AnimationNodeTransition::set_enabled_inputs(int p_inputs) {
  499. ERR_FAIL_INDEX(p_inputs, MAX_INPUTS);
  500. enabled_inputs = p_inputs;
  501. _update_inputs();
  502. }
  503. int AnimationNodeTransition::get_enabled_inputs() {
  504. return enabled_inputs;
  505. }
  506. void AnimationNodeTransition::set_input_as_auto_advance(int p_input, bool p_enable) {
  507. ERR_FAIL_INDEX(p_input, MAX_INPUTS);
  508. inputs[p_input].auto_advance = p_enable;
  509. }
  510. bool AnimationNodeTransition::is_input_set_as_auto_advance(int p_input) const {
  511. ERR_FAIL_INDEX_V(p_input, MAX_INPUTS, false);
  512. return inputs[p_input].auto_advance;
  513. }
  514. void AnimationNodeTransition::set_input_caption(int p_input, const String &p_name) {
  515. ERR_FAIL_INDEX(p_input, MAX_INPUTS);
  516. inputs[p_input].name = p_name;
  517. set_input_name(p_input, p_name);
  518. }
  519. String AnimationNodeTransition::get_input_caption(int p_input) const {
  520. ERR_FAIL_INDEX_V(p_input, MAX_INPUTS, String());
  521. return inputs[p_input].name;
  522. }
  523. void AnimationNodeTransition::set_cross_fade_time(float p_fade) {
  524. xfade = p_fade;
  525. }
  526. float AnimationNodeTransition::get_cross_fade_time() const {
  527. return xfade;
  528. }
  529. double AnimationNodeTransition::process(double p_time, bool p_seek) {
  530. int current = get_parameter(this->current);
  531. int prev = get_parameter(this->prev);
  532. int prev_current = get_parameter(this->prev_current);
  533. double time = get_parameter(this->time);
  534. double prev_xfading = get_parameter(this->prev_xfading);
  535. bool switched = current != prev_current;
  536. if (switched) {
  537. set_parameter(this->prev_current, current);
  538. set_parameter(this->prev, prev_current);
  539. prev = prev_current;
  540. prev_xfading = xfade;
  541. time = 0;
  542. switched = true;
  543. }
  544. if (current < 0 || current >= enabled_inputs || prev >= enabled_inputs) {
  545. return 0;
  546. }
  547. float rem = 0.0;
  548. if (prev < 0) { // process current animation, check for transition
  549. rem = blend_input(current, p_time, p_seek, 1.0, FILTER_IGNORE, false);
  550. if (p_seek) {
  551. time = p_time;
  552. } else {
  553. time += p_time;
  554. }
  555. if (inputs[current].auto_advance && rem <= xfade) {
  556. set_parameter(this->current, (current + 1) % enabled_inputs);
  557. }
  558. } else { // cross-fading from prev to current
  559. float blend = xfade == 0 ? 0 : (prev_xfading / xfade);
  560. if (!p_seek && switched) { //just switched, seek to start of current
  561. rem = blend_input(current, 0, true, 1.0 - blend, FILTER_IGNORE, false);
  562. } else {
  563. rem = blend_input(current, p_time, p_seek, 1.0 - blend, FILTER_IGNORE, false);
  564. }
  565. if (p_seek) { // don't seek prev animation
  566. blend_input(prev, 0, false, blend, FILTER_IGNORE, false);
  567. time = p_time;
  568. } else {
  569. blend_input(prev, p_time, false, blend, FILTER_IGNORE, false);
  570. time += p_time;
  571. prev_xfading -= p_time;
  572. if (prev_xfading < 0) {
  573. set_parameter(this->prev, -1);
  574. }
  575. }
  576. }
  577. set_parameter(this->time, time);
  578. set_parameter(this->prev_xfading, prev_xfading);
  579. return rem;
  580. }
  581. void AnimationNodeTransition::_validate_property(PropertyInfo &property) const {
  582. if (property.name.begins_with("input_")) {
  583. String n = property.name.get_slicec('/', 0).get_slicec('_', 1);
  584. if (n != "count") {
  585. int idx = n.to_int();
  586. if (idx >= enabled_inputs) {
  587. property.usage = PROPERTY_USAGE_NONE;
  588. }
  589. }
  590. }
  591. AnimationNode::_validate_property(property);
  592. }
  593. void AnimationNodeTransition::_bind_methods() {
  594. ClassDB::bind_method(D_METHOD("set_enabled_inputs", "amount"), &AnimationNodeTransition::set_enabled_inputs);
  595. ClassDB::bind_method(D_METHOD("get_enabled_inputs"), &AnimationNodeTransition::get_enabled_inputs);
  596. ClassDB::bind_method(D_METHOD("set_input_as_auto_advance", "input", "enable"), &AnimationNodeTransition::set_input_as_auto_advance);
  597. ClassDB::bind_method(D_METHOD("is_input_set_as_auto_advance", "input"), &AnimationNodeTransition::is_input_set_as_auto_advance);
  598. ClassDB::bind_method(D_METHOD("set_input_caption", "input", "caption"), &AnimationNodeTransition::set_input_caption);
  599. ClassDB::bind_method(D_METHOD("get_input_caption", "input"), &AnimationNodeTransition::get_input_caption);
  600. ClassDB::bind_method(D_METHOD("set_cross_fade_time", "time"), &AnimationNodeTransition::set_cross_fade_time);
  601. ClassDB::bind_method(D_METHOD("get_cross_fade_time"), &AnimationNodeTransition::get_cross_fade_time);
  602. ADD_PROPERTY(PropertyInfo(Variant::INT, "input_count", PROPERTY_HINT_RANGE, "0,64,1", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_enabled_inputs", "get_enabled_inputs");
  603. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01"), "set_cross_fade_time", "get_cross_fade_time");
  604. for (int i = 0; i < MAX_INPUTS; i++) {
  605. ADD_PROPERTYI(PropertyInfo(Variant::STRING, "input_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_input_caption", "get_input_caption", i);
  606. ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/auto_advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_input_as_auto_advance", "is_input_set_as_auto_advance", i);
  607. }
  608. }
  609. AnimationNodeTransition::AnimationNodeTransition() {
  610. for (int i = 0; i < MAX_INPUTS; i++) {
  611. inputs[i].name = "state " + itos(i);
  612. }
  613. }
  614. /////////////////////
  615. String AnimationNodeOutput::get_caption() const {
  616. return "Output";
  617. }
  618. double AnimationNodeOutput::process(double p_time, bool p_seek) {
  619. return blend_input(0, p_time, p_seek, 1.0);
  620. }
  621. AnimationNodeOutput::AnimationNodeOutput() {
  622. add_input("output");
  623. }
  624. ///////////////////////////////////////////////////////
  625. void AnimationNodeBlendTree::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  626. ERR_FAIL_COND(nodes.has(p_name));
  627. ERR_FAIL_COND(p_node.is_null());
  628. ERR_FAIL_COND(p_name == SceneStringNames::get_singleton()->output);
  629. ERR_FAIL_COND(String(p_name).find("/") != -1);
  630. Node n;
  631. n.node = p_node;
  632. n.position = p_position;
  633. n.connections.resize(n.node->get_input_count());
  634. nodes[p_name] = n;
  635. emit_changed();
  636. emit_signal(SNAME("tree_changed"));
  637. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendTree::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED);
  638. p_node->connect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed), varray(p_name), CONNECT_REFERENCE_COUNTED);
  639. }
  640. Ref<AnimationNode> AnimationNodeBlendTree::get_node(const StringName &p_name) const {
  641. ERR_FAIL_COND_V(!nodes.has(p_name), Ref<AnimationNode>());
  642. return nodes[p_name].node;
  643. }
  644. StringName AnimationNodeBlendTree::get_node_name(const Ref<AnimationNode> &p_node) const {
  645. for (const KeyValue<StringName, Node> &E : nodes) {
  646. if (E.value.node == p_node) {
  647. return E.key;
  648. }
  649. }
  650. ERR_FAIL_V(StringName());
  651. }
  652. void AnimationNodeBlendTree::set_node_position(const StringName &p_node, const Vector2 &p_position) {
  653. ERR_FAIL_COND(!nodes.has(p_node));
  654. nodes[p_node].position = p_position;
  655. }
  656. Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) const {
  657. ERR_FAIL_COND_V(!nodes.has(p_node), Vector2());
  658. return nodes[p_node].position;
  659. }
  660. void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
  661. Vector<StringName> ns;
  662. for (const KeyValue<StringName, Node> &E : nodes) {
  663. ns.push_back(E.key);
  664. }
  665. ns.sort_custom<StringName::AlphCompare>();
  666. for (int i = 0; i < ns.size(); i++) {
  667. ChildNode cn;
  668. cn.name = ns[i];
  669. cn.node = nodes[cn.name].node;
  670. r_child_nodes->push_back(cn);
  671. }
  672. }
  673. bool AnimationNodeBlendTree::has_node(const StringName &p_name) const {
  674. return nodes.has(p_name);
  675. }
  676. Vector<StringName> AnimationNodeBlendTree::get_node_connection_array(const StringName &p_name) const {
  677. ERR_FAIL_COND_V(!nodes.has(p_name), Vector<StringName>());
  678. return nodes[p_name].connections;
  679. }
  680. void AnimationNodeBlendTree::remove_node(const StringName &p_name) {
  681. ERR_FAIL_COND(!nodes.has(p_name));
  682. ERR_FAIL_COND(p_name == SceneStringNames::get_singleton()->output); //can't delete output
  683. {
  684. Ref<AnimationNode> node = nodes[p_name].node;
  685. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendTree::_tree_changed));
  686. node->disconnect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  687. }
  688. nodes.erase(p_name);
  689. //erase connections to name
  690. for (KeyValue<StringName, Node> &E : nodes) {
  691. for (int i = 0; i < E.value.connections.size(); i++) {
  692. if (E.value.connections[i] == p_name) {
  693. E.value.connections.write[i] = StringName();
  694. }
  695. }
  696. }
  697. emit_changed();
  698. emit_signal(SNAME("tree_changed"));
  699. }
  700. void AnimationNodeBlendTree::rename_node(const StringName &p_name, const StringName &p_new_name) {
  701. ERR_FAIL_COND(!nodes.has(p_name));
  702. ERR_FAIL_COND(nodes.has(p_new_name));
  703. ERR_FAIL_COND(p_name == SceneStringNames::get_singleton()->output);
  704. ERR_FAIL_COND(p_new_name == SceneStringNames::get_singleton()->output);
  705. nodes[p_name].node->disconnect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  706. nodes[p_new_name] = nodes[p_name];
  707. nodes.erase(p_name);
  708. //rename connections
  709. for (KeyValue<StringName, Node> &E : nodes) {
  710. for (int i = 0; i < E.value.connections.size(); i++) {
  711. if (E.value.connections[i] == p_name) {
  712. E.value.connections.write[i] = p_new_name;
  713. }
  714. }
  715. }
  716. //connection must be done with new name
  717. nodes[p_new_name].node->connect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed), varray(p_new_name), CONNECT_REFERENCE_COUNTED);
  718. emit_signal(SNAME("tree_changed"));
  719. }
  720. void AnimationNodeBlendTree::connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) {
  721. ERR_FAIL_COND(!nodes.has(p_output_node));
  722. ERR_FAIL_COND(!nodes.has(p_input_node));
  723. ERR_FAIL_COND(p_output_node == SceneStringNames::get_singleton()->output);
  724. ERR_FAIL_COND(p_input_node == p_output_node);
  725. Ref<AnimationNode> input = nodes[p_input_node].node;
  726. ERR_FAIL_INDEX(p_input_index, nodes[p_input_node].connections.size());
  727. for (KeyValue<StringName, Node> &E : nodes) {
  728. for (int i = 0; i < E.value.connections.size(); i++) {
  729. StringName output = E.value.connections[i];
  730. ERR_FAIL_COND(output == p_output_node);
  731. }
  732. }
  733. nodes[p_input_node].connections.write[p_input_index] = p_output_node;
  734. emit_changed();
  735. }
  736. void AnimationNodeBlendTree::disconnect_node(const StringName &p_node, int p_input_index) {
  737. ERR_FAIL_COND(!nodes.has(p_node));
  738. Ref<AnimationNode> input = nodes[p_node].node;
  739. ERR_FAIL_INDEX(p_input_index, nodes[p_node].connections.size());
  740. nodes[p_node].connections.write[p_input_index] = StringName();
  741. }
  742. AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const {
  743. if (!nodes.has(p_output_node) || p_output_node == SceneStringNames::get_singleton()->output) {
  744. return CONNECTION_ERROR_NO_OUTPUT;
  745. }
  746. if (!nodes.has(p_input_node)) {
  747. return CONNECTION_ERROR_NO_INPUT;
  748. }
  749. if (p_input_node == p_output_node) {
  750. return CONNECTION_ERROR_SAME_NODE;
  751. }
  752. Ref<AnimationNode> input = nodes[p_input_node].node;
  753. if (p_input_index < 0 || p_input_index >= nodes[p_input_node].connections.size()) {
  754. return CONNECTION_ERROR_NO_INPUT_INDEX;
  755. }
  756. if (nodes[p_input_node].connections[p_input_index] != StringName()) {
  757. return CONNECTION_ERROR_CONNECTION_EXISTS;
  758. }
  759. for (const KeyValue<StringName, Node> &E : nodes) {
  760. for (int i = 0; i < E.value.connections.size(); i++) {
  761. const StringName output = E.value.connections[i];
  762. if (output == p_output_node) {
  763. return CONNECTION_ERROR_CONNECTION_EXISTS;
  764. }
  765. }
  766. }
  767. return CONNECTION_OK;
  768. }
  769. void AnimationNodeBlendTree::get_node_connections(List<NodeConnection> *r_connections) const {
  770. for (const KeyValue<StringName, Node> &E : nodes) {
  771. for (int i = 0; i < E.value.connections.size(); i++) {
  772. const StringName output = E.value.connections[i];
  773. if (output != StringName()) {
  774. NodeConnection nc;
  775. nc.input_node = E.key;
  776. nc.input_index = i;
  777. nc.output_node = output;
  778. r_connections->push_back(nc);
  779. }
  780. }
  781. }
  782. }
  783. String AnimationNodeBlendTree::get_caption() const {
  784. return "BlendTree";
  785. }
  786. double AnimationNodeBlendTree::process(double p_time, bool p_seek) {
  787. Ref<AnimationNodeOutput> output = nodes[SceneStringNames::get_singleton()->output].node;
  788. return _blend_node("output", nodes[SceneStringNames::get_singleton()->output].connections, this, output, p_time, p_seek, 1.0);
  789. }
  790. void AnimationNodeBlendTree::get_node_list(List<StringName> *r_list) {
  791. for (const KeyValue<StringName, Node> &E : nodes) {
  792. r_list->push_back(E.key);
  793. }
  794. }
  795. void AnimationNodeBlendTree::set_graph_offset(const Vector2 &p_graph_offset) {
  796. graph_offset = p_graph_offset;
  797. }
  798. Vector2 AnimationNodeBlendTree::get_graph_offset() const {
  799. return graph_offset;
  800. }
  801. Ref<AnimationNode> AnimationNodeBlendTree::get_child_by_name(const StringName &p_name) {
  802. return get_node(p_name);
  803. }
  804. bool AnimationNodeBlendTree::_set(const StringName &p_name, const Variant &p_value) {
  805. String name = p_name;
  806. if (name.begins_with("nodes/")) {
  807. String node_name = name.get_slicec('/', 1);
  808. String what = name.get_slicec('/', 2);
  809. if (what == "node") {
  810. Ref<AnimationNode> anode = p_value;
  811. if (anode.is_valid()) {
  812. add_node(node_name, p_value);
  813. }
  814. return true;
  815. }
  816. if (what == "position") {
  817. if (nodes.has(node_name)) {
  818. nodes[node_name].position = p_value;
  819. }
  820. return true;
  821. }
  822. } else if (name == "node_connections") {
  823. Array conns = p_value;
  824. ERR_FAIL_COND_V(conns.size() % 3 != 0, false);
  825. for (int i = 0; i < conns.size(); i += 3) {
  826. connect_node(conns[i], conns[i + 1], conns[i + 2]);
  827. }
  828. return true;
  829. }
  830. return false;
  831. }
  832. bool AnimationNodeBlendTree::_get(const StringName &p_name, Variant &r_ret) const {
  833. String name = p_name;
  834. if (name.begins_with("nodes/")) {
  835. String node_name = name.get_slicec('/', 1);
  836. String what = name.get_slicec('/', 2);
  837. if (what == "node") {
  838. if (nodes.has(node_name)) {
  839. r_ret = nodes[node_name].node;
  840. return true;
  841. }
  842. }
  843. if (what == "position") {
  844. if (nodes.has(node_name)) {
  845. r_ret = nodes[node_name].position;
  846. return true;
  847. }
  848. }
  849. } else if (name == "node_connections") {
  850. List<NodeConnection> nc;
  851. get_node_connections(&nc);
  852. Array conns;
  853. conns.resize(nc.size() * 3);
  854. int idx = 0;
  855. for (const NodeConnection &E : nc) {
  856. conns[idx * 3 + 0] = E.input_node;
  857. conns[idx * 3 + 1] = E.input_index;
  858. conns[idx * 3 + 2] = E.output_node;
  859. idx++;
  860. }
  861. r_ret = conns;
  862. return true;
  863. }
  864. return false;
  865. }
  866. void AnimationNodeBlendTree::_get_property_list(List<PropertyInfo> *p_list) const {
  867. List<StringName> names;
  868. for (const KeyValue<StringName, Node> &E : nodes) {
  869. names.push_back(E.key);
  870. }
  871. names.sort_custom<StringName::AlphCompare>();
  872. for (const StringName &E : names) {
  873. String name = E;
  874. if (name != "output") {
  875. p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  876. }
  877. p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  878. }
  879. p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  880. }
  881. void AnimationNodeBlendTree::reset_state() {
  882. graph_offset = Vector2();
  883. nodes.clear();
  884. _initialize_node_tree();
  885. emit_changed();
  886. emit_signal(SNAME("tree_changed"));
  887. }
  888. void AnimationNodeBlendTree::_tree_changed() {
  889. emit_signal(SNAME("tree_changed"));
  890. }
  891. void AnimationNodeBlendTree::_node_changed(const StringName &p_node) {
  892. ERR_FAIL_COND(!nodes.has(p_node));
  893. nodes[p_node].connections.resize(nodes[p_node].node->get_input_count());
  894. }
  895. void AnimationNodeBlendTree::_bind_methods() {
  896. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeBlendTree::add_node, DEFVAL(Vector2()));
  897. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeBlendTree::get_node);
  898. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeBlendTree::remove_node);
  899. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeBlendTree::rename_node);
  900. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeBlendTree::has_node);
  901. ClassDB::bind_method(D_METHOD("connect_node", "input_node", "input_index", "output_node"), &AnimationNodeBlendTree::connect_node);
  902. ClassDB::bind_method(D_METHOD("disconnect_node", "input_node", "input_index"), &AnimationNodeBlendTree::disconnect_node);
  903. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeBlendTree::set_node_position);
  904. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeBlendTree::get_node_position);
  905. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeBlendTree::set_graph_offset);
  906. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeBlendTree::get_graph_offset);
  907. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");
  908. BIND_CONSTANT(CONNECTION_OK);
  909. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT);
  910. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT_INDEX);
  911. BIND_CONSTANT(CONNECTION_ERROR_NO_OUTPUT);
  912. BIND_CONSTANT(CONNECTION_ERROR_SAME_NODE);
  913. BIND_CONSTANT(CONNECTION_ERROR_CONNECTION_EXISTS);
  914. }
  915. void AnimationNodeBlendTree::_initialize_node_tree() {
  916. Ref<AnimationNodeOutput> output;
  917. output.instantiate();
  918. Node n;
  919. n.node = output;
  920. n.position = Vector2(300, 150);
  921. n.connections.resize(1);
  922. nodes["output"] = n;
  923. }
  924. AnimationNodeBlendTree::AnimationNodeBlendTree() {
  925. _initialize_node_tree();
  926. }
  927. AnimationNodeBlendTree::~AnimationNodeBlendTree() {
  928. }