animation_blend_tree.cpp 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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. void AnimationNodeAnimation::set_animation(const StringName &p_name) {
  33. animation = p_name;
  34. }
  35. StringName AnimationNodeAnimation::get_animation() const {
  36. return animation;
  37. }
  38. Vector<String> (*AnimationNodeAnimation::get_editable_animation_list)() = nullptr;
  39. void AnimationNodeAnimation::get_parameter_list(List<PropertyInfo> *r_list) const {
  40. AnimationNode::get_parameter_list(r_list);
  41. r_list->push_back(PropertyInfo(Variant::BOOL, backward, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  42. }
  43. Variant AnimationNodeAnimation::get_parameter_default_value(const StringName &p_parameter) const {
  44. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  45. if (ret != Variant()) {
  46. return ret;
  47. }
  48. if (p_parameter == backward) {
  49. return false;
  50. }
  51. return 0;
  52. }
  53. AnimationNode::NodeTimeInfo AnimationNodeAnimation::get_node_time_info() const {
  54. NodeTimeInfo nti;
  55. if (!process_state->tree->has_animation(animation)) {
  56. return nti;
  57. }
  58. if (use_custom_timeline) {
  59. nti.length = timeline_length;
  60. nti.loop_mode = loop_mode;
  61. } else {
  62. Ref<Animation> anim = process_state->tree->get_animation(animation);
  63. nti.length = (double)anim->get_length();
  64. nti.loop_mode = anim->get_loop_mode();
  65. }
  66. nti.position = get_parameter(current_position);
  67. return nti;
  68. }
  69. void AnimationNodeAnimation::_validate_property(PropertyInfo &p_property) const {
  70. if (p_property.name == "animation" && get_editable_animation_list) {
  71. Vector<String> names = get_editable_animation_list();
  72. String anims;
  73. for (int i = 0; i < names.size(); i++) {
  74. if (i > 0) {
  75. anims += ",";
  76. }
  77. anims += String(names[i]);
  78. }
  79. if (!anims.is_empty()) {
  80. p_property.hint = PROPERTY_HINT_ENUM;
  81. p_property.hint_string = anims;
  82. }
  83. }
  84. if (!use_custom_timeline) {
  85. if (p_property.name == "timeline_length" || p_property.name == "start_offset" || p_property.name == "loop_mode" || p_property.name == "stretch_time_scale") {
  86. p_property.usage = PROPERTY_USAGE_NONE;
  87. }
  88. }
  89. }
  90. AnimationNode::NodeTimeInfo AnimationNodeAnimation::process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  91. process_state->is_testing = p_test_only;
  92. AnimationMixer::PlaybackInfo pi = p_playback_info;
  93. if (p_playback_info.seeked) {
  94. if (p_playback_info.is_external_seeking) {
  95. pi.delta = get_node_time_info().position - p_playback_info.time;
  96. }
  97. } else {
  98. pi.time = get_node_time_info().position + (get_parameter(backward) ? -p_playback_info.delta : p_playback_info.delta);
  99. }
  100. NodeTimeInfo nti = _process(pi, p_test_only);
  101. if (!p_test_only) {
  102. set_node_time_info(nti);
  103. }
  104. return nti;
  105. }
  106. AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  107. if (!process_state->tree->has_animation(animation)) {
  108. AnimationNodeBlendTree *tree = Object::cast_to<AnimationNodeBlendTree>(node_state.parent);
  109. if (tree) {
  110. String node_name = tree->get_node_name(Ref<AnimationNodeAnimation>(this));
  111. make_invalid(vformat(RTR("On BlendTree node '%s', animation not found: '%s'"), node_name, animation));
  112. } else {
  113. make_invalid(vformat(RTR("Animation not found: '%s'"), animation));
  114. }
  115. return NodeTimeInfo();
  116. }
  117. Ref<Animation> anim = process_state->tree->get_animation(animation);
  118. double anim_size = (double)anim->get_length();
  119. NodeTimeInfo cur_nti = get_node_time_info();
  120. double cur_len = cur_nti.length;
  121. double cur_time = p_playback_info.time;
  122. double cur_delta = p_playback_info.delta;
  123. bool cur_backward = get_parameter(backward);
  124. Animation::LoopMode cur_loop_mode = cur_nti.loop_mode;
  125. double prev_time = cur_nti.position;
  126. Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
  127. bool node_backward = play_mode == PLAY_MODE_BACKWARD;
  128. bool p_seek = p_playback_info.seeked;
  129. bool p_is_external_seeking = p_playback_info.is_external_seeking;
  130. // 1. Progress for AnimationNode.
  131. bool will_end = Animation::is_greater_or_equal_approx(cur_time + cur_delta, cur_len);
  132. bool is_started = p_seek && !p_is_external_seeking && Math::is_zero_approx(cur_time);
  133. // 1. Progress for AnimationNode.
  134. if (is_started && advance_on_start) {
  135. cur_time = cur_delta;
  136. }
  137. if (cur_loop_mode != Animation::LOOP_NONE) {
  138. if (cur_loop_mode == Animation::LOOP_LINEAR) {
  139. if (!Math::is_zero_approx(cur_len)) {
  140. cur_time = Math::fposmod(cur_time, cur_len);
  141. }
  142. cur_backward = false;
  143. } else {
  144. if (!Math::is_zero_approx(cur_len)) {
  145. if (Animation::is_greater_or_equal_approx(prev_time, 0) && Animation::is_less_approx(cur_time, 0)) {
  146. cur_backward = !cur_backward;
  147. } else if (Animation::is_less_or_equal_approx(prev_time, cur_len) && Animation::is_greater_approx(cur_time, cur_len)) {
  148. cur_backward = !cur_backward;
  149. }
  150. cur_time = Math::pingpong(cur_time, cur_len);
  151. }
  152. }
  153. } else {
  154. if (Animation::is_less_approx(cur_time, 0)) {
  155. cur_delta += cur_time;
  156. cur_time = 0;
  157. } else if (Animation::is_greater_approx(cur_time, cur_len)) {
  158. cur_delta += cur_time - cur_len;
  159. cur_time = cur_len;
  160. }
  161. cur_backward = false;
  162. // If ended, don't progress AnimationNode. So set delta to 0.
  163. if (!Math::is_zero_approx(cur_delta)) {
  164. if (play_mode == PLAY_MODE_FORWARD) {
  165. if (Animation::is_greater_or_equal_approx(prev_time, cur_len)) {
  166. cur_delta = 0;
  167. }
  168. } else {
  169. if (Animation::is_less_or_equal_approx(prev_time, 0)) {
  170. cur_delta = 0;
  171. }
  172. }
  173. }
  174. }
  175. // 2. For return, store "AnimationNode" time info here, not "Animation" time info as below.
  176. NodeTimeInfo nti;
  177. nti.length = cur_len;
  178. nti.position = cur_time;
  179. nti.delta = cur_delta;
  180. nti.loop_mode = cur_loop_mode;
  181. nti.will_end = will_end;
  182. // 3. Progress for Animation.
  183. double prev_playback_time = prev_time + start_offset;
  184. double cur_playback_time = cur_time + start_offset;
  185. if (stretch_time_scale) {
  186. double mlt = anim_size / cur_len;
  187. prev_playback_time *= mlt;
  188. cur_playback_time *= mlt;
  189. cur_delta *= mlt;
  190. }
  191. if (cur_loop_mode == Animation::LOOP_LINEAR) {
  192. if (!Math::is_zero_approx(anim_size)) {
  193. prev_playback_time = Math::fposmod(prev_playback_time, anim_size);
  194. cur_playback_time = Math::fposmod(cur_playback_time, anim_size);
  195. if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) {
  196. looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
  197. }
  198. if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) {
  199. looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
  200. }
  201. }
  202. } else if (cur_loop_mode == Animation::LOOP_PINGPONG) {
  203. if (!Math::is_zero_approx(anim_size)) {
  204. if (Animation::is_greater_or_equal_approx(Math::fposmod(cur_playback_time, anim_size * 2.0), anim_size)) {
  205. cur_delta = -cur_delta; // Needed for retrieving discrete keys correctly.
  206. }
  207. prev_playback_time = Math::pingpong(prev_playback_time, anim_size);
  208. cur_playback_time = Math::pingpong(cur_playback_time, anim_size);
  209. if (Animation::is_greater_or_equal_approx(prev_playback_time, 0) && Animation::is_less_approx(cur_playback_time, 0)) {
  210. looped_flag = node_backward ? Animation::LOOPED_FLAG_END : Animation::LOOPED_FLAG_START;
  211. }
  212. if (Animation::is_less_or_equal_approx(prev_playback_time, anim_size) && Animation::is_greater_approx(cur_playback_time, anim_size)) {
  213. looped_flag = node_backward ? Animation::LOOPED_FLAG_START : Animation::LOOPED_FLAG_END;
  214. }
  215. }
  216. } else {
  217. if (Animation::is_less_approx(cur_playback_time, 0)) {
  218. cur_playback_time = 0;
  219. } else if (Animation::is_greater_approx(cur_playback_time, anim_size)) {
  220. cur_playback_time = anim_size;
  221. }
  222. // Emit start & finish signal. Internally, the detections are the same for backward.
  223. // We should use call_deferred since the track keys are still being processed.
  224. if (process_state->tree && !p_test_only) {
  225. // AnimationTree uses seek to 0 "internally" to process the first key of the animation, which is used as the start detection.
  226. if (is_started) {
  227. process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_started), animation);
  228. }
  229. // Finished.
  230. if (Animation::is_less_approx(prev_playback_time, anim_size) && Animation::is_greater_or_equal_approx(cur_playback_time, anim_size)) {
  231. cur_playback_time = anim_size;
  232. process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_finished), animation);
  233. }
  234. }
  235. }
  236. if (!p_test_only) {
  237. AnimationMixer::PlaybackInfo pi = p_playback_info;
  238. pi.start = 0.0;
  239. pi.end = cur_len;
  240. if (play_mode == PLAY_MODE_FORWARD) {
  241. pi.time = cur_playback_time;
  242. pi.delta = cur_delta;
  243. } else {
  244. pi.time = anim_size - cur_playback_time;
  245. pi.delta = -cur_delta;
  246. }
  247. pi.weight = 1.0;
  248. pi.looped_flag = looped_flag;
  249. blend_animation(animation, pi);
  250. }
  251. set_parameter(backward, cur_backward);
  252. return nti;
  253. }
  254. String AnimationNodeAnimation::get_caption() const {
  255. return "Animation";
  256. }
  257. void AnimationNodeAnimation::set_play_mode(PlayMode p_play_mode) {
  258. play_mode = p_play_mode;
  259. }
  260. AnimationNodeAnimation::PlayMode AnimationNodeAnimation::get_play_mode() const {
  261. return play_mode;
  262. }
  263. void AnimationNodeAnimation::set_backward(bool p_backward) {
  264. set_parameter(backward, p_backward);
  265. }
  266. bool AnimationNodeAnimation::is_backward() const {
  267. return get_parameter(backward);
  268. }
  269. void AnimationNodeAnimation::set_advance_on_start(bool p_advance_on_start) {
  270. advance_on_start = p_advance_on_start;
  271. }
  272. bool AnimationNodeAnimation::is_advance_on_start() const {
  273. return advance_on_start;
  274. }
  275. void AnimationNodeAnimation::set_use_custom_timeline(bool p_use_custom_timeline) {
  276. use_custom_timeline = p_use_custom_timeline;
  277. notify_property_list_changed();
  278. }
  279. bool AnimationNodeAnimation::is_using_custom_timeline() const {
  280. return use_custom_timeline;
  281. }
  282. void AnimationNodeAnimation::set_timeline_length(double p_length) {
  283. timeline_length = p_length;
  284. }
  285. double AnimationNodeAnimation::get_timeline_length() const {
  286. return timeline_length;
  287. }
  288. void AnimationNodeAnimation::set_stretch_time_scale(bool p_stretch_time_scale) {
  289. stretch_time_scale = p_stretch_time_scale;
  290. notify_property_list_changed();
  291. }
  292. bool AnimationNodeAnimation::is_stretching_time_scale() const {
  293. return stretch_time_scale;
  294. }
  295. void AnimationNodeAnimation::set_start_offset(double p_offset) {
  296. start_offset = p_offset;
  297. }
  298. double AnimationNodeAnimation::get_start_offset() const {
  299. return start_offset;
  300. }
  301. void AnimationNodeAnimation::set_loop_mode(Animation::LoopMode p_loop_mode) {
  302. loop_mode = p_loop_mode;
  303. }
  304. Animation::LoopMode AnimationNodeAnimation::get_loop_mode() const {
  305. return loop_mode;
  306. }
  307. void AnimationNodeAnimation::_bind_methods() {
  308. ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimationNodeAnimation::set_animation);
  309. ClassDB::bind_method(D_METHOD("get_animation"), &AnimationNodeAnimation::get_animation);
  310. ClassDB::bind_method(D_METHOD("set_play_mode", "mode"), &AnimationNodeAnimation::set_play_mode);
  311. ClassDB::bind_method(D_METHOD("get_play_mode"), &AnimationNodeAnimation::get_play_mode);
  312. ClassDB::bind_method(D_METHOD("set_advance_on_start", "advance_on_start"), &AnimationNodeAnimation::set_advance_on_start);
  313. ClassDB::bind_method(D_METHOD("is_advance_on_start"), &AnimationNodeAnimation::is_advance_on_start);
  314. ClassDB::bind_method(D_METHOD("set_use_custom_timeline", "use_custom_timeline"), &AnimationNodeAnimation::set_use_custom_timeline);
  315. ClassDB::bind_method(D_METHOD("is_using_custom_timeline"), &AnimationNodeAnimation::is_using_custom_timeline);
  316. ClassDB::bind_method(D_METHOD("set_timeline_length", "timeline_length"), &AnimationNodeAnimation::set_timeline_length);
  317. ClassDB::bind_method(D_METHOD("get_timeline_length"), &AnimationNodeAnimation::get_timeline_length);
  318. ClassDB::bind_method(D_METHOD("set_stretch_time_scale", "stretch_time_scale"), &AnimationNodeAnimation::set_stretch_time_scale);
  319. ClassDB::bind_method(D_METHOD("is_stretching_time_scale"), &AnimationNodeAnimation::is_stretching_time_scale);
  320. ClassDB::bind_method(D_METHOD("set_start_offset", "start_offset"), &AnimationNodeAnimation::set_start_offset);
  321. ClassDB::bind_method(D_METHOD("get_start_offset"), &AnimationNodeAnimation::get_start_offset);
  322. ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AnimationNodeAnimation::set_loop_mode);
  323. ClassDB::bind_method(D_METHOD("get_loop_mode"), &AnimationNodeAnimation::get_loop_mode);
  324. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
  325. ADD_PROPERTY(PropertyInfo(Variant::INT, "play_mode", PROPERTY_HINT_ENUM, "Forward,Backward"), "set_play_mode", "get_play_mode");
  326. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "advance_on_start"), "set_advance_on_start", "is_advance_on_start");
  327. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_timeline"), "set_use_custom_timeline", "is_using_custom_timeline");
  328. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "timeline_length", PROPERTY_HINT_RANGE, "0.001,60,0.001,or_greater,or_less,hide_slider,suffix:s"), "set_timeline_length", "get_timeline_length");
  329. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stretch_time_scale"), "set_stretch_time_scale", "is_stretching_time_scale");
  330. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "start_offset", PROPERTY_HINT_RANGE, "-60,60,0.001,or_greater,or_less,hide_slider,suffix:s"), "set_start_offset", "get_start_offset");
  331. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "None,Linear,Ping-Pong"), "set_loop_mode", "get_loop_mode");
  332. BIND_ENUM_CONSTANT(PLAY_MODE_FORWARD);
  333. BIND_ENUM_CONSTANT(PLAY_MODE_BACKWARD);
  334. }
  335. AnimationNodeAnimation::AnimationNodeAnimation() {
  336. }
  337. ////////////////////////////////////////////////////////
  338. void AnimationNodeSync::_bind_methods() {
  339. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeSync::set_use_sync);
  340. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeSync::is_using_sync);
  341. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync"), "set_use_sync", "is_using_sync");
  342. }
  343. void AnimationNodeSync::set_use_sync(bool p_sync) {
  344. sync = p_sync;
  345. }
  346. bool AnimationNodeSync::is_using_sync() const {
  347. return sync;
  348. }
  349. AnimationNodeSync::AnimationNodeSync() {
  350. }
  351. ////////////////////////////////////////////////////////
  352. void AnimationNodeOneShot::get_parameter_list(List<PropertyInfo> *r_list) const {
  353. AnimationNode::get_parameter_list(r_list);
  354. r_list->push_back(PropertyInfo(Variant::BOOL, active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY));
  355. r_list->push_back(PropertyInfo(Variant::BOOL, internal_active, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_READ_ONLY));
  356. r_list->push_back(PropertyInfo(Variant::INT, request, PROPERTY_HINT_ENUM, ",Fire,Abort,Fade Out"));
  357. r_list->push_back(PropertyInfo(Variant::FLOAT, fade_in_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  358. r_list->push_back(PropertyInfo(Variant::FLOAT, fade_out_remaining, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  359. r_list->push_back(PropertyInfo(Variant::FLOAT, time_to_restart, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  360. }
  361. Variant AnimationNodeOneShot::get_parameter_default_value(const StringName &p_parameter) const {
  362. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  363. if (ret != Variant()) {
  364. return ret;
  365. }
  366. if (p_parameter == request) {
  367. return ONE_SHOT_REQUEST_NONE;
  368. } else if (p_parameter == active || p_parameter == internal_active) {
  369. return false;
  370. } else if (p_parameter == time_to_restart) {
  371. return -1;
  372. } else {
  373. return 0.0;
  374. }
  375. }
  376. bool AnimationNodeOneShot::is_parameter_read_only(const StringName &p_parameter) const {
  377. if (AnimationNode::is_parameter_read_only(p_parameter)) {
  378. return true;
  379. }
  380. if (p_parameter == active || p_parameter == internal_active) {
  381. return true;
  382. }
  383. return false;
  384. }
  385. void AnimationNodeOneShot::set_fade_in_time(double p_time) {
  386. fade_in = p_time;
  387. }
  388. double AnimationNodeOneShot::get_fade_in_time() const {
  389. return fade_in;
  390. }
  391. void AnimationNodeOneShot::set_fade_out_time(double p_time) {
  392. fade_out = p_time;
  393. }
  394. double AnimationNodeOneShot::get_fade_out_time() const {
  395. return fade_out;
  396. }
  397. void AnimationNodeOneShot::set_fade_in_curve(const Ref<Curve> &p_curve) {
  398. fade_in_curve = p_curve;
  399. }
  400. Ref<Curve> AnimationNodeOneShot::get_fade_in_curve() const {
  401. return fade_in_curve;
  402. }
  403. void AnimationNodeOneShot::set_fade_out_curve(const Ref<Curve> &p_curve) {
  404. fade_out_curve = p_curve;
  405. }
  406. Ref<Curve> AnimationNodeOneShot::get_fade_out_curve() const {
  407. return fade_out_curve;
  408. }
  409. void AnimationNodeOneShot::set_auto_restart_enabled(bool p_enabled) {
  410. auto_restart = p_enabled;
  411. }
  412. void AnimationNodeOneShot::set_auto_restart_delay(double p_time) {
  413. auto_restart_delay = p_time;
  414. }
  415. void AnimationNodeOneShot::set_auto_restart_random_delay(double p_time) {
  416. auto_restart_random_delay = p_time;
  417. }
  418. bool AnimationNodeOneShot::is_auto_restart_enabled() const {
  419. return auto_restart;
  420. }
  421. double AnimationNodeOneShot::get_auto_restart_delay() const {
  422. return auto_restart_delay;
  423. }
  424. double AnimationNodeOneShot::get_auto_restart_random_delay() const {
  425. return auto_restart_random_delay;
  426. }
  427. void AnimationNodeOneShot::set_mix_mode(MixMode p_mix) {
  428. mix = p_mix;
  429. }
  430. AnimationNodeOneShot::MixMode AnimationNodeOneShot::get_mix_mode() const {
  431. return mix;
  432. }
  433. void AnimationNodeOneShot::set_break_loop_at_end(bool p_enable) {
  434. break_loop_at_end = p_enable;
  435. }
  436. bool AnimationNodeOneShot::is_loop_broken_at_end() const {
  437. return break_loop_at_end;
  438. }
  439. String AnimationNodeOneShot::get_caption() const {
  440. return "OneShot";
  441. }
  442. bool AnimationNodeOneShot::has_filter() const {
  443. return true;
  444. }
  445. AnimationNode::NodeTimeInfo AnimationNodeOneShot::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  446. OneShotRequest cur_request = static_cast<OneShotRequest>((int)get_parameter(request));
  447. bool cur_active = get_parameter(active);
  448. bool cur_internal_active = get_parameter(internal_active);
  449. NodeTimeInfo cur_nti = get_node_time_info();
  450. double cur_time_to_restart = get_parameter(time_to_restart);
  451. double cur_fade_in_remaining = get_parameter(fade_in_remaining);
  452. double cur_fade_out_remaining = get_parameter(fade_out_remaining);
  453. set_parameter(request, ONE_SHOT_REQUEST_NONE);
  454. bool is_shooting = true;
  455. bool clear_remaining_fade = false;
  456. bool is_fading_out = cur_active == true && cur_internal_active == false;
  457. double p_time = p_playback_info.time;
  458. double p_delta = p_playback_info.delta;
  459. double abs_delta = Math::abs(p_delta);
  460. bool p_seek = p_playback_info.seeked;
  461. bool p_is_external_seeking = p_playback_info.is_external_seeking;
  462. if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
  463. clear_remaining_fade = true; // Reset occurs.
  464. }
  465. bool do_start = cur_request == ONE_SHOT_REQUEST_FIRE;
  466. if (cur_request == ONE_SHOT_REQUEST_ABORT) {
  467. set_parameter(internal_active, false);
  468. set_parameter(active, false);
  469. set_parameter(time_to_restart, -1);
  470. is_shooting = false;
  471. } else if (cur_request == ONE_SHOT_REQUEST_FADE_OUT && !is_fading_out) { // If fading, keep current fade.
  472. if (cur_active) {
  473. // Request fading.
  474. is_fading_out = true;
  475. cur_fade_out_remaining = fade_out;
  476. cur_fade_in_remaining = 0;
  477. } else {
  478. // Shot is ended, do nothing.
  479. is_shooting = false;
  480. }
  481. set_parameter(internal_active, false);
  482. set_parameter(time_to_restart, -1);
  483. } else if (!do_start && !cur_active) {
  484. if (Animation::is_greater_or_equal_approx(cur_time_to_restart, 0) && !p_seek) {
  485. cur_time_to_restart -= abs_delta;
  486. if (Animation::is_less_approx(cur_time_to_restart, 0)) {
  487. do_start = true; // Restart.
  488. }
  489. set_parameter(time_to_restart, cur_time_to_restart);
  490. }
  491. if (!do_start) {
  492. is_shooting = false;
  493. }
  494. }
  495. bool os_seek = p_seek;
  496. if (clear_remaining_fade) {
  497. os_seek = false;
  498. cur_fade_out_remaining = 0;
  499. set_parameter(fade_out_remaining, 0);
  500. if (is_fading_out) {
  501. is_fading_out = false;
  502. set_parameter(internal_active, false);
  503. set_parameter(active, false);
  504. }
  505. }
  506. if (!is_shooting) {
  507. AnimationMixer::PlaybackInfo pi = p_playback_info;
  508. pi.weight = 1.0;
  509. return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
  510. }
  511. if (do_start) {
  512. os_seek = true;
  513. if (!cur_internal_active) {
  514. cur_fade_in_remaining = fade_in; // If already active, don't fade-in again.
  515. }
  516. cur_internal_active = true;
  517. set_parameter(request, ONE_SHOT_REQUEST_NONE);
  518. set_parameter(internal_active, true);
  519. set_parameter(active, true);
  520. }
  521. real_t blend = 1.0;
  522. bool use_blend = sync;
  523. if (Animation::is_greater_approx(cur_fade_in_remaining, 0)) {
  524. if (Animation::is_greater_approx(fade_in, 0)) {
  525. use_blend = true;
  526. blend = (fade_in - cur_fade_in_remaining) / fade_in;
  527. if (fade_in_curve.is_valid()) {
  528. blend = fade_in_curve->sample(blend);
  529. }
  530. } else {
  531. blend = 0; // Should not happen.
  532. }
  533. }
  534. if (is_fading_out) {
  535. use_blend = true;
  536. if (Animation::is_greater_approx(fade_out, 0)) {
  537. blend = cur_fade_out_remaining / fade_out;
  538. if (fade_out_curve.is_valid()) {
  539. blend = 1.0 - fade_out_curve->sample(1.0 - blend);
  540. }
  541. } else {
  542. blend = 0;
  543. }
  544. }
  545. AnimationMixer::PlaybackInfo pi = p_playback_info;
  546. NodeTimeInfo main_nti;
  547. if (mix == MIX_MODE_ADD) {
  548. pi.weight = 1.0;
  549. main_nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
  550. } else {
  551. pi.seeked &= use_blend;
  552. pi.weight = 1.0 - blend;
  553. main_nti = blend_input(0, pi, FILTER_BLEND, sync, p_test_only); // Unlike below, processing this edge is a corner case.
  554. }
  555. pi = p_playback_info;
  556. if (do_start) {
  557. pi.time = 0;
  558. } else if (os_seek) {
  559. pi.time = cur_nti.position;
  560. }
  561. pi.seeked = os_seek;
  562. pi.weight = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;
  563. NodeTimeInfo os_nti = blend_input(1, pi, FILTER_PASS, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  564. if (Animation::is_less_or_equal_approx(cur_fade_in_remaining, 0) && !do_start && !is_fading_out) {
  565. // Predict time scale by difference of delta times to estimate input animation's remain time in self time scale.
  566. // TODO: Time scale should be included into NodeTimeInfo for Godot 5.0.
  567. double abs_os_delta = Math::abs(os_nti.delta);
  568. double tscl = Math::is_zero_approx(abs_delta) || Math::is_zero_approx(abs_os_delta) || Math::is_equal_approx(abs_delta, abs_os_delta) ? 1.0 : (abs_delta / abs_os_delta);
  569. double os_rem = os_nti.get_remain(break_loop_at_end) * tscl;
  570. if (Animation::is_less_or_equal_approx(os_rem, fade_out)) {
  571. is_fading_out = true;
  572. cur_fade_out_remaining = os_rem;
  573. cur_fade_in_remaining = 0;
  574. set_parameter(internal_active, false);
  575. }
  576. }
  577. if (!p_seek) {
  578. if (Animation::is_less_or_equal_approx(os_nti.get_remain(break_loop_at_end), 0) || (is_fading_out && Animation::is_less_or_equal_approx(cur_fade_out_remaining, 0))) {
  579. set_parameter(internal_active, false);
  580. set_parameter(active, false);
  581. if (auto_restart) {
  582. double restart_sec = auto_restart_delay + Math::randd() * auto_restart_random_delay;
  583. set_parameter(time_to_restart, restart_sec);
  584. }
  585. }
  586. if (!do_start) {
  587. cur_fade_in_remaining = MAX(0, cur_fade_in_remaining - abs_delta); // Don't consider seeked delta by restart.
  588. }
  589. cur_fade_out_remaining = MAX(0, cur_fade_out_remaining - abs_delta);
  590. }
  591. set_parameter(fade_in_remaining, cur_fade_in_remaining);
  592. set_parameter(fade_out_remaining, cur_fade_out_remaining);
  593. return cur_internal_active ? os_nti : main_nti;
  594. }
  595. void AnimationNodeOneShot::_bind_methods() {
  596. ClassDB::bind_method(D_METHOD("set_fadein_time", "time"), &AnimationNodeOneShot::set_fade_in_time);
  597. ClassDB::bind_method(D_METHOD("get_fadein_time"), &AnimationNodeOneShot::get_fade_in_time);
  598. ClassDB::bind_method(D_METHOD("set_fadein_curve", "curve"), &AnimationNodeOneShot::set_fade_in_curve);
  599. ClassDB::bind_method(D_METHOD("get_fadein_curve"), &AnimationNodeOneShot::get_fade_in_curve);
  600. ClassDB::bind_method(D_METHOD("set_fadeout_time", "time"), &AnimationNodeOneShot::set_fade_out_time);
  601. ClassDB::bind_method(D_METHOD("get_fadeout_time"), &AnimationNodeOneShot::get_fade_out_time);
  602. ClassDB::bind_method(D_METHOD("set_fadeout_curve", "curve"), &AnimationNodeOneShot::set_fade_out_curve);
  603. ClassDB::bind_method(D_METHOD("get_fadeout_curve"), &AnimationNodeOneShot::get_fade_out_curve);
  604. ClassDB::bind_method(D_METHOD("set_break_loop_at_end", "enable"), &AnimationNodeOneShot::set_break_loop_at_end);
  605. ClassDB::bind_method(D_METHOD("is_loop_broken_at_end"), &AnimationNodeOneShot::is_loop_broken_at_end);
  606. ClassDB::bind_method(D_METHOD("set_autorestart", "active"), &AnimationNodeOneShot::set_auto_restart_enabled);
  607. ClassDB::bind_method(D_METHOD("has_autorestart"), &AnimationNodeOneShot::is_auto_restart_enabled);
  608. ClassDB::bind_method(D_METHOD("set_autorestart_delay", "time"), &AnimationNodeOneShot::set_auto_restart_delay);
  609. ClassDB::bind_method(D_METHOD("get_autorestart_delay"), &AnimationNodeOneShot::get_auto_restart_delay);
  610. ClassDB::bind_method(D_METHOD("set_autorestart_random_delay", "time"), &AnimationNodeOneShot::set_auto_restart_random_delay);
  611. ClassDB::bind_method(D_METHOD("get_autorestart_random_delay"), &AnimationNodeOneShot::get_auto_restart_random_delay);
  612. ClassDB::bind_method(D_METHOD("set_mix_mode", "mode"), &AnimationNodeOneShot::set_mix_mode);
  613. ClassDB::bind_method(D_METHOD("get_mix_mode"), &AnimationNodeOneShot::get_mix_mode);
  614. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_mode", PROPERTY_HINT_ENUM, "Blend,Add"), "set_mix_mode", "get_mix_mode");
  615. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadein_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadein_time", "get_fadein_time");
  616. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadein_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadein_curve", "get_fadein_curve");
  617. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fadeout_time", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_fadeout_time", "get_fadeout_time");
  618. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "fadeout_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_fadeout_curve", "get_fadeout_curve");
  619. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "break_loop_at_end"), "set_break_loop_at_end", "is_loop_broken_at_end");
  620. ADD_GROUP("Auto Restart", "autorestart_");
  621. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autorestart"), "set_autorestart", "has_autorestart");
  622. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "autorestart_delay", PROPERTY_HINT_RANGE, "0,60,0.01,or_greater,suffix:s"), "set_autorestart_delay", "get_autorestart_delay");
  623. 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");
  624. BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_NONE);
  625. BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FIRE);
  626. BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_ABORT);
  627. BIND_ENUM_CONSTANT(ONE_SHOT_REQUEST_FADE_OUT);
  628. BIND_ENUM_CONSTANT(MIX_MODE_BLEND);
  629. BIND_ENUM_CONSTANT(MIX_MODE_ADD);
  630. }
  631. AnimationNodeOneShot::AnimationNodeOneShot() {
  632. add_input("in");
  633. add_input("shot");
  634. }
  635. ////////////////////////////////////////////////
  636. void AnimationNodeAdd2::get_parameter_list(List<PropertyInfo> *r_list) const {
  637. AnimationNode::get_parameter_list(r_list);
  638. r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
  639. }
  640. Variant AnimationNodeAdd2::get_parameter_default_value(const StringName &p_parameter) const {
  641. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  642. if (ret != Variant()) {
  643. return ret;
  644. }
  645. return 0;
  646. }
  647. String AnimationNodeAdd2::get_caption() const {
  648. return "Add2";
  649. }
  650. bool AnimationNodeAdd2::has_filter() const {
  651. return true;
  652. }
  653. AnimationNode::NodeTimeInfo AnimationNodeAdd2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  654. double amount = get_parameter(add_amount);
  655. AnimationMixer::PlaybackInfo pi = p_playback_info;
  656. pi.weight = 1.0;
  657. NodeTimeInfo nti = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
  658. pi.weight = amount;
  659. blend_input(1, pi, FILTER_PASS, sync, p_test_only);
  660. return nti;
  661. }
  662. AnimationNodeAdd2::AnimationNodeAdd2() {
  663. add_input("in");
  664. add_input("add");
  665. }
  666. ////////////////////////////////////////////////
  667. void AnimationNodeAdd3::get_parameter_list(List<PropertyInfo> *r_list) const {
  668. AnimationNode::get_parameter_list(r_list);
  669. r_list->push_back(PropertyInfo(Variant::FLOAT, add_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater"));
  670. }
  671. Variant AnimationNodeAdd3::get_parameter_default_value(const StringName &p_parameter) const {
  672. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  673. if (ret != Variant()) {
  674. return ret;
  675. }
  676. return 0;
  677. }
  678. String AnimationNodeAdd3::get_caption() const {
  679. return "Add3";
  680. }
  681. bool AnimationNodeAdd3::has_filter() const {
  682. return true;
  683. }
  684. AnimationNode::NodeTimeInfo AnimationNodeAdd3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  685. double amount = get_parameter(add_amount);
  686. AnimationMixer::PlaybackInfo pi = p_playback_info;
  687. pi.weight = MAX(0, -amount);
  688. blend_input(0, pi, FILTER_PASS, sync, p_test_only);
  689. pi.weight = 1.0;
  690. NodeTimeInfo nti = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only);
  691. pi.weight = MAX(0, amount);
  692. blend_input(2, pi, FILTER_PASS, sync, p_test_only);
  693. return nti;
  694. }
  695. AnimationNodeAdd3::AnimationNodeAdd3() {
  696. add_input("-add");
  697. add_input("in");
  698. add_input("+add");
  699. }
  700. /////////////////////////////////////////////
  701. void AnimationNodeBlend2::get_parameter_list(List<PropertyInfo> *r_list) const {
  702. AnimationNode::get_parameter_list(r_list);
  703. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
  704. }
  705. Variant AnimationNodeBlend2::get_parameter_default_value(const StringName &p_parameter) const {
  706. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  707. if (ret != Variant()) {
  708. return ret;
  709. }
  710. return 0; // For blend amount.
  711. }
  712. String AnimationNodeBlend2::get_caption() const {
  713. return "Blend2";
  714. }
  715. AnimationNode::NodeTimeInfo AnimationNodeBlend2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  716. double amount = get_parameter(blend_amount);
  717. AnimationMixer::PlaybackInfo pi = p_playback_info;
  718. pi.weight = 1.0 - amount;
  719. NodeTimeInfo nti0 = blend_input(0, pi, FILTER_BLEND, sync, p_test_only);
  720. pi.weight = amount;
  721. NodeTimeInfo nti1 = blend_input(1, pi, FILTER_PASS, sync, p_test_only);
  722. return amount > 0.5 ? nti1 : nti0; // Hacky but good enough.
  723. }
  724. bool AnimationNodeBlend2::has_filter() const {
  725. return true;
  726. }
  727. AnimationNodeBlend2::AnimationNodeBlend2() {
  728. add_input("in");
  729. add_input("blend");
  730. }
  731. //////////////////////////////////////
  732. void AnimationNodeBlend3::get_parameter_list(List<PropertyInfo> *r_list) const {
  733. AnimationNode::get_parameter_list(r_list);
  734. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_amount, PROPERTY_HINT_RANGE, "-1,1,0.01,or_less,or_greater"));
  735. }
  736. Variant AnimationNodeBlend3::get_parameter_default_value(const StringName &p_parameter) const {
  737. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  738. if (ret != Variant()) {
  739. return ret;
  740. }
  741. return 0; // For blend amount.
  742. }
  743. String AnimationNodeBlend3::get_caption() const {
  744. return "Blend3";
  745. }
  746. AnimationNode::NodeTimeInfo AnimationNodeBlend3::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  747. double amount = get_parameter(blend_amount);
  748. AnimationMixer::PlaybackInfo pi = p_playback_info;
  749. pi.weight = MAX(0, -amount);
  750. NodeTimeInfo nti0 = blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
  751. pi.weight = 1.0 - Math::abs(amount);
  752. NodeTimeInfo nti1 = blend_input(1, pi, FILTER_IGNORE, sync, p_test_only);
  753. pi.weight = MAX(0, amount);
  754. NodeTimeInfo nti2 = blend_input(2, pi, FILTER_IGNORE, sync, p_test_only);
  755. return amount > 0.5 ? nti2 : (amount < -0.5 ? nti0 : nti1); // Hacky but good enough.
  756. }
  757. AnimationNodeBlend3::AnimationNodeBlend3() {
  758. add_input("-blend");
  759. add_input("in");
  760. add_input("+blend");
  761. }
  762. ////////////////////////////////////////////////
  763. void AnimationNodeSub2::get_parameter_list(List<PropertyInfo> *r_list) const {
  764. AnimationNode::get_parameter_list(r_list);
  765. r_list->push_back(PropertyInfo(Variant::FLOAT, sub_amount, PROPERTY_HINT_RANGE, "0,1,0.01,or_less,or_greater"));
  766. }
  767. Variant AnimationNodeSub2::get_parameter_default_value(const StringName &p_parameter) const {
  768. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  769. if (ret != Variant()) {
  770. return ret;
  771. }
  772. return 0;
  773. }
  774. String AnimationNodeSub2::get_caption() const {
  775. return "Sub2";
  776. }
  777. bool AnimationNodeSub2::has_filter() const {
  778. return true;
  779. }
  780. AnimationNode::NodeTimeInfo AnimationNodeSub2::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  781. double amount = get_parameter(sub_amount);
  782. AnimationMixer::PlaybackInfo pi = p_playback_info;
  783. // Out = Sub.Transform3D^(-1) * In.Transform3D
  784. pi.weight = -amount;
  785. blend_input(1, pi, FILTER_PASS, sync, p_test_only);
  786. pi.weight = 1.0;
  787. return blend_input(0, pi, FILTER_IGNORE, sync, p_test_only);
  788. }
  789. AnimationNodeSub2::AnimationNodeSub2() {
  790. add_input("in");
  791. add_input("sub");
  792. }
  793. /////////////////////////////////
  794. void AnimationNodeTimeScale::get_parameter_list(List<PropertyInfo> *r_list) const {
  795. AnimationNode::get_parameter_list(r_list);
  796. r_list->push_back(PropertyInfo(Variant::FLOAT, scale, PROPERTY_HINT_RANGE, "-32,32,0.01,or_less,or_greater"));
  797. }
  798. Variant AnimationNodeTimeScale::get_parameter_default_value(const StringName &p_parameter) const {
  799. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  800. if (ret != Variant()) {
  801. return ret;
  802. }
  803. return 1.0; // Initial timescale.
  804. }
  805. String AnimationNodeTimeScale::get_caption() const {
  806. return "TimeScale";
  807. }
  808. AnimationNode::NodeTimeInfo AnimationNodeTimeScale::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  809. double cur_scale = get_parameter(scale);
  810. AnimationMixer::PlaybackInfo pi = p_playback_info;
  811. pi.weight = 1.0;
  812. if (!pi.seeked) {
  813. pi.delta *= cur_scale;
  814. }
  815. return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
  816. }
  817. AnimationNodeTimeScale::AnimationNodeTimeScale() {
  818. add_input("in");
  819. }
  820. ////////////////////////////////////
  821. void AnimationNodeTimeSeek::get_parameter_list(List<PropertyInfo> *r_list) const {
  822. AnimationNode::get_parameter_list(r_list);
  823. 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.
  824. }
  825. Variant AnimationNodeTimeSeek::get_parameter_default_value(const StringName &p_parameter) const {
  826. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  827. if (ret != Variant()) {
  828. return ret;
  829. }
  830. return -1.0; // Initial seek request.
  831. }
  832. String AnimationNodeTimeSeek::get_caption() const {
  833. return "TimeSeek";
  834. }
  835. void AnimationNodeTimeSeek::set_explicit_elapse(bool p_enable) {
  836. explicit_elapse = p_enable;
  837. }
  838. bool AnimationNodeTimeSeek::is_explicit_elapse() const {
  839. return explicit_elapse;
  840. }
  841. AnimationNode::NodeTimeInfo AnimationNodeTimeSeek::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  842. double cur_seek_pos = get_parameter(seek_pos_request);
  843. AnimationMixer::PlaybackInfo pi = p_playback_info;
  844. pi.weight = 1.0;
  845. if (Animation::is_greater_or_equal_approx(cur_seek_pos, 0)) {
  846. pi.time = cur_seek_pos;
  847. pi.seeked = true;
  848. pi.is_external_seeking = explicit_elapse;
  849. set_parameter(seek_pos_request, -1.0); // Reset.
  850. }
  851. return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
  852. }
  853. AnimationNodeTimeSeek::AnimationNodeTimeSeek() {
  854. add_input("in");
  855. }
  856. void AnimationNodeTimeSeek::_bind_methods() {
  857. ClassDB::bind_method(D_METHOD("set_explicit_elapse", "enable"), &AnimationNodeTimeSeek::set_explicit_elapse);
  858. ClassDB::bind_method(D_METHOD("is_explicit_elapse"), &AnimationNodeTimeSeek::is_explicit_elapse);
  859. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "explicit_elapse"), "set_explicit_elapse", "is_explicit_elapse");
  860. }
  861. /////////////////////////////////////////////////
  862. bool AnimationNodeTransition::_set(const StringName &p_path, const Variant &p_value) {
  863. String path = p_path;
  864. if (!path.begins_with("input_")) {
  865. return false;
  866. }
  867. int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
  868. String what = path.get_slicec('/', 1);
  869. if (which == get_input_count() && what == "name") {
  870. if (add_input(p_value)) {
  871. return true;
  872. }
  873. return false;
  874. }
  875. ERR_FAIL_INDEX_V(which, get_input_count(), false);
  876. if (what == "name") {
  877. set_input_name(which, p_value);
  878. } else if (what == "auto_advance") {
  879. set_input_as_auto_advance(which, p_value);
  880. } else if (what == "break_loop_at_end") {
  881. set_input_break_loop_at_end(which, p_value);
  882. } else if (what == "reset") {
  883. set_input_reset(which, p_value);
  884. } else {
  885. return false;
  886. }
  887. return true;
  888. }
  889. bool AnimationNodeTransition::_get(const StringName &p_path, Variant &r_ret) const {
  890. String path = p_path;
  891. if (!path.begins_with("input_")) {
  892. return false;
  893. }
  894. int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
  895. String what = path.get_slicec('/', 1);
  896. ERR_FAIL_INDEX_V(which, get_input_count(), false);
  897. if (what == "name") {
  898. r_ret = get_input_name(which);
  899. } else if (what == "auto_advance") {
  900. r_ret = is_input_set_as_auto_advance(which);
  901. } else if (what == "break_loop_at_end") {
  902. r_ret = is_input_loop_broken_at_end(which);
  903. } else if (what == "reset") {
  904. r_ret = is_input_reset(which);
  905. } else {
  906. return false;
  907. }
  908. return true;
  909. }
  910. void AnimationNodeTransition::get_parameter_list(List<PropertyInfo> *r_list) const {
  911. AnimationNode::get_parameter_list(r_list);
  912. String anims;
  913. for (int i = 0; i < get_input_count(); i++) {
  914. if (i > 0) {
  915. anims += ",";
  916. }
  917. anims += inputs[i].name;
  918. }
  919. r_list->push_back(PropertyInfo(Variant::STRING, current_state, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY)); // For interface.
  920. 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.
  921. 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.
  922. r_list->push_back(PropertyInfo(Variant::INT, prev_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  923. r_list->push_back(PropertyInfo(Variant::FLOAT, prev_xfading, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  924. }
  925. Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p_parameter) const {
  926. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  927. if (ret != Variant()) {
  928. return ret;
  929. }
  930. if (p_parameter == prev_xfading) {
  931. return 0.0;
  932. } else if (p_parameter == prev_index || p_parameter == current_index) {
  933. return -1;
  934. } else {
  935. return String();
  936. }
  937. }
  938. bool AnimationNodeTransition::is_parameter_read_only(const StringName &p_parameter) const {
  939. if (AnimationNode::is_parameter_read_only(p_parameter)) {
  940. return true;
  941. }
  942. if (p_parameter == current_state || p_parameter == current_index) {
  943. return true;
  944. }
  945. return false;
  946. }
  947. String AnimationNodeTransition::get_caption() const {
  948. return "Transition";
  949. }
  950. void AnimationNodeTransition::set_input_count(int p_inputs) {
  951. for (int i = get_input_count(); i < p_inputs; i++) {
  952. add_input("state_" + itos(i));
  953. }
  954. while (get_input_count() > p_inputs) {
  955. remove_input(get_input_count() - 1);
  956. }
  957. pending_update = true;
  958. emit_signal(SNAME("tree_changed")); // For updating connect activity map.
  959. notify_property_list_changed();
  960. }
  961. bool AnimationNodeTransition::add_input(const String &p_name) {
  962. if (AnimationNode::add_input(p_name)) {
  963. input_data.push_back(InputData());
  964. return true;
  965. }
  966. return false;
  967. }
  968. void AnimationNodeTransition::remove_input(int p_index) {
  969. input_data.remove_at(p_index);
  970. AnimationNode::remove_input(p_index);
  971. }
  972. bool AnimationNodeTransition::set_input_name(int p_input, const String &p_name) {
  973. pending_update = true;
  974. if (!AnimationNode::set_input_name(p_input, p_name)) {
  975. return false;
  976. }
  977. emit_signal(SNAME("tree_changed")); // For updating enum options.
  978. return true;
  979. }
  980. void AnimationNodeTransition::set_input_as_auto_advance(int p_input, bool p_enable) {
  981. ERR_FAIL_INDEX(p_input, get_input_count());
  982. input_data[p_input].auto_advance = p_enable;
  983. }
  984. bool AnimationNodeTransition::is_input_set_as_auto_advance(int p_input) const {
  985. ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
  986. return input_data[p_input].auto_advance;
  987. }
  988. void AnimationNodeTransition::set_input_break_loop_at_end(int p_input, bool p_enable) {
  989. ERR_FAIL_INDEX(p_input, get_input_count());
  990. input_data[p_input].break_loop_at_end = p_enable;
  991. }
  992. bool AnimationNodeTransition::is_input_loop_broken_at_end(int p_input) const {
  993. ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
  994. return input_data[p_input].break_loop_at_end;
  995. }
  996. void AnimationNodeTransition::set_input_reset(int p_input, bool p_enable) {
  997. ERR_FAIL_INDEX(p_input, get_input_count());
  998. input_data[p_input].reset = p_enable;
  999. }
  1000. bool AnimationNodeTransition::is_input_reset(int p_input) const {
  1001. ERR_FAIL_INDEX_V(p_input, get_input_count(), true);
  1002. return input_data[p_input].reset;
  1003. }
  1004. void AnimationNodeTransition::set_xfade_time(double p_fade) {
  1005. xfade_time = p_fade;
  1006. }
  1007. double AnimationNodeTransition::get_xfade_time() const {
  1008. return xfade_time;
  1009. }
  1010. void AnimationNodeTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
  1011. xfade_curve = p_curve;
  1012. }
  1013. Ref<Curve> AnimationNodeTransition::get_xfade_curve() const {
  1014. return xfade_curve;
  1015. }
  1016. void AnimationNodeTransition::set_allow_transition_to_self(bool p_enable) {
  1017. allow_transition_to_self = p_enable;
  1018. }
  1019. bool AnimationNodeTransition::is_allow_transition_to_self() const {
  1020. return allow_transition_to_self;
  1021. }
  1022. AnimationNode::NodeTimeInfo AnimationNodeTransition::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  1023. String cur_transition_request = get_parameter(transition_request);
  1024. int cur_current_index = get_parameter(current_index);
  1025. int cur_prev_index = get_parameter(prev_index);
  1026. NodeTimeInfo cur_nti = get_node_time_info();
  1027. double cur_prev_xfading = get_parameter(prev_xfading);
  1028. bool switched = false;
  1029. bool restart = false;
  1030. bool clear_remaining_fade = false;
  1031. if (pending_update) {
  1032. if (cur_current_index < 0 || cur_current_index >= get_input_count()) {
  1033. set_parameter(prev_index, -1);
  1034. if (get_input_count() > 0) {
  1035. set_parameter(current_index, 0);
  1036. set_parameter(current_state, get_input_name(0));
  1037. } else {
  1038. set_parameter(current_index, -1);
  1039. set_parameter(current_state, StringName());
  1040. }
  1041. } else {
  1042. set_parameter(current_state, get_input_name(cur_current_index));
  1043. }
  1044. pending_update = false;
  1045. }
  1046. double p_time = p_playback_info.time;
  1047. bool p_seek = p_playback_info.seeked;
  1048. bool p_is_external_seeking = p_playback_info.is_external_seeking;
  1049. if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
  1050. clear_remaining_fade = true; // Reset occurs.
  1051. }
  1052. if (!cur_transition_request.is_empty()) {
  1053. int new_idx = find_input(cur_transition_request);
  1054. if (new_idx >= 0) {
  1055. if (cur_current_index == new_idx) {
  1056. if (allow_transition_to_self) {
  1057. // Transition to same state.
  1058. restart = input_data[cur_current_index].reset;
  1059. clear_remaining_fade = true;
  1060. }
  1061. } else {
  1062. switched = true;
  1063. cur_prev_index = cur_current_index;
  1064. set_parameter(prev_index, cur_current_index);
  1065. cur_current_index = new_idx;
  1066. set_parameter(current_index, cur_current_index);
  1067. set_parameter(current_state, cur_transition_request);
  1068. }
  1069. } else {
  1070. ERR_PRINT("No such input: '" + cur_transition_request + "'");
  1071. }
  1072. cur_transition_request = String();
  1073. set_parameter(transition_request, cur_transition_request);
  1074. }
  1075. if (clear_remaining_fade) {
  1076. cur_prev_xfading = 0;
  1077. set_parameter(prev_xfading, 0);
  1078. cur_prev_index = -1;
  1079. set_parameter(prev_index, -1);
  1080. }
  1081. AnimationMixer::PlaybackInfo pi = p_playback_info;
  1082. // Special case for restart.
  1083. if (restart) {
  1084. pi.time = 0;
  1085. pi.seeked = true;
  1086. pi.weight = 1.0;
  1087. return blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
  1088. }
  1089. if (switched) {
  1090. cur_prev_xfading = xfade_time;
  1091. }
  1092. if (cur_current_index < 0 || cur_current_index >= get_input_count() || cur_prev_index >= get_input_count()) {
  1093. return NodeTimeInfo();
  1094. }
  1095. if (sync) {
  1096. pi.weight = 0;
  1097. for (int i = 0; i < get_input_count(); i++) {
  1098. if (i != cur_current_index && i != cur_prev_index) {
  1099. blend_input(i, pi, FILTER_IGNORE, true, p_test_only);
  1100. }
  1101. }
  1102. }
  1103. if (cur_prev_index < 0) { // Process current animation, check for transition.
  1104. pi.weight = 1.0;
  1105. cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
  1106. if (input_data[cur_current_index].auto_advance && Animation::is_less_or_equal_approx(cur_nti.get_remain(input_data[cur_current_index].break_loop_at_end), xfade_time)) {
  1107. set_parameter(transition_request, get_input_name((cur_current_index + 1) % get_input_count()));
  1108. }
  1109. } else { // Cross-fading from prev to current.
  1110. real_t blend = 0.0;
  1111. real_t blend_inv = 1.0;
  1112. bool use_blend = sync;
  1113. if (xfade_time > 0) {
  1114. use_blend = true;
  1115. blend = cur_prev_xfading / xfade_time;
  1116. if (xfade_curve.is_valid()) {
  1117. blend = xfade_curve->sample(blend);
  1118. }
  1119. blend_inv = 1.0 - blend;
  1120. blend = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;
  1121. blend_inv = Math::is_zero_approx(blend_inv) ? CMP_EPSILON : blend_inv;
  1122. }
  1123. // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  1124. pi.weight = blend_inv;
  1125. if (input_data[cur_current_index].reset && !p_seek && switched) { // Just switched, seek to start of current.
  1126. pi.time = 0;
  1127. pi.seeked = true;
  1128. }
  1129. cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
  1130. pi = p_playback_info;
  1131. pi.seeked &= use_blend;
  1132. pi.weight = blend;
  1133. blend_input(cur_prev_index, pi, FILTER_IGNORE, true, p_test_only);
  1134. if (!p_seek) {
  1135. if (Animation::is_less_or_equal_approx(cur_prev_xfading, 0)) {
  1136. set_parameter(prev_index, -1);
  1137. }
  1138. cur_prev_xfading -= Math::abs(p_playback_info.delta);
  1139. }
  1140. }
  1141. set_parameter(prev_xfading, cur_prev_xfading);
  1142. return cur_nti;
  1143. }
  1144. void AnimationNodeTransition::_get_property_list(List<PropertyInfo> *p_list) const {
  1145. for (int i = 0; i < get_input_count(); i++) {
  1146. p_list->push_back(PropertyInfo(Variant::STRING, "input_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1147. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/auto_advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1148. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/break_loop_at_end", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1149. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/reset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1150. }
  1151. }
  1152. void AnimationNodeTransition::_bind_methods() {
  1153. ClassDB::bind_method(D_METHOD("set_input_count", "input_count"), &AnimationNodeTransition::set_input_count);
  1154. ClassDB::bind_method(D_METHOD("set_input_as_auto_advance", "input", "enable"), &AnimationNodeTransition::set_input_as_auto_advance);
  1155. ClassDB::bind_method(D_METHOD("is_input_set_as_auto_advance", "input"), &AnimationNodeTransition::is_input_set_as_auto_advance);
  1156. ClassDB::bind_method(D_METHOD("set_input_break_loop_at_end", "input", "enable"), &AnimationNodeTransition::set_input_break_loop_at_end);
  1157. ClassDB::bind_method(D_METHOD("is_input_loop_broken_at_end", "input"), &AnimationNodeTransition::is_input_loop_broken_at_end);
  1158. ClassDB::bind_method(D_METHOD("set_input_reset", "input", "enable"), &AnimationNodeTransition::set_input_reset);
  1159. ClassDB::bind_method(D_METHOD("is_input_reset", "input"), &AnimationNodeTransition::is_input_reset);
  1160. ClassDB::bind_method(D_METHOD("set_xfade_time", "time"), &AnimationNodeTransition::set_xfade_time);
  1161. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeTransition::get_xfade_time);
  1162. ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeTransition::set_xfade_curve);
  1163. ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeTransition::get_xfade_curve);
  1164. ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeTransition::set_allow_transition_to_self);
  1165. ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeTransition::is_allow_transition_to_self);
  1166. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
  1167. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
  1168. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
  1169. 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");
  1170. }
  1171. AnimationNodeTransition::AnimationNodeTransition() {
  1172. }
  1173. /////////////////////
  1174. String AnimationNodeOutput::get_caption() const {
  1175. return "Output";
  1176. }
  1177. AnimationNode::NodeTimeInfo AnimationNodeOutput::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  1178. AnimationMixer::PlaybackInfo pi = p_playback_info;
  1179. pi.weight = 1.0;
  1180. return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
  1181. }
  1182. AnimationNodeOutput::AnimationNodeOutput() {
  1183. add_input("output");
  1184. }
  1185. ///////////////////////////////////////////////////////
  1186. void AnimationNodeBlendTree::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  1187. ERR_FAIL_COND(nodes.has(p_name));
  1188. ERR_FAIL_COND(p_node.is_null());
  1189. ERR_FAIL_COND(p_name == SceneStringName(output));
  1190. ERR_FAIL_COND(String(p_name).contains_char('/'));
  1191. Node n;
  1192. n.node = p_node;
  1193. n.position = p_position;
  1194. n.connections.resize(n.node->get_input_count());
  1195. nodes[p_name] = n;
  1196. emit_changed();
  1197. emit_signal(SNAME("tree_changed"));
  1198. p_node->connect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1199. p_node->connect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  1200. p_node->connect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  1201. p_node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_name), CONNECT_REFERENCE_COUNTED);
  1202. }
  1203. Ref<AnimationNode> AnimationNodeBlendTree::get_node(const StringName &p_name) const {
  1204. ERR_FAIL_COND_V(!nodes.has(p_name), Ref<AnimationNode>());
  1205. return nodes[p_name].node;
  1206. }
  1207. StringName AnimationNodeBlendTree::get_node_name(const Ref<AnimationNode> &p_node) const {
  1208. for (const KeyValue<StringName, Node> &E : nodes) {
  1209. if (E.value.node == p_node) {
  1210. return E.key;
  1211. }
  1212. }
  1213. ERR_FAIL_V(StringName());
  1214. }
  1215. void AnimationNodeBlendTree::set_node_position(const StringName &p_node, const Vector2 &p_position) {
  1216. ERR_FAIL_COND(!nodes.has(p_node));
  1217. nodes[p_node].position = p_position;
  1218. }
  1219. Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) const {
  1220. ERR_FAIL_COND_V(!nodes.has(p_node), Vector2());
  1221. return nodes[p_node].position;
  1222. }
  1223. void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
  1224. Vector<StringName> ns;
  1225. for (const KeyValue<StringName, Node> &E : nodes) {
  1226. ns.push_back(E.key);
  1227. }
  1228. for (int i = 0; i < ns.size(); i++) {
  1229. ChildNode cn;
  1230. cn.name = ns[i];
  1231. cn.node = nodes[cn.name].node;
  1232. r_child_nodes->push_back(cn);
  1233. }
  1234. }
  1235. bool AnimationNodeBlendTree::has_node(const StringName &p_name) const {
  1236. return nodes.has(p_name);
  1237. }
  1238. Vector<StringName> AnimationNodeBlendTree::get_node_connection_array(const StringName &p_name) const {
  1239. ERR_FAIL_COND_V(!nodes.has(p_name), Vector<StringName>());
  1240. return nodes[p_name].connections;
  1241. }
  1242. void AnimationNodeBlendTree::remove_node(const StringName &p_name) {
  1243. ERR_FAIL_COND(!nodes.has(p_name));
  1244. ERR_FAIL_COND(p_name == SceneStringName(output)); //can't delete output
  1245. {
  1246. Ref<AnimationNode> node = nodes[p_name].node;
  1247. node->disconnect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed));
  1248. node->disconnect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed));
  1249. node->disconnect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed));
  1250. node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  1251. }
  1252. nodes.erase(p_name);
  1253. // Erase connections to name.
  1254. for (KeyValue<StringName, Node> &E : nodes) {
  1255. for (int i = 0; i < E.value.connections.size(); i++) {
  1256. if (E.value.connections[i] == p_name) {
  1257. E.value.connections.write[i] = StringName();
  1258. }
  1259. }
  1260. }
  1261. emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
  1262. emit_changed();
  1263. emit_signal(SNAME("tree_changed"));
  1264. }
  1265. void AnimationNodeBlendTree::rename_node(const StringName &p_name, const StringName &p_new_name) {
  1266. ERR_FAIL_COND(!nodes.has(p_name));
  1267. ERR_FAIL_COND(nodes.has(p_new_name));
  1268. ERR_FAIL_COND(p_name == SceneStringName(output));
  1269. ERR_FAIL_COND(p_new_name == SceneStringName(output));
  1270. nodes[p_name].node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  1271. nodes[p_new_name] = nodes[p_name];
  1272. nodes.erase(p_name);
  1273. // Rename connections.
  1274. for (KeyValue<StringName, Node> &E : nodes) {
  1275. for (int i = 0; i < E.value.connections.size(); i++) {
  1276. if (E.value.connections[i] == p_name) {
  1277. E.value.connections.write[i] = p_new_name;
  1278. }
  1279. }
  1280. }
  1281. // Connection must be done with new name.
  1282. nodes[p_new_name].node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_new_name), CONNECT_REFERENCE_COUNTED);
  1283. emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
  1284. emit_signal(SNAME("tree_changed"));
  1285. }
  1286. void AnimationNodeBlendTree::connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) {
  1287. ERR_FAIL_COND(!nodes.has(p_output_node));
  1288. ERR_FAIL_COND(!nodes.has(p_input_node));
  1289. ERR_FAIL_COND(p_output_node == SceneStringName(output));
  1290. ERR_FAIL_COND(p_input_node == p_output_node);
  1291. Ref<AnimationNode> input = nodes[p_input_node].node;
  1292. ERR_FAIL_INDEX(p_input_index, nodes[p_input_node].connections.size());
  1293. for (KeyValue<StringName, Node> &E : nodes) {
  1294. for (int i = 0; i < E.value.connections.size(); i++) {
  1295. StringName output = E.value.connections[i];
  1296. ERR_FAIL_COND(output == p_output_node);
  1297. }
  1298. }
  1299. nodes[p_input_node].connections.write[p_input_index] = p_output_node;
  1300. emit_changed();
  1301. }
  1302. void AnimationNodeBlendTree::disconnect_node(const StringName &p_node, int p_input_index) {
  1303. ERR_FAIL_COND(!nodes.has(p_node));
  1304. Ref<AnimationNode> input = nodes[p_node].node;
  1305. ERR_FAIL_INDEX(p_input_index, nodes[p_node].connections.size());
  1306. nodes[p_node].connections.write[p_input_index] = StringName();
  1307. }
  1308. AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const {
  1309. if (!nodes.has(p_output_node) || p_output_node == SceneStringName(output)) {
  1310. return CONNECTION_ERROR_NO_OUTPUT;
  1311. }
  1312. if (!nodes.has(p_input_node)) {
  1313. return CONNECTION_ERROR_NO_INPUT;
  1314. }
  1315. if (p_input_node == p_output_node) {
  1316. return CONNECTION_ERROR_SAME_NODE;
  1317. }
  1318. Ref<AnimationNode> input = nodes[p_input_node].node;
  1319. if (p_input_index < 0 || p_input_index >= nodes[p_input_node].connections.size()) {
  1320. return CONNECTION_ERROR_NO_INPUT_INDEX;
  1321. }
  1322. if (nodes[p_input_node].connections[p_input_index] != StringName()) {
  1323. return CONNECTION_ERROR_CONNECTION_EXISTS;
  1324. }
  1325. for (const KeyValue<StringName, Node> &E : nodes) {
  1326. for (int i = 0; i < E.value.connections.size(); i++) {
  1327. const StringName output = E.value.connections[i];
  1328. if (output == p_output_node) {
  1329. return CONNECTION_ERROR_CONNECTION_EXISTS;
  1330. }
  1331. }
  1332. }
  1333. return CONNECTION_OK;
  1334. }
  1335. void AnimationNodeBlendTree::get_node_connections(List<NodeConnection> *r_connections) const {
  1336. for (const KeyValue<StringName, Node> &E : nodes) {
  1337. for (int i = 0; i < E.value.connections.size(); i++) {
  1338. const StringName output = E.value.connections[i];
  1339. if (output != StringName()) {
  1340. NodeConnection nc;
  1341. nc.input_node = E.key;
  1342. nc.input_index = i;
  1343. nc.output_node = output;
  1344. r_connections->push_back(nc);
  1345. }
  1346. }
  1347. }
  1348. }
  1349. String AnimationNodeBlendTree::get_caption() const {
  1350. return "BlendTree";
  1351. }
  1352. AnimationNode::NodeTimeInfo AnimationNodeBlendTree::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  1353. Ref<AnimationNodeOutput> output = nodes[SceneStringName(output)].node;
  1354. node_state.connections = nodes[SceneStringName(output)].connections;
  1355. ERR_FAIL_COND_V(output.is_null(), NodeTimeInfo());
  1356. AnimationMixer::PlaybackInfo pi = p_playback_info;
  1357. pi.weight = 1.0;
  1358. return _blend_node(output, "output", this, pi, FILTER_IGNORE, true, p_test_only, nullptr);
  1359. }
  1360. LocalVector<StringName> AnimationNodeBlendTree::get_node_list() const {
  1361. LocalVector<StringName> list;
  1362. list.reserve(nodes.size());
  1363. for (const KeyValue<StringName, Node> &E : nodes) {
  1364. list.push_back(E.key);
  1365. }
  1366. list.sort_custom<StringName::AlphCompare>();
  1367. return list;
  1368. }
  1369. TypedArray<StringName> AnimationNodeBlendTree::get_node_list_as_typed_array() const {
  1370. TypedArray<StringName> typed_arr;
  1371. LocalVector<StringName> vec = get_node_list();
  1372. typed_arr.resize(vec.size());
  1373. for (uint32_t i = 0; i < vec.size(); i++) {
  1374. typed_arr[i] = vec[i];
  1375. }
  1376. return typed_arr;
  1377. }
  1378. void AnimationNodeBlendTree::set_graph_offset(const Vector2 &p_graph_offset) {
  1379. graph_offset = p_graph_offset;
  1380. }
  1381. Vector2 AnimationNodeBlendTree::get_graph_offset() const {
  1382. return graph_offset;
  1383. }
  1384. Ref<AnimationNode> AnimationNodeBlendTree::get_child_by_name(const StringName &p_name) const {
  1385. return get_node(p_name);
  1386. }
  1387. bool AnimationNodeBlendTree::_set(const StringName &p_name, const Variant &p_value) {
  1388. String prop_name = p_name;
  1389. if (prop_name.begins_with("nodes/")) {
  1390. String node_name = prop_name.get_slicec('/', 1);
  1391. String what = prop_name.get_slicec('/', 2);
  1392. if (what == "node") {
  1393. Ref<AnimationNode> anode = p_value;
  1394. if (anode.is_valid()) {
  1395. add_node(node_name, p_value);
  1396. }
  1397. return true;
  1398. }
  1399. if (what == "position") {
  1400. if (nodes.has(node_name)) {
  1401. nodes[node_name].position = p_value;
  1402. }
  1403. return true;
  1404. }
  1405. } else if (prop_name == "node_connections") {
  1406. Array conns = p_value;
  1407. ERR_FAIL_COND_V(conns.size() % 3 != 0, false);
  1408. for (int i = 0; i < conns.size(); i += 3) {
  1409. connect_node(conns[i], conns[i + 1], conns[i + 2]);
  1410. }
  1411. return true;
  1412. }
  1413. return false;
  1414. }
  1415. bool AnimationNodeBlendTree::_get(const StringName &p_name, Variant &r_ret) const {
  1416. String prop_name = p_name;
  1417. if (prop_name.begins_with("nodes/")) {
  1418. String node_name = prop_name.get_slicec('/', 1);
  1419. String what = prop_name.get_slicec('/', 2);
  1420. if (what == "node") {
  1421. if (nodes.has(node_name)) {
  1422. r_ret = nodes[node_name].node;
  1423. return true;
  1424. }
  1425. }
  1426. if (what == "position") {
  1427. if (nodes.has(node_name)) {
  1428. r_ret = nodes[node_name].position;
  1429. return true;
  1430. }
  1431. }
  1432. } else if (prop_name == "node_connections") {
  1433. List<NodeConnection> nc;
  1434. get_node_connections(&nc);
  1435. Array conns;
  1436. conns.resize(nc.size() * 3);
  1437. int idx = 0;
  1438. for (const NodeConnection &E : nc) {
  1439. conns[idx * 3 + 0] = E.input_node;
  1440. conns[idx * 3 + 1] = E.input_index;
  1441. conns[idx * 3 + 2] = E.output_node;
  1442. idx++;
  1443. }
  1444. r_ret = conns;
  1445. return true;
  1446. }
  1447. return false;
  1448. }
  1449. void AnimationNodeBlendTree::_get_property_list(List<PropertyInfo> *p_list) const {
  1450. List<StringName> names;
  1451. for (const KeyValue<StringName, Node> &E : nodes) {
  1452. names.push_back(E.key);
  1453. }
  1454. for (const StringName &E : names) {
  1455. String prop_name = E;
  1456. if (prop_name != "output") {
  1457. p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1458. }
  1459. p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1460. }
  1461. p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1462. }
  1463. void AnimationNodeBlendTree::_tree_changed() {
  1464. AnimationRootNode::_tree_changed();
  1465. }
  1466. void AnimationNodeBlendTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  1467. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  1468. }
  1469. void AnimationNodeBlendTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  1470. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  1471. }
  1472. void AnimationNodeBlendTree::reset_state() {
  1473. graph_offset = Vector2();
  1474. nodes.clear();
  1475. _initialize_node_tree();
  1476. emit_changed();
  1477. emit_signal(SNAME("tree_changed"));
  1478. }
  1479. void AnimationNodeBlendTree::_node_changed(const StringName &p_node) {
  1480. ERR_FAIL_COND(!nodes.has(p_node));
  1481. nodes[p_node].connections.resize(nodes[p_node].node->get_input_count());
  1482. emit_signal(SNAME("node_changed"), p_node);
  1483. }
  1484. #ifdef TOOLS_ENABLED
  1485. void AnimationNodeBlendTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1486. const String pf = p_function;
  1487. bool add_node_options = false;
  1488. if (p_idx == 0) {
  1489. add_node_options = (pf == "get_node" || pf == "has_node" || pf == "rename_node" || pf == "remove_node" || pf == "set_node_position" || pf == "get_node_position" || pf == "connect_node" || pf == "disconnect_node");
  1490. } else if (p_idx == 2) {
  1491. add_node_options = (pf == "connect_node" || pf == "disconnect_node");
  1492. }
  1493. if (add_node_options) {
  1494. for (const KeyValue<StringName, Node> &E : nodes) {
  1495. r_options->push_back(String(E.key).quote());
  1496. }
  1497. }
  1498. AnimationRootNode::get_argument_options(p_function, p_idx, r_options);
  1499. }
  1500. #endif
  1501. void AnimationNodeBlendTree::_bind_methods() {
  1502. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeBlendTree::add_node, DEFVAL(Vector2()));
  1503. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeBlendTree::get_node);
  1504. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeBlendTree::remove_node);
  1505. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeBlendTree::rename_node);
  1506. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeBlendTree::has_node);
  1507. ClassDB::bind_method(D_METHOD("connect_node", "input_node", "input_index", "output_node"), &AnimationNodeBlendTree::connect_node);
  1508. ClassDB::bind_method(D_METHOD("disconnect_node", "input_node", "input_index"), &AnimationNodeBlendTree::disconnect_node);
  1509. ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationNodeBlendTree::get_node_list_as_typed_array);
  1510. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeBlendTree::set_node_position);
  1511. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeBlendTree::get_node_position);
  1512. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeBlendTree::set_graph_offset);
  1513. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeBlendTree::get_graph_offset);
  1514. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");
  1515. BIND_CONSTANT(CONNECTION_OK);
  1516. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT);
  1517. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT_INDEX);
  1518. BIND_CONSTANT(CONNECTION_ERROR_NO_OUTPUT);
  1519. BIND_CONSTANT(CONNECTION_ERROR_SAME_NODE);
  1520. BIND_CONSTANT(CONNECTION_ERROR_CONNECTION_EXISTS);
  1521. ADD_SIGNAL(MethodInfo(SNAME("node_changed"), PropertyInfo(Variant::STRING_NAME, "node_name")));
  1522. }
  1523. void AnimationNodeBlendTree::_initialize_node_tree() {
  1524. Ref<AnimationNodeOutput> output;
  1525. output.instantiate();
  1526. Node n;
  1527. n.node = output;
  1528. n.position = Vector2(300, 150);
  1529. n.connections.resize(1);
  1530. nodes["output"] = n;
  1531. }
  1532. AnimationNodeBlendTree::AnimationNodeBlendTree() {
  1533. _initialize_node_tree();
  1534. }
  1535. AnimationNodeBlendTree::~AnimationNodeBlendTree() {
  1536. }