animation.cpp 82 KB

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