editor_node.cpp 282 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606
  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/config/project_settings.h"
  32. #include "core/extension/gdextension_manager.h"
  33. #include "core/input/input.h"
  34. #include "core/io/config_file.h"
  35. #include "core/io/file_access.h"
  36. #include "core/io/resource_loader.h"
  37. #include "core/io/resource_saver.h"
  38. #include "core/object/class_db.h"
  39. #include "core/os/keyboard.h"
  40. #include "core/os/os.h"
  41. #include "core/os/time.h"
  42. #include "core/string/print_string.h"
  43. #include "core/string/translation.h"
  44. #include "core/version.h"
  45. #include "editor/editor_string_names.h"
  46. #include "main/main.h"
  47. #include "scene/3d/bone_attachment_3d.h"
  48. #include "scene/animation/animation_tree.h"
  49. #include "scene/gui/color_picker.h"
  50. #include "scene/gui/dialogs.h"
  51. #include "scene/gui/file_dialog.h"
  52. #include "scene/gui/link_button.h"
  53. #include "scene/gui/menu_bar.h"
  54. #include "scene/gui/menu_button.h"
  55. #include "scene/gui/panel.h"
  56. #include "scene/gui/panel_container.h"
  57. #include "scene/gui/popup.h"
  58. #include "scene/gui/rich_text_label.h"
  59. #include "scene/gui/split_container.h"
  60. #include "scene/gui/tab_container.h"
  61. #include "scene/main/window.h"
  62. #include "scene/property_utils.h"
  63. #include "scene/resources/image_texture.h"
  64. #include "scene/resources/packed_scene.h"
  65. #include "scene/resources/portable_compressed_texture.h"
  66. #include "scene/theme/theme_db.h"
  67. #include "servers/display_server.h"
  68. #include "servers/navigation_server_3d.h"
  69. #include "servers/physics_server_2d.h"
  70. #include "servers/rendering_server.h"
  71. #include "editor/audio_stream_preview.h"
  72. #include "editor/debugger/editor_debugger_node.h"
  73. #include "editor/debugger/script_editor_debugger.h"
  74. #include "editor/dependency_editor.h"
  75. #include "editor/editor_about.h"
  76. #include "editor/editor_audio_buses.h"
  77. #include "editor/editor_build_profile.h"
  78. #include "editor/editor_command_palette.h"
  79. #include "editor/editor_data.h"
  80. #include "editor/editor_dock_manager.h"
  81. #include "editor/editor_feature_profile.h"
  82. #include "editor/editor_folding.h"
  83. #include "editor/editor_help.h"
  84. #include "editor/editor_inspector.h"
  85. #include "editor/editor_interface.h"
  86. #include "editor/editor_layouts_dialog.h"
  87. #include "editor/editor_log.h"
  88. #include "editor/editor_native_shader_source_visualizer.h"
  89. #include "editor/editor_paths.h"
  90. #include "editor/editor_properties.h"
  91. #include "editor/editor_property_name_processor.h"
  92. #include "editor/editor_quick_open.h"
  93. #include "editor/editor_resource_preview.h"
  94. #include "editor/editor_run.h"
  95. #include "editor/editor_run_native.h"
  96. #include "editor/editor_settings.h"
  97. #include "editor/editor_settings_dialog.h"
  98. #include "editor/editor_translation_parser.h"
  99. #include "editor/editor_undo_redo_manager.h"
  100. #include "editor/export/editor_export.h"
  101. #include "editor/export/export_template_manager.h"
  102. #include "editor/export/project_export.h"
  103. #include "editor/fbx_importer_manager.h"
  104. #include "editor/filesystem_dock.h"
  105. #include "editor/gui/editor_bottom_panel.h"
  106. #include "editor/gui/editor_file_dialog.h"
  107. #include "editor/gui/editor_run_bar.h"
  108. #include "editor/gui/editor_scene_tabs.h"
  109. #include "editor/gui/editor_title_bar.h"
  110. #include "editor/gui/editor_toaster.h"
  111. #include "editor/history_dock.h"
  112. #include "editor/import/3d/editor_import_collada.h"
  113. #include "editor/import/3d/resource_importer_obj.h"
  114. #include "editor/import/3d/resource_importer_scene.h"
  115. #include "editor/import/3d/scene_import_settings.h"
  116. #include "editor/import/audio_stream_import_settings.h"
  117. #include "editor/import/dynamic_font_import_settings.h"
  118. #include "editor/import/resource_importer_bitmask.h"
  119. #include "editor/import/resource_importer_bmfont.h"
  120. #include "editor/import/resource_importer_csv_translation.h"
  121. #include "editor/import/resource_importer_dynamic_font.h"
  122. #include "editor/import/resource_importer_image.h"
  123. #include "editor/import/resource_importer_imagefont.h"
  124. #include "editor/import/resource_importer_layered_texture.h"
  125. #include "editor/import/resource_importer_shader_file.h"
  126. #include "editor/import/resource_importer_texture.h"
  127. #include "editor/import/resource_importer_texture_atlas.h"
  128. #include "editor/import/resource_importer_wav.h"
  129. #include "editor/import_dock.h"
  130. #include "editor/inspector_dock.h"
  131. #include "editor/multi_node_edit.h"
  132. #include "editor/node_dock.h"
  133. #include "editor/plugins/animation_player_editor_plugin.h"
  134. #include "editor/plugins/asset_library_editor_plugin.h"
  135. #include "editor/plugins/canvas_item_editor_plugin.h"
  136. #include "editor/plugins/debugger_editor_plugin.h"
  137. #include "editor/plugins/dedicated_server_export_plugin.h"
  138. #include "editor/plugins/editor_plugin.h"
  139. #include "editor/plugins/editor_preview_plugins.h"
  140. #include "editor/plugins/editor_resource_conversion_plugin.h"
  141. #include "editor/plugins/gdextension_export_plugin.h"
  142. #include "editor/plugins/material_editor_plugin.h"
  143. #include "editor/plugins/mesh_library_editor_plugin.h"
  144. #include "editor/plugins/node_3d_editor_plugin.h"
  145. #include "editor/plugins/packed_scene_translation_parser_plugin.h"
  146. #include "editor/plugins/particle_process_material_editor_plugin.h"
  147. #include "editor/plugins/plugin_config_dialog.h"
  148. #include "editor/plugins/root_motion_editor_plugin.h"
  149. #include "editor/plugins/script_text_editor.h"
  150. #include "editor/plugins/text_editor.h"
  151. #include "editor/plugins/version_control_editor_plugin.h"
  152. #include "editor/plugins/visual_shader_editor_plugin.h"
  153. #include "editor/progress_dialog.h"
  154. #include "editor/project_settings_editor.h"
  155. #include "editor/register_exporters.h"
  156. #include "editor/scene_tree_dock.h"
  157. #include "editor/surface_upgrade_tool.h"
  158. #include "editor/themes/editor_scale.h"
  159. #include "editor/themes/editor_theme_manager.h"
  160. #include "editor/window_wrapper.h"
  161. #include <stdio.h>
  162. #include <stdlib.h>
  163. #include "modules/modules_enabled.gen.h" // For gdscript, mono.
  164. EditorNode *EditorNode::singleton = nullptr;
  165. static const String EDITOR_NODE_CONFIG_SECTION = "EditorNode";
  166. static const String REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE = "The Android build template is already installed in this project and it won't be overwritten.\nRemove the \"%s\" directory manually before attempting this operation again.";
  167. static const String INSTALL_ANDROID_BUILD_TEMPLATE_MESSAGE = "This will set up your project for gradle Android builds by installing the source template to \"%s\".\nNote that in order to make gradle builds instead of using pre-built APKs, the \"Use Gradle Build\" option should be enabled in the Android export preset.";
  168. void EditorNode::disambiguate_filenames(const Vector<String> p_full_paths, Vector<String> &r_filenames) {
  169. ERR_FAIL_COND_MSG(p_full_paths.size() != r_filenames.size(), vformat("disambiguate_filenames requires two string vectors of same length (%d != %d).", p_full_paths.size(), r_filenames.size()));
  170. // Keep track of a list of "index sets," i.e. sets of indices
  171. // within disambiguated_scene_names which contain the same name.
  172. Vector<RBSet<int>> index_sets;
  173. HashMap<String, int> scene_name_to_set_index;
  174. for (int i = 0; i < r_filenames.size(); i++) {
  175. const String &scene_name = r_filenames[i];
  176. if (!scene_name_to_set_index.has(scene_name)) {
  177. index_sets.append(RBSet<int>());
  178. scene_name_to_set_index.insert(r_filenames[i], index_sets.size() - 1);
  179. }
  180. index_sets.write[scene_name_to_set_index[scene_name]].insert(i);
  181. }
  182. // For each index set with a size > 1, we need to disambiguate.
  183. for (int i = 0; i < index_sets.size(); i++) {
  184. RBSet<int> iset = index_sets[i];
  185. while (iset.size() > 1) {
  186. // Append the parent folder to each scene name.
  187. for (const int &E : iset) {
  188. int set_idx = E;
  189. String scene_name = r_filenames[set_idx];
  190. String full_path = p_full_paths[set_idx];
  191. // Get rid of file extensions and res:// prefixes.
  192. scene_name = scene_name.get_basename();
  193. if (full_path.begins_with("res://")) {
  194. full_path = full_path.substr(6);
  195. }
  196. full_path = full_path.get_basename();
  197. // Normalize trailing slashes when normalizing directory names.
  198. scene_name = scene_name.trim_suffix("/");
  199. full_path = full_path.trim_suffix("/");
  200. int scene_name_size = scene_name.size();
  201. int full_path_size = full_path.size();
  202. int difference = full_path_size - scene_name_size;
  203. // Find just the parent folder of the current path and append it.
  204. // If the current name is foo.tscn, and the full path is /some/folder/foo.tscn
  205. // then slash_idx is the second '/', so that we select just "folder", and
  206. // append that to yield "folder/foo.tscn".
  207. if (difference > 0) {
  208. String parent = full_path.substr(0, difference);
  209. int slash_idx = parent.rfind("/");
  210. slash_idx = parent.rfind("/", slash_idx - 1);
  211. parent = (slash_idx >= 0 && parent.length() > 1) ? parent.substr(slash_idx + 1) : parent;
  212. r_filenames.write[set_idx] = parent + r_filenames[set_idx];
  213. }
  214. }
  215. // Loop back through scene names and remove non-ambiguous names.
  216. bool can_proceed = false;
  217. RBSet<int>::Element *E = iset.front();
  218. while (E) {
  219. String scene_name = r_filenames[E->get()];
  220. bool duplicate_found = false;
  221. for (const int &F : iset) {
  222. if (E->get() == F) {
  223. continue;
  224. }
  225. const String &other_scene_name = r_filenames[F];
  226. if (other_scene_name == scene_name) {
  227. duplicate_found = true;
  228. break;
  229. }
  230. }
  231. RBSet<int>::Element *to_erase = duplicate_found ? nullptr : E;
  232. // We need to check that we could actually append anymore names
  233. // if we wanted to for disambiguation. If we can't, then we have
  234. // to abort even with ambiguous names. We clean the full path
  235. // and the scene name first to remove extensions so that this
  236. // comparison actually works.
  237. String path = p_full_paths[E->get()];
  238. // Get rid of file extensions and res:// prefixes.
  239. scene_name = scene_name.get_basename();
  240. if (path.begins_with("res://")) {
  241. path = path.substr(6);
  242. }
  243. path = path.get_basename();
  244. // Normalize trailing slashes when normalizing directory names.
  245. scene_name = scene_name.trim_suffix("/");
  246. path = path.trim_suffix("/");
  247. // We can proceed if the full path is longer than the scene name,
  248. // meaning that there is at least one more parent folder we can
  249. // tack onto the name.
  250. can_proceed = can_proceed || (path.size() - scene_name.size()) >= 1;
  251. E = E->next();
  252. if (to_erase) {
  253. iset.erase(to_erase);
  254. }
  255. }
  256. if (!can_proceed) {
  257. break;
  258. }
  259. }
  260. }
  261. }
  262. void EditorNode::_version_control_menu_option(int p_idx) {
  263. switch (vcs_actions_menu->get_item_id(p_idx)) {
  264. case RUN_VCS_METADATA: {
  265. VersionControlEditorPlugin::get_singleton()->popup_vcs_metadata_dialog();
  266. } break;
  267. case RUN_VCS_SETTINGS: {
  268. VersionControlEditorPlugin::get_singleton()->popup_vcs_set_up_dialog(gui_base);
  269. } break;
  270. }
  271. }
  272. void EditorNode::_update_title() {
  273. const String appname = GLOBAL_GET("application/config/name");
  274. String title = (appname.is_empty() ? TTR("Unnamed Project") : appname);
  275. const String edited = editor_data.get_edited_scene_root() ? editor_data.get_edited_scene_root()->get_scene_file_path() : String();
  276. if (!edited.is_empty()) {
  277. // Display the edited scene name before the program name so that it can be seen in the OS task bar.
  278. title = vformat("%s - %s", edited.get_file(), title);
  279. }
  280. if (unsaved_cache) {
  281. // Display the "modified" mark before anything else so that it can always be seen in the OS task bar.
  282. title = vformat("(*) %s", title);
  283. }
  284. DisplayServer::get_singleton()->window_set_title(title + String(" - ") + VERSION_NAME);
  285. if (project_title) {
  286. project_title->set_text(title);
  287. }
  288. }
  289. void EditorNode::shortcut_input(const Ref<InputEvent> &p_event) {
  290. ERR_FAIL_COND(p_event.is_null());
  291. Ref<InputEventKey> k = p_event;
  292. if ((k.is_valid() && k->is_pressed() && !k->is_echo()) || Object::cast_to<InputEventShortcut>(*p_event)) {
  293. EditorPlugin *old_editor = editor_plugin_screen;
  294. if (ED_IS_SHORTCUT("editor/filter_files", p_event)) {
  295. FileSystemDock::get_singleton()->focus_on_filter();
  296. get_tree()->get_root()->set_input_as_handled();
  297. }
  298. if (ED_IS_SHORTCUT("editor/editor_2d", p_event)) {
  299. editor_select(EDITOR_2D);
  300. } else if (ED_IS_SHORTCUT("editor/editor_3d", p_event)) {
  301. editor_select(EDITOR_3D);
  302. } else if (ED_IS_SHORTCUT("editor/editor_script", p_event)) {
  303. editor_select(EDITOR_SCRIPT);
  304. } else if (ED_IS_SHORTCUT("editor/editor_help", p_event)) {
  305. emit_signal(SNAME("request_help_search"), "");
  306. } else if (ED_IS_SHORTCUT("editor/editor_assetlib", p_event) && AssetLibraryEditorPlugin::is_available()) {
  307. editor_select(EDITOR_ASSETLIB);
  308. } else if (ED_IS_SHORTCUT("editor/editor_next", p_event)) {
  309. _editor_select_next();
  310. } else if (ED_IS_SHORTCUT("editor/editor_prev", p_event)) {
  311. _editor_select_prev();
  312. } else if (ED_IS_SHORTCUT("editor/command_palette", p_event)) {
  313. _open_command_palette();
  314. } else if (ED_IS_SHORTCUT("editor/toggle_last_opened_bottom_panel", p_event)) {
  315. bottom_panel->toggle_last_opened_bottom_panel();
  316. } else {
  317. }
  318. if (old_editor != editor_plugin_screen) {
  319. get_tree()->get_root()->set_input_as_handled();
  320. }
  321. }
  322. }
  323. void EditorNode::_update_vsync_mode() {
  324. const DisplayServer::VSyncMode window_vsync_mode = DisplayServer::VSyncMode(int(EDITOR_GET("interface/editor/vsync_mode")));
  325. DisplayServer::get_singleton()->window_set_vsync_mode(window_vsync_mode);
  326. }
  327. void EditorNode::_update_from_settings() {
  328. _update_title();
  329. int current_filter = GLOBAL_GET("rendering/textures/canvas_textures/default_texture_filter");
  330. if (current_filter != scene_root->get_default_canvas_item_texture_filter()) {
  331. Viewport::DefaultCanvasItemTextureFilter tf = (Viewport::DefaultCanvasItemTextureFilter)current_filter;
  332. scene_root->set_default_canvas_item_texture_filter(tf);
  333. }
  334. int current_repeat = GLOBAL_GET("rendering/textures/canvas_textures/default_texture_repeat");
  335. if (current_repeat != scene_root->get_default_canvas_item_texture_repeat()) {
  336. Viewport::DefaultCanvasItemTextureRepeat tr = (Viewport::DefaultCanvasItemTextureRepeat)current_repeat;
  337. scene_root->set_default_canvas_item_texture_repeat(tr);
  338. }
  339. RS::DOFBokehShape dof_shape = RS::DOFBokehShape(int(GLOBAL_GET("rendering/camera/depth_of_field/depth_of_field_bokeh_shape")));
  340. RS::get_singleton()->camera_attributes_set_dof_blur_bokeh_shape(dof_shape);
  341. RS::DOFBlurQuality dof_quality = RS::DOFBlurQuality(int(GLOBAL_GET("rendering/camera/depth_of_field/depth_of_field_bokeh_quality")));
  342. bool dof_jitter = GLOBAL_GET("rendering/camera/depth_of_field/depth_of_field_use_jitter");
  343. RS::get_singleton()->camera_attributes_set_dof_blur_quality(dof_quality, dof_jitter);
  344. RS::get_singleton()->environment_set_ssao_quality(RS::EnvironmentSSAOQuality(int(GLOBAL_GET("rendering/environment/ssao/quality"))), GLOBAL_GET("rendering/environment/ssao/half_size"), GLOBAL_GET("rendering/environment/ssao/adaptive_target"), GLOBAL_GET("rendering/environment/ssao/blur_passes"), GLOBAL_GET("rendering/environment/ssao/fadeout_from"), GLOBAL_GET("rendering/environment/ssao/fadeout_to"));
  345. RS::get_singleton()->screen_space_roughness_limiter_set_active(GLOBAL_GET("rendering/anti_aliasing/screen_space_roughness_limiter/enabled"), GLOBAL_GET("rendering/anti_aliasing/screen_space_roughness_limiter/amount"), GLOBAL_GET("rendering/anti_aliasing/screen_space_roughness_limiter/limit"));
  346. bool glow_bicubic = int(GLOBAL_GET("rendering/environment/glow/upscale_mode")) > 0;
  347. RS::get_singleton()->environment_set_ssil_quality(RS::EnvironmentSSILQuality(int(GLOBAL_GET("rendering/environment/ssil/quality"))), GLOBAL_GET("rendering/environment/ssil/half_size"), GLOBAL_GET("rendering/environment/ssil/adaptive_target"), GLOBAL_GET("rendering/environment/ssil/blur_passes"), GLOBAL_GET("rendering/environment/ssil/fadeout_from"), GLOBAL_GET("rendering/environment/ssil/fadeout_to"));
  348. RS::get_singleton()->environment_glow_set_use_bicubic_upscale(glow_bicubic);
  349. RS::EnvironmentSSRRoughnessQuality ssr_roughness_quality = RS::EnvironmentSSRRoughnessQuality(int(GLOBAL_GET("rendering/environment/screen_space_reflection/roughness_quality")));
  350. RS::get_singleton()->environment_set_ssr_roughness_quality(ssr_roughness_quality);
  351. RS::SubSurfaceScatteringQuality sss_quality = RS::SubSurfaceScatteringQuality(int(GLOBAL_GET("rendering/environment/subsurface_scattering/subsurface_scattering_quality")));
  352. RS::get_singleton()->sub_surface_scattering_set_quality(sss_quality);
  353. float sss_scale = GLOBAL_GET("rendering/environment/subsurface_scattering/subsurface_scattering_scale");
  354. float sss_depth_scale = GLOBAL_GET("rendering/environment/subsurface_scattering/subsurface_scattering_depth_scale");
  355. RS::get_singleton()->sub_surface_scattering_set_scale(sss_scale, sss_depth_scale);
  356. uint32_t directional_shadow_size = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/size");
  357. uint32_t directional_shadow_16_bits = GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/16_bits");
  358. RS::get_singleton()->directional_shadow_atlas_set_size(directional_shadow_size, directional_shadow_16_bits);
  359. RS::ShadowQuality shadows_quality = RS::ShadowQuality(int(GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/soft_shadow_filter_quality")));
  360. RS::get_singleton()->positional_soft_shadow_filter_set_quality(shadows_quality);
  361. RS::ShadowQuality directional_shadow_quality = RS::ShadowQuality(int(GLOBAL_GET("rendering/lights_and_shadows/directional_shadow/soft_shadow_filter_quality")));
  362. RS::get_singleton()->directional_soft_shadow_filter_set_quality(directional_shadow_quality);
  363. float probe_update_speed = GLOBAL_GET("rendering/lightmapping/probe_capture/update_speed");
  364. RS::get_singleton()->lightmap_set_probe_capture_update_speed(probe_update_speed);
  365. RS::EnvironmentSDFGIFramesToConverge frames_to_converge = RS::EnvironmentSDFGIFramesToConverge(int(GLOBAL_GET("rendering/global_illumination/sdfgi/frames_to_converge")));
  366. RS::get_singleton()->environment_set_sdfgi_frames_to_converge(frames_to_converge);
  367. RS::EnvironmentSDFGIRayCount ray_count = RS::EnvironmentSDFGIRayCount(int(GLOBAL_GET("rendering/global_illumination/sdfgi/probe_ray_count")));
  368. RS::get_singleton()->environment_set_sdfgi_ray_count(ray_count);
  369. RS::VoxelGIQuality voxel_gi_quality = RS::VoxelGIQuality(int(GLOBAL_GET("rendering/global_illumination/voxel_gi/quality")));
  370. RS::get_singleton()->voxel_gi_set_quality(voxel_gi_quality);
  371. RS::get_singleton()->environment_set_volumetric_fog_volume_size(GLOBAL_GET("rendering/environment/volumetric_fog/volume_size"), GLOBAL_GET("rendering/environment/volumetric_fog/volume_depth"));
  372. RS::get_singleton()->environment_set_volumetric_fog_filter_active(bool(GLOBAL_GET("rendering/environment/volumetric_fog/use_filter")));
  373. RS::get_singleton()->canvas_set_shadow_texture_size(GLOBAL_GET("rendering/2d/shadow_atlas/size"));
  374. bool use_half_res_gi = GLOBAL_GET("rendering/global_illumination/gi/use_half_resolution");
  375. RS::get_singleton()->gi_set_use_half_resolution(use_half_res_gi);
  376. bool snap_2d_transforms = GLOBAL_GET("rendering/2d/snap/snap_2d_transforms_to_pixel");
  377. scene_root->set_snap_2d_transforms_to_pixel(snap_2d_transforms);
  378. bool snap_2d_vertices = GLOBAL_GET("rendering/2d/snap/snap_2d_vertices_to_pixel");
  379. scene_root->set_snap_2d_vertices_to_pixel(snap_2d_vertices);
  380. Viewport::SDFOversize sdf_oversize = Viewport::SDFOversize(int(GLOBAL_GET("rendering/2d/sdf/oversize")));
  381. scene_root->set_sdf_oversize(sdf_oversize);
  382. Viewport::SDFScale sdf_scale = Viewport::SDFScale(int(GLOBAL_GET("rendering/2d/sdf/scale")));
  383. scene_root->set_sdf_scale(sdf_scale);
  384. Viewport::MSAA msaa = Viewport::MSAA(int(GLOBAL_GET("rendering/anti_aliasing/quality/msaa_2d")));
  385. scene_root->set_msaa_2d(msaa);
  386. bool use_hdr_2d = GLOBAL_GET("rendering/viewport/hdr_2d");
  387. scene_root->set_use_hdr_2d(use_hdr_2d);
  388. float mesh_lod_threshold = GLOBAL_GET("rendering/mesh_lod/lod_change/threshold_pixels");
  389. scene_root->set_mesh_lod_threshold(mesh_lod_threshold);
  390. RS::get_singleton()->decals_set_filter(RS::DecalFilter(int(GLOBAL_GET("rendering/textures/decals/filter"))));
  391. RS::get_singleton()->light_projectors_set_filter(RS::LightProjectorFilter(int(GLOBAL_GET("rendering/textures/light_projectors/filter"))));
  392. SceneTree *tree = get_tree();
  393. tree->set_debug_collisions_color(GLOBAL_GET("debug/shapes/collision/shape_color"));
  394. tree->set_debug_collision_contact_color(GLOBAL_GET("debug/shapes/collision/contact_color"));
  395. ResourceImporterTexture::get_singleton()->update_imports();
  396. #ifdef DEBUG_ENABLED
  397. NavigationServer3D::get_singleton()->set_debug_navigation_edge_connection_color(GLOBAL_GET("debug/shapes/navigation/edge_connection_color"));
  398. NavigationServer3D::get_singleton()->set_debug_navigation_geometry_edge_color(GLOBAL_GET("debug/shapes/navigation/geometry_edge_color"));
  399. NavigationServer3D::get_singleton()->set_debug_navigation_geometry_face_color(GLOBAL_GET("debug/shapes/navigation/geometry_face_color"));
  400. NavigationServer3D::get_singleton()->set_debug_navigation_geometry_edge_disabled_color(GLOBAL_GET("debug/shapes/navigation/geometry_edge_disabled_color"));
  401. NavigationServer3D::get_singleton()->set_debug_navigation_geometry_face_disabled_color(GLOBAL_GET("debug/shapes/navigation/geometry_face_disabled_color"));
  402. NavigationServer3D::get_singleton()->set_debug_navigation_enable_edge_connections(GLOBAL_GET("debug/shapes/navigation/enable_edge_connections"));
  403. NavigationServer3D::get_singleton()->set_debug_navigation_enable_edge_connections_xray(GLOBAL_GET("debug/shapes/navigation/enable_edge_connections_xray"));
  404. NavigationServer3D::get_singleton()->set_debug_navigation_enable_edge_lines(GLOBAL_GET("debug/shapes/navigation/enable_edge_lines"));
  405. NavigationServer3D::get_singleton()->set_debug_navigation_enable_edge_lines_xray(GLOBAL_GET("debug/shapes/navigation/enable_edge_lines_xray"));
  406. NavigationServer3D::get_singleton()->set_debug_navigation_enable_geometry_face_random_color(GLOBAL_GET("debug/shapes/navigation/enable_geometry_face_random_color"));
  407. #endif // DEBUG_ENABLED
  408. }
  409. void EditorNode::_gdextensions_reloaded() {
  410. // In case the developer is inspecting an object that will be changed by the reload.
  411. InspectorDock::get_inspector_singleton()->update_tree();
  412. // Regenerate documentation.
  413. EditorHelp::generate_doc();
  414. }
  415. void EditorNode::_select_default_main_screen_plugin() {
  416. if (EDITOR_3D < main_editor_buttons.size() && main_editor_buttons[EDITOR_3D]->is_visible()) {
  417. // If the 3D editor is enabled, use this as the default.
  418. editor_select(EDITOR_3D);
  419. return;
  420. }
  421. // Switch to the first main screen plugin that is enabled. Usually this is
  422. // 2D, but may be subsequent ones if 2D is disabled in the feature profile.
  423. for (int i = 0; i < main_editor_buttons.size(); i++) {
  424. Button *editor_button = main_editor_buttons[i];
  425. if (editor_button->is_visible()) {
  426. editor_select(i);
  427. return;
  428. }
  429. }
  430. editor_select(-1);
  431. }
  432. void EditorNode::_update_theme(bool p_skip_creation) {
  433. if (!p_skip_creation) {
  434. theme = EditorThemeManager::generate_theme(theme);
  435. DisplayServer::set_early_window_clear_color_override(true, theme->get_color(SNAME("background"), EditorStringName(Editor)));
  436. }
  437. List<Ref<Theme>> editor_themes;
  438. editor_themes.push_back(theme);
  439. editor_themes.push_back(ThemeDB::get_singleton()->get_default_theme());
  440. ThemeContext *node_tc = ThemeDB::get_singleton()->get_theme_context(this);
  441. if (node_tc) {
  442. node_tc->set_themes(editor_themes);
  443. } else {
  444. ThemeDB::get_singleton()->create_theme_context(this, editor_themes);
  445. }
  446. Window *window = get_window();
  447. if (window) {
  448. ThemeContext *window_tc = ThemeDB::get_singleton()->get_theme_context(window);
  449. if (window_tc) {
  450. window_tc->set_themes(editor_themes);
  451. } else {
  452. ThemeDB::get_singleton()->create_theme_context(window, editor_themes);
  453. }
  454. }
  455. if (CanvasItemEditor::get_singleton()->get_theme_preview() == CanvasItemEditor::THEME_PREVIEW_EDITOR) {
  456. update_preview_themes(CanvasItemEditor::THEME_PREVIEW_EDITOR);
  457. }
  458. // Update styles.
  459. {
  460. gui_base->add_theme_style_override("panel", theme->get_stylebox(SNAME("Background"), EditorStringName(EditorStyles)));
  461. main_vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, theme->get_constant(SNAME("window_border_margin"), EditorStringName(Editor)));
  462. main_vbox->add_theme_constant_override("separation", theme->get_constant(SNAME("top_bar_separation"), EditorStringName(Editor)));
  463. scene_root_parent->add_theme_style_override("panel", theme->get_stylebox(SNAME("Content"), EditorStringName(EditorStyles)));
  464. bottom_panel->add_theme_style_override("panel", theme->get_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
  465. distraction_free->set_icon(theme->get_icon(SNAME("DistractionFree"), EditorStringName(EditorIcons)));
  466. distraction_free->add_theme_style_override(SceneStringName(pressed), theme->get_stylebox("normal", "FlatMenuButton"));
  467. help_menu->set_item_icon(help_menu->get_item_index(HELP_SEARCH), theme->get_icon(SNAME("HelpSearch"), EditorStringName(EditorIcons)));
  468. help_menu->set_item_icon(help_menu->get_item_index(HELP_COPY_SYSTEM_INFO), theme->get_icon(SNAME("ActionCopy"), EditorStringName(EditorIcons)));
  469. help_menu->set_item_icon(help_menu->get_item_index(HELP_ABOUT), theme->get_icon(SNAME("Godot"), EditorStringName(EditorIcons)));
  470. help_menu->set_item_icon(help_menu->get_item_index(HELP_SUPPORT_GODOT_DEVELOPMENT), theme->get_icon(SNAME("Heart"), EditorStringName(EditorIcons)));
  471. if (EditorDebuggerNode::get_singleton()->is_visible()) {
  472. bottom_panel->add_theme_style_override("panel", theme->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles)));
  473. }
  474. for (int i = 0; i < main_editor_buttons.size(); i++) {
  475. Button *tb = main_editor_buttons[i];
  476. EditorPlugin *p_editor = editor_table[i];
  477. Ref<Texture2D> icon = p_editor->get_icon();
  478. if (icon.is_valid()) {
  479. tb->set_icon(icon);
  480. } else if (theme->has_icon(p_editor->get_name(), EditorStringName(EditorIcons))) {
  481. tb->set_icon(theme->get_icon(p_editor->get_name(), EditorStringName(EditorIcons)));
  482. }
  483. }
  484. }
  485. editor_dock_manager->update_tab_styles();
  486. editor_dock_manager->set_tab_icon_max_width(theme->get_constant(SNAME("class_icon_size"), EditorStringName(Editor)));
  487. }
  488. void EditorNode::update_preview_themes(int p_mode) {
  489. if (!scene_root->is_inside_tree()) {
  490. return; // Too early.
  491. }
  492. List<Ref<Theme>> preview_themes;
  493. switch (p_mode) {
  494. case CanvasItemEditor::THEME_PREVIEW_PROJECT:
  495. preview_themes.push_back(ThemeDB::get_singleton()->get_project_theme());
  496. break;
  497. case CanvasItemEditor::THEME_PREVIEW_EDITOR:
  498. preview_themes.push_back(get_editor_theme());
  499. break;
  500. default:
  501. break;
  502. }
  503. preview_themes.push_back(ThemeDB::get_singleton()->get_default_theme());
  504. ThemeContext *preview_context = ThemeDB::get_singleton()->get_theme_context(scene_root);
  505. if (preview_context) {
  506. preview_context->set_themes(preview_themes);
  507. } else {
  508. ThemeDB::get_singleton()->create_theme_context(scene_root, preview_themes);
  509. }
  510. }
  511. void EditorNode::_notification(int p_what) {
  512. switch (p_what) {
  513. case NOTIFICATION_POSTINITIALIZE: {
  514. EditorHelp::generate_doc();
  515. #if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  516. EditorHelpHighlighter::create_singleton();
  517. #endif
  518. } break;
  519. case NOTIFICATION_PROCESS: {
  520. if (opening_prev && !confirmation->is_visible()) {
  521. opening_prev = false;
  522. }
  523. bool global_unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(EditorUndoRedoManager::GLOBAL_HISTORY);
  524. bool scene_or_global_unsaved = global_unsaved || EditorUndoRedoManager::get_singleton()->is_history_unsaved(editor_data.get_current_edited_scene_history_id());
  525. if (unsaved_cache != scene_or_global_unsaved) {
  526. unsaved_cache = scene_or_global_unsaved;
  527. _update_title();
  528. }
  529. if (editor_data.is_scene_changed(-1)) {
  530. scene_tabs->update_scene_tabs();
  531. }
  532. // Update the animation frame of the update spinner.
  533. uint64_t frame = Engine::get_singleton()->get_frames_drawn();
  534. uint64_t tick = OS::get_singleton()->get_ticks_msec();
  535. if (frame != update_spinner_step_frame && (tick - update_spinner_step_msec) > (1000 / 8)) {
  536. update_spinner_step++;
  537. if (update_spinner_step >= 8) {
  538. update_spinner_step = 0;
  539. }
  540. update_spinner_step_msec = tick;
  541. update_spinner_step_frame = frame + 1;
  542. // Update the icon itself only when the spinner is visible.
  543. if (_should_display_update_spinner()) {
  544. update_spinner->set_icon(theme->get_icon("Progress" + itos(update_spinner_step + 1), EditorStringName(EditorIcons)));
  545. }
  546. }
  547. editor_selection->update();
  548. ResourceImporterTexture::get_singleton()->update_imports();
  549. if (requested_first_scan) {
  550. requested_first_scan = false;
  551. OS::get_singleton()->benchmark_begin_measure("Editor", "First Scan");
  552. if (run_surface_upgrade_tool) {
  553. run_surface_upgrade_tool = false;
  554. SurfaceUpgradeTool::get_singleton()->connect("upgrade_finished", callable_mp(EditorFileSystem::get_singleton(), &EditorFileSystem::scan), CONNECT_ONE_SHOT);
  555. SurfaceUpgradeTool::get_singleton()->finish_upgrade();
  556. } else {
  557. EditorFileSystem::get_singleton()->scan();
  558. }
  559. }
  560. } break;
  561. case NOTIFICATION_ENTER_TREE: {
  562. get_tree()->set_disable_node_threading(true); // No node threading while running editor.
  563. Engine::get_singleton()->set_editor_hint(true);
  564. Window *window = get_window();
  565. if (window) {
  566. // Handle macOS fullscreen and extend-to-title changes.
  567. window->connect("titlebar_changed", callable_mp(this, &EditorNode::_titlebar_resized));
  568. }
  569. // Theme has already been created in the constructor, so we can skip that step.
  570. _update_theme(true);
  571. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/low_processor_mode_sleep_usec")));
  572. get_tree()->get_root()->set_as_audio_listener_3d(false);
  573. get_tree()->get_root()->set_as_audio_listener_2d(false);
  574. get_tree()->get_root()->set_snap_2d_transforms_to_pixel(false);
  575. get_tree()->get_root()->set_snap_2d_vertices_to_pixel(false);
  576. get_tree()->set_auto_accept_quit(false);
  577. #ifdef ANDROID_ENABLED
  578. get_tree()->set_quit_on_go_back(false);
  579. #endif
  580. get_tree()->get_root()->connect("files_dropped", callable_mp(this, &EditorNode::_dropped_files));
  581. command_palette->register_shortcuts_as_command();
  582. callable_mp(this, &EditorNode::_begin_first_scan).call_deferred();
  583. DisplayServer::get_singleton()->set_system_theme_change_callback(callable_mp(this, &EditorNode::_update_theme));
  584. /* DO NOT LOAD SCENES HERE, WAIT FOR FILE SCANNING AND REIMPORT TO COMPLETE */
  585. } break;
  586. case NOTIFICATION_EXIT_TREE: {
  587. if (progress_dialog) {
  588. progress_dialog->queue_free();
  589. }
  590. if (load_error_dialog) {
  591. load_error_dialog->queue_free();
  592. }
  593. if (execute_output_dialog) {
  594. execute_output_dialog->queue_free();
  595. }
  596. if (warning) {
  597. warning->queue_free();
  598. }
  599. if (accept) {
  600. accept->queue_free();
  601. }
  602. if (save_accept) {
  603. save_accept->queue_free();
  604. }
  605. editor_data.save_editor_external_data();
  606. FileAccess::set_file_close_fail_notify_callback(nullptr);
  607. log->deinit(); // Do not get messages anymore.
  608. editor_data.clear_edited_scenes();
  609. } break;
  610. case NOTIFICATION_READY: {
  611. {
  612. started_timestamp = Time::get_singleton()->get_unix_time_from_system();
  613. _initializing_plugins = true;
  614. Vector<String> addons;
  615. if (ProjectSettings::get_singleton()->has_setting("editor_plugins/enabled")) {
  616. addons = GLOBAL_GET("editor_plugins/enabled");
  617. }
  618. for (int i = 0; i < addons.size(); i++) {
  619. set_addon_plugin_enabled(addons[i], true);
  620. }
  621. _initializing_plugins = false;
  622. if (!pending_addons.is_empty()) {
  623. EditorFileSystem::get_singleton()->connect("script_classes_updated", callable_mp(this, &EditorNode::_enable_pending_addons));
  624. }
  625. }
  626. RenderingServer::get_singleton()->viewport_set_disable_2d(get_scene_root()->get_viewport_rid(), true);
  627. RenderingServer::get_singleton()->viewport_set_environment_mode(get_viewport()->get_viewport_rid(), RenderingServer::VIEWPORT_ENVIRONMENT_DISABLED);
  628. feature_profile_manager->notify_changed();
  629. _select_default_main_screen_plugin();
  630. // Save the project after opening to mark it as last modified, except in headless mode.
  631. if (DisplayServer::get_singleton()->window_can_draw()) {
  632. ProjectSettings::get_singleton()->save();
  633. }
  634. _titlebar_resized();
  635. // Set up a theme context for the 2D preview viewport using the stored preview theme.
  636. CanvasItemEditor::ThemePreviewMode theme_preview_mode = (CanvasItemEditor::ThemePreviewMode)(int)EditorSettings::get_singleton()->get_project_metadata("2d_editor", "theme_preview", CanvasItemEditor::THEME_PREVIEW_PROJECT);
  637. update_preview_themes(theme_preview_mode);
  638. /* DO NOT LOAD SCENES HERE, WAIT FOR FILE SCANNING AND REIMPORT TO COMPLETE */
  639. } break;
  640. case NOTIFICATION_APPLICATION_FOCUS_IN: {
  641. // Restore the original FPS cap after focusing back on the editor.
  642. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/low_processor_mode_sleep_usec")));
  643. EditorFileSystem::get_singleton()->scan_changes();
  644. _scan_external_changes();
  645. GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
  646. callable_mp(gdextension_manager, &GDExtensionManager::reload_extensions).call_deferred();
  647. } break;
  648. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  649. // Save on focus loss before applying the FPS limit to avoid slowing down the saving process.
  650. if (EDITOR_GET("interface/editor/save_on_focus_loss")) {
  651. _menu_option_confirm(FILE_SAVE_SCENE_SILENTLY, false);
  652. }
  653. // Set a low FPS cap to decrease CPU/GPU usage while the editor is unfocused.
  654. OS::get_singleton()->set_low_processor_usage_mode_sleep_usec(int(EDITOR_GET("interface/editor/unfocused_low_processor_mode_sleep_usec")));
  655. } break;
  656. case NOTIFICATION_WM_ABOUT: {
  657. show_about();
  658. } break;
  659. case NOTIFICATION_WM_CLOSE_REQUEST: {
  660. _menu_option_confirm(FILE_QUIT, false);
  661. } break;
  662. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  663. if (EditorSettings::get_singleton()->check_changed_settings_in_group("filesystem/file_dialog")) {
  664. FileDialog::set_default_show_hidden_files(EDITOR_GET("filesystem/file_dialog/show_hidden_files"));
  665. EditorFileDialog::set_default_show_hidden_files(EDITOR_GET("filesystem/file_dialog/show_hidden_files"));
  666. EditorFileDialog::set_default_display_mode((EditorFileDialog::DisplayMode)EDITOR_GET("filesystem/file_dialog/display_mode").operator int());
  667. }
  668. if (EDITOR_GET("interface/editor/import_resources_when_unfocused")) {
  669. scan_changes_timer->start();
  670. } else {
  671. scan_changes_timer->stop();
  672. }
  673. follow_system_theme = EDITOR_GET("interface/theme/follow_system_theme");
  674. use_system_accent_color = EDITOR_GET("interface/theme/use_system_accent_color");
  675. if (EditorThemeManager::is_generated_theme_outdated()) {
  676. _update_theme();
  677. _build_icon_type_cache();
  678. recent_scenes->reset_size();
  679. }
  680. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/dock_tab_style")) {
  681. editor_dock_manager->update_tab_styles();
  682. }
  683. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/scene_tabs")) {
  684. scene_tabs->update_scene_tabs();
  685. }
  686. if (EditorSettings::get_singleton()->check_changed_settings_in_group("docks/filesystem")) {
  687. HashSet<String> updated_textfile_extensions;
  688. bool extensions_match = true;
  689. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  690. for (const String &E : textfile_ext) {
  691. updated_textfile_extensions.insert(E);
  692. if (extensions_match && !textfile_extensions.has(E)) {
  693. extensions_match = false;
  694. }
  695. }
  696. if (!extensions_match || updated_textfile_extensions.size() < textfile_extensions.size()) {
  697. textfile_extensions = updated_textfile_extensions;
  698. EditorFileSystem::get_singleton()->scan();
  699. }
  700. }
  701. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor")) {
  702. _update_update_spinner();
  703. _update_vsync_mode();
  704. }
  705. #if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  706. if (EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/theme/highlighting")) {
  707. EditorHelpHighlighter::get_singleton()->reset_cache();
  708. }
  709. #endif
  710. } break;
  711. }
  712. }
  713. void EditorNode::_update_update_spinner() {
  714. update_spinner->set_visible(!RenderingServer::get_singleton()->canvas_item_get_debug_redraw() && _should_display_update_spinner());
  715. const bool update_continuously = EDITOR_GET("interface/editor/update_continuously");
  716. PopupMenu *update_popup = update_spinner->get_popup();
  717. update_popup->set_item_checked(update_popup->get_item_index(SETTINGS_UPDATE_CONTINUOUSLY), update_continuously);
  718. update_popup->set_item_checked(update_popup->get_item_index(SETTINGS_UPDATE_WHEN_CHANGED), !update_continuously);
  719. if (update_continuously) {
  720. update_spinner->set_tooltip_text(TTR("Spins when the editor window redraws.\nUpdate Continuously is enabled, which can increase power usage. Click to disable it."));
  721. // Use a different color for the update spinner when Update Continuously is enabled,
  722. // as this feature should only be enabled for troubleshooting purposes.
  723. // Make the icon modulate color overbright because icons are not completely white on a dark theme.
  724. // On a light theme, icons are dark, so we need to modulate them with an even brighter color.
  725. const bool dark_theme = EditorThemeManager::is_dark_theme();
  726. update_spinner->set_self_modulate(theme->get_color(SNAME("error_color"), EditorStringName(Editor)) * (dark_theme ? Color(1.1, 1.1, 1.1) : Color(4.25, 4.25, 4.25)));
  727. } else {
  728. update_spinner->set_tooltip_text(TTR("Spins when the editor window redraws."));
  729. update_spinner->set_self_modulate(Color(1, 1, 1));
  730. }
  731. OS::get_singleton()->set_low_processor_usage_mode(!update_continuously);
  732. }
  733. void EditorNode::_on_plugin_ready(Object *p_script, const String &p_activate_name) {
  734. Ref<Script> scr = Object::cast_to<Script>(p_script);
  735. if (scr.is_null()) {
  736. return;
  737. }
  738. project_settings_editor->update_plugins();
  739. project_settings_editor->hide();
  740. push_item(scr.operator->());
  741. if (p_activate_name.length()) {
  742. set_addon_plugin_enabled(p_activate_name, true);
  743. }
  744. }
  745. void EditorNode::_remove_plugin_from_enabled(const String &p_name) {
  746. ProjectSettings *ps = ProjectSettings::get_singleton();
  747. PackedStringArray enabled_plugins = ps->get("editor_plugins/enabled");
  748. for (int i = 0; i < enabled_plugins.size(); ++i) {
  749. if (enabled_plugins.get(i) == p_name) {
  750. enabled_plugins.remove_at(i);
  751. break;
  752. }
  753. }
  754. ps->set("editor_plugins/enabled", enabled_plugins);
  755. }
  756. void EditorNode::_plugin_over_edit(EditorPlugin *p_plugin, Object *p_object) {
  757. if (p_object) {
  758. editor_plugins_over->add_plugin(p_plugin);
  759. p_plugin->make_visible(true);
  760. p_plugin->edit(p_object);
  761. } else {
  762. editor_plugins_over->remove_plugin(p_plugin);
  763. p_plugin->make_visible(false);
  764. p_plugin->edit(nullptr);
  765. }
  766. }
  767. void EditorNode::_plugin_over_self_own(EditorPlugin *p_plugin) {
  768. active_plugins[p_plugin->get_instance_id()].insert(p_plugin);
  769. }
  770. void EditorNode::_resources_changed(const Vector<String> &p_resources) {
  771. List<Ref<Resource>> changed;
  772. int rc = p_resources.size();
  773. for (int i = 0; i < rc; i++) {
  774. Ref<Resource> res = ResourceCache::get_ref(p_resources.get(i));
  775. if (res.is_null()) {
  776. continue;
  777. }
  778. if (!res->editor_can_reload_from_file()) {
  779. Ref<Script> scr = res;
  780. // Scripts are reloaded via the script editor.
  781. if (scr.is_null() || ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
  782. continue;
  783. }
  784. }
  785. if (!res->get_path().is_resource_file() && !res->get_path().is_absolute_path()) {
  786. continue;
  787. }
  788. if (!FileAccess::exists(res->get_path())) {
  789. continue;
  790. }
  791. if (!res->get_import_path().is_empty()) {
  792. // This is an imported resource, will be reloaded if reimported via the _resources_reimported() callback.
  793. continue;
  794. }
  795. changed.push_back(res);
  796. }
  797. if (changed.size()) {
  798. for (Ref<Resource> &res : changed) {
  799. res->reload_from_file();
  800. }
  801. }
  802. }
  803. void EditorNode::_fs_changed() {
  804. for (FileDialog *E : file_dialogs) {
  805. E->invalidate();
  806. }
  807. for (EditorFileDialog *E : editor_file_dialogs) {
  808. E->invalidate();
  809. }
  810. _mark_unsaved_scenes();
  811. // FIXME: Move this to a cleaner location, it's hacky to do this in _fs_changed.
  812. String export_error;
  813. Error err = OK;
  814. if (!export_defer.preset.is_empty() && !EditorFileSystem::get_singleton()->is_scanning()) {
  815. String preset_name = export_defer.preset;
  816. // Ensures export_project does not loop infinitely, because notifications may
  817. // come during the export.
  818. export_defer.preset = "";
  819. Ref<EditorExportPreset> export_preset;
  820. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); ++i) {
  821. export_preset = EditorExport::get_singleton()->get_export_preset(i);
  822. if (export_preset->get_name() == preset_name) {
  823. break;
  824. }
  825. export_preset.unref();
  826. }
  827. if (export_preset.is_null()) {
  828. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  829. if (da->file_exists("res://export_presets.cfg")) {
  830. err = FAILED;
  831. export_error = vformat(
  832. "Invalid export preset name: %s.\nThe following presets were detected in this project's `export_presets.cfg`:\n\n",
  833. preset_name);
  834. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); ++i) {
  835. // Write the preset name between double quotes since it needs to be written between quotes on the command line if it contains spaces.
  836. export_error += vformat(" \"%s\"\n", EditorExport::get_singleton()->get_export_preset(i)->get_name());
  837. }
  838. } else {
  839. err = FAILED;
  840. 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.";
  841. }
  842. } else {
  843. Ref<EditorExportPlatform> platform = export_preset->get_platform();
  844. const String export_path = export_defer.path.is_empty() ? export_preset->get_export_path() : export_defer.path;
  845. if (export_path.is_empty()) {
  846. err = FAILED;
  847. export_error = vformat("Export preset \"%s\" doesn't have a default export path, and none was specified.", preset_name);
  848. } else if (platform.is_null()) {
  849. err = FAILED;
  850. export_error = vformat("Export preset \"%s\" doesn't have a matching platform.", preset_name);
  851. } else {
  852. export_preset->update_value_overrides();
  853. if (export_defer.pack_only) { // Only export .pck or .zip data pack.
  854. if (export_path.ends_with(".zip")) {
  855. err = platform->export_zip(export_preset, export_defer.debug, export_path);
  856. } else if (export_path.ends_with(".pck")) {
  857. err = platform->export_pack(export_preset, export_defer.debug, export_path);
  858. } else {
  859. ERR_PRINT(vformat("Export path \"%s\" doesn't end with a supported extension.", export_path));
  860. err = FAILED;
  861. }
  862. } else { // Normal project export.
  863. String config_error;
  864. bool missing_templates;
  865. if (export_defer.android_build_template) {
  866. export_template_manager->install_android_template(export_preset);
  867. }
  868. if (!platform->can_export(export_preset, config_error, missing_templates, export_defer.debug)) {
  869. ERR_PRINT(vformat("Cannot export project with preset \"%s\" due to configuration errors:\n%s", preset_name, config_error));
  870. err = missing_templates ? ERR_FILE_NOT_FOUND : ERR_UNCONFIGURED;
  871. } else {
  872. platform->clear_messages();
  873. err = platform->export_project(export_preset, export_defer.debug, export_path);
  874. }
  875. }
  876. if (err != OK) {
  877. export_error = vformat("Project export for preset \"%s\" failed.", preset_name);
  878. } else if (platform->get_worst_message_type() >= EditorExportPlatform::EXPORT_MESSAGE_WARNING) {
  879. export_error = vformat("Project export for preset \"%s\" completed with warnings.", preset_name);
  880. }
  881. }
  882. }
  883. if (err != OK) {
  884. ERR_PRINT(export_error);
  885. _exit_editor(EXIT_FAILURE);
  886. return;
  887. }
  888. if (!export_error.is_empty()) {
  889. WARN_PRINT(export_error);
  890. }
  891. _exit_editor(EXIT_SUCCESS);
  892. }
  893. }
  894. void EditorNode::_resources_reimported(const Vector<String> &p_resources) {
  895. List<String> scenes;
  896. int current_tab = scene_tabs->get_current_tab();
  897. for (int i = 0; i < p_resources.size(); i++) {
  898. String file_type = ResourceLoader::get_resource_type(p_resources[i]);
  899. if (file_type == "PackedScene") {
  900. scenes.push_back(p_resources[i]);
  901. // Reload later if needed, first go with normal resources.
  902. continue;
  903. }
  904. if (!ResourceCache::has(p_resources[i])) {
  905. // Not loaded, no need to reload.
  906. continue;
  907. }
  908. // Reload normally.
  909. Ref<Resource> resource = ResourceCache::get_ref(p_resources[i]);
  910. if (resource.is_valid()) {
  911. resource->reload_from_file();
  912. }
  913. }
  914. // Editor may crash when related animation is playing while re-importing GLTF scene, stop it in advance.
  915. AnimationPlayer *ap = AnimationPlayerEditor::get_singleton()->get_player();
  916. if (ap && scenes.size() > 0) {
  917. ap->stop(true);
  918. }
  919. for (const String &E : scenes) {
  920. reload_scene(E);
  921. reload_instances_with_path_in_edited_scenes(E);
  922. }
  923. scene_tabs->set_current_tab(current_tab);
  924. }
  925. void EditorNode::_sources_changed(bool p_exist) {
  926. if (waiting_for_first_scan) {
  927. waiting_for_first_scan = false;
  928. OS::get_singleton()->benchmark_end_measure("Editor", "First Scan");
  929. // Reload the global shader variables, but this time
  930. // loading textures, as they are now properly imported.
  931. RenderingServer::get_singleton()->global_shader_parameters_load_settings(true);
  932. _load_editor_layout();
  933. if (!defer_load_scene.is_empty()) {
  934. OS::get_singleton()->benchmark_begin_measure("Editor", "Load Scene");
  935. load_scene(defer_load_scene);
  936. defer_load_scene = "";
  937. OS::get_singleton()->benchmark_end_measure("Editor", "Load Scene");
  938. OS::get_singleton()->benchmark_dump();
  939. }
  940. if (SurfaceUpgradeTool::get_singleton()->is_show_requested()) {
  941. SurfaceUpgradeTool::get_singleton()->show_popup();
  942. }
  943. // Start preview thread now that it's safe.
  944. if (!singleton->cmdline_export_mode) {
  945. EditorResourcePreview::get_singleton()->start();
  946. }
  947. }
  948. }
  949. void EditorNode::_scan_external_changes() {
  950. disk_changed_list->clear();
  951. TreeItem *r = disk_changed_list->create_item();
  952. disk_changed_list->set_hide_root(true);
  953. bool need_reload = false;
  954. // Check if any edited scene has changed.
  955. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  956. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  957. if (editor_data.get_scene_path(i) == "" || !da->file_exists(editor_data.get_scene_path(i))) {
  958. continue;
  959. }
  960. uint64_t last_date = editor_data.get_scene_modified_time(i);
  961. uint64_t date = FileAccess::get_modified_time(editor_data.get_scene_path(i));
  962. if (date > last_date) {
  963. TreeItem *ti = disk_changed_list->create_item(r);
  964. ti->set_text(0, editor_data.get_scene_path(i).get_file());
  965. need_reload = true;
  966. }
  967. }
  968. String project_settings_path = ProjectSettings::get_singleton()->get_resource_path().path_join("project.godot");
  969. if (FileAccess::get_modified_time(project_settings_path) > ProjectSettings::get_singleton()->get_last_saved_time()) {
  970. TreeItem *ti = disk_changed_list->create_item(r);
  971. ti->set_text(0, "project.godot");
  972. need_reload = true;
  973. }
  974. if (need_reload) {
  975. callable_mp((Window *)disk_changed, &Window::popup_centered_ratio).call_deferred(0.3);
  976. }
  977. }
  978. void EditorNode::_resave_scenes(String p_str) {
  979. save_all_scenes();
  980. ProjectSettings::get_singleton()->save();
  981. disk_changed->hide();
  982. }
  983. void EditorNode::_reload_modified_scenes() {
  984. int current_idx = editor_data.get_edited_scene();
  985. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  986. if (editor_data.get_scene_path(i) == "") {
  987. continue;
  988. }
  989. uint64_t last_date = editor_data.get_scene_modified_time(i);
  990. uint64_t date = FileAccess::get_modified_time(editor_data.get_scene_path(i));
  991. if (date > last_date) {
  992. String filename = editor_data.get_scene_path(i);
  993. editor_data.set_edited_scene(i);
  994. _remove_edited_scene(false);
  995. Error err = load_scene(filename, false, false, true, false, true);
  996. if (err != OK) {
  997. ERR_PRINT(vformat("Failed to load scene: %s", filename));
  998. }
  999. editor_data.move_edited_scene_to_index(i);
  1000. }
  1001. }
  1002. _set_current_scene(current_idx);
  1003. scene_tabs->update_scene_tabs();
  1004. disk_changed->hide();
  1005. }
  1006. void EditorNode::_reload_project_settings() {
  1007. ProjectSettings::get_singleton()->setup(ProjectSettings::get_singleton()->get_resource_path(), String(), true);
  1008. }
  1009. void EditorNode::_vp_resized() {
  1010. }
  1011. void EditorNode::_titlebar_resized() {
  1012. DisplayServer::get_singleton()->window_set_window_buttons_offset(Vector2i(title_bar->get_global_position().y + title_bar->get_size().y / 2, title_bar->get_global_position().y + title_bar->get_size().y / 2), DisplayServer::MAIN_WINDOW_ID);
  1013. const Vector3i &margin = DisplayServer::get_singleton()->window_get_safe_title_margins(DisplayServer::MAIN_WINDOW_ID);
  1014. if (left_menu_spacer) {
  1015. int w = (gui_base->is_layout_rtl()) ? margin.y : margin.x;
  1016. left_menu_spacer->set_custom_minimum_size(Size2(w, 0));
  1017. }
  1018. if (right_menu_spacer) {
  1019. int w = (gui_base->is_layout_rtl()) ? margin.x : margin.y;
  1020. right_menu_spacer->set_custom_minimum_size(Size2(w, 0));
  1021. }
  1022. if (title_bar) {
  1023. title_bar->set_custom_minimum_size(Size2(0, margin.z - title_bar->get_global_position().y));
  1024. }
  1025. }
  1026. void EditorNode::_update_undo_redo_allowed() {
  1027. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1028. file_menu->set_item_disabled(file_menu->get_item_index(EDIT_UNDO), !undo_redo->has_undo());
  1029. file_menu->set_item_disabled(file_menu->get_item_index(EDIT_REDO), !undo_redo->has_redo());
  1030. }
  1031. void EditorNode::_node_renamed() {
  1032. if (InspectorDock::get_inspector_singleton()) {
  1033. InspectorDock::get_inspector_singleton()->update_tree();
  1034. }
  1035. }
  1036. void EditorNode::_editor_select_next() {
  1037. int editor = _get_current_main_editor();
  1038. do {
  1039. if (editor == editor_table.size() - 1) {
  1040. editor = 0;
  1041. } else {
  1042. editor++;
  1043. }
  1044. } while (!main_editor_buttons[editor]->is_visible());
  1045. editor_select(editor);
  1046. }
  1047. void EditorNode::_open_command_palette() {
  1048. command_palette->open_popup();
  1049. }
  1050. void EditorNode::_editor_select_prev() {
  1051. int editor = _get_current_main_editor();
  1052. do {
  1053. if (editor == 0) {
  1054. editor = editor_table.size() - 1;
  1055. } else {
  1056. editor--;
  1057. }
  1058. } while (!main_editor_buttons[editor]->is_visible());
  1059. editor_select(editor);
  1060. }
  1061. Error EditorNode::load_resource(const String &p_resource, bool p_ignore_broken_deps) {
  1062. dependency_errors.clear();
  1063. Error err;
  1064. Ref<Resource> res;
  1065. if (ResourceLoader::exists(p_resource, "")) {
  1066. res = ResourceLoader::load(p_resource, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
  1067. } else if (textfile_extensions.has(p_resource.get_extension())) {
  1068. res = ScriptEditor::get_singleton()->open_file(p_resource);
  1069. }
  1070. ERR_FAIL_COND_V(!res.is_valid(), ERR_CANT_OPEN);
  1071. if (!p_ignore_broken_deps && dependency_errors.has(p_resource)) {
  1072. Vector<String> errors;
  1073. for (const String &E : dependency_errors[p_resource]) {
  1074. errors.push_back(E);
  1075. }
  1076. dependency_error->show(DependencyErrorDialog::MODE_RESOURCE, p_resource, errors);
  1077. dependency_errors.erase(p_resource);
  1078. return ERR_FILE_MISSING_DEPENDENCIES;
  1079. }
  1080. InspectorDock::get_singleton()->edit_resource(res);
  1081. return OK;
  1082. }
  1083. void EditorNode::edit_node(Node *p_node) {
  1084. push_item(p_node);
  1085. }
  1086. void EditorNode::edit_resource(const Ref<Resource> &p_resource) {
  1087. InspectorDock::get_singleton()->edit_resource(p_resource);
  1088. }
  1089. void EditorNode::save_resource_in_path(const Ref<Resource> &p_resource, const String &p_path) {
  1090. editor_data.apply_changes_in_editors();
  1091. if (saving_resources_in_path.has(p_resource)) {
  1092. return;
  1093. }
  1094. saving_resources_in_path.insert(p_resource);
  1095. int flg = 0;
  1096. if (EDITOR_GET("filesystem/on_save/compress_binary_resources")) {
  1097. flg |= ResourceSaver::FLAG_COMPRESS;
  1098. }
  1099. String path = ProjectSettings::get_singleton()->localize_path(p_path);
  1100. Error err = ResourceSaver::save(p_resource, path, flg | ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS);
  1101. if (err != OK) {
  1102. if (ResourceLoader::is_imported(p_resource->get_path())) {
  1103. show_accept(TTR("Imported resources can't be saved."), TTR("OK"));
  1104. } else {
  1105. show_accept(TTR("Error saving resource!"), TTR("OK"));
  1106. }
  1107. saving_resources_in_path.erase(p_resource);
  1108. return;
  1109. }
  1110. ((Resource *)p_resource.ptr())->set_path(path);
  1111. saving_resources_in_path.erase(p_resource);
  1112. _resource_saved(p_resource, path);
  1113. emit_signal(SNAME("resource_saved"), p_resource);
  1114. editor_data.notify_resource_saved(p_resource);
  1115. }
  1116. void EditorNode::save_resource(const Ref<Resource> &p_resource) {
  1117. // If built-in resource, save the scene instead.
  1118. if (p_resource->is_built_in()) {
  1119. const String scene_path = p_resource->get_path().get_slice("::", 0);
  1120. if (!scene_path.is_empty()) {
  1121. if (ResourceLoader::exists(scene_path) && ResourceLoader::get_resource_type(scene_path) == "PackedScene") {
  1122. save_scene_if_open(scene_path);
  1123. } else {
  1124. // Not a packed scene, so save it as regular resource.
  1125. Ref<Resource> parent_resource = ResourceCache::get_ref(scene_path);
  1126. ERR_FAIL_COND_MSG(parent_resource.is_null(), "Parent resource not loaded, can't save.");
  1127. save_resource(parent_resource);
  1128. }
  1129. return;
  1130. }
  1131. }
  1132. // If the resource has been imported, ask the user to use a different path in order to save it.
  1133. String path = p_resource->get_path();
  1134. if (path.is_resource_file() && !FileAccess::exists(path + ".import")) {
  1135. save_resource_in_path(p_resource, p_resource->get_path());
  1136. } else {
  1137. save_resource_as(p_resource);
  1138. }
  1139. }
  1140. void EditorNode::save_resource_as(const Ref<Resource> &p_resource, const String &p_at_path) {
  1141. {
  1142. String path = p_resource->get_path();
  1143. if (!path.is_resource_file()) {
  1144. int srpos = path.find("::");
  1145. if (srpos != -1) {
  1146. String base = path.substr(0, srpos);
  1147. if (!get_edited_scene() || get_edited_scene()->get_scene_file_path() != base) {
  1148. show_warning(TTR("This resource can't be saved because it does not belong to the edited scene. Make it unique first."));
  1149. return;
  1150. }
  1151. }
  1152. } else if (FileAccess::exists(path + ".import")) {
  1153. show_warning(TTR("This resource can't be saved because it was imported from another file. Make it unique first."));
  1154. return;
  1155. }
  1156. }
  1157. file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1158. saving_resource = p_resource;
  1159. current_menu_option = RESOURCE_SAVE_AS;
  1160. List<String> extensions;
  1161. Ref<PackedScene> sd = memnew(PackedScene);
  1162. ResourceSaver::get_recognized_extensions(p_resource, &extensions);
  1163. file->clear_filters();
  1164. List<String> preferred;
  1165. for (const String &E : extensions) {
  1166. if (p_resource->is_class("Script") && (E == "tres" || E == "res")) {
  1167. // This serves no purpose and confused people.
  1168. continue;
  1169. }
  1170. file->add_filter("*." + E, E.to_upper());
  1171. preferred.push_back(E);
  1172. }
  1173. // Lowest priority extension.
  1174. List<String>::Element *res_element = preferred.find("res");
  1175. if (res_element) {
  1176. preferred.move_to_back(res_element);
  1177. }
  1178. // Highest priority extension.
  1179. List<String>::Element *tres_element = preferred.find("tres");
  1180. if (tres_element) {
  1181. preferred.move_to_front(tres_element);
  1182. }
  1183. if (!p_at_path.is_empty()) {
  1184. file->set_current_dir(p_at_path);
  1185. if (p_resource->get_path().is_resource_file()) {
  1186. file->set_current_file(p_resource->get_path().get_file());
  1187. } else {
  1188. if (extensions.size()) {
  1189. String resource_name_snake_case = p_resource->get_class().to_snake_case();
  1190. file->set_current_file("new_" + resource_name_snake_case + "." + preferred.front()->get().to_lower());
  1191. } else {
  1192. file->set_current_file(String());
  1193. }
  1194. }
  1195. } else if (!p_resource->get_path().is_empty()) {
  1196. file->set_current_path(p_resource->get_path());
  1197. if (extensions.size()) {
  1198. String ext = p_resource->get_path().get_extension().to_lower();
  1199. if (extensions.find(ext) == nullptr) {
  1200. file->set_current_path(p_resource->get_path().replacen("." + ext, "." + extensions.front()->get()));
  1201. }
  1202. }
  1203. } else if (preferred.size()) {
  1204. String existing;
  1205. if (extensions.size()) {
  1206. String resource_name_snake_case = p_resource->get_class().to_snake_case();
  1207. existing = "new_" + resource_name_snake_case + "." + preferred.front()->get().to_lower();
  1208. }
  1209. file->set_current_path(existing);
  1210. }
  1211. file->set_title(TTR("Save Resource As..."));
  1212. file->popup_file_dialog();
  1213. }
  1214. void EditorNode::_menu_option(int p_option) {
  1215. _menu_option_confirm(p_option, false);
  1216. }
  1217. void EditorNode::_menu_confirm_current() {
  1218. _menu_option_confirm(current_menu_option, true);
  1219. }
  1220. void EditorNode::trigger_menu_option(int p_option, bool p_confirmed) {
  1221. _menu_option_confirm(p_option, p_confirmed);
  1222. }
  1223. void EditorNode::_dialog_display_save_error(String p_file, Error p_error) {
  1224. if (p_error) {
  1225. switch (p_error) {
  1226. case ERR_FILE_CANT_WRITE: {
  1227. show_accept(TTR("Can't open file for writing:") + " " + p_file.get_extension(), TTR("OK"));
  1228. } break;
  1229. case ERR_FILE_UNRECOGNIZED: {
  1230. show_accept(TTR("Requested file format unknown:") + " " + p_file.get_extension(), TTR("OK"));
  1231. } break;
  1232. default: {
  1233. show_accept(TTR("Error while saving."), TTR("OK"));
  1234. } break;
  1235. }
  1236. }
  1237. }
  1238. void EditorNode::_dialog_display_load_error(String p_file, Error p_error) {
  1239. if (p_error) {
  1240. switch (p_error) {
  1241. case ERR_CANT_OPEN: {
  1242. show_accept(vformat(TTR("Can't open file '%s'. The file could have been moved or deleted."), p_file.get_file()), TTR("OK"));
  1243. } break;
  1244. case ERR_PARSE_ERROR: {
  1245. show_accept(vformat(TTR("Error while parsing file '%s'."), p_file.get_file()), TTR("OK"));
  1246. } break;
  1247. case ERR_FILE_CORRUPT: {
  1248. show_accept(vformat(TTR("Scene file '%s' appears to be invalid/corrupt."), p_file.get_file()), TTR("OK"));
  1249. } break;
  1250. case ERR_FILE_NOT_FOUND: {
  1251. show_accept(vformat(TTR("Missing file '%s' or one of its dependencies."), p_file.get_file()), TTR("OK"));
  1252. } break;
  1253. case ERR_FILE_UNRECOGNIZED: {
  1254. show_accept(vformat(TTR("File '%s' is saved in a format that is newer than the formats supported by this version of Godot, so it can't be opened."), p_file.get_file()), TTR("OK"));
  1255. } break;
  1256. default: {
  1257. show_accept(vformat(TTR("Error while loading file '%s'."), p_file.get_file()), TTR("OK"));
  1258. } break;
  1259. }
  1260. }
  1261. }
  1262. void EditorNode::_load_editor_plugin_states_from_config(const Ref<ConfigFile> &p_config_file) {
  1263. Node *scene = editor_data.get_edited_scene_root();
  1264. if (!scene) {
  1265. return;
  1266. }
  1267. List<String> esl;
  1268. p_config_file->get_section_keys("editor_states", &esl);
  1269. Dictionary md;
  1270. for (const String &E : esl) {
  1271. Variant st = p_config_file->get_value("editor_states", E);
  1272. if (st.get_type() != Variant::NIL) {
  1273. md[E] = st;
  1274. }
  1275. }
  1276. editor_data.set_editor_plugin_states(md);
  1277. }
  1278. void EditorNode::_save_editor_states(const String &p_file, int p_idx) {
  1279. Node *scene = editor_data.get_edited_scene_root(p_idx);
  1280. if (!scene) {
  1281. return;
  1282. }
  1283. String path = EditorPaths::get_singleton()->get_project_settings_dir().path_join(p_file.get_file() + "-editstate-" + p_file.md5_text() + ".cfg");
  1284. Ref<ConfigFile> cf;
  1285. cf.instantiate();
  1286. Dictionary md;
  1287. if (p_idx < 0 || editor_data.get_edited_scene() == p_idx) {
  1288. md = editor_data.get_editor_plugin_states();
  1289. } else {
  1290. md = editor_data.get_scene_editor_states(p_idx);
  1291. }
  1292. List<Variant> keys;
  1293. md.get_key_list(&keys);
  1294. for (const Variant &E : keys) {
  1295. cf->set_value("editor_states", E, md[E]);
  1296. }
  1297. // Save the currently selected nodes.
  1298. List<Node *> selection = editor_selection->get_full_selected_node_list();
  1299. TypedArray<NodePath> selection_paths;
  1300. for (Node *selected_node : selection) {
  1301. selection_paths.push_back(selected_node->get_path());
  1302. }
  1303. cf->set_value("editor_states", "selected_nodes", selection_paths);
  1304. Error err = cf->save(path);
  1305. ERR_FAIL_COND_MSG(err != OK, "Cannot save config file to '" + path + "'.");
  1306. }
  1307. bool EditorNode::_find_and_save_resource(Ref<Resource> p_res, HashMap<Ref<Resource>, bool> &processed, int32_t flags) {
  1308. if (p_res.is_null()) {
  1309. return false;
  1310. }
  1311. if (processed.has(p_res)) {
  1312. return processed[p_res];
  1313. }
  1314. bool changed = p_res->is_edited();
  1315. p_res->set_edited(false);
  1316. bool subchanged = _find_and_save_edited_subresources(p_res.ptr(), processed, flags);
  1317. if (p_res->get_path().is_resource_file()) {
  1318. if (changed || subchanged) {
  1319. ResourceSaver::save(p_res, p_res->get_path(), flags);
  1320. }
  1321. processed[p_res] = false; // Because it's a file.
  1322. return false;
  1323. } else {
  1324. processed[p_res] = changed;
  1325. return changed;
  1326. }
  1327. }
  1328. bool EditorNode::_find_and_save_edited_subresources(Object *obj, HashMap<Ref<Resource>, bool> &processed, int32_t flags) {
  1329. bool ret_changed = false;
  1330. List<PropertyInfo> pi;
  1331. obj->get_property_list(&pi);
  1332. for (const PropertyInfo &E : pi) {
  1333. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  1334. continue;
  1335. }
  1336. switch (E.type) {
  1337. case Variant::OBJECT: {
  1338. Ref<Resource> res = obj->get(E.name);
  1339. if (_find_and_save_resource(res, processed, flags)) {
  1340. ret_changed = true;
  1341. }
  1342. } break;
  1343. case Variant::ARRAY: {
  1344. Array varray = obj->get(E.name);
  1345. int len = varray.size();
  1346. for (int i = 0; i < len; i++) {
  1347. const Variant &v = varray.get(i);
  1348. Ref<Resource> res = v;
  1349. if (_find_and_save_resource(res, processed, flags)) {
  1350. ret_changed = true;
  1351. }
  1352. }
  1353. } break;
  1354. case Variant::DICTIONARY: {
  1355. Dictionary d = obj->get(E.name);
  1356. List<Variant> keys;
  1357. d.get_key_list(&keys);
  1358. for (const Variant &F : keys) {
  1359. Variant v = d[F];
  1360. Ref<Resource> res = v;
  1361. if (_find_and_save_resource(res, processed, flags)) {
  1362. ret_changed = true;
  1363. }
  1364. }
  1365. } break;
  1366. default: {
  1367. }
  1368. }
  1369. }
  1370. return ret_changed;
  1371. }
  1372. void EditorNode::_save_edited_subresources(Node *scene, HashMap<Ref<Resource>, bool> &processed, int32_t flags) {
  1373. _find_and_save_edited_subresources(scene, processed, flags);
  1374. for (int i = 0; i < scene->get_child_count(); i++) {
  1375. Node *n = scene->get_child(i);
  1376. if (n->get_owner() != editor_data.get_edited_scene_root()) {
  1377. continue;
  1378. }
  1379. _save_edited_subresources(n, processed, flags);
  1380. }
  1381. }
  1382. void EditorNode::_find_node_types(Node *p_node, int &count_2d, int &count_3d) {
  1383. if (p_node->is_class("Viewport") || (p_node != editor_data.get_edited_scene_root() && p_node->get_owner() != editor_data.get_edited_scene_root())) {
  1384. return;
  1385. }
  1386. if (p_node->is_class("CanvasItem")) {
  1387. count_2d++;
  1388. } else if (p_node->is_class("Node3D")) {
  1389. count_3d++;
  1390. }
  1391. for (int i = 0; i < p_node->get_child_count(); i++) {
  1392. _find_node_types(p_node->get_child(i), count_2d, count_3d);
  1393. }
  1394. }
  1395. void EditorNode::_save_scene_with_preview(String p_file, int p_idx) {
  1396. EditorProgress save("save", TTR("Saving Scene"), 4);
  1397. if (editor_data.get_edited_scene_root() != nullptr) {
  1398. save.step(TTR("Analyzing"), 0);
  1399. int c2d = 0;
  1400. int c3d = 0;
  1401. _find_node_types(editor_data.get_edited_scene_root(), c2d, c3d);
  1402. save.step(TTR("Creating Thumbnail"), 1);
  1403. // Current view?
  1404. Ref<Image> img;
  1405. // If neither 3D or 2D nodes are present, make a 1x1 black texture.
  1406. // We cannot fallback on the 2D editor, because it may not have been used yet,
  1407. // which would result in an invalid texture.
  1408. if (c3d == 0 && c2d == 0) {
  1409. img.instantiate();
  1410. img->initialize_data(1, 1, false, Image::FORMAT_RGB8);
  1411. } else if (c3d < c2d) {
  1412. Ref<ViewportTexture> viewport_texture = scene_root->get_texture();
  1413. if (viewport_texture->get_width() > 0 && viewport_texture->get_height() > 0) {
  1414. img = viewport_texture->get_image();
  1415. }
  1416. } else {
  1417. // The 3D editor may be disabled as a feature, but scenes can still be opened.
  1418. // This check prevents the preview from regenerating in case those scenes are then saved.
  1419. // The preview will be generated if no feature profile is set (as the 3D editor is enabled by default).
  1420. Ref<EditorFeatureProfile> profile = feature_profile_manager->get_current_profile();
  1421. if (!profile.is_valid() || !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D)) {
  1422. img = Node3DEditor::get_singleton()->get_editor_viewport(0)->get_viewport_node()->get_texture()->get_image();
  1423. }
  1424. }
  1425. if (img.is_valid() && img->get_width() > 0 && img->get_height() > 0) {
  1426. img = img->duplicate();
  1427. save.step(TTR("Creating Thumbnail"), 2);
  1428. save.step(TTR("Creating Thumbnail"), 3);
  1429. int preview_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");
  1430. preview_size *= EDSCALE;
  1431. // Consider a square region.
  1432. int vp_size = MIN(img->get_width(), img->get_height());
  1433. int x = (img->get_width() - vp_size) / 2;
  1434. int y = (img->get_height() - vp_size) / 2;
  1435. if (vp_size < preview_size) {
  1436. // Just square it.
  1437. img->crop_from_point(x, y, vp_size, vp_size);
  1438. } else {
  1439. int ratio = vp_size / preview_size;
  1440. int size = preview_size * MAX(1, ratio / 2);
  1441. x = (img->get_width() - size) / 2;
  1442. y = (img->get_height() - size) / 2;
  1443. img->crop_from_point(x, y, size, size);
  1444. img->resize(preview_size, preview_size, Image::INTERPOLATE_LANCZOS);
  1445. }
  1446. img->convert(Image::FORMAT_RGB8);
  1447. // Save thumbnail directly, as thumbnailer may not update due to actual scene not changing md5.
  1448. String temp_path = EditorPaths::get_singleton()->get_cache_dir();
  1449. String cache_base = ProjectSettings::get_singleton()->globalize_path(p_file).md5_text();
  1450. cache_base = temp_path.path_join("resthumb-" + cache_base);
  1451. // Does not have it, try to load a cached thumbnail.
  1452. post_process_preview(img);
  1453. img->save_png(cache_base + ".png");
  1454. }
  1455. }
  1456. save.step(TTR("Saving Scene"), 4);
  1457. _save_scene(p_file, p_idx);
  1458. if (!singleton->cmdline_export_mode) {
  1459. EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
  1460. }
  1461. }
  1462. bool EditorNode::_validate_scene_recursive(const String &p_filename, Node *p_node) {
  1463. for (int i = 0; i < p_node->get_child_count(); i++) {
  1464. Node *child = p_node->get_child(i);
  1465. if (child->get_scene_file_path() == p_filename) {
  1466. return true;
  1467. }
  1468. if (_validate_scene_recursive(p_filename, child)) {
  1469. return true;
  1470. }
  1471. }
  1472. return false;
  1473. }
  1474. int EditorNode::_save_external_resources() {
  1475. // Save external resources and its subresources if any was modified.
  1476. int flg = 0;
  1477. if (EDITOR_GET("filesystem/on_save/compress_binary_resources")) {
  1478. flg |= ResourceSaver::FLAG_COMPRESS;
  1479. }
  1480. flg |= ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1481. HashSet<String> edited_resources;
  1482. int saved = 0;
  1483. List<Ref<Resource>> cached;
  1484. ResourceCache::get_cached_resources(&cached);
  1485. for (Ref<Resource> res : cached) {
  1486. if (!res->is_edited()) {
  1487. continue;
  1488. }
  1489. String path = res->get_path();
  1490. if (path.begins_with("res://")) {
  1491. int subres_pos = path.find("::");
  1492. if (subres_pos == -1) {
  1493. // Actual resource.
  1494. edited_resources.insert(path);
  1495. } else {
  1496. edited_resources.insert(path.substr(0, subres_pos));
  1497. }
  1498. }
  1499. res->set_edited(false);
  1500. }
  1501. for (const String &E : edited_resources) {
  1502. Ref<Resource> res = ResourceCache::get_ref(E);
  1503. if (!res.is_valid()) {
  1504. continue; // Maybe it was erased in a thread, who knows.
  1505. }
  1506. Ref<PackedScene> ps = res;
  1507. if (ps.is_valid()) {
  1508. continue; // Do not save PackedScenes, this will mess up the editor.
  1509. }
  1510. ResourceSaver::save(res, res->get_path(), flg);
  1511. saved++;
  1512. }
  1513. EditorUndoRedoManager::get_singleton()->set_history_as_saved(EditorUndoRedoManager::GLOBAL_HISTORY);
  1514. return saved;
  1515. }
  1516. static void _reset_animation_mixers(Node *p_node, List<Pair<AnimationMixer *, Ref<AnimatedValuesBackup>>> *r_anim_backups) {
  1517. for (int i = 0; i < p_node->get_child_count(); i++) {
  1518. AnimationMixer *mixer = Object::cast_to<AnimationMixer>(p_node->get_child(i));
  1519. if (mixer && mixer->is_active() && mixer->is_reset_on_save_enabled() && mixer->can_apply_reset()) {
  1520. AnimationTree *tree = Object::cast_to<AnimationTree>(p_node->get_child(i));
  1521. if (tree) {
  1522. AnimationPlayer *player = Object::cast_to<AnimationPlayer>(tree->get_node_or_null(tree->get_animation_player()));
  1523. if (player && player->is_active() && player->is_reset_on_save_enabled() && player->can_apply_reset()) {
  1524. continue; // Avoid to process reset/restore many times.
  1525. }
  1526. }
  1527. Ref<AnimatedValuesBackup> backup = mixer->apply_reset();
  1528. if (backup.is_valid()) {
  1529. Pair<AnimationMixer *, Ref<AnimatedValuesBackup>> pair;
  1530. pair.first = mixer;
  1531. pair.second = backup;
  1532. r_anim_backups->push_back(pair);
  1533. }
  1534. }
  1535. _reset_animation_mixers(p_node->get_child(i), r_anim_backups);
  1536. }
  1537. }
  1538. void EditorNode::_save_scene(String p_file, int idx) {
  1539. if (!saving_scene.is_empty() && saving_scene == p_file) {
  1540. return;
  1541. }
  1542. Node *scene = editor_data.get_edited_scene_root(idx);
  1543. if (!scene) {
  1544. show_accept(TTR("This operation can't be done without a tree root."), TTR("OK"));
  1545. return;
  1546. }
  1547. if (!scene->get_scene_file_path().is_empty() && _validate_scene_recursive(scene->get_scene_file_path(), scene)) {
  1548. show_accept(TTR("This scene can't be saved because there is a cyclic instance inclusion.\nPlease resolve it and then attempt to save again."), TTR("OK"));
  1549. return;
  1550. }
  1551. scene->propagate_notification(NOTIFICATION_EDITOR_PRE_SAVE);
  1552. editor_data.apply_changes_in_editors();
  1553. List<Pair<AnimationMixer *, Ref<AnimatedValuesBackup>>> anim_backups;
  1554. _reset_animation_mixers(scene, &anim_backups);
  1555. save_default_environment();
  1556. _save_editor_states(p_file, idx);
  1557. Ref<PackedScene> sdata;
  1558. if (ResourceCache::has(p_file)) {
  1559. // Something may be referencing this resource and we are good with that.
  1560. // We must update it, but also let the previous scene state go, as
  1561. // old version still work for referencing changes in instantiated or inherited scenes.
  1562. sdata = ResourceCache::get_ref(p_file);
  1563. if (sdata.is_valid()) {
  1564. sdata->recreate_state();
  1565. } else {
  1566. sdata.instantiate();
  1567. }
  1568. } else {
  1569. sdata.instantiate();
  1570. }
  1571. Error err = sdata->pack(scene);
  1572. if (err != OK) {
  1573. show_accept(TTR("Couldn't save scene. Likely dependencies (instances or inheritance) couldn't be satisfied."), TTR("OK"));
  1574. return;
  1575. }
  1576. int flg = 0;
  1577. if (EDITOR_GET("filesystem/on_save/compress_binary_resources")) {
  1578. flg |= ResourceSaver::FLAG_COMPRESS;
  1579. }
  1580. flg |= ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS;
  1581. err = ResourceSaver::save(sdata, p_file, flg);
  1582. // This needs to be emitted before saving external resources.
  1583. emit_signal(SNAME("scene_saved"), p_file);
  1584. editor_data.notify_scene_saved(p_file);
  1585. _save_external_resources();
  1586. saving_scene = p_file; // Some editors may save scenes of built-in resources as external data, so avoid saving this scene again.
  1587. editor_data.save_editor_external_data();
  1588. saving_scene = "";
  1589. for (Pair<AnimationMixer *, Ref<AnimatedValuesBackup>> &E : anim_backups) {
  1590. E.first->restore(E.second);
  1591. }
  1592. if (err == OK) {
  1593. scene->set_scene_file_path(ProjectSettings::get_singleton()->localize_path(p_file));
  1594. editor_data.set_scene_as_saved(idx);
  1595. editor_data.set_scene_modified_time(idx, FileAccess::get_modified_time(p_file));
  1596. editor_folding.save_scene_folding(scene, p_file);
  1597. _update_title();
  1598. scene_tabs->update_scene_tabs();
  1599. } else {
  1600. _dialog_display_save_error(p_file, err);
  1601. }
  1602. scene->propagate_notification(NOTIFICATION_EDITOR_POST_SAVE);
  1603. }
  1604. void EditorNode::save_all_scenes() {
  1605. project_run_bar->stop_playing();
  1606. _save_all_scenes();
  1607. }
  1608. void EditorNode::save_scene_if_open(const String &p_scene_path) {
  1609. int idx = editor_data.get_edited_scene_from_path(p_scene_path);
  1610. if (idx >= 0) {
  1611. _save_scene(p_scene_path, idx);
  1612. }
  1613. }
  1614. void EditorNode::save_scene_list(const HashSet<String> &p_scene_paths) {
  1615. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1616. Node *scene = editor_data.get_edited_scene_root(i);
  1617. if (scene && p_scene_paths.has(scene->get_scene_file_path())) {
  1618. _save_scene(scene->get_scene_file_path(), i);
  1619. }
  1620. }
  1621. }
  1622. void EditorNode::save_before_run() {
  1623. current_menu_option = FILE_SAVE_AND_RUN;
  1624. _menu_option_confirm(FILE_SAVE_AS_SCENE, true);
  1625. file->set_title(TTR("Save scene before running..."));
  1626. }
  1627. void EditorNode::try_autosave() {
  1628. if (!bool(EDITOR_GET("run/auto_save/save_before_running"))) {
  1629. return;
  1630. }
  1631. if (unsaved_cache) {
  1632. Node *scene = editor_data.get_edited_scene_root();
  1633. if (scene && !scene->get_scene_file_path().is_empty()) { // Only autosave if there is a scene and if it has a path.
  1634. _save_scene_with_preview(scene->get_scene_file_path());
  1635. }
  1636. }
  1637. _menu_option(FILE_SAVE_ALL_SCENES);
  1638. editor_data.save_editor_external_data();
  1639. }
  1640. void EditorNode::restart_editor() {
  1641. exiting = true;
  1642. if (project_run_bar->is_playing()) {
  1643. project_run_bar->stop_playing();
  1644. }
  1645. String to_reopen;
  1646. if (get_tree()->get_edited_scene_root()) {
  1647. to_reopen = get_tree()->get_edited_scene_root()->get_scene_file_path();
  1648. }
  1649. _exit_editor(EXIT_SUCCESS);
  1650. List<String> args;
  1651. for (const String &a : Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_TOOL)) {
  1652. args.push_back(a);
  1653. }
  1654. args.push_back("--path");
  1655. args.push_back(ProjectSettings::get_singleton()->get_resource_path());
  1656. args.push_back("-e");
  1657. if (!to_reopen.is_empty()) {
  1658. args.push_back(to_reopen);
  1659. }
  1660. OS::get_singleton()->set_restart_on_exit(true, args);
  1661. }
  1662. void EditorNode::_save_all_scenes() {
  1663. bool all_saved = true;
  1664. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1665. Node *scene = editor_data.get_edited_scene_root(i);
  1666. if (scene) {
  1667. if (!scene->get_scene_file_path().is_empty() && DirAccess::exists(scene->get_scene_file_path().get_base_dir())) {
  1668. if (i != editor_data.get_edited_scene()) {
  1669. _save_scene(scene->get_scene_file_path(), i);
  1670. } else {
  1671. _save_scene_with_preview(scene->get_scene_file_path());
  1672. }
  1673. } else if (!scene->get_scene_file_path().is_empty()) {
  1674. all_saved = false;
  1675. }
  1676. }
  1677. }
  1678. if (!all_saved) {
  1679. show_warning(TTR("Could not save one or more scenes!"), TTR("Save All Scenes"));
  1680. }
  1681. save_default_environment();
  1682. }
  1683. void EditorNode::_mark_unsaved_scenes() {
  1684. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1685. Node *node = editor_data.get_edited_scene_root(i);
  1686. if (!node) {
  1687. continue;
  1688. }
  1689. String path = node->get_scene_file_path();
  1690. if (!path.is_empty() && !FileAccess::exists(path)) {
  1691. // Mark scene tab as unsaved if the file is gone.
  1692. EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(editor_data.get_scene_history_id(i));
  1693. }
  1694. }
  1695. _update_title();
  1696. scene_tabs->update_scene_tabs();
  1697. }
  1698. void EditorNode::_dialog_action(String p_file) {
  1699. switch (current_menu_option) {
  1700. case FILE_NEW_INHERITED_SCENE: {
  1701. Node *scene = editor_data.get_edited_scene_root();
  1702. // If the previous scene is rootless, just close it in favor of the new one.
  1703. if (!scene) {
  1704. _menu_option_confirm(FILE_CLOSE, true);
  1705. }
  1706. load_scene(p_file, false, true);
  1707. } break;
  1708. case FILE_OPEN_SCENE: {
  1709. load_scene(p_file);
  1710. } break;
  1711. case SETTINGS_PICK_MAIN_SCENE: {
  1712. ProjectSettings::get_singleton()->set("application/run/main_scene", p_file);
  1713. ProjectSettings::get_singleton()->save();
  1714. // TODO: Would be nice to show the project manager opened with the highlighted field.
  1715. project_run_bar->play_main_scene((bool)pick_main_scene->get_meta("from_native", false));
  1716. } break;
  1717. case FILE_CLOSE:
  1718. case SCENE_TAB_CLOSE:
  1719. case FILE_SAVE_SCENE:
  1720. case FILE_SAVE_AS_SCENE: {
  1721. int scene_idx = (current_menu_option == FILE_SAVE_SCENE || current_menu_option == FILE_SAVE_AS_SCENE) ? -1 : tab_closing_idx;
  1722. if (file->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) {
  1723. bool same_open_scene = false;
  1724. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  1725. if (editor_data.get_scene_path(i) == p_file && i != scene_idx) {
  1726. same_open_scene = true;
  1727. }
  1728. }
  1729. if (same_open_scene) {
  1730. show_warning(TTR("Can't overwrite scene that is still open!"));
  1731. return;
  1732. }
  1733. save_default_environment();
  1734. _save_scene_with_preview(p_file, scene_idx);
  1735. _add_to_recent_scenes(p_file);
  1736. save_editor_layout_delayed();
  1737. if (scene_idx != -1) {
  1738. _discard_changes();
  1739. } else {
  1740. // Update the path of the edited scene to ensure later do/undo action history matches.
  1741. editor_data.set_scene_path(editor_data.get_edited_scene(), p_file);
  1742. }
  1743. }
  1744. } break;
  1745. case FILE_SAVE_AND_RUN: {
  1746. if (file->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) {
  1747. save_default_environment();
  1748. _save_scene_with_preview(p_file);
  1749. project_run_bar->play_custom_scene(p_file);
  1750. }
  1751. } break;
  1752. case FILE_SAVE_AND_RUN_MAIN_SCENE: {
  1753. ProjectSettings::get_singleton()->set("application/run/main_scene", p_file);
  1754. ProjectSettings::get_singleton()->save();
  1755. if (file->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) {
  1756. save_default_environment();
  1757. _save_scene_with_preview(p_file);
  1758. project_run_bar->play_main_scene((bool)pick_main_scene->get_meta("from_native", false));
  1759. }
  1760. } break;
  1761. case FILE_EXPORT_MESH_LIBRARY: {
  1762. const Dictionary &fd_options = file_export_lib->get_selected_options();
  1763. bool merge_with_existing_library = fd_options.get(TTR("Merge With Existing"), true);
  1764. bool apply_mesh_instance_transforms = fd_options.get(TTR("Apply MeshInstance Transforms"), false);
  1765. Ref<MeshLibrary> ml;
  1766. if (merge_with_existing_library && FileAccess::exists(p_file)) {
  1767. ml = ResourceLoader::load(p_file, "MeshLibrary");
  1768. if (ml.is_null()) {
  1769. show_accept(TTR("Can't load MeshLibrary for merging!"), TTR("OK"));
  1770. return;
  1771. }
  1772. }
  1773. if (ml.is_null()) {
  1774. ml = Ref<MeshLibrary>(memnew(MeshLibrary));
  1775. }
  1776. MeshLibraryEditor::update_library_file(editor_data.get_edited_scene_root(), ml, merge_with_existing_library, apply_mesh_instance_transforms);
  1777. Error err = ResourceSaver::save(ml, p_file);
  1778. if (err) {
  1779. show_accept(TTR("Error saving MeshLibrary!"), TTR("OK"));
  1780. return;
  1781. } else if (ResourceCache::has(p_file)) {
  1782. // Make sure MeshLibrary is updated in the editor.
  1783. ResourceLoader::load(p_file)->reload_from_file();
  1784. }
  1785. } break;
  1786. case RESOURCE_SAVE:
  1787. case RESOURCE_SAVE_AS: {
  1788. ERR_FAIL_COND(saving_resource.is_null());
  1789. save_resource_in_path(saving_resource, p_file);
  1790. saving_resource = Ref<Resource>();
  1791. ObjectID current_id = editor_history.get_current();
  1792. Object *current_obj = current_id.is_valid() ? ObjectDB::get_instance(current_id) : nullptr;
  1793. ERR_FAIL_NULL(current_obj);
  1794. current_obj->notify_property_list_changed();
  1795. } break;
  1796. case SETTINGS_LAYOUT_SAVE: {
  1797. if (p_file.is_empty()) {
  1798. return;
  1799. }
  1800. Ref<ConfigFile> config;
  1801. config.instantiate();
  1802. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  1803. if (err == ERR_FILE_CANT_OPEN || err == ERR_FILE_NOT_FOUND) {
  1804. config.instantiate();
  1805. } else if (err != OK) {
  1806. show_warning(TTR("An error occurred while trying to save the editor layout.\nMake sure the editor's user data path is writable."));
  1807. return;
  1808. }
  1809. editor_dock_manager->save_docks_to_config(config, p_file);
  1810. config->save(EditorSettings::get_singleton()->get_editor_layouts_config());
  1811. layout_dialog->hide();
  1812. _update_layouts_menu();
  1813. if (p_file == "Default") {
  1814. 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."));
  1815. }
  1816. } break;
  1817. case SETTINGS_LAYOUT_DELETE: {
  1818. Ref<ConfigFile> config;
  1819. config.instantiate();
  1820. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  1821. if (err != OK || !config->has_section(p_file)) {
  1822. show_warning(TTR("Layout name not found!"));
  1823. return;
  1824. }
  1825. // Erase key values.
  1826. List<String> keys;
  1827. config->get_section_keys(p_file, &keys);
  1828. for (const String &key : keys) {
  1829. config->set_value(p_file, key, Variant());
  1830. }
  1831. config->save(EditorSettings::get_singleton()->get_editor_layouts_config());
  1832. layout_dialog->hide();
  1833. _update_layouts_menu();
  1834. if (p_file == "Default") {
  1835. show_warning(TTR("Restored the Default layout to its base settings."));
  1836. }
  1837. } break;
  1838. default: {
  1839. // Save scene?
  1840. if (file->get_file_mode() == EditorFileDialog::FILE_MODE_SAVE_FILE) {
  1841. _save_scene_with_preview(p_file);
  1842. }
  1843. } break;
  1844. }
  1845. }
  1846. bool EditorNode::_is_class_editor_disabled_by_feature_profile(const StringName &p_class) {
  1847. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  1848. if (profile.is_null()) {
  1849. return false;
  1850. }
  1851. StringName class_name = p_class;
  1852. while (class_name != StringName()) {
  1853. if (profile->is_class_disabled(class_name)) {
  1854. return true;
  1855. }
  1856. if (profile->is_class_editor_disabled(class_name)) {
  1857. return true;
  1858. }
  1859. class_name = ClassDB::get_parent_class(class_name);
  1860. }
  1861. return false;
  1862. }
  1863. void EditorNode::edit_item(Object *p_object, Object *p_editing_owner) {
  1864. ERR_FAIL_NULL(p_editing_owner);
  1865. // Editing for this type of object may be disabled by user's feature profile.
  1866. if (!p_object || _is_class_editor_disabled_by_feature_profile(p_object->get_class())) {
  1867. // Nothing to edit, clean up the owner context and return.
  1868. hide_unused_editors(p_editing_owner);
  1869. return;
  1870. }
  1871. // Get a list of editor plugins that can handle this type of object.
  1872. Vector<EditorPlugin *> available_plugins = editor_data.get_handling_sub_editors(p_object);
  1873. if (available_plugins.is_empty()) {
  1874. // None, clean up the owner context and return.
  1875. hide_unused_editors(p_editing_owner);
  1876. return;
  1877. }
  1878. ObjectID owner_id = p_editing_owner->get_instance_id();
  1879. // Remove editor plugins no longer used by this editing owner. Keep the ones that can
  1880. // still be reused by the new edited object.
  1881. List<EditorPlugin *> to_remove;
  1882. for (EditorPlugin *plugin : active_plugins[owner_id]) {
  1883. if (!available_plugins.has(plugin)) {
  1884. to_remove.push_back(plugin);
  1885. if (plugin->can_auto_hide()) {
  1886. _plugin_over_edit(plugin, nullptr);
  1887. } else {
  1888. // If plugin can't be hidden, make it own itself and become responsible for closing.
  1889. _plugin_over_self_own(plugin);
  1890. }
  1891. }
  1892. }
  1893. for (EditorPlugin *plugin : to_remove) {
  1894. active_plugins[owner_id].erase(plugin);
  1895. }
  1896. // Send the edited object to the plugins.
  1897. for (EditorPlugin *plugin : available_plugins) {
  1898. if (active_plugins[owner_id].has(plugin)) {
  1899. // Plugin was already active, just change the object.
  1900. plugin->edit(p_object);
  1901. continue;
  1902. }
  1903. if (active_plugins.has(plugin->get_instance_id())) {
  1904. // Plugin is already active, but as self-owning, so it needs a separate check.
  1905. plugin->edit(p_object);
  1906. continue;
  1907. }
  1908. // If plugin is already associated with another owner, remove it from there first.
  1909. for (KeyValue<ObjectID, HashSet<EditorPlugin *>> &kv : active_plugins) {
  1910. if (kv.key != owner_id) {
  1911. EditorPropertyResource *epres = Object::cast_to<EditorPropertyResource>(ObjectDB::get_instance(kv.key));
  1912. if (epres && kv.value.has(plugin)) {
  1913. // If it's resource property editing the same resource type, fold it.
  1914. epres->fold_resource();
  1915. }
  1916. kv.value.erase(plugin);
  1917. }
  1918. }
  1919. // Activate previously inactive plugin and edit the object.
  1920. active_plugins[owner_id].insert(plugin);
  1921. _plugin_over_edit(plugin, p_object);
  1922. }
  1923. }
  1924. void EditorNode::push_node_item(Node *p_node) {
  1925. if (p_node || Object::cast_to<Node>(InspectorDock::get_inspector_singleton()->get_edited_object()) || Object::cast_to<MultiNodeEdit>(InspectorDock::get_inspector_singleton()->get_edited_object())) {
  1926. // Don't push null if the currently edited object is not a Node.
  1927. push_item(p_node);
  1928. }
  1929. }
  1930. void EditorNode::push_item(Object *p_object, const String &p_property, bool p_inspector_only) {
  1931. if (!p_object) {
  1932. InspectorDock::get_inspector_singleton()->edit(nullptr);
  1933. NodeDock::get_singleton()->set_node(nullptr);
  1934. SceneTreeDock::get_singleton()->set_selected(nullptr);
  1935. InspectorDock::get_singleton()->update(nullptr);
  1936. hide_unused_editors();
  1937. return;
  1938. }
  1939. _add_to_history(p_object, p_property, p_inspector_only);
  1940. _edit_current();
  1941. }
  1942. void EditorNode::push_item_no_inspector(Object *p_object) {
  1943. _add_to_history(p_object, "", false);
  1944. _edit_current(false, true);
  1945. }
  1946. void EditorNode::save_default_environment() {
  1947. Ref<Environment> fallback = get_tree()->get_root()->get_world_3d()->get_fallback_environment();
  1948. if (fallback.is_valid() && fallback->get_path().is_resource_file()) {
  1949. HashMap<Ref<Resource>, bool> processed;
  1950. _find_and_save_edited_subresources(fallback.ptr(), processed, 0);
  1951. save_resource_in_path(fallback, fallback->get_path());
  1952. }
  1953. }
  1954. void EditorNode::hide_unused_editors(const Object *p_editing_owner) {
  1955. if (p_editing_owner) {
  1956. const ObjectID id = p_editing_owner->get_instance_id();
  1957. for (EditorPlugin *plugin : active_plugins[id]) {
  1958. if (plugin->can_auto_hide()) {
  1959. _plugin_over_edit(plugin, nullptr);
  1960. } else {
  1961. _plugin_over_self_own(plugin);
  1962. }
  1963. }
  1964. active_plugins.erase(id);
  1965. } else {
  1966. // If no editing owner is provided, this method will go over all owners and check if they are valid.
  1967. // This is to sweep properties that were removed from the inspector.
  1968. List<ObjectID> to_remove;
  1969. for (KeyValue<ObjectID, HashSet<EditorPlugin *>> &kv : active_plugins) {
  1970. const Object *context = ObjectDB::get_instance(kv.key);
  1971. if (context) {
  1972. // In case of self-owning plugins, they are disabled here if they can auto hide.
  1973. const EditorPlugin *self_owning = Object::cast_to<EditorPlugin>(context);
  1974. if (self_owning && self_owning->can_auto_hide()) {
  1975. context = nullptr;
  1976. }
  1977. }
  1978. if (!context) {
  1979. to_remove.push_back(kv.key);
  1980. for (EditorPlugin *plugin : kv.value) {
  1981. if (plugin->can_auto_hide()) {
  1982. _plugin_over_edit(plugin, nullptr);
  1983. } else {
  1984. _plugin_over_self_own(plugin);
  1985. }
  1986. }
  1987. }
  1988. }
  1989. for (const ObjectID &id : to_remove) {
  1990. active_plugins.erase(id);
  1991. }
  1992. }
  1993. }
  1994. static bool overrides_external_editor(Object *p_object) {
  1995. Script *script = Object::cast_to<Script>(p_object);
  1996. if (!script) {
  1997. return false;
  1998. }
  1999. return script->get_language()->overrides_external_editor();
  2000. }
  2001. void EditorNode::_add_to_history(const Object *p_object, const String &p_property, bool p_inspector_only) {
  2002. ObjectID id = p_object->get_instance_id();
  2003. ObjectID history_id = editor_history.get_current();
  2004. if (id != history_id) {
  2005. const MultiNodeEdit *multi_node_edit = Object::cast_to<const MultiNodeEdit>(p_object);
  2006. const MultiNodeEdit *history_multi_node_edit = Object::cast_to<const MultiNodeEdit>(ObjectDB::get_instance(history_id));
  2007. if (multi_node_edit && history_multi_node_edit && multi_node_edit->is_same_selection(history_multi_node_edit)) {
  2008. return;
  2009. }
  2010. if (p_inspector_only) {
  2011. editor_history.add_object(id, String(), true);
  2012. } else if (p_property.is_empty()) {
  2013. editor_history.add_object(id);
  2014. } else {
  2015. editor_history.add_object(id, p_property);
  2016. }
  2017. }
  2018. }
  2019. void EditorNode::_edit_current(bool p_skip_foreign, bool p_skip_inspector_update) {
  2020. ObjectID current_id = editor_history.get_current();
  2021. Object *current_obj = current_id.is_valid() ? ObjectDB::get_instance(current_id) : nullptr;
  2022. Ref<Resource> res = Object::cast_to<Resource>(current_obj);
  2023. if (p_skip_foreign && res.is_valid()) {
  2024. const int current_tab = scene_tabs->get_current_tab();
  2025. if (res->get_path().contains("::") && res->get_path().get_slice("::", 0) != editor_data.get_scene_path(current_tab)) {
  2026. // Trying to edit resource that belongs to another scene; abort.
  2027. current_obj = nullptr;
  2028. }
  2029. }
  2030. bool inspector_only = editor_history.is_current_inspector_only();
  2031. current = current_obj;
  2032. if (!current_obj) {
  2033. SceneTreeDock::get_singleton()->set_selected(nullptr);
  2034. InspectorDock::get_inspector_singleton()->edit(nullptr);
  2035. NodeDock::get_singleton()->set_node(nullptr);
  2036. InspectorDock::get_singleton()->update(nullptr);
  2037. hide_unused_editors();
  2038. return;
  2039. }
  2040. // Update the use folding setting and state.
  2041. bool disable_folding = bool(EDITOR_GET("interface/inspector/disable_folding")) || current_obj->is_class("EditorDebuggerRemoteObject");
  2042. if (InspectorDock::get_inspector_singleton()->is_using_folding() == disable_folding) {
  2043. InspectorDock::get_inspector_singleton()->set_use_folding(!disable_folding, false);
  2044. }
  2045. bool is_resource = current_obj->is_class("Resource");
  2046. bool is_node = current_obj->is_class("Node");
  2047. bool stay_in_script_editor_on_node_selected = bool(EDITOR_GET("text_editor/behavior/navigation/stay_in_script_editor_on_node_selected"));
  2048. bool skip_main_plugin = false;
  2049. String editable_info; // None by default.
  2050. bool info_is_warning = false;
  2051. if (current_obj->has_method("_is_read_only")) {
  2052. if (current_obj->call("_is_read_only")) {
  2053. editable_info = TTR("This object is marked as read-only, so it's not editable.");
  2054. }
  2055. }
  2056. if (is_resource) {
  2057. Resource *current_res = Object::cast_to<Resource>(current_obj);
  2058. ERR_FAIL_NULL(current_res);
  2059. if (!p_skip_inspector_update) {
  2060. InspectorDock::get_inspector_singleton()->edit(current_res);
  2061. SceneTreeDock::get_singleton()->set_selected(nullptr);
  2062. NodeDock::get_singleton()->set_node(nullptr);
  2063. InspectorDock::get_singleton()->update(nullptr);
  2064. ImportDock::get_singleton()->set_edit_path(current_res->get_path());
  2065. }
  2066. int subr_idx = current_res->get_path().find("::");
  2067. if (subr_idx != -1) {
  2068. String base_path = current_res->get_path().substr(0, subr_idx);
  2069. if (FileAccess::exists(base_path + ".import")) {
  2070. if (!base_path.is_resource_file()) {
  2071. if (get_edited_scene() && get_edited_scene()->get_scene_file_path() == base_path) {
  2072. info_is_warning = true;
  2073. }
  2074. }
  2075. editable_info = 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.");
  2076. } else if ((!get_edited_scene() || get_edited_scene()->get_scene_file_path() != base_path) && ResourceLoader::get_resource_type(base_path) == "PackedScene") {
  2077. editable_info = TTR("This resource belongs to a scene that was instantiated or inherited.\nChanges to it must be made inside the original scene.");
  2078. }
  2079. } else if (current_res->get_path().is_resource_file()) {
  2080. if (FileAccess::exists(current_res->get_path() + ".import")) {
  2081. editable_info = TTR("This resource was imported, so it's not editable. Change its settings in the import panel and then re-import.");
  2082. }
  2083. }
  2084. } else if (is_node) {
  2085. Node *current_node = Object::cast_to<Node>(current_obj);
  2086. ERR_FAIL_NULL(current_node);
  2087. InspectorDock::get_inspector_singleton()->edit(current_node);
  2088. if (current_node->is_inside_tree()) {
  2089. NodeDock::get_singleton()->set_node(current_node);
  2090. SceneTreeDock::get_singleton()->set_selected(current_node);
  2091. SceneTreeDock::get_singleton()->set_selection({ current_node });
  2092. InspectorDock::get_singleton()->update(current_node);
  2093. if (!inspector_only && !skip_main_plugin) {
  2094. skip_main_plugin = stay_in_script_editor_on_node_selected && !ScriptEditor::get_singleton()->is_editor_floating() && ScriptEditor::get_singleton()->is_visible_in_tree();
  2095. }
  2096. } else {
  2097. NodeDock::get_singleton()->set_node(nullptr);
  2098. SceneTreeDock::get_singleton()->set_selected(nullptr);
  2099. InspectorDock::get_singleton()->update(nullptr);
  2100. }
  2101. if (get_edited_scene() && !get_edited_scene()->get_scene_file_path().is_empty()) {
  2102. String source_scene = get_edited_scene()->get_scene_file_path();
  2103. if (FileAccess::exists(source_scene + ".import")) {
  2104. editable_info = TTR("This scene was imported, so changes to it won't be kept.\nInstantiating or inheriting it will allow you to make changes to it.\nPlease read the documentation relevant to importing scenes to better understand this workflow.");
  2105. info_is_warning = true;
  2106. }
  2107. }
  2108. } else {
  2109. Node *selected_node = nullptr;
  2110. Vector<Node *> multi_nodes;
  2111. if (current_obj->is_class("MultiNodeEdit")) {
  2112. Node *scene = get_edited_scene();
  2113. if (scene) {
  2114. MultiNodeEdit *multi_node_edit = Object::cast_to<MultiNodeEdit>(current_obj);
  2115. int node_count = multi_node_edit->get_node_count();
  2116. if (node_count > 0) {
  2117. for (int node_index = 0; node_index < node_count; ++node_index) {
  2118. Node *node = scene->get_node(multi_node_edit->get_node(node_index));
  2119. if (node) {
  2120. multi_nodes.push_back(node);
  2121. }
  2122. }
  2123. if (!multi_nodes.is_empty()) {
  2124. // Pick the top-most node.
  2125. multi_nodes.sort_custom<Node::Comparator>();
  2126. selected_node = multi_nodes[0];
  2127. }
  2128. }
  2129. }
  2130. }
  2131. InspectorDock::get_inspector_singleton()->edit(current_obj);
  2132. NodeDock::get_singleton()->set_node(nullptr);
  2133. SceneTreeDock::get_singleton()->set_selected(selected_node);
  2134. SceneTreeDock::get_singleton()->set_selection(multi_nodes);
  2135. InspectorDock::get_singleton()->update(nullptr);
  2136. }
  2137. InspectorDock::get_singleton()->set_info(
  2138. info_is_warning ? TTR("Changes may be lost!") : TTR("This object is read-only."),
  2139. editable_info,
  2140. info_is_warning);
  2141. Object *editor_owner = (is_node || current_obj->is_class("MultiNodeEdit")) ? (Object *)SceneTreeDock::get_singleton() : is_resource ? (Object *)InspectorDock::get_inspector_singleton()
  2142. : (Object *)this;
  2143. // Take care of the main editor plugin.
  2144. if (!inspector_only) {
  2145. EditorPlugin *main_plugin = editor_data.get_handling_main_editor(current_obj);
  2146. int plugin_index = 0;
  2147. for (; plugin_index < editor_table.size(); plugin_index++) {
  2148. if (editor_table[plugin_index] == main_plugin) {
  2149. if (!main_editor_buttons[plugin_index]->is_visible()) {
  2150. main_plugin = nullptr; // If button is not visible, then no plugin is active.
  2151. }
  2152. break;
  2153. }
  2154. }
  2155. ObjectID editor_owner_id = editor_owner->get_instance_id();
  2156. if (main_plugin && !skip_main_plugin) {
  2157. // Special case if use of external editor is true.
  2158. Resource *current_res = Object::cast_to<Resource>(current_obj);
  2159. if (main_plugin->get_name() == "Script" && current_res && !current_res->is_built_in() && (bool(EDITOR_GET("text_editor/external/use_external_editor")) || overrides_external_editor(current_obj))) {
  2160. if (!changing_scene) {
  2161. main_plugin->edit(current_obj);
  2162. }
  2163. }
  2164. else if (main_plugin != editor_plugin_screen && (!ScriptEditor::get_singleton() || !ScriptEditor::get_singleton()->is_visible_in_tree() || ScriptEditor::get_singleton()->can_take_away_focus())) {
  2165. // Unedit previous plugin.
  2166. editor_plugin_screen->edit(nullptr);
  2167. active_plugins[editor_owner_id].erase(editor_plugin_screen);
  2168. // Update screen main_plugin.
  2169. editor_select(plugin_index);
  2170. main_plugin->edit(current_obj);
  2171. } else {
  2172. editor_plugin_screen->edit(current_obj);
  2173. }
  2174. is_main_screen_editing = true;
  2175. } else if (!main_plugin && editor_plugin_screen && is_main_screen_editing) {
  2176. editor_plugin_screen->edit(nullptr);
  2177. is_main_screen_editing = false;
  2178. }
  2179. edit_item(current_obj, editor_owner);
  2180. }
  2181. InspectorDock::get_singleton()->update(current_obj);
  2182. }
  2183. void EditorNode::_android_build_source_selected(const String &p_file) {
  2184. export_template_manager->install_android_template_from_file(p_file, android_export_preset);
  2185. }
  2186. void EditorNode::_android_export_preset_selected(int p_index) {
  2187. if (p_index >= 0) {
  2188. android_export_preset = EditorExport::get_singleton()->get_export_preset(choose_android_export_profile->get_item_id(p_index));
  2189. } else {
  2190. android_export_preset.unref();
  2191. }
  2192. install_android_build_template_message->set_text(vformat(TTR(INSTALL_ANDROID_BUILD_TEMPLATE_MESSAGE), export_template_manager->get_android_build_directory(android_export_preset)));
  2193. }
  2194. void EditorNode::_menu_option_confirm(int p_option, bool p_confirmed) {
  2195. if (!p_confirmed) { // FIXME: this may be a hack.
  2196. current_menu_option = (MenuOptions)p_option;
  2197. }
  2198. switch (p_option) {
  2199. case FILE_NEW_SCENE: {
  2200. new_scene();
  2201. } break;
  2202. case FILE_NEW_INHERITED_SCENE:
  2203. case FILE_OPEN_SCENE: {
  2204. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  2205. List<String> extensions;
  2206. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  2207. file->clear_filters();
  2208. for (const String &extension : extensions) {
  2209. file->add_filter("*." + extension, extension.to_upper());
  2210. }
  2211. Node *scene = editor_data.get_edited_scene_root();
  2212. if (scene) {
  2213. file->set_current_path(scene->get_scene_file_path());
  2214. };
  2215. file->set_title(p_option == FILE_OPEN_SCENE ? TTR("Open Scene") : TTR("Open Base Scene"));
  2216. file->popup_file_dialog();
  2217. } break;
  2218. case FILE_QUICK_OPEN: {
  2219. quick_open->popup_dialog("Resource", true);
  2220. quick_open->set_title(TTR("Quick Open..."));
  2221. } break;
  2222. case FILE_QUICK_OPEN_SCENE: {
  2223. quick_open->popup_dialog("PackedScene", true);
  2224. quick_open->set_title(TTR("Quick Open Scene..."));
  2225. } break;
  2226. case FILE_QUICK_OPEN_SCRIPT: {
  2227. quick_open->popup_dialog("Script", true);
  2228. quick_open->set_title(TTR("Quick Open Script..."));
  2229. } break;
  2230. case FILE_OPEN_PREV: {
  2231. if (previous_scenes.is_empty()) {
  2232. break;
  2233. }
  2234. opening_prev = true;
  2235. open_request(previous_scenes.back()->get());
  2236. previous_scenes.pop_back();
  2237. } break;
  2238. case FILE_CLOSE_OTHERS: {
  2239. tab_closing_menu_option = -1;
  2240. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  2241. if (i == editor_data.get_edited_scene()) {
  2242. continue;
  2243. }
  2244. tabs_to_close.push_back(editor_data.get_scene_path(i));
  2245. }
  2246. _proceed_closing_scene_tabs();
  2247. } break;
  2248. case FILE_CLOSE_RIGHT: {
  2249. tab_closing_menu_option = -1;
  2250. for (int i = editor_data.get_edited_scene() + 1; i < editor_data.get_edited_scene_count(); i++) {
  2251. tabs_to_close.push_back(editor_data.get_scene_path(i));
  2252. }
  2253. _proceed_closing_scene_tabs();
  2254. } break;
  2255. case FILE_CLOSE_ALL: {
  2256. tab_closing_menu_option = -1;
  2257. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  2258. tabs_to_close.push_back(editor_data.get_scene_path(i));
  2259. }
  2260. _proceed_closing_scene_tabs();
  2261. } break;
  2262. case FILE_CLOSE: {
  2263. _scene_tab_closed(editor_data.get_edited_scene());
  2264. } break;
  2265. case FILE_SAVE_SCENE_SILENTLY: {
  2266. // Save scene without displaying progress dialog. Used to work around
  2267. // errors about parent node being busy setting up children
  2268. // when Save on Focus Loss kicks in.
  2269. Node *scene = editor_data.get_edited_scene_root();
  2270. if (scene && !scene->get_scene_file_path().is_empty() && DirAccess::exists(scene->get_scene_file_path().get_base_dir())) {
  2271. _save_scene(scene->get_scene_file_path());
  2272. save_editor_layout_delayed();
  2273. }
  2274. } break;
  2275. case SCENE_TAB_CLOSE:
  2276. case FILE_SAVE_SCENE: {
  2277. int scene_idx = (p_option == FILE_SAVE_SCENE) ? -1 : tab_closing_idx;
  2278. Node *scene = editor_data.get_edited_scene_root(scene_idx);
  2279. if (scene && !scene->get_scene_file_path().is_empty()) {
  2280. if (DirAccess::exists(scene->get_scene_file_path().get_base_dir())) {
  2281. if (scene_idx != editor_data.get_edited_scene()) {
  2282. _save_scene_with_preview(scene->get_scene_file_path(), scene_idx);
  2283. } else {
  2284. _save_scene_with_preview(scene->get_scene_file_path());
  2285. }
  2286. if (scene_idx != -1) {
  2287. _discard_changes();
  2288. }
  2289. save_editor_layout_delayed();
  2290. } else {
  2291. show_save_accept(vformat(TTR("%s no longer exists! Please specify a new save location."), scene->get_scene_file_path().get_base_dir()), TTR("OK"));
  2292. }
  2293. break;
  2294. }
  2295. [[fallthrough]];
  2296. }
  2297. case FILE_SAVE_AS_SCENE: {
  2298. int scene_idx = (p_option == FILE_SAVE_SCENE || p_option == FILE_SAVE_AS_SCENE) ? -1 : tab_closing_idx;
  2299. Node *scene = editor_data.get_edited_scene_root(scene_idx);
  2300. if (!scene) {
  2301. if (p_option == FILE_SAVE_SCENE) {
  2302. // Pressing Ctrl + S saves the current script if a scene is currently open, but it won't if the scene has no root node.
  2303. // Work around this by explicitly saving the script in this case (similar to pressing Ctrl + Alt + S).
  2304. ScriptEditor::get_singleton()->save_current_script();
  2305. }
  2306. const int saved = _save_external_resources();
  2307. if (saved > 0) {
  2308. show_accept(
  2309. vformat(TTR("The current scene has no root node, but %d modified external resource(s) were saved anyway."), saved),
  2310. TTR("OK"));
  2311. } else if (p_option == FILE_SAVE_AS_SCENE) {
  2312. // Don't show this dialog when pressing Ctrl + S to avoid interfering with script saving.
  2313. show_accept(
  2314. TTR("A root node is required to save the scene. You can add a root node using the Scene tree dock."),
  2315. TTR("OK"));
  2316. }
  2317. break;
  2318. }
  2319. file->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  2320. List<String> extensions;
  2321. Ref<PackedScene> sd = memnew(PackedScene);
  2322. ResourceSaver::get_recognized_extensions(sd, &extensions);
  2323. file->clear_filters();
  2324. for (const String &extension : extensions) {
  2325. file->add_filter("*." + extension, extension.to_upper());
  2326. }
  2327. if (!scene->get_scene_file_path().is_empty()) {
  2328. String path = scene->get_scene_file_path();
  2329. file->set_current_path(path);
  2330. if (extensions.size()) {
  2331. String ext = path.get_extension().to_lower();
  2332. if (extensions.find(ext) == nullptr) {
  2333. file->set_current_path(path.replacen("." + ext, "." + extensions.front()->get()));
  2334. }
  2335. }
  2336. } else if (extensions.size()) {
  2337. String root_name = scene->get_name();
  2338. root_name = EditorNode::adjust_scene_name_casing(root_name);
  2339. file->set_current_path(root_name + "." + extensions.front()->get().to_lower());
  2340. }
  2341. file->set_title(TTR("Save Scene As..."));
  2342. file->popup_file_dialog();
  2343. } break;
  2344. case FILE_SAVE_ALL_SCENES: {
  2345. _save_all_scenes();
  2346. } break;
  2347. case FILE_RUN_SCENE: {
  2348. project_run_bar->play_current_scene();
  2349. } break;
  2350. case FILE_EXPORT_PROJECT: {
  2351. project_export->popup_export();
  2352. } break;
  2353. case FILE_EXTERNAL_OPEN_SCENE: {
  2354. if (unsaved_cache && !p_confirmed) {
  2355. confirmation->set_ok_button_text(TTR("Open"));
  2356. confirmation->set_text(TTR("Current scene not saved. Open anyway?"));
  2357. confirmation->popup_centered();
  2358. break;
  2359. }
  2360. bool oprev = opening_prev;
  2361. Error err = load_scene(external_file);
  2362. if (err == OK && oprev) {
  2363. previous_scenes.pop_back();
  2364. opening_prev = false;
  2365. }
  2366. } break;
  2367. case EDIT_UNDO: {
  2368. if ((int)Input::get_singleton()->get_mouse_button_mask() & 0x7) {
  2369. log->add_message(TTR("Can't undo while mouse buttons are pressed."), EditorLog::MSG_TYPE_EDITOR);
  2370. } else {
  2371. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2372. String action = undo_redo->get_current_action_name();
  2373. int id = undo_redo->get_current_action_history_id();
  2374. if (!undo_redo->undo()) {
  2375. log->add_message(TTR("Nothing to undo."), EditorLog::MSG_TYPE_EDITOR);
  2376. } else if (!action.is_empty()) {
  2377. switch (id) {
  2378. case EditorUndoRedoManager::GLOBAL_HISTORY:
  2379. log->add_message(vformat(TTR("Global Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2380. break;
  2381. case EditorUndoRedoManager::REMOTE_HISTORY:
  2382. log->add_message(vformat(TTR("Remote Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2383. break;
  2384. default:
  2385. log->add_message(vformat(TTR("Scene Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2386. }
  2387. }
  2388. }
  2389. } break;
  2390. case EDIT_REDO: {
  2391. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2392. if ((int)Input::get_singleton()->get_mouse_button_mask() & 0x7) {
  2393. log->add_message(TTR("Can't redo while mouse buttons are pressed."), EditorLog::MSG_TYPE_EDITOR);
  2394. } else {
  2395. if (!undo_redo->redo()) {
  2396. log->add_message(TTR("Nothing to redo."), EditorLog::MSG_TYPE_EDITOR);
  2397. } else {
  2398. String action = undo_redo->get_current_action_name();
  2399. if (action.is_empty()) {
  2400. break;
  2401. }
  2402. switch (undo_redo->get_current_action_history_id()) {
  2403. case EditorUndoRedoManager::GLOBAL_HISTORY:
  2404. log->add_message(vformat(TTR("Global Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2405. break;
  2406. case EditorUndoRedoManager::REMOTE_HISTORY:
  2407. log->add_message(vformat(TTR("Remote Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2408. break;
  2409. default:
  2410. log->add_message(vformat(TTR("Scene Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  2411. }
  2412. }
  2413. }
  2414. } break;
  2415. case EDIT_RELOAD_SAVED_SCENE: {
  2416. Node *scene = get_edited_scene();
  2417. if (!scene) {
  2418. break;
  2419. }
  2420. String filename = scene->get_scene_file_path();
  2421. if (filename.is_empty()) {
  2422. show_warning(TTR("Can't reload a scene that was never saved."));
  2423. break;
  2424. }
  2425. if (unsaved_cache && !p_confirmed) {
  2426. confirmation->set_ok_button_text(TTR("Reload Saved Scene"));
  2427. confirmation->set_text(
  2428. TTR("The current scene has unsaved changes.\nReload the saved scene anyway? This action cannot be undone."));
  2429. confirmation->popup_centered();
  2430. break;
  2431. }
  2432. int cur_idx = editor_data.get_edited_scene();
  2433. _remove_edited_scene();
  2434. Error err = load_scene(filename);
  2435. if (err != OK) {
  2436. ERR_PRINT("Failed to load scene");
  2437. }
  2438. editor_data.move_edited_scene_to_index(cur_idx);
  2439. EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id());
  2440. scene_tabs->set_current_tab(cur_idx);
  2441. } break;
  2442. case FILE_SHOW_IN_FILESYSTEM: {
  2443. String path = editor_data.get_scene_path(editor_data.get_edited_scene());
  2444. if (!path.is_empty()) {
  2445. FileSystemDock::get_singleton()->navigate_to_path(path);
  2446. }
  2447. } break;
  2448. case RUN_SETTINGS: {
  2449. project_settings_editor->popup_project_settings();
  2450. } break;
  2451. case FILE_INSTALL_ANDROID_SOURCE: {
  2452. if (p_confirmed) {
  2453. if (export_template_manager->is_android_template_installed(android_export_preset)) {
  2454. remove_android_build_template->set_text(vformat(TTR(REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE), export_template_manager->get_android_build_directory(android_export_preset)));
  2455. remove_android_build_template->popup_centered();
  2456. } else if (!export_template_manager->can_install_android_template(android_export_preset)) {
  2457. gradle_build_manage_templates->popup_centered();
  2458. } else {
  2459. export_template_manager->install_android_template(android_export_preset);
  2460. }
  2461. } else {
  2462. bool has_custom_gradle_build = false;
  2463. choose_android_export_profile->clear();
  2464. for (int i = 0; i < EditorExport::get_singleton()->get_export_preset_count(); i++) {
  2465. Ref<EditorExportPreset> export_preset = EditorExport::get_singleton()->get_export_preset(i);
  2466. if (export_preset->get_platform()->get_class_name() == "EditorExportPlatformAndroid" && (bool)export_preset->get("gradle_build/use_gradle_build")) {
  2467. choose_android_export_profile->add_item(export_preset->get_name(), i);
  2468. String gradle_build_directory = export_preset->get("gradle_build/gradle_build_directory");
  2469. String android_source_template = export_preset->get("gradle_build/android_source_template");
  2470. if (!android_source_template.is_empty() || (gradle_build_directory != "" && gradle_build_directory != "res://android")) {
  2471. has_custom_gradle_build = true;
  2472. }
  2473. }
  2474. }
  2475. _android_export_preset_selected(choose_android_export_profile->get_item_count() >= 1 ? 0 : -1);
  2476. if (choose_android_export_profile->get_item_count() > 1 && has_custom_gradle_build) {
  2477. // If there's multiple options and at least one of them uses a custom gradle build then prompt the user to choose.
  2478. choose_android_export_profile->show();
  2479. install_android_build_template->popup_centered();
  2480. } else {
  2481. choose_android_export_profile->hide();
  2482. if (export_template_manager->is_android_template_installed(android_export_preset)) {
  2483. remove_android_build_template->set_text(vformat(TTR(REMOVE_ANDROID_BUILD_TEMPLATE_MESSAGE), export_template_manager->get_android_build_directory(android_export_preset)));
  2484. remove_android_build_template->popup_centered();
  2485. } else if (export_template_manager->can_install_android_template(android_export_preset)) {
  2486. install_android_build_template->popup_centered();
  2487. } else {
  2488. gradle_build_manage_templates->popup_centered();
  2489. }
  2490. }
  2491. }
  2492. } break;
  2493. case RUN_USER_DATA_FOLDER: {
  2494. // 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.
  2495. OS::get_singleton()->ensure_user_data_dir();
  2496. OS::get_singleton()->shell_show_in_file_manager(OS::get_singleton()->get_user_data_dir(), true);
  2497. } break;
  2498. case FILE_EXPLORE_ANDROID_BUILD_TEMPLATES: {
  2499. OS::get_singleton()->shell_show_in_file_manager(ProjectSettings::get_singleton()->globalize_path(export_template_manager->get_android_build_directory(android_export_preset).get_base_dir()), true);
  2500. } break;
  2501. case FILE_QUIT:
  2502. case RUN_PROJECT_MANAGER:
  2503. case RELOAD_CURRENT_PROJECT: {
  2504. if (p_confirmed && plugin_to_save) {
  2505. plugin_to_save->save_external_data();
  2506. p_confirmed = false;
  2507. }
  2508. if (!p_confirmed) {
  2509. bool save_each = EDITOR_GET("interface/editor/save_each_scene_on_quit");
  2510. if (_next_unsaved_scene(!save_each) == -1) {
  2511. if (EditorUndoRedoManager::get_singleton()->is_history_unsaved(EditorUndoRedoManager::GLOBAL_HISTORY)) {
  2512. if (p_option == RELOAD_CURRENT_PROJECT) {
  2513. save_confirmation->set_ok_button_text(TTR("Save & Reload"));
  2514. save_confirmation->set_text(TTR("Save modified resources before reloading?"));
  2515. } else {
  2516. save_confirmation->set_ok_button_text(TTR("Save & Quit"));
  2517. save_confirmation->set_text(TTR("Save modified resources before closing?"));
  2518. }
  2519. save_confirmation->reset_size();
  2520. save_confirmation->popup_centered();
  2521. break;
  2522. }
  2523. plugin_to_save = nullptr;
  2524. for (int i = 0; i < editor_data.get_editor_plugin_count(); i++) {
  2525. const String unsaved_status = editor_data.get_editor_plugin(i)->get_unsaved_status();
  2526. if (!unsaved_status.is_empty()) {
  2527. if (p_option == RELOAD_CURRENT_PROJECT) {
  2528. save_confirmation->set_ok_button_text(TTR("Save & Reload"));
  2529. save_confirmation->set_text(unsaved_status);
  2530. } else {
  2531. save_confirmation->set_ok_button_text(TTR("Save & Quit"));
  2532. save_confirmation->set_text(unsaved_status);
  2533. }
  2534. save_confirmation->reset_size();
  2535. save_confirmation->popup_centered();
  2536. plugin_to_save = editor_data.get_editor_plugin(i);
  2537. break;
  2538. }
  2539. }
  2540. if (plugin_to_save) {
  2541. break;
  2542. }
  2543. _discard_changes();
  2544. break;
  2545. }
  2546. if (save_each) {
  2547. tab_closing_menu_option = current_menu_option;
  2548. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  2549. tabs_to_close.push_back(editor_data.get_scene_path(i));
  2550. }
  2551. _proceed_closing_scene_tabs();
  2552. } else {
  2553. String unsaved_scenes;
  2554. int i = _next_unsaved_scene(true, 0);
  2555. while (i != -1) {
  2556. unsaved_scenes += "\n " + editor_data.get_edited_scene_root(i)->get_scene_file_path();
  2557. i = _next_unsaved_scene(true, ++i);
  2558. }
  2559. if (p_option == RELOAD_CURRENT_PROJECT) {
  2560. save_confirmation->set_ok_button_text(TTR("Save & Reload"));
  2561. save_confirmation->set_text(TTR("Save changes to the following scene(s) before reloading?") + unsaved_scenes);
  2562. } else {
  2563. save_confirmation->set_ok_button_text(TTR("Save & Quit"));
  2564. 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);
  2565. }
  2566. save_confirmation->reset_size();
  2567. save_confirmation->popup_centered();
  2568. }
  2569. DisplayServer::get_singleton()->window_request_attention();
  2570. break;
  2571. }
  2572. _save_external_resources();
  2573. _discard_changes();
  2574. } break;
  2575. case SETTINGS_UPDATE_CONTINUOUSLY: {
  2576. EditorSettings::get_singleton()->set("interface/editor/update_continuously", true);
  2577. _update_update_spinner();
  2578. show_accept(TTR("This option is deprecated. Situations where refresh must be forced are now considered a bug. Please report."), TTR("OK"));
  2579. } break;
  2580. case SETTINGS_UPDATE_WHEN_CHANGED: {
  2581. EditorSettings::get_singleton()->set("interface/editor/update_continuously", false);
  2582. _update_update_spinner();
  2583. } break;
  2584. case SETTINGS_UPDATE_SPINNER_HIDE: {
  2585. EditorSettings::get_singleton()->set("interface/editor/show_update_spinner", 2); // Disabled
  2586. _update_update_spinner();
  2587. } break;
  2588. case SETTINGS_PREFERENCES: {
  2589. editor_settings_dialog->popup_edit_settings();
  2590. } break;
  2591. case SETTINGS_EDITOR_DATA_FOLDER: {
  2592. OS::get_singleton()->shell_show_in_file_manager(EditorPaths::get_singleton()->get_data_dir(), true);
  2593. } break;
  2594. case SETTINGS_EDITOR_CONFIG_FOLDER: {
  2595. OS::get_singleton()->shell_show_in_file_manager(EditorPaths::get_singleton()->get_config_dir(), true);
  2596. } break;
  2597. case SETTINGS_MANAGE_EXPORT_TEMPLATES: {
  2598. export_template_manager->popup_manager();
  2599. } break;
  2600. case SETTINGS_MANAGE_FBX_IMPORTER: {
  2601. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  2602. fbx_importer_manager->show_dialog();
  2603. #endif
  2604. } break;
  2605. case SETTINGS_INSTALL_ANDROID_BUILD_TEMPLATE: {
  2606. gradle_build_manage_templates->hide();
  2607. file_android_build_source->popup_centered_ratio();
  2608. } break;
  2609. case SETTINGS_MANAGE_FEATURE_PROFILES: {
  2610. feature_profile_manager->popup_centered_clamped(Size2(900, 800) * EDSCALE, 0.8);
  2611. } break;
  2612. case SETTINGS_TOGGLE_FULLSCREEN: {
  2613. DisplayServer::WindowMode mode = DisplayServer::get_singleton()->window_get_mode();
  2614. if (mode == DisplayServer::WINDOW_MODE_FULLSCREEN || mode == DisplayServer::WINDOW_MODE_EXCLUSIVE_FULLSCREEN) {
  2615. DisplayServer::get_singleton()->window_set_mode(prev_mode);
  2616. } else {
  2617. prev_mode = mode;
  2618. DisplayServer::get_singleton()->window_set_mode(DisplayServer::WINDOW_MODE_FULLSCREEN);
  2619. }
  2620. } break;
  2621. case EDITOR_SCREENSHOT: {
  2622. screenshot_timer->start();
  2623. } break;
  2624. case SETTINGS_PICK_MAIN_SCENE: {
  2625. file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  2626. List<String> extensions;
  2627. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &extensions);
  2628. file->clear_filters();
  2629. for (const String &extension : extensions) {
  2630. file->add_filter("*." + extension, extension.to_upper());
  2631. }
  2632. Node *scene = editor_data.get_edited_scene_root();
  2633. if (scene) {
  2634. file->set_current_path(scene->get_scene_file_path());
  2635. }
  2636. file->set_title(TTR("Pick a Main Scene"));
  2637. file->popup_file_dialog();
  2638. } break;
  2639. case HELP_SEARCH: {
  2640. emit_signal(SNAME("request_help_search"), "");
  2641. } break;
  2642. case HELP_COMMAND_PALETTE: {
  2643. command_palette->open_popup();
  2644. } break;
  2645. case HELP_DOCS: {
  2646. OS::get_singleton()->shell_open(VERSION_DOCS_URL "/");
  2647. } break;
  2648. case HELP_FORUM: {
  2649. OS::get_singleton()->shell_open("https://forum.godotengine.org/");
  2650. } break;
  2651. case HELP_REPORT_A_BUG: {
  2652. OS::get_singleton()->shell_open("https://github.com/godotengine/godot/issues");
  2653. } break;
  2654. case HELP_COPY_SYSTEM_INFO: {
  2655. String info = _get_system_info();
  2656. DisplayServer::get_singleton()->clipboard_set(info);
  2657. } break;
  2658. case HELP_SUGGEST_A_FEATURE: {
  2659. OS::get_singleton()->shell_open("https://github.com/godotengine/godot-proposals#readme");
  2660. } break;
  2661. case HELP_SEND_DOCS_FEEDBACK: {
  2662. OS::get_singleton()->shell_open("https://github.com/godotengine/godot-docs/issues");
  2663. } break;
  2664. case HELP_COMMUNITY: {
  2665. OS::get_singleton()->shell_open("https://godotengine.org/community");
  2666. } break;
  2667. case HELP_ABOUT: {
  2668. about->popup_centered(Size2(780, 500) * EDSCALE);
  2669. } break;
  2670. case HELP_SUPPORT_GODOT_DEVELOPMENT: {
  2671. OS::get_singleton()->shell_open("https://godotengine.org/donate");
  2672. } break;
  2673. case SET_RENDERER_NAME_SAVE_AND_RESTART: {
  2674. ProjectSettings::get_singleton()->set("rendering/renderer/rendering_method", renderer_request);
  2675. if (renderer_request == "mobile" || renderer_request == "gl_compatibility") {
  2676. // Also change the mobile override if changing to a compatible rendering method.
  2677. // This prevents visual discrepancies between desktop and mobile platforms.
  2678. ProjectSettings::get_singleton()->set("rendering/renderer/rendering_method.mobile", renderer_request);
  2679. } else if (renderer_request == "forward_plus") {
  2680. // Use the equivalent mobile rendering method. This prevents the rendering method from staying
  2681. // on its old choice if moving from `gl_compatibility` to `forward_plus`.
  2682. ProjectSettings::get_singleton()->set("rendering/renderer/rendering_method.mobile", "mobile");
  2683. }
  2684. ProjectSettings::get_singleton()->save();
  2685. save_all_scenes();
  2686. restart_editor();
  2687. } break;
  2688. }
  2689. }
  2690. String EditorNode::adjust_scene_name_casing(const String &p_root_name) {
  2691. switch (GLOBAL_GET("editor/naming/scene_name_casing").operator int()) {
  2692. case SCENE_NAME_CASING_AUTO:
  2693. // Use casing of the root node.
  2694. break;
  2695. case SCENE_NAME_CASING_PASCAL_CASE:
  2696. return p_root_name.replace("-", "_").to_pascal_case();
  2697. case SCENE_NAME_CASING_SNAKE_CASE:
  2698. return p_root_name.replace("-", "_").to_snake_case();
  2699. case SCENE_NAME_CASING_KEBAB_CASE:
  2700. return p_root_name.to_snake_case().replace("_", "-");
  2701. }
  2702. return p_root_name;
  2703. }
  2704. String EditorNode::adjust_script_name_casing(const String &p_file_name, ScriptLanguage::ScriptNameCasing p_auto_casing) {
  2705. int editor_casing = GLOBAL_GET("editor/naming/script_name_casing");
  2706. if (editor_casing == ScriptLanguage::SCRIPT_NAME_CASING_AUTO) {
  2707. // Use the script language's preferred casing.
  2708. editor_casing = p_auto_casing;
  2709. }
  2710. switch (editor_casing) {
  2711. case ScriptLanguage::SCRIPT_NAME_CASING_AUTO:
  2712. // Script language has no preference, so do not adjust.
  2713. break;
  2714. case ScriptLanguage::SCRIPT_NAME_CASING_PASCAL_CASE:
  2715. return p_file_name.replace("-", "_").to_pascal_case();
  2716. case ScriptLanguage::SCRIPT_NAME_CASING_SNAKE_CASE:
  2717. return p_file_name.replace("-", "_").to_snake_case();
  2718. case ScriptLanguage::SCRIPT_NAME_CASING_KEBAB_CASE:
  2719. return p_file_name.to_snake_case().replace("_", "-");
  2720. }
  2721. return p_file_name;
  2722. }
  2723. void EditorNode::_request_screenshot() {
  2724. _screenshot();
  2725. }
  2726. void EditorNode::_screenshot(bool p_use_utc) {
  2727. String name = "editor_screenshot_" + Time::get_singleton()->get_datetime_string_from_system(p_use_utc).replace(":", "") + ".png";
  2728. NodePath path = String("user://") + name;
  2729. _save_screenshot(path);
  2730. if (EDITOR_GET("interface/editor/automatically_open_screenshots")) {
  2731. OS::get_singleton()->shell_show_in_file_manager(ProjectSettings::get_singleton()->globalize_path(path), true);
  2732. }
  2733. }
  2734. void EditorNode::_save_screenshot(NodePath p_path) {
  2735. Control *editor_main_screen = EditorInterface::get_singleton()->get_editor_main_screen();
  2736. ERR_FAIL_NULL_MSG(editor_main_screen, "Cannot get the editor main screen control.");
  2737. Viewport *viewport = editor_main_screen->get_viewport();
  2738. ERR_FAIL_NULL_MSG(viewport, "Cannot get a viewport from the editor main screen.");
  2739. Ref<ViewportTexture> texture = viewport->get_texture();
  2740. ERR_FAIL_COND_MSG(texture.is_null(), "Cannot get a viewport texture from the editor main screen.");
  2741. Ref<Image> img = texture->get_image();
  2742. ERR_FAIL_COND_MSG(img.is_null(), "Cannot get an image from a viewport texture of the editor main screen.");
  2743. Error error = img->save_png(p_path);
  2744. ERR_FAIL_COND_MSG(error != OK, "Cannot save screenshot to file '" + p_path + "'.");
  2745. }
  2746. void EditorNode::_check_system_theme_changed() {
  2747. DisplayServer *display_server = DisplayServer::get_singleton();
  2748. bool system_theme_changed = false;
  2749. if (follow_system_theme) {
  2750. if (display_server->get_base_color() != last_system_base_color) {
  2751. system_theme_changed = true;
  2752. last_system_base_color = display_server->get_base_color();
  2753. }
  2754. if (display_server->is_dark_mode_supported() && display_server->is_dark_mode() != last_dark_mode_state) {
  2755. system_theme_changed = true;
  2756. last_dark_mode_state = display_server->is_dark_mode();
  2757. }
  2758. }
  2759. if (use_system_accent_color) {
  2760. if (display_server->get_accent_color() != last_system_accent_color) {
  2761. system_theme_changed = true;
  2762. last_system_accent_color = display_server->get_accent_color();
  2763. }
  2764. }
  2765. if (system_theme_changed) {
  2766. _update_theme();
  2767. }
  2768. }
  2769. void EditorNode::_tool_menu_option(int p_idx) {
  2770. switch (tool_menu->get_item_id(p_idx)) {
  2771. case TOOLS_ORPHAN_RESOURCES: {
  2772. orphan_resources->show();
  2773. } break;
  2774. case TOOLS_BUILD_PROFILE_MANAGER: {
  2775. build_profile_manager->popup_centered_clamped(Size2(700, 800) * EDSCALE, 0.8);
  2776. } break;
  2777. case TOOLS_SURFACE_UPGRADE: {
  2778. surface_upgrade_dialog->popup_on_demand();
  2779. } break;
  2780. case TOOLS_CUSTOM: {
  2781. if (tool_menu->get_item_submenu(p_idx) == "") {
  2782. Callable callback = tool_menu->get_item_metadata(p_idx);
  2783. Callable::CallError ce;
  2784. Variant result;
  2785. callback.callp(nullptr, 0, result, ce);
  2786. if (ce.error != Callable::CallError::CALL_OK) {
  2787. String err = Variant::get_callable_error_text(callback, nullptr, 0, ce);
  2788. ERR_PRINT("Error calling function from tool menu: " + err);
  2789. }
  2790. } // Else it's a submenu so don't do anything.
  2791. } break;
  2792. }
  2793. }
  2794. void EditorNode::_export_as_menu_option(int p_idx) {
  2795. if (p_idx == 0) { // MeshLibrary
  2796. current_menu_option = FILE_EXPORT_MESH_LIBRARY;
  2797. if (!editor_data.get_edited_scene_root()) {
  2798. show_accept(TTR("This operation can't be done without a scene."), TTR("OK"));
  2799. return;
  2800. }
  2801. List<String> extensions;
  2802. Ref<MeshLibrary> ml(memnew(MeshLibrary));
  2803. ResourceSaver::get_recognized_extensions(ml, &extensions);
  2804. file_export_lib->clear_filters();
  2805. for (const String &E : extensions) {
  2806. file_export_lib->add_filter("*." + E);
  2807. }
  2808. file_export_lib->set_title(TTR("Export Mesh Library"));
  2809. file_export_lib->popup_file_dialog();
  2810. } else { // Custom menu options added by plugins
  2811. if (export_as_menu->get_item_submenu(p_idx).is_empty()) { // If not a submenu
  2812. Callable callback = export_as_menu->get_item_metadata(p_idx);
  2813. Callable::CallError ce;
  2814. Variant result;
  2815. callback.callp(nullptr, 0, result, ce);
  2816. if (ce.error != Callable::CallError::CALL_OK) {
  2817. String err = Variant::get_callable_error_text(callback, nullptr, 0, ce);
  2818. ERR_PRINT("Error calling function from export_as menu: " + err);
  2819. }
  2820. }
  2821. }
  2822. }
  2823. int EditorNode::_next_unsaved_scene(bool p_valid_filename, int p_start) {
  2824. for (int i = p_start; i < editor_data.get_edited_scene_count(); i++) {
  2825. if (!editor_data.get_edited_scene_root(i)) {
  2826. continue;
  2827. }
  2828. String scene_filename = editor_data.get_edited_scene_root(i)->get_scene_file_path();
  2829. if (p_valid_filename && scene_filename.is_empty()) {
  2830. continue;
  2831. }
  2832. bool unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(editor_data.get_scene_history_id(i));
  2833. if (unsaved) {
  2834. return i;
  2835. } else {
  2836. for (int j = 0; j < editor_data.get_editor_plugin_count(); j++) {
  2837. if (!editor_data.get_editor_plugin(j)->get_unsaved_status(scene_filename).is_empty()) {
  2838. return i;
  2839. }
  2840. }
  2841. }
  2842. }
  2843. return -1;
  2844. }
  2845. void EditorNode::_exit_editor(int p_exit_code) {
  2846. exiting = true;
  2847. resource_preview->stop(); // Stop early to avoid crashes.
  2848. _save_editor_layout();
  2849. // Dim the editor window while it's quitting to make it clearer that it's busy.
  2850. dim_editor(true);
  2851. get_tree()->quit(p_exit_code);
  2852. }
  2853. void EditorNode::_discard_changes(const String &p_str) {
  2854. switch (current_menu_option) {
  2855. case FILE_CLOSE:
  2856. case SCENE_TAB_CLOSE: {
  2857. Node *scene = editor_data.get_edited_scene_root(tab_closing_idx);
  2858. if (scene != nullptr) {
  2859. String scene_filename = scene->get_scene_file_path();
  2860. if (!scene_filename.is_empty()) {
  2861. previous_scenes.push_back(scene_filename);
  2862. }
  2863. }
  2864. // Don't close tabs when exiting the editor (required for "restore_scenes_on_load" setting).
  2865. if (!_is_closing_editor()) {
  2866. _remove_scene(tab_closing_idx);
  2867. scene_tabs->update_scene_tabs();
  2868. }
  2869. _proceed_closing_scene_tabs();
  2870. } break;
  2871. case FILE_QUIT: {
  2872. project_run_bar->stop_playing();
  2873. _exit_editor(EXIT_SUCCESS);
  2874. } break;
  2875. case RUN_PROJECT_MANAGER: {
  2876. project_run_bar->stop_playing();
  2877. _exit_editor(EXIT_SUCCESS);
  2878. String exec = OS::get_singleton()->get_executable_path();
  2879. List<String> args;
  2880. for (const String &a : Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_TOOL)) {
  2881. args.push_back(a);
  2882. }
  2883. String exec_base_dir = exec.get_base_dir();
  2884. if (!exec_base_dir.is_empty()) {
  2885. args.push_back("--path");
  2886. args.push_back(exec_base_dir);
  2887. }
  2888. args.push_back("--project-manager");
  2889. Error err = OS::get_singleton()->create_instance(args);
  2890. ERR_FAIL_COND(err);
  2891. } break;
  2892. case RELOAD_CURRENT_PROJECT: {
  2893. restart_editor();
  2894. } break;
  2895. }
  2896. }
  2897. void EditorNode::_update_file_menu_opened() {
  2898. file_menu->set_item_disabled(file_menu->get_item_index(FILE_OPEN_PREV), previous_scenes.is_empty());
  2899. _update_undo_redo_allowed();
  2900. }
  2901. void EditorNode::_update_file_menu_closed() {
  2902. file_menu->set_item_disabled(file_menu->get_item_index(FILE_OPEN_PREV), false);
  2903. }
  2904. VBoxContainer *EditorNode::get_main_screen_control() {
  2905. return main_screen_vbox;
  2906. }
  2907. void EditorNode::editor_select(int p_which) {
  2908. static bool selecting = false;
  2909. if (selecting || changing_scene) {
  2910. return;
  2911. }
  2912. ERR_FAIL_INDEX(p_which, editor_table.size());
  2913. if (!main_editor_buttons[p_which]->is_visible()) { // Button hidden, no editor.
  2914. return;
  2915. }
  2916. selecting = true;
  2917. for (int i = 0; i < main_editor_buttons.size(); i++) {
  2918. main_editor_buttons[i]->set_pressed(i == p_which);
  2919. }
  2920. selecting = false;
  2921. EditorPlugin *new_editor = editor_table[p_which];
  2922. ERR_FAIL_NULL(new_editor);
  2923. if (editor_plugin_screen == new_editor) {
  2924. return;
  2925. }
  2926. if (editor_plugin_screen) {
  2927. editor_plugin_screen->make_visible(false);
  2928. }
  2929. editor_plugin_screen = new_editor;
  2930. editor_plugin_screen->make_visible(true);
  2931. editor_plugin_screen->selected_notify();
  2932. int plugin_count = editor_data.get_editor_plugin_count();
  2933. for (int i = 0; i < plugin_count; i++) {
  2934. editor_data.get_editor_plugin(i)->notify_main_screen_changed(editor_plugin_screen->get_name());
  2935. }
  2936. if (EDITOR_GET("interface/editor/separate_distraction_mode")) {
  2937. if (p_which == EDITOR_SCRIPT) {
  2938. set_distraction_free_mode(script_distraction_free);
  2939. } else {
  2940. set_distraction_free_mode(scene_distraction_free);
  2941. }
  2942. }
  2943. }
  2944. void EditorNode::select_editor_by_name(const String &p_name) {
  2945. ERR_FAIL_COND(p_name.is_empty());
  2946. for (int i = 0; i < main_editor_buttons.size(); i++) {
  2947. if (main_editor_buttons[i]->get_text() == p_name) {
  2948. editor_select(i);
  2949. return;
  2950. }
  2951. }
  2952. ERR_FAIL_MSG("The editor name '" + p_name + "' was not found.");
  2953. }
  2954. void EditorNode::add_editor_plugin(EditorPlugin *p_editor, bool p_config_changed) {
  2955. if (p_editor->has_main_screen()) {
  2956. Button *tb = memnew(Button);
  2957. tb->set_toggle_mode(true);
  2958. tb->set_theme_type_variation("MainScreenButton");
  2959. tb->set_name(p_editor->get_name());
  2960. tb->set_text(p_editor->get_name());
  2961. Ref<Texture2D> icon = p_editor->get_icon();
  2962. if (icon.is_null() && singleton->theme->has_icon(p_editor->get_name(), EditorStringName(EditorIcons))) {
  2963. icon = singleton->theme->get_icon(p_editor->get_name(), EditorStringName(EditorIcons));
  2964. }
  2965. if (icon.is_valid()) {
  2966. tb->set_icon(icon);
  2967. // Make sure the control is updated if the icon is reimported.
  2968. icon->connect_changed(callable_mp((Control *)tb, &Control::update_minimum_size));
  2969. }
  2970. tb->connect(SceneStringName(pressed), callable_mp(singleton, &EditorNode::editor_select).bind(singleton->main_editor_buttons.size()));
  2971. singleton->main_editor_buttons.push_back(tb);
  2972. singleton->main_editor_button_hb->add_child(tb);
  2973. singleton->editor_table.push_back(p_editor);
  2974. singleton->distraction_free->move_to_front();
  2975. }
  2976. singleton->editor_data.add_editor_plugin(p_editor);
  2977. singleton->add_child(p_editor);
  2978. if (p_config_changed) {
  2979. p_editor->enable_plugin();
  2980. }
  2981. }
  2982. void EditorNode::remove_editor_plugin(EditorPlugin *p_editor, bool p_config_changed) {
  2983. if (p_editor->has_main_screen()) {
  2984. // Remove the main editor button and update the bindings of
  2985. // all buttons behind it to point to the correct main window.
  2986. for (int i = singleton->main_editor_buttons.size() - 1; i >= 0; i--) {
  2987. if (p_editor->get_name() == singleton->main_editor_buttons[i]->get_text()) {
  2988. if (singleton->main_editor_buttons[i]->is_pressed()) {
  2989. singleton->editor_select(EDITOR_SCRIPT);
  2990. }
  2991. memdelete(singleton->main_editor_buttons[i]);
  2992. singleton->main_editor_buttons.remove_at(i);
  2993. break;
  2994. } else {
  2995. singleton->main_editor_buttons[i]->disconnect(SceneStringName(pressed), callable_mp(singleton, &EditorNode::editor_select));
  2996. singleton->main_editor_buttons[i]->connect(SceneStringName(pressed), callable_mp(singleton, &EditorNode::editor_select).bind(i - 1));
  2997. }
  2998. }
  2999. if (singleton->editor_plugin_screen == p_editor) {
  3000. singleton->editor_plugin_screen = nullptr;
  3001. }
  3002. singleton->editor_table.erase(p_editor);
  3003. }
  3004. p_editor->make_visible(false);
  3005. p_editor->clear();
  3006. if (p_config_changed) {
  3007. p_editor->disable_plugin();
  3008. }
  3009. singleton->editor_plugins_over->remove_plugin(p_editor);
  3010. singleton->editor_plugins_force_over->remove_plugin(p_editor);
  3011. singleton->editor_plugins_force_input_forwarding->remove_plugin(p_editor);
  3012. singleton->remove_child(p_editor);
  3013. singleton->editor_data.remove_editor_plugin(p_editor);
  3014. for (KeyValue<ObjectID, HashSet<EditorPlugin *>> &kv : singleton->active_plugins) {
  3015. kv.value.erase(p_editor);
  3016. }
  3017. }
  3018. void EditorNode::add_extension_editor_plugin(const StringName &p_class_name) {
  3019. ERR_FAIL_COND_MSG(!ClassDB::class_exists(p_class_name), vformat("No such editor plugin registered: %s", p_class_name));
  3020. ERR_FAIL_COND_MSG(!ClassDB::is_parent_class(p_class_name, SNAME("EditorPlugin")), vformat("Class is not an editor plugin: %s", p_class_name));
  3021. ERR_FAIL_COND_MSG(singleton->editor_data.has_extension_editor_plugin(p_class_name), vformat("Editor plugin already added for class: %s", p_class_name));
  3022. EditorPlugin *plugin = Object::cast_to<EditorPlugin>(ClassDB::instantiate(p_class_name));
  3023. singleton->editor_data.add_extension_editor_plugin(p_class_name, plugin);
  3024. add_editor_plugin(plugin);
  3025. }
  3026. void EditorNode::remove_extension_editor_plugin(const StringName &p_class_name) {
  3027. // If we're exiting, the editor plugins will get cleaned up anyway, so don't do anything.
  3028. if (singleton->exiting) {
  3029. return;
  3030. }
  3031. ERR_FAIL_COND_MSG(!singleton->editor_data.has_extension_editor_plugin(p_class_name), vformat("No editor plugin added for class: %s", p_class_name));
  3032. EditorPlugin *plugin = singleton->editor_data.get_extension_editor_plugin(p_class_name);
  3033. remove_editor_plugin(plugin);
  3034. memdelete(plugin);
  3035. singleton->editor_data.remove_extension_editor_plugin(p_class_name);
  3036. }
  3037. void EditorNode::_update_addon_config() {
  3038. if (_initializing_plugins) {
  3039. return;
  3040. }
  3041. Vector<String> enabled_addons;
  3042. for (const KeyValue<String, EditorPlugin *> &E : addon_name_to_plugin) {
  3043. enabled_addons.push_back(E.key);
  3044. }
  3045. if (enabled_addons.size() == 0) {
  3046. ProjectSettings::get_singleton()->set("editor_plugins/enabled", Variant());
  3047. } else {
  3048. enabled_addons.sort();
  3049. ProjectSettings::get_singleton()->set("editor_plugins/enabled", enabled_addons);
  3050. }
  3051. project_settings_editor->queue_save();
  3052. }
  3053. void EditorNode::set_addon_plugin_enabled(const String &p_addon, bool p_enabled, bool p_config_changed) {
  3054. String addon_path = p_addon;
  3055. if (!addon_path.begins_with("res://")) {
  3056. addon_path = "res://addons/" + addon_path + "/plugin.cfg";
  3057. }
  3058. ERR_FAIL_COND(p_enabled && addon_name_to_plugin.has(addon_path));
  3059. ERR_FAIL_COND(!p_enabled && !addon_name_to_plugin.has(addon_path));
  3060. if (!p_enabled) {
  3061. EditorPlugin *addon = addon_name_to_plugin[addon_path];
  3062. remove_editor_plugin(addon, p_config_changed);
  3063. memdelete(addon);
  3064. addon_name_to_plugin.erase(addon_path);
  3065. _update_addon_config();
  3066. return;
  3067. }
  3068. Ref<ConfigFile> cf;
  3069. cf.instantiate();
  3070. if (!DirAccess::exists(addon_path.get_base_dir())) {
  3071. _remove_plugin_from_enabled(addon_path);
  3072. WARN_PRINT("Addon '" + addon_path + "' failed to load. No directory found. Removing from enabled plugins.");
  3073. return;
  3074. }
  3075. Error err = cf->load(addon_path);
  3076. if (err != OK) {
  3077. show_warning(vformat(TTR("Unable to enable addon plugin at: '%s' parsing of config failed."), addon_path));
  3078. return;
  3079. }
  3080. String plugin_version;
  3081. if (cf->has_section_key("plugin", "version")) {
  3082. plugin_version = cf->get_value("plugin", "version");
  3083. }
  3084. if (!cf->has_section_key("plugin", "script")) {
  3085. show_warning(vformat(TTR("Unable to find script field for addon plugin at: '%s'."), addon_path));
  3086. return;
  3087. }
  3088. String script_path = cf->get_value("plugin", "script");
  3089. Ref<Script> scr; // We need to save it for creating "ep" below.
  3090. // Only try to load the script if it has a name. Else, the plugin has no init script.
  3091. if (script_path.length() > 0) {
  3092. script_path = addon_path.get_base_dir().path_join(script_path);
  3093. scr = ResourceLoader::load(script_path, "Script", ResourceFormatLoader::CACHE_MODE_IGNORE);
  3094. if (scr.is_null()) {
  3095. show_warning(vformat(TTR("Unable to load addon script from path: '%s'."), script_path));
  3096. return;
  3097. }
  3098. // Errors in the script cause the base_type to be an empty StringName.
  3099. if (scr->get_instance_base_type() == StringName()) {
  3100. if (_initializing_plugins) {
  3101. // However, if it happens during initialization, waiting for file scan might help.
  3102. pending_addons.push_back(p_addon);
  3103. return;
  3104. }
  3105. 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, addon_path));
  3106. _remove_plugin_from_enabled(addon_path);
  3107. return;
  3108. }
  3109. // Plugin init scripts must inherit from EditorPlugin and be tools.
  3110. if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorPlugin")) {
  3111. show_warning(vformat(TTR("Unable to load addon script from path: '%s'. Base type is not 'EditorPlugin'."), script_path));
  3112. return;
  3113. }
  3114. if (!scr->is_tool()) {
  3115. show_warning(vformat(TTR("Unable to load addon script from path: '%s'. Script is not in tool mode."), script_path));
  3116. return;
  3117. }
  3118. }
  3119. EditorPlugin *ep = memnew(EditorPlugin);
  3120. ep->set_script(scr);
  3121. ep->set_plugin_version(plugin_version);
  3122. addon_name_to_plugin[addon_path] = ep;
  3123. add_editor_plugin(ep, p_config_changed);
  3124. _update_addon_config();
  3125. }
  3126. bool EditorNode::is_addon_plugin_enabled(const String &p_addon) const {
  3127. if (p_addon.begins_with("res://")) {
  3128. return addon_name_to_plugin.has(p_addon);
  3129. }
  3130. return addon_name_to_plugin.has("res://addons/" + p_addon + "/plugin.cfg");
  3131. }
  3132. void EditorNode::_remove_edited_scene(bool p_change_tab) {
  3133. // When scene gets closed no node is edited anymore, so make sure the editors are notified before nodes are freed.
  3134. hide_unused_editors(SceneTreeDock::get_singleton());
  3135. int new_index = editor_data.get_edited_scene();
  3136. int old_index = new_index;
  3137. if (new_index > 0) {
  3138. new_index = new_index - 1;
  3139. } else if (editor_data.get_edited_scene_count() > 1) {
  3140. new_index = 1;
  3141. } else {
  3142. editor_data.add_edited_scene(-1);
  3143. new_index = 1;
  3144. }
  3145. if (p_change_tab) {
  3146. _set_current_scene(new_index);
  3147. }
  3148. editor_data.remove_scene(old_index);
  3149. _update_title();
  3150. scene_tabs->update_scene_tabs();
  3151. }
  3152. void EditorNode::_remove_scene(int index, bool p_change_tab) {
  3153. // Clear icon cache in case some scripts are no longer needed.
  3154. // FIXME: Ideally the cache should never be cleared and only updated on per-script basis, when an icon changes.
  3155. editor_data.clear_script_icon_cache();
  3156. if (editor_data.get_edited_scene() == index) {
  3157. // Scene to remove is current scene.
  3158. _remove_edited_scene(p_change_tab);
  3159. } else {
  3160. // Scene to remove is not active scene.
  3161. editor_data.remove_scene(index);
  3162. }
  3163. }
  3164. void EditorNode::set_edited_scene(Node *p_scene) {
  3165. Node *old_edited_scene_root = get_editor_data().get_edited_scene_root();
  3166. if (old_edited_scene_root) {
  3167. if (old_edited_scene_root->get_parent() == scene_root) {
  3168. scene_root->remove_child(old_edited_scene_root);
  3169. }
  3170. old_edited_scene_root->disconnect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene));
  3171. }
  3172. get_editor_data().set_edited_scene_root(p_scene);
  3173. if (Object::cast_to<Popup>(p_scene)) {
  3174. Object::cast_to<Popup>(p_scene)->show();
  3175. }
  3176. SceneTreeDock::get_singleton()->set_edited_scene(p_scene);
  3177. if (get_tree()) {
  3178. get_tree()->set_edited_scene_root(p_scene);
  3179. }
  3180. if (p_scene) {
  3181. if (p_scene->get_parent() != scene_root) {
  3182. scene_root->add_child(p_scene, true);
  3183. }
  3184. p_scene->connect(SNAME("replacing_by"), callable_mp(this, &EditorNode::set_edited_scene));
  3185. }
  3186. }
  3187. int EditorNode::_get_current_main_editor() {
  3188. for (int i = 0; i < editor_table.size(); i++) {
  3189. if (editor_table[i] == editor_plugin_screen) {
  3190. return i;
  3191. }
  3192. }
  3193. return 0;
  3194. }
  3195. Dictionary EditorNode::_get_main_scene_state() {
  3196. Dictionary state;
  3197. state["main_tab"] = _get_current_main_editor();
  3198. state["scene_tree_offset"] = SceneTreeDock::get_singleton()->get_tree_editor()->get_scene_tree()->get_vscroll_bar()->get_value();
  3199. state["property_edit_offset"] = InspectorDock::get_inspector_singleton()->get_scroll_offset();
  3200. state["node_filter"] = SceneTreeDock::get_singleton()->get_filter();
  3201. return state;
  3202. }
  3203. void EditorNode::_set_main_scene_state(Dictionary p_state, Node *p_for_scene) {
  3204. if (get_edited_scene() != p_for_scene && p_for_scene != nullptr) {
  3205. return; // Not for this scene.
  3206. }
  3207. changing_scene = false;
  3208. int current_tab = -1;
  3209. for (int i = 0; i < editor_table.size(); i++) {
  3210. if (editor_plugin_screen == editor_table[i]) {
  3211. current_tab = i;
  3212. break;
  3213. }
  3214. }
  3215. if (p_state.has("editor_index")) {
  3216. int index = p_state["editor_index"];
  3217. if (current_tab < 2) { // If currently in spatial/2d, only switch to spatial/2d. If currently in script, stay there.
  3218. if (index < 2 || !get_edited_scene()) {
  3219. editor_select(index);
  3220. }
  3221. }
  3222. }
  3223. if (get_edited_scene()) {
  3224. if (current_tab < 2) {
  3225. Node *editor_node = SceneTreeDock::get_singleton()->get_tree_editor()->get_selected();
  3226. editor_node = editor_node == nullptr ? get_edited_scene() : editor_node;
  3227. if (Object::cast_to<CanvasItem>(editor_node)) {
  3228. editor_select(EDITOR_2D);
  3229. } else if (Object::cast_to<Node3D>(editor_node)) {
  3230. editor_select(EDITOR_3D);
  3231. }
  3232. }
  3233. }
  3234. if (p_state.has("scene_tree_offset")) {
  3235. SceneTreeDock::get_singleton()->get_tree_editor()->get_scene_tree()->get_vscroll_bar()->set_value(p_state["scene_tree_offset"]);
  3236. }
  3237. if (p_state.has("property_edit_offset")) {
  3238. InspectorDock::get_inspector_singleton()->set_scroll_offset(p_state["property_edit_offset"]);
  3239. }
  3240. if (p_state.has("node_filter")) {
  3241. SceneTreeDock::get_singleton()->set_filter(p_state["node_filter"]);
  3242. }
  3243. // This should only happen at the very end.
  3244. EditorDebuggerNode::get_singleton()->update_live_edit_root();
  3245. ScriptEditor::get_singleton()->set_scene_root_script(editor_data.get_scene_root_script(editor_data.get_edited_scene()));
  3246. editor_data.notify_edited_scene_changed();
  3247. emit_signal(SNAME("scene_changed"));
  3248. // Reset SDFGI after everything else so that any last-second scene modifications will be processed.
  3249. RenderingServer::get_singleton()->sdfgi_reset();
  3250. }
  3251. bool EditorNode::is_changing_scene() const {
  3252. return changing_scene;
  3253. }
  3254. void EditorNode::_set_current_scene(int p_idx) {
  3255. if (p_idx == editor_data.get_edited_scene()) {
  3256. return; // Pointless.
  3257. }
  3258. _set_current_scene_nocheck(p_idx);
  3259. }
  3260. void EditorNode::_set_current_scene_nocheck(int p_idx) {
  3261. // Save the folding in case the scene gets reloaded.
  3262. if (editor_data.get_scene_path(p_idx) != "" && editor_data.get_edited_scene_root(p_idx)) {
  3263. editor_folding.save_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
  3264. }
  3265. changing_scene = true;
  3266. editor_data.save_edited_scene_state(editor_selection, &editor_history, _get_main_scene_state());
  3267. if (get_editor_data().get_edited_scene_root()) {
  3268. if (get_editor_data().get_edited_scene_root()->get_parent() == scene_root) {
  3269. scene_root->remove_child(get_editor_data().get_edited_scene_root());
  3270. }
  3271. }
  3272. editor_selection->clear();
  3273. editor_data.set_edited_scene(p_idx);
  3274. Node *new_scene = editor_data.get_edited_scene_root();
  3275. if (Popup *p = Object::cast_to<Popup>(new_scene)) {
  3276. p->show();
  3277. }
  3278. SceneTreeDock::get_singleton()->set_edited_scene(new_scene);
  3279. if (get_tree()) {
  3280. get_tree()->set_edited_scene_root(new_scene);
  3281. }
  3282. if (new_scene) {
  3283. if (new_scene->get_parent() != scene_root) {
  3284. scene_root->add_child(new_scene, true);
  3285. }
  3286. }
  3287. if (editor_data.check_and_update_scene(p_idx)) {
  3288. if (!editor_data.get_scene_path(p_idx).is_empty()) {
  3289. editor_folding.load_scene_folding(editor_data.get_edited_scene_root(p_idx), editor_data.get_scene_path(p_idx));
  3290. }
  3291. EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_scene_history_id(p_idx));
  3292. }
  3293. Dictionary state = editor_data.restore_edited_scene_state(editor_selection, &editor_history);
  3294. _edit_current(true);
  3295. _update_title();
  3296. callable_mp(scene_tabs, &EditorSceneTabs::update_scene_tabs).call_deferred();
  3297. if (tabs_to_close.is_empty()) {
  3298. callable_mp(this, &EditorNode::_set_main_scene_state).call_deferred(state, get_edited_scene()); // Do after everything else is done setting up.
  3299. }
  3300. _update_undo_redo_allowed();
  3301. }
  3302. void EditorNode::setup_color_picker(ColorPicker *p_picker) {
  3303. p_picker->set_editor_settings(EditorSettings::get_singleton());
  3304. int default_color_mode = EditorSettings::get_singleton()->get_project_metadata("color_picker", "color_mode", EDITOR_GET("interface/inspector/default_color_picker_mode"));
  3305. int picker_shape = EditorSettings::get_singleton()->get_project_metadata("color_picker", "picker_shape", EDITOR_GET("interface/inspector/default_color_picker_shape"));
  3306. p_picker->set_color_mode((ColorPicker::ColorModeType)default_color_mode);
  3307. p_picker->set_picker_shape((ColorPicker::PickerShapeType)picker_shape);
  3308. }
  3309. bool EditorNode::is_scene_open(const String &p_path) {
  3310. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  3311. if (editor_data.get_scene_path(i) == p_path) {
  3312. return true;
  3313. }
  3314. }
  3315. return false;
  3316. }
  3317. bool EditorNode::is_multi_window_enabled() const {
  3318. return !SceneTree::get_singleton()->get_root()->is_embedding_subwindows() && !EDITOR_GET("interface/editor/single_window_mode") && EDITOR_GET("interface/multi_window/enable");
  3319. }
  3320. void EditorNode::fix_dependencies(const String &p_for_file) {
  3321. dependency_fixer->edit(p_for_file);
  3322. }
  3323. int EditorNode::new_scene() {
  3324. int idx = editor_data.add_edited_scene(-1);
  3325. _set_current_scene(idx); // Before trying to remove an empty scene, set the current tab index to the newly added tab index.
  3326. // Remove placeholder empty scene.
  3327. if (editor_data.get_edited_scene_count() > 1) {
  3328. for (int i = 0; i < editor_data.get_edited_scene_count() - 1; i++) {
  3329. bool unsaved = EditorUndoRedoManager::get_singleton()->is_history_unsaved(editor_data.get_scene_history_id(i));
  3330. if (!unsaved && editor_data.get_scene_path(i).is_empty() && editor_data.get_edited_scene_root(i) == nullptr) {
  3331. editor_data.remove_scene(i);
  3332. idx--;
  3333. }
  3334. }
  3335. }
  3336. editor_data.clear_editor_states();
  3337. scene_tabs->update_scene_tabs();
  3338. return idx;
  3339. }
  3340. 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) {
  3341. if (!is_inside_tree()) {
  3342. defer_load_scene = p_scene;
  3343. return OK;
  3344. }
  3345. if (!p_set_inherited) {
  3346. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  3347. if (editor_data.get_scene_path(i) == p_scene) {
  3348. _set_current_scene(i);
  3349. return OK;
  3350. }
  3351. }
  3352. if (!p_force_open_imported && FileAccess::exists(p_scene + ".import")) {
  3353. 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()));
  3354. open_imported->popup_centered();
  3355. new_inherited_button->grab_focus();
  3356. open_import_request = p_scene;
  3357. return OK;
  3358. }
  3359. }
  3360. if (p_clear_errors) {
  3361. load_errors->clear();
  3362. }
  3363. String lpath = ProjectSettings::get_singleton()->localize_path(p_scene);
  3364. if (!lpath.begins_with("res://")) {
  3365. 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"));
  3366. opening_prev = false;
  3367. return ERR_FILE_NOT_FOUND;
  3368. }
  3369. int prev = editor_data.get_edited_scene();
  3370. int idx = prev;
  3371. if (prev == -1 || editor_data.get_edited_scene_root() || !editor_data.get_scene_path(prev).is_empty()) {
  3372. idx = editor_data.add_edited_scene(-1);
  3373. if (p_silent_change_tab) {
  3374. _set_current_scene_nocheck(idx);
  3375. } else {
  3376. _set_current_scene(idx);
  3377. }
  3378. } else {
  3379. EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id());
  3380. }
  3381. dependency_errors.clear();
  3382. Error err;
  3383. Ref<PackedScene> sdata = ResourceLoader::load(lpath, "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
  3384. if (!p_ignore_broken_deps && dependency_errors.has(lpath)) {
  3385. current_menu_option = -1;
  3386. Vector<String> errors;
  3387. for (const String &E : dependency_errors[lpath]) {
  3388. errors.push_back(E);
  3389. }
  3390. dependency_error->show(DependencyErrorDialog::MODE_SCENE, lpath, errors);
  3391. opening_prev = false;
  3392. if (prev != -1 && prev != idx) {
  3393. _set_current_scene(prev);
  3394. editor_data.remove_scene(idx);
  3395. }
  3396. return ERR_FILE_MISSING_DEPENDENCIES;
  3397. }
  3398. if (!sdata.is_valid()) {
  3399. _dialog_display_load_error(lpath, err);
  3400. opening_prev = false;
  3401. if (prev != -1 && prev != idx) {
  3402. _set_current_scene(prev);
  3403. editor_data.remove_scene(idx);
  3404. }
  3405. return ERR_FILE_NOT_FOUND;
  3406. }
  3407. dependency_errors.erase(lpath); // At least not self path.
  3408. for (KeyValue<String, HashSet<String>> &E : dependency_errors) {
  3409. String txt = vformat(TTR("Scene '%s' has broken dependencies:"), E.key) + "\n";
  3410. for (const String &F : E.value) {
  3411. txt += "\t" + F + "\n";
  3412. }
  3413. add_io_error(txt);
  3414. }
  3415. if (ResourceCache::has(lpath)) {
  3416. // Used from somewhere else? No problem! Update state and replace sdata.
  3417. Ref<PackedScene> ps = ResourceCache::get_ref(lpath);
  3418. if (ps.is_valid()) {
  3419. ps->replace_state(sdata->get_state());
  3420. ps->set_last_modified_time(sdata->get_last_modified_time());
  3421. sdata = ps;
  3422. }
  3423. } else {
  3424. sdata->set_path(lpath, true); // Take over path.
  3425. }
  3426. Node *new_scene = sdata->instantiate(p_set_inherited ? PackedScene::GEN_EDIT_STATE_MAIN_INHERITED : PackedScene::GEN_EDIT_STATE_MAIN);
  3427. if (!new_scene) {
  3428. sdata.unref();
  3429. _dialog_display_load_error(lpath, ERR_FILE_CORRUPT);
  3430. opening_prev = false;
  3431. if (prev != -1 && prev != idx) {
  3432. _set_current_scene(prev);
  3433. editor_data.remove_scene(idx);
  3434. }
  3435. return ERR_FILE_CORRUPT;
  3436. }
  3437. if (p_set_inherited) {
  3438. Ref<SceneState> state = sdata->get_state();
  3439. state->set_path(lpath);
  3440. new_scene->set_scene_inherited_state(state);
  3441. new_scene->set_scene_file_path(String());
  3442. }
  3443. new_scene->set_scene_instance_state(Ref<SceneState>());
  3444. set_edited_scene(new_scene);
  3445. String config_file_path = EditorPaths::get_singleton()->get_project_settings_dir().path_join(p_scene.get_file() + "-editstate-" + p_scene.md5_text() + ".cfg");
  3446. Ref<ConfigFile> editor_state_cf;
  3447. editor_state_cf.instantiate();
  3448. Error editor_state_cf_err = editor_state_cf->load(config_file_path);
  3449. if (editor_state_cf_err == OK || editor_state_cf->has_section("editor_states")) {
  3450. _load_editor_plugin_states_from_config(editor_state_cf);
  3451. }
  3452. if (editor_folding.has_folding_data(lpath)) {
  3453. editor_folding.load_scene_folding(new_scene, lpath);
  3454. } else if (EDITOR_GET("interface/inspector/auto_unfold_foreign_scenes")) {
  3455. editor_folding.unfold_scene(new_scene);
  3456. editor_folding.save_scene_folding(new_scene, lpath);
  3457. }
  3458. opening_prev = false;
  3459. EditorDebuggerNode::get_singleton()->update_live_edit_root();
  3460. if (restoring_scenes) {
  3461. // Initialize history for restored scenes.
  3462. ObjectID id = new_scene->get_instance_id();
  3463. if (id != editor_history.get_current()) {
  3464. editor_history.add_object(id);
  3465. }
  3466. }
  3467. // Load the selected nodes.
  3468. if (editor_state_cf->has_section_key("editor_states", "selected_nodes")) {
  3469. TypedArray<NodePath> selected_node_list = editor_state_cf->get_value("editor_states", "selected_nodes", TypedArray<String>());
  3470. for (int i = 0; i < selected_node_list.size(); i++) {
  3471. Node *selected_node = new_scene->get_node_or_null(selected_node_list[i]);
  3472. if (selected_node) {
  3473. editor_selection->add_node(selected_node);
  3474. }
  3475. }
  3476. }
  3477. if (!restoring_scenes) {
  3478. save_editor_layout_delayed();
  3479. }
  3480. if (p_set_inherited) {
  3481. EditorUndoRedoManager::get_singleton()->set_history_as_unsaved(editor_data.get_current_edited_scene_history_id());
  3482. }
  3483. _update_title();
  3484. scene_tabs->update_scene_tabs();
  3485. _add_to_recent_scenes(lpath);
  3486. return OK;
  3487. }
  3488. HashMap<StringName, Variant> EditorNode::get_modified_properties_for_node(Node *p_node) {
  3489. HashMap<StringName, Variant> modified_property_map;
  3490. List<PropertyInfo> pinfo;
  3491. p_node->get_property_list(&pinfo);
  3492. for (const PropertyInfo &E : pinfo) {
  3493. if (E.usage & PROPERTY_USAGE_STORAGE) {
  3494. bool is_valid_revert = false;
  3495. Variant revert_value = EditorPropertyRevert::get_property_revert_value(p_node, E.name, &is_valid_revert);
  3496. Variant current_value = p_node->get(E.name);
  3497. if (is_valid_revert) {
  3498. if (PropertyUtils::is_property_value_different(current_value, revert_value)) {
  3499. modified_property_map[E.name] = current_value;
  3500. }
  3501. }
  3502. }
  3503. }
  3504. return modified_property_map;
  3505. }
  3506. void EditorNode::update_ownership_table_for_addition_node_ancestors(Node *p_current_node, HashMap<Node *, Node *> &p_ownership_table) {
  3507. p_ownership_table.insert(p_current_node, p_current_node->get_owner());
  3508. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  3509. Node *child = p_current_node->get_child(i);
  3510. update_ownership_table_for_addition_node_ancestors(child, p_ownership_table);
  3511. }
  3512. }
  3513. void EditorNode::update_diff_data_for_node(
  3514. Node *p_edited_scene,
  3515. Node *p_root,
  3516. Node *p_node,
  3517. HashMap<NodePath, ModificationNodeEntry> &p_modification_table,
  3518. List<AdditiveNodeEntry> &p_addition_list) {
  3519. bool node_part_of_subscene = p_node != p_edited_scene &&
  3520. p_edited_scene->get_scene_inherited_state().is_valid() &&
  3521. p_edited_scene->get_scene_inherited_state()->find_node_by_path(p_edited_scene->get_path_to(p_node)) >= 0;
  3522. // Loop through the owners until either we reach the root node or nullptr
  3523. Node *valid_node_owner = p_node->get_owner();
  3524. while (valid_node_owner) {
  3525. if (valid_node_owner == p_root) {
  3526. break;
  3527. }
  3528. valid_node_owner = valid_node_owner->get_owner();
  3529. }
  3530. if ((valid_node_owner == p_root && (p_root != p_edited_scene || !p_edited_scene->get_scene_file_path().is_empty())) || node_part_of_subscene || p_node == p_root) {
  3531. HashMap<StringName, Variant> modified_properties = get_modified_properties_for_node(p_node);
  3532. // Find all valid connections to other nodes.
  3533. List<Connection> connections_to;
  3534. p_node->get_all_signal_connections(&connections_to);
  3535. List<ConnectionWithNodePath> valid_connections_to;
  3536. for (const Connection &c : connections_to) {
  3537. Node *connection_target_node = Object::cast_to<Node>(c.callable.get_object());
  3538. if (connection_target_node) {
  3539. // TODO: add support for reinstating custom callables
  3540. if (!c.callable.is_custom()) {
  3541. ConnectionWithNodePath connection_to;
  3542. connection_to.connection = c;
  3543. connection_to.node_path = p_node->get_path_to(connection_target_node);
  3544. valid_connections_to.push_back(connection_to);
  3545. }
  3546. }
  3547. }
  3548. // Find all valid connections from other nodes.
  3549. List<Connection> connections_from;
  3550. p_node->get_signals_connected_to_this(&connections_from);
  3551. List<Connection> valid_connections_from;
  3552. for (const Connection &c : connections_from) {
  3553. Node *source_node = Object::cast_to<Node>(c.signal.get_object());
  3554. Node *valid_source_owner = nullptr;
  3555. if (source_node) {
  3556. valid_source_owner = source_node->get_owner();
  3557. while (valid_source_owner) {
  3558. if (valid_source_owner == p_root) {
  3559. break;
  3560. }
  3561. valid_source_owner = valid_source_owner->get_owner();
  3562. }
  3563. }
  3564. if (!source_node || valid_source_owner == nullptr) {
  3565. // TODO: add support for reinstating custom callables
  3566. if (!c.callable.is_custom()) {
  3567. valid_connections_from.push_back(c);
  3568. }
  3569. }
  3570. }
  3571. // Find all node groups.
  3572. List<Node::GroupInfo> groups;
  3573. p_node->get_groups(&groups);
  3574. if (!modified_properties.is_empty() || !valid_connections_to.is_empty() || !valid_connections_from.is_empty() || !groups.is_empty()) {
  3575. ModificationNodeEntry modification_node_entry;
  3576. modification_node_entry.property_table = modified_properties;
  3577. modification_node_entry.connections_to = valid_connections_to;
  3578. modification_node_entry.connections_from = valid_connections_from;
  3579. modification_node_entry.groups = groups;
  3580. p_modification_table[p_root->get_path_to(p_node)] = modification_node_entry;
  3581. }
  3582. } else {
  3583. AdditiveNodeEntry new_additive_node_entry;
  3584. new_additive_node_entry.node = p_node;
  3585. new_additive_node_entry.parent = p_root->get_path_to(p_node->get_parent());
  3586. new_additive_node_entry.owner = p_node->get_owner();
  3587. new_additive_node_entry.index = p_node->get_index();
  3588. Node2D *node_2d = Object::cast_to<Node2D>(p_node);
  3589. if (node_2d) {
  3590. new_additive_node_entry.transform_2d = node_2d->get_relative_transform_to_parent(node_2d->get_parent());
  3591. }
  3592. Node3D *node_3d = Object::cast_to<Node3D>(p_node);
  3593. if (node_3d) {
  3594. new_additive_node_entry.transform_3d = node_3d->get_relative_transform(node_3d->get_parent());
  3595. }
  3596. // Gathers the ownership of all ancestor nodes for later use.
  3597. HashMap<Node *, Node *> ownership_table;
  3598. for (int i = 0; i < p_node->get_child_count(); i++) {
  3599. Node *child = p_node->get_child(i);
  3600. update_ownership_table_for_addition_node_ancestors(child, ownership_table);
  3601. }
  3602. new_additive_node_entry.ownership_table = ownership_table;
  3603. p_addition_list.push_back(new_additive_node_entry);
  3604. return;
  3605. }
  3606. for (int i = 0; i < p_node->get_child_count(); i++) {
  3607. Node *child = p_node->get_child(i);
  3608. update_diff_data_for_node(p_edited_scene, p_root, child, p_modification_table, p_addition_list);
  3609. }
  3610. }
  3611. //
  3612. void EditorNode::open_request(const String &p_path) {
  3613. if (!opening_prev) {
  3614. List<String>::Element *prev_scene_item = previous_scenes.find(p_path);
  3615. if (prev_scene_item != nullptr) {
  3616. prev_scene_item->erase();
  3617. }
  3618. }
  3619. load_scene(p_path); // As it will be opened in separate tab.
  3620. }
  3621. bool EditorNode::has_previous_scenes() const {
  3622. return !previous_scenes.is_empty();
  3623. }
  3624. void EditorNode::edit_foreign_resource(Ref<Resource> p_resource) {
  3625. load_scene(p_resource->get_path().get_slice("::", 0));
  3626. callable_mp(InspectorDock::get_singleton(), &InspectorDock::edit_resource).call_deferred(p_resource);
  3627. }
  3628. bool EditorNode::is_resource_read_only(Ref<Resource> p_resource, bool p_foreign_resources_are_writable) {
  3629. ERR_FAIL_COND_V(p_resource.is_null(), false);
  3630. String path = p_resource->get_path();
  3631. if (!path.is_resource_file()) {
  3632. // If the resource name contains '::', that means it is a subresource embedded in another resource.
  3633. int srpos = path.find("::");
  3634. if (srpos != -1) {
  3635. String base = path.substr(0, srpos);
  3636. // If the base resource is a packed scene, we treat it as read-only if it is not the currently edited scene.
  3637. if (ResourceLoader::get_resource_type(base) == "PackedScene") {
  3638. if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
  3639. // If we have not flagged foreign resources as writable or the base scene the resource is
  3640. // part was imported, it can be considered read-only.
  3641. if (!p_foreign_resources_are_writable || FileAccess::exists(base + ".import")) {
  3642. return true;
  3643. }
  3644. }
  3645. } else {
  3646. // If a corresponding .import file exists for the base file, we assume it to be imported and should therefore treated as read-only.
  3647. if (FileAccess::exists(base + ".import")) {
  3648. return true;
  3649. }
  3650. }
  3651. }
  3652. } else if (FileAccess::exists(path + ".import")) {
  3653. // The resource is not a subresource, but if it has an .import file, it's imported so treat it as read only.
  3654. return true;
  3655. }
  3656. return false;
  3657. }
  3658. void EditorNode::request_instantiate_scene(const String &p_path) {
  3659. SceneTreeDock::get_singleton()->instantiate(p_path);
  3660. }
  3661. void EditorNode::request_instantiate_scenes(const Vector<String> &p_files) {
  3662. SceneTreeDock::get_singleton()->instantiate_scenes(p_files);
  3663. }
  3664. String EditorNode::get_multiwindow_support_tooltip_text() const {
  3665. if (SceneTree::get_singleton()->get_root()->is_embedding_subwindows()) {
  3666. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SUBWINDOWS)) {
  3667. return TTR("Multi-window support is not available because the `--single-window` command line argument was used to start the editor.");
  3668. } else {
  3669. return TTR("Multi-window support is not available because the current platform doesn't support multiple windows.");
  3670. }
  3671. } else if (EDITOR_GET("interface/editor/single_window_mode")) {
  3672. return TTR("Multi-window support is not available because Interface > Editor > Single Window Mode is enabled in the editor settings.");
  3673. }
  3674. return TTR("Multi-window support is not available because Interface > Multi Window > Enable is disabled in the editor settings.");
  3675. }
  3676. void EditorNode::_inherit_request(String p_file) {
  3677. current_menu_option = FILE_NEW_INHERITED_SCENE;
  3678. _dialog_action(p_file);
  3679. }
  3680. void EditorNode::_instantiate_request(const Vector<String> &p_files) {
  3681. request_instantiate_scenes(p_files);
  3682. }
  3683. void EditorNode::_close_messages() {
  3684. old_split_ofs = center_split->get_split_offset();
  3685. center_split->set_split_offset(0);
  3686. }
  3687. void EditorNode::_show_messages() {
  3688. center_split->set_split_offset(old_split_ofs);
  3689. }
  3690. void EditorNode::_add_to_recent_scenes(const String &p_scene) {
  3691. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
  3692. if (rc.has(p_scene)) {
  3693. rc.erase(p_scene);
  3694. }
  3695. rc.push_front(p_scene);
  3696. if (rc.size() > 10) {
  3697. rc.resize(10);
  3698. }
  3699. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", rc);
  3700. _update_recent_scenes();
  3701. }
  3702. void EditorNode::_open_recent_scene(int p_idx) {
  3703. if (p_idx == recent_scenes->get_item_count() - 1) {
  3704. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", Array());
  3705. callable_mp(this, &EditorNode::_update_recent_scenes).call_deferred();
  3706. } else {
  3707. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
  3708. ERR_FAIL_INDEX(p_idx, rc.size());
  3709. if (load_scene(rc[p_idx]) != OK) {
  3710. rc.remove_at(p_idx);
  3711. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scenes", rc);
  3712. _update_recent_scenes();
  3713. }
  3714. }
  3715. }
  3716. void EditorNode::_update_recent_scenes() {
  3717. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scenes", Array());
  3718. recent_scenes->clear();
  3719. String path;
  3720. for (int i = 0; i < rc.size(); i++) {
  3721. path = rc[i];
  3722. recent_scenes->add_item(path.replace("res://", ""), i);
  3723. }
  3724. recent_scenes->add_separator();
  3725. recent_scenes->add_shortcut(ED_SHORTCUT("editor/clear_recent", TTR("Clear Recent Scenes")));
  3726. recent_scenes->reset_size();
  3727. }
  3728. void EditorNode::_quick_opened() {
  3729. Vector<String> files = quick_open->get_selected_files();
  3730. bool open_scene_dialog = quick_open->get_base_type() == "PackedScene";
  3731. for (int i = 0; i < files.size(); i++) {
  3732. const String &res_path = files[i];
  3733. if (open_scene_dialog || ClassDB::is_parent_class(ResourceLoader::get_resource_type(res_path), "PackedScene")) {
  3734. open_request(res_path);
  3735. } else {
  3736. load_resource(res_path);
  3737. }
  3738. }
  3739. }
  3740. void EditorNode::_project_run_started() {
  3741. if (bool(EDITOR_GET("run/output/always_clear_output_on_play"))) {
  3742. log->clear();
  3743. }
  3744. if (bool(EDITOR_GET("run/output/always_open_output_on_play"))) {
  3745. bottom_panel->make_item_visible(log);
  3746. }
  3747. }
  3748. void EditorNode::_project_run_stopped() {
  3749. if (!bool(EDITOR_GET("run/output/always_close_output_on_stop"))) {
  3750. return;
  3751. }
  3752. bottom_panel->make_item_visible(log, false);
  3753. }
  3754. void EditorNode::notify_all_debug_sessions_exited() {
  3755. project_run_bar->stop_playing();
  3756. }
  3757. void EditorNode::add_io_error(const String &p_error) {
  3758. DEV_ASSERT(Thread::get_caller_id() == Thread::get_main_id());
  3759. singleton->load_errors->add_image(singleton->theme->get_icon(SNAME("Error"), EditorStringName(EditorIcons)));
  3760. singleton->load_errors->add_text(p_error + "\n");
  3761. EditorInterface::get_singleton()->popup_dialog_centered_ratio(singleton->load_error_dialog, 0.5);
  3762. }
  3763. void EditorNode::add_io_warning(const String &p_warning) {
  3764. DEV_ASSERT(Thread::get_caller_id() == Thread::get_main_id());
  3765. singleton->load_errors->add_image(singleton->theme->get_icon(SNAME("Warning"), EditorStringName(EditorIcons)));
  3766. singleton->load_errors->add_text(p_warning + "\n");
  3767. EditorInterface::get_singleton()->popup_dialog_centered_ratio(singleton->load_error_dialog, 0.5);
  3768. }
  3769. bool EditorNode::_find_scene_in_use(Node *p_node, const String &p_path) const {
  3770. if (p_node->get_scene_file_path() == p_path) {
  3771. return true;
  3772. }
  3773. for (int i = 0; i < p_node->get_child_count(); i++) {
  3774. if (_find_scene_in_use(p_node->get_child(i), p_path)) {
  3775. return true;
  3776. }
  3777. }
  3778. return false;
  3779. }
  3780. bool EditorNode::is_scene_in_use(const String &p_path) {
  3781. Node *es = get_edited_scene();
  3782. if (es) {
  3783. return _find_scene_in_use(es, p_path);
  3784. }
  3785. return false;
  3786. }
  3787. OS::ProcessID EditorNode::has_child_process(OS::ProcessID p_pid) const {
  3788. return project_run_bar->has_child_process(p_pid);
  3789. }
  3790. void EditorNode::stop_child_process(OS::ProcessID p_pid) {
  3791. project_run_bar->stop_child_process(p_pid);
  3792. }
  3793. Ref<Script> EditorNode::get_object_custom_type_base(const Object *p_object) const {
  3794. ERR_FAIL_NULL_V(p_object, nullptr);
  3795. Ref<Script> scr = p_object->get_script();
  3796. if (scr.is_valid()) {
  3797. // Uncommenting would break things! Consider adding a parameter if you need it.
  3798. // StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
  3799. // if (name != StringName()) {
  3800. // return name;
  3801. // }
  3802. // TODO: Should probably be deprecated in 4.x
  3803. StringName base = scr->get_instance_base_type();
  3804. if (base != StringName() && EditorNode::get_editor_data().get_custom_types().has(base)) {
  3805. const Vector<EditorData::CustomType> &types = EditorNode::get_editor_data().get_custom_types()[base];
  3806. Ref<Script> base_scr = scr;
  3807. while (base_scr.is_valid()) {
  3808. for (int i = 0; i < types.size(); ++i) {
  3809. if (types[i].script == base_scr) {
  3810. return types[i].script;
  3811. }
  3812. }
  3813. base_scr = base_scr->get_base_script();
  3814. }
  3815. }
  3816. }
  3817. return nullptr;
  3818. }
  3819. StringName EditorNode::get_object_custom_type_name(const Object *p_object) const {
  3820. ERR_FAIL_NULL_V(p_object, StringName());
  3821. Ref<Script> scr = p_object->get_script();
  3822. if (scr.is_null() && Object::cast_to<Script>(p_object)) {
  3823. scr = p_object;
  3824. }
  3825. if (scr.is_valid()) {
  3826. Ref<Script> base_scr = scr;
  3827. while (base_scr.is_valid()) {
  3828. StringName name = EditorNode::get_editor_data().script_class_get_name(base_scr->get_path());
  3829. if (name != StringName()) {
  3830. return name;
  3831. }
  3832. // TODO: Should probably be deprecated in 4.x.
  3833. StringName base = base_scr->get_instance_base_type();
  3834. if (base != StringName() && EditorNode::get_editor_data().get_custom_types().has(base)) {
  3835. const Vector<EditorData::CustomType> &types = EditorNode::get_editor_data().get_custom_types()[base];
  3836. for (int i = 0; i < types.size(); ++i) {
  3837. if (types[i].script == base_scr) {
  3838. return types[i].name;
  3839. }
  3840. }
  3841. }
  3842. base_scr = base_scr->get_base_script();
  3843. }
  3844. }
  3845. return StringName();
  3846. }
  3847. void EditorNode::_pick_main_scene_custom_action(const String &p_custom_action_name) {
  3848. if (p_custom_action_name == "select_current") {
  3849. Node *scene = editor_data.get_edited_scene_root();
  3850. if (!scene) {
  3851. show_accept(TTR("There is no defined scene to run."), TTR("OK"));
  3852. return;
  3853. }
  3854. pick_main_scene->hide();
  3855. if (!FileAccess::exists(scene->get_scene_file_path())) {
  3856. current_menu_option = FILE_SAVE_AND_RUN_MAIN_SCENE;
  3857. _menu_option_confirm(FILE_SAVE_AS_SCENE, true);
  3858. file->set_title(TTR("Save scene before running..."));
  3859. } else {
  3860. current_menu_option = SETTINGS_PICK_MAIN_SCENE;
  3861. _dialog_action(scene->get_scene_file_path());
  3862. }
  3863. }
  3864. }
  3865. Ref<Texture2D> EditorNode::_get_class_or_script_icon(const String &p_class, const Ref<Script> &p_script, const String &p_fallback, bool p_fallback_script_to_theme) {
  3866. ERR_FAIL_COND_V_MSG(p_class.is_empty(), nullptr, "Class name cannot be empty.");
  3867. EditorData &ed = EditorNode::get_editor_data();
  3868. // Check for a script icon first.
  3869. if (p_script.is_valid()) {
  3870. Ref<Texture2D> script_icon = ed.get_script_icon(p_script);
  3871. if (script_icon.is_valid()) {
  3872. return script_icon;
  3873. }
  3874. if (p_fallback_script_to_theme) {
  3875. // Look for the native base type in the editor theme. This is relevant for
  3876. // scripts extending other scripts and for built-in classes.
  3877. String script_class_name = p_script->get_language()->get_global_class_name(p_script->get_path());
  3878. String base_type = ScriptServer::get_global_class_native_base(script_class_name);
  3879. if (theme.is_valid() && theme->has_icon(base_type, EditorStringName(EditorIcons))) {
  3880. return theme->get_icon(base_type, EditorStringName(EditorIcons));
  3881. }
  3882. }
  3883. }
  3884. // Script was not valid or didn't yield any useful values, try the class name
  3885. // directly.
  3886. // Check if the class name is an extension-defined type.
  3887. Ref<Texture2D> ext_icon = ed.extension_class_get_icon(p_class);
  3888. if (ext_icon.is_valid()) {
  3889. return ext_icon;
  3890. }
  3891. // Check if the class name is a custom type.
  3892. // TODO: Should probably be deprecated in 4.x
  3893. const EditorData::CustomType *ctype = ed.get_custom_type_by_name(p_class);
  3894. if (ctype && ctype->icon.is_valid()) {
  3895. return ctype->icon;
  3896. }
  3897. // Look up the class name or the fallback name in the editor theme.
  3898. // This is only relevant for built-in classes.
  3899. if (theme.is_valid()) {
  3900. if (theme->has_icon(p_class, EditorStringName(EditorIcons))) {
  3901. return theme->get_icon(p_class, EditorStringName(EditorIcons));
  3902. }
  3903. if (!p_fallback.is_empty() && theme->has_icon(p_fallback, EditorStringName(EditorIcons))) {
  3904. return theme->get_icon(p_fallback, EditorStringName(EditorIcons));
  3905. }
  3906. // If the fallback is empty or wasn't found, use the default fallback.
  3907. if (ClassDB::class_exists(p_class)) {
  3908. bool instantiable = !ClassDB::is_virtual(p_class) && ClassDB::can_instantiate(p_class);
  3909. if (ClassDB::is_parent_class(p_class, SNAME("Node"))) {
  3910. return theme->get_icon(instantiable ? "Node" : "NodeDisabled", EditorStringName(EditorIcons));
  3911. } else {
  3912. return theme->get_icon(instantiable ? "Object" : "ObjectDisabled", EditorStringName(EditorIcons));
  3913. }
  3914. }
  3915. }
  3916. return nullptr;
  3917. }
  3918. Ref<Texture2D> EditorNode::get_object_icon(const Object *p_object, const String &p_fallback) {
  3919. ERR_FAIL_NULL_V_MSG(p_object, nullptr, "Object cannot be null.");
  3920. Ref<Script> scr = p_object->get_script();
  3921. if (scr.is_null() && p_object->is_class("Script")) {
  3922. scr = p_object;
  3923. }
  3924. return _get_class_or_script_icon(p_object->get_class(), scr, p_fallback);
  3925. }
  3926. Ref<Texture2D> EditorNode::get_class_icon(const String &p_class, const String &p_fallback) {
  3927. ERR_FAIL_COND_V_MSG(p_class.is_empty(), nullptr, "Class name cannot be empty.");
  3928. Ref<Script> scr;
  3929. if (ScriptServer::is_global_class(p_class)) {
  3930. scr = EditorNode::get_editor_data().script_class_load_script(p_class);
  3931. }
  3932. return _get_class_or_script_icon(p_class, scr, p_fallback, true);
  3933. }
  3934. bool EditorNode::is_object_of_custom_type(const Object *p_object, const StringName &p_class) {
  3935. ERR_FAIL_NULL_V(p_object, false);
  3936. Ref<Script> scr = p_object->get_script();
  3937. if (scr.is_null() && Object::cast_to<Script>(p_object)) {
  3938. scr = p_object;
  3939. }
  3940. if (scr.is_valid()) {
  3941. Ref<Script> base_script = scr;
  3942. while (base_script.is_valid()) {
  3943. StringName name = EditorNode::get_editor_data().script_class_get_name(base_script->get_path());
  3944. if (name == p_class) {
  3945. return true;
  3946. }
  3947. base_script = base_script->get_base_script();
  3948. }
  3949. }
  3950. return false;
  3951. }
  3952. void EditorNode::progress_add_task(const String &p_task, const String &p_label, int p_steps, bool p_can_cancel) {
  3953. if (singleton->cmdline_export_mode) {
  3954. print_line(p_task + ": begin: " + p_label + " steps: " + itos(p_steps));
  3955. } else if (singleton->progress_dialog) {
  3956. singleton->progress_dialog->add_task(p_task, p_label, p_steps, p_can_cancel);
  3957. }
  3958. }
  3959. bool EditorNode::progress_task_step(const String &p_task, const String &p_state, int p_step, bool p_force_refresh) {
  3960. if (singleton->cmdline_export_mode) {
  3961. print_line("\t" + p_task + ": step " + itos(p_step) + ": " + p_state);
  3962. return false;
  3963. } else if (singleton->progress_dialog) {
  3964. return singleton->progress_dialog->task_step(p_task, p_state, p_step, p_force_refresh);
  3965. } else {
  3966. return false;
  3967. }
  3968. }
  3969. void EditorNode::progress_end_task(const String &p_task) {
  3970. if (singleton->cmdline_export_mode) {
  3971. print_line(p_task + ": end");
  3972. } else if (singleton->progress_dialog) {
  3973. singleton->progress_dialog->end_task(p_task);
  3974. }
  3975. }
  3976. void EditorNode::progress_add_task_bg(const String &p_task, const String &p_label, int p_steps) {
  3977. singleton->progress_hb->add_task(p_task, p_label, p_steps);
  3978. }
  3979. void EditorNode::progress_task_step_bg(const String &p_task, int p_step) {
  3980. singleton->progress_hb->task_step(p_task, p_step);
  3981. }
  3982. void EditorNode::progress_end_task_bg(const String &p_task) {
  3983. singleton->progress_hb->end_task(p_task);
  3984. }
  3985. String EditorNode::_get_system_info() const {
  3986. String distribution_name = OS::get_singleton()->get_distribution_name();
  3987. if (distribution_name.is_empty()) {
  3988. distribution_name = OS::get_singleton()->get_name();
  3989. }
  3990. if (distribution_name.is_empty()) {
  3991. distribution_name = "Other";
  3992. }
  3993. const String distribution_version = OS::get_singleton()->get_version();
  3994. String godot_version = "Godot v" + String(VERSION_FULL_CONFIG);
  3995. if (String(VERSION_BUILD) != "official") {
  3996. String hash = String(VERSION_HASH);
  3997. hash = hash.is_empty() ? String("unknown") : vformat("(%s)", hash.left(9));
  3998. godot_version += " " + hash;
  3999. }
  4000. #ifdef LINUXBSD_ENABLED
  4001. const String display_server = OS::get_singleton()->get_environment("XDG_SESSION_TYPE").capitalize().replace(" ", ""); // `replace` is necessary, because `capitalize` introduces a whitespace between "x" and "11".
  4002. #endif // LINUXBSD_ENABLED
  4003. String driver_name = GLOBAL_GET("rendering/rendering_device/driver");
  4004. String rendering_method = GLOBAL_GET("rendering/renderer/rendering_method");
  4005. const String rendering_device_name = RenderingServer::get_singleton()->get_video_adapter_name();
  4006. RenderingDevice::DeviceType device_type = RenderingServer::get_singleton()->get_video_adapter_type();
  4007. String device_type_string;
  4008. switch (device_type) {
  4009. case RenderingDevice::DeviceType::DEVICE_TYPE_INTEGRATED_GPU:
  4010. device_type_string = "integrated";
  4011. break;
  4012. case RenderingDevice::DeviceType::DEVICE_TYPE_DISCRETE_GPU:
  4013. device_type_string = "dedicated";
  4014. break;
  4015. case RenderingDevice::DeviceType::DEVICE_TYPE_VIRTUAL_GPU:
  4016. device_type_string = "virtual";
  4017. break;
  4018. case RenderingDevice::DeviceType::DEVICE_TYPE_CPU:
  4019. device_type_string = "(software emulation on CPU)";
  4020. break;
  4021. case RenderingDevice::DeviceType::DEVICE_TYPE_OTHER:
  4022. case RenderingDevice::DeviceType::DEVICE_TYPE_MAX:
  4023. break; // Can't happen, but silences warning for DEVICE_TYPE_MAX
  4024. }
  4025. const Vector<String> video_adapter_driver_info = OS::get_singleton()->get_video_adapter_driver_info();
  4026. const String processor_name = OS::get_singleton()->get_processor_name();
  4027. const int processor_count = OS::get_singleton()->get_processor_count();
  4028. // Prettify
  4029. if (rendering_method == "forward_plus") {
  4030. rendering_method = "Forward+";
  4031. } else if (rendering_method == "mobile") {
  4032. rendering_method = "Mobile";
  4033. } else if (rendering_method == "gl_compatibility") {
  4034. rendering_method = "Compatibility";
  4035. driver_name = GLOBAL_GET("rendering/gl_compatibility/driver");
  4036. }
  4037. if (driver_name == "vulkan") {
  4038. driver_name = "Vulkan";
  4039. } else if (driver_name.begins_with("opengl3")) {
  4040. driver_name = "GLES3";
  4041. }
  4042. // Join info.
  4043. Vector<String> info;
  4044. info.push_back(godot_version);
  4045. if (!distribution_version.is_empty()) {
  4046. info.push_back(distribution_name + " " + distribution_version);
  4047. } else {
  4048. info.push_back(distribution_name);
  4049. }
  4050. #ifdef LINUXBSD_ENABLED
  4051. if (!display_server.is_empty()) {
  4052. info.push_back(display_server);
  4053. }
  4054. #endif // LINUXBSD_ENABLED
  4055. info.push_back(vformat("%s (%s)", driver_name, rendering_method));
  4056. String graphics;
  4057. if (!device_type_string.is_empty()) {
  4058. graphics = device_type_string + " ";
  4059. }
  4060. graphics += rendering_device_name;
  4061. if (video_adapter_driver_info.size() == 2) { // This vector is always either of length 0 or 2.
  4062. const String &vad_name = video_adapter_driver_info[0];
  4063. const String &vad_version = video_adapter_driver_info[1]; // Version could be potentially empty on Linux/BSD.
  4064. if (!vad_version.is_empty()) {
  4065. graphics += vformat(" (%s; %s)", vad_name, vad_version);
  4066. } else {
  4067. graphics += vformat(" (%s)", vad_name);
  4068. }
  4069. }
  4070. info.push_back(graphics);
  4071. info.push_back(vformat("%s (%d Threads)", processor_name, processor_count));
  4072. return String(" - ").join(info);
  4073. }
  4074. bool EditorNode::_should_display_update_spinner() const {
  4075. #ifdef DEV_ENABLED
  4076. const bool in_dev = true;
  4077. #else
  4078. const bool in_dev = false;
  4079. #endif
  4080. const int show_update_spinner_setting = EDITOR_GET("interface/editor/show_update_spinner");
  4081. return (show_update_spinner_setting == 0 && in_dev) || show_update_spinner_setting == 1;
  4082. }
  4083. Ref<Texture2D> EditorNode::_file_dialog_get_icon(const String &p_path) {
  4084. EditorFileSystemDirectory *efsd = EditorFileSystem::get_singleton()->get_filesystem_path(p_path.get_base_dir());
  4085. if (efsd) {
  4086. String file = p_path.get_file();
  4087. for (int i = 0; i < efsd->get_file_count(); i++) {
  4088. if (efsd->get_file(i) == file) {
  4089. String type = efsd->get_file_type(i);
  4090. if (singleton->icon_type_cache.has(type)) {
  4091. return singleton->icon_type_cache[type];
  4092. } else {
  4093. return singleton->icon_type_cache["Object"];
  4094. }
  4095. }
  4096. }
  4097. }
  4098. return singleton->icon_type_cache["Object"];
  4099. }
  4100. void EditorNode::_build_icon_type_cache() {
  4101. List<StringName> tl;
  4102. theme->get_icon_list(EditorStringName(EditorIcons), &tl);
  4103. for (const StringName &E : tl) {
  4104. if (!ClassDB::class_exists(E)) {
  4105. continue;
  4106. }
  4107. icon_type_cache[E] = theme->get_icon(E, EditorStringName(EditorIcons));
  4108. }
  4109. }
  4110. void EditorNode::_enable_pending_addons() {
  4111. for (uint32_t i = 0; i < pending_addons.size(); i++) {
  4112. set_addon_plugin_enabled(pending_addons[i], true);
  4113. }
  4114. pending_addons.clear();
  4115. }
  4116. void EditorNode::_file_dialog_register(FileDialog *p_dialog) {
  4117. singleton->file_dialogs.insert(p_dialog);
  4118. }
  4119. void EditorNode::_file_dialog_unregister(FileDialog *p_dialog) {
  4120. singleton->file_dialogs.erase(p_dialog);
  4121. }
  4122. void EditorNode::_editor_file_dialog_register(EditorFileDialog *p_dialog) {
  4123. singleton->editor_file_dialogs.insert(p_dialog);
  4124. }
  4125. void EditorNode::_editor_file_dialog_unregister(EditorFileDialog *p_dialog) {
  4126. singleton->editor_file_dialogs.erase(p_dialog);
  4127. }
  4128. Vector<EditorNodeInitCallback> EditorNode::_init_callbacks;
  4129. void EditorNode::_begin_first_scan() {
  4130. // In headless mode, scan right away.
  4131. // This allows users to continue using `godot --headless --editor --quit` to prepare a project.
  4132. if (!DisplayServer::get_singleton()->window_can_draw()) {
  4133. OS::get_singleton()->benchmark_begin_measure("Editor", "First Scan");
  4134. EditorFileSystem::get_singleton()->scan();
  4135. return;
  4136. }
  4137. if (!waiting_for_first_scan) {
  4138. return;
  4139. }
  4140. requested_first_scan = true;
  4141. }
  4142. Error EditorNode::export_preset(const String &p_preset, const String &p_path, bool p_debug, bool p_pack_only, bool p_android_build_template) {
  4143. export_defer.preset = p_preset;
  4144. export_defer.path = p_path;
  4145. export_defer.debug = p_debug;
  4146. export_defer.pack_only = p_pack_only;
  4147. export_defer.android_build_template = p_android_build_template;
  4148. cmdline_export_mode = true;
  4149. return OK;
  4150. }
  4151. bool EditorNode::is_project_exporting() const {
  4152. return project_export && project_export->is_exporting();
  4153. }
  4154. void EditorNode::show_accept(const String &p_text, const String &p_title) {
  4155. current_menu_option = -1;
  4156. if (accept) {
  4157. accept->set_ok_button_text(p_title);
  4158. accept->set_text(p_text);
  4159. EditorInterface::get_singleton()->popup_dialog_centered(accept);
  4160. }
  4161. }
  4162. void EditorNode::show_save_accept(const String &p_text, const String &p_title) {
  4163. current_menu_option = -1;
  4164. if (save_accept) {
  4165. save_accept->set_ok_button_text(p_title);
  4166. save_accept->set_text(p_text);
  4167. EditorInterface::get_singleton()->popup_dialog_centered(save_accept);
  4168. }
  4169. }
  4170. void EditorNode::show_warning(const String &p_text, const String &p_title) {
  4171. if (warning) {
  4172. warning->set_text(p_text);
  4173. warning->set_title(p_title);
  4174. EditorInterface::get_singleton()->popup_dialog_centered(warning);
  4175. } else {
  4176. WARN_PRINT(p_title + " " + p_text);
  4177. }
  4178. }
  4179. void EditorNode::_copy_warning(const String &p_str) {
  4180. DisplayServer::get_singleton()->clipboard_set(warning->get_text());
  4181. }
  4182. void EditorNode::_save_editor_layout() {
  4183. if (waiting_for_first_scan) {
  4184. return; // Scanning, do not touch docks.
  4185. }
  4186. Ref<ConfigFile> config;
  4187. config.instantiate();
  4188. // Load and amend existing config if it exists.
  4189. config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  4190. editor_dock_manager->save_docks_to_config(config, "docks");
  4191. _save_open_scenes_to_config(config);
  4192. _save_central_editor_layout_to_config(config);
  4193. editor_data.get_plugin_window_layout(config);
  4194. config->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  4195. }
  4196. void EditorNode::_save_open_scenes_to_config(Ref<ConfigFile> p_layout) {
  4197. PackedStringArray scenes;
  4198. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  4199. String path = editor_data.get_scene_path(i);
  4200. if (path.is_empty()) {
  4201. continue;
  4202. }
  4203. scenes.push_back(path);
  4204. }
  4205. p_layout->set_value(EDITOR_NODE_CONFIG_SECTION, "open_scenes", scenes);
  4206. String currently_edited_scene_path = editor_data.get_scene_path(editor_data.get_edited_scene());
  4207. p_layout->set_value(EDITOR_NODE_CONFIG_SECTION, "current_scene", currently_edited_scene_path);
  4208. }
  4209. void EditorNode::save_editor_layout_delayed() {
  4210. editor_layout_save_delay_timer->start();
  4211. }
  4212. void EditorNode::_load_editor_layout() {
  4213. Ref<ConfigFile> config;
  4214. config.instantiate();
  4215. Error err = config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  4216. if (err != OK) { // No config.
  4217. // If config is not found, expand the res:// folder and favorites by default.
  4218. TreeItem *root = FileSystemDock::get_singleton()->get_tree_control()->get_item_with_metadata("res://", 0);
  4219. if (root) {
  4220. root->set_collapsed(false);
  4221. }
  4222. TreeItem *favorites = FileSystemDock::get_singleton()->get_tree_control()->get_item_with_metadata("Favorites", 0);
  4223. if (favorites) {
  4224. favorites->set_collapsed(false);
  4225. }
  4226. if (overridden_default_layout >= 0) {
  4227. _layout_menu_option(overridden_default_layout);
  4228. }
  4229. return;
  4230. }
  4231. editor_dock_manager->load_docks_from_config(config, "docks");
  4232. _load_open_scenes_from_config(config);
  4233. _load_central_editor_layout_from_config(config);
  4234. editor_data.set_plugin_window_layout(config);
  4235. }
  4236. void EditorNode::_save_central_editor_layout_to_config(Ref<ConfigFile> p_config_file) {
  4237. // Bottom panel.
  4238. int center_split_offset = center_split->get_split_offset();
  4239. p_config_file->set_value(EDITOR_NODE_CONFIG_SECTION, "center_split_offset", center_split_offset);
  4240. bottom_panel->save_layout_to_config(p_config_file, EDITOR_NODE_CONFIG_SECTION);
  4241. // Debugger tab.
  4242. int selected_default_debugger_tab_idx = EditorDebuggerNode::get_singleton()->get_default_debugger()->get_current_debugger_tab();
  4243. p_config_file->set_value(EDITOR_NODE_CONFIG_SECTION, "selected_default_debugger_tab_idx", selected_default_debugger_tab_idx);
  4244. // Main editor (plugin).
  4245. int selected_main_editor_idx = -1;
  4246. for (int i = 0; i < main_editor_buttons.size(); i++) {
  4247. if (main_editor_buttons[i]->is_pressed()) {
  4248. selected_main_editor_idx = i;
  4249. break;
  4250. }
  4251. }
  4252. if (selected_main_editor_idx != -1) {
  4253. p_config_file->set_value(EDITOR_NODE_CONFIG_SECTION, "selected_main_editor_idx", selected_main_editor_idx);
  4254. } else {
  4255. p_config_file->set_value(EDITOR_NODE_CONFIG_SECTION, "selected_main_editor_idx", Variant());
  4256. }
  4257. }
  4258. void EditorNode::_load_central_editor_layout_from_config(Ref<ConfigFile> p_config_file) {
  4259. // Bottom panel.
  4260. bottom_panel->load_layout_from_config(p_config_file, EDITOR_NODE_CONFIG_SECTION);
  4261. if (p_config_file->has_section_key(EDITOR_NODE_CONFIG_SECTION, "center_split_offset")) {
  4262. int center_split_offset = p_config_file->get_value(EDITOR_NODE_CONFIG_SECTION, "center_split_offset");
  4263. center_split->set_split_offset(center_split_offset);
  4264. }
  4265. // Debugger tab.
  4266. if (p_config_file->has_section_key(EDITOR_NODE_CONFIG_SECTION, "selected_default_debugger_tab_idx")) {
  4267. int selected_default_debugger_tab_idx = p_config_file->get_value(EDITOR_NODE_CONFIG_SECTION, "selected_default_debugger_tab_idx");
  4268. EditorDebuggerNode::get_singleton()->get_default_debugger()->switch_to_debugger(selected_default_debugger_tab_idx);
  4269. }
  4270. // Main editor (plugin).
  4271. if (p_config_file->has_section_key(EDITOR_NODE_CONFIG_SECTION, "selected_main_editor_idx")) {
  4272. int selected_main_editor_idx = p_config_file->get_value(EDITOR_NODE_CONFIG_SECTION, "selected_main_editor_idx");
  4273. if (selected_main_editor_idx >= 0 && selected_main_editor_idx < main_editor_buttons.size()) {
  4274. callable_mp(this, &EditorNode::editor_select).call_deferred(selected_main_editor_idx);
  4275. }
  4276. }
  4277. }
  4278. void EditorNode::_load_open_scenes_from_config(Ref<ConfigFile> p_layout) {
  4279. if (!bool(EDITOR_GET("interface/scene_tabs/restore_scenes_on_load"))) {
  4280. return;
  4281. }
  4282. if (!p_layout->has_section(EDITOR_NODE_CONFIG_SECTION) ||
  4283. !p_layout->has_section_key(EDITOR_NODE_CONFIG_SECTION, "open_scenes")) {
  4284. return;
  4285. }
  4286. restoring_scenes = true;
  4287. PackedStringArray scenes = p_layout->get_value(EDITOR_NODE_CONFIG_SECTION, "open_scenes");
  4288. for (int i = 0; i < scenes.size(); i++) {
  4289. load_scene(scenes[i]);
  4290. }
  4291. if (p_layout->has_section_key(EDITOR_NODE_CONFIG_SECTION, "current_scene")) {
  4292. String current_scene = p_layout->get_value(EDITOR_NODE_CONFIG_SECTION, "current_scene");
  4293. int current_scene_idx = scenes.find(current_scene);
  4294. if (current_scene_idx >= 0) {
  4295. _set_current_scene(current_scene_idx);
  4296. }
  4297. }
  4298. save_editor_layout_delayed();
  4299. restoring_scenes = false;
  4300. }
  4301. bool EditorNode::has_scenes_in_session() {
  4302. if (!bool(EDITOR_GET("interface/scene_tabs/restore_scenes_on_load"))) {
  4303. return false;
  4304. }
  4305. Ref<ConfigFile> config;
  4306. config.instantiate();
  4307. Error err = config->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_layout.cfg"));
  4308. if (err != OK) {
  4309. return false;
  4310. }
  4311. if (!config->has_section(EDITOR_NODE_CONFIG_SECTION) || !config->has_section_key(EDITOR_NODE_CONFIG_SECTION, "open_scenes")) {
  4312. return false;
  4313. }
  4314. Array scenes = config->get_value(EDITOR_NODE_CONFIG_SECTION, "open_scenes");
  4315. return !scenes.is_empty();
  4316. }
  4317. bool EditorNode::ensure_main_scene(bool p_from_native) {
  4318. pick_main_scene->set_meta("from_native", p_from_native); // Whether from play button or native run.
  4319. String main_scene = GLOBAL_GET("application/run/main_scene");
  4320. if (main_scene.is_empty()) {
  4321. current_menu_option = -1;
  4322. 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."));
  4323. pick_main_scene->popup_centered();
  4324. if (editor_data.get_edited_scene_root()) {
  4325. select_current_scene_button->set_disabled(false);
  4326. select_current_scene_button->grab_focus();
  4327. } else {
  4328. select_current_scene_button->set_disabled(true);
  4329. }
  4330. return false;
  4331. }
  4332. if (!FileAccess::exists(main_scene)) {
  4333. current_menu_option = -1;
  4334. 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));
  4335. pick_main_scene->popup_centered();
  4336. return false;
  4337. }
  4338. if (ResourceLoader::get_resource_type(main_scene) != "PackedScene") {
  4339. current_menu_option = -1;
  4340. 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));
  4341. pick_main_scene->popup_centered();
  4342. return false;
  4343. }
  4344. return true;
  4345. }
  4346. void EditorNode::_immediate_dialog_confirmed() {
  4347. immediate_dialog_confirmed = true;
  4348. }
  4349. bool EditorNode::immediate_confirmation_dialog(const String &p_text, const String &p_ok_text, const String &p_cancel_text, uint32_t p_wrap_width) {
  4350. ConfirmationDialog *cd = memnew(ConfirmationDialog);
  4351. cd->set_text(p_text);
  4352. cd->set_ok_button_text(p_ok_text);
  4353. cd->set_cancel_button_text(p_cancel_text);
  4354. if (p_wrap_width > 0) {
  4355. cd->set_autowrap(true);
  4356. cd->get_label()->set_custom_minimum_size(Size2(p_wrap_width, 0) * EDSCALE);
  4357. }
  4358. cd->connect("confirmed", callable_mp(singleton, &EditorNode::_immediate_dialog_confirmed));
  4359. singleton->gui_base->add_child(cd);
  4360. cd->popup_centered();
  4361. while (true) {
  4362. OS::get_singleton()->delay_usec(1);
  4363. DisplayServer::get_singleton()->process_events();
  4364. Main::iteration();
  4365. if (singleton->immediate_dialog_confirmed || !cd->is_visible()) {
  4366. break;
  4367. }
  4368. }
  4369. memdelete(cd);
  4370. return singleton->immediate_dialog_confirmed;
  4371. }
  4372. void EditorNode::cleanup() {
  4373. _init_callbacks.clear();
  4374. }
  4375. void EditorNode::_update_layouts_menu() {
  4376. editor_layouts->clear();
  4377. overridden_default_layout = -1;
  4378. editor_layouts->reset_size();
  4379. editor_layouts->add_shortcut(ED_SHORTCUT("layout/save", TTR("Save Layout...")), SETTINGS_LAYOUT_SAVE);
  4380. editor_layouts->add_shortcut(ED_SHORTCUT("layout/delete", TTR("Delete Layout...")), SETTINGS_LAYOUT_DELETE);
  4381. editor_layouts->add_separator();
  4382. editor_layouts->add_shortcut(ED_SHORTCUT("layout/default", TTR("Default")), SETTINGS_LAYOUT_DEFAULT);
  4383. Ref<ConfigFile> config;
  4384. config.instantiate();
  4385. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  4386. if (err != OK) {
  4387. return; // No config.
  4388. }
  4389. List<String> layouts;
  4390. config.ptr()->get_sections(&layouts);
  4391. for (const String &layout : layouts) {
  4392. if (layout == TTR("Default")) {
  4393. editor_layouts->remove_item(editor_layouts->get_item_index(SETTINGS_LAYOUT_DEFAULT));
  4394. overridden_default_layout = editor_layouts->get_item_count();
  4395. }
  4396. editor_layouts->add_item(layout);
  4397. }
  4398. }
  4399. void EditorNode::_layout_menu_option(int p_id) {
  4400. switch (p_id) {
  4401. case SETTINGS_LAYOUT_SAVE: {
  4402. current_menu_option = p_id;
  4403. layout_dialog->set_title(TTR("Save Layout"));
  4404. layout_dialog->set_ok_button_text(TTR("Save"));
  4405. layout_dialog->set_name_line_enabled(true);
  4406. layout_dialog->popup_centered();
  4407. } break;
  4408. case SETTINGS_LAYOUT_DELETE: {
  4409. current_menu_option = p_id;
  4410. layout_dialog->set_title(TTR("Delete Layout"));
  4411. layout_dialog->set_ok_button_text(TTR("Delete"));
  4412. layout_dialog->set_name_line_enabled(false);
  4413. layout_dialog->popup_centered();
  4414. } break;
  4415. case SETTINGS_LAYOUT_DEFAULT: {
  4416. editor_dock_manager->load_docks_from_config(default_layout, "docks");
  4417. _save_editor_layout();
  4418. } break;
  4419. default: {
  4420. Ref<ConfigFile> config;
  4421. config.instantiate();
  4422. Error err = config->load(EditorSettings::get_singleton()->get_editor_layouts_config());
  4423. if (err != OK) {
  4424. return; // No config.
  4425. }
  4426. editor_dock_manager->load_docks_from_config(config, editor_layouts->get_item_text(p_id));
  4427. _save_editor_layout();
  4428. }
  4429. }
  4430. }
  4431. void EditorNode::_proceed_closing_scene_tabs() {
  4432. List<String>::Element *E = tabs_to_close.front();
  4433. if (!E) {
  4434. if (_is_closing_editor()) {
  4435. current_menu_option = tab_closing_menu_option;
  4436. _menu_option_confirm(tab_closing_menu_option, true);
  4437. } else {
  4438. current_menu_option = -1;
  4439. save_confirmation->hide();
  4440. }
  4441. return;
  4442. }
  4443. String scene_to_close = E->get();
  4444. tabs_to_close.pop_front();
  4445. int tab_idx = -1;
  4446. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  4447. if (editor_data.get_scene_path(i) == scene_to_close) {
  4448. tab_idx = i;
  4449. break;
  4450. }
  4451. }
  4452. ERR_FAIL_COND(tab_idx < 0);
  4453. _scene_tab_closed(tab_idx);
  4454. }
  4455. bool EditorNode::_is_closing_editor() const {
  4456. return tab_closing_menu_option == FILE_QUIT || tab_closing_menu_option == RUN_PROJECT_MANAGER || tab_closing_menu_option == RELOAD_CURRENT_PROJECT;
  4457. }
  4458. void EditorNode::_scene_tab_closed(int p_tab) {
  4459. current_menu_option = SCENE_TAB_CLOSE;
  4460. tab_closing_idx = p_tab;
  4461. Node *scene = editor_data.get_edited_scene_root(p_tab);
  4462. if (!scene) {
  4463. _discard_changes();
  4464. return;
  4465. }
  4466. String scene_filename = scene->get_scene_file_path();
  4467. String unsaved_message;
  4468. if (EditorUndoRedoManager::get_singleton()->is_history_unsaved(editor_data.get_scene_history_id(p_tab))) {
  4469. if (scene_filename.is_empty()) {
  4470. unsaved_message = TTR("This scene was never saved.");
  4471. } else {
  4472. // Consider editor startup to be a point of saving, so that when you
  4473. // close and reopen the editor, you don't get an excessively long
  4474. // "modified X hours ago".
  4475. const uint64_t last_modified_seconds = Time::get_singleton()->get_unix_time_from_system() - MAX(started_timestamp, FileAccess::get_modified_time(scene->get_scene_file_path()));
  4476. String last_modified_string;
  4477. if (last_modified_seconds < 120) {
  4478. last_modified_string = vformat(TTRN("%d second ago", "%d seconds ago", last_modified_seconds), last_modified_seconds);
  4479. } else if (last_modified_seconds < 7200) {
  4480. last_modified_string = vformat(TTRN("%d minute ago", "%d minutes ago", last_modified_seconds / 60), last_modified_seconds / 60);
  4481. } else {
  4482. last_modified_string = vformat(TTRN("%d hour ago", "%d hours ago", last_modified_seconds / 3600), last_modified_seconds / 3600);
  4483. }
  4484. unsaved_message = vformat(TTR("Scene \"%s\" has unsaved changes.\nLast saved: %s."), scene_filename, last_modified_string);
  4485. }
  4486. } else {
  4487. // Check if any plugin has unsaved changes in that scene.
  4488. for (int i = 0; i < editor_data.get_editor_plugin_count(); i++) {
  4489. unsaved_message = editor_data.get_editor_plugin(i)->get_unsaved_status(scene_filename);
  4490. if (!unsaved_message.is_empty()) {
  4491. break;
  4492. }
  4493. }
  4494. }
  4495. if (!unsaved_message.is_empty()) {
  4496. if (scene_tabs->get_current_tab() != p_tab) {
  4497. _set_current_scene(p_tab);
  4498. }
  4499. save_confirmation->set_ok_button_text(TTR("Save & Close"));
  4500. save_confirmation->set_text(unsaved_message + "\n\n" + TTR("Save before closing?"));
  4501. save_confirmation->reset_size();
  4502. save_confirmation->popup_centered();
  4503. } else {
  4504. _discard_changes();
  4505. }
  4506. save_editor_layout_delayed();
  4507. scene_tabs->update_scene_tabs();
  4508. }
  4509. void EditorNode::_cancel_close_scene_tab() {
  4510. if (_is_closing_editor()) {
  4511. tab_closing_menu_option = -1;
  4512. }
  4513. }
  4514. void EditorNode::_toggle_distraction_free_mode() {
  4515. if (EDITOR_GET("interface/editor/separate_distraction_mode")) {
  4516. int screen = -1;
  4517. for (int i = 0; i < editor_table.size(); i++) {
  4518. if (editor_plugin_screen == editor_table[i]) {
  4519. screen = i;
  4520. break;
  4521. }
  4522. }
  4523. if (screen == EDITOR_SCRIPT) {
  4524. script_distraction_free = !script_distraction_free;
  4525. set_distraction_free_mode(script_distraction_free);
  4526. } else {
  4527. scene_distraction_free = !scene_distraction_free;
  4528. set_distraction_free_mode(scene_distraction_free);
  4529. }
  4530. } else {
  4531. set_distraction_free_mode(distraction_free->is_pressed());
  4532. }
  4533. }
  4534. void EditorNode::set_distraction_free_mode(bool p_enter) {
  4535. distraction_free->set_pressed(p_enter);
  4536. if (p_enter) {
  4537. if (editor_dock_manager->are_docks_visible()) {
  4538. editor_dock_manager->set_docks_visible(false);
  4539. }
  4540. } else {
  4541. editor_dock_manager->set_docks_visible(true);
  4542. }
  4543. }
  4544. bool EditorNode::is_distraction_free_mode_enabled() const {
  4545. return distraction_free->is_pressed();
  4546. }
  4547. Dictionary EditorNode::drag_resource(const Ref<Resource> &p_res, Control *p_from) {
  4548. Control *drag_control = memnew(Control);
  4549. TextureRect *drag_preview = memnew(TextureRect);
  4550. Label *label = memnew(Label);
  4551. Ref<Texture2D> preview;
  4552. {
  4553. // TODO: make proper previews
  4554. Ref<ImageTexture> texture = theme->get_icon(SNAME("FileBigThumb"), EditorStringName(EditorIcons));
  4555. Ref<Image> img = texture->get_image();
  4556. img = img->duplicate();
  4557. img->resize(48, 48); // meh
  4558. preview = ImageTexture::create_from_image(img);
  4559. }
  4560. drag_preview->set_texture(preview);
  4561. drag_control->add_child(drag_preview);
  4562. if (p_res->get_path().is_resource_file()) {
  4563. label->set_text(p_res->get_path().get_file());
  4564. } else if (!p_res->get_name().is_empty()) {
  4565. label->set_text(p_res->get_name());
  4566. } else {
  4567. label->set_text(p_res->get_class());
  4568. }
  4569. drag_control->add_child(label);
  4570. p_from->set_drag_preview(drag_control); // Wait until it enters scene.
  4571. label->set_position(Point2((preview->get_width() - label->get_minimum_size().width) / 2, preview->get_height()));
  4572. Dictionary drag_data;
  4573. drag_data["type"] = "resource";
  4574. drag_data["resource"] = p_res;
  4575. drag_data["from"] = p_from;
  4576. return drag_data;
  4577. }
  4578. Dictionary EditorNode::drag_files_and_dirs(const Vector<String> &p_paths, Control *p_from) {
  4579. bool has_folder = false;
  4580. bool has_file = false;
  4581. for (int i = 0; i < p_paths.size(); i++) {
  4582. bool is_folder = p_paths[i].ends_with("/");
  4583. has_folder |= is_folder;
  4584. has_file |= !is_folder;
  4585. }
  4586. int max_rows = 6;
  4587. 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.
  4588. VBoxContainer *vbox = memnew(VBoxContainer);
  4589. for (int i = 0; i < num_rows; i++) {
  4590. HBoxContainer *hbox = memnew(HBoxContainer);
  4591. TextureRect *icon = memnew(TextureRect);
  4592. Label *label = memnew(Label);
  4593. if (p_paths[i].ends_with("/")) {
  4594. label->set_text(p_paths[i].substr(0, p_paths[i].length() - 1).get_file());
  4595. icon->set_texture(theme->get_icon(SNAME("Folder"), EditorStringName(EditorIcons)));
  4596. } else {
  4597. label->set_text(p_paths[i].get_file());
  4598. icon->set_texture(theme->get_icon(SNAME("File"), EditorStringName(EditorIcons)));
  4599. }
  4600. icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  4601. icon->set_size(Size2(16, 16));
  4602. hbox->add_child(icon);
  4603. hbox->add_child(label);
  4604. vbox->add_child(hbox);
  4605. }
  4606. if (p_paths.size() > num_rows) {
  4607. Label *label = memnew(Label);
  4608. if (has_file && has_folder) {
  4609. label->set_text(vformat(TTR("%d more files or folders"), p_paths.size() - num_rows));
  4610. } else if (has_folder) {
  4611. label->set_text(vformat(TTR("%d more folders"), p_paths.size() - num_rows));
  4612. } else {
  4613. label->set_text(vformat(TTR("%d more files"), p_paths.size() - num_rows));
  4614. }
  4615. vbox->add_child(label);
  4616. }
  4617. p_from->set_drag_preview(vbox); // Wait until it enters scene.
  4618. Dictionary drag_data;
  4619. drag_data["type"] = has_folder ? "files_and_dirs" : "files";
  4620. drag_data["files"] = p_paths;
  4621. drag_data["from"] = p_from;
  4622. return drag_data;
  4623. }
  4624. void EditorNode::add_tool_menu_item(const String &p_name, const Callable &p_callback) {
  4625. int idx = tool_menu->get_item_count();
  4626. tool_menu->add_item(p_name, TOOLS_CUSTOM);
  4627. tool_menu->set_item_metadata(idx, p_callback);
  4628. }
  4629. void EditorNode::add_tool_submenu_item(const String &p_name, PopupMenu *p_submenu) {
  4630. ERR_FAIL_NULL(p_submenu);
  4631. ERR_FAIL_COND(p_submenu->get_parent() != nullptr);
  4632. tool_menu->add_submenu_node_item(p_name, p_submenu, TOOLS_CUSTOM);
  4633. }
  4634. void EditorNode::remove_tool_menu_item(const String &p_name) {
  4635. for (int i = 0; i < tool_menu->get_item_count(); i++) {
  4636. if (tool_menu->get_item_id(i) != TOOLS_CUSTOM) {
  4637. continue;
  4638. }
  4639. if (tool_menu->get_item_text(i) == p_name) {
  4640. if (tool_menu->get_item_submenu(i) != "") {
  4641. Node *n = tool_menu->get_node(tool_menu->get_item_submenu(i));
  4642. tool_menu->remove_child(n);
  4643. memdelete(n);
  4644. }
  4645. tool_menu->remove_item(i);
  4646. tool_menu->reset_size();
  4647. return;
  4648. }
  4649. }
  4650. }
  4651. PopupMenu *EditorNode::get_export_as_menu() {
  4652. return export_as_menu;
  4653. }
  4654. void EditorNode::_dropped_files(const Vector<String> &p_files) {
  4655. String to_path = ProjectSettings::get_singleton()->globalize_path(FileSystemDock::get_singleton()->get_current_directory());
  4656. _add_dropped_files_recursive(p_files, to_path);
  4657. EditorFileSystem::get_singleton()->scan_changes();
  4658. }
  4659. void EditorNode::_add_dropped_files_recursive(const Vector<String> &p_files, String to_path) {
  4660. Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  4661. ERR_FAIL_COND(dir.is_null());
  4662. for (int i = 0; i < p_files.size(); i++) {
  4663. const String &from = p_files[i];
  4664. String to = to_path.path_join(from.get_file());
  4665. if (dir->dir_exists(from)) {
  4666. Vector<String> sub_files;
  4667. Ref<DirAccess> sub_dir = DirAccess::open(from);
  4668. ERR_FAIL_COND(sub_dir.is_null());
  4669. sub_dir->list_dir_begin();
  4670. String next_file = sub_dir->get_next();
  4671. while (!next_file.is_empty()) {
  4672. if (next_file == "." || next_file == "..") {
  4673. next_file = sub_dir->get_next();
  4674. continue;
  4675. }
  4676. sub_files.push_back(from.path_join(next_file));
  4677. next_file = sub_dir->get_next();
  4678. }
  4679. if (!sub_files.is_empty()) {
  4680. dir->make_dir(to);
  4681. _add_dropped_files_recursive(sub_files, to);
  4682. }
  4683. continue;
  4684. }
  4685. dir->copy(from, to);
  4686. }
  4687. }
  4688. void EditorNode::_file_access_close_error_notify(const String &p_str) {
  4689. callable_mp_static(&EditorNode::_file_access_close_error_notify_impl).bind(p_str).call_deferred();
  4690. }
  4691. void EditorNode::_file_access_close_error_notify_impl(const String &p_str) {
  4692. add_io_error(vformat(TTR("Unable to write to file '%s', file in use, locked or lacking permissions."), p_str));
  4693. }
  4694. // Since we felt that a bespoke NOTIFICATION might not be desirable, this function
  4695. // provides the hardcoded callbacks to address known bugs which occur on certain
  4696. // nodes during reimport.
  4697. // Ideally, we should probably agree on a standardized method name which could be
  4698. // called from here instead.
  4699. void EditorNode::_notify_scene_updated(Node *p_node) {
  4700. Skeleton3D *skel_3d = Object::cast_to<Skeleton3D>(p_node);
  4701. if (skel_3d) {
  4702. skel_3d->reset_bone_poses();
  4703. } else {
  4704. BoneAttachment3D *attachment = Object::cast_to<BoneAttachment3D>(p_node);
  4705. if (attachment) {
  4706. attachment->notify_rebind_required();
  4707. }
  4708. }
  4709. for (int i = 0; i < p_node->get_child_count(); i++) {
  4710. _notify_scene_updated(p_node->get_child(i));
  4711. }
  4712. }
  4713. void EditorNode::reload_scene(const String &p_path) {
  4714. int scene_idx = -1;
  4715. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  4716. if (editor_data.get_scene_path(i) == p_path) {
  4717. scene_idx = i;
  4718. break;
  4719. }
  4720. }
  4721. int current_tab = editor_data.get_edited_scene();
  4722. if (scene_idx == -1) {
  4723. if (get_edited_scene()) {
  4724. // Scene is not open, so at it might be instantiated. We'll refresh the whole scene later.
  4725. EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_current_edited_scene_history_id());
  4726. }
  4727. return;
  4728. }
  4729. if (current_tab == scene_idx) {
  4730. editor_data.apply_changes_in_editors();
  4731. _save_editor_states(p_path);
  4732. }
  4733. // Reload scene.
  4734. _remove_scene(scene_idx, false);
  4735. load_scene(p_path, true, false, true, true);
  4736. // Adjust index so tab is back a the previous position.
  4737. editor_data.move_edited_scene_to_index(scene_idx);
  4738. EditorUndoRedoManager::get_singleton()->clear_history(false, editor_data.get_scene_history_id(scene_idx));
  4739. // Recover the tab.
  4740. scene_tabs->set_current_tab(current_tab);
  4741. }
  4742. void EditorNode::find_all_instances_inheriting_path_in_node(Node *p_root, Node *p_node, const String &p_instance_path, List<Node *> &p_instance_list) {
  4743. String scene_file_path = p_node->get_scene_file_path();
  4744. // This is going to get messy...
  4745. if (p_node->get_scene_file_path() == p_instance_path) {
  4746. p_instance_list.push_back(p_node);
  4747. } else {
  4748. Node *current_node = p_node;
  4749. Ref<SceneState> inherited_state = current_node->get_scene_inherited_state();
  4750. while (inherited_state.is_valid()) {
  4751. String inherited_path = inherited_state->get_path();
  4752. if (inherited_path == p_instance_path) {
  4753. p_instance_list.push_back(p_node);
  4754. break;
  4755. }
  4756. inherited_state = inherited_state->get_base_scene_state();
  4757. }
  4758. }
  4759. for (int i = 0; i < p_node->get_child_count(); i++) {
  4760. Node *child = p_node->get_child(i);
  4761. find_all_instances_inheriting_path_in_node(p_root, child, p_instance_path, p_instance_list);
  4762. }
  4763. }
  4764. void EditorNode::reload_instances_with_path_in_edited_scenes(const String &p_instance_path) {
  4765. int original_edited_scene_idx = editor_data.get_edited_scene();
  4766. HashMap<int, List<Node *>> edited_scene_map;
  4767. // Walk through each opened scene to get a global list of all instances which match
  4768. // the current reimported scenes.
  4769. for (int i = 0; i < editor_data.get_edited_scene_count(); i++) {
  4770. if (editor_data.get_scene_path(i) != p_instance_path) {
  4771. Node *edited_scene_root = editor_data.get_edited_scene_root(i);
  4772. if (edited_scene_root) {
  4773. List<Node *> valid_nodes;
  4774. find_all_instances_inheriting_path_in_node(edited_scene_root, edited_scene_root, p_instance_path, valid_nodes);
  4775. if (valid_nodes.size() > 0) {
  4776. edited_scene_map[i] = valid_nodes;
  4777. }
  4778. }
  4779. }
  4780. }
  4781. if (edited_scene_map.size() > 0) {
  4782. // Reload the new instance.
  4783. Error err;
  4784. Ref<PackedScene> instance_scene_packed_scene = ResourceLoader::load(p_instance_path, "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
  4785. ERR_FAIL_COND(err != OK);
  4786. ERR_FAIL_COND(instance_scene_packed_scene.is_null());
  4787. HashMap<String, Ref<PackedScene>> local_scene_cache;
  4788. local_scene_cache[p_instance_path] = instance_scene_packed_scene;
  4789. for (const KeyValue<int, List<Node *>> &edited_scene_map_elem : edited_scene_map) {
  4790. // Set the current scene.
  4791. int current_scene_idx = edited_scene_map_elem.key;
  4792. editor_data.set_edited_scene(current_scene_idx);
  4793. Node *current_edited_scene = editor_data.get_edited_scene_root(current_scene_idx);
  4794. // Clear the history for this tab (should we allow history to be retained?).
  4795. EditorUndoRedoManager::get_singleton()->clear_history();
  4796. // Update the version
  4797. editor_data.is_scene_changed(current_scene_idx);
  4798. for (Node *original_node : edited_scene_map_elem.value) {
  4799. // Walk the tree for the current node and extract relevant diff data, storing it in the modification table.
  4800. // For additional nodes which are part of the current scene, they get added to the addition table.
  4801. HashMap<NodePath, ModificationNodeEntry> modification_table;
  4802. List<AdditiveNodeEntry> addition_list;
  4803. update_diff_data_for_node(current_edited_scene, original_node, original_node, modification_table, addition_list);
  4804. // Disconnect all relevant connections, all connections from and persistent connections to.
  4805. for (const KeyValue<NodePath, ModificationNodeEntry> &modification_table_entry : modification_table) {
  4806. for (Connection conn : modification_table_entry.value.connections_from) {
  4807. conn.signal.get_object()->disconnect(conn.signal.get_name(), conn.callable);
  4808. }
  4809. for (ConnectionWithNodePath cwnp : modification_table_entry.value.connections_to) {
  4810. Connection conn = cwnp.connection;
  4811. if (conn.flags & CONNECT_PERSIST) {
  4812. conn.signal.get_object()->disconnect(conn.signal.get_name(), conn.callable);
  4813. }
  4814. }
  4815. }
  4816. // Store all the paths for any selected nodes which are ancestors of the node we're replacing.
  4817. List<NodePath> selected_node_paths;
  4818. for (Node *selected_node : editor_selection->get_selected_node_list()) {
  4819. if (selected_node == original_node || original_node->is_ancestor_of(selected_node)) {
  4820. selected_node_paths.push_back(original_node->get_path_to(selected_node));
  4821. editor_selection->remove_node(selected_node);
  4822. }
  4823. }
  4824. // Remove all nodes which were added as additional elements (they will be restored later).
  4825. for (AdditiveNodeEntry additive_node_entry : addition_list) {
  4826. Node *addition_node = additive_node_entry.node;
  4827. addition_node->get_parent()->remove_child(addition_node);
  4828. }
  4829. // Clear ownership of the nodes (kind of hack to workaround an issue with
  4830. // replace_by when called on nodes in other tabs).
  4831. List<Node *> nodes_owned_by_original_node;
  4832. original_node->get_owned_by(original_node, &nodes_owned_by_original_node);
  4833. for (Node *owned_node : nodes_owned_by_original_node) {
  4834. owned_node->set_owner(nullptr);
  4835. }
  4836. // Delete all the remaining node children.
  4837. while (original_node->get_child_count()) {
  4838. Node *child = original_node->get_child(0);
  4839. original_node->remove_child(child);
  4840. child->queue_free();
  4841. }
  4842. // Reset the editable instance state.
  4843. bool is_editable = true;
  4844. Node *owner = original_node->get_owner();
  4845. if (owner) {
  4846. is_editable = owner->is_editable_instance(original_node);
  4847. }
  4848. // Load a replacement scene for the node.
  4849. Ref<PackedScene> current_packed_scene;
  4850. if (original_node->get_scene_file_path() == p_instance_path) {
  4851. // If the node file name directly matches the scene we're replacing,
  4852. // just load it since we already cached it.
  4853. current_packed_scene = instance_scene_packed_scene;
  4854. } else {
  4855. // Otherwise, check the inheritance chain, reloading and caching any scenes
  4856. // we require along the way.
  4857. List<String> required_load_paths;
  4858. String scene_path = original_node->get_scene_file_path();
  4859. // Do we need to check if the paths are empty?
  4860. if (!scene_path.is_empty()) {
  4861. required_load_paths.push_front(scene_path);
  4862. }
  4863. Ref<SceneState> inherited_state = original_node->get_scene_inherited_state();
  4864. while (inherited_state.is_valid()) {
  4865. String inherited_path = inherited_state->get_path();
  4866. // Do we need to check if the paths are empty?
  4867. if (!inherited_path.is_empty()) {
  4868. required_load_paths.push_front(inherited_path);
  4869. }
  4870. inherited_state = inherited_state->get_base_scene_state();
  4871. }
  4872. // Ensure the inheritance chain is loaded in the correct order so that cache can
  4873. // be properly updated.
  4874. for (String path : required_load_paths) {
  4875. if (!local_scene_cache.find(path)) {
  4876. current_packed_scene = ResourceLoader::load(path, "", ResourceFormatLoader::CACHE_MODE_REPLACE, &err);
  4877. local_scene_cache[path] = current_packed_scene;
  4878. } else {
  4879. current_packed_scene = local_scene_cache[path];
  4880. }
  4881. }
  4882. }
  4883. ERR_FAIL_COND(current_packed_scene.is_null());
  4884. // Instantiate the node.
  4885. Node *instantiated_node = nullptr;
  4886. if (current_packed_scene.is_valid()) {
  4887. instantiated_node = current_packed_scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  4888. }
  4889. ERR_FAIL_NULL(instantiated_node);
  4890. bool original_node_is_displayed_folded = original_node->is_displayed_folded();
  4891. bool original_node_scene_instance_load_placeholder = original_node->get_scene_instance_load_placeholder();
  4892. // Update the name to match
  4893. instantiated_node->set_name(original_node->get_name());
  4894. // Is this replacing the edited root node?
  4895. String original_node_file_path = original_node->get_scene_file_path();
  4896. if (current_edited_scene == original_node) {
  4897. instantiated_node->set_scene_instance_state(original_node->get_scene_instance_state());
  4898. // Fix unsaved inherited scene
  4899. if (original_node_file_path.is_empty()) {
  4900. Ref<SceneState> state = current_packed_scene->get_state();
  4901. state->set_path(current_packed_scene->get_path());
  4902. instantiated_node->set_scene_inherited_state(state);
  4903. instantiated_node->set_scene_file_path(String());
  4904. }
  4905. editor_data.set_edited_scene_root(instantiated_node);
  4906. current_edited_scene = instantiated_node;
  4907. if (original_node->is_inside_tree()) {
  4908. SceneTreeDock::get_singleton()->set_edited_scene(current_edited_scene);
  4909. original_node->get_tree()->set_edited_scene_root(instantiated_node);
  4910. }
  4911. }
  4912. // Replace the original node with the instantiated version.
  4913. original_node->replace_by(instantiated_node, false);
  4914. // Mark the old node for deletion.
  4915. original_node->queue_free();
  4916. // Restore the folded and placeholder state from the original node.
  4917. instantiated_node->set_display_folded(original_node_is_displayed_folded);
  4918. instantiated_node->set_scene_instance_load_placeholder(original_node_scene_instance_load_placeholder);
  4919. if (owner) {
  4920. Ref<SceneState> ss_inst = owner->get_scene_instance_state();
  4921. if (ss_inst.is_valid()) {
  4922. ss_inst->update_instance_resource(p_instance_path, current_packed_scene);
  4923. }
  4924. owner->set_editable_instance(instantiated_node, is_editable);
  4925. }
  4926. // Attempt to re-add all the additional nodes.
  4927. for (AdditiveNodeEntry additive_node_entry : addition_list) {
  4928. Node *parent_node = instantiated_node->get_node_or_null(additive_node_entry.parent);
  4929. if (!parent_node) {
  4930. parent_node = current_edited_scene;
  4931. }
  4932. parent_node->add_child(additive_node_entry.node);
  4933. parent_node->move_child(additive_node_entry.node, additive_node_entry.index);
  4934. // If the additive node's owner was the node which got replaced, update it.
  4935. if (additive_node_entry.owner == original_node) {
  4936. additive_node_entry.owner = instantiated_node;
  4937. }
  4938. additive_node_entry.node->set_owner(additive_node_entry.owner);
  4939. // If the parent node was lost, attempt to restore the original global transform.
  4940. {
  4941. Node2D *node_2d = Object::cast_to<Node2D>(additive_node_entry.node);
  4942. if (node_2d) {
  4943. node_2d->set_transform(additive_node_entry.transform_2d);
  4944. }
  4945. Node3D *node_3d = Object::cast_to<Node3D>(additive_node_entry.node);
  4946. if (node_3d) {
  4947. node_3d->set_transform(additive_node_entry.transform_3d);
  4948. }
  4949. }
  4950. // Restore the ownership of its ancestors
  4951. for (KeyValue<Node *, Node *> &E : additive_node_entry.ownership_table) {
  4952. Node *current_ancestor = E.key;
  4953. Node *ancestor_owner = E.value;
  4954. if (ancestor_owner == original_node) {
  4955. ancestor_owner = instantiated_node;
  4956. }
  4957. current_ancestor->set_owner(ancestor_owner);
  4958. }
  4959. }
  4960. // Restore the selection.
  4961. if (selected_node_paths.size()) {
  4962. for (NodePath selected_node_path : selected_node_paths) {
  4963. Node *selected_node = instantiated_node->get_node_or_null(selected_node_path);
  4964. if (selected_node) {
  4965. editor_selection->add_node(selected_node);
  4966. }
  4967. }
  4968. editor_selection->update();
  4969. }
  4970. // Attempt to restore the modified properties and signals for the instantitated node and all its owned children.
  4971. for (KeyValue<NodePath, ModificationNodeEntry> &E : modification_table) {
  4972. NodePath new_current_path = E.key;
  4973. Node *modifiable_node = instantiated_node->get_node_or_null(new_current_path);
  4974. if (modifiable_node) {
  4975. // Get properties for this node.
  4976. List<PropertyInfo> pinfo;
  4977. modifiable_node->get_property_list(&pinfo);
  4978. // Get names of all valid property names (TODO: make this more efficient).
  4979. List<String> property_names;
  4980. for (const PropertyInfo &E2 : pinfo) {
  4981. if (E2.usage & PROPERTY_USAGE_STORAGE) {
  4982. property_names.push_back(E2.name);
  4983. }
  4984. }
  4985. // Restore the modified properties for this node.
  4986. for (const KeyValue<StringName, Variant> &E2 : E.value.property_table) {
  4987. if (property_names.find(E2.key)) {
  4988. modifiable_node->set(E2.key, E2.value);
  4989. }
  4990. }
  4991. // Restore the connections to other nodes.
  4992. for (const ConnectionWithNodePath &E2 : E.value.connections_to) {
  4993. Connection conn = E2.connection;
  4994. // Get the node the callable is targeting.
  4995. Node *target_node = cast_to<Node>(conn.callable.get_object());
  4996. // If the callable object no longer exists or is marked for deletion,
  4997. // attempt to reaccquire the closest match by using the node path
  4998. // we saved earlier.
  4999. if (!target_node || !target_node->is_queued_for_deletion()) {
  5000. target_node = modifiable_node->get_node_or_null(E2.node_path);
  5001. }
  5002. if (target_node) {
  5003. // Reconstruct the callable.
  5004. Callable new_callable = Callable(target_node, conn.callable.get_method());
  5005. if (!modifiable_node->is_connected(conn.signal.get_name(), new_callable)) {
  5006. ERR_FAIL_COND(modifiable_node->connect(conn.signal.get_name(), new_callable, conn.flags) != OK);
  5007. }
  5008. }
  5009. }
  5010. // Restore the connections from other nodes.
  5011. for (const Connection &E2 : E.value.connections_from) {
  5012. Connection conn = E2;
  5013. bool valid = modifiable_node->has_method(conn.callable.get_method()) || Ref<Script>(modifiable_node->get_script()).is_null() || Ref<Script>(modifiable_node->get_script())->has_method(conn.callable.get_method());
  5014. ERR_CONTINUE_MSG(!valid, vformat("Attempt to connect signal '%s.%s' to nonexistent method '%s.%s'.", conn.signal.get_object()->get_class(), conn.signal.get_name(), conn.callable.get_object()->get_class(), conn.callable.get_method()));
  5015. // Get the object which the signal is connected from.
  5016. Object *source_object = conn.signal.get_object();
  5017. if (source_object) {
  5018. ERR_FAIL_COND(source_object->connect(conn.signal.get_name(), Callable(modifiable_node, conn.callable.get_method()), conn.flags) != OK);
  5019. }
  5020. }
  5021. // Re-add the groups.
  5022. for (const Node::GroupInfo &E2 : E.value.groups) {
  5023. modifiable_node->add_to_group(E2.name, E2.persistent);
  5024. }
  5025. }
  5026. }
  5027. }
  5028. // Cleanup the history of the changes.
  5029. editor_history.cleanup_history();
  5030. _notify_scene_updated(current_edited_scene);
  5031. }
  5032. edited_scene_map.clear();
  5033. }
  5034. editor_data.set_edited_scene(original_edited_scene_idx);
  5035. _edit_current();
  5036. }
  5037. int EditorNode::plugin_init_callback_count = 0;
  5038. void EditorNode::add_plugin_init_callback(EditorPluginInitializeCallback p_callback) {
  5039. ERR_FAIL_COND(plugin_init_callback_count == MAX_INIT_CALLBACKS);
  5040. plugin_init_callbacks[plugin_init_callback_count++] = p_callback;
  5041. }
  5042. EditorPluginInitializeCallback EditorNode::plugin_init_callbacks[EditorNode::MAX_INIT_CALLBACKS];
  5043. int EditorNode::build_callback_count = 0;
  5044. void EditorNode::add_build_callback(EditorBuildCallback p_callback) {
  5045. ERR_FAIL_COND(build_callback_count == MAX_INIT_CALLBACKS);
  5046. build_callbacks[build_callback_count++] = p_callback;
  5047. }
  5048. EditorBuildCallback EditorNode::build_callbacks[EditorNode::MAX_BUILD_CALLBACKS];
  5049. bool EditorNode::call_build() {
  5050. bool builds_successful = true;
  5051. for (int i = 0; i < build_callback_count && builds_successful; i++) {
  5052. if (!build_callbacks[i]()) {
  5053. ERR_PRINT("A Godot Engine build callback failed.");
  5054. builds_successful = false;
  5055. }
  5056. }
  5057. if (builds_successful && !editor_data.call_build()) {
  5058. ERR_PRINT("An EditorPlugin build callback failed.");
  5059. builds_successful = false;
  5060. }
  5061. return builds_successful;
  5062. }
  5063. void EditorNode::_inherit_imported(const String &p_action) {
  5064. open_imported->hide();
  5065. load_scene(open_import_request, true, true);
  5066. }
  5067. void EditorNode::_open_imported() {
  5068. load_scene(open_import_request, true, false, true, true);
  5069. }
  5070. void EditorNode::dim_editor(bool p_dimming) {
  5071. dimmed = p_dimming;
  5072. gui_base->set_modulate(p_dimming ? Color(0.5, 0.5, 0.5) : Color(1, 1, 1));
  5073. }
  5074. bool EditorNode::is_editor_dimmed() const {
  5075. return dimmed;
  5076. }
  5077. void EditorNode::open_export_template_manager() {
  5078. export_template_manager->popup_manager();
  5079. }
  5080. void EditorNode::add_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin) {
  5081. resource_conversion_plugins.push_back(p_plugin);
  5082. }
  5083. void EditorNode::remove_resource_conversion_plugin(const Ref<EditorResourceConversionPlugin> &p_plugin) {
  5084. resource_conversion_plugins.erase(p_plugin);
  5085. }
  5086. Vector<Ref<EditorResourceConversionPlugin>> EditorNode::find_resource_conversion_plugin(const Ref<Resource> &p_for_resource) {
  5087. Vector<Ref<EditorResourceConversionPlugin>> ret;
  5088. for (int i = 0; i < resource_conversion_plugins.size(); i++) {
  5089. if (resource_conversion_plugins[i].is_valid() && resource_conversion_plugins[i]->handles(p_for_resource)) {
  5090. ret.push_back(resource_conversion_plugins[i]);
  5091. }
  5092. }
  5093. return ret;
  5094. }
  5095. void EditorNode::_update_renderer_color() {
  5096. String rendering_method = renderer->get_selected_metadata();
  5097. // TODO: Use theme colors instead of hardcoded values.
  5098. if (rendering_method == "forward_plus") {
  5099. renderer->add_theme_color_override("font_color", Color::hex(0x5d8c3fff));
  5100. }
  5101. if (rendering_method == "mobile") {
  5102. renderer->add_theme_color_override("font_color", Color::hex(0xa5557dff));
  5103. }
  5104. if (rendering_method == "gl_compatibility") {
  5105. renderer->add_theme_color_override("font_color", Color::hex(0x5586a4ff));
  5106. }
  5107. }
  5108. void EditorNode::_renderer_selected(int p_which) {
  5109. String rendering_method = renderer->get_item_metadata(p_which);
  5110. String current_renderer = GLOBAL_GET("rendering/renderer/rendering_method");
  5111. if (rendering_method == current_renderer) {
  5112. return;
  5113. }
  5114. renderer_request = rendering_method;
  5115. video_restart_dialog->set_text(
  5116. vformat(TTR("Changing the renderer requires restarting the editor.\n\nChoosing Save & Restart will change the rendering method to:\n- Desktop platforms: %s\n- Mobile platforms: %s\n- Web platform: gl_compatibility"),
  5117. renderer_request, renderer_request.replace("forward_plus", "mobile")));
  5118. video_restart_dialog->popup_centered();
  5119. renderer->select(renderer_current);
  5120. _update_renderer_color();
  5121. }
  5122. void EditorNode::_add_renderer_entry(const String &p_renderer_name, bool p_mark_overridden) {
  5123. String item_text;
  5124. if (p_renderer_name == "forward_plus") {
  5125. item_text = TTR("Forward+");
  5126. }
  5127. if (p_renderer_name == "mobile") {
  5128. item_text = TTR("Mobile");
  5129. }
  5130. if (p_renderer_name == "gl_compatibility") {
  5131. item_text = TTR("Compatibility");
  5132. }
  5133. if (p_mark_overridden) {
  5134. item_text += " " + TTR("(Overridden)");
  5135. }
  5136. renderer->add_item(item_text);
  5137. }
  5138. void EditorNode::_resource_saved(Ref<Resource> p_resource, const String &p_path) {
  5139. if (singleton->saving_resources_in_path.has(p_resource)) {
  5140. // This is going to be handled by save_resource_in_path when the time is right.
  5141. return;
  5142. }
  5143. if (EditorFileSystem::get_singleton()) {
  5144. EditorFileSystem::get_singleton()->update_file(p_path);
  5145. }
  5146. singleton->editor_folding.save_resource_folding(p_resource, p_path);
  5147. }
  5148. void EditorNode::_resource_loaded(Ref<Resource> p_resource, const String &p_path) {
  5149. singleton->editor_folding.load_resource_folding(p_resource, p_path);
  5150. }
  5151. void EditorNode::_feature_profile_changed() {
  5152. Ref<EditorFeatureProfile> profile = feature_profile_manager->get_current_profile();
  5153. if (profile.is_valid()) {
  5154. editor_dock_manager->set_dock_enabled(NodeDock::get_singleton(), !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_NODE_DOCK));
  5155. // The Import dock is useless without the FileSystem dock. Ensure the configuration is valid.
  5156. bool fs_dock_disabled = profile->is_feature_disabled(EditorFeatureProfile::FEATURE_FILESYSTEM_DOCK);
  5157. editor_dock_manager->set_dock_enabled(FileSystemDock::get_singleton(), !fs_dock_disabled);
  5158. editor_dock_manager->set_dock_enabled(ImportDock::get_singleton(), !fs_dock_disabled && !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_IMPORT_DOCK));
  5159. editor_dock_manager->set_dock_enabled(history_dock, !profile->is_feature_disabled(EditorFeatureProfile::FEATURE_HISTORY_DOCK));
  5160. main_editor_buttons[EDITOR_3D]->set_visible(!profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D));
  5161. main_editor_buttons[EDITOR_SCRIPT]->set_visible(!profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT));
  5162. if (AssetLibraryEditorPlugin::is_available()) {
  5163. main_editor_buttons[EDITOR_ASSETLIB]->set_visible(!profile->is_feature_disabled(EditorFeatureProfile::FEATURE_ASSET_LIB));
  5164. }
  5165. if ((profile->is_feature_disabled(EditorFeatureProfile::FEATURE_3D) && singleton->main_editor_buttons[EDITOR_3D]->is_pressed()) ||
  5166. (profile->is_feature_disabled(EditorFeatureProfile::FEATURE_SCRIPT) && singleton->main_editor_buttons[EDITOR_SCRIPT]->is_pressed()) ||
  5167. (AssetLibraryEditorPlugin::is_available() && profile->is_feature_disabled(EditorFeatureProfile::FEATURE_ASSET_LIB) && singleton->main_editor_buttons[EDITOR_ASSETLIB]->is_pressed())) {
  5168. editor_select(EDITOR_2D);
  5169. }
  5170. } else {
  5171. editor_dock_manager->set_dock_enabled(ImportDock::get_singleton(), true);
  5172. editor_dock_manager->set_dock_enabled(NodeDock::get_singleton(), true);
  5173. editor_dock_manager->set_dock_enabled(FileSystemDock::get_singleton(), true);
  5174. editor_dock_manager->set_dock_enabled(history_dock, true);
  5175. main_editor_buttons[EDITOR_3D]->set_visible(true);
  5176. main_editor_buttons[EDITOR_SCRIPT]->set_visible(true);
  5177. if (AssetLibraryEditorPlugin::is_available()) {
  5178. main_editor_buttons[EDITOR_ASSETLIB]->set_visible(true);
  5179. }
  5180. }
  5181. }
  5182. void EditorNode::_bind_methods() {
  5183. ClassDB::bind_method(D_METHOD("push_item", "object", "property", "inspector_only"), &EditorNode::push_item, DEFVAL(""), DEFVAL(false));
  5184. ClassDB::bind_method("set_edited_scene", &EditorNode::set_edited_scene);
  5185. ClassDB::bind_method("stop_child_process", &EditorNode::stop_child_process);
  5186. ADD_SIGNAL(MethodInfo("request_help_search"));
  5187. ADD_SIGNAL(MethodInfo("script_add_function_request", PropertyInfo(Variant::OBJECT, "obj"), PropertyInfo(Variant::STRING, "function"), PropertyInfo(Variant::PACKED_STRING_ARRAY, "args")));
  5188. ADD_SIGNAL(MethodInfo("resource_saved", PropertyInfo(Variant::OBJECT, "obj")));
  5189. ADD_SIGNAL(MethodInfo("scene_saved", PropertyInfo(Variant::STRING, "path")));
  5190. ADD_SIGNAL(MethodInfo("scene_changed"));
  5191. ADD_SIGNAL(MethodInfo("scene_closed", PropertyInfo(Variant::STRING, "path")));
  5192. }
  5193. static Node *_resource_get_edited_scene() {
  5194. return EditorNode::get_singleton()->get_edited_scene();
  5195. }
  5196. void EditorNode::_print_handler(void *p_this, const String &p_string, bool p_error, bool p_rich) {
  5197. callable_mp_static(&EditorNode::_print_handler_impl).bind(p_string, p_error, p_rich).call_deferred();
  5198. }
  5199. void EditorNode::_print_handler_impl(const String &p_string, bool p_error, bool p_rich) {
  5200. if (!singleton) {
  5201. return;
  5202. }
  5203. if (p_error) {
  5204. singleton->log->add_message(p_string, EditorLog::MSG_TYPE_ERROR);
  5205. } else if (p_rich) {
  5206. singleton->log->add_message(p_string, EditorLog::MSG_TYPE_STD_RICH);
  5207. } else {
  5208. singleton->log->add_message(p_string, EditorLog::MSG_TYPE_STD);
  5209. }
  5210. }
  5211. static void _execute_thread(void *p_ud) {
  5212. EditorNode::ExecuteThreadArgs *eta = (EditorNode::ExecuteThreadArgs *)p_ud;
  5213. Error err = OS::get_singleton()->execute(eta->path, eta->args, &eta->output, &eta->exitcode, true, &eta->execute_output_mutex);
  5214. print_verbose("Thread exit status: " + itos(eta->exitcode));
  5215. if (err != OK) {
  5216. eta->exitcode = err;
  5217. }
  5218. eta->done.set();
  5219. }
  5220. 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, String *r_output) {
  5221. if (execute_output_dialog) {
  5222. execute_output_dialog->set_title(p_title);
  5223. execute_output_dialog->get_ok_button()->set_disabled(true);
  5224. execute_outputs->clear();
  5225. execute_outputs->set_scroll_follow(true);
  5226. EditorInterface::get_singleton()->popup_dialog_centered_ratio(execute_output_dialog);
  5227. }
  5228. ExecuteThreadArgs eta;
  5229. eta.path = p_path;
  5230. eta.args = p_arguments;
  5231. eta.exitcode = 255;
  5232. int prev_len = 0;
  5233. eta.execute_output_thread.start(_execute_thread, &eta);
  5234. while (!eta.done.is_set()) {
  5235. {
  5236. MutexLock lock(eta.execute_output_mutex);
  5237. if (prev_len != eta.output.length()) {
  5238. String to_add = eta.output.substr(prev_len, eta.output.length());
  5239. prev_len = eta.output.length();
  5240. execute_outputs->add_text(to_add);
  5241. DisplayServer::get_singleton()->process_events(); // Get rid of pending events.
  5242. Main::iteration();
  5243. }
  5244. }
  5245. OS::get_singleton()->delay_usec(1000);
  5246. }
  5247. eta.execute_output_thread.wait_to_finish();
  5248. execute_outputs->add_text("\nExit Code: " + itos(eta.exitcode));
  5249. if (execute_output_dialog) {
  5250. if (p_close_on_errors && eta.exitcode != 0) {
  5251. execute_output_dialog->hide();
  5252. }
  5253. if (p_close_on_ok && eta.exitcode == 0) {
  5254. execute_output_dialog->hide();
  5255. }
  5256. execute_output_dialog->get_ok_button()->set_disabled(false);
  5257. }
  5258. if (r_output) {
  5259. *r_output = eta.output;
  5260. }
  5261. return eta.exitcode;
  5262. }
  5263. EditorNode::EditorNode() {
  5264. DEV_ASSERT(!singleton);
  5265. singleton = this;
  5266. Resource::_get_local_scene_func = _resource_get_edited_scene;
  5267. {
  5268. PortableCompressedTexture2D::set_keep_all_compressed_buffers(true);
  5269. RenderingServer::get_singleton()->set_debug_generate_wireframes(true);
  5270. AudioServer::get_singleton()->set_enable_tagging_used_audio_streams(true);
  5271. // No navigation by default if in editor.
  5272. if (NavigationServer3D::get_singleton()->get_debug_enabled()) {
  5273. NavigationServer3D::get_singleton()->set_active(true);
  5274. } else {
  5275. NavigationServer3D::get_singleton()->set_active(false);
  5276. }
  5277. // No physics by default if in editor.
  5278. PhysicsServer3D::get_singleton()->set_active(false);
  5279. PhysicsServer2D::get_singleton()->set_active(false);
  5280. // No scripting by default if in editor (except for tool).
  5281. ScriptServer::set_scripting_enabled(false);
  5282. Input::get_singleton()->set_use_accumulated_input(true);
  5283. if (!DisplayServer::get_singleton()->is_touchscreen_available()) {
  5284. // Only if no touchscreen ui hint, disable emulation just in case.
  5285. Input::get_singleton()->set_emulate_touch_from_mouse(false);
  5286. }
  5287. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CUSTOM_CURSOR_SHAPE)) {
  5288. DisplayServer::get_singleton()->cursor_set_custom_image(Ref<Resource>());
  5289. }
  5290. }
  5291. SceneState::set_disable_placeholders(true);
  5292. ResourceLoader::clear_translation_remaps(); // Using no remaps if in editor.
  5293. ResourceLoader::clear_path_remaps();
  5294. ResourceLoader::set_create_missing_resources_if_class_unavailable(true);
  5295. EditorPropertyNameProcessor *epnp = memnew(EditorPropertyNameProcessor);
  5296. add_child(epnp);
  5297. EditorUndoRedoManager::get_singleton()->connect("version_changed", callable_mp(this, &EditorNode::_update_undo_redo_allowed));
  5298. EditorUndoRedoManager::get_singleton()->connect("history_changed", callable_mp(this, &EditorNode::_update_undo_redo_allowed));
  5299. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorNode::_update_from_settings));
  5300. GDExtensionManager::get_singleton()->connect("extensions_reloaded", callable_mp(this, &EditorNode::_gdextensions_reloaded));
  5301. TranslationServer::get_singleton()->set_enabled(false);
  5302. // Load settings.
  5303. if (!EditorSettings::get_singleton()) {
  5304. EditorSettings::create();
  5305. }
  5306. FileAccess::set_backup_save(EDITOR_GET("filesystem/on_save/safe_save_on_backup_then_rename"));
  5307. _update_vsync_mode();
  5308. // Warm up the surface upgrade tool as early as possible.
  5309. surface_upgrade_tool = memnew(SurfaceUpgradeTool);
  5310. run_surface_upgrade_tool = EditorSettings::get_singleton()->get_project_metadata("surface_upgrade_tool", "run_on_restart", false);
  5311. if (run_surface_upgrade_tool) {
  5312. SurfaceUpgradeTool::get_singleton()->begin_upgrade();
  5313. }
  5314. {
  5315. int display_scale = EDITOR_GET("interface/editor/display_scale");
  5316. switch (display_scale) {
  5317. case 0:
  5318. // Try applying a suitable display scale automatically.
  5319. EditorScale::set_scale(EditorSettings::get_singleton()->get_auto_display_scale());
  5320. break;
  5321. case 1:
  5322. EditorScale::set_scale(0.75);
  5323. break;
  5324. case 2:
  5325. EditorScale::set_scale(1.0);
  5326. break;
  5327. case 3:
  5328. EditorScale::set_scale(1.25);
  5329. break;
  5330. case 4:
  5331. EditorScale::set_scale(1.5);
  5332. break;
  5333. case 5:
  5334. EditorScale::set_scale(1.75);
  5335. break;
  5336. case 6:
  5337. EditorScale::set_scale(2.0);
  5338. break;
  5339. default:
  5340. EditorScale::set_scale(EDITOR_GET("interface/editor/custom_display_scale"));
  5341. break;
  5342. }
  5343. }
  5344. // Define a minimum window size to prevent UI elements from overlapping or being cut off.
  5345. Window *w = Object::cast_to<Window>(SceneTree::get_singleton()->get_root());
  5346. if (w) {
  5347. const Size2 minimum_size = Size2(1024, 600) * EDSCALE;
  5348. w->set_min_size(minimum_size); // Calling it this early doesn't sync the property with DS.
  5349. DisplayServer::get_singleton()->window_set_min_size(minimum_size);
  5350. }
  5351. EditorFileDialog::set_default_show_hidden_files(EDITOR_GET("filesystem/file_dialog/show_hidden_files"));
  5352. EditorFileDialog::set_default_display_mode((EditorFileDialog::DisplayMode)EDITOR_GET("filesystem/file_dialog/display_mode").operator int());
  5353. int swap_cancel_ok = EDITOR_GET("interface/editor/accept_dialog_cancel_ok_buttons");
  5354. if (swap_cancel_ok != 0) { // 0 is auto, set in register_scene based on DisplayServer.
  5355. // Swap on means OK first.
  5356. AcceptDialog::set_swap_cancel_ok(swap_cancel_ok == 2);
  5357. }
  5358. int ed_root_dir = EDITOR_GET("interface/editor/ui_layout_direction");
  5359. Control::set_root_layout_direction(ed_root_dir);
  5360. Window::set_root_layout_direction(ed_root_dir);
  5361. ResourceLoader::set_abort_on_missing_resources(false);
  5362. ResourceLoader::set_error_notify_func(&EditorNode::add_io_error);
  5363. ResourceLoader::set_dependency_error_notify_func(&EditorNode::_dependency_error_report);
  5364. SceneState::set_instantiation_warning_notify_func([](const String &p_warning) {
  5365. add_io_warning(p_warning);
  5366. callable_mp(EditorInterface::get_singleton(), &EditorInterface::mark_scene_as_unsaved).call_deferred();
  5367. });
  5368. {
  5369. // Register importers at the beginning, so dialogs are created with the right extensions.
  5370. Ref<ResourceImporterTexture> import_texture = memnew(ResourceImporterTexture(true));
  5371. ResourceFormatImporter::get_singleton()->add_importer(import_texture);
  5372. Ref<ResourceImporterLayeredTexture> import_cubemap;
  5373. import_cubemap.instantiate();
  5374. import_cubemap->set_mode(ResourceImporterLayeredTexture::MODE_CUBEMAP);
  5375. ResourceFormatImporter::get_singleton()->add_importer(import_cubemap);
  5376. Ref<ResourceImporterLayeredTexture> import_array;
  5377. import_array.instantiate();
  5378. import_array->set_mode(ResourceImporterLayeredTexture::MODE_2D_ARRAY);
  5379. ResourceFormatImporter::get_singleton()->add_importer(import_array);
  5380. Ref<ResourceImporterLayeredTexture> import_cubemap_array;
  5381. import_cubemap_array.instantiate();
  5382. import_cubemap_array->set_mode(ResourceImporterLayeredTexture::MODE_CUBEMAP_ARRAY);
  5383. ResourceFormatImporter::get_singleton()->add_importer(import_cubemap_array);
  5384. Ref<ResourceImporterLayeredTexture> import_3d = memnew(ResourceImporterLayeredTexture(true));
  5385. import_3d->set_mode(ResourceImporterLayeredTexture::MODE_3D);
  5386. ResourceFormatImporter::get_singleton()->add_importer(import_3d);
  5387. Ref<ResourceImporterImage> import_image;
  5388. import_image.instantiate();
  5389. ResourceFormatImporter::get_singleton()->add_importer(import_image);
  5390. Ref<ResourceImporterTextureAtlas> import_texture_atlas;
  5391. import_texture_atlas.instantiate();
  5392. ResourceFormatImporter::get_singleton()->add_importer(import_texture_atlas);
  5393. Ref<ResourceImporterDynamicFont> import_font_data_dynamic;
  5394. import_font_data_dynamic.instantiate();
  5395. ResourceFormatImporter::get_singleton()->add_importer(import_font_data_dynamic);
  5396. Ref<ResourceImporterBMFont> import_font_data_bmfont;
  5397. import_font_data_bmfont.instantiate();
  5398. ResourceFormatImporter::get_singleton()->add_importer(import_font_data_bmfont);
  5399. Ref<ResourceImporterImageFont> import_font_data_image;
  5400. import_font_data_image.instantiate();
  5401. ResourceFormatImporter::get_singleton()->add_importer(import_font_data_image);
  5402. Ref<ResourceImporterCSVTranslation> import_csv_translation;
  5403. import_csv_translation.instantiate();
  5404. ResourceFormatImporter::get_singleton()->add_importer(import_csv_translation);
  5405. Ref<ResourceImporterWAV> import_wav;
  5406. import_wav.instantiate();
  5407. ResourceFormatImporter::get_singleton()->add_importer(import_wav);
  5408. Ref<ResourceImporterOBJ> import_obj;
  5409. import_obj.instantiate();
  5410. ResourceFormatImporter::get_singleton()->add_importer(import_obj);
  5411. Ref<ResourceImporterShaderFile> import_shader_file;
  5412. import_shader_file.instantiate();
  5413. ResourceFormatImporter::get_singleton()->add_importer(import_shader_file);
  5414. Ref<ResourceImporterScene> import_scene = memnew(ResourceImporterScene(false, true));
  5415. ResourceFormatImporter::get_singleton()->add_importer(import_scene);
  5416. Ref<ResourceImporterScene> import_animation = memnew(ResourceImporterScene(true, true));
  5417. ResourceFormatImporter::get_singleton()->add_importer(import_animation);
  5418. {
  5419. Ref<EditorSceneFormatImporterCollada> import_collada;
  5420. import_collada.instantiate();
  5421. ResourceImporterScene::add_scene_importer(import_collada);
  5422. Ref<EditorOBJImporter> import_obj2;
  5423. import_obj2.instantiate();
  5424. ResourceImporterScene::add_scene_importer(import_obj2);
  5425. Ref<EditorSceneFormatImporterESCN> import_escn;
  5426. import_escn.instantiate();
  5427. ResourceImporterScene::add_scene_importer(import_escn);
  5428. }
  5429. Ref<ResourceImporterBitMap> import_bitmap;
  5430. import_bitmap.instantiate();
  5431. ResourceFormatImporter::get_singleton()->add_importer(import_bitmap);
  5432. }
  5433. {
  5434. Ref<EditorInspectorDefaultPlugin> eidp;
  5435. eidp.instantiate();
  5436. EditorInspector::add_inspector_plugin(eidp);
  5437. Ref<EditorInspectorRootMotionPlugin> rmp;
  5438. rmp.instantiate();
  5439. EditorInspector::add_inspector_plugin(rmp);
  5440. Ref<EditorInspectorVisualShaderModePlugin> smp;
  5441. smp.instantiate();
  5442. EditorInspector::add_inspector_plugin(smp);
  5443. Ref<EditorInspectorParticleProcessMaterialPlugin> ppm;
  5444. ppm.instantiate();
  5445. EditorInspector::add_inspector_plugin(ppm);
  5446. }
  5447. editor_selection = memnew(EditorSelection);
  5448. EditorFileSystem *efs = memnew(EditorFileSystem);
  5449. add_child(efs);
  5450. // Used for previews.
  5451. FileDialog::get_icon_func = _file_dialog_get_icon;
  5452. FileDialog::register_func = _file_dialog_register;
  5453. FileDialog::unregister_func = _file_dialog_unregister;
  5454. EditorFileDialog::get_icon_func = _file_dialog_get_icon;
  5455. EditorFileDialog::register_func = _editor_file_dialog_register;
  5456. EditorFileDialog::unregister_func = _editor_file_dialog_unregister;
  5457. editor_export = memnew(EditorExport);
  5458. add_child(editor_export);
  5459. // Exporters might need the theme.
  5460. EditorThemeManager::initialize();
  5461. theme = EditorThemeManager::generate_theme();
  5462. DisplayServer::set_early_window_clear_color_override(true, theme->get_color(SNAME("background"), EditorStringName(Editor)));
  5463. register_exporters();
  5464. ED_SHORTCUT("canvas_item_editor/pan_view", TTR("Pan View"), Key::SPACE);
  5465. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  5466. for (const String &E : textfile_ext) {
  5467. textfile_extensions.insert(E);
  5468. }
  5469. resource_preview = memnew(EditorResourcePreview);
  5470. add_child(resource_preview);
  5471. progress_dialog = memnew(ProgressDialog);
  5472. progress_dialog->set_unparent_when_invisible(true);
  5473. gui_base = memnew(Panel);
  5474. add_child(gui_base);
  5475. // Take up all screen.
  5476. gui_base->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  5477. gui_base->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);
  5478. gui_base->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
  5479. gui_base->set_end(Point2(0, 0));
  5480. main_vbox = memnew(VBoxContainer);
  5481. gui_base->add_child(main_vbox);
  5482. title_bar = memnew(EditorTitleBar);
  5483. main_vbox->add_child(title_bar);
  5484. left_l_hsplit = memnew(DockSplitContainer);
  5485. left_l_hsplit->set_name("DockHSplitLeftL");
  5486. main_vbox->add_child(left_l_hsplit);
  5487. left_l_hsplit->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5488. left_l_vsplit = memnew(DockSplitContainer);
  5489. left_l_vsplit->set_name("DockVSplitLeftL");
  5490. left_l_vsplit->set_vertical(true);
  5491. left_l_hsplit->add_child(left_l_vsplit);
  5492. TabContainer *dock_slot[EditorDockManager::DOCK_SLOT_MAX];
  5493. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_UL] = memnew(TabContainer);
  5494. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_UL]->set_name("DockSlotLeftUL");
  5495. left_l_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_LEFT_UL]);
  5496. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_BL] = memnew(TabContainer);
  5497. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_BL]->set_name("DockSlotLeftBL");
  5498. left_l_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_LEFT_BL]);
  5499. left_r_hsplit = memnew(DockSplitContainer);
  5500. left_r_hsplit->set_name("DockHSplitLeftR");
  5501. left_l_hsplit->add_child(left_r_hsplit);
  5502. left_r_vsplit = memnew(DockSplitContainer);
  5503. left_r_vsplit->set_name("DockVSplitLeftR");
  5504. left_r_vsplit->set_vertical(true);
  5505. left_r_hsplit->add_child(left_r_vsplit);
  5506. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_UR] = memnew(TabContainer);
  5507. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_UR]->set_name("DockSlotLeftUR");
  5508. left_r_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_LEFT_UR]);
  5509. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_BR] = memnew(TabContainer);
  5510. dock_slot[EditorDockManager::DOCK_SLOT_LEFT_BR]->set_name("DockSlotLeftBR");
  5511. left_r_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_LEFT_BR]);
  5512. main_hsplit = memnew(DockSplitContainer);
  5513. main_hsplit->set_name("DockHSplitMain");
  5514. left_r_hsplit->add_child(main_hsplit);
  5515. VBoxContainer *center_vb = memnew(VBoxContainer);
  5516. main_hsplit->add_child(center_vb);
  5517. center_vb->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5518. center_split = memnew(DockSplitContainer);
  5519. center_split->set_name("DockVSplitCenter");
  5520. center_split->set_vertical(true);
  5521. center_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5522. center_split->set_collapsed(false);
  5523. center_vb->add_child(center_split);
  5524. right_hsplit = memnew(DockSplitContainer);
  5525. right_hsplit->set_name("DockHSplitRight");
  5526. main_hsplit->add_child(right_hsplit);
  5527. right_l_vsplit = memnew(DockSplitContainer);
  5528. right_l_vsplit->set_name("DockVSplitRightL");
  5529. right_l_vsplit->set_vertical(true);
  5530. right_hsplit->add_child(right_l_vsplit);
  5531. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_UL] = memnew(TabContainer);
  5532. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_UL]->set_name("DockSlotRightUL");
  5533. right_l_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_UL]);
  5534. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_BL] = memnew(TabContainer);
  5535. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_BL]->set_name("DockSlotRightBL");
  5536. right_l_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_BL]);
  5537. right_r_vsplit = memnew(DockSplitContainer);
  5538. right_r_vsplit->set_name("DockVSplitRightR");
  5539. right_r_vsplit->set_vertical(true);
  5540. right_hsplit->add_child(right_r_vsplit);
  5541. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_UR] = memnew(TabContainer);
  5542. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_UR]->set_name("DockSlotRightUR");
  5543. right_r_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_UR]);
  5544. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_BR] = memnew(TabContainer);
  5545. dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_BR]->set_name("DockSlotRightBR");
  5546. right_r_vsplit->add_child(dock_slot[EditorDockManager::DOCK_SLOT_RIGHT_BR]);
  5547. editor_dock_manager = memnew(EditorDockManager);
  5548. // Save the splits for easier access.
  5549. editor_dock_manager->add_vsplit(left_l_vsplit);
  5550. editor_dock_manager->add_vsplit(left_r_vsplit);
  5551. editor_dock_manager->add_vsplit(right_l_vsplit);
  5552. editor_dock_manager->add_vsplit(right_r_vsplit);
  5553. editor_dock_manager->add_hsplit(left_l_hsplit);
  5554. editor_dock_manager->add_hsplit(left_r_hsplit);
  5555. editor_dock_manager->add_hsplit(main_hsplit);
  5556. editor_dock_manager->add_hsplit(right_hsplit);
  5557. for (int i = 0; i < EditorDockManager::DOCK_SLOT_MAX; i++) {
  5558. editor_dock_manager->register_dock_slot((EditorDockManager::DockSlot)i, dock_slot[i]);
  5559. }
  5560. editor_layout_save_delay_timer = memnew(Timer);
  5561. add_child(editor_layout_save_delay_timer);
  5562. editor_layout_save_delay_timer->set_wait_time(0.5);
  5563. editor_layout_save_delay_timer->set_one_shot(true);
  5564. editor_layout_save_delay_timer->connect("timeout", callable_mp(this, &EditorNode::_save_editor_layout));
  5565. scan_changes_timer = memnew(Timer);
  5566. scan_changes_timer->set_wait_time(0.5);
  5567. scan_changes_timer->set_autostart(EDITOR_GET("interface/editor/import_resources_when_unfocused"));
  5568. scan_changes_timer->connect("timeout", callable_mp(EditorFileSystem::get_singleton(), &EditorFileSystem::scan_changes));
  5569. add_child(scan_changes_timer);
  5570. top_split = memnew(VSplitContainer);
  5571. center_split->add_child(top_split);
  5572. top_split->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5573. top_split->set_collapsed(true);
  5574. VBoxContainer *srt = memnew(VBoxContainer);
  5575. srt->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5576. srt->add_theme_constant_override("separation", 0);
  5577. top_split->add_child(srt);
  5578. scene_tabs = memnew(EditorSceneTabs);
  5579. srt->add_child(scene_tabs);
  5580. scene_tabs->connect("tab_changed", callable_mp(this, &EditorNode::_set_current_scene));
  5581. scene_tabs->connect("tab_closed", callable_mp(this, &EditorNode::_scene_tab_closed));
  5582. distraction_free = memnew(Button);
  5583. distraction_free->set_theme_type_variation("FlatMenuButton");
  5584. ED_SHORTCUT_AND_COMMAND("editor/distraction_free_mode", TTR("Distraction Free Mode"), KeyModifierMask::CTRL | KeyModifierMask::SHIFT | Key::F11);
  5585. ED_SHORTCUT_OVERRIDE("editor/distraction_free_mode", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::D);
  5586. ED_SHORTCUT_AND_COMMAND("editor/toggle_last_opened_bottom_panel", TTR("Toggle Last Opened Bottom Panel"), KeyModifierMask::CMD_OR_CTRL | Key::J);
  5587. distraction_free->set_shortcut(ED_GET_SHORTCUT("editor/distraction_free_mode"));
  5588. distraction_free->set_tooltip_text(TTR("Toggle distraction-free mode."));
  5589. distraction_free->set_toggle_mode(true);
  5590. scene_tabs->add_extra_button(distraction_free);
  5591. distraction_free->connect(SceneStringName(pressed), callable_mp(this, &EditorNode::_toggle_distraction_free_mode));
  5592. scene_root_parent = memnew(PanelContainer);
  5593. scene_root_parent->set_custom_minimum_size(Size2(0, 80) * EDSCALE);
  5594. scene_root_parent->add_theme_style_override("panel", theme->get_stylebox(SNAME("Content"), EditorStringName(EditorStyles)));
  5595. scene_root_parent->set_draw_behind_parent(true);
  5596. srt->add_child(scene_root_parent);
  5597. scene_root_parent->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5598. scene_root = memnew(SubViewport);
  5599. scene_root->set_embedding_subwindows(true);
  5600. scene_root->set_disable_3d(true);
  5601. scene_root->set_disable_input(true);
  5602. scene_root->set_as_audio_listener_2d(true);
  5603. main_screen_vbox = memnew(VBoxContainer);
  5604. main_screen_vbox->set_name("MainScreen");
  5605. main_screen_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5606. main_screen_vbox->add_theme_constant_override("separation", 0);
  5607. scene_root_parent->add_child(main_screen_vbox);
  5608. bool global_menu = !bool(EDITOR_GET("interface/editor/use_embedded_menu")) && NativeMenu::get_singleton()->has_feature(NativeMenu::FEATURE_GLOBAL_MENU);
  5609. bool can_expand = bool(EDITOR_GET("interface/editor/expand_to_title")) && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EXTEND_TO_TITLE);
  5610. if (can_expand) {
  5611. // Add spacer to avoid other controls under window minimize/maximize/close buttons (left side).
  5612. left_menu_spacer = memnew(Control);
  5613. left_menu_spacer->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  5614. title_bar->add_child(left_menu_spacer);
  5615. }
  5616. main_menu = memnew(MenuBar);
  5617. title_bar->add_child(main_menu);
  5618. main_menu->set_theme_type_variation("MainMenuBar");
  5619. main_menu->set_start_index(0); // Main menu, add to the start of global menu.
  5620. main_menu->set_prefer_global_menu(global_menu);
  5621. main_menu->set_switch_on_hover(true);
  5622. file_menu = memnew(PopupMenu);
  5623. file_menu->set_name(TTR("Scene"));
  5624. main_menu->add_child(file_menu);
  5625. main_menu->set_menu_tooltip(0, TTR("Operations with scene files."));
  5626. accept = memnew(AcceptDialog);
  5627. accept->set_autowrap(true);
  5628. accept->set_min_size(Vector2i(600, 0));
  5629. accept->set_unparent_when_invisible(true);
  5630. save_accept = memnew(AcceptDialog);
  5631. save_accept->set_unparent_when_invisible(true);
  5632. save_accept->connect("confirmed", callable_mp(this, &EditorNode::_menu_option).bind((int)MenuOptions::FILE_SAVE_AS_SCENE));
  5633. project_export = memnew(ProjectExportDialog);
  5634. gui_base->add_child(project_export);
  5635. dependency_error = memnew(DependencyErrorDialog);
  5636. gui_base->add_child(dependency_error);
  5637. dependency_fixer = memnew(DependencyEditor);
  5638. gui_base->add_child(dependency_fixer);
  5639. editor_settings_dialog = memnew(EditorSettingsDialog);
  5640. gui_base->add_child(editor_settings_dialog);
  5641. project_settings_editor = memnew(ProjectSettingsEditor(&editor_data));
  5642. gui_base->add_child(project_settings_editor);
  5643. scene_import_settings = memnew(SceneImportSettingsDialog);
  5644. gui_base->add_child(scene_import_settings);
  5645. audio_stream_import_settings = memnew(AudioStreamImportSettingsDialog);
  5646. gui_base->add_child(audio_stream_import_settings);
  5647. fontdata_import_settings = memnew(DynamicFontImportSettingsDialog);
  5648. gui_base->add_child(fontdata_import_settings);
  5649. export_template_manager = memnew(ExportTemplateManager);
  5650. gui_base->add_child(export_template_manager);
  5651. feature_profile_manager = memnew(EditorFeatureProfileManager);
  5652. gui_base->add_child(feature_profile_manager);
  5653. build_profile_manager = memnew(EditorBuildProfileManager);
  5654. gui_base->add_child(build_profile_manager);
  5655. about = memnew(EditorAbout);
  5656. gui_base->add_child(about);
  5657. feature_profile_manager->connect("current_feature_profile_changed", callable_mp(this, &EditorNode::_feature_profile_changed));
  5658. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  5659. fbx_importer_manager = memnew(FBXImporterManager);
  5660. gui_base->add_child(fbx_importer_manager);
  5661. #endif
  5662. warning = memnew(AcceptDialog);
  5663. warning->set_unparent_when_invisible(true);
  5664. warning->add_button(TTR("Copy Text"), true, "copy");
  5665. warning->connect("custom_action", callable_mp(this, &EditorNode::_copy_warning));
  5666. ED_SHORTCUT("editor/next_tab", TTR("Next Scene Tab"), KeyModifierMask::CTRL + Key::TAB);
  5667. ED_SHORTCUT("editor/prev_tab", TTR("Previous Scene Tab"), KeyModifierMask::CTRL + KeyModifierMask::SHIFT + Key::TAB);
  5668. ED_SHORTCUT("editor/filter_files", TTR("Focus FileSystem Filter"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::ALT + Key::P);
  5669. command_palette = EditorCommandPalette::get_singleton();
  5670. command_palette->set_title(TTR("Command Palette"));
  5671. gui_base->add_child(command_palette);
  5672. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/new_scene", TTR("New Scene"), KeyModifierMask::CMD_OR_CTRL + Key::N), FILE_NEW_SCENE);
  5673. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/new_inherited_scene", TTR("New Inherited Scene..."), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::N), FILE_NEW_INHERITED_SCENE);
  5674. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/open_scene", TTR("Open Scene..."), KeyModifierMask::CMD_OR_CTRL + Key::O), FILE_OPEN_SCENE);
  5675. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/reopen_closed_scene", TTR("Reopen Closed Scene"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::T), FILE_OPEN_PREV);
  5676. recent_scenes = memnew(PopupMenu);
  5677. file_menu->add_submenu_node_item(TTR("Open Recent"), recent_scenes, FILE_OPEN_RECENT);
  5678. recent_scenes->connect("id_pressed", callable_mp(this, &EditorNode::_open_recent_scene));
  5679. file_menu->add_separator();
  5680. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/save_scene", TTR("Save Scene"), KeyModifierMask::CMD_OR_CTRL + Key::S), FILE_SAVE_SCENE);
  5681. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/save_scene_as", TTR("Save Scene As..."), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::S), FILE_SAVE_AS_SCENE);
  5682. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/save_all_scenes", TTR("Save All Scenes"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + KeyModifierMask::ALT + Key::S), FILE_SAVE_ALL_SCENES);
  5683. file_menu->add_separator();
  5684. file_menu->add_shortcut(ED_SHORTCUT_ARRAY_AND_COMMAND("editor/quick_open", TTR("Quick Open..."), { int32_t(KeyModifierMask::SHIFT + KeyModifierMask::ALT + Key::O), int32_t(KeyModifierMask::CMD_OR_CTRL + Key::P) }), FILE_QUICK_OPEN);
  5685. ED_SHORTCUT_OVERRIDE_ARRAY("editor/quick_open", "macos", { int32_t(KeyModifierMask::META + KeyModifierMask::CTRL + Key::O), int32_t(KeyModifierMask::CMD_OR_CTRL + Key::P) });
  5686. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/quick_open_scene", TTR("Quick Open Scene..."), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::O), FILE_QUICK_OPEN_SCENE);
  5687. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/quick_open_script", TTR("Quick Open Script..."), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::ALT + Key::O), FILE_QUICK_OPEN_SCRIPT);
  5688. file_menu->add_separator();
  5689. export_as_menu = memnew(PopupMenu);
  5690. file_menu->add_submenu_node_item(TTR("Export As..."), export_as_menu);
  5691. export_as_menu->add_shortcut(ED_SHORTCUT("editor/export_as_mesh_library", TTR("MeshLibrary...")), FILE_EXPORT_MESH_LIBRARY);
  5692. export_as_menu->connect("index_pressed", callable_mp(this, &EditorNode::_export_as_menu_option));
  5693. file_menu->add_separator();
  5694. file_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO, false, true);
  5695. file_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO, false, true);
  5696. file_menu->add_separator();
  5697. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/reload_saved_scene", TTR("Reload Saved Scene")), EDIT_RELOAD_SAVED_SCENE);
  5698. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/close_scene", TTR("Close Scene"), KeyModifierMask::CMD_OR_CTRL + KeyModifierMask::SHIFT + Key::W), FILE_CLOSE);
  5699. ED_SHORTCUT_OVERRIDE("editor/close_scene", "macos", KeyModifierMask::CMD_OR_CTRL + Key::W);
  5700. if (!global_menu || !OS::get_singleton()->has_feature("macos")) {
  5701. // On macOS "Quit" and "About" options are in the "app" menu.
  5702. file_menu->add_separator();
  5703. file_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/file_quit", TTR("Quit"), KeyModifierMask::CMD_OR_CTRL + Key::Q), FILE_QUIT, true);
  5704. }
  5705. ED_SHORTCUT_AND_COMMAND("editor/editor_settings", TTR("Editor Settings..."));
  5706. ED_SHORTCUT_OVERRIDE("editor/editor_settings", "macos", KeyModifierMask::META + Key::COMMA);
  5707. #ifdef MACOS_ENABLED
  5708. if (global_menu && NativeMenu::get_singleton()->has_system_menu(NativeMenu::APPLICATION_MENU_ID)) {
  5709. apple_menu = memnew(PopupMenu);
  5710. apple_menu->set_system_menu(NativeMenu::APPLICATION_MENU_ID);
  5711. main_menu->add_child(apple_menu);
  5712. apple_menu->add_shortcut(ED_GET_SHORTCUT("editor/editor_settings"), SETTINGS_PREFERENCES);
  5713. apple_menu->add_separator();
  5714. apple_menu->connect("id_pressed", callable_mp(this, &EditorNode::_menu_option));
  5715. }
  5716. #endif
  5717. project_menu = memnew(PopupMenu);
  5718. project_menu->set_name(TTR("Project"));
  5719. main_menu->add_child(project_menu);
  5720. project_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/project_settings", TTR("Project Settings..."), Key::NONE, TTR("Project Settings")), RUN_SETTINGS);
  5721. project_menu->connect("id_pressed", callable_mp(this, &EditorNode::_menu_option));
  5722. project_menu->add_separator();
  5723. project_menu->add_item(TTR("Version Control"), VCS_MENU);
  5724. project_menu->add_separator();
  5725. project_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/export", TTR("Export..."), Key::NONE, TTR("Export")), FILE_EXPORT_PROJECT);
  5726. #ifndef ANDROID_ENABLED
  5727. project_menu->add_item(TTR("Install Android Build Template..."), FILE_INSTALL_ANDROID_SOURCE);
  5728. project_menu->add_item(TTR("Open User Data Folder"), RUN_USER_DATA_FOLDER);
  5729. #endif
  5730. project_menu->add_separator();
  5731. tool_menu = memnew(PopupMenu);
  5732. tool_menu->connect("index_pressed", callable_mp(this, &EditorNode::_tool_menu_option));
  5733. project_menu->add_submenu_node_item(TTR("Tools"), tool_menu);
  5734. tool_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/orphan_resource_explorer", TTR("Orphan Resource Explorer...")), TOOLS_ORPHAN_RESOURCES);
  5735. tool_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/engine_compilation_configuration_editor", TTR("Engine Compilation Configuration Editor...")), TOOLS_BUILD_PROFILE_MANAGER);
  5736. tool_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/upgrade_mesh_surfaces", TTR("Upgrade Mesh Surfaces...")), TOOLS_SURFACE_UPGRADE);
  5737. project_menu->add_separator();
  5738. project_menu->add_shortcut(ED_SHORTCUT("editor/reload_current_project", TTR("Reload Current Project")), RELOAD_CURRENT_PROJECT);
  5739. ED_SHORTCUT_AND_COMMAND("editor/quit_to_project_list", TTR("Quit to Project List"), KeyModifierMask::CTRL + KeyModifierMask::SHIFT + Key::Q);
  5740. ED_SHORTCUT_OVERRIDE("editor/quit_to_project_list", "macos", KeyModifierMask::META + KeyModifierMask::CTRL + KeyModifierMask::ALT + Key::Q);
  5741. project_menu->add_shortcut(ED_GET_SHORTCUT("editor/quit_to_project_list"), RUN_PROJECT_MANAGER, true);
  5742. // Spacer to center 2D / 3D / Script buttons.
  5743. HBoxContainer *left_spacer = memnew(HBoxContainer);
  5744. left_spacer->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  5745. left_spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5746. title_bar->add_child(left_spacer);
  5747. if (can_expand && global_menu) {
  5748. project_title = memnew(Label);
  5749. project_title->add_theme_font_override("font", theme->get_font(SNAME("bold"), EditorStringName(EditorFonts)));
  5750. project_title->add_theme_font_size_override("font_size", theme->get_font_size(SNAME("bold_size"), EditorStringName(EditorFonts)));
  5751. project_title->set_focus_mode(Control::FOCUS_NONE);
  5752. project_title->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  5753. project_title->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  5754. project_title->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5755. project_title->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  5756. left_spacer->add_child(project_title);
  5757. }
  5758. main_editor_button_hb = memnew(HBoxContainer);
  5759. title_bar->add_child(main_editor_button_hb);
  5760. // Options are added and handled by DebuggerEditorPlugin.
  5761. debug_menu = memnew(PopupMenu);
  5762. debug_menu->set_name(TTR("Debug"));
  5763. main_menu->add_child(debug_menu);
  5764. settings_menu = memnew(PopupMenu);
  5765. settings_menu->set_name(TTR("Editor"));
  5766. main_menu->add_child(settings_menu);
  5767. #ifdef MACOS_ENABLED
  5768. if (!global_menu) {
  5769. settings_menu->add_shortcut(ED_GET_SHORTCUT("editor/editor_settings"), SETTINGS_PREFERENCES);
  5770. }
  5771. #else
  5772. settings_menu->add_shortcut(ED_GET_SHORTCUT("editor/editor_settings"), SETTINGS_PREFERENCES);
  5773. #endif
  5774. settings_menu->add_shortcut(ED_SHORTCUT("editor/command_palette", TTR("Command Palette..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::P), HELP_COMMAND_PALETTE);
  5775. settings_menu->add_separator();
  5776. settings_menu->add_submenu_node_item(TTR("Editor Docks"), editor_dock_manager->get_docks_menu());
  5777. editor_layouts = memnew(PopupMenu);
  5778. editor_layouts->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  5779. settings_menu->add_submenu_node_item(TTR("Editor Layout"), editor_layouts);
  5780. editor_layouts->connect("id_pressed", callable_mp(this, &EditorNode::_layout_menu_option));
  5781. settings_menu->add_separator();
  5782. ED_SHORTCUT_AND_COMMAND("editor/take_screenshot", TTR("Take Screenshot"), KeyModifierMask::CTRL | Key::F12);
  5783. ED_SHORTCUT_OVERRIDE("editor/take_screenshot", "macos", KeyModifierMask::META | Key::F12);
  5784. settings_menu->add_shortcut(ED_GET_SHORTCUT("editor/take_screenshot"), EDITOR_SCREENSHOT);
  5785. settings_menu->set_item_tooltip(-1, TTR("Screenshots are stored in the Editor Data/Settings Folder."));
  5786. #ifndef ANDROID_ENABLED
  5787. ED_SHORTCUT_AND_COMMAND("editor/fullscreen_mode", TTR("Toggle Fullscreen"), KeyModifierMask::SHIFT | Key::F11);
  5788. ED_SHORTCUT_OVERRIDE("editor/fullscreen_mode", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::F);
  5789. settings_menu->add_shortcut(ED_GET_SHORTCUT("editor/fullscreen_mode"), SETTINGS_TOGGLE_FULLSCREEN);
  5790. #endif
  5791. settings_menu->add_separator();
  5792. #ifndef ANDROID_ENABLED
  5793. if (OS::get_singleton()->get_data_path() == OS::get_singleton()->get_config_path()) {
  5794. // Configuration and data folders are located in the same place (Windows/macOS).
  5795. settings_menu->add_item(TTR("Open Editor Data/Settings Folder"), SETTINGS_EDITOR_DATA_FOLDER);
  5796. } else {
  5797. // Separate configuration and data folders (Linux).
  5798. settings_menu->add_item(TTR("Open Editor Data Folder"), SETTINGS_EDITOR_DATA_FOLDER);
  5799. settings_menu->add_item(TTR("Open Editor Settings Folder"), SETTINGS_EDITOR_CONFIG_FOLDER);
  5800. }
  5801. settings_menu->add_separator();
  5802. #endif
  5803. settings_menu->add_item(TTR("Manage Editor Features..."), SETTINGS_MANAGE_FEATURE_PROFILES);
  5804. #ifndef ANDROID_ENABLED
  5805. settings_menu->add_item(TTR("Manage Export Templates..."), SETTINGS_MANAGE_EXPORT_TEMPLATES);
  5806. #endif
  5807. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  5808. settings_menu->add_item(TTR("Configure FBX Importer..."), SETTINGS_MANAGE_FBX_IMPORTER);
  5809. #endif
  5810. help_menu = memnew(PopupMenu);
  5811. help_menu->set_name(TTR("Help"));
  5812. if (global_menu && NativeMenu::get_singleton()->has_system_menu(NativeMenu::HELP_MENU_ID)) {
  5813. help_menu->set_system_menu(NativeMenu::HELP_MENU_ID);
  5814. }
  5815. main_menu->add_child(help_menu);
  5816. help_menu->connect("id_pressed", callable_mp(this, &EditorNode::_menu_option));
  5817. ED_SHORTCUT_AND_COMMAND("editor/editor_help", TTR("Search Help..."), Key::F1);
  5818. ED_SHORTCUT_OVERRIDE("editor/editor_help", "macos", KeyModifierMask::ALT | Key::SPACE);
  5819. help_menu->add_icon_shortcut(theme->get_icon(SNAME("HelpSearch"), EditorStringName(EditorIcons)), ED_GET_SHORTCUT("editor/editor_help"), HELP_SEARCH);
  5820. help_menu->add_separator();
  5821. help_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/online_docs", TTR("Online Documentation")), HELP_DOCS);
  5822. help_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/forum", TTR("Forum")), HELP_FORUM);
  5823. help_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/community", TTR("Community")), HELP_COMMUNITY);
  5824. help_menu->add_separator();
  5825. help_menu->add_icon_shortcut(theme->get_icon(SNAME("ActionCopy"), EditorStringName(EditorIcons)), ED_SHORTCUT_AND_COMMAND("editor/copy_system_info", TTR("Copy System Info")), HELP_COPY_SYSTEM_INFO);
  5826. help_menu->set_item_tooltip(-1, TTR("Copies the system info as a single-line text into the clipboard."));
  5827. help_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/report_a_bug", TTR("Report a Bug")), HELP_REPORT_A_BUG);
  5828. help_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/suggest_a_feature", TTR("Suggest a Feature")), HELP_SUGGEST_A_FEATURE);
  5829. help_menu->add_shortcut(ED_SHORTCUT_AND_COMMAND("editor/send_docs_feedback", TTR("Send Docs Feedback")), HELP_SEND_DOCS_FEEDBACK);
  5830. help_menu->add_separator();
  5831. if (!global_menu || !OS::get_singleton()->has_feature("macos")) {
  5832. // On macOS "Quit" and "About" options are in the "app" menu.
  5833. help_menu->add_icon_shortcut(theme->get_icon(SNAME("Godot"), EditorStringName(EditorIcons)), ED_SHORTCUT_AND_COMMAND("editor/about", TTR("About Godot...")), HELP_ABOUT);
  5834. }
  5835. help_menu->add_icon_shortcut(theme->get_icon(SNAME("Heart"), EditorStringName(EditorIcons)), ED_SHORTCUT_AND_COMMAND("editor/support_development", TTR("Support Godot Development")), HELP_SUPPORT_GODOT_DEVELOPMENT);
  5836. // Spacer to center 2D / 3D / Script buttons.
  5837. Control *right_spacer = memnew(Control);
  5838. right_spacer->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  5839. right_spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  5840. title_bar->add_child(right_spacer);
  5841. project_run_bar = memnew(EditorRunBar);
  5842. title_bar->add_child(project_run_bar);
  5843. project_run_bar->connect("play_pressed", callable_mp(this, &EditorNode::_project_run_started));
  5844. project_run_bar->connect("stop_pressed", callable_mp(this, &EditorNode::_project_run_stopped));
  5845. HBoxContainer *right_menu_hb = memnew(HBoxContainer);
  5846. title_bar->add_child(right_menu_hb);
  5847. renderer = memnew(OptionButton);
  5848. renderer->set_visible(true);
  5849. renderer->set_flat(true);
  5850. renderer->set_theme_type_variation("TopBarOptionButton");
  5851. renderer->set_fit_to_longest_item(false);
  5852. renderer->set_focus_mode(Control::FOCUS_NONE);
  5853. renderer->set_tooltip_text(TTR("Choose a rendering method.\n\nNotes:\n- On mobile platforms, the Mobile rendering method is used if Forward+ is selected here.\n- On the web platform, the Compatibility rendering method is always used."));
  5854. right_menu_hb->add_child(renderer);
  5855. if (can_expand) {
  5856. // Add spacer to avoid other controls under the window minimize/maximize/close buttons (right side).
  5857. right_menu_spacer = memnew(Control);
  5858. right_menu_spacer->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  5859. title_bar->add_child(right_menu_spacer);
  5860. }
  5861. String current_renderer_ps = GLOBAL_GET("rendering/renderer/rendering_method");
  5862. current_renderer_ps = current_renderer_ps.to_lower();
  5863. String current_renderer_os = OS::get_singleton()->get_current_rendering_method().to_lower();
  5864. // Add the renderers name to the UI.
  5865. if (current_renderer_ps == current_renderer_os) {
  5866. renderer->connect("item_selected", callable_mp(this, &EditorNode::_renderer_selected));
  5867. // As we are doing string comparisons, keep in standard case to prevent problems with capitals
  5868. // "vulkan" in particular uses lowercase "v" in the code, and uppercase in the UI.
  5869. PackedStringArray renderers = ProjectSettings::get_singleton()->get_custom_property_info().get(StringName("rendering/renderer/rendering_method")).hint_string.split(",", false);
  5870. for (int i = 0; i < renderers.size(); i++) {
  5871. String rendering_method = renderers[i];
  5872. _add_renderer_entry(rendering_method, false);
  5873. renderer->set_item_metadata(i, rendering_method);
  5874. // Lowercase for standard comparison.
  5875. rendering_method = rendering_method.to_lower();
  5876. if (current_renderer_ps == rendering_method) {
  5877. renderer->select(i);
  5878. renderer_current = i;
  5879. }
  5880. }
  5881. } else {
  5882. // It's an CLI-overridden rendering method.
  5883. _add_renderer_entry(current_renderer_os, true);
  5884. renderer->set_item_metadata(0, current_renderer_os);
  5885. renderer->select(0);
  5886. renderer_current = 0;
  5887. }
  5888. _update_renderer_color();
  5889. video_restart_dialog = memnew(ConfirmationDialog);
  5890. video_restart_dialog->set_ok_button_text(TTR("Save & Restart"));
  5891. video_restart_dialog->connect("confirmed", callable_mp(this, &EditorNode::_menu_option).bind(SET_RENDERER_NAME_SAVE_AND_RESTART));
  5892. gui_base->add_child(video_restart_dialog);
  5893. progress_hb = memnew(BackgroundProgress);
  5894. layout_dialog = memnew(EditorLayoutsDialog);
  5895. gui_base->add_child(layout_dialog);
  5896. layout_dialog->set_hide_on_ok(false);
  5897. layout_dialog->set_size(Size2(225, 270) * EDSCALE);
  5898. layout_dialog->connect("name_confirmed", callable_mp(this, &EditorNode::_dialog_action));
  5899. update_spinner = memnew(MenuButton);
  5900. right_menu_hb->add_child(update_spinner);
  5901. update_spinner->set_icon(theme->get_icon(SNAME("Progress1"), EditorStringName(EditorIcons)));
  5902. update_spinner->get_popup()->connect("id_pressed", callable_mp(this, &EditorNode::_menu_option));
  5903. PopupMenu *p = update_spinner->get_popup();
  5904. p->add_radio_check_item(TTR("Update Continuously"), SETTINGS_UPDATE_CONTINUOUSLY);
  5905. p->add_radio_check_item(TTR("Update When Changed"), SETTINGS_UPDATE_WHEN_CHANGED);
  5906. p->add_separator();
  5907. p->add_item(TTR("Hide Update Spinner"), SETTINGS_UPDATE_SPINNER_HIDE);
  5908. _update_update_spinner();
  5909. // Instantiate and place editor docks.
  5910. memnew(SceneTreeDock(scene_root, editor_selection, editor_data));
  5911. memnew(InspectorDock(editor_data));
  5912. memnew(ImportDock);
  5913. memnew(NodeDock);
  5914. FileSystemDock *filesystem_dock = memnew(FileSystemDock);
  5915. filesystem_dock->connect("inherit", callable_mp(this, &EditorNode::_inherit_request));
  5916. filesystem_dock->connect("instantiate", callable_mp(this, &EditorNode::_instantiate_request));
  5917. filesystem_dock->connect("display_mode_changed", callable_mp(this, &EditorNode::_save_editor_layout));
  5918. get_project_settings()->connect_filesystem_dock_signals(filesystem_dock);
  5919. history_dock = memnew(HistoryDock);
  5920. // Scene: Top left.
  5921. editor_dock_manager->add_dock(SceneTreeDock::get_singleton(), TTR("Scene"), EditorDockManager::DOCK_SLOT_LEFT_UR, nullptr, "PackedScene");
  5922. // Import: Top left, behind Scene.
  5923. editor_dock_manager->add_dock(ImportDock::get_singleton(), TTR("Import"), EditorDockManager::DOCK_SLOT_LEFT_UR, nullptr, "FileAccess");
  5924. // FileSystem: Bottom left.
  5925. editor_dock_manager->add_dock(FileSystemDock::get_singleton(), TTR("FileSystem"), EditorDockManager::DOCK_SLOT_LEFT_BR, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_filesystem_bottom_panel", TTR("Toggle FileSystem Bottom Panel"), KeyModifierMask::ALT | Key::F), "Folder");
  5926. // Inspector: Full height right.
  5927. editor_dock_manager->add_dock(InspectorDock::get_singleton(), TTR("Inspector"), EditorDockManager::DOCK_SLOT_RIGHT_UL, nullptr, "AnimationTrackList");
  5928. // Node: Full height right, behind Inspector.
  5929. editor_dock_manager->add_dock(NodeDock::get_singleton(), TTR("Node"), EditorDockManager::DOCK_SLOT_RIGHT_UL, nullptr, "Object");
  5930. // History: Full height right, behind Node.
  5931. editor_dock_manager->add_dock(history_dock, TTR("History"), EditorDockManager::DOCK_SLOT_RIGHT_UL, nullptr, "History");
  5932. // Add some offsets to left_r and main hsplits to make LEFT_R and RIGHT_L docks wider than minsize.
  5933. left_r_hsplit->set_split_offset(270 * EDSCALE);
  5934. main_hsplit->set_split_offset(-270 * EDSCALE);
  5935. // Define corresponding default layout.
  5936. const String docks_section = "docks";
  5937. default_layout.instantiate();
  5938. // Dock numbers are based on DockSlot enum value + 1.
  5939. default_layout->set_value(docks_section, "dock_3", "Scene,Import");
  5940. default_layout->set_value(docks_section, "dock_4", "FileSystem");
  5941. default_layout->set_value(docks_section, "dock_5", "Inspector,Node,History");
  5942. // There are 4 vsplits and 4 hsplits.
  5943. for (int i = 0; i < editor_dock_manager->get_vsplit_count(); i++) {
  5944. default_layout->set_value(docks_section, "dock_split_" + itos(i + 1), 0);
  5945. }
  5946. default_layout->set_value(docks_section, "dock_hsplit_1", 0);
  5947. default_layout->set_value(docks_section, "dock_hsplit_2", 270 * EDSCALE);
  5948. default_layout->set_value(docks_section, "dock_hsplit_3", -270 * EDSCALE);
  5949. default_layout->set_value(docks_section, "dock_hsplit_4", 0);
  5950. _update_layouts_menu();
  5951. // Bottom panels.
  5952. bottom_panel = memnew(EditorBottomPanel);
  5953. center_split->add_child(bottom_panel);
  5954. center_split->set_dragger_visibility(SplitContainer::DRAGGER_HIDDEN);
  5955. log = memnew(EditorLog);
  5956. Button *output_button = bottom_panel->add_item(TTR("Output"), log, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_output_bottom_panel", TTR("Toggle Output Bottom Panel"), KeyModifierMask::ALT | Key::O));
  5957. log->set_tool_button(output_button);
  5958. center_split->connect(SceneStringName(resized), callable_mp(this, &EditorNode::_vp_resized));
  5959. native_shader_source_visualizer = memnew(EditorNativeShaderSourceVisualizer);
  5960. gui_base->add_child(native_shader_source_visualizer);
  5961. orphan_resources = memnew(OrphanResourcesDialog);
  5962. gui_base->add_child(orphan_resources);
  5963. surface_upgrade_dialog = memnew(SurfaceUpgradeDialog);
  5964. gui_base->add_child(surface_upgrade_dialog);
  5965. confirmation = memnew(ConfirmationDialog);
  5966. gui_base->add_child(confirmation);
  5967. confirmation->connect("confirmed", callable_mp(this, &EditorNode::_menu_confirm_current));
  5968. save_confirmation = memnew(ConfirmationDialog);
  5969. save_confirmation->add_button(TTR("Don't Save"), DisplayServer::get_singleton()->get_swap_cancel_ok(), "discard");
  5970. gui_base->add_child(save_confirmation);
  5971. save_confirmation->set_min_size(Vector2(450.0 * EDSCALE, 0));
  5972. save_confirmation->connect("confirmed", callable_mp(this, &EditorNode::_menu_confirm_current));
  5973. save_confirmation->connect("custom_action", callable_mp(this, &EditorNode::_discard_changes));
  5974. save_confirmation->connect("canceled", callable_mp(this, &EditorNode::_cancel_close_scene_tab));
  5975. gradle_build_manage_templates = memnew(ConfirmationDialog);
  5976. gradle_build_manage_templates->set_text(TTR("Android build template is missing, please install relevant templates."));
  5977. gradle_build_manage_templates->set_ok_button_text(TTR("Manage Templates"));
  5978. gradle_build_manage_templates->add_button(TTR("Install from file"))->connect(SceneStringName(pressed), callable_mp(this, &EditorNode::_menu_option).bind(SETTINGS_INSTALL_ANDROID_BUILD_TEMPLATE));
  5979. gradle_build_manage_templates->connect("confirmed", callable_mp(this, &EditorNode::_menu_option).bind(SETTINGS_MANAGE_EXPORT_TEMPLATES));
  5980. gui_base->add_child(gradle_build_manage_templates);
  5981. file_android_build_source = memnew(EditorFileDialog);
  5982. file_android_build_source->set_title(TTR("Select Android sources file"));
  5983. file_android_build_source->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  5984. file_android_build_source->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  5985. file_android_build_source->add_filter("*.zip");
  5986. file_android_build_source->connect("file_selected", callable_mp(this, &EditorNode::_android_build_source_selected));
  5987. gui_base->add_child(file_android_build_source);
  5988. {
  5989. VBoxContainer *vbox = memnew(VBoxContainer);
  5990. install_android_build_template_message = memnew(Label);
  5991. install_android_build_template_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  5992. install_android_build_template_message->set_custom_minimum_size(Size2(300 * EDSCALE, 1));
  5993. vbox->add_child(install_android_build_template_message);
  5994. choose_android_export_profile = memnew(OptionButton);
  5995. choose_android_export_profile->connect("item_selected", callable_mp(this, &EditorNode::_android_export_preset_selected));
  5996. vbox->add_child(choose_android_export_profile);
  5997. install_android_build_template = memnew(ConfirmationDialog);
  5998. install_android_build_template->set_ok_button_text(TTR("Install"));
  5999. install_android_build_template->connect("confirmed", callable_mp(this, &EditorNode::_menu_confirm_current));
  6000. install_android_build_template->add_child(vbox);
  6001. install_android_build_template->set_min_size(Vector2(500.0 * EDSCALE, 0));
  6002. gui_base->add_child(install_android_build_template);
  6003. }
  6004. remove_android_build_template = memnew(ConfirmationDialog);
  6005. remove_android_build_template->set_ok_button_text(TTR("Show in File Manager"));
  6006. remove_android_build_template->connect("confirmed", callable_mp(this, &EditorNode::_menu_option).bind(FILE_EXPLORE_ANDROID_BUILD_TEMPLATES));
  6007. gui_base->add_child(remove_android_build_template);
  6008. file_templates = memnew(EditorFileDialog);
  6009. file_templates->set_title(TTR("Import Templates From ZIP File"));
  6010. gui_base->add_child(file_templates);
  6011. file_templates->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  6012. file_templates->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  6013. file_templates->clear_filters();
  6014. file_templates->add_filter("*.tpz", TTR("Template Package"));
  6015. file = memnew(EditorFileDialog);
  6016. gui_base->add_child(file);
  6017. file->set_current_dir("res://");
  6018. file->set_transient_to_focused(true);
  6019. file_export_lib = memnew(EditorFileDialog);
  6020. file_export_lib->set_title(TTR("Export Library"));
  6021. file_export_lib->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  6022. file_export_lib->connect("file_selected", callable_mp(this, &EditorNode::_dialog_action));
  6023. file_export_lib->add_option(TTR("Merge With Existing"), Vector<String>(), true);
  6024. file_export_lib->add_option(TTR("Apply MeshInstance Transforms"), Vector<String>(), false);
  6025. gui_base->add_child(file_export_lib);
  6026. file_script = memnew(EditorFileDialog);
  6027. file_script->set_title(TTR("Open & Run a Script"));
  6028. file_script->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  6029. file_script->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  6030. List<String> sexts;
  6031. ResourceLoader::get_recognized_extensions_for_type("Script", &sexts);
  6032. for (const String &E : sexts) {
  6033. file_script->add_filter("*." + E);
  6034. }
  6035. gui_base->add_child(file_script);
  6036. file_script->connect("file_selected", callable_mp(this, &EditorNode::_dialog_action));
  6037. file_menu->connect("id_pressed", callable_mp(this, &EditorNode::_menu_option));
  6038. file_menu->connect("about_to_popup", callable_mp(this, &EditorNode::_update_file_menu_opened));
  6039. file_menu->connect("popup_hide", callable_mp(this, &EditorNode::_update_file_menu_closed));
  6040. settings_menu->connect("id_pressed", callable_mp(this, &EditorNode::_menu_option));
  6041. file->connect("file_selected", callable_mp(this, &EditorNode::_dialog_action));
  6042. file_templates->connect("file_selected", callable_mp(this, &EditorNode::_dialog_action));
  6043. audio_preview_gen = memnew(AudioStreamPreviewGenerator);
  6044. add_child(audio_preview_gen);
  6045. add_editor_plugin(memnew(DebuggerEditorPlugin(debug_menu)));
  6046. disk_changed = memnew(ConfirmationDialog);
  6047. {
  6048. VBoxContainer *vbc = memnew(VBoxContainer);
  6049. disk_changed->add_child(vbc);
  6050. Label *dl = memnew(Label);
  6051. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?"));
  6052. vbc->add_child(dl);
  6053. disk_changed_list = memnew(Tree);
  6054. vbc->add_child(disk_changed_list);
  6055. disk_changed_list->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  6056. disk_changed->connect("confirmed", callable_mp(this, &EditorNode::_reload_modified_scenes));
  6057. disk_changed->connect("confirmed", callable_mp(this, &EditorNode::_reload_project_settings));
  6058. disk_changed->set_ok_button_text(TTR("Reload"));
  6059. disk_changed->add_button(TTR("Resave"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave");
  6060. disk_changed->connect("custom_action", callable_mp(this, &EditorNode::_resave_scenes));
  6061. }
  6062. gui_base->add_child(disk_changed);
  6063. add_editor_plugin(memnew(AnimationPlayerEditorPlugin));
  6064. add_editor_plugin(memnew(AnimationTrackKeyEditEditorPlugin));
  6065. add_editor_plugin(memnew(CanvasItemEditorPlugin));
  6066. add_editor_plugin(memnew(Node3DEditorPlugin));
  6067. add_editor_plugin(memnew(ScriptEditorPlugin));
  6068. EditorAudioBuses *audio_bus_editor = EditorAudioBuses::register_editor();
  6069. ScriptTextEditor::register_editor(); // Register one for text scripts.
  6070. TextEditor::register_editor();
  6071. if (AssetLibraryEditorPlugin::is_available()) {
  6072. add_editor_plugin(memnew(AssetLibraryEditorPlugin));
  6073. } else {
  6074. print_verbose("Asset Library not available (due to using Web editor, or SSL support disabled).");
  6075. }
  6076. // More visually meaningful to have this later.
  6077. bottom_panel->move_item_to_end(AnimationPlayerEditor::get_singleton());
  6078. add_editor_plugin(VersionControlEditorPlugin::get_singleton());
  6079. vcs_actions_menu = VersionControlEditorPlugin::get_singleton()->get_version_control_actions_panel();
  6080. vcs_actions_menu->connect("index_pressed", callable_mp(this, &EditorNode::_version_control_menu_option));
  6081. vcs_actions_menu->add_item(TTR("Create/Override Version Control Metadata..."), RUN_VCS_METADATA);
  6082. vcs_actions_menu->add_item(TTR("Version Control Settings..."), RUN_VCS_SETTINGS);
  6083. project_menu->set_item_submenu_node(project_menu->get_item_index(VCS_MENU), vcs_actions_menu);
  6084. add_editor_plugin(memnew(AudioBusesEditorPlugin(audio_bus_editor)));
  6085. for (int i = 0; i < EditorPlugins::get_plugin_count(); i++) {
  6086. add_editor_plugin(EditorPlugins::create(i));
  6087. }
  6088. for (const StringName &extension_class_name : GDExtensionEditorPlugins::get_extension_classes()) {
  6089. add_extension_editor_plugin(extension_class_name);
  6090. }
  6091. GDExtensionEditorPlugins::editor_node_add_plugin = &EditorNode::add_extension_editor_plugin;
  6092. GDExtensionEditorPlugins::editor_node_remove_plugin = &EditorNode::remove_extension_editor_plugin;
  6093. for (int i = 0; i < plugin_init_callback_count; i++) {
  6094. plugin_init_callbacks[i]();
  6095. }
  6096. resource_preview->add_preview_generator(Ref<EditorTexturePreviewPlugin>(memnew(EditorTexturePreviewPlugin)));
  6097. resource_preview->add_preview_generator(Ref<EditorImagePreviewPlugin>(memnew(EditorImagePreviewPlugin)));
  6098. resource_preview->add_preview_generator(Ref<EditorPackedScenePreviewPlugin>(memnew(EditorPackedScenePreviewPlugin)));
  6099. resource_preview->add_preview_generator(Ref<EditorMaterialPreviewPlugin>(memnew(EditorMaterialPreviewPlugin)));
  6100. resource_preview->add_preview_generator(Ref<EditorScriptPreviewPlugin>(memnew(EditorScriptPreviewPlugin)));
  6101. resource_preview->add_preview_generator(Ref<EditorAudioStreamPreviewPlugin>(memnew(EditorAudioStreamPreviewPlugin)));
  6102. resource_preview->add_preview_generator(Ref<EditorMeshPreviewPlugin>(memnew(EditorMeshPreviewPlugin)));
  6103. resource_preview->add_preview_generator(Ref<EditorBitmapPreviewPlugin>(memnew(EditorBitmapPreviewPlugin)));
  6104. resource_preview->add_preview_generator(Ref<EditorFontPreviewPlugin>(memnew(EditorFontPreviewPlugin)));
  6105. resource_preview->add_preview_generator(Ref<EditorGradientPreviewPlugin>(memnew(EditorGradientPreviewPlugin)));
  6106. {
  6107. Ref<StandardMaterial3DConversionPlugin> spatial_mat_convert;
  6108. spatial_mat_convert.instantiate();
  6109. resource_conversion_plugins.push_back(spatial_mat_convert);
  6110. Ref<ORMMaterial3DConversionPlugin> orm_mat_convert;
  6111. orm_mat_convert.instantiate();
  6112. resource_conversion_plugins.push_back(orm_mat_convert);
  6113. Ref<CanvasItemMaterialConversionPlugin> canvas_item_mat_convert;
  6114. canvas_item_mat_convert.instantiate();
  6115. resource_conversion_plugins.push_back(canvas_item_mat_convert);
  6116. Ref<ParticleProcessMaterialConversionPlugin> particles_mat_convert;
  6117. particles_mat_convert.instantiate();
  6118. resource_conversion_plugins.push_back(particles_mat_convert);
  6119. Ref<ProceduralSkyMaterialConversionPlugin> procedural_sky_mat_convert;
  6120. procedural_sky_mat_convert.instantiate();
  6121. resource_conversion_plugins.push_back(procedural_sky_mat_convert);
  6122. Ref<PanoramaSkyMaterialConversionPlugin> panorama_sky_mat_convert;
  6123. panorama_sky_mat_convert.instantiate();
  6124. resource_conversion_plugins.push_back(panorama_sky_mat_convert);
  6125. Ref<PhysicalSkyMaterialConversionPlugin> physical_sky_mat_convert;
  6126. physical_sky_mat_convert.instantiate();
  6127. resource_conversion_plugins.push_back(physical_sky_mat_convert);
  6128. Ref<FogMaterialConversionPlugin> fog_mat_convert;
  6129. fog_mat_convert.instantiate();
  6130. resource_conversion_plugins.push_back(fog_mat_convert);
  6131. Ref<VisualShaderConversionPlugin> vshader_convert;
  6132. vshader_convert.instantiate();
  6133. resource_conversion_plugins.push_back(vshader_convert);
  6134. }
  6135. update_spinner_step_msec = OS::get_singleton()->get_ticks_msec();
  6136. update_spinner_step_frame = Engine::get_singleton()->get_frames_drawn();
  6137. editor_plugin_screen = nullptr;
  6138. editor_plugins_over = memnew(EditorPluginList);
  6139. editor_plugins_force_over = memnew(EditorPluginList);
  6140. editor_plugins_force_input_forwarding = memnew(EditorPluginList);
  6141. Ref<GDExtensionExportPlugin> gdextension_export_plugin;
  6142. gdextension_export_plugin.instantiate();
  6143. EditorExport::get_singleton()->add_export_plugin(gdextension_export_plugin);
  6144. Ref<DedicatedServerExportPlugin> dedicated_server_export_plugin;
  6145. dedicated_server_export_plugin.instantiate();
  6146. EditorExport::get_singleton()->add_export_plugin(dedicated_server_export_plugin);
  6147. Ref<PackedSceneEditorTranslationParserPlugin> packed_scene_translation_parser_plugin;
  6148. packed_scene_translation_parser_plugin.instantiate();
  6149. EditorTranslationParser::get_singleton()->add_parser(packed_scene_translation_parser_plugin, EditorTranslationParser::STANDARD);
  6150. _edit_current();
  6151. current = nullptr;
  6152. saving_resource = Ref<Resource>();
  6153. set_process(true);
  6154. open_imported = memnew(ConfirmationDialog);
  6155. open_imported->set_ok_button_text(TTR("Open Anyway"));
  6156. new_inherited_button = open_imported->add_button(TTR("New Inherited"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "inherit");
  6157. open_imported->connect("confirmed", callable_mp(this, &EditorNode::_open_imported));
  6158. open_imported->connect("custom_action", callable_mp(this, &EditorNode::_inherit_imported));
  6159. gui_base->add_child(open_imported);
  6160. quick_open = memnew(EditorQuickOpen);
  6161. gui_base->add_child(quick_open);
  6162. quick_open->connect("quick_open", callable_mp(this, &EditorNode::_quick_opened));
  6163. _update_recent_scenes();
  6164. set_process_shortcut_input(true);
  6165. load_errors = memnew(RichTextLabel);
  6166. load_error_dialog = memnew(AcceptDialog);
  6167. load_error_dialog->set_unparent_when_invisible(true);
  6168. load_error_dialog->add_child(load_errors);
  6169. load_error_dialog->set_title(TTR("Load Errors"));
  6170. execute_outputs = memnew(RichTextLabel);
  6171. execute_outputs->set_selection_enabled(true);
  6172. execute_outputs->set_context_menu_enabled(true);
  6173. execute_output_dialog = memnew(AcceptDialog);
  6174. execute_output_dialog->set_unparent_when_invisible(true);
  6175. execute_output_dialog->add_child(execute_outputs);
  6176. execute_output_dialog->set_title("");
  6177. EditorFileSystem::get_singleton()->connect("sources_changed", callable_mp(this, &EditorNode::_sources_changed));
  6178. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &EditorNode::_fs_changed));
  6179. EditorFileSystem::get_singleton()->connect("resources_reimported", callable_mp(this, &EditorNode::_resources_reimported));
  6180. EditorFileSystem::get_singleton()->connect("resources_reload", callable_mp(this, &EditorNode::_resources_changed));
  6181. _build_icon_type_cache();
  6182. pick_main_scene = memnew(ConfirmationDialog);
  6183. gui_base->add_child(pick_main_scene);
  6184. pick_main_scene->set_ok_button_text(TTR("Select"));
  6185. pick_main_scene->connect("confirmed", callable_mp(this, &EditorNode::_menu_option).bind(SETTINGS_PICK_MAIN_SCENE));
  6186. select_current_scene_button = pick_main_scene->add_button(TTR("Select Current"), true, "select_current");
  6187. pick_main_scene->connect("custom_action", callable_mp(this, &EditorNode::_pick_main_scene_custom_action));
  6188. for (int i = 0; i < _init_callbacks.size(); i++) {
  6189. _init_callbacks[i]();
  6190. }
  6191. editor_data.add_edited_scene(-1);
  6192. editor_data.set_edited_scene(0);
  6193. scene_tabs->update_scene_tabs();
  6194. ImportDock::get_singleton()->initialize_import_options();
  6195. FileAccess::set_file_close_fail_notify_callback(_file_access_close_error_notify);
  6196. print_handler.printfunc = _print_handler;
  6197. print_handler.userdata = this;
  6198. add_print_handler(&print_handler);
  6199. ResourceSaver::set_save_callback(_resource_saved);
  6200. ResourceLoader::set_load_callback(_resource_loaded);
  6201. // Use the Ctrl modifier so F2 can be used to rename nodes in the scene tree dock.
  6202. ED_SHORTCUT_AND_COMMAND("editor/editor_2d", TTR("Open 2D Editor"), KeyModifierMask::CTRL | Key::F1);
  6203. ED_SHORTCUT_AND_COMMAND("editor/editor_3d", TTR("Open 3D Editor"), KeyModifierMask::CTRL | Key::F2);
  6204. ED_SHORTCUT_AND_COMMAND("editor/editor_script", TTR("Open Script Editor"), KeyModifierMask::CTRL | Key::F3);
  6205. ED_SHORTCUT_AND_COMMAND("editor/editor_assetlib", TTR("Open Asset Library"), KeyModifierMask::CTRL | Key::F4);
  6206. ED_SHORTCUT_OVERRIDE("editor/editor_2d", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::KEY_1);
  6207. ED_SHORTCUT_OVERRIDE("editor/editor_3d", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::KEY_2);
  6208. ED_SHORTCUT_OVERRIDE("editor/editor_script", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::KEY_3);
  6209. ED_SHORTCUT_OVERRIDE("editor/editor_assetlib", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::KEY_4);
  6210. ED_SHORTCUT_AND_COMMAND("editor/editor_next", TTR("Open the next Editor"));
  6211. ED_SHORTCUT_AND_COMMAND("editor/editor_prev", TTR("Open the previous Editor"));
  6212. screenshot_timer = memnew(Timer);
  6213. screenshot_timer->set_one_shot(true);
  6214. screenshot_timer->set_wait_time(settings_menu->get_submenu_popup_delay() + 0.1f);
  6215. screenshot_timer->connect("timeout", callable_mp(this, &EditorNode::_request_screenshot));
  6216. add_child(screenshot_timer);
  6217. screenshot_timer->set_owner(get_owner());
  6218. // Adjust spacers to center 2D / 3D / Script buttons.
  6219. int max_w = MAX(project_run_bar->get_minimum_size().x + right_menu_hb->get_minimum_size().x, main_menu->get_minimum_size().x);
  6220. left_spacer->set_custom_minimum_size(Size2(MAX(0, max_w - main_menu->get_minimum_size().x), 0));
  6221. right_spacer->set_custom_minimum_size(Size2(MAX(0, max_w - project_run_bar->get_minimum_size().x - right_menu_hb->get_minimum_size().x), 0));
  6222. // Extend menu bar to window title.
  6223. if (can_expand) {
  6224. DisplayServer::get_singleton()->process_events();
  6225. DisplayServer::get_singleton()->window_set_flag(DisplayServer::WINDOW_FLAG_EXTEND_TO_TITLE, true, DisplayServer::MAIN_WINDOW_ID);
  6226. title_bar->set_can_move_window(true);
  6227. }
  6228. String exec = OS::get_singleton()->get_executable_path();
  6229. // Save editor executable path for third-party tools.
  6230. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "executable_path", exec);
  6231. follow_system_theme = EDITOR_GET("interface/theme/follow_system_theme");
  6232. use_system_accent_color = EDITOR_GET("interface/theme/use_system_accent_color");
  6233. system_theme_timer = memnew(Timer);
  6234. system_theme_timer->set_wait_time(1.0);
  6235. system_theme_timer->connect("timeout", callable_mp(this, &EditorNode::_check_system_theme_changed));
  6236. add_child(system_theme_timer);
  6237. system_theme_timer->set_owner(get_owner());
  6238. system_theme_timer->set_autostart(true);
  6239. }
  6240. EditorNode::~EditorNode() {
  6241. EditorInspector::cleanup_plugins();
  6242. EditorTranslationParser::get_singleton()->clean_parsers();
  6243. ResourceImporterScene::clean_up_importer_plugins();
  6244. remove_print_handler(&print_handler);
  6245. EditorHelp::cleanup_doc();
  6246. #if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  6247. EditorHelpHighlighter::free_singleton();
  6248. #endif
  6249. memdelete(editor_selection);
  6250. memdelete(editor_plugins_over);
  6251. memdelete(editor_plugins_force_over);
  6252. memdelete(editor_plugins_force_input_forwarding);
  6253. memdelete(progress_hb);
  6254. memdelete(surface_upgrade_tool);
  6255. memdelete(editor_dock_manager);
  6256. EditorSettings::destroy();
  6257. EditorThemeManager::finalize();
  6258. GDExtensionEditorPlugins::editor_node_add_plugin = nullptr;
  6259. GDExtensionEditorPlugins::editor_node_remove_plugin = nullptr;
  6260. FileDialog::get_icon_func = nullptr;
  6261. FileDialog::register_func = nullptr;
  6262. FileDialog::unregister_func = nullptr;
  6263. EditorFileDialog::get_icon_func = nullptr;
  6264. EditorFileDialog::register_func = nullptr;
  6265. EditorFileDialog::unregister_func = nullptr;
  6266. file_dialogs.clear();
  6267. editor_file_dialogs.clear();
  6268. singleton = nullptr;
  6269. }
  6270. /*
  6271. * EDITOR PLUGIN LIST
  6272. */
  6273. void EditorPluginList::make_visible(bool p_visible) {
  6274. for (int i = 0; i < plugins_list.size(); i++) {
  6275. plugins_list[i]->make_visible(p_visible);
  6276. }
  6277. }
  6278. void EditorPluginList::edit(Object *p_object) {
  6279. for (int i = 0; i < plugins_list.size(); i++) {
  6280. plugins_list[i]->edit(p_object);
  6281. }
  6282. }
  6283. bool EditorPluginList::forward_gui_input(const Ref<InputEvent> &p_event) {
  6284. bool discard = false;
  6285. for (int i = 0; i < plugins_list.size(); i++) {
  6286. if (plugins_list[i]->forward_canvas_gui_input(p_event)) {
  6287. discard = true;
  6288. }
  6289. }
  6290. return discard;
  6291. }
  6292. EditorPlugin::AfterGUIInput EditorPluginList::forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event, bool serve_when_force_input_enabled) {
  6293. EditorPlugin::AfterGUIInput after = EditorPlugin::AFTER_GUI_INPUT_PASS;
  6294. for (int i = 0; i < plugins_list.size(); i++) {
  6295. if ((!serve_when_force_input_enabled) && plugins_list[i]->is_input_event_forwarding_always_enabled()) {
  6296. continue;
  6297. }
  6298. EditorPlugin::AfterGUIInput current_after = plugins_list[i]->forward_3d_gui_input(p_camera, p_event);
  6299. if (current_after == EditorPlugin::AFTER_GUI_INPUT_STOP) {
  6300. after = EditorPlugin::AFTER_GUI_INPUT_STOP;
  6301. }
  6302. if (after != EditorPlugin::AFTER_GUI_INPUT_STOP && current_after == EditorPlugin::AFTER_GUI_INPUT_CUSTOM) {
  6303. after = EditorPlugin::AFTER_GUI_INPUT_CUSTOM;
  6304. }
  6305. }
  6306. return after;
  6307. }
  6308. void EditorPluginList::forward_canvas_draw_over_viewport(Control *p_overlay) {
  6309. for (int i = 0; i < plugins_list.size(); i++) {
  6310. plugins_list[i]->forward_canvas_draw_over_viewport(p_overlay);
  6311. }
  6312. }
  6313. void EditorPluginList::forward_canvas_force_draw_over_viewport(Control *p_overlay) {
  6314. for (int i = 0; i < plugins_list.size(); i++) {
  6315. plugins_list[i]->forward_canvas_force_draw_over_viewport(p_overlay);
  6316. }
  6317. }
  6318. void EditorPluginList::forward_3d_draw_over_viewport(Control *p_overlay) {
  6319. for (int i = 0; i < plugins_list.size(); i++) {
  6320. plugins_list[i]->forward_3d_draw_over_viewport(p_overlay);
  6321. }
  6322. }
  6323. void EditorPluginList::forward_3d_force_draw_over_viewport(Control *p_overlay) {
  6324. for (int i = 0; i < plugins_list.size(); i++) {
  6325. plugins_list[i]->forward_3d_force_draw_over_viewport(p_overlay);
  6326. }
  6327. }
  6328. void EditorPluginList::add_plugin(EditorPlugin *p_plugin) {
  6329. ERR_FAIL_COND(plugins_list.has(p_plugin));
  6330. plugins_list.push_back(p_plugin);
  6331. }
  6332. void EditorPluginList::remove_plugin(EditorPlugin *p_plugin) {
  6333. plugins_list.erase(p_plugin);
  6334. }
  6335. bool EditorPluginList::is_empty() {
  6336. return plugins_list.is_empty();
  6337. }
  6338. void EditorPluginList::clear() {
  6339. plugins_list.clear();
  6340. }
  6341. EditorPluginList::EditorPluginList() {
  6342. }
  6343. EditorPluginList::~EditorPluginList() {
  6344. }