animation_blend_tree.cpp 37 KB

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