animation.cpp 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943
  1. /**************************************************************************/
  2. /* animation.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.h"
  31. #include "scene/scene_string_names.h"
  32. #include "core/math/geometry.h"
  33. bool Animation::_set(const StringName &p_name, const Variant &p_value) {
  34. String name = p_name;
  35. if (name.begins_with("tracks/")) {
  36. int track = name.get_slicec('/', 1).to_int();
  37. String what = name.get_slicec('/', 2);
  38. if (tracks.size() == track && what == "type") {
  39. String type = p_value;
  40. if (type == "transform") {
  41. add_track(TYPE_TRANSFORM);
  42. } else if (type == "value") {
  43. add_track(TYPE_VALUE);
  44. } else if (type == "method") {
  45. add_track(TYPE_METHOD);
  46. } else if (type == "bezier") {
  47. add_track(TYPE_BEZIER);
  48. } else if (type == "audio") {
  49. add_track(TYPE_AUDIO);
  50. } else if (type == "animation") {
  51. add_track(TYPE_ANIMATION);
  52. } else {
  53. return false;
  54. }
  55. return true;
  56. }
  57. ERR_FAIL_INDEX_V(track, tracks.size(), false);
  58. if (what == "path") {
  59. track_set_path(track, p_value);
  60. } else if (what == "interp") {
  61. track_set_interpolation_type(track, InterpolationType(p_value.operator int()));
  62. } else if (what == "loop_wrap") {
  63. track_set_interpolation_loop_wrap(track, p_value);
  64. } else if (what == "imported") {
  65. track_set_imported(track, p_value);
  66. } else if (what == "enabled") {
  67. track_set_enabled(track, p_value);
  68. } else if (what == "keys" || what == "key_values") {
  69. if (track_get_type(track) == TYPE_TRANSFORM) {
  70. TransformTrack *tt = static_cast<TransformTrack *>(tracks[track]);
  71. PoolVector<float> values = p_value;
  72. int vcount = values.size();
  73. ERR_FAIL_COND_V(vcount % 12, false); // should be multiple of 11
  74. PoolVector<float>::Read r = values.read();
  75. tt->transforms.resize(vcount / 12);
  76. for (int i = 0; i < (vcount / 12); i++) {
  77. TKey<TransformKey> &tk = tt->transforms.write[i];
  78. const float *ofs = &r[i * 12];
  79. tk.time = ofs[0];
  80. tk.transition = ofs[1];
  81. tk.value.loc.x = ofs[2];
  82. tk.value.loc.y = ofs[3];
  83. tk.value.loc.z = ofs[4];
  84. tk.value.rot.x = ofs[5];
  85. tk.value.rot.y = ofs[6];
  86. tk.value.rot.z = ofs[7];
  87. tk.value.rot.w = ofs[8];
  88. tk.value.scale.x = ofs[9];
  89. tk.value.scale.y = ofs[10];
  90. tk.value.scale.z = ofs[11];
  91. }
  92. } else if (track_get_type(track) == TYPE_VALUE) {
  93. ValueTrack *vt = static_cast<ValueTrack *>(tracks[track]);
  94. Dictionary d = p_value;
  95. ERR_FAIL_COND_V(!d.has("times"), false);
  96. ERR_FAIL_COND_V(!d.has("values"), false);
  97. if (d.has("cont")) {
  98. bool v = d["cont"];
  99. vt->update_mode = v ? UPDATE_CONTINUOUS : UPDATE_DISCRETE;
  100. }
  101. if (d.has("update")) {
  102. int um = d["update"];
  103. if (um < 0) {
  104. um = 0;
  105. } else if (um > 3) {
  106. um = 3;
  107. }
  108. vt->update_mode = UpdateMode(um);
  109. }
  110. PoolVector<float> times = d["times"];
  111. Array values = d["values"];
  112. ERR_FAIL_COND_V(times.size() != values.size(), false);
  113. if (times.size()) {
  114. int valcount = times.size();
  115. PoolVector<float>::Read rt = times.read();
  116. vt->values.resize(valcount);
  117. for (int i = 0; i < valcount; i++) {
  118. vt->values.write[i].time = rt[i];
  119. vt->values.write[i].value = values[i];
  120. }
  121. if (d.has("transitions")) {
  122. PoolVector<float> transitions = d["transitions"];
  123. ERR_FAIL_COND_V(transitions.size() != valcount, false);
  124. PoolVector<float>::Read rtr = transitions.read();
  125. for (int i = 0; i < valcount; i++) {
  126. vt->values.write[i].transition = rtr[i];
  127. }
  128. }
  129. }
  130. return true;
  131. } else if (track_get_type(track) == TYPE_METHOD) {
  132. while (track_get_key_count(track)) {
  133. track_remove_key(track, 0); //well shouldn't be set anyway
  134. }
  135. Dictionary d = p_value;
  136. ERR_FAIL_COND_V(!d.has("times"), false);
  137. ERR_FAIL_COND_V(!d.has("values"), false);
  138. PoolVector<float> times = d["times"];
  139. Array values = d["values"];
  140. ERR_FAIL_COND_V(times.size() != values.size(), false);
  141. if (times.size()) {
  142. int valcount = times.size();
  143. PoolVector<float>::Read rt = times.read();
  144. for (int i = 0; i < valcount; i++) {
  145. track_insert_key(track, rt[i], values[i]);
  146. }
  147. if (d.has("transitions")) {
  148. PoolVector<float> transitions = d["transitions"];
  149. ERR_FAIL_COND_V(transitions.size() != valcount, false);
  150. PoolVector<float>::Read rtr = transitions.read();
  151. for (int i = 0; i < valcount; i++) {
  152. track_set_key_transition(track, i, rtr[i]);
  153. }
  154. }
  155. }
  156. } else if (track_get_type(track) == TYPE_BEZIER) {
  157. BezierTrack *bt = static_cast<BezierTrack *>(tracks[track]);
  158. Dictionary d = p_value;
  159. ERR_FAIL_COND_V(!d.has("times"), false);
  160. ERR_FAIL_COND_V(!d.has("points"), false);
  161. PoolVector<float> times = d["times"];
  162. PoolRealArray values = d["points"];
  163. ERR_FAIL_COND_V(times.size() * 5 != values.size(), false);
  164. if (times.size()) {
  165. int valcount = times.size();
  166. PoolVector<float>::Read rt = times.read();
  167. PoolVector<float>::Read rv = values.read();
  168. bt->values.resize(valcount);
  169. for (int i = 0; i < valcount; i++) {
  170. bt->values.write[i].time = rt[i];
  171. bt->values.write[i].transition = 0; //unused in bezier
  172. bt->values.write[i].value.value = rv[i * 5 + 0];
  173. bt->values.write[i].value.in_handle.x = rv[i * 5 + 1];
  174. bt->values.write[i].value.in_handle.y = rv[i * 5 + 2];
  175. bt->values.write[i].value.out_handle.x = rv[i * 5 + 3];
  176. bt->values.write[i].value.out_handle.y = rv[i * 5 + 4];
  177. }
  178. }
  179. return true;
  180. } else if (track_get_type(track) == TYPE_AUDIO) {
  181. AudioTrack *ad = static_cast<AudioTrack *>(tracks[track]);
  182. Dictionary d = p_value;
  183. ERR_FAIL_COND_V(!d.has("times"), false);
  184. ERR_FAIL_COND_V(!d.has("clips"), false);
  185. PoolVector<float> times = d["times"];
  186. Array clips = d["clips"];
  187. ERR_FAIL_COND_V(clips.size() != times.size(), false);
  188. if (times.size()) {
  189. int valcount = times.size();
  190. PoolVector<float>::Read rt = times.read();
  191. ad->values.clear();
  192. for (int i = 0; i < valcount; i++) {
  193. Dictionary d2 = clips[i];
  194. if (!d2.has("start_offset")) {
  195. continue;
  196. }
  197. if (!d2.has("end_offset")) {
  198. continue;
  199. }
  200. if (!d2.has("stream")) {
  201. continue;
  202. }
  203. TKey<AudioKey> ak;
  204. ak.time = rt[i];
  205. ak.value.start_offset = d2["start_offset"];
  206. ak.value.end_offset = d2["end_offset"];
  207. ak.value.stream = d2["stream"];
  208. ad->values.push_back(ak);
  209. }
  210. }
  211. return true;
  212. } else if (track_get_type(track) == TYPE_ANIMATION) {
  213. AnimationTrack *an = static_cast<AnimationTrack *>(tracks[track]);
  214. Dictionary d = p_value;
  215. ERR_FAIL_COND_V(!d.has("times"), false);
  216. ERR_FAIL_COND_V(!d.has("clips"), false);
  217. PoolVector<float> times = d["times"];
  218. PoolVector<String> clips = d["clips"];
  219. ERR_FAIL_COND_V(clips.size() != times.size(), false);
  220. if (times.size()) {
  221. int valcount = times.size();
  222. PoolVector<float>::Read rt = times.read();
  223. PoolVector<String>::Read rc = clips.read();
  224. an->values.resize(valcount);
  225. for (int i = 0; i < valcount; i++) {
  226. TKey<StringName> ak;
  227. ak.time = rt[i];
  228. ak.value = rc[i];
  229. an->values.write[i] = ak;
  230. }
  231. }
  232. return true;
  233. } else {
  234. return false;
  235. }
  236. } else {
  237. return false;
  238. }
  239. } else {
  240. return false;
  241. }
  242. return true;
  243. }
  244. bool Animation::_get(const StringName &p_name, Variant &r_ret) const {
  245. String name = p_name;
  246. if (name == "length") {
  247. r_ret = length;
  248. } else if (name == "loop") {
  249. r_ret = loop;
  250. } else if (name == "step") {
  251. r_ret = step;
  252. } else if (name.begins_with("tracks/")) {
  253. int track = name.get_slicec('/', 1).to_int();
  254. String what = name.get_slicec('/', 2);
  255. ERR_FAIL_INDEX_V(track, tracks.size(), false);
  256. if (what == "type") {
  257. switch (track_get_type(track)) {
  258. case TYPE_TRANSFORM:
  259. r_ret = "transform";
  260. break;
  261. case TYPE_VALUE:
  262. r_ret = "value";
  263. break;
  264. case TYPE_METHOD:
  265. r_ret = "method";
  266. break;
  267. case TYPE_BEZIER:
  268. r_ret = "bezier";
  269. break;
  270. case TYPE_AUDIO:
  271. r_ret = "audio";
  272. break;
  273. case TYPE_ANIMATION:
  274. r_ret = "animation";
  275. break;
  276. }
  277. return true;
  278. } else if (what == "path") {
  279. r_ret = track_get_path(track);
  280. } else if (what == "interp") {
  281. r_ret = track_get_interpolation_type(track);
  282. } else if (what == "loop_wrap") {
  283. r_ret = track_get_interpolation_loop_wrap(track);
  284. } else if (what == "imported") {
  285. r_ret = track_is_imported(track);
  286. } else if (what == "enabled") {
  287. r_ret = track_is_enabled(track);
  288. } else if (what == "keys") {
  289. if (track_get_type(track) == TYPE_TRANSFORM) {
  290. PoolVector<real_t> keys;
  291. int kk = track_get_key_count(track);
  292. keys.resize(kk * 12);
  293. PoolVector<real_t>::Write w = keys.write();
  294. int idx = 0;
  295. for (int i = 0; i < track_get_key_count(track); i++) {
  296. Vector3 loc;
  297. Quat rot;
  298. Vector3 scale;
  299. transform_track_get_key(track, i, &loc, &rot, &scale);
  300. w[idx++] = track_get_key_time(track, i);
  301. w[idx++] = track_get_key_transition(track, i);
  302. w[idx++] = loc.x;
  303. w[idx++] = loc.y;
  304. w[idx++] = loc.z;
  305. w[idx++] = rot.x;
  306. w[idx++] = rot.y;
  307. w[idx++] = rot.z;
  308. w[idx++] = rot.w;
  309. w[idx++] = scale.x;
  310. w[idx++] = scale.y;
  311. w[idx++] = scale.z;
  312. }
  313. w.release();
  314. r_ret = keys;
  315. return true;
  316. } else if (track_get_type(track) == TYPE_VALUE) {
  317. const ValueTrack *vt = static_cast<const ValueTrack *>(tracks[track]);
  318. Dictionary d;
  319. PoolVector<float> key_times;
  320. PoolVector<float> key_transitions;
  321. Array key_values;
  322. int kk = vt->values.size();
  323. key_times.resize(kk);
  324. key_transitions.resize(kk);
  325. key_values.resize(kk);
  326. PoolVector<float>::Write wti = key_times.write();
  327. PoolVector<float>::Write wtr = key_transitions.write();
  328. int idx = 0;
  329. const TKey<Variant> *vls = vt->values.ptr();
  330. for (int i = 0; i < kk; i++) {
  331. wti[idx] = vls[i].time;
  332. wtr[idx] = vls[i].transition;
  333. key_values[idx] = vls[i].value;
  334. idx++;
  335. }
  336. wti.release();
  337. wtr.release();
  338. d["times"] = key_times;
  339. d["transitions"] = key_transitions;
  340. d["values"] = key_values;
  341. if (track_get_type(track) == TYPE_VALUE) {
  342. d["update"] = value_track_get_update_mode(track);
  343. }
  344. r_ret = d;
  345. return true;
  346. } else if (track_get_type(track) == TYPE_METHOD) {
  347. Dictionary d;
  348. PoolVector<float> key_times;
  349. PoolVector<float> key_transitions;
  350. Array key_values;
  351. int kk = track_get_key_count(track);
  352. key_times.resize(kk);
  353. key_transitions.resize(kk);
  354. key_values.resize(kk);
  355. PoolVector<float>::Write wti = key_times.write();
  356. PoolVector<float>::Write wtr = key_transitions.write();
  357. int idx = 0;
  358. for (int i = 0; i < track_get_key_count(track); i++) {
  359. wti[idx] = track_get_key_time(track, i);
  360. wtr[idx] = track_get_key_transition(track, i);
  361. key_values[idx] = track_get_key_value(track, i);
  362. idx++;
  363. }
  364. wti.release();
  365. wtr.release();
  366. d["times"] = key_times;
  367. d["transitions"] = key_transitions;
  368. d["values"] = key_values;
  369. if (track_get_type(track) == TYPE_VALUE) {
  370. d["update"] = value_track_get_update_mode(track);
  371. }
  372. r_ret = d;
  373. return true;
  374. } else if (track_get_type(track) == TYPE_BEZIER) {
  375. const BezierTrack *bt = static_cast<const BezierTrack *>(tracks[track]);
  376. Dictionary d;
  377. PoolVector<float> key_times;
  378. PoolVector<float> key_points;
  379. int kk = bt->values.size();
  380. key_times.resize(kk);
  381. key_points.resize(kk * 5);
  382. PoolVector<float>::Write wti = key_times.write();
  383. PoolVector<float>::Write wpo = key_points.write();
  384. int idx = 0;
  385. const TKey<BezierKey> *vls = bt->values.ptr();
  386. for (int i = 0; i < kk; i++) {
  387. wti[idx] = vls[i].time;
  388. wpo[idx * 5 + 0] = vls[i].value.value;
  389. wpo[idx * 5 + 1] = vls[i].value.in_handle.x;
  390. wpo[idx * 5 + 2] = vls[i].value.in_handle.y;
  391. wpo[idx * 5 + 3] = vls[i].value.out_handle.x;
  392. wpo[idx * 5 + 4] = vls[i].value.out_handle.y;
  393. idx++;
  394. }
  395. wti.release();
  396. wpo.release();
  397. d["times"] = key_times;
  398. d["points"] = key_points;
  399. r_ret = d;
  400. return true;
  401. } else if (track_get_type(track) == TYPE_AUDIO) {
  402. const AudioTrack *ad = static_cast<const AudioTrack *>(tracks[track]);
  403. Dictionary d;
  404. PoolVector<float> key_times;
  405. Array clips;
  406. int kk = ad->values.size();
  407. key_times.resize(kk);
  408. PoolVector<float>::Write wti = key_times.write();
  409. int idx = 0;
  410. const TKey<AudioKey> *vls = ad->values.ptr();
  411. for (int i = 0; i < kk; i++) {
  412. wti[idx] = vls[i].time;
  413. Dictionary clip;
  414. clip["start_offset"] = vls[i].value.start_offset;
  415. clip["end_offset"] = vls[i].value.end_offset;
  416. clip["stream"] = vls[i].value.stream;
  417. clips.push_back(clip);
  418. idx++;
  419. }
  420. wti.release();
  421. d["times"] = key_times;
  422. d["clips"] = clips;
  423. r_ret = d;
  424. return true;
  425. } else if (track_get_type(track) == TYPE_ANIMATION) {
  426. const AnimationTrack *an = static_cast<const AnimationTrack *>(tracks[track]);
  427. Dictionary d;
  428. PoolVector<float> key_times;
  429. PoolVector<String> clips;
  430. int kk = an->values.size();
  431. key_times.resize(kk);
  432. clips.resize(kk);
  433. PoolVector<float>::Write wti = key_times.write();
  434. PoolVector<String>::Write wcl = clips.write();
  435. const TKey<StringName> *vls = an->values.ptr();
  436. for (int i = 0; i < kk; i++) {
  437. wti[i] = vls[i].time;
  438. wcl[i] = vls[i].value;
  439. }
  440. wti.release();
  441. wcl.release();
  442. d["times"] = key_times;
  443. d["clips"] = clips;
  444. r_ret = d;
  445. return true;
  446. }
  447. } else {
  448. return false;
  449. }
  450. } else {
  451. return false;
  452. }
  453. return true;
  454. }
  455. void Animation::_get_property_list(List<PropertyInfo> *p_list) const {
  456. for (int i = 0; i < tracks.size(); i++) {
  457. p_list->push_back(PropertyInfo(Variant::STRING, "tracks/" + itos(i) + "/type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  458. p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tracks/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  459. p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/interp", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  460. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/loop_wrap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  461. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/imported", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  462. p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  463. p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
  464. }
  465. }
  466. int Animation::add_track(TrackType p_type, int p_at_pos) {
  467. if (p_at_pos < 0 || p_at_pos >= tracks.size()) {
  468. p_at_pos = tracks.size();
  469. }
  470. switch (p_type) {
  471. case TYPE_TRANSFORM: {
  472. TransformTrack *tt = memnew(TransformTrack);
  473. tracks.insert(p_at_pos, tt);
  474. } break;
  475. case TYPE_VALUE: {
  476. tracks.insert(p_at_pos, memnew(ValueTrack));
  477. } break;
  478. case TYPE_METHOD: {
  479. tracks.insert(p_at_pos, memnew(MethodTrack));
  480. } break;
  481. case TYPE_BEZIER: {
  482. tracks.insert(p_at_pos, memnew(BezierTrack));
  483. } break;
  484. case TYPE_AUDIO: {
  485. tracks.insert(p_at_pos, memnew(AudioTrack));
  486. } break;
  487. case TYPE_ANIMATION: {
  488. tracks.insert(p_at_pos, memnew(AnimationTrack));
  489. } break;
  490. default: {
  491. ERR_PRINT("Unknown track type");
  492. }
  493. }
  494. emit_changed();
  495. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  496. return p_at_pos;
  497. }
  498. void Animation::remove_track(int p_track) {
  499. ERR_FAIL_INDEX(p_track, tracks.size());
  500. Track *t = tracks[p_track];
  501. switch (t->type) {
  502. case TYPE_TRANSFORM: {
  503. TransformTrack *tt = static_cast<TransformTrack *>(t);
  504. _clear(tt->transforms);
  505. } break;
  506. case TYPE_VALUE: {
  507. ValueTrack *vt = static_cast<ValueTrack *>(t);
  508. _clear(vt->values);
  509. } break;
  510. case TYPE_METHOD: {
  511. MethodTrack *mt = static_cast<MethodTrack *>(t);
  512. _clear(mt->methods);
  513. } break;
  514. case TYPE_BEZIER: {
  515. BezierTrack *bz = static_cast<BezierTrack *>(t);
  516. _clear(bz->values);
  517. } break;
  518. case TYPE_AUDIO: {
  519. AudioTrack *ad = static_cast<AudioTrack *>(t);
  520. _clear(ad->values);
  521. } break;
  522. case TYPE_ANIMATION: {
  523. AnimationTrack *an = static_cast<AnimationTrack *>(t);
  524. _clear(an->values);
  525. } break;
  526. }
  527. memdelete(t);
  528. tracks.remove(p_track);
  529. emit_changed();
  530. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  531. }
  532. int Animation::get_track_count() const {
  533. return tracks.size();
  534. }
  535. Animation::TrackType Animation::track_get_type(int p_track) const {
  536. ERR_FAIL_INDEX_V(p_track, tracks.size(), TYPE_TRANSFORM);
  537. return tracks[p_track]->type;
  538. }
  539. void Animation::track_set_path(int p_track, const NodePath &p_path) {
  540. ERR_FAIL_INDEX(p_track, tracks.size());
  541. tracks[p_track]->path = p_path;
  542. emit_changed();
  543. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  544. }
  545. NodePath Animation::track_get_path(int p_track) const {
  546. ERR_FAIL_INDEX_V(p_track, tracks.size(), NodePath());
  547. return tracks[p_track]->path;
  548. }
  549. int Animation::find_track(const NodePath &p_path) const {
  550. for (int i = 0; i < tracks.size(); i++) {
  551. if (tracks[i]->path == p_path) {
  552. return i;
  553. }
  554. };
  555. return -1;
  556. };
  557. void Animation::track_set_interpolation_type(int p_track, InterpolationType p_interp) {
  558. ERR_FAIL_INDEX(p_track, tracks.size());
  559. ERR_FAIL_INDEX(p_interp, 3);
  560. tracks[p_track]->interpolation = p_interp;
  561. emit_changed();
  562. }
  563. Animation::InterpolationType Animation::track_get_interpolation_type(int p_track) const {
  564. ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
  565. return tracks[p_track]->interpolation;
  566. }
  567. void Animation::track_set_interpolation_loop_wrap(int p_track, bool p_enable) {
  568. ERR_FAIL_INDEX(p_track, tracks.size());
  569. tracks[p_track]->loop_wrap = p_enable;
  570. emit_changed();
  571. }
  572. bool Animation::track_get_interpolation_loop_wrap(int p_track) const {
  573. ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
  574. return tracks[p_track]->loop_wrap;
  575. }
  576. // transform
  577. /*
  578. template<class T>
  579. int Animation::_insert_pos(float p_time, T& p_keys) {
  580. // simple, linear time inset that should be fast enough in reality.
  581. int idx=p_keys.size();
  582. while(true) {
  583. if (idx==0 || p_keys[idx-1].time < p_time) {
  584. //condition for insertion.
  585. p_keys.insert(idx,T());
  586. return idx;
  587. } else if (p_keys[idx-1].time == p_time) {
  588. // condition for replacing.
  589. return idx-1;
  590. }
  591. idx--;
  592. }
  593. }
  594. */
  595. template <class T, class V>
  596. int Animation::_insert(float p_time, T &p_keys, const V &p_value) {
  597. int idx = p_keys.size();
  598. while (true) {
  599. // Condition for replacement.
  600. if (idx > 0 && Math::is_equal_approx(p_keys[idx - 1].time, p_time)) {
  601. float transition = p_keys[idx - 1].transition;
  602. p_keys.write[idx - 1] = p_value;
  603. p_keys.write[idx - 1].transition = transition;
  604. return idx - 1;
  605. // Condition for insert.
  606. } else if (idx == 0 || p_keys[idx - 1].time < p_time) {
  607. p_keys.insert(idx, p_value);
  608. return idx;
  609. }
  610. idx--;
  611. }
  612. return -1;
  613. }
  614. template <class T>
  615. void Animation::_clear(T &p_keys) {
  616. p_keys.clear();
  617. }
  618. Error Animation::transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const {
  619. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  620. Track *t = tracks[p_track];
  621. TransformTrack *tt = static_cast<TransformTrack *>(t);
  622. ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, ERR_INVALID_PARAMETER);
  623. ERR_FAIL_INDEX_V(p_key, tt->transforms.size(), ERR_INVALID_PARAMETER);
  624. if (r_loc) {
  625. *r_loc = tt->transforms[p_key].value.loc;
  626. }
  627. if (r_rot) {
  628. *r_rot = tt->transforms[p_key].value.rot;
  629. }
  630. if (r_scale) {
  631. *r_scale = tt->transforms[p_key].value.scale;
  632. }
  633. return OK;
  634. }
  635. int Animation::transform_track_insert_key(int p_track, float p_time, const Vector3 &p_loc, const Quat &p_rot, const Vector3 &p_scale) {
  636. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  637. Track *t = tracks[p_track];
  638. ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, -1);
  639. TransformTrack *tt = static_cast<TransformTrack *>(t);
  640. TKey<TransformKey> tkey;
  641. tkey.time = p_time;
  642. tkey.value.loc = p_loc;
  643. tkey.value.rot = p_rot;
  644. tkey.value.scale = p_scale;
  645. int ret = _insert(p_time, tt->transforms, tkey);
  646. emit_changed();
  647. return ret;
  648. }
  649. void Animation::track_remove_key_at_position(int p_track, float p_pos) {
  650. int idx = track_find_key(p_track, p_pos, true);
  651. ERR_FAIL_COND(idx < 0);
  652. track_remove_key(p_track, idx);
  653. }
  654. void Animation::track_remove_key(int p_track, int p_idx) {
  655. ERR_FAIL_INDEX(p_track, tracks.size());
  656. Track *t = tracks[p_track];
  657. switch (t->type) {
  658. case TYPE_TRANSFORM: {
  659. TransformTrack *tt = static_cast<TransformTrack *>(t);
  660. ERR_FAIL_INDEX(p_idx, tt->transforms.size());
  661. tt->transforms.remove(p_idx);
  662. } break;
  663. case TYPE_VALUE: {
  664. ValueTrack *vt = static_cast<ValueTrack *>(t);
  665. ERR_FAIL_INDEX(p_idx, vt->values.size());
  666. vt->values.remove(p_idx);
  667. } break;
  668. case TYPE_METHOD: {
  669. MethodTrack *mt = static_cast<MethodTrack *>(t);
  670. ERR_FAIL_INDEX(p_idx, mt->methods.size());
  671. mt->methods.remove(p_idx);
  672. } break;
  673. case TYPE_BEZIER: {
  674. BezierTrack *bz = static_cast<BezierTrack *>(t);
  675. ERR_FAIL_INDEX(p_idx, bz->values.size());
  676. bz->values.remove(p_idx);
  677. } break;
  678. case TYPE_AUDIO: {
  679. AudioTrack *ad = static_cast<AudioTrack *>(t);
  680. ERR_FAIL_INDEX(p_idx, ad->values.size());
  681. ad->values.remove(p_idx);
  682. } break;
  683. case TYPE_ANIMATION: {
  684. AnimationTrack *an = static_cast<AnimationTrack *>(t);
  685. ERR_FAIL_INDEX(p_idx, an->values.size());
  686. an->values.remove(p_idx);
  687. } break;
  688. }
  689. emit_changed();
  690. }
  691. int Animation::track_find_key(int p_track, float p_time, bool p_exact) const {
  692. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  693. Track *t = tracks[p_track];
  694. switch (t->type) {
  695. case TYPE_TRANSFORM: {
  696. TransformTrack *tt = static_cast<TransformTrack *>(t);
  697. int k = _find(tt->transforms, p_time);
  698. if (k < 0 || k >= tt->transforms.size()) {
  699. return -1;
  700. }
  701. if (tt->transforms[k].time != p_time && p_exact) {
  702. return -1;
  703. }
  704. return k;
  705. } break;
  706. case TYPE_VALUE: {
  707. ValueTrack *vt = static_cast<ValueTrack *>(t);
  708. int k = _find(vt->values, p_time);
  709. if (k < 0 || k >= vt->values.size()) {
  710. return -1;
  711. }
  712. if (vt->values[k].time != p_time && p_exact) {
  713. return -1;
  714. }
  715. return k;
  716. } break;
  717. case TYPE_METHOD: {
  718. MethodTrack *mt = static_cast<MethodTrack *>(t);
  719. int k = _find(mt->methods, p_time);
  720. if (k < 0 || k >= mt->methods.size()) {
  721. return -1;
  722. }
  723. if (mt->methods[k].time != p_time && p_exact) {
  724. return -1;
  725. }
  726. return k;
  727. } break;
  728. case TYPE_BEZIER: {
  729. BezierTrack *bt = static_cast<BezierTrack *>(t);
  730. int k = _find(bt->values, p_time);
  731. if (k < 0 || k >= bt->values.size()) {
  732. return -1;
  733. }
  734. if (bt->values[k].time != p_time && p_exact) {
  735. return -1;
  736. }
  737. return k;
  738. } break;
  739. case TYPE_AUDIO: {
  740. AudioTrack *at = static_cast<AudioTrack *>(t);
  741. int k = _find(at->values, p_time);
  742. if (k < 0 || k >= at->values.size()) {
  743. return -1;
  744. }
  745. if (at->values[k].time != p_time && p_exact) {
  746. return -1;
  747. }
  748. return k;
  749. } break;
  750. case TYPE_ANIMATION: {
  751. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  752. int k = _find(at->values, p_time);
  753. if (k < 0 || k >= at->values.size()) {
  754. return -1;
  755. }
  756. if (at->values[k].time != p_time && p_exact) {
  757. return -1;
  758. }
  759. return k;
  760. } break;
  761. }
  762. return -1;
  763. }
  764. void Animation::track_insert_key(int p_track, float p_time, const Variant &p_key, float p_transition) {
  765. ERR_FAIL_INDEX(p_track, tracks.size());
  766. Track *t = tracks[p_track];
  767. switch (t->type) {
  768. case TYPE_TRANSFORM: {
  769. Dictionary d = p_key;
  770. Vector3 loc;
  771. if (d.has("location")) {
  772. loc = d["location"];
  773. }
  774. Quat rot;
  775. if (d.has("rotation")) {
  776. rot = d["rotation"];
  777. }
  778. Vector3 scale;
  779. if (d.has("scale")) {
  780. scale = d["scale"];
  781. }
  782. int idx = transform_track_insert_key(p_track, p_time, loc, rot, scale);
  783. track_set_key_transition(p_track, idx, p_transition);
  784. } break;
  785. case TYPE_VALUE: {
  786. ValueTrack *vt = static_cast<ValueTrack *>(t);
  787. TKey<Variant> k;
  788. k.time = p_time;
  789. k.transition = p_transition;
  790. k.value = p_key;
  791. _insert(p_time, vt->values, k);
  792. } break;
  793. case TYPE_METHOD: {
  794. MethodTrack *mt = static_cast<MethodTrack *>(t);
  795. ERR_FAIL_COND(p_key.get_type() != Variant::DICTIONARY);
  796. Dictionary d = p_key;
  797. ERR_FAIL_COND(!d.has("method") || d["method"].get_type() != Variant::STRING);
  798. ERR_FAIL_COND(!d.has("args") || !d["args"].is_array());
  799. MethodKey k;
  800. k.time = p_time;
  801. k.transition = p_transition;
  802. k.method = d["method"];
  803. k.params = d["args"];
  804. _insert(p_time, mt->methods, k);
  805. } break;
  806. case TYPE_BEZIER: {
  807. BezierTrack *bt = static_cast<BezierTrack *>(t);
  808. Array arr = p_key;
  809. ERR_FAIL_COND(arr.size() != 5);
  810. TKey<BezierKey> k;
  811. k.time = p_time;
  812. k.value.value = arr[0];
  813. k.value.in_handle.x = arr[1];
  814. k.value.in_handle.y = arr[2];
  815. k.value.out_handle.x = arr[3];
  816. k.value.out_handle.y = arr[4];
  817. _insert(p_time, bt->values, k);
  818. } break;
  819. case TYPE_AUDIO: {
  820. AudioTrack *at = static_cast<AudioTrack *>(t);
  821. Dictionary k = p_key;
  822. ERR_FAIL_COND(!k.has("start_offset"));
  823. ERR_FAIL_COND(!k.has("end_offset"));
  824. ERR_FAIL_COND(!k.has("stream"));
  825. TKey<AudioKey> ak;
  826. ak.time = p_time;
  827. ak.value.start_offset = k["start_offset"];
  828. ak.value.end_offset = k["end_offset"];
  829. ak.value.stream = k["stream"];
  830. _insert(p_time, at->values, ak);
  831. } break;
  832. case TYPE_ANIMATION: {
  833. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  834. TKey<StringName> ak;
  835. ak.time = p_time;
  836. ak.value = p_key;
  837. _insert(p_time, at->values, ak);
  838. } break;
  839. }
  840. emit_changed();
  841. }
  842. int Animation::track_get_key_count(int p_track) const {
  843. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  844. Track *t = tracks[p_track];
  845. switch (t->type) {
  846. case TYPE_TRANSFORM: {
  847. TransformTrack *tt = static_cast<TransformTrack *>(t);
  848. return tt->transforms.size();
  849. } break;
  850. case TYPE_VALUE: {
  851. ValueTrack *vt = static_cast<ValueTrack *>(t);
  852. return vt->values.size();
  853. } break;
  854. case TYPE_METHOD: {
  855. MethodTrack *mt = static_cast<MethodTrack *>(t);
  856. return mt->methods.size();
  857. } break;
  858. case TYPE_BEZIER: {
  859. BezierTrack *bt = static_cast<BezierTrack *>(t);
  860. return bt->values.size();
  861. } break;
  862. case TYPE_AUDIO: {
  863. AudioTrack *at = static_cast<AudioTrack *>(t);
  864. return at->values.size();
  865. } break;
  866. case TYPE_ANIMATION: {
  867. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  868. return at->values.size();
  869. } break;
  870. }
  871. ERR_FAIL_V(-1);
  872. }
  873. Variant Animation::track_get_key_value(int p_track, int p_key_idx) const {
  874. ERR_FAIL_INDEX_V(p_track, tracks.size(), Variant());
  875. Track *t = tracks[p_track];
  876. switch (t->type) {
  877. case TYPE_TRANSFORM: {
  878. TransformTrack *tt = static_cast<TransformTrack *>(t);
  879. ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), Variant());
  880. Dictionary d;
  881. d["location"] = tt->transforms[p_key_idx].value.loc;
  882. d["rotation"] = tt->transforms[p_key_idx].value.rot;
  883. d["scale"] = tt->transforms[p_key_idx].value.scale;
  884. return d;
  885. } break;
  886. case TYPE_VALUE: {
  887. ValueTrack *vt = static_cast<ValueTrack *>(t);
  888. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), Variant());
  889. return vt->values[p_key_idx].value;
  890. } break;
  891. case TYPE_METHOD: {
  892. MethodTrack *mt = static_cast<MethodTrack *>(t);
  893. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), Variant());
  894. Dictionary d;
  895. d["method"] = mt->methods[p_key_idx].method;
  896. d["args"] = mt->methods[p_key_idx].params;
  897. return d;
  898. } break;
  899. case TYPE_BEZIER: {
  900. BezierTrack *bt = static_cast<BezierTrack *>(t);
  901. ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), Variant());
  902. Array arr;
  903. arr.resize(5);
  904. arr[0] = bt->values[p_key_idx].value.value;
  905. arr[1] = bt->values[p_key_idx].value.in_handle.x;
  906. arr[2] = bt->values[p_key_idx].value.in_handle.y;
  907. arr[3] = bt->values[p_key_idx].value.out_handle.x;
  908. arr[4] = bt->values[p_key_idx].value.out_handle.y;
  909. return arr;
  910. } break;
  911. case TYPE_AUDIO: {
  912. AudioTrack *at = static_cast<AudioTrack *>(t);
  913. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
  914. Dictionary k;
  915. k["start_offset"] = at->values[p_key_idx].value.start_offset;
  916. k["end_offset"] = at->values[p_key_idx].value.end_offset;
  917. k["stream"] = at->values[p_key_idx].value.stream;
  918. return k;
  919. } break;
  920. case TYPE_ANIMATION: {
  921. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  922. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
  923. return at->values[p_key_idx].value;
  924. } break;
  925. }
  926. ERR_FAIL_V(Variant());
  927. }
  928. float Animation::track_get_key_time(int p_track, int p_key_idx) const {
  929. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  930. Track *t = tracks[p_track];
  931. switch (t->type) {
  932. case TYPE_TRANSFORM: {
  933. TransformTrack *tt = static_cast<TransformTrack *>(t);
  934. ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1);
  935. return tt->transforms[p_key_idx].time;
  936. } break;
  937. case TYPE_VALUE: {
  938. ValueTrack *vt = static_cast<ValueTrack *>(t);
  939. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
  940. return vt->values[p_key_idx].time;
  941. } break;
  942. case TYPE_METHOD: {
  943. MethodTrack *mt = static_cast<MethodTrack *>(t);
  944. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
  945. return mt->methods[p_key_idx].time;
  946. } break;
  947. case TYPE_BEZIER: {
  948. BezierTrack *bt = static_cast<BezierTrack *>(t);
  949. ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), -1);
  950. return bt->values[p_key_idx].time;
  951. } break;
  952. case TYPE_AUDIO: {
  953. AudioTrack *at = static_cast<AudioTrack *>(t);
  954. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
  955. return at->values[p_key_idx].time;
  956. } break;
  957. case TYPE_ANIMATION: {
  958. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  959. ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
  960. return at->values[p_key_idx].time;
  961. } break;
  962. }
  963. ERR_FAIL_V(-1);
  964. }
  965. void Animation::track_set_key_time(int p_track, int p_key_idx, float p_time) {
  966. ERR_FAIL_INDEX(p_track, tracks.size());
  967. Track *t = tracks[p_track];
  968. switch (t->type) {
  969. case TYPE_TRANSFORM: {
  970. TransformTrack *tt = static_cast<TransformTrack *>(t);
  971. ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
  972. TKey<TransformKey> key = tt->transforms[p_key_idx];
  973. key.time = p_time;
  974. tt->transforms.remove(p_key_idx);
  975. _insert(p_time, tt->transforms, key);
  976. return;
  977. }
  978. case TYPE_VALUE: {
  979. ValueTrack *vt = static_cast<ValueTrack *>(t);
  980. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  981. TKey<Variant> key = vt->values[p_key_idx];
  982. key.time = p_time;
  983. vt->values.remove(p_key_idx);
  984. _insert(p_time, vt->values, key);
  985. return;
  986. }
  987. case TYPE_METHOD: {
  988. MethodTrack *mt = static_cast<MethodTrack *>(t);
  989. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  990. MethodKey key = mt->methods[p_key_idx];
  991. key.time = p_time;
  992. mt->methods.remove(p_key_idx);
  993. _insert(p_time, mt->methods, key);
  994. return;
  995. }
  996. case TYPE_BEZIER: {
  997. BezierTrack *bt = static_cast<BezierTrack *>(t);
  998. ERR_FAIL_INDEX(p_key_idx, bt->values.size());
  999. TKey<BezierKey> key = bt->values[p_key_idx];
  1000. key.time = p_time;
  1001. bt->values.remove(p_key_idx);
  1002. _insert(p_time, bt->values, key);
  1003. return;
  1004. }
  1005. case TYPE_AUDIO: {
  1006. AudioTrack *at = static_cast<AudioTrack *>(t);
  1007. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1008. TKey<AudioKey> key = at->values[p_key_idx];
  1009. key.time = p_time;
  1010. at->values.remove(p_key_idx);
  1011. _insert(p_time, at->values, key);
  1012. return;
  1013. }
  1014. case TYPE_ANIMATION: {
  1015. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1016. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1017. TKey<StringName> key = at->values[p_key_idx];
  1018. key.time = p_time;
  1019. at->values.remove(p_key_idx);
  1020. _insert(p_time, at->values, key);
  1021. return;
  1022. }
  1023. }
  1024. ERR_FAIL();
  1025. }
  1026. float Animation::track_get_key_transition(int p_track, int p_key_idx) const {
  1027. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1028. Track *t = tracks[p_track];
  1029. switch (t->type) {
  1030. case TYPE_TRANSFORM: {
  1031. TransformTrack *tt = static_cast<TransformTrack *>(t);
  1032. ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1);
  1033. return tt->transforms[p_key_idx].transition;
  1034. } break;
  1035. case TYPE_VALUE: {
  1036. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1037. ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
  1038. return vt->values[p_key_idx].transition;
  1039. } break;
  1040. case TYPE_METHOD: {
  1041. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1042. ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
  1043. return mt->methods[p_key_idx].transition;
  1044. } break;
  1045. case TYPE_BEZIER: {
  1046. return 1; //bezier does not really use transitions
  1047. } break;
  1048. case TYPE_AUDIO: {
  1049. return 1; //audio does not really use transitions
  1050. } break;
  1051. case TYPE_ANIMATION: {
  1052. return 1; //animation does not really use transitions
  1053. } break;
  1054. }
  1055. ERR_FAIL_V(0);
  1056. }
  1057. void Animation::track_set_key_value(int p_track, int p_key_idx, const Variant &p_value) {
  1058. ERR_FAIL_INDEX(p_track, tracks.size());
  1059. Track *t = tracks[p_track];
  1060. switch (t->type) {
  1061. case TYPE_TRANSFORM: {
  1062. TransformTrack *tt = static_cast<TransformTrack *>(t);
  1063. ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
  1064. Dictionary d = p_value;
  1065. if (d.has("location")) {
  1066. tt->transforms.write[p_key_idx].value.loc = d["location"];
  1067. }
  1068. if (d.has("rotation")) {
  1069. tt->transforms.write[p_key_idx].value.rot = d["rotation"];
  1070. }
  1071. if (d.has("scale")) {
  1072. tt->transforms.write[p_key_idx].value.scale = d["scale"];
  1073. }
  1074. } break;
  1075. case TYPE_VALUE: {
  1076. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1077. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  1078. vt->values.write[p_key_idx].value = p_value;
  1079. } break;
  1080. case TYPE_METHOD: {
  1081. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1082. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  1083. Dictionary d = p_value;
  1084. if (d.has("method")) {
  1085. mt->methods.write[p_key_idx].method = d["method"];
  1086. }
  1087. if (d.has("args")) {
  1088. mt->methods.write[p_key_idx].params = d["args"];
  1089. }
  1090. } break;
  1091. case TYPE_BEZIER: {
  1092. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1093. ERR_FAIL_INDEX(p_key_idx, bt->values.size());
  1094. Array arr = p_value;
  1095. ERR_FAIL_COND(arr.size() != 5);
  1096. bt->values.write[p_key_idx].value.value = arr[0];
  1097. bt->values.write[p_key_idx].value.in_handle.x = arr[1];
  1098. bt->values.write[p_key_idx].value.in_handle.y = arr[2];
  1099. bt->values.write[p_key_idx].value.out_handle.x = arr[3];
  1100. bt->values.write[p_key_idx].value.out_handle.y = arr[4];
  1101. } break;
  1102. case TYPE_AUDIO: {
  1103. AudioTrack *at = static_cast<AudioTrack *>(t);
  1104. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1105. Dictionary k = p_value;
  1106. ERR_FAIL_COND(!k.has("start_offset"));
  1107. ERR_FAIL_COND(!k.has("end_offset"));
  1108. ERR_FAIL_COND(!k.has("stream"));
  1109. at->values.write[p_key_idx].value.start_offset = k["start_offset"];
  1110. at->values.write[p_key_idx].value.end_offset = k["end_offset"];
  1111. at->values.write[p_key_idx].value.stream = k["stream"];
  1112. } break;
  1113. case TYPE_ANIMATION: {
  1114. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1115. ERR_FAIL_INDEX(p_key_idx, at->values.size());
  1116. at->values.write[p_key_idx].value = p_value;
  1117. } break;
  1118. }
  1119. emit_changed();
  1120. }
  1121. void Animation::track_set_key_transition(int p_track, int p_key_idx, float p_transition) {
  1122. ERR_FAIL_INDEX(p_track, tracks.size());
  1123. Track *t = tracks[p_track];
  1124. switch (t->type) {
  1125. case TYPE_TRANSFORM: {
  1126. TransformTrack *tt = static_cast<TransformTrack *>(t);
  1127. ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
  1128. tt->transforms.write[p_key_idx].transition = p_transition;
  1129. } break;
  1130. case TYPE_VALUE: {
  1131. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1132. ERR_FAIL_INDEX(p_key_idx, vt->values.size());
  1133. vt->values.write[p_key_idx].transition = p_transition;
  1134. } break;
  1135. case TYPE_METHOD: {
  1136. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1137. ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
  1138. mt->methods.write[p_key_idx].transition = p_transition;
  1139. } break;
  1140. case TYPE_BEZIER:
  1141. case TYPE_AUDIO:
  1142. case TYPE_ANIMATION: {
  1143. // they don't use transition
  1144. } break;
  1145. }
  1146. emit_changed();
  1147. }
  1148. template <class K>
  1149. int Animation::_find(const Vector<K> &p_keys, float p_time) const {
  1150. int len = p_keys.size();
  1151. if (len == 0) {
  1152. return -2;
  1153. }
  1154. int low = 0;
  1155. int high = len - 1;
  1156. int middle = 0;
  1157. #ifdef DEBUG_ENABLED
  1158. if (low > high)
  1159. ERR_PRINT("low > high, this may be a bug");
  1160. #endif
  1161. const K *keys = &p_keys[0];
  1162. while (low <= high) {
  1163. middle = (low + high) / 2;
  1164. if (Math::is_equal_approx(p_time, keys[middle].time)) { //match
  1165. return middle;
  1166. } else if (p_time < keys[middle].time) {
  1167. high = middle - 1; //search low end of array
  1168. } else {
  1169. low = middle + 1; //search high end of array
  1170. }
  1171. }
  1172. if (keys[middle].time > p_time) {
  1173. middle--;
  1174. }
  1175. return middle;
  1176. }
  1177. Animation::TransformKey Animation::_interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, float p_c) const {
  1178. TransformKey ret;
  1179. ret.loc = _interpolate(p_a.loc, p_b.loc, p_c);
  1180. ret.rot = _interpolate(p_a.rot, p_b.rot, p_c);
  1181. ret.scale = _interpolate(p_a.scale, p_b.scale, p_c);
  1182. return ret;
  1183. }
  1184. Vector3 Animation::_interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const {
  1185. return p_a.linear_interpolate(p_b, p_c);
  1186. }
  1187. Quat Animation::_interpolate(const Quat &p_a, const Quat &p_b, float p_c) const {
  1188. return p_a.slerp(p_b, p_c);
  1189. }
  1190. Variant Animation::_interpolate(const Variant &p_a, const Variant &p_b, float p_c) const {
  1191. Variant dst;
  1192. Variant::interpolate(p_a, p_b, p_c, dst);
  1193. return dst;
  1194. }
  1195. float Animation::_interpolate(const float &p_a, const float &p_b, float p_c) const {
  1196. return p_a * (1.0 - p_c) + p_b * p_c;
  1197. }
  1198. Animation::TransformKey Animation::_cubic_interpolate(const Animation::TransformKey &p_pre_a, const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, const Animation::TransformKey &p_post_b, float p_c) const {
  1199. Animation::TransformKey tk;
  1200. tk.loc = p_a.loc.cubic_interpolate(p_b.loc, p_pre_a.loc, p_post_b.loc, p_c);
  1201. tk.scale = p_a.scale.cubic_interpolate(p_b.scale, p_pre_a.scale, p_post_b.scale, p_c);
  1202. tk.rot = p_a.rot.cubic_slerp(p_b.rot, p_pre_a.rot, p_post_b.rot, p_c);
  1203. return tk;
  1204. }
  1205. Vector3 Animation::_cubic_interpolate(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, float p_c) const {
  1206. return p_a.cubic_interpolate(p_b, p_pre_a, p_post_b, p_c);
  1207. }
  1208. Quat Animation::_cubic_interpolate(const Quat &p_pre_a, const Quat &p_a, const Quat &p_b, const Quat &p_post_b, float p_c) const {
  1209. return p_a.cubic_slerp(p_b, p_pre_a, p_post_b, p_c);
  1210. }
  1211. Variant Animation::_cubic_interpolate(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, float p_c) const {
  1212. Variant::Type type_a = p_a.get_type();
  1213. Variant::Type type_b = p_b.get_type();
  1214. Variant::Type type_pa = p_pre_a.get_type();
  1215. Variant::Type type_pb = p_post_b.get_type();
  1216. //make int and real play along
  1217. uint32_t vformat = 1 << type_a;
  1218. vformat |= 1 << type_b;
  1219. vformat |= 1 << type_pa;
  1220. vformat |= 1 << type_pb;
  1221. if (vformat == ((1 << Variant::INT) | (1 << Variant::REAL)) || vformat == (1 << Variant::REAL)) {
  1222. //mix of real and int
  1223. real_t p0 = p_pre_a;
  1224. real_t p1 = p_a;
  1225. real_t p2 = p_b;
  1226. real_t p3 = p_post_b;
  1227. float t = p_c;
  1228. float t2 = t * t;
  1229. float t3 = t2 * t;
  1230. return 0.5f *
  1231. ((p1 * 2.0f) +
  1232. (-p0 + p2) * t +
  1233. (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
  1234. (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
  1235. } else if ((vformat & (vformat - 1))) {
  1236. return p_a; //can't interpolate, mix of types
  1237. }
  1238. switch (type_a) {
  1239. case Variant::VECTOR2: {
  1240. Vector2 a = p_a;
  1241. Vector2 b = p_b;
  1242. Vector2 pa = p_pre_a;
  1243. Vector2 pb = p_post_b;
  1244. return a.cubic_interpolate(b, pa, pb, p_c);
  1245. }
  1246. case Variant::RECT2: {
  1247. Rect2 a = p_a;
  1248. Rect2 b = p_b;
  1249. Rect2 pa = p_pre_a;
  1250. Rect2 pb = p_post_b;
  1251. return Rect2(
  1252. a.position.cubic_interpolate(b.position, pa.position, pb.position, p_c),
  1253. a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c));
  1254. }
  1255. case Variant::VECTOR3: {
  1256. Vector3 a = p_a;
  1257. Vector3 b = p_b;
  1258. Vector3 pa = p_pre_a;
  1259. Vector3 pb = p_post_b;
  1260. return a.cubic_interpolate(b, pa, pb, p_c);
  1261. }
  1262. case Variant::QUAT: {
  1263. Quat a = p_a;
  1264. Quat b = p_b;
  1265. Quat pa = p_pre_a;
  1266. Quat pb = p_post_b;
  1267. return a.cubic_slerp(b, pa, pb, p_c);
  1268. }
  1269. case Variant::AABB: {
  1270. AABB a = p_a;
  1271. AABB b = p_b;
  1272. AABB pa = p_pre_a;
  1273. AABB pb = p_post_b;
  1274. return AABB(
  1275. a.position.cubic_interpolate(b.position, pa.position, pb.position, p_c),
  1276. a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c));
  1277. }
  1278. default: {
  1279. return _interpolate(p_a, p_b, p_c);
  1280. }
  1281. }
  1282. }
  1283. float Animation::_cubic_interpolate(const float &p_pre_a, const float &p_a, const float &p_b, const float &p_post_b, float p_c) const {
  1284. return _interpolate(p_a, p_b, p_c);
  1285. }
  1286. template <class T>
  1287. T Animation::_interpolate(const Vector<TKey<T>> &p_keys, float p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const {
  1288. int len = _find(p_keys, length) + 1; // try to find last key (there may be more past the end)
  1289. if (len <= 0) {
  1290. // (-1 or -2 returned originally) (plus one above)
  1291. // meaning no keys, or only key time is larger than length
  1292. if (p_ok) {
  1293. *p_ok = false;
  1294. }
  1295. return T();
  1296. } else if (len == 1) { // one key found (0+1), return it
  1297. if (p_ok) {
  1298. *p_ok = true;
  1299. }
  1300. return p_keys[0].value;
  1301. }
  1302. int idx = _find(p_keys, p_time);
  1303. ERR_FAIL_COND_V(idx == -2, T());
  1304. bool result = true;
  1305. int next = 0;
  1306. float c = 0;
  1307. // prepare for all cases of interpolation
  1308. if (loop && p_loop_wrap) {
  1309. // loop
  1310. if (idx >= 0) {
  1311. if ((idx + 1) < len) {
  1312. next = idx + 1;
  1313. float delta = p_keys[next].time - p_keys[idx].time;
  1314. float from = p_time - p_keys[idx].time;
  1315. if (Math::is_zero_approx(delta)) {
  1316. c = 0;
  1317. } else {
  1318. c = from / delta;
  1319. }
  1320. } else {
  1321. next = 0;
  1322. float delta = (length - p_keys[idx].time) + p_keys[next].time;
  1323. float from = p_time - p_keys[idx].time;
  1324. if (Math::is_zero_approx(delta)) {
  1325. c = 0;
  1326. } else {
  1327. c = from / delta;
  1328. }
  1329. }
  1330. } else {
  1331. // on loop, behind first key
  1332. idx = len - 1;
  1333. next = 0;
  1334. float endtime = (length - p_keys[idx].time);
  1335. if (endtime < 0) { // may be keys past the end
  1336. endtime = 0;
  1337. }
  1338. float delta = endtime + p_keys[next].time;
  1339. float from = endtime + p_time;
  1340. if (Math::is_zero_approx(delta)) {
  1341. c = 0;
  1342. } else {
  1343. c = from / delta;
  1344. }
  1345. }
  1346. } else { // no loop
  1347. if (idx >= 0) {
  1348. if ((idx + 1) < len) {
  1349. next = idx + 1;
  1350. float delta = p_keys[next].time - p_keys[idx].time;
  1351. float from = p_time - p_keys[idx].time;
  1352. if (Math::is_zero_approx(delta)) {
  1353. c = 0;
  1354. } else {
  1355. c = from / delta;
  1356. }
  1357. } else {
  1358. next = idx;
  1359. }
  1360. } else {
  1361. // only allow extending first key to anim start if looping
  1362. if (loop) {
  1363. idx = next = 0;
  1364. } else {
  1365. result = false;
  1366. }
  1367. }
  1368. }
  1369. if (p_ok) {
  1370. *p_ok = result;
  1371. }
  1372. if (!result) {
  1373. return T();
  1374. }
  1375. float tr = p_keys[idx].transition;
  1376. if (tr == 0 || idx == next) {
  1377. // don't interpolate if not needed
  1378. return p_keys[idx].value;
  1379. }
  1380. if (tr != 1.0) {
  1381. c = Math::ease(c, tr);
  1382. }
  1383. switch (p_interp) {
  1384. case INTERPOLATION_NEAREST: {
  1385. return p_keys[idx].value;
  1386. } break;
  1387. case INTERPOLATION_LINEAR: {
  1388. return _interpolate(p_keys[idx].value, p_keys[next].value, c);
  1389. } break;
  1390. case INTERPOLATION_CUBIC: {
  1391. int pre = idx - 1;
  1392. if (pre < 0) {
  1393. if (loop && p_loop_wrap) {
  1394. pre = len - 1;
  1395. } else {
  1396. pre = 0;
  1397. }
  1398. }
  1399. int post = next + 1;
  1400. if (post >= len) {
  1401. if (loop && p_loop_wrap) {
  1402. post = 0;
  1403. } else {
  1404. post = next;
  1405. }
  1406. }
  1407. return _cubic_interpolate(p_keys[pre].value, p_keys[idx].value, p_keys[next].value, p_keys[post].value, c);
  1408. } break;
  1409. default:
  1410. return p_keys[idx].value;
  1411. }
  1412. // do a barrel roll
  1413. }
  1414. Error Animation::transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const {
  1415. ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
  1416. Track *t = tracks[p_track];
  1417. ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, ERR_INVALID_PARAMETER);
  1418. TransformTrack *tt = static_cast<TransformTrack *>(t);
  1419. bool ok = false;
  1420. TransformKey tk = _interpolate(tt->transforms, p_time, tt->interpolation, tt->loop_wrap, &ok);
  1421. if (!ok) {
  1422. return ERR_UNAVAILABLE;
  1423. }
  1424. if (r_loc) {
  1425. *r_loc = tk.loc;
  1426. }
  1427. if (r_rot) {
  1428. *r_rot = tk.rot;
  1429. }
  1430. if (r_scale) {
  1431. *r_scale = tk.scale;
  1432. }
  1433. return OK;
  1434. }
  1435. Variant Animation::value_track_interpolate(int p_track, float p_time) const {
  1436. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  1437. Track *t = tracks[p_track];
  1438. ERR_FAIL_COND_V(t->type != TYPE_VALUE, Variant());
  1439. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1440. bool ok = false;
  1441. Variant res = _interpolate(vt->values, p_time, (vt->update_mode == UPDATE_CONTINUOUS || vt->update_mode == UPDATE_CAPTURE) ? vt->interpolation : INTERPOLATION_NEAREST, vt->loop_wrap, &ok);
  1442. if (ok) {
  1443. return res;
  1444. }
  1445. return Variant();
  1446. }
  1447. void Animation::_value_track_get_key_indices_in_range(const ValueTrack *vt, float from_time, float to_time, List<int> *p_indices) const {
  1448. if (from_time != length && to_time == length) {
  1449. to_time = length * 1.001; //include a little more if at the end
  1450. }
  1451. int to = _find(vt->values, to_time);
  1452. if (to >= 0 && from_time == to_time && vt->values[to].time == from_time) {
  1453. //find exact (0 delta), return if found
  1454. p_indices->push_back(to);
  1455. return;
  1456. }
  1457. // can't really send the events == time, will be sent in the next frame.
  1458. // if event>=len then it will probably never be requested by the anim player.
  1459. if (to >= 0 && vt->values[to].time >= to_time) {
  1460. to--;
  1461. }
  1462. if (to < 0) {
  1463. return; // not bother
  1464. }
  1465. int from = _find(vt->values, from_time);
  1466. // position in the right first event.+
  1467. if (from < 0 || vt->values[from].time < from_time) {
  1468. from++;
  1469. }
  1470. int max = vt->values.size();
  1471. for (int i = from; i <= to; i++) {
  1472. ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
  1473. p_indices->push_back(i);
  1474. }
  1475. }
  1476. void Animation::value_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
  1477. ERR_FAIL_INDEX(p_track, tracks.size());
  1478. Track *t = tracks[p_track];
  1479. ERR_FAIL_COND(t->type != TYPE_VALUE);
  1480. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1481. float from_time = p_time - p_delta;
  1482. float to_time = p_time;
  1483. if (from_time > to_time) {
  1484. SWAP(from_time, to_time);
  1485. }
  1486. if (loop) {
  1487. from_time = Math::fposmod(from_time, length);
  1488. to_time = Math::fposmod(to_time, length);
  1489. if (from_time > to_time) {
  1490. // handle loop by splitting
  1491. _value_track_get_key_indices_in_range(vt, from_time, length, p_indices);
  1492. _value_track_get_key_indices_in_range(vt, 0, to_time, p_indices);
  1493. return;
  1494. }
  1495. } else {
  1496. if (from_time < 0) {
  1497. from_time = 0;
  1498. }
  1499. if (from_time > length) {
  1500. from_time = length;
  1501. }
  1502. if (to_time < 0) {
  1503. to_time = 0;
  1504. }
  1505. if (to_time > length) {
  1506. to_time = length;
  1507. }
  1508. }
  1509. _value_track_get_key_indices_in_range(vt, from_time, to_time, p_indices);
  1510. }
  1511. void Animation::value_track_set_update_mode(int p_track, UpdateMode p_mode) {
  1512. ERR_FAIL_INDEX(p_track, tracks.size());
  1513. Track *t = tracks[p_track];
  1514. ERR_FAIL_COND(t->type != TYPE_VALUE);
  1515. ERR_FAIL_INDEX((int)p_mode, 4);
  1516. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1517. vt->update_mode = p_mode;
  1518. emit_changed();
  1519. }
  1520. Animation::UpdateMode Animation::value_track_get_update_mode(int p_track) const {
  1521. ERR_FAIL_INDEX_V(p_track, tracks.size(), UPDATE_CONTINUOUS);
  1522. Track *t = tracks[p_track];
  1523. ERR_FAIL_COND_V(t->type != TYPE_VALUE, UPDATE_CONTINUOUS);
  1524. ValueTrack *vt = static_cast<ValueTrack *>(t);
  1525. return vt->update_mode;
  1526. }
  1527. template <class T>
  1528. void Animation::_track_get_key_indices_in_range(const Vector<T> &p_array, float from_time, float to_time, List<int> *p_indices) const {
  1529. if (from_time != length && to_time == length) {
  1530. to_time = length * 1.01; //include a little more if at the end
  1531. }
  1532. int to = _find(p_array, to_time);
  1533. // can't really send the events == time, will be sent in the next frame.
  1534. // if event>=len then it will probably never be requested by the anim player.
  1535. if (to >= 0 && p_array[to].time >= to_time) {
  1536. to--;
  1537. }
  1538. if (to < 0) {
  1539. return; // not bother
  1540. }
  1541. int from = _find(p_array, from_time);
  1542. // position in the right first event.+
  1543. if (from < 0 || p_array[from].time < from_time) {
  1544. from++;
  1545. }
  1546. int max = p_array.size();
  1547. for (int i = from; i <= to; i++) {
  1548. ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
  1549. p_indices->push_back(i);
  1550. }
  1551. }
  1552. void Animation::track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
  1553. ERR_FAIL_INDEX(p_track, tracks.size());
  1554. const Track *t = tracks[p_track];
  1555. float from_time = p_time - p_delta;
  1556. float to_time = p_time;
  1557. if (from_time > to_time) {
  1558. SWAP(from_time, to_time);
  1559. }
  1560. if (loop) {
  1561. if (from_time > length || from_time < 0) {
  1562. from_time = Math::fposmod(from_time, length);
  1563. }
  1564. if (to_time > length || to_time < 0) {
  1565. to_time = Math::fposmod(to_time, length);
  1566. }
  1567. if (from_time > to_time) {
  1568. // handle loop by splitting
  1569. switch (t->type) {
  1570. case TYPE_TRANSFORM: {
  1571. const TransformTrack *tt = static_cast<const TransformTrack *>(t);
  1572. _track_get_key_indices_in_range(tt->transforms, from_time, length, p_indices);
  1573. _track_get_key_indices_in_range(tt->transforms, 0, to_time, p_indices);
  1574. } break;
  1575. case TYPE_VALUE: {
  1576. const ValueTrack *vt = static_cast<const ValueTrack *>(t);
  1577. _track_get_key_indices_in_range(vt->values, from_time, length, p_indices);
  1578. _track_get_key_indices_in_range(vt->values, 0, to_time, p_indices);
  1579. } break;
  1580. case TYPE_METHOD: {
  1581. const MethodTrack *mt = static_cast<const MethodTrack *>(t);
  1582. _track_get_key_indices_in_range(mt->methods, from_time, length, p_indices);
  1583. _track_get_key_indices_in_range(mt->methods, 0, to_time, p_indices);
  1584. } break;
  1585. case TYPE_BEZIER: {
  1586. const BezierTrack *bz = static_cast<const BezierTrack *>(t);
  1587. _track_get_key_indices_in_range(bz->values, from_time, length, p_indices);
  1588. _track_get_key_indices_in_range(bz->values, 0, to_time, p_indices);
  1589. } break;
  1590. case TYPE_AUDIO: {
  1591. const AudioTrack *ad = static_cast<const AudioTrack *>(t);
  1592. _track_get_key_indices_in_range(ad->values, from_time, length, p_indices);
  1593. _track_get_key_indices_in_range(ad->values, 0, to_time, p_indices);
  1594. } break;
  1595. case TYPE_ANIMATION: {
  1596. const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
  1597. _track_get_key_indices_in_range(an->values, from_time, length, p_indices);
  1598. _track_get_key_indices_in_range(an->values, 0, to_time, p_indices);
  1599. } break;
  1600. }
  1601. return;
  1602. }
  1603. } else {
  1604. if (from_time < 0) {
  1605. from_time = 0;
  1606. }
  1607. if (from_time > length) {
  1608. from_time = length;
  1609. }
  1610. if (to_time < 0) {
  1611. to_time = 0;
  1612. }
  1613. if (to_time > length) {
  1614. to_time = length;
  1615. }
  1616. }
  1617. switch (t->type) {
  1618. case TYPE_TRANSFORM: {
  1619. const TransformTrack *tt = static_cast<const TransformTrack *>(t);
  1620. _track_get_key_indices_in_range(tt->transforms, from_time, to_time, p_indices);
  1621. } break;
  1622. case TYPE_VALUE: {
  1623. const ValueTrack *vt = static_cast<const ValueTrack *>(t);
  1624. _track_get_key_indices_in_range(vt->values, from_time, to_time, p_indices);
  1625. } break;
  1626. case TYPE_METHOD: {
  1627. const MethodTrack *mt = static_cast<const MethodTrack *>(t);
  1628. _track_get_key_indices_in_range(mt->methods, from_time, to_time, p_indices);
  1629. } break;
  1630. case TYPE_BEZIER: {
  1631. const BezierTrack *bz = static_cast<const BezierTrack *>(t);
  1632. _track_get_key_indices_in_range(bz->values, from_time, to_time, p_indices);
  1633. } break;
  1634. case TYPE_AUDIO: {
  1635. const AudioTrack *ad = static_cast<const AudioTrack *>(t);
  1636. _track_get_key_indices_in_range(ad->values, from_time, to_time, p_indices);
  1637. } break;
  1638. case TYPE_ANIMATION: {
  1639. const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
  1640. _track_get_key_indices_in_range(an->values, from_time, to_time, p_indices);
  1641. } break;
  1642. }
  1643. }
  1644. void Animation::_method_track_get_key_indices_in_range(const MethodTrack *mt, float from_time, float to_time, List<int> *p_indices) const {
  1645. if (from_time != length && to_time == length) {
  1646. to_time = length * 1.01; //include a little more if at the end
  1647. }
  1648. int to = _find(mt->methods, to_time);
  1649. // can't really send the events == time, will be sent in the next frame.
  1650. // if event>=len then it will probably never be requested by the anim player.
  1651. if (to >= 0 && mt->methods[to].time >= to_time) {
  1652. to--;
  1653. }
  1654. if (to < 0) {
  1655. return; // not bother
  1656. }
  1657. int from = _find(mt->methods, from_time);
  1658. // position in the right first event.+
  1659. if (from < 0 || mt->methods[from].time < from_time) {
  1660. from++;
  1661. }
  1662. int max = mt->methods.size();
  1663. for (int i = from; i <= to; i++) {
  1664. ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
  1665. p_indices->push_back(i);
  1666. }
  1667. }
  1668. void Animation::method_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
  1669. ERR_FAIL_INDEX(p_track, tracks.size());
  1670. Track *t = tracks[p_track];
  1671. ERR_FAIL_COND(t->type != TYPE_METHOD);
  1672. MethodTrack *mt = static_cast<MethodTrack *>(t);
  1673. float from_time = p_time - p_delta;
  1674. float to_time = p_time;
  1675. if (from_time > to_time) {
  1676. SWAP(from_time, to_time);
  1677. }
  1678. if (loop) {
  1679. if (from_time > length || from_time < 0) {
  1680. from_time = Math::fposmod(from_time, length);
  1681. }
  1682. if (to_time > length || to_time < 0) {
  1683. to_time = Math::fposmod(to_time, length);
  1684. }
  1685. if (from_time > to_time) {
  1686. // handle loop by splitting
  1687. _method_track_get_key_indices_in_range(mt, from_time, length, p_indices);
  1688. _method_track_get_key_indices_in_range(mt, 0, to_time, p_indices);
  1689. return;
  1690. }
  1691. } else {
  1692. if (from_time < 0) {
  1693. from_time = 0;
  1694. }
  1695. if (from_time > length) {
  1696. from_time = length;
  1697. }
  1698. if (to_time < 0) {
  1699. to_time = 0;
  1700. }
  1701. if (to_time > length) {
  1702. to_time = length;
  1703. }
  1704. }
  1705. _method_track_get_key_indices_in_range(mt, from_time, to_time, p_indices);
  1706. }
  1707. Vector<Variant> Animation::method_track_get_params(int p_track, int p_key_idx) const {
  1708. ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector<Variant>());
  1709. Track *t = tracks[p_track];
  1710. ERR_FAIL_COND_V(t->type != TYPE_METHOD, Vector<Variant>());
  1711. MethodTrack *pm = static_cast<MethodTrack *>(t);
  1712. ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), Vector<Variant>());
  1713. const MethodKey &mk = pm->methods[p_key_idx];
  1714. return mk.params;
  1715. }
  1716. StringName Animation::method_track_get_name(int p_track, int p_key_idx) const {
  1717. ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
  1718. Track *t = tracks[p_track];
  1719. ERR_FAIL_COND_V(t->type != TYPE_METHOD, StringName());
  1720. MethodTrack *pm = static_cast<MethodTrack *>(t);
  1721. ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), StringName());
  1722. return pm->methods[p_key_idx].method;
  1723. }
  1724. int Animation::bezier_track_insert_key(int p_track, float p_time, float p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle) {
  1725. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1726. Track *t = tracks[p_track];
  1727. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, -1);
  1728. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1729. TKey<BezierKey> k;
  1730. k.time = p_time;
  1731. k.value.value = p_value;
  1732. k.value.in_handle = p_in_handle;
  1733. if (k.value.in_handle.x > 0) {
  1734. k.value.in_handle.x = 0;
  1735. }
  1736. k.value.out_handle = p_out_handle;
  1737. if (k.value.out_handle.x < 0) {
  1738. k.value.out_handle.x = 0;
  1739. }
  1740. int key = _insert(p_time, bt->values, k);
  1741. emit_changed();
  1742. return key;
  1743. }
  1744. void Animation::bezier_track_set_key_value(int p_track, int p_index, float p_value) {
  1745. ERR_FAIL_INDEX(p_track, tracks.size());
  1746. Track *t = tracks[p_track];
  1747. ERR_FAIL_COND(t->type != TYPE_BEZIER);
  1748. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1749. ERR_FAIL_INDEX(p_index, bt->values.size());
  1750. bt->values.write[p_index].value.value = p_value;
  1751. emit_changed();
  1752. }
  1753. void Animation::bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle) {
  1754. ERR_FAIL_INDEX(p_track, tracks.size());
  1755. Track *t = tracks[p_track];
  1756. ERR_FAIL_COND(t->type != TYPE_BEZIER);
  1757. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1758. ERR_FAIL_INDEX(p_index, bt->values.size());
  1759. bt->values.write[p_index].value.in_handle = p_handle;
  1760. if (bt->values[p_index].value.in_handle.x > 0) {
  1761. bt->values.write[p_index].value.in_handle.x = 0;
  1762. }
  1763. emit_changed();
  1764. }
  1765. void Animation::bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle) {
  1766. ERR_FAIL_INDEX(p_track, tracks.size());
  1767. Track *t = tracks[p_track];
  1768. ERR_FAIL_COND(t->type != TYPE_BEZIER);
  1769. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1770. ERR_FAIL_INDEX(p_index, bt->values.size());
  1771. bt->values.write[p_index].value.out_handle = p_handle;
  1772. if (bt->values[p_index].value.out_handle.x < 0) {
  1773. bt->values.write[p_index].value.out_handle.x = 0;
  1774. }
  1775. emit_changed();
  1776. }
  1777. float Animation::bezier_track_get_key_value(int p_track, int p_index) const {
  1778. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  1779. Track *t = tracks[p_track];
  1780. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, 0);
  1781. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1782. ERR_FAIL_INDEX_V(p_index, bt->values.size(), 0);
  1783. return bt->values[p_index].value.value;
  1784. }
  1785. Vector2 Animation::bezier_track_get_key_in_handle(int p_track, int p_index) const {
  1786. ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
  1787. Track *t = tracks[p_track];
  1788. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
  1789. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1790. ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
  1791. return bt->values[p_index].value.in_handle;
  1792. }
  1793. Vector2 Animation::bezier_track_get_key_out_handle(int p_track, int p_index) const {
  1794. ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
  1795. Track *t = tracks[p_track];
  1796. ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
  1797. BezierTrack *bt = static_cast<BezierTrack *>(t);
  1798. ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
  1799. return bt->values[p_index].value.out_handle;
  1800. }
  1801. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, const Vector2 &start, const Vector2 &control_1, const Vector2 &control_2, const Vector2 &end) {
  1802. /* Formula from Wikipedia article on Bezier curves. */
  1803. real_t omt = (1.0 - t);
  1804. real_t omt2 = omt * omt;
  1805. real_t omt3 = omt2 * omt;
  1806. real_t t2 = t * t;
  1807. real_t t3 = t2 * t;
  1808. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  1809. }
  1810. float Animation::bezier_track_interpolate(int p_track, float p_time) const {
  1811. //this uses a different interpolation scheme
  1812. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  1813. Track *track = tracks[p_track];
  1814. ERR_FAIL_COND_V(track->type != TYPE_BEZIER, 0);
  1815. BezierTrack *bt = static_cast<BezierTrack *>(track);
  1816. int len = _find(bt->values, length) + 1; // try to find last key (there may be more past the end)
  1817. if (len <= 0) {
  1818. // (-1 or -2 returned originally) (plus one above)
  1819. return 0;
  1820. } else if (len == 1) { // one key found (0+1), return it
  1821. return bt->values[0].value.value;
  1822. }
  1823. int idx = _find(bt->values, p_time);
  1824. ERR_FAIL_COND_V(idx == -2, 0);
  1825. //there really is no looping interpolation on bezier
  1826. if (idx < 0) {
  1827. return bt->values[0].value.value;
  1828. }
  1829. if (idx >= bt->values.size() - 1) {
  1830. return bt->values[bt->values.size() - 1].value.value;
  1831. }
  1832. float t = p_time - bt->values[idx].time;
  1833. int iterations = 10;
  1834. float duration = bt->values[idx + 1].time - bt->values[idx].time; // time duration between our two keyframes
  1835. float low = 0; // 0% of the current animation segment
  1836. float high = 1; // 100% of the current animation segment
  1837. float middle;
  1838. Vector2 start(0, bt->values[idx].value.value);
  1839. Vector2 start_out = start + bt->values[idx].value.out_handle;
  1840. Vector2 end(duration, bt->values[idx + 1].value.value);
  1841. Vector2 end_in = end + bt->values[idx + 1].value.in_handle;
  1842. //narrow high and low as much as possible
  1843. for (int i = 0; i < iterations; i++) {
  1844. middle = (low + high) / 2;
  1845. Vector2 interp = _bezier_interp(middle, start, start_out, end_in, end);
  1846. if (interp.x < t) {
  1847. low = middle;
  1848. } else {
  1849. high = middle;
  1850. }
  1851. }
  1852. //interpolate the result:
  1853. Vector2 low_pos = _bezier_interp(low, start, start_out, end_in, end);
  1854. Vector2 high_pos = _bezier_interp(high, start, start_out, end_in, end);
  1855. float c = (t - low_pos.x) / (high_pos.x - low_pos.x);
  1856. return low_pos.linear_interpolate(high_pos, c).y;
  1857. }
  1858. int Animation::audio_track_insert_key(int p_track, float p_time, const RES &p_stream, float p_start_offset, float p_end_offset) {
  1859. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1860. Track *t = tracks[p_track];
  1861. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, -1);
  1862. AudioTrack *at = static_cast<AudioTrack *>(t);
  1863. TKey<AudioKey> k;
  1864. k.time = p_time;
  1865. k.value.stream = p_stream;
  1866. k.value.start_offset = p_start_offset;
  1867. if (k.value.start_offset < 0) {
  1868. k.value.start_offset = 0;
  1869. }
  1870. k.value.end_offset = p_end_offset;
  1871. if (k.value.end_offset < 0) {
  1872. k.value.end_offset = 0;
  1873. }
  1874. int key = _insert(p_time, at->values, k);
  1875. emit_changed();
  1876. return key;
  1877. }
  1878. void Animation::audio_track_set_key_stream(int p_track, int p_key, const RES &p_stream) {
  1879. ERR_FAIL_INDEX(p_track, tracks.size());
  1880. Track *t = tracks[p_track];
  1881. ERR_FAIL_COND(t->type != TYPE_AUDIO);
  1882. AudioTrack *at = static_cast<AudioTrack *>(t);
  1883. ERR_FAIL_INDEX(p_key, at->values.size());
  1884. at->values.write[p_key].value.stream = p_stream;
  1885. emit_changed();
  1886. }
  1887. void Animation::audio_track_set_key_start_offset(int p_track, int p_key, float p_offset) {
  1888. ERR_FAIL_INDEX(p_track, tracks.size());
  1889. Track *t = tracks[p_track];
  1890. ERR_FAIL_COND(t->type != TYPE_AUDIO);
  1891. AudioTrack *at = static_cast<AudioTrack *>(t);
  1892. ERR_FAIL_INDEX(p_key, at->values.size());
  1893. if (p_offset < 0) {
  1894. p_offset = 0;
  1895. }
  1896. at->values.write[p_key].value.start_offset = p_offset;
  1897. emit_changed();
  1898. }
  1899. void Animation::audio_track_set_key_end_offset(int p_track, int p_key, float p_offset) {
  1900. ERR_FAIL_INDEX(p_track, tracks.size());
  1901. Track *t = tracks[p_track];
  1902. ERR_FAIL_COND(t->type != TYPE_AUDIO);
  1903. AudioTrack *at = static_cast<AudioTrack *>(t);
  1904. ERR_FAIL_INDEX(p_key, at->values.size());
  1905. if (p_offset < 0) {
  1906. p_offset = 0;
  1907. }
  1908. at->values.write[p_key].value.end_offset = p_offset;
  1909. emit_changed();
  1910. }
  1911. RES Animation::audio_track_get_key_stream(int p_track, int p_key) const {
  1912. ERR_FAIL_INDEX_V(p_track, tracks.size(), RES());
  1913. const Track *t = tracks[p_track];
  1914. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, RES());
  1915. const AudioTrack *at = static_cast<const AudioTrack *>(t);
  1916. ERR_FAIL_INDEX_V(p_key, at->values.size(), RES());
  1917. return at->values[p_key].value.stream;
  1918. }
  1919. float Animation::audio_track_get_key_start_offset(int p_track, int p_key) const {
  1920. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  1921. const Track *t = tracks[p_track];
  1922. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
  1923. const AudioTrack *at = static_cast<const AudioTrack *>(t);
  1924. ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
  1925. return at->values[p_key].value.start_offset;
  1926. }
  1927. float Animation::audio_track_get_key_end_offset(int p_track, int p_key) const {
  1928. ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
  1929. const Track *t = tracks[p_track];
  1930. ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
  1931. const AudioTrack *at = static_cast<const AudioTrack *>(t);
  1932. ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
  1933. return at->values[p_key].value.end_offset;
  1934. }
  1935. //
  1936. int Animation::animation_track_insert_key(int p_track, float p_time, const StringName &p_animation) {
  1937. ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
  1938. Track *t = tracks[p_track];
  1939. ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, -1);
  1940. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1941. TKey<StringName> k;
  1942. k.time = p_time;
  1943. k.value = p_animation;
  1944. int key = _insert(p_time, at->values, k);
  1945. emit_changed();
  1946. return key;
  1947. }
  1948. void Animation::animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation) {
  1949. ERR_FAIL_INDEX(p_track, tracks.size());
  1950. Track *t = tracks[p_track];
  1951. ERR_FAIL_COND(t->type != TYPE_ANIMATION);
  1952. AnimationTrack *at = static_cast<AnimationTrack *>(t);
  1953. ERR_FAIL_INDEX(p_key, at->values.size());
  1954. at->values.write[p_key].value = p_animation;
  1955. emit_changed();
  1956. }
  1957. StringName Animation::animation_track_get_key_animation(int p_track, int p_key) const {
  1958. ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
  1959. const Track *t = tracks[p_track];
  1960. ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, StringName());
  1961. const AnimationTrack *at = static_cast<const AnimationTrack *>(t);
  1962. ERR_FAIL_INDEX_V(p_key, at->values.size(), StringName());
  1963. return at->values[p_key].value;
  1964. }
  1965. void Animation::set_length(float p_length) {
  1966. if (p_length < ANIM_MIN_LENGTH) {
  1967. p_length = ANIM_MIN_LENGTH;
  1968. }
  1969. length = p_length;
  1970. emit_changed();
  1971. }
  1972. float Animation::get_length() const {
  1973. return length;
  1974. }
  1975. void Animation::set_loop(bool p_enabled) {
  1976. loop = p_enabled;
  1977. emit_changed();
  1978. }
  1979. bool Animation::has_loop() const {
  1980. return loop;
  1981. }
  1982. void Animation::track_set_imported(int p_track, bool p_imported) {
  1983. ERR_FAIL_INDEX(p_track, tracks.size());
  1984. tracks[p_track]->imported = p_imported;
  1985. }
  1986. bool Animation::track_is_imported(int p_track) const {
  1987. ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
  1988. return tracks[p_track]->imported;
  1989. }
  1990. void Animation::track_set_enabled(int p_track, bool p_enabled) {
  1991. ERR_FAIL_INDEX(p_track, tracks.size());
  1992. tracks[p_track]->enabled = p_enabled;
  1993. emit_changed();
  1994. }
  1995. bool Animation::track_is_enabled(int p_track) const {
  1996. ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
  1997. return tracks[p_track]->enabled;
  1998. }
  1999. void Animation::track_move_up(int p_track) {
  2000. if (p_track >= 0 && p_track < (tracks.size() - 1)) {
  2001. SWAP(tracks.write[p_track], tracks.write[p_track + 1]);
  2002. }
  2003. emit_changed();
  2004. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  2005. }
  2006. void Animation::track_move_down(int p_track) {
  2007. if (p_track > 0 && p_track < tracks.size()) {
  2008. SWAP(tracks.write[p_track], tracks.write[p_track - 1]);
  2009. }
  2010. emit_changed();
  2011. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  2012. }
  2013. void Animation::track_move_to(int p_track, int p_to_index) {
  2014. ERR_FAIL_INDEX(p_track, tracks.size());
  2015. ERR_FAIL_INDEX(p_to_index, tracks.size() + 1);
  2016. if (p_track == p_to_index || p_track == p_to_index - 1) {
  2017. return;
  2018. }
  2019. Track *track = tracks.get(p_track);
  2020. tracks.remove(p_track);
  2021. // Take into account that the position of the tracks that come after the one removed will change.
  2022. tracks.insert(p_to_index > p_track ? p_to_index - 1 : p_to_index, track);
  2023. emit_changed();
  2024. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  2025. }
  2026. void Animation::track_swap(int p_track, int p_with_track) {
  2027. ERR_FAIL_INDEX(p_track, tracks.size());
  2028. ERR_FAIL_INDEX(p_with_track, tracks.size());
  2029. if (p_track == p_with_track) {
  2030. return;
  2031. }
  2032. SWAP(tracks.write[p_track], tracks.write[p_with_track]);
  2033. emit_changed();
  2034. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  2035. }
  2036. void Animation::set_step(float p_step) {
  2037. step = p_step;
  2038. emit_changed();
  2039. }
  2040. float Animation::get_step() const {
  2041. return step;
  2042. }
  2043. void Animation::copy_track(int p_track, Ref<Animation> p_to_animation) {
  2044. ERR_FAIL_COND(p_to_animation.is_null());
  2045. ERR_FAIL_INDEX(p_track, get_track_count());
  2046. int dst_track = p_to_animation->get_track_count();
  2047. p_to_animation->add_track(track_get_type(p_track));
  2048. p_to_animation->track_set_path(dst_track, track_get_path(p_track));
  2049. p_to_animation->track_set_imported(dst_track, track_is_imported(p_track));
  2050. p_to_animation->track_set_enabled(dst_track, track_is_enabled(p_track));
  2051. p_to_animation->track_set_interpolation_type(dst_track, track_get_interpolation_type(p_track));
  2052. p_to_animation->track_set_interpolation_loop_wrap(dst_track, track_get_interpolation_loop_wrap(p_track));
  2053. if (track_get_type(p_track) == TYPE_VALUE) {
  2054. p_to_animation->value_track_set_update_mode(dst_track, value_track_get_update_mode(p_track));
  2055. }
  2056. for (int i = 0; i < track_get_key_count(p_track); i++) {
  2057. p_to_animation->track_insert_key(dst_track, track_get_key_time(p_track, i), track_get_key_value(p_track, i), track_get_key_transition(p_track, i));
  2058. }
  2059. }
  2060. void Animation::_bind_methods() {
  2061. ClassDB::bind_method(D_METHOD("add_track", "type", "at_position"), &Animation::add_track, DEFVAL(-1));
  2062. ClassDB::bind_method(D_METHOD("remove_track", "track_idx"), &Animation::remove_track);
  2063. ClassDB::bind_method(D_METHOD("get_track_count"), &Animation::get_track_count);
  2064. ClassDB::bind_method(D_METHOD("track_get_type", "track_idx"), &Animation::track_get_type);
  2065. ClassDB::bind_method(D_METHOD("track_get_path", "track_idx"), &Animation::track_get_path);
  2066. ClassDB::bind_method(D_METHOD("track_set_path", "track_idx", "path"), &Animation::track_set_path);
  2067. ClassDB::bind_method(D_METHOD("find_track", "path"), &Animation::find_track);
  2068. ClassDB::bind_method(D_METHOD("track_move_up", "track_idx"), &Animation::track_move_up);
  2069. ClassDB::bind_method(D_METHOD("track_move_down", "track_idx"), &Animation::track_move_down);
  2070. ClassDB::bind_method(D_METHOD("track_move_to", "track_idx", "to_idx"), &Animation::track_move_to);
  2071. ClassDB::bind_method(D_METHOD("track_swap", "track_idx", "with_idx"), &Animation::track_swap);
  2072. ClassDB::bind_method(D_METHOD("track_set_imported", "track_idx", "imported"), &Animation::track_set_imported);
  2073. ClassDB::bind_method(D_METHOD("track_is_imported", "track_idx"), &Animation::track_is_imported);
  2074. ClassDB::bind_method(D_METHOD("track_set_enabled", "track_idx", "enabled"), &Animation::track_set_enabled);
  2075. ClassDB::bind_method(D_METHOD("track_is_enabled", "track_idx"), &Animation::track_is_enabled);
  2076. ClassDB::bind_method(D_METHOD("transform_track_insert_key", "track_idx", "time", "location", "rotation", "scale"), &Animation::transform_track_insert_key);
  2077. ClassDB::bind_method(D_METHOD("track_insert_key", "track_idx", "time", "key", "transition"), &Animation::track_insert_key, DEFVAL(1));
  2078. ClassDB::bind_method(D_METHOD("track_remove_key", "track_idx", "key_idx"), &Animation::track_remove_key);
  2079. ClassDB::bind_method(D_METHOD("track_remove_key_at_position", "track_idx", "position"), &Animation::track_remove_key_at_position);
  2080. ClassDB::bind_method(D_METHOD("track_set_key_value", "track_idx", "key", "value"), &Animation::track_set_key_value);
  2081. ClassDB::bind_method(D_METHOD("track_set_key_transition", "track_idx", "key_idx", "transition"), &Animation::track_set_key_transition);
  2082. ClassDB::bind_method(D_METHOD("track_set_key_time", "track_idx", "key_idx", "time"), &Animation::track_set_key_time);
  2083. ClassDB::bind_method(D_METHOD("track_get_key_transition", "track_idx", "key_idx"), &Animation::track_get_key_transition);
  2084. ClassDB::bind_method(D_METHOD("track_get_key_count", "track_idx"), &Animation::track_get_key_count);
  2085. ClassDB::bind_method(D_METHOD("track_get_key_value", "track_idx", "key_idx"), &Animation::track_get_key_value);
  2086. ClassDB::bind_method(D_METHOD("track_get_key_time", "track_idx", "key_idx"), &Animation::track_get_key_time);
  2087. ClassDB::bind_method(D_METHOD("track_find_key", "track_idx", "time", "exact"), &Animation::track_find_key, DEFVAL(false));
  2088. ClassDB::bind_method(D_METHOD("track_set_interpolation_type", "track_idx", "interpolation"), &Animation::track_set_interpolation_type);
  2089. ClassDB::bind_method(D_METHOD("track_get_interpolation_type", "track_idx"), &Animation::track_get_interpolation_type);
  2090. ClassDB::bind_method(D_METHOD("track_set_interpolation_loop_wrap", "track_idx", "interpolation"), &Animation::track_set_interpolation_loop_wrap);
  2091. ClassDB::bind_method(D_METHOD("track_get_interpolation_loop_wrap", "track_idx"), &Animation::track_get_interpolation_loop_wrap);
  2092. ClassDB::bind_method(D_METHOD("transform_track_interpolate", "track_idx", "time_sec"), &Animation::_transform_track_interpolate);
  2093. ClassDB::bind_method(D_METHOD("value_track_set_update_mode", "track_idx", "mode"), &Animation::value_track_set_update_mode);
  2094. ClassDB::bind_method(D_METHOD("value_track_get_update_mode", "track_idx"), &Animation::value_track_get_update_mode);
  2095. ClassDB::bind_method(D_METHOD("value_track_get_key_indices", "track_idx", "time_sec", "delta"), &Animation::_value_track_get_key_indices);
  2096. ClassDB::bind_method(D_METHOD("value_track_interpolate", "track_idx", "time_sec"), &Animation::value_track_interpolate);
  2097. ClassDB::bind_method(D_METHOD("method_track_get_key_indices", "track_idx", "time_sec", "delta"), &Animation::_method_track_get_key_indices);
  2098. ClassDB::bind_method(D_METHOD("method_track_get_name", "track_idx", "key_idx"), &Animation::method_track_get_name);
  2099. ClassDB::bind_method(D_METHOD("method_track_get_params", "track_idx", "key_idx"), &Animation::method_track_get_params);
  2100. ClassDB::bind_method(D_METHOD("bezier_track_insert_key", "track_idx", "time", "value", "in_handle", "out_handle"), &Animation::bezier_track_insert_key, DEFVAL(Vector2()), DEFVAL(Vector2()));
  2101. ClassDB::bind_method(D_METHOD("bezier_track_set_key_value", "track_idx", "key_idx", "value"), &Animation::bezier_track_set_key_value);
  2102. ClassDB::bind_method(D_METHOD("bezier_track_set_key_in_handle", "track_idx", "key_idx", "in_handle"), &Animation::bezier_track_set_key_in_handle);
  2103. ClassDB::bind_method(D_METHOD("bezier_track_set_key_out_handle", "track_idx", "key_idx", "out_handle"), &Animation::bezier_track_set_key_out_handle);
  2104. ClassDB::bind_method(D_METHOD("bezier_track_get_key_value", "track_idx", "key_idx"), &Animation::bezier_track_get_key_value);
  2105. ClassDB::bind_method(D_METHOD("bezier_track_get_key_in_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_in_handle);
  2106. ClassDB::bind_method(D_METHOD("bezier_track_get_key_out_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_out_handle);
  2107. ClassDB::bind_method(D_METHOD("bezier_track_interpolate", "track_idx", "time"), &Animation::bezier_track_interpolate);
  2108. ClassDB::bind_method(D_METHOD("audio_track_insert_key", "track_idx", "time", "stream", "start_offset", "end_offset"), &Animation::audio_track_insert_key, DEFVAL(0), DEFVAL(0));
  2109. ClassDB::bind_method(D_METHOD("audio_track_set_key_stream", "track_idx", "key_idx", "stream"), &Animation::audio_track_set_key_stream);
  2110. ClassDB::bind_method(D_METHOD("audio_track_set_key_start_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_start_offset);
  2111. ClassDB::bind_method(D_METHOD("audio_track_set_key_end_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_end_offset);
  2112. ClassDB::bind_method(D_METHOD("audio_track_get_key_stream", "track_idx", "key_idx"), &Animation::audio_track_get_key_stream);
  2113. ClassDB::bind_method(D_METHOD("audio_track_get_key_start_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_start_offset);
  2114. ClassDB::bind_method(D_METHOD("audio_track_get_key_end_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_end_offset);
  2115. ClassDB::bind_method(D_METHOD("animation_track_insert_key", "track_idx", "time", "animation"), &Animation::animation_track_insert_key);
  2116. ClassDB::bind_method(D_METHOD("animation_track_set_key_animation", "track_idx", "key_idx", "animation"), &Animation::animation_track_set_key_animation);
  2117. ClassDB::bind_method(D_METHOD("animation_track_get_key_animation", "track_idx", "key_idx"), &Animation::animation_track_get_key_animation);
  2118. ClassDB::bind_method(D_METHOD("set_length", "time_sec"), &Animation::set_length);
  2119. ClassDB::bind_method(D_METHOD("get_length"), &Animation::get_length);
  2120. ClassDB::bind_method(D_METHOD("set_loop", "enabled"), &Animation::set_loop);
  2121. ClassDB::bind_method(D_METHOD("has_loop"), &Animation::has_loop);
  2122. ClassDB::bind_method(D_METHOD("set_step", "size_sec"), &Animation::set_step);
  2123. ClassDB::bind_method(D_METHOD("get_step"), &Animation::get_step);
  2124. ClassDB::bind_method(D_METHOD("clear"), &Animation::clear);
  2125. ClassDB::bind_method(D_METHOD("copy_track", "track_idx", "to_animation"), &Animation::copy_track);
  2126. ADD_PROPERTY(PropertyInfo(Variant::REAL, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001"), "set_length", "get_length");
  2127. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
  2128. ADD_PROPERTY(PropertyInfo(Variant::REAL, "step", PROPERTY_HINT_RANGE, "0,4096,0.001"), "set_step", "get_step");
  2129. ADD_SIGNAL(MethodInfo("tracks_changed"));
  2130. BIND_ENUM_CONSTANT(TYPE_VALUE);
  2131. BIND_ENUM_CONSTANT(TYPE_TRANSFORM);
  2132. BIND_ENUM_CONSTANT(TYPE_METHOD);
  2133. BIND_ENUM_CONSTANT(TYPE_BEZIER);
  2134. BIND_ENUM_CONSTANT(TYPE_AUDIO);
  2135. BIND_ENUM_CONSTANT(TYPE_ANIMATION);
  2136. BIND_ENUM_CONSTANT(INTERPOLATION_NEAREST);
  2137. BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR);
  2138. BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC);
  2139. BIND_ENUM_CONSTANT(UPDATE_CONTINUOUS);
  2140. BIND_ENUM_CONSTANT(UPDATE_DISCRETE);
  2141. BIND_ENUM_CONSTANT(UPDATE_TRIGGER);
  2142. BIND_ENUM_CONSTANT(UPDATE_CAPTURE);
  2143. }
  2144. void Animation::clear() {
  2145. for (int i = 0; i < tracks.size(); i++) {
  2146. memdelete(tracks[i]);
  2147. }
  2148. tracks.clear();
  2149. loop = false;
  2150. length = 1;
  2151. emit_changed();
  2152. emit_signal(SceneStringNames::get_singleton()->tracks_changed);
  2153. }
  2154. bool Animation::_transform_track_optimize_key(const TKey<TransformKey> &t0, const TKey<TransformKey> &t1, const TKey<TransformKey> &t2, float p_alowed_linear_err, float p_alowed_angular_err, float p_max_optimizable_angle, const Vector3 &p_norm) {
  2155. real_t c = (t1.time - t0.time) / (t2.time - t0.time);
  2156. real_t t[3] = { -1, -1, -1 };
  2157. { //translation
  2158. const Vector3 &v0 = t0.value.loc;
  2159. const Vector3 &v1 = t1.value.loc;
  2160. const Vector3 &v2 = t2.value.loc;
  2161. if (v0.is_equal_approx(v2)) {
  2162. //0 and 2 are close, let's see if 1 is close
  2163. if (!v0.is_equal_approx(v1)) {
  2164. //not close, not optimizable
  2165. return false;
  2166. }
  2167. } else {
  2168. Vector3 pd = (v2 - v0);
  2169. float d0 = pd.dot(v0);
  2170. float d1 = pd.dot(v1);
  2171. float d2 = pd.dot(v2);
  2172. if (d1 < d0 || d1 > d2) {
  2173. return false;
  2174. }
  2175. Vector3 s[2] = { v0, v2 };
  2176. real_t d = Geometry::get_closest_point_to_segment(v1, s).distance_to(v1);
  2177. if (d > pd.length() * p_alowed_linear_err) {
  2178. return false; //beyond allowed error for colinearity
  2179. }
  2180. if (p_norm != Vector3() && Math::acos(pd.normalized().dot(p_norm)) > p_alowed_angular_err) {
  2181. return false;
  2182. }
  2183. t[0] = (d1 - d0) / (d2 - d0);
  2184. }
  2185. }
  2186. { //rotation
  2187. const Quat &q0 = t0.value.rot;
  2188. const Quat &q1 = t1.value.rot;
  2189. const Quat &q2 = t2.value.rot;
  2190. //localize both to rotation from q0
  2191. if (q0.is_equal_approx(q2)) {
  2192. if (!q0.is_equal_approx(q1)) {
  2193. return false;
  2194. }
  2195. } else {
  2196. Quat r02 = (q0.inverse() * q2).normalized();
  2197. Quat r01 = (q0.inverse() * q1).normalized();
  2198. Vector3 v02, v01;
  2199. real_t a02, a01;
  2200. r02.get_axis_angle(v02, a02);
  2201. r01.get_axis_angle(v01, a01);
  2202. if (Math::abs(a02) > p_max_optimizable_angle) {
  2203. return false;
  2204. }
  2205. if (v01.dot(v02) < 0) {
  2206. //make sure both rotations go the same way to compare
  2207. v02 = -v02;
  2208. a02 = -a02;
  2209. }
  2210. real_t err_01 = Math::acos(v01.normalized().dot(v02.normalized())) / Math_PI;
  2211. if (err_01 > p_alowed_angular_err) {
  2212. //not rotating in the same axis
  2213. return false;
  2214. }
  2215. if (a01 * a02 < 0) {
  2216. //not rotating in the same direction
  2217. return false;
  2218. }
  2219. real_t tr = a01 / a02;
  2220. if (tr < 0 || tr > 1) {
  2221. return false; //rotating too much or too less
  2222. }
  2223. t[1] = tr;
  2224. }
  2225. }
  2226. { //scale
  2227. const Vector3 &v0 = t0.value.scale;
  2228. const Vector3 &v1 = t1.value.scale;
  2229. const Vector3 &v2 = t2.value.scale;
  2230. if (v0.is_equal_approx(v2)) {
  2231. //0 and 2 are close, let's see if 1 is close
  2232. if (!v0.is_equal_approx(v1)) {
  2233. //not close, not optimizable
  2234. return false;
  2235. }
  2236. } else {
  2237. Vector3 pd = (v2 - v0);
  2238. float d0 = pd.dot(v0);
  2239. float d1 = pd.dot(v1);
  2240. float d2 = pd.dot(v2);
  2241. if (d1 < d0 || d1 > d2) {
  2242. return false; //beyond segment range
  2243. }
  2244. Vector3 s[2] = { v0, v2 };
  2245. real_t d = Geometry::get_closest_point_to_segment(v1, s).distance_to(v1);
  2246. if (d > pd.length() * p_alowed_linear_err) {
  2247. return false; //beyond allowed error for colinearity
  2248. }
  2249. t[2] = (d1 - d0) / (d2 - d0);
  2250. }
  2251. }
  2252. bool erase = false;
  2253. if (t[0] == -1 && t[1] == -1 && t[2] == -1) {
  2254. erase = true;
  2255. } else {
  2256. erase = true;
  2257. real_t lt = -1;
  2258. for (int j = 0; j < 3; j++) {
  2259. //search for t on first, one must be it
  2260. if (t[j] != -1) {
  2261. lt = t[j]; //official t
  2262. //validate rest
  2263. for (int k = j + 1; k < 3; k++) {
  2264. if (t[k] == -1) {
  2265. continue;
  2266. }
  2267. if (Math::abs(lt - t[k]) > p_alowed_linear_err) {
  2268. erase = false;
  2269. break;
  2270. }
  2271. }
  2272. break;
  2273. }
  2274. }
  2275. ERR_FAIL_COND_V(lt == -1, false);
  2276. if (erase) {
  2277. if (Math::abs(lt - c) > p_alowed_linear_err) {
  2278. //todo, evaluate changing the transition if this fails?
  2279. //this could be done as a second pass and would be
  2280. //able to optimize more
  2281. erase = false;
  2282. }
  2283. }
  2284. }
  2285. return erase;
  2286. }
  2287. void Animation::_transform_track_optimize(int p_idx, float p_allowed_linear_err, float p_allowed_angular_err, float p_max_optimizable_angle) {
  2288. ERR_FAIL_INDEX(p_idx, tracks.size());
  2289. ERR_FAIL_COND(tracks[p_idx]->type != TYPE_TRANSFORM);
  2290. TransformTrack *tt = static_cast<TransformTrack *>(tracks[p_idx]);
  2291. bool prev_erased = false;
  2292. TKey<TransformKey> first_erased;
  2293. Vector3 norm;
  2294. for (int i = 1; i < tt->transforms.size() - 1; i++) {
  2295. TKey<TransformKey> &t0 = tt->transforms.write[i - 1];
  2296. TKey<TransformKey> &t1 = tt->transforms.write[i];
  2297. TKey<TransformKey> &t2 = tt->transforms.write[i + 1];
  2298. bool erase = _transform_track_optimize_key(t0, t1, t2, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle, norm);
  2299. if (erase && !prev_erased) {
  2300. norm = (t2.value.loc - t1.value.loc).normalized();
  2301. }
  2302. if (prev_erased && !_transform_track_optimize_key(t0, first_erased, t2, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle, norm)) {
  2303. //avoid error to go beyond first erased key
  2304. erase = false;
  2305. }
  2306. if (erase) {
  2307. if (!prev_erased) {
  2308. first_erased = t1;
  2309. prev_erased = true;
  2310. }
  2311. tt->transforms.remove(i);
  2312. i--;
  2313. } else {
  2314. prev_erased = false;
  2315. norm = Vector3();
  2316. }
  2317. }
  2318. }
  2319. void Animation::optimize(float p_allowed_linear_err, float p_allowed_angular_err, float p_max_optimizable_angle) {
  2320. for (int i = 0; i < tracks.size(); i++) {
  2321. if (tracks[i]->type == TYPE_TRANSFORM) {
  2322. _transform_track_optimize(i, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle);
  2323. }
  2324. }
  2325. }
  2326. Animation::Animation() {
  2327. step = 0.1;
  2328. loop = false;
  2329. length = 1;
  2330. }
  2331. Animation::~Animation() {
  2332. for (int i = 0; i < tracks.size(); i++) {
  2333. memdelete(tracks[i]);
  2334. }
  2335. }