animation.cpp 79 KB

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