animation.cpp 82 KB

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