editor_node.cpp 259 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374
  1. /**************************************************************************/
  2. /* editor_node.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "editor_node.h"
  31. #include "core/bind/core_bind.h"
  32. #include "core/class_db.h"
  33. #include "core/io/config_file.h"
  34. #include "core/io/image_loader.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/io/resource_saver.h"
  37. #include "core/io/stream_peer_ssl.h"
  38. #include "core/message_queue.h"
  39. #include "core/os/file_access.h"
  40. #include "core/os/input.h"
  41. #include "core/os/keyboard.h"
  42. #include "core/os/os.h"
  43. #include "core/path_remap.h"
  44. #include "core/print_string.h"
  45. #include "core/project_settings.h"
  46. #include "core/translation.h"
  47. #include "core/version.h"
  48. #include "main/input_default.h"
  49. #include "main/main.h"
  50. #include "scene/gui/center_container.h"
  51. #include "scene/gui/control.h"
  52. #include "scene/gui/dialogs.h"
  53. #include "scene/gui/file_dialog.h"
  54. #include "scene/gui/link_button.h"
  55. #include "scene/gui/menu_button.h"
  56. #include "scene/gui/panel.h"
  57. #include "scene/gui/panel_container.h"
  58. #include "scene/gui/split_container.h"
  59. #include "scene/gui/tab_container.h"
  60. #include "scene/gui/tabs.h"
  61. #include "scene/gui/texture_progress.h"
  62. #include "scene/gui/tool_button.h"
  63. #include "scene/resources/packed_scene.h"
  64. #include "servers/navigation_2d_server.h"
  65. #include "servers/navigation_server.h"
  66. #include "servers/physics_2d_server.h"
  67. #include "editor/audio_stream_preview.h"
  68. #include "editor/dependency_editor.h"
  69. #include "editor/editor_about.h"
  70. #include "editor/editor_audio_buses.h"
  71. #include "editor/editor_export.h"
  72. #include "editor/editor_feature_profile.h"
  73. #include "editor/editor_file_system.h"
  74. #include "editor/editor_help.h"
  75. #include "editor/editor_inspector.h"
  76. #include "editor/editor_layouts_dialog.h"
  77. #include "editor/editor_log.h"
  78. #include "editor/editor_plugin.h"
  79. #include "editor/editor_properties.h"
  80. #include "editor/editor_property_name_processor.h"
  81. #include "editor/editor_quick_open.h"
  82. #include "editor/editor_resource_picker.h"
  83. #include "editor/editor_resource_preview.h"
  84. #include "editor/editor_run_native.h"
  85. #include "editor/editor_run_script.h"
  86. #include "editor/editor_scale.h"
  87. #include "editor/editor_settings.h"
  88. #include "editor/editor_spin_slider.h"
  89. #include "editor/editor_themes.h"
  90. #include "editor/export_template_manager.h"
  91. #include "editor/fileserver/editor_file_server.h"
  92. #include "editor/filesystem_dock.h"
  93. #include "editor/import/editor_import_collada.h"
  94. #include "editor/import/resource_importer_bitmask.h"
  95. #include "editor/import/resource_importer_csv_translation.h"
  96. #include "editor/import/resource_importer_image.h"
  97. #include "editor/import/resource_importer_layered_texture.h"
  98. #include "editor/import/resource_importer_obj.h"
  99. #include "editor/import/resource_importer_scene.h"
  100. #include "editor/import/resource_importer_texture.h"
  101. #include "editor/import/resource_importer_texture_atlas.h"
  102. #include "editor/import/resource_importer_wav.h"
  103. #include "editor/import_dock.h"
  104. #include "editor/multi_node_edit.h"
  105. #include "editor/node_dock.h"
  106. #include "editor/pane_drag.h"
  107. #include "editor/plugin_config_dialog.h"
  108. #include "editor/plugins/animation_blend_space_1d_editor.h"
  109. #include "editor/plugins/animation_blend_space_2d_editor.h"
  110. #include "editor/plugins/animation_blend_tree_editor_plugin.h"
  111. #include "editor/plugins/animation_player_editor_plugin.h"
  112. #include "editor/plugins/animation_state_machine_editor.h"
  113. #include "editor/plugins/animation_tree_editor_plugin.h"
  114. #include "editor/plugins/animation_tree_player_editor_plugin.h"
  115. #include "editor/plugins/asset_library_editor_plugin.h"
  116. #include "editor/plugins/audio_stream_editor_plugin.h"
  117. #include "editor/plugins/baked_lightmap_editor_plugin.h"
  118. #include "editor/plugins/bit_map_editor_plugin.h"
  119. #include "editor/plugins/camera_editor_plugin.h"
  120. #include "editor/plugins/canvas_item_editor_plugin.h"
  121. #include "editor/plugins/collision_polygon_2d_editor_plugin.h"
  122. #include "editor/plugins/collision_polygon_editor_plugin.h"
  123. #include "editor/plugins/collision_shape_2d_editor_plugin.h"
  124. #include "editor/plugins/cpu_particles_2d_editor_plugin.h"
  125. #include "editor/plugins/cpu_particles_editor_plugin.h"
  126. #include "editor/plugins/curve_editor_plugin.h"
  127. #include "editor/plugins/editor_preview_plugins.h"
  128. #include "editor/plugins/gi_probe_editor_plugin.h"
  129. #include "editor/plugins/gradient_editor_plugin.h"
  130. #include "editor/plugins/gradient_texture_2d_editor_plugin.h"
  131. #include "editor/plugins/item_list_editor_plugin.h"
  132. #include "editor/plugins/light_occluder_2d_editor_plugin.h"
  133. #include "editor/plugins/line_2d_editor_plugin.h"
  134. #include "editor/plugins/material_editor_plugin.h"
  135. #include "editor/plugins/merge_group_editor_plugin.h"
  136. #include "editor/plugins/mesh_editor_plugin.h"
  137. #include "editor/plugins/mesh_instance_editor_plugin.h"
  138. #include "editor/plugins/mesh_library_editor_plugin.h"
  139. #include "editor/plugins/multimesh_editor_plugin.h"
  140. #include "editor/plugins/navigation_polygon_editor_plugin.h"
  141. #include "editor/plugins/packed_scene_editor_plugin.h"
  142. #include "editor/plugins/particles_2d_editor_plugin.h"
  143. #include "editor/plugins/particles_editor_plugin.h"
  144. #include "editor/plugins/path_2d_editor_plugin.h"
  145. #include "editor/plugins/path_editor_plugin.h"
  146. #include "editor/plugins/physical_bone_plugin.h"
  147. #include "editor/plugins/polygon_2d_editor_plugin.h"
  148. #include "editor/plugins/ray_cast_2d_editor_plugin.h"
  149. #include "editor/plugins/resource_preloader_editor_plugin.h"
  150. #include "editor/plugins/room_manager_editor_plugin.h"
  151. #include "editor/plugins/root_motion_editor_plugin.h"
  152. #include "editor/plugins/script_editor_plugin.h"
  153. #include "editor/plugins/script_text_editor.h"
  154. #include "editor/plugins/shader_editor_plugin.h"
  155. #include "editor/plugins/skeleton_2d_editor_plugin.h"
  156. #include "editor/plugins/skeleton_editor_plugin.h"
  157. #include "editor/plugins/skeleton_ik_editor_plugin.h"
  158. #include "editor/plugins/spatial_editor_plugin.h"
  159. #include "editor/plugins/sprite_editor_plugin.h"
  160. #include "editor/plugins/sprite_frames_editor_plugin.h"
  161. #include "editor/plugins/style_box_editor_plugin.h"
  162. #include "editor/plugins/text_editor.h"
  163. #include "editor/plugins/texture_editor_plugin.h"
  164. #include "editor/plugins/texture_region_editor_plugin.h"
  165. #include "editor/plugins/theme_editor_plugin.h"
  166. #include "editor/plugins/tile_map_editor_plugin.h"
  167. #include "editor/plugins/tile_set_editor_plugin.h"
  168. #include "editor/plugins/version_control_editor_plugin.h"
  169. #include "editor/plugins/viewport_preview_editor_plugin.h"
  170. #include "editor/plugins/visual_shader_editor_plugin.h"
  171. #include "editor/progress_dialog.h"
  172. #include "editor/project_export.h"
  173. #include "editor/project_settings_editor.h"
  174. #include "editor/register_exporters.h"
  175. #include "editor/run_settings_dialog.h"
  176. #include "editor/script_editor_debugger.h"
  177. #include "editor/settings_config_dialog.h"
  178. #include <stdio.h>
  179. #include <stdlib.h>
  180. EditorNode *EditorNode::singleton = nullptr;
  181. // The metadata key used to store and retrieve the version text to copy to the clipboard.
  182. static const String META_TEXT_TO_COPY = "text_to_copy";
  183. void EditorNode::disambiguate_filenames(const Vector<String> p_full_paths, Vector<String> &r_filenames) {
  184. // Keep track of a list of "index sets," i.e. sets of indices
  185. // within disambiguated_scene_names which contain the same name.
  186. Vector<Set<int>> index_sets;
  187. Map<String, int> scene_name_to_set_index;
  188. for (int i = 0; i < r_filenames.size(); i++) {
  189. String scene_name = r_filenames[i];
  190. if (!scene_name_to_set_index.has(scene_name)) {
  191. index_sets.push_back(Set<int>());
  192. scene_name_to_set_index.insert(r_filenames[i], index_sets.size() - 1);
  193. }
  194. index_sets.write[scene_name_to_set_index[scene_name]].insert(i);
  195. }
  196. // For each index set with a size > 1, we need to disambiguate
  197. for (int i = 0; i < index_sets.size(); i++) {
  198. Set<int> iset = index_sets[i];
  199. while (iset.size() > 1) {
  200. // Append the parent folder to each scene name
  201. for (Set<int>::Element *E = iset.front(); E; E = E->next()) {
  202. int set_idx = E->get();
  203. String scene_name = r_filenames[set_idx];
  204. String full_path = p_full_paths[set_idx];
  205. // Get rid of file extensions and res:// prefixes
  206. if (scene_name.rfind(".") >= 0) {
  207. scene_name = scene_name.substr(0, scene_name.rfind("."));
  208. }
  209. if (full_path.begins_with("res://")) {
  210. full_path = full_path.substr(6);
  211. }
  212. if (full_path.rfind(".") >= 0) {
  213. full_path = full_path.substr(0, full_path.rfind("."));
  214. }
  215. int scene_name_size = scene_name.size();
  216. int full_path_size = full_path.size();
  217. int difference = full_path_size - scene_name_size;
  218. // Find just the parent folder of the current path and append it.
  219. // If the current name is foo.tscn, and the full path is /some/folder/foo.tscn
  220. // then slash_idx is the second '/', so that we select just "folder", and
  221. // append that to yield "folder/foo.tscn".
  222. if (difference > 0) {
  223. String parent = full_path.substr(0, difference);
  224. int slash_idx = parent.rfind("/");
  225. slash_idx = parent.rfind("/", slash_idx - 1);
  226. parent = slash_idx >= 0 ? parent.substr(slash_idx + 1) : parent;
  227. r_filenames.write[set_idx] = parent + r_filenames[set_idx];
  228. }
  229. }
  230. // Loop back through scene names and remove non-ambiguous names
  231. bool can_proceed = false;
  232. Set<int>::Element *E = iset.front();
  233. while (E) {
  234. String scene_name = r_filenames[E->get()];
  235. bool duplicate_found = false;
  236. for (Set<int>::Element *F = iset.front(); F; F = F->next()) {
  237. if (E->get() == F->get()) {
  238. continue;
  239. }
  240. String other_scene_name = r_filenames[F->get()];
  241. if (other_scene_name == scene_name) {
  242. duplicate_found = true;
  243. break;
  244. }
  245. }
  246. Set<int>::Element *to_erase = duplicate_found ? nullptr : E;
  247. // We need to check that we could actually append anymore names
  248. // if we wanted to for disambiguation. If we can't, then we have
  249. // to abort even with ambiguous names. We clean the full path
  250. // and the scene name first to remove extensions so that this
  251. // comparison actually works.
  252. String path = p_full_paths[E->get()];
  253. if (path.begins_with("res://")) {
  254. path = path.substr(6);
  255. }
  256. if (path.rfind(".") >= 0) {
  257. path = path.substr(0, path.rfind("."));
  258. }
  259. if (scene_name.rfind(".") >= 0) {
  260. scene_name = scene_name.substr(0, scene_name.rfind("."));
  261. }
  262. // We can proceed iff the full path is longer than the scene name,
  263. // meaning that there is at least one more parent folder we can
  264. // tack onto the name.
  265. can_proceed = can_proceed || (path.size() - scene_name.size()) >= 1;
  266. E = E->next();
  267. if (to_erase) {
  268. iset.erase(to_erase);
  269. }
  270. }
  271. if (!can_proceed) {
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. void EditorNode::_update_scene_tabs() {
  278. bool show_rb = EditorSettings::get_singleton()->get("interface/scene_tabs/show_script_button");
  279. OS::get_singleton()->global_menu_clear("_dock");
  280. // Get all scene names, which may be ambiguous
  281. Vector<String> disambiguated_scene_names;
  282. Vector<String> full_path_names;
  283. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  284. disambiguated_scene_names.push_back(editor_data.get_scene_title(i));
  285. full_path_names.push_back(editor_data.get_scene_path(i));
  286. }
  287. disambiguate_filenames(full_path_names, disambiguated_scene_names);
  288. scene_tabs->clear_tabs();
  289. Ref<Texture> script_icon = gui_base->get_icon("Script", "EditorIcons");
  290. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  291. Node *type_node = editor_data.get_edited_scene_root(i);
  292. Ref<Texture> icon;
  293. if (type_node) {
  294. icon = EditorNode::get_singleton()->get_object_icon(type_node, "Node");
  295. }
  296. int current = editor_data.get_edited_scene();
  297. bool unsaved = (i == current) ? saved_version != editor_data.get_undo_redo().get_version() : editor_data.get_scene_version(i) != 0;
  298. scene_tabs->add_tab(disambiguated_scene_names[i] + (unsaved ? "(*)" : ""), icon);
  299. OS::get_singleton()->global_menu_add_item("_dock", editor_data.get_scene_title(i) + (unsaved ? "(*)" : ""), GLOBAL_SCENE, i);
  300. if (show_rb && editor_data.get_scene_root_script(i).is_valid()) {
  301. scene_tabs->set_tab_right_button(i, script_icon);
  302. }
  303. }
  304. OS::get_singleton()->global_menu_add_separator("_dock");
  305. OS::get_singleton()->global_menu_add_item("_dock", TTR("New Window"), GLOBAL_NEW_WINDOW, Variant());
  306. scene_tabs->set_current_tab(editor_data.get_edited_scene());
  307. if (scene_tabs->get_offset_buttons_visible()) {
  308. // move add button to fixed position on the tabbar
  309. if (scene_tab_add->get_parent() == scene_tabs) {
  310. scene_tab_add->set_position(Point2(0, 0));
  311. scene_tabs->remove_child(scene_tab_add);
  312. tabbar_container->add_child(scene_tab_add);
  313. tabbar_container->move_child(scene_tab_add, 1);
  314. }
  315. } else {
  316. // move add button to after last tab
  317. if (scene_tab_add->get_parent() == tabbar_container) {
  318. tabbar_container->remove_child(scene_tab_add);
  319. scene_tabs->add_child(scene_tab_add);
  320. }
  321. Rect2 last_tab = Rect2();
  322. if (scene_tabs->get_tab_count() != 0) {
  323. last_tab = scene_tabs->get_tab_rect(scene_tabs->get_tab_count() - 1);
  324. }
  325. scene_tab_add->set_position(Point2(last_tab.get_position().x + last_tab.get_size().x + 3, last_tab.get_position().y));
  326. }
  327. }
  328. void EditorNode::_version_control_menu_option(int p_idx) {
  329. switch (vcs_actions_menu->get_item_id(p_idx)) {
  330. case RUN_VCS_SETTINGS: {
  331. VersionControlEditorPlugin::get_singleton()->popup_vcs_set_up_dialog(gui_base);
  332. } break;
  333. case RUN_VCS_SHUT_DOWN: {
  334. VersionControlEditorPlugin::get_singleton()->shut_down();
  335. } break;
  336. }
  337. }
  338. void EditorNode::_update_title() {
  339. const String appname = ProjectSettings::get_singleton()->get("application/config/name");
  340. String title = (appname.empty() ? TTR("Unnamed Project") : appname) + String(" - ") + VERSION_NAME;
  341. const String edited = editor_data.get_edited_scene_root() ? editor_data.get_edited_scene_root()->get_filename() : String();
  342. if (!edited.empty()) {
  343. // Display the edited scene name before the program name so that it can be seen in the OS task bar.
  344. title = vformat("%s - %s", edited.get_file(), title);
  345. }
  346. if (unsaved_cache) {
  347. // Display the "modified" mark before anything else so that it can always be seen in the OS task bar.
  348. title = vformat("(*) %s", title);
  349. }
  350. OS::get_singleton()->set_window_title(title);
  351. }
  352. void EditorNode::_unhandled_input(const Ref<InputEvent> &p_event) {
  353. if (Node::get_viewport()->get_modal_stack_top()) {
  354. return; //ignore because of modal window
  355. }
  356. Ref<InputEventKey> k = p_event;
  357. if (k.is_valid() && k->is_pressed() && !k->is_echo() && !gui_base->get_viewport()->gui_has_modal_stack()) {
  358. EditorPlugin *old_editor = editor_plugin_screen;
  359. if (ED_IS_SHORTCUT("editor/next_tab", p_event)) {
  360. int next_tab = editor_data.get_edited_scene() + 1;
  361. next_tab %= editor_data.get_edited_scene_count();
  362. _scene_tab_changed(next_tab);
  363. }
  364. if (ED_IS_SHORTCUT("editor/prev_tab", p_event)) {
  365. int next_tab = editor_data.get_edited_scene() - 1;
  366. next_tab = next_tab >= 0 ? next_tab : editor_data.get_edited_scene_count() - 1;
  367. _scene_tab_changed(next_tab);
  368. }
  369. if (ED_IS_SHORTCUT("editor/filter_files", p_event)) {
  370. filesystem_dock->focus_on_filter();
  371. }
  372. if (ED_IS_SHORTCUT("editor/editor_2d", p_event)) {
  373. _editor_select(EDITOR_2D);
  374. } else if (ED_IS_SHORTCUT("editor/editor_3d", p_event)) {
  375. _editor_select(EDITOR_3D);
  376. } else if (ED_IS_SHORTCUT("editor/editor_script", p_event)) {
  377. _editor_select(EDITOR_SCRIPT);
  378. } else if (ED_IS_SHORTCUT("editor/editor_help", p_event)) {
  379. emit_signal("request_help_search", "");
  380. } else if (ED_IS_SHORTCUT("editor/editor_assetlib", p_event) && AssetLibraryEditorPlugin::is_available()) {
  381. _editor_select(EDITOR_ASSETLIB);
  382. } else if (ED_IS_SHORTCUT("editor/editor_next", p_event)) {
  383. _editor_select_next();
  384. } else if (ED_IS_SHORTCUT("editor/editor_prev", p_event)) {
  385. _editor_select_prev();
  386. }
  387. if (old_editor != editor_plugin_screen) {
  388. get_tree()->set_input_as_handled();
  389. }
  390. }
  391. }
  392. void EditorNode::_notification(int p_what) {
  393. switch (p_what) {
  394. case NOTIFICATION_PROCESS: {
  395. if (opening_prev && !confirmation->is_visible()) {
  396. opening_prev = false;
  397. }
  398. if (unsaved_cache != (saved_version != editor_data.get_undo_redo().get_version())) {
  399. unsaved_cache = (saved_version != editor_data.get_undo_redo().get_version());
  400. _update_title();
  401. }
  402. if (last_checked_version != editor_data.get_undo_redo().get_version()) {
  403. _update_scene_tabs();
  404. last_checked_version = editor_data.get_undo_redo().get_version();
  405. }
  406. // Update the animation frame of the update spinner.
  407. uint64_t frame = Engine::get_singleton()->get_frames_drawn();
  408. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  409. if (frame != update_spinner_step_frame && (tick - update_spinner_step_msec) > (1000 / 8)) {
  410. update_spinner_step++;
  411. if (update_spinner_step >= 8) {
  412. update_spinner_step = 0;
  413. }
  414. update_spinner_step_msec = tick;
  415. update_spinner_step_frame = frame + 1;
  416. // Update the icon itself only when the spinner is visible.
  417. if (EditorSettings::get_singleton()->get("interface/editor/show_update_spinner")) {
  418. update_spinner->set_icon(gui_base->get_icon("Progress" + itos(update_spinner_step + 1), "EditorIcons"));
  419. }
  420. }
  421. editor_selection->update();
  422. scene_root->set_size_override(true, Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height")));
  423. ResourceImporterTexture::get_singleton()->update_imports();
  424. } break;
  425. case NOTIFICATION_ENTER_TREE: {
  426. Engine::get_singleton()->set_editor_hint(true);
  427. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/low_processor_mode_sleep_usec")));
  428. get_tree()->get_root()->set_usage(Viewport::USAGE_2D_NO_SAMPLING); //reduce memory usage
  429. get_tree()->get_root()->set_disable_3d(true);
  430. get_tree()->get_root()->set_as_audio_listener(false);
  431. get_tree()->get_root()->set_as_audio_listener_2d(false);
  432. get_tree()->set_auto_accept_quit(false);
  433. #ifdef ANDROID_ENABLED
  434. get_tree()->set_quit_on_go_back(false);
  435. #endif
  436. get_tree()->connect("files_dropped", this, "_dropped_files");
  437. get_tree()->connect("global_menu_action", this, "_global_menu_action");
  438. /* DO NOT LOAD SCENES HERE, WAIT FOR FILE SCANNING AND REIMPORT TO COMPLETE */
  439. } break;
  440. case NOTIFICATION_EXIT_TREE: {
  441. editor_data.save_editor_external_data();
  442. FileAccess::set_file_close_fail_notify_callback(nullptr);
  443. log->deinit(); // do not get messages anymore
  444. editor_data.clear_edited_scenes();
  445. } break;
  446. case NOTIFICATION_READY: {
  447. {
  448. _initializing_addons = true;
  449. Vector<String> addons;
  450. if (ProjectSettings::get_singleton()->has_setting("editor_plugins/enabled")) {
  451. addons = ProjectSettings::get_singleton()->get("editor_plugins/enabled");
  452. }
  453. for (int i = 0; i < addons.size(); i++) {
  454. set_addon_plugin_enabled(addons[i], true);
  455. }
  456. _initializing_addons = false;
  457. }
  458. VisualServer::get_singleton()->viewport_set_hide_scenario(get_scene_root()->get_viewport_rid(), true);
  459. VisualServer::get_singleton()->viewport_set_hide_canvas(get_scene_root()->get_viewport_rid(), true);
  460. VisualServer::get_singleton()->viewport_set_disable_environment(get_viewport()->get_viewport_rid(), true);
  461. feature_profile_manager->notify_changed();
  462. if (!main_editor_buttons[EDITOR_3D]->is_visible()) { //may be hidden due to feature profile
  463. _editor_select(EDITOR_2D);
  464. } else {
  465. _editor_select(EDITOR_3D);
  466. }
  467. _update_debug_options();
  468. // Save the project after opening to mark it as last modified, except in headless mode.
  469. if (OS::get_singleton()->can_draw() && !OS::get_singleton()->is_no_window_mode_enabled()) {
  470. ProjectSettings::get_singleton()->save();
  471. }
  472. /* DO NOT LOAD SCENES HERE, WAIT FOR FILE SCANNING AND REIMPORT TO COMPLETE */
  473. } break;
  474. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  475. // Restore the original FPS cap after focusing back on the editor.
  476. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/low_processor_mode_sleep_usec")));
  477. EditorFileSystem::get_singleton()->scan_changes();
  478. _scan_external_changes();
  479. } break;
  480. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  481. // Save on focus loss before applying the FPS limit to avoid slowing down the saving process.
  482. if (EDITOR_GET("interface/editor/save_on_focus_loss")) {
  483. _menu_option_confirm(FILE_SAVE_SCENE, false);
  484. }
  485. // Set a low FPS cap to decrease CPU/GPU usage while the editor is unfocused.
  486. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/unfocused_low_processor_mode_sleep_usec")));
  487. } break;
  488. case MainLoop::NOTIFICATION_WM_ABOUT: {
  489. show_about();
  490. } break;
  491. case MainLoop::NOTIFICATION_WM_QUIT_REQUEST: {
  492. _menu_option_confirm(FILE_QUIT, false);
  493. } break;
  494. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  495. scene_tabs->set_tab_close_display_policy((bool(EDITOR_GET("interface/scene_tabs/always_show_close_button")) ? Tabs::CLOSE_BUTTON_SHOW_ALWAYS : Tabs::CLOSE_BUTTON_SHOW_ACTIVE_ONLY));
  496. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  497. EditorFileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  498. EditorFileDialog::set_default_display_mode((EditorFileDialog::DisplayMode)EditorSettings::get_singleton()->get("filesystem/file_dialog/display_mode").operator int());
  499. theme = create_custom_theme(theme_base->get_theme());
  500. theme_base->set_theme(theme);
  501. gui_base->set_theme(theme);
  502. gui_base->add_style_override("panel", gui_base->get_stylebox("Background", "EditorStyles"));
  503. scene_root_parent->add_style_override("panel", gui_base->get_stylebox("Content", "EditorStyles"));
  504. bottom_panel->add_style_override("panel", gui_base->get_stylebox("panel", "TabContainer"));
  505. scene_tabs->add_style_override("tab_fg", gui_base->get_stylebox("SceneTabFG", "EditorStyles"));
  506. scene_tabs->add_style_override("tab_bg", gui_base->get_stylebox("SceneTabBG", "EditorStyles"));
  507. file_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  508. project_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  509. debug_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  510. settings_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  511. help_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  512. if (EDITOR_GET("interface/scene_tabs/resize_if_many_tabs")) {
  513. scene_tabs->set_min_width(int(EDITOR_GET("interface/scene_tabs/minimum_width")) * EDSCALE);
  514. } else {
  515. scene_tabs->set_min_width(0);
  516. }
  517. _update_scene_tabs();
  518. recent_scenes->set_as_minsize();
  519. // debugger area
  520. if (ScriptEditor::get_singleton()->get_debugger()->is_visible()) {
  521. bottom_panel->add_style_override("panel", gui_base->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles"));
  522. }
  523. // update_icons
  524. for (int i = 0; i < singleton->main_editor_buttons.size(); i++) {
  525. ToolButton *tb = singleton->main_editor_buttons[i];
  526. EditorPlugin *p_editor = singleton->editor_table[i];
  527. Ref<Texture> icon = p_editor->get_icon();
  528. if (icon.is_valid()) {
  529. tb->set_icon(icon);
  530. } else if (singleton->gui_base->has_icon(p_editor->get_name(), "EditorIcons")) {
  531. tb->set_icon(singleton->gui_base->get_icon(p_editor->get_name(), "EditorIcons"));
  532. }
  533. }
  534. _build_icon_type_cache();
  535. play_button->set_icon(gui_base->get_icon("MainPlay", "EditorIcons"));
  536. play_scene_button->set_icon(gui_base->get_icon("PlayScene", "EditorIcons"));
  537. play_custom_scene_button->set_icon(gui_base->get_icon("PlayCustom", "EditorIcons"));
  538. pause_button->set_icon(gui_base->get_icon("Pause", "EditorIcons"));
  539. stop_button->set_icon(gui_base->get_icon("Stop", "EditorIcons"));
  540. prev_scene->set_icon(gui_base->get_icon("PrevScene", "EditorIcons"));
  541. distraction_free->set_icon(gui_base->get_icon("DistractionFree", "EditorIcons"));
  542. scene_tab_add->set_icon(gui_base->get_icon("Add", "EditorIcons"));
  543. bottom_panel_raise->set_icon(gui_base->get_icon("ExpandBottomDock", "EditorIcons"));
  544. // clear_button->set_icon(gui_base->get_icon("Close", "EditorIcons")); don't have access to that node. needs to become a class property
  545. dock_tab_move_left->set_icon(theme->get_icon("Back", "EditorIcons"));
  546. dock_tab_move_right->set_icon(theme->get_icon("Forward", "EditorIcons"));
  547. PopupMenu *p = help_menu->get_popup();
  548. p->set_item_icon(p->get_item_index(HELP_SEARCH), gui_base->get_icon("HelpSearch", "EditorIcons"));
  549. p->set_item_icon(p->get_item_index(HELP_DOCS), gui_base->get_icon("ExternalLink", "EditorIcons"));
  550. p->set_item_icon(p->get_item_index(HELP_QA), gui_base->get_icon("ExternalLink", "EditorIcons"));
  551. p->set_item_icon(p->get_item_index(HELP_REPORT_A_BUG), gui_base->get_icon("ExternalLink", "EditorIcons"));
  552. p->set_item_icon(p->get_item_index(HELP_SUGGEST_A_FEATURE), gui_base->get_icon("ExternalLink", "EditorIcons"));
  553. p->set_item_icon(p->get_item_index(HELP_SEND_DOCS_FEEDBACK), gui_base->get_icon("ExternalLink", "EditorIcons"));
  554. p->set_item_icon(p->get_item_index(HELP_COMMUNITY), gui_base->get_icon("ExternalLink", "EditorIcons"));
  555. p->set_item_icon(p->get_item_index(HELP_ABOUT), gui_base->get_icon("Godot", "EditorIcons"));
  556. p->set_item_icon(p->get_item_index(HELP_SUPPORT_GODOT_DEVELOPMENT), gui_base->get_icon("Heart", "EditorIcons"));
  557. _update_update_spinner();
  558. } break;
  559. case Control::NOTIFICATION_RESIZED: {
  560. _update_scene_tabs();
  561. } break;
  562. }
  563. }
  564. void EditorNode::_update_update_spinner() {
  565. update_spinner->set_visible(EditorSettings::get_singleton()->get("interface/editor/show_update_spinner"));
  566. const bool update_continuously = EditorSettings::get_singleton()->get("interface/editor/update_continuously");
  567. const bool vital_only = EditorSettings::get_singleton()->get("interface/editor/update_vital_only");
  568. PopupMenu *update_popup = update_spinner->get_popup();
  569. update_popup->set_item_checked(update_popup->get_item_index(SETTINGS_UPDATE_CONTINUOUSLY), update_continuously);
  570. if (update_continuously) {
  571. update_popup->set_item_checked(update_popup->get_item_index(SETTINGS_UPDATE_WHEN_CHANGED), false);
  572. update_popup->set_item_checked(update_popup->get_item_index(SETTINGS_UPDATE_VITAL_ONLY), false);
  573. } else {
  574. update_popup->set_item_checked(update_popup->get_item_index(SETTINGS_UPDATE_WHEN_CHANGED), !vital_only);
  575. update_popup->set_item_checked(update_popup->get_item_index(SETTINGS_UPDATE_VITAL_ONLY), vital_only);
  576. }
  577. if (update_continuously) {
  578. update_spinner->set_tooltip(TTR("Spins when the editor window redraws.\nUpdate Continuously is enabled, which can increase power usage. Click to disable it."));
  579. // Use a different color for the update spinner when Update Continuously is enabled,
  580. // as this feature should only be enabled for troubleshooting purposes.
  581. // Make the icon modulate color overbright because icons are not completely white on a dark theme.
  582. // On a light theme, icons are dark, so we need to modulate them with an even brighter color.
  583. const bool dark_theme = EditorSettings::get_singleton()->is_dark_theme();
  584. update_spinner->set_self_modulate(
  585. gui_base->get_color("error_color", "Editor") * (dark_theme ? Color(1.1, 1.1, 1.1) : Color(4.25, 4.25, 4.25)));
  586. } else {
  587. update_spinner->set_tooltip(TTR("Spins when the editor window redraws."));
  588. update_spinner->set_self_modulate(Color(1, 1, 1));
  589. }
  590. OS::get_singleton()->set_low_processor_usage_mode(!update_continuously);
  591. // Only set low priority redraws to false in the editor.
  592. // When we run the project in the editor, we don't want it to prevent
  593. // rendering any frames.
  594. OS::get_singleton()->set_update_vital_only(vital_only && !update_continuously);
  595. }
  596. void EditorNode::_on_plugin_ready(Object *p_script, const String &p_activate_name) {
  597. Ref<Script> script = Object::cast_to<Script>(p_script);
  598. if (script.is_null()) {
  599. return;
  600. }
  601. if (p_activate_name.length()) {
  602. set_addon_plugin_enabled(p_activate_name, true);
  603. }
  604. project_settings->update_plugins();
  605. project_settings->hide();
  606. push_item(script.operator->());
  607. }
  608. void EditorNode::_remove_plugin_from_enabled(const String &p_name) {
  609. ProjectSettings *ps = ProjectSettings::get_singleton();
  610. PoolStringArray enabled_plugins = ps->get("editor_plugins/enabled");
  611. for (int i = 0; i < enabled_plugins.size(); ++i) {
  612. if (enabled_plugins.get(i) == p_name) {
  613. enabled_plugins.remove(i);
  614. break;
  615. }
  616. }
  617. ps->set("editor_plugins/enabled", enabled_plugins);
  618. }
  619. void EditorNode::_resources_changed(const PoolVector<String> &p_resources) {
  620. List<Ref<Resource>> changed;
  621. int rc = p_resources.size();
  622. for (int i = 0; i < rc; i++) {
  623. Ref<Resource> res(ResourceCache::get(p_resources.get(i)));
  624. if (res.is_null()) {
  625. continue;
  626. }
  627. if (!res->editor_can_reload_from_file()) {
  628. continue;
  629. }
  630. if (!res->get_path().is_resource_file() && !res->get_path().is_abs_path()) {
  631. continue;
  632. }
  633. if (!FileAccess::exists(res->get_path())) {
  634. continue;
  635. }
  636. if (res->get_import_path() != String()) {
  637. //this is an imported resource, will be reloaded if reimported via the _resources_reimported() callback
  638. continue;
  639. }
  640. changed.push_back(res);
  641. }
  642. if (changed.size()) {
  643. for (List<Ref<Resource>>::Element *E = changed.front(); E; E = E->next()) {
  644. E->get()->reload_from_file();
  645. }
  646. }
  647. }
  648. void EditorNode::_fs_changed() {
  649. for (Set<FileDialog *>::Element *E = file_dialogs.front(); E; E = E->next()) {
  650. E->get()->invalidate();
  651. }
  652. for (Set<EditorFileDialog *>::Element *E = editor_file_dialogs.front(); E; E = E->next()) {
  653. E->get()->invalidate();
  654. }
  655. _mark_unsaved_scenes();
  656. // FIXME: Move this to a cleaner location, it's hacky to do this is _fs_changed.
  657. String export_error;
  658. Error err = OK;
  659. if (export_defer.preset != "" && !EditorFileSystem::get_singleton()->is_scanning()) {
  660. String preset_name = export_defer.preset;
  661. // Ensures export_project does not loop infinitely, because notifications may
  662. // come during the export.
  663. export_defer.preset = "";
  664. Ref<EditorExportPreset> preset;
  665. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); ++i) {
  666. preset = EditorExport::get_singleton()->get_export_preset(i);
  667. if (preset->get_name() == preset_name) {
  668. break;
  669. }
  670. preset.unref();
  671. }
  672. if (preset.is_null()) {
  673. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  674. if (da->file_exists("res://export_presets.cfg")) {
  675. err = FAILED;
  676. export_error = vformat(
  677. "Invalid export preset name: %s.\nThe following presets were detected in this project's `export_presets.cfg`:\n\n",
  678. preset_name);
  679. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); ++i) {
  680. // Write the preset name between double quotes since it needs to be written between quotes on the command line if it contains spaces.
  681. export_error += vformat(" \"%s\"\n", EditorExport::get_singleton()->get_export_preset(i)->get_name());
  682. }
  683. } else {
  684. err = FAILED;
  685. export_error = "This project doesn't have an `export_presets.cfg` file at its root.\nCreate an export preset from the \"Project > Export\" dialog and try again.";
  686. }
  687. } else {
  688. Ref<EditorExportPlatform> platform = preset->get_platform();
  689. const String export_path = export_defer.path.empty() ? preset->get_export_path() : export_defer.path;
  690. if (export_path.empty()) {
  691. err = FAILED;
  692. export_error = vformat("Export preset \"%s\" doesn't have a default export path, and none was specified.", preset_name);
  693. } else if (platform.is_null()) {
  694. err = FAILED;
  695. export_error = vformat("Export preset \"%s\" doesn't have a matching platform.", preset_name);
  696. } else {
  697. if (export_defer.pack_only) { // Only export .pck or .zip data pack.
  698. if (export_path.ends_with(".zip")) {
  699. err = platform->export_zip(preset, export_defer.debug, export_path);
  700. } else if (export_path.ends_with(".pck")) {
  701. err = platform->export_pack(preset, export_defer.debug, export_path);
  702. }
  703. } else { // Normal project export.
  704. String config_error;
  705. bool missing_templates;
  706. if (!platform->can_export(preset, config_error, missing_templates)) {
  707. ERR_PRINT(vformat("Cannot export project with preset \"%s\" due to configuration errors:\n%s", preset_name, config_error));
  708. err = missing_templates ? ERR_FILE_NOT_FOUND : ERR_UNCONFIGURED;
  709. } else {
  710. platform->clear_messages();
  711. err = platform->export_project(preset, export_defer.debug, export_path);
  712. }
  713. }
  714. if (err != OK) {
  715. export_error = vformat("Project export for preset \"%s\" failed.", preset_name);
  716. } else if (platform->get_worst_message_type() >= EditorExportPlatform::EXPORT_MESSAGE_WARNING) {
  717. export_error = vformat("Project export for preset \"%s\" completed with warnings.", preset_name);
  718. }
  719. }
  720. }
  721. if (err != OK) {
  722. ERR_PRINT(export_error);
  723. OS::get_singleton()->set_exit_code(EXIT_FAILURE);
  724. } else if (!export_error.empty()) {
  725. WARN_PRINT(export_error);
  726. }
  727. _exit_editor();
  728. }
  729. }
  730. void EditorNode::_resources_reimported(const Vector<String> &p_resources) {
  731. List<String> scenes; //will load later
  732. int current_tab = scene_tabs->get_current_tab();
  733. for (int i = 0; i < p_resources.size(); i++) {
  734. String file_type = ResourceLoader::get_resource_type(p_resources[i]);
  735. if (file_type == "PackedScene") {
  736. scenes.push_back(p_resources[i]);
  737. //reload later if needed, first go with normal resources
  738. continue;
  739. }
  740. if (!ResourceCache::has(p_resources[i])) {
  741. continue; //not loaded, no need to reload
  742. }
  743. //reload normally
  744. Resource *resource = ResourceCache::get(p_resources[i]);
  745. if (resource) {
  746. resource->reload_from_file();
  747. }
  748. }
  749. for (List<String>::Element *E = scenes.front(); E; E = E->next()) {
  750. reload_scene(E->get());
  751. }
  752. scene_tabs->set_current_tab(current_tab);
  753. }
  754. void EditorNode::_sources_changed(bool p_exist) {
  755. if (waiting_for_first_scan) {
  756. waiting_for_first_scan = false;
  757. // Start preview thread now that it's safe.
  758. if (!singleton->cmdline_export_mode) {
  759. EditorResourcePreview::get_singleton()->start();
  760. }
  761. _load_docks();
  762. if (defer_load_scene != "") {
  763. OS::get_singleton()->benchmark_begin_measure("editor_load_scene");
  764. load_scene(defer_load_scene);
  765. defer_load_scene = "";
  766. OS::get_singleton()->benchmark_end_measure("editor_load_scene");
  767. OS::get_singleton()->benchmark_dump();
  768. }
  769. }
  770. }
  771. void EditorNode::_scan_external_changes() {
  772. disk_changed_list->clear();
  773. TreeItem *r = disk_changed_list->create_item();
  774. disk_changed_list->set_hide_root(true);
  775. bool need_reload = false;
  776. // Check if any edited scene has changed.
  777. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  778. DirAccessRef da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  779. if (editor_data.get_scene_path(i) == "" || !da->file_exists(editor_data.get_scene_path(i))) {
  780. continue;
  781. }
  782. uint64_t last_date = editor_data.get_scene_modified_time(i);
  783. uint64_t date = FileAccess::get_modified_time(editor_data.get_scene_path(i));
  784. if (date > last_date) {
  785. TreeItem *ti = disk_changed_list->create_item(r);
  786. ti->set_text(0, editor_data.get_scene_path(i).get_file());
  787. need_reload = true;
  788. }
  789. }
  790. String project_settings_path = ProjectSettings::get_singleton()->get_resource_path().plus_file("project.godot");
  791. if (FileAccess::get_modified_time(project_settings_path) > ProjectSettings::get_singleton()->get_last_saved_time()) {
  792. TreeItem *ti = disk_changed_list->create_item(r);
  793. ti->set_text(0, "project.godot");
  794. need_reload = true;
  795. }
  796. if (need_reload) {
  797. disk_changed->call_deferred("popup_centered_ratio", 0.5);
  798. }
  799. }
  800. void EditorNode::_resave_scenes(String p_str) {
  801. save_all_scenes();
  802. ProjectSettings::get_singleton()->save();
  803. disk_changed->hide();
  804. }
  805. void EditorNode::_reload_modified_scenes() {
  806. int current_idx = editor_data.get_edited_scene();
  807. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  808. if (editor_data.get_scene_path(i) == "") {
  809. continue;
  810. }
  811. uint64_t last_date = editor_data.get_scene_modified_time(i);
  812. uint64_t date = FileAccess::get_modified_time(editor_data.get_scene_path(i));
  813. if (date > last_date) {
  814. String filename = editor_data.get_scene_path(i);
  815. editor_data.set_edited_scene(i);
  816. _remove_edited_scene(false);
  817. Error err = load_scene(filename, false, false, true, false, true);
  818. if (err != OK) {
  819. ERR_PRINT(vformat("Failed to load scene: %s", filename));
  820. }
  821. editor_data.move_edited_scene_to_index(i);
  822. }
  823. }
  824. get_undo_redo()->clear_history(false);
  825. set_current_scene(current_idx);
  826. _update_scene_tabs();
  827. disk_changed->hide();
  828. }
  829. void EditorNode::_reload_project_settings() {
  830. ProjectSettings::get_singleton()->setup(ProjectSettings::get_singleton()->get_resource_path(), String(), true);
  831. }
  832. void EditorNode::_vp_resized() {
  833. }
  834. void EditorNode::_version_button_pressed() {
  835. OS::get_singleton()->set_clipboard(version_btn->get_meta(META_TEXT_TO_COPY));
  836. }
  837. void EditorNode::_node_renamed() {
  838. if (get_inspector()) {
  839. get_inspector()->update_tree();
  840. }
  841. }
  842. void EditorNode::_editor_select_next() {
  843. int editor = _get_current_main_editor();
  844. do {
  845. if (editor == editor_table.size() - 1) {
  846. editor = 0;
  847. } else {
  848. editor++;
  849. }
  850. } while (!main_editor_buttons[editor]->is_visible());
  851. _editor_select(editor);
  852. }
  853. void EditorNode::_editor_select_prev() {
  854. int editor = _get_current_main_editor();
  855. do {
  856. if (editor == 0) {
  857. editor = editor_table.size() - 1;
  858. } else {
  859. editor--;
  860. }
  861. } while (!main_editor_buttons[editor]->is_visible());
  862. _editor_select(editor);
  863. }
  864. Error EditorNode::load_resource(const String &p_resource, bool p_ignore_broken_deps) {
  865. dependency_errors.clear();
  866. Error err;
  867. RES res = ResourceLoader::load(p_resource, "", false, &err);
  868. ERR_FAIL_COND_V(!res.is_valid(), ERR_CANT_OPEN);
  869. if (!p_ignore_broken_deps && dependency_errors.has(p_resource)) {
  870. //current_option = -1;
  871. Vector<String> errors;
  872. for (Set<String>::Element *E = dependency_errors[p_resource].front(); E; E = E->next()) {
  873. errors.push_back(E->get());
  874. }
  875. dependency_error->show(DependencyErrorDialog::MODE_RESOURCE, p_resource, errors);
  876. dependency_errors.erase(p_resource);
  877. return ERR_FILE_MISSING_DEPENDENCIES;
  878. }
  879. inspector_dock->edit_resource(res);
  880. return OK;
  881. }
  882. void EditorNode::edit_node(Node *p_node) {
  883. push_item(p_node);
  884. }
  885. void EditorNode::save_resource_in_path(const Ref<Resource> &p_resource, const String &p_path) {
  886. editor_data.apply_changes_in_editors();
  887. int flg = 0;
  888. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) {
  889. flg |= ResourceSaver::FLAG_COMPRESS;
  890. }
  891. String path = ProjectSettings::get_singleton()->localize_path(p_path);
  892. Error err = ResourceSaver::save(path, p_resource, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS);
  893. if (err != OK) {
  894. if (ResourceLoader::is_imported(p_resource->get_path())) {
  895. show_accept(TTR("Imported resources can't be saved."), TTR("OK"));
  896. } else {
  897. show_accept(TTR("Error saving resource!"), TTR("OK"));
  898. }
  899. return;
  900. }
  901. ((Resource *)p_resource.ptr())->set_path(path);
  902. emit_signal("resource_saved", p_resource);
  903. editor_data.notify_resource_saved(p_resource);
  904. }
  905. void EditorNode::save_resource(const Ref<Resource> &p_resource) {
  906. if (p_resource->get_path().is_resource_file()) {
  907. save_resource_in_path(p_resource, p_resource->get_path());
  908. } else {
  909. save_resource_as(p_resource);
  910. }
  911. }
  912. void EditorNode::save_resource_as(const Ref<Resource> &p_resource, const String &p_at_path) {
  913. {
  914. String path = p_resource->get_path();
  915. int srpos = path.find("::");
  916. if (srpos != -1) {
  917. String base = path.substr(0, srpos);
  918. if (!get_edited_scene() || get_edited_scene()->get_filename() != base) {
  919. show_warning(TTR("This resource can't be saved because it does not belong to the edited scene. Make it unique first."));
  920. return;
  921. }
  922. }
  923. }
  924. file->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  925. saving_resource = p_resource;
  926. current_option = RESOURCE_SAVE_AS;
  927. List<String> extensions;
  928. Ref<PackedScene> sd = memnew(PackedScene);
  929. ResourceSaver::get_recognized_extensions(p_resource, &extensions);
  930. file->clear_filters();
  931. List<String> preferred;
  932. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  933. if (p_resource->is_class("Script") && (E->get() == "tres" || E->get() == "res")) {
  934. //this serves no purpose and confused people
  935. continue;
  936. }
  937. file->add_filter("*." + E->get() + " ; " + E->get().to_upper());
  938. preferred.push_back(E->get());
  939. }
  940. // Lowest priority extension
  941. List<String>::Element *res_element = preferred.find("res");
  942. if (res_element) {
  943. preferred.move_to_back(res_element);
  944. }
  945. // Highest priority extension
  946. List<String>::Element *tres_element = preferred.find("tres");
  947. if (tres_element) {
  948. preferred.move_to_front(tres_element);
  949. }
  950. if (p_at_path != String()) {
  951. file->set_current_dir(p_at_path);
  952. if (p_resource->get_path().is_resource_file()) {
  953. file->set_current_file(p_resource->get_path().get_file());
  954. } else {
  955. if (extensions.size()) {
  956. file->set_current_file("new_" + p_resource->get_class().to_lower() + "." + preferred.front()->get().to_lower());
  957. } else {
  958. file->set_current_file(String());
  959. }
  960. }
  961. } else if (p_resource->get_path() != "") {
  962. file->set_current_path(p_resource->get_path());
  963. if (extensions.size()) {
  964. String ext = p_resource->get_path().get_extension().to_lower();
  965. if (extensions.find(ext) == nullptr) {
  966. file->set_current_path(p_resource->get_path().replacen("." + ext, "." + extensions.front()->get()));
  967. }
  968. }
  969. } else if (preferred.size()) {
  970. String existing;
  971. if (extensions.size()) {
  972. existing = "new_" + p_resource->get_class().to_lower() + "." + preferred.front()->get().to_lower();
  973. }
  974. file->set_current_path(existing);
  975. }
  976. file->popup_centered_ratio();
  977. file->set_title(TTR("Save Resource As..."));
  978. }
  979. void EditorNode::_menu_option(int p_option) {
  980. _menu_option_confirm(p_option, false);
  981. }
  982. void EditorNode::_menu_confirm_current() {
  983. _menu_option_confirm(current_option, true);
  984. }
  985. void EditorNode::_dialog_display_save_error(String p_file, Error p_error) {
  986. if (p_error) {
  987. switch (p_error) {
  988. case ERR_FILE_CANT_WRITE: {
  989. show_accept(TTR("Can't open file for writing:") + " " + p_file.get_extension(), TTR("OK"));
  990. } break;
  991. case ERR_FILE_UNRECOGNIZED: {
  992. show_accept(TTR("Requested file format unknown:") + " " + p_file.get_extension(), TTR("OK"));
  993. } break;
  994. default: {
  995. show_accept(TTR("Error while saving."), TTR("OK"));
  996. } break;
  997. }
  998. }
  999. }
  1000. void EditorNode::_dialog_display_load_error(String p_file, Error p_error) {
  1001. if (p_error) {
  1002. switch (p_error) {
  1003. case ERR_CANT_OPEN: {
  1004. show_accept(vformat(TTR("Can't open '%s'. The file could have been moved or deleted."), p_file.get_file()), TTR("OK"));
  1005. } break;
  1006. case ERR_PARSE_ERROR: {
  1007. show_accept(vformat(TTR("Error while parsing '%s'."), p_file.get_file()), TTR("OK"));
  1008. } break;
  1009. case ERR_FILE_CORRUPT: {
  1010. show_accept(vformat(TTR("Unexpected end of file '%s'."), p_file.get_file()), TTR("OK"));
  1011. } break;
  1012. case ERR_FILE_NOT_FOUND: {
  1013. show_accept(vformat(TTR("Missing '%s' or its dependencies."), p_file.get_file()), TTR("OK"));
  1014. } break;
  1015. default: {
  1016. show_accept(vformat(TTR("Error while loading '%s'."), p_file.get_file()), TTR("OK"));
  1017. } break;
  1018. }
  1019. }
  1020. }
  1021. void EditorNode::_get_scene_metadata(const String &p_file) {
  1022. Node *scene = editor_data.get_edited_scene_root();
  1023. if (!scene) {
  1024. return;
  1025. }
  1026. String path = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(p_file.get_file() + "-editstate-" + p_file.md5_text() + ".cfg");
  1027. Ref<ConfigFile> cf;
  1028. cf.instance();
  1029. Error err = cf->load(path);
  1030. if (err != OK || !cf->has_section("editor_states")) {
  1031. return; //must not exist
  1032. }
  1033. List<String> esl;
  1034. cf->get_section_keys("editor_states", &esl);
  1035. Dictionary md;
  1036. for (List<String>::Element *E = esl.front(); E; E = E->next()) {
  1037. Variant st = cf->get_value("editor_states", E->get());
  1038. if (st.get_type() != Variant::NIL) {
  1039. md[E->get()] = st;
  1040. }
  1041. }
  1042. editor_data.set_editor_states(md);
  1043. }
  1044. void EditorNode::_set_scene_metadata(const String &p_file, int p_idx) {
  1045. Node *scene = editor_data.get_edited_scene_root(p_idx);
  1046. if (!scene) {
  1047. return;
  1048. }
  1049. scene->set_meta("__editor_run_settings__", Variant()); //clear it (no point in keeping it)
  1050. scene->set_meta("__editor_plugin_states__", Variant());
  1051. String path = EditorSettings::get_singleton()->get_project_settings_dir().plus_file(p_file.get_file() + "-editstate-" + p_file.md5_text() + ".cfg");
  1052. Ref<ConfigFile> cf;
  1053. cf.instance();
  1054. Dictionary md;
  1055. if (p_idx < 0 || editor_data.get_edited_scene() == p_idx) {
  1056. md = editor_data.get_editor_states();
  1057. } else {
  1058. md = editor_data.get_scene_editor_states(p_idx);
  1059. }
  1060. List<Variant> keys;
  1061. md.get_key_list(&keys);
  1062. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  1063. cf->set_value("editor_states", E->get(), md[E->get()]);
  1064. }
  1065. Error err = cf->save(path);
  1066. ERR_FAIL_COND_MSG(err != OK, "Cannot save config file to '" + path + "'.");
  1067. }
  1068. bool EditorNode::_find_and_save_resource(RES p_res, Map<RES, bool> &processed, int32_t flags) {
  1069. if (p_res.is_null()) {
  1070. return false;
  1071. }
  1072. if (processed.has(p_res)) {
  1073. return processed[p_res];
  1074. }
  1075. bool changed = p_res->is_edited();
  1076. p_res->set_edited(false);
  1077. bool subchanged = _find_and_save_edited_subresources(p_res.ptr(), processed, flags);
  1078. if (p_res->get_path().is_resource_file()) {
  1079. if (changed || subchanged) {
  1080. //save
  1081. ResourceSaver::save(p_res->get_path(), p_res, flags);
  1082. }
  1083. processed[p_res] = false; //because it's a file
  1084. return false;
  1085. } else {
  1086. processed[p_res] = changed;
  1087. return changed;
  1088. }
  1089. }
  1090. bool EditorNode::_find_and_save_edited_subresources(Object *obj, Map<RES, bool> &processed, int32_t flags) {
  1091. bool ret_changed = false;
  1092. List<PropertyInfo> pi;
  1093. obj->get_property_list(&pi);
  1094. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  1095. if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) {
  1096. continue;
  1097. }
  1098. switch (E->get().type) {
  1099. case Variant::OBJECT: {
  1100. RES res = obj->get(E->get().name);
  1101. if (_find_and_save_resource(res, processed, flags)) {
  1102. ret_changed = true;
  1103. }
  1104. } break;
  1105. case Variant::ARRAY: {
  1106. Array varray = obj->get(E->get().name);
  1107. int len = varray.size();
  1108. for (int i = 0; i < len; i++) {
  1109. const Variant &v = varray.get(i);
  1110. RES res = v;
  1111. if (_find_and_save_resource(res, processed, flags)) {
  1112. ret_changed = true;
  1113. }
  1114. }
  1115. } break;
  1116. case Variant::DICTIONARY: {
  1117. Dictionary d = obj->get(E->get().name);
  1118. List<Variant> keys;
  1119. d.get_key_list(&keys);
  1120. for (List<Variant>::Element *F = keys.front(); F; F = F->next()) {
  1121. Variant v = d[F->get()];
  1122. RES res = v;
  1123. if (_find_and_save_resource(res, processed, flags)) {
  1124. ret_changed = true;
  1125. }
  1126. }
  1127. } break;
  1128. default: {
  1129. }
  1130. }
  1131. }
  1132. return ret_changed;
  1133. }
  1134. void EditorNode::_save_edited_subresources(Node *scene, Map<RES, bool> &processed, int32_t flags) {
  1135. _find_and_save_edited_subresources(scene, processed, flags);
  1136. for (int i = 0; i < scene->get_child_count(); i++) {
  1137. Node *n = scene->get_child(i);
  1138. if (n->get_owner() != editor_data.get_edited_scene_root()) {
  1139. continue;
  1140. }
  1141. _save_edited_subresources(n, processed, flags);
  1142. }
  1143. }
  1144. void EditorNode::_find_node_types(Node *p_node, int &count_2d, int &count_3d) {
  1145. if (p_node->is_class("Viewport") || (p_node != editor_data.get_edited_scene_root() && p_node->get_owner() != editor_data.get_edited_scene_root())) {
  1146. return;
  1147. }
  1148. if (p_node->is_class("CanvasItem")) {
  1149. count_2d++;
  1150. } else if (p_node->is_class("Spatial")) {
  1151. count_3d++;
  1152. }
  1153. for (int i = 0; i < p_node->get_child_count(); i++) {
  1154. _find_node_types(p_node->get_child(i), count_2d, count_3d);
  1155. }
  1156. }
  1157. void EditorNode::_save_scene_with_preview(String p_file, int p_idx) {
  1158. EditorProgress save("save", TTR("Saving Scene"), 4);
  1159. Ref<World> edited_world;
  1160. if (editor_data.get_edited_scene_root() != nullptr) {
  1161. // Allow a generic mechanism for the engine to make changes prior, and after saving.
  1162. if (editor_data.get_edited_scene_root()->get_tree() && editor_data.get_edited_scene_root()->get_tree()->get_root()) {
  1163. edited_world = editor_data.get_edited_scene_root()->get_tree()->get_root()->get_world();
  1164. if (edited_world.is_valid()) {
  1165. edited_world->notify_saving(true);
  1166. }
  1167. }
  1168. save.step(TTR("Analyzing"), 0);
  1169. int c2d = 0;
  1170. int c3d = 0;
  1171. _find_node_types(editor_data.get_edited_scene_root(), c2d, c3d);
  1172. save.step(TTR("Creating Thumbnail"), 1);
  1173. //current view?
  1174. Ref<Image> img;
  1175. // If neither 3D or 2D nodes are present, make a 1x1 black texture.
  1176. // We cannot fallback on the 2D editor, because it may not have been used yet,
  1177. // which would result in an invalid texture.
  1178. if (c3d == 0 && c2d == 0) {
  1179. img.instance();
  1180. img->create(1, 1, false, Image::FORMAT_RGB8);
  1181. } else if (c3d < c2d) {
  1182. Ref<ViewportTexture> viewport_texture = scene_root->get_texture();
  1183. if (viewport_texture->get_width() > 0 && viewport_texture->get_height() > 0) {
  1184. img = viewport_texture->get_data();
  1185. }
  1186. } else {
  1187. // The 3D editor may be disabled as a feature, but scenes can still be opened.
  1188. // This check prevents the preview from regenerating in case those scenes are then saved.
  1189. // The preview will be generated if no feature profile is set (as the 3D editor is enabled by default).
  1190. Ref<EditorFeatureProfile> profile = feature_profile_manager->get_current_profile();
  1191. if (!profile.is_valid() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D)) {
  1192. img = SpatialEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_texture()->get_data();
  1193. }
  1194. }
  1195. if (img.is_valid() && img->get_width() > 0 && img->get_height() > 0) {
  1196. img = img->duplicate();
  1197. save.step(TTR("Creating Thumbnail"), 2);
  1198. save.step(TTR("Creating Thumbnail"), 3);
  1199. int preview_size = EditorSettings::get_singleton()->get("filesystem/file_dialog/thumbnail_size");
  1200. preview_size *= EDSCALE;
  1201. // consider a square region
  1202. int vp_size = MIN(img->get_width(), img->get_height());
  1203. int x = (img->get_width() - vp_size) / 2;
  1204. int y = (img->get_height() - vp_size) / 2;
  1205. if (vp_size < preview_size) {
  1206. // just square it.
  1207. img->crop_from_point(x, y, vp_size, vp_size);
  1208. } else {
  1209. int ratio = vp_size / preview_size;
  1210. int size = preview_size * MAX(1, ratio / 2);
  1211. x = (img->get_width() - size) / 2;
  1212. y = (img->get_height() - size) / 2;
  1213. img->crop_from_point(x, y, size, size);
  1214. img->resize(preview_size, preview_size, Image::INTERPOLATE_LANCZOS);
  1215. }
  1216. img->convert(Image::FORMAT_RGB8);
  1217. img->flip_y();
  1218. //save thumbnail directly, as thumbnailer may not update due to actual scene not changing md5
  1219. String temp_path = EditorSettings::get_singleton()->get_cache_dir();
  1220. String cache_base = ProjectSettings::get_singleton()->globalize_path(p_file).md5_text();
  1221. cache_base = temp_path.plus_file("resthumb-" + cache_base);
  1222. //does not have it, try to load a cached thumbnail
  1223. String file = cache_base + ".png";
  1224. post_process_preview(img);
  1225. img->save_png(file);
  1226. }
  1227. }
  1228. save.step(TTR("Saving Scene"), 4);
  1229. _save_scene(p_file, p_idx);
  1230. if (edited_world.is_valid()) {
  1231. edited_world->notify_saving(false);
  1232. }
  1233. if (!singleton->cmdline_export_mode) {
  1234. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1235. }
  1236. }
  1237. bool EditorNode::_validate_scene_recursive(const String &p_filename, Node *p_node) {
  1238. for (int i = 0; i < p_node->get_child_count(); i++) {
  1239. Node *child = p_node->get_child(i);
  1240. if (child->get_filename() == p_filename) {
  1241. return true;
  1242. }
  1243. if (_validate_scene_recursive(p_filename, child)) {
  1244. return true;
  1245. }
  1246. }
  1247. return false;
  1248. }
  1249. int EditorNode::_save_external_resources() {
  1250. //save external resources and its subresources if any was modified
  1251. int flg = 0;
  1252. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) {
  1253. flg |= ResourceSaver::FLAG_COMPRESS;
  1254. }
  1255. flg |= ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1256. Set<String> edited_resources;
  1257. int saved = 0;
  1258. List<Ref<Resource>> cached;
  1259. ResourceCache::get_cached_resources(&cached);
  1260. for (List<Ref<Resource>>::Element *E = cached.front(); E; E = E->next()) {
  1261. Ref<Resource> res = E->get();
  1262. if (!res->is_edited()) {
  1263. continue;
  1264. }
  1265. String path = res->get_path();
  1266. if (path.begins_with("res://")) {
  1267. int subres_pos = path.find("::");
  1268. if (subres_pos == -1) {
  1269. // Actual resource.
  1270. edited_resources.insert(path);
  1271. } else {
  1272. edited_resources.insert(path.substr(0, subres_pos));
  1273. }
  1274. }
  1275. res->set_edited(false);
  1276. }
  1277. for (Set<String>::Element *E = edited_resources.front(); E; E = E->next()) {
  1278. Ref<Resource> res = Ref<Resource>(ResourceCache::get(E->get()));
  1279. if (!res.is_valid()) {
  1280. continue; // Maybe it was erased in a thread, who knows.
  1281. }
  1282. Ref<PackedScene> ps = res;
  1283. if (ps.is_valid()) {
  1284. continue; // Do not save PackedScenes, this will mess up the editor.
  1285. }
  1286. ResourceSaver::save(res->get_path(), res, flg);
  1287. saved++;
  1288. }
  1289. return saved;
  1290. }
  1291. static void _reset_animation_players(Node *p_node, List<Ref<AnimatedValuesBackup>> *r_anim_backups) {
  1292. for (int i = 0; i < p_node->get_child_count(); i++) {
  1293. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(p_node->get_child(i));
  1294. if (player && player->is_reset_on_save_enabled() && player->can_apply_reset()) {
  1295. Ref<AnimatedValuesBackup> old_values = player->apply_reset();
  1296. if (old_values.is_valid()) {
  1297. r_anim_backups->push_back(old_values);
  1298. }
  1299. }
  1300. _reset_animation_players(p_node->get_child(i), r_anim_backups);
  1301. }
  1302. }
  1303. void EditorNode::_save_scene(String p_file, int idx) {
  1304. Node *scene = editor_data.get_edited_scene_root(idx);
  1305. if (!scene) {
  1306. show_accept(TTR("This operation can't be done without a tree root."), TTR("OK"));
  1307. return;
  1308. }
  1309. if (scene->get_filename() != String() && _validate_scene_recursive(scene->get_filename(), scene)) {
  1310. show_accept(TTR("This scene can't be saved because there is a cyclic instancing inclusion.\nPlease resolve it and then attempt to save again."), TTR("OK"));
  1311. return;
  1312. }
  1313. editor_data.apply_changes_in_editors();
  1314. List<Ref<AnimatedValuesBackup>> anim_backups;
  1315. _reset_animation_players(scene, &anim_backups);
  1316. _save_default_environment();
  1317. _set_scene_metadata(p_file, idx);
  1318. Ref<PackedScene> sdata;
  1319. if (ResourceCache::has(p_file)) {
  1320. // something may be referencing this resource and we are good with that.
  1321. // we must update it, but also let the previous scene state go, as
  1322. // old version still work for referencing changes in instanced or inherited scenes
  1323. sdata = Ref<PackedScene>(Object::cast_to<PackedScene>(ResourceCache::get(p_file)));
  1324. if (sdata.is_valid()) {
  1325. sdata->recreate_state();
  1326. } else {
  1327. sdata.instance();
  1328. }
  1329. } else {
  1330. sdata.instance();
  1331. }
  1332. Error err = sdata->pack(scene);
  1333. if (err != OK) {
  1334. show_accept(TTR("Couldn't save scene. Likely dependencies (instances or inheritance) couldn't be satisfied."), TTR("OK"));
  1335. return;
  1336. }
  1337. int flg = 0;
  1338. if (EditorSettings::get_singleton()->get("filesystem/on_save/compress_binary_resources")) {
  1339. flg |= ResourceSaver::FLAG_COMPRESS;
  1340. }
  1341. flg |= ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1342. err = ResourceSaver::save(p_file, sdata, flg);
  1343. // This needs to be emitted before saving external resources.
  1344. emit_signal("scene_saved", p_file);
  1345. _save_external_resources();
  1346. editor_data.save_editor_external_data();
  1347. for (List<Ref<AnimatedValuesBackup>>::Element *E = anim_backups.front(); E; E = E->next()) {
  1348. E->get()->restore();
  1349. }
  1350. if (err == OK) {
  1351. scene->set_filename(ProjectSettings::get_singleton()->localize_path(p_file));
  1352. if (idx < 0 || idx == editor_data.get_edited_scene()) {
  1353. set_current_version(editor_data.get_undo_redo().get_version());
  1354. } else {
  1355. editor_data.set_edited_scene_version(0, idx);
  1356. }
  1357. editor_data.set_scene_modified_time(idx, FileAccess::get_modified_time(p_file));
  1358. editor_folding.save_scene_folding(scene, p_file);
  1359. _update_title();
  1360. _update_scene_tabs();
  1361. } else {
  1362. _dialog_display_save_error(p_file, err);
  1363. }
  1364. }
  1365. void EditorNode::save_all_scenes() {
  1366. _menu_option_confirm(RUN_STOP, true);
  1367. _save_all_scenes();
  1368. }
  1369. void EditorNode::save_scene_list(Vector<String> p_scene_filenames) {
  1370. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1371. Node *scene = editor_data.get_edited_scene_root(i);
  1372. if (scene && (p_scene_filenames.find(scene->get_filename()) >= 0)) {
  1373. _save_scene(scene->get_filename(), i);
  1374. }
  1375. }
  1376. }
  1377. void EditorNode::restart_editor() {
  1378. exiting = true;
  1379. if (editor_run.get_status() != EditorRun::STATUS_STOP) {
  1380. editor_run.stop();
  1381. }
  1382. String to_reopen;
  1383. if (get_tree()->get_edited_scene_root()) {
  1384. to_reopen = get_tree()->get_edited_scene_root()->get_filename();
  1385. }
  1386. _exit_editor();
  1387. List<String> args;
  1388. const Vector<String> &forwardable_args = Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_TOOL);
  1389. for (int i = 0; i < forwardable_args.size(); i++) {
  1390. args.push_back(forwardable_args[i]);
  1391. }
  1392. args.push_back("--path");
  1393. args.push_back(ProjectSettings::get_singleton()->get_resource_path());
  1394. args.push_back("-e");
  1395. if (to_reopen != String()) {
  1396. args.push_back(to_reopen);
  1397. }
  1398. OS::get_singleton()->set_restart_on_exit(true, args);
  1399. }
  1400. void EditorNode::_save_all_scenes() {
  1401. bool all_saved = true;
  1402. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1403. Node *scene = editor_data.get_edited_scene_root(i);
  1404. if (scene) {
  1405. if (scene->get_filename() != "" && DirAccess::exists(scene->get_filename().get_base_dir())) {
  1406. if (i != editor_data.get_edited_scene()) {
  1407. _save_scene(scene->get_filename(), i);
  1408. } else {
  1409. _save_scene_with_preview(scene->get_filename());
  1410. }
  1411. } else if (scene->get_filename() != "") {
  1412. all_saved = false;
  1413. }
  1414. }
  1415. }
  1416. if (!all_saved) {
  1417. show_warning(TTR("Could not save one or more scenes!"), TTR("Save All Scenes"));
  1418. }
  1419. _save_default_environment();
  1420. }
  1421. void EditorNode::_mark_unsaved_scenes() {
  1422. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1423. Node *node = editor_data.get_edited_scene_root(i);
  1424. if (!node) {
  1425. continue;
  1426. }
  1427. String path = node->get_filename();
  1428. if (!(path == String() || FileAccess::exists(path))) {
  1429. if (i == editor_data.get_edited_scene()) {
  1430. set_current_version(-1);
  1431. } else {
  1432. editor_data.set_edited_scene_version(-1, i);
  1433. }
  1434. }
  1435. }
  1436. _update_title();
  1437. _update_scene_tabs();
  1438. }
  1439. void EditorNode::_dialog_action(String p_file) {
  1440. switch (current_option) {
  1441. case FILE_NEW_INHERITED_SCENE: {
  1442. Node *scene = editor_data.get_edited_scene_root();
  1443. // If the previous scene is rootless, just close it in favor of the new one.
  1444. if (!scene) {
  1445. _menu_option_confirm(FILE_CLOSE, true);
  1446. }
  1447. load_scene(p_file, false, true);
  1448. } break;
  1449. case FILE_OPEN_SCENE: {
  1450. load_scene(p_file);
  1451. } break;
  1452. case SETTINGS_PICK_MAIN_SCENE: {
  1453. ProjectSettings::get_singleton()->set("application/run/main_scene", p_file);
  1454. ProjectSettings::get_singleton()->save();
  1455. //would be nice to show the project manager opened with the highlighted field..
  1456. if (pick_main_scene->has_meta("from_native") && (bool)pick_main_scene->get_meta("from_native")) {
  1457. run_native->resume_run_native();
  1458. } else {
  1459. _run(false, ""); // automatically run the project
  1460. }
  1461. } break;
  1462. case FILE_CLOSE:
  1463. case FILE_CLOSE_ALL_AND_QUIT:
  1464. case FILE_CLOSE_ALL_AND_RUN_PROJECT_MANAGER:
  1465. case FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT:
  1466. case SCENE_TAB_CLOSE:
  1467. case FILE_SAVE_SCENE:
  1468. case FILE_SAVE_AS_SCENE: {
  1469. int scene_idx = (current_option == FILE_SAVE_SCENE || current_option == FILE_SAVE_AS_SCENE) ? -1 : tab_closing;
  1470. if (file->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  1471. bool same_open_scene = false;
  1472. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1473. if (editor_data.get_scene_path(i) == p_file && i != scene_idx) {
  1474. same_open_scene = true;
  1475. }
  1476. }
  1477. if (same_open_scene) {
  1478. show_warning(TTR("Can't overwrite scene that is still open!"));
  1479. return;
  1480. }
  1481. _save_default_environment();
  1482. _save_scene_with_preview(p_file, scene_idx);
  1483. _add_to_recent_scenes(p_file);
  1484. save_layout();
  1485. if (scene_idx != -1) {
  1486. _discard_changes();
  1487. }
  1488. }
  1489. } break;
  1490. case FILE_SAVE_AND_RUN: {
  1491. if (file->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  1492. _save_default_environment();
  1493. _save_scene_with_preview(p_file);
  1494. _run(false, p_file);
  1495. }
  1496. } break;
  1497. case FILE_EXPORT_MESH_LIBRARY: {
  1498. Ref<MeshLibrary> ml;
  1499. if (file_export_lib_merge->is_pressed() && FileAccess::exists(p_file)) {
  1500. ml = ResourceLoader::load(p_file, "MeshLibrary");
  1501. if (ml.is_null()) {
  1502. show_accept(TTR("Can't load MeshLibrary for merging!"), TTR("OK"));
  1503. return;
  1504. }
  1505. }
  1506. if (ml.is_null()) {
  1507. ml = Ref<MeshLibrary>(memnew(MeshLibrary));
  1508. }
  1509. MeshLibraryEditor::update_library_file(editor_data.get_edited_scene_root(), ml, true, file_export_lib_apply_xforms->is_pressed());
  1510. Error err = ResourceSaver::save(p_file, ml);
  1511. if (err) {
  1512. show_accept(TTR("Error saving MeshLibrary!"), TTR("OK"));
  1513. return;
  1514. }
  1515. } break;
  1516. case FILE_EXPORT_TILESET: {
  1517. Ref<TileSet> tileset;
  1518. if (FileAccess::exists(p_file) && file_export_lib_merge->is_pressed()) {
  1519. tileset = ResourceLoader::load(p_file, "TileSet");
  1520. if (tileset.is_null()) {
  1521. show_accept(TTR("Can't load TileSet for merging!"), TTR("OK"));
  1522. return;
  1523. }
  1524. } else {
  1525. tileset = Ref<TileSet>(memnew(TileSet));
  1526. }
  1527. TileSetEditor::update_library_file(editor_data.get_edited_scene_root(), tileset, true);
  1528. Error err = ResourceSaver::save(p_file, tileset);
  1529. if (err) {
  1530. show_accept(TTR("Error saving TileSet!"), TTR("OK"));
  1531. return;
  1532. }
  1533. } break;
  1534. case RESOURCE_SAVE:
  1535. case RESOURCE_SAVE_AS: {
  1536. ERR_FAIL_COND(saving_resource.is_null());
  1537. if (p_file.get_file().begins_with(".")) {
  1538. show_accept(TTR("Could not use a name with a leading dot."), TTR("OK"));
  1539. return;
  1540. }
  1541. save_resource_in_path(saving_resource, p_file);
  1542. saving_resource = Ref<Resource>();
  1543. ObjectID current = editor_history.get_current();
  1544. Object *current_obj = current > 0 ? ObjectDB::get_instance(current) : nullptr;
  1545. ERR_FAIL_COND(!current_obj);
  1546. current_obj->_change_notify();
  1547. } break;
  1548. case SETTINGS_LAYOUT_SAVE: {
  1549. if (p_file.empty()) {
  1550. return;
  1551. }
  1552. Ref<ConfigFile> config;
  1553. config.instance();
  1554. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  1555. if (err == ERR_FILE_CANT_OPEN || err == ERR_FILE_NOT_FOUND) {
  1556. config.instance(); // new config
  1557. } else if (err != OK) {
  1558. show_warning(TTR("An error occurred while trying to save the editor layout.\nMake sure the editor's user data path is writable."));
  1559. return;
  1560. }
  1561. _save_docks_to_config(config, p_file);
  1562. config->save(EditorSettings::get_singleton()->get_editor_layouts_config());
  1563. layout_dialog->hide();
  1564. _update_layouts_menu();
  1565. if (p_file == "Default") {
  1566. show_warning(TTR("Default editor layout overridden.\nTo restore the Default layout to its base settings, use the Delete Layout option and delete the Default layout."));
  1567. }
  1568. } break;
  1569. case SETTINGS_LAYOUT_DELETE: {
  1570. if (p_file.empty()) {
  1571. return;
  1572. }
  1573. Ref<ConfigFile> config;
  1574. config.instance();
  1575. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  1576. if (err != OK || !config->has_section(p_file)) {
  1577. show_warning(TTR("Layout name not found!"));
  1578. return;
  1579. }
  1580. // erase
  1581. List<String> keys;
  1582. config->get_section_keys(p_file, &keys);
  1583. for (List<String>::Element *E = keys.front(); E; E = E->next()) {
  1584. config->set_value(p_file, E->get(), Variant());
  1585. }
  1586. config->save(EditorSettings::get_singleton()->get_editor_layouts_config());
  1587. layout_dialog->hide();
  1588. _update_layouts_menu();
  1589. if (p_file == "Default") {
  1590. show_warning(TTR("Restored the Default layout to its base settings."));
  1591. }
  1592. } break;
  1593. default: { //save scene?
  1594. if (file->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  1595. _save_scene_with_preview(p_file);
  1596. }
  1597. } break;
  1598. }
  1599. }
  1600. bool EditorNode::item_has_editor(Object *p_object) {
  1601. if (_is_class_editor_disabled_by_feature_profile(p_object->get_class())) {
  1602. return false;
  1603. }
  1604. return editor_data.get_subeditors(p_object).size() > 0;
  1605. }
  1606. void EditorNode::edit_item_resource(RES p_resource) {
  1607. edit_item(p_resource.ptr());
  1608. }
  1609. bool EditorNode::_is_class_editor_disabled_by_feature_profile(const StringName &p_class) {
  1610. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  1611. if (profile.is_null()) {
  1612. return false;
  1613. }
  1614. StringName class_name = p_class;
  1615. while (class_name != StringName()) {
  1616. if (profile->is_class_disabled(class_name)) {
  1617. return true;
  1618. }
  1619. if (profile->is_class_editor_disabled(class_name)) {
  1620. return true;
  1621. }
  1622. class_name = ClassDB::get_parent_class(class_name);
  1623. }
  1624. return false;
  1625. }
  1626. void EditorNode::edit_item(Object *p_object) {
  1627. Vector<EditorPlugin *> sub_plugins;
  1628. if (p_object) {
  1629. if (_is_class_editor_disabled_by_feature_profile(p_object->get_class())) {
  1630. return;
  1631. }
  1632. sub_plugins = editor_data.get_subeditors(p_object);
  1633. }
  1634. if (!sub_plugins.empty()) {
  1635. bool same = true;
  1636. if (sub_plugins.size() == editor_plugins_over->get_plugins_list().size()) {
  1637. for (int i = 0; i < sub_plugins.size(); i++) {
  1638. if (sub_plugins[i] != editor_plugins_over->get_plugins_list()[i]) {
  1639. same = false;
  1640. }
  1641. }
  1642. } else {
  1643. same = false;
  1644. }
  1645. if (!same) {
  1646. _display_top_editors(false);
  1647. _set_top_editors(sub_plugins);
  1648. }
  1649. _set_editing_top_editors(p_object);
  1650. _display_top_editors(true);
  1651. } else {
  1652. hide_top_editors();
  1653. }
  1654. }
  1655. void EditorNode::push_item(Object *p_object, const String &p_property, bool p_inspector_only) {
  1656. if (!p_object) {
  1657. get_inspector()->edit(nullptr);
  1658. node_dock->set_node(nullptr);
  1659. scene_tree_dock->set_selected(nullptr);
  1660. inspector_dock->update(nullptr);
  1661. _display_top_editors(false);
  1662. return;
  1663. }
  1664. uint32_t id = p_object->get_instance_id();
  1665. if (id != editor_history.get_current()) {
  1666. if (p_inspector_only) {
  1667. editor_history.add_object_inspector_only(id);
  1668. } else if (p_property == "") {
  1669. editor_history.add_object(id);
  1670. } else {
  1671. editor_history.add_object(id, p_property);
  1672. }
  1673. }
  1674. _edit_current();
  1675. }
  1676. void EditorNode::_save_default_environment() {
  1677. Ref<Environment> fallback = get_tree()->get_root()->get_world()->get_fallback_environment();
  1678. if (fallback.is_valid() && fallback->get_path().is_resource_file()) {
  1679. Map<RES, bool> processed;
  1680. _find_and_save_edited_subresources(fallback.ptr(), processed, 0);
  1681. save_resource_in_path(fallback, fallback->get_path());
  1682. }
  1683. }
  1684. void EditorNode::hide_top_editors() {
  1685. _display_top_editors(false);
  1686. editor_plugins_over->clear();
  1687. }
  1688. void EditorNode::_display_top_editors(bool p_display) {
  1689. editor_plugins_over->make_visible(p_display);
  1690. }
  1691. void EditorNode::_set_top_editors(Vector<EditorPlugin *> p_editor_plugins_over) {
  1692. editor_plugins_over->set_plugins_list(p_editor_plugins_over);
  1693. }
  1694. void EditorNode::_set_editing_top_editors(Object *p_current_object) {
  1695. editor_plugins_over->edit(p_current_object);
  1696. }
  1697. static bool overrides_external_editor(Object *p_object) {
  1698. Script *script = Object::cast_to<Script>(p_object);
  1699. if (!script) {
  1700. return false;
  1701. }
  1702. return script->get_language()->overrides_external_editor();
  1703. }
  1704. void EditorNode::_edit_current(bool p_skip_foreign) {
  1705. uint32_t current = editor_history.get_current();
  1706. Object *current_obj = current > 0 ? ObjectDB::get_instance(current) : nullptr;
  1707. RES res = Object::cast_to<Resource>(current_obj);
  1708. if (p_skip_foreign && res.is_valid()) {
  1709. if (res->get_path().find("::") > -1 && res->get_path().get_slice("::", 0) != editor_data.get_scene_path(get_current_tab())) {
  1710. // Trying to edit resource that belongs to another scene; abort.
  1711. current_obj = nullptr;
  1712. }
  1713. }
  1714. bool inspector_only = editor_history.is_current_inspector_only();
  1715. this->current = current_obj;
  1716. if (!current_obj) {
  1717. scene_tree_dock->set_selected(nullptr);
  1718. get_inspector()->edit(nullptr);
  1719. node_dock->set_node(nullptr);
  1720. inspector_dock->update(nullptr);
  1721. _display_top_editors(false);
  1722. return;
  1723. }
  1724. Object *prev_inspected_object = get_inspector()->get_edited_object();
  1725. bool disable_folding = bool(EDITOR_GET("interface/inspector/disable_folding"));
  1726. bool is_resource = current_obj->is_class("Resource");
  1727. bool is_node = current_obj->is_class("Node");
  1728. bool stay_in_script_editor_on_node_selected = bool(EDITOR_GET("text_editor/navigation/stay_in_script_editor_on_node_selected"));
  1729. bool skip_main_plugin = false;
  1730. String editable_warning; //none by default
  1731. if (is_resource) {
  1732. Resource *current_res = Object::cast_to<Resource>(current_obj);
  1733. ERR_FAIL_COND(!current_res);
  1734. get_inspector()->edit(current_res);
  1735. scene_tree_dock->set_selected(nullptr);
  1736. node_dock->set_node(nullptr);
  1737. inspector_dock->update(nullptr);
  1738. EditorNode::get_singleton()->get_import_dock()->set_edit_path(current_res->get_path());
  1739. int subr_idx = current_res->get_path().find("::");
  1740. if (subr_idx != -1) {
  1741. String base_path = current_res->get_path().substr(0, subr_idx);
  1742. if (FileAccess::exists(base_path + ".import")) {
  1743. editable_warning = TTR("This resource belongs to a scene that was imported, so it's not editable.\nPlease read the documentation relevant to importing scenes to better understand this workflow.");
  1744. } else {
  1745. if ((!get_edited_scene() || get_edited_scene()->get_filename() != base_path) && ResourceLoader::get_resource_type(base_path) == "PackedScene") {
  1746. editable_warning = TTR("This resource belongs to a scene that was instanced or inherited.\nChanges to it won't be kept when saving the current scene.");
  1747. }
  1748. }
  1749. } else if (current_res->get_path().is_resource_file()) {
  1750. if (FileAccess::exists(current_res->get_path() + ".import")) {
  1751. editable_warning = TTR("This resource was imported, so it's not editable. Change its settings in the import panel and then re-import.");
  1752. }
  1753. }
  1754. } else if (is_node) {
  1755. Node *current_node = Object::cast_to<Node>(current_obj);
  1756. ERR_FAIL_COND(!current_node);
  1757. get_inspector()->edit(current_node);
  1758. if (current_node->is_inside_tree()) {
  1759. node_dock->set_node(current_node);
  1760. scene_tree_dock->set_selected(current_node);
  1761. inspector_dock->update(current_node);
  1762. if (!inspector_only && !skip_main_plugin) {
  1763. skip_main_plugin = stay_in_script_editor_on_node_selected && ScriptEditor::get_singleton()->is_visible_in_tree();
  1764. }
  1765. } else {
  1766. node_dock->set_node(nullptr);
  1767. scene_tree_dock->set_selected(nullptr);
  1768. inspector_dock->update(nullptr);
  1769. }
  1770. if (get_edited_scene() && get_edited_scene()->get_filename() != String()) {
  1771. String source_scene = get_edited_scene()->get_filename();
  1772. if (FileAccess::exists(source_scene + ".import")) {
  1773. editable_warning = TTR("This scene was imported, so changes to it won't be kept.\nInstancing it or inheriting will allow making changes to it.\nPlease read the documentation relevant to importing scenes to better understand this workflow.");
  1774. }
  1775. }
  1776. } else {
  1777. Node *selected_node = nullptr;
  1778. if (current_obj->is_class("ScriptEditorDebuggerInspectedObject")) {
  1779. editable_warning = TTR("This is a remote object, so changes to it won't be kept.\nPlease read the documentation relevant to debugging to better understand this workflow.");
  1780. disable_folding = true;
  1781. } else if (current_obj->is_class("MultiNodeEdit")) {
  1782. Node *scene = get_edited_scene();
  1783. if (scene) {
  1784. MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(current_obj);
  1785. int node_count = multi_node_edit->get_node_count();
  1786. if (node_count > 0) {
  1787. List<Node *> multi_nodes;
  1788. for (int node_index = 0; node_index < node_count; ++node_index) {
  1789. Node *node = scene->get_node(multi_node_edit->get_node(node_index));
  1790. if (node) {
  1791. multi_nodes.push_back(node);
  1792. }
  1793. }
  1794. if (!multi_nodes.empty()) {
  1795. // Pick the top-most node
  1796. multi_nodes.sort_custom<Node::Comparator>();
  1797. selected_node = multi_nodes.front()->get();
  1798. }
  1799. }
  1800. }
  1801. }
  1802. get_inspector()->edit(current_obj);
  1803. node_dock->set_node(nullptr);
  1804. scene_tree_dock->set_selected(selected_node);
  1805. inspector_dock->update(nullptr);
  1806. }
  1807. if (current_obj == prev_inspected_object) {
  1808. // Make sure inspected properties are restored.
  1809. get_inspector()->update_tree();
  1810. }
  1811. inspector_dock->set_warning(editable_warning);
  1812. if (get_inspector()->is_using_folding() == disable_folding) {
  1813. get_inspector()->set_use_folding(!disable_folding);
  1814. }
  1815. /* Take care of PLUGIN EDITOR */
  1816. if (!inspector_only) {
  1817. EditorPlugin *main_plugin = editor_data.get_editor(current_obj);
  1818. int plugin_index = 0;
  1819. for (; plugin_index < editor_table.size(); plugin_index++) {
  1820. if (editor_table[plugin_index] == main_plugin) {
  1821. if (!main_editor_buttons[plugin_index]->is_visible()) {
  1822. main_plugin = nullptr; //if button is not visible, then no plugin active
  1823. }
  1824. break;
  1825. }
  1826. }
  1827. if (main_plugin && !skip_main_plugin) {
  1828. // special case if use of external editor is true
  1829. Resource *current_res = Object::cast_to<Resource>(current_obj);
  1830. if (main_plugin->get_name() == "Script" && current_obj->get_class_name() != StringName("VisualScript") && current_res && !current_res->get_path().empty() && current_res->get_path().find("::") == -1 && (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")) || overrides_external_editor(current_obj))) {
  1831. if (!changing_scene) {
  1832. main_plugin->edit(current_obj);
  1833. }
  1834. }
  1835. else if (main_plugin != editor_plugin_screen && (!ScriptEditor::get_singleton() || !ScriptEditor::get_singleton()->is_visible_in_tree() || ScriptEditor::get_singleton()->can_take_away_focus())) {
  1836. // update screen main_plugin
  1837. _editor_select(plugin_index);
  1838. main_plugin->edit(current_obj);
  1839. } else {
  1840. editor_plugin_screen->edit(current_obj);
  1841. }
  1842. }
  1843. Vector<EditorPlugin *> sub_plugins;
  1844. if (!_is_class_editor_disabled_by_feature_profile(current_obj->get_class())) {
  1845. sub_plugins = editor_data.get_subeditors(current_obj);
  1846. }
  1847. if (!sub_plugins.empty()) {
  1848. _display_top_editors(false);
  1849. _set_top_editors(sub_plugins);
  1850. _set_editing_top_editors(current_obj);
  1851. _display_top_editors(true);
  1852. } else if (!editor_plugins_over->get_plugins_list().empty()) {
  1853. hide_top_editors();
  1854. }
  1855. }
  1856. inspector_dock->update(current_obj);
  1857. inspector_dock->update_keying();
  1858. }
  1859. void EditorNode::_run(bool p_current, const String &p_custom) {
  1860. if (editor_run.get_status() == EditorRun::STATUS_PLAY) {
  1861. play_button->set_pressed(!_playing_edited);
  1862. play_scene_button->set_pressed(_playing_edited);
  1863. return;
  1864. }
  1865. play_button->set_pressed(false);
  1866. play_button->set_icon(gui_base->get_icon("MainPlay", "EditorIcons"));
  1867. play_scene_button->set_pressed(false);
  1868. play_scene_button->set_icon(gui_base->get_icon("PlayScene", "EditorIcons"));
  1869. play_custom_scene_button->set_pressed(false);
  1870. play_custom_scene_button->set_icon(gui_base->get_icon("PlayCustom", "EditorIcons"));
  1871. String run_filename;
  1872. String args;
  1873. bool skip_breakpoints;
  1874. if (p_current || (editor_data.get_edited_scene_root() && p_custom != String() && p_custom == editor_data.get_edited_scene_root()->get_filename())) {
  1875. Node *scene = editor_data.get_edited_scene_root();
  1876. if (!scene) {
  1877. show_accept(TTR("There is no defined scene to run."), TTR("OK"));
  1878. return;
  1879. }
  1880. if (scene->get_filename() == "") {
  1881. current_option = -1;
  1882. _menu_option(FILE_SAVE_AS_SCENE);
  1883. // Set the option to save and run so when the dialog is accepted, the scene runs.
  1884. current_option = FILE_SAVE_AND_RUN;
  1885. file->set_title(TTR("Save scene before running..."));
  1886. return;
  1887. }
  1888. run_filename = scene->get_filename();
  1889. } else if (p_custom != "") {
  1890. run_filename = p_custom;
  1891. }
  1892. if (run_filename == "") {
  1893. //evidently, run the scene
  1894. if (!ensure_main_scene(false)) {
  1895. return;
  1896. }
  1897. }
  1898. if (bool(EDITOR_GET("run/auto_save/save_before_running"))) {
  1899. if (unsaved_cache) {
  1900. Node *scene = editor_data.get_edited_scene_root();
  1901. if (scene && scene->get_filename() != "") { // Only autosave if there is a scene and if it has a path.
  1902. _save_scene_with_preview(scene->get_filename());
  1903. }
  1904. }
  1905. _menu_option(FILE_SAVE_ALL_SCENES);
  1906. editor_data.save_editor_external_data();
  1907. }
  1908. if (!call_build()) {
  1909. return;
  1910. }
  1911. if (bool(EDITOR_GET("run/output/always_clear_output_on_play"))) {
  1912. log->clear();
  1913. }
  1914. if (bool(EDITOR_GET("run/output/always_open_output_on_play"))) {
  1915. make_bottom_panel_item_visible(log);
  1916. }
  1917. List<String> breakpoints;
  1918. editor_data.get_editor_breakpoints(&breakpoints);
  1919. args = ProjectSettings::get_singleton()->get("editor/main_run_args");
  1920. skip_breakpoints = ScriptEditor::get_singleton()->get_debugger()->is_skip_breakpoints();
  1921. emit_signal("play_pressed");
  1922. Error error = editor_run.run(run_filename, args, breakpoints, skip_breakpoints);
  1923. if (error != OK) {
  1924. emit_signal("stop_pressed");
  1925. show_accept(TTR("Could not start subprocess!"), TTR("OK"));
  1926. return;
  1927. }
  1928. if (p_current) {
  1929. play_scene_button->set_pressed(true);
  1930. play_scene_button->set_icon(gui_base->get_icon("Reload", "EditorIcons"));
  1931. } else if (p_custom != "") {
  1932. run_custom_filename = p_custom;
  1933. play_custom_scene_button->set_pressed(true);
  1934. play_custom_scene_button->set_icon(gui_base->get_icon("Reload", "EditorIcons"));
  1935. } else {
  1936. play_button->set_pressed(true);
  1937. play_button->set_icon(gui_base->get_icon("Reload", "EditorIcons"));
  1938. }
  1939. stop_button->set_disabled(false);
  1940. _playing_edited = p_current;
  1941. }
  1942. void EditorNode::_android_build_source_selected(const String &p_file) {
  1943. export_template_manager->install_android_template_from_file(p_file);
  1944. }
  1945. void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
  1946. if (!p_confirmed) { //this may be a hack..
  1947. current_option = (MenuOptions)p_option;
  1948. }
  1949. switch (p_option) {
  1950. case FILE_NEW_SCENE: {
  1951. new_scene();
  1952. } break;
  1953. case FILE_NEW_INHERITED_SCENE:
  1954. case FILE_OPEN_SCENE: {
  1955. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  1956. List<String> extensions;
  1957. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  1958. file->clear_filters();
  1959. for (int i = 0; i < extensions.size(); i++) {
  1960. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  1961. }
  1962. Node *scene = editor_data.get_edited_scene_root();
  1963. if (scene) {
  1964. file->set_current_path(scene->get_filename());
  1965. };
  1966. file->set_title(p_option == FILE_OPEN_SCENE ? TTR("Open Scene") : TTR("Open Base Scene"));
  1967. file->popup_centered_ratio();
  1968. } break;
  1969. case FILE_QUICK_OPEN: {
  1970. quick_open->popup_dialog("Resource", true);
  1971. quick_open->set_title(TTR("Quick Open..."));
  1972. } break;
  1973. case FILE_QUICK_OPEN_SCENE: {
  1974. quick_open->popup_dialog("PackedScene", true);
  1975. quick_open->set_title(TTR("Quick Open Scene..."));
  1976. } break;
  1977. case FILE_QUICK_OPEN_SCRIPT: {
  1978. quick_open->popup_dialog("Script", true);
  1979. quick_open->set_title(TTR("Quick Open Script..."));
  1980. } break;
  1981. case FILE_OPEN_PREV: {
  1982. if (previous_scenes.empty()) {
  1983. break;
  1984. }
  1985. opening_prev = true;
  1986. open_request(previous_scenes.back()->get());
  1987. previous_scenes.pop_back();
  1988. } break;
  1989. case FILE_CLOSE_OTHERS:
  1990. case FILE_CLOSE_RIGHT:
  1991. case FILE_CLOSE_ALL: {
  1992. if (editor_data.get_edited_scene_count() > 1 && (current_option != FILE_CLOSE_RIGHT || editor_data.get_edited_scene() < editor_data.get_edited_scene_count() - 1)) {
  1993. int next_tab = editor_data.get_edited_scene() + 1;
  1994. next_tab %= editor_data.get_edited_scene_count();
  1995. _scene_tab_closed(next_tab, current_option);
  1996. } else {
  1997. if (current_option != FILE_CLOSE_ALL) {
  1998. current_option = -1;
  1999. } else {
  2000. _scene_tab_closed(editor_data.get_edited_scene());
  2001. }
  2002. }
  2003. if (p_confirmed) {
  2004. _menu_option_confirm(SCENE_TAB_CLOSE, true);
  2005. }
  2006. } break;
  2007. case FILE_CLOSE_ALL_AND_QUIT:
  2008. case FILE_CLOSE_ALL_AND_RUN_PROJECT_MANAGER:
  2009. case FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT:
  2010. case FILE_CLOSE: {
  2011. if (!p_confirmed) {
  2012. tab_closing = p_option == FILE_CLOSE ? editor_data.get_edited_scene() : _next_unsaved_scene(false);
  2013. _scene_tab_changed(tab_closing);
  2014. if (unsaved_cache || p_option == FILE_CLOSE_ALL_AND_QUIT || p_option == FILE_CLOSE_ALL_AND_RUN_PROJECT_MANAGER || p_option == FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT) {
  2015. Node *scene_root = editor_data.get_edited_scene_root(tab_closing);
  2016. if (scene_root) {
  2017. String scene_filename = scene_root->get_filename();
  2018. if (p_option == FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT) {
  2019. save_confirmation->get_ok()->set_text(TTR("Save & Reload"));
  2020. save_confirmation->set_text(vformat(TTR("Save changes to '%s' before reloading?"), scene_filename != "" ? scene_filename : "unsaved scene"));
  2021. } else {
  2022. save_confirmation->get_ok()->set_text(TTR("Save & Close"));
  2023. save_confirmation->set_text(vformat(TTR("Save changes to '%s' before closing?"), scene_filename != "" ? scene_filename : "unsaved scene"));
  2024. }
  2025. save_confirmation->popup_centered_minsize();
  2026. break;
  2027. }
  2028. }
  2029. } else if (p_option == FILE_CLOSE) {
  2030. tab_closing = editor_data.get_edited_scene();
  2031. }
  2032. if (!editor_data.get_edited_scene_root(tab_closing)) {
  2033. // empty tab
  2034. _scene_tab_closed(tab_closing);
  2035. break;
  2036. }
  2037. FALLTHROUGH;
  2038. }
  2039. case SCENE_TAB_CLOSE:
  2040. case FILE_SAVE_SCENE: {
  2041. int scene_idx = (p_option == FILE_SAVE_SCENE) ? -1 : tab_closing;
  2042. Node *scene = editor_data.get_edited_scene_root(scene_idx);
  2043. if (scene && scene->get_filename() != "") {
  2044. if (DirAccess::exists(scene->get_filename().get_base_dir())) {
  2045. if (scene_idx != editor_data.get_edited_scene()) {
  2046. _save_scene_with_preview(scene->get_filename(), scene_idx);
  2047. } else {
  2048. _save_scene_with_preview(scene->get_filename());
  2049. }
  2050. if (scene_idx != -1) {
  2051. _discard_changes();
  2052. }
  2053. save_layout();
  2054. } else {
  2055. show_save_accept(vformat(TTR("%s no longer exists! Please specify a new save location."), scene->get_filename().get_base_dir()), TTR("OK"));
  2056. }
  2057. break;
  2058. }
  2059. FALLTHROUGH;
  2060. }
  2061. case FILE_SAVE_AS_SCENE: {
  2062. int scene_idx = (p_option == FILE_SAVE_SCENE || p_option == FILE_SAVE_AS_SCENE) ? -1 : tab_closing;
  2063. Node *scene = editor_data.get_edited_scene_root(scene_idx);
  2064. if (!scene) {
  2065. if (p_option == FILE_SAVE_SCENE) {
  2066. // Pressing Ctrl + S saves the current script if a scene is currently open, but it won't if the scene has no root node.
  2067. // Work around this by explicitly saving the script in this case (similar to pressing Ctrl + Alt + S).
  2068. ScriptEditor::get_singleton()->save_current_script();
  2069. }
  2070. const int saved = _save_external_resources();
  2071. if (saved > 0) {
  2072. show_accept(
  2073. vformat(TTR("The current scene has no root node, but %d modified external resource(s) were saved anyway."), saved),
  2074. TTR("OK"));
  2075. } else if (p_option == FILE_SAVE_AS_SCENE) {
  2076. // Don't show this dialog when pressing Ctrl + S to avoid interfering with script saving.
  2077. show_accept(
  2078. TTR("A root node is required to save the scene. You can add a root node using the Scene tree dock."),
  2079. TTR("OK"));
  2080. }
  2081. break;
  2082. }
  2083. file->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  2084. List<String> extensions;
  2085. Ref<PackedScene> sd = memnew(PackedScene);
  2086. ResourceSaver::get_recognized_extensions(sd, &extensions);
  2087. file->clear_filters();
  2088. for (int i = 0; i < extensions.size(); i++) {
  2089. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  2090. }
  2091. if (scene->get_filename() != "") {
  2092. String path = scene->get_filename();
  2093. file->set_current_path(path);
  2094. if (extensions.size()) {
  2095. String ext = path.get_extension().to_lower();
  2096. if (extensions.find(ext) == nullptr) {
  2097. file->set_current_path(path.replacen("." + ext, "." + extensions.front()->get()));
  2098. }
  2099. }
  2100. } else if (extensions.size()) {
  2101. String root_name = scene->get_name();
  2102. // Very similar to node naming logic.
  2103. switch (ProjectSettings::get_singleton()->get("editor/scene_naming").operator int()) {
  2104. case SCENE_NAME_CASING_AUTO:
  2105. // Use casing of the root node.
  2106. break;
  2107. case SCENE_NAME_CASING_PASCAL_CASE: {
  2108. root_name = root_name.capitalize().replace(" ", "");
  2109. } break;
  2110. case SCENE_NAME_CASING_SNAKE_CASE:
  2111. root_name = root_name.capitalize().replace(" ", "").replace("-", "_").camelcase_to_underscore();
  2112. break;
  2113. }
  2114. file->set_current_path(root_name + "." + extensions.front()->get().to_lower());
  2115. }
  2116. file->popup_centered_ratio();
  2117. file->set_title(TTR("Save Scene As..."));
  2118. } break;
  2119. case FILE_SAVE_ALL_SCENES: {
  2120. _save_all_scenes();
  2121. } break;
  2122. case FILE_EXPORT_PROJECT: {
  2123. project_export->popup_export();
  2124. } break;
  2125. case FILE_EXPORT_MESH_LIBRARY: {
  2126. if (!editor_data.get_edited_scene_root()) {
  2127. show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  2128. break;
  2129. }
  2130. List<String> extensions;
  2131. Ref<MeshLibrary> ml(memnew(MeshLibrary));
  2132. ResourceSaver::get_recognized_extensions(ml, &extensions);
  2133. file_export_lib->clear_filters();
  2134. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  2135. file_export_lib->add_filter("*." + E->get());
  2136. }
  2137. file_export_lib->popup_centered_ratio();
  2138. file_export_lib->set_title(TTR("Export Mesh Library"));
  2139. } break;
  2140. case FILE_EXPORT_TILESET: {
  2141. //Make sure that the scene has a root before trying to convert to tileset
  2142. if (!editor_data.get_edited_scene_root()) {
  2143. show_accept(TTR("This operation can't be done without a root node."), TTR("OK"));
  2144. break;
  2145. }
  2146. List<String> extensions;
  2147. Ref<TileSet> ml(memnew(TileSet));
  2148. ResourceSaver::get_recognized_extensions(ml, &extensions);
  2149. file_export_lib->clear_filters();
  2150. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  2151. file_export_lib->add_filter("*." + E->get());
  2152. }
  2153. file_export_lib->popup_centered_ratio();
  2154. file_export_lib->set_title(TTR("Export Tile Set"));
  2155. } break;
  2156. case FILE_IMPORT_SUBSCENE: {
  2157. if (!editor_data.get_edited_scene_root()) {
  2158. show_accept(TTR("This operation can't be done without a selected node."), TTR("OK"));
  2159. break;
  2160. }
  2161. scene_tree_dock->import_subscene();
  2162. } break;
  2163. case FILE_EXTERNAL_OPEN_SCENE: {
  2164. if (unsaved_cache && !p_confirmed) {
  2165. confirmation->get_ok()->set_text(TTR("Open"));
  2166. confirmation->set_text(TTR("Current scene not saved. Open anyway?"));
  2167. confirmation->popup_centered_minsize();
  2168. break;
  2169. }
  2170. bool oprev = opening_prev;
  2171. Error err = load_scene(external_file);
  2172. if (err == OK && oprev) {
  2173. previous_scenes.pop_back();
  2174. opening_prev = false;
  2175. }
  2176. } break;
  2177. case EDIT_UNDO: {
  2178. if (Input::get_singleton()->get_mouse_button_mask() & 0x7) {
  2179. log->add_message(TTR("Can't undo while mouse buttons are pressed."), EditorLog::MSG_TYPE_EDITOR);
  2180. } else {
  2181. String action = editor_data.get_undo_redo().get_current_action_name();
  2182. if (!editor_data.get_undo_redo().undo()) {
  2183. log->add_message(TTR("Nothing to undo."), EditorLog::MSG_TYPE_EDITOR);
  2184. } else if (action != "") {
  2185. log->add_message(vformat(TTR("Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2186. }
  2187. }
  2188. } break;
  2189. case EDIT_REDO: {
  2190. if (Input::get_singleton()->get_mouse_button_mask() & 0x7) {
  2191. log->add_message(TTR("Can't redo while mouse buttons are pressed."), EditorLog::MSG_TYPE_EDITOR);
  2192. } else {
  2193. if (!editor_data.get_undo_redo().redo()) {
  2194. log->add_message(TTR("Nothing to redo."), EditorLog::MSG_TYPE_EDITOR);
  2195. } else {
  2196. String action = editor_data.get_undo_redo().get_current_action_name();
  2197. log->add_message(vformat(TTR("Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2198. }
  2199. }
  2200. } break;
  2201. case EDIT_RELOAD_SAVED_SCENE: {
  2202. Node *scene = get_edited_scene();
  2203. if (!scene) {
  2204. break;
  2205. }
  2206. String filename = scene->get_filename();
  2207. if (filename == String()) {
  2208. show_warning(TTR("Can't reload a scene that was never saved."));
  2209. break;
  2210. }
  2211. if (unsaved_cache && !p_confirmed) {
  2212. confirmation->get_ok()->set_text(TTR("Reload Saved Scene"));
  2213. confirmation->set_text(
  2214. TTR("The current scene has unsaved changes.\nReload the saved scene anyway? This action cannot be undone."));
  2215. confirmation->popup_centered_minsize();
  2216. break;
  2217. }
  2218. int cur_idx = editor_data.get_edited_scene();
  2219. _remove_edited_scene();
  2220. Error err = load_scene(filename);
  2221. if (err != OK)
  2222. ERR_PRINT("Failed to load scene");
  2223. editor_data.move_edited_scene_to_index(cur_idx);
  2224. get_undo_redo()->clear_history(false);
  2225. scene_tabs->set_current_tab(cur_idx);
  2226. } break;
  2227. case RUN_PLAY: {
  2228. run_play();
  2229. } break;
  2230. case RUN_PLAY_CUSTOM_SCENE: {
  2231. if (run_custom_filename.empty() || editor_run.get_status() == EditorRun::STATUS_STOP) {
  2232. _menu_option_confirm(RUN_STOP, true);
  2233. quick_run->popup_dialog("PackedScene", true);
  2234. quick_run->set_title(TTR("Quick Run Scene..."));
  2235. play_custom_scene_button->set_pressed(false);
  2236. } else {
  2237. String last_custom_scene = run_custom_filename;
  2238. run_play_custom(last_custom_scene);
  2239. }
  2240. } break;
  2241. case RUN_STOP: {
  2242. if (editor_run.get_status() == EditorRun::STATUS_STOP) {
  2243. break;
  2244. }
  2245. editor_run.stop();
  2246. run_custom_filename.clear();
  2247. play_button->set_pressed(false);
  2248. play_button->set_icon(gui_base->get_icon("MainPlay", "EditorIcons"));
  2249. play_scene_button->set_pressed(false);
  2250. play_scene_button->set_icon(gui_base->get_icon("PlayScene", "EditorIcons"));
  2251. play_custom_scene_button->set_pressed(false);
  2252. play_custom_scene_button->set_icon(gui_base->get_icon("PlayCustom", "EditorIcons"));
  2253. stop_button->set_disabled(true);
  2254. if (bool(EDITOR_GET("run/output/always_close_output_on_stop"))) {
  2255. for (int i = 0; i < bottom_panel_items.size(); i++) {
  2256. if (bottom_panel_items[i].control == log) {
  2257. _bottom_panel_switch(false, i);
  2258. break;
  2259. }
  2260. }
  2261. }
  2262. emit_signal("stop_pressed");
  2263. } break;
  2264. case FILE_SHOW_IN_FILESYSTEM: {
  2265. String path = editor_data.get_scene_path(editor_data.get_edited_scene());
  2266. if (path != String()) {
  2267. filesystem_dock->navigate_to_path(path);
  2268. }
  2269. } break;
  2270. case RUN_PLAY_SCENE: {
  2271. run_play_current();
  2272. } break;
  2273. case RUN_PLAY_NATIVE: {
  2274. bool autosave = EDITOR_GET("run/auto_save/save_before_running");
  2275. if (autosave) {
  2276. _menu_option_confirm(FILE_SAVE_ALL_SCENES, false);
  2277. }
  2278. if (run_native->is_deploy_debug_remote_enabled()) {
  2279. _menu_option_confirm(RUN_STOP, true);
  2280. if (!call_build()) {
  2281. break; // build failed
  2282. }
  2283. emit_signal("play_pressed");
  2284. editor_run.run_native_notify();
  2285. }
  2286. } break;
  2287. case RUN_SCENE_SETTINGS: {
  2288. run_settings_dialog->popup_run_settings();
  2289. } break;
  2290. case RUN_SETTINGS: {
  2291. project_settings->popup_project_settings();
  2292. } break;
  2293. case FILE_INSTALL_ANDROID_SOURCE: {
  2294. if (p_confirmed) {
  2295. export_template_manager->install_android_template();
  2296. } else {
  2297. if (DirAccess::exists("res://android/build")) {
  2298. remove_android_build_template->popup_centered_minsize();
  2299. } else if (export_template_manager->can_install_android_template()) {
  2300. install_android_build_template->popup_centered_minsize();
  2301. } else {
  2302. custom_build_manage_templates->popup_centered_minsize();
  2303. }
  2304. }
  2305. } break;
  2306. case RUN_USER_DATA_FOLDER: {
  2307. // ensure_user_data_dir() to prevent the edge case: "Open User Data Folder" won't work after the project was renamed in ProjectSettingsEditor unless the project is saved
  2308. OS::get_singleton()->ensure_user_data_dir();
  2309. OS::get_singleton()->shell_open(String("file://") + OS::get_singleton()->get_user_data_dir());
  2310. } break;
  2311. case FILE_EXPLORE_ANDROID_BUILD_TEMPLATES: {
  2312. OS::get_singleton()->shell_open("file://" + ProjectSettings::get_singleton()->get_resource_path().plus_file("android"));
  2313. } break;
  2314. case FILE_QUIT:
  2315. case RUN_PROJECT_MANAGER:
  2316. case RELOAD_CURRENT_PROJECT: {
  2317. if (!p_confirmed) {
  2318. bool save_each = EDITOR_GET("interface/editor/save_each_scene_on_quit");
  2319. if (_next_unsaved_scene(!save_each) == -1) {
  2320. bool confirm = (p_option != RELOAD_CURRENT_PROJECT) && EDITOR_GET("interface/editor/quit_confirmation");
  2321. if (confirm) {
  2322. confirmation->get_ok()->set_text(p_option == FILE_QUIT ? TTR("Quit") : TTR("Yes"));
  2323. confirmation->set_text(p_option == FILE_QUIT ? TTR("Exit the editor?") : TTR("Open Project Manager?"));
  2324. confirmation->popup_centered_minsize();
  2325. } else {
  2326. _discard_changes();
  2327. break;
  2328. }
  2329. } else {
  2330. if (save_each) {
  2331. if (p_option == RELOAD_CURRENT_PROJECT) {
  2332. _menu_option_confirm(FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT, false);
  2333. } else if (p_option == FILE_QUIT) {
  2334. _menu_option_confirm(FILE_CLOSE_ALL_AND_QUIT, false);
  2335. } else {
  2336. _menu_option_confirm(FILE_CLOSE_ALL_AND_RUN_PROJECT_MANAGER, false);
  2337. }
  2338. } else {
  2339. String unsaved_scenes;
  2340. int i = _next_unsaved_scene(true, 0);
  2341. while (i != -1) {
  2342. unsaved_scenes += "\n " + editor_data.get_edited_scene_root(i)->get_filename();
  2343. i = _next_unsaved_scene(true, ++i);
  2344. }
  2345. if (p_option == RELOAD_CURRENT_PROJECT) {
  2346. save_confirmation->get_ok()->set_text(TTR("Save & Reload"));
  2347. save_confirmation->set_text(TTR("Save changes to the following scene(s) before reloading?") + unsaved_scenes);
  2348. } else {
  2349. save_confirmation->get_ok()->set_text(TTR("Save & Quit"));
  2350. save_confirmation->set_text((p_option == FILE_QUIT ? TTR("Save changes to the following scene(s) before quitting?") : TTR("Save changes to the following scene(s) before opening Project Manager?")) + unsaved_scenes);
  2351. }
  2352. save_confirmation->popup_centered_minsize();
  2353. }
  2354. }
  2355. OS::get_singleton()->request_attention();
  2356. break;
  2357. }
  2358. if (_next_unsaved_scene(true) != -1) {
  2359. _save_all_scenes();
  2360. }
  2361. _discard_changes();
  2362. } break;
  2363. case RUN_FILE_SERVER: {
  2364. bool ischecked = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(RUN_FILE_SERVER));
  2365. if (ischecked) {
  2366. file_server->stop();
  2367. run_native->set_deploy_dumb(false);
  2368. } else {
  2369. file_server->start();
  2370. run_native->set_deploy_dumb(true);
  2371. }
  2372. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(RUN_FILE_SERVER), !ischecked);
  2373. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_file_server", !ischecked);
  2374. } break;
  2375. case RUN_LIVE_DEBUG: {
  2376. bool ischecked = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(RUN_LIVE_DEBUG));
  2377. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(RUN_LIVE_DEBUG), !ischecked);
  2378. ScriptEditor::get_singleton()->get_debugger()->set_live_debugging(!ischecked);
  2379. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_live_debug", !ischecked);
  2380. } break;
  2381. case RUN_DEPLOY_REMOTE_DEBUG: {
  2382. bool ischecked = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEPLOY_REMOTE_DEBUG));
  2383. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEPLOY_REMOTE_DEBUG), !ischecked);
  2384. run_native->set_deploy_debug_remote(!ischecked);
  2385. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_deploy_remote_debug", !ischecked);
  2386. } break;
  2387. case RUN_DEBUG_COLLISONS: {
  2388. bool ischecked = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEBUG_COLLISONS));
  2389. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEBUG_COLLISONS), !ischecked);
  2390. run_native->set_debug_collisions(!ischecked);
  2391. editor_run.set_debug_collisions(!ischecked);
  2392. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_debug_collisons", !ischecked);
  2393. } break;
  2394. case RUN_DEBUG_NAVIGATION: {
  2395. bool ischecked = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEBUG_NAVIGATION));
  2396. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEBUG_NAVIGATION), !ischecked);
  2397. run_native->set_debug_navigation(!ischecked);
  2398. editor_run.set_debug_navigation(!ischecked);
  2399. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_debug_navigation", !ischecked);
  2400. } break;
  2401. case RUN_DEBUG_SHADER_FALLBACKS: {
  2402. bool ischecked = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEBUG_SHADER_FALLBACKS));
  2403. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(RUN_DEBUG_SHADER_FALLBACKS), !ischecked);
  2404. run_native->set_debug_shader_fallbacks(!ischecked);
  2405. editor_run.set_debug_shader_fallbacks(!ischecked);
  2406. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_debug_shader_fallbacks", !ischecked);
  2407. } break;
  2408. case RUN_RELOAD_SCRIPTS: {
  2409. bool ischecked = debug_menu->get_popup()->is_item_checked(debug_menu->get_popup()->get_item_index(RUN_RELOAD_SCRIPTS));
  2410. debug_menu->get_popup()->set_item_checked(debug_menu->get_popup()->get_item_index(RUN_RELOAD_SCRIPTS), !ischecked);
  2411. ScriptEditor::get_singleton()->set_live_auto_reload_running_scripts(!ischecked);
  2412. EditorSettings::get_singleton()->set_project_metadata("debug_options", "run_reload_scripts", !ischecked);
  2413. } break;
  2414. case SETTINGS_UPDATE_CONTINUOUSLY: {
  2415. EditorSettings::get_singleton()->set("interface/editor/update_continuously", true);
  2416. _update_update_spinner();
  2417. show_accept(TTR("This option is deprecated. Situations where refresh must be forced are now considered a bug. Please report."), TTR("OK"));
  2418. } break;
  2419. case SETTINGS_UPDATE_WHEN_CHANGED: {
  2420. EditorSettings::get_singleton()->set("interface/editor/update_continuously", false);
  2421. EditorSettings::get_singleton()->set("interface/editor/update_vital_only", false);
  2422. _update_update_spinner();
  2423. } break;
  2424. case SETTINGS_UPDATE_VITAL_ONLY: {
  2425. EditorSettings::get_singleton()->set("interface/editor/update_continuously", false);
  2426. EditorSettings::get_singleton()->set("interface/editor/update_vital_only", true);
  2427. _update_update_spinner();
  2428. } break;
  2429. case SETTINGS_UPDATE_SPINNER_HIDE: {
  2430. EditorSettings::get_singleton()->set("interface/editor/show_update_spinner", false);
  2431. _update_update_spinner();
  2432. } break;
  2433. case SETTINGS_PREFERENCES: {
  2434. settings_config_dialog->popup_edit_settings();
  2435. } break;
  2436. case SETTINGS_EDITOR_DATA_FOLDER: {
  2437. OS::get_singleton()->shell_open(String("file://") + EditorSettings::get_singleton()->get_data_dir());
  2438. } break;
  2439. case SETTINGS_EDITOR_CONFIG_FOLDER: {
  2440. OS::get_singleton()->shell_open(String("file://") + EditorSettings::get_singleton()->get_settings_dir());
  2441. } break;
  2442. case SETTINGS_MANAGE_EXPORT_TEMPLATES: {
  2443. export_template_manager->popup_manager();
  2444. } break;
  2445. case SETTINGS_INSTALL_ANDROID_BUILD_TEMPLATE: {
  2446. custom_build_manage_templates->hide();
  2447. file_android_build_source->popup_centered_ratio();
  2448. } break;
  2449. case SETTINGS_MANAGE_FEATURE_PROFILES: {
  2450. feature_profile_manager->popup_centered_clamped(Size2(900, 800) * EDSCALE, 0.8);
  2451. } break;
  2452. case SETTINGS_TOGGLE_FULLSCREEN: {
  2453. OS::get_singleton()->set_window_fullscreen(!OS::get_singleton()->is_window_fullscreen());
  2454. } break;
  2455. case EDITOR_SCREENSHOT: {
  2456. screenshot_timer->start();
  2457. } break;
  2458. case SETTINGS_PICK_MAIN_SCENE: {
  2459. file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  2460. List<String> extensions;
  2461. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  2462. file->clear_filters();
  2463. for (int i = 0; i < extensions.size(); i++) {
  2464. file->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  2465. }
  2466. Node *scene = editor_data.get_edited_scene_root();
  2467. if (scene) {
  2468. file->set_current_path(scene->get_filename());
  2469. };
  2470. file->set_title(TTR("Pick a Main Scene"));
  2471. file->popup_centered_ratio();
  2472. } break;
  2473. case HELP_SEARCH: {
  2474. emit_signal("request_help_search", "");
  2475. } break;
  2476. case HELP_DOCS: {
  2477. OS::get_singleton()->shell_open(VERSION_DOCS_URL "/");
  2478. } break;
  2479. case HELP_QA: {
  2480. OS::get_singleton()->shell_open("https://godotengine.org/qa/");
  2481. } break;
  2482. case HELP_REPORT_A_BUG: {
  2483. OS::get_singleton()->shell_open("https://github.com/godotengine/godot/issues");
  2484. } break;
  2485. case HELP_SUGGEST_A_FEATURE: {
  2486. OS::get_singleton()->shell_open("https://github.com/godotengine/godot-proposals#readme");
  2487. } break;
  2488. case HELP_SEND_DOCS_FEEDBACK: {
  2489. OS::get_singleton()->shell_open("https://github.com/godotengine/godot-docs/issues");
  2490. } break;
  2491. case HELP_COMMUNITY: {
  2492. OS::get_singleton()->shell_open("https://godotengine.org/community");
  2493. } break;
  2494. case HELP_ABOUT: {
  2495. about->popup_centered_minsize(Size2(780, 500) * EDSCALE);
  2496. } break;
  2497. case HELP_SUPPORT_GODOT_DEVELOPMENT: {
  2498. OS::get_singleton()->shell_open("https://godotengine.org/donate");
  2499. } break;
  2500. case SET_VIDEO_DRIVER_SAVE_AND_RESTART: {
  2501. ProjectSettings::get_singleton()->set("rendering/quality/driver/driver_name", video_driver_request);
  2502. ProjectSettings::get_singleton()->save();
  2503. save_all_scenes();
  2504. restart_editor();
  2505. } break;
  2506. }
  2507. }
  2508. void EditorNode::_request_screenshot() {
  2509. _screenshot();
  2510. }
  2511. void EditorNode::_screenshot(bool p_use_utc) {
  2512. String name = "editor_screenshot_" + OS::get_singleton()->get_iso_date_time(p_use_utc).replace(":", "") + ".png";
  2513. NodePath path = String("user://") + name;
  2514. _save_screenshot(path);
  2515. if (EditorSettings::get_singleton()->get("interface/editor/automatically_open_screenshots")) {
  2516. OS::get_singleton()->shell_open(String("file://") + ProjectSettings::get_singleton()->globalize_path(path));
  2517. }
  2518. }
  2519. void EditorNode::_save_screenshot(NodePath p_path) {
  2520. Viewport *viewport = EditorInterface::get_singleton()->get_editor_viewport()->get_viewport();
  2521. viewport->set_clear_mode(Viewport::CLEAR_MODE_ONLY_NEXT_FRAME);
  2522. Ref<Image> img = viewport->get_texture()->get_data();
  2523. img->flip_y();
  2524. viewport->set_clear_mode(Viewport::CLEAR_MODE_ALWAYS);
  2525. Error error = img->save_png(p_path);
  2526. ERR_FAIL_COND_MSG(error != OK, "Cannot save screenshot to file '" + p_path + "'.");
  2527. }
  2528. void EditorNode::_tool_menu_option(int p_idx) {
  2529. switch (tool_menu->get_item_id(p_idx)) {
  2530. case TOOLS_ORPHAN_RESOURCES: {
  2531. orphan_resources->show();
  2532. } break;
  2533. case TOOLS_CUSTOM: {
  2534. if (tool_menu->get_item_submenu(p_idx) == "") {
  2535. Array params = tool_menu->get_item_metadata(p_idx);
  2536. Object *handler = ObjectDB::get_instance(params[0]);
  2537. String callback = params[1];
  2538. Variant *ud = &params[2];
  2539. Variant::CallError ce;
  2540. handler->call(callback, (const Variant **)&ud, 1, ce);
  2541. if (ce.error != Variant::CallError::CALL_OK) {
  2542. String err = Variant::get_call_error_text(handler, callback, (const Variant **)&ud, 1, ce);
  2543. ERR_PRINT("Error calling function from tool menu: " + err);
  2544. }
  2545. } // else it's a submenu so don't do anything.
  2546. } break;
  2547. }
  2548. }
  2549. int EditorNode::_next_unsaved_scene(bool p_valid_filename, int p_start) {
  2550. for (int i = p_start; i < editor_data.get_edited_scene_count(); i++) {
  2551. if (!editor_data.get_edited_scene_root(i)) {
  2552. continue;
  2553. }
  2554. int current = editor_data.get_edited_scene();
  2555. bool unsaved = (i == current) ? saved_version != editor_data.get_undo_redo().get_version() : editor_data.get_scene_version(i) != 0;
  2556. if (unsaved) {
  2557. String scene_filename = editor_data.get_edited_scene_root(i)->get_filename();
  2558. if (p_valid_filename && scene_filename.length() == 0) {
  2559. continue;
  2560. }
  2561. return i;
  2562. }
  2563. }
  2564. return -1;
  2565. }
  2566. void EditorNode::_exit_editor() {
  2567. exiting = true;
  2568. resource_preview->stop(); //stop early to avoid crashes
  2569. _save_docks();
  2570. // Dim the editor window while it's quitting to make it clearer that it's busy
  2571. dim_editor(true, true);
  2572. get_tree()->quit();
  2573. }
  2574. void EditorNode::_discard_changes(const String &p_str) {
  2575. switch (current_option) {
  2576. case FILE_CLOSE_ALL_AND_QUIT:
  2577. case FILE_CLOSE_ALL_AND_RUN_PROJECT_MANAGER:
  2578. case FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT:
  2579. case FILE_CLOSE:
  2580. case FILE_CLOSE_OTHERS:
  2581. case FILE_CLOSE_RIGHT:
  2582. case FILE_CLOSE_ALL:
  2583. case SCENE_TAB_CLOSE: {
  2584. Node *scene = editor_data.get_edited_scene_root(tab_closing);
  2585. if (scene != nullptr) {
  2586. String scene_filename = scene->get_filename();
  2587. if (scene_filename != "") {
  2588. previous_scenes.push_back(scene_filename);
  2589. }
  2590. }
  2591. _remove_scene(tab_closing);
  2592. _update_scene_tabs();
  2593. if (current_option == FILE_CLOSE_ALL_AND_QUIT || current_option == FILE_CLOSE_ALL_AND_RUN_PROJECT_MANAGER || current_option == FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT) {
  2594. // If restore tabs is enabled, reopen the scene that has just been closed, so it's remembered properly.
  2595. if (bool(EDITOR_GET("interface/scene_tabs/restore_scenes_on_load"))) {
  2596. _menu_option_confirm(FILE_OPEN_PREV, true);
  2597. }
  2598. if (_next_unsaved_scene(false) == -1) {
  2599. if (current_option == FILE_CLOSE_ALL_AND_RELOAD_CURRENT_PROJECT) {
  2600. current_option = RELOAD_CURRENT_PROJECT;
  2601. } else if (current_option == FILE_CLOSE_ALL_AND_QUIT) {
  2602. current_option = FILE_QUIT;
  2603. } else {
  2604. current_option = RUN_PROJECT_MANAGER;
  2605. }
  2606. _discard_changes();
  2607. } else {
  2608. _menu_option_confirm(current_option, false);
  2609. }
  2610. } else if (current_option == FILE_CLOSE_OTHERS || current_option == FILE_CLOSE_RIGHT) {
  2611. if (editor_data.get_edited_scene_count() == 1 || (current_option == FILE_CLOSE_RIGHT && editor_data.get_edited_scene_count() <= editor_data.get_edited_scene() + 1)) {
  2612. current_option = -1;
  2613. save_confirmation->hide();
  2614. } else {
  2615. _menu_option_confirm(current_option, false);
  2616. }
  2617. } else if (current_option == FILE_CLOSE_ALL && editor_data.get_edited_scene_count() > 0) {
  2618. _menu_option_confirm(current_option, false);
  2619. } else {
  2620. current_option = -1;
  2621. save_confirmation->hide();
  2622. }
  2623. } break;
  2624. case FILE_QUIT: {
  2625. _menu_option_confirm(RUN_STOP, true);
  2626. _exit_editor();
  2627. } break;
  2628. case RUN_PROJECT_MANAGER: {
  2629. _menu_option_confirm(RUN_STOP, true);
  2630. _exit_editor();
  2631. String exec = OS::get_singleton()->get_executable_path();
  2632. List<String> args;
  2633. const Vector<String> &forwardable_args = Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_TOOL);
  2634. for (int i = 0; i < forwardable_args.size(); i++) {
  2635. args.push_back(forwardable_args[i]);
  2636. }
  2637. String exec_base_dir = exec.get_base_dir();
  2638. if (!exec_base_dir.empty()) {
  2639. args.push_back("--path");
  2640. args.push_back(exec_base_dir);
  2641. }
  2642. args.push_back("--project-manager");
  2643. OS::ProcessID pid = 0;
  2644. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  2645. ERR_FAIL_COND(err);
  2646. } break;
  2647. case RELOAD_CURRENT_PROJECT: {
  2648. restart_editor();
  2649. } break;
  2650. }
  2651. }
  2652. void EditorNode::_update_debug_options() {
  2653. bool check_deploy_remote = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_deploy_remote_debug", false);
  2654. bool check_file_server = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_file_server", false);
  2655. bool check_debug_collisons = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_collisons", false);
  2656. bool check_debug_navigation = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_navigation", false);
  2657. bool check_debug_shader_fallbacks = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_shader_fallbacks", false);
  2658. bool check_live_debug = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_live_debug", true);
  2659. bool check_reload_scripts = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_reload_scripts", true);
  2660. if (check_deploy_remote) {
  2661. _menu_option_confirm(RUN_DEPLOY_REMOTE_DEBUG, true);
  2662. }
  2663. if (check_file_server) {
  2664. _menu_option_confirm(RUN_FILE_SERVER, true);
  2665. }
  2666. if (check_debug_collisons) {
  2667. _menu_option_confirm(RUN_DEBUG_COLLISONS, true);
  2668. }
  2669. if (check_debug_navigation) {
  2670. _menu_option_confirm(RUN_DEBUG_NAVIGATION, true);
  2671. }
  2672. if (check_debug_shader_fallbacks) {
  2673. _menu_option_confirm(RUN_DEBUG_SHADER_FALLBACKS, true);
  2674. }
  2675. if (check_live_debug) {
  2676. _menu_option_confirm(RUN_LIVE_DEBUG, true);
  2677. }
  2678. if (check_reload_scripts) {
  2679. _menu_option_confirm(RUN_RELOAD_SCRIPTS, true);
  2680. }
  2681. }
  2682. void EditorNode::_update_file_menu_opened() {
  2683. Ref<ShortCut> close_scene_sc = ED_GET_SHORTCUT("editor/close_scene");
  2684. close_scene_sc->set_name(TTR("Close Scene"));
  2685. Ref<ShortCut> reopen_closed_scene_sc = ED_GET_SHORTCUT("editor/reopen_closed_scene");
  2686. reopen_closed_scene_sc->set_name(TTR("Reopen Closed Scene"));
  2687. PopupMenu *pop = file_menu->get_popup();
  2688. pop->set_item_disabled(pop->get_item_index(FILE_OPEN_PREV), previous_scenes.empty());
  2689. const UndoRedo &undo_redo = editor_data.get_undo_redo();
  2690. pop->set_item_disabled(pop->get_item_index(EDIT_UNDO), !undo_redo.has_undo());
  2691. pop->set_item_disabled(pop->get_item_index(EDIT_REDO), !undo_redo.has_redo());
  2692. }
  2693. void EditorNode::_update_file_menu_closed() {
  2694. PopupMenu *pop = file_menu->get_popup();
  2695. pop->set_item_disabled(pop->get_item_index(FILE_OPEN_PREV), false);
  2696. }
  2697. Control *EditorNode::get_viewport() {
  2698. return viewport;
  2699. }
  2700. void EditorNode::_editor_select(int p_which) {
  2701. static bool selecting = false;
  2702. if (selecting || changing_scene) {
  2703. return;
  2704. }
  2705. ERR_FAIL_INDEX(p_which, editor_table.size());
  2706. if (!main_editor_buttons[p_which]->is_visible()) { //button hidden, no editor
  2707. return;
  2708. }
  2709. selecting = true;
  2710. for (int i = 0; i < main_editor_buttons.size(); i++) {
  2711. main_editor_buttons[i]->set_pressed(i == p_which);
  2712. }
  2713. selecting = false;
  2714. EditorPlugin *new_editor = editor_table[p_which];
  2715. ERR_FAIL_COND(!new_editor);
  2716. if (editor_plugin_screen == new_editor) {
  2717. return;
  2718. }
  2719. if (editor_plugin_screen) {
  2720. editor_plugin_screen->make_visible(false);
  2721. }
  2722. editor_plugin_screen = new_editor;
  2723. editor_plugin_screen->make_visible(true);
  2724. editor_plugin_screen->selected_notify();
  2725. int plugin_count = editor_data.get_editor_plugin_count();
  2726. for (int i = 0; i < plugin_count; i++) {
  2727. editor_data.get_editor_plugin(i)->notify_main_screen_changed(editor_plugin_screen->get_name());
  2728. }
  2729. if (EditorSettings::get_singleton()->get("interface/editor/separate_distraction_mode")) {
  2730. if (p_which == EDITOR_SCRIPT) {
  2731. set_distraction_free_mode(script_distraction);
  2732. } else {
  2733. set_distraction_free_mode(scene_distraction);
  2734. }
  2735. }
  2736. }
  2737. void EditorNode::select_editor_by_name(const String &p_name) {
  2738. ERR_FAIL_COND(p_name == "");
  2739. for (int i = 0; i < main_editor_buttons.size(); i++) {
  2740. if (main_editor_buttons[i]->get_text() == p_name) {
  2741. _editor_select(i);
  2742. return;
  2743. }
  2744. }
  2745. ERR_FAIL_MSG("The editor name '" + p_name + "' was not found.");
  2746. }
  2747. void EditorNode::add_editor_plugin(EditorPlugin *p_editor, bool p_config_changed) {
  2748. if (p_editor->has_main_screen()) {
  2749. ToolButton *tb = memnew(ToolButton);
  2750. tb->set_toggle_mode(true);
  2751. tb->connect("pressed", singleton, "_editor_select", varray(singleton->main_editor_buttons.size()));
  2752. tb->set_text(p_editor->get_name());
  2753. Ref<Texture> icon = p_editor->get_icon();
  2754. if (icon.is_valid()) {
  2755. tb->set_icon(icon);
  2756. } else if (singleton->gui_base->has_icon(p_editor->get_name(), "EditorIcons")) {
  2757. tb->set_icon(singleton->gui_base->get_icon(p_editor->get_name(), "EditorIcons"));
  2758. }
  2759. tb->set_name(p_editor->get_name());
  2760. singleton->main_editor_buttons.push_back(tb);
  2761. singleton->main_editor_button_vb->add_child(tb);
  2762. singleton->editor_table.push_back(p_editor);
  2763. singleton->distraction_free->raise();
  2764. }
  2765. singleton->editor_data.add_editor_plugin(p_editor);
  2766. singleton->add_child(p_editor);
  2767. if (p_config_changed) {
  2768. p_editor->enable_plugin();
  2769. }
  2770. }
  2771. void EditorNode::remove_editor_plugin(EditorPlugin *p_editor, bool p_config_changed) {
  2772. if (p_editor->has_main_screen()) {
  2773. for (int i = 0; i < singleton->main_editor_buttons.size(); i++) {
  2774. if (p_editor->get_name() == singleton->main_editor_buttons[i]->get_text()) {
  2775. if (singleton->main_editor_buttons[i]->is_pressed()) {
  2776. singleton->_editor_select(EDITOR_SCRIPT);
  2777. }
  2778. memdelete(singleton->main_editor_buttons[i]);
  2779. singleton->main_editor_buttons.remove(i);
  2780. break;
  2781. }
  2782. }
  2783. singleton->editor_table.erase(p_editor);
  2784. }
  2785. p_editor->make_visible(false);
  2786. p_editor->clear();
  2787. if (p_config_changed) {
  2788. p_editor->disable_plugin();
  2789. }
  2790. singleton->editor_plugins_over->remove_plugin(p_editor);
  2791. singleton->editor_plugins_force_over->remove_plugin(p_editor);
  2792. singleton->editor_plugins_force_input_forwarding->remove_plugin(p_editor);
  2793. singleton->remove_child(p_editor);
  2794. singleton->editor_data.remove_editor_plugin(p_editor);
  2795. }
  2796. void EditorNode::_update_addon_config() {
  2797. if (_initializing_addons) {
  2798. return;
  2799. }
  2800. Vector<String> enabled_addons;
  2801. for (Map<String, EditorPlugin *>::Element *E = plugin_addons.front(); E; E = E->next()) {
  2802. enabled_addons.push_back(E->key());
  2803. }
  2804. if (enabled_addons.size() == 0) {
  2805. ProjectSettings::get_singleton()->set("editor_plugins/enabled", Variant());
  2806. } else {
  2807. ProjectSettings::get_singleton()->set("editor_plugins/enabled", enabled_addons);
  2808. }
  2809. project_settings->queue_save();
  2810. }
  2811. void EditorNode::set_addon_plugin_enabled(String p_addon, bool p_enabled, bool p_config_changed) {
  2812. if (!p_addon.begins_with("res://")) {
  2813. p_addon = _to_absolute_plugin_path(p_addon);
  2814. }
  2815. ERR_FAIL_COND(p_enabled && plugin_addons.has(p_addon));
  2816. ERR_FAIL_COND(!p_enabled && !plugin_addons.has(p_addon));
  2817. if (!p_enabled) {
  2818. EditorPlugin *addon = plugin_addons[p_addon];
  2819. remove_editor_plugin(addon, p_config_changed);
  2820. memdelete(addon); //bye
  2821. plugin_addons.erase(p_addon);
  2822. _update_addon_config();
  2823. return;
  2824. }
  2825. Ref<ConfigFile> cf;
  2826. cf.instance();
  2827. if (!DirAccess::exists(p_addon.get_base_dir())) {
  2828. _remove_plugin_from_enabled(p_addon);
  2829. WARN_PRINT("Addon '" + p_addon + "' failed to load. No directory found. Removing from enabled plugins.");
  2830. return;
  2831. }
  2832. Error err = cf->load(p_addon);
  2833. if (err != OK) {
  2834. show_warning(vformat(TTR("Unable to enable addon plugin at: '%s' parsing of config failed."), p_addon));
  2835. return;
  2836. }
  2837. if (!cf->has_section_key("plugin", "script")) {
  2838. show_warning(vformat(TTR("Unable to find script field for addon plugin at: '%s'."), p_addon));
  2839. return;
  2840. }
  2841. String script_path = cf->get_value("plugin", "script");
  2842. Ref<Script> script; // We need to save it for creating "ep" below.
  2843. // Only try to load the script if it has a name. Else, the plugin has no init script.
  2844. if (script_path.length() > 0) {
  2845. script_path = p_addon.get_base_dir().plus_file(script_path);
  2846. script = ResourceLoader::load(script_path);
  2847. if (script.is_null()) {
  2848. show_warning(vformat(TTR("Unable to load addon script from path: '%s'."), script_path));
  2849. return;
  2850. }
  2851. // Errors in the script cause the base_type to be an empty string.
  2852. if (String(script->get_instance_base_type()) == "") {
  2853. show_warning(vformat(TTR("Unable to load addon script from path: '%s'. This might be due to a code error in that script.\nDisabling the addon at '%s' to prevent further errors."), script_path, p_addon));
  2854. _remove_plugin_from_enabled(p_addon);
  2855. return;
  2856. }
  2857. // Plugin init scripts must inherit from EditorPlugin and be tools.
  2858. if (String(script->get_instance_base_type()) != "EditorPlugin") {
  2859. show_warning(vformat(TTR("Unable to load addon script from path: '%s' Base type is not EditorPlugin."), script_path));
  2860. return;
  2861. }
  2862. if (!script->is_tool()) {
  2863. show_warning(vformat(TTR("Unable to load addon script from path: '%s' Script is not in tool mode."), script_path));
  2864. return;
  2865. }
  2866. }
  2867. EditorPlugin *ep = memnew(EditorPlugin);
  2868. ep->set_script(script.get_ref_ptr());
  2869. plugin_addons[p_addon] = ep;
  2870. add_editor_plugin(ep, p_config_changed);
  2871. _update_addon_config();
  2872. }
  2873. bool EditorNode::is_addon_plugin_enabled(const String &p_addon) const {
  2874. if (p_addon.begins_with("res://")) {
  2875. return plugin_addons.has(p_addon);
  2876. }
  2877. return plugin_addons.has(_to_absolute_plugin_path(p_addon));
  2878. }
  2879. void EditorNode::_remove_edited_scene(bool p_change_tab) {
  2880. int new_index = editor_data.get_edited_scene();
  2881. int old_index = new_index;
  2882. if (new_index > 0) {
  2883. new_index = new_index - 1;
  2884. } else if (editor_data.get_edited_scene_count() > 1) {
  2885. new_index = 1;
  2886. } else {
  2887. editor_data.add_edited_scene(-1);
  2888. new_index = 1;
  2889. }
  2890. if (p_change_tab) {
  2891. _scene_tab_changed(new_index);
  2892. }
  2893. editor_data.remove_scene(old_index);
  2894. editor_data.get_undo_redo().clear_history(false);
  2895. _update_title();
  2896. _update_scene_tabs();
  2897. }
  2898. void EditorNode::_remove_scene(int index, bool p_change_tab) {
  2899. // Clear icon cache in case some scripts are no longer needed.
  2900. script_icon_cache.clear();
  2901. if (editor_data.get_edited_scene() == index) {
  2902. //Scene to remove is current scene
  2903. _remove_edited_scene(p_change_tab);
  2904. } else {
  2905. //Scene to remove is not active scene
  2906. editor_data.remove_scene(index);
  2907. }
  2908. }
  2909. void EditorNode::set_edited_scene(Node *p_scene) {
  2910. if (get_editor_data().get_edited_scene_root()) {
  2911. if (get_editor_data().get_edited_scene_root()->get_parent() == scene_root) {
  2912. scene_root->remove_child(get_editor_data().get_edited_scene_root());
  2913. }
  2914. }
  2915. get_editor_data().set_edited_scene_root(p_scene);
  2916. if (Object::cast_to<Popup>(p_scene)) {
  2917. Object::cast_to<Popup>(p_scene)->show(); //show popups
  2918. }
  2919. scene_tree_dock->set_edited_scene(p_scene);
  2920. if (get_tree()) {
  2921. get_tree()->set_edited_scene_root(p_scene);
  2922. }
  2923. if (p_scene) {
  2924. if (p_scene->get_parent() != scene_root) {
  2925. scene_root->add_child(p_scene);
  2926. }
  2927. }
  2928. }
  2929. int EditorNode::_get_current_main_editor() {
  2930. for (int i = 0; i < editor_table.size(); i++) {
  2931. if (editor_table[i] == editor_plugin_screen) {
  2932. return i;
  2933. }
  2934. }
  2935. return 0;
  2936. }
  2937. Dictionary EditorNode::_get_main_scene_state() {
  2938. Dictionary state;
  2939. state["main_tab"] = _get_current_main_editor();
  2940. state["scene_tree_offset"] = scene_tree_dock->get_tree_editor()->get_scene_tree()->get_vscroll_bar()->get_value();
  2941. state["property_edit_offset"] = get_inspector()->get_scroll_offset();
  2942. state["saved_version"] = saved_version;
  2943. state["node_filter"] = scene_tree_dock->get_filter();
  2944. return state;
  2945. }
  2946. void EditorNode::_set_main_scene_state(Dictionary p_state, Node *p_for_scene) {
  2947. if (get_edited_scene() != p_for_scene && p_for_scene != nullptr) {
  2948. return; //not for this scene
  2949. }
  2950. changing_scene = false;
  2951. int current = -1;
  2952. for (int i = 0; i < editor_table.size(); i++) {
  2953. if (editor_plugin_screen == editor_table[i]) {
  2954. current = i;
  2955. break;
  2956. }
  2957. }
  2958. if (p_state.has("editor_index")) {
  2959. int index = p_state["editor_index"];
  2960. if (current < 2) { //if currently in spatial/2d, only switch to spatial/2d. if currently in script, stay there
  2961. if (index < 2 || !get_edited_scene()) {
  2962. _editor_select(index);
  2963. }
  2964. }
  2965. }
  2966. if (get_edited_scene()) {
  2967. if (current < 2) {
  2968. //use heuristic instead
  2969. int n2d = 0, n3d = 0;
  2970. _find_node_types(get_edited_scene(), n2d, n3d);
  2971. if (n2d > n3d) {
  2972. _editor_select(EDITOR_2D);
  2973. } else if (n3d > n2d) {
  2974. _editor_select(EDITOR_3D);
  2975. }
  2976. }
  2977. }
  2978. if (p_state.has("scene_tree_offset")) {
  2979. scene_tree_dock->get_tree_editor()->get_scene_tree()->get_vscroll_bar()->set_value(p_state["scene_tree_offset"]);
  2980. }
  2981. if (p_state.has("property_edit_offset")) {
  2982. get_inspector()->set_scroll_offset(p_state["property_edit_offset"]);
  2983. }
  2984. if (p_state.has("node_filter")) {
  2985. scene_tree_dock->set_filter(p_state["node_filter"]);
  2986. }
  2987. //this should only happen at the very end
  2988. ScriptEditor::get_singleton()->get_debugger()->update_live_edit_root();
  2989. ScriptEditor::get_singleton()->set_scene_root_script(editor_data.get_scene_root_script(editor_data.get_edited_scene()));
  2990. editor_data.notify_edited_scene_changed();
  2991. }
  2992. void EditorNode::set_current_version(uint64_t p_version) {
  2993. saved_version = p_version;
  2994. editor_data.set_edited_scene_version(p_version);
  2995. }
  2996. bool EditorNode::is_changing_scene() const {
  2997. return changing_scene;
  2998. }
  2999. void EditorNode::_clear_undo_history() {
  3000. get_undo_redo()->clear_history(false);
  3001. }
  3002. void EditorNode::set_current_scene(int p_idx) {
  3003. //Save the folding in case the scene gets reloaded.
  3004. if (editor_data.get_scene_path(p_idx) != "" && editor_data.get_edited_scene_root(p_idx)) {
  3005. editor_folding.save_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
  3006. }
  3007. if (editor_data.check_and_update_scene(p_idx)) {
  3008. if (editor_data.get_scene_path(p_idx) != "") {
  3009. editor_folding.load_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
  3010. }
  3011. call_deferred("_clear_undo_history");
  3012. }
  3013. changing_scene = true;
  3014. editor_data.save_edited_scene_state(editor_selection, &editor_history, _get_main_scene_state());
  3015. if (get_editor_data().get_edited_scene_root()) {
  3016. if (get_editor_data().get_edited_scene_root()->get_parent() == scene_root) {
  3017. scene_root->remove_child(get_editor_data().get_edited_scene_root());
  3018. }
  3019. }
  3020. editor_selection->clear();
  3021. editor_data.set_edited_scene(p_idx);
  3022. Node *new_scene = editor_data.get_edited_scene_root();
  3023. if (Object::cast_to<Popup>(new_scene)) {
  3024. Object::cast_to<Popup>(new_scene)->show(); //show popups
  3025. }
  3026. scene_tree_dock->set_edited_scene(new_scene);
  3027. if (get_tree()) {
  3028. get_tree()->set_edited_scene_root(new_scene);
  3029. }
  3030. if (new_scene) {
  3031. if (new_scene->get_parent() != scene_root) {
  3032. scene_root->add_child(new_scene);
  3033. }
  3034. }
  3035. Dictionary state = editor_data.restore_edited_scene_state(editor_selection, &editor_history);
  3036. _edit_current(true);
  3037. _update_title();
  3038. call_deferred("_set_main_scene_state", state, get_edited_scene()); //do after everything else is done setting up
  3039. }
  3040. bool EditorNode::is_scene_open(const String &p_path) {
  3041. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  3042. if (editor_data.get_scene_path(i) == p_path) {
  3043. return true;
  3044. }
  3045. }
  3046. return false;
  3047. }
  3048. void EditorNode::fix_dependencies(const String &p_for_file) {
  3049. dependency_fixer->edit(p_for_file);
  3050. }
  3051. int EditorNode::new_scene() {
  3052. int idx = editor_data.add_edited_scene(-1);
  3053. _scene_tab_changed(idx);
  3054. editor_data.clear_editor_states();
  3055. _update_scene_tabs();
  3056. return idx;
  3057. }
  3058. Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, bool p_set_inherited, bool p_clear_errors, bool p_force_open_imported, bool p_silent_change_tab) {
  3059. if (!is_inside_tree()) {
  3060. defer_load_scene = p_scene;
  3061. return OK;
  3062. }
  3063. if (!p_set_inherited) {
  3064. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  3065. if (editor_data.get_scene_path(i) == p_scene) {
  3066. _scene_tab_changed(i);
  3067. return OK;
  3068. }
  3069. }
  3070. if (!p_force_open_imported && FileAccess::exists(p_scene + ".import")) {
  3071. open_imported->set_text(vformat(TTR("Scene '%s' was automatically imported, so it can't be modified.\nTo make changes to it, a new inherited scene can be created."), p_scene.get_file()));
  3072. open_imported->popup_centered_minsize();
  3073. new_inherited_button->grab_focus();
  3074. open_import_request = p_scene;
  3075. return OK;
  3076. }
  3077. }
  3078. if (p_clear_errors) {
  3079. load_errors->clear();
  3080. }
  3081. String lpath = ProjectSettings::get_singleton()->localize_path(p_scene);
  3082. if (!lpath.begins_with("res://")) {
  3083. show_accept(TTR("Error loading scene, it must be inside the project path. Use 'Import' to open the scene, then save it inside the project path."), TTR("OK"));
  3084. opening_prev = false;
  3085. return ERR_FILE_NOT_FOUND;
  3086. }
  3087. int prev = editor_data.get_edited_scene();
  3088. int idx = editor_data.add_edited_scene(-1);
  3089. if (!editor_data.get_edited_scene_root() && editor_data.get_edited_scene_count() == 2) {
  3090. _remove_edited_scene();
  3091. } else if (!p_silent_change_tab) {
  3092. _scene_tab_changed(idx);
  3093. } else {
  3094. set_current_scene(idx);
  3095. }
  3096. dependency_errors.clear();
  3097. Error err;
  3098. Ref<PackedScene> sdata = ResourceLoader::load(lpath, "", true, &err);
  3099. if (!sdata.is_valid()) {
  3100. _dialog_display_load_error(lpath, err);
  3101. opening_prev = false;
  3102. if (prev != -1) {
  3103. set_current_scene(prev);
  3104. editor_data.remove_scene(idx);
  3105. }
  3106. return ERR_FILE_NOT_FOUND;
  3107. }
  3108. if (!p_ignore_broken_deps && dependency_errors.has(lpath)) {
  3109. current_option = -1;
  3110. Vector<String> errors;
  3111. for (Set<String>::Element *E = dependency_errors[lpath].front(); E; E = E->next()) {
  3112. errors.push_back(E->get());
  3113. }
  3114. dependency_error->show(DependencyErrorDialog::MODE_SCENE, lpath, errors);
  3115. opening_prev = false;
  3116. if (prev != -1) {
  3117. set_current_scene(prev);
  3118. editor_data.remove_scene(idx);
  3119. }
  3120. return ERR_FILE_MISSING_DEPENDENCIES;
  3121. }
  3122. dependency_errors.erase(lpath); //at least not self path
  3123. for (Map<String, Set<String>>::Element *E = dependency_errors.front(); E; E = E->next()) {
  3124. String txt = vformat(TTR("Scene '%s' has broken dependencies:"), E->key()) + "\n";
  3125. for (Set<String>::Element *F = E->get().front(); F; F = F->next()) {
  3126. txt += "\t" + F->get() + "\n";
  3127. }
  3128. add_io_error(txt);
  3129. }
  3130. if (ResourceCache::has(lpath)) {
  3131. //used from somewhere else? no problem! update state and replace sdata
  3132. Ref<PackedScene> ps = Ref<PackedScene>(Object::cast_to<PackedScene>(ResourceCache::get(lpath)));
  3133. if (ps.is_valid()) {
  3134. ps->replace_state(sdata->get_state());
  3135. ps->set_last_modified_time(sdata->get_last_modified_time());
  3136. sdata = ps;
  3137. }
  3138. } else {
  3139. sdata->set_path(lpath, true); //take over path
  3140. }
  3141. Node *new_scene = sdata->instance(p_set_inherited ? PackedScene::GEN_EDIT_STATE_MAIN_INHERITED : PackedScene::GEN_EDIT_STATE_MAIN);
  3142. if (!new_scene) {
  3143. sdata.unref();
  3144. _dialog_display_load_error(lpath, ERR_FILE_CORRUPT);
  3145. opening_prev = false;
  3146. if (prev != -1) {
  3147. set_current_scene(prev);
  3148. editor_data.remove_scene(idx);
  3149. }
  3150. return ERR_FILE_CORRUPT;
  3151. }
  3152. if (p_set_inherited) {
  3153. Ref<SceneState> state = sdata->get_state();
  3154. state->set_path(lpath);
  3155. new_scene->set_scene_inherited_state(state);
  3156. new_scene->set_filename(String());
  3157. }
  3158. new_scene->set_scene_instance_state(Ref<SceneState>());
  3159. set_edited_scene(new_scene);
  3160. _get_scene_metadata(p_scene);
  3161. saved_version = editor_data.get_undo_redo().get_version();
  3162. _update_title();
  3163. _update_scene_tabs();
  3164. _add_to_recent_scenes(lpath);
  3165. if (editor_folding.has_folding_data(lpath)) {
  3166. editor_folding.load_scene_folding(new_scene, lpath);
  3167. } else if (EDITOR_GET("interface/inspector/auto_unfold_foreign_scenes")) {
  3168. editor_folding.unfold_scene(new_scene);
  3169. editor_folding.save_scene_folding(new_scene, lpath);
  3170. }
  3171. prev_scene->set_disabled(previous_scenes.size() == 0);
  3172. opening_prev = false;
  3173. scene_tree_dock->set_selected(new_scene);
  3174. ScriptEditor::get_singleton()->get_debugger()->update_live_edit_root();
  3175. push_item(new_scene);
  3176. if (!restoring_scenes) {
  3177. save_layout();
  3178. }
  3179. return OK;
  3180. }
  3181. void EditorNode::open_request(const String &p_path) {
  3182. if (!opening_prev) {
  3183. List<String>::Element *prev_scene = previous_scenes.find(p_path);
  3184. if (prev_scene != nullptr) {
  3185. prev_scene->erase();
  3186. }
  3187. }
  3188. load_scene(p_path); // as it will be opened in separate tab
  3189. }
  3190. void EditorNode::request_instance_scene(const String &p_path) {
  3191. scene_tree_dock->instance(p_path);
  3192. }
  3193. void EditorNode::request_instance_scenes(const Vector<String> &p_files) {
  3194. scene_tree_dock->instance_scenes(p_files);
  3195. }
  3196. ImportDock *EditorNode::get_import_dock() {
  3197. return import_dock;
  3198. }
  3199. FileSystemDock *EditorNode::get_filesystem_dock() {
  3200. return filesystem_dock;
  3201. }
  3202. SceneTreeDock *EditorNode::get_scene_tree_dock() {
  3203. return scene_tree_dock;
  3204. }
  3205. InspectorDock *EditorNode::get_inspector_dock() {
  3206. return inspector_dock;
  3207. }
  3208. void EditorNode::_inherit_request(String p_file) {
  3209. current_option = FILE_NEW_INHERITED_SCENE;
  3210. _dialog_action(p_file);
  3211. }
  3212. void EditorNode::_instance_request(const Vector<String> &p_files) {
  3213. request_instance_scenes(p_files);
  3214. }
  3215. void EditorNode::_close_messages() {
  3216. old_split_ofs = center_split->get_split_offset();
  3217. center_split->set_split_offset(0);
  3218. }
  3219. void EditorNode::_show_messages() {
  3220. center_split->set_split_offset(old_split_ofs);
  3221. }
  3222. void EditorNode::_add_to_recent_scenes(const String &p_scene) {
  3223. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
  3224. if (rc.find(p_scene) != -1) {
  3225. rc.erase(p_scene);
  3226. }
  3227. rc.push_front(p_scene);
  3228. if (rc.size() > 10) {
  3229. rc.resize(10);
  3230. }
  3231. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", rc);
  3232. _update_recent_scenes();
  3233. }
  3234. void EditorNode::_open_recent_scene(int p_idx) {
  3235. if (p_idx == recent_scenes->get_item_count() - 1) {
  3236. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", Array());
  3237. call_deferred("_update_recent_scenes");
  3238. } else {
  3239. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
  3240. ERR_FAIL_INDEX(p_idx, rc.size());
  3241. if (load_scene(rc[p_idx]) != OK) {
  3242. rc.remove(p_idx);
  3243. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", rc);
  3244. _update_recent_scenes();
  3245. }
  3246. }
  3247. }
  3248. void EditorNode::_update_recent_scenes() {
  3249. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
  3250. recent_scenes->clear();
  3251. String path;
  3252. for (int i = 0; i < rc.size(); i++) {
  3253. path = rc[i];
  3254. recent_scenes->add_item(path.replace("res://", ""), i);
  3255. }
  3256. recent_scenes->add_separator();
  3257. recent_scenes->add_shortcut(ED_SHORTCUT("editor/clear_recent", TTR("Clear Recent Scenes")));
  3258. recent_scenes->set_as_minsize();
  3259. }
  3260. void EditorNode::_quick_opened() {
  3261. Vector<String> files = quick_open->get_selected_files();
  3262. bool open_scene_dialog = quick_open->get_base_type() == "PackedScene";
  3263. for (int i = 0; i < files.size(); i++) {
  3264. String res_path = files[i];
  3265. List<String> scene_extensions;
  3266. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  3267. if (open_scene_dialog || scene_extensions.find(files[i].get_extension().to_lower())) {
  3268. open_request(res_path);
  3269. } else {
  3270. load_resource(res_path);
  3271. }
  3272. }
  3273. }
  3274. void EditorNode::_quick_run() {
  3275. _run(false, quick_run->get_selected());
  3276. }
  3277. void EditorNode::notify_child_process_exited() {
  3278. _menu_option_confirm(RUN_STOP, false);
  3279. stop_button->set_pressed(false);
  3280. editor_run.stop();
  3281. }
  3282. void EditorNode::add_io_error(const String &p_error) {
  3283. _load_error_notify(singleton, p_error);
  3284. }
  3285. void EditorNode::_load_error_notify(void *p_ud, const String &p_text) {
  3286. EditorNode *en = (EditorNode *)p_ud;
  3287. en->load_errors->add_image(en->gui_base->get_icon("Error", "EditorIcons"));
  3288. en->load_errors->add_text(p_text + "\n");
  3289. en->load_error_dialog->popup_centered_ratio(0.5);
  3290. }
  3291. bool EditorNode::_find_scene_in_use(Node *p_node, const String &p_path) const {
  3292. if (p_node->get_filename() == p_path) {
  3293. return true;
  3294. }
  3295. for (int i = 0; i < p_node->get_child_count(); i++) {
  3296. if (_find_scene_in_use(p_node->get_child(i), p_path)) {
  3297. return true;
  3298. }
  3299. }
  3300. return false;
  3301. }
  3302. bool EditorNode::is_scene_in_use(const String &p_path) {
  3303. Node *es = get_edited_scene();
  3304. if (es) {
  3305. return _find_scene_in_use(es, p_path);
  3306. }
  3307. return false;
  3308. }
  3309. void EditorNode::register_editor_types() {
  3310. OS::get_singleton()->benchmark_begin_measure("register_editor_types");
  3311. ResourceLoader::set_timestamp_on_load(true);
  3312. ResourceSaver::set_timestamp_on_save(true);
  3313. ClassDB::register_class<EditorPlugin>();
  3314. ClassDB::register_class<EditorImportPlugin>();
  3315. ClassDB::register_class<EditorScript>();
  3316. ClassDB::register_class<EditorSelection>();
  3317. ClassDB::register_class<EditorFileDialog>();
  3318. ClassDB::register_virtual_class<EditorSettings>();
  3319. ClassDB::register_class<EditorSpatialGizmo>();
  3320. ClassDB::register_class<EditorSpatialGizmoPlugin>();
  3321. ClassDB::register_virtual_class<EditorResourcePreview>();
  3322. ClassDB::register_class<EditorResourcePreviewGenerator>();
  3323. ClassDB::register_virtual_class<EditorFileSystem>();
  3324. ClassDB::register_class<EditorFileSystemDirectory>();
  3325. ClassDB::register_class<EditorVCSInterface>();
  3326. ClassDB::register_virtual_class<ScriptEditor>();
  3327. ClassDB::register_virtual_class<EditorInterface>();
  3328. ClassDB::register_class<EditorExportPlugin>();
  3329. ClassDB::register_class<EditorResourceConversionPlugin>();
  3330. ClassDB::register_class<EditorSceneImporter>();
  3331. ClassDB::register_class<EditorInspector>();
  3332. ClassDB::register_class<EditorInspectorPlugin>();
  3333. ClassDB::register_class<EditorProperty>();
  3334. ClassDB::register_class<AnimationTrackEditPlugin>();
  3335. ClassDB::register_class<ScriptCreateDialog>();
  3336. ClassDB::register_class<EditorFeatureProfile>();
  3337. ClassDB::register_class<EditorSpinSlider>();
  3338. ClassDB::register_class<EditorResourcePicker>();
  3339. ClassDB::register_class<EditorScriptPicker>();
  3340. ClassDB::register_virtual_class<FileSystemDock>();
  3341. // FIXME: Is this stuff obsolete, or should it be ported to new APIs?
  3342. ClassDB::register_class<EditorScenePostImport>();
  3343. //ClassDB::register_type<EditorImportExport>();
  3344. OS::get_singleton()->benchmark_end_measure("register_editor_types");
  3345. }
  3346. void EditorNode::unregister_editor_types() {
  3347. OS::get_singleton()->benchmark_begin_measure("unregister_editor_types");
  3348. _init_callbacks.clear();
  3349. EditorResourcePicker::clear_caches();
  3350. OS::get_singleton()->benchmark_end_measure("unregister_editor_types");
  3351. }
  3352. void EditorNode::stop_child_process() {
  3353. _menu_option_confirm(RUN_STOP, false);
  3354. }
  3355. Ref<Script> EditorNode::get_object_custom_type_base(const Object *p_object) const {
  3356. ERR_FAIL_COND_V(!p_object, nullptr);
  3357. Ref<Script> script = p_object->get_script();
  3358. if (script.is_valid()) {
  3359. // Uncommenting would break things! Consider adding a parameter if you need it.
  3360. // StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
  3361. // if (name != StringName())
  3362. // return name;
  3363. // should probably be deprecated in 4.x
  3364. StringName base = script->get_instance_base_type();
  3365. if (base != StringName() && EditorNode::get_editor_data().get_custom_types().has(base)) {
  3366. const Vector<EditorData::CustomType> &types = EditorNode::get_editor_data().get_custom_types()[base];
  3367. Ref<Script> base_script = script;
  3368. while (base_script.is_valid()) {
  3369. for (int i = 0; i < types.size(); ++i) {
  3370. if (types[i].script == base_script) {
  3371. return types[i].script;
  3372. }
  3373. }
  3374. base_script = base_script->get_base_script();
  3375. }
  3376. }
  3377. }
  3378. return nullptr;
  3379. }
  3380. StringName EditorNode::get_object_custom_type_name(const Object *p_object) const {
  3381. ERR_FAIL_COND_V(!p_object, StringName());
  3382. Ref<Script> script = p_object->get_script();
  3383. if (script.is_null() && p_object->is_class("Script")) {
  3384. script = p_object;
  3385. }
  3386. if (script.is_valid()) {
  3387. Ref<Script> base_script = script;
  3388. while (base_script.is_valid()) {
  3389. StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
  3390. if (name != StringName()) {
  3391. return name;
  3392. }
  3393. // should probably be deprecated in 4.x
  3394. StringName base = base_script->get_instance_base_type();
  3395. if (base != StringName() && EditorNode::get_editor_data().get_custom_types().has(base)) {
  3396. const Vector<EditorData::CustomType> &types = EditorNode::get_editor_data().get_custom_types()[base];
  3397. for (int i = 0; i < types.size(); ++i) {
  3398. if (types[i].script == base_script) {
  3399. return types[i].name;
  3400. }
  3401. }
  3402. }
  3403. base_script = base_script->get_base_script();
  3404. }
  3405. }
  3406. return StringName();
  3407. }
  3408. Ref<ImageTexture> EditorNode::_load_custom_class_icon(const String &p_path) const {
  3409. if (p_path.length()) {
  3410. Ref<Image> img = memnew(Image);
  3411. Error err = ImageLoader::load_image(p_path, img);
  3412. if (err == OK) {
  3413. Ref<ImageTexture> icon = memnew(ImageTexture);
  3414. img->resize(16 * EDSCALE, 16 * EDSCALE, Image::INTERPOLATE_LANCZOS);
  3415. icon->create_from_image(img);
  3416. return icon;
  3417. }
  3418. }
  3419. return nullptr;
  3420. }
  3421. String EditorNode::_to_absolute_plugin_path(const String &p_plugin_name) {
  3422. return "res://addons/" + p_plugin_name + "/plugin.cfg";
  3423. }
  3424. void EditorNode::_pick_main_scene_custom_action(const String &p_custom_action_name) {
  3425. if (p_custom_action_name == "select_current") {
  3426. Node *scene = editor_data.get_edited_scene_root();
  3427. if (!scene) {
  3428. show_accept(TTR("There is no defined scene to run."), TTR("OK"));
  3429. return;
  3430. }
  3431. pick_main_scene->hide();
  3432. current_option = SETTINGS_PICK_MAIN_SCENE;
  3433. _dialog_action(scene->get_filename());
  3434. }
  3435. }
  3436. Ref<Texture> EditorNode::get_object_icon(const Object *p_object, const String &p_fallback) {
  3437. ERR_FAIL_COND_V(!p_object || !gui_base, nullptr);
  3438. Ref<Script> script = p_object->get_script();
  3439. if (script.is_null() && p_object->is_class("Script")) {
  3440. script = p_object;
  3441. }
  3442. if (script.is_valid() && !script_icon_cache.has(script)) {
  3443. Ref<Script> base_script = script;
  3444. while (base_script.is_valid()) {
  3445. StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
  3446. String icon_path = EditorNode::get_editor_data().script_class_get_icon_path(name);
  3447. Ref<ImageTexture> icon = _load_custom_class_icon(icon_path);
  3448. if (icon.is_valid()) {
  3449. script_icon_cache[script] = icon;
  3450. return icon;
  3451. }
  3452. // should probably be deprecated in 4.x
  3453. StringName base = base_script->get_instance_base_type();
  3454. if (base != StringName() && EditorNode::get_editor_data().get_custom_types().has(base)) {
  3455. const Vector<EditorData::CustomType> &types = EditorNode::get_editor_data().get_custom_types()[base];
  3456. for (int i = 0; i < types.size(); ++i) {
  3457. if (types[i].script == base_script && types[i].icon.is_valid()) {
  3458. script_icon_cache[script] = types[i].icon;
  3459. return types[i].icon;
  3460. }
  3461. }
  3462. }
  3463. base_script = base_script->get_base_script();
  3464. }
  3465. // If no icon found, cache it as null.
  3466. script_icon_cache[script] = Ref<Texture>();
  3467. } else if (script.is_valid() && script_icon_cache.has(script) && script_icon_cache[script].is_valid()) {
  3468. return script_icon_cache[script];
  3469. }
  3470. // should probably be deprecated in 4.x
  3471. if (p_object->has_meta("_editor_icon")) {
  3472. return p_object->get_meta("_editor_icon");
  3473. }
  3474. if (gui_base->has_icon(p_object->get_class(), "EditorIcons")) {
  3475. return gui_base->get_icon(p_object->get_class(), "EditorIcons");
  3476. }
  3477. if (p_fallback.length()) {
  3478. return gui_base->get_icon(p_fallback, "EditorIcons");
  3479. }
  3480. return nullptr;
  3481. }
  3482. Ref<Texture> EditorNode::get_class_icon(const String &p_class, const String &p_fallback) const {
  3483. ERR_FAIL_COND_V_MSG(p_class.empty(), nullptr, "Class name cannot be empty.");
  3484. if (ScriptServer::is_global_class(p_class)) {
  3485. String class_name = p_class;
  3486. Ref<Script> script = EditorNode::get_editor_data().script_class_load_script(class_name);
  3487. while (true) {
  3488. String icon_path = EditorNode::get_editor_data().script_class_get_icon_path(class_name);
  3489. Ref<Texture> icon = _load_custom_class_icon(icon_path);
  3490. if (icon.is_valid()) {
  3491. return icon; // Current global class has icon.
  3492. }
  3493. // Find next global class along the inheritance chain.
  3494. do {
  3495. Ref<Script> base_script = script->get_base_script();
  3496. if (base_script.is_null()) {
  3497. // We've reached a native class, use its icon.
  3498. String base_type;
  3499. script->get_language()->get_global_class_name(script->get_path(), &base_type);
  3500. if (gui_base->has_icon(base_type, "EditorIcons")) {
  3501. return gui_base->get_icon(base_type, "EditorIcons");
  3502. }
  3503. return gui_base->get_icon(p_fallback, "EditorIcons");
  3504. }
  3505. script = base_script;
  3506. class_name = EditorNode::get_editor_data().script_class_get_name(script->get_path());
  3507. } while (class_name.empty());
  3508. }
  3509. }
  3510. const Map<String, Vector<EditorData::CustomType>> &p_map = EditorNode::get_editor_data().get_custom_types();
  3511. for (const Map<String, Vector<EditorData::CustomType>>::Element *E = p_map.front(); E; E = E->next()) {
  3512. const Vector<EditorData::CustomType> &ct = E->value();
  3513. for (int i = 0; i < ct.size(); ++i) {
  3514. if (ct[i].name == p_class) {
  3515. if (ct[i].icon.is_valid()) {
  3516. return ct[i].icon;
  3517. }
  3518. }
  3519. }
  3520. }
  3521. if (gui_base->has_icon(p_class, "EditorIcons")) {
  3522. return gui_base->get_icon(p_class, "EditorIcons");
  3523. }
  3524. if (p_fallback.length() && gui_base->has_icon(p_fallback, "EditorIcons")) {
  3525. return gui_base->get_icon(p_fallback, "EditorIcons");
  3526. }
  3527. return nullptr;
  3528. }
  3529. void EditorNode::progress_add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  3530. if (singleton->cmdline_export_mode) {
  3531. print_line(p_task + ": begin: " + p_label + " steps: " + itos(p_steps));
  3532. } else {
  3533. singleton->progress_dialog->add_task(p_task, p_label, p_steps, p_can_cancel);
  3534. }
  3535. }
  3536. bool EditorNode::progress_task_step(const String &p_task, const String &p_state, int p_step, bool p_force_refresh) {
  3537. if (singleton->cmdline_export_mode) {
  3538. print_line("\t" + p_task + ": step " + itos(p_step) + ": " + p_state);
  3539. return false;
  3540. } else {
  3541. return singleton->progress_dialog->task_step(p_task, p_state, p_step, p_force_refresh);
  3542. }
  3543. }
  3544. void EditorNode::progress_end_task(const String &p_task) {
  3545. if (singleton->cmdline_export_mode) {
  3546. print_line(p_task + ": end");
  3547. } else {
  3548. singleton->progress_dialog->end_task(p_task);
  3549. }
  3550. }
  3551. void EditorNode::progress_add_task_bg(const String &p_task, const String &p_label, int p_steps) {
  3552. singleton->progress_hb->add_task(p_task, p_label, p_steps);
  3553. }
  3554. void EditorNode::progress_task_step_bg(const String &p_task, int p_step) {
  3555. singleton->progress_hb->task_step(p_task, p_step);
  3556. }
  3557. void EditorNode::progress_end_task_bg(const String &p_task) {
  3558. singleton->progress_hb->end_task(p_task);
  3559. }
  3560. Ref<Texture> EditorNode::_file_dialog_get_icon(const String &p_path) {
  3561. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem_path(p_path.get_base_dir());
  3562. if (efsd) {
  3563. String file = p_path.get_file();
  3564. for (int i = 0; i < efsd->get_file_count(); i++) {
  3565. if (efsd->get_file(i) == file) {
  3566. String type = efsd->get_file_type(i);
  3567. if (singleton->icon_type_cache.has(type)) {
  3568. return singleton->icon_type_cache[type];
  3569. } else {
  3570. return singleton->icon_type_cache["Object"];
  3571. }
  3572. }
  3573. }
  3574. }
  3575. return singleton->icon_type_cache["Object"];
  3576. }
  3577. void EditorNode::_build_icon_type_cache() {
  3578. List<StringName> tl;
  3579. StringName ei = "EditorIcons";
  3580. theme_base->get_theme()->get_icon_list(ei, &tl);
  3581. for (List<StringName>::Element *E = tl.front(); E; E = E->next()) {
  3582. if (!ClassDB::class_exists(E->get())) {
  3583. continue;
  3584. }
  3585. icon_type_cache[E->get()] = theme_base->get_theme()->get_icon(E->get(), ei);
  3586. }
  3587. }
  3588. void EditorNode::_file_dialog_register(FileDialog *p_dialog) {
  3589. singleton->file_dialogs.insert(p_dialog);
  3590. }
  3591. void EditorNode::_file_dialog_unregister(FileDialog *p_dialog) {
  3592. singleton->file_dialogs.erase(p_dialog);
  3593. }
  3594. void EditorNode::_editor_file_dialog_register(EditorFileDialog *p_dialog) {
  3595. singleton->editor_file_dialogs.insert(p_dialog);
  3596. }
  3597. void EditorNode::_editor_file_dialog_unregister(EditorFileDialog *p_dialog) {
  3598. singleton->editor_file_dialogs.erase(p_dialog);
  3599. }
  3600. Vector<EditorNodeInitCallback> EditorNode::_init_callbacks;
  3601. Error EditorNode::export_preset(const String &p_preset, const String &p_path, bool p_debug, bool p_pack_only) {
  3602. export_defer.preset = p_preset;
  3603. export_defer.path = p_path;
  3604. export_defer.debug = p_debug;
  3605. export_defer.pack_only = p_pack_only;
  3606. cmdline_export_mode = true;
  3607. return OK;
  3608. }
  3609. void EditorNode::show_accept(const String &p_text, const String &p_title) {
  3610. current_option = -1;
  3611. accept->get_ok()->set_text(p_title);
  3612. accept->set_text(p_text);
  3613. accept->popup_centered_minsize();
  3614. }
  3615. void EditorNode::show_save_accept(const String &p_text, const String &p_title) {
  3616. current_option = -1;
  3617. save_accept->get_ok()->set_text(p_title);
  3618. save_accept->set_text(p_text);
  3619. save_accept->popup_centered();
  3620. }
  3621. void EditorNode::show_warning(const String &p_text, const String &p_title) {
  3622. if (warning->is_inside_tree()) {
  3623. warning->set_text(p_text);
  3624. warning->set_title(p_title);
  3625. warning->popup_centered_minsize();
  3626. } else {
  3627. WARN_PRINT(p_title + " " + p_text);
  3628. }
  3629. }
  3630. void EditorNode::_copy_warning(const String &p_str) {
  3631. OS::get_singleton()->set_clipboard(warning->get_text());
  3632. }
  3633. void EditorNode::_dock_select_input(const Ref<InputEvent> &p_input) {
  3634. Ref<InputEventMouse> me = p_input;
  3635. if (me.is_valid()) {
  3636. Vector2 point = me->get_position();
  3637. int nrect = -1;
  3638. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3639. if (dock_select_rect[i].has_point(point)) {
  3640. nrect = i;
  3641. break;
  3642. }
  3643. }
  3644. if (nrect != dock_select_rect_over) {
  3645. dock_select->update();
  3646. dock_select_rect_over = nrect;
  3647. }
  3648. if (nrect == -1) {
  3649. return;
  3650. }
  3651. Ref<InputEventMouseButton> mb = me;
  3652. if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed() && dock_popup_selected != nrect) {
  3653. Control *dock = dock_slot[dock_popup_selected]->get_current_tab_control();
  3654. if (dock) {
  3655. dock_slot[dock_popup_selected]->remove_child(dock);
  3656. }
  3657. if (dock_slot[dock_popup_selected]->get_tab_count() == 0) {
  3658. dock_slot[dock_popup_selected]->hide();
  3659. } else {
  3660. dock_slot[dock_popup_selected]->set_current_tab(0);
  3661. }
  3662. dock_slot[nrect]->add_child(dock);
  3663. dock_popup_selected = nrect;
  3664. dock_slot[nrect]->set_current_tab(dock_slot[nrect]->get_tab_count() - 1);
  3665. dock_slot[nrect]->show();
  3666. dock_select->update();
  3667. for (int i = 0; i < vsplits.size(); i++) {
  3668. bool in_use = dock_slot[i * 2 + 0]->get_tab_count() || dock_slot[i * 2 + 1]->get_tab_count();
  3669. if (in_use) {
  3670. vsplits[i]->show();
  3671. } else {
  3672. vsplits[i]->hide();
  3673. }
  3674. }
  3675. if (right_l_vsplit->is_visible() || right_r_vsplit->is_visible()) {
  3676. right_hsplit->show();
  3677. } else {
  3678. right_hsplit->hide();
  3679. }
  3680. _edit_current();
  3681. _save_docks();
  3682. }
  3683. }
  3684. }
  3685. void EditorNode::_dock_popup_exit() {
  3686. dock_select_rect_over = -1;
  3687. dock_select->update();
  3688. }
  3689. void EditorNode::_dock_pre_popup(int p_which) {
  3690. dock_popup_selected = p_which;
  3691. }
  3692. void EditorNode::_dock_move_left() {
  3693. if (dock_popup_selected < 0 || dock_popup_selected >= DOCK_SLOT_MAX) {
  3694. return;
  3695. }
  3696. Control *current = dock_slot[dock_popup_selected]->get_tab_control(dock_slot[dock_popup_selected]->get_current_tab());
  3697. Control *prev = dock_slot[dock_popup_selected]->get_tab_control(dock_slot[dock_popup_selected]->get_current_tab() - 1);
  3698. if (!current || !prev) {
  3699. return;
  3700. }
  3701. dock_slot[dock_popup_selected]->move_child(current, prev->get_index());
  3702. dock_slot[dock_popup_selected]->set_current_tab(dock_slot[dock_popup_selected]->get_current_tab() - 1);
  3703. dock_select->update();
  3704. _edit_current();
  3705. _save_docks();
  3706. }
  3707. void EditorNode::_dock_move_right() {
  3708. Control *current = dock_slot[dock_popup_selected]->get_tab_control(dock_slot[dock_popup_selected]->get_current_tab());
  3709. Control *next = dock_slot[dock_popup_selected]->get_tab_control(dock_slot[dock_popup_selected]->get_current_tab() + 1);
  3710. if (!current || !next) {
  3711. return;
  3712. }
  3713. dock_slot[dock_popup_selected]->move_child(next, current->get_index());
  3714. dock_slot[dock_popup_selected]->set_current_tab(dock_slot[dock_popup_selected]->get_current_tab() + 1);
  3715. dock_select->update();
  3716. _edit_current();
  3717. _save_docks();
  3718. }
  3719. void EditorNode::_dock_select_draw() {
  3720. Size2 s = dock_select->get_size();
  3721. s.y /= 2.0;
  3722. s.x /= 6.0;
  3723. Color used = Color(0.6, 0.6, 0.6, 0.8);
  3724. Color used_selected = Color(0.8, 0.8, 0.8, 0.8);
  3725. Color tab_selected = theme_base->get_color("mono_color", "Editor");
  3726. Color unused = used;
  3727. unused.a = 0.4;
  3728. Color unusable = unused;
  3729. unusable.a = 0.1;
  3730. Rect2 unr(s.x * 2, 0, s.x * 2, s.y * 2);
  3731. unr.position += Vector2(2, 5);
  3732. unr.size -= Vector2(4, 7);
  3733. dock_select->draw_rect(unr, unusable);
  3734. dock_tab_move_left->set_disabled(true);
  3735. dock_tab_move_right->set_disabled(true);
  3736. if (dock_popup_selected != -1 && dock_slot[dock_popup_selected]->get_tab_count()) {
  3737. dock_tab_move_left->set_disabled(dock_slot[dock_popup_selected]->get_current_tab() == 0);
  3738. dock_tab_move_right->set_disabled(dock_slot[dock_popup_selected]->get_current_tab() >= dock_slot[dock_popup_selected]->get_tab_count() - 1);
  3739. }
  3740. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3741. Vector2 ofs;
  3742. switch (i) {
  3743. case DOCK_SLOT_LEFT_UL: {
  3744. } break;
  3745. case DOCK_SLOT_LEFT_BL: {
  3746. ofs.y += s.y;
  3747. } break;
  3748. case DOCK_SLOT_LEFT_UR: {
  3749. ofs.x += s.x;
  3750. } break;
  3751. case DOCK_SLOT_LEFT_BR: {
  3752. ofs += s;
  3753. } break;
  3754. case DOCK_SLOT_RIGHT_UL: {
  3755. ofs.x += s.x * 4;
  3756. } break;
  3757. case DOCK_SLOT_RIGHT_BL: {
  3758. ofs.x += s.x * 4;
  3759. ofs.y += s.y;
  3760. } break;
  3761. case DOCK_SLOT_RIGHT_UR: {
  3762. ofs.x += s.x * 4;
  3763. ofs.x += s.x;
  3764. } break;
  3765. case DOCK_SLOT_RIGHT_BR: {
  3766. ofs.x += s.x * 4;
  3767. ofs += s;
  3768. } break;
  3769. }
  3770. Rect2 r(ofs, s);
  3771. dock_select_rect[i] = r;
  3772. r.position += Vector2(2, 5);
  3773. r.size -= Vector2(4, 7);
  3774. if (i == dock_select_rect_over) {
  3775. dock_select->draw_rect(r, used_selected);
  3776. } else if (dock_slot[i]->get_child_count() == 0) {
  3777. dock_select->draw_rect(r, unused);
  3778. } else {
  3779. dock_select->draw_rect(r, used);
  3780. }
  3781. for (int j = 0; j < MIN(3, dock_slot[i]->get_child_count()); j++) {
  3782. int xofs = (r.size.width / 3) * j;
  3783. Color c = used;
  3784. if (i == dock_popup_selected && (dock_slot[i]->get_current_tab() > 3 || dock_slot[i]->get_current_tab() == j)) {
  3785. c = tab_selected;
  3786. }
  3787. dock_select->draw_rect(Rect2(2 + ofs.x + xofs, ofs.y, r.size.width / 3 - 1, 3), c);
  3788. }
  3789. }
  3790. }
  3791. void EditorNode::_save_docks() {
  3792. if (waiting_for_first_scan) {
  3793. return; //scanning, do not touch docks
  3794. }
  3795. Ref<ConfigFile> config;
  3796. config.instance();
  3797. _save_docks_to_config(config, "docks");
  3798. _save_open_scenes_to_config(config, "EditorNode");
  3799. editor_data.get_plugin_window_layout(config);
  3800. config->save(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("editor_layout.cfg"));
  3801. }
  3802. void EditorNode::_save_docks_to_config(Ref<ConfigFile> p_layout, const String &p_section) {
  3803. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3804. String names;
  3805. for (int j = 0; j < dock_slot[i]->get_tab_count(); j++) {
  3806. String name = dock_slot[i]->get_tab_control(j)->get_name();
  3807. if (names != "") {
  3808. names += ",";
  3809. }
  3810. names += name;
  3811. }
  3812. if (names != "") {
  3813. p_layout->set_value(p_section, "dock_" + itos(i + 1), names);
  3814. }
  3815. }
  3816. p_layout->set_value(p_section, "dock_filesystem_split", filesystem_dock->get_split_offset());
  3817. p_layout->set_value(p_section, "dock_filesystem_display_mode", filesystem_dock->get_display_mode());
  3818. p_layout->set_value(p_section, "dock_filesystem_file_list_display_mode", filesystem_dock->get_file_list_display_mode());
  3819. for (int i = 0; i < vsplits.size(); i++) {
  3820. if (vsplits[i]->is_visible_in_tree()) {
  3821. p_layout->set_value(p_section, "dock_split_" + itos(i + 1), vsplits[i]->get_split_offset());
  3822. }
  3823. }
  3824. for (int i = 0; i < hsplits.size(); i++) {
  3825. p_layout->set_value(p_section, "dock_hsplit_" + itos(i + 1), hsplits[i]->get_split_offset());
  3826. }
  3827. }
  3828. void EditorNode::_save_open_scenes_to_config(Ref<ConfigFile> p_layout, const String &p_section) {
  3829. Array scenes;
  3830. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  3831. String path = editor_data.get_scene_path(i);
  3832. if (path == "") {
  3833. continue;
  3834. }
  3835. scenes.push_back(path);
  3836. }
  3837. p_layout->set_value(p_section, "open_scenes", scenes);
  3838. }
  3839. void EditorNode::save_layout() {
  3840. dock_drag_timer->start();
  3841. }
  3842. void EditorNode::_dock_split_dragged(int ofs) {
  3843. dock_drag_timer->start();
  3844. }
  3845. void EditorNode::_load_docks() {
  3846. Ref<ConfigFile> config;
  3847. config.instance();
  3848. Error err = config->load(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("editor_layout.cfg"));
  3849. if (err != OK) {
  3850. //no config
  3851. if (overridden_default_layout >= 0) {
  3852. _layout_menu_option(overridden_default_layout);
  3853. }
  3854. return;
  3855. }
  3856. _load_docks_from_config(config, "docks");
  3857. _load_open_scenes_from_config(config, "EditorNode");
  3858. editor_data.set_plugin_window_layout(config);
  3859. }
  3860. void EditorNode::_update_dock_slots_visibility(bool p_keep_selected_tabs) {
  3861. if (!docks_visible) {
  3862. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3863. dock_slot[i]->hide();
  3864. }
  3865. for (int i = 0; i < vsplits.size(); i++) {
  3866. vsplits[i]->hide();
  3867. }
  3868. right_hsplit->hide();
  3869. } else {
  3870. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3871. int tabs_visible = 0;
  3872. for (int j = 0; j < dock_slot[i]->get_tab_count(); j++) {
  3873. if (!dock_slot[i]->get_tab_hidden(j)) {
  3874. tabs_visible++;
  3875. }
  3876. }
  3877. if (tabs_visible) {
  3878. dock_slot[i]->show();
  3879. } else {
  3880. dock_slot[i]->hide();
  3881. }
  3882. }
  3883. for (int i = 0; i < vsplits.size(); i++) {
  3884. bool in_use = dock_slot[i * 2 + 0]->get_tab_count() || dock_slot[i * 2 + 1]->get_tab_count();
  3885. if (in_use) {
  3886. vsplits[i]->show();
  3887. } else {
  3888. vsplits[i]->hide();
  3889. }
  3890. }
  3891. if (!p_keep_selected_tabs) {
  3892. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3893. if (dock_slot[i]->is_visible() && dock_slot[i]->get_tab_count()) {
  3894. dock_slot[i]->set_current_tab(0);
  3895. }
  3896. }
  3897. }
  3898. if (right_l_vsplit->is_visible() || right_r_vsplit->is_visible()) {
  3899. right_hsplit->show();
  3900. } else {
  3901. right_hsplit->hide();
  3902. }
  3903. }
  3904. }
  3905. void EditorNode::_dock_tab_changed(int p_tab) {
  3906. // update visibility but don't set current tab
  3907. if (!docks_visible) {
  3908. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3909. dock_slot[i]->hide();
  3910. }
  3911. for (int i = 0; i < vsplits.size(); i++) {
  3912. vsplits[i]->hide();
  3913. }
  3914. right_hsplit->hide();
  3915. bottom_panel->hide();
  3916. } else {
  3917. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3918. if (dock_slot[i]->get_tab_count()) {
  3919. dock_slot[i]->show();
  3920. } else {
  3921. dock_slot[i]->hide();
  3922. }
  3923. }
  3924. for (int i = 0; i < vsplits.size(); i++) {
  3925. bool in_use = dock_slot[i * 2 + 0]->get_tab_count() || dock_slot[i * 2 + 1]->get_tab_count();
  3926. if (in_use) {
  3927. vsplits[i]->show();
  3928. } else {
  3929. vsplits[i]->hide();
  3930. }
  3931. }
  3932. bottom_panel->show();
  3933. if (right_l_vsplit->is_visible() || right_r_vsplit->is_visible()) {
  3934. right_hsplit->show();
  3935. } else {
  3936. right_hsplit->hide();
  3937. }
  3938. }
  3939. }
  3940. void EditorNode::_load_docks_from_config(Ref<ConfigFile> p_layout, const String &p_section) {
  3941. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  3942. if (!p_layout->has_section_key(p_section, "dock_" + itos(i + 1))) {
  3943. continue;
  3944. }
  3945. Vector<String> names = String(p_layout->get_value(p_section, "dock_" + itos(i + 1))).split(",");
  3946. for (int j = 0; j < names.size(); j++) {
  3947. String name = names[j];
  3948. //find it, in a horribly inefficient way
  3949. int atidx = -1;
  3950. Control *node = nullptr;
  3951. for (int k = 0; k < DOCK_SLOT_MAX; k++) {
  3952. if (!dock_slot[k]->has_node(name)) {
  3953. continue;
  3954. }
  3955. node = Object::cast_to<Control>(dock_slot[k]->get_node(name));
  3956. if (!node) {
  3957. continue;
  3958. }
  3959. atidx = k;
  3960. break;
  3961. }
  3962. if (atidx == -1) { //well, it's not anywhere
  3963. continue;
  3964. }
  3965. if (atidx == i) {
  3966. node->raise();
  3967. continue;
  3968. }
  3969. dock_slot[atidx]->remove_child(node);
  3970. if (dock_slot[atidx]->get_tab_count() == 0) {
  3971. dock_slot[atidx]->hide();
  3972. }
  3973. dock_slot[i]->add_child(node);
  3974. dock_slot[i]->show();
  3975. }
  3976. }
  3977. if (p_layout->has_section_key(p_section, "dock_filesystem_split")) {
  3978. int fs_split_ofs = p_layout->get_value(p_section, "dock_filesystem_split");
  3979. filesystem_dock->set_split_offset(fs_split_ofs);
  3980. }
  3981. if (p_layout->has_section_key(p_section, "dock_filesystem_display_mode")) {
  3982. FileSystemDock::DisplayMode dock_filesystem_display_mode = FileSystemDock::DisplayMode(int(p_layout->get_value(p_section, "dock_filesystem_display_mode")));
  3983. filesystem_dock->set_display_mode(dock_filesystem_display_mode);
  3984. }
  3985. if (p_layout->has_section_key(p_section, "dock_filesystem_file_list_display_mode")) {
  3986. FileSystemDock::FileListDisplayMode dock_filesystem_file_list_display_mode = FileSystemDock::FileListDisplayMode(int(p_layout->get_value(p_section, "dock_filesystem_file_list_display_mode")));
  3987. filesystem_dock->set_file_list_display_mode(dock_filesystem_file_list_display_mode);
  3988. }
  3989. for (int i = 0; i < vsplits.size(); i++) {
  3990. if (!p_layout->has_section_key(p_section, "dock_split_" + itos(i + 1))) {
  3991. continue;
  3992. }
  3993. int ofs = p_layout->get_value(p_section, "dock_split_" + itos(i + 1));
  3994. vsplits[i]->set_split_offset(ofs);
  3995. }
  3996. for (int i = 0; i < hsplits.size(); i++) {
  3997. if (!p_layout->has_section_key(p_section, "dock_hsplit_" + itos(i + 1))) {
  3998. continue;
  3999. }
  4000. int ofs = p_layout->get_value(p_section, "dock_hsplit_" + itos(i + 1));
  4001. hsplits[i]->set_split_offset(ofs);
  4002. }
  4003. for (int i = 0; i < vsplits.size(); i++) {
  4004. bool in_use = dock_slot[i * 2 + 0]->get_tab_count() || dock_slot[i * 2 + 1]->get_tab_count();
  4005. if (in_use) {
  4006. vsplits[i]->show();
  4007. } else {
  4008. vsplits[i]->hide();
  4009. }
  4010. }
  4011. if (right_l_vsplit->is_visible() || right_r_vsplit->is_visible()) {
  4012. right_hsplit->show();
  4013. } else {
  4014. right_hsplit->hide();
  4015. }
  4016. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  4017. if (dock_slot[i]->is_visible() && dock_slot[i]->get_tab_count()) {
  4018. dock_slot[i]->set_current_tab(0);
  4019. }
  4020. }
  4021. }
  4022. void EditorNode::_load_open_scenes_from_config(Ref<ConfigFile> p_layout, const String &p_section) {
  4023. if (!bool(EDITOR_GET("interface/scene_tabs/restore_scenes_on_load"))) {
  4024. return;
  4025. }
  4026. if (!p_layout->has_section(p_section) || !p_layout->has_section_key(p_section, "open_scenes")) {
  4027. return;
  4028. }
  4029. restoring_scenes = true;
  4030. Array scenes = p_layout->get_value(p_section, "open_scenes");
  4031. for (int i = 0; i < scenes.size(); i++) {
  4032. load_scene(scenes[i]);
  4033. }
  4034. save_layout();
  4035. restoring_scenes = false;
  4036. }
  4037. bool EditorNode::has_scenes_in_session() {
  4038. if (!bool(EDITOR_GET("interface/scene_tabs/restore_scenes_on_load"))) {
  4039. return false;
  4040. }
  4041. Ref<ConfigFile> config;
  4042. config.instance();
  4043. Error err = config->load(EditorSettings::get_singleton()->get_project_settings_dir().plus_file("editor_layout.cfg"));
  4044. if (err != OK) {
  4045. return false;
  4046. }
  4047. if (!config->has_section("EditorNode") || !config->has_section_key("EditorNode", "open_scenes")) {
  4048. return false;
  4049. }
  4050. Array scenes = config->get_value("EditorNode", "open_scenes");
  4051. return !scenes.empty();
  4052. }
  4053. bool EditorNode::ensure_main_scene(bool p_from_native) {
  4054. pick_main_scene->set_meta("from_native", p_from_native); //whether from play button or native run
  4055. String main_scene = GLOBAL_DEF("application/run/main_scene", "");
  4056. if (main_scene == "") {
  4057. current_option = -1;
  4058. pick_main_scene->set_text(TTR("No main scene has ever been defined, select one?\nYou can change it later in \"Project Settings\" under the 'application' category."));
  4059. pick_main_scene->popup_centered_minsize();
  4060. if (editor_data.get_edited_scene_root()) {
  4061. select_current_scene_button->set_disabled(false);
  4062. select_current_scene_button->grab_focus();
  4063. } else {
  4064. select_current_scene_button->set_disabled(true);
  4065. }
  4066. return false;
  4067. }
  4068. if (!FileAccess::exists(main_scene)) {
  4069. current_option = -1;
  4070. pick_main_scene->set_text(vformat(TTR("Selected scene '%s' does not exist, select a valid one?\nYou can change it later in \"Project Settings\" under the 'application' category."), main_scene));
  4071. pick_main_scene->popup_centered_minsize();
  4072. return false;
  4073. }
  4074. if (ResourceLoader::get_resource_type(main_scene) != "PackedScene") {
  4075. current_option = -1;
  4076. pick_main_scene->set_text(vformat(TTR("Selected scene '%s' is not a scene file, select a valid one?\nYou can change it later in \"Project Settings\" under the 'application' category."), main_scene));
  4077. pick_main_scene->popup_centered_minsize();
  4078. return false;
  4079. }
  4080. return true;
  4081. }
  4082. void EditorNode::run_play() {
  4083. _menu_option_confirm(RUN_STOP, true);
  4084. _run(false);
  4085. }
  4086. void EditorNode::run_play_current() {
  4087. _save_default_environment();
  4088. _menu_option_confirm(RUN_STOP, true);
  4089. _run(true);
  4090. }
  4091. void EditorNode::run_play_custom(const String &p_custom) {
  4092. _menu_option_confirm(RUN_STOP, true);
  4093. _run(false, p_custom);
  4094. }
  4095. void EditorNode::run_stop() {
  4096. _menu_option_confirm(RUN_STOP, false);
  4097. }
  4098. bool EditorNode::is_run_playing() const {
  4099. EditorRun::Status status = editor_run.get_status();
  4100. return (status == EditorRun::STATUS_PLAY || status == EditorRun::STATUS_PAUSED);
  4101. }
  4102. String EditorNode::get_run_playing_scene() const {
  4103. String run_filename = editor_run.get_running_scene();
  4104. if (run_filename == "" && is_run_playing()) {
  4105. run_filename = GLOBAL_DEF("application/run/main_scene", ""); // Must be the main scene then.
  4106. }
  4107. return run_filename;
  4108. }
  4109. int EditorNode::get_current_tab() {
  4110. return scene_tabs->get_current_tab();
  4111. }
  4112. void EditorNode::set_current_tab(int p_tab) {
  4113. scene_tabs->set_current_tab(p_tab);
  4114. }
  4115. void EditorNode::_update_layouts_menu() {
  4116. editor_layouts->clear();
  4117. overridden_default_layout = -1;
  4118. editor_layouts->set_size(Vector2());
  4119. editor_layouts->add_shortcut(ED_SHORTCUT("layout/save", TTR("Save Layout")), SETTINGS_LAYOUT_SAVE);
  4120. editor_layouts->add_shortcut(ED_SHORTCUT("layout/delete", TTR("Delete Layout")), SETTINGS_LAYOUT_DELETE);
  4121. editor_layouts->add_separator();
  4122. editor_layouts->add_shortcut(ED_SHORTCUT("layout/default", TTR("Default")), SETTINGS_LAYOUT_DEFAULT);
  4123. Ref<ConfigFile> config;
  4124. config.instance();
  4125. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  4126. if (err != OK) {
  4127. return; //no config
  4128. }
  4129. List<String> layouts;
  4130. config.ptr()->get_sections(&layouts);
  4131. for (List<String>::Element *E = layouts.front(); E; E = E->next()) {
  4132. String layout = E->get();
  4133. if (layout == TTR("Default")) {
  4134. editor_layouts->remove_item(editor_layouts->get_item_index(SETTINGS_LAYOUT_DEFAULT));
  4135. overridden_default_layout = editor_layouts->get_item_count();
  4136. }
  4137. editor_layouts->add_item(layout);
  4138. }
  4139. }
  4140. void EditorNode::_layout_menu_option(int p_id) {
  4141. switch (p_id) {
  4142. case SETTINGS_LAYOUT_SAVE: {
  4143. current_option = p_id;
  4144. layout_dialog->set_title(TTR("Save Layout"));
  4145. layout_dialog->get_ok()->set_text(TTR("Save"));
  4146. layout_dialog->popup_centered();
  4147. layout_dialog->set_name_line_enabled(true);
  4148. } break;
  4149. case SETTINGS_LAYOUT_DELETE: {
  4150. current_option = p_id;
  4151. layout_dialog->set_title(TTR("Delete Layout"));
  4152. layout_dialog->get_ok()->set_text(TTR("Delete"));
  4153. layout_dialog->popup_centered();
  4154. layout_dialog->set_name_line_enabled(false);
  4155. } break;
  4156. case SETTINGS_LAYOUT_DEFAULT: {
  4157. _load_docks_from_config(default_layout, "docks");
  4158. _save_docks();
  4159. } break;
  4160. default: {
  4161. Ref<ConfigFile> config;
  4162. config.instance();
  4163. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  4164. if (err != OK) {
  4165. return; //no config
  4166. }
  4167. _load_docks_from_config(config, editor_layouts->get_item_text(p_id));
  4168. _save_docks();
  4169. }
  4170. }
  4171. }
  4172. void EditorNode::_scene_tab_script_edited(int p_tab) {
  4173. Ref<Script> script = editor_data.get_scene_root_script(p_tab);
  4174. if (script.is_valid()) {
  4175. inspector_dock->edit_resource(script);
  4176. }
  4177. }
  4178. void EditorNode::_scene_tab_closed(int p_tab, int option) {
  4179. current_option = option;
  4180. tab_closing = p_tab;
  4181. Node *scene = editor_data.get_edited_scene_root(p_tab);
  4182. if (!scene) {
  4183. _discard_changes();
  4184. return;
  4185. }
  4186. bool unsaved = (p_tab == editor_data.get_edited_scene())
  4187. ? saved_version != editor_data.get_undo_redo().get_version()
  4188. : editor_data.get_scene_version(p_tab) != 0;
  4189. if (unsaved) {
  4190. save_confirmation->get_ok()->set_text(TTR("Save & Close"));
  4191. save_confirmation->set_text(vformat(TTR("Save changes to '%s' before closing?"), scene->get_filename() != "" ? scene->get_filename() : "unsaved scene"));
  4192. save_confirmation->popup_centered_minsize();
  4193. } else {
  4194. _discard_changes();
  4195. }
  4196. save_layout();
  4197. _update_scene_tabs();
  4198. }
  4199. void EditorNode::_scene_tab_hover(int p_tab) {
  4200. if (!bool(EDITOR_GET("interface/scene_tabs/show_thumbnail_on_hover"))) {
  4201. return;
  4202. }
  4203. int current_tab = scene_tabs->get_current_tab();
  4204. if (p_tab == current_tab || p_tab < 0) {
  4205. tab_preview_panel->hide();
  4206. } else {
  4207. String path = editor_data.get_scene_path(p_tab);
  4208. if (path != String()) {
  4209. EditorResourcePreview::get_singleton()->queue_resource_preview(path, this, "_thumbnail_done", p_tab);
  4210. }
  4211. }
  4212. }
  4213. void EditorNode::_scene_tab_exit() {
  4214. tab_preview_panel->hide();
  4215. }
  4216. void EditorNode::_scene_tab_input(const Ref<InputEvent> &p_input) {
  4217. Ref<InputEventMouseButton> mb = p_input;
  4218. if (mb.is_valid()) {
  4219. if (scene_tabs->get_hovered_tab() >= 0) {
  4220. if (mb->get_button_index() == BUTTON_MIDDLE && mb->is_pressed()) {
  4221. _scene_tab_closed(scene_tabs->get_hovered_tab());
  4222. }
  4223. } else {
  4224. if ((mb->get_button_index() == BUTTON_LEFT && mb->is_doubleclick()) || (mb->get_button_index() == BUTTON_MIDDLE && mb->is_pressed())) {
  4225. _menu_option_confirm(FILE_NEW_SCENE, true);
  4226. }
  4227. }
  4228. if (mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
  4229. // context menu
  4230. scene_tabs_context_menu->clear();
  4231. scene_tabs_context_menu->set_size(Size2(1, 1));
  4232. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/new_scene"), FILE_NEW_SCENE);
  4233. if (scene_tabs->get_hovered_tab() >= 0) {
  4234. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene"), FILE_SAVE_SCENE);
  4235. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_scene_as"), FILE_SAVE_AS_SCENE);
  4236. }
  4237. scene_tabs_context_menu->add_shortcut(ED_GET_SHORTCUT("editor/save_all_scenes"), FILE_SAVE_ALL_SCENES);
  4238. if (scene_tabs->get_hovered_tab() >= 0) {
  4239. scene_tabs_context_menu->add_separator();
  4240. scene_tabs_context_menu->add_item(TTR("Show in FileSystem"), FILE_SHOW_IN_FILESYSTEM);
  4241. scene_tabs_context_menu->add_item(TTR("Play This Scene"), RUN_PLAY_SCENE);
  4242. scene_tabs_context_menu->add_separator();
  4243. Ref<ShortCut> close_tab_sc = ED_GET_SHORTCUT("editor/close_scene");
  4244. close_tab_sc->set_name(TTR("Close Tab"));
  4245. scene_tabs_context_menu->add_shortcut(close_tab_sc, FILE_CLOSE);
  4246. Ref<ShortCut> undo_close_tab_sc = ED_GET_SHORTCUT("editor/reopen_closed_scene");
  4247. undo_close_tab_sc->set_name(TTR("Undo Close Tab"));
  4248. scene_tabs_context_menu->add_shortcut(undo_close_tab_sc, FILE_OPEN_PREV);
  4249. if (previous_scenes.empty()) {
  4250. scene_tabs_context_menu->set_item_disabled(scene_tabs_context_menu->get_item_index(FILE_OPEN_PREV), true);
  4251. }
  4252. scene_tabs_context_menu->add_item(TTR("Close Other Tabs"), FILE_CLOSE_OTHERS);
  4253. scene_tabs_context_menu->add_item(TTR("Close Tabs to the Right"), FILE_CLOSE_RIGHT);
  4254. scene_tabs_context_menu->add_item(TTR("Close All Tabs"), FILE_CLOSE_ALL);
  4255. }
  4256. scene_tabs_context_menu->set_position(mb->get_global_position());
  4257. scene_tabs_context_menu->popup();
  4258. }
  4259. if (mb->get_button_index() == BUTTON_WHEEL_UP && mb->is_pressed()) {
  4260. int previous_tab = editor_data.get_edited_scene() - 1;
  4261. previous_tab = previous_tab >= 0 ? previous_tab : editor_data.get_edited_scene_count() - 1;
  4262. _scene_tab_changed(previous_tab);
  4263. }
  4264. if (mb->get_button_index() == BUTTON_WHEEL_DOWN && mb->is_pressed()) {
  4265. int next_tab = editor_data.get_edited_scene() + 1;
  4266. next_tab %= editor_data.get_edited_scene_count();
  4267. _scene_tab_changed(next_tab);
  4268. }
  4269. }
  4270. }
  4271. void EditorNode::_reposition_active_tab(int idx_to) {
  4272. editor_data.move_edited_scene_to_index(idx_to);
  4273. _update_scene_tabs();
  4274. }
  4275. void EditorNode::_thumbnail_done(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, const Variant &p_udata) {
  4276. int p_tab = p_udata.operator signed int();
  4277. if (p_preview.is_valid()) {
  4278. Rect2 rect = scene_tabs->get_tab_rect(p_tab);
  4279. rect.position += scene_tabs->get_global_position();
  4280. tab_preview->set_texture(p_preview);
  4281. tab_preview_panel->set_position(rect.position + Vector2(0, rect.size.height));
  4282. tab_preview_panel->show();
  4283. }
  4284. }
  4285. void EditorNode::_scene_tab_changed(int p_tab) {
  4286. tab_preview_panel->hide();
  4287. bool unsaved = (saved_version != editor_data.get_undo_redo().get_version());
  4288. if (p_tab == editor_data.get_edited_scene()) {
  4289. return; //pointless
  4290. }
  4291. uint64_t next_scene_version = editor_data.get_scene_version(p_tab);
  4292. editor_data.get_undo_redo().create_action(TTR("Switch Scene Tab"));
  4293. editor_data.get_undo_redo().add_do_method(this, "set_current_version", unsaved ? saved_version : 0);
  4294. editor_data.get_undo_redo().add_do_method(this, "set_current_scene", p_tab);
  4295. editor_data.get_undo_redo().add_do_method(this, "set_current_version", next_scene_version == 0 ? editor_data.get_undo_redo().get_version() + 1 : next_scene_version);
  4296. editor_data.get_undo_redo().add_undo_method(this, "set_current_version", next_scene_version);
  4297. editor_data.get_undo_redo().add_undo_method(this, "set_current_scene", editor_data.get_edited_scene());
  4298. editor_data.get_undo_redo().add_undo_method(this, "set_current_version", saved_version);
  4299. editor_data.get_undo_redo().commit_action();
  4300. }
  4301. ToolButton *EditorNode::add_bottom_panel_item(String p_text, Control *p_item) {
  4302. ToolButton *tb = memnew(ToolButton);
  4303. tb->connect("toggled", this, "_bottom_panel_switch", varray(bottom_panel_items.size()));
  4304. tb->set_text(p_text);
  4305. tb->set_toggle_mode(true);
  4306. tb->set_focus_mode(Control::FOCUS_NONE);
  4307. bottom_panel_vb->add_child(p_item);
  4308. bottom_panel_hb->raise();
  4309. bottom_panel_hb_editors->add_child(tb);
  4310. p_item->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4311. p_item->hide();
  4312. BottomPanelItem bpi;
  4313. bpi.button = tb;
  4314. bpi.control = p_item;
  4315. bpi.name = p_text;
  4316. bottom_panel_items.push_back(bpi);
  4317. return tb;
  4318. }
  4319. bool EditorNode::are_bottom_panels_hidden() const {
  4320. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4321. if (bottom_panel_items[i].button->is_pressed()) {
  4322. return false;
  4323. }
  4324. }
  4325. return true;
  4326. }
  4327. void EditorNode::hide_bottom_panel() {
  4328. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4329. if (bottom_panel_items[i].control->is_visible()) {
  4330. _bottom_panel_switch(false, i);
  4331. break;
  4332. }
  4333. }
  4334. }
  4335. void EditorNode::make_bottom_panel_item_visible(Control *p_item) {
  4336. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4337. if (bottom_panel_items[i].control == p_item) {
  4338. _bottom_panel_switch(true, i);
  4339. break;
  4340. }
  4341. }
  4342. }
  4343. void EditorNode::raise_bottom_panel_item(Control *p_item) {
  4344. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4345. if (bottom_panel_items[i].control == p_item) {
  4346. bottom_panel_items[i].button->raise();
  4347. SWAP(bottom_panel_items.write[i], bottom_panel_items.write[bottom_panel_items.size() - 1]);
  4348. break;
  4349. }
  4350. }
  4351. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4352. bottom_panel_items[i].button->disconnect("toggled", this, "_bottom_panel_switch");
  4353. bottom_panel_items[i].button->connect("toggled", this, "_bottom_panel_switch", varray(i));
  4354. }
  4355. }
  4356. void EditorNode::remove_bottom_panel_item(Control *p_item) {
  4357. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4358. if (bottom_panel_items[i].control == p_item) {
  4359. if (p_item->is_visible_in_tree()) {
  4360. _bottom_panel_switch(false, i);
  4361. }
  4362. bottom_panel_vb->remove_child(bottom_panel_items[i].control);
  4363. bottom_panel_hb_editors->remove_child(bottom_panel_items[i].button);
  4364. memdelete(bottom_panel_items[i].button);
  4365. bottom_panel_items.remove(i);
  4366. break;
  4367. }
  4368. }
  4369. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4370. bottom_panel_items[i].button->disconnect("toggled", this, "_bottom_panel_switch");
  4371. bottom_panel_items[i].button->connect("toggled", this, "_bottom_panel_switch", varray(i));
  4372. }
  4373. }
  4374. void EditorNode::_bottom_panel_switch(bool p_enable, int p_idx) {
  4375. ERR_FAIL_INDEX(p_idx, bottom_panel_items.size());
  4376. if (bottom_panel_items[p_idx].control->is_visible() == p_enable) {
  4377. return;
  4378. }
  4379. if (p_enable) {
  4380. for (int i = 0; i < bottom_panel_items.size(); i++) {
  4381. bottom_panel_items[i].button->set_pressed(i == p_idx);
  4382. bottom_panel_items[i].control->set_visible(i == p_idx);
  4383. }
  4384. if (ScriptEditor::get_singleton()->get_debugger() == bottom_panel_items[p_idx].control) { // this is the debug panel which uses tabs, so the top section should be smaller
  4385. bottom_panel->add_style_override("panel", gui_base->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles"));
  4386. } else {
  4387. bottom_panel->add_style_override("panel", gui_base->get_stylebox("panel", "TabContainer"));
  4388. }
  4389. center_split->set_dragger_visibility(SplitContainer::DRAGGER_VISIBLE);
  4390. center_split->set_collapsed(false);
  4391. if (bottom_panel_raise->is_pressed()) {
  4392. top_split->hide();
  4393. }
  4394. bottom_panel_raise->show();
  4395. } else {
  4396. bottom_panel->add_style_override("panel", gui_base->get_stylebox("panel", "TabContainer"));
  4397. bottom_panel_items[p_idx].button->set_pressed(false);
  4398. bottom_panel_items[p_idx].control->set_visible(false);
  4399. center_split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
  4400. center_split->set_collapsed(true);
  4401. bottom_panel_raise->hide();
  4402. if (bottom_panel_raise->is_pressed()) {
  4403. top_split->show();
  4404. }
  4405. }
  4406. }
  4407. void EditorNode::set_docks_visible(bool p_show) {
  4408. docks_visible = p_show;
  4409. _update_dock_slots_visibility(true);
  4410. }
  4411. bool EditorNode::get_docks_visible() const {
  4412. return docks_visible;
  4413. }
  4414. void EditorNode::_toggle_distraction_free_mode() {
  4415. if (EditorSettings::get_singleton()->get("interface/editor/separate_distraction_mode")) {
  4416. int screen = -1;
  4417. for (int i = 0; i < editor_table.size(); i++) {
  4418. if (editor_plugin_screen == editor_table[i]) {
  4419. screen = i;
  4420. break;
  4421. }
  4422. }
  4423. if (screen == EDITOR_SCRIPT) {
  4424. script_distraction = !script_distraction;
  4425. set_distraction_free_mode(script_distraction);
  4426. } else {
  4427. scene_distraction = !scene_distraction;
  4428. set_distraction_free_mode(scene_distraction);
  4429. }
  4430. } else {
  4431. set_distraction_free_mode(distraction_free->is_pressed());
  4432. }
  4433. }
  4434. void EditorNode::set_distraction_free_mode(bool p_enter) {
  4435. distraction_free->set_pressed(p_enter);
  4436. if (p_enter) {
  4437. if (docks_visible) {
  4438. set_docks_visible(false);
  4439. }
  4440. } else {
  4441. set_docks_visible(true);
  4442. }
  4443. }
  4444. bool EditorNode::is_distraction_free_mode_enabled() const {
  4445. return distraction_free->is_pressed();
  4446. }
  4447. void EditorNode::add_control_to_dock(DockSlot p_slot, Control *p_control) {
  4448. ERR_FAIL_INDEX(p_slot, DOCK_SLOT_MAX);
  4449. dock_slot[p_slot]->add_child(p_control);
  4450. _update_dock_slots_visibility();
  4451. }
  4452. void EditorNode::remove_control_from_dock(Control *p_control) {
  4453. Control *dock = nullptr;
  4454. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  4455. if (p_control->get_parent() == dock_slot[i]) {
  4456. dock = dock_slot[i];
  4457. break;
  4458. }
  4459. }
  4460. ERR_FAIL_COND_MSG(!dock, "Control was not in dock.");
  4461. dock->remove_child(p_control);
  4462. _update_dock_slots_visibility();
  4463. }
  4464. Variant EditorNode::drag_resource(const Ref<Resource> &p_res, Control *p_from) {
  4465. Control *drag_control = memnew(Control);
  4466. TextureRect *drag_preview = memnew(TextureRect);
  4467. Label *label = memnew(Label);
  4468. Ref<Texture> preview;
  4469. {
  4470. //todo make proper previews
  4471. Ref<ImageTexture> pic = gui_base->get_icon("FileBigThumb", "EditorIcons");
  4472. Ref<Image> img = pic->get_data();
  4473. img = img->duplicate();
  4474. img->resize(48, 48); //meh
  4475. Ref<ImageTexture> resized_pic = Ref<ImageTexture>(memnew(ImageTexture));
  4476. resized_pic->create_from_image(img);
  4477. preview = resized_pic;
  4478. }
  4479. drag_preview->set_texture(preview);
  4480. drag_control->add_child(drag_preview);
  4481. if (p_res->get_path().is_resource_file()) {
  4482. label->set_text(p_res->get_path().get_file());
  4483. } else if (p_res->get_name() != "") {
  4484. label->set_text(p_res->get_name());
  4485. } else {
  4486. label->set_text(p_res->get_class());
  4487. }
  4488. drag_control->add_child(label);
  4489. p_from->set_drag_preview(drag_control); //wait until it enters scene
  4490. label->set_position(Point2((preview->get_width() - label->get_minimum_size().width) / 2, preview->get_height()));
  4491. Dictionary drag_data;
  4492. drag_data["type"] = "resource";
  4493. drag_data["resource"] = p_res;
  4494. drag_data["from"] = p_from;
  4495. return drag_data;
  4496. }
  4497. Variant EditorNode::drag_files_and_dirs(const Vector<String> &p_paths, Control *p_from) {
  4498. bool has_folder = false;
  4499. bool has_file = false;
  4500. for (int i = 0; i < p_paths.size(); i++) {
  4501. bool is_folder = p_paths[i].ends_with("/");
  4502. has_folder |= is_folder;
  4503. has_file |= !is_folder;
  4504. }
  4505. int max_rows = 6;
  4506. int num_rows = p_paths.size() > max_rows ? max_rows - 1 : p_paths.size(); // Don't waste a row to say "1 more file" - list it instead.
  4507. VBoxContainer *vbox = memnew(VBoxContainer);
  4508. for (int i = 0; i < num_rows; i++) {
  4509. HBoxContainer *hbox = memnew(HBoxContainer);
  4510. TextureRect *icon = memnew(TextureRect);
  4511. Label *label = memnew(Label);
  4512. if (p_paths[i].ends_with("/")) {
  4513. label->set_text(p_paths[i].substr(0, p_paths[i].length() - 1).get_file());
  4514. icon->set_texture(gui_base->get_icon("Folder", "EditorIcons"));
  4515. } else {
  4516. label->set_text(p_paths[i].get_file());
  4517. icon->set_texture(gui_base->get_icon("File", "EditorIcons"));
  4518. }
  4519. icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  4520. icon->set_size(Size2(16, 16));
  4521. hbox->add_child(icon);
  4522. hbox->add_child(label);
  4523. vbox->add_child(hbox);
  4524. }
  4525. if (p_paths.size() > num_rows) {
  4526. Label *label = memnew(Label);
  4527. if (has_file && has_folder) {
  4528. label->set_text(vformat(TTR("%d more files or folders"), p_paths.size() - num_rows));
  4529. } else if (has_folder) {
  4530. label->set_text(vformat(TTR("%d more folders"), p_paths.size() - num_rows));
  4531. } else {
  4532. label->set_text(vformat(TTR("%d more files"), p_paths.size() - num_rows));
  4533. }
  4534. vbox->add_child(label);
  4535. }
  4536. p_from->set_drag_preview(vbox); //wait until it enters scene
  4537. Dictionary drag_data;
  4538. drag_data["type"] = has_folder ? "files_and_dirs" : "files";
  4539. drag_data["files"] = p_paths;
  4540. drag_data["from"] = p_from;
  4541. return drag_data;
  4542. }
  4543. void EditorNode::add_tool_menu_item(const String &p_name, Object *p_handler, const String &p_callback, const Variant &p_ud) {
  4544. ERR_FAIL_NULL(p_handler);
  4545. int idx = tool_menu->get_item_count();
  4546. tool_menu->add_item(p_name, TOOLS_CUSTOM);
  4547. Array parameters;
  4548. parameters.push_back(p_handler->get_instance_id());
  4549. parameters.push_back(p_callback);
  4550. parameters.push_back(p_ud);
  4551. tool_menu->set_item_metadata(idx, parameters);
  4552. }
  4553. void EditorNode::add_tool_submenu_item(const String &p_name, PopupMenu *p_submenu) {
  4554. ERR_FAIL_NULL(p_submenu);
  4555. ERR_FAIL_COND(p_submenu->get_parent() != nullptr);
  4556. tool_menu->add_child(p_submenu);
  4557. tool_menu->add_submenu_item(p_name, p_submenu->get_name(), TOOLS_CUSTOM);
  4558. }
  4559. void EditorNode::remove_tool_menu_item(const String &p_name) {
  4560. for (int i = 0; i < tool_menu->get_item_count(); i++) {
  4561. if (tool_menu->get_item_id(i) != TOOLS_CUSTOM) {
  4562. continue;
  4563. }
  4564. if (tool_menu->get_item_text(i) == p_name) {
  4565. if (tool_menu->get_item_submenu(i) != "") {
  4566. Node *n = tool_menu->get_node(tool_menu->get_item_submenu(i));
  4567. tool_menu->remove_child(n);
  4568. memdelete(n);
  4569. }
  4570. tool_menu->remove_item(i);
  4571. tool_menu->set_as_minsize();
  4572. return;
  4573. }
  4574. }
  4575. }
  4576. void EditorNode::_global_menu_action(const Variant &p_id, const Variant &p_meta) {
  4577. int id = (int)p_id;
  4578. if (id == GLOBAL_NEW_WINDOW) {
  4579. if (OS::get_singleton()->get_main_loop()) {
  4580. List<String> args;
  4581. args.push_back("-p");
  4582. String exec = OS::get_singleton()->get_executable_path();
  4583. OS::ProcessID pid = 0;
  4584. OS::get_singleton()->execute(exec, args, false, &pid);
  4585. }
  4586. } else if (id == GLOBAL_SCENE) {
  4587. int idx = (int)p_meta;
  4588. scene_tabs->set_current_tab(idx);
  4589. }
  4590. }
  4591. void EditorNode::_dropped_files(const Vector<String> &p_files, int p_screen) {
  4592. String to_path = ProjectSettings::get_singleton()->globalize_path(get_filesystem_dock()->get_selected_path());
  4593. _add_dropped_files_recursive(p_files, to_path);
  4594. EditorFileSystem::get_singleton()->scan_changes();
  4595. }
  4596. void EditorNode::_add_dropped_files_recursive(const Vector<String> &p_files, String to_path) {
  4597. DirAccessRef dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  4598. for (int i = 0; i < p_files.size(); i++) {
  4599. String from = p_files[i];
  4600. String to = to_path.plus_file(from.get_file());
  4601. if (dir->dir_exists(from)) {
  4602. Vector<String> sub_files;
  4603. DirAccessRef sub_dir = DirAccess::open(from);
  4604. sub_dir->list_dir_begin();
  4605. String next_file = sub_dir->get_next();
  4606. while (next_file != "") {
  4607. if (next_file == "." || next_file == "..") {
  4608. next_file = sub_dir->get_next();
  4609. continue;
  4610. }
  4611. sub_files.push_back(from.plus_file(next_file));
  4612. next_file = sub_dir->get_next();
  4613. }
  4614. if (!sub_files.empty()) {
  4615. dir->make_dir(to);
  4616. _add_dropped_files_recursive(sub_files, to);
  4617. }
  4618. continue;
  4619. }
  4620. dir->copy(from, to);
  4621. }
  4622. }
  4623. void EditorNode::_file_access_close_error_notify(const String &p_str) {
  4624. add_io_error(vformat(TTR("Unable to write to file '%s', file in use, locked or lacking permissions."), p_str));
  4625. }
  4626. void EditorNode::reload_scene(const String &p_path) {
  4627. //first of all, reload internal textures, materials, meshes, etc. as they might have changed on disk
  4628. List<Ref<Resource>> cached;
  4629. ResourceCache::get_cached_resources(&cached);
  4630. List<Ref<Resource>> to_clear; //clear internal resources from previous scene from being used
  4631. for (List<Ref<Resource>>::Element *E = cached.front(); E; E = E->next()) {
  4632. if (E->get()->get_path().begins_with(p_path + "::")) { //subresources of existing scene
  4633. to_clear.push_back(E->get());
  4634. }
  4635. }
  4636. //so reload reloads everything, clear subresources of previous scene
  4637. while (to_clear.front()) {
  4638. to_clear.front()->get()->set_path("");
  4639. to_clear.pop_front();
  4640. }
  4641. int scene_idx = -1;
  4642. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  4643. if (editor_data.get_scene_path(i) == p_path) {
  4644. scene_idx = i;
  4645. break;
  4646. }
  4647. }
  4648. int current_tab = editor_data.get_edited_scene();
  4649. if (scene_idx == -1) {
  4650. if (get_edited_scene()) {
  4651. //scene is not open, so at it might be instanced. We'll refresh the whole scene later.
  4652. editor_data.get_undo_redo().clear_history();
  4653. }
  4654. return;
  4655. }
  4656. if (current_tab == scene_idx) {
  4657. editor_data.apply_changes_in_editors();
  4658. _set_scene_metadata(p_path);
  4659. }
  4660. //remove scene
  4661. _remove_scene(scene_idx, false);
  4662. //reload scene
  4663. load_scene(p_path, true, false, true, true);
  4664. //adjust index so tab is back a the previous position
  4665. editor_data.move_edited_scene_to_index(scene_idx);
  4666. get_undo_redo()->clear_history();
  4667. //recover the tab
  4668. scene_tabs->set_current_tab(current_tab);
  4669. }
  4670. int EditorNode::plugin_init_callback_count = 0;
  4671. void EditorNode::add_plugin_init_callback(EditorPluginInitializeCallback p_callback) {
  4672. ERR_FAIL_COND(plugin_init_callback_count == MAX_INIT_CALLBACKS);
  4673. plugin_init_callbacks[plugin_init_callback_count++] = p_callback;
  4674. }
  4675. EditorPluginInitializeCallback EditorNode::plugin_init_callbacks[EditorNode::MAX_INIT_CALLBACKS];
  4676. int EditorNode::build_callback_count = 0;
  4677. void EditorNode::add_build_callback(EditorBuildCallback p_callback) {
  4678. ERR_FAIL_COND(build_callback_count == MAX_INIT_CALLBACKS);
  4679. build_callbacks[build_callback_count++] = p_callback;
  4680. }
  4681. EditorBuildCallback EditorNode::build_callbacks[EditorNode::MAX_BUILD_CALLBACKS];
  4682. bool EditorNode::call_build() {
  4683. bool builds_successful = true;
  4684. for (int i = 0; i < build_callback_count && builds_successful; i++) {
  4685. if (!build_callbacks[i]()) {
  4686. ERR_PRINT("A Godot Engine build callback failed.");
  4687. builds_successful = false;
  4688. }
  4689. }
  4690. if (builds_successful && !editor_data.call_build()) {
  4691. ERR_PRINT("An EditorPlugin build callback failed.");
  4692. builds_successful = false;
  4693. }
  4694. return builds_successful;
  4695. }
  4696. void EditorNode::_inherit_imported(const String &p_action) {
  4697. open_imported->hide();
  4698. load_scene(open_import_request, true, true);
  4699. }
  4700. void EditorNode::_open_imported() {
  4701. load_scene(open_import_request, true, false, true, true);
  4702. }
  4703. void EditorNode::dim_editor(bool p_dimming, bool p_force_dim) {
  4704. // Dimming can be forced regardless of the editor setting, which is useful when quitting the editor.
  4705. if ((p_force_dim || EditorSettings::get_singleton()->get("interface/editor/dim_editor_on_dialog_popup")) && p_dimming) {
  4706. dimmed = true;
  4707. gui_base->set_modulate(Color(0.5, 0.5, 0.5));
  4708. } else {
  4709. dimmed = false;
  4710. gui_base->set_modulate(Color(1, 1, 1));
  4711. }
  4712. }
  4713. bool EditorNode::is_editor_dimmed() const {
  4714. return dimmed;
  4715. }
  4716. void EditorNode::open_export_template_manager() {
  4717. export_template_manager->popup_manager();
  4718. }
  4719. void EditorNode::add_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin) {
  4720. resource_conversion_plugins.push_back(p_plugin);
  4721. }
  4722. void EditorNode::remove_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin) {
  4723. resource_conversion_plugins.erase(p_plugin);
  4724. }
  4725. Vector<Ref<EditorResourceConversionPlugin>> EditorNode::find_resource_conversion_plugin(const Ref<Resource> &p_for_resource) {
  4726. Vector<Ref<EditorResourceConversionPlugin>> ret;
  4727. for (int i = 0; i < resource_conversion_plugins.size(); i++) {
  4728. if (resource_conversion_plugins[i].is_valid() && resource_conversion_plugins[i]->handles(p_for_resource)) {
  4729. ret.push_back(resource_conversion_plugins[i]);
  4730. }
  4731. }
  4732. return ret;
  4733. }
  4734. void EditorNode::_bottom_panel_raise_toggled(bool p_pressed) {
  4735. top_split->set_visible(!p_pressed);
  4736. }
  4737. void EditorNode::_update_video_driver_color() {
  4738. // TODO: Probably should de-hardcode this and add to editor settings.
  4739. if (video_driver->get_text() == "GLES2") {
  4740. video_driver->add_color_override("font_color", Color::hex(0x5586a4ff));
  4741. } else if (video_driver->get_text() == "GLES3") {
  4742. video_driver->add_color_override("font_color", Color::hex(0xa5557dff));
  4743. }
  4744. }
  4745. void EditorNode::_video_driver_selected(int p_which) {
  4746. String driver = video_driver->get_item_metadata(p_which);
  4747. String current = OS::get_singleton()->get_video_driver_name(OS::get_singleton()->get_current_video_driver());
  4748. if (driver == current) {
  4749. return;
  4750. }
  4751. video_driver_request = driver;
  4752. video_restart_dialog->popup_centered_minsize();
  4753. video_driver->select(video_driver_current);
  4754. _update_video_driver_color();
  4755. }
  4756. void EditorNode::_resource_saved(RES p_resource, const String &p_path) {
  4757. if (EditorFileSystem::get_singleton()) {
  4758. EditorFileSystem::get_singleton()->update_file(p_path);
  4759. }
  4760. singleton->editor_folding.save_resource_folding(p_resource, p_path);
  4761. }
  4762. void EditorNode::_resource_loaded(RES p_resource, const String &p_path) {
  4763. singleton->editor_folding.load_resource_folding(p_resource, p_path);
  4764. }
  4765. void EditorNode::_project_settings_changed() {
  4766. SceneTree *tree = get_tree();
  4767. tree->set_debug_collisions_color(GLOBAL_GET("debug/shapes/collision/shape_color"));
  4768. tree->set_debug_collision_contact_color(GLOBAL_GET("debug/shapes/collision/contact_color"));
  4769. tree->set_debug_navigation_color(GLOBAL_GET("debug/shapes/navigation/geometry_color"));
  4770. tree->set_debug_navigation_disabled_color(GLOBAL_GET("debug/shapes/navigation/disabled_geometry_color"));
  4771. _update_title();
  4772. }
  4773. void EditorNode::_feature_profile_changed() {
  4774. Ref<EditorFeatureProfile> profile = feature_profile_manager->get_current_profile();
  4775. TabContainer *import_tabs = cast_to<TabContainer>(import_dock->get_parent());
  4776. TabContainer *node_tabs = cast_to<TabContainer>(node_dock->get_parent());
  4777. TabContainer *fs_tabs = cast_to<TabContainer>(filesystem_dock->get_parent());
  4778. if (profile.is_valid()) {
  4779. node_tabs->set_tab_hidden(node_dock->get_index(), profile->is_feature_disabled(EditorFeatureProfile::FEATURE_NODE_DOCK));
  4780. // The Import dock is useless without the FileSystem dock. Ensure the configuration is valid.
  4781. bool fs_dock_disabled = profile->is_feature_disabled(EditorFeatureProfile::FEATURE_FILESYSTEM_DOCK);
  4782. fs_tabs->set_tab_hidden(filesystem_dock->get_index(), fs_dock_disabled);
  4783. import_tabs->set_tab_hidden(import_dock->get_index(), fs_dock_disabled || profile->is_feature_disabled(EditorFeatureProfile::FEATURE_IMPORT_DOCK));
  4784. main_editor_buttons[EDITOR_3D]->set_visible(!profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D));
  4785. main_editor_buttons[EDITOR_SCRIPT]->set_visible(!profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT));
  4786. if (AssetLibraryEditorPlugin::is_available()) {
  4787. main_editor_buttons[EDITOR_ASSETLIB]->set_visible(!profile->is_feature_disabled(EditorFeatureProfile::FEATURE_ASSET_LIB));
  4788. }
  4789. if ((profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D) && singleton->main_editor_buttons[EDITOR_3D]->is_pressed()) ||
  4790. (profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT) && singleton->main_editor_buttons[EDITOR_SCRIPT]->is_pressed()) ||
  4791. (AssetLibraryEditorPlugin::is_available() && profile->is_feature_disabled(EditorFeatureProfile::FEATURE_ASSET_LIB) && singleton->main_editor_buttons[EDITOR_ASSETLIB]->is_pressed())) {
  4792. _editor_select(EDITOR_2D);
  4793. }
  4794. } else {
  4795. import_tabs->set_tab_hidden(import_dock->get_index(), false);
  4796. node_tabs->set_tab_hidden(node_dock->get_index(), false);
  4797. fs_tabs->set_tab_hidden(filesystem_dock->get_index(), false);
  4798. import_dock->set_visible(true);
  4799. node_dock->set_visible(true);
  4800. filesystem_dock->set_visible(true);
  4801. main_editor_buttons[EDITOR_3D]->set_visible(true);
  4802. main_editor_buttons[EDITOR_SCRIPT]->set_visible(true);
  4803. if (AssetLibraryEditorPlugin::is_available()) {
  4804. main_editor_buttons[EDITOR_ASSETLIB]->set_visible(true);
  4805. }
  4806. }
  4807. _update_dock_slots_visibility();
  4808. }
  4809. void EditorNode::_bind_methods() {
  4810. ClassDB::bind_method("_menu_option", &EditorNode::_menu_option);
  4811. ClassDB::bind_method("_tool_menu_option", &EditorNode::_tool_menu_option);
  4812. ClassDB::bind_method("_menu_confirm_current", &EditorNode::_menu_confirm_current);
  4813. ClassDB::bind_method("_dialog_action", &EditorNode::_dialog_action);
  4814. ClassDB::bind_method("_editor_select", &EditorNode::_editor_select);
  4815. ClassDB::bind_method("_node_renamed", &EditorNode::_node_renamed);
  4816. ClassDB::bind_method("edit_node", &EditorNode::edit_node);
  4817. ClassDB::bind_method("_unhandled_input", &EditorNode::_unhandled_input);
  4818. ClassDB::bind_method("_update_file_menu_opened", &EditorNode::_update_file_menu_opened);
  4819. ClassDB::bind_method("_update_file_menu_closed", &EditorNode::_update_file_menu_closed);
  4820. ClassDB::bind_method("_android_build_source_selected", &EditorNode::_android_build_source_selected);
  4821. ClassDB::bind_method(D_METHOD("push_item", "object", "property", "inspector_only"), &EditorNode::push_item, DEFVAL(""), DEFVAL(false));
  4822. ClassDB::bind_method("_get_scene_metadata", &EditorNode::_get_scene_metadata);
  4823. ClassDB::bind_method("set_edited_scene", &EditorNode::set_edited_scene);
  4824. ClassDB::bind_method("open_request", &EditorNode::open_request);
  4825. ClassDB::bind_method("_inherit_request", &EditorNode::_inherit_request);
  4826. ClassDB::bind_method("_instance_request", &EditorNode::_instance_request);
  4827. ClassDB::bind_method("_close_messages", &EditorNode::_close_messages);
  4828. ClassDB::bind_method("_show_messages", &EditorNode::_show_messages);
  4829. ClassDB::bind_method("_vp_resized", &EditorNode::_vp_resized);
  4830. ClassDB::bind_method("_version_button_pressed", &EditorNode::_version_button_pressed);
  4831. ClassDB::bind_method("_quick_opened", &EditorNode::_quick_opened);
  4832. ClassDB::bind_method("_quick_run", &EditorNode::_quick_run);
  4833. ClassDB::bind_method("_open_recent_scene", &EditorNode::_open_recent_scene);
  4834. ClassDB::bind_method("stop_child_process", &EditorNode::stop_child_process);
  4835. ClassDB::bind_method("get_script_create_dialog", &EditorNode::get_script_create_dialog);
  4836. ClassDB::bind_method("_sources_changed", &EditorNode::_sources_changed);
  4837. ClassDB::bind_method("_fs_changed", &EditorNode::_fs_changed);
  4838. ClassDB::bind_method("_dock_select_draw", &EditorNode::_dock_select_draw);
  4839. ClassDB::bind_method("_dock_select_input", &EditorNode::_dock_select_input);
  4840. ClassDB::bind_method("_dock_pre_popup", &EditorNode::_dock_pre_popup);
  4841. ClassDB::bind_method("_dock_split_dragged", &EditorNode::_dock_split_dragged);
  4842. ClassDB::bind_method("_save_docks", &EditorNode::_save_docks);
  4843. ClassDB::bind_method("_dock_popup_exit", &EditorNode::_dock_popup_exit);
  4844. ClassDB::bind_method("_dock_move_left", &EditorNode::_dock_move_left);
  4845. ClassDB::bind_method("_dock_move_right", &EditorNode::_dock_move_right);
  4846. ClassDB::bind_method("_dock_tab_changed", &EditorNode::_dock_tab_changed);
  4847. ClassDB::bind_method("_layout_menu_option", &EditorNode::_layout_menu_option);
  4848. ClassDB::bind_method("set_current_scene", &EditorNode::set_current_scene);
  4849. ClassDB::bind_method("set_current_version", &EditorNode::set_current_version);
  4850. ClassDB::bind_method("_scene_tab_changed", &EditorNode::_scene_tab_changed);
  4851. ClassDB::bind_method("_scene_tab_closed", &EditorNode::_scene_tab_closed);
  4852. ClassDB::bind_method("_scene_tab_hover", &EditorNode::_scene_tab_hover);
  4853. ClassDB::bind_method("_scene_tab_exit", &EditorNode::_scene_tab_exit);
  4854. ClassDB::bind_method("_scene_tab_input", &EditorNode::_scene_tab_input);
  4855. ClassDB::bind_method("_reposition_active_tab", &EditorNode::_reposition_active_tab);
  4856. ClassDB::bind_method("_thumbnail_done", &EditorNode::_thumbnail_done);
  4857. ClassDB::bind_method("_scene_tab_script_edited", &EditorNode::_scene_tab_script_edited);
  4858. ClassDB::bind_method("_set_main_scene_state", &EditorNode::_set_main_scene_state);
  4859. ClassDB::bind_method("_update_scene_tabs", &EditorNode::_update_scene_tabs);
  4860. ClassDB::bind_method("_discard_changes", &EditorNode::_discard_changes);
  4861. ClassDB::bind_method("_update_recent_scenes", &EditorNode::_update_recent_scenes);
  4862. ClassDB::bind_method("_pick_main_scene_custom_action", &EditorNode::_pick_main_scene_custom_action);
  4863. ClassDB::bind_method("_clear_undo_history", &EditorNode::_clear_undo_history);
  4864. ClassDB::bind_method("_dropped_files", &EditorNode::_dropped_files);
  4865. ClassDB::bind_method(D_METHOD("_global_menu_action"), &EditorNode::_global_menu_action, DEFVAL(Variant()));
  4866. ClassDB::bind_method("_toggle_distraction_free_mode", &EditorNode::_toggle_distraction_free_mode);
  4867. ClassDB::bind_method("_version_control_menu_option", &EditorNode::_version_control_menu_option);
  4868. ClassDB::bind_method("edit_item_resource", &EditorNode::edit_item_resource);
  4869. ClassDB::bind_method(D_METHOD("get_gui_base"), &EditorNode::get_gui_base);
  4870. ClassDB::bind_method(D_METHOD("_bottom_panel_switch"), &EditorNode::_bottom_panel_switch);
  4871. ClassDB::bind_method(D_METHOD("_open_imported"), &EditorNode::_open_imported);
  4872. ClassDB::bind_method(D_METHOD("_inherit_imported"), &EditorNode::_inherit_imported);
  4873. ClassDB::bind_method("_copy_warning", &EditorNode::_copy_warning);
  4874. ClassDB::bind_method(D_METHOD("_resources_reimported"), &EditorNode::_resources_reimported);
  4875. ClassDB::bind_method(D_METHOD("_bottom_panel_raise_toggled"), &EditorNode::_bottom_panel_raise_toggled);
  4876. ClassDB::bind_method(D_METHOD("_on_plugin_ready"), &EditorNode::_on_plugin_ready);
  4877. ClassDB::bind_method(D_METHOD("_video_driver_selected"), &EditorNode::_video_driver_selected);
  4878. ClassDB::bind_method(D_METHOD("_resources_changed"), &EditorNode::_resources_changed);
  4879. ClassDB::bind_method(D_METHOD("_reload_modified_scenes"), &EditorNode::_reload_modified_scenes);
  4880. ClassDB::bind_method(D_METHOD("_reload_project_settings"), &EditorNode::_reload_project_settings);
  4881. ClassDB::bind_method(D_METHOD("_resave_scenes"), &EditorNode::_resave_scenes);
  4882. ClassDB::bind_method(D_METHOD("_project_settings_changed"), &EditorNode::_project_settings_changed);
  4883. ClassDB::bind_method(D_METHOD("_feature_profile_changed"), &EditorNode::_feature_profile_changed);
  4884. ClassDB::bind_method("_screenshot", &EditorNode::_screenshot);
  4885. ClassDB::bind_method("_request_screenshot", &EditorNode::_request_screenshot);
  4886. ClassDB::bind_method("_save_screenshot", &EditorNode::_save_screenshot);
  4887. ADD_SIGNAL(MethodInfo("play_pressed"));
  4888. ADD_SIGNAL(MethodInfo("pause_pressed"));
  4889. ADD_SIGNAL(MethodInfo("stop_pressed"));
  4890. ADD_SIGNAL(MethodInfo("request_help_search"));
  4891. ADD_SIGNAL(MethodInfo("script_add_function_request", PropertyInfo(Variant::OBJECT, "obj"), PropertyInfo(Variant::STRING, "function"), PropertyInfo(Variant::POOL_STRING_ARRAY, "args")));
  4892. ADD_SIGNAL(MethodInfo("resource_saved", PropertyInfo(Variant::OBJECT, "obj")));
  4893. ADD_SIGNAL(MethodInfo("scene_saved", PropertyInfo(Variant::STRING, "path")));
  4894. }
  4895. static Node *_resource_get_edited_scene() {
  4896. return EditorNode::get_singleton()->get_edited_scene();
  4897. }
  4898. void EditorNode::_print_handler(void *p_this, const String &p_string, bool p_error) {
  4899. EditorNode *en = (EditorNode *)p_this;
  4900. en->log->add_message(p_string, p_error ? EditorLog::MSG_TYPE_ERROR : EditorLog::MSG_TYPE_STD);
  4901. }
  4902. static void _execute_thread(void *p_ud) {
  4903. EditorNode::ExecuteThreadArgs *eta = (EditorNode::ExecuteThreadArgs *)p_ud;
  4904. Error err = OS::get_singleton()->execute(eta->path, eta->args, true, nullptr, &eta->output, &eta->exitcode, true, &eta->execute_output_mutex);
  4905. print_verbose("Thread exit status: " + itos(eta->exitcode));
  4906. if (err != OK) {
  4907. eta->exitcode = err;
  4908. }
  4909. eta->done.set();
  4910. }
  4911. int EditorNode::execute_and_show_output(const String &p_title, const String &p_path, const List<String> &p_arguments, bool p_close_on_ok, bool p_close_on_errors) {
  4912. execute_output_dialog->set_title(p_title);
  4913. execute_output_dialog->get_ok()->set_disabled(true);
  4914. execute_outputs->clear();
  4915. execute_outputs->set_scroll_follow(true);
  4916. execute_output_dialog->popup_centered_ratio();
  4917. ExecuteThreadArgs eta;
  4918. eta.path = p_path;
  4919. eta.args = p_arguments;
  4920. eta.exitcode = 255;
  4921. int prev_len = 0;
  4922. eta.execute_output_thread.start(_execute_thread, &eta);
  4923. while (!eta.done.is_set()) {
  4924. eta.execute_output_mutex.lock();
  4925. if (prev_len != eta.output.length()) {
  4926. String to_add = eta.output.substr(prev_len, eta.output.length());
  4927. prev_len = eta.output.length();
  4928. execute_outputs->add_text(to_add);
  4929. Main::iteration();
  4930. }
  4931. eta.execute_output_mutex.unlock();
  4932. OS::get_singleton()->delay_usec(1000);
  4933. }
  4934. eta.execute_output_thread.wait_to_finish();
  4935. execute_outputs->add_text("\nExit Code: " + itos(eta.exitcode));
  4936. if (p_close_on_errors && eta.exitcode != 0) {
  4937. execute_output_dialog->hide();
  4938. }
  4939. if (p_close_on_ok && eta.exitcode == 0) {
  4940. execute_output_dialog->hide();
  4941. }
  4942. execute_output_dialog->get_ok()->set_disabled(false);
  4943. return eta.exitcode;
  4944. }
  4945. EditorNode::EditorNode() {
  4946. OS::get_singleton()->benchmark_begin_measure("editor");
  4947. EditorPropertyNameProcessor *epnp = memnew(EditorPropertyNameProcessor);
  4948. add_child(epnp);
  4949. Input::get_singleton()->set_use_accumulated_input(true);
  4950. Resource::_get_local_scene_func = _resource_get_edited_scene;
  4951. VisualServer::get_singleton()->textures_keep_original(true);
  4952. VisualServer::get_singleton()->set_debug_generate_wireframes(true);
  4953. NavigationServer::get_singleton()->set_active(false); // no nav by default if editor
  4954. PhysicsServer::get_singleton()->set_active(false); // no physics by default if editor
  4955. Physics2DServer::get_singleton()->set_active(false); // no physics by default if editor
  4956. ScriptServer::set_scripting_enabled(false); // no scripting by default if editor
  4957. EditorHelp::generate_doc(); //before any editor classes are created
  4958. SceneState::set_disable_placeholders(true);
  4959. ResourceLoader::clear_translation_remaps(); //no remaps using during editor
  4960. ResourceLoader::clear_path_remaps();
  4961. InputDefault *id = Object::cast_to<InputDefault>(Input::get_singleton());
  4962. if (id) {
  4963. if (!OS::get_singleton()->has_touchscreen_ui_hint() && Input::get_singleton()) {
  4964. //only if no touchscreen ui hint, set emulation
  4965. id->set_emulate_touch_from_mouse(false); //just disable just in case
  4966. }
  4967. id->set_custom_mouse_cursor(RES());
  4968. }
  4969. singleton = this;
  4970. exiting = false;
  4971. dimmed = false;
  4972. last_checked_version = 0;
  4973. changing_scene = false;
  4974. _initializing_addons = false;
  4975. docks_visible = true;
  4976. restoring_scenes = false;
  4977. cmdline_export_mode = false;
  4978. scene_distraction = false;
  4979. script_distraction = false;
  4980. TranslationServer::get_singleton()->set_enabled(false);
  4981. // load settings
  4982. if (!EditorSettings::get_singleton()) {
  4983. EditorSettings::create();
  4984. }
  4985. FileAccess::set_backup_save(EDITOR_GET("filesystem/on_save/safe_save_on_backup_then_rename"));
  4986. {
  4987. int display_scale = EditorSettings::get_singleton()->get("interface/editor/display_scale");
  4988. switch (display_scale) {
  4989. case 0:
  4990. // Try applying a suitable display scale automatically.
  4991. editor_set_scale(EditorSettings::get_singleton()->get_auto_display_scale());
  4992. break;
  4993. case 1:
  4994. editor_set_scale(0.75);
  4995. break;
  4996. case 2:
  4997. editor_set_scale(1.0);
  4998. break;
  4999. case 3:
  5000. editor_set_scale(1.25);
  5001. break;
  5002. case 4:
  5003. editor_set_scale(1.5);
  5004. break;
  5005. case 5:
  5006. editor_set_scale(1.75);
  5007. break;
  5008. case 6:
  5009. editor_set_scale(2.0);
  5010. break;
  5011. default:
  5012. editor_set_scale(EditorSettings::get_singleton()->get("interface/editor/custom_display_scale"));
  5013. break;
  5014. }
  5015. }
  5016. // Define a minimum window size to prevent UI elements from overlapping or being cut off
  5017. OS::get_singleton()->set_min_window_size(Size2(1024, 600) * EDSCALE);
  5018. FileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  5019. EditorFileDialog::set_default_show_hidden_files(EditorSettings::get_singleton()->get("filesystem/file_dialog/show_hidden_files"));
  5020. EditorFileDialog::set_default_display_mode((EditorFileDialog::DisplayMode)EditorSettings::get_singleton()->get("filesystem/file_dialog/display_mode").operator int());
  5021. int swap_cancel_ok = EDITOR_GET("interface/editor/accept_dialog_cancel_ok_buttons");
  5022. if (swap_cancel_ok != 0) { // 0 is auto, set in register_scene based on OS.
  5023. // Swap on means OK first.
  5024. AcceptDialog::set_swap_ok_cancel(swap_cancel_ok == 2);
  5025. }
  5026. ResourceLoader::set_abort_on_missing_resources(false);
  5027. ResourceLoader::set_error_notify_func(this, _load_error_notify);
  5028. ResourceLoader::set_dependency_error_notify_func(this, _dependency_error_report);
  5029. { //register importers at the beginning, so dialogs are created with the right extensions
  5030. Ref<ResourceImporterTexture> import_texture;
  5031. import_texture.instance();
  5032. ResourceFormatImporter::get_singleton()->add_importer(import_texture);
  5033. Ref<ResourceImporterLayeredTexture> import_3d;
  5034. import_3d.instance();
  5035. import_3d->set_3d(true);
  5036. ResourceFormatImporter::get_singleton()->add_importer(import_3d);
  5037. Ref<ResourceImporterLayeredTexture> import_array;
  5038. import_array.instance();
  5039. import_array->set_3d(false);
  5040. ResourceFormatImporter::get_singleton()->add_importer(import_array);
  5041. Ref<ResourceImporterImage> import_image;
  5042. import_image.instance();
  5043. ResourceFormatImporter::get_singleton()->add_importer(import_image);
  5044. Ref<ResourceImporterTextureAtlas> import_texture_atlas;
  5045. import_texture_atlas.instance();
  5046. ResourceFormatImporter::get_singleton()->add_importer(import_texture_atlas);
  5047. Ref<ResourceImporterCSVTranslation> import_csv_translation;
  5048. import_csv_translation.instance();
  5049. ResourceFormatImporter::get_singleton()->add_importer(import_csv_translation);
  5050. Ref<ResourceImporterWAV> import_wav;
  5051. import_wav.instance();
  5052. ResourceFormatImporter::get_singleton()->add_importer(import_wav);
  5053. Ref<ResourceImporterOBJ> import_obj;
  5054. import_obj.instance();
  5055. ResourceFormatImporter::get_singleton()->add_importer(import_obj);
  5056. Ref<ResourceImporterScene> import_scene;
  5057. import_scene.instance();
  5058. ResourceFormatImporter::get_singleton()->add_importer(import_scene);
  5059. {
  5060. Ref<EditorSceneImporterCollada> import_collada;
  5061. import_collada.instance();
  5062. import_scene->add_importer(import_collada);
  5063. Ref<EditorOBJImporter> import_obj2;
  5064. import_obj2.instance();
  5065. import_scene->add_importer(import_obj2);
  5066. Ref<EditorSceneImporterESCN> import_escn;
  5067. import_escn.instance();
  5068. import_scene->add_importer(import_escn);
  5069. }
  5070. Ref<ResourceImporterBitMap> import_bitmap;
  5071. import_bitmap.instance();
  5072. ResourceFormatImporter::get_singleton()->add_importer(import_bitmap);
  5073. }
  5074. {
  5075. Ref<EditorInspectorDefaultPlugin> eidp;
  5076. eidp.instance();
  5077. EditorInspector::add_inspector_plugin(eidp);
  5078. Ref<EditorInspectorRootMotionPlugin> rmp;
  5079. rmp.instance();
  5080. EditorInspector::add_inspector_plugin(rmp);
  5081. Ref<EditorInspectorShaderModePlugin> smp;
  5082. smp.instance();
  5083. EditorInspector::add_inspector_plugin(smp);
  5084. }
  5085. editor_selection = memnew(EditorSelection);
  5086. EditorFileSystem *efs = memnew(EditorFileSystem);
  5087. add_child(efs);
  5088. //used for previews
  5089. FileDialog::get_icon_func = _file_dialog_get_icon;
  5090. FileDialog::register_func = _file_dialog_register;
  5091. FileDialog::unregister_func = _file_dialog_unregister;
  5092. EditorFileDialog::get_icon_func = _file_dialog_get_icon;
  5093. EditorFileDialog::register_func = _editor_file_dialog_register;
  5094. EditorFileDialog::unregister_func = _editor_file_dialog_unregister;
  5095. editor_export = memnew(EditorExport);
  5096. add_child(editor_export);
  5097. // Exporters might need the theme
  5098. theme = create_custom_theme();
  5099. register_exporters();
  5100. ClassDB::set_class_enabled("RootMotionView", true);
  5101. //defs here, use EDITOR_GET in logic
  5102. EDITOR_DEF_RST("interface/scene_tabs/always_show_close_button", false);
  5103. EDITOR_DEF_RST("interface/scene_tabs/resize_if_many_tabs", true);
  5104. EDITOR_DEF_RST("interface/scene_tabs/minimum_width", 50);
  5105. EDITOR_DEF("run/output/always_clear_output_on_play", true);
  5106. EDITOR_DEF("run/output/always_open_output_on_play", true);
  5107. EDITOR_DEF("run/output/always_close_output_on_stop", true);
  5108. EDITOR_DEF("interface/editor/save_on_focus_loss", false);
  5109. EDITOR_DEF_RST("interface/editor/save_each_scene_on_quit", true);
  5110. EDITOR_DEF("interface/editor/quit_confirmation", true);
  5111. EDITOR_DEF("interface/editor/update_continuously", false);
  5112. #if defined(ANDROID_ENABLED) || defined(JAVASCRIPT_ENABLED)
  5113. EDITOR_DEF("interface/editor/show_update_spinner", true);
  5114. EDITOR_DEF("interface/editor/update_vital_only", true);
  5115. #else
  5116. EDITOR_DEF("interface/editor/show_update_spinner", false);
  5117. EDITOR_DEF("interface/editor/update_vital_only", false);
  5118. #endif
  5119. EDITOR_DEF("interface/editor/localize_settings", true);
  5120. EDITOR_DEF_RST("interface/scene_tabs/restore_scenes_on_load", false);
  5121. EDITOR_DEF_RST("interface/scene_tabs/show_thumbnail_on_hover", true);
  5122. EDITOR_DEF_RST("interface/inspector/default_property_name_style", EditorPropertyNameProcessor::STYLE_CAPITALIZED);
  5123. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/inspector/default_property_name_style", PROPERTY_HINT_ENUM, "Raw,Capitalized,Localized"));
  5124. EDITOR_DEF_RST("interface/inspector/default_float_step", 0.001);
  5125. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::REAL, "interface/inspector/default_float_step", PROPERTY_HINT_RANGE, "0,1,0"));
  5126. EDITOR_DEF_RST("interface/inspector/disable_folding", false);
  5127. EDITOR_DEF_RST("interface/inspector/auto_unfold_foreign_scenes", true);
  5128. EDITOR_DEF("interface/inspector/horizontal_vector2_editing", false);
  5129. EDITOR_DEF("interface/inspector/horizontal_vector_types_editing", true);
  5130. EDITOR_DEF("interface/inspector/open_resources_in_current_inspector", true);
  5131. EDITOR_DEF("interface/inspector/resources_to_open_in_new_inspector", "Script,MeshLibrary,TileSet");
  5132. EDITOR_DEF("interface/inspector/default_color_picker_mode", 0);
  5133. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "interface/inspector/default_color_picker_mode", PROPERTY_HINT_ENUM, "RGB,HSV,RAW", PROPERTY_USAGE_DEFAULT));
  5134. EDITOR_DEF("version_control/username", "");
  5135. EDITOR_DEF("version_control/ssh_public_key_path", "");
  5136. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "version_control/ssh_public_key_path", PROPERTY_HINT_GLOBAL_FILE));
  5137. EDITOR_DEF("version_control/ssh_private_key_path", "");
  5138. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "version_control/ssh_private_key_path", PROPERTY_HINT_GLOBAL_FILE));
  5139. theme_base = memnew(Control);
  5140. add_child(theme_base);
  5141. theme_base->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  5142. gui_base = memnew(Panel);
  5143. theme_base->add_child(gui_base);
  5144. gui_base->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  5145. theme_base->set_theme(theme);
  5146. gui_base->set_theme(theme);
  5147. gui_base->add_style_override("panel", gui_base->get_stylebox("Background", "EditorStyles"));
  5148. resource_preview = memnew(EditorResourcePreview);
  5149. add_child(resource_preview);
  5150. progress_dialog = memnew(ProgressDialog);
  5151. gui_base->add_child(progress_dialog);
  5152. // take up all screen
  5153. gui_base->set_anchor(MARGIN_RIGHT, Control::ANCHOR_END);
  5154. gui_base->set_anchor(MARGIN_BOTTOM, Control::ANCHOR_END);
  5155. gui_base->set_end(Point2(0, 0));
  5156. main_vbox = memnew(VBoxContainer);
  5157. gui_base->add_child(main_vbox);
  5158. main_vbox->set_anchors_and_margins_preset(Control::PRESET_WIDE, Control::PRESET_MODE_MINSIZE, 8);
  5159. main_vbox->add_constant_override("separation", 8 * EDSCALE);
  5160. menu_hb = memnew(HBoxContainer);
  5161. main_vbox->add_child(menu_hb);
  5162. left_l_hsplit = memnew(HSplitContainer);
  5163. main_vbox->add_child(left_l_hsplit);
  5164. left_l_hsplit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5165. left_l_vsplit = memnew(VSplitContainer);
  5166. left_l_hsplit->add_child(left_l_vsplit);
  5167. dock_slot[DOCK_SLOT_LEFT_UL] = memnew(TabContainer);
  5168. left_l_vsplit->add_child(dock_slot[DOCK_SLOT_LEFT_UL]);
  5169. dock_slot[DOCK_SLOT_LEFT_BL] = memnew(TabContainer);
  5170. left_l_vsplit->add_child(dock_slot[DOCK_SLOT_LEFT_BL]);
  5171. left_r_hsplit = memnew(HSplitContainer);
  5172. left_l_hsplit->add_child(left_r_hsplit);
  5173. left_r_vsplit = memnew(VSplitContainer);
  5174. left_r_hsplit->add_child(left_r_vsplit);
  5175. dock_slot[DOCK_SLOT_LEFT_UR] = memnew(TabContainer);
  5176. left_r_vsplit->add_child(dock_slot[DOCK_SLOT_LEFT_UR]);
  5177. dock_slot[DOCK_SLOT_LEFT_BR] = memnew(TabContainer);
  5178. left_r_vsplit->add_child(dock_slot[DOCK_SLOT_LEFT_BR]);
  5179. main_hsplit = memnew(HSplitContainer);
  5180. left_r_hsplit->add_child(main_hsplit);
  5181. VBoxContainer *center_vb = memnew(VBoxContainer);
  5182. main_hsplit->add_child(center_vb);
  5183. center_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5184. center_split = memnew(VSplitContainer);
  5185. center_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5186. center_split->set_collapsed(false);
  5187. center_vb->add_child(center_split);
  5188. right_hsplit = memnew(HSplitContainer);
  5189. main_hsplit->add_child(right_hsplit);
  5190. right_l_vsplit = memnew(VSplitContainer);
  5191. right_hsplit->add_child(right_l_vsplit);
  5192. dock_slot[DOCK_SLOT_RIGHT_UL] = memnew(TabContainer);
  5193. right_l_vsplit->add_child(dock_slot[DOCK_SLOT_RIGHT_UL]);
  5194. dock_slot[DOCK_SLOT_RIGHT_BL] = memnew(TabContainer);
  5195. right_l_vsplit->add_child(dock_slot[DOCK_SLOT_RIGHT_BL]);
  5196. right_r_vsplit = memnew(VSplitContainer);
  5197. right_hsplit->add_child(right_r_vsplit);
  5198. dock_slot[DOCK_SLOT_RIGHT_UR] = memnew(TabContainer);
  5199. right_r_vsplit->add_child(dock_slot[DOCK_SLOT_RIGHT_UR]);
  5200. dock_slot[DOCK_SLOT_RIGHT_BR] = memnew(TabContainer);
  5201. right_r_vsplit->add_child(dock_slot[DOCK_SLOT_RIGHT_BR]);
  5202. // Store them for easier access
  5203. vsplits.push_back(left_l_vsplit);
  5204. vsplits.push_back(left_r_vsplit);
  5205. vsplits.push_back(right_l_vsplit);
  5206. vsplits.push_back(right_r_vsplit);
  5207. hsplits.push_back(left_l_hsplit);
  5208. hsplits.push_back(left_r_hsplit);
  5209. hsplits.push_back(main_hsplit);
  5210. hsplits.push_back(right_hsplit);
  5211. for (int i = 0; i < vsplits.size(); i++) {
  5212. vsplits[i]->connect("dragged", this, "_dock_split_dragged");
  5213. hsplits[i]->connect("dragged", this, "_dock_split_dragged");
  5214. }
  5215. dock_select_popup = memnew(PopupPanel);
  5216. gui_base->add_child(dock_select_popup);
  5217. VBoxContainer *dock_vb = memnew(VBoxContainer);
  5218. dock_select_popup->add_child(dock_vb);
  5219. HBoxContainer *dock_hb = memnew(HBoxContainer);
  5220. dock_tab_move_left = memnew(ToolButton);
  5221. dock_tab_move_left->set_icon(theme->get_icon("Back", "EditorIcons"));
  5222. dock_tab_move_left->set_focus_mode(Control::FOCUS_NONE);
  5223. dock_tab_move_left->connect("pressed", this, "_dock_move_left");
  5224. dock_hb->add_child(dock_tab_move_left);
  5225. Label *dock_label = memnew(Label);
  5226. dock_label->set_text(TTR("Dock Position"));
  5227. dock_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5228. dock_label->set_align(Label::ALIGN_CENTER);
  5229. dock_hb->add_child(dock_label);
  5230. dock_tab_move_right = memnew(ToolButton);
  5231. dock_tab_move_right->set_icon(theme->get_icon("Forward", "EditorIcons"));
  5232. dock_tab_move_right->set_focus_mode(Control::FOCUS_NONE);
  5233. dock_tab_move_right->connect("pressed", this, "_dock_move_right");
  5234. dock_hb->add_child(dock_tab_move_right);
  5235. dock_vb->add_child(dock_hb);
  5236. dock_select = memnew(Control);
  5237. dock_select->set_custom_minimum_size(Size2(128, 64) * EDSCALE);
  5238. dock_select->connect("gui_input", this, "_dock_select_input");
  5239. dock_select->connect("draw", this, "_dock_select_draw");
  5240. dock_select->connect("mouse_exited", this, "_dock_popup_exit");
  5241. dock_select->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5242. dock_vb->add_child(dock_select);
  5243. dock_select_popup->set_as_minsize();
  5244. dock_select_rect_over = -1;
  5245. dock_popup_selected = -1;
  5246. for (int i = 0; i < DOCK_SLOT_MAX; i++) {
  5247. dock_slot[i]->set_custom_minimum_size(Size2(170, 0) * EDSCALE);
  5248. dock_slot[i]->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5249. dock_slot[i]->set_popup(dock_select_popup);
  5250. dock_slot[i]->connect("pre_popup_pressed", this, "_dock_pre_popup", varray(i));
  5251. dock_slot[i]->set_tab_align(TabContainer::ALIGN_LEFT);
  5252. dock_slot[i]->set_drag_to_rearrange_enabled(true);
  5253. dock_slot[i]->set_tabs_rearrange_group(1);
  5254. dock_slot[i]->connect("tab_changed", this, "_dock_tab_changed");
  5255. dock_slot[i]->set_use_hidden_tabs_for_min_size(true);
  5256. }
  5257. dock_drag_timer = memnew(Timer);
  5258. add_child(dock_drag_timer);
  5259. dock_drag_timer->set_wait_time(0.5);
  5260. dock_drag_timer->set_one_shot(true);
  5261. dock_drag_timer->connect("timeout", this, "_save_docks");
  5262. top_split = memnew(VSplitContainer);
  5263. center_split->add_child(top_split);
  5264. top_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5265. top_split->set_collapsed(true);
  5266. VBoxContainer *srt = memnew(VBoxContainer);
  5267. srt->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5268. top_split->add_child(srt);
  5269. srt->add_constant_override("separation", 0);
  5270. tab_preview_panel = memnew(Panel);
  5271. tab_preview_panel->set_size(Size2(100, 100) * EDSCALE);
  5272. tab_preview_panel->hide();
  5273. tab_preview_panel->set_self_modulate(Color(1, 1, 1, 0.7));
  5274. gui_base->add_child(tab_preview_panel);
  5275. tab_preview = memnew(TextureRect);
  5276. tab_preview->set_stretch_mode(TextureRect::STRETCH_KEEP_ASPECT_CENTERED);
  5277. tab_preview->set_size(Size2(96, 96) * EDSCALE);
  5278. tab_preview->set_position(Point2(2, 2) * EDSCALE);
  5279. tab_preview_panel->add_child(tab_preview);
  5280. scene_tabs = memnew(Tabs);
  5281. scene_tabs->add_style_override("tab_fg", gui_base->get_stylebox("SceneTabFG", "EditorStyles"));
  5282. scene_tabs->add_style_override("tab_bg", gui_base->get_stylebox("SceneTabBG", "EditorStyles"));
  5283. scene_tabs->set_select_with_rmb(true);
  5284. scene_tabs->add_tab("unsaved");
  5285. scene_tabs->set_tab_align(Tabs::ALIGN_LEFT);
  5286. scene_tabs->set_tab_close_display_policy((bool(EDITOR_DEF("interface/scene_tabs/always_show_close_button", false)) ? Tabs::CLOSE_BUTTON_SHOW_ALWAYS : Tabs::CLOSE_BUTTON_SHOW_ACTIVE_ONLY));
  5287. scene_tabs->set_min_width(int(EDITOR_DEF("interface/scene_tabs/minimum_width", 50)) * EDSCALE);
  5288. scene_tabs->set_drag_to_rearrange_enabled(true);
  5289. scene_tabs->connect("tab_changed", this, "_scene_tab_changed");
  5290. scene_tabs->connect("right_button_pressed", this, "_scene_tab_script_edited");
  5291. scene_tabs->connect("tab_close", this, "_scene_tab_closed", varray(SCENE_TAB_CLOSE));
  5292. scene_tabs->connect("tab_hover", this, "_scene_tab_hover");
  5293. scene_tabs->connect("mouse_exited", this, "_scene_tab_exit");
  5294. scene_tabs->connect("gui_input", this, "_scene_tab_input");
  5295. scene_tabs->connect("reposition_active_tab_request", this, "_reposition_active_tab");
  5296. scene_tabs->connect("resized", this, "_update_scene_tabs");
  5297. tabbar_container = memnew(HBoxContainer);
  5298. scene_tabs->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5299. scene_tabs_context_menu = memnew(PopupMenu);
  5300. tabbar_container->add_child(scene_tabs_context_menu);
  5301. scene_tabs_context_menu->connect("id_pressed", this, "_menu_option");
  5302. scene_tabs_context_menu->set_hide_on_window_lose_focus(true);
  5303. srt->add_child(tabbar_container);
  5304. tabbar_container->add_child(scene_tabs);
  5305. distraction_free = memnew(ToolButton);
  5306. #ifdef OSX_ENABLED
  5307. distraction_free->set_shortcut(ED_SHORTCUT("editor/distraction_free_mode", TTR("Distraction Free Mode"), KEY_MASK_CMD | KEY_MASK_CTRL | KEY_D));
  5308. #else
  5309. distraction_free->set_shortcut(ED_SHORTCUT("editor/distraction_free_mode", TTR("Distraction Free Mode"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F11));
  5310. #endif
  5311. distraction_free->set_tooltip(TTR("Toggle distraction-free mode."));
  5312. distraction_free->connect("pressed", this, "_toggle_distraction_free_mode");
  5313. distraction_free->set_icon(gui_base->get_icon("DistractionFree", "EditorIcons"));
  5314. distraction_free->set_toggle_mode(true);
  5315. scene_tab_add = memnew(ToolButton);
  5316. tabbar_container->add_child(scene_tab_add);
  5317. tabbar_container->add_child(distraction_free);
  5318. scene_tab_add->set_tooltip(TTR("Add a new scene."));
  5319. scene_tab_add->set_icon(gui_base->get_icon("Add", "EditorIcons"));
  5320. scene_tab_add->add_color_override("icon_color_normal", Color(0.6f, 0.6f, 0.6f, 0.8f));
  5321. scene_tab_add->connect("pressed", this, "_menu_option", make_binds(FILE_NEW_SCENE));
  5322. scene_root_parent = memnew(PanelContainer);
  5323. scene_root_parent->set_custom_minimum_size(Size2(0, 80) * EDSCALE);
  5324. scene_root_parent->add_style_override("panel", gui_base->get_stylebox("Content", "EditorStyles"));
  5325. scene_root_parent->set_draw_behind_parent(true);
  5326. srt->add_child(scene_root_parent);
  5327. scene_root_parent->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5328. scene_root = memnew(Viewport);
  5329. //scene_root->set_usage(Viewport::USAGE_2D); canvas BG mode prevents usage of this as 2D
  5330. scene_root->set_disable_3d(true);
  5331. VisualServer::get_singleton()->viewport_set_hide_scenario(scene_root->get_viewport_rid(), true);
  5332. scene_root->set_disable_input(true);
  5333. scene_root->set_as_audio_listener_2d(true);
  5334. viewport = memnew(VBoxContainer);
  5335. viewport->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5336. viewport->add_constant_override("separation", 0);
  5337. scene_root_parent->add_child(viewport);
  5338. HBoxContainer *left_menu_hb = memnew(HBoxContainer);
  5339. menu_hb->add_child(left_menu_hb);
  5340. file_menu = memnew(MenuButton);
  5341. file_menu->set_flat(false);
  5342. file_menu->set_switch_on_hover(true);
  5343. file_menu->set_text(TTR("Scene"));
  5344. file_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  5345. left_menu_hb->add_child(file_menu);
  5346. prev_scene = memnew(ToolButton);
  5347. prev_scene->set_icon(gui_base->get_icon("PrevScene", "EditorIcons"));
  5348. prev_scene->set_tooltip(TTR("Go to previously opened scene."));
  5349. prev_scene->set_disabled(true);
  5350. prev_scene->connect("pressed", this, "_menu_option", make_binds(FILE_OPEN_PREV));
  5351. gui_base->add_child(prev_scene);
  5352. prev_scene->set_position(Point2(3, 24));
  5353. prev_scene->hide();
  5354. accept = memnew(AcceptDialog);
  5355. gui_base->add_child(accept);
  5356. accept->connect("confirmed", this, "_menu_confirm_current");
  5357. save_accept = memnew(AcceptDialog);
  5358. gui_base->add_child(save_accept);
  5359. save_accept->connect("confirmed", this, "_menu_option", make_binds((int)MenuOptions::FILE_SAVE_AS_SCENE));
  5360. project_export = memnew(ProjectExportDialog);
  5361. gui_base->add_child(project_export);
  5362. dependency_error = memnew(DependencyErrorDialog);
  5363. gui_base->add_child(dependency_error);
  5364. dependency_fixer = memnew(DependencyEditor);
  5365. gui_base->add_child(dependency_fixer);
  5366. settings_config_dialog = memnew(EditorSettingsDialog);
  5367. gui_base->add_child(settings_config_dialog);
  5368. project_settings = memnew(ProjectSettingsEditor(&editor_data));
  5369. gui_base->add_child(project_settings);
  5370. ProjectSettings::get_singleton()->connect("project_settings_changed", this, "_project_settings_changed");
  5371. run_settings_dialog = memnew(RunSettingsDialog);
  5372. gui_base->add_child(run_settings_dialog);
  5373. export_template_manager = memnew(ExportTemplateManager);
  5374. gui_base->add_child(export_template_manager);
  5375. feature_profile_manager = memnew(EditorFeatureProfileManager);
  5376. gui_base->add_child(feature_profile_manager);
  5377. about = memnew(EditorAbout);
  5378. gui_base->add_child(about);
  5379. feature_profile_manager->connect("current_feature_profile_changed", this, "_feature_profile_changed");
  5380. warning = memnew(AcceptDialog);
  5381. warning->add_button(TTR("Copy Text"), true, "copy");
  5382. gui_base->add_child(warning);
  5383. warning->connect("custom_action", this, "_copy_warning");
  5384. ED_SHORTCUT("editor/next_tab", TTR("Next tab"), KEY_MASK_CMD + KEY_TAB);
  5385. ED_SHORTCUT("editor/prev_tab", TTR("Previous tab"), KEY_MASK_CMD + KEY_MASK_SHIFT + KEY_TAB);
  5386. ED_SHORTCUT("editor/filter_files", TTR("Filter Files..."), KEY_MASK_ALT + KEY_MASK_CMD + KEY_P);
  5387. PopupMenu *p;
  5388. file_menu->set_tooltip(TTR("Operations with scene files."));
  5389. p = file_menu->get_popup();
  5390. p->set_hide_on_window_lose_focus(true);
  5391. p->add_shortcut(ED_SHORTCUT("editor/new_scene", TTR("New Scene"), KEY_MASK_CMD + KEY_N), FILE_NEW_SCENE);
  5392. p->add_shortcut(ED_SHORTCUT("editor/new_inherited_scene", TTR("New Inherited Scene..."), KEY_MASK_CMD + KEY_MASK_SHIFT + KEY_N), FILE_NEW_INHERITED_SCENE);
  5393. p->add_shortcut(ED_SHORTCUT("editor/open_scene", TTR("Open Scene..."), KEY_MASK_CMD + KEY_O), FILE_OPEN_SCENE);
  5394. p->add_shortcut(ED_SHORTCUT("editor/reopen_closed_scene", TTR("Reopen Closed Scene"), KEY_MASK_CMD + KEY_MASK_SHIFT + KEY_T), FILE_OPEN_PREV);
  5395. p->add_submenu_item(TTR("Open Recent"), "RecentScenes", FILE_OPEN_RECENT);
  5396. p->add_separator();
  5397. p->add_shortcut(ED_SHORTCUT("editor/save_scene", TTR("Save Scene"), KEY_MASK_CMD + KEY_S), FILE_SAVE_SCENE);
  5398. p->add_shortcut(ED_SHORTCUT("editor/save_scene_as", TTR("Save Scene As..."), KEY_MASK_SHIFT + KEY_MASK_CMD + KEY_S), FILE_SAVE_AS_SCENE);
  5399. p->add_shortcut(ED_SHORTCUT("editor/save_all_scenes", TTR("Save All Scenes"), KEY_MASK_ALT + KEY_MASK_SHIFT + KEY_MASK_CMD + KEY_S), FILE_SAVE_ALL_SCENES);
  5400. p->add_separator();
  5401. p->add_shortcut(ED_SHORTCUT("editor/quick_open", TTR("Quick Open..."), KEY_MASK_SHIFT + KEY_MASK_ALT + KEY_O), FILE_QUICK_OPEN);
  5402. p->add_shortcut(ED_SHORTCUT("editor/quick_open_scene", TTR("Quick Open Scene..."), KEY_MASK_SHIFT + KEY_MASK_CMD + KEY_O), FILE_QUICK_OPEN_SCENE);
  5403. p->add_shortcut(ED_SHORTCUT("editor/quick_open_script", TTR("Quick Open Script..."), KEY_MASK_ALT + KEY_MASK_CMD + KEY_O), FILE_QUICK_OPEN_SCRIPT);
  5404. p->add_separator();
  5405. PopupMenu *pm_export = memnew(PopupMenu);
  5406. pm_export->set_name("Export");
  5407. p->add_child(pm_export);
  5408. p->add_submenu_item(TTR("Convert To..."), "Export");
  5409. pm_export->add_shortcut(ED_SHORTCUT("editor/convert_to_MeshLibrary", TTR("MeshLibrary...")), FILE_EXPORT_MESH_LIBRARY);
  5410. pm_export->add_shortcut(ED_SHORTCUT("editor/convert_to_TileSet", TTR("TileSet...")), FILE_EXPORT_TILESET);
  5411. pm_export->connect("id_pressed", this, "_menu_option");
  5412. p->add_separator();
  5413. p->add_shortcut(ED_SHORTCUT("editor/undo", TTR("Undo"), KEY_MASK_CMD + KEY_Z), EDIT_UNDO, true);
  5414. p->add_shortcut(ED_SHORTCUT("editor/redo", TTR("Redo"), KEY_MASK_SHIFT + KEY_MASK_CMD + KEY_Z), EDIT_REDO, true);
  5415. p->add_separator();
  5416. p->add_shortcut(ED_SHORTCUT("editor/reload_saved_scene", TTR("Reload Saved Scene")), EDIT_RELOAD_SAVED_SCENE);
  5417. p->add_shortcut(ED_SHORTCUT("editor/close_scene", TTR("Close Scene"), KEY_MASK_SHIFT + KEY_MASK_CMD + KEY_W), FILE_CLOSE);
  5418. recent_scenes = memnew(PopupMenu);
  5419. recent_scenes->set_name("RecentScenes");
  5420. p->add_child(recent_scenes);
  5421. recent_scenes->connect("id_pressed", this, "_open_recent_scene");
  5422. p->add_separator();
  5423. p->add_shortcut(ED_SHORTCUT("editor/file_quit", TTR("Quit"), KEY_MASK_CMD + KEY_Q), FILE_QUIT, true);
  5424. project_menu = memnew(MenuButton);
  5425. project_menu->set_flat(false);
  5426. project_menu->set_switch_on_hover(true);
  5427. project_menu->set_tooltip(TTR("Miscellaneous project or scene-wide tools."));
  5428. project_menu->set_text(TTR("Project"));
  5429. project_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  5430. left_menu_hb->add_child(project_menu);
  5431. p = project_menu->get_popup();
  5432. p->set_hide_on_window_lose_focus(true);
  5433. p->add_shortcut(ED_SHORTCUT("editor/project_settings", TTR("Project Settings...")), RUN_SETTINGS);
  5434. p->connect("id_pressed", this, "_menu_option");
  5435. vcs_actions_menu = VersionControlEditorPlugin::get_singleton()->get_version_control_actions_panel();
  5436. vcs_actions_menu->set_name("Version Control");
  5437. vcs_actions_menu->connect("index_pressed", this, "_version_control_menu_option");
  5438. p->add_separator();
  5439. p->add_child(vcs_actions_menu);
  5440. p->add_submenu_item(TTR("Version Control"), "Version Control");
  5441. vcs_actions_menu->add_item(TTR("Set Up Version Control"), RUN_VCS_SETTINGS);
  5442. vcs_actions_menu->add_item(TTR("Shut Down Version Control"), RUN_VCS_SHUT_DOWN);
  5443. p->add_separator();
  5444. p->add_shortcut(ED_SHORTCUT("editor/export", TTR("Export...")), FILE_EXPORT_PROJECT);
  5445. #ifndef ANDROID_ENABLED
  5446. p->add_item(TTR("Install Android Build Template..."), FILE_INSTALL_ANDROID_SOURCE);
  5447. p->add_item(TTR("Open User Data Folder"), RUN_USER_DATA_FOLDER);
  5448. #endif
  5449. plugin_config_dialog = memnew(PluginConfigDialog);
  5450. plugin_config_dialog->connect("plugin_ready", this, "_on_plugin_ready");
  5451. gui_base->add_child(plugin_config_dialog);
  5452. tool_menu = memnew(PopupMenu);
  5453. tool_menu->set_name("Tools");
  5454. tool_menu->connect("index_pressed", this, "_tool_menu_option");
  5455. p->add_child(tool_menu);
  5456. p->add_submenu_item(TTR("Tools"), "Tools");
  5457. tool_menu->add_item(TTR("Orphan Resource Explorer..."), TOOLS_ORPHAN_RESOURCES);
  5458. p->add_separator();
  5459. p->add_shortcut(ED_SHORTCUT("editor/reload_current_project", TTR("Reload Current Project")), RELOAD_CURRENT_PROJECT);
  5460. #ifdef OSX_ENABLED
  5461. p->add_shortcut(ED_SHORTCUT("editor/quit_to_project_list", TTR("Quit to Project List"), KEY_MASK_SHIFT + KEY_MASK_ALT + KEY_Q), RUN_PROJECT_MANAGER, true);
  5462. #else
  5463. p->add_shortcut(ED_SHORTCUT("editor/quit_to_project_list", TTR("Quit to Project List"), KEY_MASK_SHIFT + KEY_MASK_CMD + KEY_Q), RUN_PROJECT_MANAGER, true);
  5464. #endif
  5465. menu_hb->add_spacer();
  5466. main_editor_button_vb = memnew(HBoxContainer);
  5467. menu_hb->add_child(main_editor_button_vb);
  5468. debug_menu = memnew(MenuButton);
  5469. debug_menu->set_flat(false);
  5470. debug_menu->set_switch_on_hover(true);
  5471. debug_menu->set_text(TTR("Debug"));
  5472. debug_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  5473. left_menu_hb->add_child(debug_menu);
  5474. p = debug_menu->get_popup();
  5475. p->set_hide_on_window_lose_focus(true);
  5476. p->set_hide_on_checkable_item_selection(false);
  5477. p->add_check_shortcut(ED_SHORTCUT("editor/deploy_with_remote_debug", TTR("Deploy with Remote Debug")), RUN_DEPLOY_REMOTE_DEBUG);
  5478. p->set_item_tooltip(
  5479. p->get_item_count() - 1,
  5480. TTR("When this option is enabled, using one-click deploy will make the executable attempt to connect to this computer's IP so the running project can be debugged.\nThis option is intended to be used for remote debugging (typically with a mobile device).\nYou don't need to enable it to use the GDScript debugger locally."));
  5481. p->add_check_shortcut(ED_SHORTCUT("editor/small_deploy_with_network_fs", TTR("Small Deploy with Network Filesystem")), RUN_FILE_SERVER);
  5482. p->set_item_tooltip(
  5483. p->get_item_count() - 1,
  5484. TTR("When this option is enabled, using one-click deploy for Android will only export an executable without the project data.\nThe filesystem will be provided from the project by the editor over the network.\nOn Android, deploying will use the USB cable for faster performance. This option speeds up testing for projects with large assets."));
  5485. p->add_separator();
  5486. p->add_check_shortcut(ED_SHORTCUT("editor/visible_collision_shapes", TTR("Visible Collision Shapes")), RUN_DEBUG_COLLISONS);
  5487. p->set_item_tooltip(
  5488. p->get_item_count() - 1,
  5489. TTR("When this option is enabled, collision shapes and raycast nodes (for 2D and 3D) will be visible in the running project."));
  5490. p->add_check_shortcut(ED_SHORTCUT("editor/visible_navigation", TTR("Visible Navigation")), RUN_DEBUG_NAVIGATION);
  5491. p->set_item_tooltip(
  5492. p->get_item_count() - 1,
  5493. TTR("When this option is enabled, navigation meshes and polygons will be visible in the running project."));
  5494. if (GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES3") {
  5495. p->add_separator();
  5496. p->add_check_shortcut(ED_SHORTCUT("editor/use_shader_fallbacks", TTR("Force Shader Fallbacks")), RUN_DEBUG_SHADER_FALLBACKS);
  5497. p->set_item_tooltip(
  5498. p->get_item_count() - 1,
  5499. TTR("When this option is enabled, shaders will be used in their fallback form (either visible via an ubershader or hidden) during all the run time.\nThis is useful for verifying the look and performance of fallbacks, which are normally displayed briefly.\nAsynchronous shader compilation must be enabled in the project settings for this option to make a difference."));
  5500. }
  5501. p->add_separator();
  5502. p->add_check_shortcut(ED_SHORTCUT("editor/sync_scene_changes", TTR("Synchronize Scene Changes")), RUN_LIVE_DEBUG);
  5503. p->set_item_tooltip(
  5504. p->get_item_count() - 1,
  5505. TTR("When this option is enabled, any changes made to the scene in the editor will be replicated in the running project.\nWhen used remotely on a device, this is more efficient when the network filesystem option is enabled."));
  5506. p->add_check_shortcut(ED_SHORTCUT("editor/sync_script_changes", TTR("Synchronize Script Changes")), RUN_RELOAD_SCRIPTS);
  5507. p->set_item_tooltip(
  5508. p->get_item_count() - 1,
  5509. TTR("When this option is enabled, any script that is saved will be reloaded in the running project.\nWhen used remotely on a device, this is more efficient when the network filesystem option is enabled."));
  5510. p->connect("id_pressed", this, "_menu_option");
  5511. menu_hb->add_spacer();
  5512. settings_menu = memnew(MenuButton);
  5513. settings_menu->set_flat(false);
  5514. settings_menu->set_switch_on_hover(true);
  5515. settings_menu->set_text(TTR("Editor"));
  5516. settings_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  5517. left_menu_hb->add_child(settings_menu);
  5518. p = settings_menu->get_popup();
  5519. p->set_hide_on_window_lose_focus(true);
  5520. #ifdef OSX_ENABLED
  5521. p->add_shortcut(ED_SHORTCUT("editor/editor_settings", TTR("Editor Settings..."), KEY_MASK_CMD + KEY_COMMA), SETTINGS_PREFERENCES);
  5522. #else
  5523. p->add_shortcut(ED_SHORTCUT("editor/editor_settings", TTR("Editor Settings...")), SETTINGS_PREFERENCES);
  5524. #endif
  5525. p->add_separator();
  5526. editor_layouts = memnew(PopupMenu);
  5527. editor_layouts->set_name("Layouts");
  5528. p->add_child(editor_layouts);
  5529. editor_layouts->connect("id_pressed", this, "_layout_menu_option");
  5530. p->add_submenu_item(TTR("Editor Layout"), "Layouts");
  5531. p->add_separator();
  5532. #ifdef OSX_ENABLED
  5533. p->add_shortcut(ED_SHORTCUT("editor/take_screenshot", TTR("Take Screenshot"), KEY_MASK_CMD | KEY_F12), EDITOR_SCREENSHOT);
  5534. #else
  5535. p->add_shortcut(ED_SHORTCUT("editor/take_screenshot", TTR("Take Screenshot"), KEY_MASK_CTRL | KEY_F12), EDITOR_SCREENSHOT);
  5536. #endif
  5537. p->set_item_tooltip(p->get_item_count() - 1, TTR("Screenshots are stored in the Editor Data/Settings Folder."));
  5538. #ifndef ANDROID_ENABLED
  5539. #ifdef OSX_ENABLED
  5540. p->add_shortcut(ED_SHORTCUT("editor/fullscreen_mode", TTR("Toggle Fullscreen"), KEY_MASK_CMD | KEY_MASK_CTRL | KEY_F), SETTINGS_TOGGLE_FULLSCREEN);
  5541. #else
  5542. p->add_shortcut(ED_SHORTCUT("editor/fullscreen_mode", TTR("Toggle Fullscreen"), KEY_MASK_SHIFT | KEY_F11), SETTINGS_TOGGLE_FULLSCREEN);
  5543. #endif
  5544. #endif
  5545. p->add_separator();
  5546. #ifndef ANDROID_ENABLED
  5547. if (OS::get_singleton()->get_data_path() == OS::get_singleton()->get_config_path()) {
  5548. // Configuration and data folders are located in the same place (Windows/macOS)
  5549. p->add_item(TTR("Open Editor Data/Settings Folder"), SETTINGS_EDITOR_DATA_FOLDER);
  5550. } else {
  5551. // Separate configuration and data folders (Linux)
  5552. p->add_item(TTR("Open Editor Data Folder"), SETTINGS_EDITOR_DATA_FOLDER);
  5553. p->add_item(TTR("Open Editor Settings Folder"), SETTINGS_EDITOR_CONFIG_FOLDER);
  5554. }
  5555. p->add_separator();
  5556. #endif
  5557. p->add_item(TTR("Manage Editor Features..."), SETTINGS_MANAGE_FEATURE_PROFILES);
  5558. #ifndef ANDROID_ENABLED
  5559. p->add_item(TTR("Manage Export Templates..."), SETTINGS_MANAGE_EXPORT_TEMPLATES);
  5560. #endif
  5561. // Help Menu
  5562. help_menu = memnew(MenuButton);
  5563. help_menu->set_flat(false);
  5564. help_menu->set_switch_on_hover(true);
  5565. help_menu->set_text(TTR("Help"));
  5566. help_menu->add_style_override("hover", gui_base->get_stylebox("MenuHover", "EditorStyles"));
  5567. left_menu_hb->add_child(help_menu);
  5568. p = help_menu->get_popup();
  5569. p->set_hide_on_window_lose_focus(true);
  5570. p->connect("id_pressed", this, "_menu_option");
  5571. #ifdef OSX_ENABLED
  5572. p->add_icon_shortcut(gui_base->get_icon("HelpSearch", "EditorIcons"), ED_SHORTCUT("editor/editor_help", TTR("Search Help"), KEY_MASK_ALT | KEY_SPACE), HELP_SEARCH);
  5573. #else
  5574. p->add_icon_shortcut(gui_base->get_icon("HelpSearch", "EditorIcons"), ED_SHORTCUT("editor/editor_help", TTR("Search Help"), KEY_F1), HELP_SEARCH);
  5575. #endif
  5576. p->add_separator();
  5577. p->add_icon_shortcut(gui_base->get_icon("ExternalLink", "EditorIcons"), ED_SHORTCUT("editor/online_docs", TTR("Online Documentation")), HELP_DOCS);
  5578. p->add_icon_shortcut(gui_base->get_icon("ExternalLink", "EditorIcons"), ED_SHORTCUT("editor/q&a", TTR("Questions & Answers")), HELP_QA);
  5579. p->add_icon_shortcut(gui_base->get_icon("ExternalLink", "EditorIcons"), ED_SHORTCUT("editor/report_a_bug", TTR("Report a Bug")), HELP_REPORT_A_BUG);
  5580. p->add_icon_shortcut(gui_base->get_icon("ExternalLink", "EditorIcons"), ED_SHORTCUT("editor/suggest_a_feature", TTR("Suggest a Feature")), HELP_SUGGEST_A_FEATURE);
  5581. p->add_icon_shortcut(gui_base->get_icon("ExternalLink", "EditorIcons"), ED_SHORTCUT("editor/send_docs_feedback", TTR("Send Docs Feedback")), HELP_SEND_DOCS_FEEDBACK);
  5582. p->add_icon_shortcut(gui_base->get_icon("ExternalLink", "EditorIcons"), ED_SHORTCUT("editor/community", TTR("Community")), HELP_COMMUNITY);
  5583. p->add_separator();
  5584. p->add_icon_shortcut(gui_base->get_icon("Godot", "EditorIcons"), ED_SHORTCUT("editor/about", TTR("About Godot")), HELP_ABOUT);
  5585. p->add_icon_shortcut(gui_base->get_icon("Heart", "EditorIcons"), ED_SHORTCUT("editor/support_development", TTR("Support Godot Development")), HELP_SUPPORT_GODOT_DEVELOPMENT);
  5586. HBoxContainer *play_hb = memnew(HBoxContainer);
  5587. menu_hb->add_child(play_hb);
  5588. play_button = memnew(ToolButton);
  5589. play_hb->add_child(play_button);
  5590. play_button->set_toggle_mode(true);
  5591. play_button->set_icon(gui_base->get_icon("MainPlay", "EditorIcons"));
  5592. play_button->set_focus_mode(Control::FOCUS_NONE);
  5593. play_button->connect("pressed", this, "_menu_option", make_binds(RUN_PLAY));
  5594. play_button->set_tooltip(TTR("Play the project."));
  5595. #ifdef OSX_ENABLED
  5596. play_button->set_shortcut(ED_SHORTCUT("editor/play", TTR("Play"), KEY_MASK_CMD | KEY_B));
  5597. #else
  5598. play_button->set_shortcut(ED_SHORTCUT("editor/play", TTR("Play"), KEY_F5));
  5599. #endif
  5600. pause_button = memnew(ToolButton);
  5601. pause_button->set_toggle_mode(true);
  5602. pause_button->set_icon(gui_base->get_icon("Pause", "EditorIcons"));
  5603. pause_button->set_focus_mode(Control::FOCUS_NONE);
  5604. pause_button->set_tooltip(TTR("Pause the scene execution for debugging."));
  5605. pause_button->set_disabled(true);
  5606. play_hb->add_child(pause_button);
  5607. #ifdef OSX_ENABLED
  5608. pause_button->set_shortcut(ED_SHORTCUT("editor/pause_scene", TTR("Pause Scene"), KEY_MASK_CMD | KEY_MASK_CTRL | KEY_Y));
  5609. #else
  5610. pause_button->set_shortcut(ED_SHORTCUT("editor/pause_scene", TTR("Pause Scene"), KEY_F7));
  5611. #endif
  5612. stop_button = memnew(ToolButton);
  5613. play_hb->add_child(stop_button);
  5614. stop_button->set_focus_mode(Control::FOCUS_NONE);
  5615. stop_button->set_icon(gui_base->get_icon("Stop", "EditorIcons"));
  5616. stop_button->connect("pressed", this, "_menu_option", make_binds(RUN_STOP));
  5617. stop_button->set_tooltip(TTR("Stop the scene."));
  5618. stop_button->set_disabled(true);
  5619. #ifdef OSX_ENABLED
  5620. stop_button->set_shortcut(ED_SHORTCUT("editor/stop", TTR("Stop"), KEY_MASK_CMD | KEY_PERIOD));
  5621. #else
  5622. stop_button->set_shortcut(ED_SHORTCUT("editor/stop", TTR("Stop"), KEY_F8));
  5623. #endif
  5624. run_native = memnew(EditorRunNative);
  5625. play_hb->add_child(run_native);
  5626. run_native->connect("native_run", this, "_menu_option", varray(RUN_PLAY_NATIVE));
  5627. play_scene_button = memnew(ToolButton);
  5628. play_hb->add_child(play_scene_button);
  5629. play_scene_button->set_toggle_mode(true);
  5630. play_scene_button->set_focus_mode(Control::FOCUS_NONE);
  5631. play_scene_button->set_icon(gui_base->get_icon("PlayScene", "EditorIcons"));
  5632. play_scene_button->connect("pressed", this, "_menu_option", make_binds(RUN_PLAY_SCENE));
  5633. play_scene_button->set_tooltip(TTR("Play the edited scene."));
  5634. #ifdef OSX_ENABLED
  5635. play_scene_button->set_shortcut(ED_SHORTCUT("editor/play_scene", TTR("Play Scene"), KEY_MASK_CMD | KEY_R));
  5636. #else
  5637. play_scene_button->set_shortcut(ED_SHORTCUT("editor/play_scene", TTR("Play Scene"), KEY_F6));
  5638. #endif
  5639. play_custom_scene_button = memnew(ToolButton);
  5640. play_hb->add_child(play_custom_scene_button);
  5641. play_custom_scene_button->set_toggle_mode(true);
  5642. play_custom_scene_button->set_focus_mode(Control::FOCUS_NONE);
  5643. play_custom_scene_button->set_icon(gui_base->get_icon("PlayCustom", "EditorIcons"));
  5644. play_custom_scene_button->connect("pressed", this, "_menu_option", make_binds(RUN_PLAY_CUSTOM_SCENE));
  5645. play_custom_scene_button->set_tooltip(TTR("Play custom scene"));
  5646. #ifdef OSX_ENABLED
  5647. play_custom_scene_button->set_shortcut(ED_SHORTCUT("editor/play_custom_scene", TTR("Play Custom Scene"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_R));
  5648. #else
  5649. play_custom_scene_button->set_shortcut(ED_SHORTCUT("editor/play_custom_scene", TTR("Play Custom Scene"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F5));
  5650. #endif
  5651. HBoxContainer *right_menu_hb = memnew(HBoxContainer);
  5652. menu_hb->add_child(right_menu_hb);
  5653. // Toggle for video driver
  5654. video_driver = memnew(OptionButton);
  5655. video_driver->set_flat(true);
  5656. video_driver->set_focus_mode(Control::FOCUS_NONE);
  5657. video_driver->connect("item_selected", this, "_video_driver_selected");
  5658. video_driver->add_font_override("font", gui_base->get_font("bold", "EditorFonts"));
  5659. right_menu_hb->add_child(video_driver);
  5660. String video_drivers = ProjectSettings::get_singleton()->get_custom_property_info()["rendering/quality/driver/driver_name"].hint_string;
  5661. String current_video_driver = OS::get_singleton()->get_video_driver_name(OS::get_singleton()->get_current_video_driver());
  5662. video_driver_current = 0;
  5663. for (int i = 0; i < video_drivers.get_slice_count(","); i++) {
  5664. String driver = video_drivers.get_slice(",", i);
  5665. video_driver->add_item(driver);
  5666. video_driver->set_item_metadata(i, driver);
  5667. if (current_video_driver == driver) {
  5668. video_driver->select(i);
  5669. video_driver_current = i;
  5670. }
  5671. }
  5672. _update_video_driver_color();
  5673. video_restart_dialog = memnew(ConfirmationDialog);
  5674. video_restart_dialog->set_text(TTR("Changing the video driver requires restarting the editor."));
  5675. video_restart_dialog->get_ok()->set_text(TTR("Save & Restart"));
  5676. video_restart_dialog->connect("confirmed", this, "_menu_option", varray(SET_VIDEO_DRIVER_SAVE_AND_RESTART));
  5677. gui_base->add_child(video_restart_dialog);
  5678. progress_hb = memnew(BackgroundProgress);
  5679. layout_dialog = memnew(EditorLayoutsDialog);
  5680. gui_base->add_child(layout_dialog);
  5681. layout_dialog->set_hide_on_ok(false);
  5682. layout_dialog->set_size(Size2(225, 270) * EDSCALE);
  5683. layout_dialog->connect("name_confirmed", this, "_dialog_action");
  5684. update_spinner = memnew(MenuButton);
  5685. right_menu_hb->add_child(update_spinner);
  5686. update_spinner->set_icon(gui_base->get_icon("Progress1", "EditorIcons"));
  5687. update_spinner->get_popup()->connect("id_pressed", this, "_menu_option");
  5688. p = update_spinner->get_popup();
  5689. p->add_radio_check_item(TTR("Update Continuously"), SETTINGS_UPDATE_CONTINUOUSLY);
  5690. p->add_radio_check_item(TTR("Update All Changes"), SETTINGS_UPDATE_WHEN_CHANGED);
  5691. p->add_radio_check_item(TTR("Update Vital Changes"), SETTINGS_UPDATE_VITAL_ONLY);
  5692. p->add_separator();
  5693. p->add_item(TTR("Hide Update Spinner"), SETTINGS_UPDATE_SPINNER_HIDE);
  5694. _update_update_spinner();
  5695. // Instantiate and place editor docks
  5696. scene_tree_dock = memnew(SceneTreeDock(this, scene_root, editor_selection, editor_data));
  5697. inspector_dock = memnew(InspectorDock(this, editor_data));
  5698. import_dock = memnew(ImportDock);
  5699. node_dock = memnew(NodeDock);
  5700. filesystem_dock = memnew(FileSystemDock(this));
  5701. filesystem_dock->connect("inherit", this, "_inherit_request");
  5702. filesystem_dock->connect("instance", this, "_instance_request");
  5703. filesystem_dock->connect("display_mode_changed", this, "_save_docks");
  5704. // Scene: Top left
  5705. dock_slot[DOCK_SLOT_LEFT_UR]->add_child(scene_tree_dock);
  5706. dock_slot[DOCK_SLOT_LEFT_UR]->set_tab_title(scene_tree_dock->get_index(), TTR("Scene"));
  5707. // Import: Top left, behind Scene
  5708. dock_slot[DOCK_SLOT_LEFT_UR]->add_child(import_dock);
  5709. dock_slot[DOCK_SLOT_LEFT_UR]->set_tab_title(import_dock->get_index(), TTR("Import"));
  5710. // FileSystem: Bottom left
  5711. dock_slot[DOCK_SLOT_LEFT_BR]->add_child(filesystem_dock);
  5712. dock_slot[DOCK_SLOT_LEFT_BR]->set_tab_title(filesystem_dock->get_index(), TTR("FileSystem"));
  5713. // Inspector: Full height right
  5714. dock_slot[DOCK_SLOT_RIGHT_UL]->add_child(inspector_dock);
  5715. dock_slot[DOCK_SLOT_RIGHT_UL]->set_tab_title(inspector_dock->get_index(), TTR("Inspector"));
  5716. // Node: Full height right, behind Inspector
  5717. dock_slot[DOCK_SLOT_RIGHT_UL]->add_child(node_dock);
  5718. dock_slot[DOCK_SLOT_RIGHT_UL]->set_tab_title(node_dock->get_index(), TTR("Node"));
  5719. // Hide unused dock slots and vsplits
  5720. dock_slot[DOCK_SLOT_LEFT_UL]->hide();
  5721. dock_slot[DOCK_SLOT_LEFT_BL]->hide();
  5722. dock_slot[DOCK_SLOT_RIGHT_BL]->hide();
  5723. dock_slot[DOCK_SLOT_RIGHT_UR]->hide();
  5724. dock_slot[DOCK_SLOT_RIGHT_BR]->hide();
  5725. left_l_vsplit->hide();
  5726. right_r_vsplit->hide();
  5727. // Add some offsets to left_r and main hsplits to make LEFT_R and RIGHT_L docks wider than minsize
  5728. left_r_hsplit->set_split_offset(70 * EDSCALE);
  5729. main_hsplit->set_split_offset(-70 * EDSCALE);
  5730. // Define corresponding default layout
  5731. const String docks_section = "docks";
  5732. overridden_default_layout = -1;
  5733. default_layout.instance();
  5734. // Dock numbers are based on DockSlot enum value + 1
  5735. default_layout->set_value(docks_section, "dock_3", "Scene,Import");
  5736. default_layout->set_value(docks_section, "dock_4", "FileSystem");
  5737. default_layout->set_value(docks_section, "dock_5", "Inspector,Node");
  5738. for (int i = 0; i < vsplits.size(); i++) {
  5739. default_layout->set_value(docks_section, "dock_split_" + itos(i + 1), 0);
  5740. }
  5741. default_layout->set_value(docks_section, "dock_hsplit_1", 0);
  5742. default_layout->set_value(docks_section, "dock_hsplit_2", 70 * EDSCALE);
  5743. default_layout->set_value(docks_section, "dock_hsplit_3", -70 * EDSCALE);
  5744. default_layout->set_value(docks_section, "dock_hsplit_4", 0);
  5745. _update_layouts_menu();
  5746. // Bottom panels
  5747. bottom_panel = memnew(PanelContainer);
  5748. bottom_panel->add_style_override("panel", gui_base->get_stylebox("panel", "TabContainer"));
  5749. center_split->add_child(bottom_panel);
  5750. center_split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
  5751. bottom_panel_vb = memnew(VBoxContainer);
  5752. bottom_panel->add_child(bottom_panel_vb);
  5753. bottom_panel_hb = memnew(HBoxContainer);
  5754. bottom_panel_hb->set_custom_minimum_size(Size2(0, 24 * EDSCALE)); // Adjust for the height of the "Expand Bottom Dock" icon.
  5755. bottom_panel_vb->add_child(bottom_panel_hb);
  5756. bottom_panel_hb_editors = memnew(HBoxContainer);
  5757. bottom_panel_hb_editors->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5758. bottom_panel_hb->add_child(bottom_panel_hb_editors);
  5759. VBoxContainer *version_info_vbc = memnew(VBoxContainer);
  5760. bottom_panel_hb->add_child(version_info_vbc);
  5761. // Add a dummy control node for vertical spacing.
  5762. Control *v_spacer = memnew(Control);
  5763. version_info_vbc->add_child(v_spacer);
  5764. version_btn = memnew(LinkButton);
  5765. version_btn->set_text(VERSION_FULL_CONFIG);
  5766. String hash = String(VERSION_HASH);
  5767. if (hash.length() != 0) {
  5768. hash = " " + vformat("[%s]", hash.left(9));
  5769. }
  5770. // Set the text to copy in metadata as it slightly differs from the button's text.
  5771. version_btn->set_meta(META_TEXT_TO_COPY, "v" VERSION_FULL_BUILD + hash);
  5772. // Fade out the version label to be less prominent, but still readable
  5773. version_btn->set_self_modulate(Color(1, 1, 1, 0.65));
  5774. version_btn->set_underline_mode(LinkButton::UNDERLINE_MODE_ON_HOVER);
  5775. version_btn->set_tooltip(TTR("Click to copy."));
  5776. version_btn->connect("pressed", this, "_version_button_pressed");
  5777. version_info_vbc->add_child(version_btn);
  5778. // Add a dummy control node for horizontal spacing.
  5779. Control *h_spacer = memnew(Control);
  5780. bottom_panel_hb->add_child(h_spacer);
  5781. bottom_panel_raise = memnew(ToolButton);
  5782. bottom_panel_raise->set_icon(gui_base->get_icon("ExpandBottomDock", "EditorIcons"));
  5783. bottom_panel_raise->set_shortcut(ED_SHORTCUT("editor/bottom_panel_expand", TTR("Expand Bottom Panel"), KEY_MASK_SHIFT | KEY_F12));
  5784. bottom_panel_hb->add_child(bottom_panel_raise);
  5785. bottom_panel_raise->hide();
  5786. bottom_panel_raise->set_toggle_mode(true);
  5787. bottom_panel_raise->connect("toggled", this, "_bottom_panel_raise_toggled");
  5788. log = memnew(EditorLog);
  5789. ToolButton *output_button = add_bottom_panel_item(TTR("Output"), log);
  5790. log->set_tool_button(output_button);
  5791. old_split_ofs = 0;
  5792. center_split->connect("resized", this, "_vp_resized");
  5793. orphan_resources = memnew(OrphanResourcesDialog);
  5794. gui_base->add_child(orphan_resources);
  5795. confirmation = memnew(ConfirmationDialog);
  5796. gui_base->add_child(confirmation);
  5797. confirmation->connect("confirmed", this, "_menu_confirm_current");
  5798. save_confirmation = memnew(ConfirmationDialog);
  5799. save_confirmation->add_button(TTR("Don't Save"), OS::get_singleton()->get_swap_ok_cancel(), "discard");
  5800. gui_base->add_child(save_confirmation);
  5801. save_confirmation->connect("confirmed", this, "_menu_confirm_current");
  5802. save_confirmation->connect("custom_action", this, "_discard_changes");
  5803. custom_build_manage_templates = memnew(ConfirmationDialog);
  5804. custom_build_manage_templates->set_text(TTR("Android build template is missing, please install relevant templates."));
  5805. custom_build_manage_templates->get_ok()->set_text(TTR("Manage Templates"));
  5806. custom_build_manage_templates->add_button(TTR("Install from file"))->connect("pressed", this, "_menu_option", varray(SETTINGS_INSTALL_ANDROID_BUILD_TEMPLATE));
  5807. custom_build_manage_templates->connect("confirmed", this, "_menu_option", varray(SETTINGS_MANAGE_EXPORT_TEMPLATES));
  5808. gui_base->add_child(custom_build_manage_templates);
  5809. file_android_build_source = memnew(EditorFileDialog);
  5810. file_android_build_source->set_title(TTR("Select android sources file"));
  5811. file_android_build_source->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  5812. file_android_build_source->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  5813. file_android_build_source->add_filter("*.zip");
  5814. file_android_build_source->connect("file_selected", this, "_android_build_source_selected");
  5815. gui_base->add_child(file_android_build_source);
  5816. install_android_build_template = memnew(ConfirmationDialog);
  5817. install_android_build_template->set_text(TTR("This will set up your project for custom Android builds by installing the source template to \"res://android/build\".\nYou can then apply modifications and build your own custom APK on export (adding modules, changing the AndroidManifest.xml, etc.).\nNote that in order to make custom builds instead of using pre-built APKs, the \"Use Custom Build\" option should be enabled in the Android export preset."));
  5818. install_android_build_template->get_ok()->set_text(TTR("Install"));
  5819. install_android_build_template->connect("confirmed", this, "_menu_confirm_current");
  5820. gui_base->add_child(install_android_build_template);
  5821. remove_android_build_template = memnew(ConfirmationDialog);
  5822. remove_android_build_template->set_text(TTR("The Android build template is already installed in this project and it won't be overwritten.\nRemove the \"res://android/build\" directory manually before attempting this operation again."));
  5823. remove_android_build_template->get_ok()->set_text(TTR("Show in File Manager"));
  5824. remove_android_build_template->connect("confirmed", this, "_menu_option", varray(FILE_EXPLORE_ANDROID_BUILD_TEMPLATES));
  5825. gui_base->add_child(remove_android_build_template);
  5826. file_templates = memnew(EditorFileDialog);
  5827. file_templates->set_title(TTR("Import Templates From ZIP File"));
  5828. gui_base->add_child(file_templates);
  5829. file_templates->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  5830. file_templates->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  5831. file_templates->clear_filters();
  5832. file_templates->add_filter("*.tpz ; " + TTR("Template Package"));
  5833. file = memnew(EditorFileDialog);
  5834. gui_base->add_child(file);
  5835. file->set_current_dir("res://");
  5836. file_export_lib = memnew(EditorFileDialog);
  5837. file_export_lib->set_title(TTR("Export Library"));
  5838. file_export_lib->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  5839. file_export_lib->connect("file_selected", this, "_dialog_action");
  5840. file_export_lib_merge = memnew(CheckBox);
  5841. file_export_lib_merge->set_text(TTR("Merge With Existing"));
  5842. file_export_lib_merge->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  5843. file_export_lib_merge->set_pressed(true);
  5844. file_export_lib->get_vbox()->add_child(file_export_lib_merge);
  5845. file_export_lib_apply_xforms = memnew(CheckBox);
  5846. file_export_lib_apply_xforms->set_text(TTR("Apply MeshInstance Transforms"));
  5847. file_export_lib_apply_xforms->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  5848. file_export_lib_apply_xforms->set_pressed(false);
  5849. file_export_lib->get_vbox()->add_child(file_export_lib_apply_xforms);
  5850. gui_base->add_child(file_export_lib);
  5851. file_script = memnew(EditorFileDialog);
  5852. file_script->set_title(TTR("Open & Run a Script"));
  5853. file_script->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  5854. file_script->set_mode(EditorFileDialog::MODE_OPEN_FILE);
  5855. List<String> sexts;
  5856. ResourceLoader::get_recognized_extensions_for_type("Script", &sexts);
  5857. for (List<String>::Element *E = sexts.front(); E; E = E->next()) {
  5858. file_script->add_filter("*." + E->get());
  5859. }
  5860. gui_base->add_child(file_script);
  5861. file_script->connect("file_selected", this, "_dialog_action");
  5862. file_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  5863. file_menu->connect("about_to_show", this, "_update_file_menu_opened");
  5864. file_menu->get_popup()->connect("popup_hide", this, "_update_file_menu_closed");
  5865. settings_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  5866. file->connect("file_selected", this, "_dialog_action");
  5867. file_templates->connect("file_selected", this, "_dialog_action");
  5868. preview_gen = memnew(AudioStreamPreviewGenerator);
  5869. add_child(preview_gen);
  5870. //plugin stuff
  5871. file_server = memnew(EditorFileServer);
  5872. disk_changed = memnew(ConfirmationDialog);
  5873. {
  5874. VBoxContainer *vbc = memnew(VBoxContainer);
  5875. disk_changed->add_child(vbc);
  5876. Label *dl = memnew(Label);
  5877. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?"));
  5878. vbc->add_child(dl);
  5879. disk_changed_list = memnew(Tree);
  5880. vbc->add_child(disk_changed_list);
  5881. disk_changed_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5882. disk_changed->connect("confirmed", this, "_reload_modified_scenes");
  5883. disk_changed->connect("confirmed", this, "_reload_project_settings");
  5884. disk_changed->get_ok()->set_text(TTR("Reload"));
  5885. disk_changed->add_button(TTR("Resave"), !OS::get_singleton()->get_swap_ok_cancel(), "resave");
  5886. disk_changed->connect("custom_action", this, "_resave_scenes");
  5887. }
  5888. gui_base->add_child(disk_changed);
  5889. add_editor_plugin(memnew(AnimationPlayerEditorPlugin(this)));
  5890. add_editor_plugin(memnew(CanvasItemEditorPlugin(this)));
  5891. add_editor_plugin(memnew(SpatialEditorPlugin(this)));
  5892. add_editor_plugin(memnew(ScriptEditorPlugin(this)));
  5893. EditorAudioBuses *audio_bus_editor = EditorAudioBuses::register_editor();
  5894. ScriptTextEditor::register_editor(); //register one for text scripts
  5895. TextEditor::register_editor();
  5896. if (AssetLibraryEditorPlugin::is_available()) {
  5897. add_editor_plugin(memnew(AssetLibraryEditorPlugin(this)));
  5898. } else {
  5899. print_verbose("Asset Library not available (due to using Web editor, or SSL support disabled).");
  5900. }
  5901. //add interface before adding plugins
  5902. editor_interface = memnew(EditorInterface);
  5903. add_child(editor_interface);
  5904. //more visually meaningful to have this later
  5905. raise_bottom_panel_item(AnimationPlayerEditor::singleton);
  5906. add_editor_plugin(VersionControlEditorPlugin::get_singleton());
  5907. add_editor_plugin(memnew(ShaderEditorPlugin(this)));
  5908. add_editor_plugin(memnew(VisualShaderEditorPlugin(this)));
  5909. add_editor_plugin(memnew(CameraEditorPlugin(this)));
  5910. add_editor_plugin(memnew(ThemeEditorPlugin(this)));
  5911. add_editor_plugin(memnew(MultiMeshEditorPlugin(this)));
  5912. add_editor_plugin(memnew(MeshInstanceEditorPlugin(this)));
  5913. add_editor_plugin(memnew(AnimationTreeEditorPlugin(this)));
  5914. add_editor_plugin(memnew(AnimationTreePlayerEditorPlugin(this)));
  5915. add_editor_plugin(memnew(MeshLibraryEditorPlugin(this)));
  5916. add_editor_plugin(memnew(StyleBoxEditorPlugin(this)));
  5917. add_editor_plugin(memnew(SpriteEditorPlugin(this)));
  5918. add_editor_plugin(memnew(Skeleton2DEditorPlugin(this)));
  5919. add_editor_plugin(memnew(ParticlesEditorPlugin(this)));
  5920. add_editor_plugin(memnew(CPUParticles2DEditorPlugin(this)));
  5921. add_editor_plugin(memnew(CPUParticlesEditorPlugin(this)));
  5922. add_editor_plugin(memnew(ResourcePreloaderEditorPlugin(this)));
  5923. add_editor_plugin(memnew(ItemListEditorPlugin(this)));
  5924. add_editor_plugin(memnew(Polygon3DEditorPlugin(this)));
  5925. add_editor_plugin(memnew(CollisionPolygon2DEditorPlugin(this)));
  5926. add_editor_plugin(memnew(TileSetEditorPlugin(this)));
  5927. add_editor_plugin(memnew(TileMapEditorPlugin(this)));
  5928. add_editor_plugin(memnew(SpriteFramesEditorPlugin(this)));
  5929. add_editor_plugin(memnew(TextureRegionEditorPlugin(this)));
  5930. add_editor_plugin(memnew(Particles2DEditorPlugin(this)));
  5931. add_editor_plugin(memnew(GIProbeEditorPlugin(this)));
  5932. add_editor_plugin(memnew(BakedLightmapEditorPlugin(this)));
  5933. add_editor_plugin(memnew(RoomManagerEditorPlugin(this)));
  5934. add_editor_plugin(memnew(RoomEditorPlugin(this)));
  5935. add_editor_plugin(memnew(OccluderEditorPlugin(this)));
  5936. add_editor_plugin(memnew(PortalEditorPlugin(this)));
  5937. add_editor_plugin(memnew(PackedSceneEditorPlugin(this)));
  5938. add_editor_plugin(memnew(MergeGroupEditorPlugin(this)));
  5939. add_editor_plugin(memnew(Path2DEditorPlugin(this)));
  5940. add_editor_plugin(memnew(PathEditorPlugin(this)));
  5941. add_editor_plugin(memnew(Line2DEditorPlugin(this)));
  5942. add_editor_plugin(memnew(Polygon2DEditorPlugin(this)));
  5943. add_editor_plugin(memnew(LightOccluder2DEditorPlugin(this)));
  5944. add_editor_plugin(memnew(NavigationPolygonEditorPlugin(this)));
  5945. add_editor_plugin(memnew(GradientEditorPlugin(this)));
  5946. add_editor_plugin(memnew(CollisionShape2DEditorPlugin(this)));
  5947. add_editor_plugin(memnew(CurveEditorPlugin(this)));
  5948. add_editor_plugin(memnew(TextureEditorPlugin(this)));
  5949. add_editor_plugin(memnew(AudioStreamEditorPlugin(this)));
  5950. add_editor_plugin(memnew(AudioBusesEditorPlugin(audio_bus_editor)));
  5951. add_editor_plugin(memnew(SkeletonEditorPlugin(this)));
  5952. add_editor_plugin(memnew(SkeletonIKEditorPlugin(this)));
  5953. add_editor_plugin(memnew(PhysicalBonePlugin(this)));
  5954. add_editor_plugin(memnew(MeshEditorPlugin(this)));
  5955. add_editor_plugin(memnew(MaterialEditorPlugin(this)));
  5956. add_editor_plugin(memnew(ViewportPreviewEditorPlugin(this)));
  5957. add_editor_plugin(memnew(GradientTexture2DEditorPlugin(this)));
  5958. add_editor_plugin(memnew(RayCast2DEditorPlugin(this)));
  5959. add_editor_plugin(memnew(BitMapEditorPlugin(this)));
  5960. for (int i = 0; i < EditorPlugins::get_plugin_count(); i++) {
  5961. add_editor_plugin(EditorPlugins::create(i, this));
  5962. }
  5963. for (int i = 0; i < plugin_init_callback_count; i++) {
  5964. plugin_init_callbacks[i]();
  5965. }
  5966. resource_preview->add_preview_generator(Ref<EditorTexturePreviewPlugin>(memnew(EditorTexturePreviewPlugin)));
  5967. resource_preview->add_preview_generator(Ref<EditorImagePreviewPlugin>(memnew(EditorImagePreviewPlugin)));
  5968. resource_preview->add_preview_generator(Ref<EditorPackedScenePreviewPlugin>(memnew(EditorPackedScenePreviewPlugin)));
  5969. resource_preview->add_preview_generator(Ref<EditorMaterialPreviewPlugin>(memnew(EditorMaterialPreviewPlugin)));
  5970. resource_preview->add_preview_generator(Ref<EditorScriptPreviewPlugin>(memnew(EditorScriptPreviewPlugin)));
  5971. resource_preview->add_preview_generator(Ref<EditorAudioStreamPreviewPlugin>(memnew(EditorAudioStreamPreviewPlugin)));
  5972. resource_preview->add_preview_generator(Ref<EditorMeshPreviewPlugin>(memnew(EditorMeshPreviewPlugin)));
  5973. resource_preview->add_preview_generator(Ref<EditorBitmapPreviewPlugin>(memnew(EditorBitmapPreviewPlugin)));
  5974. resource_preview->add_preview_generator(Ref<EditorFontPreviewPlugin>(memnew(EditorFontPreviewPlugin)));
  5975. resource_preview->add_preview_generator(Ref<EditorGradientPreviewPlugin>(memnew(EditorGradientPreviewPlugin)));
  5976. {
  5977. Ref<SpatialMaterialConversionPlugin> spatial_mat_convert;
  5978. spatial_mat_convert.instance();
  5979. resource_conversion_plugins.push_back(spatial_mat_convert);
  5980. Ref<CanvasItemMaterialConversionPlugin> canvas_item_mat_convert;
  5981. canvas_item_mat_convert.instance();
  5982. resource_conversion_plugins.push_back(canvas_item_mat_convert);
  5983. Ref<ParticlesMaterialConversionPlugin> particles_mat_convert;
  5984. particles_mat_convert.instance();
  5985. resource_conversion_plugins.push_back(particles_mat_convert);
  5986. Ref<VisualShaderConversionPlugin> vshader_convert;
  5987. vshader_convert.instance();
  5988. resource_conversion_plugins.push_back(vshader_convert);
  5989. }
  5990. update_spinner_step_msec = OS::get_singleton()->get_ticks_msec();
  5991. update_spinner_step_frame = Engine::get_singleton()->get_frames_drawn();
  5992. update_spinner_step = 0;
  5993. editor_plugin_screen = nullptr;
  5994. editor_plugins_over = memnew(EditorPluginList);
  5995. editor_plugins_force_over = memnew(EditorPluginList);
  5996. editor_plugins_force_input_forwarding = memnew(EditorPluginList);
  5997. Ref<EditorExportTextSceneToBinaryPlugin> export_text_to_binary_plugin;
  5998. export_text_to_binary_plugin.instance();
  5999. EditorExport::get_singleton()->add_export_plugin(export_text_to_binary_plugin);
  6000. _edit_current();
  6001. current = nullptr;
  6002. saving_resource = Ref<Resource>();
  6003. reference_resource_mem = true;
  6004. save_external_resources_mem = true;
  6005. set_process(true);
  6006. open_imported = memnew(ConfirmationDialog);
  6007. open_imported->get_ok()->set_text(TTR("Open Anyway"));
  6008. new_inherited_button = open_imported->add_button(TTR("New Inherited"), !OS::get_singleton()->get_swap_ok_cancel(), "inherit");
  6009. open_imported->connect("confirmed", this, "_open_imported");
  6010. open_imported->connect("custom_action", this, "_inherit_imported");
  6011. gui_base->add_child(open_imported);
  6012. saved_version = 1;
  6013. unsaved_cache = true;
  6014. _last_instanced_scene = nullptr;
  6015. quick_open = memnew(EditorQuickOpen);
  6016. gui_base->add_child(quick_open);
  6017. quick_open->connect("quick_open", this, "_quick_opened");
  6018. quick_run = memnew(EditorQuickOpen);
  6019. gui_base->add_child(quick_run);
  6020. quick_run->connect("quick_open", this, "_quick_run");
  6021. _update_recent_scenes();
  6022. editor_data.restore_editor_global_states();
  6023. convert_old = false;
  6024. opening_prev = false;
  6025. set_process_unhandled_input(true);
  6026. _playing_edited = false;
  6027. load_errors = memnew(RichTextLabel);
  6028. load_error_dialog = memnew(AcceptDialog);
  6029. load_error_dialog->add_child(load_errors);
  6030. load_error_dialog->set_title(TTR("Load Errors"));
  6031. gui_base->add_child(load_error_dialog);
  6032. execute_outputs = memnew(RichTextLabel);
  6033. execute_outputs->set_selection_enabled(true);
  6034. execute_output_dialog = memnew(AcceptDialog);
  6035. execute_output_dialog->add_child(execute_outputs);
  6036. execute_output_dialog->set_title("");
  6037. // Prevent closing too fast and missing important information.
  6038. execute_output_dialog->set_exclusive(true);
  6039. gui_base->add_child(execute_output_dialog);
  6040. EditorFileSystem::get_singleton()->connect("sources_changed", this, "_sources_changed");
  6041. EditorFileSystem::get_singleton()->connect("filesystem_changed", this, "_fs_changed");
  6042. EditorFileSystem::get_singleton()->connect("resources_reimported", this, "_resources_reimported");
  6043. EditorFileSystem::get_singleton()->connect("resources_reload", this, "_resources_changed");
  6044. _build_icon_type_cache();
  6045. Node::set_human_readable_collision_renaming(true);
  6046. pick_main_scene = memnew(ConfirmationDialog);
  6047. gui_base->add_child(pick_main_scene);
  6048. pick_main_scene->get_ok()->set_text(TTR("Select"));
  6049. pick_main_scene->connect("confirmed", this, "_menu_option", varray(SETTINGS_PICK_MAIN_SCENE));
  6050. select_current_scene_button = pick_main_scene->add_button(TTR("Select Current"), true, "select_current");
  6051. pick_main_scene->connect("custom_action", this, "_pick_main_scene_custom_action");
  6052. for (int i = 0; i < _init_callbacks.size(); i++) {
  6053. _init_callbacks[i]();
  6054. }
  6055. editor_data.add_edited_scene(-1);
  6056. editor_data.set_edited_scene(0);
  6057. _update_scene_tabs();
  6058. import_dock->initialize_import_options();
  6059. FileAccess::set_file_close_fail_notify_callback(_file_access_close_error_notify);
  6060. waiting_for_first_scan = true;
  6061. print_handler.printfunc = _print_handler;
  6062. print_handler.userdata = this;
  6063. add_print_handler(&print_handler);
  6064. ResourceSaver::set_save_callback(_resource_saved);
  6065. ResourceLoader::set_load_callback(_resource_loaded);
  6066. #ifdef OSX_ENABLED
  6067. ED_SHORTCUT("editor/editor_2d", TTR("Open 2D Editor"), KEY_MASK_ALT | KEY_1);
  6068. ED_SHORTCUT("editor/editor_3d", TTR("Open 3D Editor"), KEY_MASK_ALT | KEY_2);
  6069. ED_SHORTCUT("editor/editor_script", TTR("Open Script Editor"), KEY_MASK_ALT | KEY_3);
  6070. ED_SHORTCUT("editor/editor_assetlib", TTR("Open Asset Library"), KEY_MASK_ALT | KEY_4);
  6071. #else
  6072. // Use the Ctrl modifier so F2 can be used to rename nodes in the scene tree dock.
  6073. ED_SHORTCUT("editor/editor_2d", TTR("Open 2D Editor"), KEY_MASK_CTRL | KEY_F1);
  6074. ED_SHORTCUT("editor/editor_3d", TTR("Open 3D Editor"), KEY_MASK_CTRL | KEY_F2);
  6075. ED_SHORTCUT("editor/editor_script", TTR("Open Script Editor"), KEY_MASK_CTRL | KEY_F3);
  6076. ED_SHORTCUT("editor/editor_assetlib", TTR("Open Asset Library"), KEY_MASK_CTRL | KEY_F4);
  6077. #endif
  6078. ED_SHORTCUT("editor/editor_next", TTR("Open the next Editor"));
  6079. ED_SHORTCUT("editor/editor_prev", TTR("Open the previous Editor"));
  6080. screenshot_timer = memnew(Timer);
  6081. screenshot_timer->set_one_shot(true);
  6082. screenshot_timer->set_wait_time(settings_menu->get_popup()->get_submenu_popup_delay() + 0.1f);
  6083. screenshot_timer->connect("timeout", this, "_request_screenshot");
  6084. add_child(screenshot_timer);
  6085. screenshot_timer->set_owner(get_owner());
  6086. String exec = OS::get_singleton()->get_executable_path();
  6087. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "executable_path", exec); // Save editor executable path for third-party tools
  6088. OS::get_singleton()->benchmark_end_measure("editor");
  6089. }
  6090. EditorNode::~EditorNode() {
  6091. EditorInspector::cleanup_plugins();
  6092. remove_print_handler(&print_handler);
  6093. memdelete(EditorHelp::get_doc_data());
  6094. memdelete(editor_selection);
  6095. memdelete(editor_plugins_over);
  6096. memdelete(editor_plugins_force_over);
  6097. memdelete(editor_plugins_force_input_forwarding);
  6098. memdelete(file_server);
  6099. memdelete(progress_hb);
  6100. EditorSettings::destroy();
  6101. }
  6102. /*
  6103. * EDITOR PLUGIN LIST
  6104. */
  6105. void EditorPluginList::make_visible(bool p_visible) {
  6106. for (int i = 0; i < plugins_list.size(); i++) {
  6107. plugins_list[i]->make_visible(p_visible);
  6108. }
  6109. }
  6110. void EditorPluginList::edit(Object *p_object) {
  6111. for (int i = 0; i < plugins_list.size(); i++) {
  6112. plugins_list[i]->edit(p_object);
  6113. }
  6114. }
  6115. bool EditorPluginList::forward_gui_input(const Ref<InputEvent> &p_event) {
  6116. bool discard = false;
  6117. for (int i = 0; i < plugins_list.size(); i++) {
  6118. if (plugins_list[i]->forward_canvas_gui_input(p_event)) {
  6119. discard = true;
  6120. }
  6121. }
  6122. return discard;
  6123. }
  6124. bool EditorPluginList::forward_spatial_gui_input(Camera *p_camera, const Ref<InputEvent> &p_event, bool serve_when_force_input_enabled) {
  6125. bool discard = false;
  6126. for (int i = 0; i < plugins_list.size(); i++) {
  6127. if ((!serve_when_force_input_enabled) && plugins_list[i]->is_input_event_forwarding_always_enabled()) {
  6128. continue;
  6129. }
  6130. if (plugins_list[i]->forward_spatial_gui_input(p_camera, p_event)) {
  6131. discard = true;
  6132. }
  6133. }
  6134. return discard;
  6135. }
  6136. void EditorPluginList::forward_canvas_draw_over_viewport(Control *p_overlay) {
  6137. for (int i = 0; i < plugins_list.size(); i++) {
  6138. plugins_list[i]->forward_canvas_draw_over_viewport(p_overlay);
  6139. }
  6140. }
  6141. void EditorPluginList::forward_canvas_force_draw_over_viewport(Control *p_overlay) {
  6142. for (int i = 0; i < plugins_list.size(); i++) {
  6143. plugins_list[i]->forward_canvas_force_draw_over_viewport(p_overlay);
  6144. }
  6145. }
  6146. void EditorPluginList::forward_spatial_draw_over_viewport(Control *p_overlay) {
  6147. for (int i = 0; i < plugins_list.size(); i++) {
  6148. plugins_list[i]->forward_spatial_draw_over_viewport(p_overlay);
  6149. }
  6150. }
  6151. void EditorPluginList::forward_spatial_force_draw_over_viewport(Control *p_overlay) {
  6152. for (int i = 0; i < plugins_list.size(); i++) {
  6153. plugins_list[i]->forward_spatial_force_draw_over_viewport(p_overlay);
  6154. }
  6155. }
  6156. void EditorPluginList::add_plugin(EditorPlugin *p_plugin) {
  6157. plugins_list.push_back(p_plugin);
  6158. }
  6159. void EditorPluginList::remove_plugin(EditorPlugin *p_plugin) {
  6160. plugins_list.erase(p_plugin);
  6161. }
  6162. bool EditorPluginList::empty() {
  6163. return plugins_list.empty();
  6164. }
  6165. void EditorPluginList::clear() {
  6166. plugins_list.clear();
  6167. }
  6168. EditorPluginList::EditorPluginList() {
  6169. }
  6170. EditorPluginList::~EditorPluginList() {
  6171. }