animation.cpp 79 KB

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