animation_blend_tree.cpp 39 KB

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