spatial_editor_plugin.cpp 204 KB

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