animation_blend_tree.cpp 52 KB

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