editor_inspector.cpp 206 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983
  1. /**************************************************************************/
  2. /* editor_inspector.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_inspector.h"
  31. #include "editor_inspector.compat.inc"
  32. #include "core/os/keyboard.h"
  33. #include "editor/debugger/editor_debugger_inspector.h"
  34. #include "editor/doc/doc_tools.h"
  35. #include "editor/docks/inspector_dock.h"
  36. #include "editor/editor_main_screen.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/editor_toaster.h"
  41. #include "editor/inspector/add_metadata_dialog.h"
  42. #include "editor/inspector/editor_properties.h"
  43. #include "editor/inspector/editor_property_name_processor.h"
  44. #include "editor/inspector/editor_resource_picker.h"
  45. #include "editor/inspector/multi_node_edit.h"
  46. #include "editor/script/script_editor_plugin.h"
  47. #include "editor/settings/editor_feature_profile.h"
  48. #include "editor/settings/editor_settings.h"
  49. #include "editor/themes/editor_scale.h"
  50. #include "scene/gui/margin_container.h"
  51. #include "scene/gui/separator.h"
  52. #include "scene/gui/spin_box.h"
  53. #include "scene/gui/texture_rect.h"
  54. #include "scene/main/timer.h"
  55. #include "scene/property_utils.h"
  56. #include "scene/resources/packed_scene.h"
  57. #include "scene/resources/style_box_flat.h"
  58. #include "scene/scene_string_names.h"
  59. void EditorInspectorActionButton::_notification(int p_what) {
  60. switch (p_what) {
  61. case NOTIFICATION_THEME_CHANGED: {
  62. set_button_icon(get_editor_theme_icon(icon_name));
  63. } break;
  64. }
  65. }
  66. EditorInspectorActionButton::EditorInspectorActionButton(const String &p_text, const StringName &p_icon_name) {
  67. icon_name = p_icon_name;
  68. set_text(p_text);
  69. set_theme_type_variation(SNAME("InspectorActionButton"));
  70. set_h_size_flags(SIZE_SHRINK_CENTER);
  71. }
  72. bool EditorInspector::_property_path_matches(const String &p_property_path, const String &p_filter, EditorPropertyNameProcessor::Style p_style) {
  73. if (p_property_path.containsn(p_filter)) {
  74. return true;
  75. }
  76. const Vector<String> prop_sections = p_property_path.split("/");
  77. for (int i = 0; i < prop_sections.size(); i++) {
  78. if (p_filter.is_subsequence_ofn(EditorPropertyNameProcessor::get_singleton()->process_name(prop_sections[i], p_style, p_property_path))) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. bool EditorInspector::_resource_properties_matches(const Ref<Resource> &p_resource, const String &p_filter) {
  85. String group;
  86. String group_base;
  87. String subgroup;
  88. String subgroup_base;
  89. List<PropertyInfo> plist;
  90. p_resource->get_property_list(&plist, true);
  91. // Employ a lighter version of the update_tree() property listing to find a match.
  92. for (PropertyInfo &p : plist) {
  93. if (p.usage & PROPERTY_USAGE_SUBGROUP) {
  94. subgroup = p.name;
  95. subgroup_base = p.hint_string.get_slicec(',', 0);
  96. continue;
  97. } else if (p.usage & PROPERTY_USAGE_GROUP) {
  98. group = p.name;
  99. group_base = p.hint_string.get_slicec(',', 0);
  100. subgroup = "";
  101. subgroup_base = "";
  102. continue;
  103. } else if (p.usage & PROPERTY_USAGE_CATEGORY) {
  104. group = "";
  105. group_base = "";
  106. subgroup = "";
  107. subgroup_base = "";
  108. continue;
  109. } else if (p.name.begins_with("metadata/_") || !(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name) ||
  110. (p_filter.is_empty() && restrict_to_basic && !(p.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  111. // Ignore properties that are not supposed to be in the inspector.
  112. continue;
  113. }
  114. if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) {
  115. // Do not show this property in low end gfx.
  116. continue;
  117. }
  118. if (p.name == "script") {
  119. // The script is always hidden in sub inspectors.
  120. continue;
  121. }
  122. if (p.name.begins_with("metadata/") && bool(object->call(SNAME("_hide_metadata_from_inspector")))) {
  123. // Hide metadata from inspector if required.
  124. continue;
  125. }
  126. String path = p.name;
  127. // Check if we exit or not a subgroup. If there is a prefix, remove it from the property label string.
  128. if (!subgroup.is_empty() && !subgroup_base.is_empty()) {
  129. if (path.begins_with(subgroup_base)) {
  130. path = path.trim_prefix(subgroup_base);
  131. } else if (subgroup_base.begins_with(path)) {
  132. // Keep it, this is used pretty often.
  133. } else {
  134. subgroup = ""; // The prefix changed, we are no longer in the subgroup.
  135. }
  136. }
  137. // Check if we exit or not a group. If there is a prefix, remove it from the property label string.
  138. if (!group.is_empty() && !group_base.is_empty() && subgroup.is_empty()) {
  139. if (path.begins_with(group_base)) {
  140. path = path.trim_prefix(group_base);
  141. } else if (group_base.begins_with(path)) {
  142. // Keep it, this is used pretty often.
  143. } else {
  144. group = ""; // The prefix changed, we are no longer in the group.
  145. subgroup = "";
  146. }
  147. }
  148. // Add the group and subgroup to the path.
  149. if (!subgroup.is_empty()) {
  150. path = subgroup + "/" + path;
  151. }
  152. if (!group.is_empty()) {
  153. path = group + "/" + path;
  154. }
  155. // Get the property label's string.
  156. String name_override = (path.contains_char('/')) ? path.substr(path.rfind_char('/') + 1) : path;
  157. const int dot = name_override.find_char('.');
  158. if (dot != -1) {
  159. name_override = name_override.substr(0, dot);
  160. }
  161. // Remove the property from the path.
  162. int idx = path.rfind_char('/');
  163. if (idx > -1) {
  164. path = path.left(idx);
  165. } else {
  166. path = "";
  167. }
  168. // Check if the property matches the filter.
  169. const String property_path = (path.is_empty() ? "" : path + "/") + name_override;
  170. if (_property_path_matches(property_path, p_filter, property_name_style)) {
  171. return true;
  172. }
  173. // Check if the sub-resource has any properties that match the filter.
  174. if (p.hint && p.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  175. Ref<Resource> res = p_resource->get(p.name);
  176. if (res.is_valid() && _resource_properties_matches(res, p_filter)) {
  177. return true;
  178. }
  179. }
  180. }
  181. return false;
  182. }
  183. String EditorProperty::get_tooltip_string(const String &p_string) const {
  184. // Trim to 100 characters to prevent the tooltip from being too long.
  185. constexpr int TOOLTIP_MAX_LENGTH = 100;
  186. return p_string.left(TOOLTIP_MAX_LENGTH).strip_edges() + String((p_string.length() > TOOLTIP_MAX_LENGTH) ? "..." : "");
  187. }
  188. Size2 EditorProperty::get_minimum_size() const {
  189. if (theme_cache.font.is_null()) {
  190. // Too early.
  191. return Vector2();
  192. }
  193. Size2 ms = Size2(0, theme_cache.inspector_property_height);
  194. for (int i = 0; i < get_child_count(); i++) {
  195. Control *c = as_sortable_control(get_child(i));
  196. if (!c) {
  197. continue;
  198. }
  199. if (c == bottom_editor) {
  200. continue;
  201. }
  202. if (c == left_container) {
  203. continue;
  204. }
  205. if (c == right_container) {
  206. continue;
  207. }
  208. Size2 minsize = c->get_combined_minimum_size();
  209. ms = ms.max(minsize);
  210. }
  211. if (keying) {
  212. ms.width += theme_cache.key_icon->get_width() + theme_cache.padding + theme_cache.horizontal_separation;
  213. }
  214. if (deletable) {
  215. ms.width += theme_cache.delete_icon->get_width() + theme_cache.padding + theme_cache.horizontal_separation;
  216. }
  217. if (checkable) {
  218. ms.width += theme_cache.checked_icon->get_width() + theme_cache.padding + theme_cache.horizontal_separation;
  219. }
  220. Size2 ls = left_container->get_combined_minimum_size();
  221. ms.width += ls.x;
  222. ms.height = MAX(ms.height, ls.y);
  223. Size2 rs = right_container->get_combined_minimum_size();
  224. ms.width += rs.x;
  225. ms.height = MAX(ms.height, rs.y);
  226. if (bottom_editor != nullptr && bottom_editor->is_visible()) {
  227. ms.height += label.is_empty() ? 0 : _get_v_separation();
  228. Size2 bems = bottom_editor->get_combined_minimum_size();
  229. ms.height += bems.height;
  230. ms.width = MAX(ms.width, bems.width);
  231. }
  232. return ms;
  233. }
  234. void EditorProperty::emit_changed(const StringName &p_property, const Variant &p_value, const StringName &p_field, bool p_changing) {
  235. Variant args[4] = { p_property, p_value, p_field, p_changing };
  236. const Variant *argptrs[4] = { &args[0], &args[1], &args[2], &args[3] };
  237. cache[p_property] = p_value;
  238. emit_signalp(SNAME("property_changed"), (const Variant **)argptrs, 4);
  239. }
  240. void EditorProperty::_notification(int p_what) {
  241. switch (p_what) {
  242. case NOTIFICATION_THEME_CHANGED: {
  243. EditorInspector::initialize_property_theme(theme_cache, this);
  244. } break;
  245. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  246. RID ae = get_accessibility_element();
  247. ERR_FAIL_COND(ae.is_null());
  248. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  249. DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Property: %s"), label));
  250. DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Property: %s"), label));
  251. DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
  252. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &EditorProperty::_accessibility_action_menu));
  253. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &EditorProperty::_accessibility_action_click));
  254. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_READONLY, read_only);
  255. if (checkable) {
  256. DisplayServer::get_singleton()->accessibility_update_set_checked(ae, checked);
  257. }
  258. } break;
  259. case NOTIFICATION_SORT_CHILDREN: {
  260. Size2 size = get_size();
  261. Rect2 rect;
  262. Rect2 bottom_rect;
  263. right_child_rect = Rect2();
  264. bottom_child_rect = Rect2();
  265. {
  266. int child_room = size.width * (1.0 - split_ratio);
  267. int separation = 4 * EDSCALE;
  268. int height = theme_cache.inspector_property_height;
  269. int half_padding = theme_cache.padding / 2;
  270. bool no_children = true;
  271. //compute room needed
  272. for (int i = 0; i < get_child_count(); i++) {
  273. Control *c = as_sortable_control(get_child(i));
  274. if (!c) {
  275. continue;
  276. }
  277. if (c == bottom_editor) {
  278. continue;
  279. }
  280. Size2 minsize = c->get_combined_minimum_size();
  281. if (c != left_container && c != right_container) {
  282. child_room = MAX(child_room, minsize.width);
  283. no_children = false;
  284. }
  285. height = MAX(height, minsize.height);
  286. }
  287. if (no_children) {
  288. text_size = size.width;
  289. rect = Rect2(size.width - 1, 0, 1, height);
  290. } else if (!draw_label) {
  291. text_size = 0;
  292. rect = Rect2(1, 0, size.width - 1, height);
  293. } else {
  294. text_size = MAX(0, size.width - (child_room + separation));
  295. if (is_layout_rtl()) {
  296. rect = Rect2(1, 0, child_room, height);
  297. } else {
  298. rect = Rect2(size.width - child_room, 0, child_room, height);
  299. }
  300. }
  301. if (rect.size.x > 1) {
  302. rect.size.x -= right_container->get_combined_minimum_size().x;
  303. if (is_layout_rtl()) {
  304. rect.position.x += right_container->get_combined_minimum_size().x;
  305. }
  306. }
  307. if (bottom_editor) {
  308. int v_offset = label.is_empty() ? 0 : _get_v_separation();
  309. bottom_rect = Rect2(0, rect.size.height + v_offset, size.width, bottom_editor->get_combined_minimum_size().height);
  310. }
  311. if (keying) {
  312. Ref<Texture2D> key;
  313. if (use_keying_next()) {
  314. key = theme_cache.key_next_icon;
  315. } else {
  316. key = theme_cache.key_icon;
  317. }
  318. rect.size.x -= key->get_width() + half_padding + theme_cache.horizontal_separation;
  319. if (is_layout_rtl()) {
  320. rect.position.x += key->get_width() + half_padding + theme_cache.horizontal_separation;
  321. }
  322. if (no_children) {
  323. // Use full padding to avoid overlapping with the revert button.
  324. text_size -= key->get_width() + half_padding * 2 + separation;
  325. }
  326. }
  327. if (deletable) {
  328. rect.size.x -= theme_cache.delete_icon->get_width() + half_padding + theme_cache.horizontal_separation;
  329. if (is_layout_rtl()) {
  330. rect.position.x += theme_cache.delete_icon->get_width() + half_padding + theme_cache.horizontal_separation;
  331. }
  332. if (no_children) {
  333. text_size -= theme_cache.delete_icon->get_width() + half_padding + separation;
  334. }
  335. }
  336. // Account for the space needed on the outer side
  337. // when any of the icons are visible.
  338. if (keying || deletable) {
  339. separation = theme_cache.horizontal_separation;
  340. rect.size.x -= separation;
  341. if (is_layout_rtl()) {
  342. rect.position.x += separation;
  343. }
  344. }
  345. }
  346. //set children
  347. for (int i = 0; i < get_child_count(); i++) {
  348. Control *c = as_sortable_control(get_child(i));
  349. if (!c) {
  350. continue;
  351. }
  352. if (c == bottom_editor) {
  353. continue;
  354. }
  355. if (c == left_container) {
  356. continue;
  357. }
  358. if (c == right_container) {
  359. continue;
  360. }
  361. fit_child_in_rect(c, rect);
  362. right_child_rect = rect;
  363. }
  364. if (bottom_editor) {
  365. fit_child_in_rect(bottom_editor, bottom_rect);
  366. bottom_child_rect = bottom_rect;
  367. }
  368. Size2 rs = right_container->get_combined_minimum_size();
  369. rs.y = MAX(rs.y, rect.size.y);
  370. if (is_layout_rtl()) {
  371. fit_child_in_rect(right_container, Rect2(0, 0, rs.width, rs.y));
  372. } else {
  373. fit_child_in_rect(right_container, Rect2(size.width - rs.width, 0, rs.width, rs.y));
  374. }
  375. Size2 ls = left_container->get_combined_minimum_size();
  376. real_t right_size = rect.size.x + rs.x;
  377. ls.y = MAX(ls.y, rect.size.y);
  378. if (is_layout_rtl()) {
  379. fit_child_in_rect(left_container, Rect2(right_size, 0, size.x - right_size, ls.y));
  380. } else {
  381. fit_child_in_rect(left_container, Rect2(0, 0, size.x - right_size, ls.y));
  382. }
  383. queue_redraw(); //need to redraw text
  384. } break;
  385. case NOTIFICATION_DRAW: {
  386. Ref<Font> font = theme_cache.font;
  387. int font_size = theme_cache.font_size;
  388. bool rtl = is_layout_rtl();
  389. Size2 size = get_size();
  390. if (bottom_editor) {
  391. size.height = bottom_editor->get_offset(SIDE_TOP) - _get_v_separation();
  392. } else if (label_reference) {
  393. size.height = label_reference->get_size().height;
  394. }
  395. size.height = MAX(size.height, left_container->get_size().height);
  396. // Only draw the label if it's not empty.
  397. if (label.is_empty()) {
  398. size.height = 0;
  399. } else if (sub_inspector_color_level >= 0 && theme_cache.sub_inspector_background[sub_inspector_color_level].is_valid()) {
  400. draw_style_box(theme_cache.sub_inspector_background[sub_inspector_color_level], Rect2(Vector2(), size));
  401. } else {
  402. draw_style_box(selected ? theme_cache.background_selected : theme_cache.background, Rect2(Vector2(), size));
  403. }
  404. if (draw_top_bg && right_child_rect != Rect2() && draw_background) {
  405. draw_style_box(theme_cache.child_background, right_child_rect);
  406. }
  407. if (bottom_child_rect != Rect2() && draw_background) {
  408. draw_style_box(theme_cache.child_background, bottom_child_rect);
  409. }
  410. Color color;
  411. if (draw_warning || draw_prop_warning) {
  412. color = is_read_only() ? theme_cache.readonly_warning_color : theme_cache.warning_color;
  413. } else if (is_read_only()) {
  414. color = (sub_inspector_color_level >= 0) ? theme_cache.sub_inspector_property_color : theme_cache.readonly_property_color;
  415. } else {
  416. color = (sub_inspector_color_level >= 0) ? theme_cache.sub_inspector_property_color : theme_cache.property_color;
  417. }
  418. if (label.contains_char('.')) {
  419. // FIXME: Move this to the project settings editor, as this is only used
  420. // for project settings feature tag overrides.
  421. color.a = 0.5;
  422. }
  423. int ofs = theme_cache.font_offset;
  424. int text_limit = text_size - ofs;
  425. int half_padding = EDITOR_GET("interface/theme/base_spacing");
  426. int padding = half_padding * 2;
  427. int left_ofs = left_container->get_combined_minimum_size().x;
  428. ofs += left_ofs;
  429. text_limit -= left_ofs;
  430. if (checkable) {
  431. Ref<Texture2D> checkbox;
  432. if (checked) {
  433. checkbox = theme_cache.checked_icon;
  434. } else {
  435. checkbox = theme_cache.unchecked_icon;
  436. }
  437. check_rect = Rect2(ofs, 0, checkbox->get_width() + padding, size.height);
  438. Point2 rtl_pos;
  439. if (rtl) {
  440. rtl_pos = Point2(size.width - check_rect.position.x - (checkbox->get_width() + padding + (1 * EDSCALE)), check_rect.position.y);
  441. }
  442. Color color2(1, 1, 1);
  443. if (check_hover) {
  444. color2.r *= 1.2;
  445. color2.g *= 1.2;
  446. color2.b *= 1.2;
  447. if (rtl) {
  448. draw_style_box(theme_cache.hover, Rect2(rtl_pos, check_rect.size));
  449. } else {
  450. draw_style_box(theme_cache.hover, check_rect);
  451. }
  452. }
  453. Point2 icon_ofs = (Point2(padding, size.height - checkbox->get_height()) / 2).round();
  454. if (rtl) {
  455. draw_texture(checkbox, rtl_pos + icon_ofs, color2);
  456. } else {
  457. draw_texture(checkbox, check_rect.position + icon_ofs, color2);
  458. }
  459. int check_ofs = checkbox->get_width() + padding + theme_cache.horizontal_separation;
  460. ofs += check_ofs;
  461. text_limit -= check_ofs;
  462. } else {
  463. check_rect = Rect2();
  464. }
  465. if (can_revert && !is_read_only()) {
  466. const Ref<Texture2D> &reload_icon = theme_cache.revert_icon;
  467. text_limit -= reload_icon->get_width() + half_padding + theme_cache.horizontal_separation;
  468. revert_rect = Rect2(ofs + text_limit, 0, reload_icon->get_width() + padding + (1 * EDSCALE), size.height);
  469. Point2 rtl_pos;
  470. if (rtl) {
  471. rtl_pos = Point2(size.width - revert_rect.position.x - (reload_icon->get_width() + padding + (1 * EDSCALE)), revert_rect.position.y);
  472. }
  473. Color color2(1, 1, 1);
  474. if (revert_hover) {
  475. color2.r *= 1.2;
  476. color2.g *= 1.2;
  477. color2.b *= 1.2;
  478. if (rtl) {
  479. draw_style_box(theme_cache.hover, Rect2(rtl_pos, revert_rect.size));
  480. } else {
  481. draw_style_box(theme_cache.hover, revert_rect);
  482. }
  483. }
  484. Point2 icon_ofs = (Point2(padding, size.height - reload_icon->get_height()) / 2).round();
  485. if (rtl) {
  486. draw_texture(reload_icon, rtl_pos + icon_ofs, color2);
  487. } else {
  488. draw_texture(reload_icon, revert_rect.position + icon_ofs, color2);
  489. }
  490. text_limit -= half_padding;
  491. } else {
  492. revert_rect = Rect2();
  493. }
  494. if (!pin_hidden && pinned) {
  495. const Ref<Texture2D> &pinned_icon = theme_cache.pin_icon;
  496. int margin_w = theme_cache.horizontal_separation;
  497. int total_icon_w = margin_w + pinned_icon->get_width();
  498. int text_w = font->get_string_size(label, rtl ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT, text_limit - total_icon_w, font_size).x;
  499. int y = (size.height - pinned_icon->get_height()) / 2;
  500. if (rtl) {
  501. draw_texture(pinned_icon, Vector2(size.width - ofs - text_w - total_icon_w, y), color);
  502. } else {
  503. draw_texture(pinned_icon, Vector2(ofs + text_w + margin_w, y), color);
  504. }
  505. text_limit -= total_icon_w;
  506. }
  507. int v_ofs = (size.height - font->get_height(font_size)) / 2;
  508. if (rtl) {
  509. draw_string(font, Point2(size.width - ofs - text_limit, v_ofs + font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_RIGHT, text_limit, font_size, color);
  510. } else {
  511. draw_string(font, Point2(ofs, v_ofs + font->get_ascent(font_size)), label, HORIZONTAL_ALIGNMENT_LEFT, text_limit, font_size, color);
  512. }
  513. ofs = size.width;
  514. ofs -= right_container->get_minimum_size().x;
  515. if (keying) {
  516. Ref<Texture2D> key;
  517. if (use_keying_next()) {
  518. key = theme_cache.key_next_icon;
  519. } else {
  520. key = theme_cache.key_icon;
  521. }
  522. ofs -= key->get_width() + half_padding + theme_cache.horizontal_separation;
  523. keying_rect = Rect2(ofs, 0, key->get_width() + padding, size.height);
  524. Point2 rtl_pos;
  525. if (rtl) {
  526. rtl_pos = Point2(size.width - keying_rect.position.x - (key->get_width() + padding), keying_rect.position.y);
  527. }
  528. Color color2(1, 1, 1);
  529. if (keying_hover) {
  530. color2.r *= 1.2;
  531. color2.g *= 1.2;
  532. color2.b *= 1.2;
  533. if (rtl) {
  534. draw_style_box(theme_cache.hover, Rect2(rtl_pos, keying_rect.size));
  535. } else {
  536. draw_style_box(theme_cache.hover, keying_rect);
  537. }
  538. }
  539. Point2 icon_ofs = (Point2(padding, size.height - key->get_height()) / 2).round();
  540. if (rtl) {
  541. draw_texture(key, rtl_pos + icon_ofs, color2);
  542. } else {
  543. draw_texture(key, keying_rect.position + icon_ofs, color2);
  544. }
  545. } else {
  546. keying_rect = Rect2();
  547. }
  548. if (deletable) {
  549. const Ref<Texture2D> &close = theme_cache.delete_icon;
  550. ofs -= close->get_width() + half_padding + theme_cache.horizontal_separation;
  551. delete_rect = Rect2(ofs, 0, close->get_width() + padding, size.height);
  552. Point2 rtl_pos;
  553. if (rtl) {
  554. rtl_pos = Point2(size.width - delete_rect.position.x - (close->get_width() + padding), delete_rect.position.y);
  555. }
  556. Color color2(1, 1, 1);
  557. if (delete_hover) {
  558. color2.r *= 1.2;
  559. color2.g *= 1.2;
  560. color2.b *= 1.2;
  561. if (rtl) {
  562. draw_style_box(theme_cache.hover, Rect2(rtl_pos, delete_rect.size));
  563. } else {
  564. draw_style_box(theme_cache.hover, delete_rect);
  565. }
  566. }
  567. Point2 icon_ofs = (Point2(padding, size.height - close->get_height()) / 2).round();
  568. if (rtl) {
  569. draw_texture(close, rtl_pos + icon_ofs, color2);
  570. } else {
  571. draw_texture(close, delete_rect.position + icon_ofs, color2);
  572. }
  573. } else {
  574. delete_rect = Rect2();
  575. }
  576. } break;
  577. case NOTIFICATION_ENTER_TREE: {
  578. EditorInspector *inspector = get_parent_inspector();
  579. if (inspector) {
  580. inspector = inspector->get_root_inspector();
  581. }
  582. set_shortcut_context(inspector);
  583. if (has_borders) {
  584. get_parent()->connect(SceneStringName(theme_changed), callable_mp(this, &EditorProperty::_update_property_bg));
  585. _update_property_bg();
  586. }
  587. } break;
  588. case NOTIFICATION_EXIT_TREE: {
  589. if (has_borders) {
  590. get_parent()->disconnect(SceneStringName(theme_changed), callable_mp(this, &EditorProperty::_update_property_bg));
  591. }
  592. } break;
  593. case NOTIFICATION_MOUSE_EXIT_SELF:
  594. case NOTIFICATION_MOUSE_EXIT: {
  595. if (keying_hover || revert_hover || check_hover || delete_hover) {
  596. keying_hover = false;
  597. revert_hover = false;
  598. check_hover = false;
  599. delete_hover = false;
  600. queue_redraw();
  601. }
  602. } break;
  603. }
  604. }
  605. void EditorProperty::set_label(const String &p_label) {
  606. label = p_label;
  607. queue_redraw();
  608. }
  609. String EditorProperty::get_label() const {
  610. return label;
  611. }
  612. Object *EditorProperty::get_edited_object() {
  613. return object;
  614. }
  615. StringName EditorProperty::get_edited_property() const {
  616. return property;
  617. }
  618. Variant EditorProperty::get_edited_property_display_value() const {
  619. ERR_FAIL_NULL_V(object, Variant());
  620. Control *control = Object::cast_to<Control>(object);
  621. if (checkable && !checked && control && String(property).begins_with("theme_override_")) {
  622. return control->get_used_theme_item(property);
  623. } else {
  624. return get_edited_property_value();
  625. }
  626. }
  627. EditorInspector *EditorProperty::get_parent_inspector() const {
  628. Node *parent = get_parent();
  629. while (parent) {
  630. EditorInspector *ei = Object::cast_to<EditorInspector>(parent);
  631. if (ei) {
  632. return ei;
  633. }
  634. parent = parent->get_parent();
  635. }
  636. return nullptr;
  637. }
  638. void EditorProperty::set_doc_path(const String &p_doc_path) {
  639. doc_path = p_doc_path;
  640. }
  641. void EditorProperty::set_internal(bool p_internal) {
  642. internal = p_internal;
  643. }
  644. void EditorProperty::update_property() {
  645. GDVIRTUAL_CALL(_update_property);
  646. }
  647. void EditorProperty::_set_read_only(bool p_read_only) {
  648. }
  649. void EditorProperty::set_read_only(bool p_read_only) {
  650. read_only = p_read_only;
  651. if (GDVIRTUAL_CALL(_set_read_only, p_read_only)) {
  652. return;
  653. }
  654. _set_read_only(p_read_only);
  655. }
  656. bool EditorProperty::is_read_only() const {
  657. return read_only;
  658. }
  659. Variant EditorPropertyRevert::get_property_revert_value(Object *p_object, const StringName &p_property, bool *r_is_valid) {
  660. if (p_object->property_can_revert(p_property)) {
  661. if (r_is_valid) {
  662. *r_is_valid = true;
  663. }
  664. return p_object->property_get_revert(p_property);
  665. }
  666. return PropertyUtils::get_property_default_value(p_object, p_property, r_is_valid);
  667. }
  668. bool EditorPropertyRevert::can_property_revert(Object *p_object, const StringName &p_property, const Variant *p_custom_current_value) {
  669. bool is_valid_revert = false;
  670. Variant revert_value = EditorPropertyRevert::get_property_revert_value(p_object, p_property, &is_valid_revert);
  671. if (!is_valid_revert) {
  672. return false;
  673. }
  674. Variant current_value = p_custom_current_value ? *p_custom_current_value : p_object->get(p_property);
  675. return PropertyUtils::is_property_value_different(p_object, current_value, revert_value);
  676. }
  677. StringName EditorProperty::_get_revert_property() const {
  678. return property;
  679. }
  680. void EditorProperty::_update_property_bg() {
  681. // This function is to be called on EditorPropertyResource, EditorPropertyArray, and EditorPropertyDictionary.
  682. // Behavior is undetermined on any other EditorProperty.
  683. if (!is_inside_tree()) {
  684. return;
  685. }
  686. if (bottom_editor) {
  687. ColorationMode nested_color_mode = (ColorationMode)(int)EDITOR_GET("interface/inspector/nested_color_mode");
  688. bool delimitate_all_container_and_resources = EDITOR_GET("interface/inspector/delimitate_all_container_and_resources");
  689. sub_inspector_color_level = 0;
  690. if (is_colored(nested_color_mode)) {
  691. Node *n = this;
  692. while (n) {
  693. EditorProperty *ep = Object::cast_to<EditorProperty>(n);
  694. if (ep && ep->is_colored(nested_color_mode)) {
  695. sub_inspector_color_level++;
  696. if (sub_inspector_color_level == 16) {
  697. break;
  698. }
  699. }
  700. n = n->get_parent();
  701. }
  702. }
  703. add_theme_style_override(SNAME("DictionaryAddItem"), get_theme_stylebox("DictionaryAddItem" + itos(sub_inspector_color_level), EditorStringName(EditorStyles)));
  704. if (delimitate_all_container_and_resources || is_colored(nested_color_mode)) {
  705. bottom_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("sub_inspector_bg" + itos(sub_inspector_color_level), EditorStringName(EditorStyles)));
  706. } else {
  707. bottom_editor->add_theme_style_override(SceneStringName(panel), get_theme_stylebox("sub_inspector_bg_no_border", EditorStringName(EditorStyles)));
  708. sub_inspector_color_level = -1;
  709. }
  710. } else {
  711. sub_inspector_color_level = -1;
  712. }
  713. queue_redraw();
  714. }
  715. void EditorProperty::update_editor_property_status() {
  716. if (property == StringName()) {
  717. return; //no property, so nothing to do
  718. }
  719. bool new_pinned = false;
  720. if (can_pin) {
  721. Node *node = Object::cast_to<Node>(object);
  722. CRASH_COND(!node);
  723. new_pinned = node->is_property_pinned(property);
  724. }
  725. bool new_warning = false;
  726. if (object->has_method("_get_property_warning")) {
  727. new_warning = !String(object->call("_get_property_warning", property)).is_empty();
  728. }
  729. Variant current = object->get(_get_revert_property());
  730. bool new_can_revert = EditorPropertyRevert::can_property_revert(object, property, &current) && !is_read_only();
  731. bool new_checked = checked;
  732. if (checkable) { // for properties like theme overrides.
  733. bool valid = false;
  734. Variant value = object->get(property, &valid);
  735. if (valid) {
  736. new_checked = value.get_type() != Variant::NIL;
  737. }
  738. }
  739. if (new_can_revert != can_revert || new_pinned != pinned || new_checked != checked || new_warning != draw_prop_warning) {
  740. if (new_can_revert != can_revert) {
  741. emit_signal(SNAME("property_can_revert_changed"), property, new_can_revert);
  742. }
  743. draw_prop_warning = new_warning;
  744. can_revert = new_can_revert;
  745. pinned = new_pinned;
  746. checked = new_checked;
  747. queue_redraw();
  748. }
  749. }
  750. bool EditorProperty::use_keying_next() const {
  751. List<PropertyInfo> plist;
  752. object->get_property_list(&plist, true);
  753. for (const PropertyInfo &p : plist) {
  754. if (p.name == property) {
  755. return (p.usage & PROPERTY_USAGE_KEYING_INCREMENTS);
  756. }
  757. }
  758. return false;
  759. }
  760. void EditorProperty::set_draw_label(bool p_draw_label) {
  761. draw_label = p_draw_label;
  762. queue_redraw();
  763. queue_sort();
  764. }
  765. bool EditorProperty::is_draw_label() const {
  766. return draw_label;
  767. }
  768. void EditorProperty::set_draw_background(bool p_draw_background) {
  769. draw_background = p_draw_background;
  770. queue_redraw();
  771. }
  772. bool EditorProperty::is_draw_background() const {
  773. return draw_background;
  774. }
  775. void EditorProperty::set_checkable(bool p_checkable) {
  776. checkable = p_checkable;
  777. queue_redraw();
  778. queue_sort();
  779. }
  780. bool EditorProperty::is_checkable() const {
  781. return checkable;
  782. }
  783. void EditorProperty::set_checked(bool p_checked) {
  784. checked = p_checked;
  785. queue_redraw();
  786. }
  787. bool EditorProperty::is_checked() const {
  788. return checked;
  789. }
  790. void EditorProperty::set_draw_warning(bool p_draw_warning) {
  791. draw_warning = p_draw_warning;
  792. queue_redraw();
  793. }
  794. void EditorProperty::set_keying(bool p_keying) {
  795. keying = p_keying;
  796. queue_redraw();
  797. queue_sort();
  798. }
  799. void EditorProperty::set_deletable(bool p_deletable) {
  800. deletable = p_deletable;
  801. queue_redraw();
  802. queue_sort();
  803. }
  804. bool EditorProperty::is_deletable() const {
  805. return deletable;
  806. }
  807. bool EditorProperty::is_keying() const {
  808. return keying;
  809. }
  810. bool EditorProperty::is_draw_warning() const {
  811. return draw_warning;
  812. }
  813. void EditorProperty::_focusable_focused(int p_index) {
  814. if (!selectable) {
  815. return;
  816. }
  817. bool already_selected = selected;
  818. selected = true;
  819. selected_focusable = p_index;
  820. queue_redraw();
  821. if (!already_selected && selected) {
  822. emit_signal(SNAME("selected"), property, selected_focusable);
  823. }
  824. }
  825. void EditorProperty::add_focusable(Control *p_control) {
  826. p_control->connect(SceneStringName(focus_entered), callable_mp(this, &EditorProperty::_focusable_focused).bind(focusables.size()));
  827. focusables.push_back(p_control);
  828. }
  829. void EditorProperty::grab_focus(int p_focusable) {
  830. if (focusables.is_empty()) {
  831. return;
  832. }
  833. if (p_focusable >= 0) {
  834. ERR_FAIL_INDEX(p_focusable, focusables.size());
  835. focusables[p_focusable]->grab_focus(true);
  836. } else {
  837. focusables[0]->grab_focus(true);
  838. }
  839. }
  840. void EditorProperty::select(int p_focusable) {
  841. bool already_selected = selected;
  842. if (!selectable) {
  843. return;
  844. }
  845. if (p_focusable >= 0) {
  846. ERR_FAIL_INDEX(p_focusable, focusables.size());
  847. focusables[p_focusable]->grab_focus(true);
  848. } else {
  849. selected = true;
  850. queue_redraw();
  851. }
  852. if (!already_selected && selected) {
  853. emit_signal(SNAME("selected"), property, selected_focusable);
  854. }
  855. }
  856. void EditorProperty::deselect() {
  857. selected = false;
  858. selected_focusable = -1;
  859. queue_redraw();
  860. }
  861. bool EditorProperty::is_selected() const {
  862. return selected;
  863. }
  864. void EditorProperty::add_inline_control(Control *p_control, InlineControlSide p_side) {
  865. Node *parent = p_control->get_parent();
  866. if (parent != nullptr) {
  867. parent->remove_child(p_control);
  868. }
  869. if (p_side == INLINE_CONTROL_LEFT) {
  870. left_container->add_child(p_control);
  871. } else {
  872. right_container->add_child(p_control);
  873. }
  874. }
  875. void EditorProperty::set_label_overlayed(bool p_overlay) {
  876. label_overlayed = p_overlay;
  877. }
  878. void EditorProperty::gui_input(const Ref<InputEvent> &p_event) {
  879. ERR_FAIL_COND(p_event.is_null());
  880. if (property == StringName()) {
  881. return;
  882. }
  883. Ref<InputEventMouse> me = p_event;
  884. if (me.is_valid()) {
  885. Vector2 mpos = me->get_position();
  886. if (bottom_child_rect.has_point(mpos)) {
  887. return; // Makes child EditorProperties behave like sibling nodes when handling mouse events.
  888. }
  889. if (is_layout_rtl()) {
  890. mpos.x = get_size().x - mpos.x;
  891. }
  892. bool button_left = me->get_button_mask().has_flag(MouseButtonMask::LEFT);
  893. bool new_keying_hover = keying_rect.has_point(mpos) && !button_left;
  894. if (new_keying_hover != keying_hover) {
  895. keying_hover = new_keying_hover;
  896. queue_redraw();
  897. }
  898. bool new_delete_hover = delete_rect.has_point(mpos) && !button_left;
  899. if (new_delete_hover != delete_hover) {
  900. delete_hover = new_delete_hover;
  901. queue_redraw();
  902. }
  903. bool new_revert_hover = revert_rect.has_point(mpos) && !button_left;
  904. if (new_revert_hover != revert_hover) {
  905. revert_hover = new_revert_hover;
  906. queue_redraw();
  907. }
  908. bool new_check_hover = check_rect.has_point(mpos) && !button_left;
  909. if (new_check_hover != check_hover) {
  910. check_hover = new_check_hover;
  911. queue_redraw();
  912. }
  913. }
  914. Ref<InputEventMouseButton> mb = p_event;
  915. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  916. Vector2 mpos = mb->get_position();
  917. if (is_layout_rtl()) {
  918. mpos.x = get_size().x - mpos.x;
  919. }
  920. select();
  921. if (keying_rect.has_point(mpos)) {
  922. accept_event();
  923. emit_signal(SNAME("property_keyed"), property, use_keying_next());
  924. if (use_keying_next()) {
  925. if (property == "frame_coords" && (object->is_class("Sprite2D") || object->is_class("Sprite3D"))) {
  926. Vector2i new_coords = object->get(property);
  927. new_coords.x++;
  928. if (new_coords.x >= int64_t(object->get("hframes"))) {
  929. new_coords.x = 0;
  930. new_coords.y++;
  931. }
  932. if (new_coords.x < int64_t(object->get("hframes")) && new_coords.y < int64_t(object->get("vframes"))) {
  933. callable_mp(this, &EditorProperty::emit_changed).call_deferred(property, new_coords, "", false);
  934. }
  935. } else {
  936. if (int64_t(object->get(property)) + 1 < (int64_t(object->get("hframes")) * int64_t(object->get("vframes")))) {
  937. callable_mp(this, &EditorProperty::emit_changed).call_deferred(property, object->get(property).operator int64_t() + 1, "", false);
  938. }
  939. }
  940. callable_mp(this, &EditorProperty::update_property).call_deferred();
  941. }
  942. }
  943. if (delete_rect.has_point(mpos)) {
  944. accept_event();
  945. emit_signal(SNAME("property_deleted"), property);
  946. }
  947. if (revert_rect.has_point(mpos)) {
  948. accept_event();
  949. get_viewport()->gui_release_focus();
  950. bool is_valid_revert = false;
  951. Variant revert_value = EditorPropertyRevert::get_property_revert_value(object, property, &is_valid_revert);
  952. ERR_FAIL_COND(!is_valid_revert);
  953. emit_changed(_get_revert_property(), revert_value);
  954. update_property();
  955. }
  956. if (check_rect.has_point(mpos)) {
  957. accept_event();
  958. if (!checked && Object::cast_to<Control>(object) && property_path.begins_with("theme_override_")) {
  959. List<PropertyInfo> pinfo;
  960. object->get_property_list(&pinfo);
  961. for (const PropertyInfo &E : pinfo) {
  962. if (E.type == Variant::OBJECT && E.name == property_path) {
  963. EditorToaster::get_singleton()->popup_str(TTR("Toggling the checkbox is disabled for Resource properties. Modify the property using the resource picker instead."), EditorToaster::SEVERITY_WARNING);
  964. return; // Disallow clicking to toggle the checkbox of type Resource to checked.
  965. }
  966. }
  967. }
  968. checked = !checked;
  969. queue_redraw();
  970. emit_signal(SNAME("property_checked"), property, checked);
  971. }
  972. } else if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  973. accept_event();
  974. _update_popup();
  975. menu->set_position(get_screen_position() + get_local_mouse_position());
  976. menu->reset_size();
  977. menu->popup();
  978. select();
  979. return;
  980. }
  981. }
  982. void EditorProperty::_accessibility_action_click(const Variant &p_data) {
  983. select();
  984. if (checkable) {
  985. if (!checked && Object::cast_to<Control>(object) && property_path.begins_with("theme_override_")) {
  986. List<PropertyInfo> pinfo;
  987. object->get_property_list(&pinfo);
  988. for (const PropertyInfo &E : pinfo) {
  989. if (E.type == Variant::OBJECT && E.name == property_path) {
  990. EditorToaster::get_singleton()->popup_str(TTR("Toggling the checkbox is disabled for Resource properties. Modify the property using the resource picker instead."), EditorToaster::SEVERITY_WARNING);
  991. return;
  992. }
  993. }
  994. }
  995. checked = !checked;
  996. queue_redraw();
  997. emit_signal(SNAME("property_checked"), property, checked);
  998. }
  999. }
  1000. void EditorProperty::_accessibility_action_menu(const Variant &p_data) {
  1001. _update_popup();
  1002. menu->set_position(get_screen_position());
  1003. menu->reset_size();
  1004. menu->popup();
  1005. }
  1006. void EditorProperty::shortcut_input(const Ref<InputEvent> &p_event) {
  1007. if (!selected || !p_event->is_pressed()) {
  1008. return;
  1009. }
  1010. const Ref<InputEventKey> k = p_event;
  1011. if (k.is_valid() && k->is_pressed()) {
  1012. if (ED_IS_SHORTCUT("property_editor/copy_value", p_event)) {
  1013. menu_option(MENU_COPY_VALUE);
  1014. accept_event();
  1015. } else if (!is_read_only() && ED_IS_SHORTCUT("property_editor/paste_value", p_event)) {
  1016. menu_option(MENU_PASTE_VALUE);
  1017. accept_event();
  1018. } else if (!internal && ED_IS_SHORTCUT("property_editor/copy_property_path", p_event)) {
  1019. menu_option(MENU_COPY_PROPERTY_PATH);
  1020. accept_event();
  1021. }
  1022. }
  1023. }
  1024. const Color *EditorProperty::_get_property_colors() {
  1025. static Color c[4];
  1026. c[0] = theme_cache.property_color_x;
  1027. c[1] = theme_cache.property_color_y;
  1028. c[2] = theme_cache.property_color_z;
  1029. c[3] = theme_cache.property_color_w;
  1030. return c;
  1031. }
  1032. void EditorProperty::set_label_reference(Control *p_control) {
  1033. label_reference = p_control;
  1034. }
  1035. HBoxContainer *EditorProperty::get_inline_container(InlineControlSide p_side) {
  1036. if (p_side == INLINE_CONTROL_LEFT) {
  1037. return left_container;
  1038. } else {
  1039. return right_container;
  1040. }
  1041. }
  1042. void EditorProperty::set_bottom_editor(Control *p_control) {
  1043. bottom_editor = p_control;
  1044. if (has_borders) {
  1045. _update_property_bg();
  1046. }
  1047. }
  1048. Variant EditorProperty::_get_cache_value(const StringName &p_prop, bool &r_valid) const {
  1049. return object->get(p_prop, &r_valid);
  1050. }
  1051. bool EditorProperty::is_cache_valid() const {
  1052. if (object) {
  1053. for (const KeyValue<StringName, Variant> &E : cache) {
  1054. bool valid;
  1055. Variant value = _get_cache_value(E.key, valid);
  1056. if (!valid || value != E.value) {
  1057. return false;
  1058. }
  1059. }
  1060. }
  1061. return true;
  1062. }
  1063. void EditorProperty::update_cache() {
  1064. cache.clear();
  1065. if (object && property != StringName()) {
  1066. bool valid;
  1067. Variant value = _get_cache_value(property, valid);
  1068. if (valid) {
  1069. cache[property] = value;
  1070. }
  1071. }
  1072. }
  1073. Variant EditorProperty::get_drag_data(const Point2 &p_point) {
  1074. if (property == StringName()) {
  1075. return Variant();
  1076. }
  1077. Dictionary dp;
  1078. dp["type"] = "obj_property";
  1079. dp["object"] = object;
  1080. dp["property"] = property;
  1081. dp["value"] = object->get(property);
  1082. Label *drag_label = memnew(Label);
  1083. drag_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  1084. drag_label->set_text(property);
  1085. drag_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate raw property name.
  1086. set_drag_preview(drag_label);
  1087. return dp;
  1088. }
  1089. void EditorProperty::set_use_folding(bool p_use_folding) {
  1090. use_folding = p_use_folding;
  1091. }
  1092. bool EditorProperty::is_using_folding() const {
  1093. return use_folding;
  1094. }
  1095. void EditorProperty::expand_all_folding() {
  1096. }
  1097. void EditorProperty::collapse_all_folding() {
  1098. }
  1099. void EditorProperty::expand_revertable() {
  1100. }
  1101. void EditorProperty::set_selectable(bool p_selectable) {
  1102. selectable = p_selectable;
  1103. }
  1104. bool EditorProperty::is_selectable() const {
  1105. return selectable;
  1106. }
  1107. void EditorProperty::set_name_split_ratio(float p_ratio) {
  1108. split_ratio = p_ratio;
  1109. }
  1110. float EditorProperty::get_name_split_ratio() const {
  1111. return split_ratio;
  1112. }
  1113. void EditorProperty::set_favoritable(bool p_favoritable) {
  1114. can_favorite = p_favoritable;
  1115. }
  1116. bool EditorProperty::is_favoritable() const {
  1117. return can_favorite;
  1118. }
  1119. void EditorProperty::set_object_and_property(Object *p_object, const StringName &p_property) {
  1120. object = p_object;
  1121. property = p_property;
  1122. _update_flags();
  1123. }
  1124. static bool _is_value_potential_override(Node *p_node, const String &p_property) {
  1125. // Consider a value is potentially overriding another if either of the following is true:
  1126. // a) The node is foreign (inheriting or an instance), so the original value may come from another scene.
  1127. // b) The node belongs to the scene, but the original value comes from somewhere but the builtin class (i.e., a script).
  1128. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1129. Vector<SceneState::PackState> states_stack = PropertyUtils::get_node_states_stack(p_node, edited_scene);
  1130. if (states_stack.size()) {
  1131. return true;
  1132. } else {
  1133. bool is_valid_default = false;
  1134. bool is_class_default = false;
  1135. PropertyUtils::get_property_default_value(p_node, p_property, &is_valid_default, &states_stack, false, nullptr, &is_class_default);
  1136. return !is_class_default;
  1137. }
  1138. }
  1139. void EditorProperty::_update_flags() {
  1140. can_pin = false;
  1141. pin_hidden = true;
  1142. if (read_only) {
  1143. return;
  1144. }
  1145. if (Node *node = Object::cast_to<Node>(object)) {
  1146. // Avoid errors down the road by ignoring nodes which are not part of a scene
  1147. if (!node->get_owner()) {
  1148. bool is_scene_root = false;
  1149. for (int i = 0; i < EditorNode::get_editor_data().get_edited_scene_count(); ++i) {
  1150. if (EditorNode::get_editor_data().get_edited_scene_root(i) == node) {
  1151. is_scene_root = true;
  1152. break;
  1153. }
  1154. }
  1155. if (!is_scene_root) {
  1156. return;
  1157. }
  1158. }
  1159. if (!_is_value_potential_override(node, property)) {
  1160. return;
  1161. }
  1162. pin_hidden = false;
  1163. {
  1164. HashSet<StringName> storable_properties;
  1165. node->get_storable_properties(storable_properties);
  1166. if (storable_properties.has(node->get_property_store_alias(property))) {
  1167. can_pin = true;
  1168. }
  1169. }
  1170. }
  1171. }
  1172. Control *EditorProperty::make_custom_tooltip(const String &p_text) const {
  1173. String symbol;
  1174. String prologue;
  1175. if (object->has_method("_get_property_warning")) {
  1176. const String custom_warning = object->call("_get_property_warning", property);
  1177. if (!custom_warning.is_empty()) {
  1178. prologue = "[b][color=" + theme_cache.warning_color.to_html(false) + "]" + custom_warning + "[/color][/b]";
  1179. }
  1180. }
  1181. if (has_doc_tooltip) {
  1182. symbol = p_text;
  1183. const EditorInspector *inspector = get_parent_inspector();
  1184. if (inspector) {
  1185. const String custom_description = inspector->get_custom_property_description(p_text);
  1186. if (!custom_description.is_empty()) {
  1187. if (!prologue.is_empty()) {
  1188. prologue += '\n';
  1189. }
  1190. prologue += custom_description;
  1191. }
  1192. }
  1193. }
  1194. if (!symbol.is_empty() || !prologue.is_empty()) {
  1195. return EditorHelpBitTooltip::make_tooltip(const_cast<EditorProperty *>(this), symbol, prologue);
  1196. }
  1197. return nullptr;
  1198. }
  1199. void EditorProperty::menu_option(int p_option) {
  1200. switch (p_option) {
  1201. case MENU_COPY_VALUE: {
  1202. EditorInspector::set_property_clipboard(object->get(property));
  1203. } break;
  1204. case MENU_PASTE_VALUE: {
  1205. EditorPropertyResource *epr = Object::cast_to<EditorPropertyResource>(this);
  1206. if (epr) {
  1207. const Ref<Resource> res = InspectorDock::get_inspector_singleton()->get_property_clipboard();
  1208. if (res.is_valid() && !epr->get_resource_picker()->is_resource_allowed(res)) {
  1209. return;
  1210. }
  1211. }
  1212. emit_changed(property, EditorInspector::get_property_clipboard());
  1213. } break;
  1214. case MENU_COPY_PROPERTY_PATH: {
  1215. DisplayServer::get_singleton()->clipboard_set(property_path);
  1216. } break;
  1217. case MENU_OVERRIDE_FOR_PROJECT: {
  1218. emit_signal(SNAME("property_overridden"));
  1219. } break;
  1220. case MENU_FAVORITE_PROPERTY: {
  1221. emit_signal(SNAME("property_favorited"), property, !favorited);
  1222. queue_redraw();
  1223. } break;
  1224. case MENU_PIN_VALUE: {
  1225. emit_signal(SNAME("property_pinned"), property, !pinned);
  1226. queue_redraw();
  1227. } break;
  1228. case MENU_DELETE: {
  1229. accept_event();
  1230. emit_signal(SNAME("property_deleted"), property);
  1231. } break;
  1232. case MENU_REVERT_VALUE: {
  1233. accept_event();
  1234. get_viewport()->gui_release_focus();
  1235. bool is_valid_revert = false;
  1236. Variant revert_value = EditorPropertyRevert::get_property_revert_value(object, property, &is_valid_revert);
  1237. ERR_FAIL_COND(!is_valid_revert);
  1238. emit_changed(_get_revert_property(), revert_value);
  1239. update_property();
  1240. } break;
  1241. case MENU_OPEN_DOCUMENTATION: {
  1242. ScriptEditor::get_singleton()->goto_help(doc_path);
  1243. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1244. } break;
  1245. }
  1246. }
  1247. void EditorProperty::_bind_methods() {
  1248. ClassDB::bind_method(D_METHOD("set_label", "text"), &EditorProperty::set_label);
  1249. ClassDB::bind_method(D_METHOD("get_label"), &EditorProperty::get_label);
  1250. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorProperty::set_read_only);
  1251. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorProperty::is_read_only);
  1252. ClassDB::bind_method(D_METHOD("set_draw_label", "draw_label"), &EditorProperty::set_draw_label);
  1253. ClassDB::bind_method(D_METHOD("is_draw_label"), &EditorProperty::is_draw_label);
  1254. ClassDB::bind_method(D_METHOD("set_draw_background", "draw_background"), &EditorProperty::set_draw_background);
  1255. ClassDB::bind_method(D_METHOD("is_draw_background"), &EditorProperty::is_draw_background);
  1256. ClassDB::bind_method(D_METHOD("set_checkable", "checkable"), &EditorProperty::set_checkable);
  1257. ClassDB::bind_method(D_METHOD("is_checkable"), &EditorProperty::is_checkable);
  1258. ClassDB::bind_method(D_METHOD("set_checked", "checked"), &EditorProperty::set_checked);
  1259. ClassDB::bind_method(D_METHOD("is_checked"), &EditorProperty::is_checked);
  1260. ClassDB::bind_method(D_METHOD("set_draw_warning", "draw_warning"), &EditorProperty::set_draw_warning);
  1261. ClassDB::bind_method(D_METHOD("is_draw_warning"), &EditorProperty::is_draw_warning);
  1262. ClassDB::bind_method(D_METHOD("set_keying", "keying"), &EditorProperty::set_keying);
  1263. ClassDB::bind_method(D_METHOD("is_keying"), &EditorProperty::is_keying);
  1264. ClassDB::bind_method(D_METHOD("set_deletable", "deletable"), &EditorProperty::set_deletable);
  1265. ClassDB::bind_method(D_METHOD("is_deletable"), &EditorProperty::is_deletable);
  1266. ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property);
  1267. ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object);
  1268. ClassDB::bind_method(D_METHOD("update_property"), &EditorProperty::update_property);
  1269. ClassDB::bind_method(D_METHOD("add_focusable", "control"), &EditorProperty::add_focusable);
  1270. ClassDB::bind_method(D_METHOD("set_bottom_editor", "editor"), &EditorProperty::set_bottom_editor);
  1271. ClassDB::bind_method(D_METHOD("set_selectable", "selectable"), &EditorProperty::set_selectable);
  1272. ClassDB::bind_method(D_METHOD("is_selectable"), &EditorProperty::is_selectable);
  1273. ClassDB::bind_method(D_METHOD("set_use_folding", "use_folding"), &EditorProperty::set_use_folding);
  1274. ClassDB::bind_method(D_METHOD("is_using_folding"), &EditorProperty::is_using_folding);
  1275. ClassDB::bind_method(D_METHOD("set_name_split_ratio", "ratio"), &EditorProperty::set_name_split_ratio);
  1276. ClassDB::bind_method(D_METHOD("get_name_split_ratio"), &EditorProperty::get_name_split_ratio);
  1277. ClassDB::bind_method(D_METHOD("deselect"), &EditorProperty::deselect);
  1278. ClassDB::bind_method(D_METHOD("is_selected"), &EditorProperty::is_selected);
  1279. ClassDB::bind_method(D_METHOD("select", "focusable"), &EditorProperty::select, DEFVAL(-1));
  1280. ClassDB::bind_method(D_METHOD("set_object_and_property", "object", "property"), &EditorProperty::set_object_and_property);
  1281. ClassDB::bind_method(D_METHOD("set_label_reference", "control"), &EditorProperty::set_label_reference);
  1282. ClassDB::bind_method(D_METHOD("emit_changed", "property", "value", "field", "changing"), &EditorProperty::emit_changed, DEFVAL(StringName()), DEFVAL(false));
  1283. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  1284. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  1285. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_label"), "set_draw_label", "is_draw_label");
  1286. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_background"), "set_draw_background", "is_draw_background");
  1287. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checkable"), "set_checkable", "is_checkable");
  1288. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checked"), "set_checked", "is_checked");
  1289. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_warning"), "set_draw_warning", "is_draw_warning");
  1290. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keying"), "set_keying", "is_keying");
  1291. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deletable"), "set_deletable", "is_deletable");
  1292. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selectable"), "set_selectable", "is_selectable");
  1293. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_folding"), "set_use_folding", "is_using_folding");
  1294. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "name_split_ratio"), "set_name_split_ratio", "get_name_split_ratio");
  1295. ADD_SIGNAL(MethodInfo("property_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::STRING_NAME, "field"), PropertyInfo(Variant::BOOL, "changing")));
  1296. ADD_SIGNAL(MethodInfo("multiple_properties_changed", PropertyInfo(Variant::PACKED_STRING_ARRAY, "properties"), PropertyInfo(Variant::ARRAY, "value")));
  1297. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING_NAME, "property")));
  1298. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING_NAME, "property")));
  1299. ADD_SIGNAL(MethodInfo("property_keyed_with_value", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1300. ADD_SIGNAL(MethodInfo("property_checked", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "checked")));
  1301. ADD_SIGNAL(MethodInfo("property_overridden"));
  1302. ADD_SIGNAL(MethodInfo("property_favorited", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "favorited")));
  1303. ADD_SIGNAL(MethodInfo("property_pinned", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "pinned")));
  1304. ADD_SIGNAL(MethodInfo("property_can_revert_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "can_revert")));
  1305. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  1306. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::INT, "id")));
  1307. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "focusable_idx")));
  1308. GDVIRTUAL_BIND(_update_property)
  1309. GDVIRTUAL_BIND(_set_read_only, "read_only")
  1310. ClassDB::bind_method(D_METHOD("_update_editor_property_status"), &EditorProperty::update_editor_property_status);
  1311. }
  1312. EditorProperty::EditorProperty() {
  1313. set_focus_mode(FOCUS_ACCESSIBILITY);
  1314. object = nullptr;
  1315. split_ratio = 0.5;
  1316. text_size = 0;
  1317. property_usage = 0;
  1318. selected_focusable = -1;
  1319. label_reference = nullptr;
  1320. bottom_editor = nullptr;
  1321. menu = nullptr;
  1322. left_container = memnew(HBoxContainer);
  1323. left_container->add_theme_constant_override(SNAME("separation"), 0);
  1324. add_child(left_container);
  1325. right_container = memnew(HBoxContainer);
  1326. right_container->add_theme_constant_override(SNAME("separation"), 0);
  1327. add_child(right_container);
  1328. set_process_shortcut_input(true);
  1329. }
  1330. void EditorProperty::_update_popup() {
  1331. if (menu) {
  1332. menu->clear();
  1333. } else {
  1334. menu = memnew(PopupMenu);
  1335. add_child(menu);
  1336. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorProperty::menu_option));
  1337. }
  1338. menu->add_icon_shortcut(theme_cache.copy_icon, ED_GET_SHORTCUT("property_editor/copy_value"), MENU_COPY_VALUE);
  1339. menu->add_icon_shortcut(theme_cache.paste_icon, ED_GET_SHORTCUT("property_editor/paste_value"), MENU_PASTE_VALUE);
  1340. menu->add_icon_shortcut(theme_cache.copy_node_path_icon, ED_GET_SHORTCUT("property_editor/copy_property_path"), MENU_COPY_PROPERTY_PATH);
  1341. menu->set_item_disabled(MENU_PASTE_VALUE, is_read_only());
  1342. menu->set_item_disabled(MENU_COPY_PROPERTY_PATH, internal);
  1343. if (can_favorite || !pin_hidden) {
  1344. menu->add_separator();
  1345. }
  1346. if (can_favorite) {
  1347. if (favorited) {
  1348. menu->add_icon_item(theme_cache.unfavorite_icon, TTR("Unfavorite Property"), MENU_FAVORITE_PROPERTY);
  1349. menu->set_item_tooltip(menu->get_item_index(MENU_FAVORITE_PROPERTY), TTR("Make this property be put back at its original place."));
  1350. } else {
  1351. // TRANSLATORS: This is a menu item to add a property to the favorites.
  1352. menu->add_icon_item(theme_cache.favorite_icon, TTR("Favorite Property"), MENU_FAVORITE_PROPERTY);
  1353. menu->set_item_tooltip(menu->get_item_index(MENU_FAVORITE_PROPERTY), TTR("Make this property be placed at the top for all objects of this class."));
  1354. }
  1355. }
  1356. if (!pin_hidden) {
  1357. if (can_pin) {
  1358. menu->add_icon_check_item(theme_cache.pin_icon, TTR("Pin Value"), MENU_PIN_VALUE);
  1359. menu->set_item_checked(menu->get_item_index(MENU_PIN_VALUE), pinned);
  1360. } else {
  1361. menu->add_icon_check_item(theme_cache.pin_icon, vformat(TTR("Pin Value [Disabled because '%s' is editor-only]"), property), MENU_PIN_VALUE);
  1362. menu->set_item_disabled(menu->get_item_index(MENU_PIN_VALUE), true);
  1363. }
  1364. menu->set_item_tooltip(menu->get_item_index(MENU_PIN_VALUE), TTR("Pinning a value forces it to be saved even if it's equal to the default."));
  1365. }
  1366. if (deletable || can_revert || can_override) {
  1367. menu->add_separator();
  1368. if (can_override) {
  1369. menu->add_icon_item(theme_cache.override_icon, TTRC("Override for Project"), MENU_OVERRIDE_FOR_PROJECT);
  1370. }
  1371. if (deletable) {
  1372. menu->add_icon_item(theme_cache.remove_icon, TTR("Delete Property"), MENU_DELETE);
  1373. }
  1374. if (can_revert) {
  1375. menu->add_icon_item(theme_cache.revert_icon, TTR("Revert Value"), MENU_REVERT_VALUE);
  1376. }
  1377. }
  1378. if (!doc_path.is_empty() && ScriptEditor::get_singleton() && EditorNode::get_singleton()) {
  1379. menu->add_separator();
  1380. menu->add_icon_item(theme_cache.help_icon, TTR("Open Documentation"), MENU_OPEN_DOCUMENTATION);
  1381. }
  1382. }
  1383. ////////////////////////////////////////////////
  1384. ////////////////////////////////////////////////
  1385. void EditorInspectorPlugin::add_custom_control(Control *control) {
  1386. AddedEditor ae;
  1387. ae.property_editor = control;
  1388. added_editors.push_back(ae);
  1389. }
  1390. void EditorInspectorPlugin::add_property_editor(const String &p_for_property, Control *p_prop, bool p_add_to_end, const String &p_label) {
  1391. AddedEditor ae;
  1392. ae.properties.push_back(p_for_property);
  1393. ae.property_editor = p_prop;
  1394. ae.add_to_end = p_add_to_end;
  1395. ae.label = p_label;
  1396. added_editors.push_back(ae);
  1397. }
  1398. void EditorInspectorPlugin::add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop) {
  1399. AddedEditor ae;
  1400. ae.properties = p_properties;
  1401. ae.property_editor = p_prop;
  1402. ae.label = p_label;
  1403. added_editors.push_back(ae);
  1404. }
  1405. bool EditorInspectorPlugin::can_handle(Object *p_object) {
  1406. bool success = false;
  1407. GDVIRTUAL_CALL(_can_handle, p_object, success);
  1408. return success;
  1409. }
  1410. void EditorInspectorPlugin::parse_begin(Object *p_object) {
  1411. GDVIRTUAL_CALL(_parse_begin, p_object);
  1412. }
  1413. void EditorInspectorPlugin::parse_category(Object *p_object, const String &p_category) {
  1414. GDVIRTUAL_CALL(_parse_category, p_object, p_category);
  1415. }
  1416. void EditorInspectorPlugin::parse_group(Object *p_object, const String &p_group) {
  1417. GDVIRTUAL_CALL(_parse_group, p_object, p_group);
  1418. }
  1419. bool EditorInspectorPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  1420. bool ret = false;
  1421. GDVIRTUAL_CALL(_parse_property, p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide, ret);
  1422. return ret;
  1423. }
  1424. void EditorInspectorPlugin::parse_end(Object *p_object) {
  1425. GDVIRTUAL_CALL(_parse_end, p_object);
  1426. }
  1427. void EditorInspectorPlugin::_bind_methods() {
  1428. ClassDB::bind_method(D_METHOD("add_custom_control", "control"), &EditorInspectorPlugin::add_custom_control);
  1429. ClassDB::bind_method(D_METHOD("add_property_editor", "property", "editor", "add_to_end", "label"), &EditorInspectorPlugin::add_property_editor, DEFVAL(false), DEFVAL(String()));
  1430. ClassDB::bind_method(D_METHOD("add_property_editor_for_multiple_properties", "label", "properties", "editor"), &EditorInspectorPlugin::add_property_editor_for_multiple_properties);
  1431. GDVIRTUAL_BIND(_can_handle, "object")
  1432. GDVIRTUAL_BIND(_parse_begin, "object")
  1433. GDVIRTUAL_BIND(_parse_category, "object", "category")
  1434. GDVIRTUAL_BIND(_parse_group, "object", "group")
  1435. GDVIRTUAL_BIND(_parse_property, "object", "type", "name", "hint_type", "hint_string", "usage_flags", "wide");
  1436. GDVIRTUAL_BIND(_parse_end, "object")
  1437. }
  1438. ////////////////////////////////////////////////
  1439. ////////////////////////////////////////////////
  1440. static Ref<Script> _get_category_script(const PropertyInfo &p_info) {
  1441. if (!p_info.hint_string.is_empty() && !EditorNode::get_editor_data().is_type_recognized(p_info.name) && ResourceLoader::exists(p_info.hint_string, "Script")) {
  1442. return ResourceLoader::load(p_info.hint_string, "Script");
  1443. }
  1444. return Ref<Script>();
  1445. }
  1446. void EditorInspectorCategory::_bind_methods() {
  1447. ADD_SIGNAL(MethodInfo("unfavorite_all"));
  1448. }
  1449. void EditorInspectorCategory::_notification(int p_what) {
  1450. switch (p_what) {
  1451. case NOTIFICATION_POSTINITIALIZE: {
  1452. connect(SceneStringName(theme_changed), callable_mp(this, &EditorInspectorCategory::_theme_changed));
  1453. } break;
  1454. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  1455. RID ae = get_accessibility_element();
  1456. ERR_FAIL_COND(ae.is_null());
  1457. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  1458. DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Category: %s"), label));
  1459. DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Category: %s"), label));
  1460. DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
  1461. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &EditorInspectorCategory::_accessibility_action_menu));
  1462. } break;
  1463. case NOTIFICATION_TRANSLATION_CHANGED: {
  1464. if (is_favorite) {
  1465. label = TTR("Favorites");
  1466. }
  1467. queue_accessibility_update();
  1468. } break;
  1469. case NOTIFICATION_DRAW: {
  1470. const Ref<StyleBox> &sb = theme_cache.background;
  1471. draw_style_box(sb, Rect2(Vector2(), get_size()));
  1472. const Ref<Font> &font = theme_cache.bold_font;
  1473. int font_size = theme_cache.bold_font_size;
  1474. int hs = theme_cache.horizontal_separation;
  1475. int icon_size = theme_cache.class_icon_size;
  1476. int w = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
  1477. if (icon.is_valid()) {
  1478. w += hs + icon_size;
  1479. }
  1480. w = MIN(w, get_size().width - sb->get_minimum_size().width);
  1481. int ofs = (get_size().width - w) / 2;
  1482. float v_margin_offset = sb->get_content_margin(SIDE_TOP) - sb->get_content_margin(SIDE_BOTTOM);
  1483. if (icon.is_valid()) {
  1484. Size2 rect_size = Size2(icon_size, icon_size);
  1485. Point2 rect_pos = Point2(ofs, (get_size().height - icon_size) / 2 + v_margin_offset).round();
  1486. if (is_layout_rtl()) {
  1487. rect_pos.x = get_size().width - rect_pos.x - icon_size;
  1488. }
  1489. draw_texture_rect(icon, Rect2(rect_pos, rect_size));
  1490. ofs += hs + icon_size;
  1491. w -= hs + icon_size;
  1492. }
  1493. if (is_layout_rtl()) {
  1494. ofs = get_size().width - ofs - w;
  1495. }
  1496. // Use TextLine so we have access to accurate font metrics. This way,
  1497. // we can ensure the line is vertically centered regardless of the font used
  1498. // or its size.
  1499. Ref<TextLine> tl;
  1500. tl.instantiate();
  1501. tl->add_string(label, font, font_size);
  1502. tl->set_width(w);
  1503. tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1504. float text_pos_y = (get_size().height - tl->get_size().height) / 2 + v_margin_offset;
  1505. Point2 text_pos = Point2(ofs, text_pos_y).round();
  1506. tl->draw(get_canvas_item(), text_pos, theme_cache.font_color);
  1507. } break;
  1508. }
  1509. }
  1510. void EditorInspectorCategory::_accessibility_action_menu(const Variant &p_data) {
  1511. _popup_context_menu(get_screen_position());
  1512. }
  1513. Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) const {
  1514. // If it's not a doc tooltip, fallback to the default one.
  1515. if (doc_class_name.is_empty()) {
  1516. return nullptr;
  1517. }
  1518. return EditorHelpBitTooltip::make_tooltip(const_cast<EditorInspectorCategory *>(this), p_text);
  1519. }
  1520. void EditorInspectorCategory::set_as_favorite() {
  1521. is_favorite = true;
  1522. _update_icon();
  1523. }
  1524. void EditorInspectorCategory::set_property_info(const PropertyInfo &p_info) {
  1525. info = p_info;
  1526. Ref<Script> scr = _get_category_script(info);
  1527. if (scr.is_valid()) {
  1528. StringName script_name = EditorNode::get_editor_data().script_class_get_name(scr->get_path());
  1529. if (script_name != StringName()) {
  1530. label = script_name;
  1531. }
  1532. }
  1533. if (label.is_empty()) {
  1534. label = info.name;
  1535. }
  1536. _update_icon();
  1537. }
  1538. void EditorInspectorCategory::set_doc_class_name(const String &p_name) {
  1539. doc_class_name = p_name;
  1540. }
  1541. Size2 EditorInspectorCategory::get_minimum_size() const {
  1542. Size2 ms;
  1543. if (theme_cache.bold_font.is_valid()) {
  1544. ms.height = theme_cache.bold_font->get_height(theme_cache.bold_font_size);
  1545. }
  1546. if (icon.is_valid()) {
  1547. ms.height = MAX(theme_cache.class_icon_size, ms.height);
  1548. }
  1549. ms.height += theme_cache.vertical_separation;
  1550. if (theme_cache.background.is_valid()) {
  1551. ms.height += theme_cache.background->get_content_margin(SIDE_TOP) + theme_cache.background->get_content_margin(SIDE_BOTTOM);
  1552. }
  1553. return ms;
  1554. }
  1555. void EditorInspectorCategory::_handle_menu_option(int p_option) {
  1556. switch (p_option) {
  1557. case MENU_OPEN_DOCS: {
  1558. ScriptEditor::get_singleton()->goto_help("class:" + doc_class_name);
  1559. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1560. } break;
  1561. case MENU_UNFAVORITE_ALL: {
  1562. emit_signal(SNAME("unfavorite_all"));
  1563. } break;
  1564. }
  1565. }
  1566. void EditorInspectorCategory::_popup_context_menu(const Point2i &p_position) {
  1567. if (!is_favorite && doc_class_name.is_empty()) {
  1568. return;
  1569. }
  1570. if (menu == nullptr) {
  1571. menu = memnew(PopupMenu);
  1572. if (is_favorite) {
  1573. menu->add_item(TTRC("Unfavorite All"), MENU_UNFAVORITE_ALL);
  1574. } else {
  1575. menu->add_item(TTRC("Open Documentation"), MENU_OPEN_DOCS);
  1576. menu->set_item_disabled(-1, !EditorHelp::get_doc_data()->class_list.has(doc_class_name));
  1577. }
  1578. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorCategory::_handle_menu_option));
  1579. add_child(menu);
  1580. }
  1581. if (menu_icon_dirty) {
  1582. if (is_favorite) {
  1583. menu->set_item_icon(menu->get_item_index(MENU_UNFAVORITE_ALL), theme_cache.icon_unfavorite);
  1584. } else {
  1585. menu->set_item_icon(menu->get_item_index(MENU_OPEN_DOCS), theme_cache.icon_help);
  1586. }
  1587. menu_icon_dirty = false;
  1588. }
  1589. menu->set_position(p_position);
  1590. menu->reset_size();
  1591. menu->popup();
  1592. }
  1593. void EditorInspectorCategory::_update_icon() {
  1594. if (is_favorite) {
  1595. icon = theme_cache.icon_favorites;
  1596. return;
  1597. }
  1598. icon = Ref<Texture2D>();
  1599. Ref<Script> scr = _get_category_script(info);
  1600. if (scr.is_valid()) {
  1601. StringName script_name = EditorNode::get_editor_data().script_class_get_name(scr->get_path());
  1602. if (script_name == StringName()) {
  1603. icon = EditorNode::get_singleton()->get_object_icon(scr.ptr());
  1604. } else {
  1605. icon = EditorNode::get_singleton()->get_class_icon(script_name);
  1606. }
  1607. }
  1608. if (icon.is_null() && !info.name.is_empty()) {
  1609. icon = EditorNode::get_singleton()->get_class_icon(info.name);
  1610. }
  1611. }
  1612. void EditorInspectorCategory::_theme_changed() {
  1613. // This needs to be done via the signal, as it's fired before the minimum since is updated.
  1614. EditorInspector::initialize_category_theme(theme_cache, this);
  1615. menu_icon_dirty = true;
  1616. _update_icon();
  1617. }
  1618. void EditorInspectorCategory::gui_input(const Ref<InputEvent> &p_event) {
  1619. const Ref<InputEventMouseButton> &mb = p_event;
  1620. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
  1621. _popup_context_menu(get_screen_position() + mb->get_position());
  1622. }
  1623. }
  1624. EditorInspectorCategory::EditorInspectorCategory() {
  1625. set_focus_mode(FOCUS_ACCESSIBILITY);
  1626. }
  1627. ////////////////////////////////////////////////
  1628. ////////////////////////////////////////////////
  1629. void EditorInspectorSection::_test_unfold() {
  1630. if (!vbox_added) {
  1631. add_child(vbox);
  1632. move_child(vbox, 0);
  1633. vbox_added = true;
  1634. }
  1635. }
  1636. Ref<Texture2D> EditorInspectorSection::_get_arrow() {
  1637. Ref<Texture2D> arrow;
  1638. if (foldable) {
  1639. if (object->editor_is_section_unfolded(section)) {
  1640. arrow = theme_cache.arrow;
  1641. } else {
  1642. if (is_layout_rtl()) {
  1643. arrow = theme_cache.arrow_collapsed_mirrored;
  1644. } else {
  1645. arrow = theme_cache.arrow_collapsed;
  1646. }
  1647. }
  1648. }
  1649. return arrow;
  1650. }
  1651. Ref<Texture2D> EditorInspectorSection::_get_checkbox() {
  1652. Ref<Texture2D> checkbox;
  1653. if (checkable) {
  1654. if (checked) {
  1655. checkbox = theme_cache.icon_gui_checked;
  1656. } else {
  1657. checkbox = theme_cache.icon_gui_unchecked;
  1658. }
  1659. }
  1660. return checkbox;
  1661. }
  1662. int EditorInspectorSection::_get_header_height() {
  1663. int header_height = theme_cache.bold_font->get_height(theme_cache.bold_font_size);
  1664. Ref<Texture2D> arrow = _get_arrow();
  1665. if (arrow.is_valid()) {
  1666. header_height = MAX(header_height, arrow->get_height());
  1667. }
  1668. header_height += theme_cache.vertical_separation;
  1669. return header_height;
  1670. }
  1671. void EditorInspectorSection::_notification(int p_what) {
  1672. switch (p_what) {
  1673. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  1674. RID ae = get_accessibility_element();
  1675. ERR_FAIL_COND(ae.is_null());
  1676. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  1677. DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Section: %s"), label));
  1678. DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Section: %s"), label));
  1679. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &EditorInspectorSection::_accessibility_action_collapse));
  1680. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &EditorInspectorSection::_accessibility_action_expand));
  1681. } break;
  1682. case NOTIFICATION_THEME_CHANGED: {
  1683. EditorInspector::initialize_section_theme(theme_cache, this);
  1684. bg_color = theme_cache.prop_subsection;
  1685. bg_color.a /= level;
  1686. } break;
  1687. case NOTIFICATION_SORT_CHILDREN: {
  1688. if (!vbox_added) {
  1689. return;
  1690. }
  1691. int inspector_margin = theme_cache.inspector_margin;
  1692. if (indent_depth > 0 && theme_cache.indent_size > 0) {
  1693. inspector_margin += indent_depth * theme_cache.indent_size;
  1694. }
  1695. if (indent_depth > 0 && theme_cache.indent_box.is_valid()) {
  1696. inspector_margin += theme_cache.indent_box->get_margin(SIDE_LEFT) + theme_cache.indent_box->get_margin(SIDE_RIGHT);
  1697. }
  1698. Size2 size = get_size() - Vector2(inspector_margin, 0);
  1699. int header_height = _get_header_height();
  1700. Vector2 offset = Vector2(is_layout_rtl() ? 0 : inspector_margin, header_height);
  1701. for (int i = 0; i < get_child_count(); i++) {
  1702. Control *c = as_sortable_control(get_child(i));
  1703. if (!c) {
  1704. continue;
  1705. }
  1706. fit_child_in_rect(c, Rect2(offset, size));
  1707. }
  1708. } break;
  1709. case NOTIFICATION_DRAW: {
  1710. int section_indent = 0;
  1711. int section_indent_size = theme_cache.indent_size;
  1712. if (indent_depth > 0 && section_indent_size > 0) {
  1713. section_indent = indent_depth * section_indent_size;
  1714. }
  1715. Ref<StyleBoxFlat> section_indent_style = theme_cache.indent_box;
  1716. if (indent_depth > 0 && section_indent_style.is_valid()) {
  1717. section_indent += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
  1718. }
  1719. int header_width = get_size().width - section_indent;
  1720. int header_offset_x = 0.0;
  1721. bool rtl = is_layout_rtl();
  1722. if (!rtl) {
  1723. header_offset_x += section_indent;
  1724. }
  1725. bool can_click_unfold = vbox->get_child_count(false) != 0 && !(checkable && !checked && !checkbox_only);
  1726. // Draw header area.
  1727. int header_height = _get_header_height();
  1728. Rect2 header_rect = Rect2(Vector2(header_offset_x, 0.0), Vector2(header_width, header_height));
  1729. Color c = bg_color;
  1730. c.a *= 0.4;
  1731. if (header_hover) {
  1732. c = c.lightened(Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT) ? -0.05 : 0.2);
  1733. }
  1734. draw_rect(header_rect, c);
  1735. // Draw header title, folding arrow and count of revertable properties.
  1736. {
  1737. int outer_margin = Math::round(2 * EDSCALE);
  1738. int margin_start = section_indent + outer_margin;
  1739. int margin_end = outer_margin;
  1740. // - Arrow.
  1741. Ref<Texture2D> arrow = _get_arrow();
  1742. if (arrow.is_valid()) {
  1743. Point2 arrow_position;
  1744. if (rtl) {
  1745. arrow_position.x = get_size().width - (margin_start + arrow->get_width());
  1746. } else {
  1747. arrow_position.x = margin_start;
  1748. }
  1749. arrow_position.y = (header_height - arrow->get_height()) / 2;
  1750. if (can_click_unfold) {
  1751. draw_texture(arrow, arrow_position, Color(1, 1, 1, header_hover ? 1.0 : 0.85));
  1752. }
  1753. margin_start += arrow->get_width() + theme_cache.horizontal_separation;
  1754. }
  1755. Ref<Font> font = theme_cache.bold_font;
  1756. int font_size = theme_cache.bold_font_size;
  1757. Color font_color = theme_cache.font_color;
  1758. Ref<Font> light_font = theme_cache.light_font;
  1759. int light_font_size = theme_cache.light_font_size;
  1760. // - Keying
  1761. Ref<Texture2D> key = theme_cache.icon_gui_animation_key;
  1762. if (keying && key.is_valid()) {
  1763. Point2 key_position;
  1764. key_position.x = (rtl ? margin_end : (get_size().width - key->get_width() - margin_end)) - theme_cache.key_padding_size / 2;
  1765. keying_rect = Rect2(key_position.x - theme_cache.key_padding_size / 2, 0, key->get_width() + theme_cache.key_padding_size, header_height);
  1766. Color key_color(1, 1, 1);
  1767. if (keying_hover) {
  1768. key_color.r *= 1.2;
  1769. key_color.g *= 1.2;
  1770. key_color.b *= 1.2;
  1771. Ref<StyleBox> sb_hover = theme_cache.key_hover;
  1772. draw_style_box(sb_hover, keying_rect);
  1773. }
  1774. key_position.y = (header_height - key->get_height()) / 2;
  1775. draw_texture(key, key_position, key_color);
  1776. margin_end += keying_rect.size.width + theme_cache.horizontal_separation;
  1777. } else {
  1778. keying_rect = Rect2();
  1779. }
  1780. // - Checkbox.
  1781. Ref<Texture2D> checkbox = _get_checkbox();
  1782. if (checkbox.is_valid()) {
  1783. const String checkbox_text = TTR("On");
  1784. Size2 label_size = light_font->get_string_size(checkbox_text, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size);
  1785. Point2 checkbox_position;
  1786. Point2 label_position;
  1787. if (rtl) {
  1788. label_position.x = margin_end;
  1789. checkbox_position.x = margin_end + label_size.width + 2 * EDSCALE;
  1790. } else {
  1791. label_position.x = get_size().width - (margin_end + label_size.width);
  1792. checkbox_position.x = label_position.x - checkbox->get_width() - 2 * EDSCALE;
  1793. }
  1794. checkbox_position.y = (header_height - checkbox->get_height()) / 2;
  1795. label_position.y = light_font->get_ascent(light_font_size) + (header_height - label_size.height) / 2.0;
  1796. check_rect = Rect2(checkbox_position.x, 0, checkbox->get_width() + label_size.width + 2 * EDSCALE, header_height);
  1797. Color check_font_color = font_color;
  1798. Color checkbox_color(1, 1, 1);
  1799. if (check_hover) {
  1800. checkbox_color.r *= 1.2;
  1801. checkbox_color.g *= 1.2;
  1802. checkbox_color.b *= 1.2;
  1803. check_font_color = checked ? theme_cache.font_hover_pressed_color : theme_cache.font_hover_color;
  1804. } else if (checked) {
  1805. check_font_color = theme_cache.font_pressed_color;
  1806. }
  1807. draw_texture(checkbox, checkbox_position, checkbox_color);
  1808. draw_string(light_font, label_position, checkbox_text, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, check_font_color, TextServer::JUSTIFICATION_NONE);
  1809. margin_end += label_size.width + checkbox->get_width() + 6 * EDSCALE;
  1810. } else {
  1811. check_rect = Rect2();
  1812. }
  1813. int available = get_size().width - (margin_start + margin_end);
  1814. // - Count of revertable properties.
  1815. String num_revertable_str;
  1816. int num_revertable_width = 0;
  1817. bool folded = (foldable || !checkbox_only) && !vbox->is_visible();
  1818. if (folded && revertable_properties.size()) {
  1819. int label_width = theme_cache.bold_font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, available, theme_cache.bold_font_size, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS).x;
  1820. // Can we fit the long version of the revertable count text?
  1821. num_revertable_str = vformat(TTRN("(%d change)", "(%d changes)", revertable_properties.size()), revertable_properties.size());
  1822. num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
  1823. if (label_width + outer_margin + num_revertable_width > available) {
  1824. // We'll have to use the short version.
  1825. num_revertable_str = vformat("(%d)", revertable_properties.size());
  1826. num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
  1827. }
  1828. float text_offset_y = light_font->get_ascent(light_font_size) + (header_height - light_font->get_height(light_font_size)) / 2;
  1829. Point2 text_offset = Point2(margin_end, text_offset_y).round();
  1830. if (!rtl) {
  1831. text_offset.x = get_size().width - (text_offset.x + num_revertable_width);
  1832. }
  1833. draw_string(light_font, text_offset, num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, theme_cache.font_disabled_color, TextServer::JUSTIFICATION_NONE);
  1834. margin_end += num_revertable_width + outer_margin;
  1835. available -= num_revertable_width + outer_margin;
  1836. }
  1837. // - Label.
  1838. float text_offset_y = font->get_ascent(font_size) + (header_height - font->get_height(font_size)) / 2;
  1839. Point2 text_offset = Point2(margin_start, text_offset_y).round();
  1840. if (rtl) {
  1841. text_offset.x = margin_end;
  1842. }
  1843. if (object->has_method("_get_property_warning") && !String(object->call("_get_property_warning", related_enable_property)).is_empty()) {
  1844. font_color = theme_cache.warning_color;
  1845. }
  1846. const Color string_color = header_hover ? theme_cache.font_hover_mono_color : font_color;
  1847. HorizontalAlignment text_align = rtl ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT;
  1848. draw_string(font, text_offset, label, text_align, available, font_size, string_color, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  1849. }
  1850. // Draw section indentation.
  1851. if (section_indent_style.is_valid() && section_indent > 0) {
  1852. Rect2 indent_rect = Rect2(Vector2(), Vector2(indent_depth * section_indent_size, get_size().height));
  1853. if (rtl) {
  1854. indent_rect.position.x = get_size().width - section_indent + section_indent_style->get_margin(SIDE_RIGHT);
  1855. } else {
  1856. indent_rect.position.x = section_indent_style->get_margin(SIDE_LEFT);
  1857. }
  1858. draw_style_box(section_indent_style, indent_rect);
  1859. }
  1860. } break;
  1861. case NOTIFICATION_DRAG_BEGIN: {
  1862. dropping_for_unfold = true;
  1863. } break;
  1864. case NOTIFICATION_DRAG_END: {
  1865. dropping_for_unfold = false;
  1866. } break;
  1867. case NOTIFICATION_MOUSE_ENTER: {
  1868. if (dropping_for_unfold) {
  1869. dropping_unfold_timer->start();
  1870. }
  1871. queue_redraw();
  1872. } break;
  1873. case NOTIFICATION_MOUSE_EXIT_SELF:
  1874. case NOTIFICATION_MOUSE_EXIT: {
  1875. if (dropping_for_unfold) {
  1876. dropping_unfold_timer->stop();
  1877. }
  1878. if (header_hover || check_hover || keying_hover) {
  1879. header_hover = false;
  1880. check_hover = false;
  1881. keying_hover = false;
  1882. queue_redraw();
  1883. }
  1884. } break;
  1885. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1886. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor")) {
  1887. dropping_unfold_timer->set_wait_time(EDITOR_GET("interface/editor/dragging_hover_wait_seconds"));
  1888. }
  1889. } break;
  1890. }
  1891. }
  1892. Size2 EditorInspectorSection::get_minimum_size() const {
  1893. Size2 ms;
  1894. for (int i = 0; i < get_child_count(); i++) {
  1895. Control *c = as_sortable_control(get_child(i));
  1896. if (!c) {
  1897. continue;
  1898. }
  1899. Size2 minsize = c->get_combined_minimum_size();
  1900. ms = ms.max(minsize);
  1901. }
  1902. if (theme_cache.font.is_valid()) {
  1903. ms.height += theme_cache.font->get_height(theme_cache.font_size) + theme_cache.vertical_separation;
  1904. ms.width += theme_cache.inspector_margin;
  1905. }
  1906. if (indent_depth > 0 && theme_cache.indent_size > 0) {
  1907. ms.width += indent_depth * theme_cache.indent_size;
  1908. }
  1909. if (indent_depth > 0 && theme_cache.indent_box.is_valid()) {
  1910. ms.width += theme_cache.indent_box->get_margin(SIDE_LEFT) + theme_cache.indent_box->get_margin(SIDE_RIGHT);
  1911. }
  1912. return ms;
  1913. }
  1914. EditorInspector *EditorInspectorSection::_get_parent_inspector() const {
  1915. Node *parent = get_parent();
  1916. while (parent) {
  1917. EditorInspector *ei = Object::cast_to<EditorInspector>(parent);
  1918. if (ei) {
  1919. return ei;
  1920. }
  1921. parent = parent->get_parent();
  1922. }
  1923. return nullptr;
  1924. }
  1925. Control *EditorInspectorSection::make_custom_tooltip(const String &p_text) const {
  1926. if (!checkable) {
  1927. return Container::make_custom_tooltip(p_text);
  1928. }
  1929. String symbol;
  1930. String prologue;
  1931. if (object->has_method("_get_property_warning")) {
  1932. const String custom_warning = object->call("_get_property_warning", related_enable_property);
  1933. if (!custom_warning.is_empty()) {
  1934. prologue = "[b][color=" + theme_cache.warning_color.to_html(false) + "]" + custom_warning + "[/color][/b]";
  1935. }
  1936. }
  1937. symbol = p_text;
  1938. const EditorInspector *inspector = _get_parent_inspector();
  1939. if (inspector) {
  1940. const String custom_description = inspector->get_custom_property_description(p_text);
  1941. if (!custom_description.is_empty()) {
  1942. if (!prologue.is_empty()) {
  1943. prologue += '\n';
  1944. }
  1945. prologue += custom_description;
  1946. }
  1947. }
  1948. if (!symbol.is_empty() || !prologue.is_empty()) {
  1949. return EditorHelpBitTooltip::make_tooltip(const_cast<EditorInspectorSection *>(this), symbol, prologue);
  1950. }
  1951. return nullptr;
  1952. }
  1953. void EditorInspectorSection::setup(const String &p_section, const String &p_label, Object *p_object, const Color &p_bg_color, bool p_foldable, int p_indent_depth, int p_level) {
  1954. section = p_section;
  1955. label = p_label;
  1956. object = p_object;
  1957. bg_color = p_bg_color;
  1958. foldable = p_foldable;
  1959. indent_depth = p_indent_depth;
  1960. level = p_level;
  1961. _test_unfold();
  1962. if (foldable) {
  1963. if (object->editor_is_section_unfolded(section)) {
  1964. vbox->show();
  1965. } else {
  1966. vbox->hide();
  1967. }
  1968. }
  1969. }
  1970. void EditorInspectorSection::gui_input(const Ref<InputEvent> &p_event) {
  1971. ERR_FAIL_COND(p_event.is_null());
  1972. bool can_click_unfold = vbox->get_child_count(false) != 0 && !(!checkbox_only && checkable && !checked);
  1973. Ref<InputEventMouseMotion> mm = p_event;
  1974. if (mm.is_valid()) {
  1975. Vector2 mpos = mm->get_position();
  1976. bool new_check_hover = check_rect.has_point(mpos);
  1977. if (new_check_hover != check_hover) {
  1978. check_hover = new_check_hover;
  1979. queue_redraw();
  1980. }
  1981. bool new_keying_hover = keying_rect.has_point(mpos);
  1982. if (new_keying_hover != keying_hover) {
  1983. keying_hover = new_keying_hover;
  1984. queue_redraw();
  1985. }
  1986. bool new_header_hover = foldable && can_click_unfold && (get_local_mouse_position().y < _get_header_height());
  1987. if (new_header_hover != header_hover) {
  1988. header_hover = new_header_hover;
  1989. queue_redraw();
  1990. }
  1991. }
  1992. Ref<InputEventKey> k = p_event;
  1993. if (k.is_valid() && k->is_pressed()) {
  1994. if (foldable && can_click_unfold && k->is_action("ui_accept", true)) {
  1995. accept_event();
  1996. bool should_unfold = !object->editor_is_section_unfolded(section);
  1997. if (should_unfold) {
  1998. unfold();
  1999. } else {
  2000. fold();
  2001. }
  2002. }
  2003. }
  2004. Ref<InputEventMouseButton> mb = p_event;
  2005. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  2006. Vector2 pos = mb->get_position();
  2007. if (object->editor_is_section_unfolded(section)) {
  2008. int header_height = _get_header_height();
  2009. if (pos.y >= header_height) {
  2010. return;
  2011. }
  2012. }
  2013. accept_event();
  2014. if (checkable && check_rect.has_point(pos)) {
  2015. checked = !checked;
  2016. emit_signal(SNAME("section_toggled_by_user"), related_enable_property, checked);
  2017. if (checked) {
  2018. unfold();
  2019. } else if (!checkbox_only) {
  2020. vbox->hide();
  2021. }
  2022. } else if (keying && keying_rect.has_point(pos)) {
  2023. emit_signal(SNAME("property_keyed"), related_enable_property, false);
  2024. } else if (foldable) {
  2025. bool should_unfold = can_click_unfold && !object->editor_is_section_unfolded(section);
  2026. if (should_unfold) {
  2027. unfold();
  2028. } else {
  2029. fold();
  2030. }
  2031. }
  2032. } else if (mb.is_valid() && !mb->is_pressed()) {
  2033. queue_redraw();
  2034. }
  2035. }
  2036. String EditorInspectorSection::get_section() const {
  2037. return section;
  2038. }
  2039. VBoxContainer *EditorInspectorSection::get_vbox() {
  2040. return vbox;
  2041. }
  2042. void EditorInspectorSection::_accessibility_action_collapse(const Variant &p_data) {
  2043. fold();
  2044. }
  2045. void EditorInspectorSection::_accessibility_action_expand(const Variant &p_data) {
  2046. unfold();
  2047. }
  2048. void EditorInspectorSection::unfold() {
  2049. if ((!foldable && !checkable) || (!checkbox_only && checkable && !checked)) {
  2050. return;
  2051. }
  2052. _test_unfold();
  2053. if (foldable) {
  2054. object->editor_set_section_unfold(section, true);
  2055. }
  2056. vbox->show();
  2057. queue_redraw();
  2058. }
  2059. void EditorInspectorSection::fold() {
  2060. if (!foldable || !vbox_added) {
  2061. return;
  2062. }
  2063. object->editor_set_section_unfold(section, false);
  2064. vbox->hide();
  2065. queue_redraw();
  2066. }
  2067. void EditorInspectorSection::set_bg_color(const Color &p_bg_color) {
  2068. bg_color = p_bg_color;
  2069. queue_redraw();
  2070. }
  2071. void EditorInspectorSection::set_keying(bool p_keying) {
  2072. if (keying == (checkable && p_keying)) {
  2073. return;
  2074. }
  2075. keying = checkable && p_keying;
  2076. if (checkable) {
  2077. queue_redraw();
  2078. }
  2079. }
  2080. void EditorInspectorSection::reset_timer() {
  2081. if (dropping_for_unfold && !dropping_unfold_timer->is_stopped()) {
  2082. dropping_unfold_timer->start();
  2083. }
  2084. }
  2085. void EditorInspectorSection::set_checkable(const String &p_related_check_property, bool p_checkbox_only, bool p_checked) {
  2086. if (checkable == !p_related_check_property.is_empty()) {
  2087. return;
  2088. }
  2089. checkbox_only = p_checkbox_only;
  2090. checkable = !p_related_check_property.is_empty();
  2091. checked = p_checked;
  2092. related_enable_property = p_related_check_property;
  2093. if (InspectorDock::get_singleton()) {
  2094. if (checkable) {
  2095. InspectorDock::get_inspector_singleton()->connect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
  2096. } else {
  2097. InspectorDock::get_inspector_singleton()->disconnect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
  2098. }
  2099. }
  2100. if (!checkbox_only && checkable && !checked) {
  2101. vbox->hide();
  2102. }
  2103. queue_redraw();
  2104. }
  2105. void EditorInspectorSection::set_checked(bool p_checked) {
  2106. if (checked == p_checked) {
  2107. return;
  2108. }
  2109. checked = p_checked;
  2110. if (!checkbox_only && checkable && !checked) {
  2111. vbox->hide();
  2112. } else if (!checkbox_only) {
  2113. unfold();
  2114. }
  2115. queue_redraw();
  2116. }
  2117. bool EditorInspectorSection::has_revertable_properties() const {
  2118. return !revertable_properties.is_empty();
  2119. }
  2120. void EditorInspectorSection::property_can_revert_changed(const String &p_path, bool p_can_revert) {
  2121. bool had_revertable_properties = has_revertable_properties();
  2122. if (p_can_revert) {
  2123. revertable_properties.insert(p_path);
  2124. } else {
  2125. revertable_properties.erase(p_path);
  2126. }
  2127. if (has_revertable_properties() != had_revertable_properties) {
  2128. queue_redraw();
  2129. }
  2130. }
  2131. void EditorInspectorSection::_property_edited(const String &p_property) {
  2132. if (!related_enable_property.is_empty() && p_property == related_enable_property) {
  2133. update_property();
  2134. }
  2135. }
  2136. void EditorInspectorSection::update_property() {
  2137. if (!checkable) {
  2138. return;
  2139. }
  2140. bool valid = false;
  2141. Variant value_checked = object->get(related_enable_property, &valid);
  2142. if (valid) {
  2143. set_checked(value_checked.operator bool());
  2144. }
  2145. }
  2146. void EditorInspectorSection::_bind_methods() {
  2147. ClassDB::bind_method(D_METHOD("setup", "section", "label", "object", "bg_color", "foldable", "indent_depth", "level"), &EditorInspectorSection::setup, DEFVAL(0), DEFVAL(1));
  2148. ClassDB::bind_method(D_METHOD("get_vbox"), &EditorInspectorSection::get_vbox);
  2149. ClassDB::bind_method(D_METHOD("unfold"), &EditorInspectorSection::unfold);
  2150. ClassDB::bind_method(D_METHOD("fold"), &EditorInspectorSection::fold);
  2151. ADD_SIGNAL(MethodInfo("section_toggled_by_user", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "value")));
  2152. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING_NAME, "property")));
  2153. }
  2154. EditorInspectorSection::EditorInspectorSection() {
  2155. set_focus_mode(FOCUS_ACCESSIBILITY);
  2156. vbox = memnew(VBoxContainer);
  2157. vbox->set_theme_type_variation(SNAME("EditorPropertyContainer"));
  2158. dropping_unfold_timer = memnew(Timer);
  2159. dropping_unfold_timer->set_wait_time(EDITOR_GET("interface/editor/dragging_hover_wait_seconds"));
  2160. dropping_unfold_timer->set_one_shot(true);
  2161. add_child(dropping_unfold_timer);
  2162. dropping_unfold_timer->connect("timeout", callable_mp(this, &EditorInspectorSection::unfold));
  2163. }
  2164. EditorInspectorSection::~EditorInspectorSection() {
  2165. if (!vbox_added) {
  2166. memdelete(vbox);
  2167. }
  2168. if (checkable && InspectorDock::get_singleton()) {
  2169. InspectorDock::get_inspector_singleton()->disconnect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
  2170. }
  2171. }
  2172. ////////////////////////////////////////////////
  2173. ////////////////////////////////////////////////
  2174. int EditorInspectorArray::_get_array_count() {
  2175. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2176. List<PropertyInfo> object_property_list;
  2177. object->get_property_list(&object_property_list);
  2178. return _extract_properties_as_array(object_property_list).size();
  2179. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2180. bool valid;
  2181. int count_val = object->get(count_property, &valid);
  2182. ERR_FAIL_COND_V_MSG(!valid, 0, vformat("%s is not a valid property to be used as array count.", count_property));
  2183. return count_val;
  2184. }
  2185. return 0;
  2186. }
  2187. void EditorInspectorArray::_add_button_pressed() {
  2188. _move_element(-1, -1);
  2189. }
  2190. void EditorInspectorArray::_paginator_page_changed(int p_page) {
  2191. emit_signal("page_change_request", p_page);
  2192. }
  2193. void EditorInspectorArray::_rmb_popup_id_pressed(int p_id) {
  2194. switch (p_id) {
  2195. case OPTION_MOVE_UP:
  2196. if (popup_array_index_pressed > 0) {
  2197. _move_element(popup_array_index_pressed, popup_array_index_pressed - 1);
  2198. }
  2199. break;
  2200. case OPTION_MOVE_DOWN:
  2201. if (popup_array_index_pressed < count - 1) {
  2202. _move_element(popup_array_index_pressed, popup_array_index_pressed + 2);
  2203. }
  2204. break;
  2205. case OPTION_NEW_BEFORE:
  2206. _move_element(-1, popup_array_index_pressed);
  2207. break;
  2208. case OPTION_NEW_AFTER:
  2209. _move_element(-1, popup_array_index_pressed + 1);
  2210. break;
  2211. case OPTION_REMOVE:
  2212. _move_element(popup_array_index_pressed, -1);
  2213. break;
  2214. case OPTION_CLEAR_ARRAY:
  2215. _clear_array();
  2216. break;
  2217. case OPTION_RESIZE_ARRAY:
  2218. new_size_spin_box->set_value(count);
  2219. resize_dialog->get_ok_button()->set_disabled(true);
  2220. resize_dialog->popup_centered(Size2(250, 0) * EDSCALE);
  2221. new_size_spin_box->get_line_edit()->grab_focus();
  2222. new_size_spin_box->get_line_edit()->select_all();
  2223. break;
  2224. default:
  2225. break;
  2226. }
  2227. }
  2228. void EditorInspectorArray::_control_dropping_draw() {
  2229. int drop_position = _drop_position();
  2230. if (dropping && drop_position >= 0) {
  2231. Vector2 from;
  2232. Vector2 to;
  2233. if (drop_position < elements_vbox->get_child_count()) {
  2234. Transform2D xform = Object::cast_to<Control>(elements_vbox->get_child(drop_position))->get_transform();
  2235. from = xform.xform(Vector2());
  2236. to = xform.xform(Vector2(elements_vbox->get_size().x, 0));
  2237. } else {
  2238. Control *child = Object::cast_to<Control>(elements_vbox->get_child(drop_position - 1));
  2239. Transform2D xform = child->get_transform();
  2240. from = xform.xform(Vector2(0, child->get_size().y));
  2241. to = xform.xform(Vector2(elements_vbox->get_size().x, child->get_size().y));
  2242. }
  2243. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  2244. control_dropping->draw_line(from, to, color, 2);
  2245. }
  2246. }
  2247. void EditorInspectorArray::_vbox_visibility_changed() {
  2248. control_dropping->set_visible(vbox->is_visible_in_tree());
  2249. }
  2250. void EditorInspectorArray::_panel_draw(int p_index) {
  2251. ERR_FAIL_INDEX(p_index, (int)array_elements.size());
  2252. Ref<StyleBox> style = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles));
  2253. if (style.is_null()) {
  2254. return;
  2255. }
  2256. if (array_elements[p_index].panel->has_focus(true)) {
  2257. array_elements[p_index].panel->draw_style_box(style, Rect2(Vector2(), array_elements[p_index].panel->get_size()));
  2258. }
  2259. }
  2260. void EditorInspectorArray::_panel_gui_focus(int p_index) {
  2261. array_elements[p_index].panel->queue_redraw();
  2262. selected = p_index;
  2263. }
  2264. void EditorInspectorArray::_panel_gui_unfocus(int p_index) {
  2265. array_elements[p_index].panel->queue_redraw();
  2266. if (selected == p_index) {
  2267. selected = -1;
  2268. }
  2269. }
  2270. void EditorInspectorArray::_panel_gui_input(Ref<InputEvent> p_event, int p_index) {
  2271. ERR_FAIL_INDEX(p_index, (int)array_elements.size());
  2272. if (read_only) {
  2273. return;
  2274. }
  2275. Ref<InputEventKey> key_ref = p_event;
  2276. if (key_ref.is_valid()) {
  2277. const InputEventKey &key = **key_ref;
  2278. if (array_elements[p_index].panel->has_focus() && key.is_pressed() && key.get_keycode() == Key::KEY_DELETE) {
  2279. _move_element(begin_array_index + p_index, -1);
  2280. array_elements[p_index].panel->accept_event();
  2281. }
  2282. }
  2283. Ref<InputEventMouseButton> mb = p_event;
  2284. if (mb.is_valid()) {
  2285. if (movable && mb->get_button_index() == MouseButton::RIGHT) {
  2286. array_elements[p_index].panel->accept_event();
  2287. popup_array_index_pressed = begin_array_index + p_index;
  2288. rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0);
  2289. rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1);
  2290. rmb_popup->set_position(array_elements[p_index].panel->get_screen_position() + mb->get_position());
  2291. rmb_popup->reset_size();
  2292. rmb_popup->popup();
  2293. }
  2294. }
  2295. }
  2296. void EditorInspectorArray::show_menu(int p_index, const Vector2 &p_offset) {
  2297. popup_array_index_pressed = begin_array_index + p_index;
  2298. rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0);
  2299. rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1);
  2300. rmb_popup->set_position(get_screen_position() + p_offset);
  2301. rmb_popup->reset_size();
  2302. rmb_popup->popup();
  2303. }
  2304. void EditorInspectorArray::_move_element(int p_element_index, int p_to_pos) {
  2305. String action_name;
  2306. if (p_element_index < 0) {
  2307. action_name = vformat(TTR("Add element to property array with prefix %s."), array_element_prefix);
  2308. } else if (p_to_pos < 0) {
  2309. action_name = vformat(TTR("Remove element %d from property array with prefix %s."), p_element_index, array_element_prefix);
  2310. } else {
  2311. action_name = vformat(TTR("Move element %d to position %d in property array with prefix %s."), p_element_index, p_to_pos, array_element_prefix);
  2312. }
  2313. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2314. undo_redo->create_action(action_name);
  2315. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2316. // Call the function.
  2317. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2318. if (move_function.is_valid()) {
  2319. move_function.call(undo_redo, object, array_element_prefix, p_element_index, p_to_pos);
  2320. } else {
  2321. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  2322. }
  2323. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2324. ERR_FAIL_COND(p_to_pos < -1 || p_to_pos > count);
  2325. if (!swap_method.is_empty()) {
  2326. ERR_FAIL_COND(!object->has_method(swap_method));
  2327. // Swap method was provided, use it.
  2328. if (p_element_index < 0) {
  2329. // Add an element at position
  2330. undo_redo->add_do_property(object, count_property, count + 1);
  2331. if (p_to_pos >= 0) {
  2332. for (int i = count; i > p_to_pos; i--) {
  2333. undo_redo->add_do_method(object, swap_method, i, i - 1);
  2334. }
  2335. for (int i = p_to_pos; i < count; i++) {
  2336. undo_redo->add_undo_method(object, swap_method, i, i + 1);
  2337. }
  2338. }
  2339. undo_redo->add_undo_property(object, count_property, count);
  2340. } else if (p_to_pos < 0) {
  2341. if (count > 0) {
  2342. // Remove element at position
  2343. undo_redo->add_undo_property(object, count_property, count);
  2344. List<PropertyInfo> object_property_list;
  2345. object->get_property_list(&object_property_list);
  2346. for (int i = p_element_index; i < count - 1; i++) {
  2347. undo_redo->add_do_method(object, swap_method, i, i + 1);
  2348. }
  2349. for (int i = count; i > p_element_index; i--) {
  2350. undo_redo->add_undo_method(object, swap_method, i, i - 1);
  2351. }
  2352. String erase_prefix = String(array_element_prefix) + itos(p_element_index);
  2353. for (const PropertyInfo &E : object_property_list) {
  2354. if (E.name.begins_with(erase_prefix)) {
  2355. undo_redo->add_undo_property(object, E.name, object->get(E.name));
  2356. }
  2357. }
  2358. undo_redo->add_do_property(object, count_property, count - 1);
  2359. }
  2360. } else {
  2361. if (p_to_pos > p_element_index) {
  2362. p_to_pos--;
  2363. }
  2364. if (p_to_pos < p_element_index) {
  2365. for (int i = p_element_index; i > p_to_pos; i--) {
  2366. undo_redo->add_do_method(object, swap_method, i, i - 1);
  2367. }
  2368. for (int i = p_to_pos; i < p_element_index; i++) {
  2369. undo_redo->add_undo_method(object, swap_method, i, i + 1);
  2370. }
  2371. } else if (p_to_pos > p_element_index) {
  2372. for (int i = p_element_index; i < p_to_pos; i++) {
  2373. undo_redo->add_do_method(object, swap_method, i, i + 1);
  2374. }
  2375. for (int i = p_to_pos; i > p_element_index; i--) {
  2376. undo_redo->add_undo_method(object, swap_method, i, i - 1);
  2377. }
  2378. }
  2379. }
  2380. } else {
  2381. // Use standard properties.
  2382. List<PropertyInfo> object_property_list;
  2383. object->get_property_list(&object_property_list);
  2384. Array properties_as_array = _extract_properties_as_array(object_property_list);
  2385. properties_as_array.resize(count);
  2386. // For undoing things
  2387. undo_redo->add_undo_property(object, count_property, properties_as_array.size());
  2388. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  2389. Dictionary d = Dictionary(properties_as_array[i]);
  2390. for (const KeyValue<Variant, Variant> &kv : d) {
  2391. undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
  2392. }
  2393. }
  2394. if (p_element_index < 0) {
  2395. // Add an element.
  2396. properties_as_array.insert(p_to_pos < 0 ? properties_as_array.size() : p_to_pos, Dictionary());
  2397. } else if (p_to_pos < 0) {
  2398. // Delete the element.
  2399. properties_as_array.remove_at(p_element_index);
  2400. } else {
  2401. // Move the element.
  2402. properties_as_array.insert(p_to_pos, properties_as_array[p_element_index].duplicate());
  2403. properties_as_array.remove_at(p_to_pos < p_element_index ? p_element_index + 1 : p_element_index);
  2404. }
  2405. // Change the array size then set the properties.
  2406. undo_redo->add_do_property(object, count_property, properties_as_array.size());
  2407. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  2408. Dictionary d = properties_as_array[i];
  2409. for (const KeyValue<Variant, Variant> &kv : d) {
  2410. undo_redo->add_do_property(object, vformat(kv.key, i), kv.value);
  2411. }
  2412. }
  2413. }
  2414. }
  2415. undo_redo->commit_action();
  2416. // Handle page change and update counts.
  2417. if (p_element_index < 0) {
  2418. int added_index = p_to_pos < 0 ? count : p_to_pos;
  2419. emit_signal(SNAME("page_change_request"), added_index / page_length);
  2420. count += 1;
  2421. } else if (p_to_pos < 0) {
  2422. count -= 1;
  2423. if (page == max_page && (MAX(0, count - 1) / page_length != max_page)) {
  2424. emit_signal(SNAME("page_change_request"), max_page - 1);
  2425. }
  2426. } else if (p_to_pos == begin_array_index - 1) {
  2427. emit_signal(SNAME("page_change_request"), page - 1);
  2428. } else if (p_to_pos > end_array_index) {
  2429. emit_signal(SNAME("page_change_request"), page + 1);
  2430. }
  2431. begin_array_index = page * page_length;
  2432. end_array_index = MIN(count, (page + 1) * page_length);
  2433. max_page = MAX(0, count - 1) / page_length;
  2434. }
  2435. void EditorInspectorArray::_clear_array() {
  2436. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2437. undo_redo->create_action(vformat(TTR("Clear Property Array with Prefix %s"), array_element_prefix));
  2438. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2439. for (int i = count - 1; i >= 0; i--) {
  2440. // Call the function.
  2441. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2442. if (move_function.is_valid()) {
  2443. move_function.call(undo_redo, object, array_element_prefix, i, -1);
  2444. } else {
  2445. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  2446. }
  2447. }
  2448. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2449. List<PropertyInfo> object_property_list;
  2450. object->get_property_list(&object_property_list);
  2451. Array properties_as_array = _extract_properties_as_array(object_property_list);
  2452. properties_as_array.resize(count);
  2453. // For undoing things
  2454. undo_redo->add_undo_property(object, count_property, count);
  2455. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  2456. Dictionary d = Dictionary(properties_as_array[i]);
  2457. for (const KeyValue<Variant, Variant> &kv : d) {
  2458. undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
  2459. }
  2460. }
  2461. // Change the array size then set the properties.
  2462. undo_redo->add_do_property(object, count_property, 0);
  2463. }
  2464. undo_redo->commit_action();
  2465. // Handle page change and update counts.
  2466. emit_signal(SNAME("page_change_request"), 0);
  2467. count = 0;
  2468. begin_array_index = 0;
  2469. end_array_index = 0;
  2470. max_page = 0;
  2471. }
  2472. void EditorInspectorArray::_resize_array(int p_size) {
  2473. ERR_FAIL_COND(p_size < 0);
  2474. if (p_size == count) {
  2475. return;
  2476. }
  2477. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2478. undo_redo->create_action(vformat(TTR("Resize Property Array with Prefix %s"), array_element_prefix));
  2479. if (p_size > count) {
  2480. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2481. for (int i = count; i < p_size; i++) {
  2482. // Call the function.
  2483. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2484. if (move_function.is_valid()) {
  2485. move_function.call(undo_redo, object, array_element_prefix, -1, -1);
  2486. } else {
  2487. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  2488. }
  2489. }
  2490. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2491. undo_redo->add_undo_property(object, count_property, count);
  2492. undo_redo->add_do_property(object, count_property, p_size);
  2493. }
  2494. } else {
  2495. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2496. for (int i = count - 1; i > p_size - 1; i--) {
  2497. // Call the function.
  2498. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2499. if (move_function.is_valid()) {
  2500. move_function.call(undo_redo, object, array_element_prefix, i, -1);
  2501. } else {
  2502. WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
  2503. }
  2504. }
  2505. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2506. List<PropertyInfo> object_property_list;
  2507. object->get_property_list(&object_property_list);
  2508. Array properties_as_array = _extract_properties_as_array(object_property_list);
  2509. properties_as_array.resize(count);
  2510. // For undoing things
  2511. undo_redo->add_undo_property(object, count_property, count);
  2512. for (int i = count - 1; i > p_size - 1; i--) {
  2513. Dictionary d = Dictionary(properties_as_array[i]);
  2514. for (const KeyValue<Variant, Variant> &kv : d) {
  2515. undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
  2516. }
  2517. }
  2518. // Change the array size then set the properties.
  2519. undo_redo->add_do_property(object, count_property, p_size);
  2520. }
  2521. }
  2522. undo_redo->commit_action();
  2523. // Handle page change and update counts.
  2524. emit_signal(SNAME("page_change_request"), 0);
  2525. /*
  2526. count = 0;
  2527. begin_array_index = 0;
  2528. end_array_index = 0;
  2529. max_page = 0;
  2530. */
  2531. }
  2532. Array EditorInspectorArray::_extract_properties_as_array(const List<PropertyInfo> &p_list) {
  2533. Array output;
  2534. for (const PropertyInfo &pi : p_list) {
  2535. if (!(pi.usage & PROPERTY_USAGE_EDITOR)) {
  2536. continue;
  2537. }
  2538. if (pi.name.begins_with(array_element_prefix)) {
  2539. String str = pi.name.trim_prefix(array_element_prefix);
  2540. int to_char_index = 0;
  2541. while (to_char_index < str.length()) {
  2542. if (!is_digit(str[to_char_index])) {
  2543. break;
  2544. }
  2545. to_char_index++;
  2546. }
  2547. if (to_char_index > 0) {
  2548. int array_index = str.left(to_char_index).to_int();
  2549. Error error = OK;
  2550. if (array_index >= output.size()) {
  2551. error = output.resize(array_index + 1);
  2552. }
  2553. if (error == OK) {
  2554. String format_string = String(array_element_prefix) + "%d" + str.substr(to_char_index);
  2555. Dictionary dict = output[array_index];
  2556. dict[format_string] = object->get(pi.name);
  2557. output[array_index] = dict;
  2558. } else {
  2559. WARN_PRINT(vformat("Array element %s has an index too high. Array allocation failed.", pi.name));
  2560. }
  2561. }
  2562. }
  2563. }
  2564. return output;
  2565. }
  2566. int EditorInspectorArray::_drop_position() const {
  2567. for (int i = 0; i < (int)array_elements.size(); i++) {
  2568. const ArrayElement &ae = array_elements[i];
  2569. Size2 size = ae.panel->get_size();
  2570. Vector2 mp = ae.panel->get_local_mouse_position();
  2571. if (Rect2(Vector2(), size).has_point(mp)) {
  2572. if (mp.y < size.y / 2) {
  2573. return i;
  2574. } else {
  2575. return i + 1;
  2576. }
  2577. }
  2578. }
  2579. return -1;
  2580. }
  2581. void EditorInspectorArray::_resize_dialog_confirmed() {
  2582. if (int(new_size_spin_box->get_value()) == count) {
  2583. return;
  2584. }
  2585. resize_dialog->hide();
  2586. _resize_array(int(new_size_spin_box->get_value()));
  2587. }
  2588. void EditorInspectorArray::_new_size_spin_box_value_changed(float p_value) {
  2589. resize_dialog->get_ok_button()->set_disabled(int(p_value) == count);
  2590. }
  2591. void EditorInspectorArray::_new_size_spin_box_text_submitted(const String &p_text) {
  2592. _resize_dialog_confirmed();
  2593. }
  2594. void EditorInspectorArray::_setup() {
  2595. // Setup counts.
  2596. count = _get_array_count();
  2597. begin_array_index = page * page_length;
  2598. end_array_index = MIN(count, (page + 1) * page_length);
  2599. max_page = MAX(0, count - 1) / page_length;
  2600. array_elements.resize(MAX(0, end_array_index - begin_array_index));
  2601. if (page < 0 || page > max_page) {
  2602. WARN_PRINT(vformat("Invalid page number %d", page));
  2603. page = CLAMP(page, 0, max_page);
  2604. }
  2605. Ref<Font> numbers_font;
  2606. int numbers_min_w = 0;
  2607. bool unresizable = is_const || read_only;
  2608. if (numbered) {
  2609. numbers_font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  2610. int digits_found = count;
  2611. String test;
  2612. while (digits_found) {
  2613. test += "8";
  2614. digits_found /= 10;
  2615. }
  2616. numbers_min_w = numbers_font->get_string_size(test).width;
  2617. }
  2618. for (int i = 0; i < (int)array_elements.size(); i++) {
  2619. ArrayElement &ae = array_elements[i];
  2620. // Panel and its hbox.
  2621. ae.panel = memnew(ArrayPanelContainer);
  2622. ae.panel->set_focus_mode(FOCUS_ALL);
  2623. ae.panel->set_mouse_filter(MOUSE_FILTER_PASS);
  2624. SET_DRAG_FORWARDING_GCD(ae.panel, EditorInspectorArray);
  2625. int element_position = begin_array_index + i;
  2626. String ae_name = vformat(TTR("Element %d: %s%d*"), element_position, array_element_prefix, element_position);
  2627. ae.panel->set_meta("index", element_position);
  2628. ae.panel->set_meta("name", ae_name);
  2629. ae.panel->set_meta("element", this);
  2630. ae.panel->set_tooltip_text(ae_name);
  2631. ae.panel->connect(SceneStringName(focus_entered), callable_mp(this, &EditorInspectorArray::_panel_gui_focus).bind(i));
  2632. ae.panel->connect(SceneStringName(focus_exited), callable_mp(this, &EditorInspectorArray::_panel_gui_unfocus).bind(i));
  2633. ae.panel->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_panel_draw).bind(i));
  2634. ae.panel->connect(SceneStringName(gui_input), callable_mp(this, &EditorInspectorArray::_panel_gui_input).bind(i));
  2635. ae.panel->add_theme_style_override(SceneStringName(panel), i % 2 ? odd_style : even_style);
  2636. elements_vbox->add_child(ae.panel);
  2637. ae.margin = memnew(MarginContainer);
  2638. ae.margin->set_mouse_filter(MOUSE_FILTER_PASS);
  2639. if (is_inside_tree()) {
  2640. Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
  2641. ae.margin->begin_bulk_theme_override();
  2642. ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
  2643. ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
  2644. ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
  2645. ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
  2646. ae.margin->end_bulk_theme_override();
  2647. }
  2648. ae.panel->add_child(ae.margin);
  2649. ae.hbox = memnew(HBoxContainer);
  2650. ae.hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2651. ae.hbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2652. ae.margin->add_child(ae.hbox);
  2653. // Move button.
  2654. if (movable) {
  2655. VBoxContainer *move_vbox = memnew(VBoxContainer);
  2656. move_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2657. move_vbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  2658. ae.hbox->add_child(move_vbox);
  2659. if (element_position > 0) {
  2660. ae.move_up = memnew(Button);
  2661. ae.move_up->set_accessibility_name(TTRC("Move Up"));
  2662. ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  2663. ae.move_up->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position - 1));
  2664. move_vbox->add_child(ae.move_up);
  2665. }
  2666. ae.move_texture_rect = memnew(TextureRect);
  2667. ae.move_texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  2668. ae.move_texture_rect->set_default_cursor_shape(Control::CURSOR_MOVE);
  2669. if (is_inside_tree()) {
  2670. ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
  2671. }
  2672. move_vbox->add_child(ae.move_texture_rect);
  2673. if (element_position < count - 1) {
  2674. ae.move_down = memnew(Button);
  2675. ae.move_down->set_accessibility_name(TTRC("Move Down"));
  2676. ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  2677. ae.move_down->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position + 2));
  2678. move_vbox->add_child(ae.move_down);
  2679. }
  2680. }
  2681. if (numbered) {
  2682. ae.number = memnew(Label);
  2683. ae.number->add_theme_font_override(SceneStringName(font), numbers_font);
  2684. ae.number->set_custom_minimum_size(Size2(numbers_min_w, 0));
  2685. ae.number->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  2686. ae.number->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  2687. ae.number->set_text(itos(element_position));
  2688. ae.hbox->add_child(ae.number);
  2689. }
  2690. // Right vbox.
  2691. ae.vbox = memnew(VBoxContainer);
  2692. ae.vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2693. ae.vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2694. ae.hbox->add_child(ae.vbox);
  2695. if (!unresizable) {
  2696. ae.erase = memnew(Button);
  2697. ae.erase->set_accessibility_name(TTRC("Remove"));
  2698. ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2699. ae.erase->set_v_size_flags(SIZE_SHRINK_CENTER);
  2700. ae.erase->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_remove_item).bind(element_position));
  2701. ae.hbox->add_child(ae.erase);
  2702. }
  2703. }
  2704. // Hide/show the add button.
  2705. add_button->set_visible(page == max_page && !unresizable);
  2706. // Add paginator if there's more than 1 page.
  2707. if (max_page > 0) {
  2708. EditorPaginator *paginator = memnew(EditorPaginator);
  2709. paginator->update(page, max_page);
  2710. paginator->connect("page_changed", callable_mp(this, &EditorInspectorArray::_paginator_page_changed));
  2711. vbox->add_child(paginator);
  2712. }
  2713. }
  2714. void EditorInspectorArray::_remove_item(int p_index) {
  2715. _move_element(p_index, -1);
  2716. }
  2717. Variant EditorInspectorArray::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2718. if (!movable) {
  2719. return Variant();
  2720. }
  2721. int index = p_from->get_meta("index");
  2722. Dictionary dict;
  2723. dict["type"] = "property_array_element";
  2724. dict["property_array_prefix"] = array_element_prefix;
  2725. dict["index"] = index;
  2726. return dict;
  2727. }
  2728. void EditorInspectorArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2729. Dictionary dict = p_data;
  2730. int to_drop = dict["index"];
  2731. int drop_position = (p_point == Vector2(Math::INF, Math::INF)) ? selected : _drop_position();
  2732. if (drop_position < 0) {
  2733. return;
  2734. }
  2735. _move_element(to_drop, begin_array_index + drop_position);
  2736. }
  2737. bool EditorInspectorArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2738. if (!movable || read_only) {
  2739. return false;
  2740. }
  2741. // First, update drawing.
  2742. control_dropping->queue_redraw();
  2743. if (p_data.get_type() != Variant::DICTIONARY) {
  2744. return false;
  2745. }
  2746. Dictionary dict = p_data;
  2747. int drop_position = (p_point == Vector2(Math::INF, Math::INF)) ? selected : _drop_position();
  2748. if (!dict.has("type") || dict["type"] != "property_array_element" || String(dict["property_array_prefix"]) != array_element_prefix || drop_position < 0) {
  2749. return false;
  2750. }
  2751. // Check in dropping at the given index does indeed move the item.
  2752. int moved_array_index = (int)dict["index"];
  2753. int drop_array_index = begin_array_index + drop_position;
  2754. return drop_array_index != moved_array_index && drop_array_index - 1 != moved_array_index;
  2755. }
  2756. void ArrayPanelContainer::_accessibility_action_menu(const Variant &p_data) {
  2757. EditorInspectorArray *el = Object::cast_to<EditorInspectorArray>(get_meta("element"));
  2758. if (el) {
  2759. int index = get_meta("index");
  2760. el->show_menu(index, Vector2());
  2761. }
  2762. }
  2763. void ArrayPanelContainer::_notification(int p_what) {
  2764. switch (p_what) {
  2765. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  2766. RID ae = get_accessibility_element();
  2767. ERR_FAIL_COND(ae.is_null());
  2768. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  2769. DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_meta("text"));
  2770. DisplayServer::get_singleton()->accessibility_update_set_value(ae, get_meta("text"));
  2771. DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
  2772. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &ArrayPanelContainer::_accessibility_action_menu));
  2773. } break;
  2774. }
  2775. }
  2776. ArrayPanelContainer::ArrayPanelContainer() {
  2777. set_focus_mode(FOCUS_ACCESSIBILITY);
  2778. }
  2779. void EditorInspectorArray::_notification(int p_what) {
  2780. switch (p_what) {
  2781. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  2782. RID ae = get_accessibility_element();
  2783. ERR_FAIL_COND(ae.is_null());
  2784. DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Array: %s"), get_label()));
  2785. DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Array: %s"), get_label()));
  2786. } break;
  2787. case NOTIFICATION_THEME_CHANGED: {
  2788. Color color = get_theme_color(SNAME("bg"), SNAME("EditorInspectorArray"));
  2789. odd_style->set_bg_color(color.darkened(-0.1));
  2790. even_style->set_bg_color(color.darkened(0.1));
  2791. for (ArrayElement &ae : array_elements) {
  2792. if (ae.move_texture_rect) {
  2793. ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
  2794. }
  2795. if (ae.move_up) {
  2796. ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  2797. }
  2798. if (ae.move_down) {
  2799. ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  2800. }
  2801. Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
  2802. ae.margin->begin_bulk_theme_override();
  2803. ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
  2804. ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
  2805. ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
  2806. ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
  2807. ae.margin->end_bulk_theme_override();
  2808. if (ae.erase) {
  2809. ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2810. }
  2811. }
  2812. } break;
  2813. case NOTIFICATION_DRAG_BEGIN: {
  2814. Dictionary dict = get_viewport()->gui_get_drag_data();
  2815. if (dict.has("type") && dict["type"] == "property_array_element" && String(dict["property_array_prefix"]) == array_element_prefix) {
  2816. dropping = true;
  2817. control_dropping->queue_redraw();
  2818. }
  2819. } break;
  2820. case NOTIFICATION_DRAG_END: {
  2821. if (dropping) {
  2822. dropping = false;
  2823. control_dropping->queue_redraw();
  2824. }
  2825. } break;
  2826. }
  2827. }
  2828. void EditorInspectorArray::_bind_methods() {
  2829. ADD_SIGNAL(MethodInfo("page_change_request"));
  2830. }
  2831. void EditorInspectorArray::setup_with_move_element_function(Object *p_object, const String &p_label, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable, bool p_is_const, bool p_numbered, int p_page_length, const String &p_add_item_text) {
  2832. count_property = "";
  2833. mode = MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION;
  2834. array_element_prefix = p_array_element_prefix;
  2835. page = p_page;
  2836. movable = p_movable;
  2837. is_const = p_is_const;
  2838. page_length = p_page_length;
  2839. numbered = p_numbered;
  2840. EditorInspectorSection::setup(String(p_array_element_prefix) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
  2841. _setup();
  2842. }
  2843. void EditorInspectorArray::setup_with_count_property(Object *p_object, const String &p_label, const StringName &p_count_property, const StringName &p_array_element_prefix, int p_page, const Color &p_bg_color, bool p_foldable, bool p_movable, bool p_is_const, bool p_numbered, int p_page_length, const String &p_add_item_text, const String &p_swap_method) {
  2844. count_property = p_count_property;
  2845. mode = MODE_USE_COUNT_PROPERTY;
  2846. array_element_prefix = p_array_element_prefix;
  2847. page = p_page;
  2848. movable = p_movable;
  2849. is_const = p_is_const;
  2850. page_length = p_page_length;
  2851. numbered = p_numbered;
  2852. swap_method = p_swap_method;
  2853. add_button->set_text(p_add_item_text);
  2854. EditorInspectorSection::setup(String(count_property) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
  2855. _setup();
  2856. }
  2857. VBoxContainer *EditorInspectorArray::get_vbox(int p_index) {
  2858. if (p_index >= begin_array_index && p_index < end_array_index) {
  2859. return array_elements[p_index - begin_array_index].vbox;
  2860. } else if (p_index < 0) {
  2861. return vbox;
  2862. } else {
  2863. return nullptr;
  2864. }
  2865. }
  2866. EditorInspectorArray::EditorInspectorArray(bool p_read_only) {
  2867. read_only = p_read_only;
  2868. odd_style.instantiate();
  2869. even_style.instantiate();
  2870. rmb_popup = memnew(PopupMenu);
  2871. rmb_popup->set_accessibility_name(TTRC("Move"));
  2872. rmb_popup->add_item(TTR("Move Up"), OPTION_MOVE_UP);
  2873. rmb_popup->add_item(TTR("Move Down"), OPTION_MOVE_DOWN);
  2874. rmb_popup->add_separator();
  2875. rmb_popup->add_item(TTR("Insert New Before"), OPTION_NEW_BEFORE);
  2876. rmb_popup->add_item(TTR("Insert New After"), OPTION_NEW_AFTER);
  2877. rmb_popup->add_separator();
  2878. rmb_popup->add_item(TTR("Remove"), OPTION_REMOVE);
  2879. rmb_popup->add_separator();
  2880. rmb_popup->add_item(TTR("Clear Array"), OPTION_CLEAR_ARRAY);
  2881. rmb_popup->add_item(TTR("Resize Array..."), OPTION_RESIZE_ARRAY);
  2882. rmb_popup->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorArray::_rmb_popup_id_pressed));
  2883. add_child(rmb_popup);
  2884. elements_vbox = memnew(VBoxContainer);
  2885. elements_vbox->add_theme_constant_override("separation", 0);
  2886. vbox->add_child(elements_vbox);
  2887. add_button = memnew(EditorInspectorActionButton(TTRC("Add Element"), SNAME("Add")));
  2888. add_button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_add_button_pressed));
  2889. add_button->set_disabled(read_only);
  2890. vbox->add_child(add_button);
  2891. control_dropping = memnew(Control);
  2892. control_dropping->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_control_dropping_draw));
  2893. control_dropping->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  2894. add_child(control_dropping);
  2895. resize_dialog = memnew(AcceptDialog);
  2896. resize_dialog->set_title(TTRC("Resize Array"));
  2897. resize_dialog->add_cancel_button();
  2898. resize_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspectorArray::_resize_dialog_confirmed));
  2899. add_child(resize_dialog);
  2900. VBoxContainer *resize_dialog_vbox = memnew(VBoxContainer);
  2901. resize_dialog->add_child(resize_dialog_vbox);
  2902. new_size_spin_box = memnew(SpinBox);
  2903. new_size_spin_box->set_accessibility_name(TTRC("New Size:"));
  2904. new_size_spin_box->set_max(16384);
  2905. new_size_spin_box->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_value_changed));
  2906. new_size_spin_box->get_line_edit()->connect(SceneStringName(text_submitted), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_text_submitted));
  2907. new_size_spin_box->set_editable(!read_only);
  2908. resize_dialog_vbox->add_margin_child(TTRC("New Size:"), new_size_spin_box);
  2909. vbox->connect(SceneStringName(visibility_changed), callable_mp(this, &EditorInspectorArray::_vbox_visibility_changed));
  2910. }
  2911. ////////////////////////////////////////////////
  2912. ////////////////////////////////////////////////
  2913. void EditorPaginator::_first_page_button_pressed() {
  2914. emit_signal("page_changed", 0);
  2915. }
  2916. void EditorPaginator::_prev_page_button_pressed() {
  2917. emit_signal("page_changed", MAX(0, page - 1));
  2918. }
  2919. void EditorPaginator::_page_line_edit_text_submitted(const String &p_text) {
  2920. if (p_text.is_valid_int()) {
  2921. int new_page = p_text.to_int() - 1;
  2922. new_page = MIN(MAX(0, new_page), max_page);
  2923. page_line_edit->set_text(Variant(new_page));
  2924. emit_signal("page_changed", new_page);
  2925. } else {
  2926. page_line_edit->set_text(Variant(page));
  2927. }
  2928. }
  2929. void EditorPaginator::_next_page_button_pressed() {
  2930. emit_signal("page_changed", MIN(max_page, page + 1));
  2931. }
  2932. void EditorPaginator::_last_page_button_pressed() {
  2933. emit_signal("page_changed", max_page);
  2934. }
  2935. void EditorPaginator::update(int p_page, int p_max_page) {
  2936. page = p_page;
  2937. max_page = p_max_page;
  2938. // Update buttons.
  2939. first_page_button->set_disabled(page == 0);
  2940. prev_page_button->set_disabled(page == 0);
  2941. next_page_button->set_disabled(page == max_page);
  2942. last_page_button->set_disabled(page == max_page);
  2943. // Update page number and page count.
  2944. page_line_edit->set_text(vformat("%d", page + 1));
  2945. page_count_label->set_text(vformat("/ %d", max_page + 1));
  2946. }
  2947. void EditorPaginator::_notification(int p_what) {
  2948. switch (p_what) {
  2949. case NOTIFICATION_THEME_CHANGED: {
  2950. first_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageFirst")));
  2951. prev_page_button->set_button_icon(get_editor_theme_icon(SNAME("PagePrevious")));
  2952. next_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageNext")));
  2953. last_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageLast")));
  2954. } break;
  2955. }
  2956. }
  2957. void EditorPaginator::_bind_methods() {
  2958. ADD_SIGNAL(MethodInfo("page_changed", PropertyInfo(Variant::INT, "page")));
  2959. }
  2960. EditorPaginator::EditorPaginator() {
  2961. set_h_size_flags(SIZE_EXPAND_FILL);
  2962. set_alignment(ALIGNMENT_CENTER);
  2963. first_page_button = memnew(Button);
  2964. first_page_button->set_accessibility_name(TTRC("First Page"));
  2965. first_page_button->set_flat(true);
  2966. first_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_first_page_button_pressed));
  2967. add_child(first_page_button);
  2968. prev_page_button = memnew(Button);
  2969. prev_page_button->set_accessibility_name(TTRC("Previous Page"));
  2970. prev_page_button->set_flat(true);
  2971. prev_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_prev_page_button_pressed));
  2972. add_child(prev_page_button);
  2973. page_line_edit = memnew(LineEdit);
  2974. page_line_edit->set_accessibility_name(TTRC("Page Number"));
  2975. page_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPaginator::_page_line_edit_text_submitted));
  2976. page_line_edit->add_theme_constant_override("minimum_character_width", 2);
  2977. add_child(page_line_edit);
  2978. page_count_label = memnew(Label);
  2979. page_count_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  2980. add_child(page_count_label);
  2981. next_page_button = memnew(Button);
  2982. prev_page_button->set_accessibility_name(TTRC("Next Page"));
  2983. next_page_button->set_flat(true);
  2984. next_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_next_page_button_pressed));
  2985. add_child(next_page_button);
  2986. last_page_button = memnew(Button);
  2987. last_page_button->set_accessibility_name(TTRC("Last Page"));
  2988. last_page_button->set_flat(true);
  2989. last_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_last_page_button_pressed));
  2990. add_child(last_page_button);
  2991. }
  2992. ////////////////////////////////////////////////
  2993. ////////////////////////////////////////////////
  2994. Ref<EditorInspectorPlugin> EditorInspector::inspector_plugins[MAX_PLUGINS];
  2995. int EditorInspector::inspector_plugin_count = 0;
  2996. EditorProperty *EditorInspector::instantiate_property_editor(Object *p_object, const Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) {
  2997. for (int i = inspector_plugin_count - 1; i >= 0; i--) {
  2998. if (!inspector_plugins[i]->can_handle(p_object)) {
  2999. continue;
  3000. }
  3001. inspector_plugins[i]->parse_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide);
  3002. if (inspector_plugins[i]->added_editors.size()) {
  3003. for (List<EditorInspectorPlugin::AddedEditor>::Element *E = inspector_plugins[i]->added_editors.front()->next(); E; E = E->next()) { //only keep first one
  3004. memdelete(E->get().property_editor);
  3005. }
  3006. EditorProperty *prop = Object::cast_to<EditorProperty>(inspector_plugins[i]->added_editors.front()->get().property_editor);
  3007. if (prop) {
  3008. inspector_plugins[i]->added_editors.clear();
  3009. return prop;
  3010. } else {
  3011. memdelete(inspector_plugins[i]->added_editors.front()->get().property_editor);
  3012. inspector_plugins[i]->added_editors.clear();
  3013. }
  3014. }
  3015. }
  3016. return nullptr;
  3017. }
  3018. void EditorInspector::initialize_section_theme(EditorInspectorSection::ThemeCache &p_cache, Control *p_control) {
  3019. EditorInspector *parent_inspector = _get_control_parent_inspector(p_control);
  3020. if (parent_inspector && parent_inspector != p_control) {
  3021. p_cache = parent_inspector->section_theme_cache;
  3022. return;
  3023. }
  3024. p_cache.horizontal_separation = p_control->get_theme_constant(SNAME("h_separation"), SNAME("EditorInspectorSection"));
  3025. p_cache.vertical_separation = p_control->get_theme_constant(SNAME("separation"), SNAME("EditorPropertyContainer"));
  3026. p_cache.inspector_margin = p_control->get_theme_constant(SNAME("inspector_margin"), EditorStringName(Editor));
  3027. p_cache.indent_size = p_control->get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
  3028. p_cache.key_padding_size = int(EDITOR_GET("interface/theme/base_spacing")) * 2;
  3029. p_cache.warning_color = p_control->get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  3030. p_cache.prop_subsection = p_control->get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
  3031. p_cache.font_color = p_control->get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
  3032. p_cache.font_disabled_color = p_control->get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
  3033. p_cache.font_hover_color = p_control->get_theme_color(SNAME("font_hover_color"), EditorStringName(Editor));
  3034. p_cache.font_pressed_color = p_control->get_theme_color(SNAME("font_pressed_color"), EditorStringName(Editor));
  3035. p_cache.font_hover_pressed_color = p_control->get_theme_color(SNAME("font_hover_pressed_color"), EditorStringName(Editor));
  3036. p_cache.font_hover_mono_color = p_control->get_theme_color(SNAME("mono_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.925);
  3037. p_cache.font = p_control->get_theme_font(SceneStringName(font), SNAME("Tree"));
  3038. p_cache.font_size = p_control->get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
  3039. p_cache.bold_font = p_control->get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  3040. p_cache.bold_font_size = p_control->get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  3041. p_cache.light_font = p_control->get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  3042. p_cache.light_font_size = p_control->get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  3043. p_cache.arrow = p_control->get_theme_icon(SNAME("arrow"), SNAME("Tree"));
  3044. p_cache.arrow_collapsed = p_control->get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));
  3045. p_cache.arrow_collapsed_mirrored = p_control->get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));
  3046. p_cache.icon_gui_checked = p_control->get_editor_theme_icon(SNAME("GuiChecked"));
  3047. p_cache.icon_gui_unchecked = p_control->get_editor_theme_icon(SNAME("GuiUnchecked"));
  3048. p_cache.icon_gui_animation_key = p_control->get_editor_theme_icon(SNAME("Key"));
  3049. p_cache.indent_box = p_control->get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
  3050. p_cache.key_hover = p_control->get_theme_stylebox(SceneStringName(hover), SceneStringName(FlatButton));
  3051. }
  3052. void EditorInspector::initialize_category_theme(EditorInspectorCategory::ThemeCache &p_cache, Control *p_control) {
  3053. EditorInspector *parent_inspector = _get_control_parent_inspector(p_control);
  3054. if (parent_inspector && parent_inspector != p_control) {
  3055. p_cache = parent_inspector->category_theme_cache;
  3056. return;
  3057. }
  3058. p_cache.horizontal_separation = p_control->get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  3059. p_cache.vertical_separation = p_control->get_theme_constant(SNAME("separation"), SNAME("EditorPropertyContainer"));
  3060. p_cache.class_icon_size = p_control->get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  3061. p_cache.font_color = p_control->get_theme_color(SceneStringName(font_color), SNAME("Tree"));
  3062. p_cache.bold_font = p_control->get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  3063. p_cache.bold_font_size = p_control->get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  3064. p_cache.icon_favorites = p_control->get_editor_theme_icon(SNAME("Favorites"));
  3065. p_cache.icon_unfavorite = p_control->get_editor_theme_icon(SNAME("Unfavorite"));
  3066. p_cache.icon_help = p_control->get_editor_theme_icon(SNAME("Help"));
  3067. p_cache.background = p_control->get_theme_stylebox(SNAME("bg"), SNAME("EditorInspectorCategory"));
  3068. }
  3069. void EditorInspector::initialize_property_theme(EditorProperty::ThemeCache &p_cache, Control *p_control) {
  3070. EditorInspector *parent_inspector = _get_control_parent_inspector(p_control);
  3071. if (parent_inspector && parent_inspector != p_control) {
  3072. p_cache = parent_inspector->property_theme_cache;
  3073. return;
  3074. }
  3075. p_cache.font = p_control->get_theme_font(SceneStringName(font), SNAME("Tree"));
  3076. p_cache.background = p_control->get_theme_stylebox(SNAME("bg"), SNAME("EditorProperty"));
  3077. p_cache.background_selected = p_control->get_theme_stylebox(SNAME("bg_selected"), SNAME("EditorProperty"));
  3078. p_cache.child_background = p_control->get_theme_stylebox(SNAME("child_bg"), SNAME("EditorProperty"));
  3079. p_cache.hover = p_control->get_theme_stylebox(SceneStringName(hover), SceneStringName(FlatButton));
  3080. p_cache.key_icon = p_control->get_editor_theme_icon(SNAME("Key"));
  3081. p_cache.key_next_icon = p_control->get_editor_theme_icon(SNAME("KeyNext"));
  3082. p_cache.delete_icon = p_control->get_editor_theme_icon(SNAME("Close"));
  3083. p_cache.checked_icon = p_control->get_editor_theme_icon(SNAME("GuiChecked"));
  3084. p_cache.unchecked_icon = p_control->get_editor_theme_icon(SNAME("GuiUnchecked"));
  3085. p_cache.revert_icon = p_control->get_editor_theme_icon(SNAME("ReloadSmall"));
  3086. p_cache.pin_icon = p_control->get_editor_theme_icon(SNAME("Pin"));
  3087. p_cache.copy_icon = p_control->get_editor_theme_icon(SNAME("ActionCopy"));
  3088. p_cache.copy_node_path_icon = p_control->get_editor_theme_icon(SNAME("CopyNodePath"));
  3089. p_cache.paste_icon = p_control->get_editor_theme_icon(SNAME("ActionPaste"));
  3090. p_cache.unfavorite_icon = p_control->get_editor_theme_icon(SNAME("Unfavorite"));
  3091. p_cache.favorite_icon = p_control->get_editor_theme_icon(SNAME("Favorites"));
  3092. p_cache.override_icon = p_control->get_editor_theme_icon(SNAME("Override"));
  3093. p_cache.remove_icon = p_control->get_editor_theme_icon(SNAME("Remove"));
  3094. p_cache.help_icon = p_control->get_editor_theme_icon(SNAME("Help"));
  3095. p_cache.font_size = p_control->get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
  3096. p_cache.font_offset = p_control->get_theme_constant(SNAME("font_offset"), SNAME("EditorProperty"));
  3097. p_cache.horizontal_separation = p_control->get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  3098. p_cache.vertical_separation = p_control->get_theme_constant(SNAME("separation"), SNAME("EditorPropertyContainer"));
  3099. p_cache.padding = int(EDITOR_GET("interface/theme/base_spacing")) * 2;
  3100. p_cache.inspector_property_height = p_control->get_theme_constant(SNAME("inspector_property_height"), EditorStringName(Editor));
  3101. p_cache.property_color = p_control->get_theme_color(SNAME("property_color"), SNAME("EditorProperty"));
  3102. p_cache.readonly_property_color = p_control->get_theme_color(SNAME("readonly_color"), SNAME("EditorProperty"));
  3103. p_cache.warning_color = p_control->get_theme_color(SNAME("warning_color"), SNAME("EditorProperty"));
  3104. p_cache.readonly_warning_color = p_control->get_theme_color(SNAME("readonly_warning_color"), SNAME("EditorProperty"));
  3105. p_cache.property_color_x = p_control->get_theme_color(SNAME("property_color_x"), EditorStringName(Editor));
  3106. p_cache.property_color_y = p_control->get_theme_color(SNAME("property_color_y"), EditorStringName(Editor));
  3107. p_cache.property_color_z = p_control->get_theme_color(SNAME("property_color_z"), EditorStringName(Editor));
  3108. p_cache.property_color_w = p_control->get_theme_color(SNAME("property_color_w"), EditorStringName(Editor));
  3109. p_cache.sub_inspector_property_color = p_control->get_theme_color(SNAME("sub_inspector_property_color"), EditorStringName(EditorStyles));
  3110. if (p_control == parent_inspector) {
  3111. // Only initialize for the inspector, as stand-alone properties won't need it.
  3112. for (int i = 0; i <= 16; i++) {
  3113. p_cache.sub_inspector_background[i] = p_control->get_theme_stylebox("sub_inspector_property_bg" + itos(i), EditorStringName(EditorStyles));
  3114. }
  3115. }
  3116. }
  3117. void EditorInspector::add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  3118. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  3119. for (int i = 0; i < inspector_plugin_count; i++) {
  3120. if (inspector_plugins[i] == p_plugin) {
  3121. return; //already exists
  3122. }
  3123. }
  3124. inspector_plugins[inspector_plugin_count++] = p_plugin;
  3125. }
  3126. void EditorInspector::remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  3127. int idx = -1;
  3128. for (int i = 0; i < inspector_plugin_count; i++) {
  3129. if (inspector_plugins[i] == p_plugin) {
  3130. idx = i;
  3131. break;
  3132. }
  3133. }
  3134. ERR_FAIL_COND_MSG(idx == -1, "Trying to remove nonexistent inspector plugin.");
  3135. for (int i = idx; i < inspector_plugin_count - 1; i++) {
  3136. inspector_plugins[i] = inspector_plugins[i + 1];
  3137. }
  3138. inspector_plugins[inspector_plugin_count - 1] = Ref<EditorInspectorPlugin>();
  3139. inspector_plugin_count--;
  3140. }
  3141. void EditorInspector::cleanup_plugins() {
  3142. for (int i = 0; i < inspector_plugin_count; i++) {
  3143. inspector_plugins[i].unref();
  3144. }
  3145. inspector_plugin_count = 0;
  3146. }
  3147. bool EditorInspector::is_main_editor_inspector() const {
  3148. return InspectorDock::get_singleton() && InspectorDock::get_inspector_singleton() == this;
  3149. }
  3150. String EditorInspector::get_selected_path() const {
  3151. return property_selected;
  3152. }
  3153. void EditorInspector::_parse_added_editors(VBoxContainer *p_current_vbox, EditorInspectorSection *p_section, Ref<EditorInspectorPlugin> p_plugin) {
  3154. for (const EditorInspectorPlugin::AddedEditor &F : p_plugin->added_editors) {
  3155. EditorProperty *ep = Object::cast_to<EditorProperty>(F.property_editor);
  3156. if (ep && !F.properties.is_empty() && current_favorites.has(F.properties[0])) {
  3157. ep->favorited = true;
  3158. favorites_vbox->add_child(F.property_editor);
  3159. } else {
  3160. p_current_vbox->add_child(F.property_editor);
  3161. }
  3162. if (ep) {
  3163. ep->object = object;
  3164. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(false));
  3165. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  3166. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
  3167. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  3168. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  3169. ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
  3170. ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
  3171. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  3172. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  3173. ep->connect("resource_selected", callable_mp(get_root_inspector(), &EditorInspector::_resource_selected), CONNECT_DEFERRED);
  3174. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
  3175. if (F.properties.size()) {
  3176. if (F.properties.size() == 1) {
  3177. //since it's one, associate:
  3178. ep->property = F.properties[0];
  3179. ep->property_path = property_prefix + F.properties[0];
  3180. ep->property_usage = 0;
  3181. }
  3182. if (!F.label.is_empty()) {
  3183. ep->set_label(F.label);
  3184. }
  3185. for (int i = 0; i < F.properties.size(); i++) {
  3186. String prop = F.properties[i];
  3187. if (!editor_property_map.has(prop)) {
  3188. editor_property_map[prop] = List<EditorProperty *>();
  3189. }
  3190. editor_property_map[prop].push_back(ep);
  3191. }
  3192. }
  3193. Node *section_search = p_section;
  3194. while (section_search) {
  3195. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  3196. if (section) {
  3197. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  3198. }
  3199. section_search = section_search->get_parent();
  3200. if (Object::cast_to<EditorInspector>(section_search)) {
  3201. // Skip sub-resource inspectors.
  3202. break;
  3203. }
  3204. }
  3205. ep->set_read_only(read_only);
  3206. ep->update_property();
  3207. ep->_update_flags();
  3208. ep->update_editor_property_status();
  3209. ep->set_deletable(deletable_properties);
  3210. ep->update_cache();
  3211. }
  3212. }
  3213. p_plugin->added_editors.clear();
  3214. }
  3215. bool EditorInspector::_is_property_disabled_by_feature_profile(const StringName &p_property) {
  3216. if (!EditorFeatureProfileManager::get_singleton()) {
  3217. return false;
  3218. }
  3219. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  3220. if (profile.is_null()) {
  3221. return false;
  3222. }
  3223. StringName class_name = object->get_class();
  3224. while (class_name != StringName()) {
  3225. if (profile->is_class_property_disabled(class_name, p_property)) {
  3226. return true;
  3227. }
  3228. if (profile->is_class_disabled(class_name)) {
  3229. //won't see properties of a disabled class
  3230. return true;
  3231. }
  3232. class_name = ClassDB::get_parent_class(class_name);
  3233. }
  3234. return false;
  3235. }
  3236. void EditorInspector::_add_section_in_tree(EditorInspectorSection *p_section, VBoxContainer *p_current_vbox) {
  3237. // Place adjacent sections in their own vbox with a theme-specific separation.
  3238. VBoxContainer *container = nullptr;
  3239. if (p_current_vbox->get_child_count() > 0) {
  3240. Node *last_child = p_current_vbox->get_child(-1);
  3241. container = Object::cast_to<VBoxContainer>(last_child);
  3242. }
  3243. if (!container) {
  3244. container = memnew(VBoxContainer);
  3245. container->set_theme_type_variation(SNAME("EditorSectionContainer"));
  3246. p_current_vbox->add_child(container);
  3247. }
  3248. container->add_child(p_section);
  3249. }
  3250. void EditorInspector::update_tree() {
  3251. if (!object) {
  3252. return;
  3253. }
  3254. bool root_inspector_was_following_focus = get_root_inspector()->is_following_focus();
  3255. if (root_inspector_was_following_focus) {
  3256. // Temporarily disable focus following on the root inspector to avoid jumping while the inspector is updating.
  3257. get_root_inspector()->set_follow_focus(false);
  3258. }
  3259. // Store currently selected and focused elements to restore after the update.
  3260. // TODO: Can be useful to store more context for the focusable, such as the caret position in LineEdit.
  3261. StringName current_selected = property_selected;
  3262. int current_focusable = -1;
  3263. if (property_focusable != -1) {
  3264. // Check that focusable is actually focusable.
  3265. bool restore_focus = false;
  3266. Control *focused = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  3267. if (focused) {
  3268. Node *parent = focused->get_parent();
  3269. while (parent) {
  3270. EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
  3271. if (inspector) {
  3272. restore_focus = inspector == this; // May be owned by another inspector.
  3273. break; // Exit after the first inspector is found, since there may be nested ones.
  3274. }
  3275. parent = parent->get_parent();
  3276. }
  3277. }
  3278. if (restore_focus) {
  3279. current_focusable = property_focusable;
  3280. }
  3281. }
  3282. // The call here is for the edited object that has not changed, but the tree needs to be updated (for example, the object's property list has been modified).
  3283. // Since the edited object has not changed, there is no need to hide the plugin at this time.
  3284. _clear(false);
  3285. List<Ref<EditorInspectorPlugin>> valid_plugins;
  3286. for (int i = inspector_plugin_count - 1; i >= 0; i--) { //start by last, so lastly added can override newly added
  3287. if (!inspector_plugins[i]->can_handle(object)) {
  3288. continue;
  3289. }
  3290. valid_plugins.push_back(inspector_plugins[i]);
  3291. }
  3292. // Decide if properties should be drawn with the warning color (yellow),
  3293. // or if the whole object should be considered read-only.
  3294. bool draw_warning = false;
  3295. bool all_read_only = false;
  3296. if (is_inside_tree() && EditorNode::get_singleton()) {
  3297. if (object->has_method("_is_read_only")) {
  3298. all_read_only = object->call("_is_read_only");
  3299. }
  3300. Node *nod = Object::cast_to<Node>(object);
  3301. Node *es = EditorNode::get_singleton()->get_edited_scene();
  3302. if (nod && es != nod && nod->get_owner() != es) {
  3303. // Draw in warning color edited nodes that are not in the currently edited scene,
  3304. // as changes may be lost in the future.
  3305. draw_warning = true;
  3306. } else {
  3307. if (!all_read_only) {
  3308. Resource *res = Object::cast_to<Resource>(object);
  3309. if (res) {
  3310. all_read_only = EditorNode::get_singleton()->is_resource_read_only(res);
  3311. }
  3312. }
  3313. }
  3314. }
  3315. String filter = search_box ? search_box->get_text() : "";
  3316. String group;
  3317. String group_base;
  3318. EditorInspectorSection *group_togglable_property = nullptr;
  3319. String subgroup;
  3320. String subgroup_base;
  3321. EditorInspectorSection *subgroup_togglable_property = nullptr;
  3322. int section_depth = 0;
  3323. bool disable_favorite = false;
  3324. VBoxContainer *category_vbox = nullptr;
  3325. List<PropertyInfo> plist;
  3326. object->get_property_list(&plist, true);
  3327. HashMap<VBoxContainer *, HashMap<String, VBoxContainer *>> vbox_per_path;
  3328. HashMap<String, EditorInspectorArray *> editor_inspector_array_per_prefix;
  3329. HashMap<String, HashMap<String, LocalVector<EditorProperty *>>> favorites_to_add;
  3330. HashMap<String, EditorInspectorSection *> togglable_editor_inspector_sections;
  3331. const Color sscolor = theme_cache.prop_subsection;
  3332. bool sub_inspectors_enabled = EDITOR_GET("interface/inspector/open_resources_in_current_inspector");
  3333. if (!valid_plugins.is_empty()) {
  3334. // Show early to avoid sizing problems.
  3335. begin_vbox->show();
  3336. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3337. ped->parse_begin(object);
  3338. _parse_added_editors(begin_vbox, nullptr, ped);
  3339. }
  3340. // Hide it again if no editors were added to the beginning.
  3341. if (begin_vbox->get_child_count() == 0) {
  3342. begin_vbox->hide();
  3343. }
  3344. }
  3345. StringName doc_name;
  3346. // Get the lists of editors for properties.
  3347. for (List<PropertyInfo>::Element *E_property = plist.front(); E_property; E_property = E_property->next()) {
  3348. PropertyInfo &p = E_property->get();
  3349. if (p.usage & PROPERTY_USAGE_SUBGROUP) {
  3350. // Setup a property sub-group.
  3351. subgroup = p.name;
  3352. subgroup_togglable_property = nullptr;
  3353. subgroup_base = p.hint_string.get_slicec(',', 0);
  3354. if (p.hint_string.get_slice_count(",") > 1) {
  3355. section_depth = p.hint_string.get_slicec(',', 1).to_int();
  3356. } else {
  3357. section_depth = 0;
  3358. }
  3359. continue;
  3360. } else if (p.usage & PROPERTY_USAGE_GROUP) {
  3361. // Setup a property group.
  3362. group = p.name;
  3363. group_togglable_property = nullptr;
  3364. group_base = p.hint_string.get_slicec(',', 0);
  3365. if (p.hint_string.get_slice_count(",") > 1) {
  3366. section_depth = p.hint_string.get_slicec(',', 1).to_int();
  3367. } else {
  3368. section_depth = 0;
  3369. }
  3370. subgroup = "";
  3371. subgroup_base = "";
  3372. subgroup_togglable_property = nullptr;
  3373. continue;
  3374. } else if (p.usage & PROPERTY_USAGE_CATEGORY) {
  3375. // Setup a property category.
  3376. group = "";
  3377. group_base = "";
  3378. group_togglable_property = nullptr;
  3379. subgroup = "";
  3380. subgroup_base = "";
  3381. subgroup_togglable_property = nullptr;
  3382. section_depth = 0;
  3383. disable_favorite = false;
  3384. vbox_per_path.clear();
  3385. editor_inspector_array_per_prefix.clear();
  3386. // `hint_script` should contain a native class name or a script path.
  3387. // Otherwise the category was probably added via `@export_category` or `_get_property_list()`.
  3388. const bool is_custom_category = p.hint_string.is_empty();
  3389. // Iterate over remaining properties. If no properties in category, skip the category.
  3390. List<PropertyInfo>::Element *N = E_property->next();
  3391. bool valid = true;
  3392. while (N) {
  3393. if (!N->get().name.begins_with("metadata/_") && N->get().usage & PROPERTY_USAGE_EDITOR &&
  3394. (!filter.is_empty() || !restrict_to_basic || (N->get().usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  3395. break;
  3396. }
  3397. // Treat custom categories as second-level ones. Do not skip a normal category if it is followed by a custom one.
  3398. // Skip in the other 3 cases (normal -> normal, custom -> custom, custom -> normal).
  3399. if ((N->get().usage & PROPERTY_USAGE_CATEGORY) && (is_custom_category || !N->get().hint_string.is_empty())) {
  3400. valid = false;
  3401. break;
  3402. }
  3403. N = N->next();
  3404. }
  3405. if (!valid) {
  3406. continue; // Empty, ignore it.
  3407. }
  3408. String category_tooltip;
  3409. // Do not add an icon, do not change the current class (`doc_name`) for custom categories.
  3410. if (is_custom_category) {
  3411. category_tooltip = p.name;
  3412. } else {
  3413. doc_name = p.name;
  3414. // Use category's owner script to update some of its information.
  3415. if (!EditorNode::get_editor_data().is_type_recognized(p.name) && ResourceLoader::exists(p.hint_string, "Script")) {
  3416. Ref<Script> scr = ResourceLoader::load(p.hint_string, "Script");
  3417. if (scr.is_valid()) {
  3418. doc_name = scr->get_doc_class_name();
  3419. // Property favorites aren't compatible with built-in scripts.
  3420. if (scr->is_built_in()) {
  3421. disable_favorite = true;
  3422. }
  3423. }
  3424. }
  3425. if (use_doc_hints) {
  3426. // `|` separators used in `EditorHelpBit`.
  3427. category_tooltip = "class|" + doc_name + "|";
  3428. }
  3429. }
  3430. if ((is_custom_category && !show_custom_categories) || (!is_custom_category && !show_standard_categories)) {
  3431. continue;
  3432. }
  3433. // Hide the "MultiNodeEdit" category for MultiNodeEdit.
  3434. if (Object::cast_to<MultiNodeEdit>(object) && p.name == "MultiNodeEdit") {
  3435. continue;
  3436. }
  3437. // Create an EditorInspectorCategory and add it to the inspector.
  3438. EditorInspectorCategory *category = memnew(EditorInspectorCategory);
  3439. category->set_property_info(p);
  3440. main_vbox->add_child(category);
  3441. category_vbox = nullptr; // Reset.
  3442. // Set the category info.
  3443. category->set_tooltip_text(category_tooltip);
  3444. if (!is_custom_category) {
  3445. category->set_doc_class_name(doc_name);
  3446. }
  3447. // Add editors at the start of a category.
  3448. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3449. ped->parse_category(object, p.name);
  3450. _parse_added_editors(main_vbox, nullptr, ped);
  3451. }
  3452. continue;
  3453. } else if (p.name.begins_with("metadata/_") || !(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name) ||
  3454. (filter.is_empty() && restrict_to_basic && !(p.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  3455. // Ignore properties that are not supposed to be in the inspector.
  3456. continue;
  3457. }
  3458. if (p.name == "script") {
  3459. // Script should go into its own category.
  3460. category_vbox = nullptr;
  3461. }
  3462. if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) {
  3463. // Do not show this property in low end gfx.
  3464. continue;
  3465. }
  3466. if (p.name == "script" && (hide_script || bool(object->call("_hide_script_from_inspector")))) {
  3467. // Hide script variables from inspector if required.
  3468. continue;
  3469. }
  3470. if (p.name.begins_with("metadata/") && bool(object->call(SNAME("_hide_metadata_from_inspector")))) {
  3471. // Hide metadata from inspector if required.
  3472. continue;
  3473. }
  3474. // Get the path for property.
  3475. String path = p.name;
  3476. // First check if we have an array that fits the prefix.
  3477. String array_prefix = "";
  3478. int array_index = -1;
  3479. for (KeyValue<String, EditorInspectorArray *> &E : editor_inspector_array_per_prefix) {
  3480. if (p.name.begins_with(E.key) && E.key.length() > array_prefix.length()) {
  3481. array_prefix = E.key;
  3482. }
  3483. }
  3484. if (!array_prefix.is_empty()) {
  3485. // If we have an array element, find the according index in array.
  3486. String str = p.name.trim_prefix(array_prefix);
  3487. int to_char_index = 0;
  3488. while (to_char_index < str.length()) {
  3489. if (!is_digit(str[to_char_index])) {
  3490. break;
  3491. }
  3492. to_char_index++;
  3493. }
  3494. if (to_char_index > 0) {
  3495. array_index = str.left(to_char_index).to_int();
  3496. } else {
  3497. array_prefix = "";
  3498. }
  3499. }
  3500. // Don't allow to favorite array items.
  3501. if (!disable_favorite) {
  3502. disable_favorite = !array_prefix.is_empty();
  3503. }
  3504. if (!array_prefix.is_empty()) {
  3505. path = path.trim_prefix(array_prefix);
  3506. int char_index = path.find_char('/');
  3507. if (char_index >= 0) {
  3508. path = path.right(-char_index - 1);
  3509. } else {
  3510. path = vformat(TTR("Element %s"), array_index);
  3511. }
  3512. } else {
  3513. // Check if we exit or not a subgroup. If there is a prefix, remove it from the property label string.
  3514. if (!subgroup.is_empty() && !subgroup_base.is_empty()) {
  3515. if (path.begins_with(subgroup_base)) {
  3516. path = path.trim_prefix(subgroup_base);
  3517. } else if (subgroup_base.begins_with(path)) {
  3518. // Keep it, this is used pretty often.
  3519. } else {
  3520. subgroup = ""; // The prefix changed, we are no longer in the subgroup.
  3521. subgroup_togglable_property = nullptr;
  3522. }
  3523. }
  3524. // Check if we exit or not a group. If there is a prefix, remove it from the property label string.
  3525. if (!group.is_empty() && !group_base.is_empty() && subgroup.is_empty()) {
  3526. if (path.begins_with(group_base)) {
  3527. path = path.trim_prefix(group_base);
  3528. } else if (group_base.begins_with(path)) {
  3529. // Keep it, this is used pretty often.
  3530. } else {
  3531. group = ""; // The prefix changed, we are no longer in the group.
  3532. group_togglable_property = nullptr;
  3533. subgroup = "";
  3534. subgroup_togglable_property = nullptr;
  3535. }
  3536. }
  3537. // Add the group and subgroup to the path.
  3538. if (!subgroup.is_empty()) {
  3539. path = subgroup + "/" + path;
  3540. }
  3541. if (!group.is_empty()) {
  3542. path = group + "/" + path;
  3543. }
  3544. }
  3545. // Get the property label's string.
  3546. String name_override = (path.contains_char('/')) ? path.substr(path.rfind_char('/') + 1) : path;
  3547. String feature_tag;
  3548. {
  3549. const int dot = name_override.find_char('.');
  3550. if (dot != -1) {
  3551. feature_tag = name_override.substr(dot);
  3552. name_override = name_override.substr(0, dot);
  3553. }
  3554. }
  3555. name_override = name_override.uri_decode();
  3556. // Don't localize script variables.
  3557. EditorPropertyNameProcessor::Style name_style = property_name_style;
  3558. if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  3559. name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
  3560. }
  3561. const String property_label_string = EditorPropertyNameProcessor::get_singleton()->process_name(name_override, name_style, p.name, doc_name) + feature_tag;
  3562. // Remove the property from the path.
  3563. int idx = path.rfind_char('/');
  3564. if (idx > -1) {
  3565. path = path.left(idx);
  3566. } else {
  3567. path = "";
  3568. }
  3569. // Ignore properties that do not fit the filter.
  3570. bool sub_inspector_use_filter = false;
  3571. if (use_filter && !filter.is_empty()) {
  3572. const String property_path = property_prefix + (path.is_empty() ? "" : path + "/") + name_override;
  3573. if (!_property_path_matches(property_path, filter, property_name_style)) {
  3574. if (!sub_inspectors_enabled || p.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  3575. continue;
  3576. }
  3577. Ref<Resource> res = object->get(p.name);
  3578. if (res.is_null()) {
  3579. continue;
  3580. }
  3581. // Check if the sub-resource has any properties that match the filter.
  3582. if (!_resource_properties_matches(res, filter)) {
  3583. continue;
  3584. }
  3585. sub_inspector_use_filter = true;
  3586. }
  3587. }
  3588. // Recreate the category vbox if it was reset.
  3589. if (category_vbox == nullptr) {
  3590. category_vbox = memnew(VBoxContainer);
  3591. category_vbox->set_theme_type_variation(SNAME("EditorPropertyContainer"));
  3592. category_vbox->hide();
  3593. main_vbox->add_child(category_vbox);
  3594. }
  3595. // Find the correct section/vbox to add the property editor to.
  3596. VBoxContainer *root_vbox = array_prefix.is_empty() ? category_vbox : editor_inspector_array_per_prefix[array_prefix]->get_vbox(array_index);
  3597. if (!root_vbox) {
  3598. continue;
  3599. }
  3600. category_vbox->show();
  3601. if (!vbox_per_path.has(root_vbox)) {
  3602. vbox_per_path[root_vbox] = HashMap<String, VBoxContainer *>();
  3603. vbox_per_path[root_vbox][""] = root_vbox;
  3604. }
  3605. VBoxContainer *current_vbox = root_vbox;
  3606. String acc_path = "";
  3607. int level = 1;
  3608. Vector<String> components = path.split("/");
  3609. for (int i = 0; i < components.size(); i++) {
  3610. const String &component = components[i];
  3611. acc_path += (i > 0) ? "/" + component : component;
  3612. if (!vbox_per_path[root_vbox].has(acc_path)) {
  3613. // If the section does not exists, create it.
  3614. EditorInspectorSection *section = memnew(EditorInspectorSection);
  3615. get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
  3616. _add_section_in_tree(section, current_vbox);
  3617. sections.push_back(section);
  3618. String label;
  3619. String tooltip;
  3620. // Don't localize groups for script variables.
  3621. EditorPropertyNameProcessor::Style section_name_style = property_name_style;
  3622. if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  3623. section_name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
  3624. }
  3625. // Only process group label if this is not the group or subgroup.
  3626. if ((i == 0 && component == group) || (i == 1 && component == subgroup)) {
  3627. if (section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  3628. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
  3629. tooltip = component;
  3630. } else {
  3631. label = component;
  3632. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
  3633. }
  3634. } else {
  3635. label = EditorPropertyNameProcessor::get_singleton()->process_name(component, section_name_style, p.name, doc_name);
  3636. tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(component, EditorPropertyNameProcessor::get_tooltip_style(section_name_style), p.name, doc_name);
  3637. }
  3638. Color c = sscolor;
  3639. c.a /= level;
  3640. section->setup(acc_path, label, object, c, use_folding, section_depth, level);
  3641. section->set_tooltip_text(tooltip);
  3642. section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
  3643. section->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  3644. // Add editors at the start of a group.
  3645. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3646. ped->parse_group(object, path);
  3647. _parse_added_editors(section->get_vbox(), section, ped);
  3648. }
  3649. vbox_per_path[root_vbox][acc_path] = section->get_vbox();
  3650. }
  3651. current_vbox = vbox_per_path[root_vbox][acc_path];
  3652. level = (MIN(level + 1, 4));
  3653. }
  3654. // If we did not find a section to add the property to, add it to the category vbox instead (the category vbox handles margins correctly).
  3655. if (current_vbox == main_vbox) {
  3656. category_vbox->show();
  3657. current_vbox = category_vbox;
  3658. }
  3659. // Check if the property is an array counter, if so create a dedicated array editor for the array.
  3660. if (p.usage & PROPERTY_USAGE_ARRAY) {
  3661. EditorInspectorArray *editor_inspector_array = nullptr;
  3662. StringName array_element_prefix;
  3663. Color c = sscolor;
  3664. c.a /= level;
  3665. Vector<String> class_name_components = String(p.class_name).split(",");
  3666. int page_size = 5;
  3667. bool movable = true;
  3668. bool is_const = false;
  3669. bool numbered = false;
  3670. bool foldable = use_folding;
  3671. String add_button_text = TTRC("Add Element");
  3672. String swap_method;
  3673. for (int i = (p.type == Variant::NIL ? 1 : 2); i < class_name_components.size(); i++) {
  3674. if (class_name_components[i].begins_with("page_size") && class_name_components[i].get_slice_count("=") == 2) {
  3675. page_size = class_name_components[i].get_slicec('=', 1).to_int();
  3676. } else if (class_name_components[i].begins_with("add_button_text") && class_name_components[i].get_slice_count("=") == 2) {
  3677. add_button_text = class_name_components[i].get_slicec('=', 1).strip_edges();
  3678. } else if (class_name_components[i] == "static") {
  3679. movable = false;
  3680. } else if (class_name_components[i] == "const") {
  3681. is_const = true;
  3682. } else if (class_name_components[i] == "numbered") {
  3683. numbered = true;
  3684. } else if (class_name_components[i] == "unfoldable") {
  3685. foldable = false;
  3686. } else if (class_name_components[i].begins_with("swap_method") && class_name_components[i].get_slice_count("=") == 2) {
  3687. swap_method = class_name_components[i].get_slicec('=', 1).strip_edges();
  3688. }
  3689. }
  3690. if (p.type == Variant::NIL) {
  3691. // Setup the array to use a method to create/move/delete elements.
  3692. array_element_prefix = class_name_components[0];
  3693. editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
  3694. String array_label = path.contains_char('/') ? path.substr(path.rfind_char('/') + 1) : path;
  3695. array_label = EditorPropertyNameProcessor::get_singleton()->process_name(property_label_string, property_name_style, p.name, doc_name);
  3696. int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
  3697. editor_inspector_array->setup_with_move_element_function(object, array_label, array_element_prefix, page, c, use_folding);
  3698. editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
  3699. } else if (p.type == Variant::INT) {
  3700. // Setup the array to use the count property and built-in functions to create/move/delete elements.
  3701. if (class_name_components.size() >= 2) {
  3702. array_element_prefix = class_name_components[1];
  3703. editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
  3704. int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
  3705. editor_inspector_array->setup_with_count_property(object, class_name_components[0], p.name, array_element_prefix, page, c, foldable, movable, is_const, numbered, page_size, add_button_text, swap_method);
  3706. editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
  3707. }
  3708. }
  3709. if (editor_inspector_array) {
  3710. _add_section_in_tree(editor_inspector_array, current_vbox);
  3711. editor_inspector_array_per_prefix[array_element_prefix] = editor_inspector_array;
  3712. }
  3713. continue;
  3714. }
  3715. // Checkable and checked properties.
  3716. bool checkable = false;
  3717. bool checked = false;
  3718. if (p.usage & PROPERTY_USAGE_CHECKABLE) {
  3719. checkable = true;
  3720. checked = p.usage & PROPERTY_USAGE_CHECKED;
  3721. }
  3722. bool property_read_only = (p.usage & PROPERTY_USAGE_READ_ONLY) || read_only;
  3723. // Mark properties that would require an editor restart (mostly when editing editor settings).
  3724. if (p.usage & PROPERTY_USAGE_RESTART_IF_CHANGED) {
  3725. restart_request_props.insert(p.name);
  3726. }
  3727. String doc_path;
  3728. String theme_item_name;
  3729. String doc_tooltip_text;
  3730. // Build the doc hint, to use as tooltip.
  3731. if (use_doc_hints) {
  3732. StringName classname = doc_name;
  3733. if (!object_class.is_empty()) {
  3734. classname = object_class;
  3735. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  3736. classname = Object::cast_to<MultiNodeEdit>(object)->get_edited_class_name();
  3737. } else if (classname == "") {
  3738. classname = object->get_class_name();
  3739. Resource *res = Object::cast_to<Resource>(object);
  3740. if (res && !res->get_script().is_null()) {
  3741. // Grab the script of this resource to get the evaluated script class.
  3742. Ref<Script> scr = res->get_script();
  3743. if (scr.is_valid()) {
  3744. Vector<DocData::ClassDoc> docs = scr->get_documentation();
  3745. if (!docs.is_empty()) {
  3746. // The documentation of a GDScript's main class is at the end of the array.
  3747. // Hacky because this isn't necessarily always guaranteed.
  3748. classname = docs[docs.size() - 1].name;
  3749. }
  3750. }
  3751. }
  3752. }
  3753. StringName propname = property_prefix + p.name;
  3754. for (const KeyValue<String, String> &E : doc_property_class_remaps) {
  3755. if (property_prefix.begins_with(E.key)) {
  3756. propname = property_prefix.trim_prefix(E.key) + p.name;
  3757. classname = E.value;
  3758. break;
  3759. }
  3760. }
  3761. bool found = false;
  3762. // Small hack for theme_overrides. They are listed under Control, but come from another class.
  3763. if (classname == "Control" && p.name.begins_with("theme_override_")) {
  3764. classname = get_edited_object()->get_class();
  3765. }
  3766. // Search for the doc path in the cache.
  3767. HashMap<StringName, HashMap<StringName, DocCacheInfo>>::Iterator E = doc_cache.find(classname);
  3768. if (E) {
  3769. HashMap<StringName, DocCacheInfo>::Iterator F = E->value.find(propname);
  3770. if (F) {
  3771. found = true;
  3772. doc_path = F->value.doc_path;
  3773. theme_item_name = F->value.theme_item_name;
  3774. }
  3775. }
  3776. if (!found) {
  3777. DocTools *dd = EditorHelp::get_doc_data();
  3778. // Do not cache the doc path information of scripts.
  3779. bool is_native_class = ClassDB::class_exists(classname);
  3780. HashMap<String, DocData::ClassDoc>::ConstIterator F = dd->class_list.find(classname);
  3781. while (F) {
  3782. Vector<String> slices = propname.operator String().split("/");
  3783. // Check if it's a theme item first.
  3784. if (slices.size() == 2 && slices[0].begins_with("theme_override_")) {
  3785. for (int i = 0; i < F->value.theme_properties.size(); i++) {
  3786. String doc_path_current = "class_theme_item:" + F->value.name + ":" + F->value.theme_properties[i].name;
  3787. if (F->value.theme_properties[i].name == slices[1]) {
  3788. doc_path = doc_path_current;
  3789. theme_item_name = F->value.theme_properties[i].name;
  3790. }
  3791. }
  3792. } else {
  3793. for (int i = 0; i < F->value.properties.size(); i++) {
  3794. String doc_path_current = "class_property:" + F->value.name + ":" + F->value.properties[i].name;
  3795. if (F->value.properties[i].name == propname.operator String()) {
  3796. doc_path = doc_path_current;
  3797. }
  3798. }
  3799. }
  3800. if (is_native_class) {
  3801. DocCacheInfo cache_info;
  3802. cache_info.doc_path = doc_path;
  3803. cache_info.theme_item_name = theme_item_name;
  3804. doc_cache[classname][propname] = cache_info;
  3805. }
  3806. if (!doc_path.is_empty() || F->value.inherits.is_empty()) {
  3807. break;
  3808. }
  3809. // Couldn't find the doc path in the class itself, try its super class.
  3810. F = dd->class_list.find(F->value.inherits);
  3811. }
  3812. }
  3813. // `|` separators used in `EditorHelpBit`.
  3814. if (theme_item_name.is_empty()) {
  3815. if (p.name.contains("shader_parameter/")) {
  3816. doc_tooltip_text = "property|" + p.class_name + "|" + property_prefix + propname;
  3817. } else if (p.usage & PROPERTY_USAGE_INTERNAL) {
  3818. doc_tooltip_text = "internal_property|" + classname + "|" + propname;
  3819. } else {
  3820. doc_tooltip_text = "property|" + classname + "|" + propname;
  3821. }
  3822. } else {
  3823. doc_tooltip_text = "theme_item|" + classname + "|" + theme_item_name;
  3824. }
  3825. }
  3826. // Only used for boolean types. Makes the section header a checkable group and adds tooltips.
  3827. if (p.hint == PROPERTY_HINT_GROUP_ENABLE) {
  3828. if (p.type == Variant::BOOL && (p.name.begins_with(group_base) || p.name.begins_with(subgroup_base))) {
  3829. EditorInspectorSection *last_created_section = Object::cast_to<EditorInspectorSection>(current_vbox->get_parent());
  3830. if (last_created_section) {
  3831. bool valid = false;
  3832. Variant value_checked = object->get(p.name, &valid);
  3833. if (valid) {
  3834. last_created_section->set_checkable(p.name, p.hint_string == "checkbox_only", value_checked.operator bool());
  3835. last_created_section->set_keying(keying);
  3836. if (p.name.begins_with(group_base)) {
  3837. group_togglable_property = last_created_section;
  3838. } else {
  3839. subgroup_togglable_property = last_created_section;
  3840. }
  3841. if (use_doc_hints) {
  3842. last_created_section->set_tooltip_text(doc_tooltip_text);
  3843. }
  3844. continue;
  3845. }
  3846. }
  3847. } else {
  3848. ERR_PRINT("PROPERTY_HINT_GROUP_ENABLE can only be used on boolean types and must have the same prefix as the group.");
  3849. }
  3850. }
  3851. Vector<EditorInspectorPlugin::AddedEditor> editors;
  3852. Vector<EditorInspectorPlugin::AddedEditor> late_editors;
  3853. // Search for the inspector plugin that will handle the properties. Then add the correct property editor to it.
  3854. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3855. bool exclusive = ped->parse_property(object, p.type, p.name, p.hint, p.hint_string, p.usage, wide_editors);
  3856. for (const EditorInspectorPlugin::AddedEditor &F : ped->added_editors) {
  3857. if (F.add_to_end) {
  3858. late_editors.push_back(F);
  3859. } else {
  3860. editors.push_back(F);
  3861. }
  3862. }
  3863. ped->added_editors.clear();
  3864. if (exclusive) {
  3865. break;
  3866. }
  3867. }
  3868. editors.append_array(late_editors);
  3869. const Node *node = Object::cast_to<Node>(object);
  3870. Vector<SceneState::PackState> sstack;
  3871. if (node != nullptr) {
  3872. const Node *es = EditorNode::get_singleton()->get_edited_scene();
  3873. sstack = PropertyUtils::get_node_states_stack(node, es);
  3874. }
  3875. for (int i = 0; i < editors.size(); i++) {
  3876. EditorProperty *ep = Object::cast_to<EditorProperty>(editors[i].property_editor);
  3877. const Vector<String> &properties = editors[i].properties;
  3878. if (ep) {
  3879. // Set all this before the control gets the ENTER_TREE notification.
  3880. ep->object = object;
  3881. if (properties.size()) {
  3882. if (properties.size() == 1) {
  3883. // Since it's one, associate:
  3884. ep->property = properties[0];
  3885. ep->property_path = property_prefix + properties[0];
  3886. ep->property_usage = p.usage;
  3887. // And set label?
  3888. }
  3889. if (!editors[i].label.is_empty()) {
  3890. ep->set_label(editors[i].label);
  3891. } else {
  3892. // Use the existing one.
  3893. ep->set_label(property_label_string);
  3894. }
  3895. for (int j = 0; j < properties.size(); j++) {
  3896. String prop = properties[j];
  3897. if (!editor_property_map.has(prop)) {
  3898. editor_property_map[prop] = List<EditorProperty *>();
  3899. }
  3900. editor_property_map[prop].push_back(ep);
  3901. }
  3902. }
  3903. if (sub_inspector_use_filter) {
  3904. EditorPropertyResource *epr = Object::cast_to<EditorPropertyResource>(ep);
  3905. if (epr) {
  3906. epr->set_use_filter(true);
  3907. }
  3908. }
  3909. if (p.name.begins_with("metadata/")) {
  3910. Variant _default = Variant();
  3911. if (node != nullptr) {
  3912. _default = PropertyUtils::get_property_default_value(node, p.name, nullptr, &sstack, false, nullptr, nullptr);
  3913. }
  3914. ep->set_deletable(_default == Variant());
  3915. } else {
  3916. ep->set_deletable(deletable_properties);
  3917. }
  3918. ep->set_draw_warning(draw_warning);
  3919. ep->set_use_folding(use_folding);
  3920. ep->set_favoritable(can_favorite && !disable_favorite && !ep->is_deletable());
  3921. ep->set_checkable(checkable);
  3922. ep->set_checked(checked);
  3923. ep->set_keying(keying);
  3924. ep->set_read_only(property_read_only || all_read_only);
  3925. }
  3926. if (ep && ep->is_favoritable() && current_favorites.has(p.name)) {
  3927. ep->favorited = true;
  3928. favorites_to_add[group][subgroup].push_back(ep);
  3929. if (group_togglable_property) {
  3930. togglable_editor_inspector_sections[group] = group_togglable_property;
  3931. }
  3932. if (subgroup_togglable_property) {
  3933. togglable_editor_inspector_sections[group + "/" + subgroup] = subgroup_togglable_property;
  3934. }
  3935. } else {
  3936. current_vbox->add_child(editors[i].property_editor);
  3937. if (ep) {
  3938. Node *section_search = current_vbox->get_parent();
  3939. while (section_search) {
  3940. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  3941. if (section) {
  3942. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  3943. }
  3944. section_search = section_search->get_parent();
  3945. if (Object::cast_to<EditorInspector>(section_search)) {
  3946. // Skip sub-resource inspectors.
  3947. break;
  3948. }
  3949. }
  3950. }
  3951. }
  3952. if (ep) {
  3953. // Eventually, set other properties/signals after the property editor got added to the tree.
  3954. bool update_all = (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED);
  3955. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(update_all));
  3956. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  3957. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
  3958. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  3959. ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
  3960. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  3961. ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
  3962. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  3963. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  3964. ep->connect("resource_selected", callable_mp(get_root_inspector(), &EditorInspector::_resource_selected), CONNECT_DEFERRED);
  3965. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
  3966. ep->set_tooltip_text(doc_tooltip_text);
  3967. ep->has_doc_tooltip = use_doc_hints;
  3968. ep->set_doc_path(doc_path);
  3969. ep->set_internal(p.usage & PROPERTY_USAGE_INTERNAL);
  3970. // If this property is favorited, it won't be in the tree yet. So don't do this setup right now.
  3971. if (ep->is_inside_tree()) {
  3972. ep->update_property();
  3973. ep->_update_flags();
  3974. ep->update_editor_property_status();
  3975. ep->update_cache();
  3976. if (current_selected && ep->property == current_selected) {
  3977. ep->select(current_focusable);
  3978. }
  3979. }
  3980. }
  3981. }
  3982. }
  3983. if (!current_favorites.is_empty()) {
  3984. favorites_section->show();
  3985. // Organize the favorited properties in their sections, to keep context and differentiate from others with the same name.
  3986. bool is_localized = property_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED;
  3987. for (const KeyValue<String, HashMap<String, LocalVector<EditorProperty *>>> &KV : favorites_to_add) {
  3988. String section_name = KV.key;
  3989. String label;
  3990. String tooltip;
  3991. VBoxContainer *parent_vbox = favorites_vbox;
  3992. if (!section_name.is_empty()) {
  3993. favorites_groups_vbox->show();
  3994. if (is_localized) {
  3995. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3996. tooltip = section_name;
  3997. } else {
  3998. label = section_name;
  3999. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  4000. }
  4001. EditorInspectorSection *section = memnew(EditorInspectorSection);
  4002. get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
  4003. favorites_groups_vbox->add_child(section);
  4004. parent_vbox = section->get_vbox();
  4005. section->setup("", section_name, object, sscolor, false);
  4006. section->set_tooltip_text(tooltip);
  4007. if (togglable_editor_inspector_sections.has(section_name)) {
  4008. EditorInspectorSection *corresponding_section = togglable_editor_inspector_sections.get(section_name);
  4009. bool valid = false;
  4010. Variant value_checked = object->get(corresponding_section->related_enable_property, &valid);
  4011. if (valid) {
  4012. section->section = corresponding_section->section;
  4013. section->set_checkable(corresponding_section->related_enable_property, corresponding_section->checkbox_only, value_checked.operator bool());
  4014. section->set_keying(keying);
  4015. if (use_doc_hints) {
  4016. section->set_tooltip_text(corresponding_section->get_tooltip_text());
  4017. }
  4018. section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
  4019. section->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  4020. sections.push_back(section);
  4021. }
  4022. }
  4023. }
  4024. for (const KeyValue<String, LocalVector<EditorProperty *>> &KV2 : KV.value) {
  4025. section_name = KV2.key;
  4026. VBoxContainer *vbox = parent_vbox;
  4027. if (!section_name.is_empty()) {
  4028. if (is_localized) {
  4029. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  4030. tooltip = section_name;
  4031. } else {
  4032. label = section_name;
  4033. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  4034. }
  4035. EditorInspectorSection *section = memnew(EditorInspectorSection);
  4036. get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
  4037. vbox->add_child(section);
  4038. vbox = section->get_vbox();
  4039. section->setup("", section_name, object, sscolor, false);
  4040. section->set_tooltip_text(tooltip);
  4041. if (togglable_editor_inspector_sections.has(KV.key + "/" + section_name)) {
  4042. EditorInspectorSection *corresponding_section = togglable_editor_inspector_sections.get(KV.key + "/" + section_name);
  4043. bool valid = false;
  4044. Variant value_checked = object->get(corresponding_section->related_enable_property, &valid);
  4045. if (valid) {
  4046. section->section = corresponding_section->section;
  4047. section->set_checkable(corresponding_section->related_enable_property, corresponding_section->checkbox_only, value_checked.operator bool());
  4048. section->set_keying(keying);
  4049. if (use_doc_hints) {
  4050. section->set_tooltip_text(corresponding_section->get_tooltip_text());
  4051. }
  4052. section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
  4053. section->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  4054. sections.push_back(section);
  4055. }
  4056. }
  4057. }
  4058. for (EditorProperty *ep : KV2.value) {
  4059. vbox->add_child(ep);
  4060. Node *section_search = vbox->get_parent();
  4061. while (section_search) {
  4062. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  4063. if (section) {
  4064. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  4065. }
  4066. section_search = section_search->get_parent();
  4067. if (Object::cast_to<EditorInspector>(section_search)) {
  4068. // Skip sub-resource inspectors.
  4069. break;
  4070. }
  4071. }
  4072. // Now that it's inside the tree, do the setup.
  4073. ep->update_property();
  4074. ep->_update_flags();
  4075. ep->update_editor_property_status();
  4076. ep->update_cache();
  4077. if (current_selected && ep->property == current_selected) {
  4078. ep->select(current_focusable);
  4079. }
  4080. }
  4081. }
  4082. if (favorites_vbox->get_child_count() > 0) {
  4083. favorites_vbox->show();
  4084. }
  4085. }
  4086. // Show a separator if there's no category to clearly divide the properties.
  4087. favorites_separator->hide();
  4088. if (main_vbox->get_child_count() > 0) {
  4089. EditorInspectorCategory *category = Object::cast_to<EditorInspectorCategory>(main_vbox->get_child(0));
  4090. if (!category) {
  4091. favorites_separator->show();
  4092. }
  4093. }
  4094. // Clean up empty sections.
  4095. for (List<EditorInspectorSection *>::Element *I = sections.back(); I;) {
  4096. EditorInspectorSection *section = I->get();
  4097. I = I->prev(); // Note: Advance before erasing element.
  4098. if (section->get_vbox()->get_child_count() == 0) {
  4099. sections.erase(section);
  4100. vbox_per_path[main_vbox].erase(section->get_section());
  4101. memdelete(section);
  4102. }
  4103. }
  4104. }
  4105. if (!hide_metadata && !object->call("_hide_metadata_from_inspector")) {
  4106. // Add 4px of spacing between the "Add Metadata" button and the content above it.
  4107. Control *spacer = memnew(Control);
  4108. spacer->set_custom_minimum_size(Size2(0, 4) * EDSCALE);
  4109. main_vbox->add_child(spacer);
  4110. Button *add_md = memnew(EditorInspectorActionButton(TTRC("Add Metadata"), SNAME("Add")));
  4111. add_md->connect(SceneStringName(pressed), callable_mp(this, &EditorInspector::_show_add_meta_dialog));
  4112. main_vbox->add_child(add_md);
  4113. if (all_read_only) {
  4114. add_md->set_disabled(true);
  4115. }
  4116. }
  4117. // Get the lists of to add at the end.
  4118. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  4119. ped->parse_end(object);
  4120. _parse_added_editors(main_vbox, nullptr, ped);
  4121. }
  4122. if (is_main_editor_inspector()) {
  4123. // Updating inspector might invalidate some editing owners.
  4124. EditorNode::get_singleton()->hide_unused_editors();
  4125. }
  4126. if (root_inspector_was_following_focus) {
  4127. get_root_inspector()->set_follow_focus(true);
  4128. }
  4129. }
  4130. void EditorInspector::update_property(const String &p_prop) {
  4131. if (!editor_property_map.has(p_prop)) {
  4132. return;
  4133. }
  4134. for (EditorProperty *E : editor_property_map[p_prop]) {
  4135. E->update_property();
  4136. E->update_editor_property_status();
  4137. E->update_cache();
  4138. }
  4139. for (EditorInspectorSection *S : sections) {
  4140. if (S->is_checkable()) {
  4141. S->_property_edited(p_prop);
  4142. }
  4143. }
  4144. }
  4145. void EditorInspector::_clear(bool p_hide_plugins) {
  4146. begin_vbox->hide();
  4147. while (begin_vbox->get_child_count()) {
  4148. memdelete(begin_vbox->get_child(0));
  4149. }
  4150. favorites_section->hide();
  4151. favorites_vbox->hide();
  4152. while (favorites_vbox->get_child_count()) {
  4153. memdelete(favorites_vbox->get_child(0));
  4154. }
  4155. favorites_groups_vbox->hide();
  4156. while (favorites_groups_vbox->get_child_count()) {
  4157. memdelete(favorites_groups_vbox->get_child(0));
  4158. }
  4159. while (main_vbox->get_child_count()) {
  4160. memdelete(main_vbox->get_child(0));
  4161. }
  4162. property_selected = StringName();
  4163. property_focusable = -1;
  4164. editor_property_map.clear();
  4165. sections.clear();
  4166. pending.clear();
  4167. restart_request_props.clear();
  4168. if (p_hide_plugins && is_main_editor_inspector()) {
  4169. EditorNode::get_singleton()->hide_unused_editors(this);
  4170. }
  4171. }
  4172. Object *EditorInspector::get_edited_object() {
  4173. return object;
  4174. }
  4175. Object *EditorInspector::get_next_edited_object() {
  4176. return next_object;
  4177. }
  4178. void EditorInspector::edit(Object *p_object) {
  4179. if (object == p_object) {
  4180. return;
  4181. }
  4182. next_object = p_object; // Some plugins need to know the next edited object when clearing the inspector.
  4183. if (object) {
  4184. if (likely(Variant(object).get_validated_object())) {
  4185. object->disconnect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
  4186. }
  4187. _clear();
  4188. }
  4189. per_array_page.clear();
  4190. object = p_object;
  4191. if (object) {
  4192. update_scroll_request = 0; //reset
  4193. if (scroll_cache.has(object->get_instance_id())) { //if exists, set something else
  4194. update_scroll_request = scroll_cache[object->get_instance_id()]; //done this way because wait until full size is accommodated
  4195. }
  4196. object->connect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
  4197. can_favorite = Object::cast_to<Node>(object) || Object::cast_to<Resource>(object);
  4198. _update_current_favorites();
  4199. update_tree();
  4200. }
  4201. // Keep it available until the end so it works with both main and sub inspectors.
  4202. next_object = nullptr;
  4203. emit_signal(SNAME("edited_object_changed"));
  4204. }
  4205. void EditorInspector::set_keying(bool p_active) {
  4206. if (keying == p_active) {
  4207. return;
  4208. }
  4209. keying = p_active;
  4210. _keying_changed();
  4211. }
  4212. void EditorInspector::_keying_changed() {
  4213. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4214. for (EditorProperty *E : F.value) {
  4215. if (E) {
  4216. E->set_keying(keying);
  4217. }
  4218. }
  4219. }
  4220. for (EditorInspectorSection *S : sections) {
  4221. S->set_keying(keying);
  4222. }
  4223. }
  4224. void EditorInspector::set_read_only(bool p_read_only) {
  4225. if (p_read_only == read_only) {
  4226. return;
  4227. }
  4228. read_only = p_read_only;
  4229. update_tree();
  4230. }
  4231. EditorPropertyNameProcessor::Style EditorInspector::get_property_name_style() const {
  4232. return property_name_style;
  4233. }
  4234. void EditorInspector::set_property_name_style(EditorPropertyNameProcessor::Style p_style) {
  4235. if (property_name_style == p_style) {
  4236. return;
  4237. }
  4238. property_name_style = p_style;
  4239. update_tree();
  4240. }
  4241. void EditorInspector::set_use_settings_name_style(bool p_enable) {
  4242. if (use_settings_name_style == p_enable) {
  4243. return;
  4244. }
  4245. use_settings_name_style = p_enable;
  4246. if (use_settings_name_style) {
  4247. set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
  4248. }
  4249. }
  4250. void EditorInspector::set_autoclear(bool p_enable) {
  4251. autoclear = p_enable;
  4252. }
  4253. void EditorInspector::set_show_categories(bool p_show_standard, bool p_show_custom) {
  4254. show_standard_categories = p_show_standard;
  4255. show_custom_categories = p_show_custom;
  4256. update_tree();
  4257. }
  4258. void EditorInspector::set_use_doc_hints(bool p_enable) {
  4259. use_doc_hints = p_enable;
  4260. update_tree();
  4261. }
  4262. void EditorInspector::set_hide_script(bool p_hide) {
  4263. hide_script = p_hide;
  4264. update_tree();
  4265. }
  4266. void EditorInspector::set_hide_metadata(bool p_hide) {
  4267. hide_metadata = p_hide;
  4268. update_tree();
  4269. }
  4270. void EditorInspector::set_use_filter(bool p_use) {
  4271. use_filter = p_use;
  4272. update_tree();
  4273. }
  4274. void EditorInspector::register_text_enter(Node *p_line_edit) {
  4275. search_box = Object::cast_to<LineEdit>(p_line_edit);
  4276. if (search_box) {
  4277. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorInspector::update_tree).unbind(1));
  4278. }
  4279. }
  4280. void EditorInspector::set_use_folding(bool p_use_folding, bool p_update_tree) {
  4281. use_folding = p_use_folding;
  4282. if (p_update_tree) {
  4283. update_tree();
  4284. }
  4285. }
  4286. bool EditorInspector::is_using_folding() {
  4287. return use_folding;
  4288. }
  4289. void EditorInspector::collapse_all_folding() {
  4290. for (EditorInspectorSection *E : sections) {
  4291. E->fold();
  4292. }
  4293. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4294. for (EditorProperty *E : F.value) {
  4295. E->collapse_all_folding();
  4296. }
  4297. }
  4298. }
  4299. void EditorInspector::expand_all_folding() {
  4300. for (EditorInspectorSection *E : sections) {
  4301. E->unfold();
  4302. }
  4303. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4304. for (EditorProperty *E : F.value) {
  4305. E->expand_all_folding();
  4306. }
  4307. }
  4308. }
  4309. void EditorInspector::expand_revertable() {
  4310. HashSet<EditorInspectorSection *> sections_to_unfold[2];
  4311. for (EditorInspectorSection *E : sections) {
  4312. if (E->has_revertable_properties()) {
  4313. sections_to_unfold[0].insert(E);
  4314. }
  4315. }
  4316. // Climb up the hierarchy doing double buffering with the sets.
  4317. int a = 0;
  4318. int b = 1;
  4319. while (sections_to_unfold[a].size()) {
  4320. for (EditorInspectorSection *E : sections_to_unfold[a]) {
  4321. E->unfold();
  4322. Node *n = E->get_parent();
  4323. while (n) {
  4324. if (Object::cast_to<EditorInspector>(n)) {
  4325. break;
  4326. }
  4327. if (Object::cast_to<EditorInspectorSection>(n) && !sections_to_unfold[a].has((EditorInspectorSection *)n)) {
  4328. sections_to_unfold[b].insert((EditorInspectorSection *)n);
  4329. }
  4330. n = n->get_parent();
  4331. }
  4332. }
  4333. sections_to_unfold[a].clear();
  4334. SWAP(a, b);
  4335. }
  4336. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4337. for (EditorProperty *E : F.value) {
  4338. E->expand_revertable();
  4339. }
  4340. }
  4341. }
  4342. void EditorInspector::set_scroll_offset(int p_offset) {
  4343. // This can be called before the container finishes sorting its children, so defer it.
  4344. callable_mp((ScrollContainer *)this, &ScrollContainer::set_v_scroll).call_deferred(p_offset);
  4345. }
  4346. int EditorInspector::get_scroll_offset() const {
  4347. return get_v_scroll();
  4348. }
  4349. void EditorInspector::set_use_wide_editors(bool p_enable) {
  4350. wide_editors = p_enable;
  4351. }
  4352. void EditorInspector::set_root_inspector(EditorInspector *p_root_inspector) {
  4353. root_inspector = p_root_inspector;
  4354. // Only the root inspector should follow focus.
  4355. set_follow_focus(false);
  4356. }
  4357. void EditorInspector::_section_toggled_by_user(const String &p_path, bool p_value) {
  4358. _property_changed(p_path, p_value);
  4359. }
  4360. void EditorInspector::set_use_deletable_properties(bool p_enabled) {
  4361. deletable_properties = p_enabled;
  4362. }
  4363. void EditorInspector::_page_change_request(int p_new_page, const StringName &p_array_prefix) {
  4364. int prev_page = per_array_page.has(p_array_prefix) ? per_array_page[p_array_prefix] : 0;
  4365. int new_page = MAX(0, p_new_page);
  4366. if (new_page != prev_page) {
  4367. per_array_page[p_array_prefix] = new_page;
  4368. update_tree_pending = true;
  4369. }
  4370. }
  4371. void EditorInspector::_edit_request_change(Object *p_object, const String &p_property) {
  4372. if (object != p_object) { //may be undoing/redoing for a non edited object, so ignore
  4373. return;
  4374. }
  4375. if (changing) {
  4376. return;
  4377. }
  4378. if (p_property.is_empty()) {
  4379. update_tree_pending = true;
  4380. } else {
  4381. pending.insert(p_property);
  4382. }
  4383. }
  4384. void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) {
  4385. if (autoclear && editor_property_map.has(p_name)) {
  4386. for (EditorProperty *E : editor_property_map[p_name]) {
  4387. if (E->is_checkable()) {
  4388. E->set_checked(true);
  4389. }
  4390. }
  4391. }
  4392. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4393. if (!undo_redo || bool(object->call("_dont_undo_redo"))) {
  4394. object->set(p_name, p_value);
  4395. if (p_refresh_all) {
  4396. _edit_request_change(object, "");
  4397. } else {
  4398. _edit_request_change(object, p_name);
  4399. }
  4400. emit_signal(_prop_edited, p_name);
  4401. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  4402. Object::cast_to<MultiNodeEdit>(object)->set_property_field(p_name, p_value, p_changed_field);
  4403. _edit_request_change(object, p_name);
  4404. emit_signal(_prop_edited, p_name);
  4405. } else if (Object::cast_to<EditorDebuggerRemoteObjects>(object)) {
  4406. Object::cast_to<EditorDebuggerRemoteObjects>(object)->set_property_field(p_name, p_value, p_changed_field);
  4407. _edit_request_change(object, p_name);
  4408. emit_signal(_prop_edited, p_name);
  4409. } else {
  4410. undo_redo->create_action(vformat(TTR("Set %s"), p_name), UndoRedo::MERGE_ENDS, nullptr, false, mark_unsaved);
  4411. undo_redo->add_do_property(object, p_name, p_value);
  4412. bool valid = false;
  4413. Variant value = object->get(p_name, &valid);
  4414. Variant::Type type = p_value.get_type();
  4415. if (valid) {
  4416. if (Object::cast_to<Control>(object) && (p_name == "anchors_preset" || p_name == "layout_mode")) {
  4417. undo_redo->add_undo_method(object, "_edit_set_state", Object::cast_to<Control>(object)->_edit_get_state());
  4418. } else {
  4419. undo_redo->add_undo_property(object, p_name, value);
  4420. }
  4421. Node *N = Object::cast_to<Node>(object);
  4422. bool double_counting = Object::cast_to<Node>(p_value) == N || Object::cast_to<Node>(value) == N;
  4423. if (N && !double_counting && (type == Variant::OBJECT || type == Variant::ARRAY || type == Variant::DICTIONARY) && value != p_value) {
  4424. undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", value, N, true);
  4425. undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, false);
  4426. // Perhaps an inefficient way of updating the resource count.
  4427. // We could go in depth and check which Resource values changed/got removed and which ones stayed the same, but this is more readable at the moment.
  4428. undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, true);
  4429. undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", value, N, false);
  4430. }
  4431. }
  4432. List<StringName> linked_properties;
  4433. ClassDB::get_linked_properties_info(object->get_class_name(), p_name, &linked_properties);
  4434. for (const StringName &linked_prop : linked_properties) {
  4435. valid = false;
  4436. Variant undo_value = object->get(linked_prop, &valid);
  4437. if (valid) {
  4438. undo_redo->add_undo_property(object, linked_prop, undo_value);
  4439. }
  4440. }
  4441. PackedStringArray linked_properties_dynamic = object->call("_get_linked_undo_properties", p_name, p_value);
  4442. for (int i = 0; i < linked_properties_dynamic.size(); i++) {
  4443. valid = false;
  4444. Variant undo_value = object->get(linked_properties_dynamic[i], &valid);
  4445. if (valid) {
  4446. undo_redo->add_undo_property(object, linked_properties_dynamic[i], undo_value);
  4447. }
  4448. }
  4449. Variant v_undo_redo = undo_redo;
  4450. Variant v_object = object;
  4451. Variant v_name = p_name;
  4452. const Vector<Callable> &callbacks = EditorNode::get_editor_data().get_undo_redo_inspector_hook_callback();
  4453. for (int i = 0; i < callbacks.size(); i++) {
  4454. const Callable &callback = callbacks[i];
  4455. const Variant *p_arguments[] = { &v_undo_redo, &v_object, &v_name, &p_value };
  4456. Variant return_value;
  4457. Callable::CallError call_error;
  4458. callback.callp(p_arguments, 4, return_value, call_error);
  4459. if (call_error.error != Callable::CallError::CALL_OK) {
  4460. ERR_PRINT("Invalid UndoRedo callback.");
  4461. }
  4462. }
  4463. if (p_refresh_all) {
  4464. undo_redo->add_do_method(this, "_edit_request_change", object, "");
  4465. undo_redo->add_undo_method(this, "_edit_request_change", object, "");
  4466. } else {
  4467. undo_redo->add_do_method(this, "_edit_request_change", object, p_name);
  4468. undo_redo->add_undo_method(this, "_edit_request_change", object, p_name);
  4469. }
  4470. Resource *r = Object::cast_to<Resource>(object);
  4471. if (r) {
  4472. //Setting a Subresource. Since there's possibly multiple Nodes referencing 'r', we need to link them to the Subresource.
  4473. List<Node *> shared_nodes = EditorNode::get_singleton()->get_resource_node_list(r);
  4474. for (Node *N : shared_nodes) {
  4475. if ((type == Variant::OBJECT || type == Variant::ARRAY || type == Variant::DICTIONARY) && value != p_value) {
  4476. undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", value, N, true);
  4477. undo_redo->add_do_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, false);
  4478. // Perhaps an inefficient way of updating the resource count.
  4479. // We could go in depth and check which Resource values changed/got removed and which ones stayed the same, but this is more readable at the moment.
  4480. undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", p_value, N, true);
  4481. undo_redo->add_undo_method(EditorNode::get_singleton(), "update_node_reference", value, N, false);
  4482. }
  4483. }
  4484. if (String(p_name) == "resource_local_to_scene") {
  4485. bool prev = object->get(p_name);
  4486. bool next = p_value;
  4487. if (next) {
  4488. undo_redo->add_do_method(r, "setup_local_to_scene");
  4489. }
  4490. if (prev) {
  4491. undo_redo->add_undo_method(r, "setup_local_to_scene");
  4492. }
  4493. }
  4494. }
  4495. undo_redo->add_do_method(this, "emit_signal", _prop_edited, p_name);
  4496. undo_redo->add_undo_method(this, "emit_signal", _prop_edited, p_name);
  4497. undo_redo->commit_action();
  4498. }
  4499. if (editor_property_map.has(p_name)) {
  4500. for (EditorProperty *E : editor_property_map[p_name]) {
  4501. E->update_editor_property_status();
  4502. }
  4503. }
  4504. }
  4505. void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing, bool p_update_all) {
  4506. // The "changing" variable must be true for properties that trigger events as typing occurs,
  4507. // like "text_changed" signal. E.g. text property of Label, Button, RichTextLabel, etc.
  4508. if (p_changing) {
  4509. changing++;
  4510. }
  4511. _edit_set(p_path, p_value, p_update_all, p_name);
  4512. if (p_changing) {
  4513. changing--;
  4514. }
  4515. if (restart_request_props.has(p_path)) {
  4516. emit_signal(SNAME("restart_requested"));
  4517. }
  4518. }
  4519. void EditorInspector::_multiple_properties_changed(const Vector<String> &p_paths, const Array &p_values, bool p_changing) {
  4520. ERR_FAIL_COND(p_paths.is_empty() || p_values.is_empty());
  4521. ERR_FAIL_COND(p_paths.size() != p_values.size());
  4522. String names;
  4523. for (int i = 0; i < p_paths.size(); i++) {
  4524. if (i > 0) {
  4525. names += ",";
  4526. }
  4527. names += p_paths[i];
  4528. }
  4529. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4530. // TRANSLATORS: This is describing a change to multiple properties at once. The parameter is a list of property names.
  4531. undo_redo->create_action(vformat(TTR("Set Multiple: %s"), names), UndoRedo::MERGE_ENDS);
  4532. for (int i = 0; i < p_paths.size(); i++) {
  4533. _edit_set(p_paths[i], p_values[i], false, "");
  4534. if (restart_request_props.has(p_paths[i])) {
  4535. emit_signal(SNAME("restart_requested"));
  4536. }
  4537. }
  4538. if (p_changing) {
  4539. changing++;
  4540. }
  4541. undo_redo->commit_action();
  4542. if (p_changing) {
  4543. changing--;
  4544. }
  4545. }
  4546. void EditorInspector::_property_keyed(const String &p_path, bool p_advance) {
  4547. if (!object) {
  4548. return;
  4549. }
  4550. // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
  4551. const Variant args[3] = { p_path, object->get(p_path), p_advance };
  4552. const Variant *argp[3] = { &args[0], &args[1], &args[2] };
  4553. emit_signalp(SNAME("property_keyed"), argp, 3);
  4554. }
  4555. void EditorInspector::_property_deleted(const String &p_path) {
  4556. if (!object) {
  4557. return;
  4558. }
  4559. if (p_path.begins_with("metadata/")) {
  4560. String name = p_path.replace_first("metadata/", "");
  4561. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4562. undo_redo->create_action(vformat(TTR("Remove metadata %s"), name));
  4563. undo_redo->add_do_method(object, "remove_meta", name);
  4564. undo_redo->add_undo_method(object, "set_meta", name, object->get_meta(name));
  4565. undo_redo->commit_action();
  4566. }
  4567. if (restart_request_props.has(p_path)) {
  4568. emit_signal(SNAME("restart_requested"));
  4569. }
  4570. emit_signal(SNAME("property_deleted"), p_path);
  4571. }
  4572. void EditorInspector::_property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance) {
  4573. if (!object) {
  4574. return;
  4575. }
  4576. // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
  4577. const Variant args[3] = { p_path, p_value, p_advance };
  4578. const Variant *argp[3] = { &args[0], &args[1], &args[2] };
  4579. emit_signalp(SNAME("property_keyed"), argp, 3);
  4580. }
  4581. void EditorInspector::_property_checked(const String &p_path, bool p_checked) {
  4582. if (!object) {
  4583. return;
  4584. }
  4585. //property checked
  4586. if (autoclear) {
  4587. if (!p_checked) {
  4588. _edit_set(p_path, Variant(), false, "");
  4589. } else {
  4590. Variant to_create;
  4591. Control *control = Object::cast_to<Control>(object);
  4592. if (control && p_path.begins_with("theme_override_")) {
  4593. to_create = control->get_used_theme_item(p_path);
  4594. } else {
  4595. List<PropertyInfo> pinfo;
  4596. object->get_property_list(&pinfo);
  4597. for (const PropertyInfo &E : pinfo) {
  4598. if (E.name == p_path) {
  4599. Callable::CallError ce;
  4600. Variant::construct(E.type, to_create, nullptr, 0, ce);
  4601. break;
  4602. }
  4603. }
  4604. }
  4605. _edit_set(p_path, to_create, false, "");
  4606. }
  4607. if (editor_property_map.has(p_path)) {
  4608. for (EditorProperty *E : editor_property_map[p_path]) {
  4609. E->set_checked(p_checked);
  4610. E->update_property();
  4611. E->update_editor_property_status();
  4612. E->update_cache();
  4613. }
  4614. }
  4615. } else {
  4616. emit_signal(SNAME("property_toggled"), p_path, p_checked);
  4617. }
  4618. }
  4619. void EditorInspector::_property_pinned(const String &p_path, bool p_pinned) {
  4620. if (!object) {
  4621. return;
  4622. }
  4623. Node *node = Object::cast_to<Node>(object);
  4624. ERR_FAIL_NULL(node);
  4625. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4626. undo_redo->create_action(vformat(p_pinned ? TTR("Pinned %s") : TTR("Unpinned %s"), p_path));
  4627. undo_redo->add_do_method(node, "_set_property_pinned", p_path, p_pinned);
  4628. undo_redo->add_undo_method(node, "_set_property_pinned", p_path, !p_pinned);
  4629. if (editor_property_map.has(p_path)) {
  4630. for (List<EditorProperty *>::Element *E = editor_property_map[p_path].front(); E; E = E->next()) {
  4631. undo_redo->add_do_method(E->get(), "_update_editor_property_status");
  4632. undo_redo->add_undo_method(E->get(), "_update_editor_property_status");
  4633. }
  4634. }
  4635. undo_redo->commit_action();
  4636. }
  4637. void EditorInspector::_property_selected(const String &p_path, int p_focusable) {
  4638. property_selected = p_path;
  4639. property_focusable = p_focusable;
  4640. // Deselect the others.
  4641. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4642. if (F.key == property_selected) {
  4643. continue;
  4644. }
  4645. for (EditorProperty *E : F.value) {
  4646. if (E->is_selected()) {
  4647. E->deselect();
  4648. }
  4649. }
  4650. }
  4651. emit_signal(SNAME("property_selected"), p_path);
  4652. }
  4653. void EditorInspector::_object_id_selected(const String &p_path, ObjectID p_id) {
  4654. emit_signal(SNAME("object_id_selected"), p_id);
  4655. }
  4656. void EditorInspector::_resource_selected(const String &p_path, Ref<Resource> p_resource) {
  4657. emit_signal(SNAME("resource_selected"), p_resource, p_path);
  4658. }
  4659. void EditorInspector::_node_removed(Node *p_node) {
  4660. if (p_node == object) {
  4661. edit(nullptr);
  4662. }
  4663. }
  4664. void EditorInspector::_update_current_favorites() {
  4665. current_favorites.clear();
  4666. if (!can_favorite) {
  4667. return;
  4668. }
  4669. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  4670. // Fetch script properties.
  4671. Ref<Script> scr = object->get_script();
  4672. if (scr.is_valid()) {
  4673. List<PropertyInfo> plist;
  4674. // FIXME: Only properties from a saved script will be available, unsaved ones will be ignored.
  4675. // Can cause a little wonkiness, while nothing serious, would be nice to find a way to get
  4676. // unsaved ones without needing to get the entire property list of an object.
  4677. scr->get_script_property_list(&plist);
  4678. String path;
  4679. HashMap<String, LocalVector<String>> props;
  4680. for (PropertyInfo &p : plist) {
  4681. if (p.usage & PROPERTY_USAGE_CATEGORY) {
  4682. path = favorites.has(p.hint_string) ? p.hint_string : String();
  4683. } else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && !path.is_empty()) {
  4684. props[path].push_back(p.name);
  4685. }
  4686. }
  4687. // Add favorited properties while removing invalid ones.
  4688. bool invalid_props = false;
  4689. for (const KeyValue<String, LocalVector<String>> &KV : props) {
  4690. path = KV.key;
  4691. for (int i = 0; i < favorites[path].size(); i++) {
  4692. String prop = favorites[path][i];
  4693. if (KV.value.has(prop)) {
  4694. current_favorites.append(prop);
  4695. } else {
  4696. invalid_props = true;
  4697. favorites[path].erase(prop);
  4698. i--;
  4699. }
  4700. }
  4701. if (favorites[path].is_empty()) {
  4702. favorites.erase(path);
  4703. }
  4704. }
  4705. if (invalid_props) {
  4706. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  4707. }
  4708. }
  4709. // Fetch built-in properties.
  4710. StringName class_name = object->get_class_name();
  4711. for (const KeyValue<String, PackedStringArray> &KV : favorites) {
  4712. if (ClassDB::is_parent_class(class_name, KV.key)) {
  4713. current_favorites.append_array(KV.value);
  4714. }
  4715. }
  4716. }
  4717. void EditorInspector::_set_property_favorited(const String &p_path, bool p_favorited) {
  4718. if (!object) {
  4719. return;
  4720. }
  4721. StringName validate_name = object->get_class_name();
  4722. StringName class_name;
  4723. String theme_property;
  4724. if (p_path.begins_with("theme_override_")) {
  4725. theme_property = p_path.get_slicec('/', 1);
  4726. }
  4727. while (!validate_name.is_empty()) {
  4728. class_name = validate_name;
  4729. if (!theme_property.is_empty()) { // Deal with theme properties.
  4730. bool found = false;
  4731. HashMap<String, DocData::ClassDoc>::ConstIterator F = EditorHelp::get_doc_data()->class_list.find(class_name);
  4732. if (F) {
  4733. for (const DocData::ThemeItemDoc &prop : F->value.theme_properties) {
  4734. if (prop.name == theme_property) {
  4735. found = true;
  4736. break;
  4737. }
  4738. }
  4739. }
  4740. if (found) {
  4741. break;
  4742. }
  4743. } else if (ClassDB::has_property(class_name, p_path, true)) { // Check if the property is built-in.
  4744. break;
  4745. }
  4746. validate_name = ClassDB::get_parent_class_nocheck(class_name);
  4747. }
  4748. // "script" isn't a real property, so a hack is necessary.
  4749. if (validate_name.is_empty() && p_path != "script") {
  4750. class_name = "";
  4751. }
  4752. if (class_name.is_empty()) {
  4753. // Check if it's part of a script.
  4754. Ref<Script> scr = object->get_script();
  4755. if (scr.is_valid()) {
  4756. List<PropertyInfo> plist;
  4757. scr->get_script_property_list(&plist);
  4758. String path;
  4759. for (PropertyInfo &p : plist) {
  4760. if (p.usage & PROPERTY_USAGE_CATEGORY) {
  4761. path = p.hint_string;
  4762. } else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && p.name == p_path) {
  4763. class_name = path;
  4764. break;
  4765. }
  4766. }
  4767. }
  4768. ERR_FAIL_COND_MSG(class_name.is_empty(), "Can't favorite invalid property. If said property was from a script and recently renamed, try saving it first.");
  4769. }
  4770. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  4771. if (p_favorited) {
  4772. current_favorites.append(p_path);
  4773. favorites[class_name].append(p_path);
  4774. } else {
  4775. current_favorites.erase(p_path);
  4776. if (favorites.has(class_name) && favorites[class_name].has(p_path)) {
  4777. if (favorites[class_name].size() > 1) {
  4778. favorites[class_name].erase(p_path);
  4779. } else {
  4780. favorites.erase(class_name);
  4781. }
  4782. }
  4783. }
  4784. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  4785. update_tree();
  4786. }
  4787. void EditorInspector::_clear_current_favorites() {
  4788. current_favorites.clear();
  4789. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  4790. Ref<Script> scr = object->get_script();
  4791. if (scr.is_valid()) {
  4792. List<PropertyInfo> plist;
  4793. scr->get_script_property_list(&plist);
  4794. for (PropertyInfo &p : plist) {
  4795. if (p.usage & PROPERTY_USAGE_CATEGORY && favorites.has(p.hint_string)) {
  4796. favorites.erase(p.hint_string);
  4797. }
  4798. }
  4799. }
  4800. StringName class_name = object->get_class_name();
  4801. while (class_name) {
  4802. if (favorites.has(class_name)) {
  4803. favorites.erase(class_name);
  4804. }
  4805. class_name = ClassDB::get_parent_class(class_name);
  4806. }
  4807. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  4808. update_tree();
  4809. }
  4810. void EditorInspector::_notification(int p_what) {
  4811. switch (p_what) {
  4812. case NOTIFICATION_TRANSLATION_CHANGED: {
  4813. if (property_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  4814. update_tree_pending = true;
  4815. }
  4816. } break;
  4817. case NOTIFICATION_THEME_CHANGED: {
  4818. if (updating_theme) {
  4819. break;
  4820. }
  4821. theme_cache.prop_subsection = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
  4822. initialize_section_theme(section_theme_cache, this);
  4823. initialize_category_theme(category_theme_cache, this);
  4824. initialize_property_theme(property_theme_cache, this);
  4825. } break;
  4826. case NOTIFICATION_READY: {
  4827. if (EditorFeatureProfileManager::get_singleton()) {
  4828. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &EditorInspector::_feature_profile_changed));
  4829. }
  4830. set_process(is_visible_in_tree());
  4831. if (!is_sub_inspector()) {
  4832. get_tree()->connect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  4833. }
  4834. } break;
  4835. case NOTIFICATION_PREDELETE: {
  4836. if (!is_sub_inspector() && is_inside_tree()) {
  4837. get_tree()->disconnect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  4838. }
  4839. edit(nullptr);
  4840. } break;
  4841. case NOTIFICATION_VISIBILITY_CHANGED: {
  4842. set_process(is_visible_in_tree());
  4843. } break;
  4844. case NOTIFICATION_PROCESS: {
  4845. if (update_scroll_request >= 0) {
  4846. callable_mp((Range *)get_v_scroll_bar(), &Range::set_value).call_deferred(update_scroll_request);
  4847. update_scroll_request = -1;
  4848. }
  4849. if (update_tree_pending) {
  4850. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  4851. } else if (refresh_countdown > 0) {
  4852. refresh_countdown -= get_process_delta_time();
  4853. if (refresh_countdown <= 0) {
  4854. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4855. for (EditorProperty *E : F.value) {
  4856. if (E && !E->is_cache_valid()) {
  4857. E->update_property();
  4858. E->update_editor_property_status();
  4859. E->update_cache();
  4860. }
  4861. }
  4862. }
  4863. for (EditorInspectorSection *S : sections) {
  4864. S->update_property();
  4865. }
  4866. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  4867. }
  4868. }
  4869. changing++;
  4870. if (update_tree_pending) {
  4871. update_tree();
  4872. update_tree_pending = false;
  4873. } else {
  4874. for (const StringName &prop : pending) {
  4875. if (editor_property_map.has(prop)) {
  4876. for (EditorProperty *E : editor_property_map[prop]) {
  4877. E->update_property();
  4878. E->update_editor_property_status();
  4879. E->update_cache();
  4880. }
  4881. }
  4882. }
  4883. for (EditorInspectorSection *S : sections) {
  4884. S->update_property();
  4885. }
  4886. }
  4887. pending.clear();
  4888. changing--;
  4889. } break;
  4890. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  4891. if (use_settings_name_style && EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
  4892. EditorPropertyNameProcessor::Style style = EditorPropertyNameProcessor::get_settings_style();
  4893. if (property_name_style != style) {
  4894. property_name_style = style;
  4895. update_tree_pending = true;
  4896. }
  4897. }
  4898. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/inspector")) {
  4899. update_tree_pending = true;
  4900. }
  4901. } break;
  4902. }
  4903. }
  4904. void EditorInspector::_changed_callback() {
  4905. //this is called when property change is notified via notify_property_list_changed()
  4906. if (object != nullptr) {
  4907. _update_current_favorites();
  4908. _edit_request_change(object, String());
  4909. }
  4910. }
  4911. void EditorInspector::_vscroll_changed(double p_offset) {
  4912. if (update_scroll_request >= 0) { //waiting, do nothing
  4913. return;
  4914. }
  4915. if (object) {
  4916. scroll_cache[object->get_instance_id()] = p_offset;
  4917. }
  4918. }
  4919. void EditorInspector::set_property_prefix(const String &p_prefix) {
  4920. property_prefix = p_prefix;
  4921. }
  4922. String EditorInspector::get_property_prefix() const {
  4923. return property_prefix;
  4924. }
  4925. void EditorInspector::add_custom_property_description(const String &p_class, const String &p_property, const String &p_description) {
  4926. const String key = vformat("property|%s|%s", p_class, p_property);
  4927. custom_property_descriptions[key] = p_description;
  4928. }
  4929. String EditorInspector::get_custom_property_description(const String &p_property) const {
  4930. HashMap<String, String>::ConstIterator E = custom_property_descriptions.find(p_property);
  4931. if (E) {
  4932. return E->value;
  4933. }
  4934. return "";
  4935. }
  4936. void EditorInspector::remap_doc_property_class(const String &p_property_prefix, const String &p_class) {
  4937. doc_property_class_remaps[p_property_prefix] = p_class;
  4938. }
  4939. void EditorInspector::set_object_class(const String &p_class) {
  4940. object_class = p_class;
  4941. }
  4942. String EditorInspector::get_object_class() const {
  4943. return object_class;
  4944. }
  4945. void EditorInspector::_feature_profile_changed() {
  4946. update_tree();
  4947. }
  4948. void EditorInspector::set_restrict_to_basic_settings(bool p_restrict) {
  4949. restrict_to_basic = p_restrict;
  4950. update_tree();
  4951. }
  4952. void EditorInspector::set_property_clipboard(const Variant &p_value) {
  4953. property_clipboard = p_value;
  4954. }
  4955. Variant EditorInspector::get_property_clipboard() {
  4956. return property_clipboard;
  4957. }
  4958. void EditorInspector::_show_add_meta_dialog() {
  4959. if (!add_meta_dialog) {
  4960. add_meta_dialog = memnew(AddMetadataDialog());
  4961. add_meta_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspector::_add_meta_confirm));
  4962. add_child(add_meta_dialog);
  4963. }
  4964. StringName dialog_title;
  4965. Node *node = Object::cast_to<Node>(object);
  4966. // If object is derived from Node use node name, if derived from Resource use classname.
  4967. dialog_title = node ? node->get_name() : StringName(object->get_class());
  4968. List<StringName> existing_meta_keys;
  4969. object->get_meta_list(&existing_meta_keys);
  4970. add_meta_dialog->open(dialog_title, existing_meta_keys);
  4971. }
  4972. void EditorInspector::_add_meta_confirm() {
  4973. // Ensure metadata is unfolded when adding a new metadata.
  4974. object->editor_set_section_unfold("metadata", true);
  4975. String name = add_meta_dialog->get_meta_name();
  4976. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4977. undo_redo->create_action(vformat(TTR("Add metadata %s"), name));
  4978. undo_redo->add_do_method(object, "set_meta", name, add_meta_dialog->get_meta_defval());
  4979. undo_redo->add_undo_method(object, "remove_meta", name);
  4980. undo_redo->commit_action();
  4981. }
  4982. EditorInspector *EditorInspector::_get_control_parent_inspector(Control *p_control) {
  4983. {
  4984. EditorInspector *inspector = Object::cast_to<EditorInspector>(p_control);
  4985. if (inspector) {
  4986. return inspector;
  4987. }
  4988. }
  4989. Control *parent = p_control->get_parent_control();
  4990. while (parent) {
  4991. EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
  4992. if (inspector) {
  4993. return inspector;
  4994. }
  4995. parent = parent->get_parent_control();
  4996. }
  4997. return nullptr;
  4998. }
  4999. void EditorInspector::_bind_methods() {
  5000. ClassDB::bind_method(D_METHOD("edit", "object"), &EditorInspector::edit);
  5001. ClassDB::bind_method("_edit_request_change", &EditorInspector::_edit_request_change);
  5002. ClassDB::bind_method("get_selected_path", &EditorInspector::get_selected_path);
  5003. ClassDB::bind_method("get_edited_object", &EditorInspector::get_edited_object);
  5004. ClassDB::bind_static_method("EditorInspector", D_METHOD("instantiate_property_editor", "object", "type", "path", "hint", "hint_text", "usage", "wide"), &EditorInspector::instantiate_property_editor, DEFVAL(false));
  5005. ADD_SIGNAL(MethodInfo("property_selected", PropertyInfo(Variant::STRING, "property")));
  5006. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::BOOL, "advance")));
  5007. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING, "property")));
  5008. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::STRING, "path")));
  5009. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
  5010. ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
  5011. ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "checked")));
  5012. ADD_SIGNAL(MethodInfo("edited_object_changed"));
  5013. ADD_SIGNAL(MethodInfo("restart_requested"));
  5014. }
  5015. EditorInspector::EditorInspector() {
  5016. object = nullptr;
  5017. base_vbox = memnew(VBoxContainer);
  5018. base_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
  5019. base_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  5020. add_child(base_vbox);
  5021. begin_vbox = memnew(VBoxContainer);
  5022. begin_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
  5023. base_vbox->add_child(begin_vbox);
  5024. begin_vbox->hide();
  5025. favorites_section = memnew(VBoxContainer);
  5026. favorites_section->set_theme_type_variation(SNAME("EditorInspectorContainer"));
  5027. base_vbox->add_child(favorites_section);
  5028. favorites_section->hide();
  5029. EditorInspectorCategory *favorites_category = memnew(EditorInspectorCategory);
  5030. favorites_category->set_as_favorite();
  5031. favorites_category->connect("unfavorite_all", callable_mp(this, &EditorInspector::_clear_current_favorites));
  5032. favorites_section->add_child(favorites_category);
  5033. favorites_vbox = memnew(VBoxContainer);
  5034. favorites_vbox->set_theme_type_variation(SNAME("EditorPropertyContainer"));
  5035. favorites_section->add_child(favorites_vbox);
  5036. favorites_vbox->hide();
  5037. favorites_groups_vbox = memnew(VBoxContainer);
  5038. favorites_groups_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
  5039. favorites_section->add_child(favorites_groups_vbox);
  5040. favorites_groups_vbox->hide();
  5041. favorites_separator = memnew(HSeparator);
  5042. favorites_section->add_child(favorites_separator);
  5043. favorites_separator->hide();
  5044. main_vbox = memnew(VBoxContainer);
  5045. main_vbox->set_theme_type_variation(SNAME("EditorInspectorContainer"));
  5046. base_vbox->add_child(main_vbox);
  5047. set_horizontal_scroll_mode(SCROLL_MODE_DISABLED);
  5048. set_follow_focus(true);
  5049. changing = 0;
  5050. search_box = nullptr;
  5051. _prop_edited = "property_edited";
  5052. set_process(false);
  5053. set_focus_mode(FocusMode::FOCUS_ALL);
  5054. property_focusable = -1;
  5055. get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspector::_vscroll_changed));
  5056. update_scroll_request = -1;
  5057. if (EditorSettings::get_singleton()) {
  5058. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  5059. } else {
  5060. //used when class is created by the docgen to dump default values of everything bindable, editorsettings may not be created
  5061. refresh_countdown = 0.33;
  5062. }
  5063. ED_SHORTCUT("property_editor/copy_value", TTRC("Copy Value"), KeyModifierMask::CMD_OR_CTRL | Key::C);
  5064. ED_SHORTCUT("property_editor/paste_value", TTRC("Paste Value"), KeyModifierMask::CMD_OR_CTRL | Key::V);
  5065. ED_SHORTCUT("property_editor/copy_property_path", TTRC("Copy Property Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::C);
  5066. // `use_settings_name_style` is true by default, set the name style accordingly.
  5067. set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
  5068. set_draw_focus_border(true);
  5069. set_scroll_on_drag_hover(true);
  5070. }