spatial_editor_plugin.cpp 175 KB

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