editor_inspector.cpp 158 KB

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