animation_blend_tree.cpp 39 KB

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