animation.cpp 78 KB

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