editor_inspector.cpp 180 KB

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