animation_blend_tree.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /**************************************************************************/
  2. /* animation_blend_tree.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation_blend_tree.h"
  31. #include "scene/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_is_external_seeking) {
  60. AnimationPlayer *ap = state->player;
  61. ERR_FAIL_COND_V(!ap, 0);
  62. double cur_time = get_parameter(time);
  63. if (!ap->has_animation(animation)) {
  64. AnimationNodeBlendTree *tree = Object::cast_to<AnimationNodeBlendTree>(parent);
  65. if (tree) {
  66. String node_name = tree->get_node_name(Ref<AnimationNodeAnimation>(this));
  67. make_invalid(vformat(RTR("On BlendTree node '%s', animation not found: '%s'"), node_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 = cur_time;
  77. Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
  78. bool node_backward = play_mode == PLAY_MODE_BACKWARD;
  79. if (p_seek) {
  80. step = p_time - cur_time;
  81. cur_time = p_time;
  82. } else {
  83. p_time *= backward ? -1.0 : 1.0;
  84. cur_time = cur_time + p_time;
  85. step = p_time;
  86. }
  87. if (anim->get_loop_mode() == Animation::LOOP_PINGPONG) {
  88. if (!Math::is_zero_approx(anim_size)) {
  89. if (prev_time >= 0 && cur_time < 0) {
  90. backward = !backward;
  91. looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
  92. }
  93. if (prev_time <= anim_size && cur_time > anim_size) {
  94. backward = !backward;
  95. looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
  96. }
  97. cur_time = Math::pingpong(cur_time, anim_size);
  98. }
  99. } else if (anim->get_loop_mode() == Animation::LOOP_LINEAR) {
  100. if (!Math::is_zero_approx(anim_size)) {
  101. if (prev_time >= 0 && cur_time < 0) {
  102. looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
  103. }
  104. if (prev_time <= anim_size && cur_time > anim_size) {
  105. looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
  106. }
  107. cur_time = Math::fposmod(cur_time, anim_size);
  108. }
  109. backward = false;
  110. } else {
  111. if (cur_time < 0) {
  112. step += cur_time;
  113. cur_time = 0;
  114. } else if (cur_time > anim_size) {
  115. step += anim_size - cur_time;
  116. cur_time = anim_size;
  117. }
  118. backward = false;
  119. // If ended, don't progress animation. So set delta to 0.
  120. if (p_time > 0) {
  121. if (play_mode == PLAY_MODE_FORWARD) {
  122. if (prev_time >= anim_size) {
  123. step = 0;
  124. }
  125. } else {
  126. if (prev_time <= 0) {
  127. step = 0;
  128. }
  129. }
  130. }
  131. // Emit start & finish signal. Internally, the detections are the same for backward.
  132. // We should use call_deferred since the track keys are still being processed.
  133. if (state->tree) {
  134. // AnimationTree uses seek to 0 "internally" to process the first key of the animation, which is used as the start detection.
  135. if (p_seek && !p_is_external_seeking && cur_time == 0) {
  136. state->tree->call_deferred(SNAME("emit_signal"), "animation_started", animation);
  137. }
  138. // Finished.
  139. if (prev_time < anim_size && cur_time >= anim_size) {
  140. state->tree->call_deferred(SNAME("emit_signal"), "animation_finished", animation);
  141. }
  142. }
  143. }
  144. if (play_mode == PLAY_MODE_FORWARD) {
  145. blend_animation(animation, cur_time, step, p_seek, p_is_external_seeking, 1.0, looped_flag);
  146. } else {
  147. blend_animation(animation, anim_size - cur_time, -step, p_seek, p_is_external_seeking, 1.0, looped_flag);
  148. }
  149. set_parameter(time, cur_time);
  150. return anim_size - cur_time;
  151. }
  152. String AnimationNodeAnimation::get_caption() const {
  153. return "Animation";
  154. }
  155. void AnimationNodeAnimation::set_play_mode(PlayMode p_play_mode) {
  156. play_mode = p_play_mode;
  157. }
  158. AnimationNodeAnimation::PlayMode AnimationNodeAnimation::get_play_mode() const {
  159. return play_mode;
  160. }
  161. void AnimationNodeAnimation::set_backward(bool p_backward) {
  162. backward = p_backward;
  163. }
  164. bool AnimationNodeAnimation::is_backward() const {
  165. return backward;
  166. }
  167. void AnimationNodeAnimation::_bind_methods() {
  168. ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimationNodeAnimation::set_animation);
  169. ClassDB::bind_method(D_METHOD("get_animation"), &AnimationNodeAnimation::get_animation);
  170. ClassDB::bind_method(D_METHOD("set_play_mode", "mode"), &AnimationNodeAnimation::set_play_mode);
  171. ClassDB::bind_method(D_METHOD("get_play_mode"), &AnimationNodeAnimation::get_play_mode);
  172. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
  173. ADD_PROPERTY(PropertyInfo(Variant::INT, "play_mode", PROPERTY_HINT_ENUM, "Forward,Backward"), "set_play_mode", "get_play_mode");
  174. BIND_ENUM_CONSTANT(PLAY_MODE_FORWARD);
  175. BIND_ENUM_CONSTANT(PLAY_MODE_BACKWARD);
  176. }
  177. AnimationNodeAnimation::AnimationNodeAnimation() {
  178. }
  179. ////////////////////////////////////////////////////////
  180. void AnimationNodeSync::_bind_methods() {
  181. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeSync::set_use_sync);
  182. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeSync::is_using_sync);
  183. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
  184. }
  185. void AnimationNodeSync::set_use_sync(bool p_sync) {
  186. sync = p_sync;
  187. }
  188. bool AnimationNodeSync::is_using_sync() const {
  189. return sync;
  190. }
  191. AnimationNodeSync::AnimationNodeSync() {
  192. }
  193. ////////////////////////////////////////////////////////
  194. void AnimationNodeOneShot::get_parameter_list(List<PropertyInfo> *r_list) const {
  195. r_list->push_back(PropertyInfo(Variant::BOOL, active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY));
  196. r_list->push_back(PropertyInfo(Variant::INT, request, PROPERTY_HINT_ENUM, ",Fire,Abort"));
  197. r_list->push_back(PropertyInfo(Variant::FLOAT, time, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  198. r_list->push_back(PropertyInfo(Variant::FLOAT, remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  199. r_list->push_back(PropertyInfo(Variant::FLOAT, time_to_restart, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  200. }
  201. Variant AnimationNodeOneShot::get_parameter_default_value(const StringName &p_parameter) const {
  202. if (p_parameter == request) {
  203. return ONE_SHOT_REQUEST_NONE;
  204. } else if (p_parameter == active) {
  205. return false;
  206. } else if (p_parameter == time_to_restart) {
  207. return -1;
  208. } else {
  209. return 0.0;
  210. }
  211. }
  212. bool AnimationNodeOneShot::is_parameter_read_only(const StringName &p_parameter) const {
  213. if (p_parameter == active) {
  214. return true;
  215. }
  216. return false;
  217. }
  218. void AnimationNodeOneShot::set_fadein_time(double p_time) {
  219. fade_in = p_time;
  220. }
  221. void AnimationNodeOneShot::set_fadeout_time(double p_time) {
  222. fade_out = p_time;
  223. }
  224. double AnimationNodeOneShot::get_fadein_time() const {
  225. return fade_in;
  226. }
  227. double AnimationNodeOneShot::get_fadeout_time() const {
  228. return fade_out;
  229. }
  230. void AnimationNodeOneShot::set_autorestart(bool p_active) {
  231. autorestart = p_active;
  232. }
  233. void AnimationNodeOneShot::set_autorestart_delay(double p_time) {
  234. autorestart_delay = p_time;
  235. }
  236. void AnimationNodeOneShot::set_autorestart_random_delay(double p_time) {
  237. autorestart_random_delay = p_time;
  238. }
  239. bool AnimationNodeOneShot::has_autorestart() const {
  240. return autorestart;
  241. }
  242. double AnimationNodeOneShot::get_autorestart_delay() const {
  243. return autorestart_delay;
  244. }
  245. double AnimationNodeOneShot::get_autorestart_random_delay() const {
  246. return autorestart_random_delay;
  247. }
  248. void AnimationNodeOneShot::set_mix_mode(MixMode p_mix) {
  249. mix = p_mix;
  250. }
  251. AnimationNodeOneShot::MixMode AnimationNodeOneShot::get_mix_mode() const {
  252. return mix;
  253. }
  254. String AnimationNodeOneShot::get_caption() const {
  255. return "OneShot";
  256. }
  257. bool AnimationNodeOneShot::has_filter() const {
  258. return true;
  259. }
  260. double AnimationNodeOneShot::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  261. OneShotRequest cur_request = static_cast<OneShotRequest>((int)get_parameter(request));
  262. bool cur_active = get_parameter(active);
  263. double cur_time = get_parameter(time);
  264. double cur_remaining = get_parameter(remaining);
  265. double cur_time_to_restart = get_parameter(time_to_restart);
  266. set_parameter(request, ONE_SHOT_REQUEST_NONE);
  267. bool do_start = cur_request == ONE_SHOT_REQUEST_FIRE;
  268. if (cur_request == ONE_SHOT_REQUEST_ABORT) {
  269. set_parameter(active, false);
  270. set_parameter(time_to_restart, -1);
  271. return blend_input(0, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, sync);
  272. } else if (!do_start && !cur_active) {
  273. if (cur_time_to_restart >= 0.0 && !p_seek) {
  274. cur_time_to_restart -= p_time;
  275. if (cur_time_to_restart < 0) {
  276. do_start = true; // Restart.
  277. }
  278. set_parameter(time_to_restart, cur_time_to_restart);
  279. }
  280. if (!do_start) {
  281. return blend_input(0, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, sync);
  282. }
  283. }
  284. bool os_seek = p_seek;
  285. if (p_seek) {
  286. cur_time = p_time;
  287. }
  288. if (do_start) {
  289. cur_time = 0;
  290. os_seek = true;
  291. set_parameter(request, ONE_SHOT_REQUEST_NONE);
  292. set_parameter(active, true);
  293. }
  294. real_t blend = 1.0;
  295. bool use_blend = sync;
  296. if (cur_time < fade_in) {
  297. if (fade_in > 0) {
  298. use_blend = true;
  299. blend = cur_time / fade_in;
  300. } else {
  301. blend = 0; // Should not happen.
  302. }
  303. } else if (!do_start && cur_remaining <= fade_out) {
  304. use_blend = true;
  305. if (fade_out > 0) {
  306. blend = (cur_remaining / fade_out);
  307. } else {
  308. blend = 0;
  309. }
  310. }
  311. double main_rem = 0.0;
  312. if (mix == MIX_MODE_ADD) {
  313. main_rem = blend_input(0, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, sync);
  314. } else {
  315. main_rem = blend_input(0, p_time, use_blend && p_seek, p_is_external_seeking, 1.0 - blend, FILTER_BLEND, sync); // Unlike below, processing this edge is a corner case.
  316. }
  317. double os_rem = blend_input(1, os_seek ? cur_time : p_time, os_seek, p_is_external_seeking, Math::is_zero_approx(blend) ? CMP_EPSILON : blend, FILTER_PASS, true); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  318. if (do_start) {
  319. cur_remaining = os_rem;
  320. }
  321. if (!p_seek) {
  322. cur_time += p_time;
  323. cur_remaining = os_rem;
  324. if (cur_remaining <= 0) {
  325. set_parameter(active, false);
  326. if (autorestart) {
  327. double restart_sec = autorestart_delay + Math::randd() * autorestart_random_delay;
  328. set_parameter(time_to_restart, restart_sec);
  329. }
  330. }
  331. }
  332. set_parameter(time, cur_time);
  333. set_parameter(remaining, cur_remaining);
  334. return MAX(main_rem, cur_remaining);
  335. }
  336. void AnimationNodeOneShot::_bind_methods() {
  337. ClassDB::bind_method(D_METHOD("set_fadein_time", "time"), &AnimationNodeOneShot::set_fadein_time);
  338. ClassDB::bind_method(D_METHOD("get_fadein_time"), &AnimationNodeOneShot::get_fadein_time);
  339. ClassDB::bind_method(D_METHOD("set_fadeout_time", "time"), &AnimationNodeOneShot::set_fadeout_time);
  340. ClassDB::bind_method(D_METHOD("get_fadeout_time"), &AnimationNodeOneShot::get_fadeout_time);
  341. ClassDB::bind_method(D_METHOD("set_autorestart", "enable"), &AnimationNodeOneShot::set_autorestart);
  342. ClassDB::bind_method(D_METHOD("has_autorestart"), &AnimationNodeOneShot::has_autorestart);
  343. ClassDB::bind_method(D_METHOD("set_autorestart_delay", "enable"), &AnimationNodeOneShot::set_autorestart_delay);
  344. ClassDB::bind_method(D_METHOD("get_autorestart_delay"), &AnimationNodeOneShot::get_autorestart_delay);
  345. ClassDB::bind_method(D_METHOD("set_autorestart_random_delay", "enable"), &AnimationNodeOneShot::set_autorestart_random_delay);
  346. ClassDB::bind_method(D_METHOD("get_autorestart_random_delay"), &AnimationNodeOneShot::get_autorestart_random_delay);
  347. ClassDB::bind_method(D_METHOD("set_mix_mode", "mode"), &AnimationNodeOneShot::set_mix_mode);
  348. ClassDB::bind_method(D_METHOD("get_mix_mode"), &AnimationNodeOneShot::get_mix_mode);
  349. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_mode", PROPERTY_HINT_ENUM, "Blend,Add"), "set_mix_mode", "get_mix_mode");
  350. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadein_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadein_time", "get_fadein_time");
  351. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadeout_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadeout_time", "get_fadeout_time");
  352. ADD_GROUP("Auto Restart", "autorestart_");
  353. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autorestart"), "set_autorestart", "has_autorestart");
  354. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_delay", "get_autorestart_delay");
  355. 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");
  356. BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_NONE);
  357. BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FIRE);
  358. BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_ABORT);
  359. BIND_ENUM_CONSTANT(MIX_MODE_BLEND);
  360. BIND_ENUM_CONSTANT(MIX_MODE_ADD);
  361. }
  362. AnimationNodeOneShot::AnimationNodeOneShot() {
  363. add_input("in");
  364. add_input("shot");
  365. }
  366. ////////////////////////////////////////////////
  367. void AnimationNodeAdd2::get_parameter_list(List<PropertyInfo> *r_list) const {
  368. r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "0,1,0.01"));
  369. }
  370. Variant AnimationNodeAdd2::get_parameter_default_value(const StringName &p_parameter) const {
  371. return 0;
  372. }
  373. String AnimationNodeAdd2::get_caption() const {
  374. return "Add2";
  375. }
  376. bool AnimationNodeAdd2::has_filter() const {
  377. return true;
  378. }
  379. double AnimationNodeAdd2::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  380. double amount = get_parameter(add_amount);
  381. double rem0 = blend_input(0, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, sync);
  382. blend_input(1, p_time, p_seek, p_is_external_seeking, amount, FILTER_PASS, sync);
  383. return rem0;
  384. }
  385. void AnimationNodeAdd2::_bind_methods() {
  386. }
  387. AnimationNodeAdd2::AnimationNodeAdd2() {
  388. add_input("in");
  389. add_input("add");
  390. }
  391. ////////////////////////////////////////////////
  392. void AnimationNodeAdd3::get_parameter_list(List<PropertyInfo> *r_list) const {
  393. r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "-1,1,0.01"));
  394. }
  395. Variant AnimationNodeAdd3::get_parameter_default_value(const StringName &p_parameter) const {
  396. return 0;
  397. }
  398. String AnimationNodeAdd3::get_caption() const {
  399. return "Add3";
  400. }
  401. bool AnimationNodeAdd3::has_filter() const {
  402. return true;
  403. }
  404. double AnimationNodeAdd3::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  405. double amount = get_parameter(add_amount);
  406. blend_input(0, p_time, p_seek, p_is_external_seeking, MAX(0, -amount), FILTER_PASS, sync);
  407. double rem0 = blend_input(1, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, sync);
  408. blend_input(2, p_time, p_seek, p_is_external_seeking, MAX(0, amount), FILTER_PASS, sync);
  409. return rem0;
  410. }
  411. void AnimationNodeAdd3::_bind_methods() {
  412. }
  413. AnimationNodeAdd3::AnimationNodeAdd3() {
  414. add_input("-add");
  415. add_input("in");
  416. add_input("+add");
  417. }
  418. /////////////////////////////////////////////
  419. void AnimationNodeBlend2::get_parameter_list(List<PropertyInfo> *r_list) const {
  420. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "0,1,0.01"));
  421. }
  422. Variant AnimationNodeBlend2::get_parameter_default_value(const StringName &p_parameter) const {
  423. return 0; // For blend amount.
  424. }
  425. String AnimationNodeBlend2::get_caption() const {
  426. return "Blend2";
  427. }
  428. double AnimationNodeBlend2::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  429. double amount = get_parameter(blend_amount);
  430. double rem0 = blend_input(0, p_time, p_seek, p_is_external_seeking, 1.0 - amount, FILTER_BLEND, sync);
  431. double rem1 = blend_input(1, p_time, p_seek, p_is_external_seeking, amount, FILTER_PASS, sync);
  432. return amount > 0.5 ? rem1 : rem0; // Hacky but good enough.
  433. }
  434. bool AnimationNodeBlend2::has_filter() const {
  435. return true;
  436. }
  437. void AnimationNodeBlend2::_bind_methods() {
  438. }
  439. AnimationNodeBlend2::AnimationNodeBlend2() {
  440. add_input("in");
  441. add_input("blend");
  442. }
  443. //////////////////////////////////////
  444. void AnimationNodeBlend3::get_parameter_list(List<PropertyInfo> *r_list) const {
  445. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "-1,1,0.01"));
  446. }
  447. Variant AnimationNodeBlend3::get_parameter_default_value(const StringName &p_parameter) const {
  448. return 0; // For blend amount.
  449. }
  450. String AnimationNodeBlend3::get_caption() const {
  451. return "Blend3";
  452. }
  453. double AnimationNodeBlend3::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  454. double amount = get_parameter(blend_amount);
  455. double rem0 = blend_input(0, p_time, p_seek, p_is_external_seeking, MAX(0, -amount), FILTER_IGNORE, sync);
  456. double rem1 = blend_input(1, p_time, p_seek, p_is_external_seeking, 1.0 - ABS(amount), FILTER_IGNORE, sync);
  457. double rem2 = blend_input(2, p_time, p_seek, p_is_external_seeking, MAX(0, amount), FILTER_IGNORE, sync);
  458. return amount > 0.5 ? rem2 : (amount < -0.5 ? rem0 : rem1); // Hacky but good enough.
  459. }
  460. void AnimationNodeBlend3::_bind_methods() {
  461. }
  462. AnimationNodeBlend3::AnimationNodeBlend3() {
  463. add_input("-blend");
  464. add_input("in");
  465. add_input("+blend");
  466. }
  467. /////////////////////////////////
  468. void AnimationNodeTimeScale::get_parameter_list(List<PropertyInfo> *r_list) const {
  469. r_list->push_back(PropertyInfo(Variant::FLOAT, scale, PROPERTY_HINT_RANGE, "-32,32,0.01,or_less,or_greater"));
  470. }
  471. Variant AnimationNodeTimeScale::get_parameter_default_value(const StringName &p_parameter) const {
  472. return 1.0; // Initial timescale.
  473. }
  474. String AnimationNodeTimeScale::get_caption() const {
  475. return "TimeScale";
  476. }
  477. double AnimationNodeTimeScale::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  478. double cur_scale = get_parameter(scale);
  479. if (p_seek) {
  480. return blend_input(0, p_time, true, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  481. } else {
  482. return blend_input(0, p_time * cur_scale, false, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  483. }
  484. }
  485. void AnimationNodeTimeScale::_bind_methods() {
  486. }
  487. AnimationNodeTimeScale::AnimationNodeTimeScale() {
  488. add_input("in");
  489. }
  490. ////////////////////////////////////
  491. void AnimationNodeTimeSeek::get_parameter_list(List<PropertyInfo> *r_list) const {
  492. r_list->push_back(PropertyInfo(Variant::FLOAT, seek_pos_request, PROPERTY_HINT_RANGE, "-1,3600,0.01,or_greater")); // It will be reset to -1 after seeking the position immediately.
  493. }
  494. Variant AnimationNodeTimeSeek::get_parameter_default_value(const StringName &p_parameter) const {
  495. return -1.0; // Initial seek request.
  496. }
  497. String AnimationNodeTimeSeek::get_caption() const {
  498. return "TimeSeek";
  499. }
  500. double AnimationNodeTimeSeek::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  501. double cur_seek_pos = get_parameter(seek_pos_request);
  502. if (p_seek) {
  503. return blend_input(0, p_time, true, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  504. } else if (cur_seek_pos >= 0) {
  505. double ret = blend_input(0, cur_seek_pos, true, true, 1.0, FILTER_IGNORE, true);
  506. set_parameter(seek_pos_request, -1.0); // Reset.
  507. return ret;
  508. } else {
  509. return blend_input(0, p_time, false, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  510. }
  511. }
  512. void AnimationNodeTimeSeek::_bind_methods() {
  513. }
  514. AnimationNodeTimeSeek::AnimationNodeTimeSeek() {
  515. add_input("in");
  516. }
  517. /////////////////////////////////////////////////
  518. bool AnimationNodeTransition::_set(const StringName &p_path, const Variant &p_value) {
  519. String path = p_path;
  520. if (!path.begins_with("input_")) {
  521. return false;
  522. }
  523. int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
  524. String what = path.get_slicec('/', 1);
  525. if (which == get_input_count() && what == "name") {
  526. if (add_input(p_value)) {
  527. return true;
  528. }
  529. return false;
  530. }
  531. ERR_FAIL_INDEX_V(which, get_input_count(), false);
  532. if (what == "name") {
  533. set_input_name(which, p_value);
  534. } else if (what == "auto_advance") {
  535. set_input_as_auto_advance(which, p_value);
  536. } else if (what == "reset") {
  537. set_input_reset(which, p_value);
  538. } else {
  539. return false;
  540. }
  541. return true;
  542. }
  543. bool AnimationNodeTransition::_get(const StringName &p_path, Variant &r_ret) const {
  544. String path = p_path;
  545. if (!path.begins_with("input_")) {
  546. return false;
  547. }
  548. int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
  549. String what = path.get_slicec('/', 1);
  550. ERR_FAIL_INDEX_V(which, get_input_count(), false);
  551. if (what == "name") {
  552. r_ret = get_input_name(which);
  553. } else if (what == "auto_advance") {
  554. r_ret = is_input_set_as_auto_advance(which);
  555. } else if (what == "reset") {
  556. r_ret = is_input_reset(which);
  557. } else {
  558. return false;
  559. }
  560. return true;
  561. }
  562. void AnimationNodeTransition::get_parameter_list(List<PropertyInfo> *r_list) const {
  563. String anims;
  564. for (int i = 0; i < get_input_count(); i++) {
  565. if (i > 0) {
  566. anims += ",";
  567. }
  568. anims += inputs[i].name;
  569. }
  570. r_list->push_back(PropertyInfo(Variant::STRING, current_state, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY)); // For interface.
  571. r_list->push_back(PropertyInfo(Variant::STRING, transition_request, PROPERTY_HINT_ENUM, anims)); // For transition request. It will be cleared after setting the value immediately.
  572. r_list->push_back(PropertyInfo(Variant::INT, current_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY)); // To avoid finding the index every frame, use this internally.
  573. r_list->push_back(PropertyInfo(Variant::INT, prev_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  574. r_list->push_back(PropertyInfo(Variant::FLOAT, time, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  575. r_list->push_back(PropertyInfo(Variant::FLOAT, prev_xfading, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  576. }
  577. Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p_parameter) const {
  578. if (p_parameter == time || p_parameter == prev_xfading) {
  579. return 0.0;
  580. } else if (p_parameter == prev_index || p_parameter == current_index) {
  581. return -1;
  582. } else {
  583. return String();
  584. }
  585. }
  586. bool AnimationNodeTransition::is_parameter_read_only(const StringName &p_parameter) const {
  587. if (p_parameter == current_state || p_parameter == current_index) {
  588. return true;
  589. }
  590. return false;
  591. }
  592. String AnimationNodeTransition::get_caption() const {
  593. return "Transition";
  594. }
  595. void AnimationNodeTransition::set_input_count(int p_inputs) {
  596. for (int i = get_input_count(); i < p_inputs; i++) {
  597. add_input("state_" + itos(i));
  598. }
  599. while (get_input_count() > p_inputs) {
  600. remove_input(get_input_count() - 1);
  601. }
  602. pending_update = true;
  603. emit_signal(SNAME("tree_changed")); // For updating connect activity map.
  604. notify_property_list_changed();
  605. }
  606. bool AnimationNodeTransition::add_input(const String &p_name) {
  607. if (AnimationNode::add_input(p_name)) {
  608. input_data.push_back(InputData());
  609. return true;
  610. }
  611. return false;
  612. }
  613. void AnimationNodeTransition::remove_input(int p_index) {
  614. input_data.remove_at(p_index);
  615. AnimationNode::remove_input(p_index);
  616. }
  617. bool AnimationNodeTransition::set_input_name(int p_input, const String &p_name) {
  618. pending_update = true;
  619. return AnimationNode::set_input_name(p_input, p_name);
  620. }
  621. void AnimationNodeTransition::set_input_as_auto_advance(int p_input, bool p_enable) {
  622. ERR_FAIL_INDEX(p_input, get_input_count());
  623. input_data.write[p_input].auto_advance = p_enable;
  624. }
  625. bool AnimationNodeTransition::is_input_set_as_auto_advance(int p_input) const {
  626. ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
  627. return input_data[p_input].auto_advance;
  628. }
  629. void AnimationNodeTransition::set_input_reset(int p_input, bool p_enable) {
  630. ERR_FAIL_INDEX(p_input, get_input_count());
  631. input_data.write[p_input].reset = p_enable;
  632. }
  633. bool AnimationNodeTransition::is_input_reset(int p_input) const {
  634. ERR_FAIL_INDEX_V(p_input, get_input_count(), true);
  635. return input_data[p_input].reset;
  636. }
  637. void AnimationNodeTransition::set_xfade_time(double p_fade) {
  638. xfade_time = p_fade;
  639. }
  640. double AnimationNodeTransition::get_xfade_time() const {
  641. return xfade_time;
  642. }
  643. void AnimationNodeTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
  644. xfade_curve = p_curve;
  645. }
  646. Ref<Curve> AnimationNodeTransition::get_xfade_curve() const {
  647. return xfade_curve;
  648. }
  649. void AnimationNodeTransition::set_allow_transition_to_self(bool p_enable) {
  650. allow_transition_to_self = p_enable;
  651. }
  652. bool AnimationNodeTransition::is_allow_transition_to_self() const {
  653. return allow_transition_to_self;
  654. }
  655. double AnimationNodeTransition::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  656. String cur_transition_request = get_parameter(transition_request);
  657. int cur_current_index = get_parameter(current_index);
  658. int cur_prev_index = get_parameter(prev_index);
  659. double cur_time = get_parameter(time);
  660. double cur_prev_xfading = get_parameter(prev_xfading);
  661. bool switched = false;
  662. bool restart = false;
  663. bool clear_remaining_fade = false;
  664. if (pending_update) {
  665. if (cur_current_index < 0 || cur_current_index >= get_input_count()) {
  666. set_parameter(prev_index, -1);
  667. if (get_input_count() > 0) {
  668. set_parameter(current_index, 0);
  669. set_parameter(current_state, get_input_name(0));
  670. } else {
  671. set_parameter(current_index, -1);
  672. set_parameter(current_state, StringName());
  673. }
  674. } else {
  675. set_parameter(current_state, get_input_name(cur_current_index));
  676. }
  677. pending_update = false;
  678. }
  679. if (p_time == 0 && p_seek && !p_is_external_seeking) {
  680. clear_remaining_fade = true; // Reset occurs.
  681. }
  682. if (!cur_transition_request.is_empty()) {
  683. int new_idx = find_input(cur_transition_request);
  684. if (new_idx >= 0) {
  685. if (cur_current_index == new_idx) {
  686. if (allow_transition_to_self) {
  687. // Transition to same state.
  688. restart = input_data[cur_current_index].reset;
  689. clear_remaining_fade = true;
  690. }
  691. } else {
  692. switched = true;
  693. cur_prev_index = cur_current_index;
  694. set_parameter(prev_index, cur_current_index);
  695. cur_current_index = new_idx;
  696. set_parameter(current_index, cur_current_index);
  697. set_parameter(current_state, cur_transition_request);
  698. }
  699. } else {
  700. ERR_PRINT("No such input: '" + cur_transition_request + "'");
  701. }
  702. cur_transition_request = String();
  703. set_parameter(transition_request, cur_transition_request);
  704. }
  705. if (clear_remaining_fade) {
  706. cur_prev_xfading = 0;
  707. set_parameter(prev_xfading, 0);
  708. cur_prev_index = -1;
  709. set_parameter(prev_index, -1);
  710. }
  711. // Special case for restart.
  712. if (restart) {
  713. set_parameter(time, 0);
  714. return blend_input(cur_current_index, 0, true, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  715. }
  716. if (switched) {
  717. cur_prev_xfading = xfade_time;
  718. cur_time = 0;
  719. }
  720. if (cur_current_index < 0 || cur_current_index >= get_input_count() || cur_prev_index >= get_input_count()) {
  721. return 0;
  722. }
  723. double rem = 0.0;
  724. if (sync) {
  725. for (int i = 0; i < get_input_count(); i++) {
  726. if (i != cur_current_index && i != cur_prev_index) {
  727. blend_input(i, p_time, p_seek, p_is_external_seeking, 0, FILTER_IGNORE, true);
  728. }
  729. }
  730. }
  731. if (cur_prev_index < 0) { // Process current animation, check for transition.
  732. rem = blend_input(cur_current_index, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  733. if (p_seek) {
  734. cur_time = p_time;
  735. } else {
  736. cur_time += p_time;
  737. }
  738. if (input_data[cur_current_index].auto_advance && rem <= xfade_time) {
  739. set_parameter(transition_request, get_input_name((cur_current_index + 1) % get_input_count()));
  740. }
  741. } else { // Cross-fading from prev to current.
  742. real_t blend = 0.0;
  743. real_t blend_inv = 1.0;
  744. bool use_blend = sync;
  745. if (xfade_time > 0) {
  746. use_blend = true;
  747. blend = cur_prev_xfading / xfade_time;
  748. if (xfade_curve.is_valid()) {
  749. blend = xfade_curve->sample(blend);
  750. }
  751. blend_inv = 1.0 - blend;
  752. blend = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;
  753. blend_inv = Math::is_zero_approx(blend_inv) ? CMP_EPSILON : blend_inv;
  754. }
  755. // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  756. if (input_data[cur_current_index].reset && !p_seek && switched) { // Just switched, seek to start of current.
  757. rem = blend_input(cur_current_index, 0, true, p_is_external_seeking, blend_inv, FILTER_IGNORE, true);
  758. } else {
  759. rem = blend_input(cur_current_index, p_time, p_seek, p_is_external_seeking, blend_inv, FILTER_IGNORE, true);
  760. }
  761. blend_input(cur_prev_index, p_time, use_blend && p_seek, p_is_external_seeking, blend, FILTER_IGNORE, true);
  762. if (p_seek) {
  763. cur_time = p_time;
  764. } else {
  765. cur_time += p_time;
  766. cur_prev_xfading -= p_time;
  767. if (cur_prev_xfading < 0) {
  768. set_parameter(prev_index, -1);
  769. }
  770. }
  771. }
  772. set_parameter(time, cur_time);
  773. set_parameter(prev_xfading, cur_prev_xfading);
  774. return rem;
  775. }
  776. void AnimationNodeTransition::_get_property_list(List<PropertyInfo> *p_list) const {
  777. for (int i = 0; i < get_input_count(); i++) {
  778. p_list->push_back(PropertyInfo(Variant::STRING, "input_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  779. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/auto_advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  780. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/reset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  781. }
  782. }
  783. void AnimationNodeTransition::_bind_methods() {
  784. ClassDB::bind_method(D_METHOD("set_input_count", "input_count"), &AnimationNodeTransition::set_input_count);
  785. ClassDB::bind_method(D_METHOD("set_input_as_auto_advance", "input", "enable"), &AnimationNodeTransition::set_input_as_auto_advance);
  786. ClassDB::bind_method(D_METHOD("is_input_set_as_auto_advance", "input"), &AnimationNodeTransition::is_input_set_as_auto_advance);
  787. ClassDB::bind_method(D_METHOD("set_input_reset", "input", "enable"), &AnimationNodeTransition::set_input_reset);
  788. ClassDB::bind_method(D_METHOD("is_input_reset", "input"), &AnimationNodeTransition::is_input_reset);
  789. ClassDB::bind_method(D_METHOD("set_xfade_time", "time"), &AnimationNodeTransition::set_xfade_time);
  790. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeTransition::get_xfade_time);
  791. ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeTransition::set_xfade_curve);
  792. ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeTransition::get_xfade_curve);
  793. ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeTransition::set_allow_transition_to_self);
  794. ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeTransition::is_allow_transition_to_self);
  795. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
  796. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
  797. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
  798. ADD_PROPERTY(PropertyInfo(Variant::INT, "input_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED, "Inputs,input_"), "set_input_count", "get_input_count");
  799. }
  800. AnimationNodeTransition::AnimationNodeTransition() {
  801. }
  802. /////////////////////
  803. String AnimationNodeOutput::get_caption() const {
  804. return "Output";
  805. }
  806. double AnimationNodeOutput::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  807. return blend_input(0, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  808. }
  809. AnimationNodeOutput::AnimationNodeOutput() {
  810. add_input("output");
  811. }
  812. ///////////////////////////////////////////////////////
  813. void AnimationNodeBlendTree::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  814. ERR_FAIL_COND(nodes.has(p_name));
  815. ERR_FAIL_COND(p_node.is_null());
  816. ERR_FAIL_COND(p_name == SceneStringNames::get_singleton()->output);
  817. ERR_FAIL_COND(String(p_name).contains("/"));
  818. Node n;
  819. n.node = p_node;
  820. n.position = p_position;
  821. n.connections.resize(n.node->get_input_count());
  822. nodes[p_name] = n;
  823. emit_changed();
  824. emit_signal(SNAME("tree_changed"));
  825. p_node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendTree::_tree_changed), CONNECT_REFERENCE_COUNTED);
  826. p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  827. p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  828. p_node->connect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_name), CONNECT_REFERENCE_COUNTED);
  829. }
  830. Ref<AnimationNode> AnimationNodeBlendTree::get_node(const StringName &p_name) const {
  831. ERR_FAIL_COND_V(!nodes.has(p_name), Ref<AnimationNode>());
  832. return nodes[p_name].node;
  833. }
  834. StringName AnimationNodeBlendTree::get_node_name(const Ref<AnimationNode> &p_node) const {
  835. for (const KeyValue<StringName, Node> &E : nodes) {
  836. if (E.value.node == p_node) {
  837. return E.key;
  838. }
  839. }
  840. ERR_FAIL_V(StringName());
  841. }
  842. void AnimationNodeBlendTree::set_node_position(const StringName &p_node, const Vector2 &p_position) {
  843. ERR_FAIL_COND(!nodes.has(p_node));
  844. nodes[p_node].position = p_position;
  845. }
  846. Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) const {
  847. ERR_FAIL_COND_V(!nodes.has(p_node), Vector2());
  848. return nodes[p_node].position;
  849. }
  850. void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
  851. Vector<StringName> ns;
  852. for (const KeyValue<StringName, Node> &E : nodes) {
  853. ns.push_back(E.key);
  854. }
  855. ns.sort_custom<StringName::AlphCompare>();
  856. for (int i = 0; i < ns.size(); i++) {
  857. ChildNode cn;
  858. cn.name = ns[i];
  859. cn.node = nodes[cn.name].node;
  860. r_child_nodes->push_back(cn);
  861. }
  862. }
  863. bool AnimationNodeBlendTree::has_node(const StringName &p_name) const {
  864. return nodes.has(p_name);
  865. }
  866. Vector<StringName> AnimationNodeBlendTree::get_node_connection_array(const StringName &p_name) const {
  867. ERR_FAIL_COND_V(!nodes.has(p_name), Vector<StringName>());
  868. return nodes[p_name].connections;
  869. }
  870. void AnimationNodeBlendTree::remove_node(const StringName &p_name) {
  871. ERR_FAIL_COND(!nodes.has(p_name));
  872. ERR_FAIL_COND(p_name == SceneStringNames::get_singleton()->output); //can't delete output
  873. {
  874. Ref<AnimationNode> node = nodes[p_name].node;
  875. node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendTree::_tree_changed));
  876. node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed));
  877. node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed));
  878. node->disconnect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  879. }
  880. nodes.erase(p_name);
  881. // Erase connections to name.
  882. for (KeyValue<StringName, Node> &E : nodes) {
  883. for (int i = 0; i < E.value.connections.size(); i++) {
  884. if (E.value.connections[i] == p_name) {
  885. E.value.connections.write[i] = StringName();
  886. }
  887. }
  888. }
  889. emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
  890. emit_changed();
  891. emit_signal(SNAME("tree_changed"));
  892. }
  893. void AnimationNodeBlendTree::rename_node(const StringName &p_name, const StringName &p_new_name) {
  894. ERR_FAIL_COND(!nodes.has(p_name));
  895. ERR_FAIL_COND(nodes.has(p_new_name));
  896. ERR_FAIL_COND(p_name == SceneStringNames::get_singleton()->output);
  897. ERR_FAIL_COND(p_new_name == SceneStringNames::get_singleton()->output);
  898. nodes[p_name].node->disconnect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  899. nodes[p_new_name] = nodes[p_name];
  900. nodes.erase(p_name);
  901. // Rename connections.
  902. for (KeyValue<StringName, Node> &E : nodes) {
  903. for (int i = 0; i < E.value.connections.size(); i++) {
  904. if (E.value.connections[i] == p_name) {
  905. E.value.connections.write[i] = p_new_name;
  906. }
  907. }
  908. }
  909. // Connection must be done with new name.
  910. nodes[p_new_name].node->connect("changed", callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_new_name), CONNECT_REFERENCE_COUNTED);
  911. emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
  912. emit_signal(SNAME("tree_changed"));
  913. }
  914. void AnimationNodeBlendTree::connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) {
  915. ERR_FAIL_COND(!nodes.has(p_output_node));
  916. ERR_FAIL_COND(!nodes.has(p_input_node));
  917. ERR_FAIL_COND(p_output_node == SceneStringNames::get_singleton()->output);
  918. ERR_FAIL_COND(p_input_node == p_output_node);
  919. Ref<AnimationNode> input = nodes[p_input_node].node;
  920. ERR_FAIL_INDEX(p_input_index, nodes[p_input_node].connections.size());
  921. for (KeyValue<StringName, Node> &E : nodes) {
  922. for (int i = 0; i < E.value.connections.size(); i++) {
  923. StringName output = E.value.connections[i];
  924. ERR_FAIL_COND(output == p_output_node);
  925. }
  926. }
  927. nodes[p_input_node].connections.write[p_input_index] = p_output_node;
  928. emit_changed();
  929. }
  930. void AnimationNodeBlendTree::disconnect_node(const StringName &p_node, int p_input_index) {
  931. ERR_FAIL_COND(!nodes.has(p_node));
  932. Ref<AnimationNode> input = nodes[p_node].node;
  933. ERR_FAIL_INDEX(p_input_index, nodes[p_node].connections.size());
  934. nodes[p_node].connections.write[p_input_index] = StringName();
  935. }
  936. AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const {
  937. if (!nodes.has(p_output_node) || p_output_node == SceneStringNames::get_singleton()->output) {
  938. return CONNECTION_ERROR_NO_OUTPUT;
  939. }
  940. if (!nodes.has(p_input_node)) {
  941. return CONNECTION_ERROR_NO_INPUT;
  942. }
  943. if (p_input_node == p_output_node) {
  944. return CONNECTION_ERROR_SAME_NODE;
  945. }
  946. Ref<AnimationNode> input = nodes[p_input_node].node;
  947. if (p_input_index < 0 || p_input_index >= nodes[p_input_node].connections.size()) {
  948. return CONNECTION_ERROR_NO_INPUT_INDEX;
  949. }
  950. if (nodes[p_input_node].connections[p_input_index] != StringName()) {
  951. return CONNECTION_ERROR_CONNECTION_EXISTS;
  952. }
  953. for (const KeyValue<StringName, Node> &E : nodes) {
  954. for (int i = 0; i < E.value.connections.size(); i++) {
  955. const StringName output = E.value.connections[i];
  956. if (output == p_output_node) {
  957. return CONNECTION_ERROR_CONNECTION_EXISTS;
  958. }
  959. }
  960. }
  961. return CONNECTION_OK;
  962. }
  963. void AnimationNodeBlendTree::get_node_connections(List<NodeConnection> *r_connections) const {
  964. for (const KeyValue<StringName, Node> &E : nodes) {
  965. for (int i = 0; i < E.value.connections.size(); i++) {
  966. const StringName output = E.value.connections[i];
  967. if (output != StringName()) {
  968. NodeConnection nc;
  969. nc.input_node = E.key;
  970. nc.input_index = i;
  971. nc.output_node = output;
  972. r_connections->push_back(nc);
  973. }
  974. }
  975. }
  976. }
  977. String AnimationNodeBlendTree::get_caption() const {
  978. return "BlendTree";
  979. }
  980. double AnimationNodeBlendTree::process(double p_time, bool p_seek, bool p_is_external_seeking) {
  981. Ref<AnimationNodeOutput> output = nodes[SceneStringNames::get_singleton()->output].node;
  982. return _blend_node("output", nodes[SceneStringNames::get_singleton()->output].connections, this, output, p_time, p_seek, p_is_external_seeking, 1.0, FILTER_IGNORE, true);
  983. }
  984. void AnimationNodeBlendTree::get_node_list(List<StringName> *r_list) {
  985. for (const KeyValue<StringName, Node> &E : nodes) {
  986. r_list->push_back(E.key);
  987. }
  988. }
  989. void AnimationNodeBlendTree::set_graph_offset(const Vector2 &p_graph_offset) {
  990. graph_offset = p_graph_offset;
  991. }
  992. Vector2 AnimationNodeBlendTree::get_graph_offset() const {
  993. return graph_offset;
  994. }
  995. Ref<AnimationNode> AnimationNodeBlendTree::get_child_by_name(const StringName &p_name) {
  996. return get_node(p_name);
  997. }
  998. bool AnimationNodeBlendTree::_set(const StringName &p_name, const Variant &p_value) {
  999. String prop_name = p_name;
  1000. if (prop_name.begins_with("nodes/")) {
  1001. String node_name = prop_name.get_slicec('/', 1);
  1002. String what = prop_name.get_slicec('/', 2);
  1003. if (what == "node") {
  1004. Ref<AnimationNode> anode = p_value;
  1005. if (anode.is_valid()) {
  1006. add_node(node_name, p_value);
  1007. }
  1008. return true;
  1009. }
  1010. if (what == "position") {
  1011. if (nodes.has(node_name)) {
  1012. nodes[node_name].position = p_value;
  1013. }
  1014. return true;
  1015. }
  1016. } else if (prop_name == "node_connections") {
  1017. Array conns = p_value;
  1018. ERR_FAIL_COND_V(conns.size() % 3 != 0, false);
  1019. for (int i = 0; i < conns.size(); i += 3) {
  1020. connect_node(conns[i], conns[i + 1], conns[i + 2]);
  1021. }
  1022. return true;
  1023. }
  1024. return false;
  1025. }
  1026. bool AnimationNodeBlendTree::_get(const StringName &p_name, Variant &r_ret) const {
  1027. String prop_name = p_name;
  1028. if (prop_name.begins_with("nodes/")) {
  1029. String node_name = prop_name.get_slicec('/', 1);
  1030. String what = prop_name.get_slicec('/', 2);
  1031. if (what == "node") {
  1032. if (nodes.has(node_name)) {
  1033. r_ret = nodes[node_name].node;
  1034. return true;
  1035. }
  1036. }
  1037. if (what == "position") {
  1038. if (nodes.has(node_name)) {
  1039. r_ret = nodes[node_name].position;
  1040. return true;
  1041. }
  1042. }
  1043. } else if (prop_name == "node_connections") {
  1044. List<NodeConnection> nc;
  1045. get_node_connections(&nc);
  1046. Array conns;
  1047. conns.resize(nc.size() * 3);
  1048. int idx = 0;
  1049. for (const NodeConnection &E : nc) {
  1050. conns[idx * 3 + 0] = E.input_node;
  1051. conns[idx * 3 + 1] = E.input_index;
  1052. conns[idx * 3 + 2] = E.output_node;
  1053. idx++;
  1054. }
  1055. r_ret = conns;
  1056. return true;
  1057. }
  1058. return false;
  1059. }
  1060. void AnimationNodeBlendTree::_get_property_list(List<PropertyInfo> *p_list) const {
  1061. List<StringName> names;
  1062. for (const KeyValue<StringName, Node> &E : nodes) {
  1063. names.push_back(E.key);
  1064. }
  1065. names.sort_custom<StringName::AlphCompare>();
  1066. for (const StringName &E : names) {
  1067. String prop_name = E;
  1068. if (prop_name != "output") {
  1069. p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1070. }
  1071. p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1072. }
  1073. p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1074. }
  1075. void AnimationNodeBlendTree::_tree_changed() {
  1076. AnimationRootNode::_tree_changed();
  1077. }
  1078. void AnimationNodeBlendTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  1079. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  1080. }
  1081. void AnimationNodeBlendTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  1082. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  1083. }
  1084. void AnimationNodeBlendTree::reset_state() {
  1085. graph_offset = Vector2();
  1086. nodes.clear();
  1087. _initialize_node_tree();
  1088. emit_changed();
  1089. emit_signal(SNAME("tree_changed"));
  1090. }
  1091. void AnimationNodeBlendTree::_node_changed(const StringName &p_node) {
  1092. ERR_FAIL_COND(!nodes.has(p_node));
  1093. nodes[p_node].connections.resize(nodes[p_node].node->get_input_count());
  1094. emit_signal(SNAME("node_changed"), p_node);
  1095. }
  1096. void AnimationNodeBlendTree::_bind_methods() {
  1097. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeBlendTree::add_node, DEFVAL(Vector2()));
  1098. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeBlendTree::get_node);
  1099. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeBlendTree::remove_node);
  1100. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeBlendTree::rename_node);
  1101. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeBlendTree::has_node);
  1102. ClassDB::bind_method(D_METHOD("connect_node", "input_node", "input_index", "output_node"), &AnimationNodeBlendTree::connect_node);
  1103. ClassDB::bind_method(D_METHOD("disconnect_node", "input_node", "input_index"), &AnimationNodeBlendTree::disconnect_node);
  1104. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeBlendTree::set_node_position);
  1105. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeBlendTree::get_node_position);
  1106. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeBlendTree::set_graph_offset);
  1107. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeBlendTree::get_graph_offset);
  1108. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");
  1109. BIND_CONSTANT(CONNECTION_OK);
  1110. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT);
  1111. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT_INDEX);
  1112. BIND_CONSTANT(CONNECTION_ERROR_NO_OUTPUT);
  1113. BIND_CONSTANT(CONNECTION_ERROR_SAME_NODE);
  1114. BIND_CONSTANT(CONNECTION_ERROR_CONNECTION_EXISTS);
  1115. ADD_SIGNAL(MethodInfo("node_changed", PropertyInfo(Variant::STRING_NAME, "node_name")));
  1116. }
  1117. void AnimationNodeBlendTree::_initialize_node_tree() {
  1118. Ref<AnimationNodeOutput> output;
  1119. output.instantiate();
  1120. Node n;
  1121. n.node = output;
  1122. n.position = Vector2(300, 150);
  1123. n.connections.resize(1);
  1124. nodes["output"] = n;
  1125. }
  1126. AnimationNodeBlendTree::AnimationNodeBlendTree() {
  1127. _initialize_node_tree();
  1128. }
  1129. AnimationNodeBlendTree::~AnimationNodeBlendTree() {
  1130. }