animation_track_editor.cpp 159 KB

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