animation_track_editor.cpp 154 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060
  1. /*************************************************************************/
  2. /* animation_track_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "animation_track_editor.h"
  31. #include "animation_track_editor_plugins.h"
  32. #include "core/os/keyboard.h"
  33. #include "editor/animation_bezier_editor.h"
  34. #include "editor/plugins/animation_player_editor_plugin.h"
  35. #include "editor_node.h"
  36. #include "editor_scale.h"
  37. #include "scene/main/viewport.h"
  38. #include "servers/audio/audio_stream.h"
  39. class AnimationTrackKeyEdit : public Object {
  40. GDCLASS(AnimationTrackKeyEdit, Object);
  41. public:
  42. bool setting;
  43. bool hidden;
  44. bool _hide_script_from_inspector() {
  45. return true;
  46. }
  47. static void _bind_methods() {
  48. ClassDB::bind_method("_update_obj", &AnimationTrackKeyEdit::_update_obj);
  49. ClassDB::bind_method("_key_ofs_changed", &AnimationTrackKeyEdit::_key_ofs_changed);
  50. ClassDB::bind_method("_hide_script_from_inspector", &AnimationTrackKeyEdit::_hide_script_from_inspector);
  51. ClassDB::bind_method("get_root_path", &AnimationTrackKeyEdit::get_root_path);
  52. }
  53. //PopupDialog *ke_dialog;
  54. void _fix_node_path(Variant &value) {
  55. NodePath np = value;
  56. if (np == NodePath())
  57. return;
  58. Node *root = EditorNode::get_singleton()->get_tree()->get_root();
  59. Node *np_node = root->get_node(np);
  60. ERR_FAIL_COND(!np_node);
  61. Node *edited_node = root->get_node(base);
  62. ERR_FAIL_COND(!edited_node);
  63. value = edited_node->get_path_to(np_node);
  64. }
  65. void _update_obj(const Ref<Animation> &p_anim) {
  66. if (setting)
  67. return;
  68. if (hidden)
  69. return;
  70. if (!(animation == p_anim))
  71. return;
  72. notify_change();
  73. }
  74. void _key_ofs_changed(const Ref<Animation> &p_anim, float from, float to) {
  75. if (hidden)
  76. return;
  77. if (!(animation == p_anim))
  78. return;
  79. if (from != key_ofs)
  80. return;
  81. key_ofs = to;
  82. if (setting)
  83. return;
  84. notify_change();
  85. }
  86. bool _set(const StringName &p_name, const Variant &p_value) {
  87. int key = animation->track_find_key(track, key_ofs, true);
  88. ERR_FAIL_COND_V(key == -1, false);
  89. String name = p_name;
  90. if (name == "time") {
  91. float new_time = p_value;
  92. if (new_time == key_ofs)
  93. return true;
  94. int existing = animation->track_find_key(track, new_time, true);
  95. setting = true;
  96. undo_redo->create_action(TTR("Anim Change Keyframe Time"), UndoRedo::MERGE_ENDS);
  97. Variant val = animation->track_get_key_value(track, key);
  98. float trans = animation->track_get_key_transition(track, key);
  99. undo_redo->add_do_method(animation.ptr(), "track_remove_key", track, key);
  100. undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, new_time, val, trans);
  101. undo_redo->add_do_method(this, "_key_ofs_changed", animation, key_ofs, new_time);
  102. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", track, new_time);
  103. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, key_ofs, val, trans);
  104. undo_redo->add_undo_method(this, "_key_ofs_changed", animation, new_time, key_ofs);
  105. if (existing != -1) {
  106. Variant v = animation->track_get_key_value(track, existing);
  107. float trans = animation->track_get_key_transition(track, existing);
  108. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, new_time, v, trans);
  109. }
  110. undo_redo->commit_action();
  111. setting = false;
  112. return true;
  113. } else if (name == "easing") {
  114. float val = p_value;
  115. float prev_val = animation->track_get_key_transition(track, key);
  116. setting = true;
  117. undo_redo->create_action(TTR("Anim Change Transition"), UndoRedo::MERGE_ENDS);
  118. undo_redo->add_do_method(animation.ptr(), "track_set_key_transition", track, key, val);
  119. undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", track, key, prev_val);
  120. undo_redo->add_do_method(this, "_update_obj", animation);
  121. undo_redo->add_undo_method(this, "_update_obj", animation);
  122. undo_redo->commit_action();
  123. setting = false;
  124. return true;
  125. }
  126. switch (animation->track_get_type(track)) {
  127. case Animation::TYPE_TRANSFORM: {
  128. Dictionary d_old = animation->track_get_key_value(track, key);
  129. Dictionary d_new = d_old;
  130. d_new[p_name] = p_value;
  131. setting = true;
  132. undo_redo->create_action(TTR("Anim Change Transform"));
  133. undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, d_new);
  134. undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, d_old);
  135. undo_redo->add_do_method(this, "_update_obj", animation);
  136. undo_redo->add_undo_method(this, "_update_obj", animation);
  137. undo_redo->commit_action();
  138. setting = false;
  139. return true;
  140. } break;
  141. case Animation::TYPE_VALUE: {
  142. if (name == "value") {
  143. Variant value = p_value;
  144. if (value.get_type() == Variant::NODE_PATH) {
  145. _fix_node_path(value);
  146. }
  147. setting = true;
  148. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  149. Variant prev = animation->track_get_key_value(track, key);
  150. undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, value);
  151. undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, prev);
  152. undo_redo->add_do_method(this, "_update_obj", animation);
  153. undo_redo->add_undo_method(this, "_update_obj", animation);
  154. undo_redo->commit_action();
  155. setting = false;
  156. return true;
  157. }
  158. } break;
  159. case Animation::TYPE_METHOD: {
  160. Dictionary d_old = animation->track_get_key_value(track, key);
  161. Dictionary d_new = d_old;
  162. bool change_notify_deserved = false;
  163. bool mergeable = false;
  164. if (name == "name") {
  165. d_new["method"] = p_value;
  166. }
  167. if (name == "arg_count") {
  168. Vector<Variant> args = d_old["args"];
  169. args.resize(p_value);
  170. d_new["args"] = args;
  171. change_notify_deserved = true;
  172. }
  173. if (name.begins_with("args/")) {
  174. Vector<Variant> args = d_old["args"];
  175. int idx = name.get_slice("/", 1).to_int();
  176. ERR_FAIL_INDEX_V(idx, args.size(), false);
  177. String what = name.get_slice("/", 2);
  178. if (what == "type") {
  179. Variant::Type t = Variant::Type(int(p_value));
  180. if (t != args[idx].get_type()) {
  181. Variant::CallError err;
  182. if (Variant::can_convert(args[idx].get_type(), t)) {
  183. Variant old = args[idx];
  184. Variant *ptrs[1] = { &old };
  185. args.write[idx] = Variant::construct(t, (const Variant **)ptrs, 1, err);
  186. } else {
  187. args.write[idx] = Variant::construct(t, NULL, 0, err);
  188. }
  189. change_notify_deserved = true;
  190. d_new["args"] = args;
  191. }
  192. }
  193. if (what == "value") {
  194. Variant value = p_value;
  195. if (value.get_type() == Variant::NODE_PATH) {
  196. _fix_node_path(value);
  197. }
  198. args.write[idx] = value;
  199. d_new["args"] = args;
  200. mergeable = true;
  201. }
  202. }
  203. if (mergeable)
  204. undo_redo->create_action(TTR("Anim Change Call"), UndoRedo::MERGE_ENDS);
  205. else
  206. undo_redo->create_action(TTR("Anim Change Call"));
  207. Variant prev = animation->track_get_key_value(track, key);
  208. setting = true;
  209. undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, d_new);
  210. undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, d_old);
  211. undo_redo->add_do_method(this, "_update_obj", animation);
  212. undo_redo->add_undo_method(this, "_update_obj", animation);
  213. undo_redo->commit_action();
  214. setting = false;
  215. if (change_notify_deserved)
  216. notify_change();
  217. return true;
  218. } break;
  219. case Animation::TYPE_BEZIER: {
  220. if (name == "value") {
  221. Variant value = p_value;
  222. setting = true;
  223. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  224. float prev = animation->bezier_track_get_key_value(track, key);
  225. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_value", track, key, value);
  226. undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_value", track, key, prev);
  227. undo_redo->add_do_method(this, "_update_obj", animation);
  228. undo_redo->add_undo_method(this, "_update_obj", animation);
  229. undo_redo->commit_action();
  230. setting = false;
  231. return true;
  232. }
  233. if (name == "in_handle") {
  234. Variant value = p_value;
  235. setting = true;
  236. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  237. Vector2 prev = animation->bezier_track_get_key_in_handle(track, key);
  238. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_in_handle", track, key, value);
  239. undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_in_handle", track, key, prev);
  240. undo_redo->add_do_method(this, "_update_obj", animation);
  241. undo_redo->add_undo_method(this, "_update_obj", animation);
  242. undo_redo->commit_action();
  243. setting = false;
  244. return true;
  245. }
  246. if (name == "out_handle") {
  247. Variant value = p_value;
  248. setting = true;
  249. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  250. Vector2 prev = animation->bezier_track_get_key_out_handle(track, key);
  251. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_out_handle", track, key, value);
  252. undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_out_handle", track, key, prev);
  253. undo_redo->add_do_method(this, "_update_obj", animation);
  254. undo_redo->add_undo_method(this, "_update_obj", animation);
  255. undo_redo->commit_action();
  256. setting = false;
  257. return true;
  258. }
  259. } break;
  260. case Animation::TYPE_AUDIO: {
  261. if (name == "stream") {
  262. Ref<AudioStream> stream = p_value;
  263. setting = true;
  264. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  265. RES prev = animation->audio_track_get_key_stream(track, key);
  266. undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_stream", track, key, stream);
  267. undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_stream", track, key, prev);
  268. undo_redo->add_do_method(this, "_update_obj", animation);
  269. undo_redo->add_undo_method(this, "_update_obj", animation);
  270. undo_redo->commit_action();
  271. setting = false;
  272. return true;
  273. }
  274. if (name == "start_offset") {
  275. float value = p_value;
  276. setting = true;
  277. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  278. float prev = animation->audio_track_get_key_start_offset(track, key);
  279. undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_start_offset", track, key, value);
  280. undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_start_offset", track, key, prev);
  281. undo_redo->add_do_method(this, "_update_obj", animation);
  282. undo_redo->add_undo_method(this, "_update_obj", animation);
  283. undo_redo->commit_action();
  284. setting = false;
  285. return true;
  286. }
  287. if (name == "end_offset") {
  288. float value = p_value;
  289. setting = true;
  290. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  291. float prev = animation->audio_track_get_key_end_offset(track, key);
  292. undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_end_offset", track, key, value);
  293. undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_end_offset", track, key, prev);
  294. undo_redo->add_do_method(this, "_update_obj", animation);
  295. undo_redo->add_undo_method(this, "_update_obj", animation);
  296. undo_redo->commit_action();
  297. setting = false;
  298. return true;
  299. }
  300. } break;
  301. case Animation::TYPE_ANIMATION: {
  302. if (name == "animation") {
  303. StringName name = p_value;
  304. setting = true;
  305. undo_redo->create_action(TTR("Anim Change Keyframe Value"), UndoRedo::MERGE_ENDS);
  306. StringName prev = animation->animation_track_get_key_animation(track, key);
  307. undo_redo->add_do_method(animation.ptr(), "animation_track_set_key_animation", track, key, name);
  308. undo_redo->add_undo_method(animation.ptr(), "animation_track_set_key_animation", track, key, prev);
  309. undo_redo->add_do_method(this, "_update_obj", animation);
  310. undo_redo->add_undo_method(this, "_update_obj", animation);
  311. undo_redo->commit_action();
  312. setting = false;
  313. return true;
  314. }
  315. } break;
  316. }
  317. return false;
  318. }
  319. bool _get(const StringName &p_name, Variant &r_ret) const {
  320. int key = animation->track_find_key(track, key_ofs, true);
  321. ERR_FAIL_COND_V(key == -1, false);
  322. String name = p_name;
  323. if (name == "time") {
  324. r_ret = key_ofs;
  325. return true;
  326. } else if (name == "easing") {
  327. r_ret = animation->track_get_key_transition(track, key);
  328. return true;
  329. }
  330. switch (animation->track_get_type(track)) {
  331. case Animation::TYPE_TRANSFORM: {
  332. Dictionary d = animation->track_get_key_value(track, key);
  333. ERR_FAIL_COND_V(!d.has(name), false);
  334. r_ret = d[p_name];
  335. return true;
  336. } break;
  337. case Animation::TYPE_VALUE: {
  338. if (name == "value") {
  339. r_ret = animation->track_get_key_value(track, key);
  340. return true;
  341. }
  342. } break;
  343. case Animation::TYPE_METHOD: {
  344. Dictionary d = animation->track_get_key_value(track, key);
  345. if (name == "name") {
  346. ERR_FAIL_COND_V(!d.has("method"), false);
  347. r_ret = d["method"];
  348. return true;
  349. }
  350. ERR_FAIL_COND_V(!d.has("args"), false);
  351. Vector<Variant> args = d["args"];
  352. if (name == "arg_count") {
  353. r_ret = args.size();
  354. return true;
  355. }
  356. if (name.begins_with("args/")) {
  357. int idx = name.get_slice("/", 1).to_int();
  358. ERR_FAIL_INDEX_V(idx, args.size(), false);
  359. String what = name.get_slice("/", 2);
  360. if (what == "type") {
  361. r_ret = args[idx].get_type();
  362. return true;
  363. }
  364. if (what == "value") {
  365. r_ret = args[idx];
  366. return true;
  367. }
  368. }
  369. } break;
  370. case Animation::TYPE_BEZIER: {
  371. if (name == "value") {
  372. r_ret = animation->bezier_track_get_key_value(track, key);
  373. return true;
  374. }
  375. if (name == "in_handle") {
  376. r_ret = animation->bezier_track_get_key_in_handle(track, key);
  377. return true;
  378. }
  379. if (name == "out_handle") {
  380. r_ret = animation->bezier_track_get_key_out_handle(track, key);
  381. return true;
  382. }
  383. } break;
  384. case Animation::TYPE_AUDIO: {
  385. if (name == "stream") {
  386. r_ret = animation->audio_track_get_key_stream(track, key);
  387. return true;
  388. }
  389. if (name == "start_offset") {
  390. r_ret = animation->audio_track_get_key_start_offset(track, key);
  391. return true;
  392. }
  393. if (name == "end_offset") {
  394. r_ret = animation->audio_track_get_key_end_offset(track, key);
  395. return true;
  396. }
  397. } break;
  398. case Animation::TYPE_ANIMATION: {
  399. if (name == "animation") {
  400. r_ret = animation->animation_track_get_key_animation(track, key);
  401. return true;
  402. }
  403. } break;
  404. }
  405. return false;
  406. }
  407. void _get_property_list(List<PropertyInfo> *p_list) const {
  408. if (animation.is_null())
  409. return;
  410. ERR_FAIL_INDEX(track, animation->get_track_count());
  411. int key = animation->track_find_key(track, key_ofs, true);
  412. ERR_FAIL_COND(key == -1);
  413. p_list->push_back(PropertyInfo(Variant::REAL, "time", PROPERTY_HINT_RANGE, "0," + rtos(animation->get_length()) + ",0.01"));
  414. switch (animation->track_get_type(track)) {
  415. case Animation::TYPE_TRANSFORM: {
  416. p_list->push_back(PropertyInfo(Variant::VECTOR3, "location"));
  417. p_list->push_back(PropertyInfo(Variant::QUAT, "rotation"));
  418. p_list->push_back(PropertyInfo(Variant::VECTOR3, "scale"));
  419. } break;
  420. case Animation::TYPE_VALUE: {
  421. Variant v = animation->track_get_key_value(track, key);
  422. if (hint.type != Variant::NIL) {
  423. PropertyInfo pi = hint;
  424. pi.name = "value";
  425. p_list->push_back(pi);
  426. } else {
  427. PropertyHint hint = PROPERTY_HINT_NONE;
  428. String hint_string;
  429. if (v.get_type() == Variant::OBJECT) {
  430. //could actually check the object property if exists..? yes i will!
  431. Ref<Resource> res = v;
  432. if (res.is_valid()) {
  433. hint = PROPERTY_HINT_RESOURCE_TYPE;
  434. hint_string = res->get_class();
  435. }
  436. }
  437. if (v.get_type() != Variant::NIL)
  438. p_list->push_back(PropertyInfo(v.get_type(), "value", hint, hint_string));
  439. }
  440. } break;
  441. case Animation::TYPE_METHOD: {
  442. p_list->push_back(PropertyInfo(Variant::STRING, "name"));
  443. p_list->push_back(PropertyInfo(Variant::INT, "arg_count", PROPERTY_HINT_RANGE, "0,5,1"));
  444. Dictionary d = animation->track_get_key_value(track, key);
  445. ERR_FAIL_COND(!d.has("args"));
  446. Vector<Variant> args = d["args"];
  447. String vtypes;
  448. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  449. if (i > 0)
  450. vtypes += ",";
  451. vtypes += Variant::get_type_name(Variant::Type(i));
  452. }
  453. for (int i = 0; i < args.size(); i++) {
  454. p_list->push_back(PropertyInfo(Variant::INT, "args/" + itos(i) + "/type", PROPERTY_HINT_ENUM, vtypes));
  455. if (args[i].get_type() != Variant::NIL)
  456. p_list->push_back(PropertyInfo(args[i].get_type(), "args/" + itos(i) + "/value"));
  457. }
  458. } break;
  459. case Animation::TYPE_BEZIER: {
  460. p_list->push_back(PropertyInfo(Variant::REAL, "value"));
  461. p_list->push_back(PropertyInfo(Variant::VECTOR2, "in_handle"));
  462. p_list->push_back(PropertyInfo(Variant::VECTOR2, "out_handle"));
  463. } break;
  464. case Animation::TYPE_AUDIO: {
  465. p_list->push_back(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"));
  466. p_list->push_back(PropertyInfo(Variant::REAL, "start_offset", PROPERTY_HINT_RANGE, "0,3600,0.01,or_greater"));
  467. p_list->push_back(PropertyInfo(Variant::REAL, "end_offset", PROPERTY_HINT_RANGE, "0,3600,0.01,or_greater"));
  468. } break;
  469. case Animation::TYPE_ANIMATION: {
  470. String animations;
  471. if (root_path && root_path->has_node(animation->track_get_path(track))) {
  472. AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(root_path->get_node(animation->track_get_path(track)));
  473. if (ap) {
  474. List<StringName> anims;
  475. ap->get_animation_list(&anims);
  476. for (List<StringName>::Element *E = anims.front(); E; E = E->next()) {
  477. if (animations != String()) {
  478. animations += ",";
  479. }
  480. animations += String(E->get());
  481. }
  482. }
  483. }
  484. if (animations != String()) {
  485. animations += ",";
  486. }
  487. animations += "[stop]";
  488. p_list->push_back(PropertyInfo(Variant::STRING, "animation", PROPERTY_HINT_ENUM, animations));
  489. } break;
  490. }
  491. if (animation->track_get_type(track) == Animation::TYPE_VALUE) {
  492. p_list->push_back(PropertyInfo(Variant::REAL, "easing", PROPERTY_HINT_EXP_EASING));
  493. }
  494. }
  495. UndoRedo *undo_redo;
  496. Ref<Animation> animation;
  497. int track;
  498. float key_ofs;
  499. Node *root_path;
  500. PropertyInfo hint;
  501. NodePath base;
  502. void notify_change() {
  503. _change_notify();
  504. }
  505. Node *get_root_path() {
  506. return root_path;
  507. }
  508. AnimationTrackKeyEdit() {
  509. hidden = true;
  510. key_ofs = 0;
  511. track = -1;
  512. setting = false;
  513. root_path = NULL;
  514. }
  515. };
  516. void AnimationTimelineEdit::_zoom_changed(double) {
  517. update();
  518. play_position->update();
  519. emit_signal("zoom_changed");
  520. }
  521. float AnimationTimelineEdit::get_zoom_scale() const {
  522. float zv = zoom->get_value();
  523. if (zv < 1) {
  524. zv = 1.0 - zv;
  525. return Math::pow(1.0f + zv, 8.0f) * 100;
  526. } else {
  527. return 1.0 / Math::pow(zv, 8.0f) * 100;
  528. }
  529. }
  530. void AnimationTimelineEdit::_anim_length_changed(double p_new_len) {
  531. if (editing)
  532. return;
  533. p_new_len = MAX(0.001, p_new_len);
  534. editing = true;
  535. *block_animation_update_ptr = true;
  536. undo_redo->create_action("Change animation length");
  537. undo_redo->add_do_method(animation.ptr(), "set_length", p_new_len);
  538. undo_redo->add_undo_method(animation.ptr(), "set_length", animation->get_length());
  539. undo_redo->commit_action();
  540. *block_animation_update_ptr = false;
  541. editing = false;
  542. update();
  543. emit_signal("length_changed", p_new_len);
  544. }
  545. void AnimationTimelineEdit::_anim_loop_pressed() {
  546. *block_animation_update_ptr = true;
  547. undo_redo->create_action("Change animation loop");
  548. undo_redo->add_do_method(animation.ptr(), "set_loop", loop->is_pressed());
  549. undo_redo->add_undo_method(animation.ptr(), "set_loop", animation->has_loop());
  550. undo_redo->commit_action();
  551. *block_animation_update_ptr = false;
  552. }
  553. int AnimationTimelineEdit::get_buttons_width() const {
  554. Ref<Texture> interp_mode = get_icon("TrackContinuous", "EditorIcons");
  555. Ref<Texture> interp_type = get_icon("InterpRaw", "EditorIcons");
  556. Ref<Texture> loop_type = get_icon("InterpWrapClamp", "EditorIcons");
  557. Ref<Texture> remove_icon = get_icon("Remove", "EditorIcons");
  558. Ref<Texture> down_icon = get_icon("select_arrow", "Tree");
  559. int total_w = interp_mode->get_width() + interp_type->get_width() + loop_type->get_width() + remove_icon->get_width();
  560. total_w += (down_icon->get_width() + 4 * EDSCALE) * 4;
  561. return total_w;
  562. }
  563. int AnimationTimelineEdit::get_name_limit() const {
  564. Ref<Texture> hsize_icon = get_icon("Hsize", "EditorIcons");
  565. int limit = MAX(name_limit, add_track->get_minimum_size().width + hsize_icon->get_width());
  566. limit = MIN(limit, get_size().width - get_buttons_width() - 1);
  567. return limit;
  568. }
  569. void AnimationTimelineEdit::_notification(int p_what) {
  570. if (p_what == NOTIFICATION_ENTER_TREE) {
  571. add_track->set_icon(get_icon("Add", "EditorIcons"));
  572. loop->set_icon(get_icon("Loop", "EditorIcons"));
  573. time_icon->set_texture(get_icon("Time", "EditorIcons"));
  574. add_track->get_popup()->clear();
  575. add_track->get_popup()->add_icon_item(get_icon("KeyValue", "EditorIcons"), TTR("Property Track"));
  576. add_track->get_popup()->add_icon_item(get_icon("KeyXform", "EditorIcons"), TTR("3D Transform Track"));
  577. add_track->get_popup()->add_icon_item(get_icon("KeyCall", "EditorIcons"), TTR("Call Method Track"));
  578. add_track->get_popup()->add_icon_item(get_icon("KeyBezier", "EditorIcons"), TTR("Bezier Curve Track"));
  579. add_track->get_popup()->add_icon_item(get_icon("KeyAudio", "EditorIcons"), TTR("Audio Playback Track"));
  580. add_track->get_popup()->add_icon_item(get_icon("KeyAnimation", "EditorIcons"), TTR("Animation Playback Track"));
  581. }
  582. if (p_what == NOTIFICATION_RESIZED) {
  583. len_hb->set_position(Vector2(get_size().width - get_buttons_width(), 0));
  584. len_hb->set_size(Size2(get_buttons_width(), get_size().height));
  585. }
  586. if (p_what == NOTIFICATION_DRAW) {
  587. int key_range = get_size().width - get_buttons_width() - get_name_limit();
  588. if (!animation.is_valid())
  589. return;
  590. Ref<Font> font = get_font("font", "Label");
  591. Color color = get_color("font_color", "Label");
  592. int zoomw = key_range;
  593. float scale = get_zoom_scale();
  594. int h = get_size().height;
  595. float l = animation->get_length();
  596. if (l <= 0)
  597. l = 0.001; //avoid crashor
  598. Ref<Texture> hsize_icon = get_icon("Hsize", "EditorIcons");
  599. hsize_rect = Rect2(get_name_limit() - hsize_icon->get_width() - 2 * EDSCALE, (get_size().height - hsize_icon->get_height()) / 2, hsize_icon->get_width(), hsize_icon->get_height());
  600. draw_texture(hsize_icon, hsize_rect.position);
  601. {
  602. float time_min = 0;
  603. float time_max = animation->get_length();
  604. for (int i = 0; i < animation->get_track_count(); i++) {
  605. if (animation->track_get_key_count(i) > 0) {
  606. float beg = animation->track_get_key_time(i, 0);
  607. /*if (animation->track_get_type(i) == Animation::TYPE_BEZIER) {
  608. beg += animation->bezier_track_get_key_in_handle(i, 0).x;
  609. }* not worth it since they have no use */
  610. if (beg < time_min)
  611. time_min = beg;
  612. float end = animation->track_get_key_time(i, animation->track_get_key_count(i) - 1);
  613. /*if (animation->track_get_type(i) == Animation::TYPE_BEZIER) {
  614. end += animation->bezier_track_get_key_out_handle(i, animation->track_get_key_count(i) - 1).x;
  615. } not worth it since they have no use */
  616. if (end > time_max)
  617. time_max = end;
  618. }
  619. }
  620. float extra = (zoomw / scale) * 0.5;
  621. //if (time_min < -0.001)
  622. // time_min -= extra;
  623. time_max += extra;
  624. set_min(time_min);
  625. set_max(time_max);
  626. if (zoomw / scale < (time_max - time_min)) {
  627. hscroll->show();
  628. } else {
  629. hscroll->hide();
  630. }
  631. }
  632. set_page(zoomw / scale);
  633. int end_px = (l - get_value()) * scale;
  634. int begin_px = -get_value() * scale;
  635. Color notimecol = get_color("dark_color_2", "Editor");
  636. Color timecolor = color;
  637. timecolor.a = 0.2;
  638. Color linecolor = color;
  639. linecolor.a = 0.2;
  640. {
  641. draw_rect(Rect2(Point2(get_name_limit(), 0), Point2(zoomw - 1, h)), notimecol);
  642. if (begin_px < zoomw && end_px > 0) {
  643. if (begin_px < 0)
  644. begin_px = 0;
  645. if (end_px > zoomw)
  646. end_px = zoomw;
  647. draw_rect(Rect2(Point2(get_name_limit() + begin_px, 0), Point2(end_px - begin_px - 1, h)), timecolor);
  648. }
  649. }
  650. Color color_time_sec = color;
  651. Color color_time_dec = color;
  652. color_time_dec.a *= 0.5;
  653. #define SC_ADJ 100
  654. int min = 30;
  655. int dec = 1;
  656. int step = 1;
  657. int decimals = 2;
  658. bool step_found = false;
  659. const int period_width = font->get_char_size('.').width;
  660. int max_digit_width = font->get_char_size('0').width;
  661. for (int i = 1; i <= 9; i++) {
  662. const int digit_width = font->get_char_size('0' + i).width;
  663. max_digit_width = MAX(digit_width, max_digit_width);
  664. }
  665. const int max_sc = int(Math::ceil(zoomw / scale));
  666. const int max_sc_width = String::num(max_sc).length() * max_digit_width;
  667. while (!step_found) {
  668. min = max_sc_width;
  669. if (decimals > 0)
  670. min += period_width + max_digit_width * decimals;
  671. static const int _multp[3] = { 1, 2, 5 };
  672. for (int i = 0; i < 3; i++) {
  673. step = (_multp[i] * dec);
  674. if (step * scale / SC_ADJ > min) {
  675. step_found = true;
  676. break;
  677. }
  678. }
  679. if (step_found)
  680. break;
  681. dec *= 10;
  682. decimals--;
  683. if (decimals < 0)
  684. decimals = 0;
  685. }
  686. for (int i = 0; i < zoomw; i++) {
  687. float pos = get_value() + double(i) / scale;
  688. float prev = get_value() + (double(i) - 1.0) / scale;
  689. int sc = int(Math::floor(pos * SC_ADJ));
  690. int prev_sc = int(Math::floor(prev * SC_ADJ));
  691. bool sub = (sc % SC_ADJ);
  692. if ((sc / step) != (prev_sc / step) || (prev_sc < 0 && sc >= 0)) {
  693. int scd = sc < 0 ? prev_sc : sc;
  694. draw_line(Point2(get_name_limit() + i, 0), Point2(get_name_limit() + i, h), linecolor);
  695. draw_string(font, Point2(get_name_limit() + i + 3, (h - font->get_height()) / 2 + font->get_ascent()).floor(), String::num((scd - (scd % step)) / double(SC_ADJ), decimals), sub ? color_time_dec : color_time_sec, zoomw - i);
  696. }
  697. }
  698. draw_line(Vector2(0, get_size().height), get_size(), linecolor);
  699. }
  700. }
  701. void AnimationTimelineEdit::set_animation(const Ref<Animation> &p_animation) {
  702. animation = p_animation;
  703. if (animation.is_valid()) {
  704. len_hb->show();
  705. add_track->show();
  706. play_position->show();
  707. } else {
  708. len_hb->hide();
  709. add_track->hide();
  710. play_position->hide();
  711. }
  712. update();
  713. update_values();
  714. }
  715. Size2 AnimationTimelineEdit::get_minimum_size() const {
  716. Size2 ms = add_track->get_minimum_size();
  717. Ref<Font> font = get_font("font", "Label");
  718. ms.height = MAX(ms.height, font->get_height());
  719. ms.width = get_buttons_width() + add_track->get_minimum_size().width + get_icon("Hsize", "EditorIcons")->get_width() + 2;
  720. return ms;
  721. }
  722. void AnimationTimelineEdit::set_block_animation_update_ptr(bool *p_block_ptr) {
  723. block_animation_update_ptr = p_block_ptr;
  724. }
  725. void AnimationTimelineEdit::set_undo_redo(UndoRedo *p_undo_redo) {
  726. undo_redo = p_undo_redo;
  727. }
  728. void AnimationTimelineEdit::set_zoom(Range *p_zoom) {
  729. zoom = p_zoom;
  730. zoom->connect("value_changed", this, "_zoom_changed");
  731. }
  732. void AnimationTimelineEdit::set_play_position(float p_pos) {
  733. play_position_pos = p_pos;
  734. play_position->update();
  735. }
  736. float AnimationTimelineEdit::get_play_position() const {
  737. return play_position_pos;
  738. }
  739. void AnimationTimelineEdit::update_play_position() {
  740. play_position->update();
  741. }
  742. void AnimationTimelineEdit::update_values() {
  743. if (!animation.is_valid() || editing)
  744. return;
  745. editing = true;
  746. length->set_value(animation->get_length());
  747. loop->set_pressed(animation->has_loop());
  748. editing = false;
  749. }
  750. void AnimationTimelineEdit::_play_position_draw() {
  751. if (!animation.is_valid() || play_position_pos < 0)
  752. return;
  753. float scale = get_zoom_scale();
  754. int h = play_position->get_size().height;
  755. int px = (-get_value() + play_position_pos) * scale + get_name_limit();
  756. if (px >= get_name_limit() && px < (play_position->get_size().width - get_buttons_width())) {
  757. Color color = get_color("accent_color", "Editor");
  758. play_position->draw_line(Point2(px, 0), Point2(px, h), color);
  759. }
  760. }
  761. void AnimationTimelineEdit::_gui_input(const Ref<InputEvent> &p_event) {
  762. Ref<InputEventMouseButton> mb = p_event;
  763. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && hsize_rect.has_point(mb->get_position())) {
  764. dragging_hsize = true;
  765. dragging_hsize_from = mb->get_position().x;
  766. dragging_hsize_at = name_limit;
  767. }
  768. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && dragging_hsize) {
  769. dragging_hsize = false;
  770. }
  771. if (mb.is_valid() && mb->get_position().x > get_name_limit() && mb->get_position().x < (get_size().width - get_buttons_width())) {
  772. if (!panning_timeline && mb->get_button_index() == BUTTON_LEFT) {
  773. int x = mb->get_position().x - get_name_limit();
  774. float ofs = x / get_zoom_scale() + get_value();
  775. emit_signal("timeline_changed", ofs, false);
  776. dragging_timeline = true;
  777. }
  778. if (!dragging_timeline && mb->get_button_index() == BUTTON_MIDDLE) {
  779. int x = mb->get_position().x - get_name_limit();
  780. panning_timeline_from = x / get_zoom_scale();
  781. panning_timeline = true;
  782. panning_timeline_at = get_value();
  783. }
  784. }
  785. if (dragging_timeline && mb.is_valid() && mb->get_button_index() == BUTTON_LEFT && !mb->is_pressed()) {
  786. dragging_timeline = false;
  787. }
  788. if (panning_timeline && mb.is_valid() && mb->get_button_index() == BUTTON_MIDDLE && !mb->is_pressed()) {
  789. panning_timeline = false;
  790. }
  791. Ref<InputEventMouseMotion> mm = p_event;
  792. if (mm.is_valid()) {
  793. if (dragging_hsize) {
  794. int ofs = mm->get_position().x - dragging_hsize_from;
  795. name_limit = dragging_hsize_at + ofs;
  796. update();
  797. emit_signal("name_limit_changed");
  798. play_position->update();
  799. }
  800. if (dragging_timeline) {
  801. int x = mm->get_position().x - get_name_limit();
  802. float ofs = x / get_zoom_scale() + get_value();
  803. emit_signal("timeline_changed", ofs, false);
  804. }
  805. if (panning_timeline) {
  806. int x = mm->get_position().x - get_name_limit();
  807. float ofs = x / get_zoom_scale();
  808. float diff = ofs - panning_timeline_from;
  809. set_value(panning_timeline_at - diff);
  810. }
  811. }
  812. }
  813. void AnimationTimelineEdit::set_hscroll(HScrollBar *p_hscroll) {
  814. hscroll = p_hscroll;
  815. }
  816. void AnimationTimelineEdit::_track_added(int p_track) {
  817. emit_signal("track_added", p_track);
  818. }
  819. void AnimationTimelineEdit::_bind_methods() {
  820. ClassDB::bind_method("_zoom_changed", &AnimationTimelineEdit::_zoom_changed);
  821. ClassDB::bind_method("_anim_length_changed", &AnimationTimelineEdit::_anim_length_changed);
  822. ClassDB::bind_method("_anim_loop_pressed", &AnimationTimelineEdit::_anim_loop_pressed);
  823. ClassDB::bind_method("_play_position_draw", &AnimationTimelineEdit::_play_position_draw);
  824. ClassDB::bind_method("_gui_input", &AnimationTimelineEdit::_gui_input);
  825. ClassDB::bind_method("_track_added", &AnimationTimelineEdit::_track_added);
  826. ADD_SIGNAL(MethodInfo("zoom_changed"));
  827. ADD_SIGNAL(MethodInfo("name_limit_changed"));
  828. ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::REAL, "position"), PropertyInfo(Variant::BOOL, "drag")));
  829. ADD_SIGNAL(MethodInfo("track_added", PropertyInfo(Variant::INT, "track")));
  830. ADD_SIGNAL(MethodInfo("length_changed", PropertyInfo(Variant::REAL, "size")));
  831. }
  832. AnimationTimelineEdit::AnimationTimelineEdit() {
  833. block_animation_update_ptr = NULL;
  834. editing = false;
  835. name_limit = 150;
  836. zoom = NULL;
  837. play_position_pos = 0;
  838. play_position = memnew(Control);
  839. play_position->set_mouse_filter(MOUSE_FILTER_PASS);
  840. add_child(play_position);
  841. play_position->set_anchors_and_margins_preset(PRESET_WIDE);
  842. play_position->connect("draw", this, "_play_position_draw");
  843. add_track = memnew(MenuButton);
  844. add_track->set_position(Vector2(0, 0));
  845. add_child(add_track);
  846. add_track->set_text(TTR("Add Track"));
  847. len_hb = memnew(HBoxContainer);
  848. Control *expander = memnew(Control);
  849. expander->set_h_size_flags(SIZE_EXPAND_FILL);
  850. len_hb->add_child(expander);
  851. time_icon = memnew(TextureRect);
  852. time_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
  853. time_icon->set_tooltip(TTR("Animation Length Time (seconds)"));
  854. len_hb->add_child(time_icon);
  855. length = memnew(EditorSpinSlider);
  856. length->set_min(0.001);
  857. length->set_max(3600);
  858. length->set_step(0.01);
  859. length->set_allow_greater(true);
  860. length->set_custom_minimum_size(Vector2(70 * EDSCALE, 0));
  861. length->set_hide_slider(true);
  862. length->set_tooltip(TTR("Animation Length Time (seconds)"));
  863. length->connect("value_changed", this, "_anim_length_changed");
  864. len_hb->add_child(length);
  865. loop = memnew(ToolButton);
  866. loop->set_tooltip(TTR("Animation Looping"));
  867. loop->connect("pressed", this, "_anim_loop_pressed");
  868. loop->set_toggle_mode(true);
  869. len_hb->add_child(loop);
  870. add_child(len_hb);
  871. add_track->hide();
  872. add_track->get_popup()->connect("index_pressed", this, "_track_added");
  873. len_hb->hide();
  874. panning_timeline = false;
  875. dragging_timeline = false;
  876. dragging_hsize = false;
  877. }
  878. ////////////////////////////////////
  879. void AnimationTrackEdit::_notification(int p_what) {
  880. if (p_what == NOTIFICATION_DRAW) {
  881. if (animation.is_null())
  882. return;
  883. ERR_FAIL_INDEX(track, animation->get_track_count());
  884. int limit = timeline->get_name_limit();
  885. if (has_focus()) {
  886. Color accent = get_color("accent_color", "Editor");
  887. accent.a *= 0.7;
  888. draw_rect(Rect2(Point2(), get_size()), accent, false);
  889. }
  890. Ref<Font> font = get_font("font", "Label");
  891. Color color = get_color("font_color", "Label");
  892. Ref<Texture> type_icons[6] = {
  893. get_icon("KeyValue", "EditorIcons"),
  894. get_icon("KeyXform", "EditorIcons"),
  895. get_icon("KeyCall", "EditorIcons"),
  896. get_icon("KeyBezier", "EditorIcons"),
  897. get_icon("KeyAudio", "EditorIcons"),
  898. get_icon("KeyAnimation", "EditorIcons")
  899. };
  900. int hsep = get_constant("hseparation", "ItemList");
  901. Color linecolor = color;
  902. linecolor.a = 0.2;
  903. // NAMES AND ICONS //
  904. {
  905. Ref<Texture> check = animation->track_is_enabled(track) ? get_icon("checked", "CheckBox") : get_icon("unchecked", "CheckBox");
  906. int ofs = in_group ? check->get_width() : 0; //not the best reference for margin but..
  907. check_rect = Rect2(Point2(ofs, int(get_size().height - check->get_height()) / 2), check->get_size());
  908. draw_texture(check, check_rect.position);
  909. ofs += check->get_width() + hsep;
  910. Ref<Texture> type_icon = type_icons[animation->track_get_type(track)];
  911. draw_texture(type_icon, Point2(ofs, int(get_size().height - type_icon->get_height()) / 2));
  912. ofs += type_icon->get_width() + hsep;
  913. NodePath path = animation->track_get_path(track);
  914. Node *node = NULL;
  915. if (root && root->has_node(path)) {
  916. node = root->get_node(path);
  917. }
  918. String text;
  919. Color text_color = color;
  920. if (node && EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
  921. text_color = get_color("accent_color", "Editor");
  922. }
  923. if (in_group) {
  924. if (animation->track_get_type(track) == Animation::TYPE_METHOD) {
  925. text = TTR("Functions:");
  926. } else if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
  927. text = TTR("Audio Clips:");
  928. } else if (animation->track_get_type(track) == Animation::TYPE_ANIMATION) {
  929. text = TTR("Anim Clips:");
  930. } else {
  931. Vector<StringName> sn = path.get_subnames();
  932. for (int i = 0; i < sn.size(); i++) {
  933. if (i > 0) {
  934. text += ".";
  935. }
  936. text += sn[i];
  937. }
  938. }
  939. text_color.a *= 0.7;
  940. } else if (node) {
  941. Ref<Texture> icon;
  942. if (has_icon(node->get_class(), "EditorIcons")) {
  943. icon = get_icon(node->get_class(), "EditorIcons");
  944. } else {
  945. icon = get_icon("Node", "EditorIcons");
  946. }
  947. draw_texture(icon, Point2(ofs, int(get_size().height - icon->get_height()) / 2));
  948. icon_cache = icon;
  949. text = node->get_name();
  950. ofs += hsep;
  951. ofs += icon->get_width();
  952. Vector<StringName> sn = path.get_subnames();
  953. for (int i = 0; i < sn.size(); i++) {
  954. text += ".";
  955. text += sn[i];
  956. }
  957. } else {
  958. icon_cache = type_icon;
  959. text = path;
  960. }
  961. path_cache = text;
  962. path_rect = Rect2(ofs, 0, limit - ofs - hsep, get_size().height);
  963. Vector2 string_pos = Point2(ofs, (get_size().height - font->get_height()) / 2 + font->get_ascent());
  964. string_pos = string_pos.floor();
  965. draw_string(font, string_pos, text, text_color, limit - ofs - hsep);
  966. draw_line(Point2(limit, 0), Point2(limit, get_size().height), linecolor);
  967. }
  968. // KEYFAMES //
  969. draw_bg(limit, get_size().width - timeline->get_buttons_width());
  970. {
  971. float scale = timeline->get_zoom_scale();
  972. int limit_end = get_size().width - timeline->get_buttons_width();
  973. for (int i = 0; i < animation->track_get_key_count(track); i++) {
  974. float offset = animation->track_get_key_time(track, i) - timeline->get_value();
  975. if (editor->is_key_selected(track, i) && editor->is_moving_selection()) {
  976. offset = editor->snap_time(offset + editor->get_moving_selection_offset());
  977. }
  978. offset = offset * scale + limit;
  979. if (i < animation->track_get_key_count(track) - 1) {
  980. float offset_n = animation->track_get_key_time(track, i + 1) - timeline->get_value();
  981. if (editor->is_key_selected(track, i + 1) && editor->is_moving_selection()) {
  982. offset_n = editor->snap_time(offset_n + editor->get_moving_selection_offset());
  983. }
  984. offset_n = offset_n * scale + limit;
  985. draw_key_link(i, scale, int(offset), int(offset_n), limit, limit_end);
  986. }
  987. draw_key(i, scale, int(offset), editor->is_key_selected(track, i), limit, limit_end);
  988. }
  989. }
  990. draw_fg(limit, get_size().width - timeline->get_buttons_width());
  991. // BUTTONS //
  992. {
  993. Ref<Texture> wrap_icon[2] = {
  994. get_icon("InterpWrapClamp", "EditorIcons"),
  995. get_icon("InterpWrapLoop", "EditorIcons"),
  996. };
  997. Ref<Texture> interp_icon[3] = {
  998. get_icon("InterpRaw", "EditorIcons"),
  999. get_icon("InterpLinear", "EditorIcons"),
  1000. get_icon("InterpCubic", "EditorIcons")
  1001. };
  1002. Ref<Texture> cont_icon[4] = {
  1003. get_icon("TrackContinuous", "EditorIcons"),
  1004. get_icon("TrackDiscrete", "EditorIcons"),
  1005. get_icon("TrackTrigger", "EditorIcons"),
  1006. get_icon("TrackCapture", "EditorIcons")
  1007. };
  1008. int ofs = get_size().width - timeline->get_buttons_width();
  1009. Ref<Texture> down_icon = get_icon("select_arrow", "Tree");
  1010. draw_line(Point2(ofs, 0), Point2(ofs, get_size().height), linecolor);
  1011. ofs += hsep;
  1012. {
  1013. //callmode
  1014. Animation::UpdateMode update_mode;
  1015. if (animation->track_get_type(track) == Animation::TYPE_VALUE) {
  1016. update_mode = animation->value_track_get_update_mode(track);
  1017. } else {
  1018. update_mode = Animation::UPDATE_CONTINUOUS;
  1019. }
  1020. Ref<Texture> update_icon = cont_icon[update_mode];
  1021. update_mode_rect.position.x = ofs;
  1022. update_mode_rect.position.y = int(get_size().height - update_icon->get_height()) / 2;
  1023. update_mode_rect.size = update_icon->get_size();
  1024. if (animation->track_get_type(track) == Animation::TYPE_VALUE) {
  1025. draw_texture(update_icon, update_mode_rect.position);
  1026. }
  1027. //make it easier to click
  1028. update_mode_rect.position.y = 0;
  1029. update_mode_rect.size.y = get_size().height;
  1030. ofs += update_icon->get_width() + hsep;
  1031. update_mode_rect.size.x += hsep;
  1032. if (animation->track_get_type(track) == Animation::TYPE_VALUE) {
  1033. draw_texture(down_icon, Vector2(ofs, int(get_size().height - down_icon->get_height()) / 2));
  1034. update_mode_rect.size.x += down_icon->get_width();
  1035. bezier_edit_rect = Rect2();
  1036. } else if (animation->track_get_type(track) == Animation::TYPE_BEZIER) {
  1037. Ref<Texture> bezier_icon = get_icon("EditBezier", "EditorIcons");
  1038. update_mode_rect.size.x += down_icon->get_width();
  1039. bezier_edit_rect.position = update_mode_rect.position + (update_mode_rect.size - bezier_icon->get_size()) / 2;
  1040. bezier_edit_rect.size = bezier_icon->get_size();
  1041. draw_texture(bezier_icon, bezier_edit_rect.position);
  1042. update_mode_rect = Rect2();
  1043. } else {
  1044. update_mode_rect = Rect2();
  1045. bezier_edit_rect = Rect2();
  1046. }
  1047. ofs += down_icon->get_width();
  1048. draw_line(Point2(ofs + hsep * 0.5, 0), Point2(ofs + hsep * 0.5, get_size().height), linecolor);
  1049. ofs += hsep;
  1050. }
  1051. {
  1052. //interp
  1053. Animation::InterpolationType interp_mode = animation->track_get_interpolation_type(track);
  1054. Ref<Texture> icon = interp_icon[interp_mode];
  1055. interp_mode_rect.position.x = ofs;
  1056. interp_mode_rect.position.y = int(get_size().height - icon->get_height()) / 2;
  1057. interp_mode_rect.size = icon->get_size();
  1058. if (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_TRANSFORM) {
  1059. draw_texture(icon, interp_mode_rect.position);
  1060. }
  1061. //make it easier to click
  1062. interp_mode_rect.position.y = 0;
  1063. interp_mode_rect.size.y = get_size().height;
  1064. ofs += icon->get_width() + hsep;
  1065. interp_mode_rect.size.x += hsep;
  1066. if (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_TRANSFORM) {
  1067. draw_texture(down_icon, Vector2(ofs, int(get_size().height - down_icon->get_height()) / 2));
  1068. interp_mode_rect.size.x += down_icon->get_width();
  1069. } else {
  1070. interp_mode_rect = Rect2();
  1071. }
  1072. ofs += down_icon->get_width();
  1073. draw_line(Point2(ofs + hsep * 0.5, 0), Point2(ofs + hsep * 0.5, get_size().height), linecolor);
  1074. ofs += hsep;
  1075. }
  1076. {
  1077. //loop
  1078. bool loop_wrap = animation->track_get_interpolation_loop_wrap(track);
  1079. Ref<Texture> icon = wrap_icon[loop_wrap ? 1 : 0];
  1080. loop_mode_rect.position.x = ofs;
  1081. loop_mode_rect.position.y = int(get_size().height - icon->get_height()) / 2;
  1082. loop_mode_rect.size = icon->get_size();
  1083. if (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_TRANSFORM) {
  1084. draw_texture(icon, loop_mode_rect.position);
  1085. }
  1086. loop_mode_rect.position.y = 0;
  1087. loop_mode_rect.size.y = get_size().height;
  1088. ofs += icon->get_width() + hsep;
  1089. loop_mode_rect.size.x += hsep;
  1090. if (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_TRANSFORM) {
  1091. draw_texture(down_icon, Vector2(ofs, int(get_size().height - down_icon->get_height()) / 2));
  1092. loop_mode_rect.size.x += down_icon->get_width();
  1093. } else {
  1094. loop_mode_rect = Rect2();
  1095. }
  1096. ofs += down_icon->get_width();
  1097. draw_line(Point2(ofs + hsep * 0.5, 0), Point2(ofs + hsep * 0.5, get_size().height), linecolor);
  1098. ofs += hsep;
  1099. }
  1100. {
  1101. //erase
  1102. Ref<Texture> icon = get_icon("Remove", "EditorIcons");
  1103. remove_rect.position.x = ofs + ((get_size().width - ofs) - icon->get_width()) / 2;
  1104. remove_rect.position.y = int(get_size().height - icon->get_height()) / 2;
  1105. remove_rect.size = icon->get_size();
  1106. draw_texture(icon, remove_rect.position);
  1107. }
  1108. }
  1109. if (in_group) {
  1110. draw_line(Vector2(timeline->get_name_limit(), get_size().height), get_size(), linecolor);
  1111. } else {
  1112. draw_line(Vector2(0, get_size().height), get_size(), linecolor);
  1113. }
  1114. if (dropping_at != 0) {
  1115. Color drop_color = get_color("accent_color", "Editor");
  1116. if (dropping_at < 0) {
  1117. draw_line(Vector2(0, 0), Vector2(get_size().width, 0), drop_color);
  1118. } else {
  1119. draw_line(Vector2(0, get_size().height), get_size(), drop_color);
  1120. }
  1121. }
  1122. }
  1123. if (p_what == NOTIFICATION_MOUSE_EXIT || p_what == NOTIFICATION_DRAG_END) {
  1124. cancel_drop();
  1125. }
  1126. }
  1127. int AnimationTrackEdit::get_key_height() const {
  1128. if (!animation.is_valid())
  1129. return 0;
  1130. return type_icon->get_height();
  1131. }
  1132. Rect2 AnimationTrackEdit::get_key_rect(int p_index, float p_pixels_sec) {
  1133. if (!animation.is_valid())
  1134. return Rect2();
  1135. Rect2 rect = Rect2(-type_icon->get_width() / 2, 0, type_icon->get_width(), get_size().height);
  1136. //make it a big easier to click
  1137. rect.position.x -= rect.size.x * 0.5;
  1138. rect.size.x *= 2;
  1139. return rect;
  1140. }
  1141. bool AnimationTrackEdit::is_key_selectable_by_distance() const {
  1142. return true;
  1143. }
  1144. void AnimationTrackEdit::draw_key_link(int p_index, float p_pixels_sec, int p_x, int p_next_x, int p_clip_left, int p_clip_right) {
  1145. if (p_next_x < p_clip_left)
  1146. return;
  1147. if (p_x > p_clip_right)
  1148. return;
  1149. Variant current = animation->track_get_key_value(get_track(), p_index);
  1150. Variant next = animation->track_get_key_value(get_track(), p_index + 1);
  1151. if (current != next)
  1152. return;
  1153. Color color = get_color("font_color", "Label");
  1154. color.a = 0.5;
  1155. int from_x = MAX(p_x, p_clip_left);
  1156. int to_x = MIN(p_next_x, p_clip_right);
  1157. draw_line(Point2(from_x + 1, get_size().height / 2), Point2(to_x, get_size().height / 2), color, 2);
  1158. }
  1159. void AnimationTrackEdit::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
  1160. if (!animation.is_valid())
  1161. return;
  1162. if (p_x < p_clip_left || p_x > p_clip_right)
  1163. return;
  1164. Vector2 ofs(p_x - type_icon->get_width() / 2, int(get_size().height - type_icon->get_height()) / 2);
  1165. if (animation->track_get_type(track) == Animation::TYPE_METHOD) {
  1166. Ref<Font> font = get_font("font", "Label");
  1167. Color color = get_color("font_color", "Label");
  1168. color.a = 0.5;
  1169. Dictionary d = animation->track_get_key_value(track, p_index);
  1170. String text;
  1171. if (d.has("method"))
  1172. text += String(d["method"]);
  1173. text += "(";
  1174. Vector<Variant> args;
  1175. if (d.has("args"))
  1176. args = d["args"];
  1177. for (int i = 0; i < args.size(); i++) {
  1178. if (i > 0)
  1179. text += ", ";
  1180. text += String(args[i]);
  1181. }
  1182. text += ")";
  1183. int limit = MAX(0, p_clip_right - p_x - type_icon->get_width());
  1184. if (limit > 0) {
  1185. draw_string(font, Vector2(p_x + type_icon->get_width(), int(get_size().height - font->get_height()) / 2 + font->get_ascent()), text, color, limit);
  1186. }
  1187. }
  1188. if (p_selected) {
  1189. draw_texture(selected_icon, ofs);
  1190. } else {
  1191. draw_texture(type_icon, ofs);
  1192. }
  1193. }
  1194. //helper
  1195. void AnimationTrackEdit::draw_rect_clipped(const Rect2 &p_rect, const Color &p_color, bool p_filled) {
  1196. int clip_left = timeline->get_name_limit();
  1197. int clip_right = get_size().width - timeline->get_buttons_width();
  1198. if (p_rect.position.x > clip_right)
  1199. return;
  1200. if (p_rect.position.x + p_rect.size.x < clip_left)
  1201. return;
  1202. Rect2 clip = Rect2(clip_left, 0, clip_right - clip_left, get_size().height);
  1203. draw_rect(clip.clip(p_rect), p_color, p_filled);
  1204. }
  1205. void AnimationTrackEdit::draw_bg(int p_clip_left, int p_clip_right) {
  1206. }
  1207. void AnimationTrackEdit::draw_fg(int p_clip_left, int p_clip_right) {
  1208. }
  1209. void AnimationTrackEdit::draw_texture_clipped(const Ref<Texture> &p_texture, const Vector2 &p_pos) {
  1210. draw_texture_region_clipped(p_texture, Rect2(p_pos, p_texture->get_size()), Rect2(Point2(), p_texture->get_size()));
  1211. }
  1212. void AnimationTrackEdit::draw_texture_region_clipped(const Ref<Texture> &p_texture, const Rect2 &p_rect, const Rect2 &p_region) {
  1213. int clip_left = timeline->get_name_limit();
  1214. int clip_right = get_size().width - timeline->get_buttons_width();
  1215. //clip left and right
  1216. if (clip_left > p_rect.position.x + p_rect.size.x)
  1217. return;
  1218. if (clip_right < p_rect.position.x)
  1219. return;
  1220. Rect2 rect = p_rect;
  1221. Rect2 region = p_region;
  1222. if (clip_left > rect.position.x) {
  1223. int rect_pixels = (clip_left - rect.position.x);
  1224. int region_pixels = rect_pixels * region.size.x / rect.size.x;
  1225. rect.position.x += rect_pixels;
  1226. rect.size.x -= rect_pixels;
  1227. region.position.x += region_pixels;
  1228. region.size.x -= region_pixels;
  1229. }
  1230. if (clip_right < rect.position.x + rect.size.x) {
  1231. int rect_pixels = rect.position.x + rect.size.x - clip_right;
  1232. int region_pixels = rect_pixels * region.size.x / rect.size.x;
  1233. rect.size.x -= rect_pixels;
  1234. region.size.x -= region_pixels;
  1235. }
  1236. draw_texture_rect_region(p_texture, rect, region);
  1237. }
  1238. int AnimationTrackEdit::get_track() const {
  1239. return track;
  1240. }
  1241. Ref<Animation> AnimationTrackEdit::get_animation() const {
  1242. return animation;
  1243. }
  1244. void AnimationTrackEdit::set_animation_and_track(const Ref<Animation> &p_animation, int p_track) {
  1245. animation = p_animation;
  1246. track = p_track;
  1247. update();
  1248. Ref<Texture> type_icons[6] = {
  1249. get_icon("KeyValue", "EditorIcons"),
  1250. get_icon("KeyXform", "EditorIcons"),
  1251. get_icon("KeyCall", "EditorIcons"),
  1252. get_icon("KeyBezier", "EditorIcons"),
  1253. get_icon("KeyAudio", "EditorIcons"),
  1254. get_icon("KeyAnimation", "EditorIcons")
  1255. };
  1256. ERR_FAIL_INDEX(track, animation->get_track_count());
  1257. type_icon = type_icons[animation->track_get_type(track)];
  1258. selected_icon = get_icon("KeySelected", "EditorIcons");
  1259. }
  1260. Size2 AnimationTrackEdit::get_minimum_size() const {
  1261. Ref<Texture> texture = get_icon("Object", "EditorIcons");
  1262. Ref<Font> font = get_font("font", "Label");
  1263. int separation = get_constant("vseparation", "ItemList");
  1264. int max_h = MAX(texture->get_height(), font->get_height());
  1265. max_h = MAX(max_h, get_key_height());
  1266. return Vector2(1, max_h + separation);
  1267. }
  1268. void AnimationTrackEdit::set_undo_redo(UndoRedo *p_undo_redo) {
  1269. undo_redo = p_undo_redo;
  1270. }
  1271. void AnimationTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) {
  1272. timeline = p_timeline;
  1273. timeline->connect("zoom_changed", this, "_zoom_changed");
  1274. timeline->connect("name_limit_changed", this, "_zoom_changed");
  1275. }
  1276. void AnimationTrackEdit::set_editor(AnimationTrackEditor *p_editor) {
  1277. editor = p_editor;
  1278. }
  1279. void AnimationTrackEdit::_play_position_draw() {
  1280. if (!animation.is_valid() || play_position_pos < 0)
  1281. return;
  1282. float scale = timeline->get_zoom_scale();
  1283. int h = get_size().height;
  1284. int px = (-timeline->get_value() + play_position_pos) * scale + timeline->get_name_limit();
  1285. if (px >= timeline->get_name_limit() && px < (get_size().width - timeline->get_buttons_width())) {
  1286. Color color = get_color("accent_color", "Editor");
  1287. play_position->draw_line(Point2(px, 0), Point2(px, h), color);
  1288. }
  1289. }
  1290. void AnimationTrackEdit::set_play_position(float p_pos) {
  1291. play_position_pos = p_pos;
  1292. play_position->update();
  1293. }
  1294. void AnimationTrackEdit::update_play_position() {
  1295. play_position->update();
  1296. }
  1297. void AnimationTrackEdit::set_root(Node *p_root) {
  1298. root = p_root;
  1299. }
  1300. void AnimationTrackEdit::_zoom_changed() {
  1301. update();
  1302. play_position->update();
  1303. }
  1304. void AnimationTrackEdit::_path_entered(const String &p_text) {
  1305. undo_redo->create_action("Change Track Path");
  1306. undo_redo->add_do_method(animation.ptr(), "track_set_path", track, p_text);
  1307. undo_redo->add_undo_method(animation.ptr(), "track_set_path", track, animation->track_get_path(track));
  1308. undo_redo->commit_action();
  1309. }
  1310. String AnimationTrackEdit::get_tooltip(const Point2 &p_pos) const {
  1311. if (check_rect.has_point(p_pos)) {
  1312. return TTR("Toggle this track on/off.");
  1313. }
  1314. if (path_rect.has_point(p_pos)) {
  1315. return animation->track_get_path(track);
  1316. }
  1317. if (update_mode_rect.has_point(p_pos)) {
  1318. return TTR("Update Mode (How this property is set)");
  1319. }
  1320. if (interp_mode_rect.has_point(p_pos)) {
  1321. return TTR("Interpolation Mode");
  1322. }
  1323. if (loop_mode_rect.has_point(p_pos)) {
  1324. return TTR("Loop Wrap Mode (Interpolate end with beginning on loop)");
  1325. }
  1326. if (remove_rect.has_point(p_pos)) {
  1327. return TTR("Remove this track.");
  1328. }
  1329. if (p_pos.x >= timeline->get_name_limit() && p_pos.x <= (get_size().width - timeline->get_buttons_width())) {
  1330. int key_idx = -1;
  1331. float key_distance = 1e20;
  1332. for (int i = animation->track_get_key_count(track) - 1; i >= 0; i--) { //select should happen in the opposite order of drawing for more accurate overlap select
  1333. Rect2 rect = const_cast<AnimationTrackEdit *>(this)->get_key_rect(i, timeline->get_zoom_scale());
  1334. float offset = animation->track_get_key_time(track, i) - timeline->get_value();
  1335. offset = offset * timeline->get_zoom_scale() + timeline->get_name_limit();
  1336. rect.position.x += offset;
  1337. if (rect.has_point(p_pos)) {
  1338. if (const_cast<AnimationTrackEdit *>(this)->is_key_selectable_by_distance()) {
  1339. float distance = ABS(offset - p_pos.x);
  1340. if (key_idx == -1 || distance < key_distance) {
  1341. key_idx = i;
  1342. key_distance = distance;
  1343. }
  1344. } else {
  1345. //first one does it
  1346. break;
  1347. }
  1348. }
  1349. }
  1350. if (key_idx != -1) {
  1351. String text = TTR("Time (s): ") + rtos(animation->track_get_key_time(track, key_idx)) + "\n";
  1352. switch (animation->track_get_type(track)) {
  1353. case Animation::TYPE_TRANSFORM: {
  1354. Dictionary d = animation->track_get_key_value(track, key_idx);
  1355. if (d.has("location"))
  1356. text += "Pos: " + String(d["location"]) + "\n";
  1357. if (d.has("rotation"))
  1358. text += "Rot: " + String(d["rotation"]) + "\n";
  1359. if (d.has("scale"))
  1360. text += "Scale: " + String(d["scale"]) + "\n";
  1361. } break;
  1362. case Animation::TYPE_VALUE: {
  1363. Variant v = animation->track_get_key_value(track, key_idx);
  1364. //text+="value: "+String(v)+"\n";
  1365. bool prop_exists = false;
  1366. Variant::Type valid_type = Variant::NIL;
  1367. Object *obj = NULL;
  1368. RES res;
  1369. Vector<StringName> leftover_path;
  1370. Node *node = root->get_node_and_resource(animation->track_get_path(track), res, leftover_path);
  1371. if (res.is_valid()) {
  1372. obj = res.ptr();
  1373. } else if (node) {
  1374. obj = node;
  1375. }
  1376. if (obj) {
  1377. valid_type = obj->get_static_property_type_indexed(leftover_path, &prop_exists);
  1378. }
  1379. text += "Type: " + Variant::get_type_name(v.get_type()) + "\n";
  1380. if (prop_exists && !Variant::can_convert(v.get_type(), valid_type)) {
  1381. text += "Value: " + String(v) + " (Invalid, expected type: " + Variant::get_type_name(valid_type) + ")\n";
  1382. } else {
  1383. text += "Value: " + String(v) + "\n";
  1384. }
  1385. text += "Easing: " + rtos(animation->track_get_key_transition(track, key_idx));
  1386. } break;
  1387. case Animation::TYPE_METHOD: {
  1388. Dictionary d = animation->track_get_key_value(track, key_idx);
  1389. if (d.has("method"))
  1390. text += String(d["method"]);
  1391. text += "(";
  1392. Vector<Variant> args;
  1393. if (d.has("args"))
  1394. args = d["args"];
  1395. for (int i = 0; i < args.size(); i++) {
  1396. if (i > 0)
  1397. text += ", ";
  1398. text += String(args[i]);
  1399. }
  1400. text += ")\n";
  1401. } break;
  1402. case Animation::TYPE_BEZIER: {
  1403. float h = animation->bezier_track_get_key_value(track, key_idx);
  1404. text += "Value: " + rtos(h) + "\n";
  1405. Vector2 ih = animation->bezier_track_get_key_in_handle(track, key_idx);
  1406. text += "In-Handle: " + ih + "\n";
  1407. Vector2 oh = animation->bezier_track_get_key_out_handle(track, key_idx);
  1408. text += "Out-Handle: " + oh + "\n";
  1409. } break;
  1410. case Animation::TYPE_AUDIO: {
  1411. String stream_name = "null";
  1412. RES stream = animation->audio_track_get_key_stream(track, key_idx);
  1413. if (stream.is_valid()) {
  1414. if (stream->get_path().is_resource_file()) {
  1415. stream_name = stream->get_path().get_file();
  1416. } else if (stream->get_name() != "") {
  1417. stream_name = stream->get_name();
  1418. } else {
  1419. stream_name = stream->get_class();
  1420. }
  1421. }
  1422. text += "Stream: " + stream_name + "\n";
  1423. float so = animation->audio_track_get_key_start_offset(track, key_idx);
  1424. text += "Start (s): " + rtos(so) + "\n";
  1425. float eo = animation->audio_track_get_key_end_offset(track, key_idx);
  1426. text += "End (s): " + rtos(eo) + "\n";
  1427. } break;
  1428. case Animation::TYPE_ANIMATION: {
  1429. String name = animation->animation_track_get_key_animation(track, key_idx);
  1430. text += "Animation Clip: " + name + "\n";
  1431. } break;
  1432. }
  1433. return text;
  1434. }
  1435. }
  1436. return Control::get_tooltip(p_pos);
  1437. }
  1438. void AnimationTrackEdit::_gui_input(const Ref<InputEvent> &p_event) {
  1439. if (p_event->is_pressed()) {
  1440. if (ED_GET_SHORTCUT("animation_editor/duplicate_selection")->is_shortcut(p_event)) {
  1441. emit_signal("duplicate_request");
  1442. accept_event();
  1443. }
  1444. if (ED_GET_SHORTCUT("animation_editor/duplicate_selection_transposed")->is_shortcut(p_event)) {
  1445. emit_signal("duplicate_transpose_request");
  1446. accept_event();
  1447. }
  1448. if (ED_GET_SHORTCUT("animation_editor/delete_selection")->is_shortcut(p_event)) {
  1449. emit_signal("delete_request");
  1450. accept_event();
  1451. }
  1452. }
  1453. Ref<InputEventMouseButton> mb = p_event;
  1454. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1455. Point2 pos = mb->get_position();
  1456. if (check_rect.has_point(pos)) {
  1457. *block_animation_update_ptr = true;
  1458. undo_redo->create_action("Toggle track enabled");
  1459. undo_redo->add_do_method(animation.ptr(), "track_set_enabled", track, !animation->track_is_enabled(track));
  1460. undo_redo->add_undo_method(animation.ptr(), "track_set_enabled", track, animation->track_is_enabled(track));
  1461. undo_redo->commit_action();
  1462. *block_animation_update_ptr = false;
  1463. update();
  1464. accept_event();
  1465. }
  1466. if (path_rect.has_point(pos)) {
  1467. clicking_on_name = true;
  1468. accept_event();
  1469. }
  1470. if (update_mode_rect.has_point(pos)) {
  1471. if (!menu) {
  1472. menu = memnew(PopupMenu);
  1473. add_child(menu);
  1474. menu->connect("id_pressed", this, "_menu_selected");
  1475. }
  1476. menu->clear();
  1477. menu->add_icon_item(get_icon("TrackContinuous", "EditorIcons"), TTR("Continuous"), MENU_CALL_MODE_CONTINUOUS);
  1478. menu->add_icon_item(get_icon("TrackDiscrete", "EditorIcons"), TTR("Discrete"), MENU_CALL_MODE_DISCRETE);
  1479. menu->add_icon_item(get_icon("TrackTrigger", "EditorIcons"), TTR("Trigger"), MENU_CALL_MODE_TRIGGER);
  1480. menu->add_icon_item(get_icon("TrackCapture", "EditorIcons"), TTR("Capture"), MENU_CALL_MODE_CAPTURE);
  1481. menu->set_as_minsize();
  1482. Vector2 popup_pos = get_global_position() + update_mode_rect.position + Vector2(0, update_mode_rect.size.height);
  1483. menu->set_global_position(popup_pos);
  1484. menu->popup();
  1485. accept_event();
  1486. }
  1487. if (interp_mode_rect.has_point(pos)) {
  1488. if (!menu) {
  1489. menu = memnew(PopupMenu);
  1490. add_child(menu);
  1491. menu->connect("id_pressed", this, "_menu_selected");
  1492. }
  1493. menu->clear();
  1494. menu->add_icon_item(get_icon("InterpRaw", "EditorIcons"), TTR("Nearest"), MENU_INTERPOLATION_NEAREST);
  1495. menu->add_icon_item(get_icon("InterpLinear", "EditorIcons"), TTR("Linear"), MENU_INTERPOLATION_LINEAR);
  1496. menu->add_icon_item(get_icon("InterpCubic", "EditorIcons"), TTR("Cubic"), MENU_INTERPOLATION_CUBIC);
  1497. menu->set_as_minsize();
  1498. Vector2 popup_pos = get_global_position() + interp_mode_rect.position + Vector2(0, interp_mode_rect.size.height);
  1499. menu->set_global_position(popup_pos);
  1500. menu->popup();
  1501. accept_event();
  1502. }
  1503. if (loop_mode_rect.has_point(pos)) {
  1504. if (!menu) {
  1505. menu = memnew(PopupMenu);
  1506. add_child(menu);
  1507. menu->connect("id_pressed", this, "_menu_selected");
  1508. }
  1509. menu->clear();
  1510. menu->add_icon_item(get_icon("InterpWrapClamp", "EditorIcons"), TTR("Clamp Loop Interp"), MENU_LOOP_CLAMP);
  1511. menu->add_icon_item(get_icon("InterpWrapLoop", "EditorIcons"), TTR("Wrap Loop Interp"), MENU_LOOP_WRAP);
  1512. menu->set_as_minsize();
  1513. Vector2 popup_pos = get_global_position() + loop_mode_rect.position + Vector2(0, loop_mode_rect.size.height);
  1514. menu->set_global_position(popup_pos);
  1515. menu->popup();
  1516. accept_event();
  1517. }
  1518. if (remove_rect.has_point(pos)) {
  1519. emit_signal("remove_request", track);
  1520. accept_event();
  1521. }
  1522. if (bezier_edit_rect.has_point(pos)) {
  1523. emit_signal("bezier_edit");
  1524. accept_event();
  1525. }
  1526. //check keyframes
  1527. float scale = timeline->get_zoom_scale();
  1528. int limit = timeline->get_name_limit();
  1529. int limit_end = get_size().width - timeline->get_buttons_width();
  1530. if (pos.x >= limit && pos.x <= limit_end) {
  1531. int key_idx = -1;
  1532. float key_distance = 1e20;
  1533. for (int i = animation->track_get_key_count(track) - 1; i >= 0; i--) { //select should happen in the opposite order of drawing for more accurate overlap select
  1534. Rect2 rect = get_key_rect(i, scale);
  1535. float offset = animation->track_get_key_time(track, i) - timeline->get_value();
  1536. offset = offset * scale + limit;
  1537. rect.position.x += offset;
  1538. if (rect.has_point(pos)) {
  1539. if (is_key_selectable_by_distance()) {
  1540. float distance = ABS(offset - pos.x);
  1541. if (key_idx == -1 || distance < key_distance) {
  1542. key_idx = i;
  1543. key_distance = distance;
  1544. }
  1545. } else {
  1546. //first one does it
  1547. key_idx = i;
  1548. break;
  1549. }
  1550. }
  1551. }
  1552. if (key_idx != -1) {
  1553. if (mb->get_command() || mb->get_shift()) {
  1554. if (editor->is_key_selected(track, key_idx)) {
  1555. emit_signal("deselect_key", key_idx);
  1556. } else {
  1557. emit_signal("select_key", key_idx, false);
  1558. moving_selection_attempt = true;
  1559. select_single_attempt = -1;
  1560. moving_selection_from_ofs = (mb->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale();
  1561. }
  1562. } else {
  1563. if (!editor->is_key_selected(track, key_idx)) {
  1564. emit_signal("select_key", key_idx, true);
  1565. select_single_attempt = -1;
  1566. } else {
  1567. select_single_attempt = key_idx;
  1568. }
  1569. moving_selection_attempt = true;
  1570. moving_selection_from_ofs = (mb->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale();
  1571. }
  1572. accept_event();
  1573. } else {
  1574. emit_signal("clear_selection");
  1575. }
  1576. }
  1577. /*using focus instead
  1578. * if (!selected && pos.x >= timeline->get_name_limit() && pos.x < (get_size().width - timeline->get_buttons_width())) {
  1579. set_selected(true);
  1580. emit_signal("selected");
  1581. }
  1582. */
  1583. }
  1584. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1585. Point2 pos = mb->get_position();
  1586. if (pos.x >= timeline->get_name_limit() && pos.x <= get_size().width - timeline->get_buttons_width()) {
  1587. //can do something with menu too! show insert key
  1588. float offset = (pos.x - timeline->get_name_limit()) / timeline->get_zoom_scale();
  1589. if (!menu) {
  1590. menu = memnew(PopupMenu);
  1591. add_child(menu);
  1592. menu->connect("id_pressed", this, "_menu_selected");
  1593. }
  1594. menu->clear();
  1595. menu->add_icon_item(get_icon("Key", "EditorIcons"), TTR("Insert Key"), MENU_KEY_INSERT);
  1596. if (editor->is_selection_active()) {
  1597. menu->add_separator();
  1598. menu->add_icon_item(get_icon("Duplicate", "EditorIcons"), TTR("Duplicate Key(s)"), MENU_KEY_DUPLICATE);
  1599. menu->add_separator();
  1600. menu->add_icon_item(get_icon("Remove", "EditorIcons"), TTR("Delete Key(s)"), MENU_KEY_DELETE);
  1601. }
  1602. menu->set_as_minsize();
  1603. Vector2 popup_pos = get_global_transform().xform(get_local_mouse_position());
  1604. menu->set_global_position(popup_pos);
  1605. menu->popup();
  1606. insert_at_pos = offset + timeline->get_value();
  1607. accept_event();
  1608. }
  1609. }
  1610. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT && clicking_on_name) {
  1611. if (!path) {
  1612. path = memnew(LineEdit);
  1613. add_child(path);
  1614. path->set_as_toplevel(true);
  1615. path->connect("text_entered", this, "_path_entered");
  1616. }
  1617. path->set_text(animation->track_get_path(track));
  1618. Vector2 theme_ofs = path->get_stylebox("normal", "LineEdit")->get_offset();
  1619. path->set_position(get_global_position() + path_rect.position - theme_ofs);
  1620. path->set_size(path_rect.size);
  1621. path->show_modal();
  1622. path->grab_focus();
  1623. path->set_cursor_position(path->get_text().length());
  1624. clicking_on_name = false;
  1625. }
  1626. if (mb.is_valid() && moving_selection_attempt) {
  1627. if (!mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  1628. moving_selection_attempt = false;
  1629. if (moving_selection) {
  1630. emit_signal("move_selection_commit");
  1631. } else if (select_single_attempt != -1) {
  1632. emit_signal("select_key", select_single_attempt, true);
  1633. }
  1634. moving_selection = false;
  1635. select_single_attempt = -1;
  1636. }
  1637. if (moving_selection && mb->is_pressed() && mb->get_button_index() == BUTTON_RIGHT) {
  1638. moving_selection_attempt = false;
  1639. moving_selection = false;
  1640. emit_signal("move_selection_cancel");
  1641. }
  1642. }
  1643. Ref<InputEventMouseMotion> mm = p_event;
  1644. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_LEFT && moving_selection_attempt) {
  1645. if (!moving_selection) {
  1646. moving_selection = true;
  1647. emit_signal("move_selection_begin");
  1648. }
  1649. float new_ofs = (mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale();
  1650. emit_signal("move_selection", new_ofs - moving_selection_from_ofs);
  1651. }
  1652. }
  1653. Variant AnimationTrackEdit::get_drag_data(const Point2 &p_point) {
  1654. if (!clicking_on_name)
  1655. return Variant();
  1656. Dictionary drag_data;
  1657. drag_data["type"] = "animation_track";
  1658. drag_data["index"] = track;
  1659. ToolButton *tb = memnew(ToolButton);
  1660. tb->set_text(path_cache);
  1661. tb->set_icon(icon_cache);
  1662. set_drag_preview(tb);
  1663. clicking_on_name = false;
  1664. return drag_data;
  1665. }
  1666. bool AnimationTrackEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  1667. Dictionary d = p_data;
  1668. if (!d.has("type")) {
  1669. return false;
  1670. }
  1671. String type = d["type"];
  1672. if (type != "animation_track")
  1673. return false;
  1674. if (p_point.y < get_size().height / 2) {
  1675. dropping_at = -1;
  1676. } else {
  1677. dropping_at = 1;
  1678. }
  1679. const_cast<AnimationTrackEdit *>(this)->update();
  1680. const_cast<AnimationTrackEdit *>(this)->emit_signal("drop_attempted", track);
  1681. return true;
  1682. }
  1683. void AnimationTrackEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  1684. Dictionary d = p_data;
  1685. if (!d.has("type")) {
  1686. return;
  1687. }
  1688. String type = d["type"];
  1689. if (type != "animation_track")
  1690. return;
  1691. int from_track = d["index"];
  1692. if (dropping_at < 0) {
  1693. emit_signal("dropped", from_track, track);
  1694. } else {
  1695. emit_signal("dropped", from_track, track + 1);
  1696. }
  1697. }
  1698. void AnimationTrackEdit::_menu_selected(int p_index) {
  1699. switch (p_index) {
  1700. case MENU_CALL_MODE_CONTINUOUS:
  1701. case MENU_CALL_MODE_DISCRETE:
  1702. case MENU_CALL_MODE_TRIGGER:
  1703. case MENU_CALL_MODE_CAPTURE: {
  1704. Animation::UpdateMode update_mode = Animation::UpdateMode(p_index);
  1705. *block_animation_update_ptr = true;
  1706. undo_redo->create_action("Change animation update mode");
  1707. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", track, update_mode);
  1708. undo_redo->add_undo_method(animation.ptr(), "value_track_set_update_mode", track, animation->value_track_get_update_mode(track));
  1709. undo_redo->commit_action();
  1710. *block_animation_update_ptr = false;
  1711. update();
  1712. } break;
  1713. case MENU_INTERPOLATION_NEAREST:
  1714. case MENU_INTERPOLATION_LINEAR:
  1715. case MENU_INTERPOLATION_CUBIC: {
  1716. Animation::InterpolationType interp_mode = Animation::InterpolationType(p_index - MENU_INTERPOLATION_NEAREST);
  1717. *block_animation_update_ptr = true;
  1718. undo_redo->create_action("Change animation interpolation mode");
  1719. undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", track, interp_mode);
  1720. undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", track, animation->track_get_interpolation_type(track));
  1721. undo_redo->commit_action();
  1722. *block_animation_update_ptr = false;
  1723. update();
  1724. } break;
  1725. case MENU_LOOP_WRAP:
  1726. case MENU_LOOP_CLAMP: {
  1727. bool loop_wrap = p_index == MENU_LOOP_WRAP;
  1728. *block_animation_update_ptr = true;
  1729. undo_redo->create_action("Change animation loop mode");
  1730. undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", track, loop_wrap);
  1731. undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_loop_wrap", track, animation->track_get_interpolation_loop_wrap(track));
  1732. undo_redo->commit_action();
  1733. *block_animation_update_ptr = false;
  1734. update();
  1735. } break;
  1736. case MENU_KEY_INSERT: {
  1737. emit_signal("insert_key", insert_at_pos);
  1738. } break;
  1739. case MENU_KEY_DUPLICATE: {
  1740. emit_signal("duplicate_request");
  1741. } break;
  1742. case MENU_KEY_DELETE: {
  1743. emit_signal("delete_request");
  1744. } break;
  1745. }
  1746. }
  1747. void AnimationTrackEdit::set_block_animation_update_ptr(bool *p_block_ptr) {
  1748. block_animation_update_ptr = p_block_ptr;
  1749. }
  1750. void AnimationTrackEdit::cancel_drop() {
  1751. if (dropping_at != 0) {
  1752. dropping_at = 0;
  1753. update();
  1754. }
  1755. }
  1756. void AnimationTrackEdit::set_in_group(bool p_enable) {
  1757. in_group = p_enable;
  1758. update();
  1759. }
  1760. void AnimationTrackEdit::append_to_selection(const Rect2 &p_box) {
  1761. Rect2 select_rect(timeline->get_name_limit(), 0, get_size().width - timeline->get_name_limit() - timeline->get_buttons_width(), get_size().height);
  1762. select_rect = select_rect.clip(p_box);
  1763. for (int i = animation->track_get_key_count(track) - 1; i >= 0; i--) { //select should happen in the opposite order of drawing for more accurate overlap select
  1764. Rect2 rect = const_cast<AnimationTrackEdit *>(this)->get_key_rect(i, timeline->get_zoom_scale());
  1765. float offset = animation->track_get_key_time(track, i) - timeline->get_value();
  1766. offset = offset * timeline->get_zoom_scale() + timeline->get_name_limit();
  1767. rect.position.x += offset;
  1768. if (select_rect.intersects(rect)) {
  1769. emit_signal("select_key", i, false);
  1770. }
  1771. }
  1772. }
  1773. void AnimationTrackEdit::_bind_methods() {
  1774. ClassDB::bind_method("_zoom_changed", &AnimationTrackEdit::_zoom_changed);
  1775. ClassDB::bind_method("_menu_selected", &AnimationTrackEdit::_menu_selected);
  1776. ClassDB::bind_method("_gui_input", &AnimationTrackEdit::_gui_input);
  1777. ClassDB::bind_method("_path_entered", &AnimationTrackEdit::_path_entered);
  1778. ClassDB::bind_method("_play_position_draw", &AnimationTrackEdit::_play_position_draw);
  1779. ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::REAL, "position"), PropertyInfo(Variant::BOOL, "drag")));
  1780. ADD_SIGNAL(MethodInfo("remove_request", PropertyInfo(Variant::INT, "track")));
  1781. ADD_SIGNAL(MethodInfo("dropped", PropertyInfo(Variant::INT, "from_track"), PropertyInfo(Variant::INT, "to_track")));
  1782. ADD_SIGNAL(MethodInfo("insert_key", PropertyInfo(Variant::REAL, "ofs")));
  1783. ADD_SIGNAL(MethodInfo("select_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "single")));
  1784. ADD_SIGNAL(MethodInfo("deselect_key", PropertyInfo(Variant::INT, "index")));
  1785. ADD_SIGNAL(MethodInfo("clear_selection"));
  1786. ADD_SIGNAL(MethodInfo("bezier_edit"));
  1787. ADD_SIGNAL(MethodInfo("move_selection_begin"));
  1788. ADD_SIGNAL(MethodInfo("move_selection", PropertyInfo(Variant::REAL, "ofs")));
  1789. ADD_SIGNAL(MethodInfo("move_selection_commit"));
  1790. ADD_SIGNAL(MethodInfo("move_selection_cancel"));
  1791. ADD_SIGNAL(MethodInfo("duplicate_request"));
  1792. ADD_SIGNAL(MethodInfo("duplicate_transpose_request"));
  1793. ADD_SIGNAL(MethodInfo("delete_request"));
  1794. }
  1795. AnimationTrackEdit::AnimationTrackEdit() {
  1796. undo_redo = NULL;
  1797. timeline = NULL;
  1798. root = NULL;
  1799. path = NULL;
  1800. menu = NULL;
  1801. block_animation_update_ptr = NULL;
  1802. clicking_on_name = false;
  1803. dropping_at = 0;
  1804. in_group = false;
  1805. moving_selection_attempt = false;
  1806. moving_selection = false;
  1807. select_single_attempt = -1;
  1808. play_position_pos = 0;
  1809. play_position = memnew(Control);
  1810. play_position->set_mouse_filter(MOUSE_FILTER_PASS);
  1811. add_child(play_position);
  1812. play_position->set_anchors_and_margins_preset(PRESET_WIDE);
  1813. play_position->connect("draw", this, "_play_position_draw");
  1814. set_focus_mode(FOCUS_CLICK);
  1815. set_mouse_filter(MOUSE_FILTER_PASS); //scroll has to work too for selection
  1816. }
  1817. //////////////////////////////////////
  1818. AnimationTrackEdit *AnimationTrackEditPlugin::create_value_track_edit(Object *p_object, Variant::Type p_type, const String &p_property, PropertyHint p_hint, const String &p_hint_string, int p_usage) {
  1819. if (get_script_instance()) {
  1820. Variant args[6] = {
  1821. p_object,
  1822. p_type,
  1823. p_property,
  1824. p_hint,
  1825. p_hint_string,
  1826. p_usage
  1827. };
  1828. Variant *argptrs[6] = {
  1829. &args[0],
  1830. &args[1],
  1831. &args[2],
  1832. &args[3],
  1833. &args[4],
  1834. &args[5]
  1835. };
  1836. Variant::CallError ce;
  1837. return Object::cast_to<AnimationTrackEdit>(get_script_instance()->call("create_value_track_edit", (const Variant **)&argptrs, 6, ce).operator Object *());
  1838. }
  1839. return NULL;
  1840. }
  1841. AnimationTrackEdit *AnimationTrackEditPlugin::create_audio_track_edit() {
  1842. if (get_script_instance()) {
  1843. return Object::cast_to<AnimationTrackEdit>(get_script_instance()->call("create_audio_track_edit").operator Object *());
  1844. }
  1845. return NULL;
  1846. }
  1847. AnimationTrackEdit *AnimationTrackEditPlugin::create_animation_track_edit(Object *p_object) {
  1848. if (get_script_instance()) {
  1849. return Object::cast_to<AnimationTrackEdit>(get_script_instance()->call("create_animation_track_edit", p_object).operator Object *());
  1850. }
  1851. return NULL;
  1852. }
  1853. ///////////////////////////////////////
  1854. void AnimationTrackEditGroup::_notification(int p_what) {
  1855. if (p_what == NOTIFICATION_DRAW) {
  1856. Ref<Font> font = get_font("font", "Label");
  1857. int separation = get_constant("hseparation", "ItemList");
  1858. Color color = get_color("font_color", "Label");
  1859. if (root && root->has_node(node)) {
  1860. Node *n = root->get_node(node);
  1861. if (n && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
  1862. color = get_color("accent_color", "Editor");
  1863. }
  1864. }
  1865. Color bgcol = get_color("dark_color_2", "Editor");
  1866. bgcol.a *= 0.6;
  1867. draw_rect(Rect2(Point2(), get_size()), bgcol);
  1868. Color linecolor = color;
  1869. linecolor.a = 0.2;
  1870. draw_line(Point2(), Point2(get_size().width, 0), linecolor);
  1871. draw_line(Point2(timeline->get_name_limit(), 0), Point2(timeline->get_name_limit(), get_size().height), linecolor);
  1872. draw_line(Point2(get_size().width - timeline->get_buttons_width(), 0), Point2(get_size().width - timeline->get_buttons_width(), get_size().height), linecolor);
  1873. int ofs = 0;
  1874. draw_texture(icon, Point2(ofs, int(get_size().height - icon->get_height()) / 2));
  1875. ofs += separation + icon->get_width();
  1876. draw_string(font, Point2(ofs, int(get_size().height - font->get_height()) / 2 + font->get_ascent()), node_name, color, timeline->get_name_limit() - ofs);
  1877. int px = (-timeline->get_value() + timeline->get_play_position()) * timeline->get_zoom_scale() + timeline->get_name_limit();
  1878. if (px >= timeline->get_name_limit() && px < (get_size().width - timeline->get_buttons_width())) {
  1879. Color accent = get_color("accent_color", "Editor");
  1880. draw_line(Point2(px, 0), Point2(px, get_size().height), accent);
  1881. }
  1882. }
  1883. }
  1884. void AnimationTrackEditGroup::set_type_and_name(const Ref<Texture> &p_type, const String &p_name, const NodePath &p_node) {
  1885. icon = p_type;
  1886. node_name = p_name;
  1887. node = p_node;
  1888. update();
  1889. minimum_size_changed();
  1890. }
  1891. Size2 AnimationTrackEditGroup::get_minimum_size() const {
  1892. Ref<Font> font = get_font("font", "Label");
  1893. int separation = get_constant("vseparation", "ItemList");
  1894. return Vector2(0, MAX(font->get_height(), icon->get_height()) + separation);
  1895. }
  1896. void AnimationTrackEditGroup::set_timeline(AnimationTimelineEdit *p_timeline) {
  1897. timeline = p_timeline;
  1898. timeline->connect("zoom_changed", this, "_zoom_changed");
  1899. timeline->connect("name_limit_changed", this, "_zoom_changed");
  1900. }
  1901. void AnimationTrackEditGroup::set_root(Node *p_root) {
  1902. root = p_root;
  1903. update();
  1904. }
  1905. void AnimationTrackEditGroup::_zoom_changed() {
  1906. update();
  1907. }
  1908. void AnimationTrackEditGroup::_bind_methods() {
  1909. ClassDB::bind_method("_zoom_changed", &AnimationTrackEditGroup::_zoom_changed);
  1910. }
  1911. AnimationTrackEditGroup::AnimationTrackEditGroup() {
  1912. set_mouse_filter(MOUSE_FILTER_PASS);
  1913. }
  1914. //////////////////////////////////////
  1915. void AnimationTrackEditor::add_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin) {
  1916. if (track_edit_plugins.find(p_plugin) != -1)
  1917. return;
  1918. track_edit_plugins.push_back(p_plugin);
  1919. }
  1920. void AnimationTrackEditor::remove_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin) {
  1921. track_edit_plugins.erase(p_plugin);
  1922. }
  1923. void AnimationTrackEditor::set_animation(const Ref<Animation> &p_anim) {
  1924. if (animation != p_anim && _get_track_selected() >= 0) {
  1925. track_edits[_get_track_selected()]->release_focus();
  1926. }
  1927. if (animation.is_valid()) {
  1928. animation->disconnect("changed", this, "_animation_changed");
  1929. _clear_selection();
  1930. }
  1931. animation = p_anim;
  1932. timeline->set_animation(p_anim);
  1933. _cancel_bezier_edit();
  1934. _update_tracks();
  1935. if (animation.is_valid()) {
  1936. animation->connect("changed", this, "_animation_changed");
  1937. hscroll->show();
  1938. edit->set_disabled(false);
  1939. step->set_block_signals(true);
  1940. step->set_value(animation->get_step());
  1941. step->set_block_signals(false);
  1942. step->set_read_only(false);
  1943. snap->set_disabled(false);
  1944. } else {
  1945. hscroll->hide();
  1946. edit->set_disabled(true);
  1947. step->set_block_signals(true);
  1948. step->set_value(0);
  1949. step->set_block_signals(false);
  1950. step->set_read_only(true);
  1951. snap->set_disabled(true);
  1952. }
  1953. }
  1954. Ref<Animation> AnimationTrackEditor::get_current_animation() const {
  1955. return animation;
  1956. }
  1957. void AnimationTrackEditor::_root_removed(Node *p_root) {
  1958. root = NULL;
  1959. }
  1960. void AnimationTrackEditor::set_root(Node *p_root) {
  1961. if (root) {
  1962. root->disconnect("tree_exiting", this, "_root_removed");
  1963. }
  1964. root = p_root;
  1965. if (root) {
  1966. root->connect("tree_exiting", this, "_root_removed", make_binds(), CONNECT_ONESHOT);
  1967. }
  1968. _update_tracks();
  1969. }
  1970. Node *AnimationTrackEditor::get_root() const {
  1971. return root;
  1972. }
  1973. void AnimationTrackEditor::update_keying() {
  1974. bool keying_enabled = is_visible_in_tree() && animation.is_valid();
  1975. if (keying_enabled == keying)
  1976. return;
  1977. keying = keying_enabled;
  1978. //_update_menu();
  1979. emit_signal("keying_changed");
  1980. }
  1981. bool AnimationTrackEditor::has_keying() const {
  1982. return keying;
  1983. }
  1984. void AnimationTrackEditor::cleanup() {
  1985. set_animation(Ref<Animation>());
  1986. }
  1987. void AnimationTrackEditor::_name_limit_changed() {
  1988. for (int i = 0; i < track_edits.size(); i++) {
  1989. track_edits[i]->update();
  1990. }
  1991. }
  1992. void AnimationTrackEditor::_timeline_changed(float p_new_pos, bool p_drag) {
  1993. emit_signal("timeline_changed", p_new_pos, p_drag);
  1994. }
  1995. void AnimationTrackEditor::_track_remove_request(int p_track) {
  1996. int idx = p_track;
  1997. if (idx >= 0 && idx < animation->get_track_count()) {
  1998. _clear_selection();
  1999. undo_redo->create_action(TTR("Remove Anim Track"));
  2000. undo_redo->add_do_method(animation.ptr(), "remove_track", idx);
  2001. undo_redo->add_undo_method(animation.ptr(), "add_track", animation->track_get_type(idx), idx);
  2002. undo_redo->add_undo_method(animation.ptr(), "track_set_path", idx, animation->track_get_path(idx));
  2003. //todo interpolation
  2004. for (int i = 0; i < animation->track_get_key_count(idx); i++) {
  2005. Variant v = animation->track_get_key_value(idx, i);
  2006. float time = animation->track_get_key_time(idx, i);
  2007. float trans = animation->track_get_key_transition(idx, i);
  2008. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", idx, time, v);
  2009. undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", idx, i, trans);
  2010. }
  2011. undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", idx, animation->track_get_interpolation_type(idx));
  2012. if (animation->track_get_type(idx) == Animation::TYPE_VALUE) {
  2013. undo_redo->add_undo_method(animation.ptr(), "value_track_set_update_mode", idx, animation->value_track_get_update_mode(idx));
  2014. }
  2015. undo_redo->commit_action();
  2016. }
  2017. }
  2018. void AnimationTrackEditor::set_anim_pos(float p_pos) {
  2019. timeline->set_play_position(p_pos);
  2020. for (int i = 0; i < track_edits.size(); i++) {
  2021. track_edits[i]->set_play_position(p_pos);
  2022. }
  2023. for (int i = 0; i < groups.size(); i++) {
  2024. groups[i]->update();
  2025. }
  2026. bezier_edit->set_play_position(p_pos);
  2027. }
  2028. void AnimationTrackEditor::_query_insert(const InsertData &p_id) {
  2029. if (insert_frame != Engine::get_singleton()->get_frames_drawn()) {
  2030. //clear insert list for the frame if frame changed
  2031. if (insert_confirm->is_visible_in_tree())
  2032. return; //do nothing
  2033. insert_data.clear();
  2034. insert_query = false;
  2035. }
  2036. insert_frame = Engine::get_singleton()->get_frames_drawn();
  2037. for (List<InsertData>::Element *E = insert_data.front(); E; E = E->next()) {
  2038. //prevent insertion of multiple tracks
  2039. if (E->get().path == p_id.path)
  2040. return; //already inserted a track for this on this frame
  2041. }
  2042. insert_data.push_back(p_id);
  2043. if (p_id.track_idx == -1) {
  2044. if (bool(EDITOR_DEF("editors/animation/confirm_insert_track", true))) {
  2045. //potential new key, does not exist
  2046. if (insert_data.size() == 1)
  2047. insert_confirm_text->set_text(vformat(TTR("Create NEW track for %s and insert key?"), p_id.query));
  2048. else
  2049. insert_confirm_text->set_text(vformat(TTR("Create %d NEW tracks and insert keys?"), insert_data.size()));
  2050. bool all_bezier = true;
  2051. for (int i = 0; i < insert_data.size(); i++) {
  2052. if (insert_data[i].type != Animation::TYPE_VALUE && insert_data[i].type != Animation::TYPE_BEZIER) {
  2053. all_bezier = false;
  2054. }
  2055. if (insert_data[i].type != Animation::TYPE_VALUE) {
  2056. continue;
  2057. }
  2058. switch (insert_data[i].value.get_type()) {
  2059. case Variant::INT:
  2060. case Variant::REAL:
  2061. case Variant::VECTOR2:
  2062. case Variant::VECTOR3:
  2063. case Variant::QUAT:
  2064. case Variant::PLANE:
  2065. case Variant::COLOR: {
  2066. //good
  2067. } break;
  2068. default: {
  2069. all_bezier = false;
  2070. }
  2071. }
  2072. }
  2073. insert_confirm_bezier->set_visible(all_bezier);
  2074. insert_confirm->get_ok()->set_text(TTR("Create"));
  2075. insert_confirm->popup_centered_minsize();
  2076. insert_query = true;
  2077. } else {
  2078. call_deferred("_insert_delay");
  2079. insert_queue = true;
  2080. }
  2081. } else {
  2082. if (!insert_query && !insert_queue) {
  2083. call_deferred("_insert_delay");
  2084. insert_queue = true;
  2085. }
  2086. }
  2087. }
  2088. void AnimationTrackEditor::_insert_delay() {
  2089. if (insert_query) {
  2090. //discard since it's entered into query mode
  2091. insert_queue = false;
  2092. return;
  2093. }
  2094. undo_redo->create_action(TTR("Anim Insert"));
  2095. int last_track = animation->get_track_count();
  2096. bool advance = false;
  2097. while (insert_data.size()) {
  2098. if (insert_data.front()->get().advance)
  2099. advance = true;
  2100. last_track = _confirm_insert(insert_data.front()->get(), last_track);
  2101. insert_data.pop_front();
  2102. }
  2103. undo_redo->commit_action();
  2104. if (advance) {
  2105. float step = animation->get_step();
  2106. if (step == 0)
  2107. step = 1;
  2108. float pos = timeline->get_play_position();
  2109. pos = Math::stepify(pos + step, step);
  2110. if (pos > animation->get_length())
  2111. pos = animation->get_length();
  2112. set_anim_pos(pos);
  2113. emit_signal("timeline_changed", pos, true);
  2114. }
  2115. insert_queue = false;
  2116. }
  2117. void AnimationTrackEditor::insert_transform_key(Spatial *p_node, const String &p_sub, const Transform &p_xform) {
  2118. if (!keying)
  2119. return;
  2120. if (!animation.is_valid())
  2121. return;
  2122. ERR_FAIL_COND(!root);
  2123. //let's build a node path
  2124. String path = root->get_path_to(p_node);
  2125. if (p_sub != "")
  2126. path += ":" + p_sub;
  2127. NodePath np = path;
  2128. int track_idx = -1;
  2129. for (int i = 0; i < animation->get_track_count(); i++) {
  2130. if (animation->track_get_type(i) != Animation::TYPE_TRANSFORM)
  2131. continue;
  2132. if (animation->track_get_path(i) != np)
  2133. continue;
  2134. track_idx = i;
  2135. break;
  2136. }
  2137. InsertData id;
  2138. Dictionary val;
  2139. id.path = np;
  2140. id.track_idx = track_idx;
  2141. id.value = p_xform;
  2142. id.type = Animation::TYPE_TRANSFORM;
  2143. id.query = "node '" + p_node->get_name() + "'";
  2144. id.advance = false;
  2145. //dialog insert
  2146. _query_insert(id);
  2147. }
  2148. void AnimationTrackEditor::_insert_animation_key(NodePath p_path, const Variant &p_value) {
  2149. String path = p_path;
  2150. //animation property is a special case, always creates an animation track
  2151. for (int i = 0; i < animation->get_track_count(); i++) {
  2152. String np = animation->track_get_path(i);
  2153. if (path == np && animation->track_get_type(i) == Animation::TYPE_ANIMATION) {
  2154. //exists
  2155. InsertData id;
  2156. id.path = path;
  2157. id.track_idx = i;
  2158. id.value = p_value;
  2159. id.type = Animation::TYPE_ANIMATION;
  2160. id.query = "animation";
  2161. id.advance = false;
  2162. //dialog insert
  2163. _query_insert(id);
  2164. return;
  2165. }
  2166. }
  2167. InsertData id;
  2168. id.path = path;
  2169. id.track_idx = -1;
  2170. id.value = p_value;
  2171. id.type = Animation::TYPE_ANIMATION;
  2172. id.query = "animation";
  2173. id.advance = false;
  2174. //dialog insert
  2175. _query_insert(id);
  2176. }
  2177. void AnimationTrackEditor::insert_node_value_key(Node *p_node, const String &p_property, const Variant &p_value, bool p_only_if_exists) {
  2178. ERR_FAIL_COND(!root);
  2179. //let's build a node path
  2180. Node *node = p_node;
  2181. String path = root->get_path_to(node);
  2182. if (Object::cast_to<AnimationPlayer>(node) && p_property == "current_animation") {
  2183. if (node == AnimationPlayerEditor::singleton->get_player()) {
  2184. EditorNode::get_singleton()->show_warning(TTR("AnimationPlayer can't animate itself, only other players."));
  2185. return;
  2186. }
  2187. _insert_animation_key(path, p_value);
  2188. return;
  2189. }
  2190. EditorHistory *history = EditorNode::get_singleton()->get_editor_history();
  2191. for (int i = 1; i < history->get_path_size(); i++) {
  2192. String prop = history->get_path_property(i);
  2193. ERR_FAIL_COND(prop == "");
  2194. path += ":" + prop;
  2195. }
  2196. path += ":" + p_property;
  2197. NodePath np = path;
  2198. //locate track
  2199. bool inserted = false;
  2200. for (int i = 0; i < animation->get_track_count(); i++) {
  2201. if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
  2202. if (animation->track_get_path(i) != np)
  2203. continue;
  2204. InsertData id;
  2205. id.path = np;
  2206. id.track_idx = i;
  2207. id.value = p_value;
  2208. id.type = Animation::TYPE_VALUE;
  2209. id.query = "property '" + p_property + "'";
  2210. id.advance = false;
  2211. //dialog insert
  2212. _query_insert(id);
  2213. inserted = true;
  2214. } else if (animation->track_get_type(i) == Animation::TYPE_BEZIER) {
  2215. Variant value;
  2216. if (animation->track_get_path(i) == np) {
  2217. value = p_value; //all good
  2218. } else {
  2219. String path = animation->track_get_path(i);
  2220. if (NodePath(path.get_basename()) == np) {
  2221. String subindex = path.get_extension();
  2222. value = p_value.get(subindex);
  2223. } else {
  2224. continue;
  2225. }
  2226. }
  2227. InsertData id;
  2228. id.path = animation->track_get_path(i);
  2229. id.track_idx = i;
  2230. id.value = value;
  2231. id.type = Animation::TYPE_BEZIER;
  2232. id.query = "property '" + p_property + "'";
  2233. id.advance = false;
  2234. //dialog insert
  2235. _query_insert(id);
  2236. inserted = true;
  2237. }
  2238. }
  2239. if (inserted || p_only_if_exists)
  2240. return;
  2241. InsertData id;
  2242. id.path = np;
  2243. id.track_idx = -1;
  2244. id.value = p_value;
  2245. id.type = Animation::TYPE_VALUE;
  2246. id.query = "property '" + p_property + "'";
  2247. id.advance = false;
  2248. //dialog insert
  2249. _query_insert(id);
  2250. }
  2251. void AnimationTrackEditor::insert_value_key(const String &p_property, const Variant &p_value, bool p_advance) {
  2252. EditorHistory *history = EditorNode::get_singleton()->get_editor_history();
  2253. ERR_FAIL_COND(!root);
  2254. //let's build a node path
  2255. ERR_FAIL_COND(history->get_path_size() == 0);
  2256. Object *obj = ObjectDB::get_instance(history->get_path_object(0));
  2257. ERR_FAIL_COND(!Object::cast_to<Node>(obj));
  2258. Node *node = Object::cast_to<Node>(obj);
  2259. String path = root->get_path_to(node);
  2260. if (Object::cast_to<AnimationPlayer>(node) && p_property == "current_animation") {
  2261. if (node == AnimationPlayerEditor::singleton->get_player()) {
  2262. EditorNode::get_singleton()->show_warning(TTR("AnimationPlayer can't animate itself, only other players."));
  2263. return;
  2264. }
  2265. _insert_animation_key(path, p_value);
  2266. return;
  2267. }
  2268. for (int i = 1; i < history->get_path_size(); i++) {
  2269. String prop = history->get_path_property(i);
  2270. ERR_FAIL_COND(prop == "");
  2271. path += ":" + prop;
  2272. }
  2273. path += ":" + p_property;
  2274. NodePath np = path;
  2275. //locate track
  2276. bool inserted = false;
  2277. for (int i = 0; i < animation->get_track_count(); i++) {
  2278. if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
  2279. if (animation->track_get_path(i) != np)
  2280. continue;
  2281. InsertData id;
  2282. id.path = np;
  2283. id.track_idx = i;
  2284. id.value = p_value;
  2285. id.type = Animation::TYPE_VALUE;
  2286. id.query = "property '" + p_property + "'";
  2287. id.advance = p_advance;
  2288. //dialog insert
  2289. _query_insert(id);
  2290. inserted = true;
  2291. } else if (animation->track_get_type(i) == Animation::TYPE_BEZIER) {
  2292. Variant value;
  2293. if (animation->track_get_path(i) == np) {
  2294. value = p_value; //all good
  2295. } else {
  2296. String path = animation->track_get_path(i);
  2297. if (NodePath(path.get_basename()) == np) {
  2298. String subindex = path.get_extension();
  2299. value = p_value.get(subindex);
  2300. } else {
  2301. continue;
  2302. }
  2303. }
  2304. InsertData id;
  2305. id.path = animation->track_get_path(i);
  2306. id.track_idx = i;
  2307. id.value = value;
  2308. id.type = Animation::TYPE_BEZIER;
  2309. id.query = "property '" + p_property + "'";
  2310. id.advance = p_advance;
  2311. //dialog insert
  2312. _query_insert(id);
  2313. inserted = true;
  2314. }
  2315. }
  2316. if (!inserted) {
  2317. InsertData id;
  2318. id.path = np;
  2319. id.track_idx = -1;
  2320. id.value = p_value;
  2321. id.type = Animation::TYPE_VALUE;
  2322. id.query = "property '" + p_property + "'";
  2323. id.advance = p_advance;
  2324. //dialog insert
  2325. _query_insert(id);
  2326. }
  2327. }
  2328. void AnimationTrackEditor::_confirm_insert_list() {
  2329. undo_redo->create_action(TTR("Anim Create & Insert"));
  2330. int last_track = animation->get_track_count();
  2331. while (insert_data.size()) {
  2332. last_track = _confirm_insert(insert_data.front()->get(), last_track, insert_confirm_bezier->is_pressed());
  2333. insert_data.pop_front();
  2334. }
  2335. undo_redo->commit_action();
  2336. }
  2337. PropertyInfo AnimationTrackEditor::_find_hint_for_track(int p_idx, NodePath &r_base_path, Variant *r_current_val) {
  2338. r_base_path = NodePath();
  2339. ERR_FAIL_COND_V(!animation.is_valid(), PropertyInfo());
  2340. ERR_FAIL_INDEX_V(p_idx, animation->get_track_count(), PropertyInfo());
  2341. if (!root) {
  2342. return PropertyInfo();
  2343. }
  2344. NodePath path = animation->track_get_path(p_idx);
  2345. if (!root->has_node_and_resource(path)) {
  2346. return PropertyInfo();
  2347. }
  2348. RES res;
  2349. Vector<StringName> leftover_path;
  2350. Node *node = root->get_node_and_resource(path, res, leftover_path, true);
  2351. if (node) {
  2352. r_base_path = node->get_path();
  2353. }
  2354. if (leftover_path.empty()) {
  2355. if (r_current_val) {
  2356. if (res.is_valid()) {
  2357. *r_current_val = res;
  2358. } else if (node) {
  2359. *r_current_val = node;
  2360. }
  2361. }
  2362. return PropertyInfo();
  2363. }
  2364. Variant property_info_base;
  2365. if (res.is_valid()) {
  2366. property_info_base = res;
  2367. if (r_current_val) {
  2368. *r_current_val = res->get_indexed(leftover_path);
  2369. }
  2370. } else if (node) {
  2371. property_info_base = node;
  2372. if (r_current_val) {
  2373. *r_current_val = node->get_indexed(leftover_path);
  2374. }
  2375. }
  2376. for (int i = 0; i < leftover_path.size() - 1; i++) {
  2377. property_info_base = property_info_base.get_named(leftover_path[i]);
  2378. }
  2379. List<PropertyInfo> pinfo;
  2380. property_info_base.get_property_list(&pinfo);
  2381. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  2382. if (E->get().name == leftover_path[leftover_path.size() - 1]) {
  2383. return E->get();
  2384. }
  2385. }
  2386. return PropertyInfo();
  2387. }
  2388. static Vector<String> _get_bezier_subindices_for_type(Variant::Type p_type, bool *r_valid = NULL) {
  2389. Vector<String> subindices;
  2390. if (r_valid) {
  2391. *r_valid = true;
  2392. }
  2393. switch (p_type) {
  2394. case Variant::INT: {
  2395. subindices.push_back("");
  2396. } break;
  2397. case Variant::REAL: {
  2398. subindices.push_back("");
  2399. } break;
  2400. case Variant::VECTOR2: {
  2401. subindices.push_back(":x");
  2402. subindices.push_back(":y");
  2403. } break;
  2404. case Variant::VECTOR3: {
  2405. subindices.push_back(":x");
  2406. subindices.push_back(":y");
  2407. subindices.push_back(":z");
  2408. } break;
  2409. case Variant::QUAT: {
  2410. subindices.push_back(":x");
  2411. subindices.push_back(":y");
  2412. subindices.push_back(":z");
  2413. subindices.push_back(":w");
  2414. } break;
  2415. case Variant::COLOR: {
  2416. subindices.push_back(":r");
  2417. subindices.push_back(":g");
  2418. subindices.push_back(":b");
  2419. subindices.push_back(":a");
  2420. } break;
  2421. case Variant::PLANE: {
  2422. subindices.push_back(":x");
  2423. subindices.push_back(":y");
  2424. subindices.push_back(":z");
  2425. subindices.push_back(":d");
  2426. } break;
  2427. default: {
  2428. if (r_valid) {
  2429. *r_valid = false;
  2430. }
  2431. }
  2432. }
  2433. return subindices;
  2434. }
  2435. int AnimationTrackEditor::_confirm_insert(InsertData p_id, int p_last_track, bool p_create_beziers) {
  2436. if (p_last_track == -1)
  2437. p_last_track = animation->get_track_count();
  2438. bool created = false;
  2439. if (p_id.track_idx < 0) {
  2440. if (p_create_beziers && (p_id.value.get_type() == Variant::VECTOR2 ||
  2441. p_id.value.get_type() == Variant::VECTOR3 ||
  2442. p_id.value.get_type() == Variant::QUAT ||
  2443. p_id.value.get_type() == Variant::COLOR ||
  2444. p_id.value.get_type() == Variant::PLANE)) {
  2445. Vector<String> subindices = _get_bezier_subindices_for_type(p_id.value.get_type());
  2446. for (int i = 0; i < subindices.size(); i++) {
  2447. InsertData id = p_id;
  2448. id.type = Animation::TYPE_BEZIER;
  2449. id.value = p_id.value.get(subindices[i].substr(1, subindices[i].length()));
  2450. id.path = String(p_id.path) + subindices[i];
  2451. _confirm_insert(id, p_last_track + i);
  2452. }
  2453. return p_last_track + subindices.size() - 1;
  2454. }
  2455. created = true;
  2456. undo_redo->create_action(TTR("Anim Insert Track & Key"));
  2457. Animation::UpdateMode update_mode = Animation::UPDATE_DISCRETE;
  2458. if (p_id.type == Animation::TYPE_VALUE || p_id.type == Animation::TYPE_BEZIER) {
  2459. //wants a new tack
  2460. {
  2461. //hack
  2462. NodePath np;
  2463. animation->add_track(p_id.type);
  2464. animation->track_set_path(animation->get_track_count() - 1, p_id.path);
  2465. PropertyInfo h = _find_hint_for_track(animation->get_track_count() - 1, np);
  2466. animation->remove_track(animation->get_track_count() - 1); //hack
  2467. if (h.type == Variant::REAL ||
  2468. h.type == Variant::VECTOR2 ||
  2469. h.type == Variant::RECT2 ||
  2470. h.type == Variant::VECTOR3 ||
  2471. h.type == Variant::AABB ||
  2472. h.type == Variant::QUAT ||
  2473. h.type == Variant::COLOR ||
  2474. h.type == Variant::PLANE ||
  2475. h.type == Variant::TRANSFORM2D ||
  2476. h.type == Variant::TRANSFORM) {
  2477. update_mode = Animation::UPDATE_CONTINUOUS;
  2478. }
  2479. if (h.usage & PROPERTY_USAGE_ANIMATE_AS_TRIGGER) {
  2480. update_mode = Animation::UPDATE_TRIGGER;
  2481. }
  2482. }
  2483. }
  2484. p_id.track_idx = p_last_track;
  2485. undo_redo->add_do_method(animation.ptr(), "add_track", p_id.type);
  2486. undo_redo->add_do_method(animation.ptr(), "track_set_path", p_id.track_idx, p_id.path);
  2487. if (p_id.type == Animation::TYPE_VALUE)
  2488. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", p_id.track_idx, update_mode);
  2489. } else {
  2490. undo_redo->create_action(TTR("Anim Insert Key"));
  2491. }
  2492. float time = timeline->get_play_position();
  2493. Variant value;
  2494. switch (p_id.type) {
  2495. case Animation::TYPE_VALUE: {
  2496. value = p_id.value;
  2497. } break;
  2498. case Animation::TYPE_TRANSFORM: {
  2499. Transform tr = p_id.value;
  2500. Dictionary d;
  2501. d["location"] = tr.origin;
  2502. d["scale"] = tr.basis.get_scale();
  2503. d["rotation"] = Quat(tr.basis); //.orthonormalized();
  2504. value = d;
  2505. } break;
  2506. case Animation::TYPE_BEZIER: {
  2507. Array array;
  2508. array.resize(5);
  2509. array[0] = p_id.value;
  2510. array[1] = -0.25;
  2511. array[2] = 0;
  2512. array[3] = 0.25;
  2513. array[4] = 0;
  2514. value = array;
  2515. } break;
  2516. case Animation::TYPE_ANIMATION: {
  2517. value = p_id.value;
  2518. } break;
  2519. default: {
  2520. }
  2521. }
  2522. undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, value);
  2523. if (created) {
  2524. //just remove the track
  2525. undo_redo->add_undo_method(animation.ptr(), "remove_track", p_last_track);
  2526. p_last_track++;
  2527. } else {
  2528. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", p_id.track_idx, time);
  2529. int existing = animation->track_find_key(p_id.track_idx, time, true);
  2530. if (existing != -1) {
  2531. Variant v = animation->track_get_key_value(p_id.track_idx, existing);
  2532. float trans = animation->track_get_key_transition(p_id.track_idx, existing);
  2533. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, v, trans);
  2534. }
  2535. }
  2536. /*
  2537. undo_redo->add_do_method(this, "update_tracks");
  2538. undo_redo->add_undo_method(this, "update");
  2539. undo_redo->add_do_method(track_editor, "update");
  2540. undo_redo->add_undo_method(track_editor, "update");
  2541. undo_redo->add_do_method(track_pos, "update");
  2542. undo_redo->add_undo_method(track_pos, "update");
  2543. */
  2544. undo_redo->commit_action();
  2545. return p_last_track;
  2546. }
  2547. void AnimationTrackEditor::show_select_node_warning(bool p_show) {
  2548. }
  2549. bool AnimationTrackEditor::is_key_selected(int p_track, int p_key) const {
  2550. SelectedKey sk;
  2551. sk.key = p_key;
  2552. sk.track = p_track;
  2553. return selection.has(sk);
  2554. }
  2555. bool AnimationTrackEditor::is_selection_active() const {
  2556. return selection.size();
  2557. }
  2558. void AnimationTrackEditor::_update_tracks() {
  2559. int selected = _get_track_selected();
  2560. while (track_vbox->get_child_count()) {
  2561. memdelete(track_vbox->get_child(0));
  2562. }
  2563. track_edits.clear();
  2564. groups.clear();
  2565. if (animation.is_null())
  2566. return;
  2567. Map<String, VBoxContainer *> group_sort;
  2568. bool use_grouping = !view_group->is_pressed();
  2569. bool use_filter = selected_filter->is_pressed();
  2570. for (int i = 0; i < animation->get_track_count(); i++) {
  2571. AnimationTrackEdit *track_edit = NULL;
  2572. //find hint and info for plugin
  2573. if (use_filter) {
  2574. NodePath path = animation->track_get_path(i);
  2575. if (root && root->has_node(path)) {
  2576. Node *node = root->get_node(path);
  2577. if (!node) {
  2578. continue; // no node, no filter
  2579. }
  2580. if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
  2581. continue; //skip track due to not selected
  2582. }
  2583. }
  2584. }
  2585. if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
  2586. NodePath path = animation->track_get_path(i);
  2587. if (root && root->has_node_and_resource(path)) {
  2588. RES res;
  2589. NodePath base_path;
  2590. Vector<StringName> leftover_path;
  2591. Node *node = root->get_node_and_resource(path, res, leftover_path, true);
  2592. PropertyInfo pinfo = _find_hint_for_track(i, base_path);
  2593. Object *object = node;
  2594. if (res.is_valid()) {
  2595. object = res.ptr();
  2596. }
  2597. if (object && !leftover_path.empty()) {
  2598. if (pinfo.name.empty()) {
  2599. pinfo.name = leftover_path[leftover_path.size() - 1];
  2600. }
  2601. for (int j = 0; j < track_edit_plugins.size(); j++) {
  2602. track_edit = track_edit_plugins.write[j]->create_value_track_edit(object, pinfo.type, pinfo.name, pinfo.hint, pinfo.hint_string, pinfo.usage);
  2603. if (track_edit) {
  2604. break;
  2605. }
  2606. }
  2607. }
  2608. }
  2609. }
  2610. if (animation->track_get_type(i) == Animation::TYPE_AUDIO) {
  2611. for (int j = 0; j < track_edit_plugins.size(); j++) {
  2612. track_edit = track_edit_plugins.write[j]->create_audio_track_edit();
  2613. if (track_edit) {
  2614. break;
  2615. }
  2616. }
  2617. }
  2618. if (animation->track_get_type(i) == Animation::TYPE_ANIMATION) {
  2619. NodePath path = animation->track_get_path(i);
  2620. Node *node = NULL;
  2621. if (root && root->has_node(path)) {
  2622. node = root->get_node(path);
  2623. }
  2624. if (node && Object::cast_to<AnimationPlayer>(node)) {
  2625. for (int j = 0; j < track_edit_plugins.size(); j++) {
  2626. track_edit = track_edit_plugins.write[j]->create_animation_track_edit(node);
  2627. if (track_edit) {
  2628. break;
  2629. }
  2630. }
  2631. }
  2632. }
  2633. if (track_edit == NULL) {
  2634. //no valid plugin_found
  2635. track_edit = memnew(AnimationTrackEdit);
  2636. }
  2637. track_edits.push_back(track_edit);
  2638. if (use_grouping) {
  2639. String base_path = animation->track_get_path(i);
  2640. base_path = base_path.get_slice(":", 0); // remove subpath
  2641. if (!group_sort.has(base_path)) {
  2642. AnimationTrackEditGroup *g = memnew(AnimationTrackEditGroup);
  2643. Ref<Texture> icon = get_icon("Node", "EditorIcons");
  2644. String name = base_path;
  2645. String tooltip;
  2646. if (root) {
  2647. Node *n = root->get_node(base_path);
  2648. if (n) {
  2649. if (has_icon(n->get_class(), "EditorIcons")) {
  2650. icon = get_icon(n->get_class(), "EditorIcons");
  2651. }
  2652. name = n->get_name();
  2653. tooltip = root->get_path_to(n);
  2654. }
  2655. }
  2656. g->set_type_and_name(icon, name, animation->track_get_path(i));
  2657. g->set_root(root);
  2658. g->set_tooltip(tooltip);
  2659. g->set_timeline(timeline);
  2660. groups.push_back(g);
  2661. VBoxContainer *vb = memnew(VBoxContainer);
  2662. vb->add_constant_override("separation", 0);
  2663. vb->add_child(g);
  2664. track_vbox->add_child(vb);
  2665. group_sort[base_path] = vb;
  2666. }
  2667. track_edit->set_in_group(true);
  2668. group_sort[base_path]->add_child(track_edit);
  2669. } else {
  2670. track_edit->set_in_group(false);
  2671. track_vbox->add_child(track_edit);
  2672. }
  2673. track_edit->set_undo_redo(undo_redo);
  2674. track_edit->set_timeline(timeline);
  2675. track_edit->set_block_animation_update_ptr(&block_animation_update);
  2676. track_edit->set_root(root);
  2677. track_edit->set_animation_and_track(animation, i);
  2678. track_edit->set_play_position(timeline->get_play_position());
  2679. track_edit->set_editor(this);
  2680. if (selected == i) {
  2681. track_edit->grab_focus();
  2682. }
  2683. track_edit->connect("timeline_changed", this, "_timeline_changed");
  2684. track_edit->connect("remove_request", this, "_track_remove_request", varray(), CONNECT_DEFERRED);
  2685. track_edit->connect("dropped", this, "_dropped_track", varray(), CONNECT_DEFERRED);
  2686. track_edit->connect("insert_key", this, "_insert_key_from_track", varray(i), CONNECT_DEFERRED);
  2687. track_edit->connect("select_key", this, "_key_selected", varray(i), CONNECT_DEFERRED);
  2688. track_edit->connect("deselect_key", this, "_key_deselected", varray(i), CONNECT_DEFERRED);
  2689. track_edit->connect("bezier_edit", this, "_bezier_edit", varray(i), CONNECT_DEFERRED);
  2690. track_edit->connect("clear_selection", this, "_clear_selection");
  2691. track_edit->connect("move_selection_begin", this, "_move_selection_begin");
  2692. track_edit->connect("move_selection", this, "_move_selection");
  2693. track_edit->connect("move_selection_commit", this, "_move_selection_commit");
  2694. track_edit->connect("move_selection_cancel", this, "_move_selection_cancel");
  2695. track_edit->connect("duplicate_request", this, "_edit_menu_pressed", varray(EDIT_DUPLICATE_SELECTION), CONNECT_DEFERRED);
  2696. track_edit->connect("duplicate_transpose_request", this, "_edit_menu_pressed", varray(EDIT_DUPLICATE_TRANSPOSED), CONNECT_DEFERRED);
  2697. track_edit->connect("delete_request", this, "_edit_menu_pressed", varray(EDIT_DELETE_SELECTION), CONNECT_DEFERRED);
  2698. }
  2699. }
  2700. void AnimationTrackEditor::_animation_changed() {
  2701. timeline->update();
  2702. timeline->update_values();
  2703. if (block_animation_update) {
  2704. for (int i = 0; i < track_edits.size(); i++) {
  2705. track_edits[i]->update();
  2706. }
  2707. for (int i = 0; i < groups.size(); i++) {
  2708. groups[i]->update();
  2709. }
  2710. } else {
  2711. _update_tracks();
  2712. }
  2713. bezier_edit->update();
  2714. step->set_block_signals(true);
  2715. step->set_value(animation->get_step());
  2716. step->set_block_signals(false);
  2717. }
  2718. MenuButton *AnimationTrackEditor::get_edit_menu() {
  2719. return edit;
  2720. }
  2721. void AnimationTrackEditor::_notification(int p_what) {
  2722. if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) {
  2723. zoom_icon->set_texture(get_icon("Zoom", "EditorIcons"));
  2724. snap->set_icon(get_icon("Snap", "EditorIcons"));
  2725. view_group->set_icon(get_icon(view_group->is_pressed() ? "AnimationTrackList" : "AnimationTrackGroup", "EditorIcons"));
  2726. selected_filter->set_icon(get_icon("AnimationFilter", "EditorIcons"));
  2727. main_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
  2728. }
  2729. if (p_what == NOTIFICATION_READY) {
  2730. EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", this, "_selection_changed");
  2731. }
  2732. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2733. update_keying();
  2734. EditorNode::get_singleton()->update_keying();
  2735. emit_signal("keying_changed");
  2736. }
  2737. }
  2738. void AnimationTrackEditor::_update_scroll(double) {
  2739. for (int i = 0; i < track_edits.size(); i++) {
  2740. track_edits[i]->update();
  2741. }
  2742. for (int i = 0; i < groups.size(); i++) {
  2743. groups[i]->update();
  2744. }
  2745. }
  2746. void AnimationTrackEditor::_update_step(double p_new_step) {
  2747. undo_redo->create_action("Change animation step");
  2748. undo_redo->add_do_method(animation.ptr(), "set_step", p_new_step);
  2749. undo_redo->add_undo_method(animation.ptr(), "set_step", animation->get_step());
  2750. step->set_block_signals(true);
  2751. undo_redo->commit_action();
  2752. step->set_block_signals(false);
  2753. emit_signal("animation_step_changed", p_new_step);
  2754. }
  2755. void AnimationTrackEditor::_update_length(double p_new_len) {
  2756. emit_signal("animation_len_changed", p_new_len);
  2757. }
  2758. void AnimationTrackEditor::_dropped_track(int p_from_track, int p_to_track) {
  2759. if (p_to_track >= track_edits.size()) {
  2760. p_to_track = track_edits.size() - 1;
  2761. }
  2762. if (p_from_track == p_to_track)
  2763. return;
  2764. _clear_selection();
  2765. undo_redo->create_action("Rearrange tracks");
  2766. undo_redo->add_do_method(animation.ptr(), "track_swap", p_from_track, p_to_track);
  2767. undo_redo->add_undo_method(animation.ptr(), "track_swap", p_to_track, p_from_track);
  2768. undo_redo->commit_action();
  2769. }
  2770. void AnimationTrackEditor::_new_track_node_selected(NodePath p_path) {
  2771. ERR_FAIL_COND(!root);
  2772. Node *node = get_node(p_path);
  2773. ERR_FAIL_COND(!node);
  2774. NodePath path_to = root->get_path_to(node);
  2775. if (adding_track_type == Animation::TYPE_TRANSFORM && !node->is_class("Spatial")) {
  2776. EditorNode::get_singleton()->show_warning(TTR("Transform tracks only apply to Spatial-based nodes."));
  2777. return;
  2778. }
  2779. switch (adding_track_type) {
  2780. case Animation::TYPE_VALUE: {
  2781. adding_track_path = path_to;
  2782. prop_selector->set_type_filter(Vector<Variant::Type>());
  2783. prop_selector->select_property_from_instance(node);
  2784. } break;
  2785. case Animation::TYPE_TRANSFORM:
  2786. case Animation::TYPE_METHOD: {
  2787. undo_redo->create_action("Add Track");
  2788. undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
  2789. undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path_to);
  2790. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  2791. undo_redo->commit_action();
  2792. } break;
  2793. case Animation::TYPE_BEZIER: {
  2794. Vector<Variant::Type> filter;
  2795. filter.push_back(Variant::INT);
  2796. filter.push_back(Variant::REAL);
  2797. filter.push_back(Variant::VECTOR2);
  2798. filter.push_back(Variant::VECTOR3);
  2799. filter.push_back(Variant::QUAT);
  2800. filter.push_back(Variant::PLANE);
  2801. filter.push_back(Variant::COLOR);
  2802. adding_track_path = path_to;
  2803. prop_selector->set_type_filter(filter);
  2804. prop_selector->select_property_from_instance(node);
  2805. } break;
  2806. case Animation::TYPE_AUDIO: {
  2807. if (!node->is_class("AudioStreamPlayer") && !node->is_class("AudioStreamPlayer2D") && !node->is_class("AudioStreamPlayer3D")) {
  2808. EditorNode::get_singleton()->show_warning(TTR("Audio tracks can only point to nodes of type:\n-AudioStreamPlayer\n-AudioStreamPlayer2D\n-AudioStreamPlayer3D"));
  2809. return;
  2810. }
  2811. undo_redo->create_action("Add Track");
  2812. undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
  2813. undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path_to);
  2814. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  2815. undo_redo->commit_action();
  2816. } break;
  2817. case Animation::TYPE_ANIMATION: {
  2818. if (!node->is_class("AnimationPlayer")) {
  2819. EditorNode::get_singleton()->show_warning(TTR("Animation tracks can only point to AnimationPlayer nodes."));
  2820. return;
  2821. }
  2822. if (node == AnimationPlayerEditor::singleton->get_player()) {
  2823. EditorNode::get_singleton()->show_warning(TTR("An animation player can't animate itself, only other players."));
  2824. return;
  2825. }
  2826. undo_redo->create_action("Add Track");
  2827. undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
  2828. undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path_to);
  2829. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  2830. undo_redo->commit_action();
  2831. } break;
  2832. }
  2833. }
  2834. void AnimationTrackEditor::_add_track(int p_type) {
  2835. if (!root) {
  2836. EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new track without a root"));
  2837. return;
  2838. }
  2839. adding_track_type = p_type;
  2840. pick_track->popup_centered_ratio();
  2841. }
  2842. void AnimationTrackEditor::_new_track_property_selected(String p_name) {
  2843. String full_path = String(adding_track_path) + ":" + p_name;
  2844. if (adding_track_type == Animation::TYPE_VALUE) {
  2845. Animation::UpdateMode update_mode = Animation::UPDATE_DISCRETE;
  2846. {
  2847. //hack
  2848. NodePath np;
  2849. animation->add_track(Animation::TYPE_VALUE);
  2850. animation->track_set_path(animation->get_track_count() - 1, full_path);
  2851. PropertyInfo h = _find_hint_for_track(animation->get_track_count() - 1, np);
  2852. animation->remove_track(animation->get_track_count() - 1); //hack
  2853. if (h.type == Variant::REAL ||
  2854. h.type == Variant::VECTOR2 ||
  2855. h.type == Variant::RECT2 ||
  2856. h.type == Variant::VECTOR3 ||
  2857. h.type == Variant::AABB ||
  2858. h.type == Variant::QUAT ||
  2859. h.type == Variant::COLOR ||
  2860. h.type == Variant::PLANE ||
  2861. h.type == Variant::TRANSFORM2D ||
  2862. h.type == Variant::TRANSFORM) {
  2863. update_mode = Animation::UPDATE_CONTINUOUS;
  2864. }
  2865. if (h.usage & PROPERTY_USAGE_ANIMATE_AS_TRIGGER) {
  2866. update_mode = Animation::UPDATE_TRIGGER;
  2867. }
  2868. }
  2869. undo_redo->create_action("Add Track");
  2870. undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
  2871. undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), full_path);
  2872. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", animation->get_track_count(), update_mode);
  2873. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  2874. undo_redo->commit_action();
  2875. } else {
  2876. Vector<String> subindices;
  2877. {
  2878. //hack
  2879. NodePath np;
  2880. animation->add_track(Animation::TYPE_VALUE);
  2881. animation->track_set_path(animation->get_track_count() - 1, full_path);
  2882. PropertyInfo h = _find_hint_for_track(animation->get_track_count() - 1, np);
  2883. animation->remove_track(animation->get_track_count() - 1); //hack
  2884. bool valid;
  2885. subindices = _get_bezier_subindices_for_type(h.type, &valid);
  2886. if (!valid) {
  2887. EditorNode::get_singleton()->show_warning("Invalid track for Bezier (no suitable sub-properties)");
  2888. return;
  2889. }
  2890. }
  2891. undo_redo->create_action("Add Bezier Track");
  2892. int base_track = animation->get_track_count();
  2893. for (int i = 0; i < subindices.size(); i++) {
  2894. undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
  2895. undo_redo->add_do_method(animation.ptr(), "track_set_path", base_track + i, full_path + subindices[i]);
  2896. undo_redo->add_undo_method(animation.ptr(), "remove_track", base_track + i);
  2897. }
  2898. undo_redo->commit_action();
  2899. }
  2900. }
  2901. void AnimationTrackEditor::_timeline_value_changed(double) {
  2902. timeline->update_play_position();
  2903. for (int i = 0; i < track_edits.size(); i++) {
  2904. track_edits[i]->update();
  2905. track_edits[i]->update_play_position();
  2906. }
  2907. for (int i = 0; i < groups.size(); i++) {
  2908. groups[i]->update();
  2909. }
  2910. bezier_edit->update();
  2911. bezier_edit->update_play_position();
  2912. }
  2913. int AnimationTrackEditor::_get_track_selected() {
  2914. for (int i = 0; i < track_edits.size(); i++) {
  2915. if (track_edits[i]->has_focus())
  2916. return i;
  2917. }
  2918. return -1;
  2919. }
  2920. void AnimationTrackEditor::_insert_key_from_track(float p_ofs, int p_track) {
  2921. ERR_FAIL_INDEX(p_track, animation->get_track_count());
  2922. if (snap->is_pressed() && step->get_value() != 0) {
  2923. p_ofs = Math::stepify(p_ofs, step->get_value());
  2924. }
  2925. while (animation->track_find_key(p_track, p_ofs, true) != -1) { //make sure insertion point is valid
  2926. p_ofs += 0.001;
  2927. }
  2928. switch (animation->track_get_type(p_track)) {
  2929. case Animation::TYPE_TRANSFORM: {
  2930. if (!root->has_node(animation->track_get_path(p_track))) {
  2931. EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a key."));
  2932. return;
  2933. }
  2934. Spatial *base = Object::cast_to<Spatial>(root->get_node(animation->track_get_path(p_track)));
  2935. if (!base) {
  2936. EditorNode::get_singleton()->show_warning(TTR("Track is not of type Spatial, can't insert key"));
  2937. return;
  2938. }
  2939. Transform xf = base->get_transform();
  2940. Vector3 loc = xf.get_origin();
  2941. Vector3 scale = xf.basis.get_scale_local();
  2942. Quat rot = xf.basis;
  2943. undo_redo->create_action("Add Transform Track Key");
  2944. undo_redo->add_do_method(animation.ptr(), "transform_track_insert_key", p_track, p_ofs, loc, rot, scale);
  2945. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", p_track, p_ofs);
  2946. undo_redo->commit_action();
  2947. } break;
  2948. case Animation::TYPE_VALUE: {
  2949. NodePath bp;
  2950. Variant value;
  2951. _find_hint_for_track(p_track, bp, &value);
  2952. undo_redo->create_action("Add Track Key");
  2953. undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, value);
  2954. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", p_track, p_ofs);
  2955. undo_redo->commit_action();
  2956. } break;
  2957. case Animation::TYPE_METHOD: {
  2958. if (!root->has_node(animation->track_get_path(p_track))) {
  2959. EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a method key."));
  2960. return;
  2961. }
  2962. Node *base = root->get_node(animation->track_get_path(p_track));
  2963. method_selector->select_method_from_instance(base);
  2964. insert_key_from_track_call_ofs = p_ofs;
  2965. insert_key_from_track_call_track = p_track;
  2966. } break;
  2967. case Animation::TYPE_BEZIER: {
  2968. NodePath bp;
  2969. Variant value;
  2970. _find_hint_for_track(p_track, bp, &value);
  2971. Array arr;
  2972. arr.resize(5);
  2973. arr[0] = value;
  2974. arr[1] = -0.25;
  2975. arr[2] = 0;
  2976. arr[3] = 0.25;
  2977. arr[4] = 0;
  2978. undo_redo->create_action("Add Track Key");
  2979. undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, arr);
  2980. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", p_track, p_ofs);
  2981. undo_redo->commit_action();
  2982. } break;
  2983. case Animation::TYPE_AUDIO: {
  2984. Dictionary ak;
  2985. ak["stream"] = RES();
  2986. ak["start_offset"] = 0;
  2987. ak["end_offset"] = 0;
  2988. undo_redo->create_action("Add Track Key");
  2989. undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, ak);
  2990. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", p_track, p_ofs);
  2991. undo_redo->commit_action();
  2992. } break;
  2993. case Animation::TYPE_ANIMATION: {
  2994. StringName anim = "[stop]";
  2995. undo_redo->create_action("Add Track Key");
  2996. undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_track, p_ofs, anim);
  2997. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", p_track, p_ofs);
  2998. undo_redo->commit_action();
  2999. } break;
  3000. }
  3001. }
  3002. void AnimationTrackEditor::_add_method_key(const String &p_method) {
  3003. if (!root->has_node(animation->track_get_path(insert_key_from_track_call_track))) {
  3004. EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a method key."));
  3005. return;
  3006. }
  3007. Node *base = root->get_node(animation->track_get_path(insert_key_from_track_call_track));
  3008. List<MethodInfo> minfo;
  3009. base->get_method_list(&minfo);
  3010. for (List<MethodInfo>::Element *E = minfo.front(); E; E = E->next()) {
  3011. if (E->get().name == p_method) {
  3012. Dictionary d;
  3013. d["method"] = p_method;
  3014. Array params;
  3015. int first_defarg = E->get().arguments.size() - E->get().default_arguments.size();
  3016. for (int i = 0; i < E->get().arguments.size(); i++) {
  3017. if (i >= first_defarg) {
  3018. Variant arg = E->get().default_arguments[i - first_defarg];
  3019. params.push_back(arg);
  3020. } else {
  3021. Variant::CallError ce;
  3022. Variant arg = Variant::construct(E->get().arguments[i].type, NULL, 0, ce);
  3023. params.push_back(arg);
  3024. }
  3025. }
  3026. d["args"] = params;
  3027. undo_redo->create_action("Add Method Track Key");
  3028. undo_redo->add_do_method(animation.ptr(), "track_insert_key", insert_key_from_track_call_track, insert_key_from_track_call_ofs, d);
  3029. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", insert_key_from_track_call_track, insert_key_from_track_call_ofs);
  3030. undo_redo->commit_action();
  3031. return;
  3032. }
  3033. }
  3034. EditorNode::get_singleton()->show_warning(TTR("Method not found in object: ") + p_method);
  3035. }
  3036. void AnimationTrackEditor::_key_selected(int p_key, bool p_single, int p_track) {
  3037. ERR_FAIL_INDEX(p_track, animation->get_track_count());
  3038. ERR_FAIL_INDEX(p_key, animation->track_get_key_count(p_track));
  3039. SelectedKey sk;
  3040. sk.key = p_key;
  3041. sk.track = p_track;
  3042. if (p_single) {
  3043. _clear_selection();
  3044. }
  3045. KeyInfo ki;
  3046. ki.pos = animation->track_get_key_time(p_track, p_key);
  3047. selection[sk] = ki;
  3048. for (int i = 0; i < track_edits.size(); i++) {
  3049. track_edits[i]->update();
  3050. }
  3051. _update_key_edit();
  3052. }
  3053. void AnimationTrackEditor::_key_deselected(int p_key, int p_track) {
  3054. ERR_FAIL_INDEX(p_track, animation->get_track_count());
  3055. ERR_FAIL_INDEX(p_key, animation->track_get_key_count(p_track));
  3056. SelectedKey sk;
  3057. sk.key = p_key;
  3058. sk.track = p_track;
  3059. selection.erase(sk);
  3060. for (int i = 0; i < track_edits.size(); i++) {
  3061. track_edits[i]->update();
  3062. }
  3063. _update_key_edit();
  3064. }
  3065. void AnimationTrackEditor::_move_selection_begin() {
  3066. moving_selection = true;
  3067. moving_selection_offset = 0;
  3068. }
  3069. void AnimationTrackEditor::_move_selection(float p_offset) {
  3070. moving_selection_offset = p_offset;
  3071. for (int i = 0; i < track_edits.size(); i++) {
  3072. track_edits[i]->update();
  3073. }
  3074. }
  3075. struct _AnimMoveRestore {
  3076. int track;
  3077. float time;
  3078. Variant key;
  3079. float transition;
  3080. };
  3081. //used for undo/redo
  3082. void AnimationTrackEditor::_clear_key_edit() {
  3083. if (key_edit) {
  3084. #if 0
  3085. // going back seems like the most comfortable thing to do, but it results
  3086. // in weird behaviors and crashes, because going back to animation editor
  3087. // triggers the editor setting up again itself
  3088. bool go_back = false;
  3089. if (EditorNode::get_singleton()->get_inspector()->get_edited_object() == key_edit) {
  3090. EditorNode::get_singleton()->push_item(NULL);
  3091. go_back = true;
  3092. }
  3093. memdelete(key_edit);
  3094. key_edit = NULL;
  3095. if (go_back) {
  3096. EditorNode::get_singleton()->get_inspector_dock()->go_back();
  3097. }
  3098. #else
  3099. //if key edit is the object being inspected, remove it first
  3100. if (EditorNode::get_singleton()->get_inspector()->get_edited_object() == key_edit) {
  3101. EditorNode::get_singleton()->push_item(NULL);
  3102. }
  3103. //then actually delete it
  3104. memdelete(key_edit);
  3105. key_edit = NULL;
  3106. #endif
  3107. }
  3108. }
  3109. void AnimationTrackEditor::_clear_selection() {
  3110. selection.clear();
  3111. for (int i = 0; i < track_edits.size(); i++) {
  3112. track_edits[i]->update();
  3113. }
  3114. _clear_key_edit();
  3115. }
  3116. void AnimationTrackEditor::_update_key_edit() {
  3117. _clear_key_edit();
  3118. if (!animation.is_valid())
  3119. return;
  3120. if (selection.size() != 1) {
  3121. return;
  3122. }
  3123. key_edit = memnew(AnimationTrackKeyEdit);
  3124. key_edit->animation = animation;
  3125. key_edit->track = selection.front()->key().track;
  3126. float ofs = animation->track_get_key_time(key_edit->track, selection.front()->key().key);
  3127. key_edit->key_ofs = ofs;
  3128. key_edit->root_path = root;
  3129. NodePath np;
  3130. key_edit->hint = _find_hint_for_track(key_edit->track, np);
  3131. key_edit->undo_redo = undo_redo;
  3132. key_edit->base = np;
  3133. EditorNode::get_singleton()->push_item(key_edit);
  3134. }
  3135. void AnimationTrackEditor::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
  3136. if (!(animation == p_anim))
  3137. return;
  3138. //selection.clear();
  3139. _clear_selection();
  3140. }
  3141. void AnimationTrackEditor::_select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos) {
  3142. if (!(animation == p_anim))
  3143. return;
  3144. int idx = animation->track_find_key(p_track, p_pos, true);
  3145. ERR_FAIL_COND(idx < 0);
  3146. SelectedKey sk;
  3147. sk.track = p_track;
  3148. sk.key = idx;
  3149. KeyInfo ki;
  3150. ki.pos = p_pos;
  3151. selection.insert(sk, ki);
  3152. }
  3153. void AnimationTrackEditor::_move_selection_commit() {
  3154. undo_redo->create_action(TTR("Anim Move Keys"));
  3155. List<_AnimMoveRestore> to_restore;
  3156. float motion = moving_selection_offset;
  3157. // 1-remove the keys
  3158. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3159. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  3160. }
  3161. // 2- remove overlapped keys
  3162. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3163. float newtime = snap_time(E->get().pos + motion);
  3164. int idx = animation->track_find_key(E->key().track, newtime, true);
  3165. if (idx == -1)
  3166. continue;
  3167. SelectedKey sk;
  3168. sk.key = idx;
  3169. sk.track = E->key().track;
  3170. if (selection.has(sk))
  3171. continue; //already in selection, don't save
  3172. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newtime);
  3173. _AnimMoveRestore amr;
  3174. amr.key = animation->track_get_key_value(E->key().track, idx);
  3175. amr.track = E->key().track;
  3176. amr.time = newtime;
  3177. amr.transition = animation->track_get_key_transition(E->key().track, idx);
  3178. to_restore.push_back(amr);
  3179. }
  3180. // 3-move the keys (re insert them)
  3181. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3182. float newpos = snap_time(E->get().pos + motion);
  3183. /*
  3184. if (newpos<0)
  3185. continue; //no add at the beginning
  3186. */
  3187. undo_redo->add_do_method(animation.ptr(), "track_insert_key", E->key().track, newpos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  3188. }
  3189. // 4-(undo) remove inserted keys
  3190. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3191. float newpos = snap_time(E->get().pos + motion);
  3192. /*
  3193. if (newpos<0)
  3194. continue; //no remove what no inserted
  3195. */
  3196. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newpos);
  3197. }
  3198. // 5-(undo) reinsert keys
  3199. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3200. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  3201. }
  3202. // 6-(undo) reinsert overlapped keys
  3203. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  3204. _AnimMoveRestore &amr = E->get();
  3205. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  3206. }
  3207. // 6-(undo) reinsert overlapped keys
  3208. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  3209. _AnimMoveRestore &amr = E->get();
  3210. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  3211. }
  3212. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  3213. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  3214. // 7-reselect
  3215. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3216. float oldpos = E->get().pos;
  3217. float newpos = snap_time(oldpos + motion);
  3218. //if (newpos>=0)
  3219. undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
  3220. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
  3221. }
  3222. undo_redo->commit_action();
  3223. moving_selection = false;
  3224. for (int i = 0; i < track_edits.size(); i++) {
  3225. track_edits[i]->update();
  3226. }
  3227. _update_key_edit();
  3228. }
  3229. void AnimationTrackEditor::_move_selection_cancel() {
  3230. moving_selection = false;
  3231. for (int i = 0; i < track_edits.size(); i++) {
  3232. track_edits[i]->update();
  3233. }
  3234. }
  3235. bool AnimationTrackEditor::is_moving_selection() const {
  3236. return moving_selection;
  3237. }
  3238. float AnimationTrackEditor::get_moving_selection_offset() const {
  3239. return moving_selection_offset;
  3240. }
  3241. void AnimationTrackEditor::_box_selection_draw() {
  3242. Color color = get_color("accent_color", "Editor");
  3243. color.a = 0.2;
  3244. Rect2 rect = Rect2(Point2(), box_selection->get_size());
  3245. box_selection->draw_rect(rect, color);
  3246. }
  3247. void AnimationTrackEditor::_scroll_input(const Ref<InputEvent> &p_event) {
  3248. Ref<InputEventMouseButton> mb = p_event;
  3249. if (mb.is_valid() && mb->is_pressed() && mb->get_command() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  3250. timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() * 1.05);
  3251. scroll->accept_event();
  3252. }
  3253. if (mb.is_valid() && mb->is_pressed() && mb->get_command() && mb->get_button_index() == BUTTON_WHEEL_UP) {
  3254. timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() / 1.05);
  3255. scroll->accept_event();
  3256. }
  3257. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  3258. if (mb->is_pressed()) {
  3259. box_selecting = true;
  3260. box_selecting_from = scroll->get_global_transform().xform(mb->get_position());
  3261. box_select_rect = Rect2();
  3262. } else if (box_selecting) {
  3263. if (box_selection->is_visible_in_tree()) {
  3264. //only if moved
  3265. for (int i = 0; i < track_edits.size(); i++) {
  3266. Rect2 local_rect = box_select_rect;
  3267. local_rect.position -= track_edits[i]->get_global_position();
  3268. track_edits[i]->append_to_selection(local_rect);
  3269. }
  3270. if (_get_track_selected() == -1 && track_edits.size() > 0) { //minimal hack to make shortcuts work
  3271. track_edits[track_edits.size() - 1]->grab_focus();
  3272. }
  3273. } else {
  3274. _clear_selection(); //clear it
  3275. }
  3276. box_selection->hide();
  3277. box_selecting = false;
  3278. }
  3279. }
  3280. Ref<InputEventMouseMotion> mm = p_event;
  3281. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_MIDDLE) {
  3282. timeline->set_value(timeline->get_value() - mm->get_relative().x / timeline->get_zoom_scale());
  3283. }
  3284. if (mm.is_valid() && box_selecting) {
  3285. if (!(mm->get_button_mask() & BUTTON_MASK_LEFT)) {
  3286. //no longer
  3287. box_selection->hide();
  3288. box_selecting = false;
  3289. return;
  3290. }
  3291. if (!box_selection->is_visible_in_tree()) {
  3292. if (!mm->get_shift()) {
  3293. _clear_selection(); //only append if shift is pressed
  3294. }
  3295. box_selection->show();
  3296. }
  3297. Vector2 from = box_selecting_from;
  3298. Vector2 to = scroll->get_global_transform().xform(mm->get_position());
  3299. if (from.x > to.x) {
  3300. SWAP(from.x, to.x);
  3301. }
  3302. if (from.y > to.y) {
  3303. SWAP(from.y, to.y);
  3304. }
  3305. Rect2 rect(from, to - from);
  3306. Rect2 scroll_rect = Rect2(scroll->get_global_position(), scroll->get_size());
  3307. rect = scroll_rect.clip(rect);
  3308. box_selection->set_position(rect.position);
  3309. box_selection->set_size(rect.size);
  3310. box_select_rect = rect;
  3311. if (get_local_mouse_position().y < 0) {
  3312. //avoid box selection from going up and lose focus to viewport
  3313. warp_mouse(Vector2(mm->get_position().x, 0));
  3314. }
  3315. }
  3316. }
  3317. void AnimationTrackEditor::_cancel_bezier_edit() {
  3318. bezier_edit->hide();
  3319. scroll->show();
  3320. }
  3321. void AnimationTrackEditor::_bezier_edit(int p_for_track) {
  3322. _clear_selection(); //bezier probably wants to use a separate selection mode
  3323. bezier_edit->set_root(root);
  3324. bezier_edit->set_animation_and_track(animation, p_for_track);
  3325. scroll->hide();
  3326. bezier_edit->show();
  3327. //search everything within the track and curve- edit it
  3328. }
  3329. void AnimationTrackEditor::_anim_duplicate_keys(bool transpose) {
  3330. //duplicait!
  3331. if (selection.size() && animation.is_valid() && (!transpose || (_get_track_selected() >= 0 && _get_track_selected() < animation->get_track_count()))) {
  3332. int top_track = 0x7FFFFFFF;
  3333. float top_time = 1e10;
  3334. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3335. const SelectedKey &sk = E->key();
  3336. float t = animation->track_get_key_time(sk.track, sk.key);
  3337. if (t < top_time)
  3338. top_time = t;
  3339. if (sk.track < top_track)
  3340. top_track = sk.track;
  3341. }
  3342. ERR_FAIL_COND(top_track == 0x7FFFFFFF || top_time == 1e10);
  3343. //
  3344. int start_track = transpose ? _get_track_selected() : top_track;
  3345. undo_redo->create_action(TTR("Anim Duplicate Keys"));
  3346. List<Pair<int, float> > new_selection_values;
  3347. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3348. const SelectedKey &sk = E->key();
  3349. float t = animation->track_get_key_time(sk.track, sk.key);
  3350. float dst_time = t + (timeline->get_play_position() - top_time);
  3351. int dst_track = sk.track + (start_track - top_track);
  3352. if (dst_track < 0 || dst_track >= animation->get_track_count())
  3353. continue;
  3354. if (animation->track_get_type(dst_track) != animation->track_get_type(sk.track))
  3355. continue;
  3356. int existing_idx = animation->track_find_key(dst_track, dst_time, true);
  3357. undo_redo->add_do_method(animation.ptr(), "track_insert_key", dst_track, dst_time, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  3358. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", dst_track, dst_time);
  3359. Pair<int, float> p;
  3360. p.first = dst_track;
  3361. p.second = dst_time;
  3362. new_selection_values.push_back(p);
  3363. if (existing_idx != -1) {
  3364. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", dst_track, dst_time, animation->track_get_key_value(dst_track, existing_idx), animation->track_get_key_transition(dst_track, existing_idx));
  3365. }
  3366. }
  3367. undo_redo->commit_action();
  3368. //reselect duplicated
  3369. Map<SelectedKey, KeyInfo> new_selection;
  3370. for (List<Pair<int, float> >::Element *E = new_selection_values.front(); E; E = E->next()) {
  3371. int track = E->get().first;
  3372. float time = E->get().second;
  3373. int existing_idx = animation->track_find_key(track, time, true);
  3374. if (existing_idx == -1)
  3375. continue;
  3376. SelectedKey sk2;
  3377. sk2.track = track;
  3378. sk2.key = existing_idx;
  3379. KeyInfo ki;
  3380. ki.pos = time;
  3381. new_selection[sk2] = ki;
  3382. }
  3383. selection = new_selection;
  3384. _update_tracks();
  3385. _update_key_edit();
  3386. }
  3387. }
  3388. void AnimationTrackEditor::_edit_menu_pressed(int p_option) {
  3389. last_menu_track_opt = p_option;
  3390. switch (p_option) {
  3391. case EDIT_COPY_TRACKS: {
  3392. track_copy_select->clear();
  3393. TreeItem *troot = track_copy_select->create_item();
  3394. for (int i = 0; i < animation->get_track_count(); i++) {
  3395. NodePath path = animation->track_get_path(i);
  3396. Node *node = NULL;
  3397. if (root && root->has_node(path)) {
  3398. node = root->get_node(path);
  3399. }
  3400. String text;
  3401. Ref<Texture> icon = get_icon("Node", "EditorIcons");
  3402. if (node) {
  3403. if (has_icon(node->get_class(), "EditorIcons")) {
  3404. icon = get_icon(node->get_class(), "EditorIcons");
  3405. }
  3406. text = node->get_name();
  3407. Vector<StringName> sn = path.get_subnames();
  3408. for (int i = 0; i < sn.size(); i++) {
  3409. text += ".";
  3410. text += sn[i];
  3411. }
  3412. path = NodePath(node->get_path().get_names(), path.get_subnames(), true); //store full path instead for copying
  3413. } else {
  3414. text = path;
  3415. int sep = text.find(":");
  3416. if (sep != -1) {
  3417. text = text.substr(sep + 1, text.length());
  3418. }
  3419. }
  3420. switch (animation->track_get_type(i)) {
  3421. case Animation::TYPE_TRANSFORM: text += " (Transform)"; break;
  3422. case Animation::TYPE_METHOD: text += " (Methods)"; break;
  3423. case Animation::TYPE_BEZIER: text += " (Bezier)"; break;
  3424. case Animation::TYPE_AUDIO: text += " (Audio)"; break;
  3425. default: {
  3426. };
  3427. }
  3428. TreeItem *it = track_copy_select->create_item(troot);
  3429. it->set_editable(0, true);
  3430. it->set_selectable(0, true);
  3431. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  3432. it->set_icon(0, icon);
  3433. it->set_text(0, text);
  3434. Dictionary md;
  3435. md["track_idx"] = i;
  3436. md["path"] = path;
  3437. it->set_metadata(0, md);
  3438. }
  3439. track_copy_dialog->popup_centered_minsize(Size2(300, 500) * EDSCALE);
  3440. } break;
  3441. case EDIT_COPY_TRACKS_CONFIRM: {
  3442. track_clipboard.clear();
  3443. TreeItem *root = track_copy_select->get_root();
  3444. if (root) {
  3445. TreeItem *it = root->get_children();
  3446. while (it) {
  3447. Dictionary md = it->get_metadata(0);
  3448. int idx = md["track_idx"];
  3449. if (it->is_checked(0) && idx >= 0 && idx < animation->get_track_count()) {
  3450. TrackClipboard tc;
  3451. tc.base_path = animation->track_get_path(idx);
  3452. tc.full_path = md["path"];
  3453. tc.track_type = animation->track_get_type(idx);
  3454. tc.interp_type = animation->track_get_interpolation_type(idx);
  3455. if (tc.track_type == Animation::TYPE_VALUE) {
  3456. tc.update_mode = animation->value_track_get_update_mode(idx);
  3457. }
  3458. tc.loop_wrap = animation->track_get_interpolation_loop_wrap(idx);
  3459. tc.enabled = animation->track_is_enabled(idx);
  3460. for (int i = 0; i < animation->track_get_key_count(idx); i++) {
  3461. TrackClipboard::Key k;
  3462. k.time = animation->track_get_key_time(idx, i);
  3463. k.value = animation->track_get_key_value(idx, i);
  3464. k.transition = animation->track_get_key_transition(idx, i);
  3465. tc.keys.push_back(k);
  3466. }
  3467. track_clipboard.push_back(tc);
  3468. }
  3469. it = it->get_next();
  3470. }
  3471. }
  3472. } break;
  3473. case EDIT_PASTE_TRACKS: {
  3474. if (track_clipboard.size() == 0) {
  3475. EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty"));
  3476. break;
  3477. }
  3478. int base_track = animation->get_track_count();
  3479. undo_redo->create_action("Paste Tracks");
  3480. for (int i = 0; i < track_clipboard.size(); i++) {
  3481. undo_redo->add_do_method(animation.ptr(), "add_track", track_clipboard[i].track_type);
  3482. Node *exists = NULL;
  3483. NodePath path = track_clipboard[i].base_path;
  3484. if (root) {
  3485. NodePath np = track_clipboard[i].full_path;
  3486. exists = root->get_node(np);
  3487. if (exists) {
  3488. path = NodePath(root->get_path_to(exists).get_names(), track_clipboard[i].full_path.get_subnames(), false);
  3489. }
  3490. }
  3491. undo_redo->add_do_method(animation.ptr(), "track_set_path", base_track, path);
  3492. undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", base_track, track_clipboard[i].interp_type);
  3493. undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", base_track, track_clipboard[i].loop_wrap);
  3494. undo_redo->add_do_method(animation.ptr(), "track_set_enabled", base_track, track_clipboard[i].enabled);
  3495. if (track_clipboard[i].track_type == Animation::TYPE_VALUE) {
  3496. undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", base_track, track_clipboard[i].update_mode);
  3497. }
  3498. for (int j = 0; j < track_clipboard[i].keys.size(); j++) {
  3499. undo_redo->add_do_method(animation.ptr(), "track_insert_key", base_track, track_clipboard[i].keys[j].time, track_clipboard[i].keys[j].value, track_clipboard[i].keys[j].transition);
  3500. }
  3501. undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
  3502. base_track++;
  3503. }
  3504. undo_redo->commit_action();
  3505. } break;
  3506. case EDIT_SCALE_SELECTION:
  3507. case EDIT_SCALE_FROM_CURSOR: {
  3508. scale_dialog->popup_centered(Size2(200, 100) * EDSCALE);
  3509. } break;
  3510. case EDIT_SCALE_CONFIRM: {
  3511. if (selection.empty())
  3512. return;
  3513. float from_t = 1e20;
  3514. float to_t = -1e20;
  3515. float len = -1e20;
  3516. float pivot = 0;
  3517. for (Map<SelectedKey, KeyInfo>::Element *E = selection.front(); E; E = E->next()) {
  3518. float t = animation->track_get_key_time(E->key().track, E->key().key);
  3519. if (t < from_t)
  3520. from_t = t;
  3521. if (t > to_t)
  3522. to_t = t;
  3523. }
  3524. len = to_t - from_t;
  3525. if (last_menu_track_opt == EDIT_SCALE_FROM_CURSOR) {
  3526. pivot = timeline->get_play_position();
  3527. } else {
  3528. pivot = from_t;
  3529. }
  3530. float s = scale->get_value();
  3531. if (s == 0) {
  3532. ERR_PRINT("Can't scale to 0");
  3533. }
  3534. undo_redo->create_action(TTR("Anim Scale Keys"));
  3535. List<_AnimMoveRestore> to_restore;
  3536. // 1-remove the keys
  3537. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3538. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  3539. }
  3540. // 2- remove overlapped keys
  3541. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3542. float newtime = (E->get().pos - from_t) * s + from_t;
  3543. int idx = animation->track_find_key(E->key().track, newtime, true);
  3544. if (idx == -1)
  3545. continue;
  3546. SelectedKey sk;
  3547. sk.key = idx;
  3548. sk.track = E->key().track;
  3549. if (selection.has(sk))
  3550. continue; //already in selection, don't save
  3551. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newtime);
  3552. _AnimMoveRestore amr;
  3553. amr.key = animation->track_get_key_value(E->key().track, idx);
  3554. amr.track = E->key().track;
  3555. amr.time = newtime;
  3556. amr.transition = animation->track_get_key_transition(E->key().track, idx);
  3557. to_restore.push_back(amr);
  3558. }
  3559. #define _NEW_POS(m_ofs) (((s > 0) ? m_ofs : from_t + (len - (m_ofs - from_t))) - pivot) * ABS(s) + from_t
  3560. // 3-move the keys (re insert them)
  3561. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3562. float newpos = _NEW_POS(E->get().pos);
  3563. undo_redo->add_do_method(animation.ptr(), "track_insert_key", E->key().track, newpos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  3564. }
  3565. // 4-(undo) remove inserted keys
  3566. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3567. float newpos = _NEW_POS(E->get().pos);
  3568. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", E->key().track, newpos);
  3569. }
  3570. // 5-(undo) reinsert keys
  3571. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3572. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  3573. }
  3574. // 6-(undo) reinsert overlapped keys
  3575. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  3576. _AnimMoveRestore &amr = E->get();
  3577. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  3578. }
  3579. // 6-(undo) reinsert overlapped keys
  3580. for (List<_AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  3581. _AnimMoveRestore &amr = E->get();
  3582. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
  3583. }
  3584. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  3585. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  3586. // 7-reselect
  3587. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3588. float oldpos = E->get().pos;
  3589. float newpos = _NEW_POS(oldpos);
  3590. if (newpos >= 0)
  3591. undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
  3592. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
  3593. }
  3594. #undef _NEW_POS
  3595. undo_redo->commit_action();
  3596. } break;
  3597. case EDIT_DUPLICATE_SELECTION: {
  3598. if (bezier_edit->is_visible()) {
  3599. bezier_edit->duplicate_selection();
  3600. break;
  3601. }
  3602. _anim_duplicate_keys(false);
  3603. } break;
  3604. case EDIT_DUPLICATE_TRANSPOSED: {
  3605. if (bezier_edit->is_visible()) {
  3606. EditorNode::get_singleton()->show_warning(TTR("This option does not work for Bezier editing, as it's only a single track."));
  3607. break;
  3608. }
  3609. _anim_duplicate_keys(true);
  3610. } break;
  3611. case EDIT_DELETE_SELECTION: {
  3612. if (bezier_edit->is_visible()) {
  3613. bezier_edit->delete_selection();
  3614. break;
  3615. }
  3616. if (selection.size()) {
  3617. undo_redo->create_action(TTR("Anim Delete Keys"));
  3618. for (Map<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
  3619. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
  3620. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
  3621. }
  3622. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  3623. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  3624. undo_redo->commit_action();
  3625. //selection.clear();
  3626. _update_key_edit();
  3627. }
  3628. } break;
  3629. case EDIT_GOTO_NEXT_STEP: {
  3630. if (animation.is_null())
  3631. break;
  3632. float step = animation->get_step();
  3633. if (step == 0)
  3634. step = 1;
  3635. float pos = timeline->get_play_position();
  3636. pos = Math::stepify(pos + step, step);
  3637. if (pos > animation->get_length())
  3638. pos = animation->get_length();
  3639. set_anim_pos(pos);
  3640. emit_signal("timeline_changed", pos, true);
  3641. } break;
  3642. case EDIT_GOTO_PREV_STEP: {
  3643. if (animation.is_null())
  3644. break;
  3645. float step = animation->get_step();
  3646. if (step == 0)
  3647. step = 1;
  3648. float pos = timeline->get_play_position();
  3649. pos = Math::stepify(pos - step, step);
  3650. if (pos < 0)
  3651. pos = 0;
  3652. set_anim_pos(pos);
  3653. emit_signal("timeline_changed", pos, true);
  3654. } break;
  3655. case EDIT_OPTIMIZE_ANIMATION: {
  3656. optimize_dialog->popup_centered(Size2(250, 180) * EDSCALE);
  3657. } break;
  3658. case EDIT_OPTIMIZE_ANIMATION_CONFIRM: {
  3659. animation->optimize(optimize_linear_error->get_value(), optimize_angular_error->get_value(), optimize_max_angle->get_value());
  3660. _update_tracks();
  3661. undo_redo->clear_history();
  3662. } break;
  3663. case EDIT_CLEAN_UP_ANIMATION: {
  3664. cleanup_dialog->popup_centered_minsize(Size2(300, 0) * EDSCALE);
  3665. } break;
  3666. case EDIT_CLEAN_UP_ANIMATION_CONFIRM: {
  3667. if (cleanup_all->is_pressed()) {
  3668. List<StringName> names;
  3669. AnimationPlayerEditor::singleton->get_player()->get_animation_list(&names);
  3670. for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
  3671. _cleanup_animation(AnimationPlayerEditor::singleton->get_player()->get_animation(E->get()));
  3672. }
  3673. } else {
  3674. _cleanup_animation(animation);
  3675. }
  3676. } break;
  3677. }
  3678. }
  3679. void AnimationTrackEditor::_cleanup_animation(Ref<Animation> p_animation) {
  3680. for (int i = 0; i < p_animation->get_track_count(); i++) {
  3681. bool prop_exists = false;
  3682. Variant::Type valid_type = Variant::NIL;
  3683. Object *obj = NULL;
  3684. RES res;
  3685. Vector<StringName> leftover_path;
  3686. Node *node = root->get_node_and_resource(p_animation->track_get_path(i), res, leftover_path);
  3687. if (res.is_valid()) {
  3688. obj = res.ptr();
  3689. } else if (node) {
  3690. obj = node;
  3691. }
  3692. if (obj && p_animation->track_get_type(i) == Animation::TYPE_VALUE) {
  3693. valid_type = obj->get_static_property_type_indexed(leftover_path, &prop_exists);
  3694. }
  3695. if (!obj && cleanup_tracks->is_pressed()) {
  3696. p_animation->remove_track(i);
  3697. i--;
  3698. continue;
  3699. }
  3700. if (!prop_exists || p_animation->track_get_type(i) != Animation::TYPE_VALUE || cleanup_keys->is_pressed() == false)
  3701. continue;
  3702. for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
  3703. Variant v = p_animation->track_get_key_value(i, j);
  3704. if (!Variant::can_convert(v.get_type(), valid_type)) {
  3705. p_animation->track_remove_key(i, j);
  3706. j--;
  3707. }
  3708. }
  3709. if (p_animation->track_get_key_count(i) == 0 && cleanup_tracks->is_pressed()) {
  3710. p_animation->remove_track(i);
  3711. i--;
  3712. }
  3713. }
  3714. undo_redo->clear_history();
  3715. _update_tracks();
  3716. }
  3717. void AnimationTrackEditor::_view_group_toggle() {
  3718. _update_tracks();
  3719. view_group->set_icon(get_icon(view_group->is_pressed() ? "AnimationTrackList" : "AnimationTrackGroup", "EditorIcons"));
  3720. }
  3721. void AnimationTrackEditor::_selection_changed() {
  3722. if (selected_filter->is_pressed()) {
  3723. _update_tracks(); //needs updatin
  3724. } else {
  3725. for (int i = 0; i < track_edits.size(); i++) {
  3726. track_edits[i]->update();
  3727. }
  3728. for (int i = 0; i < groups.size(); i++) {
  3729. groups[i]->update();
  3730. }
  3731. }
  3732. }
  3733. float AnimationTrackEditor::snap_time(float p_value) {
  3734. if (snap->is_pressed()) {
  3735. p_value = Math::stepify(p_value, step->get_value());
  3736. }
  3737. return p_value;
  3738. }
  3739. void AnimationTrackEditor::_bind_methods() {
  3740. ClassDB::bind_method("_animation_changed", &AnimationTrackEditor::_animation_changed);
  3741. ClassDB::bind_method("_timeline_changed", &AnimationTrackEditor::_timeline_changed);
  3742. ClassDB::bind_method("_track_remove_request", &AnimationTrackEditor::_track_remove_request);
  3743. ClassDB::bind_method("_name_limit_changed", &AnimationTrackEditor::_name_limit_changed);
  3744. ClassDB::bind_method("_update_scroll", &AnimationTrackEditor::_update_scroll);
  3745. ClassDB::bind_method("_update_step", &AnimationTrackEditor::_update_step);
  3746. ClassDB::bind_method("_update_length", &AnimationTrackEditor::_update_length);
  3747. ClassDB::bind_method("_dropped_track", &AnimationTrackEditor::_dropped_track);
  3748. ClassDB::bind_method("_add_track", &AnimationTrackEditor::_add_track);
  3749. ClassDB::bind_method("_new_track_node_selected", &AnimationTrackEditor::_new_track_node_selected);
  3750. ClassDB::bind_method("_new_track_property_selected", &AnimationTrackEditor::_new_track_property_selected);
  3751. ClassDB::bind_method("_root_removed", &AnimationTrackEditor::_root_removed);
  3752. ClassDB::bind_method("_confirm_insert_list", &AnimationTrackEditor::_confirm_insert_list);
  3753. ClassDB::bind_method("_insert_delay", &AnimationTrackEditor::_insert_delay);
  3754. ClassDB::bind_method("_timeline_value_changed", &AnimationTrackEditor::_timeline_value_changed);
  3755. ClassDB::bind_method("_insert_key_from_track", &AnimationTrackEditor::_insert_key_from_track);
  3756. ClassDB::bind_method("_add_method_key", &AnimationTrackEditor::_add_method_key);
  3757. ClassDB::bind_method("_key_selected", &AnimationTrackEditor::_key_selected);
  3758. ClassDB::bind_method("_key_deselected", &AnimationTrackEditor::_key_deselected);
  3759. ClassDB::bind_method("_clear_selection", &AnimationTrackEditor::_clear_selection);
  3760. ClassDB::bind_method("_move_selection_begin", &AnimationTrackEditor::_move_selection_begin);
  3761. ClassDB::bind_method("_move_selection", &AnimationTrackEditor::_move_selection);
  3762. ClassDB::bind_method("_move_selection_commit", &AnimationTrackEditor::_move_selection_commit);
  3763. ClassDB::bind_method("_move_selection_cancel", &AnimationTrackEditor::_move_selection_cancel);
  3764. ClassDB::bind_method("_clear_selection_for_anim", &AnimationTrackEditor::_clear_selection_for_anim);
  3765. ClassDB::bind_method("_select_at_anim", &AnimationTrackEditor::_select_at_anim);
  3766. ClassDB::bind_method("_scroll_input", &AnimationTrackEditor::_scroll_input);
  3767. ClassDB::bind_method("_box_selection_draw", &AnimationTrackEditor::_box_selection_draw);
  3768. ClassDB::bind_method("_bezier_edit", &AnimationTrackEditor::_bezier_edit);
  3769. ClassDB::bind_method("_cancel_bezier_edit", &AnimationTrackEditor::_cancel_bezier_edit);
  3770. ClassDB::bind_method("_edit_menu_pressed", &AnimationTrackEditor::_edit_menu_pressed);
  3771. ClassDB::bind_method("_view_group_toggle", &AnimationTrackEditor::_view_group_toggle);
  3772. ClassDB::bind_method("_selection_changed", &AnimationTrackEditor::_selection_changed);
  3773. ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::REAL, "position"), PropertyInfo(Variant::BOOL, "drag")));
  3774. ADD_SIGNAL(MethodInfo("keying_changed"));
  3775. ADD_SIGNAL(MethodInfo("animation_len_changed", PropertyInfo(Variant::REAL, "len")));
  3776. ADD_SIGNAL(MethodInfo("animation_step_changed", PropertyInfo(Variant::REAL, "step")));
  3777. }
  3778. AnimationTrackEditor::AnimationTrackEditor() {
  3779. root = NULL;
  3780. block_animation_update = false;
  3781. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3782. main_panel = memnew(PanelContainer);
  3783. add_child(main_panel);
  3784. main_panel->set_v_size_flags(SIZE_EXPAND_FILL);
  3785. HBoxContainer *timeline_scroll = memnew(HBoxContainer);
  3786. main_panel->add_child(timeline_scroll);
  3787. timeline_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  3788. VBoxContainer *timeline_vbox = memnew(VBoxContainer);
  3789. timeline_scroll->add_child(timeline_vbox);
  3790. timeline_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  3791. timeline_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3792. timeline_vbox->add_constant_override("separation", 0);
  3793. timeline = memnew(AnimationTimelineEdit);
  3794. timeline->set_block_animation_update_ptr(&block_animation_update);
  3795. timeline->set_undo_redo(undo_redo);
  3796. timeline_vbox->add_child(timeline);
  3797. timeline->connect("timeline_changed", this, "_timeline_changed");
  3798. timeline->connect("name_limit_changed", this, "_name_limit_changed");
  3799. timeline->connect("track_added", this, "_add_track");
  3800. timeline->connect("value_changed", this, "_timeline_value_changed");
  3801. timeline->connect("length_changed", this, "_update_length");
  3802. scroll = memnew(ScrollContainer);
  3803. timeline_vbox->add_child(scroll);
  3804. scroll->set_v_size_flags(SIZE_EXPAND_FILL);
  3805. VScrollBar *sb = scroll->get_v_scrollbar();
  3806. scroll->remove_child(sb);
  3807. timeline_scroll->add_child(sb); //move here so timeline and tracks are always aligned
  3808. scroll->connect("gui_input", this, "_scroll_input");
  3809. bezier_edit = memnew(AnimationBezierTrackEdit);
  3810. timeline_vbox->add_child(bezier_edit);
  3811. bezier_edit->set_block_animation_update_ptr(&block_animation_update);
  3812. bezier_edit->set_undo_redo(undo_redo);
  3813. bezier_edit->set_editor(this);
  3814. bezier_edit->set_timeline(timeline);
  3815. bezier_edit->hide();
  3816. bezier_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  3817. bezier_edit->connect("close_request", this, "_cancel_bezier_edit");
  3818. timeline_vbox->set_custom_minimum_size(Size2(0, 150) * EDSCALE);
  3819. hscroll = memnew(HScrollBar);
  3820. hscroll->share(timeline);
  3821. hscroll->hide();
  3822. hscroll->connect("value_changed", this, "_update_scroll");
  3823. timeline_vbox->add_child(hscroll);
  3824. timeline->set_hscroll(hscroll);
  3825. track_vbox = memnew(VBoxContainer);
  3826. scroll->add_child(track_vbox);
  3827. track_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  3828. scroll->set_enable_h_scroll(false);
  3829. scroll->set_enable_v_scroll(true);
  3830. track_vbox->add_constant_override("separation", 0);
  3831. //timeline_vbox->add_child(memnew(HSeparator));
  3832. HBoxContainer *bottom_hb = memnew(HBoxContainer);
  3833. add_child(bottom_hb);
  3834. bottom_hb->add_spacer();
  3835. selected_filter = memnew(ToolButton);
  3836. selected_filter->connect("pressed", this, "_view_group_toggle"); //same function works the same
  3837. selected_filter->set_toggle_mode(true);
  3838. selected_filter->set_tooltip(TTR("Only show tracks from nodes selected in tree."));
  3839. bottom_hb->add_child(selected_filter);
  3840. view_group = memnew(ToolButton);
  3841. view_group->connect("pressed", this, "_view_group_toggle");
  3842. view_group->set_toggle_mode(true);
  3843. view_group->set_tooltip(TTR("Group tracks by node or display them as plain list."));
  3844. bottom_hb->add_child(view_group);
  3845. bottom_hb->add_child(memnew(VSeparator));
  3846. snap = memnew(ToolButton);
  3847. snap->set_text(TTR("Snap (s): "));
  3848. bottom_hb->add_child(snap);
  3849. snap->set_disabled(true);
  3850. snap->set_toggle_mode(true);
  3851. snap->set_pressed(true);
  3852. step = memnew(EditorSpinSlider);
  3853. step->set_min(0);
  3854. step->set_max(1000);
  3855. step->set_step(0.01);
  3856. step->set_hide_slider(true);
  3857. step->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  3858. step->set_tooltip(TTR("Animation step value."));
  3859. bottom_hb->add_child(step);
  3860. step->connect("value_changed", this, "_update_step");
  3861. step->set_read_only(true);
  3862. bottom_hb->add_child(memnew(VSeparator));
  3863. zoom_icon = memnew(TextureRect);
  3864. zoom_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
  3865. bottom_hb->add_child(zoom_icon);
  3866. zoom = memnew(HSlider);
  3867. zoom->set_step(0.01);
  3868. zoom->set_min(0.0);
  3869. zoom->set_max(2.0);
  3870. zoom->set_value(1.0);
  3871. zoom->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  3872. zoom->set_v_size_flags(SIZE_SHRINK_CENTER);
  3873. bottom_hb->add_child(zoom);
  3874. timeline->set_zoom(zoom);
  3875. edit = memnew(MenuButton);
  3876. edit->set_text(TTR("Edit"));
  3877. edit->set_flat(false);
  3878. edit->set_disabled(true);
  3879. edit->set_tooltip(TTR("Animation properties."));
  3880. edit->get_popup()->add_item(TTR("Copy Tracks"), EDIT_COPY_TRACKS);
  3881. edit->get_popup()->add_item(TTR("Paste Tracks"), EDIT_PASTE_TRACKS);
  3882. edit->get_popup()->add_separator();
  3883. edit->get_popup()->add_item(TTR("Scale Selection"), EDIT_SCALE_SELECTION);
  3884. edit->get_popup()->add_item(TTR("Scale From Cursor"), EDIT_SCALE_FROM_CURSOR);
  3885. edit->get_popup()->add_separator();
  3886. edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/duplicate_selection", TTR("Duplicate Selection"), KEY_MASK_CMD | KEY_D), EDIT_DUPLICATE_SELECTION);
  3887. edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/duplicate_selection_transposed", TTR("Duplicate Transposed"), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_D), EDIT_DUPLICATE_TRANSPOSED);
  3888. edit->get_popup()->set_item_shortcut_disabled(edit->get_popup()->get_item_index(EDIT_DUPLICATE_SELECTION), true);
  3889. edit->get_popup()->set_item_shortcut_disabled(edit->get_popup()->get_item_index(EDIT_DUPLICATE_TRANSPOSED), true);
  3890. edit->get_popup()->add_separator();
  3891. edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/delete_selection", TTR("Delete Selection"), KEY_DELETE), EDIT_DELETE_SELECTION);
  3892. edit->get_popup()->set_item_shortcut_disabled(edit->get_popup()->get_item_index(EDIT_DELETE_SELECTION), true);
  3893. //this shortcut will be checked from the track itself. so no need to enable it here (will conflict with scenetree dock)
  3894. edit->get_popup()->add_separator();
  3895. edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/goto_next_step", TTR("Go to Next Step"), KEY_MASK_CMD | KEY_RIGHT), EDIT_GOTO_NEXT_STEP);
  3896. edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/goto_prev_step", TTR("Go to Previous Step"), KEY_MASK_CMD | KEY_LEFT), EDIT_GOTO_PREV_STEP);
  3897. edit->get_popup()->add_separator();
  3898. edit->get_popup()->add_item(TTR("Optimize Animation"), EDIT_OPTIMIZE_ANIMATION);
  3899. edit->get_popup()->add_item(TTR("Clean-Up Animation"), EDIT_CLEAN_UP_ANIMATION);
  3900. edit->get_popup()->connect("id_pressed", this, "_edit_menu_pressed");
  3901. pick_track = memnew(SceneTreeDialog);
  3902. add_child(pick_track);
  3903. pick_track->set_title(TTR("Pick the node that will be animated:"));
  3904. pick_track->connect("selected", this, "_new_track_node_selected");
  3905. prop_selector = memnew(PropertySelector);
  3906. add_child(prop_selector);
  3907. prop_selector->connect("selected", this, "_new_track_property_selected");
  3908. method_selector = memnew(PropertySelector);
  3909. add_child(method_selector);
  3910. method_selector->connect("selected", this, "_add_method_key");
  3911. inserting = false;
  3912. insert_query = false;
  3913. insert_frame = 0;
  3914. insert_queue = false;
  3915. insert_confirm = memnew(ConfirmationDialog);
  3916. add_child(insert_confirm);
  3917. insert_confirm->connect("confirmed", this, "_confirm_insert_list");
  3918. VBoxContainer *icvb = memnew(VBoxContainer);
  3919. insert_confirm->add_child(icvb);
  3920. insert_confirm_text = memnew(Label);
  3921. icvb->add_child(insert_confirm_text);
  3922. insert_confirm_bezier = memnew(CheckBox);
  3923. insert_confirm_bezier->set_text(TTR("Use Bezier Curves"));
  3924. icvb->add_child(insert_confirm_bezier);
  3925. keying = false;
  3926. moving_selection = 0;
  3927. key_edit = NULL;
  3928. box_selection = memnew(Control);
  3929. add_child(box_selection);
  3930. box_selection->set_as_toplevel(true);
  3931. box_selection->set_mouse_filter(MOUSE_FILTER_IGNORE);
  3932. box_selection->hide();
  3933. box_selection->connect("draw", this, "_box_selection_draw");
  3934. box_selecting = false;
  3935. //default plugins
  3936. Ref<AnimationTrackEditDefaultPlugin> def_plugin;
  3937. def_plugin.instance();
  3938. add_track_edit_plugin(def_plugin);
  3939. //dialogs
  3940. optimize_dialog = memnew(ConfirmationDialog);
  3941. add_child(optimize_dialog);
  3942. optimize_dialog->set_title(TTR("Anim. Optimizer"));
  3943. VBoxContainer *optimize_vb = memnew(VBoxContainer);
  3944. optimize_dialog->add_child(optimize_vb);
  3945. optimize_linear_error = memnew(SpinBox);
  3946. optimize_linear_error->set_max(1.0);
  3947. optimize_linear_error->set_min(0.001);
  3948. optimize_linear_error->set_step(0.001);
  3949. optimize_linear_error->set_value(0.05);
  3950. optimize_vb->add_margin_child(TTR("Max. Linear Error:"), optimize_linear_error);
  3951. optimize_angular_error = memnew(SpinBox);
  3952. optimize_angular_error->set_max(1.0);
  3953. optimize_angular_error->set_min(0.001);
  3954. optimize_angular_error->set_step(0.001);
  3955. optimize_angular_error->set_value(0.01);
  3956. optimize_vb->add_margin_child(TTR("Max. Angular Error:"), optimize_angular_error);
  3957. optimize_max_angle = memnew(SpinBox);
  3958. optimize_vb->add_margin_child(TTR("Max Optimizable Angle:"), optimize_max_angle);
  3959. optimize_max_angle->set_max(360.0);
  3960. optimize_max_angle->set_min(0.0);
  3961. optimize_max_angle->set_step(0.1);
  3962. optimize_max_angle->set_value(22);
  3963. optimize_dialog->get_ok()->set_text(TTR("Optimize"));
  3964. optimize_dialog->connect("confirmed", this, "_edit_menu_pressed", varray(EDIT_CLEAN_UP_ANIMATION_CONFIRM));
  3965. //
  3966. cleanup_dialog = memnew(ConfirmationDialog);
  3967. add_child(cleanup_dialog);
  3968. VBoxContainer *cleanup_vb = memnew(VBoxContainer);
  3969. cleanup_dialog->add_child(cleanup_vb);
  3970. cleanup_keys = memnew(CheckButton);
  3971. cleanup_keys->set_text(TTR("Remove invalid keys"));
  3972. cleanup_keys->set_pressed(true);
  3973. cleanup_vb->add_child(cleanup_keys);
  3974. cleanup_tracks = memnew(CheckButton);
  3975. cleanup_tracks->set_text(TTR("Remove unresolved and empty tracks"));
  3976. cleanup_tracks->set_pressed(true);
  3977. cleanup_vb->add_child(cleanup_tracks);
  3978. cleanup_all = memnew(CheckButton);
  3979. cleanup_all->set_text(TTR("Clean-up all animations"));
  3980. cleanup_vb->add_child(cleanup_all);
  3981. cleanup_dialog->set_title(TTR("Clean-Up Animation(s) (NO UNDO!)"));
  3982. cleanup_dialog->get_ok()->set_text(TTR("Clean-Up"));
  3983. cleanup_dialog->connect("confirmed", this, "_edit_menu_pressed", varray(EDIT_CLEAN_UP_ANIMATION_CONFIRM));
  3984. //
  3985. scale_dialog = memnew(ConfirmationDialog);
  3986. VBoxContainer *vbc = memnew(VBoxContainer);
  3987. scale_dialog->add_child(vbc);
  3988. scale = memnew(SpinBox);
  3989. scale->set_min(-99999);
  3990. scale->set_max(99999);
  3991. scale->set_step(0.001);
  3992. vbc->add_margin_child(TTR("Scale Ratio:"), scale);
  3993. scale_dialog->connect("confirmed", this, "_edit_menu_pressed", varray(EDIT_SCALE_CONFIRM));
  3994. add_child(scale_dialog);
  3995. track_copy_dialog = memnew(ConfirmationDialog);
  3996. add_child(track_copy_dialog);
  3997. track_copy_dialog->set_title(TTR("Select tracks to copy:"));
  3998. track_copy_dialog->get_ok()->set_text(TTR("Copy"));
  3999. track_copy_select = memnew(Tree);
  4000. track_copy_select->set_hide_root(true);
  4001. track_copy_dialog->add_child(track_copy_select);
  4002. track_copy_dialog->connect("confirmed", this, "_edit_menu_pressed", varray(EDIT_COPY_TRACKS_CONFIRM));
  4003. }
  4004. AnimationTrackEditor::~AnimationTrackEditor() {
  4005. if (key_edit) {
  4006. memdelete(key_edit);
  4007. }
  4008. }