spatial_editor_plugin.cpp 174 KB

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