spatial_editor_plugin.cpp 203 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060
  1. /*************************************************************************/
  2. /* spatial_editor_plugin.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 "spatial_editor_plugin.h"
  31. #include "core/math/camera_matrix.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/print_string.h"
  35. #include "core/project_settings.h"
  36. #include "core/sort_array.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_settings.h"
  39. #include "editor/plugins/animation_player_editor_plugin.h"
  40. #include "editor/plugins/script_editor_plugin.h"
  41. #include "editor/script_editor_debugger.h"
  42. #include "editor/spatial_editor_gizmos.h"
  43. #include "scene/3d/camera.h"
  44. #include "scene/3d/collision_shape.h"
  45. #include "scene/3d/mesh_instance.h"
  46. #include "scene/3d/physics_body.h"
  47. #include "scene/3d/visual_instance.h"
  48. #include "scene/resources/packed_scene.h"
  49. #include "scene/resources/surface_tool.h"
  50. #define DISTANCE_DEFAULT 4
  51. #define GIZMO_ARROW_SIZE 0.35
  52. #define GIZMO_RING_HALF_WIDTH 0.1
  53. #define GIZMO_SCALE_DEFAULT 0.15
  54. #define GIZMO_PLANE_SIZE 0.2
  55. #define GIZMO_PLANE_DST 0.3
  56. #define GIZMO_CIRCLE_SIZE 1.1
  57. #define GIZMO_SCALE_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  58. #define GIZMO_ARROW_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  59. #define ZOOM_MIN_DISTANCE 0.001
  60. #define ZOOM_MULTIPLIER 1.08
  61. #define ZOOM_INDICATOR_DELAY_S 1.5
  62. #define FREELOOK_MIN_SPEED 0.01
  63. #define FREELOOK_SPEED_MULTIPLIER 1.08
  64. #define MIN_Z 0.01
  65. #define MAX_Z 10000
  66. #define MIN_FOV 0.01
  67. #define MAX_FOV 179
  68. #ifdef TOOLS_ENABLED
  69. #define get_global_gizmo_transform get_global_gizmo_transform
  70. #define get_local_gizmo_transform get_local_gizmo_transform
  71. #else
  72. #define get_global_gizmo_transform get_global_transform
  73. #define get_local_gizmo_transform get_transform
  74. #endif
  75. void SpatialEditorViewport::_update_camera(float p_interp_delta) {
  76. bool is_orthogonal = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  77. Cursor old_camera_cursor = camera_cursor;
  78. camera_cursor = cursor;
  79. if (p_interp_delta > 0) {
  80. //-------
  81. // Perform smoothing
  82. if (is_freelook_active()) {
  83. // Higher inertia should increase "lag" (lerp with factor between 0 and 1)
  84. // Inertia of zero should produce instant movement (lerp with factor of 1) in this case it returns a really high value and gets clamped to 1.
  85. real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia");
  86. inertia = MAX(0.001, inertia);
  87. real_t factor = (1.0 / inertia) * p_interp_delta;
  88. // We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos
  89. camera_cursor.eye_pos = old_camera_cursor.eye_pos.linear_interpolate(cursor.eye_pos, CLAMP(factor, 0, 1));
  90. float orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  91. orbit_inertia = MAX(0.0001, orbit_inertia);
  92. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  93. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  94. Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1));
  95. camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance;
  96. } else {
  97. //when not being manipulated, move softly
  98. float free_orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  99. float free_translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
  100. //when being manipulated, move more quickly
  101. float manip_orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/manipulation_orbit_inertia");
  102. float manip_translation_inertia = EDITOR_GET("editors/3d/navigation_feel/manipulation_translation_inertia");
  103. float zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
  104. //determine if being manipulated
  105. bool manipulated = Input::get_singleton()->get_mouse_button_mask() & (2 | 4);
  106. manipulated |= Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  107. manipulated |= Input::get_singleton()->is_key_pressed(KEY_ALT);
  108. manipulated |= Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  109. float orbit_inertia = MAX(0.00001, manipulated ? manip_orbit_inertia : free_orbit_inertia);
  110. float translation_inertia = MAX(0.0001, manipulated ? manip_translation_inertia : free_translation_inertia);
  111. zoom_inertia = MAX(0.0001, zoom_inertia);
  112. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  113. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  114. camera_cursor.pos = old_camera_cursor.pos.linear_interpolate(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia)));
  115. camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN(1.f, p_interp_delta * (1 / zoom_inertia)));
  116. }
  117. }
  118. //-------
  119. // Apply camera transform
  120. float tolerance = 0.001;
  121. bool equal = true;
  122. if (Math::abs(old_camera_cursor.x_rot - camera_cursor.x_rot) > tolerance || Math::abs(old_camera_cursor.y_rot - camera_cursor.y_rot) > tolerance) {
  123. equal = false;
  124. }
  125. if (equal && old_camera_cursor.pos.distance_squared_to(camera_cursor.pos) > tolerance * tolerance) {
  126. equal = false;
  127. }
  128. if (equal && Math::abs(old_camera_cursor.distance - camera_cursor.distance) > tolerance) {
  129. equal = false;
  130. }
  131. if (!equal || p_interp_delta == 0 || is_freelook_active() || is_orthogonal != orthogonal) {
  132. camera->set_global_transform(to_camera_transform(camera_cursor));
  133. if (orthogonal)
  134. camera->set_orthogonal(2 * cursor.distance, 0.1, 8192);
  135. else
  136. camera->set_perspective(get_fov(), get_znear(), get_zfar());
  137. update_transform_gizmo_view();
  138. }
  139. }
  140. Transform SpatialEditorViewport::to_camera_transform(const Cursor &p_cursor) const {
  141. Transform camera_transform;
  142. camera_transform.translate(p_cursor.pos);
  143. camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot);
  144. camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot);
  145. if (orthogonal)
  146. camera_transform.translate(0, 0, 4096);
  147. else
  148. camera_transform.translate(0, 0, p_cursor.distance);
  149. return camera_transform;
  150. }
  151. int SpatialEditorViewport::get_selected_count() const {
  152. Map<Node *, Object *> &selection = editor_selection->get_selection();
  153. int count = 0;
  154. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  155. Spatial *sp = Object::cast_to<Spatial>(E->key());
  156. if (!sp)
  157. continue;
  158. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  159. if (!se)
  160. continue;
  161. count++;
  162. }
  163. return count;
  164. }
  165. float SpatialEditorViewport::get_znear() const {
  166. return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z);
  167. }
  168. float SpatialEditorViewport::get_zfar() const {
  169. return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z);
  170. }
  171. float SpatialEditorViewport::get_fov() const {
  172. return CLAMP(spatial_editor->get_fov(), MIN_FOV, MAX_FOV);
  173. }
  174. Transform SpatialEditorViewport::_get_camera_transform() const {
  175. return camera->get_global_transform();
  176. }
  177. Vector3 SpatialEditorViewport::_get_camera_position() const {
  178. return _get_camera_transform().origin;
  179. }
  180. Point2 SpatialEditorViewport::_point_to_screen(const Vector3 &p_point) {
  181. return camera->unproject_position(p_point) * viewport_container->get_stretch_shrink();
  182. }
  183. Vector3 SpatialEditorViewport::_get_ray_pos(const Vector2 &p_pos) const {
  184. return camera->project_ray_origin(p_pos / viewport_container->get_stretch_shrink());
  185. }
  186. Vector3 SpatialEditorViewport::_get_camera_normal() const {
  187. return -_get_camera_transform().basis.get_axis(2);
  188. }
  189. Vector3 SpatialEditorViewport::_get_ray(const Vector2 &p_pos) const {
  190. return camera->project_ray_normal(p_pos / viewport_container->get_stretch_shrink());
  191. }
  192. /*
  193. void SpatialEditorViewport::_clear_id(Spatial *p_node) {
  194. editor_selection->remove_node(p_node);
  195. }
  196. */
  197. void SpatialEditorViewport::_clear_selected() {
  198. editor_selection->clear();
  199. }
  200. void SpatialEditorViewport::_select_clicked(bool p_append, bool p_single) {
  201. if (!clicked)
  202. return;
  203. Spatial *sp = Object::cast_to<Spatial>(ObjectDB::get_instance(clicked));
  204. if (!sp)
  205. return;
  206. _select(sp, clicked_wants_append, true);
  207. }
  208. void SpatialEditorViewport::_select(Node *p_node, bool p_append, bool p_single) {
  209. if (!p_append) {
  210. editor_selection->clear();
  211. }
  212. if (editor_selection->is_selected(p_node)) {
  213. //erase
  214. editor_selection->remove_node(p_node);
  215. } else {
  216. editor_selection->add_node(p_node);
  217. }
  218. if (p_single) {
  219. if (Engine::get_singleton()->is_editor_hint())
  220. editor->call("edit_node", p_node);
  221. }
  222. }
  223. ObjectID SpatialEditorViewport::_select_ray(const Point2 &p_pos, bool p_append, bool &r_includes_current, int *r_gizmo_handle, bool p_alt_select) {
  224. if (r_gizmo_handle)
  225. *r_gizmo_handle = -1;
  226. Vector3 ray = _get_ray(p_pos);
  227. Vector3 pos = _get_ray_pos(p_pos);
  228. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  229. Set<Ref<EditorSpatialGizmo> > found_gizmos;
  230. Node *edited_scene = get_tree()->get_edited_scene_root();
  231. ObjectID closest = 0;
  232. Node *item = NULL;
  233. float closest_dist = 1e20;
  234. int selected_handle = -1;
  235. for (int i = 0; i < instances.size(); i++) {
  236. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  237. if (!spat)
  238. continue;
  239. Ref<EditorSpatialGizmo> seg = spat->get_gizmo();
  240. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  241. continue;
  242. }
  243. found_gizmos.insert(seg);
  244. Vector3 point;
  245. Vector3 normal;
  246. int handle = -1;
  247. bool inters = seg->intersect_ray(camera, p_pos, point, normal, &handle, p_alt_select);
  248. if (!inters)
  249. continue;
  250. float dist = pos.distance_to(point);
  251. if (dist < 0)
  252. continue;
  253. if (dist < closest_dist) {
  254. item = Object::cast_to<Node>(spat);
  255. while (item->get_owner() && item->get_owner() != edited_scene && !edited_scene->is_editable_instance(item->get_owner())) {
  256. item = item->get_owner();
  257. }
  258. closest = item->get_instance_id();
  259. closest_dist = dist;
  260. selected_handle = handle;
  261. }
  262. }
  263. if (!item)
  264. return 0;
  265. if (!editor_selection->is_selected(item) || (r_gizmo_handle && selected_handle >= 0)) {
  266. if (r_gizmo_handle)
  267. *r_gizmo_handle = selected_handle;
  268. }
  269. return closest;
  270. }
  271. void SpatialEditorViewport::_find_items_at_pos(const Point2 &p_pos, bool &r_includes_current, Vector<_RayResult> &results, bool p_alt_select) {
  272. Vector3 ray = _get_ray(p_pos);
  273. Vector3 pos = _get_ray_pos(p_pos);
  274. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  275. Set<Ref<EditorSpatialGizmo> > found_gizmos;
  276. r_includes_current = false;
  277. for (int i = 0; i < instances.size(); i++) {
  278. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  279. if (!spat)
  280. continue;
  281. Ref<EditorSpatialGizmo> seg = spat->get_gizmo();
  282. if (!seg.is_valid())
  283. continue;
  284. if (found_gizmos.has(seg))
  285. continue;
  286. found_gizmos.insert(seg);
  287. Vector3 point;
  288. Vector3 normal;
  289. int handle = -1;
  290. bool inters = seg->intersect_ray(camera, p_pos, point, normal, NULL, p_alt_select);
  291. if (!inters)
  292. continue;
  293. float dist = pos.distance_to(point);
  294. if (dist < 0)
  295. continue;
  296. if (editor_selection->is_selected(spat))
  297. r_includes_current = true;
  298. _RayResult res;
  299. res.item = spat;
  300. res.depth = dist;
  301. res.handle = handle;
  302. results.push_back(res);
  303. }
  304. if (results.empty())
  305. return;
  306. results.sort();
  307. }
  308. Vector3 SpatialEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) {
  309. CameraMatrix cm;
  310. if (orthogonal) {
  311. cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  312. } else {
  313. cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  314. }
  315. float screen_w, screen_h;
  316. cm.get_viewport_size(screen_w, screen_h);
  317. Transform camera_transform;
  318. camera_transform.translate(cursor.pos);
  319. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  320. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  321. camera_transform.translate(0, 0, cursor.distance);
  322. return camera_transform.xform(Vector3(((p_vector3.x / get_size().width) * 2.0 - 1.0) * screen_w, ((1.0 - (p_vector3.y / get_size().height)) * 2.0 - 1.0) * screen_h, -(get_znear() + p_vector3.z)));
  323. }
  324. void SpatialEditorViewport::_select_region() {
  325. if (cursor.region_begin == cursor.region_end)
  326. return; //nothing really
  327. float z_offset = MAX(0.0, 5.0 - get_znear());
  328. Vector3 box[4] = {
  329. Vector3(
  330. MIN(cursor.region_begin.x, cursor.region_end.x),
  331. MIN(cursor.region_begin.y, cursor.region_end.y),
  332. z_offset),
  333. Vector3(
  334. MAX(cursor.region_begin.x, cursor.region_end.x),
  335. MIN(cursor.region_begin.y, cursor.region_end.y),
  336. z_offset),
  337. Vector3(
  338. MAX(cursor.region_begin.x, cursor.region_end.x),
  339. MAX(cursor.region_begin.y, cursor.region_end.y),
  340. z_offset),
  341. Vector3(
  342. MIN(cursor.region_begin.x, cursor.region_end.x),
  343. MAX(cursor.region_begin.y, cursor.region_end.y),
  344. z_offset)
  345. };
  346. Vector<Plane> frustum;
  347. Vector3 cam_pos = _get_camera_position();
  348. for (int i = 0; i < 4; i++) {
  349. Vector3 a = _get_screen_to_space(box[i]);
  350. Vector3 b = _get_screen_to_space(box[(i + 1) % 4]);
  351. if (orthogonal) {
  352. frustum.push_back(Plane(a, (a - b).normalized()));
  353. } else {
  354. frustum.push_back(Plane(a, b, cam_pos));
  355. }
  356. }
  357. if (!orthogonal) {
  358. Plane near(cam_pos, -_get_camera_normal());
  359. near.d -= get_znear();
  360. frustum.push_back(near);
  361. Plane far = -near;
  362. far.d += get_zfar();
  363. frustum.push_back(far);
  364. }
  365. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_convex(frustum, get_tree()->get_root()->get_world()->get_scenario());
  366. Vector<Node *> selected;
  367. Node *edited_scene = get_tree()->get_edited_scene_root();
  368. for (int i = 0; i < instances.size(); i++) {
  369. Spatial *sp = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  370. if (!sp)
  371. continue;
  372. Node *item = Object::cast_to<Node>(sp);
  373. while (item->get_owner() && item->get_owner() != edited_scene && !edited_scene->is_editable_instance(item->get_owner())) {
  374. item = item->get_owner();
  375. }
  376. if (selected.find(item) != -1) continue;
  377. Ref<EditorSpatialGizmo> seg = sp->get_gizmo();
  378. if (!seg.is_valid())
  379. continue;
  380. if (seg->intersect_frustum(camera, frustum)) {
  381. selected.push_back(item);
  382. }
  383. }
  384. bool single = selected.size() == 1;
  385. for (int i = 0; i < selected.size(); i++) {
  386. _select(selected[i], true, single);
  387. }
  388. }
  389. void SpatialEditorViewport::_update_name() {
  390. String view_mode = orthogonal ? TTR("Orthogonal") : TTR("Perspective");
  391. if (name != "")
  392. view_menu->set_text(name + " " + view_mode);
  393. else
  394. view_menu->set_text(view_mode);
  395. view_menu->set_size(Vector2(0, 0)); // resets the button size
  396. }
  397. void SpatialEditorViewport::_compute_edit(const Point2 &p_point) {
  398. _edit.click_ray = _get_ray(Vector2(p_point.x, p_point.y));
  399. _edit.click_ray_pos = _get_ray_pos(Vector2(p_point.x, p_point.y));
  400. _edit.plane = TRANSFORM_VIEW;
  401. spatial_editor->update_transform_gizmo();
  402. _edit.center = spatial_editor->get_gizmo_transform().origin;
  403. List<Node *> &selection = editor_selection->get_selected_node_list();
  404. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  405. Spatial *sp = Object::cast_to<Spatial>(E->get());
  406. if (!sp)
  407. continue;
  408. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  409. if (!se)
  410. continue;
  411. se->original = se->sp->get_global_gizmo_transform();
  412. se->original_local = se->sp->get_local_gizmo_transform();
  413. }
  414. }
  415. static int _get_key_modifier_setting(const String &p_property) {
  416. switch (EditorSettings::get_singleton()->get(p_property).operator int()) {
  417. case 0: return 0;
  418. case 1: return KEY_SHIFT;
  419. case 2: return KEY_ALT;
  420. case 3: return KEY_META;
  421. case 4: return KEY_CONTROL;
  422. }
  423. return 0;
  424. }
  425. static int _get_key_modifier(Ref<InputEventWithModifiers> e) {
  426. if (e->get_shift())
  427. return KEY_SHIFT;
  428. if (e->get_alt())
  429. return KEY_ALT;
  430. if (e->get_control())
  431. return KEY_CONTROL;
  432. if (e->get_metakey())
  433. return KEY_META;
  434. return 0;
  435. }
  436. bool SpatialEditorViewport::_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only) {
  437. if (!spatial_editor->is_gizmo_visible())
  438. return false;
  439. if (get_selected_count() == 0) {
  440. if (p_highlight_only)
  441. spatial_editor->select_gizmo_highlight_axis(-1);
  442. return false;
  443. }
  444. Vector3 ray_pos = _get_ray_pos(Vector2(p_screenpos.x, p_screenpos.y));
  445. Vector3 ray = _get_ray(Vector2(p_screenpos.x, p_screenpos.y));
  446. Transform gt = spatial_editor->get_gizmo_transform();
  447. float gs = gizmo_scale;
  448. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  449. int col_axis = -1;
  450. float col_d = 1e20;
  451. for (int i = 0; i < 3; i++) {
  452. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * (GIZMO_ARROW_OFFSET + (GIZMO_ARROW_SIZE * 0.5));
  453. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  454. Vector3 r;
  455. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * 10000.0, grabber_pos, grabber_radius, &r)) {
  456. float d = r.distance_to(ray_pos);
  457. if (d < col_d) {
  458. col_d = d;
  459. col_axis = i;
  460. }
  461. }
  462. }
  463. bool is_plane_translate = false;
  464. // plane select
  465. if (col_axis == -1) {
  466. col_d = 1e20;
  467. for (int i = 0; i < 3; i++) {
  468. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  469. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  470. Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST);
  471. Vector3 r;
  472. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  473. if (plane.intersects_ray(ray_pos, ray, &r)) {
  474. float dist = r.distance_to(grabber_pos);
  475. if (dist < (gs * GIZMO_PLANE_SIZE)) {
  476. float d = ray_pos.distance_to(r);
  477. if (d < col_d) {
  478. col_d = d;
  479. col_axis = i;
  480. is_plane_translate = true;
  481. }
  482. }
  483. }
  484. }
  485. }
  486. if (col_axis != -1) {
  487. if (p_highlight_only) {
  488. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_translate ? 6 : 0));
  489. } else {
  490. //handle plane translate
  491. _edit.mode = TRANSFORM_TRANSLATE;
  492. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  493. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_translate ? 3 : 0));
  494. }
  495. return true;
  496. }
  497. }
  498. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  499. int col_axis = -1;
  500. float col_d = 1e20;
  501. for (int i = 0; i < 3; i++) {
  502. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  503. Vector3 r;
  504. if (!plane.intersects_ray(ray_pos, ray, &r))
  505. continue;
  506. float dist = r.distance_to(gt.origin);
  507. if (dist > gs * (GIZMO_CIRCLE_SIZE - GIZMO_RING_HALF_WIDTH) && dist < gs * (GIZMO_CIRCLE_SIZE + GIZMO_RING_HALF_WIDTH)) {
  508. float d = ray_pos.distance_to(r);
  509. if (d < col_d) {
  510. col_d = d;
  511. col_axis = i;
  512. }
  513. }
  514. }
  515. if (col_axis != -1) {
  516. if (p_highlight_only) {
  517. spatial_editor->select_gizmo_highlight_axis(col_axis + 3);
  518. } else {
  519. //handle rotate
  520. _edit.mode = TRANSFORM_ROTATE;
  521. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  522. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis);
  523. }
  524. return true;
  525. }
  526. }
  527. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  528. int col_axis = -1;
  529. float col_d = 1e20;
  530. for (int i = 0; i < 3; i++) {
  531. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * GIZMO_SCALE_OFFSET;
  532. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  533. Vector3 r;
  534. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * 10000.0, grabber_pos, grabber_radius, &r)) {
  535. float d = r.distance_to(ray_pos);
  536. if (d < col_d) {
  537. col_d = d;
  538. col_axis = i;
  539. }
  540. }
  541. }
  542. bool is_plane_scale = false;
  543. // plane select
  544. if (col_axis == -1) {
  545. col_d = 1e20;
  546. for (int i = 0; i < 3; i++) {
  547. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  548. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  549. Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST);
  550. Vector3 r;
  551. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  552. if (plane.intersects_ray(ray_pos, ray, &r)) {
  553. float dist = r.distance_to(grabber_pos);
  554. if (dist < (gs * GIZMO_PLANE_SIZE)) {
  555. float d = ray_pos.distance_to(r);
  556. if (d < col_d) {
  557. col_d = d;
  558. col_axis = i;
  559. is_plane_scale = true;
  560. }
  561. }
  562. }
  563. }
  564. }
  565. if (col_axis != -1) {
  566. if (p_highlight_only) {
  567. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_scale ? 12 : 9));
  568. } else {
  569. //handle scale
  570. _edit.mode = TRANSFORM_SCALE;
  571. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  572. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_scale ? 3 : 0));
  573. }
  574. return true;
  575. }
  576. }
  577. if (p_highlight_only)
  578. spatial_editor->select_gizmo_highlight_axis(-1);
  579. return false;
  580. }
  581. void SpatialEditorViewport::_surface_mouse_enter() {
  582. if (!surface->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field()))
  583. surface->grab_focus();
  584. }
  585. void SpatialEditorViewport::_surface_mouse_exit() {
  586. _remove_preview();
  587. }
  588. void SpatialEditorViewport::_surface_focus_enter() {
  589. view_menu->set_disable_shortcuts(false);
  590. }
  591. void SpatialEditorViewport::_surface_focus_exit() {
  592. view_menu->set_disable_shortcuts(true);
  593. }
  594. void SpatialEditorViewport::_list_select(Ref<InputEventMouseButton> b) {
  595. _find_items_at_pos(b->get_position(), clicked_includes_current, selection_results, b->get_shift());
  596. Node *scene = editor->get_edited_scene();
  597. for (int i = 0; i < selection_results.size(); i++) {
  598. Spatial *item = selection_results[i].item;
  599. if (item != scene && item->get_owner() != scene && !scene->is_editable_instance(item->get_owner())) {
  600. //invalid result
  601. selection_results.remove(i);
  602. i--;
  603. }
  604. }
  605. clicked_wants_append = b->get_shift();
  606. if (selection_results.size() == 1) {
  607. clicked = selection_results[0].item->get_instance_id();
  608. selection_results.clear();
  609. if (clicked) {
  610. _select_clicked(clicked_wants_append, true);
  611. clicked = 0;
  612. }
  613. } else if (!selection_results.empty()) {
  614. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  615. StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
  616. for (int i = 0; i < selection_results.size(); i++) {
  617. Spatial *spat = selection_results[i].item;
  618. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(spat, "Node");
  619. String node_path = "/" + root_name + "/" + root_path.rel_path_to(spat->get_path());
  620. selection_menu->add_item(spat->get_name());
  621. selection_menu->set_item_icon(i, icon);
  622. selection_menu->set_item_metadata(i, node_path);
  623. selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path);
  624. }
  625. selection_menu->set_global_position(b->get_global_position());
  626. selection_menu->popup();
  627. }
  628. }
  629. void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
  630. if (previewing)
  631. return; //do NONE
  632. {
  633. EditorNode *en = editor;
  634. EditorPluginList *force_input_forwarding_list = en->get_editor_plugins_force_input_forwarding();
  635. if (!force_input_forwarding_list->empty()) {
  636. bool discard = force_input_forwarding_list->forward_spatial_gui_input(camera, p_event, true);
  637. if (discard)
  638. return;
  639. }
  640. }
  641. {
  642. EditorNode *en = editor;
  643. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  644. if (!over_plugin_list->empty()) {
  645. bool discard = over_plugin_list->forward_spatial_gui_input(camera, p_event, false);
  646. if (discard)
  647. return;
  648. }
  649. }
  650. Ref<InputEventMouseButton> b = p_event;
  651. if (b.is_valid()) {
  652. float zoom_factor = 1 + (ZOOM_MULTIPLIER - 1) * b->get_factor();
  653. switch (b->get_button_index()) {
  654. case BUTTON_WHEEL_UP: {
  655. if (is_freelook_active())
  656. scale_freelook_speed(zoom_factor);
  657. else
  658. scale_cursor_distance(1.0 / zoom_factor);
  659. } break;
  660. case BUTTON_WHEEL_DOWN: {
  661. if (is_freelook_active())
  662. scale_freelook_speed(1.0 / zoom_factor);
  663. else
  664. scale_cursor_distance(zoom_factor);
  665. } break;
  666. case BUTTON_RIGHT: {
  667. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  668. if (b->is_pressed() && _edit.gizmo.is_valid()) {
  669. //restore
  670. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, true);
  671. _edit.gizmo = Ref<EditorSpatialGizmo>();
  672. }
  673. if (_edit.mode == TRANSFORM_NONE && b->is_pressed()) {
  674. if (b->get_alt()) {
  675. if (nav_scheme == NAVIGATION_MAYA)
  676. break;
  677. _list_select(b);
  678. return;
  679. }
  680. }
  681. if (_edit.mode != TRANSFORM_NONE && b->is_pressed()) {
  682. //cancel motion
  683. _edit.mode = TRANSFORM_NONE;
  684. List<Node *> &selection = editor_selection->get_selected_node_list();
  685. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  686. Spatial *sp = Object::cast_to<Spatial>(E->get());
  687. if (!sp)
  688. continue;
  689. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  690. if (!se)
  691. continue;
  692. sp->set_global_transform(se->original);
  693. }
  694. surface->update();
  695. set_message(TTR("Transform Aborted."), 3);
  696. }
  697. if (b->is_pressed()) {
  698. int mod = _get_key_modifier(b);
  699. if (!orthogonal) {
  700. if (mod == _get_key_modifier_setting("editors/3d/freelook/freelook_activation_modifier")) {
  701. set_freelook_active(true);
  702. }
  703. }
  704. } else {
  705. set_freelook_active(false);
  706. }
  707. if (freelook_active && !surface->has_focus()) {
  708. // Focus usually doesn't trigger on right-click, but in case of freelook it should,
  709. // otherwise using keyboard navigation would misbehave
  710. surface->grab_focus();
  711. }
  712. } break;
  713. case BUTTON_MIDDLE: {
  714. if (b->is_pressed() && _edit.mode != TRANSFORM_NONE) {
  715. switch (_edit.plane) {
  716. case TRANSFORM_VIEW: {
  717. _edit.plane = TRANSFORM_X_AXIS;
  718. set_message(TTR("X-Axis Transform."), 2);
  719. name = "";
  720. _update_name();
  721. } break;
  722. case TRANSFORM_X_AXIS: {
  723. _edit.plane = TRANSFORM_Y_AXIS;
  724. set_message(TTR("Y-Axis Transform."), 2);
  725. } break;
  726. case TRANSFORM_Y_AXIS: {
  727. _edit.plane = TRANSFORM_Z_AXIS;
  728. set_message(TTR("Z-Axis Transform."), 2);
  729. } break;
  730. case TRANSFORM_Z_AXIS: {
  731. _edit.plane = TRANSFORM_VIEW;
  732. set_message(TTR("View Plane Transform."), 2);
  733. } break;
  734. case TRANSFORM_YZ:
  735. case TRANSFORM_XZ:
  736. case TRANSFORM_XY: {
  737. } break;
  738. }
  739. }
  740. } break;
  741. case BUTTON_LEFT: {
  742. if (b->is_pressed()) {
  743. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  744. if ((nav_scheme == NAVIGATION_MAYA || nav_scheme == NAVIGATION_MODO) && b->get_alt()) {
  745. break;
  746. }
  747. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_LIST_SELECT) {
  748. _list_select(b);
  749. break;
  750. }
  751. _edit.mouse_pos = b->get_position();
  752. _edit.snap = false;
  753. _edit.mode = TRANSFORM_NONE;
  754. //gizmo has priority over everything
  755. bool can_select_gizmos = true;
  756. {
  757. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  758. can_select_gizmos = view_menu->get_popup()->is_item_checked(idx);
  759. }
  760. if (can_select_gizmos && spatial_editor->get_selected()) {
  761. Ref<EditorSpatialGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  762. if (seg.is_valid()) {
  763. int handle = -1;
  764. Vector3 point;
  765. Vector3 normal;
  766. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, b->get_shift());
  767. if (inters && handle != -1) {
  768. _edit.gizmo = seg;
  769. _edit.gizmo_handle = handle;
  770. _edit.gizmo_initial_value = seg->get_handle_value(handle);
  771. break;
  772. }
  773. }
  774. }
  775. if (_gizmo_select(_edit.mouse_pos))
  776. break;
  777. clicked = 0;
  778. clicked_includes_current = false;
  779. if ((spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT && b->get_control()) || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  780. /* HANDLE ROTATION */
  781. if (get_selected_count() == 0)
  782. break; //bye
  783. //handle rotate
  784. _edit.mode = TRANSFORM_ROTATE;
  785. _compute_edit(b->get_position());
  786. break;
  787. }
  788. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  789. if (get_selected_count() == 0)
  790. break; //bye
  791. //handle translate
  792. _edit.mode = TRANSFORM_TRANSLATE;
  793. _compute_edit(b->get_position());
  794. break;
  795. }
  796. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  797. if (get_selected_count() == 0)
  798. break; //bye
  799. //handle scale
  800. _edit.mode = TRANSFORM_SCALE;
  801. _compute_edit(b->get_position());
  802. break;
  803. }
  804. // todo scale
  805. int gizmo_handle = -1;
  806. clicked = _select_ray(b->get_position(), b->get_shift(), clicked_includes_current, &gizmo_handle, b->get_shift());
  807. //clicking is always deferred to either move or release
  808. clicked_wants_append = b->get_shift();
  809. if (!clicked) {
  810. if (!clicked_wants_append)
  811. _clear_selected();
  812. //default to regionselect
  813. cursor.region_select = true;
  814. cursor.region_begin = b->get_position();
  815. cursor.region_end = b->get_position();
  816. }
  817. if (clicked && gizmo_handle >= 0) {
  818. Spatial *spa = Object::cast_to<Spatial>(ObjectDB::get_instance(clicked));
  819. if (spa) {
  820. Ref<EditorSpatialGizmo> seg = spa->get_gizmo();
  821. if (seg.is_valid()) {
  822. _edit.gizmo = seg;
  823. _edit.gizmo_handle = gizmo_handle;
  824. _edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle);
  825. break;
  826. }
  827. }
  828. }
  829. surface->update();
  830. } else {
  831. if (_edit.gizmo.is_valid()) {
  832. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, false);
  833. _edit.gizmo = Ref<EditorSpatialGizmo>();
  834. break;
  835. }
  836. if (clicked) {
  837. _select_clicked(clicked_wants_append, true);
  838. // Processing was deferred.
  839. clicked = 0;
  840. }
  841. if (cursor.region_select) {
  842. if (!clicked_wants_append) _clear_selected();
  843. _select_region();
  844. cursor.region_select = false;
  845. surface->update();
  846. }
  847. if (_edit.mode != TRANSFORM_NONE) {
  848. static const char *_transform_name[4] = { "None", "Rotate", "Translate", "Scale" };
  849. undo_redo->create_action(_transform_name[_edit.mode]);
  850. List<Node *> &selection = editor_selection->get_selected_node_list();
  851. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  852. Spatial *sp = Object::cast_to<Spatial>(E->get());
  853. if (!sp)
  854. continue;
  855. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  856. if (!se)
  857. continue;
  858. undo_redo->add_do_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  859. undo_redo->add_undo_method(sp, "set_global_transform", se->original);
  860. }
  861. undo_redo->commit_action();
  862. _edit.mode = TRANSFORM_NONE;
  863. set_message("");
  864. }
  865. surface->update();
  866. }
  867. } break;
  868. }
  869. }
  870. Ref<InputEventMouseMotion> m = p_event;
  871. if (m.is_valid()) {
  872. _edit.mouse_pos = m->get_position();
  873. if (spatial_editor->get_selected()) {
  874. Ref<EditorSpatialGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  875. if (seg.is_valid()) {
  876. int selected_handle = -1;
  877. int handle = -1;
  878. Vector3 point;
  879. Vector3 normal;
  880. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, false);
  881. if (inters && handle != -1) {
  882. selected_handle = handle;
  883. }
  884. if (selected_handle != spatial_editor->get_over_gizmo_handle()) {
  885. spatial_editor->set_over_gizmo_handle(selected_handle);
  886. spatial_editor->get_selected()->update_gizmo();
  887. if (selected_handle != -1)
  888. spatial_editor->select_gizmo_highlight_axis(-1);
  889. }
  890. }
  891. }
  892. if (spatial_editor->get_over_gizmo_handle() == -1 && !(m->get_button_mask() & 1) && !_edit.gizmo.is_valid()) {
  893. _gizmo_select(_edit.mouse_pos, true);
  894. }
  895. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  896. NavigationMode nav_mode = NAVIGATION_NONE;
  897. if (_edit.gizmo.is_valid()) {
  898. _edit.gizmo->set_handle(_edit.gizmo_handle, camera, m->get_position());
  899. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle);
  900. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle);
  901. set_message(n + ": " + String(v));
  902. } else if (m->get_button_mask() & BUTTON_MASK_LEFT) {
  903. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  904. nav_mode = NAVIGATION_ORBIT;
  905. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_shift()) {
  906. nav_mode = NAVIGATION_PAN;
  907. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_control()) {
  908. nav_mode = NAVIGATION_ZOOM;
  909. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt()) {
  910. nav_mode = NAVIGATION_ORBIT;
  911. } else {
  912. if (clicked) {
  913. if (!clicked_includes_current) {
  914. _select_clicked(clicked_wants_append, true);
  915. // Processing was deferred.
  916. }
  917. _compute_edit(_edit.mouse_pos);
  918. clicked = 0;
  919. _edit.mode = TRANSFORM_TRANSLATE;
  920. }
  921. if (cursor.region_select && nav_mode == NAVIGATION_NONE) {
  922. cursor.region_end = m->get_position();
  923. surface->update();
  924. return;
  925. }
  926. if (_edit.mode == TRANSFORM_NONE && nav_mode == NAVIGATION_NONE)
  927. return;
  928. Vector3 ray_pos = _get_ray_pos(m->get_position());
  929. Vector3 ray = _get_ray(m->get_position());
  930. switch (_edit.mode) {
  931. case TRANSFORM_SCALE: {
  932. Vector3 motion_mask;
  933. Plane plane;
  934. bool plane_mv = false;
  935. switch (_edit.plane) {
  936. case TRANSFORM_VIEW:
  937. motion_mask = Vector3(0, 0, 0);
  938. plane = Plane(_edit.center, _get_camera_normal());
  939. break;
  940. case TRANSFORM_X_AXIS:
  941. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  942. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  943. break;
  944. case TRANSFORM_Y_AXIS:
  945. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  946. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  947. break;
  948. case TRANSFORM_Z_AXIS:
  949. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  950. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  951. break;
  952. case TRANSFORM_YZ:
  953. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  954. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  955. plane_mv = true;
  956. break;
  957. case TRANSFORM_XZ:
  958. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(0);
  959. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  960. plane_mv = true;
  961. break;
  962. case TRANSFORM_XY:
  963. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  964. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  965. plane_mv = true;
  966. break;
  967. }
  968. Vector3 intersection;
  969. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  970. break;
  971. Vector3 click;
  972. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  973. break;
  974. Vector3 motion = intersection - click;
  975. if (_edit.plane != TRANSFORM_VIEW) {
  976. if (!plane_mv) {
  977. motion = motion_mask.dot(motion) * motion_mask;
  978. } else {
  979. // Alternative planar scaling mode
  980. if (_get_key_modifier(m) != KEY_SHIFT) {
  981. motion = motion_mask.dot(motion) * motion_mask;
  982. }
  983. }
  984. } else {
  985. float center_click_dist = click.distance_to(_edit.center);
  986. float center_inters_dist = intersection.distance_to(_edit.center);
  987. if (center_click_dist == 0)
  988. break;
  989. float scale = center_inters_dist - center_click_dist;
  990. motion = Vector3(scale, scale, scale);
  991. }
  992. List<Node *> &selection = editor_selection->get_selected_node_list();
  993. // Disable local transformation for TRANSFORM_VIEW
  994. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  995. float snap = 0;
  996. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  997. snap = spatial_editor->get_scale_snap() / 100;
  998. Vector3 motion_snapped = motion;
  999. motion_snapped.snap(Vector3(snap, snap, snap));
  1000. set_message(TTR("Scaling: ") + motion_snapped);
  1001. } else {
  1002. set_message(TTR("Scaling: ") + motion);
  1003. }
  1004. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1005. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1006. if (!sp) {
  1007. continue;
  1008. }
  1009. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1010. if (!se) {
  1011. continue;
  1012. }
  1013. if (sp->has_meta("_edit_lock_")) {
  1014. continue;
  1015. }
  1016. Transform original = se->original;
  1017. Transform original_local = se->original_local;
  1018. Transform base = Transform(Basis(), _edit.center);
  1019. Transform t;
  1020. Vector3 local_scale;
  1021. if (local_coords) {
  1022. Basis g = original.basis.orthonormalized();
  1023. Vector3 local_motion = g.inverse().xform(motion);
  1024. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1025. local_motion.snap(Vector3(snap, snap, snap));
  1026. }
  1027. local_scale = original_local.basis.get_scale() * (local_motion + Vector3(1, 1, 1));
  1028. // Prevent scaling to 0 it would break the gizmo
  1029. Basis check = original_local.basis;
  1030. check.scale(local_scale);
  1031. if (check.determinant() != 0) {
  1032. // Apply scale
  1033. sp->set_scale(local_scale);
  1034. }
  1035. } else {
  1036. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1037. motion.snap(Vector3(snap, snap, snap));
  1038. }
  1039. Transform r;
  1040. r.basis.scale(motion + Vector3(1, 1, 1));
  1041. t = base * (r * (base.inverse() * original));
  1042. // Apply scale
  1043. sp->set_global_transform(t);
  1044. }
  1045. }
  1046. surface->update();
  1047. } break;
  1048. case TRANSFORM_TRANSLATE: {
  1049. Vector3 motion_mask;
  1050. Plane plane;
  1051. bool plane_mv = false;
  1052. switch (_edit.plane) {
  1053. case TRANSFORM_VIEW:
  1054. plane = Plane(_edit.center, _get_camera_normal());
  1055. break;
  1056. case TRANSFORM_X_AXIS:
  1057. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  1058. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1059. break;
  1060. case TRANSFORM_Y_AXIS:
  1061. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  1062. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1063. break;
  1064. case TRANSFORM_Z_AXIS:
  1065. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  1066. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1067. break;
  1068. case TRANSFORM_YZ:
  1069. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1070. plane_mv = true;
  1071. break;
  1072. case TRANSFORM_XZ:
  1073. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1074. plane_mv = true;
  1075. break;
  1076. case TRANSFORM_XY:
  1077. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1078. plane_mv = true;
  1079. break;
  1080. }
  1081. Vector3 intersection;
  1082. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  1083. break;
  1084. Vector3 click;
  1085. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  1086. break;
  1087. Vector3 motion = intersection - click;
  1088. if (_edit.plane != TRANSFORM_VIEW) {
  1089. if (!plane_mv) {
  1090. motion = motion_mask.dot(motion) * motion_mask;
  1091. }
  1092. }
  1093. List<Node *> &selection = editor_selection->get_selected_node_list();
  1094. // Disable local transformation for TRANSFORM_VIEW
  1095. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  1096. float snap = 0;
  1097. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1098. snap = spatial_editor->get_translate_snap();
  1099. Vector3 motion_snapped = motion;
  1100. motion_snapped.snap(Vector3(snap, snap, snap));
  1101. set_message(TTR("Translating: ") + motion_snapped);
  1102. } else {
  1103. set_message(TTR("Translating: ") + motion);
  1104. }
  1105. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1106. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1107. if (!sp) {
  1108. continue;
  1109. }
  1110. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1111. if (!se) {
  1112. continue;
  1113. }
  1114. if (sp->has_meta("_edit_lock_")) {
  1115. continue;
  1116. }
  1117. Transform original = se->original;
  1118. Transform t;
  1119. if (local_coords) {
  1120. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1121. Basis g = original.basis.orthonormalized();
  1122. Vector3 local_motion = g.inverse().xform(motion);
  1123. local_motion.snap(Vector3(snap, snap, snap));
  1124. motion = g.xform(local_motion);
  1125. }
  1126. } else {
  1127. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1128. motion.snap(Vector3(snap, snap, snap));
  1129. }
  1130. }
  1131. // Apply translation
  1132. t = original;
  1133. t.origin += motion;
  1134. sp->set_global_transform(t);
  1135. }
  1136. surface->update();
  1137. } break;
  1138. case TRANSFORM_ROTATE: {
  1139. Plane plane;
  1140. Vector3 axis;
  1141. switch (_edit.plane) {
  1142. case TRANSFORM_VIEW:
  1143. plane = Plane(_edit.center, _get_camera_normal());
  1144. break;
  1145. case TRANSFORM_X_AXIS:
  1146. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1147. axis = Vector3(1, 0, 0);
  1148. break;
  1149. case TRANSFORM_Y_AXIS:
  1150. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1151. axis = Vector3(0, 1, 0);
  1152. break;
  1153. case TRANSFORM_Z_AXIS:
  1154. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1155. axis = Vector3(0, 0, 1);
  1156. break;
  1157. case TRANSFORM_YZ:
  1158. case TRANSFORM_XZ:
  1159. case TRANSFORM_XY:
  1160. break;
  1161. }
  1162. Vector3 intersection;
  1163. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  1164. break;
  1165. Vector3 click;
  1166. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  1167. break;
  1168. Vector3 y_axis = (click - _edit.center).normalized();
  1169. Vector3 x_axis = plane.normal.cross(y_axis).normalized();
  1170. float angle = Math::atan2(x_axis.dot(intersection - _edit.center), y_axis.dot(intersection - _edit.center));
  1171. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1172. float snap = spatial_editor->get_rotate_snap();
  1173. if (snap) {
  1174. angle = Math::rad2deg(angle) + snap * 0.5; //else it won't reach +180
  1175. angle -= Math::fmod(angle, snap);
  1176. set_message(vformat(TTR("Rotating %s degrees."), rtos(angle)));
  1177. angle = Math::deg2rad(angle);
  1178. } else
  1179. set_message(vformat(TTR("Rotating %s degrees."), rtos(Math::rad2deg(angle))));
  1180. } else {
  1181. set_message(vformat(TTR("Rotating %s degrees."), rtos(Math::rad2deg(angle))));
  1182. }
  1183. List<Node *> &selection = editor_selection->get_selected_node_list();
  1184. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  1185. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1186. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1187. if (!sp)
  1188. continue;
  1189. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1190. if (!se)
  1191. continue;
  1192. if (sp->has_meta("_edit_lock_")) {
  1193. continue;
  1194. }
  1195. Transform t;
  1196. if (local_coords) {
  1197. Transform original_local = se->original_local;
  1198. Basis rot = Basis(axis, angle);
  1199. t.basis = original_local.get_basis().orthonormalized() * rot;
  1200. t.origin = original_local.origin;
  1201. // Apply rotation
  1202. sp->set_transform(t);
  1203. sp->set_scale(original_local.basis.get_scale()); // re-apply original scale
  1204. } else {
  1205. Transform original = se->original;
  1206. Transform r;
  1207. Transform base = Transform(Basis(), _edit.center);
  1208. r.basis.rotate(plane.normal, angle);
  1209. t = base * r * base.inverse() * original;
  1210. // Apply rotation
  1211. sp->set_global_transform(t);
  1212. }
  1213. }
  1214. surface->update();
  1215. } break;
  1216. default: {}
  1217. }
  1218. }
  1219. } else if ((m->get_button_mask() & BUTTON_MASK_RIGHT) || freelook_active) {
  1220. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  1221. nav_mode = NAVIGATION_ZOOM;
  1222. } else if (freelook_active) {
  1223. nav_mode = NAVIGATION_LOOK;
  1224. } else if (orthogonal) {
  1225. nav_mode = NAVIGATION_PAN;
  1226. }
  1227. } else if (m->get_button_mask() & BUTTON_MASK_MIDDLE) {
  1228. if (nav_scheme == NAVIGATION_GODOT) {
  1229. int mod = _get_key_modifier(m);
  1230. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1231. nav_mode = NAVIGATION_PAN;
  1232. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1233. nav_mode = NAVIGATION_ZOOM;
  1234. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1235. nav_mode = NAVIGATION_ORBIT;
  1236. } else if (nav_scheme == NAVIGATION_MAYA) {
  1237. if (m->get_alt())
  1238. nav_mode = NAVIGATION_PAN;
  1239. }
  1240. } else if (EditorSettings::get_singleton()->get("editors/3d/navigation/emulate_3_button_mouse")) {
  1241. // Handle trackpad (no external mouse) use case
  1242. int mod = _get_key_modifier(m);
  1243. if (mod) {
  1244. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1245. nav_mode = NAVIGATION_PAN;
  1246. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1247. nav_mode = NAVIGATION_ZOOM;
  1248. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1249. nav_mode = NAVIGATION_ORBIT;
  1250. }
  1251. }
  1252. switch (nav_mode) {
  1253. case NAVIGATION_PAN: {
  1254. _nav_pan(m, _get_warped_mouse_motion(m));
  1255. } break;
  1256. case NAVIGATION_ZOOM: {
  1257. _nav_zoom(m, m->get_relative());
  1258. } break;
  1259. case NAVIGATION_ORBIT: {
  1260. _nav_orbit(m, _get_warped_mouse_motion(m));
  1261. } break;
  1262. case NAVIGATION_LOOK: {
  1263. _nav_look(m, _get_warped_mouse_motion(m));
  1264. } break;
  1265. default: {}
  1266. }
  1267. }
  1268. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  1269. if (magnify_gesture.is_valid()) {
  1270. if (is_freelook_active())
  1271. scale_freelook_speed(magnify_gesture->get_factor());
  1272. else
  1273. scale_cursor_distance(1.0 / magnify_gesture->get_factor());
  1274. }
  1275. Ref<InputEventPanGesture> pan_gesture = p_event;
  1276. if (pan_gesture.is_valid()) {
  1277. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1278. NavigationMode nav_mode = NAVIGATION_NONE;
  1279. if (nav_scheme == NAVIGATION_GODOT) {
  1280. int mod = _get_key_modifier(pan_gesture);
  1281. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1282. nav_mode = NAVIGATION_PAN;
  1283. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1284. nav_mode = NAVIGATION_ZOOM;
  1285. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1286. nav_mode = NAVIGATION_ORBIT;
  1287. } else if (nav_scheme == NAVIGATION_MAYA) {
  1288. if (pan_gesture->get_alt())
  1289. nav_mode = NAVIGATION_PAN;
  1290. }
  1291. switch (nav_mode) {
  1292. case NAVIGATION_PAN: {
  1293. _nav_pan(m, pan_gesture->get_delta());
  1294. } break;
  1295. case NAVIGATION_ZOOM: {
  1296. _nav_zoom(m, pan_gesture->get_delta());
  1297. } break;
  1298. case NAVIGATION_ORBIT: {
  1299. _nav_orbit(m, pan_gesture->get_delta());
  1300. } break;
  1301. case NAVIGATION_LOOK: {
  1302. _nav_look(m, pan_gesture->get_delta());
  1303. } break;
  1304. default: {}
  1305. }
  1306. }
  1307. Ref<InputEventKey> k = p_event;
  1308. if (k.is_valid()) {
  1309. if (!k->is_pressed())
  1310. return;
  1311. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1312. if (_edit.mode != TRANSFORM_NONE) {
  1313. _edit.snap = true;
  1314. }
  1315. }
  1316. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1317. _menu_option(VIEW_BOTTOM);
  1318. }
  1319. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1320. _menu_option(VIEW_TOP);
  1321. }
  1322. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1323. _menu_option(VIEW_REAR);
  1324. }
  1325. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1326. _menu_option(VIEW_FRONT);
  1327. }
  1328. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1329. _menu_option(VIEW_LEFT);
  1330. }
  1331. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1332. _menu_option(VIEW_RIGHT);
  1333. }
  1334. if (ED_IS_SHORTCUT("spatial_editor/focus_origin", p_event)) {
  1335. _menu_option(VIEW_CENTER_TO_ORIGIN);
  1336. }
  1337. if (ED_IS_SHORTCUT("spatial_editor/focus_selection", p_event)) {
  1338. _menu_option(VIEW_CENTER_TO_SELECTION);
  1339. }
  1340. // Orthgonal mode doesn't work in freelook.
  1341. if (!freelook_active && ED_IS_SHORTCUT("spatial_editor/switch_perspective_orthogonal", p_event)) {
  1342. _menu_option(orthogonal ? VIEW_PERSPECTIVE : VIEW_ORTHOGONAL);
  1343. _update_name();
  1344. }
  1345. if (ED_IS_SHORTCUT("spatial_editor/align_selection_with_view", p_event)) {
  1346. _menu_option(VIEW_ALIGN_SELECTION_WITH_VIEW);
  1347. }
  1348. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  1349. if (!get_selected_count() || _edit.mode != TRANSFORM_NONE)
  1350. return;
  1351. if (!AnimationPlayerEditor::singleton->get_track_editor()->has_keying()) {
  1352. set_message(TTR("Keying is disabled (no key inserted)."));
  1353. return;
  1354. }
  1355. List<Node *> &selection = editor_selection->get_selected_node_list();
  1356. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1357. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1358. if (!sp)
  1359. continue;
  1360. emit_signal("transform_key_request", sp, "", sp->get_transform());
  1361. }
  1362. set_message(TTR("Animation Key Inserted."));
  1363. }
  1364. // Freelook doesn't work in orthogonal mode.
  1365. if (!orthogonal && ED_IS_SHORTCUT("spatial_editor/freelook_toggle", p_event)) {
  1366. set_freelook_active(!is_freelook_active());
  1367. } else if (k->get_scancode() == KEY_ESCAPE) {
  1368. set_freelook_active(false);
  1369. }
  1370. if (k->get_scancode() == KEY_SPACE) {
  1371. if (!k->is_pressed()) emit_signal("toggle_maximize_view", this);
  1372. }
  1373. }
  1374. // freelook uses most of the useful shortcuts, like save, so its ok
  1375. // to consider freelook active as end of the line for future events.
  1376. if (freelook_active)
  1377. accept_event();
  1378. }
  1379. void SpatialEditorViewport::_nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1380. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1381. real_t pan_speed = 1 / 150.0;
  1382. int pan_speed_modifier = 10;
  1383. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift())
  1384. pan_speed *= pan_speed_modifier;
  1385. Transform camera_transform;
  1386. camera_transform.translate(cursor.pos);
  1387. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1388. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1389. Vector3 translation(-p_relative.x * pan_speed, p_relative.y * pan_speed, 0);
  1390. translation *= cursor.distance / DISTANCE_DEFAULT;
  1391. camera_transform.translate(translation);
  1392. cursor.pos = camera_transform.origin;
  1393. }
  1394. void SpatialEditorViewport::_nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1395. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1396. real_t zoom_speed = 1 / 80.0;
  1397. int zoom_speed_modifier = 10;
  1398. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift())
  1399. zoom_speed *= zoom_speed_modifier;
  1400. NavigationZoomStyle zoom_style = (NavigationZoomStyle)EditorSettings::get_singleton()->get("editors/3d/navigation/zoom_style").operator int();
  1401. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  1402. if (p_relative.x > 0)
  1403. scale_cursor_distance(1 - p_relative.x * zoom_speed);
  1404. else if (p_relative.x < 0)
  1405. scale_cursor_distance(1.0 / (1 + p_relative.x * zoom_speed));
  1406. } else {
  1407. if (p_relative.y > 0)
  1408. scale_cursor_distance(1 + p_relative.y * zoom_speed);
  1409. else if (p_relative.y < 0)
  1410. scale_cursor_distance(1.0 / (1 - p_relative.y * zoom_speed));
  1411. }
  1412. }
  1413. void SpatialEditorViewport::_nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1414. if (lock_rotation) {
  1415. _nav_pan(p_event, p_relative);
  1416. return;
  1417. }
  1418. real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1419. real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1420. bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1421. if (invert_y_axis) {
  1422. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1423. } else {
  1424. cursor.x_rot += p_relative.y * radians_per_pixel;
  1425. }
  1426. cursor.y_rot += p_relative.x * radians_per_pixel;
  1427. if (cursor.x_rot > Math_PI / 2.0)
  1428. cursor.x_rot = Math_PI / 2.0;
  1429. if (cursor.x_rot < -Math_PI / 2.0)
  1430. cursor.x_rot = -Math_PI / 2.0;
  1431. name = "";
  1432. _update_name();
  1433. }
  1434. void SpatialEditorViewport::_nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1435. if (orthogonal) {
  1436. _nav_pan(p_event, p_relative);
  1437. return;
  1438. }
  1439. real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1440. real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1441. bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1442. // Note: do NOT assume the camera has the "current" transform, because it is interpolated and may have "lag".
  1443. Transform prev_camera_transform = to_camera_transform(cursor);
  1444. if (invert_y_axis) {
  1445. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1446. } else {
  1447. cursor.x_rot += p_relative.y * radians_per_pixel;
  1448. }
  1449. cursor.y_rot += p_relative.x * radians_per_pixel;
  1450. if (cursor.x_rot > Math_PI / 2.0)
  1451. cursor.x_rot = Math_PI / 2.0;
  1452. if (cursor.x_rot < -Math_PI / 2.0)
  1453. cursor.x_rot = -Math_PI / 2.0;
  1454. // Look is like the opposite of Orbit: the focus point rotates around the camera
  1455. Transform camera_transform = to_camera_transform(cursor);
  1456. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  1457. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  1458. Vector3 diff = prev_pos - pos;
  1459. cursor.pos += diff;
  1460. name = "";
  1461. _update_name();
  1462. }
  1463. void SpatialEditorViewport::set_freelook_active(bool active_now) {
  1464. if (!freelook_active && active_now) {
  1465. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1466. cursor = camera_cursor;
  1467. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  1468. Vector3 forward = to_camera_transform(cursor).basis.xform(Vector3(0, 0, -1));
  1469. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  1470. // Also sync the camera cursor, otherwise switching to freelook will be trippy if inertia is active
  1471. camera_cursor.eye_pos = cursor.eye_pos;
  1472. if (EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_speed_zoom_link")) {
  1473. // Re-adjust freelook speed from the current zoom level
  1474. real_t base_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  1475. freelook_speed = base_speed * cursor.distance;
  1476. }
  1477. // Hide mouse like in an FPS (warping doesn't work)
  1478. OS::get_singleton()->set_mouse_mode(OS::MOUSE_MODE_CAPTURED);
  1479. } else if (freelook_active && !active_now) {
  1480. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1481. cursor = camera_cursor;
  1482. // Restore mouse
  1483. OS::get_singleton()->set_mouse_mode(OS::MOUSE_MODE_VISIBLE);
  1484. }
  1485. freelook_active = active_now;
  1486. }
  1487. void SpatialEditorViewport::scale_cursor_distance(real_t scale) {
  1488. // Prevents zero distance which would short-circuit any scaling
  1489. if (cursor.distance < ZOOM_MIN_DISTANCE)
  1490. cursor.distance = ZOOM_MIN_DISTANCE;
  1491. cursor.distance *= scale;
  1492. if (cursor.distance < ZOOM_MIN_DISTANCE)
  1493. cursor.distance = ZOOM_MIN_DISTANCE;
  1494. zoom_indicator_delay = ZOOM_INDICATOR_DELAY_S;
  1495. surface->update();
  1496. }
  1497. void SpatialEditorViewport::scale_freelook_speed(real_t scale) {
  1498. // Prevents zero distance which would short-circuit any scaling
  1499. if (freelook_speed < FREELOOK_MIN_SPEED)
  1500. freelook_speed = FREELOOK_MIN_SPEED;
  1501. freelook_speed *= scale;
  1502. if (freelook_speed < FREELOOK_MIN_SPEED)
  1503. freelook_speed = FREELOOK_MIN_SPEED;
  1504. zoom_indicator_delay = ZOOM_INDICATOR_DELAY_S;
  1505. surface->update();
  1506. }
  1507. Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const {
  1508. Point2i relative;
  1509. if (bool(EDITOR_DEF("editors/3d/navigation/warped_mouse_panning", false))) {
  1510. relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect());
  1511. } else {
  1512. relative = p_ev_mouse_motion->get_relative();
  1513. }
  1514. return relative;
  1515. }
  1516. static bool is_shortcut_pressed(const String &p_path) {
  1517. Ref<ShortCut> shortcut = ED_GET_SHORTCUT(p_path);
  1518. if (shortcut.is_null()) {
  1519. return false;
  1520. }
  1521. InputEventKey *k = Object::cast_to<InputEventKey>(shortcut->get_shortcut().ptr());
  1522. if (k == NULL) {
  1523. return false;
  1524. }
  1525. const Input &input = *Input::get_singleton();
  1526. int scancode = k->get_scancode();
  1527. return input.is_key_pressed(scancode);
  1528. }
  1529. void SpatialEditorViewport::_update_freelook(real_t delta) {
  1530. if (!is_freelook_active()) {
  1531. return;
  1532. }
  1533. Vector3 forward = camera->get_transform().basis.xform(Vector3(0, 0, -1));
  1534. Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0));
  1535. Vector3 up = camera->get_transform().basis.xform(Vector3(0, 1, 0));
  1536. Vector3 direction;
  1537. bool speed_modifier = false;
  1538. if (is_shortcut_pressed("spatial_editor/freelook_left")) {
  1539. direction -= right;
  1540. }
  1541. if (is_shortcut_pressed("spatial_editor/freelook_right")) {
  1542. direction += right;
  1543. }
  1544. if (is_shortcut_pressed("spatial_editor/freelook_forward")) {
  1545. direction += forward;
  1546. }
  1547. if (is_shortcut_pressed("spatial_editor/freelook_backwards")) {
  1548. direction -= forward;
  1549. }
  1550. if (is_shortcut_pressed("spatial_editor/freelook_up")) {
  1551. direction += up;
  1552. }
  1553. if (is_shortcut_pressed("spatial_editor/freelook_down")) {
  1554. direction -= up;
  1555. }
  1556. if (is_shortcut_pressed("spatial_editor/freelook_speed_modifier")) {
  1557. speed_modifier = true;
  1558. }
  1559. real_t speed = freelook_speed;
  1560. if (speed_modifier) {
  1561. real_t modifier_speed_factor = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_modifier_speed_factor");
  1562. speed *= modifier_speed_factor;
  1563. }
  1564. Vector3 motion = direction * speed * delta;
  1565. cursor.pos += motion;
  1566. cursor.eye_pos += motion;
  1567. }
  1568. void SpatialEditorViewport::set_message(String p_message, float p_time) {
  1569. message = p_message;
  1570. message_time = p_time;
  1571. }
  1572. void SpatialEditorPlugin::edited_scene_changed() {
  1573. for (uint32_t i = 0; i < SpatialEditor::VIEWPORTS_COUNT; i++) {
  1574. SpatialEditorViewport *viewport = SpatialEditor::get_singleton()->get_editor_viewport(i);
  1575. if (viewport->is_visible()) {
  1576. viewport->notification(Control::NOTIFICATION_VISIBILITY_CHANGED);
  1577. }
  1578. }
  1579. }
  1580. void SpatialEditorViewport::_notification(int p_what) {
  1581. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  1582. bool visible = is_visible_in_tree();
  1583. set_process(visible);
  1584. if (visible) {
  1585. _update_camera(0);
  1586. } else {
  1587. set_freelook_active(false);
  1588. }
  1589. call_deferred("update_transform_gizmo_view");
  1590. }
  1591. if (p_what == NOTIFICATION_RESIZED) {
  1592. call_deferred("update_transform_gizmo_view");
  1593. }
  1594. if (p_what == NOTIFICATION_PROCESS) {
  1595. real_t delta = get_process_delta_time();
  1596. if (zoom_indicator_delay > 0) {
  1597. zoom_indicator_delay -= delta;
  1598. if (zoom_indicator_delay <= 0) {
  1599. surface->update();
  1600. }
  1601. }
  1602. _update_freelook(delta);
  1603. Node *scene_root = editor->get_scene_tree_dock()->get_editor_data()->get_edited_scene_root();
  1604. if (previewing_cinema && scene_root != NULL) {
  1605. Camera *cam = scene_root->get_viewport()->get_camera();
  1606. if (cam != NULL && cam != previewing) {
  1607. //then switch the viewport's camera to the scene's viewport camera
  1608. if (previewing != NULL) {
  1609. previewing->disconnect("tree_exited", this, "_preview_exited_scene");
  1610. }
  1611. previewing = cam;
  1612. previewing->connect("tree_exited", this, "_preview_exited_scene");
  1613. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), cam->get_camera());
  1614. surface->update();
  1615. }
  1616. }
  1617. _update_camera(delta);
  1618. Map<Node *, Object *> &selection = editor_selection->get_selection();
  1619. bool changed = false;
  1620. bool exist = false;
  1621. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  1622. Spatial *sp = Object::cast_to<Spatial>(E->key());
  1623. if (!sp)
  1624. continue;
  1625. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1626. if (!se)
  1627. continue;
  1628. VisualInstance *vi = Object::cast_to<VisualInstance>(sp);
  1629. se->aabb = vi ? vi->get_aabb() : AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4));
  1630. Transform t = sp->get_global_gizmo_transform();
  1631. t.translate(se->aabb.position);
  1632. // apply AABB scaling before item's global transform
  1633. Basis aabb_s;
  1634. aabb_s.scale(se->aabb.size);
  1635. t.basis = t.basis * aabb_s;
  1636. exist = true;
  1637. if (se->last_xform == t)
  1638. continue;
  1639. changed = true;
  1640. se->last_xform = t;
  1641. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance, t);
  1642. }
  1643. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  1644. spatial_editor->update_transform_gizmo();
  1645. }
  1646. if (message_time > 0) {
  1647. if (message != last_message) {
  1648. surface->update();
  1649. last_message = message;
  1650. }
  1651. message_time -= get_physics_process_delta_time();
  1652. if (message_time < 0)
  1653. surface->update();
  1654. }
  1655. //update shadow atlas if changed
  1656. int shadowmap_size = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/size");
  1657. int atlas_q0 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_0_subdiv");
  1658. int atlas_q1 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_1_subdiv");
  1659. int atlas_q2 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_2_subdiv");
  1660. int atlas_q3 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_3_subdiv");
  1661. viewport->set_shadow_atlas_size(shadowmap_size);
  1662. viewport->set_shadow_atlas_quadrant_subdiv(0, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q0));
  1663. viewport->set_shadow_atlas_quadrant_subdiv(1, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q1));
  1664. viewport->set_shadow_atlas_quadrant_subdiv(2, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q2));
  1665. viewport->set_shadow_atlas_quadrant_subdiv(3, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q3));
  1666. bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION));
  1667. if (shrink != (viewport_container->get_stretch_shrink() > 1)) {
  1668. viewport_container->set_stretch_shrink(shrink ? 2 : 1);
  1669. }
  1670. //update msaa if changed
  1671. int msaa_mode = ProjectSettings::get_singleton()->get("rendering/quality/filters/msaa");
  1672. viewport->set_msaa(Viewport::MSAA(msaa_mode));
  1673. bool hdr = ProjectSettings::get_singleton()->get("rendering/quality/depth/hdr");
  1674. viewport->set_hdr(hdr);
  1675. bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  1676. info_label->set_visible(show_info);
  1677. Camera *current_camera;
  1678. if (previewing) {
  1679. current_camera = previewing;
  1680. } else {
  1681. current_camera = camera;
  1682. }
  1683. if (show_info) {
  1684. String text;
  1685. text += "X: " + rtos(current_camera->get_translation().x).pad_decimals(1) + "\n";
  1686. text += "Y: " + rtos(current_camera->get_translation().y).pad_decimals(1) + "\n";
  1687. text += "Z: " + rtos(current_camera->get_translation().z).pad_decimals(1) + "\n";
  1688. text += TTR("Pitch") + ": " + itos(Math::round(current_camera->get_rotation_degrees().x)) + "\n";
  1689. text += TTR("Yaw") + ": " + itos(Math::round(current_camera->get_rotation_degrees().y)) + "\n\n";
  1690. text += TTR("Objects Drawn") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_OBJECTS_IN_FRAME)) + "\n";
  1691. text += TTR("Material Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_MATERIAL_CHANGES_IN_FRAME)) + "\n";
  1692. text += TTR("Shader Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SHADER_CHANGES_IN_FRAME)) + "\n";
  1693. text += TTR("Surface Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SURFACE_CHANGES_IN_FRAME)) + "\n";
  1694. text += TTR("Draw Calls") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME)) + "\n";
  1695. text += TTR("Vertices") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_VERTICES_IN_FRAME));
  1696. info_label->set_text(text);
  1697. }
  1698. // FPS Counter.
  1699. bool show_fps = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  1700. fps_label->set_visible(show_fps);
  1701. if (show_fps) {
  1702. String text;
  1703. const float temp_fps = Engine::get_singleton()->get_frames_per_second();
  1704. text += TTR(vformat("FPS: %d (%s ms)", temp_fps, String::num(1000.0f / temp_fps, 2)));
  1705. fps_label->set_text(text);
  1706. }
  1707. bool show_cinema = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  1708. cinema_label->set_visible(show_cinema);
  1709. if (show_cinema) {
  1710. float cinema_half_width = cinema_label->get_size().width / 2.0f;
  1711. cinema_label->set_anchor_and_margin(MARGIN_LEFT, 0.5f, -cinema_half_width);
  1712. }
  1713. if (lock_rotation) {
  1714. float locked_half_width = locked_label->get_size().width / 2.0f;
  1715. locked_label->set_anchor_and_margin(MARGIN_LEFT, 0.5f, -locked_half_width);
  1716. }
  1717. }
  1718. if (p_what == NOTIFICATION_ENTER_TREE) {
  1719. surface->connect("draw", this, "_draw");
  1720. surface->connect("gui_input", this, "_sinput");
  1721. surface->connect("mouse_entered", this, "_surface_mouse_enter");
  1722. surface->connect("mouse_exited", this, "_surface_mouse_exit");
  1723. surface->connect("focus_entered", this, "_surface_focus_enter");
  1724. surface->connect("focus_exited", this, "_surface_focus_exit");
  1725. _init_gizmo_instance(index);
  1726. }
  1727. if (p_what == NOTIFICATION_EXIT_TREE) {
  1728. _finish_gizmo_instances();
  1729. }
  1730. if (p_what == NOTIFICATION_THEME_CHANGED) {
  1731. view_menu->set_icon(get_icon("GuiMiniTabMenu", "EditorIcons"));
  1732. preview_camera->set_icon(get_icon("Camera", "EditorIcons"));
  1733. view_menu->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1734. view_menu->add_style_override("hover", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1735. view_menu->add_style_override("pressed", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1736. view_menu->add_style_override("focus", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1737. view_menu->add_style_override("disabled", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1738. preview_camera->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1739. preview_camera->add_style_override("hover", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1740. preview_camera->add_style_override("pressed", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1741. preview_camera->add_style_override("focus", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1742. preview_camera->add_style_override("disabled", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1743. info_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1744. fps_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1745. cinema_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1746. locked_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1747. }
  1748. }
  1749. // TODO That should be part of the drawing API...
  1750. static void stroke_rect(CanvasItem &ci, Rect2 rect, Color color, real_t width = 1.0) {
  1751. // a---b
  1752. // | |
  1753. // c---d
  1754. Vector2 a(rect.position);
  1755. Vector2 b(rect.position.x + rect.size.x, rect.position.y);
  1756. Vector2 c(rect.position.x, rect.position.y + rect.size.y);
  1757. Vector2 d(rect.position + rect.size);
  1758. ci.draw_line(a, b, color, width);
  1759. ci.draw_line(b, d, color, width);
  1760. ci.draw_line(d, c, color, width);
  1761. ci.draw_line(c, a, color, width);
  1762. }
  1763. static void draw_indicator_bar(Control &surface, real_t fill, Ref<Texture> icon) {
  1764. // Adjust bar size from control height
  1765. Vector2 surface_size = surface.get_size();
  1766. real_t h = surface_size.y / 2.0;
  1767. real_t y = (surface_size.y - h) / 2.0;
  1768. Rect2 r(10, y, 6, h);
  1769. real_t sy = r.size.y * fill;
  1770. // Note: because this bar appears over the viewport, it has to stay readable for any background color
  1771. // Draw both neutral dark and bright colors to account this
  1772. surface.draw_rect(r, Color(1, 1, 1, 0.2));
  1773. surface.draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), Color(1, 1, 1, 0.6));
  1774. stroke_rect(surface, r.grow(1), Color(0, 0, 0, 0.7));
  1775. Vector2 icon_size = icon->get_size();
  1776. Vector2 icon_pos = Vector2(r.position.x - (icon_size.x - r.size.x) / 2, r.position.y + r.size.y + 2);
  1777. surface.draw_texture(icon, icon_pos);
  1778. }
  1779. void SpatialEditorViewport::_draw() {
  1780. EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
  1781. if (!over_plugin_list->empty()) {
  1782. over_plugin_list->forward_spatial_draw_over_viewport(surface);
  1783. }
  1784. EditorPluginList *force_over_plugin_list = editor->get_editor_plugins_force_over();
  1785. if (!force_over_plugin_list->empty()) {
  1786. force_over_plugin_list->forward_spatial_force_draw_over_viewport(surface);
  1787. }
  1788. if (surface->has_focus()) {
  1789. Size2 size = surface->get_size();
  1790. Rect2 r = Rect2(Point2(), size);
  1791. get_stylebox("Focus", "EditorStyles")->draw(surface->get_canvas_item(), r);
  1792. }
  1793. RID ci = surface->get_canvas_item();
  1794. if (cursor.region_select) {
  1795. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor.region_begin, cursor.region_end - cursor.region_begin), Color(0.7, 0.7, 1.0, 0.3));
  1796. }
  1797. if (message_time > 0) {
  1798. Ref<Font> font = get_font("font", "Label");
  1799. Point2 msgpos = Point2(5, get_size().y - 20);
  1800. font->draw(ci, msgpos + Point2(1, 1), message, Color(0, 0, 0, 0.8));
  1801. font->draw(ci, msgpos + Point2(-1, -1), message, Color(0, 0, 0, 0.8));
  1802. font->draw(ci, msgpos, message, Color(1, 1, 1, 1));
  1803. }
  1804. if (_edit.mode == TRANSFORM_ROTATE) {
  1805. Point2 center = _point_to_screen(_edit.center);
  1806. VisualServer::get_singleton()->canvas_item_add_line(ci, _edit.mouse_pos, center, Color(0.4, 0.7, 1.0, 0.8));
  1807. }
  1808. if (previewing) {
  1809. Size2 ss = Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height"));
  1810. float aspect = ss.aspect();
  1811. Size2 s = get_size();
  1812. Rect2 draw_rect;
  1813. switch (previewing->get_keep_aspect_mode()) {
  1814. case Camera::KEEP_WIDTH: {
  1815. draw_rect.size = Size2(s.width, s.width / aspect);
  1816. draw_rect.position.x = 0;
  1817. draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5;
  1818. } break;
  1819. case Camera::KEEP_HEIGHT: {
  1820. draw_rect.size = Size2(s.height * aspect, s.height);
  1821. draw_rect.position.y = 0;
  1822. draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5;
  1823. } break;
  1824. }
  1825. draw_rect = Rect2(Vector2(), s).clip(draw_rect);
  1826. stroke_rect(*surface, draw_rect, Color(0.6, 0.6, 0.1, 0.5), 2.0);
  1827. } else {
  1828. if (zoom_indicator_delay > 0.0) {
  1829. if (is_freelook_active()) {
  1830. // Show speed
  1831. real_t min_speed = FREELOOK_MIN_SPEED;
  1832. real_t max_speed = camera->get_zfar();
  1833. real_t scale_length = (max_speed - min_speed);
  1834. if (Math::abs(scale_length) > CMP_EPSILON) {
  1835. real_t logscale_t = 1.0 - Math::log(1 + freelook_speed - min_speed) / Math::log(1 + scale_length);
  1836. // There is no real maximum speed so that factor can become negative,
  1837. // Let's make it look asymptotic instead (will decrease slower and slower).
  1838. if (logscale_t < 0.25)
  1839. logscale_t = 0.25 * Math::exp(4.0 * logscale_t - 1.0);
  1840. draw_indicator_bar(*surface, 1.0 - logscale_t, get_icon("ViewportSpeed", "EditorIcons"));
  1841. }
  1842. } else {
  1843. // Show zoom
  1844. real_t min_distance = ZOOM_MIN_DISTANCE; // TODO Why not pick znear to limit zoom?
  1845. real_t max_distance = camera->get_zfar();
  1846. real_t scale_length = (max_distance - min_distance);
  1847. if (Math::abs(scale_length) > CMP_EPSILON) {
  1848. real_t logscale_t = 1.0 - Math::log(1 + cursor.distance - min_distance) / Math::log(1 + scale_length);
  1849. // There is no real maximum distance so that factor can become negative,
  1850. // Let's make it look asymptotic instead (will decrease slower and slower).
  1851. if (logscale_t < 0.25)
  1852. logscale_t = 0.25 * Math::exp(4.0 * logscale_t - 1.0);
  1853. draw_indicator_bar(*surface, logscale_t, get_icon("ViewportZoom", "EditorIcons"));
  1854. }
  1855. }
  1856. }
  1857. }
  1858. }
  1859. void SpatialEditorViewport::_menu_option(int p_option) {
  1860. switch (p_option) {
  1861. case VIEW_TOP: {
  1862. cursor.y_rot = 0;
  1863. cursor.x_rot = Math_PI / 2.0;
  1864. set_message(TTR("Top View."), 2);
  1865. name = TTR("Top");
  1866. _update_name();
  1867. } break;
  1868. case VIEW_BOTTOM: {
  1869. cursor.y_rot = 0;
  1870. cursor.x_rot = -Math_PI / 2.0;
  1871. set_message(TTR("Bottom View."), 2);
  1872. name = TTR("Bottom");
  1873. _update_name();
  1874. } break;
  1875. case VIEW_LEFT: {
  1876. cursor.x_rot = 0;
  1877. cursor.y_rot = Math_PI / 2.0;
  1878. set_message(TTR("Left View."), 2);
  1879. name = TTR("Left");
  1880. _update_name();
  1881. } break;
  1882. case VIEW_RIGHT: {
  1883. cursor.x_rot = 0;
  1884. cursor.y_rot = -Math_PI / 2.0;
  1885. set_message(TTR("Right View."), 2);
  1886. name = TTR("Right");
  1887. _update_name();
  1888. } break;
  1889. case VIEW_FRONT: {
  1890. cursor.x_rot = 0;
  1891. cursor.y_rot = 0;
  1892. set_message(TTR("Front View."), 2);
  1893. name = TTR("Front");
  1894. _update_name();
  1895. } break;
  1896. case VIEW_REAR: {
  1897. cursor.x_rot = 0;
  1898. cursor.y_rot = Math_PI;
  1899. set_message(TTR("Rear View."), 2);
  1900. name = TTR("Rear");
  1901. _update_name();
  1902. } break;
  1903. case VIEW_CENTER_TO_ORIGIN: {
  1904. cursor.pos = Vector3(0, 0, 0);
  1905. } break;
  1906. case VIEW_CENTER_TO_SELECTION: {
  1907. focus_selection();
  1908. } break;
  1909. case VIEW_ALIGN_SELECTION_WITH_VIEW: {
  1910. if (!get_selected_count())
  1911. break;
  1912. Transform camera_transform = camera->get_global_transform();
  1913. List<Node *> &selection = editor_selection->get_selected_node_list();
  1914. undo_redo->create_action(TTR("Align with View"));
  1915. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1916. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1917. if (!sp)
  1918. continue;
  1919. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1920. if (!se)
  1921. continue;
  1922. Transform xform;
  1923. if (orthogonal) {
  1924. xform = sp->get_global_transform();
  1925. xform.basis.set_euler(camera_transform.basis.get_euler());
  1926. } else {
  1927. xform = camera_transform;
  1928. xform.scale_basis(sp->get_scale());
  1929. }
  1930. undo_redo->add_do_method(sp, "set_global_transform", xform);
  1931. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  1932. }
  1933. undo_redo->commit_action();
  1934. } break;
  1935. case VIEW_ENVIRONMENT: {
  1936. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  1937. bool current = view_menu->get_popup()->is_item_checked(idx);
  1938. current = !current;
  1939. if (current) {
  1940. camera->set_environment(RES());
  1941. } else {
  1942. camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1943. }
  1944. view_menu->get_popup()->set_item_checked(idx, current);
  1945. } break;
  1946. case VIEW_PERSPECTIVE: {
  1947. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  1948. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false);
  1949. orthogonal = false;
  1950. call_deferred("update_transform_gizmo_view");
  1951. _update_name();
  1952. } break;
  1953. case VIEW_ORTHOGONAL: {
  1954. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false);
  1955. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true);
  1956. orthogonal = true;
  1957. call_deferred("update_transform_gizmo_view");
  1958. _update_name();
  1959. } break;
  1960. case VIEW_LOCK_ROTATION: {
  1961. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  1962. bool current = view_menu->get_popup()->is_item_checked(idx);
  1963. lock_rotation = !current;
  1964. view_menu->get_popup()->set_item_checked(idx, !current);
  1965. if (lock_rotation) {
  1966. locked_label->show();
  1967. } else {
  1968. locked_label->hide();
  1969. }
  1970. } break;
  1971. case VIEW_AUDIO_LISTENER: {
  1972. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1973. bool current = view_menu->get_popup()->is_item_checked(idx);
  1974. current = !current;
  1975. viewport->set_as_audio_listener(current);
  1976. view_menu->get_popup()->set_item_checked(idx, current);
  1977. } break;
  1978. case VIEW_AUDIO_DOPPLER: {
  1979. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  1980. bool current = view_menu->get_popup()->is_item_checked(idx);
  1981. current = !current;
  1982. camera->set_doppler_tracking(current ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  1983. view_menu->get_popup()->set_item_checked(idx, current);
  1984. } break;
  1985. case VIEW_CINEMATIC_PREVIEW: {
  1986. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  1987. bool current = view_menu->get_popup()->is_item_checked(idx);
  1988. current = !current;
  1989. view_menu->get_popup()->set_item_checked(idx, current);
  1990. previewing_cinema = true;
  1991. _toggle_cinema_preview(current);
  1992. if (current) {
  1993. preview_camera->hide();
  1994. } else {
  1995. if (previewing != NULL)
  1996. preview_camera->show();
  1997. }
  1998. } break;
  1999. case VIEW_GIZMOS: {
  2000. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2001. bool current = view_menu->get_popup()->is_item_checked(idx);
  2002. current = !current;
  2003. if (current)
  2004. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER));
  2005. else
  2006. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_GRID_LAYER));
  2007. view_menu->get_popup()->set_item_checked(idx, current);
  2008. } break;
  2009. case VIEW_HALF_RESOLUTION: {
  2010. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2011. bool current = view_menu->get_popup()->is_item_checked(idx);
  2012. current = !current;
  2013. view_menu->get_popup()->set_item_checked(idx, current);
  2014. } break;
  2015. case VIEW_INFORMATION: {
  2016. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2017. bool current = view_menu->get_popup()->is_item_checked(idx);
  2018. view_menu->get_popup()->set_item_checked(idx, !current);
  2019. } break;
  2020. case VIEW_FPS: {
  2021. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  2022. bool current = view_menu->get_popup()->is_item_checked(idx);
  2023. view_menu->get_popup()->set_item_checked(idx, !current);
  2024. } break;
  2025. case VIEW_DISPLAY_NORMAL: {
  2026. viewport->set_debug_draw(Viewport::DEBUG_DRAW_DISABLED);
  2027. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  2028. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2029. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2030. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2031. } break;
  2032. case VIEW_DISPLAY_WIREFRAME: {
  2033. viewport->set_debug_draw(Viewport::DEBUG_DRAW_WIREFRAME);
  2034. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2035. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), true);
  2036. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2037. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2038. } break;
  2039. case VIEW_DISPLAY_OVERDRAW: {
  2040. viewport->set_debug_draw(Viewport::DEBUG_DRAW_OVERDRAW);
  2041. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW);
  2042. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2043. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2044. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), true);
  2045. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2046. } break;
  2047. case VIEW_DISPLAY_SHADELESS: {
  2048. viewport->set_debug_draw(Viewport::DEBUG_DRAW_UNSHADED);
  2049. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS);
  2050. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2051. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2052. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2053. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), true);
  2054. } break;
  2055. }
  2056. }
  2057. void SpatialEditorViewport::_preview_exited_scene() {
  2058. preview_camera->set_pressed(false);
  2059. _toggle_camera_preview(false);
  2060. view_menu->show();
  2061. }
  2062. void SpatialEditorViewport::_init_gizmo_instance(int p_idx) {
  2063. uint32_t layer = 1 << (GIZMO_BASE_LAYER + p_idx);
  2064. for (int i = 0; i < 3; i++) {
  2065. move_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2066. VS::get_singleton()->instance_set_base(move_gizmo_instance[i], spatial_editor->get_move_gizmo(i)->get_rid());
  2067. VS::get_singleton()->instance_set_scenario(move_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2068. VS::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2069. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2070. VS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i], layer);
  2071. move_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2072. VS::get_singleton()->instance_set_base(move_plane_gizmo_instance[i], spatial_editor->get_move_plane_gizmo(i)->get_rid());
  2073. VS::get_singleton()->instance_set_scenario(move_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2074. VS::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2075. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2076. VS::get_singleton()->instance_set_layer_mask(move_plane_gizmo_instance[i], layer);
  2077. rotate_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2078. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[i], spatial_editor->get_rotate_gizmo(i)->get_rid());
  2079. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2080. VS::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2081. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2082. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i], layer);
  2083. scale_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2084. VS::get_singleton()->instance_set_base(scale_gizmo_instance[i], spatial_editor->get_scale_gizmo(i)->get_rid());
  2085. VS::get_singleton()->instance_set_scenario(scale_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2086. VS::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2087. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2088. VS::get_singleton()->instance_set_layer_mask(scale_gizmo_instance[i], layer);
  2089. scale_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2090. VS::get_singleton()->instance_set_base(scale_plane_gizmo_instance[i], spatial_editor->get_scale_plane_gizmo(i)->get_rid());
  2091. VS::get_singleton()->instance_set_scenario(scale_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2092. VS::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2093. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2094. VS::get_singleton()->instance_set_layer_mask(scale_plane_gizmo_instance[i], layer);
  2095. }
  2096. }
  2097. void SpatialEditorViewport::_finish_gizmo_instances() {
  2098. for (int i = 0; i < 3; i++) {
  2099. VS::get_singleton()->free(move_gizmo_instance[i]);
  2100. VS::get_singleton()->free(move_plane_gizmo_instance[i]);
  2101. VS::get_singleton()->free(rotate_gizmo_instance[i]);
  2102. VS::get_singleton()->free(scale_gizmo_instance[i]);
  2103. VS::get_singleton()->free(scale_plane_gizmo_instance[i]);
  2104. }
  2105. }
  2106. void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
  2107. ERR_FAIL_COND(p_activate && !preview);
  2108. ERR_FAIL_COND(!p_activate && !previewing);
  2109. if (!p_activate) {
  2110. previewing->disconnect("tree_exiting", this, "_preview_exited_scene");
  2111. previewing = NULL;
  2112. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2113. if (!preview)
  2114. preview_camera->hide();
  2115. view_menu->set_disabled(false);
  2116. surface->update();
  2117. } else {
  2118. previewing = preview;
  2119. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2120. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), preview->get_camera()); //replace
  2121. view_menu->set_disabled(true);
  2122. surface->update();
  2123. }
  2124. }
  2125. void SpatialEditorViewport::_toggle_cinema_preview(bool p_activate) {
  2126. previewing_cinema = p_activate;
  2127. if (!previewing_cinema) {
  2128. if (previewing != NULL)
  2129. previewing->disconnect("tree_exited", this, "_preview_exited_scene");
  2130. previewing = NULL;
  2131. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2132. preview_camera->set_pressed(false);
  2133. if (!preview) {
  2134. preview_camera->hide();
  2135. } else {
  2136. preview_camera->show();
  2137. }
  2138. view_menu->show();
  2139. surface->update();
  2140. }
  2141. }
  2142. void SpatialEditorViewport::_selection_result_pressed(int p_result) {
  2143. if (selection_results.size() <= p_result)
  2144. return;
  2145. clicked = selection_results[p_result].item->get_instance_id();
  2146. if (clicked) {
  2147. _select_clicked(clicked_wants_append, true);
  2148. clicked = 0;
  2149. }
  2150. }
  2151. void SpatialEditorViewport::_selection_menu_hide() {
  2152. selection_results.clear();
  2153. selection_menu->clear();
  2154. selection_menu->set_size(Vector2(0, 0));
  2155. }
  2156. void SpatialEditorViewport::set_can_preview(Camera *p_preview) {
  2157. preview = p_preview;
  2158. if (!preview_camera->is_pressed() && !previewing_cinema)
  2159. preview_camera->set_visible(p_preview);
  2160. }
  2161. void SpatialEditorViewport::update_transform_gizmo_view() {
  2162. if (!is_visible_in_tree())
  2163. return;
  2164. Transform xform = spatial_editor->get_gizmo_transform();
  2165. Transform camera_xform = camera->get_transform();
  2166. if (xform.origin.distance_squared_to(camera_xform.origin) < 0.01) {
  2167. for (int i = 0; i < 3; i++) {
  2168. VisualServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2169. VisualServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2170. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2171. VisualServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2172. VisualServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2173. }
  2174. return;
  2175. }
  2176. Vector3 camz = -camera_xform.get_basis().get_axis(2).normalized();
  2177. Vector3 camy = -camera_xform.get_basis().get_axis(1).normalized();
  2178. Plane p(camera_xform.origin, camz);
  2179. float gizmo_d = Math::abs(p.distance_to(xform.origin));
  2180. float d0 = camera->unproject_position(camera_xform.origin + camz * gizmo_d).y;
  2181. float d1 = camera->unproject_position(camera_xform.origin + camz * gizmo_d + camy).y;
  2182. float dd = Math::abs(d0 - d1);
  2183. if (dd == 0)
  2184. dd = 0.0001;
  2185. float gsize = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_size");
  2186. gizmo_scale = (gsize / Math::abs(dd)) * MAX(1, EDSCALE) / viewport_container->get_stretch_shrink();
  2187. Vector3 scale = Vector3(1, 1, 1) * gizmo_scale;
  2188. xform.basis.scale(scale);
  2189. for (int i = 0; i < 3; i++) {
  2190. VisualServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], xform);
  2191. VisualServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE));
  2192. VisualServer::get_singleton()->instance_set_transform(move_plane_gizmo_instance[i], xform);
  2193. VisualServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE));
  2194. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], xform);
  2195. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE));
  2196. VisualServer::get_singleton()->instance_set_transform(scale_gizmo_instance[i], xform);
  2197. VisualServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE));
  2198. VisualServer::get_singleton()->instance_set_transform(scale_plane_gizmo_instance[i], xform);
  2199. VisualServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE));
  2200. }
  2201. }
  2202. void SpatialEditorViewport::set_state(const Dictionary &p_state) {
  2203. if (p_state.has("position"))
  2204. cursor.pos = p_state["position"];
  2205. if (p_state.has("x_rotation"))
  2206. cursor.x_rot = p_state["x_rotation"];
  2207. if (p_state.has("y_rotation"))
  2208. cursor.y_rot = p_state["y_rotation"];
  2209. if (p_state.has("distance"))
  2210. cursor.distance = p_state["distance"];
  2211. if (p_state.has("use_orthogonal")) {
  2212. bool orth = p_state["use_orthogonal"];
  2213. if (orth)
  2214. _menu_option(VIEW_ORTHOGONAL);
  2215. else
  2216. _menu_option(VIEW_PERSPECTIVE);
  2217. }
  2218. if (p_state.has("display_mode")) {
  2219. int display = p_state["display_mode"];
  2220. int idx = view_menu->get_popup()->get_item_index(display);
  2221. if (!view_menu->get_popup()->is_item_checked(idx))
  2222. _menu_option(display);
  2223. }
  2224. if (p_state.has("lock_rotation")) {
  2225. lock_rotation = p_state["lock_rotation"];
  2226. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2227. view_menu->get_popup()->set_item_checked(idx, lock_rotation);
  2228. }
  2229. if (p_state.has("use_environment")) {
  2230. bool env = p_state["use_environment"];
  2231. if (env != camera->get_environment().is_valid())
  2232. _menu_option(VIEW_ENVIRONMENT);
  2233. }
  2234. if (p_state.has("listener")) {
  2235. bool listener = p_state["listener"];
  2236. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2237. viewport->set_as_audio_listener(listener);
  2238. view_menu->get_popup()->set_item_checked(idx, listener);
  2239. }
  2240. if (p_state.has("doppler")) {
  2241. bool doppler = p_state["doppler"];
  2242. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2243. camera->set_doppler_tracking(doppler ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  2244. view_menu->get_popup()->set_item_checked(idx, doppler);
  2245. }
  2246. if (p_state.has("gizmos")) {
  2247. bool gizmos = p_state["gizmos"];
  2248. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2249. if (view_menu->get_popup()->is_item_checked(idx) != gizmos)
  2250. _menu_option(VIEW_GIZMOS);
  2251. }
  2252. if (p_state.has("information")) {
  2253. bool information = p_state["information"];
  2254. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2255. if (view_menu->get_popup()->is_item_checked(idx) != information)
  2256. _menu_option(VIEW_INFORMATION);
  2257. }
  2258. if (p_state.has("fps")) {
  2259. bool fps = p_state["fps"];
  2260. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  2261. if (view_menu->get_popup()->is_item_checked(idx) != fps)
  2262. _menu_option(VIEW_FPS);
  2263. }
  2264. if (p_state.has("half_res")) {
  2265. bool half_res = p_state["half_res"];
  2266. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2267. view_menu->get_popup()->set_item_checked(idx, half_res);
  2268. }
  2269. if (p_state.has("cinematic_preview")) {
  2270. previewing_cinema = p_state["cinematic_preview"];
  2271. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  2272. view_menu->get_popup()->set_item_checked(idx, previewing_cinema);
  2273. }
  2274. if (p_state.has("previewing")) {
  2275. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  2276. if (Object::cast_to<Camera>(pv)) {
  2277. previewing = Object::cast_to<Camera>(pv);
  2278. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2279. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), previewing->get_camera()); //replace
  2280. view_menu->hide();
  2281. surface->update();
  2282. preview_camera->set_pressed(true);
  2283. preview_camera->show();
  2284. }
  2285. }
  2286. }
  2287. Dictionary SpatialEditorViewport::get_state() const {
  2288. Dictionary d;
  2289. d["position"] = cursor.pos;
  2290. d["x_rotation"] = cursor.x_rot;
  2291. d["y_rotation"] = cursor.y_rot;
  2292. d["distance"] = cursor.distance;
  2293. d["use_environment"] = camera->get_environment().is_valid();
  2294. d["use_orthogonal"] = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  2295. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL)))
  2296. d["display_mode"] = VIEW_DISPLAY_NORMAL;
  2297. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME)))
  2298. d["display_mode"] = VIEW_DISPLAY_WIREFRAME;
  2299. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW)))
  2300. d["display_mode"] = VIEW_DISPLAY_OVERDRAW;
  2301. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS)))
  2302. d["display_mode"] = VIEW_DISPLAY_SHADELESS;
  2303. d["listener"] = viewport->is_audio_listener();
  2304. d["doppler"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER));
  2305. d["gizmos"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS));
  2306. d["information"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  2307. d["fps"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  2308. d["half_res"] = viewport_container->get_stretch_shrink() > 1;
  2309. d["cinematic_preview"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  2310. if (previewing)
  2311. d["previewing"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  2312. if (lock_rotation)
  2313. d["lock_rotation"] = lock_rotation;
  2314. return d;
  2315. }
  2316. void SpatialEditorViewport::_bind_methods() {
  2317. ClassDB::bind_method(D_METHOD("_draw"), &SpatialEditorViewport::_draw);
  2318. ClassDB::bind_method(D_METHOD("_surface_mouse_enter"), &SpatialEditorViewport::_surface_mouse_enter);
  2319. ClassDB::bind_method(D_METHOD("_surface_mouse_exit"), &SpatialEditorViewport::_surface_mouse_exit);
  2320. ClassDB::bind_method(D_METHOD("_surface_focus_enter"), &SpatialEditorViewport::_surface_focus_enter);
  2321. ClassDB::bind_method(D_METHOD("_surface_focus_exit"), &SpatialEditorViewport::_surface_focus_exit);
  2322. ClassDB::bind_method(D_METHOD("_sinput"), &SpatialEditorViewport::_sinput);
  2323. ClassDB::bind_method(D_METHOD("_menu_option"), &SpatialEditorViewport::_menu_option);
  2324. ClassDB::bind_method(D_METHOD("_toggle_camera_preview"), &SpatialEditorViewport::_toggle_camera_preview);
  2325. ClassDB::bind_method(D_METHOD("_preview_exited_scene"), &SpatialEditorViewport::_preview_exited_scene);
  2326. ClassDB::bind_method(D_METHOD("update_transform_gizmo_view"), &SpatialEditorViewport::update_transform_gizmo_view);
  2327. ClassDB::bind_method(D_METHOD("_selection_result_pressed"), &SpatialEditorViewport::_selection_result_pressed);
  2328. ClassDB::bind_method(D_METHOD("_selection_menu_hide"), &SpatialEditorViewport::_selection_menu_hide);
  2329. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpatialEditorViewport::can_drop_data_fw);
  2330. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SpatialEditorViewport::drop_data_fw);
  2331. ADD_SIGNAL(MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")));
  2332. }
  2333. void SpatialEditorViewport::reset() {
  2334. orthogonal = false;
  2335. lock_rotation = false;
  2336. message_time = 0;
  2337. message = "";
  2338. last_message = "";
  2339. name = "";
  2340. cursor.x_rot = 0.5;
  2341. cursor.y_rot = 0.5;
  2342. cursor.distance = 4;
  2343. cursor.region_select = false;
  2344. cursor.pos = Vector3();
  2345. _update_name();
  2346. }
  2347. void SpatialEditorViewport::focus_selection() {
  2348. if (!get_selected_count())
  2349. return;
  2350. Vector3 center;
  2351. int count = 0;
  2352. List<Node *> &selection = editor_selection->get_selected_node_list();
  2353. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2354. Spatial *sp = Object::cast_to<Spatial>(E->get());
  2355. if (!sp)
  2356. continue;
  2357. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2358. if (!se)
  2359. continue;
  2360. center += sp->get_global_gizmo_transform().origin;
  2361. count++;
  2362. }
  2363. if (count != 0) {
  2364. center /= float(count);
  2365. }
  2366. cursor.pos = center;
  2367. }
  2368. void SpatialEditorViewport::assign_pending_data_pointers(Spatial *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) {
  2369. preview_node = p_preview_node;
  2370. preview_bounds = p_preview_bounds;
  2371. accept = p_accept;
  2372. }
  2373. Vector3 SpatialEditorViewport::_get_instance_position(const Point2 &p_pos) const {
  2374. const float MAX_DISTANCE = 10;
  2375. Vector3 world_ray = _get_ray(p_pos);
  2376. Vector3 world_pos = _get_ray_pos(p_pos);
  2377. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(world_pos, world_ray, get_tree()->get_root()->get_world()->get_scenario());
  2378. Set<Ref<EditorSpatialGizmo> > found_gizmos;
  2379. float closest_dist = MAX_DISTANCE;
  2380. Vector3 point = world_pos + world_ray * MAX_DISTANCE;
  2381. Vector3 normal = Vector3(0.0, 0.0, 0.0);
  2382. for (int i = 0; i < instances.size(); i++) {
  2383. MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(ObjectDB::get_instance(instances[i]));
  2384. if (!mesh_instance)
  2385. continue;
  2386. Ref<EditorSpatialGizmo> seg = mesh_instance->get_gizmo();
  2387. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  2388. continue;
  2389. }
  2390. found_gizmos.insert(seg);
  2391. Vector3 hit_point;
  2392. Vector3 hit_normal;
  2393. bool inters = seg->intersect_ray(camera, p_pos, hit_point, hit_normal, NULL, false);
  2394. if (!inters)
  2395. continue;
  2396. float dist = world_pos.distance_to(hit_point);
  2397. if (dist < 0)
  2398. continue;
  2399. if (dist < closest_dist) {
  2400. closest_dist = dist;
  2401. point = hit_point;
  2402. normal = hit_normal;
  2403. }
  2404. }
  2405. Vector3 offset = Vector3();
  2406. for (int i = 0; i < 3; i++) {
  2407. if (normal[i] > 0.0)
  2408. offset[i] = (preview_bounds->get_size()[i] - (preview_bounds->get_size()[i] + preview_bounds->get_position()[i]));
  2409. else if (normal[i] < 0.0)
  2410. offset[i] = -(preview_bounds->get_size()[i] + preview_bounds->get_position()[i]);
  2411. }
  2412. return point + offset;
  2413. }
  2414. AABB SpatialEditorViewport::_calculate_spatial_bounds(const Spatial *p_parent, const AABB p_bounds) {
  2415. AABB bounds = p_bounds;
  2416. for (int i = 0; i < p_parent->get_child_count(); i++) {
  2417. Spatial *child = Object::cast_to<Spatial>(p_parent->get_child(i));
  2418. if (child) {
  2419. MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(child);
  2420. if (mesh_instance) {
  2421. AABB mesh_instance_bounds = mesh_instance->get_aabb();
  2422. mesh_instance_bounds.position += mesh_instance->get_global_gizmo_transform().origin - p_parent->get_global_gizmo_transform().origin;
  2423. bounds.merge_with(mesh_instance_bounds);
  2424. }
  2425. bounds = _calculate_spatial_bounds(child, bounds);
  2426. }
  2427. }
  2428. return bounds;
  2429. }
  2430. void SpatialEditorViewport::_create_preview(const Vector<String> &files) const {
  2431. for (int i = 0; i < files.size(); i++) {
  2432. String path = files[i];
  2433. RES res = ResourceLoader::load(path);
  2434. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2435. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2436. if (mesh != NULL || scene != NULL) {
  2437. if (mesh != NULL) {
  2438. MeshInstance *mesh_instance = memnew(MeshInstance);
  2439. mesh_instance->set_mesh(mesh);
  2440. preview_node->add_child(mesh_instance);
  2441. } else {
  2442. if (scene.is_valid()) {
  2443. Node *instance = scene->instance();
  2444. if (instance) {
  2445. preview_node->add_child(instance);
  2446. }
  2447. }
  2448. }
  2449. editor->get_scene_root()->add_child(preview_node);
  2450. }
  2451. }
  2452. *preview_bounds = _calculate_spatial_bounds(preview_node, AABB());
  2453. }
  2454. void SpatialEditorViewport::_remove_preview() {
  2455. if (preview_node->get_parent()) {
  2456. for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
  2457. Node *node = preview_node->get_child(i);
  2458. node->queue_delete();
  2459. preview_node->remove_child(node);
  2460. }
  2461. editor->get_scene_root()->remove_child(preview_node);
  2462. }
  2463. }
  2464. bool SpatialEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  2465. if (p_desired_node->get_filename() == p_target_scene_path) {
  2466. return true;
  2467. }
  2468. int childCount = p_desired_node->get_child_count();
  2469. for (int i = 0; i < childCount; i++) {
  2470. Node *child = p_desired_node->get_child(i);
  2471. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  2472. return true;
  2473. }
  2474. }
  2475. return false;
  2476. }
  2477. bool SpatialEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) {
  2478. RES res = ResourceLoader::load(path);
  2479. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2480. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2481. Node *instanced_scene = NULL;
  2482. if (mesh != NULL || scene != NULL) {
  2483. if (mesh != NULL) {
  2484. MeshInstance *mesh_instance = memnew(MeshInstance);
  2485. mesh_instance->set_mesh(mesh);
  2486. mesh_instance->set_name(path.get_file().get_basename());
  2487. instanced_scene = mesh_instance;
  2488. } else {
  2489. if (!scene.is_valid()) { // invalid scene
  2490. return false;
  2491. } else {
  2492. instanced_scene = scene->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  2493. }
  2494. }
  2495. }
  2496. if (instanced_scene == NULL) {
  2497. return false;
  2498. }
  2499. if (editor->get_edited_scene()->get_filename() != "") { // cyclical instancing
  2500. if (_cyclical_dependency_exists(editor->get_edited_scene()->get_filename(), instanced_scene)) {
  2501. memdelete(instanced_scene);
  2502. return false;
  2503. }
  2504. }
  2505. if (scene != NULL) {
  2506. instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(path));
  2507. }
  2508. editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
  2509. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", editor->get_edited_scene());
  2510. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  2511. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
  2512. String new_name = parent->validate_child_name(instanced_scene);
  2513. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  2514. editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", editor->get_edited_scene()->get_path_to(parent), path, new_name);
  2515. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(editor->get_edited_scene()->get_path_to(parent)) + "/" + new_name));
  2516. Transform global_transform;
  2517. Spatial *parent_spatial = Object::cast_to<Spatial>(parent);
  2518. if (parent_spatial)
  2519. global_transform = parent_spatial->get_global_gizmo_transform();
  2520. global_transform.origin = spatial_editor->snap_point(_get_instance_position(p_point));
  2521. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_global_transform", global_transform);
  2522. return true;
  2523. }
  2524. void SpatialEditorViewport::_perform_drop_data() {
  2525. _remove_preview();
  2526. Vector<String> error_files;
  2527. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  2528. for (int i = 0; i < selected_files.size(); i++) {
  2529. String path = selected_files[i];
  2530. RES res = ResourceLoader::load(path);
  2531. if (res.is_null()) {
  2532. continue;
  2533. }
  2534. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2535. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2536. if (mesh != NULL || scene != NULL) {
  2537. bool success = _create_instance(target_node, path, drop_pos);
  2538. if (!success) {
  2539. error_files.push_back(path);
  2540. }
  2541. }
  2542. }
  2543. editor_data->get_undo_redo().commit_action();
  2544. if (error_files.size() > 0) {
  2545. String files_str;
  2546. for (int i = 0; i < error_files.size(); i++) {
  2547. files_str += error_files[i].get_file().get_basename() + ",";
  2548. }
  2549. files_str = files_str.substr(0, files_str.length() - 1);
  2550. accept->set_text(vformat(TTR("Error instancing scene from %s"), files_str.c_str()));
  2551. accept->popup_centered_minsize();
  2552. }
  2553. }
  2554. bool SpatialEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2555. bool can_instance = false;
  2556. if (!preview_node->is_inside_tree()) {
  2557. Dictionary d = p_data;
  2558. if (d.has("type") && (String(d["type"]) == "files")) {
  2559. Vector<String> files = d["files"];
  2560. List<String> scene_extensions;
  2561. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  2562. List<String> mesh_extensions;
  2563. ResourceLoader::get_recognized_extensions_for_type("Mesh", &mesh_extensions);
  2564. for (int i = 0; i < files.size(); i++) {
  2565. if (mesh_extensions.find(files[i].get_extension()) || scene_extensions.find(files[i].get_extension())) {
  2566. RES res = ResourceLoader::load(files[i]);
  2567. if (res.is_null()) {
  2568. continue;
  2569. }
  2570. String type = res->get_class();
  2571. if (type == "PackedScene") {
  2572. Ref<PackedScene> sdata = ResourceLoader::load(files[i]);
  2573. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  2574. if (!instanced_scene) {
  2575. continue;
  2576. }
  2577. memdelete(instanced_scene);
  2578. } else if (type == "Mesh" || type == "ArrayMesh" || type == "PrimitiveMesh") {
  2579. Ref<Mesh> mesh = ResourceLoader::load(files[i]);
  2580. if (!mesh.is_valid()) {
  2581. continue;
  2582. }
  2583. } else {
  2584. continue;
  2585. }
  2586. can_instance = true;
  2587. break;
  2588. }
  2589. }
  2590. if (can_instance) {
  2591. _create_preview(files);
  2592. }
  2593. }
  2594. } else {
  2595. can_instance = true;
  2596. }
  2597. if (can_instance) {
  2598. Transform global_transform = Transform(Basis(), _get_instance_position(p_point));
  2599. preview_node->set_global_transform(global_transform);
  2600. }
  2601. return can_instance;
  2602. }
  2603. void SpatialEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2604. if (!can_drop_data_fw(p_point, p_data, p_from))
  2605. return;
  2606. bool is_shift = Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  2607. selected_files.clear();
  2608. Dictionary d = p_data;
  2609. if (d.has("type") && String(d["type"]) == "files") {
  2610. selected_files = d["files"];
  2611. }
  2612. List<Node *> list = editor->get_editor_selection()->get_selected_node_list();
  2613. if (list.size() == 0) {
  2614. Node *root_node = editor->get_edited_scene();
  2615. if (root_node) {
  2616. list.push_back(root_node);
  2617. } else {
  2618. accept->set_text(TTR("No parent to instance a child at."));
  2619. accept->popup_centered_minsize();
  2620. _remove_preview();
  2621. return;
  2622. }
  2623. }
  2624. if (list.size() != 1) {
  2625. accept->set_text(TTR("This operation requires a single selected node."));
  2626. accept->popup_centered_minsize();
  2627. _remove_preview();
  2628. return;
  2629. }
  2630. target_node = list[0];
  2631. if (is_shift && target_node != editor->get_edited_scene()) {
  2632. target_node = target_node->get_parent();
  2633. }
  2634. drop_pos = p_point;
  2635. _perform_drop_data();
  2636. }
  2637. SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index) {
  2638. _edit.mode = TRANSFORM_NONE;
  2639. _edit.plane = TRANSFORM_VIEW;
  2640. _edit.edited_gizmo = 0;
  2641. _edit.snap = 1;
  2642. _edit.gizmo_handle = 0;
  2643. index = p_index;
  2644. editor = p_editor;
  2645. editor_data = editor->get_scene_tree_dock()->get_editor_data();
  2646. editor_selection = editor->get_editor_selection();
  2647. undo_redo = editor->get_undo_redo();
  2648. clicked = 0;
  2649. clicked_includes_current = false;
  2650. orthogonal = false;
  2651. lock_rotation = false;
  2652. message_time = 0;
  2653. zoom_indicator_delay = 0.0;
  2654. spatial_editor = p_spatial_editor;
  2655. ViewportContainer *c = memnew(ViewportContainer);
  2656. viewport_container = c;
  2657. c->set_stretch(true);
  2658. add_child(c);
  2659. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2660. viewport = memnew(Viewport);
  2661. viewport->set_disable_input(true);
  2662. c->add_child(viewport);
  2663. surface = memnew(Control);
  2664. surface->set_drag_forwarding(this);
  2665. add_child(surface);
  2666. surface->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2667. surface->set_clip_contents(true);
  2668. camera = memnew(Camera);
  2669. camera->set_disable_gizmo(true);
  2670. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + p_index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER));
  2671. viewport->add_child(camera);
  2672. camera->make_current();
  2673. surface->set_focus_mode(FOCUS_ALL);
  2674. view_menu = memnew(MenuButton);
  2675. view_menu->set_flat(false);
  2676. surface->add_child(view_menu);
  2677. view_menu->set_position(Point2(10, 10) * EDSCALE);
  2678. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  2679. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  2680. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  2681. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  2682. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  2683. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  2684. view_menu->get_popup()->add_separator();
  2685. view_menu->get_popup()->add_radio_check_item(TTR("Perspective") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_PERSPECTIVE);
  2686. view_menu->get_popup()->add_radio_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL);
  2687. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  2688. view_menu->get_popup()->add_separator();
  2689. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_lock_rotation", TTR("Lock View Rotation")), VIEW_LOCK_ROTATION);
  2690. view_menu->get_popup()->add_separator();
  2691. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal", TTR("Display Normal")), VIEW_DISPLAY_NORMAL);
  2692. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe", TTR("Display Wireframe")), VIEW_DISPLAY_WIREFRAME);
  2693. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw", TTR("Display Overdraw")), VIEW_DISPLAY_OVERDRAW);
  2694. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded", TTR("Display Unshaded")), VIEW_DISPLAY_SHADELESS);
  2695. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  2696. view_menu->get_popup()->add_separator();
  2697. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("View Environment")), VIEW_ENVIRONMENT);
  2698. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("View Gizmos")), VIEW_GIZMOS);
  2699. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information", TTR("View Information")), VIEW_INFORMATION);
  2700. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_fps", TTR("View FPS")), VIEW_FPS);
  2701. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true);
  2702. view_menu->get_popup()->add_separator();
  2703. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_half_resolution", TTR("Half Resolution")), VIEW_HALF_RESOLUTION);
  2704. view_menu->get_popup()->add_separator();
  2705. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  2706. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_doppler", TTR("Doppler Enable")), VIEW_AUDIO_DOPPLER);
  2707. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true);
  2708. view_menu->get_popup()->add_separator();
  2709. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_cinematic_preview", TTR("Cinematic Preview")), VIEW_CINEMATIC_PREVIEW);
  2710. view_menu->get_popup()->add_separator();
  2711. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin"), VIEW_CENTER_TO_ORIGIN);
  2712. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  2713. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_selection_with_view"), VIEW_ALIGN_SELECTION_WITH_VIEW);
  2714. view_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2715. view_menu->set_disable_shortcuts(true);
  2716. ED_SHORTCUT("spatial_editor/freelook_left", TTR("Freelook Left"), KEY_A);
  2717. ED_SHORTCUT("spatial_editor/freelook_right", TTR("Freelook Right"), KEY_D);
  2718. ED_SHORTCUT("spatial_editor/freelook_forward", TTR("Freelook Forward"), KEY_W);
  2719. ED_SHORTCUT("spatial_editor/freelook_backwards", TTR("Freelook Backwards"), KEY_S);
  2720. ED_SHORTCUT("spatial_editor/freelook_up", TTR("Freelook Up"), KEY_E);
  2721. ED_SHORTCUT("spatial_editor/freelook_down", TTR("Freelook Down"), KEY_Q);
  2722. ED_SHORTCUT("spatial_editor/freelook_speed_modifier", TTR("Freelook Speed Modifier"), KEY_SHIFT);
  2723. preview_camera = memnew(CheckBox);
  2724. preview_camera->set_position(Point2(10, 38) * EDSCALE); // Below the 'view_menu' MenuButton.
  2725. preview_camera->set_text(TTR("Preview"));
  2726. surface->add_child(preview_camera);
  2727. preview_camera->hide();
  2728. preview_camera->connect("toggled", this, "_toggle_camera_preview");
  2729. previewing = NULL;
  2730. gizmo_scale = 1.0;
  2731. preview_node = NULL;
  2732. info_label = memnew(Label);
  2733. info_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  2734. info_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -90 * EDSCALE);
  2735. info_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  2736. info_label->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  2737. info_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  2738. info_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  2739. surface->add_child(info_label);
  2740. info_label->hide();
  2741. fps_label = memnew(Label);
  2742. fps_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  2743. fps_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  2744. fps_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  2745. fps_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  2746. fps_label->set_tooltip(TTR("Note: The FPS value displayed is the editor's framerate.\nIt cannot be used as a reliable indication of in-game performance."));
  2747. fps_label->set_mouse_filter(MOUSE_FILTER_PASS); // Otherwise tooltip doesn't show.
  2748. surface->add_child(fps_label);
  2749. fps_label->hide();
  2750. cinema_label = memnew(Label);
  2751. cinema_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  2752. cinema_label->set_h_grow_direction(GROW_DIRECTION_END);
  2753. cinema_label->set_align(Label::ALIGN_CENTER);
  2754. surface->add_child(cinema_label);
  2755. cinema_label->set_text(TTR("Cinematic Preview"));
  2756. cinema_label->hide();
  2757. previewing_cinema = false;
  2758. locked_label = memnew(Label);
  2759. locked_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -20 * EDSCALE);
  2760. locked_label->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  2761. locked_label->set_h_grow_direction(GROW_DIRECTION_END);
  2762. locked_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  2763. locked_label->set_align(Label::ALIGN_CENTER);
  2764. surface->add_child(locked_label);
  2765. locked_label->set_text(TTR("View Rotation Locked"));
  2766. locked_label->hide();
  2767. accept = NULL;
  2768. freelook_active = false;
  2769. freelook_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  2770. selection_menu = memnew(PopupMenu);
  2771. add_child(selection_menu);
  2772. selection_menu->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  2773. selection_menu->connect("id_pressed", this, "_selection_result_pressed");
  2774. selection_menu->connect("popup_hide", this, "_selection_menu_hide");
  2775. if (p_index == 0) {
  2776. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER), true);
  2777. viewport->set_as_audio_listener(true);
  2778. }
  2779. name = "";
  2780. _update_name();
  2781. EditorSettings::get_singleton()->connect("settings_changed", this, "update_transform_gizmo_view");
  2782. }
  2783. //////////////////////////////////////////////////////////////
  2784. void SpatialEditorViewportContainer::_gui_input(const Ref<InputEvent> &p_event) {
  2785. Ref<InputEventMouseButton> mb = p_event;
  2786. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  2787. if (mb->is_pressed()) {
  2788. Vector2 size = get_size();
  2789. int h_sep = get_constant("separation", "HSplitContainer");
  2790. int v_sep = get_constant("separation", "VSplitContainer");
  2791. int mid_w = size.width * ratio_h;
  2792. int mid_h = size.height * ratio_v;
  2793. dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2);
  2794. dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2);
  2795. drag_begin_pos = mb->get_position();
  2796. drag_begin_ratio.x = ratio_h;
  2797. drag_begin_ratio.y = ratio_v;
  2798. switch (view) {
  2799. case VIEW_USE_1_VIEWPORT: {
  2800. dragging_h = false;
  2801. dragging_v = false;
  2802. } break;
  2803. case VIEW_USE_2_VIEWPORTS: {
  2804. dragging_h = false;
  2805. } break;
  2806. case VIEW_USE_2_VIEWPORTS_ALT: {
  2807. dragging_v = false;
  2808. } break;
  2809. case VIEW_USE_3_VIEWPORTS:
  2810. case VIEW_USE_3_VIEWPORTS_ALT:
  2811. case VIEW_USE_4_VIEWPORTS: {
  2812. // Do nothing.
  2813. } break;
  2814. }
  2815. } else {
  2816. dragging_h = false;
  2817. dragging_v = false;
  2818. }
  2819. }
  2820. Ref<InputEventMouseMotion> mm = p_event;
  2821. if (mm.is_valid()) {
  2822. if (view == VIEW_USE_3_VIEWPORTS || view == VIEW_USE_3_VIEWPORTS_ALT || view == VIEW_USE_4_VIEWPORTS) {
  2823. Vector2 size = get_size();
  2824. int h_sep = get_constant("separation", "HSplitContainer");
  2825. int v_sep = get_constant("separation", "VSplitContainer");
  2826. int mid_w = size.width * ratio_h;
  2827. int mid_h = size.height * ratio_v;
  2828. bool was_hovering_h = hovering_h;
  2829. bool was_hovering_v = hovering_v;
  2830. hovering_h = mm->get_position().x > (mid_w - h_sep / 2) && mm->get_position().x < (mid_w + h_sep / 2);
  2831. hovering_v = mm->get_position().y > (mid_h - v_sep / 2) && mm->get_position().y < (mid_h + v_sep / 2);
  2832. if (was_hovering_h != hovering_h || was_hovering_v != hovering_v) {
  2833. update();
  2834. }
  2835. }
  2836. if (dragging_h) {
  2837. float new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width;
  2838. new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width);
  2839. ratio_h = new_ratio;
  2840. queue_sort();
  2841. update();
  2842. }
  2843. if (dragging_v) {
  2844. float new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height;
  2845. new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height);
  2846. ratio_v = new_ratio;
  2847. queue_sort();
  2848. update();
  2849. }
  2850. }
  2851. }
  2852. void SpatialEditorViewportContainer::_notification(int p_what) {
  2853. if (p_what == NOTIFICATION_MOUSE_ENTER || p_what == NOTIFICATION_MOUSE_EXIT) {
  2854. mouseover = (p_what == NOTIFICATION_MOUSE_ENTER);
  2855. update();
  2856. }
  2857. if (p_what == NOTIFICATION_DRAW && mouseover) {
  2858. Ref<Texture> h_grabber = get_icon("grabber", "HSplitContainer");
  2859. Ref<Texture> v_grabber = get_icon("grabber", "VSplitContainer");
  2860. Ref<Texture> hdiag_grabber = get_icon("GuiViewportHdiagsplitter", "EditorIcons");
  2861. Ref<Texture> vdiag_grabber = get_icon("GuiViewportVdiagsplitter", "EditorIcons");
  2862. Ref<Texture> vh_grabber = get_icon("GuiViewportVhsplitter", "EditorIcons");
  2863. Vector2 size = get_size();
  2864. int h_sep = get_constant("separation", "HSplitContainer");
  2865. int v_sep = get_constant("separation", "VSplitContainer");
  2866. int mid_w = size.width * ratio_h;
  2867. int mid_h = size.height * ratio_v;
  2868. int size_left = mid_w - h_sep / 2;
  2869. int size_bottom = size.height - mid_h - v_sep / 2;
  2870. switch (view) {
  2871. case VIEW_USE_1_VIEWPORT: {
  2872. // Nothing to show.
  2873. } break;
  2874. case VIEW_USE_2_VIEWPORTS: {
  2875. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2876. set_default_cursor_shape(CURSOR_VSPLIT);
  2877. } break;
  2878. case VIEW_USE_2_VIEWPORTS_ALT: {
  2879. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  2880. set_default_cursor_shape(CURSOR_HSPLIT);
  2881. } break;
  2882. case VIEW_USE_3_VIEWPORTS: {
  2883. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  2884. draw_texture(hdiag_grabber, Vector2(mid_w - hdiag_grabber->get_width() / 2, mid_h - v_grabber->get_height() / 4));
  2885. set_default_cursor_shape(CURSOR_DRAG);
  2886. } else if ((hovering_v && !dragging_h) || dragging_v) {
  2887. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2888. set_default_cursor_shape(CURSOR_VSPLIT);
  2889. } else if (hovering_h || dragging_h) {
  2890. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, mid_h + v_grabber->get_height() / 2 + (size_bottom - h_grabber->get_height()) / 2));
  2891. set_default_cursor_shape(CURSOR_HSPLIT);
  2892. }
  2893. } break;
  2894. case VIEW_USE_3_VIEWPORTS_ALT: {
  2895. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  2896. draw_texture(vdiag_grabber, Vector2(mid_w - vdiag_grabber->get_width() + v_grabber->get_height() / 4, mid_h - vdiag_grabber->get_height() / 2));
  2897. set_default_cursor_shape(CURSOR_DRAG);
  2898. } else if ((hovering_v && !dragging_h) || dragging_v) {
  2899. draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2900. set_default_cursor_shape(CURSOR_VSPLIT);
  2901. } else if (hovering_h || dragging_h) {
  2902. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  2903. set_default_cursor_shape(CURSOR_HSPLIT);
  2904. }
  2905. } break;
  2906. case VIEW_USE_4_VIEWPORTS: {
  2907. Vector2 half(mid_w, mid_h);
  2908. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  2909. draw_texture(vh_grabber, half - vh_grabber->get_size() / 2.0);
  2910. set_default_cursor_shape(CURSOR_DRAG);
  2911. } else if ((hovering_v && !dragging_h) || dragging_v) {
  2912. draw_texture(v_grabber, half - v_grabber->get_size() / 2.0);
  2913. set_default_cursor_shape(CURSOR_VSPLIT);
  2914. } else if (hovering_h || dragging_h) {
  2915. draw_texture(h_grabber, half - h_grabber->get_size() / 2.0);
  2916. set_default_cursor_shape(CURSOR_HSPLIT);
  2917. }
  2918. } break;
  2919. }
  2920. }
  2921. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  2922. SpatialEditorViewport *viewports[4];
  2923. int vc = 0;
  2924. for (int i = 0; i < get_child_count(); i++) {
  2925. viewports[vc] = Object::cast_to<SpatialEditorViewport>(get_child(i));
  2926. if (viewports[vc]) {
  2927. vc++;
  2928. }
  2929. }
  2930. ERR_FAIL_COND(vc != 4);
  2931. Size2 size = get_size();
  2932. if (size.x < 10 || size.y < 10) {
  2933. for (int i = 0; i < 4; i++) {
  2934. viewports[i]->hide();
  2935. }
  2936. return;
  2937. }
  2938. int h_sep = get_constant("separation", "HSplitContainer");
  2939. int v_sep = get_constant("separation", "VSplitContainer");
  2940. int mid_w = size.width * ratio_h;
  2941. int mid_h = size.height * ratio_v;
  2942. int size_left = mid_w - h_sep / 2;
  2943. int size_right = size.width - mid_w - h_sep / 2;
  2944. int size_top = mid_h - v_sep / 2;
  2945. int size_bottom = size.height - mid_h - v_sep / 2;
  2946. switch (view) {
  2947. case VIEW_USE_1_VIEWPORT: {
  2948. viewports[0]->show();
  2949. for (int i = 1; i < 4; i++) {
  2950. viewports[i]->hide();
  2951. }
  2952. fit_child_in_rect(viewports[0], Rect2(Vector2(), size));
  2953. } break;
  2954. case VIEW_USE_2_VIEWPORTS: {
  2955. for (int i = 0; i < 4; i++) {
  2956. if (i == 1 || i == 3)
  2957. viewports[i]->hide();
  2958. else
  2959. viewports[i]->show();
  2960. }
  2961. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  2962. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom)));
  2963. } break;
  2964. case VIEW_USE_2_VIEWPORTS_ALT: {
  2965. for (int i = 0; i < 4; i++) {
  2966. if (i == 1 || i == 3)
  2967. viewports[i]->hide();
  2968. else
  2969. viewports[i]->show();
  2970. }
  2971. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height)));
  2972. fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  2973. } break;
  2974. case VIEW_USE_3_VIEWPORTS: {
  2975. for (int i = 0; i < 4; i++) {
  2976. if (i == 1)
  2977. viewports[i]->hide();
  2978. else
  2979. viewports[i]->show();
  2980. }
  2981. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  2982. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  2983. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  2984. } break;
  2985. case VIEW_USE_3_VIEWPORTS_ALT: {
  2986. for (int i = 0; i < 4; i++) {
  2987. if (i == 1)
  2988. viewports[i]->hide();
  2989. else
  2990. viewports[i]->show();
  2991. }
  2992. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  2993. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  2994. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  2995. } break;
  2996. case VIEW_USE_4_VIEWPORTS: {
  2997. for (int i = 0; i < 4; i++) {
  2998. viewports[i]->show();
  2999. }
  3000. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  3001. fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top)));
  3002. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  3003. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  3004. } break;
  3005. }
  3006. }
  3007. }
  3008. void SpatialEditorViewportContainer::set_view(View p_view) {
  3009. view = p_view;
  3010. queue_sort();
  3011. }
  3012. SpatialEditorViewportContainer::View SpatialEditorViewportContainer::get_view() {
  3013. return view;
  3014. }
  3015. void SpatialEditorViewportContainer::_bind_methods() {
  3016. ClassDB::bind_method("_gui_input", &SpatialEditorViewportContainer::_gui_input);
  3017. }
  3018. SpatialEditorViewportContainer::SpatialEditorViewportContainer() {
  3019. set_clip_contents(true);
  3020. view = VIEW_USE_1_VIEWPORT;
  3021. mouseover = false;
  3022. ratio_h = 0.5;
  3023. ratio_v = 0.5;
  3024. hovering_v = false;
  3025. hovering_h = false;
  3026. dragging_v = false;
  3027. dragging_h = false;
  3028. }
  3029. ///////////////////////////////////////////////////////////////////
  3030. SpatialEditor *SpatialEditor::singleton = NULL;
  3031. SpatialEditorSelectedItem::~SpatialEditorSelectedItem() {
  3032. if (sbox_instance.is_valid())
  3033. VisualServer::get_singleton()->free(sbox_instance);
  3034. }
  3035. void SpatialEditor::select_gizmo_highlight_axis(int p_axis) {
  3036. for (int i = 0; i < 3; i++) {
  3037. move_gizmo[i]->surface_set_material(0, i == p_axis ? gizmo_hl : gizmo_color[i]);
  3038. move_plane_gizmo[i]->surface_set_material(0, (i + 6) == p_axis ? gizmo_hl : plane_gizmo_color[i]);
  3039. rotate_gizmo[i]->surface_set_material(0, (i + 3) == p_axis ? gizmo_hl : gizmo_color[i]);
  3040. scale_gizmo[i]->surface_set_material(0, (i + 9) == p_axis ? gizmo_hl : gizmo_color[i]);
  3041. scale_plane_gizmo[i]->surface_set_material(0, (i + 12) == p_axis ? gizmo_hl : plane_gizmo_color[i]);
  3042. }
  3043. }
  3044. void SpatialEditor::update_transform_gizmo() {
  3045. List<Node *> &selection = editor_selection->get_selected_node_list();
  3046. AABB center;
  3047. bool first = true;
  3048. Basis gizmo_basis;
  3049. bool local_gizmo_coords = are_local_coords_enabled();
  3050. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3051. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3052. if (!sp)
  3053. continue;
  3054. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  3055. if (!se)
  3056. continue;
  3057. Transform xf = se->sp->get_global_gizmo_transform();
  3058. if (first) {
  3059. center.position = xf.origin;
  3060. first = false;
  3061. if (local_gizmo_coords) {
  3062. gizmo_basis = xf.basis;
  3063. gizmo_basis.orthonormalize();
  3064. }
  3065. } else {
  3066. center.expand_to(xf.origin);
  3067. gizmo_basis = Basis();
  3068. }
  3069. }
  3070. Vector3 pcenter = center.position + center.size * 0.5;
  3071. gizmo.visible = !first;
  3072. gizmo.transform.origin = pcenter;
  3073. gizmo.transform.basis = gizmo_basis;
  3074. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3075. viewports[i]->update_transform_gizmo_view();
  3076. }
  3077. }
  3078. void _update_all_gizmos(Node *p_node) {
  3079. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  3080. Spatial *spatial_node = Object::cast_to<Spatial>(p_node->get_child(i));
  3081. if (spatial_node) {
  3082. spatial_node->update_gizmo();
  3083. }
  3084. _update_all_gizmos(p_node->get_child(i));
  3085. }
  3086. }
  3087. void SpatialEditor::update_all_gizmos(Node *p_node) {
  3088. if (!p_node) p_node = SceneTree::get_singleton()->get_root();
  3089. _update_all_gizmos(p_node);
  3090. }
  3091. Object *SpatialEditor::_get_editor_data(Object *p_what) {
  3092. Spatial *sp = Object::cast_to<Spatial>(p_what);
  3093. if (!sp)
  3094. return NULL;
  3095. SpatialEditorSelectedItem *si = memnew(SpatialEditorSelectedItem);
  3096. si->sp = sp;
  3097. si->sbox_instance = VisualServer::get_singleton()->instance_create2(selection_box->get_rid(), sp->get_world()->get_scenario());
  3098. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(si->sbox_instance, VS::SHADOW_CASTING_SETTING_OFF);
  3099. return si;
  3100. }
  3101. void SpatialEditor::_generate_selection_box() {
  3102. AABB aabb(Vector3(), Vector3(1, 1, 1));
  3103. aabb.grow_by(aabb.get_longest_axis_size() / 20.0);
  3104. Ref<SurfaceTool> st = memnew(SurfaceTool);
  3105. st->begin(Mesh::PRIMITIVE_LINES);
  3106. for (int i = 0; i < 12; i++) {
  3107. Vector3 a, b;
  3108. aabb.get_edge(i, a, b);
  3109. /*Vector<Vector3> points;
  3110. Vector<Color> colors;
  3111. points.push_back(a);
  3112. points.push_back(b);*/
  3113. st->add_color(Color(1.0, 1.0, 0.8, 0.8));
  3114. st->add_vertex(a);
  3115. st->add_color(Color(1.0, 1.0, 0.8, 0.4));
  3116. st->add_vertex(a.linear_interpolate(b, 0.2));
  3117. st->add_color(Color(1.0, 1.0, 0.8, 0.4));
  3118. st->add_vertex(a.linear_interpolate(b, 0.8));
  3119. st->add_color(Color(1.0, 1.0, 0.8, 0.8));
  3120. st->add_vertex(b);
  3121. }
  3122. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  3123. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3124. mat->set_albedo(Color(1, 1, 1));
  3125. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3126. mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  3127. mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  3128. st->set_material(mat);
  3129. selection_box = st->commit();
  3130. }
  3131. Dictionary SpatialEditor::get_state() const {
  3132. Dictionary d;
  3133. d["snap_enabled"] = snap_enabled;
  3134. d["translate_snap"] = get_translate_snap();
  3135. d["rotate_snap"] = get_rotate_snap();
  3136. d["scale_snap"] = get_scale_snap();
  3137. d["local_coords"] = tool_option_button[TOOL_OPT_LOCAL_COORDS]->is_pressed();
  3138. int vc = 0;
  3139. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT)))
  3140. vc = 1;
  3141. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS)))
  3142. vc = 2;
  3143. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS)))
  3144. vc = 3;
  3145. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS)))
  3146. vc = 4;
  3147. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT)))
  3148. vc = 5;
  3149. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT)))
  3150. vc = 6;
  3151. d["viewport_mode"] = vc;
  3152. Array vpdata;
  3153. for (int i = 0; i < 4; i++) {
  3154. vpdata.push_back(viewports[i]->get_state());
  3155. }
  3156. d["viewports"] = vpdata;
  3157. d["show_grid"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID));
  3158. d["show_origin"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN));
  3159. d["fov"] = get_fov();
  3160. d["znear"] = get_znear();
  3161. d["zfar"] = get_zfar();
  3162. Dictionary gizmos_status;
  3163. for (int i = 0; i < gizmo_plugins.size(); i++) {
  3164. if (!gizmo_plugins[i]->can_be_hidden()) continue;
  3165. int state = gizmos_menu->get_item_state(gizmos_menu->get_item_index(i));
  3166. String name = gizmo_plugins[i]->get_name();
  3167. gizmos_status[name] = state;
  3168. }
  3169. d["gizmos_status"] = gizmos_status;
  3170. return d;
  3171. }
  3172. void SpatialEditor::set_state(const Dictionary &p_state) {
  3173. Dictionary d = p_state;
  3174. if (d.has("snap_enabled")) {
  3175. snap_enabled = d["snap_enabled"];
  3176. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(d["snap_enabled"]);
  3177. }
  3178. if (d.has("translate_snap"))
  3179. snap_translate->set_text(d["translate_snap"]);
  3180. if (d.has("rotate_snap"))
  3181. snap_rotate->set_text(d["rotate_snap"]);
  3182. if (d.has("scale_snap"))
  3183. snap_scale->set_text(d["scale_snap"]);
  3184. if (d.has("local_coords")) {
  3185. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(d["local_coords"]);
  3186. update_transform_gizmo();
  3187. }
  3188. if (d.has("viewport_mode")) {
  3189. int vc = d["viewport_mode"];
  3190. if (vc == 1)
  3191. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  3192. else if (vc == 2)
  3193. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  3194. else if (vc == 3)
  3195. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  3196. else if (vc == 4)
  3197. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  3198. else if (vc == 5)
  3199. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  3200. else if (vc == 6)
  3201. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  3202. }
  3203. if (d.has("viewports")) {
  3204. Array vp = d["viewports"];
  3205. ERR_FAIL_COND(vp.size() > 4);
  3206. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3207. viewports[i]->set_state(vp[i]);
  3208. }
  3209. }
  3210. if (d.has("zfar"))
  3211. settings_zfar->set_value(float(d["zfar"]));
  3212. if (d.has("znear"))
  3213. settings_znear->set_value(float(d["znear"]));
  3214. if (d.has("fov"))
  3215. settings_fov->set_value(float(d["fov"]));
  3216. if (d.has("show_grid")) {
  3217. bool use = d["show_grid"];
  3218. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  3219. _menu_item_pressed(MENU_VIEW_GRID);
  3220. }
  3221. }
  3222. if (d.has("show_origin")) {
  3223. bool use = d["show_origin"];
  3224. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  3225. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use);
  3226. VisualServer::get_singleton()->instance_set_visible(origin_instance, use);
  3227. }
  3228. }
  3229. if (d.has("gizmos_status")) {
  3230. Dictionary gizmos_status = d["gizmos_status"];
  3231. List<Variant> keys;
  3232. gizmos_status.get_key_list(&keys);
  3233. for (int j = 0; j < gizmo_plugins.size(); ++j) {
  3234. if (!gizmo_plugins[j]->can_be_hidden()) continue;
  3235. int state = EditorSpatialGizmoPlugin::ON_TOP;
  3236. for (int i = 0; i < keys.size(); i++) {
  3237. if (gizmo_plugins.write[j]->get_name() == keys[i]) {
  3238. state = gizmos_status[keys[i]];
  3239. }
  3240. }
  3241. const int idx = gizmos_menu->get_item_index(j);
  3242. gizmos_menu->set_item_multistate(idx, state);
  3243. gizmo_plugins.write[j]->set_state(state);
  3244. switch (state) {
  3245. case EditorSpatialGizmoPlugin::VISIBLE:
  3246. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_visible"));
  3247. break;
  3248. case EditorSpatialGizmoPlugin::ON_TOP:
  3249. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_xray"));
  3250. break;
  3251. case EditorSpatialGizmoPlugin::HIDDEN:
  3252. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_hidden"));
  3253. break;
  3254. }
  3255. }
  3256. }
  3257. }
  3258. void SpatialEditor::edit(Spatial *p_spatial) {
  3259. if (p_spatial != selected) {
  3260. if (selected) {
  3261. Ref<EditorSpatialGizmo> seg = selected->get_gizmo();
  3262. if (seg.is_valid()) {
  3263. seg->set_selected(false);
  3264. selected->update_gizmo();
  3265. }
  3266. }
  3267. selected = p_spatial;
  3268. over_gizmo_handle = -1;
  3269. if (selected) {
  3270. Ref<EditorSpatialGizmo> seg = selected->get_gizmo();
  3271. if (seg.is_valid()) {
  3272. seg->set_selected(true);
  3273. selected->update_gizmo();
  3274. }
  3275. }
  3276. }
  3277. }
  3278. void SpatialEditor::_xform_dialog_action() {
  3279. Transform t;
  3280. //translation
  3281. Vector3 scale;
  3282. Vector3 rotate;
  3283. Vector3 translate;
  3284. for (int i = 0; i < 3; i++) {
  3285. translate[i] = xform_translate[i]->get_text().to_double();
  3286. rotate[i] = Math::deg2rad(xform_rotate[i]->get_text().to_double());
  3287. scale[i] = xform_scale[i]->get_text().to_double();
  3288. }
  3289. t.basis.scale(scale);
  3290. t.basis.rotate(rotate);
  3291. t.origin = translate;
  3292. undo_redo->create_action(TTR("XForm Dialog"));
  3293. List<Node *> &selection = editor_selection->get_selected_node_list();
  3294. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3295. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3296. if (!sp)
  3297. continue;
  3298. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  3299. if (!se)
  3300. continue;
  3301. bool post = xform_type->get_selected() > 0;
  3302. Transform tr = sp->get_global_gizmo_transform();
  3303. if (post)
  3304. tr = tr * t;
  3305. else {
  3306. tr.basis = t.basis * tr.basis;
  3307. tr.origin += t.origin;
  3308. }
  3309. undo_redo->add_do_method(sp, "set_global_transform", tr);
  3310. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  3311. }
  3312. undo_redo->commit_action();
  3313. }
  3314. void SpatialEditor::_menu_item_toggled(bool pressed, int p_option) {
  3315. switch (p_option) {
  3316. case MENU_TOOL_LOCAL_COORDS: {
  3317. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(pressed);
  3318. update_transform_gizmo();
  3319. } break;
  3320. case MENU_TOOL_USE_SNAP: {
  3321. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(pressed);
  3322. snap_enabled = pressed;
  3323. } break;
  3324. }
  3325. }
  3326. void SpatialEditor::_menu_gizmo_toggled(int p_option) {
  3327. const int idx = gizmos_menu->get_item_index(p_option);
  3328. gizmos_menu->toggle_item_multistate(idx);
  3329. // Change icon
  3330. const int state = gizmos_menu->get_item_state(idx);
  3331. switch (state) {
  3332. case EditorSpatialGizmoPlugin::VISIBLE:
  3333. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_visible"));
  3334. break;
  3335. case EditorSpatialGizmoPlugin::ON_TOP:
  3336. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_xray"));
  3337. break;
  3338. case EditorSpatialGizmoPlugin::HIDDEN:
  3339. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_hidden"));
  3340. break;
  3341. }
  3342. gizmo_plugins.write[p_option]->set_state(state);
  3343. update_all_gizmos();
  3344. }
  3345. void SpatialEditor::_menu_item_pressed(int p_option) {
  3346. switch (p_option) {
  3347. case MENU_TOOL_SELECT:
  3348. case MENU_TOOL_MOVE:
  3349. case MENU_TOOL_ROTATE:
  3350. case MENU_TOOL_SCALE:
  3351. case MENU_TOOL_LIST_SELECT: {
  3352. for (int i = 0; i < TOOL_MAX; i++)
  3353. tool_button[i]->set_pressed(i == p_option);
  3354. tool_mode = (ToolMode)p_option;
  3355. update_transform_gizmo();
  3356. } break;
  3357. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  3358. snap_dialog->popup_centered(Size2(200, 180));
  3359. } break;
  3360. case MENU_TRANSFORM_DIALOG: {
  3361. for (int i = 0; i < 3; i++) {
  3362. xform_translate[i]->set_text("0");
  3363. xform_rotate[i]->set_text("0");
  3364. xform_scale[i]->set_text("1");
  3365. }
  3366. xform_dialog->popup_centered(Size2(320, 240) * EDSCALE);
  3367. } break;
  3368. case MENU_VIEW_USE_1_VIEWPORT: {
  3369. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_1_VIEWPORT);
  3370. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true);
  3371. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3372. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3373. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3374. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3375. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3376. } break;
  3377. case MENU_VIEW_USE_2_VIEWPORTS: {
  3378. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS);
  3379. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3380. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true);
  3381. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3382. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3383. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3384. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3385. } break;
  3386. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  3387. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT);
  3388. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3389. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3390. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3391. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3392. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true);
  3393. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3394. } break;
  3395. case MENU_VIEW_USE_3_VIEWPORTS: {
  3396. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS);
  3397. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3398. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3399. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true);
  3400. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3401. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3402. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3403. } break;
  3404. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  3405. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT);
  3406. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3407. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3408. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3409. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3410. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3411. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true);
  3412. } break;
  3413. case MENU_VIEW_USE_4_VIEWPORTS: {
  3414. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_4_VIEWPORTS);
  3415. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3416. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3417. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3418. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true);
  3419. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3420. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3421. } break;
  3422. case MENU_VIEW_ORIGIN: {
  3423. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  3424. origin_enabled = !is_checked;
  3425. VisualServer::get_singleton()->instance_set_visible(origin_instance, origin_enabled);
  3426. // Update the grid since its appearance depends on whether the origin is enabled
  3427. _finish_grid();
  3428. _init_grid();
  3429. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), origin_enabled);
  3430. } break;
  3431. case MENU_VIEW_GRID: {
  3432. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  3433. grid_enabled = !is_checked;
  3434. for (int i = 0; i < 3; ++i) {
  3435. if (grid_enable[i]) {
  3436. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], grid_enabled);
  3437. grid_visible[i] = grid_enabled;
  3438. }
  3439. }
  3440. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), grid_enabled);
  3441. } break;
  3442. case MENU_VIEW_CAMERA_SETTINGS: {
  3443. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  3444. } break;
  3445. case MENU_SNAP_TO_FLOOR: {
  3446. snap_selected_nodes_to_floor();
  3447. } break;
  3448. case MENU_LOCK_SELECTED: {
  3449. List<Node *> &selection = editor_selection->get_selected_node_list();
  3450. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3451. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3452. if (!spatial || !spatial->is_visible_in_tree())
  3453. continue;
  3454. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3455. continue;
  3456. spatial->set_meta("_edit_lock_", true);
  3457. emit_signal("item_lock_status_changed");
  3458. }
  3459. _refresh_menu_icons();
  3460. } break;
  3461. case MENU_UNLOCK_SELECTED: {
  3462. List<Node *> &selection = editor_selection->get_selected_node_list();
  3463. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3464. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3465. if (!spatial || !spatial->is_visible_in_tree())
  3466. continue;
  3467. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3468. continue;
  3469. spatial->set_meta("_edit_lock_", Variant());
  3470. emit_signal("item_lock_status_changed");
  3471. }
  3472. _refresh_menu_icons();
  3473. } break;
  3474. }
  3475. }
  3476. void SpatialEditor::_init_indicators() {
  3477. {
  3478. origin_enabled = true;
  3479. grid_enabled = true;
  3480. indicator_mat.instance();
  3481. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3482. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  3483. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  3484. Vector<Color> origin_colors;
  3485. Vector<Vector3> origin_points;
  3486. for (int i = 0; i < 3; i++) {
  3487. Vector3 axis;
  3488. axis[i] = 1;
  3489. grid_enable[i] = false;
  3490. grid_visible[i] = false;
  3491. origin_colors.push_back(Color(axis.x, axis.y, axis.z));
  3492. origin_colors.push_back(Color(axis.x, axis.y, axis.z));
  3493. origin_points.push_back(axis * 4096);
  3494. origin_points.push_back(axis * -4096);
  3495. }
  3496. grid_enable[1] = true;
  3497. grid_visible[1] = true;
  3498. _init_grid();
  3499. origin = VisualServer::get_singleton()->mesh_create();
  3500. Array d;
  3501. d.resize(VS::ARRAY_MAX);
  3502. d[VisualServer::ARRAY_VERTEX] = origin_points;
  3503. d[VisualServer::ARRAY_COLOR] = origin_colors;
  3504. VisualServer::get_singleton()->mesh_add_surface_from_arrays(origin, VisualServer::PRIMITIVE_LINES, d);
  3505. VisualServer::get_singleton()->mesh_surface_set_material(origin, 0, indicator_mat->get_rid());
  3506. origin_instance = VisualServer::get_singleton()->instance_create2(origin, get_tree()->get_root()->get_world()->get_scenario());
  3507. VS::get_singleton()->instance_set_layer_mask(origin_instance, 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  3508. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, VS::SHADOW_CASTING_SETTING_OFF);
  3509. }
  3510. {
  3511. //move gizmo
  3512. float gizmo_alph = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_opacity");
  3513. gizmo_hl = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  3514. gizmo_hl->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3515. gizmo_hl->set_on_top_of_alpha();
  3516. gizmo_hl->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3517. gizmo_hl->set_albedo(Color(1, 1, 1, gizmo_alph + 0.2f));
  3518. gizmo_hl->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3519. for (int i = 0; i < 3; i++) {
  3520. move_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3521. move_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3522. rotate_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3523. scale_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3524. scale_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3525. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  3526. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3527. mat->set_on_top_of_alpha();
  3528. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3529. Color col;
  3530. col[i] = 1.0;
  3531. col.a = gizmo_alph;
  3532. mat->set_albedo(col);
  3533. gizmo_color[i] = mat;
  3534. Vector3 ivec;
  3535. ivec[i] = 1;
  3536. Vector3 nivec;
  3537. nivec[(i + 1) % 3] = 1;
  3538. nivec[(i + 2) % 3] = 1;
  3539. Vector3 ivec2;
  3540. ivec2[(i + 1) % 3] = 1;
  3541. Vector3 ivec3;
  3542. ivec3[(i + 2) % 3] = 1;
  3543. //translate
  3544. {
  3545. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3546. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3547. // Arrow profile
  3548. const int arrow_points = 5;
  3549. Vector3 arrow[5] = {
  3550. nivec * 0.0 + ivec * 0.0,
  3551. nivec * 0.01 + ivec * 0.0,
  3552. nivec * 0.01 + ivec * GIZMO_ARROW_OFFSET,
  3553. nivec * 0.065 + ivec * GIZMO_ARROW_OFFSET,
  3554. nivec * 0.0 + ivec * (GIZMO_ARROW_OFFSET + GIZMO_ARROW_SIZE),
  3555. };
  3556. int arrow_sides = 16;
  3557. for (int k = 0; k < arrow_sides; k++) {
  3558. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  3559. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  3560. for (int j = 0; j < arrow_points - 1; j++) {
  3561. Vector3 points[4] = {
  3562. ma.xform(arrow[j]),
  3563. mb.xform(arrow[j]),
  3564. mb.xform(arrow[j + 1]),
  3565. ma.xform(arrow[j + 1]),
  3566. };
  3567. surftool->add_vertex(points[0]);
  3568. surftool->add_vertex(points[1]);
  3569. surftool->add_vertex(points[2]);
  3570. surftool->add_vertex(points[0]);
  3571. surftool->add_vertex(points[2]);
  3572. surftool->add_vertex(points[3]);
  3573. }
  3574. }
  3575. surftool->set_material(mat);
  3576. surftool->commit(move_gizmo[i]);
  3577. }
  3578. // Plane Translation
  3579. {
  3580. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3581. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3582. Vector3 vec = ivec2 - ivec3;
  3583. Vector3 plane[4] = {
  3584. vec * GIZMO_PLANE_DST,
  3585. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  3586. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  3587. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  3588. };
  3589. Basis ma(ivec, Math_PI / 2);
  3590. Vector3 points[4] = {
  3591. ma.xform(plane[0]),
  3592. ma.xform(plane[1]),
  3593. ma.xform(plane[2]),
  3594. ma.xform(plane[3]),
  3595. };
  3596. surftool->add_vertex(points[0]);
  3597. surftool->add_vertex(points[1]);
  3598. surftool->add_vertex(points[2]);
  3599. surftool->add_vertex(points[0]);
  3600. surftool->add_vertex(points[2]);
  3601. surftool->add_vertex(points[3]);
  3602. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  3603. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3604. plane_mat->set_on_top_of_alpha();
  3605. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3606. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3607. Color col2;
  3608. col2[i] = 1.0;
  3609. col2.a = gizmo_alph;
  3610. plane_mat->set_albedo(col2);
  3611. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  3612. surftool->set_material(plane_mat);
  3613. surftool->commit(move_plane_gizmo[i]);
  3614. }
  3615. // Rotate
  3616. {
  3617. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3618. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3619. Vector3 circle[5] = {
  3620. ivec * 0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3621. ivec * -0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3622. ivec * -0.02 + ivec2 * -0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3623. ivec * 0.02 + ivec2 * -0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3624. ivec * 0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3625. };
  3626. for (int k = 0; k < 64; k++) {
  3627. Basis ma(ivec, Math_PI * 2 * float(k) / 64);
  3628. Basis mb(ivec, Math_PI * 2 * float(k + 1) / 64);
  3629. for (int j = 0; j < 4; j++) {
  3630. Vector3 points[4] = {
  3631. ma.xform(circle[j]),
  3632. mb.xform(circle[j]),
  3633. mb.xform(circle[j + 1]),
  3634. ma.xform(circle[j + 1]),
  3635. };
  3636. surftool->add_vertex(points[0]);
  3637. surftool->add_vertex(points[1]);
  3638. surftool->add_vertex(points[2]);
  3639. surftool->add_vertex(points[0]);
  3640. surftool->add_vertex(points[2]);
  3641. surftool->add_vertex(points[3]);
  3642. }
  3643. }
  3644. surftool->set_material(mat);
  3645. surftool->commit(rotate_gizmo[i]);
  3646. }
  3647. // Scale
  3648. {
  3649. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3650. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3651. // Cube arrow profile
  3652. const int arrow_points = 6;
  3653. Vector3 arrow[6] = {
  3654. nivec * 0.0 + ivec * 0.0,
  3655. nivec * 0.01 + ivec * 0.0,
  3656. nivec * 0.01 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  3657. nivec * 0.07 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  3658. nivec * 0.07 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  3659. nivec * 0.0 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  3660. };
  3661. int arrow_sides = 4;
  3662. for (int k = 0; k < 4; k++) {
  3663. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  3664. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  3665. for (int j = 0; j < arrow_points - 1; j++) {
  3666. Vector3 points[4] = {
  3667. ma.xform(arrow[j]),
  3668. mb.xform(arrow[j]),
  3669. mb.xform(arrow[j + 1]),
  3670. ma.xform(arrow[j + 1]),
  3671. };
  3672. surftool->add_vertex(points[0]);
  3673. surftool->add_vertex(points[1]);
  3674. surftool->add_vertex(points[2]);
  3675. surftool->add_vertex(points[0]);
  3676. surftool->add_vertex(points[2]);
  3677. surftool->add_vertex(points[3]);
  3678. }
  3679. }
  3680. surftool->set_material(mat);
  3681. surftool->commit(scale_gizmo[i]);
  3682. }
  3683. // Plane Scale
  3684. {
  3685. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3686. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3687. Vector3 vec = ivec2 - ivec3;
  3688. Vector3 plane[4] = {
  3689. vec * GIZMO_PLANE_DST,
  3690. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  3691. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  3692. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  3693. };
  3694. Basis ma(ivec, Math_PI / 2);
  3695. Vector3 points[4] = {
  3696. ma.xform(plane[0]),
  3697. ma.xform(plane[1]),
  3698. ma.xform(plane[2]),
  3699. ma.xform(plane[3]),
  3700. };
  3701. surftool->add_vertex(points[0]);
  3702. surftool->add_vertex(points[1]);
  3703. surftool->add_vertex(points[2]);
  3704. surftool->add_vertex(points[0]);
  3705. surftool->add_vertex(points[2]);
  3706. surftool->add_vertex(points[3]);
  3707. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  3708. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3709. plane_mat->set_on_top_of_alpha();
  3710. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3711. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3712. Color col2;
  3713. col2[i] = 1.0;
  3714. col2.a = gizmo_alph;
  3715. plane_mat->set_albedo(col2);
  3716. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  3717. surftool->set_material(plane_mat);
  3718. surftool->commit(scale_plane_gizmo[i]);
  3719. }
  3720. }
  3721. }
  3722. _generate_selection_box();
  3723. }
  3724. struct _GizmoPluginComparator {
  3725. bool operator()(const Ref<EditorSpatialGizmoPlugin> &p_a, const Ref<EditorSpatialGizmoPlugin> &p_b) const {
  3726. return p_a->get_name() < p_b->get_name();
  3727. }
  3728. };
  3729. void SpatialEditor::_update_gizmos_menu() {
  3730. gizmos_menu->clear();
  3731. gizmo_plugins.sort_custom<_GizmoPluginComparator>();
  3732. for (int i = 0; i < gizmo_plugins.size(); ++i) {
  3733. if (!gizmo_plugins[i]->can_be_hidden()) continue;
  3734. String plugin_name = gizmo_plugins[i]->get_name();
  3735. gizmos_menu->add_multistate_item(TTR(plugin_name), 3, EditorSpatialGizmoPlugin::VISIBLE, i);
  3736. gizmos_menu->set_item_icon(gizmos_menu->get_item_index(i), gizmos_menu->get_icon("visibility_visible"));
  3737. }
  3738. }
  3739. void SpatialEditor::_update_gizmos_menu_theme() {
  3740. for (int i = 0; i < gizmo_plugins.size(); ++i) {
  3741. if (!gizmo_plugins[i]->can_be_hidden()) continue;
  3742. gizmos_menu->set_item_icon(gizmos_menu->get_item_index(i), gizmos_menu->get_icon("visibility_visible"));
  3743. }
  3744. }
  3745. void SpatialEditor::_init_grid() {
  3746. PoolVector<Color> grid_colors[3];
  3747. PoolVector<Vector3> grid_points[3];
  3748. Color primary_grid_color = EditorSettings::get_singleton()->get("editors/3d/primary_grid_color");
  3749. Color secondary_grid_color = EditorSettings::get_singleton()->get("editors/3d/secondary_grid_color");
  3750. int grid_size = EditorSettings::get_singleton()->get("editors/3d/grid_size");
  3751. int primary_grid_steps = EditorSettings::get_singleton()->get("editors/3d/primary_grid_steps");
  3752. for (int i = 0; i < 3; i++) {
  3753. Vector3 axis;
  3754. axis[i] = 1;
  3755. Vector3 axis_n1;
  3756. axis_n1[(i + 1) % 3] = 1;
  3757. Vector3 axis_n2;
  3758. axis_n2[(i + 2) % 3] = 1;
  3759. for (int j = -grid_size; j <= grid_size; j++) {
  3760. Vector3 p1 = axis_n1 * j + axis_n2 * -grid_size;
  3761. Vector3 p1_dest = p1 * (-axis_n2 + axis_n1);
  3762. Vector3 p2 = axis_n2 * j + axis_n1 * -grid_size;
  3763. Vector3 p2_dest = p2 * (-axis_n1 + axis_n2);
  3764. Color line_color = secondary_grid_color;
  3765. if (origin_enabled && j == 0) {
  3766. // Don't draw the center lines of the grid if the origin is enabled
  3767. // The origin would overlap the grid lines in this case, causing flickering
  3768. continue;
  3769. } else if (j % primary_grid_steps == 0) {
  3770. line_color = primary_grid_color;
  3771. }
  3772. grid_points[i].push_back(p1);
  3773. grid_points[i].push_back(p1_dest);
  3774. grid_colors[i].push_back(line_color);
  3775. grid_colors[i].push_back(line_color);
  3776. grid_points[i].push_back(p2);
  3777. grid_points[i].push_back(p2_dest);
  3778. grid_colors[i].push_back(line_color);
  3779. grid_colors[i].push_back(line_color);
  3780. }
  3781. grid[i] = VisualServer::get_singleton()->mesh_create();
  3782. Array d;
  3783. d.resize(VS::ARRAY_MAX);
  3784. d[VisualServer::ARRAY_VERTEX] = grid_points[i];
  3785. d[VisualServer::ARRAY_COLOR] = grid_colors[i];
  3786. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  3787. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  3788. grid_instance[i] = VisualServer::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  3789. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], grid_visible[i]);
  3790. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  3791. VS::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  3792. }
  3793. }
  3794. void SpatialEditor::_finish_indicators() {
  3795. VisualServer::get_singleton()->free(origin_instance);
  3796. VisualServer::get_singleton()->free(origin);
  3797. _finish_grid();
  3798. }
  3799. void SpatialEditor::_finish_grid() {
  3800. for (int i = 0; i < 3; i++) {
  3801. VisualServer::get_singleton()->free(grid_instance[i]);
  3802. VisualServer::get_singleton()->free(grid[i]);
  3803. }
  3804. }
  3805. bool SpatialEditor::is_any_freelook_active() const {
  3806. for (unsigned int i = 0; i < VIEWPORTS_COUNT; ++i) {
  3807. if (viewports[i]->is_freelook_active())
  3808. return true;
  3809. }
  3810. return false;
  3811. }
  3812. void SpatialEditor::_refresh_menu_icons() {
  3813. bool all_locked = true;
  3814. List<Node *> &selection = editor_selection->get_selected_node_list();
  3815. if (selection.empty()) {
  3816. all_locked = false;
  3817. } else {
  3818. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3819. if (Object::cast_to<Spatial>(E->get()) && !Object::cast_to<Spatial>(E->get())->has_meta("_edit_lock_")) {
  3820. all_locked = false;
  3821. break;
  3822. }
  3823. }
  3824. }
  3825. tool_button[TOOL_LOCK_SELECTED]->set_visible(!all_locked);
  3826. tool_button[TOOL_LOCK_SELECTED]->set_disabled(selection.empty());
  3827. tool_button[TOOL_UNLOCK_SELECTED]->set_visible(all_locked);
  3828. }
  3829. template <typename T>
  3830. Set<T *> _get_child_nodes(Node *parent_node) {
  3831. Set<T *> nodes = Set<T *>();
  3832. T *node = Node::cast_to<T>(parent_node);
  3833. if (node) {
  3834. nodes.insert(node);
  3835. }
  3836. for (int i = 0; i < parent_node->get_child_count(); i++) {
  3837. Node *child_node = parent_node->get_child(i);
  3838. Set<T *> child_nodes = _get_child_nodes<T>(child_node);
  3839. for (typename Set<T *>::Element *I = child_nodes.front(); I; I = I->next()) {
  3840. nodes.insert(I->get());
  3841. }
  3842. }
  3843. return nodes;
  3844. }
  3845. Set<RID> _get_physics_bodies_rid(Node *node) {
  3846. Set<RID> rids = Set<RID>();
  3847. PhysicsBody *pb = Node::cast_to<PhysicsBody>(node);
  3848. if (pb) {
  3849. rids.insert(pb->get_rid());
  3850. }
  3851. Set<PhysicsBody *> child_nodes = _get_child_nodes<PhysicsBody>(node);
  3852. for (Set<PhysicsBody *>::Element *I = child_nodes.front(); I; I = I->next()) {
  3853. rids.insert(I->get()->get_rid());
  3854. }
  3855. return rids;
  3856. }
  3857. void SpatialEditor::snap_selected_nodes_to_floor() {
  3858. List<Node *> &selection = editor_selection->get_selected_node_list();
  3859. Dictionary snap_data;
  3860. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3861. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3862. if (sp) {
  3863. Vector3 from = Vector3();
  3864. Vector3 position_offset = Vector3();
  3865. // Priorities for snapping to floor are CollisionShapes, VisualInstances and then origin
  3866. Set<VisualInstance *> vi = _get_child_nodes<VisualInstance>(sp);
  3867. Set<CollisionShape *> cs = _get_child_nodes<CollisionShape>(sp);
  3868. if (cs.size()) {
  3869. AABB aabb = sp->get_global_transform().xform(cs.front()->get()->get_shape()->get_debug_mesh()->get_aabb());
  3870. for (Set<CollisionShape *>::Element *I = cs.front(); I; I = I->next()) {
  3871. aabb.merge_with(sp->get_global_transform().xform(I->get()->get_shape()->get_debug_mesh()->get_aabb()));
  3872. }
  3873. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  3874. from = aabb.position + size;
  3875. position_offset.y = from.y - sp->get_global_transform().origin.y;
  3876. } else if (vi.size()) {
  3877. AABB aabb = vi.front()->get()->get_transformed_aabb();
  3878. for (Set<VisualInstance *>::Element *I = vi.front(); I; I = I->next()) {
  3879. aabb.merge_with(I->get()->get_transformed_aabb());
  3880. }
  3881. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  3882. from = aabb.position + size;
  3883. position_offset.y = from.y - sp->get_global_transform().origin.y;
  3884. } else {
  3885. from = sp->get_global_transform().origin;
  3886. }
  3887. // We add a bit of margin to the from position to avoid it from snapping
  3888. // when the spatial is already on a floor and there's another floor under
  3889. // it
  3890. from = from + Vector3(0.0, 0.1, 0.0);
  3891. Dictionary d;
  3892. d["from"] = from;
  3893. d["position_offset"] = position_offset;
  3894. snap_data[sp] = d;
  3895. }
  3896. }
  3897. PhysicsDirectSpaceState *ss = get_tree()->get_root()->get_world()->get_direct_space_state();
  3898. PhysicsDirectSpaceState::RayResult result;
  3899. Array keys = snap_data.keys();
  3900. if (keys.size()) {
  3901. undo_redo->create_action(TTR("Snap Nodes To Floor"));
  3902. for (int i = 0; i < keys.size(); i++) {
  3903. Node *node = keys[i];
  3904. Spatial *sp = Object::cast_to<Spatial>(node);
  3905. Dictionary d = snap_data[node];
  3906. Vector3 from = d["from"];
  3907. Vector3 position_offset = d["position_offset"];
  3908. Vector3 to = from - Vector3(0.0, 10.0, 0.0);
  3909. Set<RID> excluded = _get_physics_bodies_rid(sp);
  3910. if (ss->intersect_ray(from, to, result, excluded)) {
  3911. Transform new_transform = sp->get_global_transform();
  3912. new_transform.origin.y = result.position.y;
  3913. new_transform.origin = new_transform.origin - position_offset;
  3914. undo_redo->add_do_method(sp, "set_global_transform", new_transform);
  3915. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_transform());
  3916. }
  3917. }
  3918. undo_redo->commit_action();
  3919. }
  3920. }
  3921. void SpatialEditor::_unhandled_key_input(Ref<InputEvent> p_event) {
  3922. if (!is_visible_in_tree() || get_viewport()->gui_has_modal_stack())
  3923. return;
  3924. snap_key_enabled = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  3925. Ref<InputEventKey> k = p_event;
  3926. if (k.is_valid()) {
  3927. // Note: need to check is_echo because first person movement keys might still be held
  3928. if (!is_any_freelook_active() && !p_event->is_echo()) {
  3929. if (!k->is_pressed())
  3930. return;
  3931. if (ED_IS_SHORTCUT("spatial_editor/tool_select", p_event)) {
  3932. _menu_item_pressed(MENU_TOOL_SELECT);
  3933. } else if (ED_IS_SHORTCUT("spatial_editor/tool_move", p_event)) {
  3934. _menu_item_pressed(MENU_TOOL_MOVE);
  3935. } else if (ED_IS_SHORTCUT("spatial_editor/tool_rotate", p_event)) {
  3936. _menu_item_pressed(MENU_TOOL_ROTATE);
  3937. } else if (ED_IS_SHORTCUT("spatial_editor/tool_scale", p_event)) {
  3938. _menu_item_pressed(MENU_TOOL_SCALE);
  3939. } else if (ED_IS_SHORTCUT("spatial_editor/snap_to_floor", p_event)) {
  3940. snap_selected_nodes_to_floor();
  3941. } else if (ED_IS_SHORTCUT("spatial_editor/local_coords", p_event)) {
  3942. if (are_local_coords_enabled()) {
  3943. _menu_item_toggled(false, MENU_TOOL_LOCAL_COORDS);
  3944. } else {
  3945. _menu_item_toggled(true, MENU_TOOL_LOCAL_COORDS);
  3946. }
  3947. } else if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  3948. if (is_snap_enabled()) {
  3949. _menu_item_toggled(false, MENU_TOOL_USE_SNAP);
  3950. } else {
  3951. _menu_item_toggled(true, MENU_TOOL_USE_SNAP);
  3952. }
  3953. }
  3954. }
  3955. }
  3956. }
  3957. void SpatialEditor::_notification(int p_what) {
  3958. if (p_what == NOTIFICATION_READY) {
  3959. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  3960. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  3961. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  3962. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  3963. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  3964. tool_button[SpatialEditor::TOOL_LOCK_SELECTED]->set_icon(get_icon("Lock", "EditorIcons"));
  3965. tool_button[SpatialEditor::TOOL_UNLOCK_SELECTED]->set_icon(get_icon("Unlock", "EditorIcons"));
  3966. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  3967. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  3968. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  3969. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  3970. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  3971. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  3972. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  3973. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  3974. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  3975. _refresh_menu_icons();
  3976. get_tree()->connect("node_removed", this, "_node_removed");
  3977. EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor()->connect("node_changed", this, "_refresh_menu_icons");
  3978. editor_selection->connect("selection_changed", this, "_refresh_menu_icons");
  3979. } else if (p_what == NOTIFICATION_ENTER_TREE) {
  3980. _register_all_gizmos();
  3981. _update_gizmos_menu();
  3982. _init_indicators();
  3983. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  3984. _update_gizmos_menu_theme();
  3985. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  3986. _finish_indicators();
  3987. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  3988. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  3989. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  3990. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  3991. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  3992. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  3993. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  3994. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  3995. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  3996. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  3997. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  3998. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  3999. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  4000. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  4001. // Update grid color by rebuilding grid.
  4002. _finish_grid();
  4003. _init_grid();
  4004. }
  4005. }
  4006. void SpatialEditor::add_control_to_menu_panel(Control *p_control) {
  4007. hbc_menu->add_child(p_control);
  4008. }
  4009. void SpatialEditor::remove_control_from_menu_panel(Control *p_control) {
  4010. hbc_menu->remove_child(p_control);
  4011. }
  4012. void SpatialEditor::set_can_preview(Camera *p_preview) {
  4013. for (int i = 0; i < 4; i++) {
  4014. viewports[i]->set_can_preview(p_preview);
  4015. }
  4016. }
  4017. VSplitContainer *SpatialEditor::get_shader_split() {
  4018. return shader_split;
  4019. }
  4020. HSplitContainer *SpatialEditor::get_palette_split() {
  4021. return palette_split;
  4022. }
  4023. void SpatialEditor::_request_gizmo(Object *p_obj) {
  4024. Spatial *sp = Object::cast_to<Spatial>(p_obj);
  4025. if (!sp)
  4026. return;
  4027. if (editor->get_edited_scene() && (sp == editor->get_edited_scene() || (sp->get_owner() && editor->get_edited_scene()->is_a_parent_of(sp)))) {
  4028. Ref<EditorSpatialGizmo> seg;
  4029. for (int i = 0; i < gizmo_plugins.size(); ++i) {
  4030. seg = gizmo_plugins.write[i]->get_gizmo(sp);
  4031. if (seg.is_valid()) {
  4032. sp->set_gizmo(seg);
  4033. if (sp == selected) {
  4034. seg->set_selected(true);
  4035. selected->update_gizmo();
  4036. }
  4037. break;
  4038. }
  4039. }
  4040. }
  4041. }
  4042. void SpatialEditor::_toggle_maximize_view(Object *p_viewport) {
  4043. if (!p_viewport) return;
  4044. SpatialEditorViewport *current_viewport = Object::cast_to<SpatialEditorViewport>(p_viewport);
  4045. if (!current_viewport) return;
  4046. int index = -1;
  4047. bool maximized = false;
  4048. for (int i = 0; i < 4; i++) {
  4049. if (viewports[i] == current_viewport) {
  4050. index = i;
  4051. if (current_viewport->get_global_rect() == viewport_base->get_global_rect())
  4052. maximized = true;
  4053. break;
  4054. }
  4055. }
  4056. if (index == -1) return;
  4057. if (!maximized) {
  4058. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4059. if (i == (uint32_t)index)
  4060. viewports[i]->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  4061. else
  4062. viewports[i]->hide();
  4063. }
  4064. } else {
  4065. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++)
  4066. viewports[i]->show();
  4067. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT)))
  4068. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  4069. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS)))
  4070. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  4071. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT)))
  4072. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  4073. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS)))
  4074. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  4075. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT)))
  4076. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  4077. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS)))
  4078. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  4079. }
  4080. }
  4081. void SpatialEditor::_node_removed(Node *p_node) {
  4082. if (p_node == selected)
  4083. selected = NULL;
  4084. }
  4085. void SpatialEditor::_register_all_gizmos() {
  4086. add_gizmo_plugin(Ref<CameraSpatialGizmoPlugin>(memnew(CameraSpatialGizmoPlugin)));
  4087. add_gizmo_plugin(Ref<LightSpatialGizmoPlugin>(memnew(LightSpatialGizmoPlugin)));
  4088. add_gizmo_plugin(Ref<AudioStreamPlayer3DSpatialGizmoPlugin>(memnew(AudioStreamPlayer3DSpatialGizmoPlugin)));
  4089. add_gizmo_plugin(Ref<MeshInstanceSpatialGizmoPlugin>(memnew(MeshInstanceSpatialGizmoPlugin)));
  4090. add_gizmo_plugin(Ref<SoftBodySpatialGizmoPlugin>(memnew(SoftBodySpatialGizmoPlugin)));
  4091. add_gizmo_plugin(Ref<Sprite3DSpatialGizmoPlugin>(memnew(Sprite3DSpatialGizmoPlugin)));
  4092. add_gizmo_plugin(Ref<SkeletonSpatialGizmoPlugin>(memnew(SkeletonSpatialGizmoPlugin)));
  4093. add_gizmo_plugin(Ref<Position3DSpatialGizmoPlugin>(memnew(Position3DSpatialGizmoPlugin)));
  4094. add_gizmo_plugin(Ref<RayCastSpatialGizmoPlugin>(memnew(RayCastSpatialGizmoPlugin)));
  4095. add_gizmo_plugin(Ref<SpringArmSpatialGizmoPlugin>(memnew(SpringArmSpatialGizmoPlugin)));
  4096. add_gizmo_plugin(Ref<VehicleWheelSpatialGizmoPlugin>(memnew(VehicleWheelSpatialGizmoPlugin)));
  4097. add_gizmo_plugin(Ref<VisibilityNotifierGizmoPlugin>(memnew(VisibilityNotifierGizmoPlugin)));
  4098. add_gizmo_plugin(Ref<ParticlesGizmoPlugin>(memnew(ParticlesGizmoPlugin)));
  4099. add_gizmo_plugin(Ref<ReflectionProbeGizmoPlugin>(memnew(ReflectionProbeGizmoPlugin)));
  4100. add_gizmo_plugin(Ref<GIProbeGizmoPlugin>(memnew(GIProbeGizmoPlugin)));
  4101. add_gizmo_plugin(Ref<BakedIndirectLightGizmoPlugin>(memnew(BakedIndirectLightGizmoPlugin)));
  4102. add_gizmo_plugin(Ref<CollisionShapeSpatialGizmoPlugin>(memnew(CollisionShapeSpatialGizmoPlugin)));
  4103. add_gizmo_plugin(Ref<CollisionPolygonSpatialGizmoPlugin>(memnew(CollisionPolygonSpatialGizmoPlugin)));
  4104. add_gizmo_plugin(Ref<NavigationMeshSpatialGizmoPlugin>(memnew(NavigationMeshSpatialGizmoPlugin)));
  4105. add_gizmo_plugin(Ref<JointSpatialGizmoPlugin>(memnew(JointSpatialGizmoPlugin)));
  4106. add_gizmo_plugin(Ref<PhysicalBoneSpatialGizmoPlugin>(memnew(PhysicalBoneSpatialGizmoPlugin)));
  4107. }
  4108. void SpatialEditor::_bind_methods() {
  4109. ClassDB::bind_method("_unhandled_key_input", &SpatialEditor::_unhandled_key_input);
  4110. ClassDB::bind_method("_node_removed", &SpatialEditor::_node_removed);
  4111. ClassDB::bind_method("_menu_item_pressed", &SpatialEditor::_menu_item_pressed);
  4112. ClassDB::bind_method("_menu_gizmo_toggled", &SpatialEditor::_menu_gizmo_toggled);
  4113. ClassDB::bind_method("_menu_item_toggled", &SpatialEditor::_menu_item_toggled);
  4114. ClassDB::bind_method("_xform_dialog_action", &SpatialEditor::_xform_dialog_action);
  4115. ClassDB::bind_method("_get_editor_data", &SpatialEditor::_get_editor_data);
  4116. ClassDB::bind_method("_request_gizmo", &SpatialEditor::_request_gizmo);
  4117. ClassDB::bind_method("_toggle_maximize_view", &SpatialEditor::_toggle_maximize_view);
  4118. ClassDB::bind_method("_refresh_menu_icons", &SpatialEditor::_refresh_menu_icons);
  4119. ADD_SIGNAL(MethodInfo("transform_key_request"));
  4120. ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
  4121. }
  4122. void SpatialEditor::clear() {
  4123. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  4124. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  4125. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500.0));
  4126. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4127. viewports[i]->reset();
  4128. }
  4129. VisualServer::get_singleton()->instance_set_visible(origin_instance, true);
  4130. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  4131. for (int i = 0; i < 3; ++i) {
  4132. if (grid_enable[i]) {
  4133. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], true);
  4134. grid_visible[i] = true;
  4135. }
  4136. }
  4137. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4138. viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(SpatialEditorViewport::VIEW_AUDIO_LISTENER), i == 0);
  4139. viewports[i]->viewport->set_as_audio_listener(i == 0);
  4140. }
  4141. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true);
  4142. }
  4143. SpatialEditor::SpatialEditor(EditorNode *p_editor) {
  4144. gizmo.visible = true;
  4145. gizmo.scale = 1.0;
  4146. viewport_environment = Ref<Environment>(memnew(Environment));
  4147. undo_redo = p_editor->get_undo_redo();
  4148. VBoxContainer *vbc = this;
  4149. custom_camera = NULL;
  4150. singleton = this;
  4151. editor = p_editor;
  4152. editor_selection = editor->get_editor_selection();
  4153. editor_selection->add_editor_plugin(this);
  4154. snap_enabled = false;
  4155. snap_key_enabled = false;
  4156. tool_mode = TOOL_MODE_SELECT;
  4157. hbc_menu = memnew(HBoxContainer);
  4158. vbc->add_child(hbc_menu);
  4159. Vector<Variant> button_binds;
  4160. button_binds.resize(1);
  4161. String sct;
  4162. tool_button[TOOL_MODE_SELECT] = memnew(ToolButton);
  4163. hbc_menu->add_child(tool_button[TOOL_MODE_SELECT]);
  4164. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  4165. tool_button[TOOL_MODE_SELECT]->set_flat(true);
  4166. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  4167. button_binds.write[0] = MENU_TOOL_SELECT;
  4168. tool_button[TOOL_MODE_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4169. tool_button[TOOL_MODE_SELECT]->set_tooltip(TTR("Select Mode (Q)") + "\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Drag: Rotate\nAlt+Drag: Move\nAlt+RMB: Depth list selection"));
  4170. hbc_menu->add_child(memnew(VSeparator));
  4171. tool_button[TOOL_MODE_MOVE] = memnew(ToolButton);
  4172. hbc_menu->add_child(tool_button[TOOL_MODE_MOVE]);
  4173. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  4174. tool_button[TOOL_MODE_MOVE]->set_flat(true);
  4175. button_binds.write[0] = MENU_TOOL_MOVE;
  4176. tool_button[TOOL_MODE_MOVE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4177. tool_button[TOOL_MODE_MOVE]->set_tooltip(TTR("Move Mode (W)"));
  4178. tool_button[TOOL_MODE_ROTATE] = memnew(ToolButton);
  4179. hbc_menu->add_child(tool_button[TOOL_MODE_ROTATE]);
  4180. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  4181. tool_button[TOOL_MODE_ROTATE]->set_flat(true);
  4182. button_binds.write[0] = MENU_TOOL_ROTATE;
  4183. tool_button[TOOL_MODE_ROTATE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4184. tool_button[TOOL_MODE_ROTATE]->set_tooltip(TTR("Rotate Mode (E)"));
  4185. tool_button[TOOL_MODE_SCALE] = memnew(ToolButton);
  4186. hbc_menu->add_child(tool_button[TOOL_MODE_SCALE]);
  4187. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  4188. tool_button[TOOL_MODE_SCALE]->set_flat(true);
  4189. button_binds.write[0] = MENU_TOOL_SCALE;
  4190. tool_button[TOOL_MODE_SCALE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4191. tool_button[TOOL_MODE_SCALE]->set_tooltip(TTR("Scale Mode (R)"));
  4192. hbc_menu->add_child(memnew(VSeparator));
  4193. tool_button[TOOL_MODE_LIST_SELECT] = memnew(ToolButton);
  4194. hbc_menu->add_child(tool_button[TOOL_MODE_LIST_SELECT]);
  4195. tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true);
  4196. tool_button[TOOL_MODE_LIST_SELECT]->set_flat(true);
  4197. button_binds.write[0] = MENU_TOOL_LIST_SELECT;
  4198. tool_button[TOOL_MODE_LIST_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4199. tool_button[TOOL_MODE_LIST_SELECT]->set_tooltip(TTR("Show a list of all objects at the position clicked\n(same as Alt+RMB in select mode)."));
  4200. tool_button[TOOL_LOCK_SELECTED] = memnew(ToolButton);
  4201. hbc_menu->add_child(tool_button[TOOL_LOCK_SELECTED]);
  4202. button_binds.write[0] = MENU_LOCK_SELECTED;
  4203. tool_button[TOOL_LOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4204. tool_button[TOOL_LOCK_SELECTED]->set_tooltip(TTR("Lock the selected object in place (can't be moved)."));
  4205. tool_button[TOOL_UNLOCK_SELECTED] = memnew(ToolButton);
  4206. hbc_menu->add_child(tool_button[TOOL_UNLOCK_SELECTED]);
  4207. button_binds.write[0] = MENU_UNLOCK_SELECTED;
  4208. tool_button[TOOL_UNLOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4209. tool_button[TOOL_UNLOCK_SELECTED]->set_tooltip(TTR("Unlock the selected object (can be moved)."));
  4210. hbc_menu->add_child(memnew(VSeparator));
  4211. tool_option_button[TOOL_OPT_LOCAL_COORDS] = memnew(ToolButton);
  4212. hbc_menu->add_child(tool_option_button[TOOL_OPT_LOCAL_COORDS]);
  4213. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_toggle_mode(true);
  4214. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_flat(true);
  4215. button_binds.write[0] = MENU_TOOL_LOCAL_COORDS;
  4216. tool_option_button[TOOL_OPT_LOCAL_COORDS]->connect("toggled", this, "_menu_item_toggled", button_binds);
  4217. ED_SHORTCUT("spatial_editor/local_coords", TTR("Local Coords"), KEY_T);
  4218. sct = ED_GET_SHORTCUT("spatial_editor/local_coords").ptr()->get_as_text();
  4219. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_tooltip(vformat(TTR("Local Space Mode (%s)"), sct));
  4220. tool_option_button[TOOL_OPT_USE_SNAP] = memnew(ToolButton);
  4221. hbc_menu->add_child(tool_option_button[TOOL_OPT_USE_SNAP]);
  4222. tool_option_button[TOOL_OPT_USE_SNAP]->set_toggle_mode(true);
  4223. tool_option_button[TOOL_OPT_USE_SNAP]->set_flat(true);
  4224. button_binds.write[0] = MENU_TOOL_USE_SNAP;
  4225. tool_option_button[TOOL_OPT_USE_SNAP]->connect("toggled", this, "_menu_item_toggled", button_binds);
  4226. ED_SHORTCUT("spatial_editor/snap", TTR("Snap"), KEY_Y);
  4227. sct = ED_GET_SHORTCUT("spatial_editor/snap").ptr()->get_as_text();
  4228. tool_option_button[TOOL_OPT_USE_SNAP]->set_tooltip(vformat(TTR("Snap Mode (%s)"), sct));
  4229. hbc_menu->add_child(memnew(VSeparator));
  4230. // Drag and drop support;
  4231. preview_node = memnew(Spatial);
  4232. preview_bounds = AABB();
  4233. ED_SHORTCUT("spatial_editor/bottom_view", TTR("Bottom View"), KEY_MASK_ALT + KEY_KP_7);
  4234. ED_SHORTCUT("spatial_editor/top_view", TTR("Top View"), KEY_KP_7);
  4235. ED_SHORTCUT("spatial_editor/rear_view", TTR("Rear View"), KEY_MASK_ALT + KEY_KP_1);
  4236. ED_SHORTCUT("spatial_editor/front_view", TTR("Front View"), KEY_KP_1);
  4237. ED_SHORTCUT("spatial_editor/left_view", TTR("Left View"), KEY_MASK_ALT + KEY_KP_3);
  4238. ED_SHORTCUT("spatial_editor/right_view", TTR("Right View"), KEY_KP_3);
  4239. ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTR("Switch Perspective/Orthogonal view"), KEY_KP_5);
  4240. ED_SHORTCUT("spatial_editor/insert_anim_key", TTR("Insert Animation Key"), KEY_K);
  4241. ED_SHORTCUT("spatial_editor/focus_origin", TTR("Focus Origin"), KEY_O);
  4242. ED_SHORTCUT("spatial_editor/focus_selection", TTR("Focus Selection"), KEY_F);
  4243. ED_SHORTCUT("spatial_editor/align_selection_with_view", TTR("Align Selection With View"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_F);
  4244. ED_SHORTCUT("spatial_editor/tool_select", TTR("Tool Select"), KEY_Q);
  4245. ED_SHORTCUT("spatial_editor/tool_move", TTR("Tool Move"), KEY_W);
  4246. ED_SHORTCUT("spatial_editor/tool_rotate", TTR("Tool Rotate"), KEY_E);
  4247. ED_SHORTCUT("spatial_editor/tool_scale", TTR("Tool Scale"), KEY_R);
  4248. ED_SHORTCUT("spatial_editor/freelook_toggle", TTR("Toggle Freelook"), KEY_MASK_SHIFT + KEY_F);
  4249. PopupMenu *p;
  4250. transform_menu = memnew(MenuButton);
  4251. transform_menu->set_text(TTR("Transform"));
  4252. transform_menu->set_switch_on_hover(true);
  4253. hbc_menu->add_child(transform_menu);
  4254. p = transform_menu->get_popup();
  4255. p->add_shortcut(ED_SHORTCUT("spatial_editor/snap_to_floor", TTR("Snap object to floor"), KEY_PAGEDOWN), MENU_SNAP_TO_FLOOR);
  4256. p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap", TTR("Configure Snap...")), MENU_TRANSFORM_CONFIGURE_SNAP);
  4257. p->add_separator();
  4258. p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog", TTR("Transform Dialog...")), MENU_TRANSFORM_DIALOG);
  4259. p->connect("id_pressed", this, "_menu_item_pressed");
  4260. view_menu = memnew(MenuButton);
  4261. view_menu->set_text(TTR("View"));
  4262. view_menu->set_switch_on_hover(true);
  4263. hbc_menu->add_child(view_menu);
  4264. p = view_menu->get_popup();
  4265. accept = memnew(AcceptDialog);
  4266. editor->get_gui_base()->add_child(accept);
  4267. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport", TTR("1 Viewport"), KEY_MASK_CMD + KEY_1), MENU_VIEW_USE_1_VIEWPORT);
  4268. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports", TTR("2 Viewports"), KEY_MASK_CMD + KEY_2), MENU_VIEW_USE_2_VIEWPORTS);
  4269. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports_alt", TTR("2 Viewports (Alt)"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_2), MENU_VIEW_USE_2_VIEWPORTS_ALT);
  4270. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports", TTR("3 Viewports"), KEY_MASK_CMD + KEY_3), MENU_VIEW_USE_3_VIEWPORTS);
  4271. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports_alt", TTR("3 Viewports (Alt)"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_3), MENU_VIEW_USE_3_VIEWPORTS_ALT);
  4272. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KEY_MASK_CMD + KEY_4), MENU_VIEW_USE_4_VIEWPORTS);
  4273. p->add_separator();
  4274. p->add_submenu_item(TTR("Gizmos"), "GizmosMenu");
  4275. p->add_separator();
  4276. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN);
  4277. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid")), MENU_VIEW_GRID);
  4278. p->add_separator();
  4279. p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings")), MENU_VIEW_CAMERA_SETTINGS);
  4280. p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true);
  4281. p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true);
  4282. p->connect("id_pressed", this, "_menu_item_pressed");
  4283. gizmos_menu = memnew(PopupMenu);
  4284. p->add_child(gizmos_menu);
  4285. gizmos_menu->set_name("GizmosMenu");
  4286. gizmos_menu->set_hide_on_checkable_item_selection(false);
  4287. gizmos_menu->connect("id_pressed", this, "_menu_gizmo_toggled");
  4288. /* REST OF MENU */
  4289. palette_split = memnew(HSplitContainer);
  4290. palette_split->set_v_size_flags(SIZE_EXPAND_FILL);
  4291. vbc->add_child(palette_split);
  4292. shader_split = memnew(VSplitContainer);
  4293. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  4294. palette_split->add_child(shader_split);
  4295. viewport_base = memnew(SpatialEditorViewportContainer);
  4296. shader_split->add_child(viewport_base);
  4297. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  4298. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4299. viewports[i] = memnew(SpatialEditorViewport(this, editor, i));
  4300. viewports[i]->connect("toggle_maximize_view", this, "_toggle_maximize_view");
  4301. viewports[i]->assign_pending_data_pointers(preview_node, &preview_bounds, accept);
  4302. viewport_base->add_child(viewports[i]);
  4303. }
  4304. /* SNAP DIALOG */
  4305. snap_dialog = memnew(ConfirmationDialog);
  4306. snap_dialog->set_title(TTR("Snap Settings"));
  4307. add_child(snap_dialog);
  4308. VBoxContainer *snap_dialog_vbc = memnew(VBoxContainer);
  4309. snap_dialog->add_child(snap_dialog_vbc);
  4310. snap_translate = memnew(LineEdit);
  4311. snap_translate->set_text("1");
  4312. snap_dialog_vbc->add_margin_child(TTR("Translate Snap:"), snap_translate);
  4313. snap_rotate = memnew(LineEdit);
  4314. snap_rotate->set_text("5");
  4315. snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):"), snap_rotate);
  4316. snap_scale = memnew(LineEdit);
  4317. snap_scale->set_text("5");
  4318. snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):"), snap_scale);
  4319. /* SETTINGS DIALOG */
  4320. settings_dialog = memnew(ConfirmationDialog);
  4321. settings_dialog->set_title(TTR("Viewport Settings"));
  4322. add_child(settings_dialog);
  4323. settings_vbc = memnew(VBoxContainer);
  4324. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  4325. settings_dialog->add_child(settings_vbc);
  4326. settings_fov = memnew(SpinBox);
  4327. settings_fov->set_max(MAX_FOV);
  4328. settings_fov->set_min(MIN_FOV);
  4329. settings_fov->set_step(0.01);
  4330. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  4331. settings_vbc->add_margin_child(TTR("Perspective FOV (deg.):"), settings_fov);
  4332. settings_znear = memnew(SpinBox);
  4333. settings_znear->set_max(MAX_Z);
  4334. settings_znear->set_min(MIN_Z);
  4335. settings_znear->set_step(0.01);
  4336. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  4337. settings_vbc->add_margin_child(TTR("View Z-Near:"), settings_znear);
  4338. settings_zfar = memnew(SpinBox);
  4339. settings_zfar->set_max(MAX_Z);
  4340. settings_zfar->set_min(MIN_Z);
  4341. settings_zfar->set_step(0.01);
  4342. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500));
  4343. settings_vbc->add_margin_child(TTR("View Z-Far:"), settings_zfar);
  4344. /* XFORM DIALOG */
  4345. xform_dialog = memnew(ConfirmationDialog);
  4346. xform_dialog->set_title(TTR("Transform Change"));
  4347. add_child(xform_dialog);
  4348. VBoxContainer *xform_vbc = memnew(VBoxContainer);
  4349. xform_dialog->add_child(xform_vbc);
  4350. Label *l = memnew(Label);
  4351. l->set_text(TTR("Translate:"));
  4352. xform_vbc->add_child(l);
  4353. HBoxContainer *xform_hbc = memnew(HBoxContainer);
  4354. xform_vbc->add_child(xform_hbc);
  4355. for (int i = 0; i < 3; i++) {
  4356. xform_translate[i] = memnew(LineEdit);
  4357. xform_translate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4358. xform_hbc->add_child(xform_translate[i]);
  4359. }
  4360. l = memnew(Label);
  4361. l->set_text(TTR("Rotate (deg.):"));
  4362. xform_vbc->add_child(l);
  4363. xform_hbc = memnew(HBoxContainer);
  4364. xform_vbc->add_child(xform_hbc);
  4365. for (int i = 0; i < 3; i++) {
  4366. xform_rotate[i] = memnew(LineEdit);
  4367. xform_rotate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4368. xform_hbc->add_child(xform_rotate[i]);
  4369. }
  4370. l = memnew(Label);
  4371. l->set_text(TTR("Scale (ratio):"));
  4372. xform_vbc->add_child(l);
  4373. xform_hbc = memnew(HBoxContainer);
  4374. xform_vbc->add_child(xform_hbc);
  4375. for (int i = 0; i < 3; i++) {
  4376. xform_scale[i] = memnew(LineEdit);
  4377. xform_scale[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4378. xform_hbc->add_child(xform_scale[i]);
  4379. }
  4380. l = memnew(Label);
  4381. l->set_text(TTR("Transform Type"));
  4382. xform_vbc->add_child(l);
  4383. xform_type = memnew(OptionButton);
  4384. xform_type->set_h_size_flags(SIZE_EXPAND_FILL);
  4385. xform_type->add_item(TTR("Pre"));
  4386. xform_type->add_item(TTR("Post"));
  4387. xform_vbc->add_child(xform_type);
  4388. xform_dialog->connect("confirmed", this, "_xform_dialog_action");
  4389. scenario_debug = VisualServer::SCENARIO_DEBUG_DISABLED;
  4390. selected = NULL;
  4391. set_process_unhandled_key_input(true);
  4392. add_to_group("_spatial_editor_group");
  4393. EDITOR_DEF("editors/3d/manipulator_gizmo_size", 80);
  4394. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d/manipulator_gizmo_size", PROPERTY_HINT_RANGE, "16,1024,1"));
  4395. EDITOR_DEF("editors/3d/manipulator_gizmo_opacity", 0.2);
  4396. over_gizmo_handle = -1;
  4397. }
  4398. SpatialEditor::~SpatialEditor() {
  4399. memdelete(preview_node);
  4400. }
  4401. void SpatialEditorPlugin::make_visible(bool p_visible) {
  4402. if (p_visible) {
  4403. spatial_editor->show();
  4404. spatial_editor->set_process(true);
  4405. } else {
  4406. spatial_editor->hide();
  4407. spatial_editor->set_process(false);
  4408. }
  4409. }
  4410. void SpatialEditorPlugin::edit(Object *p_object) {
  4411. spatial_editor->edit(Object::cast_to<Spatial>(p_object));
  4412. }
  4413. bool SpatialEditorPlugin::handles(Object *p_object) const {
  4414. return p_object->is_class("Spatial");
  4415. }
  4416. Dictionary SpatialEditorPlugin::get_state() const {
  4417. return spatial_editor->get_state();
  4418. }
  4419. void SpatialEditorPlugin::set_state(const Dictionary &p_state) {
  4420. spatial_editor->set_state(p_state);
  4421. }
  4422. void SpatialEditor::snap_cursor_to_plane(const Plane &p_plane) {
  4423. //cursor.pos=p_plane.project(cursor.pos);
  4424. }
  4425. Vector3 SpatialEditor::snap_point(Vector3 p_target, Vector3 p_start) const {
  4426. if (is_snap_enabled()) {
  4427. p_target.x = Math::snap_scalar(0.0, get_translate_snap(), p_target.x);
  4428. p_target.y = Math::snap_scalar(0.0, get_translate_snap(), p_target.y);
  4429. p_target.z = Math::snap_scalar(0.0, get_translate_snap(), p_target.z);
  4430. }
  4431. return p_target;
  4432. }
  4433. void SpatialEditorPlugin::_bind_methods() {
  4434. ClassDB::bind_method("snap_cursor_to_plane", &SpatialEditorPlugin::snap_cursor_to_plane);
  4435. }
  4436. void SpatialEditorPlugin::snap_cursor_to_plane(const Plane &p_plane) {
  4437. spatial_editor->snap_cursor_to_plane(p_plane);
  4438. }
  4439. void SpatialEditor::add_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin) {
  4440. ERR_FAIL_NULL(p_plugin.ptr());
  4441. gizmo_plugins.push_back(p_plugin);
  4442. _update_gizmos_menu();
  4443. SpatialEditor::get_singleton()->update_all_gizmos();
  4444. }
  4445. void SpatialEditor::remove_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin) {
  4446. gizmo_plugins.erase(p_plugin);
  4447. _update_gizmos_menu();
  4448. }
  4449. SpatialEditorPlugin::SpatialEditorPlugin(EditorNode *p_node) {
  4450. editor = p_node;
  4451. spatial_editor = memnew(SpatialEditor(p_node));
  4452. spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4453. editor->get_viewport()->add_child(spatial_editor);
  4454. spatial_editor->hide();
  4455. spatial_editor->connect("transform_key_request", editor, "_transform_keyed");
  4456. }
  4457. SpatialEditorPlugin::~SpatialEditorPlugin() {
  4458. }
  4459. void EditorSpatialGizmoPlugin::create_material(const String &p_name, const Color &p_color, bool p_billboard, bool p_on_top, bool p_use_vertex_color) {
  4460. Color instanced_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/instanced", Color(0.7, 0.7, 0.7, 0.6));
  4461. Vector<Ref<SpatialMaterial> > mats;
  4462. for (int i = 0; i < 4; i++) {
  4463. bool selected = i % 2 == 1;
  4464. bool instanced = i < 2;
  4465. Ref<SpatialMaterial> material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  4466. Color color = instanced ? instanced_color : p_color;
  4467. if (!selected) {
  4468. color.a *= 0.3;
  4469. }
  4470. material->set_albedo(color);
  4471. material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4472. material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4473. material->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MIN + 1);
  4474. if (p_use_vertex_color) {
  4475. material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  4476. material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  4477. }
  4478. if (p_billboard) {
  4479. material->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  4480. }
  4481. if (p_on_top && selected) {
  4482. material->set_on_top_of_alpha();
  4483. }
  4484. mats.push_back(material);
  4485. }
  4486. materials[p_name] = mats;
  4487. }
  4488. void EditorSpatialGizmoPlugin::create_icon_material(const String &p_name, const Ref<Texture> &p_texture, bool p_on_top, const Color &p_albedo) {
  4489. Color instanced_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/instanced", Color(0.7, 0.7, 0.7, 0.6));
  4490. Vector<Ref<SpatialMaterial> > icons;
  4491. for (int i = 0; i < 4; i++) {
  4492. bool selected = i % 2 == 1;
  4493. bool instanced = i < 2;
  4494. Ref<SpatialMaterial> icon = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  4495. Color color = instanced ? instanced_color : p_albedo;
  4496. if (!selected) {
  4497. color.a *= 0.85;
  4498. }
  4499. icon->set_albedo(color);
  4500. icon->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4501. icon->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  4502. icon->set_depth_draw_mode(SpatialMaterial::DEPTH_DRAW_DISABLED);
  4503. icon->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4504. icon->set_texture(SpatialMaterial::TEXTURE_ALBEDO, p_texture);
  4505. icon->set_flag(SpatialMaterial::FLAG_FIXED_SIZE, true);
  4506. icon->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  4507. icon->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MIN);
  4508. if (p_on_top && selected) {
  4509. icon->set_on_top_of_alpha();
  4510. }
  4511. icons.push_back(icon);
  4512. }
  4513. materials[p_name] = icons;
  4514. }
  4515. void EditorSpatialGizmoPlugin::create_handle_material(const String &p_name, bool p_billboard) {
  4516. Ref<SpatialMaterial> handle_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  4517. handle_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  4518. handle_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4519. handle_material->set_flag(SpatialMaterial::FLAG_USE_POINT_SIZE, true);
  4520. Ref<Texture> handle_t = SpatialEditor::get_singleton()->get_icon("Editor3DHandle", "EditorIcons");
  4521. handle_material->set_point_size(handle_t->get_width());
  4522. handle_material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, handle_t);
  4523. handle_material->set_albedo(Color(1, 1, 1));
  4524. handle_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4525. handle_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  4526. handle_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  4527. handle_material->set_on_top_of_alpha();
  4528. if (p_billboard) {
  4529. handle_material->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  4530. handle_material->set_on_top_of_alpha();
  4531. }
  4532. materials[p_name] = Vector<Ref<SpatialMaterial> >();
  4533. materials[p_name].push_back(handle_material);
  4534. }
  4535. void EditorSpatialGizmoPlugin::add_material(const String &p_name, Ref<SpatialMaterial> p_material) {
  4536. materials[p_name] = Vector<Ref<SpatialMaterial> >();
  4537. materials[p_name].push_back(p_material);
  4538. }
  4539. Ref<SpatialMaterial> EditorSpatialGizmoPlugin::get_material(const String &p_name, const Ref<EditorSpatialGizmo> &p_gizmo) {
  4540. ERR_FAIL_COND_V(!materials.has(p_name), Ref<SpatialMaterial>());
  4541. ERR_FAIL_COND_V(materials[p_name].size() == 0, Ref<SpatialMaterial>());
  4542. if (p_gizmo.is_null() || materials[p_name].size() == 1) return materials[p_name][0];
  4543. int index = (p_gizmo->is_selected() ? 1 : 0) + (p_gizmo->is_editable() ? 2 : 0);
  4544. Ref<SpatialMaterial> mat = materials[p_name][index];
  4545. if (current_state == ON_TOP && p_gizmo->is_selected()) {
  4546. mat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true);
  4547. } else {
  4548. mat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, false);
  4549. }
  4550. return mat;
  4551. }
  4552. String EditorSpatialGizmoPlugin::get_name() const {
  4553. if (get_script_instance() && get_script_instance()->has_method("get_name")) {
  4554. return get_script_instance()->call("get_name");
  4555. }
  4556. return TTR("Nameless gizmo");
  4557. }
  4558. Ref<EditorSpatialGizmo> EditorSpatialGizmoPlugin::get_gizmo(Spatial *p_spatial) {
  4559. if (get_script_instance() && get_script_instance()->has_method("get_gizmo")) {
  4560. return get_script_instance()->call("get_gizmo", p_spatial);
  4561. }
  4562. Ref<EditorSpatialGizmo> ref = create_gizmo(p_spatial);
  4563. if (ref.is_null()) return ref;
  4564. ref->set_plugin(this);
  4565. ref->set_spatial_node(p_spatial);
  4566. ref->set_hidden(current_state == HIDDEN);
  4567. current_gizmos.push_back(ref.ptr());
  4568. return ref;
  4569. }
  4570. void EditorSpatialGizmoPlugin::_bind_methods() {
  4571. #define GIZMO_REF PropertyInfo(Variant::OBJECT, "gizmo", PROPERTY_HINT_RESOURCE_TYPE, "EditorSpatialGizmo")
  4572. BIND_VMETHOD(MethodInfo(Variant::BOOL, "has_gizmo", PropertyInfo(Variant::OBJECT, "spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial")));
  4573. BIND_VMETHOD(MethodInfo(GIZMO_REF, "create_gizmo", PropertyInfo(Variant::OBJECT, "spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial")));
  4574. ClassDB::bind_method(D_METHOD("create_material", "name", "color", "billboard", "on_top", "use_vertex_color"), &EditorSpatialGizmoPlugin::create_material, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  4575. ClassDB::bind_method(D_METHOD("create_icon_material", "name", "texture", "on_top", "color"), &EditorSpatialGizmoPlugin::create_icon_material, DEFVAL(false), DEFVAL(Color(1, 1, 1, 1)));
  4576. ClassDB::bind_method(D_METHOD("create_handle_material", "name", "billboard"), &EditorSpatialGizmoPlugin::create_handle_material, DEFVAL(false));
  4577. ClassDB::bind_method(D_METHOD("add_material", "name", "material"), &EditorSpatialGizmoPlugin::add_material);
  4578. ClassDB::bind_method(D_METHOD("get_material", "name", "gizmo"), &EditorSpatialGizmoPlugin::get_material); //, DEFVAL(Ref<EditorSpatialGizmo>()));
  4579. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_name"));
  4580. BIND_VMETHOD(MethodInfo(Variant::BOOL, "can_be_hidden"));
  4581. BIND_VMETHOD(MethodInfo(Variant::BOOL, "is_selectable_when_hidden"));
  4582. BIND_VMETHOD(MethodInfo("redraw", GIZMO_REF));
  4583. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_handle_name", GIZMO_REF, PropertyInfo(Variant::INT, "index")));
  4584. MethodInfo hvget(Variant::NIL, "get_handle_value", GIZMO_REF, PropertyInfo(Variant::INT, "index"));
  4585. hvget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  4586. BIND_VMETHOD(hvget);
  4587. BIND_VMETHOD(MethodInfo("set_handle", GIZMO_REF, PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::VECTOR2, "point")));
  4588. MethodInfo cm = MethodInfo("commit_handle", GIZMO_REF, PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::NIL, "restore"), PropertyInfo(Variant::BOOL, "cancel"));
  4589. cm.default_arguments.push_back(false);
  4590. BIND_VMETHOD(cm);
  4591. BIND_VMETHOD(MethodInfo(Variant::BOOL, "is_handle_highlighted", GIZMO_REF, PropertyInfo(Variant::INT, "index")));
  4592. #undef GIZMO_REF
  4593. }
  4594. bool EditorSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  4595. if (get_script_instance() && get_script_instance()->has_method("has_gizmo")) {
  4596. return get_script_instance()->call("has_gizmo", p_spatial);
  4597. }
  4598. return false;
  4599. }
  4600. Ref<EditorSpatialGizmo> EditorSpatialGizmoPlugin::create_gizmo(Spatial *p_spatial) {
  4601. if (get_script_instance() && get_script_instance()->has_method("create_gizmo")) {
  4602. return get_script_instance()->call("create_gizmo", p_spatial);
  4603. }
  4604. Ref<EditorSpatialGizmo> ref;
  4605. if (has_gizmo(p_spatial)) ref.instance();
  4606. return ref;
  4607. }
  4608. bool EditorSpatialGizmoPlugin::can_be_hidden() const {
  4609. if (get_script_instance() && get_script_instance()->has_method("can_be_hidden")) {
  4610. return get_script_instance()->call("can_be_hidden");
  4611. }
  4612. return true;
  4613. }
  4614. bool EditorSpatialGizmoPlugin::is_selectable_when_hidden() const {
  4615. if (get_script_instance() && get_script_instance()->has_method("is_selectable_when_hidden")) {
  4616. return get_script_instance()->call("is_selectable_when_hidden");
  4617. }
  4618. return false;
  4619. }
  4620. void EditorSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  4621. if (get_script_instance() && get_script_instance()->has_method("redraw")) {
  4622. Ref<EditorSpatialGizmo> ref(p_gizmo);
  4623. get_script_instance()->call("redraw", ref);
  4624. }
  4625. }
  4626. String EditorSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  4627. if (get_script_instance() && get_script_instance()->has_method("get_handle_name")) {
  4628. return get_script_instance()->call("get_handle_name", p_gizmo, p_idx);
  4629. }
  4630. return "";
  4631. }
  4632. Variant EditorSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  4633. if (get_script_instance() && get_script_instance()->has_method("get_handle_value")) {
  4634. return get_script_instance()->call("get_handle_value", p_gizmo, p_idx);
  4635. }
  4636. return Variant();
  4637. }
  4638. void EditorSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  4639. if (get_script_instance() && get_script_instance()->has_method("set_handle")) {
  4640. get_script_instance()->call("set_handle", p_gizmo, p_idx, p_camera, p_point);
  4641. }
  4642. }
  4643. void EditorSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  4644. if (get_script_instance() && get_script_instance()->has_method("commit_handle")) {
  4645. get_script_instance()->call("commit_handle", p_gizmo, p_idx, p_restore, p_cancel);
  4646. }
  4647. }
  4648. bool EditorSpatialGizmoPlugin::is_handle_highlighted(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  4649. if (get_script_instance() && get_script_instance()->has_method("is_handle_highlighted")) {
  4650. return get_script_instance()->call("is_handle_highlighted", p_gizmo, p_idx);
  4651. }
  4652. return false;
  4653. }
  4654. void EditorSpatialGizmoPlugin::set_state(int p_state) {
  4655. current_state = p_state;
  4656. for (int i = 0; i < current_gizmos.size(); ++i) {
  4657. current_gizmos[i]->set_hidden(current_state == HIDDEN);
  4658. }
  4659. }
  4660. void EditorSpatialGizmoPlugin::unregister_gizmo(EditorSpatialGizmo *p_gizmo) {
  4661. current_gizmos.erase(p_gizmo);
  4662. }
  4663. EditorSpatialGizmoPlugin::EditorSpatialGizmoPlugin() {
  4664. current_state = VISIBLE;
  4665. }
  4666. EditorSpatialGizmoPlugin::~EditorSpatialGizmoPlugin() {
  4667. for (int i = 0; i < current_gizmos.size(); ++i) {
  4668. current_gizmos[i]->set_plugin(NULL);
  4669. current_gizmos[i]->get_spatial_node()->set_gizmo(NULL);
  4670. }
  4671. SpatialEditor::get_singleton()->update_all_gizmos();
  4672. }