spatial_editor_plugin.cpp 175 KB

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