2
0

spatial_editor_plugin.cpp 220 KB

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