editor_inspector.cpp 184 KB

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