editor_inspector.cpp 189 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516
  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_OVERRIDE_FOR_PROJECT: {
  1104. emit_signal(SNAME("property_overridden"));
  1105. } break;
  1106. case MENU_FAVORITE_PROPERTY: {
  1107. emit_signal(SNAME("property_favorited"), property, !favorited);
  1108. queue_redraw();
  1109. } break;
  1110. case MENU_PIN_VALUE: {
  1111. emit_signal(SNAME("property_pinned"), property, !pinned);
  1112. queue_redraw();
  1113. } break;
  1114. case MENU_DELETE: {
  1115. accept_event();
  1116. emit_signal(SNAME("property_deleted"), property);
  1117. } break;
  1118. case MENU_REVERT_VALUE: {
  1119. accept_event();
  1120. get_viewport()->gui_release_focus();
  1121. bool is_valid_revert = false;
  1122. Variant revert_value = EditorPropertyRevert::get_property_revert_value(object, property, &is_valid_revert);
  1123. ERR_FAIL_COND(!is_valid_revert);
  1124. emit_changed(_get_revert_property(), revert_value);
  1125. update_property();
  1126. } break;
  1127. case MENU_OPEN_DOCUMENTATION: {
  1128. ScriptEditor::get_singleton()->goto_help(doc_path);
  1129. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1130. } break;
  1131. }
  1132. }
  1133. void EditorProperty::_bind_methods() {
  1134. ClassDB::bind_method(D_METHOD("set_label", "text"), &EditorProperty::set_label);
  1135. ClassDB::bind_method(D_METHOD("get_label"), &EditorProperty::get_label);
  1136. ClassDB::bind_method(D_METHOD("set_read_only", "read_only"), &EditorProperty::set_read_only);
  1137. ClassDB::bind_method(D_METHOD("is_read_only"), &EditorProperty::is_read_only);
  1138. ClassDB::bind_method(D_METHOD("set_draw_label", "draw_label"), &EditorProperty::set_draw_label);
  1139. ClassDB::bind_method(D_METHOD("is_draw_label"), &EditorProperty::is_draw_label);
  1140. ClassDB::bind_method(D_METHOD("set_draw_background", "draw_background"), &EditorProperty::set_draw_background);
  1141. ClassDB::bind_method(D_METHOD("is_draw_background"), &EditorProperty::is_draw_background);
  1142. ClassDB::bind_method(D_METHOD("set_checkable", "checkable"), &EditorProperty::set_checkable);
  1143. ClassDB::bind_method(D_METHOD("is_checkable"), &EditorProperty::is_checkable);
  1144. ClassDB::bind_method(D_METHOD("set_checked", "checked"), &EditorProperty::set_checked);
  1145. ClassDB::bind_method(D_METHOD("is_checked"), &EditorProperty::is_checked);
  1146. ClassDB::bind_method(D_METHOD("set_draw_warning", "draw_warning"), &EditorProperty::set_draw_warning);
  1147. ClassDB::bind_method(D_METHOD("is_draw_warning"), &EditorProperty::is_draw_warning);
  1148. ClassDB::bind_method(D_METHOD("set_keying", "keying"), &EditorProperty::set_keying);
  1149. ClassDB::bind_method(D_METHOD("is_keying"), &EditorProperty::is_keying);
  1150. ClassDB::bind_method(D_METHOD("set_deletable", "deletable"), &EditorProperty::set_deletable);
  1151. ClassDB::bind_method(D_METHOD("is_deletable"), &EditorProperty::is_deletable);
  1152. ClassDB::bind_method(D_METHOD("get_edited_property"), &EditorProperty::get_edited_property);
  1153. ClassDB::bind_method(D_METHOD("get_edited_object"), &EditorProperty::get_edited_object);
  1154. ClassDB::bind_method(D_METHOD("update_property"), &EditorProperty::update_property);
  1155. ClassDB::bind_method(D_METHOD("add_focusable", "control"), &EditorProperty::add_focusable);
  1156. ClassDB::bind_method(D_METHOD("set_bottom_editor", "editor"), &EditorProperty::set_bottom_editor);
  1157. ClassDB::bind_method(D_METHOD("set_selectable", "selectable"), &EditorProperty::set_selectable);
  1158. ClassDB::bind_method(D_METHOD("is_selectable"), &EditorProperty::is_selectable);
  1159. ClassDB::bind_method(D_METHOD("set_use_folding", "use_folding"), &EditorProperty::set_use_folding);
  1160. ClassDB::bind_method(D_METHOD("is_using_folding"), &EditorProperty::is_using_folding);
  1161. ClassDB::bind_method(D_METHOD("set_name_split_ratio", "ratio"), &EditorProperty::set_name_split_ratio);
  1162. ClassDB::bind_method(D_METHOD("get_name_split_ratio"), &EditorProperty::get_name_split_ratio);
  1163. ClassDB::bind_method(D_METHOD("deselect"), &EditorProperty::deselect);
  1164. ClassDB::bind_method(D_METHOD("is_selected"), &EditorProperty::is_selected);
  1165. ClassDB::bind_method(D_METHOD("select", "focusable"), &EditorProperty::select, DEFVAL(-1));
  1166. ClassDB::bind_method(D_METHOD("set_object_and_property", "object", "property"), &EditorProperty::set_object_and_property);
  1167. ClassDB::bind_method(D_METHOD("set_label_reference", "control"), &EditorProperty::set_label_reference);
  1168. ClassDB::bind_method(D_METHOD("emit_changed", "property", "value", "field", "changing"), &EditorProperty::emit_changed, DEFVAL(StringName()), DEFVAL(false));
  1169. ADD_PROPERTY(PropertyInfo(Variant::STRING, "label"), "set_label", "get_label");
  1170. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "read_only"), "set_read_only", "is_read_only");
  1171. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_label"), "set_draw_label", "is_draw_label");
  1172. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_background"), "set_draw_background", "is_draw_background");
  1173. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checkable"), "set_checkable", "is_checkable");
  1174. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "checked"), "set_checked", "is_checked");
  1175. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_warning"), "set_draw_warning", "is_draw_warning");
  1176. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keying"), "set_keying", "is_keying");
  1177. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deletable"), "set_deletable", "is_deletable");
  1178. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selectable"), "set_selectable", "is_selectable");
  1179. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_folding"), "set_use_folding", "is_using_folding");
  1180. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "name_split_ratio"), "set_name_split_ratio", "get_name_split_ratio");
  1181. ADD_SIGNAL(MethodInfo("property_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT), PropertyInfo(Variant::STRING_NAME, "field"), PropertyInfo(Variant::BOOL, "changing")));
  1182. ADD_SIGNAL(MethodInfo("multiple_properties_changed", PropertyInfo(Variant::PACKED_STRING_ARRAY, "properties"), PropertyInfo(Variant::ARRAY, "value")));
  1183. ADD_SIGNAL(MethodInfo("property_keyed", PropertyInfo(Variant::STRING_NAME, "property")));
  1184. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING_NAME, "property")));
  1185. ADD_SIGNAL(MethodInfo("property_keyed_with_value", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::NIL, "value", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1186. ADD_SIGNAL(MethodInfo("property_checked", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "checked")));
  1187. ADD_SIGNAL(MethodInfo("property_overridden"));
  1188. ADD_SIGNAL(MethodInfo("property_favorited", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "favorited")));
  1189. ADD_SIGNAL(MethodInfo("property_pinned", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "pinned")));
  1190. ADD_SIGNAL(MethodInfo("property_can_revert_changed", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "can_revert")));
  1191. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
  1192. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::INT, "id")));
  1193. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "focusable_idx")));
  1194. GDVIRTUAL_BIND(_update_property)
  1195. GDVIRTUAL_BIND(_set_read_only, "read_only")
  1196. ClassDB::bind_method(D_METHOD("_update_editor_property_status"), &EditorProperty::update_editor_property_status);
  1197. }
  1198. EditorProperty::EditorProperty() {
  1199. set_focus_mode(FOCUS_ACCESSIBILITY);
  1200. object = nullptr;
  1201. split_ratio = 0.5;
  1202. text_size = 0;
  1203. property_usage = 0;
  1204. selected_focusable = -1;
  1205. label_reference = nullptr;
  1206. bottom_editor = nullptr;
  1207. menu = nullptr;
  1208. set_process_shortcut_input(true);
  1209. }
  1210. void EditorProperty::_update_popup() {
  1211. if (menu) {
  1212. menu->clear();
  1213. } else {
  1214. menu = memnew(PopupMenu);
  1215. add_child(menu);
  1216. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorProperty::menu_option));
  1217. }
  1218. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("ActionCopy")), ED_GET_SHORTCUT("property_editor/copy_value"), MENU_COPY_VALUE);
  1219. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("ActionPaste")), ED_GET_SHORTCUT("property_editor/paste_value"), MENU_PASTE_VALUE);
  1220. menu->add_icon_shortcut(get_editor_theme_icon(SNAME("CopyNodePath")), ED_GET_SHORTCUT("property_editor/copy_property_path"), MENU_COPY_PROPERTY_PATH);
  1221. menu->set_item_disabled(MENU_PASTE_VALUE, is_read_only());
  1222. menu->set_item_disabled(MENU_COPY_PROPERTY_PATH, internal);
  1223. if (can_favorite || !pin_hidden) {
  1224. menu->add_separator();
  1225. }
  1226. if (can_favorite) {
  1227. if (favorited) {
  1228. menu->add_icon_item(get_editor_theme_icon(SNAME("Unfavorite")), TTR("Unfavorite Property"), MENU_FAVORITE_PROPERTY);
  1229. menu->set_item_tooltip(menu->get_item_index(MENU_FAVORITE_PROPERTY), TTR("Make this property be put back at its original place."));
  1230. } else {
  1231. // TRANSLATORS: This is a menu item to add a property to the favorites.
  1232. menu->add_icon_item(get_editor_theme_icon(SNAME("Favorites")), TTR("Favorite Property"), MENU_FAVORITE_PROPERTY);
  1233. 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."));
  1234. }
  1235. }
  1236. if (!pin_hidden) {
  1237. if (can_pin) {
  1238. menu->add_icon_check_item(get_editor_theme_icon(SNAME("Pin")), TTR("Pin Value"), MENU_PIN_VALUE);
  1239. menu->set_item_checked(menu->get_item_index(MENU_PIN_VALUE), pinned);
  1240. } else {
  1241. 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);
  1242. menu->set_item_disabled(menu->get_item_index(MENU_PIN_VALUE), true);
  1243. }
  1244. 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."));
  1245. }
  1246. if (deletable || can_revert || can_override) {
  1247. menu->add_separator();
  1248. if (can_override) {
  1249. menu->add_icon_item(get_editor_theme_icon(SNAME("Override")), TTRC("Override for Project"), MENU_OVERRIDE_FOR_PROJECT);
  1250. }
  1251. if (deletable) {
  1252. menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete Property"), MENU_DELETE);
  1253. }
  1254. if (can_revert) {
  1255. menu->add_icon_item(get_editor_theme_icon(SNAME("Reload")), TTR("Revert Value"), MENU_REVERT_VALUE);
  1256. }
  1257. }
  1258. if (!doc_path.is_empty()) {
  1259. menu->add_separator();
  1260. menu->add_icon_item(get_editor_theme_icon(SNAME("Help")), TTR("Open Documentation"), MENU_OPEN_DOCUMENTATION);
  1261. }
  1262. }
  1263. ////////////////////////////////////////////////
  1264. ////////////////////////////////////////////////
  1265. void EditorInspectorPlugin::add_custom_control(Control *control) {
  1266. AddedEditor ae;
  1267. ae.property_editor = control;
  1268. added_editors.push_back(ae);
  1269. }
  1270. void EditorInspectorPlugin::add_property_editor(const String &p_for_property, Control *p_prop, bool p_add_to_end, const String &p_label) {
  1271. AddedEditor ae;
  1272. ae.properties.push_back(p_for_property);
  1273. ae.property_editor = p_prop;
  1274. ae.add_to_end = p_add_to_end;
  1275. ae.label = p_label;
  1276. added_editors.push_back(ae);
  1277. }
  1278. void EditorInspectorPlugin::add_property_editor_for_multiple_properties(const String &p_label, const Vector<String> &p_properties, Control *p_prop) {
  1279. AddedEditor ae;
  1280. ae.properties = p_properties;
  1281. ae.property_editor = p_prop;
  1282. ae.label = p_label;
  1283. added_editors.push_back(ae);
  1284. }
  1285. bool EditorInspectorPlugin::can_handle(Object *p_object) {
  1286. bool success = false;
  1287. GDVIRTUAL_CALL(_can_handle, p_object, success);
  1288. return success;
  1289. }
  1290. void EditorInspectorPlugin::parse_begin(Object *p_object) {
  1291. GDVIRTUAL_CALL(_parse_begin, p_object);
  1292. }
  1293. void EditorInspectorPlugin::parse_category(Object *p_object, const String &p_category) {
  1294. GDVIRTUAL_CALL(_parse_category, p_object, p_category);
  1295. }
  1296. void EditorInspectorPlugin::parse_group(Object *p_object, const String &p_group) {
  1297. GDVIRTUAL_CALL(_parse_group, p_object, p_group);
  1298. }
  1299. 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) {
  1300. bool ret = false;
  1301. GDVIRTUAL_CALL(_parse_property, p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide, ret);
  1302. return ret;
  1303. }
  1304. void EditorInspectorPlugin::parse_end(Object *p_object) {
  1305. GDVIRTUAL_CALL(_parse_end, p_object);
  1306. }
  1307. void EditorInspectorPlugin::_bind_methods() {
  1308. ClassDB::bind_method(D_METHOD("add_custom_control", "control"), &EditorInspectorPlugin::add_custom_control);
  1309. ClassDB::bind_method(D_METHOD("add_property_editor", "property", "editor", "add_to_end", "label"), &EditorInspectorPlugin::add_property_editor, DEFVAL(false), DEFVAL(String()));
  1310. ClassDB::bind_method(D_METHOD("add_property_editor_for_multiple_properties", "label", "properties", "editor"), &EditorInspectorPlugin::add_property_editor_for_multiple_properties);
  1311. GDVIRTUAL_BIND(_can_handle, "object")
  1312. GDVIRTUAL_BIND(_parse_begin, "object")
  1313. GDVIRTUAL_BIND(_parse_category, "object", "category")
  1314. GDVIRTUAL_BIND(_parse_group, "object", "group")
  1315. GDVIRTUAL_BIND(_parse_property, "object", "type", "name", "hint_type", "hint_string", "usage_flags", "wide");
  1316. GDVIRTUAL_BIND(_parse_end, "object")
  1317. }
  1318. ////////////////////////////////////////////////
  1319. ////////////////////////////////////////////////
  1320. void EditorInspectorCategory::_notification(int p_what) {
  1321. switch (p_what) {
  1322. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  1323. RID ae = get_accessibility_element();
  1324. ERR_FAIL_COND(ae.is_null());
  1325. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  1326. DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Category: %s"), label));
  1327. DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Category: %s"), label));
  1328. DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
  1329. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &EditorInspectorCategory::_accessibility_action_menu));
  1330. } break;
  1331. case NOTIFICATION_THEME_CHANGED: {
  1332. if (menu) {
  1333. if (is_favorite) {
  1334. menu->set_item_icon(menu->get_item_index(EditorInspector::MENU_UNFAVORITE_ALL), get_editor_theme_icon(SNAME("Unfavorite")));
  1335. } else {
  1336. menu->set_item_icon(menu->get_item_index(MENU_OPEN_DOCS), get_editor_theme_icon(SNAME("Help")));
  1337. }
  1338. }
  1339. } break;
  1340. case NOTIFICATION_DRAW: {
  1341. Ref<StyleBox> sb = get_theme_stylebox(SNAME("bg"));
  1342. draw_style_box(sb, Rect2(Vector2(), get_size()));
  1343. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1344. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1345. int hs = get_theme_constant(SNAME("h_separation"), SNAME("Tree"));
  1346. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  1347. int w = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
  1348. if (icon.is_valid()) {
  1349. w += hs + icon_size;
  1350. }
  1351. w = MIN(w, get_size().width - sb->get_minimum_size().width);
  1352. int ofs = (get_size().width - w) / 2;
  1353. float v_margin_offset = sb->get_content_margin(SIDE_TOP) - sb->get_content_margin(SIDE_BOTTOM);
  1354. if (icon.is_valid()) {
  1355. Size2 rect_size = Size2(icon_size, icon_size);
  1356. Point2 rect_pos = Point2(ofs, (get_size().height - icon_size) / 2 + v_margin_offset).round();
  1357. if (is_layout_rtl()) {
  1358. rect_pos.x = get_size().width - rect_pos.x - icon_size;
  1359. }
  1360. draw_texture_rect(icon, Rect2(rect_pos, rect_size));
  1361. ofs += hs + icon_size;
  1362. w -= hs + icon_size;
  1363. }
  1364. Color color = get_theme_color(SceneStringName(font_color), SNAME("Tree"));
  1365. if (is_layout_rtl()) {
  1366. ofs = get_size().width - ofs - w;
  1367. }
  1368. float text_pos_y = font->get_ascent(font_size) + (get_size().height - font->get_height(font_size)) / 2 + v_margin_offset;
  1369. Point2 text_pos = Point2(ofs, text_pos_y).round();
  1370. draw_string(font, text_pos, label, HORIZONTAL_ALIGNMENT_LEFT, w, font_size, color);
  1371. } break;
  1372. }
  1373. }
  1374. void EditorInspectorCategory::_accessibility_action_menu(const Variant &p_data) {
  1375. if (!is_favorite) {
  1376. if (!menu) {
  1377. menu = memnew(PopupMenu);
  1378. menu->add_icon_item(get_editor_theme_icon(SNAME("Help")), TTR("Open Documentation"), MENU_OPEN_DOCS);
  1379. add_child(menu);
  1380. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorCategory::_handle_menu_option));
  1381. }
  1382. menu->set_item_disabled(menu->get_item_index(MENU_OPEN_DOCS), !EditorHelp::get_doc_data()->class_list.has(doc_class_name));
  1383. }
  1384. menu->set_position(get_screen_position());
  1385. menu->reset_size();
  1386. menu->popup();
  1387. }
  1388. Control *EditorInspectorCategory::make_custom_tooltip(const String &p_text) const {
  1389. // If it's not a doc tooltip, fallback to the default one.
  1390. if (doc_class_name.is_empty()) {
  1391. return nullptr;
  1392. }
  1393. return EditorHelpBitTooltip::show_tooltip(const_cast<EditorInspectorCategory *>(this), p_text);
  1394. }
  1395. void EditorInspectorCategory::set_as_favorite(EditorInspector *p_for_inspector) {
  1396. is_favorite = true;
  1397. menu = memnew(PopupMenu);
  1398. menu->add_item(TTR("Unfavorite All"), EditorInspector::MENU_UNFAVORITE_ALL);
  1399. add_child(menu);
  1400. menu->connect(SceneStringName(id_pressed), callable_mp(p_for_inspector, &EditorInspector::_handle_menu_option));
  1401. }
  1402. Size2 EditorInspectorCategory::get_minimum_size() const {
  1403. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1404. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1405. Size2 ms;
  1406. ms.height = font->get_height(font_size);
  1407. if (icon.is_valid()) {
  1408. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  1409. ms.height = MAX(icon_size, ms.height);
  1410. }
  1411. ms.height += get_theme_constant(SNAME("v_separation"), SNAME("Tree"));
  1412. const Ref<StyleBox> &bg_style = get_theme_stylebox(SNAME("bg"));
  1413. ms.height += bg_style->get_content_margin(SIDE_TOP) + bg_style->get_content_margin(SIDE_BOTTOM);
  1414. return ms;
  1415. }
  1416. void EditorInspectorCategory::_handle_menu_option(int p_option) {
  1417. switch (p_option) {
  1418. case MENU_OPEN_DOCS:
  1419. ScriptEditor::get_singleton()->goto_help("class:" + doc_class_name);
  1420. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1421. break;
  1422. }
  1423. }
  1424. void EditorInspectorCategory::gui_input(const Ref<InputEvent> &p_event) {
  1425. if (!is_favorite && doc_class_name.is_empty()) {
  1426. return;
  1427. }
  1428. const Ref<InputEventMouseButton> &mb_event = p_event;
  1429. if (mb_event.is_null() || !mb_event->is_pressed() || mb_event->get_button_index() != MouseButton::RIGHT) {
  1430. return;
  1431. }
  1432. if (!is_favorite) {
  1433. if (!menu) {
  1434. menu = memnew(PopupMenu);
  1435. menu->add_icon_item(get_editor_theme_icon(SNAME("Help")), TTR("Open Documentation"), MENU_OPEN_DOCS);
  1436. add_child(menu);
  1437. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorCategory::_handle_menu_option));
  1438. }
  1439. menu->set_item_disabled(menu->get_item_index(MENU_OPEN_DOCS), !EditorHelp::get_doc_data()->class_list.has(doc_class_name));
  1440. }
  1441. menu->set_position(get_screen_position() + mb_event->get_position());
  1442. menu->reset_size();
  1443. menu->popup();
  1444. }
  1445. EditorInspectorCategory::EditorInspectorCategory() {
  1446. set_focus_mode(FOCUS_ACCESSIBILITY);
  1447. }
  1448. ////////////////////////////////////////////////
  1449. ////////////////////////////////////////////////
  1450. void EditorInspectorSection::_test_unfold() {
  1451. if (!vbox_added) {
  1452. add_child(vbox);
  1453. move_child(vbox, 0);
  1454. vbox_added = true;
  1455. }
  1456. }
  1457. Ref<Texture2D> EditorInspectorSection::_get_arrow() {
  1458. Ref<Texture2D> arrow;
  1459. if (foldable) {
  1460. if (object->editor_is_section_unfolded(section)) {
  1461. arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree"));
  1462. } else {
  1463. if (is_layout_rtl()) {
  1464. arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));
  1465. } else {
  1466. arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));
  1467. }
  1468. }
  1469. }
  1470. return arrow;
  1471. }
  1472. Ref<Texture2D> EditorInspectorSection::_get_checkbox() {
  1473. Ref<Texture2D> checkbox;
  1474. if (checkable) {
  1475. if (checked) {
  1476. checkbox = get_editor_theme_icon(SNAME("GuiChecked"));
  1477. } else {
  1478. checkbox = get_editor_theme_icon(SNAME("GuiUnchecked"));
  1479. }
  1480. }
  1481. return checkbox;
  1482. }
  1483. int EditorInspectorSection::_get_header_height() {
  1484. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1485. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1486. int header_height = font->get_height(font_size);
  1487. Ref<Texture2D> arrow = _get_arrow();
  1488. if (arrow.is_valid()) {
  1489. header_height = MAX(header_height, arrow->get_height());
  1490. }
  1491. header_height += get_theme_constant(SNAME("v_separation"), SNAME("Tree"));
  1492. return header_height;
  1493. }
  1494. void EditorInspectorSection::_notification(int p_what) {
  1495. switch (p_what) {
  1496. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  1497. RID ae = get_accessibility_element();
  1498. ERR_FAIL_COND(ae.is_null());
  1499. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  1500. DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Section: %s"), label));
  1501. DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Section: %s"), label));
  1502. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &EditorInspectorSection::_accessibility_action_collapse));
  1503. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &EditorInspectorSection::_accessibility_action_expand));
  1504. } break;
  1505. case NOTIFICATION_THEME_CHANGED: {
  1506. update_minimum_size();
  1507. bg_color = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
  1508. bg_color.a /= level;
  1509. int separation = get_theme_constant(SNAME("v_separation"), SNAME("EditorInspector"));
  1510. vbox->add_theme_constant_override(SNAME("separation"), separation);
  1511. } break;
  1512. case NOTIFICATION_SORT_CHILDREN: {
  1513. if (!vbox_added) {
  1514. return;
  1515. }
  1516. int inspector_margin = get_theme_constant(SNAME("inspector_margin"), EditorStringName(Editor));
  1517. int section_indent_size = get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
  1518. if (indent_depth > 0 && section_indent_size > 0) {
  1519. inspector_margin += indent_depth * section_indent_size;
  1520. }
  1521. Ref<StyleBoxFlat> section_indent_style = get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
  1522. if (indent_depth > 0 && section_indent_style.is_valid()) {
  1523. inspector_margin += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
  1524. }
  1525. Size2 size = get_size() - Vector2(inspector_margin, 0);
  1526. int header_height = _get_header_height();
  1527. Vector2 offset = Vector2(is_layout_rtl() ? 0 : inspector_margin, header_height);
  1528. for (int i = 0; i < get_child_count(); i++) {
  1529. Control *c = as_sortable_control(get_child(i));
  1530. if (!c) {
  1531. continue;
  1532. }
  1533. fit_child_in_rect(c, Rect2(offset, size));
  1534. }
  1535. } break;
  1536. case NOTIFICATION_DRAW: {
  1537. int section_indent = 0;
  1538. int section_indent_size = get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
  1539. if (indent_depth > 0 && section_indent_size > 0) {
  1540. section_indent = indent_depth * section_indent_size;
  1541. }
  1542. Ref<StyleBoxFlat> section_indent_style = get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
  1543. if (indent_depth > 0 && section_indent_style.is_valid()) {
  1544. section_indent += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
  1545. }
  1546. int header_width = get_size().width - section_indent;
  1547. int header_offset_x = 0.0;
  1548. bool rtl = is_layout_rtl();
  1549. if (!rtl) {
  1550. header_offset_x += section_indent;
  1551. }
  1552. bool can_click_unfold = vbox->get_child_count(false) != 0 && !(checkable && !checked && hide_feature);
  1553. // Draw header area.
  1554. int header_height = _get_header_height();
  1555. Rect2 header_rect = Rect2(Vector2(header_offset_x, 0.0), Vector2(header_width, header_height));
  1556. Color c = bg_color;
  1557. c.a *= 0.4;
  1558. if (foldable && header_rect.has_point(get_local_mouse_position())) {
  1559. c = c.lightened((can_click_unfold && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) ? -0.05 : 0.2);
  1560. }
  1561. draw_rect(header_rect, c);
  1562. // Draw header title, folding arrow and count of revertable properties.
  1563. {
  1564. int outer_margin = Math::round(2 * EDSCALE);
  1565. int separation = get_theme_constant(SNAME("h_separation"), SNAME("EditorInspectorSection"));
  1566. int margin_start = section_indent + outer_margin;
  1567. int margin_end = outer_margin;
  1568. // - Arrow.
  1569. Ref<Texture2D> arrow = _get_arrow();
  1570. if (arrow.is_valid()) {
  1571. Point2 arrow_position;
  1572. if (rtl) {
  1573. arrow_position.x = get_size().width - (margin_start + arrow->get_width());
  1574. } else {
  1575. arrow_position.x = margin_start;
  1576. }
  1577. arrow_position.y = (header_height - arrow->get_height()) / 2;
  1578. if (can_click_unfold) {
  1579. draw_texture(arrow, arrow_position);
  1580. }
  1581. margin_start += arrow->get_width() + separation;
  1582. }
  1583. Ref<Font> font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  1584. int font_size = get_theme_font_size(SNAME("bold_size"), EditorStringName(EditorFonts));
  1585. Color font_color = get_theme_color(SceneStringName(font_color), EditorStringName(Editor));
  1586. Ref<Font> light_font = get_theme_font(SNAME("main"), EditorStringName(EditorFonts));
  1587. int light_font_size = get_theme_font_size(SNAME("main_size"), EditorStringName(EditorFonts));
  1588. // - Checkbox.
  1589. Ref<Texture2D> checkbox = _get_checkbox();
  1590. if (checkbox.is_valid()) {
  1591. const String checkbox_text = TTR("On");
  1592. Size2 label_size = light_font->get_string_size(checkbox_text, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size);
  1593. Point2 checkbox_position;
  1594. Point2 label_position;
  1595. if (rtl) {
  1596. label_position.x = margin_start;
  1597. checkbox_position.x = margin_start + label_size.width + 2 * EDSCALE;
  1598. } else {
  1599. label_position.x = get_size().width - (margin_end + label_size.width);
  1600. checkbox_position.x = label_position.x - checkbox->get_width() - 2 * EDSCALE;
  1601. }
  1602. checkbox_position.y = (header_height - checkbox->get_height()) / 2;
  1603. label_position.y = light_font->get_ascent(light_font_size) + (header_height - label_size.height) / 2.0;
  1604. check_rect = Rect2(checkbox_position.x, 0, checkbox->get_width() + label_size.width + 2 * EDSCALE, header_height);
  1605. Color check_font_color = font_color;
  1606. Color checkbox_color(1, 1, 1);
  1607. if (check_hover) {
  1608. checkbox_color.r *= 1.2;
  1609. checkbox_color.g *= 1.2;
  1610. checkbox_color.b *= 1.2;
  1611. check_font_color = checked ? get_theme_color(SNAME("font_hover_pressed_color"), EditorStringName(Editor)) : get_theme_color(SNAME("font_hover_color"), EditorStringName(Editor));
  1612. } else if (checked) {
  1613. check_font_color = get_theme_color(SNAME("font_pressed_color"), EditorStringName(Editor));
  1614. }
  1615. draw_texture(checkbox, checkbox_position, checkbox_color);
  1616. draw_string(light_font, label_position, checkbox_text, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, check_font_color, TextServer::JUSTIFICATION_NONE);
  1617. margin_end += label_size.width + checkbox->get_width() + 6 * EDSCALE;
  1618. }
  1619. int available = get_size().width - (margin_start + margin_end);
  1620. // - Count of revertable properties.
  1621. String num_revertable_str;
  1622. int num_revertable_width = 0;
  1623. bool folded = (foldable || hide_feature) && !object->editor_is_section_unfolded(section);
  1624. if (folded && revertable_properties.size()) {
  1625. int label_width = font->get_string_size(label, HORIZONTAL_ALIGNMENT_LEFT, available, font_size, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS).x;
  1626. Color light_font_color = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
  1627. // Can we fit the long version of the revertable count text?
  1628. num_revertable_str = vformat(TTRN("(%d change)", "(%d changes)", revertable_properties.size()), revertable_properties.size());
  1629. num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
  1630. if (label_width + outer_margin + num_revertable_width > available) {
  1631. // We'll have to use the short version.
  1632. num_revertable_str = vformat("(%d)", revertable_properties.size());
  1633. num_revertable_width = light_font->get_string_size(num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, TextServer::JUSTIFICATION_NONE).x;
  1634. }
  1635. float text_offset_y = light_font->get_ascent(light_font_size) + (header_height - light_font->get_height(light_font_size)) / 2;
  1636. Point2 text_offset = Point2(margin_end, text_offset_y).round();
  1637. if (!rtl) {
  1638. text_offset.x = get_size().width - (text_offset.x + num_revertable_width);
  1639. }
  1640. draw_string(light_font, text_offset, num_revertable_str, HORIZONTAL_ALIGNMENT_LEFT, -1.0f, light_font_size, light_font_color, TextServer::JUSTIFICATION_NONE);
  1641. margin_end += num_revertable_width + outer_margin;
  1642. available -= num_revertable_width + outer_margin;
  1643. }
  1644. // - Label.
  1645. float text_offset_y = font->get_ascent(font_size) + (header_height - font->get_height(font_size)) / 2;
  1646. Point2 text_offset = Point2(margin_start, text_offset_y).round();
  1647. if (rtl) {
  1648. text_offset.x = margin_end;
  1649. }
  1650. if (object->has_method("_get_property_warning") && !String(object->call("_get_property_warning", related_enable_property)).is_empty()) {
  1651. font_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
  1652. }
  1653. HorizontalAlignment text_align = rtl ? HORIZONTAL_ALIGNMENT_RIGHT : HORIZONTAL_ALIGNMENT_LEFT;
  1654. draw_string(font, text_offset, label, text_align, available, font_size, font_color, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  1655. }
  1656. // Draw section indentation.
  1657. if (section_indent_style.is_valid() && section_indent > 0) {
  1658. Rect2 indent_rect = Rect2(Vector2(), Vector2(indent_depth * section_indent_size, get_size().height));
  1659. if (rtl) {
  1660. indent_rect.position.x = get_size().width - section_indent + section_indent_style->get_margin(SIDE_RIGHT);
  1661. } else {
  1662. indent_rect.position.x = section_indent_style->get_margin(SIDE_LEFT);
  1663. }
  1664. draw_style_box(section_indent_style, indent_rect);
  1665. }
  1666. } break;
  1667. case NOTIFICATION_DRAG_BEGIN: {
  1668. dropping_for_unfold = true;
  1669. } break;
  1670. case NOTIFICATION_DRAG_END: {
  1671. dropping_for_unfold = false;
  1672. } break;
  1673. case NOTIFICATION_MOUSE_ENTER: {
  1674. if (dropping_for_unfold) {
  1675. dropping_unfold_timer->start();
  1676. }
  1677. queue_redraw();
  1678. } break;
  1679. case NOTIFICATION_MOUSE_EXIT: {
  1680. if (dropping_for_unfold) {
  1681. dropping_unfold_timer->stop();
  1682. }
  1683. queue_redraw();
  1684. } break;
  1685. }
  1686. }
  1687. Size2 EditorInspectorSection::get_minimum_size() const {
  1688. Size2 ms;
  1689. for (int i = 0; i < get_child_count(); i++) {
  1690. Control *c = as_sortable_control(get_child(i));
  1691. if (!c) {
  1692. continue;
  1693. }
  1694. Size2 minsize = c->get_combined_minimum_size();
  1695. ms = ms.max(minsize);
  1696. }
  1697. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Tree"));
  1698. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Tree"));
  1699. ms.height += font->get_height(font_size) + get_theme_constant(SNAME("v_separation"), SNAME("Tree"));
  1700. ms.width += get_theme_constant(SNAME("inspector_margin"), EditorStringName(Editor));
  1701. int section_indent_size = get_theme_constant(SNAME("indent_size"), SNAME("EditorInspectorSection"));
  1702. if (indent_depth > 0 && section_indent_size > 0) {
  1703. ms.width += indent_depth * section_indent_size;
  1704. }
  1705. Ref<StyleBoxFlat> section_indent_style = get_theme_stylebox(SNAME("indent_box"), SNAME("EditorInspectorSection"));
  1706. if (indent_depth > 0 && section_indent_style.is_valid()) {
  1707. ms.width += section_indent_style->get_margin(SIDE_LEFT) + section_indent_style->get_margin(SIDE_RIGHT);
  1708. }
  1709. return ms;
  1710. }
  1711. EditorInspector *EditorInspectorSection::_get_parent_inspector() const {
  1712. Node *parent = get_parent();
  1713. while (parent) {
  1714. EditorInspector *ei = Object::cast_to<EditorInspector>(parent);
  1715. if (ei) {
  1716. return ei;
  1717. }
  1718. parent = parent->get_parent();
  1719. }
  1720. return nullptr;
  1721. }
  1722. Control *EditorInspectorSection::make_custom_tooltip(const String &p_text) const {
  1723. if (!checkable) {
  1724. return Container::make_custom_tooltip(p_text);
  1725. }
  1726. String symbol;
  1727. String prologue;
  1728. if (object->has_method("_get_property_warning")) {
  1729. const String custom_warning = object->call("_get_property_warning", related_enable_property);
  1730. if (!custom_warning.is_empty()) {
  1731. prologue = "[b][color=" + get_theme_color(SNAME("warning_color"), EditorStringName(Editor)).to_html(false) + "]" + custom_warning + "[/color][/b]";
  1732. }
  1733. }
  1734. symbol = p_text;
  1735. const EditorInspector *inspector = _get_parent_inspector();
  1736. if (inspector) {
  1737. const String custom_description = inspector->get_custom_property_description(p_text);
  1738. if (!custom_description.is_empty()) {
  1739. if (!prologue.is_empty()) {
  1740. prologue += '\n';
  1741. }
  1742. prologue += custom_description;
  1743. }
  1744. }
  1745. if (!symbol.is_empty() || !prologue.is_empty()) {
  1746. return EditorHelpBitTooltip::show_tooltip(const_cast<EditorInspectorSection *>(this), symbol, prologue);
  1747. }
  1748. return nullptr;
  1749. }
  1750. 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) {
  1751. section = p_section;
  1752. label = p_label;
  1753. object = p_object;
  1754. bg_color = p_bg_color;
  1755. foldable = p_foldable;
  1756. indent_depth = p_indent_depth;
  1757. level = p_level;
  1758. _test_unfold();
  1759. if (foldable) {
  1760. if (object->editor_is_section_unfolded(section)) {
  1761. vbox->show();
  1762. } else {
  1763. vbox->hide();
  1764. }
  1765. }
  1766. }
  1767. void EditorInspectorSection::gui_input(const Ref<InputEvent> &p_event) {
  1768. ERR_FAIL_COND(p_event.is_null());
  1769. bool has_children_to_show = vbox->get_child_count(false) != 0;
  1770. Ref<InputEventMouse> me = p_event;
  1771. if (me.is_valid()) {
  1772. bool new_check_hover = check_rect.has_point(me->get_position());
  1773. if (new_check_hover != check_hover) {
  1774. check_hover = new_check_hover;
  1775. queue_redraw();
  1776. }
  1777. }
  1778. Ref<InputEventKey> k = p_event;
  1779. if (k.is_valid() && k->is_pressed()) {
  1780. if (foldable && has_children_to_show && !(checkable && !checked && hide_feature) && k->is_action("ui_accept", true)) {
  1781. accept_event();
  1782. bool should_unfold = !object->editor_is_section_unfolded(section);
  1783. if (should_unfold) {
  1784. unfold();
  1785. } else {
  1786. fold();
  1787. }
  1788. }
  1789. }
  1790. Ref<InputEventMouseButton> mb = p_event;
  1791. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1792. if (object->editor_is_section_unfolded(section)) {
  1793. int header_height = _get_header_height();
  1794. if (mb->get_position().y >= header_height) {
  1795. return;
  1796. }
  1797. }
  1798. accept_event();
  1799. if (checkable && check_rect.has_point(mb->get_position())) {
  1800. checked = !checked;
  1801. emit_signal(SNAME("section_toggled_by_user"), related_enable_property, checked);
  1802. if (checked) {
  1803. unfold();
  1804. } else if (hide_feature) {
  1805. fold();
  1806. }
  1807. } else if (foldable) {
  1808. bool should_unfold = has_children_to_show && !(checkable && !checked && hide_feature) && !object->editor_is_section_unfolded(section);
  1809. if (should_unfold) {
  1810. unfold();
  1811. } else {
  1812. fold();
  1813. }
  1814. }
  1815. } else if (mb.is_valid() && !mb->is_pressed()) {
  1816. queue_redraw();
  1817. }
  1818. Ref<InputEventMouseMotion> mm = p_event;
  1819. if (mm.is_valid()) {
  1820. int header_height = _get_header_height();
  1821. Vector2 previous = mm->get_position() - mm->get_relative();
  1822. if (has_children_to_show && ((mm->get_position().y >= header_height) != (previous.y >= header_height))) {
  1823. queue_redraw();
  1824. }
  1825. }
  1826. }
  1827. String EditorInspectorSection::get_section() const {
  1828. return section;
  1829. }
  1830. VBoxContainer *EditorInspectorSection::get_vbox() {
  1831. return vbox;
  1832. }
  1833. void EditorInspectorSection::_accessibility_action_collapse(const Variant &p_data) {
  1834. fold();
  1835. }
  1836. void EditorInspectorSection::_accessibility_action_expand(const Variant &p_data) {
  1837. unfold();
  1838. }
  1839. void EditorInspectorSection::unfold() {
  1840. if (!foldable && !hide_feature) {
  1841. return;
  1842. }
  1843. _test_unfold();
  1844. object->editor_set_section_unfold(section, true);
  1845. vbox->show();
  1846. queue_redraw();
  1847. }
  1848. void EditorInspectorSection::fold() {
  1849. if (!foldable && !hide_feature) {
  1850. return;
  1851. }
  1852. if (!vbox_added) {
  1853. return;
  1854. }
  1855. object->editor_set_section_unfold(section, false);
  1856. vbox->hide();
  1857. queue_redraw();
  1858. }
  1859. void EditorInspectorSection::set_bg_color(const Color &p_bg_color) {
  1860. bg_color = p_bg_color;
  1861. queue_redraw();
  1862. }
  1863. void EditorInspectorSection::reset_timer() {
  1864. if (dropping_for_unfold && !dropping_unfold_timer->is_stopped()) {
  1865. dropping_unfold_timer->start();
  1866. }
  1867. }
  1868. void EditorInspectorSection::set_checkable(const String &p_related_check_property, bool p_hide_feature) {
  1869. if (checkable == !p_related_check_property.is_empty()) {
  1870. return;
  1871. }
  1872. hide_feature = p_hide_feature;
  1873. checkable = !p_related_check_property.is_empty();
  1874. related_enable_property = p_related_check_property;
  1875. queue_redraw();
  1876. if (InspectorDock::get_singleton()) {
  1877. if (checkable) {
  1878. InspectorDock::get_inspector_singleton()->connect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
  1879. } else {
  1880. InspectorDock::get_inspector_singleton()->disconnect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
  1881. }
  1882. }
  1883. }
  1884. void EditorInspectorSection::set_checked(bool p_checked) {
  1885. checked = p_checked;
  1886. if (hide_feature && !checked) {
  1887. fold();
  1888. } else {
  1889. unfold();
  1890. }
  1891. }
  1892. bool EditorInspectorSection::has_revertable_properties() const {
  1893. return !revertable_properties.is_empty();
  1894. }
  1895. void EditorInspectorSection::property_can_revert_changed(const String &p_path, bool p_can_revert) {
  1896. bool had_revertable_properties = has_revertable_properties();
  1897. if (p_can_revert) {
  1898. revertable_properties.insert(p_path);
  1899. } else {
  1900. revertable_properties.erase(p_path);
  1901. }
  1902. if (has_revertable_properties() != had_revertable_properties) {
  1903. queue_redraw();
  1904. }
  1905. }
  1906. void EditorInspectorSection::_property_edited(const String &p_property) {
  1907. if (!related_enable_property.is_empty() && p_property == related_enable_property) {
  1908. bool valid = false;
  1909. Variant value_checked = object->get(related_enable_property, &valid);
  1910. if (valid) {
  1911. set_checked(value_checked.operator bool());
  1912. }
  1913. }
  1914. }
  1915. void EditorInspectorSection::_bind_methods() {
  1916. ClassDB::bind_method(D_METHOD("setup", "section", "label", "object", "bg_color", "foldable", "indent_depth", "level"), &EditorInspectorSection::setup, DEFVAL(0), DEFVAL(1));
  1917. ClassDB::bind_method(D_METHOD("get_vbox"), &EditorInspectorSection::get_vbox);
  1918. ClassDB::bind_method(D_METHOD("unfold"), &EditorInspectorSection::unfold);
  1919. ClassDB::bind_method(D_METHOD("fold"), &EditorInspectorSection::fold);
  1920. ADD_SIGNAL(MethodInfo("section_toggled_by_user", PropertyInfo(Variant::STRING_NAME, "property"), PropertyInfo(Variant::BOOL, "value")));
  1921. }
  1922. EditorInspectorSection::EditorInspectorSection() {
  1923. set_focus_mode(FOCUS_ACCESSIBILITY);
  1924. vbox = memnew(VBoxContainer);
  1925. dropping_unfold_timer = memnew(Timer);
  1926. dropping_unfold_timer->set_wait_time(0.6);
  1927. dropping_unfold_timer->set_one_shot(true);
  1928. add_child(dropping_unfold_timer);
  1929. dropping_unfold_timer->connect("timeout", callable_mp(this, &EditorInspectorSection::unfold));
  1930. }
  1931. EditorInspectorSection::~EditorInspectorSection() {
  1932. if (!vbox_added) {
  1933. memdelete(vbox);
  1934. }
  1935. if (checkable && InspectorDock::get_singleton()) {
  1936. InspectorDock::get_inspector_singleton()->disconnect("property_edited", callable_mp(this, &EditorInspectorSection::_property_edited));
  1937. }
  1938. }
  1939. ////////////////////////////////////////////////
  1940. ////////////////////////////////////////////////
  1941. int EditorInspectorArray::_get_array_count() {
  1942. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  1943. List<PropertyInfo> object_property_list;
  1944. object->get_property_list(&object_property_list);
  1945. return _extract_properties_as_array(object_property_list).size();
  1946. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  1947. bool valid;
  1948. int count_val = object->get(count_property, &valid);
  1949. ERR_FAIL_COND_V_MSG(!valid, 0, vformat("%s is not a valid property to be used as array count.", count_property));
  1950. return count_val;
  1951. }
  1952. return 0;
  1953. }
  1954. void EditorInspectorArray::_add_button_pressed() {
  1955. _move_element(-1, -1);
  1956. }
  1957. void EditorInspectorArray::_paginator_page_changed(int p_page) {
  1958. emit_signal("page_change_request", p_page);
  1959. }
  1960. void EditorInspectorArray::_rmb_popup_id_pressed(int p_id) {
  1961. switch (p_id) {
  1962. case OPTION_MOVE_UP:
  1963. if (popup_array_index_pressed > 0) {
  1964. _move_element(popup_array_index_pressed, popup_array_index_pressed - 1);
  1965. }
  1966. break;
  1967. case OPTION_MOVE_DOWN:
  1968. if (popup_array_index_pressed < count - 1) {
  1969. _move_element(popup_array_index_pressed, popup_array_index_pressed + 2);
  1970. }
  1971. break;
  1972. case OPTION_NEW_BEFORE:
  1973. _move_element(-1, popup_array_index_pressed);
  1974. break;
  1975. case OPTION_NEW_AFTER:
  1976. _move_element(-1, popup_array_index_pressed + 1);
  1977. break;
  1978. case OPTION_REMOVE:
  1979. _move_element(popup_array_index_pressed, -1);
  1980. break;
  1981. case OPTION_CLEAR_ARRAY:
  1982. _clear_array();
  1983. break;
  1984. case OPTION_RESIZE_ARRAY:
  1985. new_size_spin_box->set_value(count);
  1986. resize_dialog->get_ok_button()->set_disabled(true);
  1987. resize_dialog->popup_centered(Size2(250, 0) * EDSCALE);
  1988. new_size_spin_box->get_line_edit()->grab_focus();
  1989. new_size_spin_box->get_line_edit()->select_all();
  1990. break;
  1991. default:
  1992. break;
  1993. }
  1994. }
  1995. void EditorInspectorArray::_control_dropping_draw() {
  1996. int drop_position = _drop_position();
  1997. if (dropping && drop_position >= 0) {
  1998. Vector2 from;
  1999. Vector2 to;
  2000. if (drop_position < elements_vbox->get_child_count()) {
  2001. Transform2D xform = Object::cast_to<Control>(elements_vbox->get_child(drop_position))->get_transform();
  2002. from = xform.xform(Vector2());
  2003. to = xform.xform(Vector2(elements_vbox->get_size().x, 0));
  2004. } else {
  2005. Control *child = Object::cast_to<Control>(elements_vbox->get_child(drop_position - 1));
  2006. Transform2D xform = child->get_transform();
  2007. from = xform.xform(Vector2(0, child->get_size().y));
  2008. to = xform.xform(Vector2(elements_vbox->get_size().x, child->get_size().y));
  2009. }
  2010. Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  2011. control_dropping->draw_line(from, to, color, 2);
  2012. }
  2013. }
  2014. void EditorInspectorArray::_vbox_visibility_changed() {
  2015. control_dropping->set_visible(vbox->is_visible_in_tree());
  2016. }
  2017. void EditorInspectorArray::_panel_draw(int p_index) {
  2018. ERR_FAIL_INDEX(p_index, (int)array_elements.size());
  2019. Ref<StyleBox> style = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles));
  2020. if (style.is_null()) {
  2021. return;
  2022. }
  2023. if (array_elements[p_index].panel->has_focus()) {
  2024. array_elements[p_index].panel->draw_style_box(style, Rect2(Vector2(), array_elements[p_index].panel->get_size()));
  2025. }
  2026. }
  2027. void EditorInspectorArray::_panel_gui_focus(int p_index) {
  2028. array_elements[p_index].panel->queue_redraw();
  2029. selected = p_index;
  2030. }
  2031. void EditorInspectorArray::_panel_gui_unfocus(int p_index) {
  2032. array_elements[p_index].panel->queue_redraw();
  2033. if (selected == p_index) {
  2034. selected = -1;
  2035. }
  2036. }
  2037. void EditorInspectorArray::_panel_gui_input(Ref<InputEvent> p_event, int p_index) {
  2038. ERR_FAIL_INDEX(p_index, (int)array_elements.size());
  2039. if (read_only) {
  2040. return;
  2041. }
  2042. Ref<InputEventKey> key_ref = p_event;
  2043. if (key_ref.is_valid()) {
  2044. const InputEventKey &key = **key_ref;
  2045. if (array_elements[p_index].panel->has_focus() && key.is_pressed() && key.get_keycode() == Key::KEY_DELETE) {
  2046. _move_element(begin_array_index + p_index, -1);
  2047. array_elements[p_index].panel->accept_event();
  2048. }
  2049. }
  2050. Ref<InputEventMouseButton> mb = p_event;
  2051. if (mb.is_valid()) {
  2052. if (movable && mb->get_button_index() == MouseButton::RIGHT) {
  2053. array_elements[p_index].panel->accept_event();
  2054. popup_array_index_pressed = begin_array_index + p_index;
  2055. rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0);
  2056. rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1);
  2057. rmb_popup->set_position(array_elements[p_index].panel->get_screen_position() + mb->get_position());
  2058. rmb_popup->reset_size();
  2059. rmb_popup->popup();
  2060. }
  2061. }
  2062. }
  2063. void EditorInspectorArray::show_menu(int p_index, const Vector2 &p_offset) {
  2064. popup_array_index_pressed = begin_array_index + p_index;
  2065. rmb_popup->set_item_disabled(OPTION_MOVE_UP, popup_array_index_pressed == 0);
  2066. rmb_popup->set_item_disabled(OPTION_MOVE_DOWN, popup_array_index_pressed == count - 1);
  2067. rmb_popup->set_position(get_screen_position() + p_offset);
  2068. rmb_popup->reset_size();
  2069. rmb_popup->popup();
  2070. }
  2071. void EditorInspectorArray::_move_element(int p_element_index, int p_to_pos) {
  2072. String action_name;
  2073. if (p_element_index < 0) {
  2074. action_name = vformat(TTR("Add element to property array with prefix %s."), array_element_prefix);
  2075. } else if (p_to_pos < 0) {
  2076. action_name = vformat(TTR("Remove element %d from property array with prefix %s."), p_element_index, array_element_prefix);
  2077. } else {
  2078. 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);
  2079. }
  2080. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2081. undo_redo->create_action(action_name);
  2082. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2083. // Call the function.
  2084. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2085. if (move_function.is_valid()) {
  2086. move_function.call(undo_redo, object, array_element_prefix, p_element_index, p_to_pos);
  2087. } else {
  2088. 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()));
  2089. }
  2090. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2091. ERR_FAIL_COND(p_to_pos < -1 || p_to_pos > count);
  2092. if (!swap_method.is_empty()) {
  2093. ERR_FAIL_COND(!object->has_method(swap_method));
  2094. // Swap method was provided, use it.
  2095. if (p_element_index < 0) {
  2096. // Add an element at position
  2097. undo_redo->add_do_property(object, count_property, count + 1);
  2098. if (p_to_pos >= 0) {
  2099. for (int i = count; i > p_to_pos; i--) {
  2100. undo_redo->add_do_method(object, swap_method, i, i - 1);
  2101. }
  2102. for (int i = p_to_pos; i < count; i++) {
  2103. undo_redo->add_undo_method(object, swap_method, i, i + 1);
  2104. }
  2105. }
  2106. undo_redo->add_undo_property(object, count_property, count);
  2107. } else if (p_to_pos < 0) {
  2108. if (count > 0) {
  2109. // Remove element at position
  2110. undo_redo->add_undo_property(object, count_property, count);
  2111. List<PropertyInfo> object_property_list;
  2112. object->get_property_list(&object_property_list);
  2113. for (int i = p_element_index; i < count - 1; i++) {
  2114. undo_redo->add_do_method(object, swap_method, i, i + 1);
  2115. }
  2116. for (int i = count; i > p_element_index; i--) {
  2117. undo_redo->add_undo_method(object, swap_method, i, i - 1);
  2118. }
  2119. String erase_prefix = String(array_element_prefix) + itos(p_element_index);
  2120. for (const PropertyInfo &E : object_property_list) {
  2121. if (E.name.begins_with(erase_prefix)) {
  2122. undo_redo->add_undo_property(object, E.name, object->get(E.name));
  2123. }
  2124. }
  2125. undo_redo->add_do_property(object, count_property, count - 1);
  2126. }
  2127. } else {
  2128. if (p_to_pos > p_element_index) {
  2129. p_to_pos--;
  2130. }
  2131. if (p_to_pos < p_element_index) {
  2132. for (int i = p_element_index; i > p_to_pos; i--) {
  2133. undo_redo->add_do_method(object, swap_method, i, i - 1);
  2134. }
  2135. for (int i = p_to_pos; i < p_element_index; i++) {
  2136. undo_redo->add_undo_method(object, swap_method, i, i + 1);
  2137. }
  2138. } else if (p_to_pos > p_element_index) {
  2139. for (int i = p_element_index; i < p_to_pos; i++) {
  2140. undo_redo->add_do_method(object, swap_method, i, i + 1);
  2141. }
  2142. for (int i = p_to_pos; i > p_element_index; i--) {
  2143. undo_redo->add_undo_method(object, swap_method, i, i - 1);
  2144. }
  2145. }
  2146. }
  2147. } else {
  2148. // Use standard properties.
  2149. List<PropertyInfo> object_property_list;
  2150. object->get_property_list(&object_property_list);
  2151. Array properties_as_array = _extract_properties_as_array(object_property_list);
  2152. properties_as_array.resize(count);
  2153. // For undoing things
  2154. undo_redo->add_undo_property(object, count_property, properties_as_array.size());
  2155. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  2156. Dictionary d = Dictionary(properties_as_array[i]);
  2157. for (const KeyValue<Variant, Variant> &kv : d) {
  2158. undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
  2159. }
  2160. }
  2161. if (p_element_index < 0) {
  2162. // Add an element.
  2163. properties_as_array.insert(p_to_pos < 0 ? properties_as_array.size() : p_to_pos, Dictionary());
  2164. } else if (p_to_pos < 0) {
  2165. // Delete the element.
  2166. properties_as_array.remove_at(p_element_index);
  2167. } else {
  2168. // Move the element.
  2169. properties_as_array.insert(p_to_pos, properties_as_array[p_element_index].duplicate());
  2170. properties_as_array.remove_at(p_to_pos < p_element_index ? p_element_index + 1 : p_element_index);
  2171. }
  2172. // Change the array size then set the properties.
  2173. undo_redo->add_do_property(object, count_property, properties_as_array.size());
  2174. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  2175. Dictionary d = properties_as_array[i];
  2176. for (const KeyValue<Variant, Variant> &kv : d) {
  2177. undo_redo->add_do_property(object, vformat(kv.key, i), kv.value);
  2178. }
  2179. }
  2180. }
  2181. }
  2182. undo_redo->commit_action();
  2183. // Handle page change and update counts.
  2184. if (p_element_index < 0) {
  2185. int added_index = p_to_pos < 0 ? count : p_to_pos;
  2186. emit_signal(SNAME("page_change_request"), added_index / page_length);
  2187. count += 1;
  2188. } else if (p_to_pos < 0) {
  2189. count -= 1;
  2190. if (page == max_page && (MAX(0, count - 1) / page_length != max_page)) {
  2191. emit_signal(SNAME("page_change_request"), max_page - 1);
  2192. }
  2193. } else if (p_to_pos == begin_array_index - 1) {
  2194. emit_signal(SNAME("page_change_request"), page - 1);
  2195. } else if (p_to_pos > end_array_index) {
  2196. emit_signal(SNAME("page_change_request"), page + 1);
  2197. }
  2198. begin_array_index = page * page_length;
  2199. end_array_index = MIN(count, (page + 1) * page_length);
  2200. max_page = MAX(0, count - 1) / page_length;
  2201. }
  2202. void EditorInspectorArray::_clear_array() {
  2203. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2204. undo_redo->create_action(vformat(TTR("Clear Property Array with Prefix %s"), array_element_prefix));
  2205. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2206. for (int i = count - 1; i >= 0; i--) {
  2207. // Call the function.
  2208. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2209. if (move_function.is_valid()) {
  2210. move_function.call(undo_redo, object, array_element_prefix, i, -1);
  2211. } else {
  2212. 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()));
  2213. }
  2214. }
  2215. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2216. List<PropertyInfo> object_property_list;
  2217. object->get_property_list(&object_property_list);
  2218. Array properties_as_array = _extract_properties_as_array(object_property_list);
  2219. properties_as_array.resize(count);
  2220. // For undoing things
  2221. undo_redo->add_undo_property(object, count_property, count);
  2222. for (int i = 0; i < (int)properties_as_array.size(); i++) {
  2223. Dictionary d = Dictionary(properties_as_array[i]);
  2224. for (const KeyValue<Variant, Variant> &kv : d) {
  2225. undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
  2226. }
  2227. }
  2228. // Change the array size then set the properties.
  2229. undo_redo->add_do_property(object, count_property, 0);
  2230. }
  2231. undo_redo->commit_action();
  2232. // Handle page change and update counts.
  2233. emit_signal(SNAME("page_change_request"), 0);
  2234. count = 0;
  2235. begin_array_index = 0;
  2236. end_array_index = 0;
  2237. max_page = 0;
  2238. }
  2239. void EditorInspectorArray::_resize_array(int p_size) {
  2240. ERR_FAIL_COND(p_size < 0);
  2241. if (p_size == count) {
  2242. return;
  2243. }
  2244. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2245. undo_redo->create_action(vformat(TTR("Resize Property Array with Prefix %s"), array_element_prefix));
  2246. if (p_size > count) {
  2247. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2248. for (int i = count; i < p_size; i++) {
  2249. // Call the function.
  2250. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2251. if (move_function.is_valid()) {
  2252. move_function.call(undo_redo, object, array_element_prefix, -1, -1);
  2253. } else {
  2254. 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()));
  2255. }
  2256. }
  2257. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2258. undo_redo->add_undo_property(object, count_property, count);
  2259. undo_redo->add_do_property(object, count_property, p_size);
  2260. }
  2261. } else {
  2262. if (mode == MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION) {
  2263. for (int i = count - 1; i > p_size - 1; i--) {
  2264. // Call the function.
  2265. Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
  2266. if (move_function.is_valid()) {
  2267. move_function.call(undo_redo, object, array_element_prefix, i, -1);
  2268. } else {
  2269. 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()));
  2270. }
  2271. }
  2272. } else if (mode == MODE_USE_COUNT_PROPERTY) {
  2273. List<PropertyInfo> object_property_list;
  2274. object->get_property_list(&object_property_list);
  2275. Array properties_as_array = _extract_properties_as_array(object_property_list);
  2276. properties_as_array.resize(count);
  2277. // For undoing things
  2278. undo_redo->add_undo_property(object, count_property, count);
  2279. for (int i = count - 1; i > p_size - 1; i--) {
  2280. Dictionary d = Dictionary(properties_as_array[i]);
  2281. for (const KeyValue<Variant, Variant> &kv : d) {
  2282. undo_redo->add_undo_property(object, vformat(kv.key, i), kv.value);
  2283. }
  2284. }
  2285. // Change the array size then set the properties.
  2286. undo_redo->add_do_property(object, count_property, p_size);
  2287. }
  2288. }
  2289. undo_redo->commit_action();
  2290. // Handle page change and update counts.
  2291. emit_signal(SNAME("page_change_request"), 0);
  2292. /*
  2293. count = 0;
  2294. begin_array_index = 0;
  2295. end_array_index = 0;
  2296. max_page = 0;
  2297. */
  2298. }
  2299. Array EditorInspectorArray::_extract_properties_as_array(const List<PropertyInfo> &p_list) {
  2300. Array output;
  2301. for (const PropertyInfo &pi : p_list) {
  2302. if (!(pi.usage & PROPERTY_USAGE_EDITOR)) {
  2303. continue;
  2304. }
  2305. if (pi.name.begins_with(array_element_prefix)) {
  2306. String str = pi.name.trim_prefix(array_element_prefix);
  2307. int to_char_index = 0;
  2308. while (to_char_index < str.length()) {
  2309. if (!is_digit(str[to_char_index])) {
  2310. break;
  2311. }
  2312. to_char_index++;
  2313. }
  2314. if (to_char_index > 0) {
  2315. int array_index = str.left(to_char_index).to_int();
  2316. Error error = OK;
  2317. if (array_index >= output.size()) {
  2318. error = output.resize(array_index + 1);
  2319. }
  2320. if (error == OK) {
  2321. String format_string = String(array_element_prefix) + "%d" + str.substr(to_char_index);
  2322. Dictionary dict = output[array_index];
  2323. dict[format_string] = object->get(pi.name);
  2324. output[array_index] = dict;
  2325. } else {
  2326. WARN_PRINT(vformat("Array element %s has an index too high. Array allocation failed.", pi.name));
  2327. }
  2328. }
  2329. }
  2330. }
  2331. return output;
  2332. }
  2333. int EditorInspectorArray::_drop_position() const {
  2334. for (int i = 0; i < (int)array_elements.size(); i++) {
  2335. const ArrayElement &ae = array_elements[i];
  2336. Size2 size = ae.panel->get_size();
  2337. Vector2 mp = ae.panel->get_local_mouse_position();
  2338. if (Rect2(Vector2(), size).has_point(mp)) {
  2339. if (mp.y < size.y / 2) {
  2340. return i;
  2341. } else {
  2342. return i + 1;
  2343. }
  2344. }
  2345. }
  2346. return -1;
  2347. }
  2348. void EditorInspectorArray::_resize_dialog_confirmed() {
  2349. if (int(new_size_spin_box->get_value()) == count) {
  2350. return;
  2351. }
  2352. resize_dialog->hide();
  2353. _resize_array(int(new_size_spin_box->get_value()));
  2354. }
  2355. void EditorInspectorArray::_new_size_spin_box_value_changed(float p_value) {
  2356. resize_dialog->get_ok_button()->set_disabled(int(p_value) == count);
  2357. }
  2358. void EditorInspectorArray::_new_size_spin_box_text_submitted(const String &p_text) {
  2359. _resize_dialog_confirmed();
  2360. }
  2361. void EditorInspectorArray::_setup() {
  2362. // Setup counts.
  2363. count = _get_array_count();
  2364. begin_array_index = page * page_length;
  2365. end_array_index = MIN(count, (page + 1) * page_length);
  2366. max_page = MAX(0, count - 1) / page_length;
  2367. array_elements.resize(MAX(0, end_array_index - begin_array_index));
  2368. if (page < 0 || page > max_page) {
  2369. WARN_PRINT(vformat("Invalid page number %d", page));
  2370. page = CLAMP(page, 0, max_page);
  2371. }
  2372. Ref<Font> numbers_font;
  2373. int numbers_min_w = 0;
  2374. bool unresizable = is_const || read_only;
  2375. if (numbered) {
  2376. numbers_font = get_theme_font(SNAME("bold"), EditorStringName(EditorFonts));
  2377. int digits_found = count;
  2378. String test;
  2379. while (digits_found) {
  2380. test += "8";
  2381. digits_found /= 10;
  2382. }
  2383. numbers_min_w = numbers_font->get_string_size(test).width;
  2384. }
  2385. for (int i = 0; i < (int)array_elements.size(); i++) {
  2386. ArrayElement &ae = array_elements[i];
  2387. // Panel and its hbox.
  2388. ae.panel = memnew(ArrayPanelContainer);
  2389. ae.panel->set_focus_mode(FOCUS_ALL);
  2390. ae.panel->set_mouse_filter(MOUSE_FILTER_PASS);
  2391. SET_DRAG_FORWARDING_GCD(ae.panel, EditorInspectorArray);
  2392. int element_position = begin_array_index + i;
  2393. String ae_name = vformat(TTR("Element %d: %s%d*"), element_position, array_element_prefix, element_position);
  2394. ae.panel->set_meta("index", element_position);
  2395. ae.panel->set_meta("name", ae_name);
  2396. ae.panel->set_meta("element", this);
  2397. ae.panel->set_tooltip_text(ae_name);
  2398. ae.panel->connect(SceneStringName(focus_entered), callable_mp(this, &EditorInspectorArray::_panel_gui_focus).bind(i));
  2399. ae.panel->connect(SceneStringName(focus_exited), callable_mp(this, &EditorInspectorArray::_panel_gui_unfocus).bind(i));
  2400. ae.panel->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_panel_draw).bind(i));
  2401. ae.panel->connect(SceneStringName(gui_input), callable_mp(this, &EditorInspectorArray::_panel_gui_input).bind(i));
  2402. ae.panel->add_theme_style_override(SceneStringName(panel), i % 2 ? odd_style : even_style);
  2403. elements_vbox->add_child(ae.panel);
  2404. ae.margin = memnew(MarginContainer);
  2405. ae.margin->set_mouse_filter(MOUSE_FILTER_PASS);
  2406. if (is_inside_tree()) {
  2407. Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
  2408. ae.margin->begin_bulk_theme_override();
  2409. ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
  2410. ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
  2411. ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
  2412. ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
  2413. ae.margin->end_bulk_theme_override();
  2414. }
  2415. ae.panel->add_child(ae.margin);
  2416. ae.hbox = memnew(HBoxContainer);
  2417. ae.hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2418. ae.hbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2419. ae.margin->add_child(ae.hbox);
  2420. // Move button.
  2421. if (movable) {
  2422. VBoxContainer *move_vbox = memnew(VBoxContainer);
  2423. move_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2424. move_vbox->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  2425. ae.hbox->add_child(move_vbox);
  2426. if (element_position > 0) {
  2427. ae.move_up = memnew(Button);
  2428. ae.move_up->set_accessibility_name(TTRC("Move Up"));
  2429. ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  2430. ae.move_up->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position - 1));
  2431. move_vbox->add_child(ae.move_up);
  2432. }
  2433. ae.move_texture_rect = memnew(TextureRect);
  2434. ae.move_texture_rect->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  2435. ae.move_texture_rect->set_default_cursor_shape(Control::CURSOR_MOVE);
  2436. if (is_inside_tree()) {
  2437. ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
  2438. }
  2439. move_vbox->add_child(ae.move_texture_rect);
  2440. if (element_position < count - 1) {
  2441. ae.move_down = memnew(Button);
  2442. ae.move_down->set_accessibility_name(TTRC("Move Down"));
  2443. ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  2444. ae.move_down->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_move_element).bind(element_position, element_position + 2));
  2445. move_vbox->add_child(ae.move_down);
  2446. }
  2447. }
  2448. if (numbered) {
  2449. ae.number = memnew(Label);
  2450. ae.number->add_theme_font_override(SceneStringName(font), numbers_font);
  2451. ae.number->set_custom_minimum_size(Size2(numbers_min_w, 0));
  2452. ae.number->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  2453. ae.number->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  2454. ae.number->set_text(itos(element_position));
  2455. ae.hbox->add_child(ae.number);
  2456. }
  2457. // Right vbox.
  2458. ae.vbox = memnew(VBoxContainer);
  2459. ae.vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  2460. ae.vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  2461. ae.hbox->add_child(ae.vbox);
  2462. if (!unresizable) {
  2463. ae.erase = memnew(Button);
  2464. ae.erase->set_accessibility_name(TTRC("Remove"));
  2465. ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2466. ae.erase->set_v_size_flags(SIZE_SHRINK_CENTER);
  2467. ae.erase->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_remove_item).bind(element_position));
  2468. ae.hbox->add_child(ae.erase);
  2469. }
  2470. }
  2471. // Hide/show the add button.
  2472. add_button->set_visible(page == max_page && !unresizable);
  2473. // Add paginator if there's more than 1 page.
  2474. if (max_page > 0) {
  2475. EditorPaginator *paginator = memnew(EditorPaginator);
  2476. paginator->update(page, max_page);
  2477. paginator->connect("page_changed", callable_mp(this, &EditorInspectorArray::_paginator_page_changed));
  2478. vbox->add_child(paginator);
  2479. }
  2480. }
  2481. void EditorInspectorArray::_remove_item(int p_index) {
  2482. _move_element(p_index, -1);
  2483. }
  2484. Variant EditorInspectorArray::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2485. if (!movable) {
  2486. return Variant();
  2487. }
  2488. int index = p_from->get_meta("index");
  2489. Dictionary dict;
  2490. dict["type"] = "property_array_element";
  2491. dict["property_array_prefix"] = array_element_prefix;
  2492. dict["index"] = index;
  2493. return dict;
  2494. }
  2495. void EditorInspectorArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2496. Dictionary dict = p_data;
  2497. int to_drop = dict["index"];
  2498. int drop_position = (p_point == Vector2(Math::INF, Math::INF)) ? selected : _drop_position();
  2499. if (drop_position < 0) {
  2500. return;
  2501. }
  2502. _move_element(to_drop, begin_array_index + drop_position);
  2503. }
  2504. bool EditorInspectorArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2505. if (!movable || read_only) {
  2506. return false;
  2507. }
  2508. // First, update drawing.
  2509. control_dropping->queue_redraw();
  2510. if (p_data.get_type() != Variant::DICTIONARY) {
  2511. return false;
  2512. }
  2513. Dictionary dict = p_data;
  2514. int drop_position = (p_point == Vector2(Math::INF, Math::INF)) ? selected : _drop_position();
  2515. if (!dict.has("type") || dict["type"] != "property_array_element" || String(dict["property_array_prefix"]) != array_element_prefix || drop_position < 0) {
  2516. return false;
  2517. }
  2518. // Check in dropping at the given index does indeed move the item.
  2519. int moved_array_index = (int)dict["index"];
  2520. int drop_array_index = begin_array_index + drop_position;
  2521. return drop_array_index != moved_array_index && drop_array_index - 1 != moved_array_index;
  2522. }
  2523. void ArrayPanelContainer::_accessibility_action_menu(const Variant &p_data) {
  2524. EditorInspectorArray *el = Object::cast_to<EditorInspectorArray>(get_meta("element"));
  2525. if (el) {
  2526. int index = get_meta("index");
  2527. el->show_menu(index, Vector2());
  2528. }
  2529. }
  2530. void ArrayPanelContainer::_notification(int p_what) {
  2531. switch (p_what) {
  2532. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  2533. RID ae = get_accessibility_element();
  2534. ERR_FAIL_COND(ae.is_null());
  2535. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  2536. DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_meta("text"));
  2537. DisplayServer::get_singleton()->accessibility_update_set_value(ae, get_meta("text"));
  2538. DisplayServer::get_singleton()->accessibility_update_set_popup_type(ae, DisplayServer::AccessibilityPopupType::POPUP_MENU);
  2539. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &ArrayPanelContainer::_accessibility_action_menu));
  2540. } break;
  2541. }
  2542. }
  2543. ArrayPanelContainer::ArrayPanelContainer() {
  2544. set_focus_mode(FOCUS_ACCESSIBILITY);
  2545. }
  2546. void EditorInspectorArray::_notification(int p_what) {
  2547. switch (p_what) {
  2548. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  2549. RID ae = get_accessibility_element();
  2550. ERR_FAIL_COND(ae.is_null());
  2551. DisplayServer::get_singleton()->accessibility_update_set_name(ae, vformat(TTR("Array: %s"), get_label()));
  2552. DisplayServer::get_singleton()->accessibility_update_set_value(ae, vformat(TTR("Array: %s"), get_label()));
  2553. } break;
  2554. case NOTIFICATION_THEME_CHANGED: {
  2555. Color color = get_theme_color(SNAME("dark_color_1"), EditorStringName(Editor));
  2556. odd_style->set_bg_color(color.darkened(-0.08));
  2557. even_style->set_bg_color(color.darkened(0.08));
  2558. for (ArrayElement &ae : array_elements) {
  2559. if (ae.move_texture_rect) {
  2560. ae.move_texture_rect->set_texture(get_editor_theme_icon(SNAME("TripleBar")));
  2561. }
  2562. if (ae.move_up) {
  2563. ae.move_up->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  2564. }
  2565. if (ae.move_down) {
  2566. ae.move_down->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  2567. }
  2568. Size2 min_size = get_theme_stylebox(SNAME("Focus"), EditorStringName(EditorStyles))->get_minimum_size();
  2569. ae.margin->begin_bulk_theme_override();
  2570. ae.margin->add_theme_constant_override("margin_left", min_size.x / 2);
  2571. ae.margin->add_theme_constant_override("margin_top", min_size.y / 2);
  2572. ae.margin->add_theme_constant_override("margin_right", min_size.x / 2);
  2573. ae.margin->add_theme_constant_override("margin_bottom", min_size.y / 2);
  2574. ae.margin->end_bulk_theme_override();
  2575. if (ae.erase) {
  2576. ae.erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  2577. }
  2578. }
  2579. add_button->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  2580. } break;
  2581. case NOTIFICATION_DRAG_BEGIN: {
  2582. Dictionary dict = get_viewport()->gui_get_drag_data();
  2583. if (dict.has("type") && dict["type"] == "property_array_element" && String(dict["property_array_prefix"]) == array_element_prefix) {
  2584. dropping = true;
  2585. control_dropping->queue_redraw();
  2586. }
  2587. } break;
  2588. case NOTIFICATION_DRAG_END: {
  2589. if (dropping) {
  2590. dropping = false;
  2591. control_dropping->queue_redraw();
  2592. }
  2593. } break;
  2594. }
  2595. }
  2596. void EditorInspectorArray::_bind_methods() {
  2597. ADD_SIGNAL(MethodInfo("page_change_request"));
  2598. }
  2599. 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) {
  2600. count_property = "";
  2601. mode = MODE_USE_MOVE_ARRAY_ELEMENT_FUNCTION;
  2602. array_element_prefix = p_array_element_prefix;
  2603. page = p_page;
  2604. movable = p_movable;
  2605. is_const = p_is_const;
  2606. page_length = p_page_length;
  2607. numbered = p_numbered;
  2608. EditorInspectorSection::setup(String(p_array_element_prefix) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
  2609. _setup();
  2610. }
  2611. 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) {
  2612. count_property = p_count_property;
  2613. mode = MODE_USE_COUNT_PROPERTY;
  2614. array_element_prefix = p_array_element_prefix;
  2615. page = p_page;
  2616. movable = p_movable;
  2617. is_const = p_is_const;
  2618. page_length = p_page_length;
  2619. numbered = p_numbered;
  2620. swap_method = p_swap_method;
  2621. add_button->set_text(p_add_item_text);
  2622. EditorInspectorSection::setup(String(count_property) + "_array", p_label, p_object, p_bg_color, p_foldable, 0);
  2623. _setup();
  2624. }
  2625. VBoxContainer *EditorInspectorArray::get_vbox(int p_index) {
  2626. if (p_index >= begin_array_index && p_index < end_array_index) {
  2627. return array_elements[p_index - begin_array_index].vbox;
  2628. } else if (p_index < 0) {
  2629. return vbox;
  2630. } else {
  2631. return nullptr;
  2632. }
  2633. }
  2634. EditorInspectorArray::EditorInspectorArray(bool p_read_only) {
  2635. read_only = p_read_only;
  2636. odd_style.instantiate();
  2637. even_style.instantiate();
  2638. rmb_popup = memnew(PopupMenu);
  2639. rmb_popup->set_accessibility_name(TTRC("Move"));
  2640. rmb_popup->add_item(TTR("Move Up"), OPTION_MOVE_UP);
  2641. rmb_popup->add_item(TTR("Move Down"), OPTION_MOVE_DOWN);
  2642. rmb_popup->add_separator();
  2643. rmb_popup->add_item(TTR("Insert New Before"), OPTION_NEW_BEFORE);
  2644. rmb_popup->add_item(TTR("Insert New After"), OPTION_NEW_AFTER);
  2645. rmb_popup->add_separator();
  2646. rmb_popup->add_item(TTR("Remove"), OPTION_REMOVE);
  2647. rmb_popup->add_separator();
  2648. rmb_popup->add_item(TTR("Clear Array"), OPTION_CLEAR_ARRAY);
  2649. rmb_popup->add_item(TTR("Resize Array..."), OPTION_RESIZE_ARRAY);
  2650. rmb_popup->connect(SceneStringName(id_pressed), callable_mp(this, &EditorInspectorArray::_rmb_popup_id_pressed));
  2651. add_child(rmb_popup);
  2652. elements_vbox = memnew(VBoxContainer);
  2653. elements_vbox->add_theme_constant_override("separation", 0);
  2654. vbox->add_child(elements_vbox);
  2655. add_button = EditorInspector::create_inspector_action_button(TTR("Add Element"));
  2656. add_button->connect(SceneStringName(pressed), callable_mp(this, &EditorInspectorArray::_add_button_pressed));
  2657. add_button->set_disabled(read_only);
  2658. vbox->add_child(add_button);
  2659. control_dropping = memnew(Control);
  2660. control_dropping->connect(SceneStringName(draw), callable_mp(this, &EditorInspectorArray::_control_dropping_draw));
  2661. control_dropping->set_mouse_filter(Control::MOUSE_FILTER_IGNORE);
  2662. add_child(control_dropping);
  2663. resize_dialog = memnew(AcceptDialog);
  2664. resize_dialog->set_title(TTRC("Resize Array"));
  2665. resize_dialog->add_cancel_button();
  2666. resize_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspectorArray::_resize_dialog_confirmed));
  2667. add_child(resize_dialog);
  2668. VBoxContainer *resize_dialog_vbox = memnew(VBoxContainer);
  2669. resize_dialog->add_child(resize_dialog_vbox);
  2670. new_size_spin_box = memnew(SpinBox);
  2671. new_size_spin_box->set_accessibility_name(TTRC("Size"));
  2672. new_size_spin_box->set_max(16384);
  2673. new_size_spin_box->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_value_changed));
  2674. new_size_spin_box->get_line_edit()->connect(SceneStringName(text_submitted), callable_mp(this, &EditorInspectorArray::_new_size_spin_box_text_submitted));
  2675. new_size_spin_box->set_editable(!read_only);
  2676. resize_dialog_vbox->add_margin_child(TTRC("New Size:"), new_size_spin_box);
  2677. vbox->connect(SceneStringName(visibility_changed), callable_mp(this, &EditorInspectorArray::_vbox_visibility_changed));
  2678. }
  2679. ////////////////////////////////////////////////
  2680. ////////////////////////////////////////////////
  2681. void EditorPaginator::_first_page_button_pressed() {
  2682. emit_signal("page_changed", 0);
  2683. }
  2684. void EditorPaginator::_prev_page_button_pressed() {
  2685. emit_signal("page_changed", MAX(0, page - 1));
  2686. }
  2687. void EditorPaginator::_page_line_edit_text_submitted(const String &p_text) {
  2688. if (p_text.is_valid_int()) {
  2689. int new_page = p_text.to_int() - 1;
  2690. new_page = MIN(MAX(0, new_page), max_page);
  2691. page_line_edit->set_text(Variant(new_page));
  2692. emit_signal("page_changed", new_page);
  2693. } else {
  2694. page_line_edit->set_text(Variant(page));
  2695. }
  2696. }
  2697. void EditorPaginator::_next_page_button_pressed() {
  2698. emit_signal("page_changed", MIN(max_page, page + 1));
  2699. }
  2700. void EditorPaginator::_last_page_button_pressed() {
  2701. emit_signal("page_changed", max_page);
  2702. }
  2703. void EditorPaginator::update(int p_page, int p_max_page) {
  2704. page = p_page;
  2705. max_page = p_max_page;
  2706. // Update buttons.
  2707. first_page_button->set_disabled(page == 0);
  2708. prev_page_button->set_disabled(page == 0);
  2709. next_page_button->set_disabled(page == max_page);
  2710. last_page_button->set_disabled(page == max_page);
  2711. // Update page number and page count.
  2712. page_line_edit->set_text(vformat("%d", page + 1));
  2713. page_count_label->set_text(vformat("/ %d", max_page + 1));
  2714. }
  2715. void EditorPaginator::_notification(int p_what) {
  2716. switch (p_what) {
  2717. case NOTIFICATION_THEME_CHANGED: {
  2718. first_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageFirst")));
  2719. prev_page_button->set_button_icon(get_editor_theme_icon(SNAME("PagePrevious")));
  2720. next_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageNext")));
  2721. last_page_button->set_button_icon(get_editor_theme_icon(SNAME("PageLast")));
  2722. } break;
  2723. }
  2724. }
  2725. void EditorPaginator::_bind_methods() {
  2726. ADD_SIGNAL(MethodInfo("page_changed", PropertyInfo(Variant::INT, "page")));
  2727. }
  2728. EditorPaginator::EditorPaginator() {
  2729. set_h_size_flags(SIZE_EXPAND_FILL);
  2730. set_alignment(ALIGNMENT_CENTER);
  2731. first_page_button = memnew(Button);
  2732. first_page_button->set_accessibility_name(TTRC("First Page"));
  2733. first_page_button->set_flat(true);
  2734. first_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_first_page_button_pressed));
  2735. add_child(first_page_button);
  2736. prev_page_button = memnew(Button);
  2737. prev_page_button->set_accessibility_name(TTRC("Previuos Page"));
  2738. prev_page_button->set_flat(true);
  2739. prev_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_prev_page_button_pressed));
  2740. add_child(prev_page_button);
  2741. page_line_edit = memnew(LineEdit);
  2742. page_line_edit->set_accessibility_name(TTRC("Page"));
  2743. page_line_edit->connect(SceneStringName(text_submitted), callable_mp(this, &EditorPaginator::_page_line_edit_text_submitted));
  2744. page_line_edit->add_theme_constant_override("minimum_character_width", 2);
  2745. add_child(page_line_edit);
  2746. page_count_label = memnew(Label);
  2747. page_count_label->set_focus_mode(FOCUS_ACCESSIBILITY);
  2748. add_child(page_count_label);
  2749. next_page_button = memnew(Button);
  2750. prev_page_button->set_accessibility_name(TTRC("Next Page"));
  2751. next_page_button->set_flat(true);
  2752. next_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_next_page_button_pressed));
  2753. add_child(next_page_button);
  2754. last_page_button = memnew(Button);
  2755. last_page_button->set_accessibility_name(TTRC("Last Page"));
  2756. last_page_button->set_flat(true);
  2757. last_page_button->connect(SceneStringName(pressed), callable_mp(this, &EditorPaginator::_last_page_button_pressed));
  2758. add_child(last_page_button);
  2759. }
  2760. ////////////////////////////////////////////////
  2761. ////////////////////////////////////////////////
  2762. Ref<EditorInspectorPlugin> EditorInspector::inspector_plugins[MAX_PLUGINS];
  2763. int EditorInspector::inspector_plugin_count = 0;
  2764. 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) {
  2765. for (int i = inspector_plugin_count - 1; i >= 0; i--) {
  2766. if (!inspector_plugins[i]->can_handle(p_object)) {
  2767. continue;
  2768. }
  2769. inspector_plugins[i]->parse_property(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide);
  2770. if (inspector_plugins[i]->added_editors.size()) {
  2771. for (List<EditorInspectorPlugin::AddedEditor>::Element *E = inspector_plugins[i]->added_editors.front()->next(); E; E = E->next()) { //only keep first one
  2772. memdelete(E->get().property_editor);
  2773. }
  2774. EditorProperty *prop = Object::cast_to<EditorProperty>(inspector_plugins[i]->added_editors.front()->get().property_editor);
  2775. if (prop) {
  2776. inspector_plugins[i]->added_editors.clear();
  2777. return prop;
  2778. } else {
  2779. memdelete(inspector_plugins[i]->added_editors.front()->get().property_editor);
  2780. inspector_plugins[i]->added_editors.clear();
  2781. }
  2782. }
  2783. }
  2784. return nullptr;
  2785. }
  2786. void EditorInspector::add_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  2787. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  2788. for (int i = 0; i < inspector_plugin_count; i++) {
  2789. if (inspector_plugins[i] == p_plugin) {
  2790. return; //already exists
  2791. }
  2792. }
  2793. inspector_plugins[inspector_plugin_count++] = p_plugin;
  2794. }
  2795. void EditorInspector::remove_inspector_plugin(const Ref<EditorInspectorPlugin> &p_plugin) {
  2796. ERR_FAIL_COND(inspector_plugin_count == MAX_PLUGINS);
  2797. int idx = -1;
  2798. for (int i = 0; i < inspector_plugin_count; i++) {
  2799. if (inspector_plugins[i] == p_plugin) {
  2800. idx = i;
  2801. break;
  2802. }
  2803. }
  2804. ERR_FAIL_COND_MSG(idx == -1, "Trying to remove nonexistent inspector plugin.");
  2805. for (int i = idx; i < inspector_plugin_count - 1; i++) {
  2806. inspector_plugins[i] = inspector_plugins[i + 1];
  2807. }
  2808. inspector_plugins[inspector_plugin_count - 1] = Ref<EditorInspectorPlugin>();
  2809. inspector_plugin_count--;
  2810. }
  2811. void EditorInspector::cleanup_plugins() {
  2812. for (int i = 0; i < inspector_plugin_count; i++) {
  2813. inspector_plugins[i].unref();
  2814. }
  2815. inspector_plugin_count = 0;
  2816. }
  2817. Button *EditorInspector::create_inspector_action_button(const String &p_text) {
  2818. Button *button = memnew(Button);
  2819. button->set_text(p_text);
  2820. button->set_theme_type_variation(SNAME("InspectorActionButton"));
  2821. button->set_h_size_flags(SIZE_SHRINK_CENTER);
  2822. return button;
  2823. }
  2824. bool EditorInspector::is_main_editor_inspector() const {
  2825. return InspectorDock::get_singleton() && InspectorDock::get_inspector_singleton() == this;
  2826. }
  2827. String EditorInspector::get_selected_path() const {
  2828. return property_selected;
  2829. }
  2830. void EditorInspector::_parse_added_editors(VBoxContainer *current_vbox, EditorInspectorSection *p_section, Ref<EditorInspectorPlugin> ped) {
  2831. for (const EditorInspectorPlugin::AddedEditor &F : ped->added_editors) {
  2832. EditorProperty *ep = Object::cast_to<EditorProperty>(F.property_editor);
  2833. if (ep && !F.properties.is_empty() && current_favorites.has(F.properties[0])) {
  2834. ep->favorited = true;
  2835. favorites_vbox->add_child(F.property_editor);
  2836. } else {
  2837. current_vbox->add_child(F.property_editor);
  2838. }
  2839. if (ep) {
  2840. ep->object = object;
  2841. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(false));
  2842. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  2843. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
  2844. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  2845. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  2846. ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
  2847. ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
  2848. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  2849. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  2850. ep->connect("resource_selected", callable_mp(get_root_inspector(), &EditorInspector::_resource_selected), CONNECT_DEFERRED);
  2851. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
  2852. if (F.properties.size()) {
  2853. if (F.properties.size() == 1) {
  2854. //since it's one, associate:
  2855. ep->property = F.properties[0];
  2856. ep->property_path = property_prefix + F.properties[0];
  2857. ep->property_usage = 0;
  2858. }
  2859. if (!F.label.is_empty()) {
  2860. ep->set_label(F.label);
  2861. }
  2862. for (int i = 0; i < F.properties.size(); i++) {
  2863. String prop = F.properties[i];
  2864. if (!editor_property_map.has(prop)) {
  2865. editor_property_map[prop] = List<EditorProperty *>();
  2866. }
  2867. editor_property_map[prop].push_back(ep);
  2868. }
  2869. }
  2870. Node *section_search = p_section;
  2871. while (section_search) {
  2872. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  2873. if (section) {
  2874. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  2875. }
  2876. section_search = section_search->get_parent();
  2877. if (Object::cast_to<EditorInspector>(section_search)) {
  2878. // Skip sub-resource inspectors.
  2879. break;
  2880. }
  2881. }
  2882. ep->set_read_only(read_only);
  2883. ep->update_property();
  2884. ep->_update_flags();
  2885. ep->update_editor_property_status();
  2886. ep->set_deletable(deletable_properties);
  2887. ep->update_cache();
  2888. }
  2889. }
  2890. ped->added_editors.clear();
  2891. }
  2892. bool EditorInspector::_is_property_disabled_by_feature_profile(const StringName &p_property) {
  2893. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  2894. if (profile.is_null()) {
  2895. return false;
  2896. }
  2897. StringName class_name = object->get_class();
  2898. while (class_name != StringName()) {
  2899. if (profile->is_class_property_disabled(class_name, p_property)) {
  2900. return true;
  2901. }
  2902. if (profile->is_class_disabled(class_name)) {
  2903. //won't see properties of a disabled class
  2904. return true;
  2905. }
  2906. class_name = ClassDB::get_parent_class(class_name);
  2907. }
  2908. return false;
  2909. }
  2910. void EditorInspector::update_tree() {
  2911. if (!object) {
  2912. return;
  2913. }
  2914. bool root_inspector_was_following_focus = get_root_inspector()->is_following_focus();
  2915. if (root_inspector_was_following_focus) {
  2916. // Temporarily disable focus following on the root inspector to avoid jumping while the inspector is updating.
  2917. get_root_inspector()->set_follow_focus(false);
  2918. }
  2919. // Store currently selected and focused elements to restore after the update.
  2920. // TODO: Can be useful to store more context for the focusable, such as the caret position in LineEdit.
  2921. StringName current_selected = property_selected;
  2922. int current_focusable = -1;
  2923. if (property_focusable != -1) {
  2924. // Check that focusable is actually focusable.
  2925. bool restore_focus = false;
  2926. Control *focused = get_viewport() ? get_viewport()->gui_get_focus_owner() : nullptr;
  2927. if (focused) {
  2928. Node *parent = focused->get_parent();
  2929. while (parent) {
  2930. EditorInspector *inspector = Object::cast_to<EditorInspector>(parent);
  2931. if (inspector) {
  2932. restore_focus = inspector == this; // May be owned by another inspector.
  2933. break; // Exit after the first inspector is found, since there may be nested ones.
  2934. }
  2935. parent = parent->get_parent();
  2936. }
  2937. }
  2938. if (restore_focus) {
  2939. current_focusable = property_focusable;
  2940. }
  2941. }
  2942. // 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).
  2943. // Since the edited object has not changed, there is no need to hide the plugin at this time.
  2944. _clear(false);
  2945. List<Ref<EditorInspectorPlugin>> valid_plugins;
  2946. for (int i = inspector_plugin_count - 1; i >= 0; i--) { //start by last, so lastly added can override newly added
  2947. if (!inspector_plugins[i]->can_handle(object)) {
  2948. continue;
  2949. }
  2950. valid_plugins.push_back(inspector_plugins[i]);
  2951. }
  2952. // Decide if properties should be drawn with the warning color (yellow),
  2953. // or if the whole object should be considered read-only.
  2954. bool draw_warning = false;
  2955. bool all_read_only = false;
  2956. if (is_inside_tree()) {
  2957. if (object->has_method("_is_read_only")) {
  2958. all_read_only = object->call("_is_read_only");
  2959. }
  2960. Node *nod = Object::cast_to<Node>(object);
  2961. Node *es = EditorNode::get_singleton()->get_edited_scene();
  2962. if (nod && es != nod && nod->get_owner() != es) {
  2963. // Draw in warning color edited nodes that are not in the currently edited scene,
  2964. // as changes may be lost in the future.
  2965. draw_warning = true;
  2966. } else {
  2967. if (!all_read_only) {
  2968. Resource *res = Object::cast_to<Resource>(object);
  2969. if (res) {
  2970. all_read_only = EditorNode::get_singleton()->is_resource_read_only(res);
  2971. }
  2972. }
  2973. }
  2974. }
  2975. String filter = search_box ? search_box->get_text() : "";
  2976. String group;
  2977. String group_base;
  2978. EditorInspectorSection *group_togglable_property = nullptr;
  2979. String subgroup;
  2980. String subgroup_base;
  2981. EditorInspectorSection *subgroup_togglable_property = nullptr;
  2982. int section_depth = 0;
  2983. bool disable_favorite = false;
  2984. VBoxContainer *category_vbox = nullptr;
  2985. List<PropertyInfo> plist;
  2986. object->get_property_list(&plist, true);
  2987. HashMap<VBoxContainer *, HashMap<String, VBoxContainer *>> vbox_per_path;
  2988. HashMap<String, EditorInspectorArray *> editor_inspector_array_per_prefix;
  2989. HashMap<String, HashMap<String, LocalVector<EditorProperty *>>> favorites_to_add;
  2990. HashMap<String, EditorInspectorSection *> togglable_editor_inspector_sections;
  2991. Color sscolor = get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor));
  2992. bool sub_inspectors_enabled = EDITOR_GET("interface/inspector/open_resources_in_current_inspector");
  2993. if (!valid_plugins.is_empty()) {
  2994. begin_vbox->show();
  2995. // Get the lists of editors to add the beginning.
  2996. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  2997. ped->parse_begin(object);
  2998. _parse_added_editors(begin_vbox, nullptr, ped);
  2999. }
  3000. }
  3001. StringName doc_name;
  3002. // Get the lists of editors for properties.
  3003. for (List<PropertyInfo>::Element *E_property = plist.front(); E_property; E_property = E_property->next()) {
  3004. PropertyInfo &p = E_property->get();
  3005. if (p.usage & PROPERTY_USAGE_SUBGROUP) {
  3006. // Setup a property sub-group.
  3007. subgroup = p.name;
  3008. subgroup_togglable_property = nullptr;
  3009. Vector<String> hint_parts = p.hint_string.split(",");
  3010. subgroup_base = hint_parts[0];
  3011. if (hint_parts.size() > 1) {
  3012. section_depth = hint_parts[1].to_int();
  3013. } else {
  3014. section_depth = 0;
  3015. }
  3016. continue;
  3017. } else if (p.usage & PROPERTY_USAGE_GROUP) {
  3018. // Setup a property group.
  3019. group = p.name;
  3020. group_togglable_property = nullptr;
  3021. Vector<String> hint_parts = p.hint_string.split(",");
  3022. group_base = hint_parts[0];
  3023. if (hint_parts.size() > 1) {
  3024. section_depth = hint_parts[1].to_int();
  3025. } else {
  3026. section_depth = 0;
  3027. }
  3028. subgroup = "";
  3029. subgroup_base = "";
  3030. subgroup_togglable_property = nullptr;
  3031. continue;
  3032. } else if (p.usage & PROPERTY_USAGE_CATEGORY) {
  3033. // Setup a property category.
  3034. group = "";
  3035. group_base = "";
  3036. group_togglable_property = nullptr;
  3037. subgroup = "";
  3038. subgroup_base = "";
  3039. subgroup_togglable_property = nullptr;
  3040. section_depth = 0;
  3041. disable_favorite = false;
  3042. vbox_per_path.clear();
  3043. editor_inspector_array_per_prefix.clear();
  3044. // `hint_script` should contain a native class name or a script path.
  3045. // Otherwise the category was probably added via `@export_category` or `_get_property_list()`.
  3046. const bool is_custom_category = p.hint_string.is_empty();
  3047. // Iterate over remaining properties. If no properties in category, skip the category.
  3048. List<PropertyInfo>::Element *N = E_property->next();
  3049. bool valid = true;
  3050. while (N) {
  3051. if (!N->get().name.begins_with("metadata/_") && N->get().usage & PROPERTY_USAGE_EDITOR &&
  3052. (!filter.is_empty() || !restrict_to_basic || (N->get().usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  3053. break;
  3054. }
  3055. // Treat custom categories as second-level ones. Do not skip a normal category if it is followed by a custom one.
  3056. // Skip in the other 3 cases (normal -> normal, custom -> custom, custom -> normal).
  3057. if ((N->get().usage & PROPERTY_USAGE_CATEGORY) && (is_custom_category || !N->get().hint_string.is_empty())) {
  3058. valid = false;
  3059. break;
  3060. }
  3061. N = N->next();
  3062. }
  3063. if (!valid) {
  3064. continue; // Empty, ignore it.
  3065. }
  3066. String category_label;
  3067. String category_tooltip;
  3068. Ref<Texture> category_icon;
  3069. // Do not add an icon, do not change the current class (`doc_name`) for custom categories.
  3070. if (is_custom_category) {
  3071. category_label = p.name;
  3072. category_tooltip = p.name;
  3073. } else {
  3074. doc_name = p.name;
  3075. category_label = p.name;
  3076. // Use category's owner script to update some of its information.
  3077. if (!EditorNode::get_editor_data().is_type_recognized(p.name) && ResourceLoader::exists(p.hint_string, "Script")) {
  3078. Ref<Script> scr = ResourceLoader::load(p.hint_string, "Script");
  3079. if (scr.is_valid()) {
  3080. doc_name = scr->get_doc_class_name();
  3081. StringName script_name = EditorNode::get_editor_data().script_class_get_name(scr->get_path());
  3082. if (script_name != StringName()) {
  3083. category_label = script_name;
  3084. category_icon = EditorNode::get_singleton()->get_class_icon(script_name);
  3085. } else {
  3086. category_icon = EditorNode::get_singleton()->get_object_icon(scr.ptr(), "Object");
  3087. }
  3088. // Property favorites aren't compatible with built-in scripts.
  3089. if (scr->is_built_in()) {
  3090. disable_favorite = true;
  3091. }
  3092. }
  3093. }
  3094. if (category_icon.is_null() && !p.name.is_empty()) {
  3095. category_icon = EditorNode::get_singleton()->get_class_icon(p.name);
  3096. }
  3097. if (use_doc_hints) {
  3098. // `|` separators used in `EditorHelpBit`.
  3099. category_tooltip = "class|" + doc_name + "|";
  3100. }
  3101. }
  3102. if ((is_custom_category && !show_custom_categories) || (!is_custom_category && !show_standard_categories)) {
  3103. continue;
  3104. }
  3105. // Hide the "MultiNodeEdit" category for MultiNodeEdit.
  3106. if (Object::cast_to<MultiNodeEdit>(object) && p.name == "MultiNodeEdit") {
  3107. continue;
  3108. }
  3109. // Create an EditorInspectorCategory and add it to the inspector.
  3110. EditorInspectorCategory *category = memnew(EditorInspectorCategory);
  3111. main_vbox->add_child(category);
  3112. category_vbox = nullptr; // Reset.
  3113. // Set the category info.
  3114. category->label = category_label;
  3115. category->set_tooltip_text(category_tooltip);
  3116. category->icon = category_icon;
  3117. if (!is_custom_category) {
  3118. category->doc_class_name = doc_name;
  3119. }
  3120. // Add editors at the start of a category.
  3121. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3122. ped->parse_category(object, p.name);
  3123. _parse_added_editors(main_vbox, nullptr, ped);
  3124. }
  3125. continue;
  3126. } else if (p.name.begins_with("metadata/_") || !(p.usage & PROPERTY_USAGE_EDITOR) || _is_property_disabled_by_feature_profile(p.name) ||
  3127. (filter.is_empty() && restrict_to_basic && !(p.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  3128. // Ignore properties that are not supposed to be in the inspector.
  3129. continue;
  3130. }
  3131. if (p.name == "script") {
  3132. // Script should go into its own category.
  3133. category_vbox = nullptr;
  3134. }
  3135. if (p.usage & PROPERTY_USAGE_HIGH_END_GFX && RS::get_singleton()->is_low_end()) {
  3136. // Do not show this property in low end gfx.
  3137. continue;
  3138. }
  3139. if (p.name == "script" && (hide_script || bool(object->call("_hide_script_from_inspector")))) {
  3140. // Hide script variables from inspector if required.
  3141. continue;
  3142. }
  3143. if (p.name.begins_with("metadata/") && bool(object->call(SNAME("_hide_metadata_from_inspector")))) {
  3144. // Hide metadata from inspector if required.
  3145. continue;
  3146. }
  3147. // Get the path for property.
  3148. String path = p.name;
  3149. // First check if we have an array that fits the prefix.
  3150. String array_prefix = "";
  3151. int array_index = -1;
  3152. for (KeyValue<String, EditorInspectorArray *> &E : editor_inspector_array_per_prefix) {
  3153. if (p.name.begins_with(E.key) && E.key.length() > array_prefix.length()) {
  3154. array_prefix = E.key;
  3155. }
  3156. }
  3157. if (!array_prefix.is_empty()) {
  3158. // If we have an array element, find the according index in array.
  3159. String str = p.name.trim_prefix(array_prefix);
  3160. int to_char_index = 0;
  3161. while (to_char_index < str.length()) {
  3162. if (!is_digit(str[to_char_index])) {
  3163. break;
  3164. }
  3165. to_char_index++;
  3166. }
  3167. if (to_char_index > 0) {
  3168. array_index = str.left(to_char_index).to_int();
  3169. } else {
  3170. array_prefix = "";
  3171. }
  3172. }
  3173. // Don't allow to favorite array items.
  3174. if (!disable_favorite) {
  3175. disable_favorite = !array_prefix.is_empty();
  3176. }
  3177. if (!array_prefix.is_empty()) {
  3178. path = path.trim_prefix(array_prefix);
  3179. int char_index = path.find_char('/');
  3180. if (char_index >= 0) {
  3181. path = path.right(-char_index - 1);
  3182. } else {
  3183. path = vformat(TTR("Element %s"), array_index);
  3184. }
  3185. } else {
  3186. // Check if we exit or not a subgroup. If there is a prefix, remove it from the property label string.
  3187. if (!subgroup.is_empty() && !subgroup_base.is_empty()) {
  3188. if (path.begins_with(subgroup_base)) {
  3189. path = path.trim_prefix(subgroup_base);
  3190. } else if (subgroup_base.begins_with(path)) {
  3191. // Keep it, this is used pretty often.
  3192. } else {
  3193. subgroup = ""; // The prefix changed, we are no longer in the subgroup.
  3194. subgroup_togglable_property = nullptr;
  3195. }
  3196. }
  3197. // Check if we exit or not a group. If there is a prefix, remove it from the property label string.
  3198. if (!group.is_empty() && !group_base.is_empty() && subgroup.is_empty()) {
  3199. if (path.begins_with(group_base)) {
  3200. path = path.trim_prefix(group_base);
  3201. } else if (group_base.begins_with(path)) {
  3202. // Keep it, this is used pretty often.
  3203. } else {
  3204. group = ""; // The prefix changed, we are no longer in the group.
  3205. group_togglable_property = nullptr;
  3206. subgroup = "";
  3207. subgroup_togglable_property = nullptr;
  3208. }
  3209. }
  3210. // Add the group and subgroup to the path.
  3211. if (!subgroup.is_empty()) {
  3212. path = subgroup + "/" + path;
  3213. }
  3214. if (!group.is_empty()) {
  3215. path = group + "/" + path;
  3216. }
  3217. }
  3218. // Get the property label's string.
  3219. String name_override = (path.contains_char('/')) ? path.substr(path.rfind_char('/') + 1) : path;
  3220. String feature_tag;
  3221. {
  3222. const int dot = name_override.find_char('.');
  3223. if (dot != -1) {
  3224. feature_tag = name_override.substr(dot);
  3225. name_override = name_override.substr(0, dot);
  3226. }
  3227. }
  3228. // Don't localize script variables.
  3229. EditorPropertyNameProcessor::Style name_style = property_name_style;
  3230. if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  3231. name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
  3232. }
  3233. const String property_label_string = EditorPropertyNameProcessor::get_singleton()->process_name(name_override, name_style, p.name, doc_name) + feature_tag;
  3234. // Remove the property from the path.
  3235. int idx = path.rfind_char('/');
  3236. if (idx > -1) {
  3237. path = path.left(idx);
  3238. } else {
  3239. path = "";
  3240. }
  3241. // Ignore properties that do not fit the filter.
  3242. bool sub_inspector_use_filter = false;
  3243. if (use_filter && !filter.is_empty()) {
  3244. const String property_path = property_prefix + (path.is_empty() ? "" : path + "/") + name_override;
  3245. if (!_property_path_matches(property_path, filter, property_name_style)) {
  3246. if (!sub_inspectors_enabled || p.hint != PROPERTY_HINT_RESOURCE_TYPE) {
  3247. continue;
  3248. }
  3249. Ref<Resource> res = object->get(p.name);
  3250. if (res.is_null()) {
  3251. continue;
  3252. }
  3253. // Check if the sub-resource has any properties that match the filter.
  3254. if (!_resource_properties_matches(res, filter)) {
  3255. continue;
  3256. }
  3257. sub_inspector_use_filter = true;
  3258. }
  3259. }
  3260. // Recreate the category vbox if it was reset.
  3261. if (category_vbox == nullptr) {
  3262. category_vbox = memnew(VBoxContainer);
  3263. int separation = get_theme_constant(SNAME("v_separation"), SNAME("EditorInspector"));
  3264. category_vbox->add_theme_constant_override(SNAME("separation"), separation);
  3265. category_vbox->hide();
  3266. main_vbox->add_child(category_vbox);
  3267. }
  3268. // Find the correct section/vbox to add the property editor to.
  3269. VBoxContainer *root_vbox = array_prefix.is_empty() ? main_vbox : editor_inspector_array_per_prefix[array_prefix]->get_vbox(array_index);
  3270. if (!root_vbox) {
  3271. continue;
  3272. }
  3273. if (!vbox_per_path.has(root_vbox)) {
  3274. vbox_per_path[root_vbox] = HashMap<String, VBoxContainer *>();
  3275. vbox_per_path[root_vbox][""] = root_vbox;
  3276. }
  3277. VBoxContainer *current_vbox = root_vbox;
  3278. String acc_path = "";
  3279. int level = 1;
  3280. Vector<String> components = path.split("/");
  3281. for (int i = 0; i < components.size(); i++) {
  3282. const String &component = components[i];
  3283. acc_path += (i > 0) ? "/" + component : component;
  3284. if (!vbox_per_path[root_vbox].has(acc_path)) {
  3285. // If the section does not exists, create it.
  3286. EditorInspectorSection *section = memnew(EditorInspectorSection);
  3287. get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
  3288. current_vbox->add_child(section);
  3289. sections.push_back(section);
  3290. String label;
  3291. String tooltip;
  3292. // Don't localize groups for script variables.
  3293. EditorPropertyNameProcessor::Style section_name_style = property_name_style;
  3294. if ((p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE) && section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  3295. section_name_style = EditorPropertyNameProcessor::STYLE_CAPITALIZED;
  3296. }
  3297. // Only process group label if this is not the group or subgroup.
  3298. if ((i == 0 && component == group) || (i == 1 && component == subgroup)) {
  3299. if (section_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  3300. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
  3301. tooltip = component;
  3302. } else {
  3303. label = component;
  3304. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(component);
  3305. }
  3306. } else {
  3307. label = EditorPropertyNameProcessor::get_singleton()->process_name(component, section_name_style, p.name, doc_name);
  3308. tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(component, EditorPropertyNameProcessor::get_tooltip_style(section_name_style), p.name, doc_name);
  3309. }
  3310. Color c = sscolor;
  3311. c.a /= level;
  3312. section->setup(acc_path, label, object, c, use_folding, section_depth, level);
  3313. section->set_tooltip_text(tooltip);
  3314. section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
  3315. // Add editors at the start of a group.
  3316. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3317. ped->parse_group(object, path);
  3318. _parse_added_editors(section->get_vbox(), section, ped);
  3319. }
  3320. vbox_per_path[root_vbox][acc_path] = section->get_vbox();
  3321. }
  3322. current_vbox = vbox_per_path[root_vbox][acc_path];
  3323. level = (MIN(level + 1, 4));
  3324. }
  3325. // 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).
  3326. if (current_vbox == main_vbox) {
  3327. category_vbox->show();
  3328. current_vbox = category_vbox;
  3329. }
  3330. // Check if the property is an array counter, if so create a dedicated array editor for the array.
  3331. if (p.usage & PROPERTY_USAGE_ARRAY) {
  3332. EditorInspectorArray *editor_inspector_array = nullptr;
  3333. StringName array_element_prefix;
  3334. Color c = sscolor;
  3335. c.a /= level;
  3336. Vector<String> class_name_components = String(p.class_name).split(",");
  3337. int page_size = 5;
  3338. bool movable = true;
  3339. bool is_const = false;
  3340. bool numbered = false;
  3341. bool foldable = use_folding;
  3342. String add_button_text = TTR("Add Element");
  3343. String swap_method;
  3344. for (int i = (p.type == Variant::NIL ? 1 : 2); i < class_name_components.size(); i++) {
  3345. if (class_name_components[i].begins_with("page_size") && class_name_components[i].get_slice_count("=") == 2) {
  3346. page_size = class_name_components[i].get_slicec('=', 1).to_int();
  3347. } else if (class_name_components[i].begins_with("add_button_text") && class_name_components[i].get_slice_count("=") == 2) {
  3348. add_button_text = class_name_components[i].get_slicec('=', 1).strip_edges();
  3349. } else if (class_name_components[i] == "static") {
  3350. movable = false;
  3351. } else if (class_name_components[i] == "const") {
  3352. is_const = true;
  3353. } else if (class_name_components[i] == "numbered") {
  3354. numbered = true;
  3355. } else if (class_name_components[i] == "unfoldable") {
  3356. foldable = false;
  3357. } else if (class_name_components[i].begins_with("swap_method") && class_name_components[i].get_slice_count("=") == 2) {
  3358. swap_method = class_name_components[i].get_slicec('=', 1).strip_edges();
  3359. }
  3360. }
  3361. if (p.type == Variant::NIL) {
  3362. // Setup the array to use a method to create/move/delete elements.
  3363. array_element_prefix = class_name_components[0];
  3364. editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
  3365. String array_label = path.contains_char('/') ? path.substr(path.rfind_char('/') + 1) : path;
  3366. array_label = EditorPropertyNameProcessor::get_singleton()->process_name(property_label_string, property_name_style, p.name, doc_name);
  3367. int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
  3368. editor_inspector_array->setup_with_move_element_function(object, array_label, array_element_prefix, page, c, use_folding);
  3369. editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
  3370. } else if (p.type == Variant::INT) {
  3371. // Setup the array to use the count property and built-in functions to create/move/delete elements.
  3372. if (class_name_components.size() >= 2) {
  3373. array_element_prefix = class_name_components[1];
  3374. editor_inspector_array = memnew(EditorInspectorArray(all_read_only));
  3375. int page = per_array_page.has(array_element_prefix) ? per_array_page[array_element_prefix] : 0;
  3376. 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);
  3377. editor_inspector_array->connect("page_change_request", callable_mp(this, &EditorInspector::_page_change_request).bind(array_element_prefix));
  3378. }
  3379. }
  3380. if (editor_inspector_array) {
  3381. current_vbox->add_child(editor_inspector_array);
  3382. editor_inspector_array_per_prefix[array_element_prefix] = editor_inspector_array;
  3383. }
  3384. continue;
  3385. }
  3386. // Checkable and checked properties.
  3387. bool checkable = false;
  3388. bool checked = false;
  3389. if (p.usage & PROPERTY_USAGE_CHECKABLE) {
  3390. checkable = true;
  3391. checked = p.usage & PROPERTY_USAGE_CHECKED;
  3392. }
  3393. bool property_read_only = (p.usage & PROPERTY_USAGE_READ_ONLY) || read_only;
  3394. // Mark properties that would require an editor restart (mostly when editing editor settings).
  3395. if (p.usage & PROPERTY_USAGE_RESTART_IF_CHANGED) {
  3396. restart_request_props.insert(p.name);
  3397. }
  3398. String doc_path;
  3399. String theme_item_name;
  3400. String doc_tooltip_text;
  3401. StringName classname = doc_name;
  3402. // Build the doc hint, to use as tooltip.
  3403. if (use_doc_hints) {
  3404. if (!object_class.is_empty()) {
  3405. classname = object_class;
  3406. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  3407. classname = Object::cast_to<MultiNodeEdit>(object)->get_edited_class_name();
  3408. } else if (classname == "") {
  3409. classname = object->get_class_name();
  3410. Resource *res = Object::cast_to<Resource>(object);
  3411. if (res && !res->get_script().is_null()) {
  3412. // Grab the script of this resource to get the evaluated script class.
  3413. Ref<Script> scr = res->get_script();
  3414. if (scr.is_valid()) {
  3415. Vector<DocData::ClassDoc> docs = scr->get_documentation();
  3416. if (!docs.is_empty()) {
  3417. // The documentation of a GDScript's main class is at the end of the array.
  3418. // Hacky because this isn't necessarily always guaranteed.
  3419. classname = docs[docs.size() - 1].name;
  3420. }
  3421. }
  3422. }
  3423. }
  3424. StringName propname = property_prefix + p.name;
  3425. bool found = false;
  3426. // Small hack for theme_overrides. They are listed under Control, but come from another class.
  3427. if (classname == "Control" && p.name.begins_with("theme_override_")) {
  3428. classname = get_edited_object()->get_class();
  3429. }
  3430. // Search for the doc path in the cache.
  3431. HashMap<StringName, HashMap<StringName, DocCacheInfo>>::Iterator E = doc_cache.find(classname);
  3432. if (E) {
  3433. HashMap<StringName, DocCacheInfo>::Iterator F = E->value.find(propname);
  3434. if (F) {
  3435. found = true;
  3436. doc_path = F->value.doc_path;
  3437. theme_item_name = F->value.theme_item_name;
  3438. }
  3439. }
  3440. if (!found) {
  3441. DocTools *dd = EditorHelp::get_doc_data();
  3442. // Do not cache the doc path information of scripts.
  3443. bool is_native_class = ClassDB::class_exists(classname);
  3444. HashMap<String, DocData::ClassDoc>::ConstIterator F = dd->class_list.find(classname);
  3445. while (F) {
  3446. Vector<String> slices = propname.operator String().split("/");
  3447. // Check if it's a theme item first.
  3448. if (slices.size() == 2 && slices[0].begins_with("theme_override_")) {
  3449. for (int i = 0; i < F->value.theme_properties.size(); i++) {
  3450. String doc_path_current = "class_theme_item:" + F->value.name + ":" + F->value.theme_properties[i].name;
  3451. if (F->value.theme_properties[i].name == slices[1]) {
  3452. doc_path = doc_path_current;
  3453. theme_item_name = F->value.theme_properties[i].name;
  3454. }
  3455. }
  3456. } else {
  3457. for (int i = 0; i < F->value.properties.size(); i++) {
  3458. String doc_path_current = "class_property:" + F->value.name + ":" + F->value.properties[i].name;
  3459. if (F->value.properties[i].name == propname.operator String()) {
  3460. doc_path = doc_path_current;
  3461. }
  3462. }
  3463. }
  3464. if (is_native_class) {
  3465. DocCacheInfo cache_info;
  3466. cache_info.doc_path = doc_path;
  3467. cache_info.theme_item_name = theme_item_name;
  3468. doc_cache[classname][propname] = cache_info;
  3469. }
  3470. if (!doc_path.is_empty() || F->value.inherits.is_empty()) {
  3471. break;
  3472. }
  3473. // Couldn't find the doc path in the class itself, try its super class.
  3474. F = dd->class_list.find(F->value.inherits);
  3475. }
  3476. }
  3477. // `|` separators used in `EditorHelpBit`.
  3478. if (theme_item_name.is_empty()) {
  3479. if (p.name.contains("shader_parameter/")) {
  3480. ShaderMaterial *shader_material = Object::cast_to<ShaderMaterial>(object);
  3481. if (shader_material) {
  3482. doc_tooltip_text = "property|" + shader_material->get_shader()->get_path() + "|" + property_prefix + p.name;
  3483. }
  3484. } else if (p.usage & PROPERTY_USAGE_INTERNAL) {
  3485. doc_tooltip_text = "internal_property|" + classname + "|" + property_prefix + p.name;
  3486. } else {
  3487. doc_tooltip_text = "property|" + classname + "|" + property_prefix + p.name;
  3488. }
  3489. } else {
  3490. doc_tooltip_text = "theme_item|" + classname + "|" + theme_item_name;
  3491. }
  3492. }
  3493. // Only used for boolean types. Makes the section header a checkable group and adds tooltips.
  3494. if (p.hint == PROPERTY_HINT_GROUP_ENABLE) {
  3495. if (p.type == Variant::BOOL && (p.name.begins_with(group_base) || p.name.begins_with(subgroup_base))) {
  3496. EditorInspectorSection *last_created_section = Object::cast_to<EditorInspectorSection>(current_vbox->get_parent());
  3497. if (last_created_section) {
  3498. bool valid = false;
  3499. Variant value_checked = object->get(p.name, &valid);
  3500. if (valid) {
  3501. last_created_section->set_checkable(p.name, p.hint_string == "feature");
  3502. last_created_section->set_checked(value_checked.operator bool());
  3503. if (p.name.begins_with(group_base)) {
  3504. group_togglable_property = last_created_section;
  3505. } else {
  3506. subgroup_togglable_property = last_created_section;
  3507. }
  3508. if (use_doc_hints) {
  3509. last_created_section->set_tooltip_text(doc_tooltip_text);
  3510. }
  3511. continue;
  3512. }
  3513. }
  3514. } else {
  3515. ERR_PRINT("PROPERTY_HINT_GROUP_ENABLE can only be used on boolean types and must have the same prefix as the group.");
  3516. }
  3517. }
  3518. Vector<EditorInspectorPlugin::AddedEditor> editors;
  3519. Vector<EditorInspectorPlugin::AddedEditor> late_editors;
  3520. // Search for the inspector plugin that will handle the properties. Then add the correct property editor to it.
  3521. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3522. bool exclusive = ped->parse_property(object, p.type, p.name, p.hint, p.hint_string, p.usage, wide_editors);
  3523. for (const EditorInspectorPlugin::AddedEditor &F : ped->added_editors) {
  3524. if (F.add_to_end) {
  3525. late_editors.push_back(F);
  3526. } else {
  3527. editors.push_back(F);
  3528. }
  3529. }
  3530. ped->added_editors.clear();
  3531. if (exclusive) {
  3532. break;
  3533. }
  3534. }
  3535. editors.append_array(late_editors);
  3536. const Node *node = Object::cast_to<Node>(object);
  3537. Vector<SceneState::PackState> sstack;
  3538. if (node != nullptr) {
  3539. const Node *es = EditorNode::get_singleton()->get_edited_scene();
  3540. sstack = PropertyUtils::get_node_states_stack(node, es);
  3541. }
  3542. for (int i = 0; i < editors.size(); i++) {
  3543. EditorProperty *ep = Object::cast_to<EditorProperty>(editors[i].property_editor);
  3544. const Vector<String> &properties = editors[i].properties;
  3545. if (ep) {
  3546. // Set all this before the control gets the ENTER_TREE notification.
  3547. ep->object = object;
  3548. if (properties.size()) {
  3549. if (properties.size() == 1) {
  3550. // Since it's one, associate:
  3551. ep->property = properties[0];
  3552. ep->property_path = property_prefix + properties[0];
  3553. ep->property_usage = p.usage;
  3554. // And set label?
  3555. }
  3556. if (!editors[i].label.is_empty()) {
  3557. ep->set_label(editors[i].label);
  3558. } else {
  3559. // Use the existing one.
  3560. ep->set_label(property_label_string);
  3561. }
  3562. for (int j = 0; j < properties.size(); j++) {
  3563. String prop = properties[j];
  3564. if (!editor_property_map.has(prop)) {
  3565. editor_property_map[prop] = List<EditorProperty *>();
  3566. }
  3567. editor_property_map[prop].push_back(ep);
  3568. }
  3569. }
  3570. if (sub_inspector_use_filter) {
  3571. EditorPropertyResource *epr = Object::cast_to<EditorPropertyResource>(ep);
  3572. if (epr) {
  3573. epr->set_use_filter(true);
  3574. }
  3575. }
  3576. if (p.name.begins_with("metadata/")) {
  3577. Variant _default = Variant();
  3578. if (node != nullptr) {
  3579. _default = PropertyUtils::get_property_default_value(node, p.name, nullptr, &sstack, false, nullptr, nullptr);
  3580. }
  3581. ep->set_deletable(_default == Variant());
  3582. } else {
  3583. ep->set_deletable(deletable_properties);
  3584. }
  3585. ep->set_draw_warning(draw_warning);
  3586. ep->set_use_folding(use_folding);
  3587. ep->set_favoritable(can_favorite && !disable_favorite && !ep->is_deletable());
  3588. ep->set_checkable(checkable);
  3589. ep->set_checked(checked);
  3590. ep->set_keying(keying);
  3591. ep->set_read_only(property_read_only || all_read_only);
  3592. }
  3593. if (ep && ep->is_favoritable() && current_favorites.has(p.name)) {
  3594. ep->favorited = true;
  3595. favorites_to_add[group][subgroup].push_back(ep);
  3596. if (group_togglable_property) {
  3597. togglable_editor_inspector_sections[group] = group_togglable_property;
  3598. }
  3599. if (subgroup_togglable_property) {
  3600. togglable_editor_inspector_sections[group + "/" + subgroup] = subgroup_togglable_property;
  3601. }
  3602. } else {
  3603. current_vbox->add_child(editors[i].property_editor);
  3604. if (ep) {
  3605. Node *section_search = current_vbox->get_parent();
  3606. while (section_search) {
  3607. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  3608. if (section) {
  3609. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  3610. }
  3611. section_search = section_search->get_parent();
  3612. if (Object::cast_to<EditorInspector>(section_search)) {
  3613. // Skip sub-resource inspectors.
  3614. break;
  3615. }
  3616. }
  3617. }
  3618. }
  3619. if (ep) {
  3620. // Eventually, set other properties/signals after the property editor got added to the tree.
  3621. bool update_all = (p.usage & PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED);
  3622. ep->connect("property_changed", callable_mp(this, &EditorInspector::_property_changed).bind(update_all));
  3623. ep->connect("property_keyed", callable_mp(this, &EditorInspector::_property_keyed));
  3624. ep->connect("property_deleted", callable_mp(this, &EditorInspector::_property_deleted), CONNECT_DEFERRED);
  3625. ep->connect("property_keyed_with_value", callable_mp(this, &EditorInspector::_property_keyed_with_value));
  3626. ep->connect("property_favorited", callable_mp(this, &EditorInspector::_set_property_favorited), CONNECT_DEFERRED);
  3627. ep->connect("property_checked", callable_mp(this, &EditorInspector::_property_checked));
  3628. ep->connect("property_pinned", callable_mp(this, &EditorInspector::_property_pinned));
  3629. ep->connect("selected", callable_mp(this, &EditorInspector::_property_selected));
  3630. ep->connect("multiple_properties_changed", callable_mp(this, &EditorInspector::_multiple_properties_changed));
  3631. ep->connect("resource_selected", callable_mp(get_root_inspector(), &EditorInspector::_resource_selected), CONNECT_DEFERRED);
  3632. ep->connect("object_id_selected", callable_mp(this, &EditorInspector::_object_id_selected), CONNECT_DEFERRED);
  3633. ep->set_tooltip_text(doc_tooltip_text);
  3634. ep->has_doc_tooltip = use_doc_hints;
  3635. ep->set_doc_path(doc_path);
  3636. ep->set_internal(p.usage & PROPERTY_USAGE_INTERNAL);
  3637. // If this property is favorited, it won't be in the tree yet. So don't do this setup right now.
  3638. if (ep->is_inside_tree()) {
  3639. ep->update_property();
  3640. ep->_update_flags();
  3641. ep->update_editor_property_status();
  3642. ep->update_cache();
  3643. if (current_selected && ep->property == current_selected) {
  3644. ep->select(current_focusable);
  3645. }
  3646. }
  3647. }
  3648. }
  3649. }
  3650. if (!current_favorites.is_empty()) {
  3651. favorites_section->show();
  3652. // Organize the favorited properties in their sections, to keep context and differentiate from others with the same name.
  3653. bool is_localized = property_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED;
  3654. for (const KeyValue<String, HashMap<String, LocalVector<EditorProperty *>>> &KV : favorites_to_add) {
  3655. String section_name = KV.key;
  3656. String label;
  3657. String tooltip;
  3658. VBoxContainer *parent_vbox = favorites_vbox;
  3659. if (!section_name.is_empty()) {
  3660. if (is_localized) {
  3661. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3662. tooltip = section_name;
  3663. } else {
  3664. label = section_name;
  3665. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3666. }
  3667. EditorInspectorSection *section = memnew(EditorInspectorSection);
  3668. get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
  3669. favorites_groups_vbox->add_child(section);
  3670. parent_vbox = section->get_vbox();
  3671. section->setup("", section_name, object, sscolor, false);
  3672. section->set_tooltip_text(tooltip);
  3673. if (togglable_editor_inspector_sections.has(section_name)) {
  3674. EditorInspectorSection *corresponding_section = togglable_editor_inspector_sections.get(section_name);
  3675. bool valid = false;
  3676. Variant value_checked = object->get(corresponding_section->related_enable_property, &valid);
  3677. if (valid) {
  3678. section->section = corresponding_section->section;
  3679. section->set_checkable(corresponding_section->related_enable_property, corresponding_section->hide_feature);
  3680. section->set_checked(value_checked.operator bool());
  3681. if (use_doc_hints) {
  3682. section->set_tooltip_text(corresponding_section->get_tooltip_text());
  3683. }
  3684. section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
  3685. }
  3686. }
  3687. }
  3688. for (const KeyValue<String, LocalVector<EditorProperty *>> &KV2 : KV.value) {
  3689. section_name = KV2.key;
  3690. VBoxContainer *vbox = parent_vbox;
  3691. if (!section_name.is_empty()) {
  3692. if (is_localized) {
  3693. label = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3694. tooltip = section_name;
  3695. } else {
  3696. label = section_name;
  3697. tooltip = EditorPropertyNameProcessor::get_singleton()->translate_group_name(section_name);
  3698. }
  3699. EditorInspectorSection *section = memnew(EditorInspectorSection);
  3700. get_root_inspector()->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(section, &EditorInspectorSection::reset_timer).unbind(1));
  3701. vbox->add_child(section);
  3702. vbox = section->get_vbox();
  3703. section->setup("", section_name, object, sscolor, false);
  3704. section->set_tooltip_text(tooltip);
  3705. if (togglable_editor_inspector_sections.has(KV.key + "/" + section_name)) {
  3706. EditorInspectorSection *corresponding_section = togglable_editor_inspector_sections.get(KV.key + "/" + section_name);
  3707. bool valid = false;
  3708. Variant value_checked = object->get(corresponding_section->related_enable_property, &valid);
  3709. if (valid) {
  3710. section->section = corresponding_section->section;
  3711. section->set_checkable(corresponding_section->related_enable_property, corresponding_section->hide_feature);
  3712. section->set_checked(value_checked.operator bool());
  3713. if (use_doc_hints) {
  3714. section->set_tooltip_text(corresponding_section->get_tooltip_text());
  3715. }
  3716. section->connect("section_toggled_by_user", callable_mp(this, &EditorInspector::_section_toggled_by_user));
  3717. }
  3718. }
  3719. }
  3720. for (EditorProperty *ep : KV2.value) {
  3721. vbox->add_child(ep);
  3722. Node *section_search = vbox->get_parent();
  3723. while (section_search) {
  3724. EditorInspectorSection *section = Object::cast_to<EditorInspectorSection>(section_search);
  3725. if (section) {
  3726. ep->connect("property_can_revert_changed", callable_mp(section, &EditorInspectorSection::property_can_revert_changed));
  3727. }
  3728. section_search = section_search->get_parent();
  3729. if (Object::cast_to<EditorInspector>(section_search)) {
  3730. // Skip sub-resource inspectors.
  3731. break;
  3732. }
  3733. }
  3734. // Now that it's inside the tree, do the setup.
  3735. ep->update_property();
  3736. ep->_update_flags();
  3737. ep->update_editor_property_status();
  3738. ep->update_cache();
  3739. if (current_selected && ep->property == current_selected) {
  3740. ep->select(current_focusable);
  3741. }
  3742. }
  3743. }
  3744. }
  3745. // Show a separator if there's no category to clearly divide the properties.
  3746. favorites_separator->hide();
  3747. if (main_vbox->get_child_count() > 0) {
  3748. EditorInspectorCategory *category = Object::cast_to<EditorInspectorCategory>(main_vbox->get_child(0));
  3749. if (!category) {
  3750. favorites_separator->show();
  3751. }
  3752. }
  3753. // Clean up empty sections.
  3754. for (List<EditorInspectorSection *>::Element *I = sections.back(); I;) {
  3755. EditorInspectorSection *section = I->get();
  3756. I = I->prev(); // Note: Advance before erasing element.
  3757. if (section->get_vbox()->get_child_count() == 0) {
  3758. sections.erase(section);
  3759. vbox_per_path[main_vbox].erase(section->get_section());
  3760. memdelete(section);
  3761. }
  3762. }
  3763. }
  3764. if (!hide_metadata && !object->call("_hide_metadata_from_inspector")) {
  3765. // Add 4px of spacing between the "Add Metadata" button and the content above it.
  3766. Control *spacer = memnew(Control);
  3767. spacer->set_custom_minimum_size(Size2(0, 4) * EDSCALE);
  3768. main_vbox->add_child(spacer);
  3769. Button *add_md = EditorInspector::create_inspector_action_button(TTR("Add Metadata"));
  3770. add_md->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  3771. add_md->connect(SceneStringName(pressed), callable_mp(this, &EditorInspector::_show_add_meta_dialog));
  3772. main_vbox->add_child(add_md);
  3773. if (all_read_only) {
  3774. add_md->set_disabled(true);
  3775. }
  3776. }
  3777. // Get the lists of to add at the end.
  3778. for (Ref<EditorInspectorPlugin> &ped : valid_plugins) {
  3779. ped->parse_end(object);
  3780. _parse_added_editors(main_vbox, nullptr, ped);
  3781. }
  3782. if (is_main_editor_inspector()) {
  3783. // Updating inspector might invalidate some editing owners.
  3784. EditorNode::get_singleton()->hide_unused_editors();
  3785. }
  3786. if (root_inspector_was_following_focus) {
  3787. get_root_inspector()->set_follow_focus(true);
  3788. }
  3789. }
  3790. void EditorInspector::update_property(const String &p_prop) {
  3791. if (!editor_property_map.has(p_prop)) {
  3792. return;
  3793. }
  3794. for (EditorProperty *E : editor_property_map[p_prop]) {
  3795. E->update_property();
  3796. E->update_editor_property_status();
  3797. E->update_cache();
  3798. }
  3799. }
  3800. void EditorInspector::_clear(bool p_hide_plugins) {
  3801. begin_vbox->hide();
  3802. while (begin_vbox->get_child_count()) {
  3803. memdelete(begin_vbox->get_child(0));
  3804. }
  3805. favorites_section->hide();
  3806. while (favorites_vbox->get_child_count()) {
  3807. memdelete(favorites_vbox->get_child(0));
  3808. }
  3809. while (favorites_groups_vbox->get_child_count()) {
  3810. memdelete(favorites_groups_vbox->get_child(0));
  3811. }
  3812. while (main_vbox->get_child_count()) {
  3813. memdelete(main_vbox->get_child(0));
  3814. }
  3815. property_selected = StringName();
  3816. property_focusable = -1;
  3817. editor_property_map.clear();
  3818. sections.clear();
  3819. pending.clear();
  3820. restart_request_props.clear();
  3821. if (p_hide_plugins && is_main_editor_inspector()) {
  3822. EditorNode::get_singleton()->hide_unused_editors(this);
  3823. }
  3824. }
  3825. Object *EditorInspector::get_edited_object() {
  3826. return object;
  3827. }
  3828. Object *EditorInspector::get_next_edited_object() {
  3829. return next_object;
  3830. }
  3831. void EditorInspector::edit(Object *p_object) {
  3832. if (object == p_object) {
  3833. return;
  3834. }
  3835. next_object = p_object; // Some plugins need to know the next edited object when clearing the inspector.
  3836. if (object) {
  3837. if (likely(Variant(object).get_validated_object())) {
  3838. object->disconnect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
  3839. }
  3840. _clear();
  3841. }
  3842. per_array_page.clear();
  3843. object = p_object;
  3844. if (object) {
  3845. update_scroll_request = 0; //reset
  3846. if (scroll_cache.has(object->get_instance_id())) { //if exists, set something else
  3847. update_scroll_request = scroll_cache[object->get_instance_id()]; //done this way because wait until full size is accommodated
  3848. }
  3849. object->connect(CoreStringName(property_list_changed), callable_mp(this, &EditorInspector::_changed_callback));
  3850. can_favorite = Object::cast_to<Node>(object) || Object::cast_to<Resource>(object);
  3851. _update_current_favorites();
  3852. update_tree();
  3853. }
  3854. // Keep it available until the end so it works with both main and sub inspectors.
  3855. next_object = nullptr;
  3856. emit_signal(SNAME("edited_object_changed"));
  3857. }
  3858. void EditorInspector::set_keying(bool p_active) {
  3859. if (keying == p_active) {
  3860. return;
  3861. }
  3862. keying = p_active;
  3863. _keying_changed();
  3864. }
  3865. void EditorInspector::_keying_changed() {
  3866. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3867. for (EditorProperty *E : F.value) {
  3868. if (E) {
  3869. E->set_keying(keying);
  3870. }
  3871. }
  3872. }
  3873. }
  3874. void EditorInspector::set_read_only(bool p_read_only) {
  3875. if (p_read_only == read_only) {
  3876. return;
  3877. }
  3878. read_only = p_read_only;
  3879. update_tree();
  3880. }
  3881. EditorPropertyNameProcessor::Style EditorInspector::get_property_name_style() const {
  3882. return property_name_style;
  3883. }
  3884. void EditorInspector::set_property_name_style(EditorPropertyNameProcessor::Style p_style) {
  3885. if (property_name_style == p_style) {
  3886. return;
  3887. }
  3888. property_name_style = p_style;
  3889. update_tree();
  3890. }
  3891. void EditorInspector::set_use_settings_name_style(bool p_enable) {
  3892. if (use_settings_name_style == p_enable) {
  3893. return;
  3894. }
  3895. use_settings_name_style = p_enable;
  3896. if (use_settings_name_style) {
  3897. set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
  3898. }
  3899. }
  3900. void EditorInspector::set_autoclear(bool p_enable) {
  3901. autoclear = p_enable;
  3902. }
  3903. void EditorInspector::set_show_categories(bool p_show_standard, bool p_show_custom) {
  3904. show_standard_categories = p_show_standard;
  3905. show_custom_categories = p_show_custom;
  3906. update_tree();
  3907. }
  3908. void EditorInspector::set_use_doc_hints(bool p_enable) {
  3909. use_doc_hints = p_enable;
  3910. update_tree();
  3911. }
  3912. void EditorInspector::set_hide_script(bool p_hide) {
  3913. hide_script = p_hide;
  3914. update_tree();
  3915. }
  3916. void EditorInspector::set_hide_metadata(bool p_hide) {
  3917. hide_metadata = p_hide;
  3918. update_tree();
  3919. }
  3920. void EditorInspector::set_use_filter(bool p_use) {
  3921. use_filter = p_use;
  3922. update_tree();
  3923. }
  3924. void EditorInspector::register_text_enter(Node *p_line_edit) {
  3925. search_box = Object::cast_to<LineEdit>(p_line_edit);
  3926. if (search_box) {
  3927. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorInspector::update_tree).unbind(1));
  3928. }
  3929. }
  3930. void EditorInspector::set_use_folding(bool p_use_folding, bool p_update_tree) {
  3931. use_folding = p_use_folding;
  3932. if (p_update_tree) {
  3933. update_tree();
  3934. }
  3935. }
  3936. bool EditorInspector::is_using_folding() {
  3937. return use_folding;
  3938. }
  3939. void EditorInspector::collapse_all_folding() {
  3940. for (EditorInspectorSection *E : sections) {
  3941. E->fold();
  3942. }
  3943. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3944. for (EditorProperty *E : F.value) {
  3945. E->collapse_all_folding();
  3946. }
  3947. }
  3948. }
  3949. void EditorInspector::expand_all_folding() {
  3950. for (EditorInspectorSection *E : sections) {
  3951. E->unfold();
  3952. }
  3953. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3954. for (EditorProperty *E : F.value) {
  3955. E->expand_all_folding();
  3956. }
  3957. }
  3958. }
  3959. void EditorInspector::expand_revertable() {
  3960. HashSet<EditorInspectorSection *> sections_to_unfold[2];
  3961. for (EditorInspectorSection *E : sections) {
  3962. if (E->has_revertable_properties()) {
  3963. sections_to_unfold[0].insert(E);
  3964. }
  3965. }
  3966. // Climb up the hierarchy doing double buffering with the sets.
  3967. int a = 0;
  3968. int b = 1;
  3969. while (sections_to_unfold[a].size()) {
  3970. for (EditorInspectorSection *E : sections_to_unfold[a]) {
  3971. E->unfold();
  3972. Node *n = E->get_parent();
  3973. while (n) {
  3974. if (Object::cast_to<EditorInspector>(n)) {
  3975. break;
  3976. }
  3977. if (Object::cast_to<EditorInspectorSection>(n) && !sections_to_unfold[a].has((EditorInspectorSection *)n)) {
  3978. sections_to_unfold[b].insert((EditorInspectorSection *)n);
  3979. }
  3980. n = n->get_parent();
  3981. }
  3982. }
  3983. sections_to_unfold[a].clear();
  3984. SWAP(a, b);
  3985. }
  3986. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  3987. for (EditorProperty *E : F.value) {
  3988. E->expand_revertable();
  3989. }
  3990. }
  3991. }
  3992. void EditorInspector::set_scroll_offset(int p_offset) {
  3993. // This can be called before the container finishes sorting its children, so defer it.
  3994. callable_mp((ScrollContainer *)this, &ScrollContainer::set_v_scroll).call_deferred(p_offset);
  3995. }
  3996. int EditorInspector::get_scroll_offset() const {
  3997. return get_v_scroll();
  3998. }
  3999. void EditorInspector::set_use_wide_editors(bool p_enable) {
  4000. wide_editors = p_enable;
  4001. }
  4002. void EditorInspector::set_root_inspector(EditorInspector *p_root_inspector) {
  4003. root_inspector = p_root_inspector;
  4004. // Only the root inspector should follow focus.
  4005. set_follow_focus(false);
  4006. }
  4007. void EditorInspector::_section_toggled_by_user(const String &p_path, bool p_value) {
  4008. _property_changed(p_path, p_value);
  4009. }
  4010. void EditorInspector::set_use_deletable_properties(bool p_enabled) {
  4011. deletable_properties = p_enabled;
  4012. }
  4013. void EditorInspector::_page_change_request(int p_new_page, const StringName &p_array_prefix) {
  4014. int prev_page = per_array_page.has(p_array_prefix) ? per_array_page[p_array_prefix] : 0;
  4015. int new_page = MAX(0, p_new_page);
  4016. if (new_page != prev_page) {
  4017. per_array_page[p_array_prefix] = new_page;
  4018. update_tree_pending = true;
  4019. }
  4020. }
  4021. void EditorInspector::_edit_request_change(Object *p_object, const String &p_property) {
  4022. if (object != p_object) { //may be undoing/redoing for a non edited object, so ignore
  4023. return;
  4024. }
  4025. if (changing) {
  4026. return;
  4027. }
  4028. if (p_property.is_empty()) {
  4029. update_tree_pending = true;
  4030. } else {
  4031. pending.insert(p_property);
  4032. }
  4033. }
  4034. void EditorInspector::_edit_set(const String &p_name, const Variant &p_value, bool p_refresh_all, const String &p_changed_field) {
  4035. if (autoclear && editor_property_map.has(p_name)) {
  4036. for (EditorProperty *E : editor_property_map[p_name]) {
  4037. if (E->is_checkable()) {
  4038. E->set_checked(true);
  4039. }
  4040. }
  4041. }
  4042. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4043. if (bool(object->call("_dont_undo_redo"))) {
  4044. object->set(p_name, p_value);
  4045. if (p_refresh_all) {
  4046. _edit_request_change(object, "");
  4047. } else {
  4048. _edit_request_change(object, p_name);
  4049. }
  4050. emit_signal(_prop_edited, p_name);
  4051. } else if (Object::cast_to<MultiNodeEdit>(object)) {
  4052. Object::cast_to<MultiNodeEdit>(object)->set_property_field(p_name, p_value, p_changed_field);
  4053. _edit_request_change(object, p_name);
  4054. emit_signal(_prop_edited, p_name);
  4055. } else if (Object::cast_to<EditorDebuggerRemoteObjects>(object)) {
  4056. Object::cast_to<EditorDebuggerRemoteObjects>(object)->set_property_field(p_name, p_value, p_changed_field);
  4057. _edit_request_change(object, p_name);
  4058. emit_signal(_prop_edited, p_name);
  4059. } else {
  4060. undo_redo->create_action(vformat(TTR("Set %s"), p_name), UndoRedo::MERGE_ENDS, nullptr, false, mark_unsaved);
  4061. undo_redo->add_do_property(object, p_name, p_value);
  4062. bool valid = false;
  4063. Variant value = object->get(p_name, &valid);
  4064. if (valid) {
  4065. if (Object::cast_to<Control>(object) && (p_name == "anchors_preset" || p_name == "layout_mode")) {
  4066. undo_redo->add_undo_method(object, "_edit_set_state", Object::cast_to<Control>(object)->_edit_get_state());
  4067. } else {
  4068. undo_redo->add_undo_property(object, p_name, value);
  4069. }
  4070. }
  4071. List<StringName> linked_properties;
  4072. ClassDB::get_linked_properties_info(object->get_class_name(), p_name, &linked_properties);
  4073. for (const StringName &linked_prop : linked_properties) {
  4074. valid = false;
  4075. Variant undo_value = object->get(linked_prop, &valid);
  4076. if (valid) {
  4077. undo_redo->add_undo_property(object, linked_prop, undo_value);
  4078. }
  4079. }
  4080. PackedStringArray linked_properties_dynamic = object->call("_get_linked_undo_properties", p_name, p_value);
  4081. for (int i = 0; i < linked_properties_dynamic.size(); i++) {
  4082. valid = false;
  4083. Variant undo_value = object->get(linked_properties_dynamic[i], &valid);
  4084. if (valid) {
  4085. undo_redo->add_undo_property(object, linked_properties_dynamic[i], undo_value);
  4086. }
  4087. }
  4088. Variant v_undo_redo = undo_redo;
  4089. Variant v_object = object;
  4090. Variant v_name = p_name;
  4091. const Vector<Callable> &callbacks = EditorNode::get_editor_data().get_undo_redo_inspector_hook_callback();
  4092. for (int i = 0; i < callbacks.size(); i++) {
  4093. const Callable &callback = callbacks[i];
  4094. const Variant *p_arguments[] = { &v_undo_redo, &v_object, &v_name, &p_value };
  4095. Variant return_value;
  4096. Callable::CallError call_error;
  4097. callback.callp(p_arguments, 4, return_value, call_error);
  4098. if (call_error.error != Callable::CallError::CALL_OK) {
  4099. ERR_PRINT("Invalid UndoRedo callback.");
  4100. }
  4101. }
  4102. if (p_refresh_all) {
  4103. undo_redo->add_do_method(this, "_edit_request_change", object, "");
  4104. undo_redo->add_undo_method(this, "_edit_request_change", object, "");
  4105. } else {
  4106. undo_redo->add_do_method(this, "_edit_request_change", object, p_name);
  4107. undo_redo->add_undo_method(this, "_edit_request_change", object, p_name);
  4108. }
  4109. Resource *r = Object::cast_to<Resource>(object);
  4110. if (r) {
  4111. if (String(p_name) == "resource_local_to_scene") {
  4112. bool prev = object->get(p_name);
  4113. bool next = p_value;
  4114. if (next) {
  4115. undo_redo->add_do_method(r, "setup_local_to_scene");
  4116. }
  4117. if (prev) {
  4118. undo_redo->add_undo_method(r, "setup_local_to_scene");
  4119. }
  4120. }
  4121. }
  4122. undo_redo->add_do_method(this, "emit_signal", _prop_edited, p_name);
  4123. undo_redo->add_undo_method(this, "emit_signal", _prop_edited, p_name);
  4124. undo_redo->commit_action();
  4125. }
  4126. if (editor_property_map.has(p_name)) {
  4127. for (EditorProperty *E : editor_property_map[p_name]) {
  4128. E->update_editor_property_status();
  4129. }
  4130. }
  4131. }
  4132. void EditorInspector::_property_changed(const String &p_path, const Variant &p_value, const String &p_name, bool p_changing, bool p_update_all) {
  4133. // The "changing" variable must be true for properties that trigger events as typing occurs,
  4134. // like "text_changed" signal. E.g. text property of Label, Button, RichTextLabel, etc.
  4135. if (p_changing) {
  4136. changing++;
  4137. }
  4138. _edit_set(p_path, p_value, p_update_all, p_name);
  4139. if (p_changing) {
  4140. changing--;
  4141. }
  4142. if (restart_request_props.has(p_path)) {
  4143. emit_signal(SNAME("restart_requested"));
  4144. }
  4145. }
  4146. void EditorInspector::_multiple_properties_changed(const Vector<String> &p_paths, const Array &p_values, bool p_changing) {
  4147. ERR_FAIL_COND(p_paths.is_empty() || p_values.is_empty());
  4148. ERR_FAIL_COND(p_paths.size() != p_values.size());
  4149. String names;
  4150. for (int i = 0; i < p_paths.size(); i++) {
  4151. if (i > 0) {
  4152. names += ",";
  4153. }
  4154. names += p_paths[i];
  4155. }
  4156. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4157. // TRANSLATORS: This is describing a change to multiple properties at once. The parameter is a list of property names.
  4158. undo_redo->create_action(vformat(TTR("Set Multiple: %s"), names), UndoRedo::MERGE_ENDS);
  4159. for (int i = 0; i < p_paths.size(); i++) {
  4160. _edit_set(p_paths[i], p_values[i], false, "");
  4161. if (restart_request_props.has(p_paths[i])) {
  4162. emit_signal(SNAME("restart_requested"));
  4163. }
  4164. }
  4165. if (p_changing) {
  4166. changing++;
  4167. }
  4168. undo_redo->commit_action();
  4169. if (p_changing) {
  4170. changing--;
  4171. }
  4172. }
  4173. void EditorInspector::_property_keyed(const String &p_path, bool p_advance) {
  4174. if (!object) {
  4175. return;
  4176. }
  4177. // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
  4178. const Variant args[3] = { p_path, object->get(p_path), p_advance };
  4179. const Variant *argp[3] = { &args[0], &args[1], &args[2] };
  4180. emit_signalp(SNAME("property_keyed"), argp, 3);
  4181. }
  4182. void EditorInspector::_property_deleted(const String &p_path) {
  4183. if (!object) {
  4184. return;
  4185. }
  4186. if (p_path.begins_with("metadata/")) {
  4187. String name = p_path.replace_first("metadata/", "");
  4188. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4189. undo_redo->create_action(vformat(TTR("Remove metadata %s"), name));
  4190. undo_redo->add_do_method(object, "remove_meta", name);
  4191. undo_redo->add_undo_method(object, "set_meta", name, object->get_meta(name));
  4192. undo_redo->commit_action();
  4193. }
  4194. if (restart_request_props.has(p_path)) {
  4195. emit_signal(SNAME("restart_requested"));
  4196. }
  4197. emit_signal(SNAME("property_deleted"), p_path);
  4198. }
  4199. void EditorInspector::_property_keyed_with_value(const String &p_path, const Variant &p_value, bool p_advance) {
  4200. if (!object) {
  4201. return;
  4202. }
  4203. // The second parameter could be null, causing the event to fire with less arguments, so use the pointer call which preserves it.
  4204. const Variant args[3] = { p_path, p_value, p_advance };
  4205. const Variant *argp[3] = { &args[0], &args[1], &args[2] };
  4206. emit_signalp(SNAME("property_keyed"), argp, 3);
  4207. }
  4208. void EditorInspector::_property_checked(const String &p_path, bool p_checked) {
  4209. if (!object) {
  4210. return;
  4211. }
  4212. //property checked
  4213. if (autoclear) {
  4214. if (!p_checked) {
  4215. _edit_set(p_path, Variant(), false, "");
  4216. } else {
  4217. Variant to_create;
  4218. Control *control = Object::cast_to<Control>(object);
  4219. bool skip = false;
  4220. if (control && p_path.begins_with("theme_override_")) {
  4221. to_create = control->get_used_theme_item(p_path);
  4222. Ref<Resource> resource = to_create;
  4223. if (resource.is_valid()) {
  4224. to_create = resource->duplicate();
  4225. }
  4226. skip = true;
  4227. }
  4228. if (!skip) {
  4229. List<PropertyInfo> pinfo;
  4230. object->get_property_list(&pinfo);
  4231. for (const PropertyInfo &E : pinfo) {
  4232. if (E.name == p_path) {
  4233. Callable::CallError ce;
  4234. Variant::construct(E.type, to_create, nullptr, 0, ce);
  4235. break;
  4236. }
  4237. }
  4238. }
  4239. _edit_set(p_path, to_create, false, "");
  4240. }
  4241. if (editor_property_map.has(p_path)) {
  4242. for (EditorProperty *E : editor_property_map[p_path]) {
  4243. E->set_checked(p_checked);
  4244. E->update_property();
  4245. E->update_editor_property_status();
  4246. E->update_cache();
  4247. }
  4248. }
  4249. } else {
  4250. emit_signal(SNAME("property_toggled"), p_path, p_checked);
  4251. }
  4252. }
  4253. void EditorInspector::_property_pinned(const String &p_path, bool p_pinned) {
  4254. if (!object) {
  4255. return;
  4256. }
  4257. Node *node = Object::cast_to<Node>(object);
  4258. ERR_FAIL_NULL(node);
  4259. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4260. undo_redo->create_action(vformat(p_pinned ? TTR("Pinned %s") : TTR("Unpinned %s"), p_path));
  4261. undo_redo->add_do_method(node, "_set_property_pinned", p_path, p_pinned);
  4262. undo_redo->add_undo_method(node, "_set_property_pinned", p_path, !p_pinned);
  4263. if (editor_property_map.has(p_path)) {
  4264. for (List<EditorProperty *>::Element *E = editor_property_map[p_path].front(); E; E = E->next()) {
  4265. undo_redo->add_do_method(E->get(), "_update_editor_property_status");
  4266. undo_redo->add_undo_method(E->get(), "_update_editor_property_status");
  4267. }
  4268. }
  4269. undo_redo->commit_action();
  4270. }
  4271. void EditorInspector::_property_selected(const String &p_path, int p_focusable) {
  4272. property_selected = p_path;
  4273. property_focusable = p_focusable;
  4274. // Deselect the others.
  4275. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4276. if (F.key == property_selected) {
  4277. continue;
  4278. }
  4279. for (EditorProperty *E : F.value) {
  4280. if (E->is_selected()) {
  4281. E->deselect();
  4282. }
  4283. }
  4284. }
  4285. emit_signal(SNAME("property_selected"), p_path);
  4286. }
  4287. void EditorInspector::_object_id_selected(const String &p_path, ObjectID p_id) {
  4288. emit_signal(SNAME("object_id_selected"), p_id);
  4289. }
  4290. void EditorInspector::_resource_selected(const String &p_path, Ref<Resource> p_resource) {
  4291. emit_signal(SNAME("resource_selected"), p_resource, p_path);
  4292. }
  4293. void EditorInspector::_node_removed(Node *p_node) {
  4294. if (p_node == object) {
  4295. edit(nullptr);
  4296. }
  4297. }
  4298. void EditorInspector::_update_current_favorites() {
  4299. current_favorites.clear();
  4300. if (!can_favorite) {
  4301. return;
  4302. }
  4303. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  4304. // Fetch script properties.
  4305. Ref<Script> scr = object->get_script();
  4306. if (scr.is_valid()) {
  4307. List<PropertyInfo> plist;
  4308. // FIXME: Only properties from a saved script will be available, unsaved ones will be ignored.
  4309. // Can cause a little wonkiness, while nothing serious, would be nice to find a way to get
  4310. // unsaved ones without needing to get the entire property list of an object.
  4311. scr->get_script_property_list(&plist);
  4312. String path;
  4313. HashMap<String, LocalVector<String>> props;
  4314. for (PropertyInfo &p : plist) {
  4315. if (p.usage & PROPERTY_USAGE_CATEGORY) {
  4316. path = favorites.has(p.hint_string) ? p.hint_string : String();
  4317. } else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && !path.is_empty()) {
  4318. props[path].push_back(p.name);
  4319. }
  4320. }
  4321. // Add favorited properties while removing invalid ones.
  4322. bool invalid_props = false;
  4323. for (const KeyValue<String, LocalVector<String>> &KV : props) {
  4324. path = KV.key;
  4325. for (int i = 0; i < favorites[path].size(); i++) {
  4326. String prop = favorites[path][i];
  4327. if (KV.value.has(prop)) {
  4328. current_favorites.append(prop);
  4329. } else {
  4330. invalid_props = true;
  4331. favorites[path].erase(prop);
  4332. i--;
  4333. }
  4334. }
  4335. if (favorites[path].is_empty()) {
  4336. favorites.erase(path);
  4337. }
  4338. }
  4339. if (invalid_props) {
  4340. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  4341. }
  4342. }
  4343. // Fetch built-in properties.
  4344. StringName class_name = object->get_class_name();
  4345. for (const KeyValue<String, PackedStringArray> &KV : favorites) {
  4346. if (ClassDB::is_parent_class(class_name, KV.key)) {
  4347. current_favorites.append_array(KV.value);
  4348. }
  4349. }
  4350. }
  4351. void EditorInspector::_set_property_favorited(const String &p_path, bool p_favorited) {
  4352. if (!object) {
  4353. return;
  4354. }
  4355. StringName validate_name = object->get_class_name();
  4356. StringName class_name;
  4357. String theme_property;
  4358. if (p_path.begins_with("theme_override_")) {
  4359. theme_property = p_path.get_slicec('/', 1);
  4360. }
  4361. while (!validate_name.is_empty()) {
  4362. class_name = validate_name;
  4363. if (!theme_property.is_empty()) { // Deal with theme properties.
  4364. bool found = false;
  4365. HashMap<String, DocData::ClassDoc>::ConstIterator F = EditorHelp::get_doc_data()->class_list.find(class_name);
  4366. if (F) {
  4367. for (const DocData::ThemeItemDoc &prop : F->value.theme_properties) {
  4368. if (prop.name == theme_property) {
  4369. found = true;
  4370. break;
  4371. }
  4372. }
  4373. }
  4374. if (found) {
  4375. break;
  4376. }
  4377. } else if (ClassDB::has_property(class_name, p_path, true)) { // Check if the property is built-in.
  4378. break;
  4379. }
  4380. validate_name = ClassDB::get_parent_class_nocheck(class_name);
  4381. }
  4382. // "script" isn't a real property, so a hack is necessary.
  4383. if (validate_name.is_empty() && p_path != "script") {
  4384. class_name = "";
  4385. }
  4386. if (class_name.is_empty()) {
  4387. // Check if it's part of a script.
  4388. Ref<Script> scr = object->get_script();
  4389. if (scr.is_valid()) {
  4390. List<PropertyInfo> plist;
  4391. scr->get_script_property_list(&plist);
  4392. String path;
  4393. for (PropertyInfo &p : plist) {
  4394. if (p.usage & PROPERTY_USAGE_CATEGORY) {
  4395. path = p.hint_string;
  4396. } else if (p.usage & PROPERTY_USAGE_SCRIPT_VARIABLE && p.name == p_path) {
  4397. class_name = path;
  4398. break;
  4399. }
  4400. }
  4401. }
  4402. 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.");
  4403. }
  4404. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  4405. if (p_favorited) {
  4406. current_favorites.append(p_path);
  4407. favorites[class_name].append(p_path);
  4408. } else {
  4409. current_favorites.erase(p_path);
  4410. if (favorites.has(class_name) && favorites[class_name].has(p_path)) {
  4411. if (favorites[class_name].size() > 1) {
  4412. favorites[class_name].erase(p_path);
  4413. } else {
  4414. favorites.erase(class_name);
  4415. }
  4416. }
  4417. }
  4418. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  4419. update_tree();
  4420. }
  4421. void EditorInspector::_clear_current_favorites() {
  4422. current_favorites.clear();
  4423. HashMap<String, PackedStringArray> favorites = EditorSettings::get_singleton()->get_favorite_properties();
  4424. Ref<Script> scr = object->get_script();
  4425. if (scr.is_valid()) {
  4426. List<PropertyInfo> plist;
  4427. scr->get_script_property_list(&plist);
  4428. for (PropertyInfo &p : plist) {
  4429. if (p.usage & PROPERTY_USAGE_CATEGORY && favorites.has(p.hint_string)) {
  4430. favorites.erase(p.hint_string);
  4431. }
  4432. }
  4433. }
  4434. StringName class_name = object->get_class_name();
  4435. while (class_name) {
  4436. if (favorites.has(class_name)) {
  4437. favorites.erase(class_name);
  4438. }
  4439. class_name = ClassDB::get_parent_class(class_name);
  4440. }
  4441. EditorSettings::get_singleton()->set_favorite_properties(favorites);
  4442. update_tree();
  4443. }
  4444. void EditorInspector::_notification(int p_what) {
  4445. switch (p_what) {
  4446. case NOTIFICATION_TRANSLATION_CHANGED: {
  4447. if (property_name_style == EditorPropertyNameProcessor::STYLE_LOCALIZED) {
  4448. update_tree_pending = true;
  4449. }
  4450. } break;
  4451. case NOTIFICATION_THEME_CHANGED: {
  4452. if (updating_theme) {
  4453. break;
  4454. }
  4455. favorites_category->icon = get_editor_theme_icon(SNAME("Favorites"));
  4456. int separation = get_theme_constant(SNAME("v_separation"), SNAME("EditorInspector"));
  4457. base_vbox->add_theme_constant_override("separation", separation);
  4458. begin_vbox->add_theme_constant_override("separation", separation);
  4459. favorites_section->add_theme_constant_override("separation", separation);
  4460. favorites_groups_vbox->add_theme_constant_override("separation", separation);
  4461. main_vbox->add_theme_constant_override("separation", separation);
  4462. } break;
  4463. case NOTIFICATION_READY: {
  4464. ERR_FAIL_NULL(EditorFeatureProfileManager::get_singleton());
  4465. EditorFeatureProfileManager::get_singleton()->connect("current_feature_profile_changed", callable_mp(this, &EditorInspector::_feature_profile_changed));
  4466. set_process(is_visible_in_tree());
  4467. if (!is_sub_inspector()) {
  4468. get_tree()->connect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  4469. }
  4470. } break;
  4471. case NOTIFICATION_PREDELETE: {
  4472. if (!is_sub_inspector() && is_inside_tree()) {
  4473. get_tree()->disconnect("node_removed", callable_mp(this, &EditorInspector::_node_removed));
  4474. }
  4475. edit(nullptr);
  4476. } break;
  4477. case NOTIFICATION_VISIBILITY_CHANGED: {
  4478. set_process(is_visible_in_tree());
  4479. } break;
  4480. case NOTIFICATION_PROCESS: {
  4481. if (update_scroll_request >= 0) {
  4482. callable_mp((Range *)get_v_scroll_bar(), &Range::set_value).call_deferred(update_scroll_request);
  4483. update_scroll_request = -1;
  4484. }
  4485. if (update_tree_pending) {
  4486. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  4487. } else if (refresh_countdown > 0) {
  4488. refresh_countdown -= get_process_delta_time();
  4489. if (refresh_countdown <= 0) {
  4490. for (const KeyValue<StringName, List<EditorProperty *>> &F : editor_property_map) {
  4491. for (EditorProperty *E : F.value) {
  4492. if (E && !E->is_cache_valid()) {
  4493. E->update_property();
  4494. E->update_editor_property_status();
  4495. E->update_cache();
  4496. }
  4497. }
  4498. }
  4499. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  4500. }
  4501. }
  4502. changing++;
  4503. if (update_tree_pending) {
  4504. update_tree();
  4505. update_tree_pending = false;
  4506. pending.clear();
  4507. } else {
  4508. while (pending.size()) {
  4509. StringName prop = *pending.begin();
  4510. if (editor_property_map.has(prop)) {
  4511. for (EditorProperty *E : editor_property_map[prop]) {
  4512. E->update_property();
  4513. E->update_editor_property_status();
  4514. E->update_cache();
  4515. }
  4516. }
  4517. pending.remove(pending.begin());
  4518. }
  4519. }
  4520. changing--;
  4521. } break;
  4522. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  4523. if (use_settings_name_style && EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
  4524. EditorPropertyNameProcessor::Style style = EditorPropertyNameProcessor::get_settings_style();
  4525. if (property_name_style != style) {
  4526. property_name_style = style;
  4527. update_tree_pending = true;
  4528. }
  4529. }
  4530. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/inspector")) {
  4531. update_tree_pending = true;
  4532. }
  4533. } break;
  4534. }
  4535. }
  4536. void EditorInspector::_changed_callback() {
  4537. //this is called when property change is notified via notify_property_list_changed()
  4538. if (object != nullptr) {
  4539. _update_current_favorites();
  4540. _edit_request_change(object, String());
  4541. }
  4542. }
  4543. void EditorInspector::_vscroll_changed(double p_offset) {
  4544. if (update_scroll_request >= 0) { //waiting, do nothing
  4545. return;
  4546. }
  4547. if (object) {
  4548. scroll_cache[object->get_instance_id()] = p_offset;
  4549. }
  4550. }
  4551. void EditorInspector::set_property_prefix(const String &p_prefix) {
  4552. property_prefix = p_prefix;
  4553. }
  4554. String EditorInspector::get_property_prefix() const {
  4555. return property_prefix;
  4556. }
  4557. void EditorInspector::add_custom_property_description(const String &p_class, const String &p_property, const String &p_description) {
  4558. const String key = vformat("property|%s|%s", p_class, p_property);
  4559. custom_property_descriptions[key] = p_description;
  4560. }
  4561. String EditorInspector::get_custom_property_description(const String &p_property) const {
  4562. HashMap<String, String>::ConstIterator E = custom_property_descriptions.find(p_property);
  4563. if (E) {
  4564. return E->value;
  4565. }
  4566. return "";
  4567. }
  4568. void EditorInspector::set_object_class(const String &p_class) {
  4569. object_class = p_class;
  4570. }
  4571. String EditorInspector::get_object_class() const {
  4572. return object_class;
  4573. }
  4574. void EditorInspector::_feature_profile_changed() {
  4575. update_tree();
  4576. }
  4577. void EditorInspector::set_restrict_to_basic_settings(bool p_restrict) {
  4578. restrict_to_basic = p_restrict;
  4579. update_tree();
  4580. }
  4581. void EditorInspector::set_property_clipboard(const Variant &p_value) {
  4582. property_clipboard = p_value;
  4583. }
  4584. Variant EditorInspector::get_property_clipboard() const {
  4585. return property_clipboard;
  4586. }
  4587. void EditorInspector::_show_add_meta_dialog() {
  4588. if (!add_meta_dialog) {
  4589. add_meta_dialog = memnew(AddMetadataDialog());
  4590. add_meta_dialog->connect(SceneStringName(confirmed), callable_mp(this, &EditorInspector::_add_meta_confirm));
  4591. add_child(add_meta_dialog);
  4592. }
  4593. StringName dialog_title;
  4594. Node *node = Object::cast_to<Node>(object);
  4595. // If object is derived from Node use node name, if derived from Resource use classname.
  4596. dialog_title = node ? node->get_name() : StringName(object->get_class());
  4597. List<StringName> existing_meta_keys;
  4598. object->get_meta_list(&existing_meta_keys);
  4599. add_meta_dialog->open(dialog_title, existing_meta_keys);
  4600. }
  4601. void EditorInspector::_add_meta_confirm() {
  4602. // Ensure metadata is unfolded when adding a new metadata.
  4603. object->editor_set_section_unfold("metadata", true);
  4604. String name = add_meta_dialog->get_meta_name();
  4605. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4606. undo_redo->create_action(vformat(TTR("Add metadata %s"), name));
  4607. undo_redo->add_do_method(object, "set_meta", name, add_meta_dialog->get_meta_defval());
  4608. undo_redo->add_undo_method(object, "remove_meta", name);
  4609. undo_redo->commit_action();
  4610. }
  4611. void EditorInspector::_handle_menu_option(int p_option) {
  4612. switch (p_option) {
  4613. case MENU_UNFAVORITE_ALL:
  4614. _clear_current_favorites();
  4615. break;
  4616. }
  4617. }
  4618. void EditorInspector::_bind_methods() {
  4619. ClassDB::bind_method(D_METHOD("edit", "object"), &EditorInspector::edit);
  4620. ClassDB::bind_method("_edit_request_change", &EditorInspector::_edit_request_change);
  4621. ClassDB::bind_method("get_selected_path", &EditorInspector::get_selected_path);
  4622. ClassDB::bind_method("get_edited_object", &EditorInspector::get_edited_object);
  4623. ClassDB::bind_static_method("EditorInspector", D_METHOD("instantiate_property_editor", "object", "type", "path", "hint", "hint_text", "usage", "wide"), &EditorInspector::instantiate_property_editor, DEFVAL(false));
  4624. ADD_SIGNAL(MethodInfo("property_selected", PropertyInfo(Variant::STRING, "property")));
  4625. 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")));
  4626. ADD_SIGNAL(MethodInfo("property_deleted", PropertyInfo(Variant::STRING, "property")));
  4627. ADD_SIGNAL(MethodInfo("resource_selected", PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), PropertyInfo(Variant::STRING, "path")));
  4628. ADD_SIGNAL(MethodInfo("object_id_selected", PropertyInfo(Variant::INT, "id")));
  4629. ADD_SIGNAL(MethodInfo("property_edited", PropertyInfo(Variant::STRING, "property")));
  4630. ADD_SIGNAL(MethodInfo("property_toggled", PropertyInfo(Variant::STRING, "property"), PropertyInfo(Variant::BOOL, "checked")));
  4631. ADD_SIGNAL(MethodInfo("edited_object_changed"));
  4632. ADD_SIGNAL(MethodInfo("restart_requested"));
  4633. }
  4634. EditorInspector::EditorInspector() {
  4635. object = nullptr;
  4636. base_vbox = memnew(VBoxContainer);
  4637. base_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  4638. add_child(base_vbox);
  4639. begin_vbox = memnew(VBoxContainer);
  4640. base_vbox->add_child(begin_vbox);
  4641. begin_vbox->hide();
  4642. favorites_section = memnew(VBoxContainer);
  4643. base_vbox->add_child(favorites_section);
  4644. favorites_section->hide();
  4645. favorites_category = memnew(EditorInspectorCategory);
  4646. favorites_category->set_as_favorite(this);
  4647. favorites_section->add_child(favorites_category);
  4648. favorites_category->label = TTR("Favorites");
  4649. favorites_vbox = memnew(VBoxContainer);
  4650. favorites_section->add_child(favorites_vbox);
  4651. favorites_groups_vbox = memnew(VBoxContainer);
  4652. favorites_section->add_child(favorites_groups_vbox);
  4653. favorites_separator = memnew(HSeparator);
  4654. favorites_section->add_child(favorites_separator);
  4655. favorites_separator->hide();
  4656. main_vbox = memnew(VBoxContainer);
  4657. base_vbox->add_child(main_vbox);
  4658. set_horizontal_scroll_mode(SCROLL_MODE_DISABLED);
  4659. set_follow_focus(true);
  4660. changing = 0;
  4661. search_box = nullptr;
  4662. _prop_edited = "property_edited";
  4663. set_process(false);
  4664. set_focus_mode(FocusMode::FOCUS_ALL);
  4665. property_focusable = -1;
  4666. property_clipboard = Variant();
  4667. get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &EditorInspector::_vscroll_changed));
  4668. update_scroll_request = -1;
  4669. if (EditorSettings::get_singleton()) {
  4670. refresh_countdown = float(EDITOR_GET("docks/property_editor/auto_refresh_interval"));
  4671. } else {
  4672. //used when class is created by the docgen to dump default values of everything bindable, editorsettings may not be created
  4673. refresh_countdown = 0.33;
  4674. }
  4675. ED_SHORTCUT("property_editor/copy_value", TTRC("Copy Value"), KeyModifierMask::CMD_OR_CTRL | Key::C);
  4676. ED_SHORTCUT("property_editor/paste_value", TTRC("Paste Value"), KeyModifierMask::CMD_OR_CTRL | Key::V);
  4677. ED_SHORTCUT("property_editor/copy_property_path", TTRC("Copy Property Path"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::C);
  4678. // `use_settings_name_style` is true by default, set the name style accordingly.
  4679. set_property_name_style(EditorPropertyNameProcessor::get_singleton()->get_settings_style());
  4680. set_draw_focus_border(true);
  4681. set_scroll_on_drag_hover(true);
  4682. }