animation_track_editor.cpp 154 KB

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