spatial_editor_plugin.cpp 160 KB

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