animation_blend_tree.cpp 64 KB

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