animation_blend_tree.cpp 37 KB

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