12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101 |
- /*************************************************************************/
- /* animation.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "animation.h"
- #include "scene/scene_string_names.h"
- #include "core/math/geometry.h"
- #define ANIM_MIN_LENGTH 0.001
- bool Animation::_set(const StringName &p_name, const Variant &p_value) {
- String name = p_name;
- if (name.begins_with("tracks/")) {
- int track = name.get_slicec('/', 1).to_int();
- String what = name.get_slicec('/', 2);
- if (tracks.size() == track && what == "type") {
- String type = p_value;
- if (type == "transform") {
- add_track(TYPE_TRANSFORM);
- } else if (type == "value") {
- add_track(TYPE_VALUE);
- } else if (type == "method") {
- add_track(TYPE_METHOD);
- } else if (type == "bezier") {
- add_track(TYPE_BEZIER);
- } else if (type == "audio") {
- add_track(TYPE_AUDIO);
- } else if (type == "animation") {
- add_track(TYPE_ANIMATION);
- } else {
- return false;
- }
- return true;
- }
- ERR_FAIL_INDEX_V(track, tracks.size(), false);
- if (what == "path")
- track_set_path(track, p_value);
- else if (what == "interp")
- track_set_interpolation_type(track, InterpolationType(p_value.operator int()));
- else if (what == "loop_wrap")
- track_set_interpolation_loop_wrap(track, p_value);
- else if (what == "imported")
- track_set_imported(track, p_value);
- else if (what == "enabled")
- track_set_enabled(track, p_value);
- else if (what == "keys" || what == "key_values") {
- if (track_get_type(track) == TYPE_TRANSFORM) {
- TransformTrack *tt = static_cast<TransformTrack *>(tracks[track]);
- PoolVector<float> values = p_value;
- int vcount = values.size();
- ERR_FAIL_COND_V(vcount % 12, false); // should be multiple of 11
- PoolVector<float>::Read r = values.read();
- tt->transforms.resize(vcount / 12);
- for (int i = 0; i < (vcount / 12); i++) {
- TKey<TransformKey> &tk = tt->transforms.write[i];
- const float *ofs = &r[i * 12];
- tk.time = ofs[0];
- tk.transition = ofs[1];
- tk.value.loc.x = ofs[2];
- tk.value.loc.y = ofs[3];
- tk.value.loc.z = ofs[4];
- tk.value.rot.x = ofs[5];
- tk.value.rot.y = ofs[6];
- tk.value.rot.z = ofs[7];
- tk.value.rot.w = ofs[8];
- tk.value.scale.x = ofs[9];
- tk.value.scale.y = ofs[10];
- tk.value.scale.z = ofs[11];
- }
- } else if (track_get_type(track) == TYPE_VALUE) {
- ValueTrack *vt = static_cast<ValueTrack *>(tracks[track]);
- Dictionary d = p_value;
- ERR_FAIL_COND_V(!d.has("times"), false);
- ERR_FAIL_COND_V(!d.has("values"), false);
- if (d.has("cont")) {
- bool v = d["cont"];
- vt->update_mode = v ? UPDATE_CONTINUOUS : UPDATE_DISCRETE;
- }
- if (d.has("update")) {
- int um = d["update"];
- if (um < 0)
- um = 0;
- else if (um > 3)
- um = 3;
- vt->update_mode = UpdateMode(um);
- }
- PoolVector<float> times = d["times"];
- Array values = d["values"];
- ERR_FAIL_COND_V(times.size() != values.size(), false);
- if (times.size()) {
- int valcount = times.size();
- PoolVector<float>::Read rt = times.read();
- vt->values.resize(valcount);
- for (int i = 0; i < valcount; i++) {
- vt->values.write[i].time = rt[i];
- vt->values.write[i].value = values[i];
- }
- if (d.has("transitions")) {
- PoolVector<float> transitions = d["transitions"];
- ERR_FAIL_COND_V(transitions.size() != valcount, false);
- PoolVector<float>::Read rtr = transitions.read();
- for (int i = 0; i < valcount; i++) {
- vt->values.write[i].transition = rtr[i];
- }
- }
- }
- return true;
- } else if (track_get_type(track) == TYPE_METHOD) {
- while (track_get_key_count(track))
- track_remove_key(track, 0); //well shouldn't be set anyway
- Dictionary d = p_value;
- ERR_FAIL_COND_V(!d.has("times"), false);
- ERR_FAIL_COND_V(!d.has("values"), false);
- PoolVector<float> times = d["times"];
- Array values = d["values"];
- ERR_FAIL_COND_V(times.size() != values.size(), false);
- if (times.size()) {
- int valcount = times.size();
- PoolVector<float>::Read rt = times.read();
- for (int i = 0; i < valcount; i++) {
- track_insert_key(track, rt[i], values[i]);
- }
- if (d.has("transitions")) {
- PoolVector<float> transitions = d["transitions"];
- ERR_FAIL_COND_V(transitions.size() != valcount, false);
- PoolVector<float>::Read rtr = transitions.read();
- for (int i = 0; i < valcount; i++) {
- track_set_key_transition(track, i, rtr[i]);
- }
- }
- }
- } else if (track_get_type(track) == TYPE_BEZIER) {
- BezierTrack *bt = static_cast<BezierTrack *>(tracks[track]);
- Dictionary d = p_value;
- ERR_FAIL_COND_V(!d.has("times"), false);
- ERR_FAIL_COND_V(!d.has("points"), false);
- PoolVector<float> times = d["times"];
- PoolRealArray values = d["points"];
- ERR_FAIL_COND_V(times.size() * 5 != values.size(), false);
- if (times.size()) {
- int valcount = times.size();
- PoolVector<float>::Read rt = times.read();
- PoolVector<float>::Read rv = values.read();
- bt->values.resize(valcount);
- for (int i = 0; i < valcount; i++) {
- bt->values.write[i].time = rt[i];
- bt->values.write[i].transition = 0; //unused in bezier
- bt->values.write[i].value.value = rv[i * 5 + 0];
- bt->values.write[i].value.in_handle.x = rv[i * 5 + 1];
- bt->values.write[i].value.in_handle.y = rv[i * 5 + 2];
- bt->values.write[i].value.out_handle.x = rv[i * 5 + 3];
- bt->values.write[i].value.out_handle.y = rv[i * 5 + 4];
- }
- }
- return true;
- } else if (track_get_type(track) == TYPE_AUDIO) {
- AudioTrack *ad = static_cast<AudioTrack *>(tracks[track]);
- Dictionary d = p_value;
- ERR_FAIL_COND_V(!d.has("times"), false);
- ERR_FAIL_COND_V(!d.has("clips"), false);
- PoolVector<float> times = d["times"];
- Array clips = d["clips"];
- ERR_FAIL_COND_V(clips.size() != times.size(), false);
- if (times.size()) {
- int valcount = times.size();
- PoolVector<float>::Read rt = times.read();
- ad->values.clear();
- for (int i = 0; i < valcount; i++) {
- Dictionary d2 = clips[i];
- if (!d2.has("start_offset"))
- continue;
- if (!d2.has("end_offset"))
- continue;
- if (!d2.has("stream"))
- continue;
- TKey<AudioKey> ak;
- ak.time = rt[i];
- ak.value.start_offset = d2["start_offset"];
- ak.value.end_offset = d2["end_offset"];
- ak.value.stream = d2["stream"];
- ad->values.push_back(ak);
- }
- }
- return true;
- } else if (track_get_type(track) == TYPE_ANIMATION) {
- AnimationTrack *an = static_cast<AnimationTrack *>(tracks[track]);
- Dictionary d = p_value;
- ERR_FAIL_COND_V(!d.has("times"), false);
- ERR_FAIL_COND_V(!d.has("clips"), false);
- PoolVector<float> times = d["times"];
- PoolVector<String> clips = d["clips"];
- ERR_FAIL_COND_V(clips.size() != times.size(), false);
- if (times.size()) {
- int valcount = times.size();
- PoolVector<float>::Read rt = times.read();
- PoolVector<String>::Read rc = clips.read();
- an->values.resize(valcount);
- for (int i = 0; i < valcount; i++) {
- TKey<StringName> ak;
- ak.time = rt[i];
- ak.value = rc[i];
- an->values.write[i] = ak;
- }
- }
- return true;
- } else {
- return false;
- }
- } else
- return false;
- } else
- return false;
- return true;
- }
- bool Animation::_get(const StringName &p_name, Variant &r_ret) const {
- String name = p_name;
- if (name == "length")
- r_ret = length;
- else if (name == "loop")
- r_ret = loop;
- else if (name == "step")
- r_ret = step;
- else if (name.begins_with("tracks/")) {
- int track = name.get_slicec('/', 1).to_int();
- String what = name.get_slicec('/', 2);
- ERR_FAIL_INDEX_V(track, tracks.size(), false);
- if (what == "type") {
- switch (track_get_type(track)) {
- case TYPE_TRANSFORM: r_ret = "transform"; break;
- case TYPE_VALUE: r_ret = "value"; break;
- case TYPE_METHOD: r_ret = "method"; break;
- case TYPE_BEZIER: r_ret = "bezier"; break;
- case TYPE_AUDIO: r_ret = "audio"; break;
- case TYPE_ANIMATION: r_ret = "animation"; break;
- }
- return true;
- } else if (what == "path")
- r_ret = track_get_path(track);
- else if (what == "interp")
- r_ret = track_get_interpolation_type(track);
- else if (what == "loop_wrap")
- r_ret = track_get_interpolation_loop_wrap(track);
- else if (what == "imported")
- r_ret = track_is_imported(track);
- else if (what == "enabled")
- r_ret = track_is_enabled(track);
- else if (what == "keys") {
- if (track_get_type(track) == TYPE_TRANSFORM) {
- PoolVector<real_t> keys;
- int kk = track_get_key_count(track);
- keys.resize(kk * 12);
- PoolVector<real_t>::Write w = keys.write();
- int idx = 0;
- for (int i = 0; i < track_get_key_count(track); i++) {
- Vector3 loc;
- Quat rot;
- Vector3 scale;
- transform_track_get_key(track, i, &loc, &rot, &scale);
- w[idx++] = track_get_key_time(track, i);
- w[idx++] = track_get_key_transition(track, i);
- w[idx++] = loc.x;
- w[idx++] = loc.y;
- w[idx++] = loc.z;
- w[idx++] = rot.x;
- w[idx++] = rot.y;
- w[idx++] = rot.z;
- w[idx++] = rot.w;
- w[idx++] = scale.x;
- w[idx++] = scale.y;
- w[idx++] = scale.z;
- }
- w.release();
- r_ret = keys;
- return true;
- } else if (track_get_type(track) == TYPE_VALUE) {
- const ValueTrack *vt = static_cast<const ValueTrack *>(tracks[track]);
- Dictionary d;
- PoolVector<float> key_times;
- PoolVector<float> key_transitions;
- Array key_values;
- int kk = vt->values.size();
- key_times.resize(kk);
- key_transitions.resize(kk);
- key_values.resize(kk);
- PoolVector<float>::Write wti = key_times.write();
- PoolVector<float>::Write wtr = key_transitions.write();
- int idx = 0;
- const TKey<Variant> *vls = vt->values.ptr();
- for (int i = 0; i < kk; i++) {
- wti[idx] = vls[i].time;
- wtr[idx] = vls[i].transition;
- key_values[idx] = vls[i].value;
- idx++;
- }
- wti.release();
- wtr.release();
- d["times"] = key_times;
- d["transitions"] = key_transitions;
- d["values"] = key_values;
- if (track_get_type(track) == TYPE_VALUE) {
- d["update"] = value_track_get_update_mode(track);
- }
- r_ret = d;
- return true;
- } else if (track_get_type(track) == TYPE_METHOD) {
- Dictionary d;
- PoolVector<float> key_times;
- PoolVector<float> key_transitions;
- Array key_values;
- int kk = track_get_key_count(track);
- key_times.resize(kk);
- key_transitions.resize(kk);
- key_values.resize(kk);
- PoolVector<float>::Write wti = key_times.write();
- PoolVector<float>::Write wtr = key_transitions.write();
- int idx = 0;
- for (int i = 0; i < track_get_key_count(track); i++) {
- wti[idx] = track_get_key_time(track, i);
- wtr[idx] = track_get_key_transition(track, i);
- key_values[idx] = track_get_key_value(track, i);
- idx++;
- }
- wti.release();
- wtr.release();
- d["times"] = key_times;
- d["transitions"] = key_transitions;
- d["values"] = key_values;
- if (track_get_type(track) == TYPE_VALUE) {
- d["update"] = value_track_get_update_mode(track);
- }
- r_ret = d;
- return true;
- } else if (track_get_type(track) == TYPE_BEZIER) {
- const BezierTrack *bt = static_cast<const BezierTrack *>(tracks[track]);
- Dictionary d;
- PoolVector<float> key_times;
- PoolVector<float> key_points;
- int kk = bt->values.size();
- key_times.resize(kk);
- key_points.resize(kk * 5);
- PoolVector<float>::Write wti = key_times.write();
- PoolVector<float>::Write wpo = key_points.write();
- int idx = 0;
- const TKey<BezierKey> *vls = bt->values.ptr();
- for (int i = 0; i < kk; i++) {
- wti[idx] = vls[i].time;
- wpo[idx * 5 + 0] = vls[i].value.value;
- wpo[idx * 5 + 1] = vls[i].value.in_handle.x;
- wpo[idx * 5 + 2] = vls[i].value.in_handle.y;
- wpo[idx * 5 + 3] = vls[i].value.out_handle.x;
- wpo[idx * 5 + 4] = vls[i].value.out_handle.y;
- idx++;
- }
- wti.release();
- wpo.release();
- d["times"] = key_times;
- d["points"] = key_points;
- r_ret = d;
- return true;
- } else if (track_get_type(track) == TYPE_AUDIO) {
- const AudioTrack *ad = static_cast<const AudioTrack *>(tracks[track]);
- Dictionary d;
- PoolVector<float> key_times;
- Array clips;
- int kk = ad->values.size();
- key_times.resize(kk);
- PoolVector<float>::Write wti = key_times.write();
- int idx = 0;
- const TKey<AudioKey> *vls = ad->values.ptr();
- for (int i = 0; i < kk; i++) {
- wti[idx] = vls[i].time;
- Dictionary clip;
- clip["start_offset"] = vls[i].value.start_offset;
- clip["end_offset"] = vls[i].value.end_offset;
- clip["stream"] = vls[i].value.stream;
- clips.push_back(clip);
- idx++;
- }
- wti.release();
- d["times"] = key_times;
- d["clips"] = clips;
- r_ret = d;
- return true;
- } else if (track_get_type(track) == TYPE_ANIMATION) {
- const AnimationTrack *an = static_cast<const AnimationTrack *>(tracks[track]);
- Dictionary d;
- PoolVector<float> key_times;
- PoolVector<String> clips;
- int kk = an->values.size();
- key_times.resize(kk);
- clips.resize(kk);
- PoolVector<float>::Write wti = key_times.write();
- PoolVector<String>::Write wcl = clips.write();
- const TKey<StringName> *vls = an->values.ptr();
- for (int i = 0; i < kk; i++) {
- wti[i] = vls[i].time;
- wcl[i] = vls[i].value;
- }
- wti.release();
- wcl.release();
- d["times"] = key_times;
- d["clips"] = clips;
- r_ret = d;
- return true;
- }
- } else
- return false;
- } else
- return false;
- return true;
- }
- void Animation::_get_property_list(List<PropertyInfo> *p_list) const {
- for (int i = 0; i < tracks.size(); i++) {
- p_list->push_back(PropertyInfo(Variant::STRING, "tracks/" + itos(i) + "/type", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
- p_list->push_back(PropertyInfo(Variant::NODE_PATH, "tracks/" + itos(i) + "/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
- p_list->push_back(PropertyInfo(Variant::INT, "tracks/" + itos(i) + "/interp", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
- p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/loop_wrap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
- p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/imported", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
- p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/enabled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
- p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL));
- }
- }
- int Animation::add_track(TrackType p_type, int p_at_pos) {
- if (p_at_pos < 0 || p_at_pos >= tracks.size())
- p_at_pos = tracks.size();
- switch (p_type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = memnew(TransformTrack);
- tracks.insert(p_at_pos, tt);
- } break;
- case TYPE_VALUE: {
- tracks.insert(p_at_pos, memnew(ValueTrack));
- } break;
- case TYPE_METHOD: {
- tracks.insert(p_at_pos, memnew(MethodTrack));
- } break;
- case TYPE_BEZIER: {
- tracks.insert(p_at_pos, memnew(BezierTrack));
- } break;
- case TYPE_AUDIO: {
- tracks.insert(p_at_pos, memnew(AudioTrack));
- } break;
- case TYPE_ANIMATION: {
- tracks.insert(p_at_pos, memnew(AnimationTrack));
- } break;
- default: {
- ERR_PRINT("Unknown track type");
- }
- }
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- return p_at_pos;
- }
- void Animation::remove_track(int p_track) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- _clear(tt->transforms);
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- _clear(vt->values);
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- _clear(mt->methods);
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bz = static_cast<BezierTrack *>(t);
- _clear(bz->values);
- } break;
- case TYPE_AUDIO: {
- AudioTrack *ad = static_cast<AudioTrack *>(t);
- _clear(ad->values);
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *an = static_cast<AnimationTrack *>(t);
- _clear(an->values);
- } break;
- }
- memdelete(t);
- tracks.remove(p_track);
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- }
- int Animation::get_track_count() const {
- return tracks.size();
- }
- Animation::TrackType Animation::track_get_type(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), TYPE_TRANSFORM);
- return tracks[p_track]->type;
- }
- void Animation::track_set_path(int p_track, const NodePath &p_path) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- tracks[p_track]->path = p_path;
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- }
- NodePath Animation::track_get_path(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), NodePath());
- return tracks[p_track]->path;
- }
- int Animation::find_track(const NodePath &p_path) const {
- for (int i = 0; i < tracks.size(); i++) {
- if (tracks[i]->path == p_path)
- return i;
- };
- return -1;
- };
- void Animation::track_set_interpolation_type(int p_track, InterpolationType p_interp) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- ERR_FAIL_INDEX(p_interp, 3);
- tracks[p_track]->interpolation = p_interp;
- emit_changed();
- }
- Animation::InterpolationType Animation::track_get_interpolation_type(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
- return tracks[p_track]->interpolation;
- }
- void Animation::track_set_interpolation_loop_wrap(int p_track, bool p_enable) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- tracks[p_track]->loop_wrap = p_enable;
- emit_changed();
- }
- bool Animation::track_get_interpolation_loop_wrap(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), INTERPOLATION_NEAREST);
- return tracks[p_track]->loop_wrap;
- }
- // transform
- /*
- template<class T>
- int Animation::_insert_pos(float p_time, T& p_keys) {
- // simple, linear time inset that should be fast enough in reality.
- int idx=p_keys.size();
- while(true) {
- if (idx==0 || p_keys[idx-1].time < p_time) {
- //condition for insertion.
- p_keys.insert(idx,T());
- return idx;
- } else if (p_keys[idx-1].time == p_time) {
- // condition for replacing.
- return idx-1;
- }
- idx--;
- }
- }
- */
- template <class T, class V>
- int Animation::_insert(float p_time, T &p_keys, const V &p_value) {
- int idx = p_keys.size();
- while (true) {
- // Condition for replacement.
- if (idx > 0 && Math::is_equal_approx(p_keys[idx - 1].time, p_time)) {
- float transition = p_keys[idx - 1].transition;
- p_keys.write[idx - 1] = p_value;
- p_keys.write[idx - 1].transition = transition;
- return idx - 1;
- // Condition for insert.
- } else if (idx == 0 || p_keys[idx - 1].time < p_time) {
- p_keys.insert(idx, p_value);
- return idx;
- }
- idx--;
- }
- return -1;
- }
- template <class T>
- void Animation::_clear(T &p_keys) {
- p_keys.clear();
- }
- Error Animation::transform_track_get_key(int p_track, int p_key, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
- Track *t = tracks[p_track];
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, ERR_INVALID_PARAMETER);
- ERR_FAIL_INDEX_V(p_key, tt->transforms.size(), ERR_INVALID_PARAMETER);
- if (r_loc)
- *r_loc = tt->transforms[p_key].value.loc;
- if (r_rot)
- *r_rot = tt->transforms[p_key].value.rot;
- if (r_scale)
- *r_scale = tt->transforms[p_key].value.scale;
- return OK;
- }
- int Animation::transform_track_insert_key(int p_track, float p_time, const Vector3 &p_loc, const Quat &p_rot, const Vector3 &p_scale) {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, -1);
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- TKey<TransformKey> tkey;
- tkey.time = p_time;
- tkey.value.loc = p_loc;
- tkey.value.rot = p_rot;
- tkey.value.scale = p_scale;
- int ret = _insert(p_time, tt->transforms, tkey);
- emit_changed();
- return ret;
- }
- void Animation::track_remove_key_at_position(int p_track, float p_pos) {
- int idx = track_find_key(p_track, p_pos, true);
- ERR_FAIL_COND(idx < 0);
- track_remove_key(p_track, idx);
- }
- void Animation::track_remove_key(int p_track, int p_idx) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_INDEX(p_idx, tt->transforms.size());
- tt->transforms.remove(p_idx);
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- ERR_FAIL_INDEX(p_idx, vt->values.size());
- vt->values.remove(p_idx);
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX(p_idx, mt->methods.size());
- mt->methods.remove(p_idx);
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bz = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX(p_idx, bz->values.size());
- bz->values.remove(p_idx);
- } break;
- case TYPE_AUDIO: {
- AudioTrack *ad = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX(p_idx, ad->values.size());
- ad->values.remove(p_idx);
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *an = static_cast<AnimationTrack *>(t);
- ERR_FAIL_INDEX(p_idx, an->values.size());
- an->values.remove(p_idx);
- } break;
- }
- emit_changed();
- }
- int Animation::track_find_key(int p_track, float p_time, bool p_exact) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- int k = _find(tt->transforms, p_time);
- if (k < 0 || k >= tt->transforms.size())
- return -1;
- if (tt->transforms[k].time != p_time && p_exact)
- return -1;
- return k;
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- int k = _find(vt->values, p_time);
- if (k < 0 || k >= vt->values.size())
- return -1;
- if (vt->values[k].time != p_time && p_exact)
- return -1;
- return k;
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- int k = _find(mt->methods, p_time);
- if (k < 0 || k >= mt->methods.size())
- return -1;
- if (mt->methods[k].time != p_time && p_exact)
- return -1;
- return k;
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- int k = _find(bt->values, p_time);
- if (k < 0 || k >= bt->values.size())
- return -1;
- if (bt->values[k].time != p_time && p_exact)
- return -1;
- return k;
- } break;
- case TYPE_AUDIO: {
- AudioTrack *at = static_cast<AudioTrack *>(t);
- int k = _find(at->values, p_time);
- if (k < 0 || k >= at->values.size())
- return -1;
- if (at->values[k].time != p_time && p_exact)
- return -1;
- return k;
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- int k = _find(at->values, p_time);
- if (k < 0 || k >= at->values.size())
- return -1;
- if (at->values[k].time != p_time && p_exact)
- return -1;
- return k;
- } break;
- }
- return -1;
- }
- void Animation::track_insert_key(int p_track, float p_time, const Variant &p_key, float p_transition) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- Dictionary d = p_key;
- Vector3 loc;
- if (d.has("location"))
- loc = d["location"];
- Quat rot;
- if (d.has("rotation"))
- rot = d["rotation"];
- Vector3 scale;
- if (d.has("scale"))
- scale = d["scale"];
- int idx = transform_track_insert_key(p_track, p_time, loc, rot, scale);
- track_set_key_transition(p_track, idx, p_transition);
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- TKey<Variant> k;
- k.time = p_time;
- k.transition = p_transition;
- k.value = p_key;
- _insert(p_time, vt->values, k);
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_COND(p_key.get_type() != Variant::DICTIONARY);
- Dictionary d = p_key;
- ERR_FAIL_COND(!d.has("method") || d["method"].get_type() != Variant::STRING);
- ERR_FAIL_COND(!d.has("args") || !d["args"].is_array());
- MethodKey k;
- k.time = p_time;
- k.transition = p_transition;
- k.method = d["method"];
- k.params = d["args"];
- _insert(p_time, mt->methods, k);
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- Array arr = p_key;
- ERR_FAIL_COND(arr.size() != 5);
- TKey<BezierKey> k;
- k.time = p_time;
- k.value.value = arr[0];
- k.value.in_handle.x = arr[1];
- k.value.in_handle.y = arr[2];
- k.value.out_handle.x = arr[3];
- k.value.out_handle.y = arr[4];
- _insert(p_time, bt->values, k);
- } break;
- case TYPE_AUDIO: {
- AudioTrack *at = static_cast<AudioTrack *>(t);
- Dictionary k = p_key;
- ERR_FAIL_COND(!k.has("start_offset"));
- ERR_FAIL_COND(!k.has("end_offset"));
- ERR_FAIL_COND(!k.has("stream"));
- TKey<AudioKey> ak;
- ak.time = p_time;
- ak.value.start_offset = k["start_offset"];
- ak.value.end_offset = k["end_offset"];
- ak.value.stream = k["stream"];
- _insert(p_time, at->values, ak);
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- TKey<StringName> ak;
- ak.time = p_time;
- ak.value = p_key;
- _insert(p_time, at->values, ak);
- } break;
- }
- emit_changed();
- }
- int Animation::track_get_key_count(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- return tt->transforms.size();
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- return vt->values.size();
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- return mt->methods.size();
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- return bt->values.size();
- } break;
- case TYPE_AUDIO: {
- AudioTrack *at = static_cast<AudioTrack *>(t);
- return at->values.size();
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- return at->values.size();
- } break;
- }
- ERR_FAIL_V(-1);
- }
- Variant Animation::track_get_key_value(int p_track, int p_key_idx) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), Variant());
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), Variant());
- Dictionary d;
- d["location"] = tt->transforms[p_key_idx].value.loc;
- d["rotation"] = tt->transforms[p_key_idx].value.rot;
- d["scale"] = tt->transforms[p_key_idx].value.scale;
- return d;
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), Variant());
- return vt->values[p_key_idx].value;
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), Variant());
- Dictionary d;
- d["method"] = mt->methods[p_key_idx].method;
- d["args"] = mt->methods[p_key_idx].params;
- return d;
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), Variant());
- Array arr;
- arr.resize(5);
- arr[0] = bt->values[p_key_idx].value.value;
- arr[1] = bt->values[p_key_idx].value.in_handle.x;
- arr[2] = bt->values[p_key_idx].value.in_handle.y;
- arr[3] = bt->values[p_key_idx].value.out_handle.x;
- arr[4] = bt->values[p_key_idx].value.out_handle.y;
- return arr;
- } break;
- case TYPE_AUDIO: {
- AudioTrack *at = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
- Dictionary k;
- k["start_offset"] = at->values[p_key_idx].value.start_offset;
- k["end_offset"] = at->values[p_key_idx].value.end_offset;
- k["stream"] = at->values[p_key_idx].value.stream;
- return k;
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), Variant());
- return at->values[p_key_idx].value;
- } break;
- }
- ERR_FAIL_V(Variant());
- }
- float Animation::track_get_key_time(int p_track, int p_key_idx) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1);
- return tt->transforms[p_key_idx].time;
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
- return vt->values[p_key_idx].time;
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
- return mt->methods[p_key_idx].time;
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, bt->values.size(), -1);
- return bt->values[p_key_idx].time;
- } break;
- case TYPE_AUDIO: {
- AudioTrack *at = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
- return at->values[p_key_idx].time;
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, at->values.size(), -1);
- return at->values[p_key_idx].time;
- } break;
- }
- ERR_FAIL_V(-1);
- }
- void Animation::track_set_key_time(int p_track, int p_key_idx, float p_time) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
- TKey<TransformKey> key = tt->transforms[p_key_idx];
- key.time = p_time;
- tt->transforms.remove(p_key_idx);
- _insert(p_time, tt->transforms, key);
- return;
- }
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, vt->values.size());
- TKey<Variant> key = vt->values[p_key_idx];
- key.time = p_time;
- vt->values.remove(p_key_idx);
- _insert(p_time, vt->values, key);
- return;
- }
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
- MethodKey key = mt->methods[p_key_idx];
- key.time = p_time;
- mt->methods.remove(p_key_idx);
- _insert(p_time, mt->methods, key);
- return;
- }
- case TYPE_BEZIER: {
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, bt->values.size());
- TKey<BezierKey> key = bt->values[p_key_idx];
- key.time = p_time;
- bt->values.remove(p_key_idx);
- _insert(p_time, bt->values, key);
- return;
- }
- case TYPE_AUDIO: {
- AudioTrack *at = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, at->values.size());
- TKey<AudioKey> key = at->values[p_key_idx];
- key.time = p_time;
- at->values.remove(p_key_idx);
- _insert(p_time, at->values, key);
- return;
- }
- case TYPE_ANIMATION: {
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, at->values.size());
- TKey<StringName> key = at->values[p_key_idx];
- key.time = p_time;
- at->values.remove(p_key_idx);
- _insert(p_time, at->values, key);
- return;
- }
- }
- ERR_FAIL();
- }
- float Animation::track_get_key_transition(int p_track, int p_key_idx) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, tt->transforms.size(), -1);
- return tt->transforms[p_key_idx].transition;
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, vt->values.size(), -1);
- return vt->values[p_key_idx].transition;
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, mt->methods.size(), -1);
- return mt->methods[p_key_idx].transition;
- } break;
- case TYPE_BEZIER: {
- return 1; //bezier does not really use transitions
- } break;
- case TYPE_AUDIO: {
- return 1; //audio does not really use transitions
- } break;
- case TYPE_ANIMATION: {
- return 1; //animation does not really use transitions
- } break;
- }
- ERR_FAIL_V(0);
- }
- void Animation::track_set_key_value(int p_track, int p_key_idx, const Variant &p_value) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
- Dictionary d = p_value;
- if (d.has("location"))
- tt->transforms.write[p_key_idx].value.loc = d["location"];
- if (d.has("rotation"))
- tt->transforms.write[p_key_idx].value.rot = d["rotation"];
- if (d.has("scale"))
- tt->transforms.write[p_key_idx].value.scale = d["scale"];
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, vt->values.size());
- vt->values.write[p_key_idx].value = p_value;
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
- Dictionary d = p_value;
- if (d.has("method"))
- mt->methods.write[p_key_idx].method = d["method"];
- if (d.has("args"))
- mt->methods.write[p_key_idx].params = d["args"];
- } break;
- case TYPE_BEZIER: {
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, bt->values.size());
- Array arr = p_value;
- ERR_FAIL_COND(arr.size() != 5);
- bt->values.write[p_key_idx].value.value = arr[0];
- bt->values.write[p_key_idx].value.in_handle.x = arr[1];
- bt->values.write[p_key_idx].value.in_handle.y = arr[2];
- bt->values.write[p_key_idx].value.out_handle.x = arr[3];
- bt->values.write[p_key_idx].value.out_handle.y = arr[4];
- } break;
- case TYPE_AUDIO: {
- AudioTrack *at = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, at->values.size());
- Dictionary k = p_value;
- ERR_FAIL_COND(!k.has("start_offset"));
- ERR_FAIL_COND(!k.has("end_offset"));
- ERR_FAIL_COND(!k.has("stream"));
- at->values.write[p_key_idx].value.start_offset = k["start_offset"];
- at->values.write[p_key_idx].value.end_offset = k["end_offset"];
- at->values.write[p_key_idx].value.stream = k["stream"];
- } break;
- case TYPE_ANIMATION: {
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, at->values.size());
- at->values.write[p_key_idx].value = p_value;
- } break;
- }
- emit_changed();
- }
- void Animation::track_set_key_transition(int p_track, int p_key_idx, float p_transition) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- switch (t->type) {
- case TYPE_TRANSFORM: {
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, tt->transforms.size());
- tt->transforms.write[p_key_idx].transition = p_transition;
- } break;
- case TYPE_VALUE: {
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, vt->values.size());
- vt->values.write[p_key_idx].transition = p_transition;
- } break;
- case TYPE_METHOD: {
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX(p_key_idx, mt->methods.size());
- mt->methods.write[p_key_idx].transition = p_transition;
- } break;
- case TYPE_BEZIER:
- case TYPE_AUDIO:
- case TYPE_ANIMATION: {
- // they don't use transition
- } break;
- }
- emit_changed();
- }
- template <class K>
- int Animation::_find(const Vector<K> &p_keys, float p_time) const {
- int len = p_keys.size();
- if (len == 0)
- return -2;
- int low = 0;
- int high = len - 1;
- int middle = 0;
- #ifdef DEBUG_ENABLED
- if (low > high)
- ERR_PRINT("low > high, this may be a bug");
- #endif
- const K *keys = &p_keys[0];
- while (low <= high) {
- middle = (low + high) / 2;
- if (Math::is_equal_approx(p_time, keys[middle].time)) { //match
- return middle;
- } else if (p_time < keys[middle].time)
- high = middle - 1; //search low end of array
- else
- low = middle + 1; //search high end of array
- }
- if (keys[middle].time > p_time)
- middle--;
- return middle;
- }
- Animation::TransformKey Animation::_interpolate(const Animation::TransformKey &p_a, const Animation::TransformKey &p_b, float p_c) const {
- TransformKey ret;
- ret.loc = _interpolate(p_a.loc, p_b.loc, p_c);
- ret.rot = _interpolate(p_a.rot, p_b.rot, p_c);
- ret.scale = _interpolate(p_a.scale, p_b.scale, p_c);
- return ret;
- }
- Vector3 Animation::_interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const {
- return p_a.linear_interpolate(p_b, p_c);
- }
- Quat Animation::_interpolate(const Quat &p_a, const Quat &p_b, float p_c) const {
- return p_a.slerp(p_b, p_c);
- }
- Variant Animation::_interpolate(const Variant &p_a, const Variant &p_b, float p_c) const {
- Variant dst;
- Variant::interpolate(p_a, p_b, p_c, dst);
- return dst;
- }
- float Animation::_interpolate(const float &p_a, const float &p_b, float p_c) const {
- return p_a * (1.0 - p_c) + p_b * p_c;
- }
- 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 {
- Animation::TransformKey tk;
- tk.loc = p_a.loc.cubic_interpolate(p_b.loc, p_pre_a.loc, p_post_b.loc, p_c);
- tk.scale = p_a.scale.cubic_interpolate(p_b.scale, p_pre_a.scale, p_post_b.scale, p_c);
- tk.rot = p_a.rot.cubic_slerp(p_b.rot, p_pre_a.rot, p_post_b.rot, p_c);
- return tk;
- }
- 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 {
- return p_a.cubic_interpolate(p_b, p_pre_a, p_post_b, p_c);
- }
- 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 {
- return p_a.cubic_slerp(p_b, p_pre_a, p_post_b, p_c);
- }
- 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 {
- Variant::Type type_a = p_a.get_type();
- Variant::Type type_b = p_b.get_type();
- Variant::Type type_pa = p_pre_a.get_type();
- Variant::Type type_pb = p_post_b.get_type();
- //make int and real play along
- uint32_t vformat = 1 << type_a;
- vformat |= 1 << type_b;
- vformat |= 1 << type_pa;
- vformat |= 1 << type_pb;
- if (vformat == ((1 << Variant::INT) | (1 << Variant::REAL)) || vformat == (1 << Variant::REAL)) {
- //mix of real and int
- real_t p0 = p_pre_a;
- real_t p1 = p_a;
- real_t p2 = p_b;
- real_t p3 = p_post_b;
- float t = p_c;
- float t2 = t * t;
- float t3 = t2 * t;
- return 0.5f * ((p1 * 2.0f) +
- (-p0 + p2) * t +
- (2.0f * p0 - 5.0f * p1 + 4 * p2 - p3) * t2 +
- (-p0 + 3.0f * p1 - 3.0f * p2 + p3) * t3);
- } else if ((vformat & (vformat - 1))) {
- return p_a; //can't interpolate, mix of types
- }
- switch (type_a) {
- case Variant::VECTOR2: {
- Vector2 a = p_a;
- Vector2 b = p_b;
- Vector2 pa = p_pre_a;
- Vector2 pb = p_post_b;
- return a.cubic_interpolate(b, pa, pb, p_c);
- }
- case Variant::RECT2: {
- Rect2 a = p_a;
- Rect2 b = p_b;
- Rect2 pa = p_pre_a;
- Rect2 pb = p_post_b;
- return Rect2(
- a.position.cubic_interpolate(b.position, pa.position, pb.position, p_c),
- a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c));
- }
- case Variant::VECTOR3: {
- Vector3 a = p_a;
- Vector3 b = p_b;
- Vector3 pa = p_pre_a;
- Vector3 pb = p_post_b;
- return a.cubic_interpolate(b, pa, pb, p_c);
- }
- case Variant::QUAT: {
- Quat a = p_a;
- Quat b = p_b;
- Quat pa = p_pre_a;
- Quat pb = p_post_b;
- return a.cubic_slerp(b, pa, pb, p_c);
- }
- case Variant::AABB: {
- AABB a = p_a;
- AABB b = p_b;
- AABB pa = p_pre_a;
- AABB pb = p_post_b;
- return AABB(
- a.position.cubic_interpolate(b.position, pa.position, pb.position, p_c),
- a.size.cubic_interpolate(b.size, pa.size, pb.size, p_c));
- }
- default: {
- return _interpolate(p_a, p_b, p_c);
- }
- }
- }
- 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 {
- return _interpolate(p_a, p_b, p_c);
- }
- template <class T>
- T Animation::_interpolate(const Vector<TKey<T> > &p_keys, float p_time, InterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const {
- int len = _find(p_keys, length) + 1; // try to find last key (there may be more past the end)
- if (len <= 0) {
- // (-1 or -2 returned originally) (plus one above)
- // meaning no keys, or only key time is larger than length
- if (p_ok)
- *p_ok = false;
- return T();
- } else if (len == 1) { // one key found (0+1), return it
- if (p_ok)
- *p_ok = true;
- return p_keys[0].value;
- }
- int idx = _find(p_keys, p_time);
- ERR_FAIL_COND_V(idx == -2, T());
- bool result = true;
- int next = 0;
- float c = 0;
- // prepare for all cases of interpolation
- if (loop && p_loop_wrap) {
- // loop
- if (idx >= 0) {
- if ((idx + 1) < len) {
- next = idx + 1;
- float delta = p_keys[next].time - p_keys[idx].time;
- float from = p_time - p_keys[idx].time;
- if (Math::is_zero_approx(delta))
- c = 0;
- else
- c = from / delta;
- } else {
- next = 0;
- float delta = (length - p_keys[idx].time) + p_keys[next].time;
- float from = p_time - p_keys[idx].time;
- if (Math::is_zero_approx(delta))
- c = 0;
- else
- c = from / delta;
- }
- } else {
- // on loop, behind first key
- idx = len - 1;
- next = 0;
- float endtime = (length - p_keys[idx].time);
- if (endtime < 0) // may be keys past the end
- endtime = 0;
- float delta = endtime + p_keys[next].time;
- float from = endtime + p_time;
- if (Math::is_zero_approx(delta))
- c = 0;
- else
- c = from / delta;
- }
- } else { // no loop
- if (idx >= 0) {
- if ((idx + 1) < len) {
- next = idx + 1;
- float delta = p_keys[next].time - p_keys[idx].time;
- float from = p_time - p_keys[idx].time;
- if (Math::is_zero_approx(delta))
- c = 0;
- else
- c = from / delta;
- } else {
- next = idx;
- }
- } else {
- // only allow extending first key to anim start if looping
- if (loop)
- idx = next = 0;
- else
- result = false;
- }
- }
- if (p_ok)
- *p_ok = result;
- if (!result)
- return T();
- float tr = p_keys[idx].transition;
- if (tr == 0 || idx == next) {
- // don't interpolate if not needed
- return p_keys[idx].value;
- }
- if (tr != 1.0) {
- c = Math::ease(c, tr);
- }
- switch (p_interp) {
- case INTERPOLATION_NEAREST: {
- return p_keys[idx].value;
- } break;
- case INTERPOLATION_LINEAR: {
- return _interpolate(p_keys[idx].value, p_keys[next].value, c);
- } break;
- case INTERPOLATION_CUBIC: {
- int pre = idx - 1;
- if (pre < 0)
- pre = 0;
- int post = next + 1;
- if (post >= len)
- post = next;
- return _cubic_interpolate(p_keys[pre].value, p_keys[idx].value, p_keys[next].value, p_keys[post].value, c);
- } break;
- default: return p_keys[idx].value;
- }
- // do a barrel roll
- }
- Error Animation::transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), ERR_INVALID_PARAMETER);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_TRANSFORM, ERR_INVALID_PARAMETER);
- TransformTrack *tt = static_cast<TransformTrack *>(t);
- bool ok = false;
- TransformKey tk = _interpolate(tt->transforms, p_time, tt->interpolation, tt->loop_wrap, &ok);
- if (!ok)
- return ERR_UNAVAILABLE;
- if (r_loc)
- *r_loc = tk.loc;
- if (r_rot)
- *r_rot = tk.rot;
- if (r_scale)
- *r_scale = tk.scale;
- return OK;
- }
- Variant Animation::value_track_interpolate(int p_track, float p_time) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_VALUE, Variant());
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- bool ok = false;
- 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);
- if (ok) {
- return res;
- }
- return Variant();
- }
- void Animation::_value_track_get_key_indices_in_range(const ValueTrack *vt, float from_time, float to_time, List<int> *p_indices) const {
- if (from_time != length && to_time == length)
- to_time = length * 1.001; //include a little more if at the end
- int to = _find(vt->values, to_time);
- if (to >= 0 && from_time == to_time && vt->values[to].time == from_time) {
- //find exact (0 delta), return if found
- p_indices->push_back(to);
- return;
- }
- // can't really send the events == time, will be sent in the next frame.
- // if event>=len then it will probably never be requested by the anim player.
- if (to >= 0 && vt->values[to].time >= to_time)
- to--;
- if (to < 0)
- return; // not bother
- int from = _find(vt->values, from_time);
- // position in the right first event.+
- if (from < 0 || vt->values[from].time < from_time)
- from++;
- int max = vt->values.size();
- for (int i = from; i <= to; i++) {
- ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
- p_indices->push_back(i);
- }
- }
- void Animation::value_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_VALUE);
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- float from_time = p_time - p_delta;
- float to_time = p_time;
- if (from_time > to_time)
- SWAP(from_time, to_time);
- if (loop) {
- from_time = Math::fposmod(from_time, length);
- to_time = Math::fposmod(to_time, length);
- if (from_time > to_time) {
- // handle loop by splitting
- _value_track_get_key_indices_in_range(vt, from_time, length, p_indices);
- _value_track_get_key_indices_in_range(vt, 0, to_time, p_indices);
- return;
- }
- } else {
- if (from_time < 0)
- from_time = 0;
- if (from_time > length)
- from_time = length;
- if (to_time < 0)
- to_time = 0;
- if (to_time > length)
- to_time = length;
- }
- _value_track_get_key_indices_in_range(vt, from_time, to_time, p_indices);
- }
- void Animation::value_track_set_update_mode(int p_track, UpdateMode p_mode) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_VALUE);
- ERR_FAIL_INDEX((int)p_mode, 4);
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- vt->update_mode = p_mode;
- }
- Animation::UpdateMode Animation::value_track_get_update_mode(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), UPDATE_CONTINUOUS);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_VALUE, UPDATE_CONTINUOUS);
- ValueTrack *vt = static_cast<ValueTrack *>(t);
- return vt->update_mode;
- }
- template <class T>
- void Animation::_track_get_key_indices_in_range(const Vector<T> &p_array, float from_time, float to_time, List<int> *p_indices) const {
- if (from_time != length && to_time == length)
- to_time = length * 1.01; //include a little more if at the end
- int to = _find(p_array, to_time);
- // can't really send the events == time, will be sent in the next frame.
- // if event>=len then it will probably never be requested by the anim player.
- if (to >= 0 && p_array[to].time >= to_time)
- to--;
- if (to < 0)
- return; // not bother
- int from = _find(p_array, from_time);
- // position in the right first event.+
- if (from < 0 || p_array[from].time < from_time)
- from++;
- int max = p_array.size();
- for (int i = from; i <= to; i++) {
- ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
- p_indices->push_back(i);
- }
- }
- void Animation::track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
- ERR_FAIL_INDEX(p_track, tracks.size());
- const Track *t = tracks[p_track];
- float from_time = p_time - p_delta;
- float to_time = p_time;
- if (from_time > to_time)
- SWAP(from_time, to_time);
- if (loop) {
- if (from_time > length || from_time < 0)
- from_time = Math::fposmod(from_time, length);
- if (to_time > length || to_time < 0)
- to_time = Math::fposmod(to_time, length);
- if (from_time > to_time) {
- // handle loop by splitting
- switch (t->type) {
- case TYPE_TRANSFORM: {
- const TransformTrack *tt = static_cast<const TransformTrack *>(t);
- _track_get_key_indices_in_range(tt->transforms, from_time, length, p_indices);
- _track_get_key_indices_in_range(tt->transforms, 0, to_time, p_indices);
- } break;
- case TYPE_VALUE: {
- const ValueTrack *vt = static_cast<const ValueTrack *>(t);
- _track_get_key_indices_in_range(vt->values, from_time, length, p_indices);
- _track_get_key_indices_in_range(vt->values, 0, to_time, p_indices);
- } break;
- case TYPE_METHOD: {
- const MethodTrack *mt = static_cast<const MethodTrack *>(t);
- _track_get_key_indices_in_range(mt->methods, from_time, length, p_indices);
- _track_get_key_indices_in_range(mt->methods, 0, to_time, p_indices);
- } break;
- case TYPE_BEZIER: {
- const BezierTrack *bz = static_cast<const BezierTrack *>(t);
- _track_get_key_indices_in_range(bz->values, from_time, length, p_indices);
- _track_get_key_indices_in_range(bz->values, 0, to_time, p_indices);
- } break;
- case TYPE_AUDIO: {
- const AudioTrack *ad = static_cast<const AudioTrack *>(t);
- _track_get_key_indices_in_range(ad->values, from_time, length, p_indices);
- _track_get_key_indices_in_range(ad->values, 0, to_time, p_indices);
- } break;
- case TYPE_ANIMATION: {
- const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
- _track_get_key_indices_in_range(an->values, from_time, length, p_indices);
- _track_get_key_indices_in_range(an->values, 0, to_time, p_indices);
- } break;
- }
- return;
- }
- } else {
- if (from_time < 0)
- from_time = 0;
- if (from_time > length)
- from_time = length;
- if (to_time < 0)
- to_time = 0;
- if (to_time > length)
- to_time = length;
- }
- switch (t->type) {
- case TYPE_TRANSFORM: {
- const TransformTrack *tt = static_cast<const TransformTrack *>(t);
- _track_get_key_indices_in_range(tt->transforms, from_time, to_time, p_indices);
- } break;
- case TYPE_VALUE: {
- const ValueTrack *vt = static_cast<const ValueTrack *>(t);
- _track_get_key_indices_in_range(vt->values, from_time, to_time, p_indices);
- } break;
- case TYPE_METHOD: {
- const MethodTrack *mt = static_cast<const MethodTrack *>(t);
- _track_get_key_indices_in_range(mt->methods, from_time, to_time, p_indices);
- } break;
- case TYPE_BEZIER: {
- const BezierTrack *bz = static_cast<const BezierTrack *>(t);
- _track_get_key_indices_in_range(bz->values, from_time, to_time, p_indices);
- } break;
- case TYPE_AUDIO: {
- const AudioTrack *ad = static_cast<const AudioTrack *>(t);
- _track_get_key_indices_in_range(ad->values, from_time, to_time, p_indices);
- } break;
- case TYPE_ANIMATION: {
- const AnimationTrack *an = static_cast<const AnimationTrack *>(t);
- _track_get_key_indices_in_range(an->values, from_time, to_time, p_indices);
- } break;
- }
- }
- void Animation::_method_track_get_key_indices_in_range(const MethodTrack *mt, float from_time, float to_time, List<int> *p_indices) const {
- if (from_time != length && to_time == length)
- to_time = length * 1.01; //include a little more if at the end
- int to = _find(mt->methods, to_time);
- // can't really send the events == time, will be sent in the next frame.
- // if event>=len then it will probably never be requested by the anim player.
- if (to >= 0 && mt->methods[to].time >= to_time)
- to--;
- if (to < 0)
- return; // not bother
- int from = _find(mt->methods, from_time);
- // position in the right first event.+
- if (from < 0 || mt->methods[from].time < from_time)
- from++;
- int max = mt->methods.size();
- for (int i = from; i <= to; i++) {
- ERR_CONTINUE(i < 0 || i >= max); // shouldn't happen
- p_indices->push_back(i);
- }
- }
- void Animation::method_track_get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_METHOD);
- MethodTrack *mt = static_cast<MethodTrack *>(t);
- float from_time = p_time - p_delta;
- float to_time = p_time;
- if (from_time > to_time)
- SWAP(from_time, to_time);
- if (loop) {
- if (from_time > length || from_time < 0)
- from_time = Math::fposmod(from_time, length);
- if (to_time > length || to_time < 0)
- to_time = Math::fposmod(to_time, length);
- if (from_time > to_time) {
- // handle loop by splitting
- _method_track_get_key_indices_in_range(mt, from_time, length, p_indices);
- _method_track_get_key_indices_in_range(mt, 0, to_time, p_indices);
- return;
- }
- } else {
- if (from_time < 0)
- from_time = 0;
- if (from_time > length)
- from_time = length;
- if (to_time < 0)
- to_time = 0;
- if (to_time > length)
- to_time = length;
- }
- _method_track_get_key_indices_in_range(mt, from_time, to_time, p_indices);
- }
- Vector<Variant> Animation::method_track_get_params(int p_track, int p_key_idx) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector<Variant>());
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_METHOD, Vector<Variant>());
- MethodTrack *pm = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), Vector<Variant>());
- const MethodKey &mk = pm->methods[p_key_idx];
- return mk.params;
- }
- StringName Animation::method_track_get_name(int p_track, int p_key_idx) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_METHOD, StringName());
- MethodTrack *pm = static_cast<MethodTrack *>(t);
- ERR_FAIL_INDEX_V(p_key_idx, pm->methods.size(), StringName());
- return pm->methods[p_key_idx].method;
- }
- 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) {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_BEZIER, -1);
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- TKey<BezierKey> k;
- k.time = p_time;
- k.value.value = p_value;
- k.value.in_handle = p_in_handle;
- if (k.value.in_handle.x > 0) {
- k.value.in_handle.x = 0;
- }
- k.value.out_handle = p_out_handle;
- if (k.value.out_handle.x < 0) {
- k.value.out_handle.x = 0;
- }
- int key = _insert(p_time, bt->values, k);
- emit_changed();
- return key;
- }
- void Animation::bezier_track_set_key_value(int p_track, int p_index, float p_value) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_BEZIER);
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX(p_index, bt->values.size());
- bt->values.write[p_index].value.value = p_value;
- emit_changed();
- }
- void Animation::bezier_track_set_key_in_handle(int p_track, int p_index, const Vector2 &p_handle) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_BEZIER);
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX(p_index, bt->values.size());
- bt->values.write[p_index].value.in_handle = p_handle;
- if (bt->values[p_index].value.in_handle.x > 0) {
- bt->values.write[p_index].value.in_handle.x = 0;
- }
- emit_changed();
- }
- void Animation::bezier_track_set_key_out_handle(int p_track, int p_index, const Vector2 &p_handle) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_BEZIER);
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX(p_index, bt->values.size());
- bt->values.write[p_index].value.out_handle = p_handle;
- if (bt->values[p_index].value.out_handle.x < 0) {
- bt->values.write[p_index].value.out_handle.x = 0;
- }
- emit_changed();
- }
- float Animation::bezier_track_get_key_value(int p_track, int p_index) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_BEZIER, 0);
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX_V(p_index, bt->values.size(), 0);
- return bt->values[p_index].value.value;
- }
- Vector2 Animation::bezier_track_get_key_in_handle(int p_track, int p_index) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
- return bt->values[p_index].value.in_handle;
- }
- Vector2 Animation::bezier_track_get_key_out_handle(int p_track, int p_index) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), Vector2());
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_BEZIER, Vector2());
- BezierTrack *bt = static_cast<BezierTrack *>(t);
- ERR_FAIL_INDEX_V(p_index, bt->values.size(), Vector2());
- return bt->values[p_index].value.out_handle;
- }
- static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, const Vector2 &start, const Vector2 &control_1, const Vector2 &control_2, const Vector2 &end) {
- /* Formula from Wikipedia article on Bezier curves. */
- real_t omt = (1.0 - t);
- real_t omt2 = omt * omt;
- real_t omt3 = omt2 * omt;
- real_t t2 = t * t;
- real_t t3 = t2 * t;
- return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
- }
- float Animation::bezier_track_interpolate(int p_track, float p_time) const {
- //this uses a different interpolation scheme
- ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
- Track *track = tracks[p_track];
- ERR_FAIL_COND_V(track->type != TYPE_BEZIER, 0);
- BezierTrack *bt = static_cast<BezierTrack *>(track);
- int len = _find(bt->values, length) + 1; // try to find last key (there may be more past the end)
- if (len <= 0) {
- // (-1 or -2 returned originally) (plus one above)
- return 0;
- } else if (len == 1) { // one key found (0+1), return it
- return bt->values[0].value.value;
- }
- int idx = _find(bt->values, p_time);
- ERR_FAIL_COND_V(idx == -2, 0);
- //there really is no looping interpolation on bezier
- if (idx < 0) {
- return bt->values[0].value.value;
- }
- if (idx >= bt->values.size() - 1) {
- return bt->values[bt->values.size() - 1].value.value;
- }
- float t = p_time - bt->values[idx].time;
- int iterations = 10;
- float duration = bt->values[idx + 1].time - bt->values[idx].time; // time duration between our two keyframes
- float low = 0; // 0% of the current animation segment
- float high = 1; // 100% of the current animation segment
- float middle;
- Vector2 start(0, bt->values[idx].value.value);
- Vector2 start_out = start + bt->values[idx].value.out_handle;
- Vector2 end(duration, bt->values[idx + 1].value.value);
- Vector2 end_in = end + bt->values[idx + 1].value.in_handle;
- //narrow high and low as much as possible
- for (int i = 0; i < iterations; i++) {
- middle = (low + high) / 2;
- Vector2 interp = _bezier_interp(middle, start, start_out, end_in, end);
- if (interp.x < t) {
- low = middle;
- } else {
- high = middle;
- }
- }
- //interpolate the result:
- Vector2 low_pos = _bezier_interp(low, start, start_out, end_in, end);
- Vector2 high_pos = _bezier_interp(high, start, start_out, end_in, end);
- float c = (t - low_pos.x) / (high_pos.x - low_pos.x);
- return low_pos.linear_interpolate(high_pos, c).y;
- }
- int Animation::audio_track_insert_key(int p_track, float p_time, const RES &p_stream, float p_start_offset, float p_end_offset) {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_AUDIO, -1);
- AudioTrack *at = static_cast<AudioTrack *>(t);
- TKey<AudioKey> k;
- k.time = p_time;
- k.value.stream = p_stream;
- k.value.start_offset = p_start_offset;
- if (k.value.start_offset < 0)
- k.value.start_offset = 0;
- k.value.end_offset = p_end_offset;
- if (k.value.end_offset < 0)
- k.value.end_offset = 0;
- int key = _insert(p_time, at->values, k);
- emit_changed();
- return key;
- }
- void Animation::audio_track_set_key_stream(int p_track, int p_key, const RES &p_stream) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_AUDIO);
- AudioTrack *at = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX(p_key, at->values.size());
- at->values.write[p_key].value.stream = p_stream;
- emit_changed();
- }
- void Animation::audio_track_set_key_start_offset(int p_track, int p_key, float p_offset) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_AUDIO);
- AudioTrack *at = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX(p_key, at->values.size());
- if (p_offset < 0)
- p_offset = 0;
- at->values.write[p_key].value.start_offset = p_offset;
- emit_changed();
- }
- void Animation::audio_track_set_key_end_offset(int p_track, int p_key, float p_offset) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_AUDIO);
- AudioTrack *at = static_cast<AudioTrack *>(t);
- ERR_FAIL_INDEX(p_key, at->values.size());
- if (p_offset < 0)
- p_offset = 0;
- at->values.write[p_key].value.end_offset = p_offset;
- emit_changed();
- }
- RES Animation::audio_track_get_key_stream(int p_track, int p_key) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), RES());
- const Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_AUDIO, RES());
- const AudioTrack *at = static_cast<const AudioTrack *>(t);
- ERR_FAIL_INDEX_V(p_key, at->values.size(), RES());
- return at->values[p_key].value.stream;
- }
- float Animation::audio_track_get_key_start_offset(int p_track, int p_key) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
- const Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
- const AudioTrack *at = static_cast<const AudioTrack *>(t);
- ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
- return at->values[p_key].value.start_offset;
- }
- float Animation::audio_track_get_key_end_offset(int p_track, int p_key) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), 0);
- const Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_AUDIO, 0);
- const AudioTrack *at = static_cast<const AudioTrack *>(t);
- ERR_FAIL_INDEX_V(p_key, at->values.size(), 0);
- return at->values[p_key].value.end_offset;
- }
- //
- int Animation::animation_track_insert_key(int p_track, float p_time, const StringName &p_animation) {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), -1);
- Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, -1);
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- TKey<StringName> k;
- k.time = p_time;
- k.value = p_animation;
- int key = _insert(p_time, at->values, k);
- emit_changed();
- return key;
- }
- void Animation::animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- Track *t = tracks[p_track];
- ERR_FAIL_COND(t->type != TYPE_ANIMATION);
- AnimationTrack *at = static_cast<AnimationTrack *>(t);
- ERR_FAIL_INDEX(p_key, at->values.size());
- at->values.write[p_key].value = p_animation;
- emit_changed();
- }
- StringName Animation::animation_track_get_key_animation(int p_track, int p_key) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), StringName());
- const Track *t = tracks[p_track];
- ERR_FAIL_COND_V(t->type != TYPE_ANIMATION, StringName());
- const AnimationTrack *at = static_cast<const AnimationTrack *>(t);
- ERR_FAIL_INDEX_V(p_key, at->values.size(), StringName());
- return at->values[p_key].value;
- }
- void Animation::set_length(float p_length) {
- if (p_length < ANIM_MIN_LENGTH) {
- p_length = ANIM_MIN_LENGTH;
- }
- length = p_length;
- emit_changed();
- }
- float Animation::get_length() const {
- return length;
- }
- void Animation::set_loop(bool p_enabled) {
- loop = p_enabled;
- emit_changed();
- }
- bool Animation::has_loop() const {
- return loop;
- }
- void Animation::track_set_imported(int p_track, bool p_imported) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- tracks[p_track]->imported = p_imported;
- }
- bool Animation::track_is_imported(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
- return tracks[p_track]->imported;
- }
- void Animation::track_set_enabled(int p_track, bool p_enabled) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- tracks[p_track]->enabled = p_enabled;
- emit_changed();
- }
- bool Animation::track_is_enabled(int p_track) const {
- ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
- return tracks[p_track]->enabled;
- }
- void Animation::track_move_up(int p_track) {
- if (p_track >= 0 && p_track < (tracks.size() - 1)) {
- SWAP(tracks.write[p_track], tracks.write[p_track + 1]);
- }
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- }
- void Animation::track_move_down(int p_track) {
- if (p_track > 0 && p_track < tracks.size()) {
- SWAP(tracks.write[p_track], tracks.write[p_track - 1]);
- }
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- }
- void Animation::track_move_to(int p_track, int p_to_index) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- ERR_FAIL_INDEX(p_to_index, tracks.size() + 1);
- if (p_track == p_to_index || p_track == p_to_index - 1)
- return;
- Track *track = tracks.get(p_track);
- tracks.remove(p_track);
- // Take into account that the position of the tracks that come after the one removed will change.
- tracks.insert(p_to_index > p_track ? p_to_index - 1 : p_to_index, track);
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- }
- void Animation::track_swap(int p_track, int p_with_track) {
- ERR_FAIL_INDEX(p_track, tracks.size());
- ERR_FAIL_INDEX(p_with_track, tracks.size());
- if (p_track == p_with_track)
- return;
- SWAP(tracks.write[p_track], tracks.write[p_with_track]);
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- }
- void Animation::set_step(float p_step) {
- step = p_step;
- emit_changed();
- }
- float Animation::get_step() const {
- return step;
- }
- void Animation::copy_track(int p_track, Ref<Animation> p_to_animation) {
- ERR_FAIL_COND(p_to_animation.is_null());
- ERR_FAIL_INDEX(p_track, get_track_count());
- int dst_track = p_to_animation->get_track_count();
- p_to_animation->add_track(track_get_type(p_track));
- p_to_animation->track_set_path(dst_track, track_get_path(p_track));
- p_to_animation->track_set_imported(dst_track, track_is_imported(p_track));
- p_to_animation->track_set_enabled(dst_track, track_is_enabled(p_track));
- p_to_animation->track_set_interpolation_type(dst_track, track_get_interpolation_type(p_track));
- p_to_animation->track_set_interpolation_loop_wrap(dst_track, track_get_interpolation_loop_wrap(p_track));
- if (track_get_type(p_track) == TYPE_VALUE) {
- p_to_animation->value_track_set_update_mode(dst_track, value_track_get_update_mode(p_track));
- }
- for (int i = 0; i < track_get_key_count(p_track); i++) {
- 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));
- }
- }
- void Animation::_bind_methods() {
- ClassDB::bind_method(D_METHOD("add_track", "type", "at_position"), &Animation::add_track, DEFVAL(-1));
- ClassDB::bind_method(D_METHOD("remove_track", "track_idx"), &Animation::remove_track);
- ClassDB::bind_method(D_METHOD("get_track_count"), &Animation::get_track_count);
- ClassDB::bind_method(D_METHOD("track_get_type", "track_idx"), &Animation::track_get_type);
- ClassDB::bind_method(D_METHOD("track_get_path", "track_idx"), &Animation::track_get_path);
- ClassDB::bind_method(D_METHOD("track_set_path", "track_idx", "path"), &Animation::track_set_path);
- ClassDB::bind_method(D_METHOD("find_track", "path"), &Animation::find_track);
- ClassDB::bind_method(D_METHOD("track_move_up", "track_idx"), &Animation::track_move_up);
- ClassDB::bind_method(D_METHOD("track_move_down", "track_idx"), &Animation::track_move_down);
- ClassDB::bind_method(D_METHOD("track_move_to", "track_idx", "to_idx"), &Animation::track_move_to);
- ClassDB::bind_method(D_METHOD("track_swap", "track_idx", "with_idx"), &Animation::track_swap);
- ClassDB::bind_method(D_METHOD("track_set_imported", "track_idx", "imported"), &Animation::track_set_imported);
- ClassDB::bind_method(D_METHOD("track_is_imported", "track_idx"), &Animation::track_is_imported);
- ClassDB::bind_method(D_METHOD("track_set_enabled", "track_idx", "enabled"), &Animation::track_set_enabled);
- ClassDB::bind_method(D_METHOD("track_is_enabled", "track_idx"), &Animation::track_is_enabled);
- ClassDB::bind_method(D_METHOD("transform_track_insert_key", "track_idx", "time", "location", "rotation", "scale"), &Animation::transform_track_insert_key);
- ClassDB::bind_method(D_METHOD("track_insert_key", "track_idx", "time", "key", "transition"), &Animation::track_insert_key, DEFVAL(1));
- ClassDB::bind_method(D_METHOD("track_remove_key", "track_idx", "key_idx"), &Animation::track_remove_key);
- ClassDB::bind_method(D_METHOD("track_remove_key_at_position", "track_idx", "position"), &Animation::track_remove_key_at_position);
- ClassDB::bind_method(D_METHOD("track_set_key_value", "track_idx", "key", "value"), &Animation::track_set_key_value);
- ClassDB::bind_method(D_METHOD("track_set_key_transition", "track_idx", "key_idx", "transition"), &Animation::track_set_key_transition);
- ClassDB::bind_method(D_METHOD("track_set_key_time", "track_idx", "key_idx", "time"), &Animation::track_set_key_time);
- ClassDB::bind_method(D_METHOD("track_get_key_transition", "track_idx", "key_idx"), &Animation::track_get_key_transition);
- ClassDB::bind_method(D_METHOD("track_get_key_count", "track_idx"), &Animation::track_get_key_count);
- ClassDB::bind_method(D_METHOD("track_get_key_value", "track_idx", "key_idx"), &Animation::track_get_key_value);
- ClassDB::bind_method(D_METHOD("track_get_key_time", "track_idx", "key_idx"), &Animation::track_get_key_time);
- ClassDB::bind_method(D_METHOD("track_find_key", "track_idx", "time", "exact"), &Animation::track_find_key, DEFVAL(false));
- ClassDB::bind_method(D_METHOD("track_set_interpolation_type", "track_idx", "interpolation"), &Animation::track_set_interpolation_type);
- ClassDB::bind_method(D_METHOD("track_get_interpolation_type", "track_idx"), &Animation::track_get_interpolation_type);
- ClassDB::bind_method(D_METHOD("track_set_interpolation_loop_wrap", "track_idx", "interpolation"), &Animation::track_set_interpolation_loop_wrap);
- ClassDB::bind_method(D_METHOD("track_get_interpolation_loop_wrap", "track_idx"), &Animation::track_get_interpolation_loop_wrap);
- ClassDB::bind_method(D_METHOD("transform_track_interpolate", "track_idx", "time_sec"), &Animation::_transform_track_interpolate);
- ClassDB::bind_method(D_METHOD("value_track_set_update_mode", "track_idx", "mode"), &Animation::value_track_set_update_mode);
- ClassDB::bind_method(D_METHOD("value_track_get_update_mode", "track_idx"), &Animation::value_track_get_update_mode);
- ClassDB::bind_method(D_METHOD("value_track_get_key_indices", "track_idx", "time_sec", "delta"), &Animation::_value_track_get_key_indices);
- ClassDB::bind_method(D_METHOD("value_track_interpolate", "track_idx", "time_sec"), &Animation::value_track_interpolate);
- ClassDB::bind_method(D_METHOD("method_track_get_key_indices", "track_idx", "time_sec", "delta"), &Animation::_method_track_get_key_indices);
- ClassDB::bind_method(D_METHOD("method_track_get_name", "track_idx", "key_idx"), &Animation::method_track_get_name);
- ClassDB::bind_method(D_METHOD("method_track_get_params", "track_idx", "key_idx"), &Animation::method_track_get_params);
- ClassDB::bind_method(D_METHOD("bezier_track_insert_key", "track_idx", "time", "value", "in_handle", "out_handle"), &Animation::bezier_track_insert_key, DEFVAL(Vector2()), DEFVAL(Vector2()));
- ClassDB::bind_method(D_METHOD("bezier_track_set_key_value", "track_idx", "key_idx", "value"), &Animation::bezier_track_set_key_value);
- ClassDB::bind_method(D_METHOD("bezier_track_set_key_in_handle", "track_idx", "key_idx", "in_handle"), &Animation::bezier_track_set_key_in_handle);
- ClassDB::bind_method(D_METHOD("bezier_track_set_key_out_handle", "track_idx", "key_idx", "out_handle"), &Animation::bezier_track_set_key_out_handle);
- ClassDB::bind_method(D_METHOD("bezier_track_get_key_value", "track_idx", "key_idx"), &Animation::bezier_track_get_key_value);
- ClassDB::bind_method(D_METHOD("bezier_track_get_key_in_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_in_handle);
- ClassDB::bind_method(D_METHOD("bezier_track_get_key_out_handle", "track_idx", "key_idx"), &Animation::bezier_track_get_key_out_handle);
- ClassDB::bind_method(D_METHOD("bezier_track_interpolate", "track_idx", "time"), &Animation::bezier_track_interpolate);
- ClassDB::bind_method(D_METHOD("audio_track_insert_key", "track_idx", "time", "stream", "start_offset", "end_offset"), &Animation::audio_track_insert_key, DEFVAL(0), DEFVAL(0));
- ClassDB::bind_method(D_METHOD("audio_track_set_key_stream", "track_idx", "key_idx", "stream"), &Animation::audio_track_set_key_stream);
- ClassDB::bind_method(D_METHOD("audio_track_set_key_start_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_start_offset);
- ClassDB::bind_method(D_METHOD("audio_track_set_key_end_offset", "track_idx", "key_idx", "offset"), &Animation::audio_track_set_key_end_offset);
- ClassDB::bind_method(D_METHOD("audio_track_get_key_stream", "track_idx", "key_idx"), &Animation::audio_track_get_key_stream);
- ClassDB::bind_method(D_METHOD("audio_track_get_key_start_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_start_offset);
- ClassDB::bind_method(D_METHOD("audio_track_get_key_end_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_end_offset);
- ClassDB::bind_method(D_METHOD("animation_track_insert_key", "track_idx", "time", "animation"), &Animation::animation_track_insert_key);
- ClassDB::bind_method(D_METHOD("animation_track_set_key_animation", "track_idx", "key_idx", "animation"), &Animation::animation_track_set_key_animation);
- ClassDB::bind_method(D_METHOD("animation_track_get_key_animation", "track_idx", "key_idx"), &Animation::animation_track_get_key_animation);
- ClassDB::bind_method(D_METHOD("set_length", "time_sec"), &Animation::set_length);
- ClassDB::bind_method(D_METHOD("get_length"), &Animation::get_length);
- ClassDB::bind_method(D_METHOD("set_loop", "enabled"), &Animation::set_loop);
- ClassDB::bind_method(D_METHOD("has_loop"), &Animation::has_loop);
- ClassDB::bind_method(D_METHOD("set_step", "size_sec"), &Animation::set_step);
- ClassDB::bind_method(D_METHOD("get_step"), &Animation::get_step);
- ClassDB::bind_method(D_METHOD("clear"), &Animation::clear);
- ClassDB::bind_method(D_METHOD("copy_track", "track_idx", "to_animation"), &Animation::copy_track);
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "length", PROPERTY_HINT_RANGE, "0.001,99999,0.001"), "set_length", "get_length");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "loop"), "set_loop", "has_loop");
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "step", PROPERTY_HINT_RANGE, "0,4096,0.001"), "set_step", "get_step");
- ADD_SIGNAL(MethodInfo("tracks_changed"));
- BIND_ENUM_CONSTANT(TYPE_VALUE);
- BIND_ENUM_CONSTANT(TYPE_TRANSFORM);
- BIND_ENUM_CONSTANT(TYPE_METHOD);
- BIND_ENUM_CONSTANT(TYPE_BEZIER);
- BIND_ENUM_CONSTANT(TYPE_AUDIO);
- BIND_ENUM_CONSTANT(TYPE_ANIMATION);
- BIND_ENUM_CONSTANT(INTERPOLATION_NEAREST);
- BIND_ENUM_CONSTANT(INTERPOLATION_LINEAR);
- BIND_ENUM_CONSTANT(INTERPOLATION_CUBIC);
- BIND_ENUM_CONSTANT(UPDATE_CONTINUOUS);
- BIND_ENUM_CONSTANT(UPDATE_DISCRETE);
- BIND_ENUM_CONSTANT(UPDATE_TRIGGER);
- BIND_ENUM_CONSTANT(UPDATE_CAPTURE);
- }
- void Animation::clear() {
- for (int i = 0; i < tracks.size(); i++)
- memdelete(tracks[i]);
- tracks.clear();
- loop = false;
- length = 1;
- emit_changed();
- emit_signal(SceneStringNames::get_singleton()->tracks_changed);
- }
- 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) {
- real_t c = (t1.time - t0.time) / (t2.time - t0.time);
- real_t t[3] = { -1, -1, -1 };
- { //translation
- const Vector3 &v0 = t0.value.loc;
- const Vector3 &v1 = t1.value.loc;
- const Vector3 &v2 = t2.value.loc;
- if (v0.is_equal_approx(v2)) {
- //0 and 2 are close, let's see if 1 is close
- if (!v0.is_equal_approx(v1)) {
- //not close, not optimizable
- return false;
- }
- } else {
- Vector3 pd = (v2 - v0);
- float d0 = pd.dot(v0);
- float d1 = pd.dot(v1);
- float d2 = pd.dot(v2);
- if (d1 < d0 || d1 > d2) {
- return false;
- }
- Vector3 s[2] = { v0, v2 };
- real_t d = Geometry::get_closest_point_to_segment(v1, s).distance_to(v1);
- if (d > pd.length() * p_alowed_linear_err) {
- return false; //beyond allowed error for colinearity
- }
- if (p_norm != Vector3() && Math::acos(pd.normalized().dot(p_norm)) > p_alowed_angular_err)
- return false;
- t[0] = (d1 - d0) / (d2 - d0);
- }
- }
- { //rotation
- const Quat &q0 = t0.value.rot;
- const Quat &q1 = t1.value.rot;
- const Quat &q2 = t2.value.rot;
- //localize both to rotation from q0
- if (q0.is_equal_approx(q2)) {
- if (!q0.is_equal_approx(q1))
- return false;
- } else {
- Quat r02 = (q0.inverse() * q2).normalized();
- Quat r01 = (q0.inverse() * q1).normalized();
- Vector3 v02, v01;
- real_t a02, a01;
- r02.get_axis_angle(v02, a02);
- r01.get_axis_angle(v01, a01);
- if (Math::abs(a02) > p_max_optimizable_angle)
- return false;
- if (v01.dot(v02) < 0) {
- //make sure both rotations go the same way to compare
- v02 = -v02;
- a02 = -a02;
- }
- real_t err_01 = Math::acos(v01.normalized().dot(v02.normalized())) / Math_PI;
- if (err_01 > p_alowed_angular_err) {
- //not rotating in the same axis
- return false;
- }
- if (a01 * a02 < 0) {
- //not rotating in the same direction
- return false;
- }
- real_t tr = a01 / a02;
- if (tr < 0 || tr > 1)
- return false; //rotating too much or too less
- t[1] = tr;
- }
- }
- { //scale
- const Vector3 &v0 = t0.value.scale;
- const Vector3 &v1 = t1.value.scale;
- const Vector3 &v2 = t2.value.scale;
- if (v0.is_equal_approx(v2)) {
- //0 and 2 are close, let's see if 1 is close
- if (!v0.is_equal_approx(v1)) {
- //not close, not optimizable
- return false;
- }
- } else {
- Vector3 pd = (v2 - v0);
- float d0 = pd.dot(v0);
- float d1 = pd.dot(v1);
- float d2 = pd.dot(v2);
- if (d1 < d0 || d1 > d2) {
- return false; //beyond segment range
- }
- Vector3 s[2] = { v0, v2 };
- real_t d = Geometry::get_closest_point_to_segment(v1, s).distance_to(v1);
- if (d > pd.length() * p_alowed_linear_err) {
- return false; //beyond allowed error for colinearity
- }
- t[2] = (d1 - d0) / (d2 - d0);
- }
- }
- bool erase = false;
- if (t[0] == -1 && t[1] == -1 && t[2] == -1) {
- erase = true;
- } else {
- erase = true;
- real_t lt = -1;
- for (int j = 0; j < 3; j++) {
- //search for t on first, one must be it
- if (t[j] != -1) {
- lt = t[j]; //official t
- //validate rest
- for (int k = j + 1; k < 3; k++) {
- if (t[k] == -1)
- continue;
- if (Math::abs(lt - t[k]) > p_alowed_linear_err) {
- erase = false;
- break;
- }
- }
- break;
- }
- }
- ERR_FAIL_COND_V(lt == -1, false);
- if (erase) {
- if (Math::abs(lt - c) > p_alowed_linear_err) {
- //todo, evaluate changing the transition if this fails?
- //this could be done as a second pass and would be
- //able to optimize more
- erase = false;
- }
- }
- }
- return erase;
- }
- void Animation::_transform_track_optimize(int p_idx, float p_allowed_linear_err, float p_allowed_angular_err, float p_max_optimizable_angle) {
- ERR_FAIL_INDEX(p_idx, tracks.size());
- ERR_FAIL_COND(tracks[p_idx]->type != TYPE_TRANSFORM);
- TransformTrack *tt = static_cast<TransformTrack *>(tracks[p_idx]);
- bool prev_erased = false;
- TKey<TransformKey> first_erased;
- Vector3 norm;
- for (int i = 1; i < tt->transforms.size() - 1; i++) {
- TKey<TransformKey> &t0 = tt->transforms.write[i - 1];
- TKey<TransformKey> &t1 = tt->transforms.write[i];
- TKey<TransformKey> &t2 = tt->transforms.write[i + 1];
- bool erase = _transform_track_optimize_key(t0, t1, t2, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle, norm);
- if (erase && !prev_erased) {
- norm = (t2.value.loc - t1.value.loc).normalized();
- }
- if (prev_erased && !_transform_track_optimize_key(t0, first_erased, t2, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle, norm)) {
- //avoid error to go beyond first erased key
- erase = false;
- }
- if (erase) {
- if (!prev_erased) {
- first_erased = t1;
- prev_erased = true;
- }
- tt->transforms.remove(i);
- i--;
- } else {
- prev_erased = false;
- norm = Vector3();
- }
- }
- }
- void Animation::optimize(float p_allowed_linear_err, float p_allowed_angular_err, float p_max_optimizable_angle) {
- for (int i = 0; i < tracks.size(); i++) {
- if (tracks[i]->type == TYPE_TRANSFORM)
- _transform_track_optimize(i, p_allowed_linear_err, p_allowed_angular_err, p_max_optimizable_angle);
- }
- }
- Animation::Animation() {
- step = 0.1;
- loop = false;
- length = 1;
- }
- Animation::~Animation() {
- for (int i = 0; i < tracks.size(); i++)
- memdelete(tracks[i]);
- }
|