editor_inspector.cpp 195 KB

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