animation_track_editor.cpp 154 KB

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