animation_blend_tree.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836
  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. AnimationNode::NodeTimeInfo AnimationNodeTimeSeek::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  817. double cur_seek_pos = get_parameter(seek_pos_request);
  818. AnimationMixer::PlaybackInfo pi = p_playback_info;
  819. pi.weight = 1.0;
  820. if (Animation::is_greater_or_equal_approx(cur_seek_pos, 0)) {
  821. pi.time = cur_seek_pos;
  822. pi.seeked = true;
  823. pi.is_external_seeking = true;
  824. set_parameter(seek_pos_request, -1.0); // Reset.
  825. }
  826. return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
  827. }
  828. AnimationNodeTimeSeek::AnimationNodeTimeSeek() {
  829. add_input("in");
  830. }
  831. /////////////////////////////////////////////////
  832. bool AnimationNodeTransition::_set(const StringName &p_path, const Variant &p_value) {
  833. String path = p_path;
  834. if (!path.begins_with("input_")) {
  835. return false;
  836. }
  837. int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
  838. String what = path.get_slicec('/', 1);
  839. if (which == get_input_count() && what == "name") {
  840. if (add_input(p_value)) {
  841. return true;
  842. }
  843. return false;
  844. }
  845. ERR_FAIL_INDEX_V(which, get_input_count(), false);
  846. if (what == "name") {
  847. set_input_name(which, p_value);
  848. } else if (what == "auto_advance") {
  849. set_input_as_auto_advance(which, p_value);
  850. } else if (what == "break_loop_at_end") {
  851. set_input_break_loop_at_end(which, p_value);
  852. } else if (what == "reset") {
  853. set_input_reset(which, p_value);
  854. } else {
  855. return false;
  856. }
  857. return true;
  858. }
  859. bool AnimationNodeTransition::_get(const StringName &p_path, Variant &r_ret) const {
  860. String path = p_path;
  861. if (!path.begins_with("input_")) {
  862. return false;
  863. }
  864. int which = path.get_slicec('/', 0).get_slicec('_', 1).to_int();
  865. String what = path.get_slicec('/', 1);
  866. ERR_FAIL_INDEX_V(which, get_input_count(), false);
  867. if (what == "name") {
  868. r_ret = get_input_name(which);
  869. } else if (what == "auto_advance") {
  870. r_ret = is_input_set_as_auto_advance(which);
  871. } else if (what == "break_loop_at_end") {
  872. r_ret = is_input_loop_broken_at_end(which);
  873. } else if (what == "reset") {
  874. r_ret = is_input_reset(which);
  875. } else {
  876. return false;
  877. }
  878. return true;
  879. }
  880. void AnimationNodeTransition::get_parameter_list(List<PropertyInfo> *r_list) const {
  881. AnimationNode::get_parameter_list(r_list);
  882. String anims;
  883. for (int i = 0; i < get_input_count(); i++) {
  884. if (i > 0) {
  885. anims += ",";
  886. }
  887. anims += inputs[i].name;
  888. }
  889. r_list->push_back(PropertyInfo(Variant::STRING, current_state, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY)); // For interface.
  890. 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.
  891. 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.
  892. r_list->push_back(PropertyInfo(Variant::INT, prev_index, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  893. r_list->push_back(PropertyInfo(Variant::FLOAT, prev_xfading, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  894. }
  895. Variant AnimationNodeTransition::get_parameter_default_value(const StringName &p_parameter) const {
  896. Variant ret = AnimationNode::get_parameter_default_value(p_parameter);
  897. if (ret != Variant()) {
  898. return ret;
  899. }
  900. if (p_parameter == prev_xfading) {
  901. return 0.0;
  902. } else if (p_parameter == prev_index || p_parameter == current_index) {
  903. return -1;
  904. } else {
  905. return String();
  906. }
  907. }
  908. bool AnimationNodeTransition::is_parameter_read_only(const StringName &p_parameter) const {
  909. if (AnimationNode::is_parameter_read_only(p_parameter)) {
  910. return true;
  911. }
  912. if (p_parameter == current_state || p_parameter == current_index) {
  913. return true;
  914. }
  915. return false;
  916. }
  917. String AnimationNodeTransition::get_caption() const {
  918. return "Transition";
  919. }
  920. void AnimationNodeTransition::set_input_count(int p_inputs) {
  921. for (int i = get_input_count(); i < p_inputs; i++) {
  922. add_input("state_" + itos(i));
  923. }
  924. while (get_input_count() > p_inputs) {
  925. remove_input(get_input_count() - 1);
  926. }
  927. pending_update = true;
  928. emit_signal(SNAME("tree_changed")); // For updating connect activity map.
  929. notify_property_list_changed();
  930. }
  931. bool AnimationNodeTransition::add_input(const String &p_name) {
  932. if (AnimationNode::add_input(p_name)) {
  933. input_data.push_back(InputData());
  934. return true;
  935. }
  936. return false;
  937. }
  938. void AnimationNodeTransition::remove_input(int p_index) {
  939. input_data.remove_at(p_index);
  940. AnimationNode::remove_input(p_index);
  941. }
  942. bool AnimationNodeTransition::set_input_name(int p_input, const String &p_name) {
  943. pending_update = true;
  944. if (!AnimationNode::set_input_name(p_input, p_name)) {
  945. return false;
  946. }
  947. emit_signal(SNAME("tree_changed")); // For updating enum options.
  948. return true;
  949. }
  950. void AnimationNodeTransition::set_input_as_auto_advance(int p_input, bool p_enable) {
  951. ERR_FAIL_INDEX(p_input, get_input_count());
  952. input_data.write[p_input].auto_advance = p_enable;
  953. }
  954. bool AnimationNodeTransition::is_input_set_as_auto_advance(int p_input) const {
  955. ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
  956. return input_data[p_input].auto_advance;
  957. }
  958. void AnimationNodeTransition::set_input_break_loop_at_end(int p_input, bool p_enable) {
  959. ERR_FAIL_INDEX(p_input, get_input_count());
  960. input_data.write[p_input].break_loop_at_end = p_enable;
  961. }
  962. bool AnimationNodeTransition::is_input_loop_broken_at_end(int p_input) const {
  963. ERR_FAIL_INDEX_V(p_input, get_input_count(), false);
  964. return input_data[p_input].break_loop_at_end;
  965. }
  966. void AnimationNodeTransition::set_input_reset(int p_input, bool p_enable) {
  967. ERR_FAIL_INDEX(p_input, get_input_count());
  968. input_data.write[p_input].reset = p_enable;
  969. }
  970. bool AnimationNodeTransition::is_input_reset(int p_input) const {
  971. ERR_FAIL_INDEX_V(p_input, get_input_count(), true);
  972. return input_data[p_input].reset;
  973. }
  974. void AnimationNodeTransition::set_xfade_time(double p_fade) {
  975. xfade_time = p_fade;
  976. }
  977. double AnimationNodeTransition::get_xfade_time() const {
  978. return xfade_time;
  979. }
  980. void AnimationNodeTransition::set_xfade_curve(const Ref<Curve> &p_curve) {
  981. xfade_curve = p_curve;
  982. }
  983. Ref<Curve> AnimationNodeTransition::get_xfade_curve() const {
  984. return xfade_curve;
  985. }
  986. void AnimationNodeTransition::set_allow_transition_to_self(bool p_enable) {
  987. allow_transition_to_self = p_enable;
  988. }
  989. bool AnimationNodeTransition::is_allow_transition_to_self() const {
  990. return allow_transition_to_self;
  991. }
  992. AnimationNode::NodeTimeInfo AnimationNodeTransition::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  993. String cur_transition_request = get_parameter(transition_request);
  994. int cur_current_index = get_parameter(current_index);
  995. int cur_prev_index = get_parameter(prev_index);
  996. NodeTimeInfo cur_nti = get_node_time_info();
  997. double cur_prev_xfading = get_parameter(prev_xfading);
  998. bool switched = false;
  999. bool restart = false;
  1000. bool clear_remaining_fade = false;
  1001. if (pending_update) {
  1002. if (cur_current_index < 0 || cur_current_index >= get_input_count()) {
  1003. set_parameter(prev_index, -1);
  1004. if (get_input_count() > 0) {
  1005. set_parameter(current_index, 0);
  1006. set_parameter(current_state, get_input_name(0));
  1007. } else {
  1008. set_parameter(current_index, -1);
  1009. set_parameter(current_state, StringName());
  1010. }
  1011. } else {
  1012. set_parameter(current_state, get_input_name(cur_current_index));
  1013. }
  1014. pending_update = false;
  1015. }
  1016. double p_time = p_playback_info.time;
  1017. bool p_seek = p_playback_info.seeked;
  1018. bool p_is_external_seeking = p_playback_info.is_external_seeking;
  1019. if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {
  1020. clear_remaining_fade = true; // Reset occurs.
  1021. }
  1022. if (!cur_transition_request.is_empty()) {
  1023. int new_idx = find_input(cur_transition_request);
  1024. if (new_idx >= 0) {
  1025. if (cur_current_index == new_idx) {
  1026. if (allow_transition_to_self) {
  1027. // Transition to same state.
  1028. restart = input_data[cur_current_index].reset;
  1029. clear_remaining_fade = true;
  1030. }
  1031. } else {
  1032. switched = true;
  1033. cur_prev_index = cur_current_index;
  1034. set_parameter(prev_index, cur_current_index);
  1035. cur_current_index = new_idx;
  1036. set_parameter(current_index, cur_current_index);
  1037. set_parameter(current_state, cur_transition_request);
  1038. }
  1039. } else {
  1040. ERR_PRINT("No such input: '" + cur_transition_request + "'");
  1041. }
  1042. cur_transition_request = String();
  1043. set_parameter(transition_request, cur_transition_request);
  1044. }
  1045. if (clear_remaining_fade) {
  1046. cur_prev_xfading = 0;
  1047. set_parameter(prev_xfading, 0);
  1048. cur_prev_index = -1;
  1049. set_parameter(prev_index, -1);
  1050. }
  1051. AnimationMixer::PlaybackInfo pi = p_playback_info;
  1052. // Special case for restart.
  1053. if (restart) {
  1054. pi.time = 0;
  1055. pi.seeked = true;
  1056. pi.weight = 1.0;
  1057. return blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
  1058. }
  1059. if (switched) {
  1060. cur_prev_xfading = xfade_time;
  1061. }
  1062. if (cur_current_index < 0 || cur_current_index >= get_input_count() || cur_prev_index >= get_input_count()) {
  1063. return NodeTimeInfo();
  1064. }
  1065. if (sync) {
  1066. pi.weight = 0;
  1067. for (int i = 0; i < get_input_count(); i++) {
  1068. if (i != cur_current_index && i != cur_prev_index) {
  1069. blend_input(i, pi, FILTER_IGNORE, true, p_test_only);
  1070. }
  1071. }
  1072. }
  1073. if (cur_prev_index < 0) { // Process current animation, check for transition.
  1074. pi.weight = 1.0;
  1075. cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
  1076. 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)) {
  1077. set_parameter(transition_request, get_input_name((cur_current_index + 1) % get_input_count()));
  1078. }
  1079. } else { // Cross-fading from prev to current.
  1080. real_t blend = 0.0;
  1081. real_t blend_inv = 1.0;
  1082. bool use_blend = sync;
  1083. if (xfade_time > 0) {
  1084. use_blend = true;
  1085. blend = cur_prev_xfading / xfade_time;
  1086. if (xfade_curve.is_valid()) {
  1087. blend = xfade_curve->sample(blend);
  1088. }
  1089. blend_inv = 1.0 - blend;
  1090. blend = Math::is_zero_approx(blend) ? CMP_EPSILON : blend;
  1091. blend_inv = Math::is_zero_approx(blend_inv) ? CMP_EPSILON : blend_inv;
  1092. }
  1093. // Blend values must be more than CMP_EPSILON to process discrete keys in edge.
  1094. pi.weight = blend_inv;
  1095. if (input_data[cur_current_index].reset && !p_seek && switched) { // Just switched, seek to start of current.
  1096. pi.time = 0;
  1097. pi.seeked = true;
  1098. }
  1099. cur_nti = blend_input(cur_current_index, pi, FILTER_IGNORE, true, p_test_only);
  1100. pi = p_playback_info;
  1101. pi.seeked &= use_blend;
  1102. pi.weight = blend;
  1103. blend_input(cur_prev_index, pi, FILTER_IGNORE, true, p_test_only);
  1104. if (!p_seek) {
  1105. if (Animation::is_less_or_equal_approx(cur_prev_xfading, 0)) {
  1106. set_parameter(prev_index, -1);
  1107. }
  1108. cur_prev_xfading -= Math::abs(p_playback_info.delta);
  1109. }
  1110. }
  1111. set_parameter(prev_xfading, cur_prev_xfading);
  1112. return cur_nti;
  1113. }
  1114. void AnimationNodeTransition::_get_property_list(List<PropertyInfo> *p_list) const {
  1115. for (int i = 0; i < get_input_count(); i++) {
  1116. p_list->push_back(PropertyInfo(Variant::STRING, "input_" + itos(i) + "/name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1117. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/auto_advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1118. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/break_loop_at_end", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1119. p_list->push_back(PropertyInfo(Variant::BOOL, "input_" + itos(i) + "/reset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL));
  1120. }
  1121. }
  1122. void AnimationNodeTransition::_bind_methods() {
  1123. ClassDB::bind_method(D_METHOD("set_input_count", "input_count"), &AnimationNodeTransition::set_input_count);
  1124. ClassDB::bind_method(D_METHOD("set_input_as_auto_advance", "input", "enable"), &AnimationNodeTransition::set_input_as_auto_advance);
  1125. ClassDB::bind_method(D_METHOD("is_input_set_as_auto_advance", "input"), &AnimationNodeTransition::is_input_set_as_auto_advance);
  1126. ClassDB::bind_method(D_METHOD("set_input_break_loop_at_end", "input", "enable"), &AnimationNodeTransition::set_input_break_loop_at_end);
  1127. ClassDB::bind_method(D_METHOD("is_input_loop_broken_at_end", "input"), &AnimationNodeTransition::is_input_loop_broken_at_end);
  1128. ClassDB::bind_method(D_METHOD("set_input_reset", "input", "enable"), &AnimationNodeTransition::set_input_reset);
  1129. ClassDB::bind_method(D_METHOD("is_input_reset", "input"), &AnimationNodeTransition::is_input_reset);
  1130. ClassDB::bind_method(D_METHOD("set_xfade_time", "time"), &AnimationNodeTransition::set_xfade_time);
  1131. ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeTransition::get_xfade_time);
  1132. ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeTransition::set_xfade_curve);
  1133. ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeTransition::get_xfade_curve);
  1134. ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeTransition::set_allow_transition_to_self);
  1135. ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeTransition::is_allow_transition_to_self);
  1136. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,120,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");
  1137. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");
  1138. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");
  1139. 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");
  1140. }
  1141. AnimationNodeTransition::AnimationNodeTransition() {
  1142. }
  1143. /////////////////////
  1144. String AnimationNodeOutput::get_caption() const {
  1145. return "Output";
  1146. }
  1147. AnimationNode::NodeTimeInfo AnimationNodeOutput::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  1148. AnimationMixer::PlaybackInfo pi = p_playback_info;
  1149. pi.weight = 1.0;
  1150. return blend_input(0, pi, FILTER_IGNORE, true, p_test_only);
  1151. }
  1152. AnimationNodeOutput::AnimationNodeOutput() {
  1153. add_input("output");
  1154. }
  1155. ///////////////////////////////////////////////////////
  1156. void AnimationNodeBlendTree::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {
  1157. ERR_FAIL_COND(nodes.has(p_name));
  1158. ERR_FAIL_COND(p_node.is_null());
  1159. ERR_FAIL_COND(p_name == SceneStringName(output));
  1160. ERR_FAIL_COND(String(p_name).contains("/"));
  1161. Node n;
  1162. n.node = p_node;
  1163. n.position = p_position;
  1164. n.connections.resize(n.node->get_input_count());
  1165. nodes[p_name] = n;
  1166. emit_changed();
  1167. emit_signal(SNAME("tree_changed"));
  1168. p_node->connect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed), CONNECT_REFERENCE_COUNTED);
  1169. p_node->connect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  1170. p_node->connect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  1171. p_node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_name), CONNECT_REFERENCE_COUNTED);
  1172. }
  1173. Ref<AnimationNode> AnimationNodeBlendTree::get_node(const StringName &p_name) const {
  1174. ERR_FAIL_COND_V(!nodes.has(p_name), Ref<AnimationNode>());
  1175. return nodes[p_name].node;
  1176. }
  1177. StringName AnimationNodeBlendTree::get_node_name(const Ref<AnimationNode> &p_node) const {
  1178. for (const KeyValue<StringName, Node> &E : nodes) {
  1179. if (E.value.node == p_node) {
  1180. return E.key;
  1181. }
  1182. }
  1183. ERR_FAIL_V(StringName());
  1184. }
  1185. void AnimationNodeBlendTree::set_node_position(const StringName &p_node, const Vector2 &p_position) {
  1186. ERR_FAIL_COND(!nodes.has(p_node));
  1187. nodes[p_node].position = p_position;
  1188. }
  1189. Vector2 AnimationNodeBlendTree::get_node_position(const StringName &p_node) const {
  1190. ERR_FAIL_COND_V(!nodes.has(p_node), Vector2());
  1191. return nodes[p_node].position;
  1192. }
  1193. void AnimationNodeBlendTree::get_child_nodes(List<ChildNode> *r_child_nodes) {
  1194. Vector<StringName> ns;
  1195. for (const KeyValue<StringName, Node> &E : nodes) {
  1196. ns.push_back(E.key);
  1197. }
  1198. for (int i = 0; i < ns.size(); i++) {
  1199. ChildNode cn;
  1200. cn.name = ns[i];
  1201. cn.node = nodes[cn.name].node;
  1202. r_child_nodes->push_back(cn);
  1203. }
  1204. }
  1205. bool AnimationNodeBlendTree::has_node(const StringName &p_name) const {
  1206. return nodes.has(p_name);
  1207. }
  1208. Vector<StringName> AnimationNodeBlendTree::get_node_connection_array(const StringName &p_name) const {
  1209. ERR_FAIL_COND_V(!nodes.has(p_name), Vector<StringName>());
  1210. return nodes[p_name].connections;
  1211. }
  1212. void AnimationNodeBlendTree::remove_node(const StringName &p_name) {
  1213. ERR_FAIL_COND(!nodes.has(p_name));
  1214. ERR_FAIL_COND(p_name == SceneStringName(output)); //can't delete output
  1215. {
  1216. Ref<AnimationNode> node = nodes[p_name].node;
  1217. node->disconnect(SNAME("tree_changed"), callable_mp(this, &AnimationNodeBlendTree::_tree_changed));
  1218. node->disconnect(SNAME("animation_node_renamed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_renamed));
  1219. node->disconnect(SNAME("animation_node_removed"), callable_mp(this, &AnimationNodeBlendTree::_animation_node_removed));
  1220. node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  1221. }
  1222. nodes.erase(p_name);
  1223. // Erase connections to name.
  1224. for (KeyValue<StringName, Node> &E : nodes) {
  1225. for (int i = 0; i < E.value.connections.size(); i++) {
  1226. if (E.value.connections[i] == p_name) {
  1227. E.value.connections.write[i] = StringName();
  1228. }
  1229. }
  1230. }
  1231. emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);
  1232. emit_changed();
  1233. emit_signal(SNAME("tree_changed"));
  1234. }
  1235. void AnimationNodeBlendTree::rename_node(const StringName &p_name, const StringName &p_new_name) {
  1236. ERR_FAIL_COND(!nodes.has(p_name));
  1237. ERR_FAIL_COND(nodes.has(p_new_name));
  1238. ERR_FAIL_COND(p_name == SceneStringName(output));
  1239. ERR_FAIL_COND(p_new_name == SceneStringName(output));
  1240. nodes[p_name].node->disconnect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed));
  1241. nodes[p_new_name] = nodes[p_name];
  1242. nodes.erase(p_name);
  1243. // Rename connections.
  1244. for (KeyValue<StringName, Node> &E : nodes) {
  1245. for (int i = 0; i < E.value.connections.size(); i++) {
  1246. if (E.value.connections[i] == p_name) {
  1247. E.value.connections.write[i] = p_new_name;
  1248. }
  1249. }
  1250. }
  1251. // Connection must be done with new name.
  1252. nodes[p_new_name].node->connect_changed(callable_mp(this, &AnimationNodeBlendTree::_node_changed).bind(p_new_name), CONNECT_REFERENCE_COUNTED);
  1253. emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);
  1254. emit_signal(SNAME("tree_changed"));
  1255. }
  1256. void AnimationNodeBlendTree::connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) {
  1257. ERR_FAIL_COND(!nodes.has(p_output_node));
  1258. ERR_FAIL_COND(!nodes.has(p_input_node));
  1259. ERR_FAIL_COND(p_output_node == SceneStringName(output));
  1260. ERR_FAIL_COND(p_input_node == p_output_node);
  1261. Ref<AnimationNode> input = nodes[p_input_node].node;
  1262. ERR_FAIL_INDEX(p_input_index, nodes[p_input_node].connections.size());
  1263. for (KeyValue<StringName, Node> &E : nodes) {
  1264. for (int i = 0; i < E.value.connections.size(); i++) {
  1265. StringName output = E.value.connections[i];
  1266. ERR_FAIL_COND(output == p_output_node);
  1267. }
  1268. }
  1269. nodes[p_input_node].connections.write[p_input_index] = p_output_node;
  1270. emit_changed();
  1271. }
  1272. void AnimationNodeBlendTree::disconnect_node(const StringName &p_node, int p_input_index) {
  1273. ERR_FAIL_COND(!nodes.has(p_node));
  1274. Ref<AnimationNode> input = nodes[p_node].node;
  1275. ERR_FAIL_INDEX(p_input_index, nodes[p_node].connections.size());
  1276. nodes[p_node].connections.write[p_input_index] = StringName();
  1277. }
  1278. AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node(const StringName &p_input_node, int p_input_index, const StringName &p_output_node) const {
  1279. if (!nodes.has(p_output_node) || p_output_node == SceneStringName(output)) {
  1280. return CONNECTION_ERROR_NO_OUTPUT;
  1281. }
  1282. if (!nodes.has(p_input_node)) {
  1283. return CONNECTION_ERROR_NO_INPUT;
  1284. }
  1285. if (p_input_node == p_output_node) {
  1286. return CONNECTION_ERROR_SAME_NODE;
  1287. }
  1288. Ref<AnimationNode> input = nodes[p_input_node].node;
  1289. if (p_input_index < 0 || p_input_index >= nodes[p_input_node].connections.size()) {
  1290. return CONNECTION_ERROR_NO_INPUT_INDEX;
  1291. }
  1292. if (nodes[p_input_node].connections[p_input_index] != StringName()) {
  1293. return CONNECTION_ERROR_CONNECTION_EXISTS;
  1294. }
  1295. for (const KeyValue<StringName, Node> &E : nodes) {
  1296. for (int i = 0; i < E.value.connections.size(); i++) {
  1297. const StringName output = E.value.connections[i];
  1298. if (output == p_output_node) {
  1299. return CONNECTION_ERROR_CONNECTION_EXISTS;
  1300. }
  1301. }
  1302. }
  1303. return CONNECTION_OK;
  1304. }
  1305. void AnimationNodeBlendTree::get_node_connections(List<NodeConnection> *r_connections) const {
  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 != StringName()) {
  1310. NodeConnection nc;
  1311. nc.input_node = E.key;
  1312. nc.input_index = i;
  1313. nc.output_node = output;
  1314. r_connections->push_back(nc);
  1315. }
  1316. }
  1317. }
  1318. }
  1319. String AnimationNodeBlendTree::get_caption() const {
  1320. return "BlendTree";
  1321. }
  1322. AnimationNode::NodeTimeInfo AnimationNodeBlendTree::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  1323. Ref<AnimationNodeOutput> output = nodes[SceneStringName(output)].node;
  1324. node_state.connections = nodes[SceneStringName(output)].connections;
  1325. ERR_FAIL_COND_V(output.is_null(), NodeTimeInfo());
  1326. AnimationMixer::PlaybackInfo pi = p_playback_info;
  1327. pi.weight = 1.0;
  1328. return _blend_node(output, "output", this, pi, FILTER_IGNORE, true, p_test_only, nullptr);
  1329. }
  1330. void AnimationNodeBlendTree::get_node_list(List<StringName> *r_list) {
  1331. for (const KeyValue<StringName, Node> &E : nodes) {
  1332. r_list->push_back(E.key);
  1333. }
  1334. }
  1335. void AnimationNodeBlendTree::set_graph_offset(const Vector2 &p_graph_offset) {
  1336. graph_offset = p_graph_offset;
  1337. }
  1338. Vector2 AnimationNodeBlendTree::get_graph_offset() const {
  1339. return graph_offset;
  1340. }
  1341. Ref<AnimationNode> AnimationNodeBlendTree::get_child_by_name(const StringName &p_name) const {
  1342. return get_node(p_name);
  1343. }
  1344. bool AnimationNodeBlendTree::_set(const StringName &p_name, const Variant &p_value) {
  1345. String prop_name = p_name;
  1346. if (prop_name.begins_with("nodes/")) {
  1347. String node_name = prop_name.get_slicec('/', 1);
  1348. String what = prop_name.get_slicec('/', 2);
  1349. if (what == "node") {
  1350. Ref<AnimationNode> anode = p_value;
  1351. if (anode.is_valid()) {
  1352. add_node(node_name, p_value);
  1353. }
  1354. return true;
  1355. }
  1356. if (what == "position") {
  1357. if (nodes.has(node_name)) {
  1358. nodes[node_name].position = p_value;
  1359. }
  1360. return true;
  1361. }
  1362. } else if (prop_name == "node_connections") {
  1363. Array conns = p_value;
  1364. ERR_FAIL_COND_V(conns.size() % 3 != 0, false);
  1365. for (int i = 0; i < conns.size(); i += 3) {
  1366. connect_node(conns[i], conns[i + 1], conns[i + 2]);
  1367. }
  1368. return true;
  1369. }
  1370. return false;
  1371. }
  1372. bool AnimationNodeBlendTree::_get(const StringName &p_name, Variant &r_ret) const {
  1373. String prop_name = p_name;
  1374. if (prop_name.begins_with("nodes/")) {
  1375. String node_name = prop_name.get_slicec('/', 1);
  1376. String what = prop_name.get_slicec('/', 2);
  1377. if (what == "node") {
  1378. if (nodes.has(node_name)) {
  1379. r_ret = nodes[node_name].node;
  1380. return true;
  1381. }
  1382. }
  1383. if (what == "position") {
  1384. if (nodes.has(node_name)) {
  1385. r_ret = nodes[node_name].position;
  1386. return true;
  1387. }
  1388. }
  1389. } else if (prop_name == "node_connections") {
  1390. List<NodeConnection> nc;
  1391. get_node_connections(&nc);
  1392. Array conns;
  1393. conns.resize(nc.size() * 3);
  1394. int idx = 0;
  1395. for (const NodeConnection &E : nc) {
  1396. conns[idx * 3 + 0] = E.input_node;
  1397. conns[idx * 3 + 1] = E.input_index;
  1398. conns[idx * 3 + 2] = E.output_node;
  1399. idx++;
  1400. }
  1401. r_ret = conns;
  1402. return true;
  1403. }
  1404. return false;
  1405. }
  1406. void AnimationNodeBlendTree::_get_property_list(List<PropertyInfo> *p_list) const {
  1407. List<StringName> names;
  1408. for (const KeyValue<StringName, Node> &E : nodes) {
  1409. names.push_back(E.key);
  1410. }
  1411. for (const StringName &E : names) {
  1412. String prop_name = E;
  1413. if (prop_name != "output") {
  1414. p_list->push_back(PropertyInfo(Variant::OBJECT, "nodes/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));
  1415. }
  1416. p_list->push_back(PropertyInfo(Variant::VECTOR2, "nodes/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1417. }
  1418. p_list->push_back(PropertyInfo(Variant::ARRAY, "node_connections", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  1419. }
  1420. void AnimationNodeBlendTree::_tree_changed() {
  1421. AnimationRootNode::_tree_changed();
  1422. }
  1423. void AnimationNodeBlendTree::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  1424. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  1425. }
  1426. void AnimationNodeBlendTree::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  1427. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  1428. }
  1429. void AnimationNodeBlendTree::reset_state() {
  1430. graph_offset = Vector2();
  1431. nodes.clear();
  1432. _initialize_node_tree();
  1433. emit_changed();
  1434. emit_signal(SNAME("tree_changed"));
  1435. }
  1436. void AnimationNodeBlendTree::_node_changed(const StringName &p_node) {
  1437. ERR_FAIL_COND(!nodes.has(p_node));
  1438. nodes[p_node].connections.resize(nodes[p_node].node->get_input_count());
  1439. emit_signal(SNAME("node_changed"), p_node);
  1440. }
  1441. #ifdef TOOLS_ENABLED
  1442. void AnimationNodeBlendTree::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
  1443. const String pf = p_function;
  1444. bool add_node_options = false;
  1445. if (p_idx == 0) {
  1446. 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");
  1447. } else if (p_idx == 2) {
  1448. add_node_options = (pf == "connect_node" || pf == "disconnect_node");
  1449. }
  1450. if (add_node_options) {
  1451. for (const KeyValue<StringName, Node> &E : nodes) {
  1452. r_options->push_back(String(E.key).quote());
  1453. }
  1454. }
  1455. AnimationRootNode::get_argument_options(p_function, p_idx, r_options);
  1456. }
  1457. #endif
  1458. void AnimationNodeBlendTree::_bind_methods() {
  1459. ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeBlendTree::add_node, DEFVAL(Vector2()));
  1460. ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeBlendTree::get_node);
  1461. ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeBlendTree::remove_node);
  1462. ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeBlendTree::rename_node);
  1463. ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeBlendTree::has_node);
  1464. ClassDB::bind_method(D_METHOD("connect_node", "input_node", "input_index", "output_node"), &AnimationNodeBlendTree::connect_node);
  1465. ClassDB::bind_method(D_METHOD("disconnect_node", "input_node", "input_index"), &AnimationNodeBlendTree::disconnect_node);
  1466. ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeBlendTree::set_node_position);
  1467. ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeBlendTree::get_node_position);
  1468. ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeBlendTree::set_graph_offset);
  1469. ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeBlendTree::get_graph_offset);
  1470. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_graph_offset", "get_graph_offset");
  1471. BIND_CONSTANT(CONNECTION_OK);
  1472. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT);
  1473. BIND_CONSTANT(CONNECTION_ERROR_NO_INPUT_INDEX);
  1474. BIND_CONSTANT(CONNECTION_ERROR_NO_OUTPUT);
  1475. BIND_CONSTANT(CONNECTION_ERROR_SAME_NODE);
  1476. BIND_CONSTANT(CONNECTION_ERROR_CONNECTION_EXISTS);
  1477. ADD_SIGNAL(MethodInfo(SNAME("node_changed"), PropertyInfo(Variant::STRING_NAME, "node_name")));
  1478. }
  1479. void AnimationNodeBlendTree::_initialize_node_tree() {
  1480. Ref<AnimationNodeOutput> output;
  1481. output.instantiate();
  1482. Node n;
  1483. n.node = output;
  1484. n.position = Vector2(300, 150);
  1485. n.connections.resize(1);
  1486. nodes["output"] = n;
  1487. }
  1488. AnimationNodeBlendTree::AnimationNodeBlendTree() {
  1489. _initialize_node_tree();
  1490. }
  1491. AnimationNodeBlendTree::~AnimationNodeBlendTree() {
  1492. }