spatial_editor_plugin.cpp 175 KB

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