editor_inspector.cpp 169 KB

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