tree.cpp 215 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702
  1. /**************************************************************************/
  2. /* tree.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 "tree.h"
  31. #include "tree.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/os/os.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/line_edit.h"
  39. #include "scene/gui/popup_menu.h"
  40. #include "scene/gui/scroll_bar.h"
  41. #include "scene/gui/slider.h"
  42. #include "scene/gui/text_edit.h"
  43. #include "scene/main/window.h"
  44. #include "scene/theme/theme_db.h"
  45. #include <climits>
  46. Size2 TreeItem::Cell::get_icon_size() const {
  47. if (icon.is_null()) {
  48. return Size2();
  49. }
  50. if (icon_region == Rect2i()) {
  51. return icon->get_size();
  52. } else {
  53. return icon_region.size;
  54. }
  55. }
  56. void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Color &p_color) const {
  57. if (icon.is_null()) {
  58. return;
  59. }
  60. Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size;
  61. if (icon_region == Rect2i()) {
  62. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
  63. if (icon_overlay.is_valid()) {
  64. Vector2 offset = icon->get_size() - icon_overlay->get_size();
  65. icon_overlay->draw_rect_region(p_where, Rect2(p_pos + offset, dsize), Rect2(Point2(), icon_overlay->get_size()), p_color);
  66. }
  67. } else {
  68. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  69. if (icon_overlay.is_valid()) {
  70. icon_overlay->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  71. }
  72. }
  73. }
  74. void TreeItem::_changed_notify(int p_cell) {
  75. if (tree) {
  76. tree->item_changed(p_cell, this);
  77. }
  78. }
  79. void TreeItem::_changed_notify() {
  80. if (tree) {
  81. tree->item_changed(-1, this);
  82. }
  83. }
  84. void TreeItem::_cell_selected(int p_cell) {
  85. if (tree) {
  86. tree->item_selected(p_cell, this);
  87. }
  88. }
  89. void TreeItem::_cell_deselected(int p_cell) {
  90. if (tree) {
  91. tree->item_deselected(p_cell, this);
  92. }
  93. }
  94. void TreeItem::_change_tree(Tree *p_tree) {
  95. if (p_tree == tree) {
  96. return;
  97. }
  98. accessibility_row_dirty = true;
  99. TreeItem *c = first_child;
  100. while (c) {
  101. c->_change_tree(p_tree);
  102. c = c->next;
  103. }
  104. if (tree) {
  105. if (tree->root == this) {
  106. tree->root = nullptr;
  107. }
  108. if (tree->popup_edited_item == this) {
  109. tree->popup_edited_item = nullptr;
  110. tree->popup_pressing_edited_item = nullptr;
  111. tree->pressing_for_editor = false;
  112. }
  113. if (tree->cache.hover_item == this) {
  114. tree->cache.hover_item = nullptr;
  115. }
  116. if (tree->selected_item == this) {
  117. for (int i = 0; i < tree->selected_item->cells.size(); i++) {
  118. tree->selected_item->cells.write[i].selected = false;
  119. }
  120. tree->selected_item = nullptr;
  121. }
  122. if (tree->drop_mode_over == this) {
  123. tree->drop_mode_over = nullptr;
  124. }
  125. if (tree->single_select_defer == this) {
  126. tree->single_select_defer = nullptr;
  127. }
  128. if (tree->edited_item == this) {
  129. tree->edited_item = nullptr;
  130. tree->pressing_for_editor = false;
  131. }
  132. tree->queue_accessibility_update();
  133. tree->queue_redraw();
  134. }
  135. tree = p_tree;
  136. if (accessibility_row_element.is_valid()) {
  137. DisplayServer::get_singleton()->accessibility_free_element(accessibility_row_element);
  138. accessibility_row_element = RID();
  139. }
  140. for (Cell &cell : cells) {
  141. if (cell.accessibility_cell_element.is_valid()) {
  142. DisplayServer::get_singleton()->accessibility_free_element(cell.accessibility_cell_element);
  143. cell.accessibility_cell_element = RID();
  144. }
  145. for (Cell::Button &btn : cell.buttons) {
  146. if (btn.accessibility_button_element.is_valid()) {
  147. DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
  148. btn.accessibility_button_element = RID();
  149. }
  150. }
  151. }
  152. if (tree) {
  153. tree->queue_accessibility_update();
  154. tree->queue_redraw();
  155. cells.resize(tree->columns.size());
  156. }
  157. }
  158. void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) {
  159. ERR_FAIL_INDEX(p_column, cells.size());
  160. if (cells[p_column].mode == p_mode) {
  161. return;
  162. }
  163. Cell &c = cells.write[p_column];
  164. c.mode = p_mode;
  165. c.min = 0;
  166. c.max = 100;
  167. c.step = 1;
  168. c.val = 0;
  169. c.checked = false;
  170. c.icon = Ref<Texture2D>();
  171. c.text = "";
  172. c.dirty = true;
  173. c.icon_max_w = 0;
  174. c.cached_minimum_size_dirty = true;
  175. _changed_notify(p_column);
  176. }
  177. TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
  178. ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING);
  179. return cells[p_column].mode;
  180. }
  181. void TreeItem::set_auto_translate_mode(int p_column, Node::AutoTranslateMode p_mode) {
  182. ERR_FAIL_INDEX(p_column, cells.size());
  183. if (cells[p_column].auto_translate_mode == p_mode) {
  184. return;
  185. }
  186. cells.write[p_column].auto_translate_mode = p_mode;
  187. cells.write[p_column].dirty = true;
  188. cells.write[p_column].cached_minimum_size_dirty = true;
  189. _changed_notify(p_column);
  190. }
  191. Node::AutoTranslateMode TreeItem::get_auto_translate_mode(int p_column) const {
  192. ERR_FAIL_INDEX_V(p_column, cells.size(), Node::AUTO_TRANSLATE_MODE_INHERIT);
  193. return cells[p_column].auto_translate_mode;
  194. }
  195. void TreeItem::set_edit_multiline(int p_column, bool p_multiline) {
  196. ERR_FAIL_INDEX(p_column, cells.size());
  197. cells.write[p_column].edit_multiline = p_multiline;
  198. _changed_notify(p_column);
  199. }
  200. bool TreeItem::is_edit_multiline(int p_column) const {
  201. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  202. return cells[p_column].edit_multiline;
  203. }
  204. void TreeItem::set_checked(int p_column, bool p_checked) {
  205. ERR_FAIL_INDEX(p_column, cells.size());
  206. if (cells[p_column].checked == p_checked) {
  207. return;
  208. }
  209. cells.write[p_column].checked = p_checked;
  210. cells.write[p_column].indeterminate = false;
  211. cells.write[p_column].cached_minimum_size_dirty = true;
  212. _changed_notify(p_column);
  213. }
  214. void TreeItem::set_indeterminate(int p_column, bool p_indeterminate) {
  215. ERR_FAIL_INDEX(p_column, cells.size());
  216. // Prevent uncheck if indeterminate set to false twice.
  217. if (p_indeterminate == cells[p_column].indeterminate) {
  218. return;
  219. }
  220. cells.write[p_column].indeterminate = p_indeterminate;
  221. cells.write[p_column].checked = false;
  222. cells.write[p_column].cached_minimum_size_dirty = true;
  223. _changed_notify(p_column);
  224. }
  225. bool TreeItem::is_checked(int p_column) const {
  226. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  227. return cells[p_column].checked;
  228. }
  229. bool TreeItem::is_indeterminate(int p_column) const {
  230. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  231. return cells[p_column].indeterminate;
  232. }
  233. void TreeItem::propagate_check(int p_column, bool p_emit_signal) {
  234. bool ch = cells[p_column].checked;
  235. if (p_emit_signal) {
  236. tree->emit_signal(SNAME("check_propagated_to_item"), this, p_column);
  237. }
  238. _propagate_check_through_children(p_column, ch, p_emit_signal);
  239. _propagate_check_through_parents(p_column, p_emit_signal);
  240. }
  241. String TreeItem::atr(int p_column, const String &p_text) const {
  242. ERR_FAIL_INDEX_V(p_column, cells.size(), tree->atr(p_text));
  243. switch (cells[p_column].auto_translate_mode) {
  244. case Node::AUTO_TRANSLATE_MODE_INHERIT: {
  245. return tree->atr(p_text);
  246. } break;
  247. case Node::AUTO_TRANSLATE_MODE_ALWAYS: {
  248. return tree->tr(p_text);
  249. } break;
  250. case Node::AUTO_TRANSLATE_MODE_DISABLED: {
  251. return p_text;
  252. } break;
  253. }
  254. ERR_FAIL_V_MSG(tree->atr(p_text), "Unexpected auto translate mode: " + itos(cells[p_column].auto_translate_mode));
  255. }
  256. void TreeItem::_propagate_check_through_children(int p_column, bool p_checked, bool p_emit_signal) {
  257. TreeItem *current = get_first_child();
  258. while (current) {
  259. current->set_checked(p_column, p_checked);
  260. if (p_emit_signal) {
  261. current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
  262. }
  263. current->_propagate_check_through_children(p_column, p_checked, p_emit_signal);
  264. current = current->get_next();
  265. }
  266. }
  267. void TreeItem::_propagate_check_through_parents(int p_column, bool p_emit_signal) {
  268. TreeItem *current = get_parent();
  269. if (!current) {
  270. return;
  271. }
  272. bool any_checked = false;
  273. bool any_unchecked = false;
  274. bool any_indeterminate = false;
  275. TreeItem *child_item = current->get_first_child();
  276. while (child_item) {
  277. if (!child_item->is_checked(p_column)) {
  278. any_unchecked = true;
  279. if (child_item->is_indeterminate(p_column)) {
  280. any_indeterminate = true;
  281. break;
  282. }
  283. } else {
  284. any_checked = true;
  285. }
  286. child_item = child_item->get_next();
  287. }
  288. if (any_indeterminate || (any_checked && any_unchecked)) {
  289. current->set_indeterminate(p_column, true);
  290. } else if (current->is_indeterminate(p_column) && !any_checked) {
  291. current->set_indeterminate(p_column, false);
  292. } else {
  293. current->set_checked(p_column, any_checked);
  294. }
  295. if (p_emit_signal) {
  296. current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
  297. }
  298. current->_propagate_check_through_parents(p_column, p_emit_signal);
  299. }
  300. void TreeItem::set_text(int p_column, String p_text) {
  301. ERR_FAIL_INDEX(p_column, cells.size());
  302. if (cells[p_column].text == p_text) {
  303. return;
  304. }
  305. cells.write[p_column].text = p_text;
  306. cells.write[p_column].dirty = true;
  307. if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE) {
  308. Vector<String> strings = p_text.split(",");
  309. cells.write[p_column].min = INT_MAX;
  310. cells.write[p_column].max = INT_MIN;
  311. for (int i = 0; i < strings.size(); i++) {
  312. int value = i;
  313. if (!strings[i].get_slicec(':', 1).is_empty()) {
  314. value = strings[i].get_slicec(':', 1).to_int();
  315. }
  316. cells.write[p_column].min = MIN(cells[p_column].min, value);
  317. cells.write[p_column].max = MAX(cells[p_column].max, value);
  318. }
  319. cells.write[p_column].step = 0;
  320. } else {
  321. // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
  322. if (tree && (!cells[p_column].editable || cells[p_column].mode != TreeItem::CELL_MODE_STRING)) {
  323. cells.write[p_column].xl_text = atr(p_column, p_text);
  324. } else {
  325. cells.write[p_column].xl_text = p_text;
  326. }
  327. }
  328. cells.write[p_column].cached_minimum_size_dirty = true;
  329. _changed_notify(p_column);
  330. if (get_tree()) {
  331. get_tree()->update_configuration_warnings();
  332. }
  333. }
  334. String TreeItem::get_text(int p_column) const {
  335. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  336. return cells[p_column].text;
  337. }
  338. void TreeItem::set_alt_text(int p_column, String p_text) {
  339. ERR_FAIL_INDEX(p_column, cells.size());
  340. if (cells[p_column].alt_text == p_text) {
  341. return;
  342. }
  343. cells.write[p_column].alt_text = p_text;
  344. _changed_notify(p_column);
  345. if (get_tree()) {
  346. get_tree()->update_configuration_warnings();
  347. }
  348. }
  349. String TreeItem::get_alt_text(int p_column) const {
  350. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  351. return cells[p_column].alt_text;
  352. }
  353. void TreeItem::set_text_direction(int p_column, Control::TextDirection p_text_direction) {
  354. ERR_FAIL_INDEX(p_column, cells.size());
  355. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  356. if (cells[p_column].text_direction == p_text_direction) {
  357. return;
  358. }
  359. cells.write[p_column].text_direction = p_text_direction;
  360. cells.write[p_column].dirty = true;
  361. _changed_notify(p_column);
  362. cells.write[p_column].cached_minimum_size_dirty = true;
  363. }
  364. Control::TextDirection TreeItem::get_text_direction(int p_column) const {
  365. ERR_FAIL_INDEX_V(p_column, cells.size(), Control::TEXT_DIRECTION_INHERITED);
  366. return cells[p_column].text_direction;
  367. }
  368. void TreeItem::set_autowrap_mode(int p_column, TextServer::AutowrapMode p_mode) {
  369. ERR_FAIL_INDEX(p_column, cells.size());
  370. ERR_FAIL_COND(p_mode < TextServer::AUTOWRAP_OFF || p_mode > TextServer::AUTOWRAP_WORD_SMART);
  371. if (cells[p_column].autowrap_mode == p_mode) {
  372. return;
  373. }
  374. cells.write[p_column].autowrap_mode = p_mode;
  375. cells.write[p_column].dirty = true;
  376. _changed_notify(p_column);
  377. cells.write[p_column].cached_minimum_size_dirty = true;
  378. }
  379. TextServer::AutowrapMode TreeItem::get_autowrap_mode(int p_column) const {
  380. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::AUTOWRAP_OFF);
  381. return cells[p_column].autowrap_mode;
  382. }
  383. void TreeItem::set_text_overrun_behavior(int p_column, TextServer::OverrunBehavior p_behavior) {
  384. ERR_FAIL_INDEX(p_column, cells.size());
  385. if (cells[p_column].text_buf->get_text_overrun_behavior() == p_behavior) {
  386. return;
  387. }
  388. cells.write[p_column].text_buf->set_text_overrun_behavior(p_behavior);
  389. cells.write[p_column].dirty = true;
  390. cells.write[p_column].cached_minimum_size_dirty = true;
  391. _changed_notify(p_column);
  392. }
  393. TextServer::OverrunBehavior TreeItem::get_text_overrun_behavior(int p_column) const {
  394. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::OVERRUN_TRIM_ELLIPSIS);
  395. return cells[p_column].text_buf->get_text_overrun_behavior();
  396. }
  397. void TreeItem::set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser) {
  398. ERR_FAIL_INDEX(p_column, cells.size());
  399. if (cells[p_column].st_parser != p_parser) {
  400. cells.write[p_column].st_parser = p_parser;
  401. cells.write[p_column].dirty = true;
  402. cells.write[p_column].cached_minimum_size_dirty = true;
  403. _changed_notify(p_column);
  404. }
  405. }
  406. TextServer::StructuredTextParser TreeItem::get_structured_text_bidi_override(int p_column) const {
  407. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::STRUCTURED_TEXT_DEFAULT);
  408. return cells[p_column].st_parser;
  409. }
  410. void TreeItem::set_structured_text_bidi_override_options(int p_column, Array p_args) {
  411. ERR_FAIL_INDEX(p_column, cells.size());
  412. if (cells[p_column].st_args == p_args) {
  413. return;
  414. }
  415. cells.write[p_column].st_args = p_args;
  416. cells.write[p_column].dirty = true;
  417. cells.write[p_column].cached_minimum_size_dirty = true;
  418. _changed_notify(p_column);
  419. }
  420. Array TreeItem::get_structured_text_bidi_override_options(int p_column) const {
  421. ERR_FAIL_INDEX_V(p_column, cells.size(), Array());
  422. return cells[p_column].st_args;
  423. }
  424. void TreeItem::set_language(int p_column, const String &p_language) {
  425. ERR_FAIL_INDEX(p_column, cells.size());
  426. if (cells[p_column].language != p_language) {
  427. cells.write[p_column].language = p_language;
  428. cells.write[p_column].dirty = true;
  429. cells.write[p_column].cached_minimum_size_dirty = true;
  430. _changed_notify(p_column);
  431. }
  432. }
  433. String TreeItem::get_language(int p_column) const {
  434. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  435. return cells[p_column].language;
  436. }
  437. void TreeItem::set_suffix(int p_column, String p_suffix) {
  438. ERR_FAIL_INDEX(p_column, cells.size());
  439. if (cells[p_column].suffix == p_suffix) {
  440. return;
  441. }
  442. cells.write[p_column].suffix = p_suffix;
  443. cells.write[p_column].cached_minimum_size_dirty = true;
  444. _changed_notify(p_column);
  445. }
  446. String TreeItem::get_suffix(int p_column) const {
  447. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  448. return cells[p_column].suffix;
  449. }
  450. void TreeItem::set_icon(int p_column, const Ref<Texture2D> &p_icon) {
  451. ERR_FAIL_INDEX(p_column, cells.size());
  452. if (cells[p_column].icon == p_icon) {
  453. return;
  454. }
  455. cells.write[p_column].icon = p_icon;
  456. cells.write[p_column].cached_minimum_size_dirty = true;
  457. _changed_notify(p_column);
  458. }
  459. Ref<Texture2D> TreeItem::get_icon(int p_column) const {
  460. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  461. return cells[p_column].icon;
  462. }
  463. void TreeItem::set_icon_overlay(int p_column, const Ref<Texture2D> &p_icon_overlay) {
  464. ERR_FAIL_INDEX(p_column, cells.size());
  465. if (cells[p_column].icon_overlay == p_icon_overlay) {
  466. return;
  467. }
  468. cells.write[p_column].icon_overlay = p_icon_overlay;
  469. cells.write[p_column].cached_minimum_size_dirty = true;
  470. _changed_notify(p_column);
  471. }
  472. Ref<Texture2D> TreeItem::get_icon_overlay(int p_column) const {
  473. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  474. return cells[p_column].icon_overlay;
  475. }
  476. void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
  477. ERR_FAIL_INDEX(p_column, cells.size());
  478. if (cells[p_column].icon_region == p_icon_region) {
  479. return;
  480. }
  481. cells.write[p_column].icon_region = p_icon_region;
  482. cells.write[p_column].cached_minimum_size_dirty = true;
  483. _changed_notify(p_column);
  484. }
  485. Rect2 TreeItem::get_icon_region(int p_column) const {
  486. ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2());
  487. return cells[p_column].icon_region;
  488. }
  489. void TreeItem::set_icon_modulate(int p_column, const Color &p_modulate) {
  490. ERR_FAIL_INDEX(p_column, cells.size());
  491. if (cells[p_column].icon_color == p_modulate) {
  492. return;
  493. }
  494. cells.write[p_column].icon_color = p_modulate;
  495. _changed_notify(p_column);
  496. }
  497. Color TreeItem::get_icon_modulate(int p_column) const {
  498. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  499. return cells[p_column].icon_color;
  500. }
  501. void TreeItem::set_icon_max_width(int p_column, int p_max) {
  502. ERR_FAIL_INDEX(p_column, cells.size());
  503. if (cells[p_column].icon_max_w == p_max) {
  504. return;
  505. }
  506. cells.write[p_column].icon_max_w = p_max;
  507. cells.write[p_column].cached_minimum_size_dirty = true;
  508. _changed_notify(p_column);
  509. }
  510. int TreeItem::get_icon_max_width(int p_column) const {
  511. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  512. return cells[p_column].icon_max_w;
  513. }
  514. void TreeItem::set_range(int p_column, double p_value) {
  515. ERR_FAIL_INDEX(p_column, cells.size());
  516. if (cells[p_column].step > 0) {
  517. p_value = Math::snapped(p_value, cells[p_column].step);
  518. }
  519. if (p_value < cells[p_column].min) {
  520. p_value = cells[p_column].min;
  521. }
  522. if (p_value > cells[p_column].max) {
  523. p_value = cells[p_column].max;
  524. }
  525. if (cells[p_column].val == p_value) {
  526. return;
  527. }
  528. cells.write[p_column].val = p_value;
  529. cells.write[p_column].dirty = true;
  530. _changed_notify(p_column);
  531. }
  532. double TreeItem::get_range(int p_column) const {
  533. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  534. return cells[p_column].val;
  535. }
  536. bool TreeItem::is_range_exponential(int p_column) const {
  537. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  538. return cells[p_column].expr;
  539. }
  540. void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) {
  541. ERR_FAIL_INDEX(p_column, cells.size());
  542. if (cells[p_column].min == p_min && cells[p_column].max == p_max && cells[p_column].step == p_step && cells[p_column].expr == p_exp) {
  543. return;
  544. }
  545. cells.write[p_column].min = p_min;
  546. cells.write[p_column].max = p_max;
  547. cells.write[p_column].step = p_step;
  548. cells.write[p_column].expr = p_exp;
  549. _changed_notify(p_column);
  550. }
  551. void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const {
  552. ERR_FAIL_INDEX(p_column, cells.size());
  553. r_min = cells[p_column].min;
  554. r_max = cells[p_column].max;
  555. r_step = cells[p_column].step;
  556. }
  557. void TreeItem::set_metadata(int p_column, const Variant &p_meta) {
  558. ERR_FAIL_INDEX(p_column, cells.size());
  559. cells.write[p_column].meta = p_meta;
  560. }
  561. Variant TreeItem::get_metadata(int p_column) const {
  562. ERR_FAIL_INDEX_V(p_column, cells.size(), Variant());
  563. return cells[p_column].meta;
  564. }
  565. #ifndef DISABLE_DEPRECATED
  566. void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
  567. WARN_DEPRECATED_MSG(R"*(The "set_custom_draw()" method is deprecated, use "set_custom_draw_callback()" instead.)*");
  568. ERR_FAIL_INDEX(p_column, cells.size());
  569. ERR_FAIL_NULL(p_object);
  570. cells.write[p_column].custom_draw_callback = Callable(p_object, p_callback);
  571. _changed_notify(p_column);
  572. }
  573. #endif // DISABLE_DEPRECATED
  574. void TreeItem::set_custom_draw_callback(int p_column, const Callable &p_callback) {
  575. ERR_FAIL_INDEX(p_column, cells.size());
  576. cells.write[p_column].custom_draw_callback = p_callback;
  577. _changed_notify(p_column);
  578. }
  579. Callable TreeItem::get_custom_draw_callback(int p_column) const {
  580. ERR_FAIL_INDEX_V(p_column, cells.size(), Callable());
  581. return cells[p_column].custom_draw_callback;
  582. }
  583. void TreeItem::set_collapsed(bool p_collapsed) {
  584. if (collapsed == p_collapsed || !tree) {
  585. return;
  586. }
  587. collapsed = p_collapsed;
  588. TreeItem *ci = tree->selected_item;
  589. if (ci) {
  590. while (ci && ci != this) {
  591. ci = ci->parent;
  592. }
  593. if (ci) { // Collapsing cursor/selected, move it!
  594. if (tree->select_mode == Tree::SELECT_MULTI) {
  595. tree->selected_item = this;
  596. emit_signal(SNAME("cell_selected"));
  597. } else {
  598. select(tree->selected_col);
  599. }
  600. tree->queue_accessibility_update();
  601. tree->queue_redraw();
  602. }
  603. }
  604. _changed_notify();
  605. tree->emit_signal(SNAME("item_collapsed"), this);
  606. }
  607. bool TreeItem::is_collapsed() {
  608. return collapsed;
  609. }
  610. void TreeItem::set_collapsed_recursive(bool p_collapsed) {
  611. if (!tree) {
  612. return;
  613. }
  614. set_collapsed(p_collapsed);
  615. TreeItem *child = get_first_child();
  616. while (child) {
  617. child->set_collapsed_recursive(p_collapsed);
  618. child = child->get_next();
  619. }
  620. }
  621. bool TreeItem::_is_any_collapsed(bool p_only_visible) {
  622. TreeItem *child = get_first_child();
  623. // Check on children directly first (avoid recursing if possible).
  624. while (child) {
  625. if (child->get_first_child() && child->is_collapsed() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count()))) {
  626. return true;
  627. }
  628. child = child->get_next();
  629. }
  630. child = get_first_child();
  631. // Otherwise recurse on children.
  632. while (child) {
  633. if (child->get_first_child() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count())) && child->_is_any_collapsed(p_only_visible)) {
  634. return true;
  635. }
  636. child = child->get_next();
  637. }
  638. return false;
  639. }
  640. bool TreeItem::is_any_collapsed(bool p_only_visible) {
  641. if (p_only_visible && !is_visible_in_tree()) {
  642. return false;
  643. }
  644. // Collapsed if this is collapsed and it has children (only considers visible if only visible is set).
  645. if (is_collapsed() && get_first_child() && (!p_only_visible || get_visible_child_count())) {
  646. return true;
  647. }
  648. return _is_any_collapsed(p_only_visible);
  649. }
  650. void TreeItem::set_visible(bool p_visible) {
  651. if (visible == p_visible) {
  652. return;
  653. }
  654. visible = p_visible;
  655. if (tree) {
  656. if (!visible) {
  657. for (int i = 0; i < tree->columns.size(); i++) {
  658. if (cells[i].selected) {
  659. deselect(i);
  660. }
  661. }
  662. }
  663. tree->queue_accessibility_update();
  664. tree->queue_redraw();
  665. _changed_notify();
  666. }
  667. _handle_visibility_changed(p_visible);
  668. }
  669. bool TreeItem::is_visible() {
  670. return visible;
  671. }
  672. bool TreeItem::is_visible_in_tree() const {
  673. return visible && parent_visible_in_tree;
  674. }
  675. void TreeItem::_handle_visibility_changed(bool p_visible) {
  676. TreeItem *child = get_first_child();
  677. while (child) {
  678. child->_propagate_visibility_changed(p_visible);
  679. child = child->get_next();
  680. }
  681. }
  682. void TreeItem::_propagate_visibility_changed(bool p_parent_visible_in_tree) {
  683. parent_visible_in_tree = p_parent_visible_in_tree;
  684. _handle_visibility_changed(p_parent_visible_in_tree);
  685. }
  686. void TreeItem::uncollapse_tree() {
  687. TreeItem *t = this;
  688. while (t) {
  689. t->set_collapsed(false);
  690. t = t->parent;
  691. }
  692. }
  693. void TreeItem::set_custom_minimum_height(int p_height) {
  694. if (custom_min_height == p_height) {
  695. return;
  696. }
  697. custom_min_height = p_height;
  698. for (Cell &c : cells) {
  699. c.cached_minimum_size_dirty = true;
  700. }
  701. _changed_notify();
  702. }
  703. int TreeItem::get_custom_minimum_height() const {
  704. return custom_min_height;
  705. }
  706. TreeItem *TreeItem::create_child(int p_index) {
  707. TreeItem *ti = memnew(TreeItem(tree));
  708. if (tree) {
  709. ti->cells.resize(tree->columns.size());
  710. tree->queue_accessibility_update();
  711. tree->queue_redraw();
  712. }
  713. TreeItem *item_prev = nullptr;
  714. TreeItem *item_next = first_child;
  715. if (p_index < 0 && last_child) {
  716. item_prev = last_child;
  717. } else {
  718. int idx = 0;
  719. while (item_next) {
  720. if (idx == p_index) {
  721. item_next->prev = ti;
  722. ti->next = item_next;
  723. break;
  724. }
  725. item_prev = item_next;
  726. item_next = item_next->next;
  727. idx++;
  728. }
  729. }
  730. if (item_prev) {
  731. item_prev->next = ti;
  732. ti->prev = item_prev;
  733. if (!children_cache.is_empty()) {
  734. if (ti->next) {
  735. children_cache.insert(p_index, ti);
  736. } else {
  737. children_cache.append(ti);
  738. }
  739. }
  740. } else {
  741. first_child = ti;
  742. if (!children_cache.is_empty()) {
  743. children_cache.insert(0, ti);
  744. }
  745. }
  746. if (item_prev == last_child) {
  747. last_child = ti;
  748. }
  749. ti->parent = this;
  750. ti->parent_visible_in_tree = is_visible_in_tree();
  751. return ti;
  752. }
  753. void TreeItem::add_child(TreeItem *p_item) {
  754. ERR_FAIL_NULL(p_item);
  755. ERR_FAIL_COND(p_item->tree);
  756. ERR_FAIL_COND(p_item->parent);
  757. p_item->_change_tree(tree);
  758. p_item->parent = this;
  759. p_item->parent_visible_in_tree = is_visible_in_tree();
  760. p_item->_handle_visibility_changed(p_item->parent_visible_in_tree);
  761. if (last_child) {
  762. last_child->next = p_item;
  763. p_item->prev = last_child;
  764. } else {
  765. first_child = p_item;
  766. }
  767. last_child = p_item;
  768. if (!children_cache.is_empty()) {
  769. children_cache.append(p_item);
  770. }
  771. validate_cache();
  772. }
  773. void TreeItem::remove_child(TreeItem *p_item) {
  774. ERR_FAIL_NULL(p_item);
  775. ERR_FAIL_COND(p_item->parent != this);
  776. p_item->_unlink_from_tree();
  777. p_item->_change_tree(nullptr);
  778. p_item->prev = nullptr;
  779. p_item->next = nullptr;
  780. p_item->parent = nullptr;
  781. validate_cache();
  782. }
  783. Tree *TreeItem::get_tree() const {
  784. return tree;
  785. }
  786. TreeItem *TreeItem::get_next() const {
  787. return next;
  788. }
  789. TreeItem *TreeItem::get_prev() {
  790. if (prev) {
  791. return prev;
  792. }
  793. if (!parent || parent->first_child == this) {
  794. return nullptr;
  795. }
  796. // This is an edge case.
  797. TreeItem *l_prev = parent->first_child;
  798. while (l_prev && l_prev->next != this) {
  799. l_prev = l_prev->next;
  800. }
  801. prev = l_prev;
  802. return prev;
  803. }
  804. TreeItem *TreeItem::get_parent() const {
  805. return parent;
  806. }
  807. TreeItem *TreeItem::get_first_child() const {
  808. return first_child;
  809. }
  810. TreeItem *TreeItem::_get_prev_in_tree(bool p_wrap, bool p_include_invisible) {
  811. TreeItem *current = this;
  812. TreeItem *prev_item = current->get_prev();
  813. if (!prev_item) {
  814. current = current->parent;
  815. if (!current || (current == tree->root && tree->hide_root)) {
  816. if (!p_wrap) {
  817. return nullptr;
  818. }
  819. // Wrap around to the last visible item.
  820. current = this;
  821. TreeItem *temp = get_next_visible();
  822. while (temp) {
  823. current = temp;
  824. temp = temp->get_next_visible();
  825. }
  826. }
  827. } else {
  828. current = prev_item;
  829. while ((!current->collapsed || p_include_invisible) && current->last_child) {
  830. current = current->last_child;
  831. }
  832. }
  833. return current;
  834. }
  835. TreeItem *TreeItem::get_prev_visible(bool p_wrap) {
  836. TreeItem *loop = this;
  837. TreeItem *prev_item = _get_prev_in_tree(p_wrap);
  838. while (prev_item && !prev_item->is_visible_in_tree()) {
  839. prev_item = prev_item->_get_prev_in_tree(p_wrap);
  840. if (prev_item == loop) {
  841. // Check that we haven't looped all the way around to the start.
  842. prev_item = nullptr;
  843. break;
  844. }
  845. }
  846. return prev_item;
  847. }
  848. TreeItem *TreeItem::_get_next_in_tree(bool p_wrap, bool p_include_invisible) {
  849. TreeItem *current = this;
  850. if ((!current->collapsed || p_include_invisible) && current->first_child) {
  851. current = current->first_child;
  852. } else if (current->next) {
  853. current = current->next;
  854. } else {
  855. while (current && !current->next) {
  856. current = current->parent;
  857. }
  858. if (!current) {
  859. if (p_wrap) {
  860. return tree->root;
  861. } else {
  862. return nullptr;
  863. }
  864. } else {
  865. current = current->next;
  866. }
  867. }
  868. return current;
  869. }
  870. TreeItem *TreeItem::get_next_visible(bool p_wrap) {
  871. TreeItem *loop = this;
  872. TreeItem *next_item = _get_next_in_tree(p_wrap);
  873. while (next_item && !next_item->is_visible_in_tree()) {
  874. next_item = next_item->_get_next_in_tree(p_wrap);
  875. if (next_item == loop) {
  876. // Check that we haven't looped all the way around to the start.
  877. next_item = nullptr;
  878. break;
  879. }
  880. }
  881. return next_item;
  882. }
  883. TreeItem *TreeItem::get_prev_in_tree(bool p_wrap) {
  884. TreeItem *prev_item = _get_prev_in_tree(p_wrap, true);
  885. return prev_item;
  886. }
  887. TreeItem *TreeItem::get_next_in_tree(bool p_wrap) {
  888. TreeItem *next_item = _get_next_in_tree(p_wrap, true);
  889. return next_item;
  890. }
  891. TreeItem *TreeItem::get_child(int p_index) {
  892. _create_children_cache();
  893. if (p_index < 0) {
  894. p_index += children_cache.size();
  895. }
  896. ERR_FAIL_INDEX_V(p_index, children_cache.size(), nullptr);
  897. return children_cache.get(p_index);
  898. }
  899. int TreeItem::get_visible_child_count() {
  900. _create_children_cache();
  901. int visible_count = 0;
  902. for (int i = 0; i < children_cache.size(); i++) {
  903. if (children_cache[i]->is_visible()) {
  904. visible_count += 1;
  905. }
  906. }
  907. return visible_count;
  908. }
  909. int TreeItem::get_child_count() {
  910. _create_children_cache();
  911. return children_cache.size();
  912. }
  913. TypedArray<TreeItem> TreeItem::get_children() {
  914. // Don't need to explicitly create children cache, because get_child_count creates it.
  915. int size = get_child_count();
  916. TypedArray<TreeItem> arr;
  917. arr.resize(size);
  918. for (int i = 0; i < size; i++) {
  919. arr[i] = children_cache[i];
  920. }
  921. return arr;
  922. }
  923. void TreeItem::clear_children() {
  924. TreeItem *c = first_child;
  925. while (c) {
  926. TreeItem *aux = c;
  927. c = c->get_next();
  928. aux->parent = nullptr; // So it won't try to recursively auto-remove from me in here.
  929. memdelete(aux);
  930. }
  931. first_child = nullptr;
  932. last_child = nullptr;
  933. children_cache.clear();
  934. }
  935. int TreeItem::get_index() {
  936. int idx = 0;
  937. TreeItem *c = this;
  938. while (c) {
  939. c = c->get_prev();
  940. idx++;
  941. }
  942. return idx - 1;
  943. }
  944. #ifdef DEV_ENABLED
  945. void TreeItem::validate_cache() const {
  946. if (!parent || parent->children_cache.is_empty()) {
  947. return;
  948. }
  949. TreeItem *scan = parent->first_child;
  950. int index = 0;
  951. while (scan) {
  952. DEV_ASSERT(parent->children_cache[index] == scan);
  953. ++index;
  954. scan = scan->get_next();
  955. }
  956. DEV_ASSERT(index == parent->children_cache.size());
  957. }
  958. #endif
  959. void TreeItem::move_before(TreeItem *p_item) {
  960. ERR_FAIL_NULL(p_item);
  961. ERR_FAIL_COND(is_root);
  962. ERR_FAIL_NULL(p_item->parent);
  963. if (p_item == this) {
  964. return;
  965. }
  966. TreeItem *p = p_item->parent;
  967. while (p) {
  968. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  969. p = p->parent;
  970. }
  971. Tree *old_tree = tree;
  972. _unlink_from_tree();
  973. _change_tree(p_item->tree);
  974. parent = p_item->parent;
  975. TreeItem *item_prev = p_item->get_prev();
  976. if (item_prev) {
  977. item_prev->next = this;
  978. parent->children_cache.clear();
  979. } else {
  980. parent->first_child = this;
  981. // If the cache is empty, it has not been built but there
  982. // are items in the tree (note p_item != nullptr) so we cannot update it.
  983. if (!parent->children_cache.is_empty()) {
  984. parent->children_cache.insert(0, this);
  985. }
  986. }
  987. prev = item_prev;
  988. next = p_item;
  989. p_item->prev = this;
  990. if (tree && old_tree == tree) {
  991. tree->queue_accessibility_update();
  992. tree->queue_redraw();
  993. }
  994. validate_cache();
  995. }
  996. void TreeItem::move_after(TreeItem *p_item) {
  997. ERR_FAIL_NULL(p_item);
  998. ERR_FAIL_COND(is_root);
  999. ERR_FAIL_NULL(p_item->parent);
  1000. if (p_item == this) {
  1001. return;
  1002. }
  1003. TreeItem *p = p_item->parent;
  1004. while (p) {
  1005. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  1006. p = p->parent;
  1007. }
  1008. Tree *old_tree = tree;
  1009. _unlink_from_tree();
  1010. _change_tree(p_item->tree);
  1011. if (p_item->next) {
  1012. p_item->next->prev = this;
  1013. }
  1014. parent = p_item->parent;
  1015. prev = p_item;
  1016. next = p_item->next;
  1017. p_item->next = this;
  1018. if (next) {
  1019. parent->children_cache.clear();
  1020. } else {
  1021. parent->last_child = this;
  1022. // If the cache is empty, it has not been built but there
  1023. // are items in the tree (note p_item != nullptr,) so we cannot update it.
  1024. if (!parent->children_cache.is_empty()) {
  1025. parent->children_cache.append(this);
  1026. }
  1027. }
  1028. if (tree && old_tree == tree) {
  1029. tree->queue_accessibility_update();
  1030. tree->queue_redraw();
  1031. }
  1032. validate_cache();
  1033. }
  1034. void TreeItem::set_selectable(int p_column, bool p_selectable) {
  1035. ERR_FAIL_INDEX(p_column, cells.size());
  1036. cells.write[p_column].selectable = p_selectable;
  1037. }
  1038. bool TreeItem::is_selectable(int p_column) const {
  1039. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1040. return cells[p_column].selectable;
  1041. }
  1042. bool TreeItem::is_selected(int p_column) {
  1043. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1044. return cells[p_column].selectable && cells[p_column].selected;
  1045. }
  1046. void TreeItem::set_as_cursor(int p_column) {
  1047. ERR_FAIL_INDEX(p_column, cells.size());
  1048. if (!tree) {
  1049. return;
  1050. }
  1051. if (tree->select_mode != Tree::SELECT_MULTI) {
  1052. return;
  1053. }
  1054. if (tree->selected_item == this && tree->selected_col == p_column) {
  1055. return;
  1056. }
  1057. tree->selected_item = this;
  1058. tree->selected_col = p_column;
  1059. tree->selected_button = -1;
  1060. tree->queue_accessibility_update();
  1061. tree->queue_redraw();
  1062. }
  1063. void TreeItem::select(int p_column) {
  1064. ERR_FAIL_INDEX(p_column, cells.size());
  1065. _cell_selected(p_column);
  1066. }
  1067. void TreeItem::deselect(int p_column) {
  1068. ERR_FAIL_INDEX(p_column, cells.size());
  1069. _cell_deselected(p_column);
  1070. }
  1071. void TreeItem::clear_buttons() {
  1072. int i = 0;
  1073. for (Cell &cell : cells) {
  1074. if (!cell.buttons.is_empty()) {
  1075. for (Cell::Button &btn : cell.buttons) {
  1076. if (btn.accessibility_button_element.is_valid()) {
  1077. DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
  1078. }
  1079. }
  1080. cell.buttons.clear();
  1081. cell.cached_minimum_size_dirty = true;
  1082. _changed_notify(i);
  1083. }
  1084. ++i;
  1085. }
  1086. }
  1087. void TreeItem::add_button(int p_column, const Ref<Texture2D> &p_button, int p_id, bool p_disabled, const String &p_tooltip, const String &p_alt_text) {
  1088. ERR_FAIL_INDEX(p_column, cells.size());
  1089. ERR_FAIL_COND(p_button.is_null());
  1090. TreeItem::Cell::Button button;
  1091. button.texture = p_button;
  1092. if (p_id < 0) {
  1093. p_id = cells[p_column].buttons.size();
  1094. }
  1095. button.id = p_id;
  1096. button.disabled = p_disabled;
  1097. button.tooltip = p_tooltip;
  1098. button.alt_text = p_alt_text;
  1099. cells.write[p_column].buttons.push_back(button);
  1100. cells.write[p_column].cached_minimum_size_dirty = true;
  1101. _changed_notify(p_column);
  1102. if (get_tree()) {
  1103. get_tree()->update_configuration_warnings();
  1104. }
  1105. }
  1106. int TreeItem::get_button_count(int p_column) const {
  1107. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1108. return cells[p_column].buttons.size();
  1109. }
  1110. Ref<Texture2D> TreeItem::get_button(int p_column, int p_index) const {
  1111. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  1112. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Ref<Texture2D>());
  1113. return cells[p_column].buttons[p_index].texture;
  1114. }
  1115. String TreeItem::get_button_tooltip_text(int p_column, int p_index) const {
  1116. ERR_FAIL_INDEX_V(p_column, cells.size(), String());
  1117. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), String());
  1118. return cells[p_column].buttons[p_index].tooltip;
  1119. }
  1120. int TreeItem::get_button_id(int p_column, int p_index) const {
  1121. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1122. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), -1);
  1123. return cells[p_column].buttons[p_index].id;
  1124. }
  1125. void TreeItem::erase_button(int p_column, int p_index) {
  1126. ERR_FAIL_INDEX(p_column, cells.size());
  1127. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1128. if (cells[p_column].buttons[p_index].accessibility_button_element.is_valid()) {
  1129. DisplayServer::get_singleton()->accessibility_free_element(cells.write[p_column].buttons.write[p_index].accessibility_button_element);
  1130. }
  1131. cells.write[p_column].buttons.remove_at(p_index);
  1132. _changed_notify(p_column);
  1133. if (get_tree()) {
  1134. get_tree()->update_configuration_warnings();
  1135. }
  1136. }
  1137. int TreeItem::get_button_by_id(int p_column, int p_id) const {
  1138. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1139. for (int i = 0; i < cells[p_column].buttons.size(); i++) {
  1140. if (cells[p_column].buttons[i].id == p_id) {
  1141. return i;
  1142. }
  1143. }
  1144. return -1;
  1145. }
  1146. Color TreeItem::get_button_color(int p_column, int p_index) const {
  1147. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1148. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Color());
  1149. return cells[p_column].buttons[p_index].color;
  1150. }
  1151. void TreeItem::set_button_tooltip_text(int p_column, int p_index, const String &p_tooltip) {
  1152. ERR_FAIL_INDEX(p_column, cells.size());
  1153. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1154. cells.write[p_column].buttons.write[p_index].tooltip = p_tooltip;
  1155. _changed_notify(p_column);
  1156. }
  1157. void TreeItem::set_button(int p_column, int p_index, const Ref<Texture2D> &p_button) {
  1158. ERR_FAIL_COND(p_button.is_null());
  1159. ERR_FAIL_INDEX(p_column, cells.size());
  1160. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1161. if (cells[p_column].buttons[p_index].texture == p_button) {
  1162. return;
  1163. }
  1164. cells.write[p_column].buttons.write[p_index].texture = p_button;
  1165. cells.write[p_column].cached_minimum_size_dirty = true;
  1166. _changed_notify(p_column);
  1167. }
  1168. void TreeItem::set_button_alt_text(int p_column, int p_index, const String &p_alt_text) {
  1169. ERR_FAIL_INDEX(p_column, cells.size());
  1170. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1171. if (cells[p_column].buttons[p_index].alt_text == p_alt_text) {
  1172. return;
  1173. }
  1174. cells.write[p_column].buttons.write[p_index].alt_text = p_alt_text;
  1175. _changed_notify(p_column);
  1176. if (get_tree()) {
  1177. get_tree()->update_configuration_warnings();
  1178. }
  1179. }
  1180. void TreeItem::set_button_color(int p_column, int p_index, const Color &p_color) {
  1181. ERR_FAIL_INDEX(p_column, cells.size());
  1182. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1183. if (cells[p_column].buttons[p_index].color == p_color) {
  1184. return;
  1185. }
  1186. cells.write[p_column].buttons.write[p_index].color = p_color;
  1187. _changed_notify(p_column);
  1188. }
  1189. void TreeItem::set_button_disabled(int p_column, int p_index, bool p_disabled) {
  1190. ERR_FAIL_INDEX(p_column, cells.size());
  1191. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1192. if (cells[p_column].buttons[p_index].disabled == p_disabled) {
  1193. return;
  1194. }
  1195. cells.write[p_column].buttons.write[p_index].disabled = p_disabled;
  1196. cells.write[p_column].cached_minimum_size_dirty = true;
  1197. _changed_notify(p_column);
  1198. }
  1199. bool TreeItem::is_button_disabled(int p_column, int p_index) const {
  1200. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1201. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), false);
  1202. return cells[p_column].buttons[p_index].disabled;
  1203. }
  1204. void TreeItem::set_editable(int p_column, bool p_editable) {
  1205. ERR_FAIL_INDEX(p_column, cells.size());
  1206. if (cells[p_column].editable == p_editable) {
  1207. return;
  1208. }
  1209. cells.write[p_column].editable = p_editable;
  1210. cells.write[p_column].cached_minimum_size_dirty = true;
  1211. _changed_notify(p_column);
  1212. }
  1213. bool TreeItem::is_editable(int p_column) {
  1214. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1215. return cells[p_column].editable;
  1216. }
  1217. void TreeItem::set_custom_color(int p_column, const Color &p_color) {
  1218. ERR_FAIL_INDEX(p_column, cells.size());
  1219. if (cells[p_column].custom_color && cells[p_column].color == p_color) {
  1220. return;
  1221. }
  1222. cells.write[p_column].custom_color = true;
  1223. cells.write[p_column].color = p_color;
  1224. _changed_notify(p_column);
  1225. }
  1226. Color TreeItem::get_custom_color(int p_column) const {
  1227. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1228. if (!cells[p_column].custom_color) {
  1229. return Color();
  1230. }
  1231. return cells[p_column].color;
  1232. }
  1233. void TreeItem::clear_custom_color(int p_column) {
  1234. ERR_FAIL_INDEX(p_column, cells.size());
  1235. cells.write[p_column].custom_color = false;
  1236. cells.write[p_column].color = Color();
  1237. _changed_notify(p_column);
  1238. }
  1239. void TreeItem::set_custom_font(int p_column, const Ref<Font> &p_font) {
  1240. ERR_FAIL_INDEX(p_column, cells.size());
  1241. if (cells[p_column].custom_font == p_font) {
  1242. return;
  1243. }
  1244. cells.write[p_column].custom_font = p_font;
  1245. cells.write[p_column].cached_minimum_size_dirty = true;
  1246. _changed_notify(p_column);
  1247. }
  1248. Ref<Font> TreeItem::get_custom_font(int p_column) const {
  1249. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Font>());
  1250. return cells[p_column].custom_font;
  1251. }
  1252. void TreeItem::set_custom_font_size(int p_column, int p_font_size) {
  1253. ERR_FAIL_INDEX(p_column, cells.size());
  1254. if (cells[p_column].custom_font_size == p_font_size) {
  1255. return;
  1256. }
  1257. cells.write[p_column].custom_font_size = p_font_size;
  1258. cells.write[p_column].cached_minimum_size_dirty = true;
  1259. _changed_notify(p_column);
  1260. }
  1261. int TreeItem::get_custom_font_size(int p_column) const {
  1262. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1263. return cells[p_column].custom_font_size;
  1264. }
  1265. void TreeItem::set_tooltip_text(int p_column, const String &p_tooltip) {
  1266. ERR_FAIL_INDEX(p_column, cells.size());
  1267. cells.write[p_column].tooltip = p_tooltip;
  1268. }
  1269. String TreeItem::get_tooltip_text(int p_column) const {
  1270. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  1271. return cells[p_column].tooltip;
  1272. }
  1273. void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
  1274. ERR_FAIL_INDEX(p_column, cells.size());
  1275. if (cells[p_column].custom_bg_color && cells[p_column].custom_bg_outline == p_bg_outline && cells[p_column].bg_color == p_color) {
  1276. return;
  1277. }
  1278. cells.write[p_column].custom_bg_color = true;
  1279. cells.write[p_column].custom_bg_outline = p_bg_outline;
  1280. cells.write[p_column].bg_color = p_color;
  1281. _changed_notify(p_column);
  1282. }
  1283. void TreeItem::clear_custom_bg_color(int p_column) {
  1284. ERR_FAIL_INDEX(p_column, cells.size());
  1285. cells.write[p_column].custom_bg_color = false;
  1286. cells.write[p_column].bg_color = Color();
  1287. _changed_notify(p_column);
  1288. }
  1289. Color TreeItem::get_custom_bg_color(int p_column) const {
  1290. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1291. if (!cells[p_column].custom_bg_color) {
  1292. return Color();
  1293. }
  1294. return cells[p_column].bg_color;
  1295. }
  1296. void TreeItem::set_custom_as_button(int p_column, bool p_button) {
  1297. ERR_FAIL_INDEX(p_column, cells.size());
  1298. if (cells[p_column].custom_button == p_button) {
  1299. return;
  1300. }
  1301. cells.write[p_column].custom_button = p_button;
  1302. cells.write[p_column].cached_minimum_size_dirty = true;
  1303. _changed_notify(p_column);
  1304. }
  1305. bool TreeItem::is_custom_set_as_button(int p_column) const {
  1306. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1307. return cells[p_column].custom_button;
  1308. }
  1309. void TreeItem::set_text_alignment(int p_column, HorizontalAlignment p_alignment) {
  1310. ERR_FAIL_INDEX(p_column, cells.size());
  1311. if (cells[p_column].text_alignment == p_alignment) {
  1312. return;
  1313. }
  1314. cells.write[p_column].text_alignment = p_alignment;
  1315. cells.write[p_column].cached_minimum_size_dirty = true;
  1316. _changed_notify(p_column);
  1317. }
  1318. HorizontalAlignment TreeItem::get_text_alignment(int p_column) const {
  1319. ERR_FAIL_INDEX_V(p_column, cells.size(), HORIZONTAL_ALIGNMENT_LEFT);
  1320. return cells[p_column].text_alignment;
  1321. }
  1322. void TreeItem::set_expand_right(int p_column, bool p_enable) {
  1323. ERR_FAIL_INDEX(p_column, cells.size());
  1324. if (cells[p_column].expand_right == p_enable) {
  1325. return;
  1326. }
  1327. cells.write[p_column].expand_right = p_enable;
  1328. cells.write[p_column].cached_minimum_size_dirty = true;
  1329. _changed_notify(p_column);
  1330. }
  1331. bool TreeItem::get_expand_right(int p_column) const {
  1332. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1333. return cells[p_column].expand_right;
  1334. }
  1335. void TreeItem::set_disable_folding(bool p_disable) {
  1336. if (disable_folding == p_disable) {
  1337. return;
  1338. }
  1339. disable_folding = p_disable;
  1340. for (Cell &c : cells) {
  1341. c.cached_minimum_size_dirty = true;
  1342. }
  1343. _changed_notify(0);
  1344. }
  1345. bool TreeItem::is_folding_disabled() const {
  1346. return disable_folding;
  1347. }
  1348. Size2 TreeItem::get_minimum_size(int p_column) {
  1349. ERR_FAIL_INDEX_V(p_column, cells.size(), Size2());
  1350. Tree *parent_tree = get_tree();
  1351. ERR_FAIL_NULL_V(parent_tree, Size2());
  1352. const TreeItem::Cell &cell = cells[p_column];
  1353. if (cell.cached_minimum_size_dirty || cell.text_buf->is_dirty() || cell.dirty) {
  1354. Size2 size = Size2(
  1355. parent_tree->theme_cache.inner_item_margin_left + parent_tree->theme_cache.inner_item_margin_right,
  1356. parent_tree->theme_cache.inner_item_margin_top + parent_tree->theme_cache.inner_item_margin_bottom);
  1357. // Text.
  1358. if (!cell.text.is_empty()) {
  1359. if (cell.dirty) {
  1360. parent_tree->update_item_cell(this, p_column);
  1361. }
  1362. Size2 text_size = cell.text_buf->get_size();
  1363. if (get_text_overrun_behavior(p_column) == TextServer::OVERRUN_NO_TRIMMING) {
  1364. size.width += text_size.width;
  1365. }
  1366. size.height = MAX(size.height, text_size.height);
  1367. }
  1368. // Icon.
  1369. if (cell.mode == CELL_MODE_CHECK) {
  1370. Size2i check_size = parent_tree->theme_cache.checked->get_size();
  1371. size.width += check_size.width + parent_tree->theme_cache.h_separation;
  1372. size.height = MAX(size.height, check_size.height);
  1373. }
  1374. if (cell.icon.is_valid()) {
  1375. Size2i icon_size = parent_tree->_get_cell_icon_size(cell);
  1376. size.width += icon_size.width + parent_tree->theme_cache.h_separation;
  1377. size.height = MAX(size.height, icon_size.height);
  1378. }
  1379. // Buttons.
  1380. for (int i = 0; i < cell.buttons.size(); i++) {
  1381. Ref<Texture2D> texture = cell.buttons[i].texture;
  1382. if (texture.is_valid()) {
  1383. Size2 button_size = texture->get_size();
  1384. button_size.width += parent_tree->theme_cache.button_pressed->get_minimum_size().width;
  1385. size.width += button_size.width + parent_tree->theme_cache.button_margin;
  1386. size.height = MAX(size.height, button_size.height);
  1387. }
  1388. }
  1389. cells.write[p_column].cached_minimum_size = size;
  1390. cells.write[p_column].cached_minimum_size_dirty = false;
  1391. }
  1392. return cell.cached_minimum_size;
  1393. }
  1394. void TreeItem::_call_recursive_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1395. if (p_argcount < 1) {
  1396. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1397. r_error.expected = 1;
  1398. return;
  1399. }
  1400. if (!p_args[0]->is_string()) {
  1401. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1402. r_error.argument = 0;
  1403. r_error.expected = Variant::STRING_NAME;
  1404. return;
  1405. }
  1406. StringName method = *p_args[0];
  1407. call_recursive(method, &p_args[1], p_argcount - 1, r_error);
  1408. }
  1409. void recursive_call_aux(TreeItem *p_item, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1410. if (!p_item) {
  1411. return;
  1412. }
  1413. p_item->callp(p_method, p_args, p_argcount, r_error);
  1414. TreeItem *c = p_item->get_first_child();
  1415. while (c) {
  1416. recursive_call_aux(c, p_method, p_args, p_argcount, r_error);
  1417. c = c->get_next();
  1418. }
  1419. }
  1420. void TreeItem::call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1421. recursive_call_aux(this, p_method, p_args, p_argcount, r_error);
  1422. }
  1423. void TreeItem::_bind_methods() {
  1424. ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
  1425. ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
  1426. ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "column", "mode"), &TreeItem::set_auto_translate_mode);
  1427. ClassDB::bind_method(D_METHOD("get_auto_translate_mode", "column"), &TreeItem::get_auto_translate_mode);
  1428. ClassDB::bind_method(D_METHOD("set_edit_multiline", "column", "multiline"), &TreeItem::set_edit_multiline);
  1429. ClassDB::bind_method(D_METHOD("is_edit_multiline", "column"), &TreeItem::is_edit_multiline);
  1430. ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
  1431. ClassDB::bind_method(D_METHOD("set_indeterminate", "column", "indeterminate"), &TreeItem::set_indeterminate);
  1432. ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
  1433. ClassDB::bind_method(D_METHOD("is_indeterminate", "column"), &TreeItem::is_indeterminate);
  1434. ClassDB::bind_method(D_METHOD("propagate_check", "column", "emit_signal"), &TreeItem::propagate_check, DEFVAL(true));
  1435. ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
  1436. ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
  1437. ClassDB::bind_method(D_METHOD("set_alt_text", "column", "text"), &TreeItem::set_alt_text);
  1438. ClassDB::bind_method(D_METHOD("get_alt_text", "column"), &TreeItem::get_alt_text);
  1439. ClassDB::bind_method(D_METHOD("set_text_direction", "column", "direction"), &TreeItem::set_text_direction);
  1440. ClassDB::bind_method(D_METHOD("get_text_direction", "column"), &TreeItem::get_text_direction);
  1441. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "column", "autowrap_mode"), &TreeItem::set_autowrap_mode);
  1442. ClassDB::bind_method(D_METHOD("get_autowrap_mode", "column"), &TreeItem::get_autowrap_mode);
  1443. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "column", "overrun_behavior"), &TreeItem::set_text_overrun_behavior);
  1444. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior", "column"), &TreeItem::get_text_overrun_behavior);
  1445. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "column", "parser"), &TreeItem::set_structured_text_bidi_override);
  1446. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override);
  1447. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "column", "args"), &TreeItem::set_structured_text_bidi_override_options);
  1448. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options", "column"), &TreeItem::get_structured_text_bidi_override_options);
  1449. ClassDB::bind_method(D_METHOD("set_language", "column", "language"), &TreeItem::set_language);
  1450. ClassDB::bind_method(D_METHOD("get_language", "column"), &TreeItem::get_language);
  1451. ClassDB::bind_method(D_METHOD("set_suffix", "column", "text"), &TreeItem::set_suffix);
  1452. ClassDB::bind_method(D_METHOD("get_suffix", "column"), &TreeItem::get_suffix);
  1453. ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
  1454. ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
  1455. ClassDB::bind_method(D_METHOD("set_icon_overlay", "column", "texture"), &TreeItem::set_icon_overlay);
  1456. ClassDB::bind_method(D_METHOD("get_icon_overlay", "column"), &TreeItem::get_icon_overlay);
  1457. ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
  1458. ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
  1459. ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
  1460. ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
  1461. ClassDB::bind_method(D_METHOD("set_icon_modulate", "column", "modulate"), &TreeItem::set_icon_modulate);
  1462. ClassDB::bind_method(D_METHOD("get_icon_modulate", "column"), &TreeItem::get_icon_modulate);
  1463. ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
  1464. ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
  1465. ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
  1466. ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
  1467. ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
  1468. ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
  1469. #ifndef DISABLE_DEPRECATED
  1470. ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
  1471. #endif // DISABLE_DEPRECATED
  1472. ClassDB::bind_method(D_METHOD("set_custom_draw_callback", "column", "callback"), &TreeItem::set_custom_draw_callback);
  1473. ClassDB::bind_method(D_METHOD("get_custom_draw_callback", "column"), &TreeItem::get_custom_draw_callback);
  1474. ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
  1475. ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
  1476. ClassDB::bind_method(D_METHOD("set_collapsed_recursive", "enable"), &TreeItem::set_collapsed_recursive);
  1477. ClassDB::bind_method(D_METHOD("is_any_collapsed", "only_visible"), &TreeItem::is_any_collapsed, DEFVAL(false));
  1478. ClassDB::bind_method(D_METHOD("set_visible", "enable"), &TreeItem::set_visible);
  1479. ClassDB::bind_method(D_METHOD("is_visible"), &TreeItem::is_visible);
  1480. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &TreeItem::is_visible_in_tree);
  1481. ClassDB::bind_method(D_METHOD("uncollapse_tree"), &TreeItem::uncollapse_tree);
  1482. ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
  1483. ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
  1484. ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
  1485. ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
  1486. ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
  1487. ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
  1488. ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
  1489. ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
  1490. ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
  1491. ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
  1492. ClassDB::bind_method(D_METHOD("get_custom_color", "column"), &TreeItem::get_custom_color);
  1493. ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
  1494. ClassDB::bind_method(D_METHOD("set_custom_font", "column", "font"), &TreeItem::set_custom_font);
  1495. ClassDB::bind_method(D_METHOD("get_custom_font", "column"), &TreeItem::get_custom_font);
  1496. ClassDB::bind_method(D_METHOD("set_custom_font_size", "column", "font_size"), &TreeItem::set_custom_font_size);
  1497. ClassDB::bind_method(D_METHOD("get_custom_font_size", "column"), &TreeItem::get_custom_font_size);
  1498. ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
  1499. ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
  1500. ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
  1501. ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
  1502. ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
  1503. ClassDB::bind_method(D_METHOD("clear_buttons"), &TreeItem::clear_buttons);
  1504. ClassDB::bind_method(D_METHOD("add_button", "column", "button", "id", "disabled", "tooltip_text", "alt_text"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""), DEFVAL(""));
  1505. ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
  1506. ClassDB::bind_method(D_METHOD("get_button_tooltip_text", "column", "button_index"), &TreeItem::get_button_tooltip_text);
  1507. ClassDB::bind_method(D_METHOD("get_button_id", "column", "button_index"), &TreeItem::get_button_id);
  1508. ClassDB::bind_method(D_METHOD("get_button_by_id", "column", "id"), &TreeItem::get_button_by_id);
  1509. ClassDB::bind_method(D_METHOD("get_button_color", "column", "id"), &TreeItem::get_button_color);
  1510. ClassDB::bind_method(D_METHOD("get_button", "column", "button_index"), &TreeItem::get_button);
  1511. ClassDB::bind_method(D_METHOD("set_button_tooltip_text", "column", "button_index", "tooltip"), &TreeItem::set_button_tooltip_text);
  1512. ClassDB::bind_method(D_METHOD("set_button", "column", "button_index", "button"), &TreeItem::set_button);
  1513. ClassDB::bind_method(D_METHOD("erase_button", "column", "button_index"), &TreeItem::erase_button);
  1514. ClassDB::bind_method(D_METHOD("set_button_alt_text", "column", "button_index", "alt_text"), &TreeItem::set_button_alt_text);
  1515. ClassDB::bind_method(D_METHOD("set_button_disabled", "column", "button_index", "disabled"), &TreeItem::set_button_disabled);
  1516. ClassDB::bind_method(D_METHOD("set_button_color", "column", "button_index", "color"), &TreeItem::set_button_color);
  1517. ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_index"), &TreeItem::is_button_disabled);
  1518. ClassDB::bind_method(D_METHOD("set_tooltip_text", "column", "tooltip"), &TreeItem::set_tooltip_text);
  1519. ClassDB::bind_method(D_METHOD("get_tooltip_text", "column"), &TreeItem::get_tooltip_text);
  1520. ClassDB::bind_method(D_METHOD("set_text_alignment", "column", "text_alignment"), &TreeItem::set_text_alignment);
  1521. ClassDB::bind_method(D_METHOD("get_text_alignment", "column"), &TreeItem::get_text_alignment);
  1522. ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
  1523. ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
  1524. ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
  1525. ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
  1526. ClassDB::bind_method(D_METHOD("create_child", "index"), &TreeItem::create_child, DEFVAL(-1));
  1527. ClassDB::bind_method(D_METHOD("add_child", "child"), &TreeItem::add_child);
  1528. ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::remove_child);
  1529. ClassDB::bind_method(D_METHOD("get_tree"), &TreeItem::get_tree);
  1530. ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
  1531. ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
  1532. ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
  1533. ClassDB::bind_method(D_METHOD("get_first_child"), &TreeItem::get_first_child);
  1534. ClassDB::bind_method(D_METHOD("get_next_in_tree", "wrap"), &TreeItem::get_next_in_tree, DEFVAL(false));
  1535. ClassDB::bind_method(D_METHOD("get_prev_in_tree", "wrap"), &TreeItem::get_prev_in_tree, DEFVAL(false));
  1536. ClassDB::bind_method(D_METHOD("get_next_visible", "wrap"), &TreeItem::get_next_visible, DEFVAL(false));
  1537. ClassDB::bind_method(D_METHOD("get_prev_visible", "wrap"), &TreeItem::get_prev_visible, DEFVAL(false));
  1538. ClassDB::bind_method(D_METHOD("get_child", "index"), &TreeItem::get_child);
  1539. ClassDB::bind_method(D_METHOD("get_child_count"), &TreeItem::get_child_count);
  1540. ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
  1541. ClassDB::bind_method(D_METHOD("get_index"), &TreeItem::get_index);
  1542. ClassDB::bind_method(D_METHOD("move_before", "item"), &TreeItem::move_before);
  1543. ClassDB::bind_method(D_METHOD("move_after", "item"), &TreeItem::move_after);
  1544. {
  1545. MethodInfo mi;
  1546. mi.name = "call_recursive";
  1547. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1548. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_recursive", &TreeItem::_call_recursive_bind, mi);
  1549. }
  1550. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  1551. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1552. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
  1553. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
  1554. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  1555. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  1556. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  1557. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  1558. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  1559. }
  1560. TreeItem::TreeItem(Tree *p_tree) {
  1561. tree = p_tree;
  1562. }
  1563. TreeItem::~TreeItem() {
  1564. _unlink_from_tree();
  1565. _change_tree(nullptr);
  1566. validate_cache();
  1567. prev = nullptr;
  1568. clear_children();
  1569. }
  1570. /*************************** END of TreeItem ***************************/
  1571. void Tree::_update_theme_item_cache() {
  1572. Control::_update_theme_item_cache();
  1573. theme_cache.base_scale = get_theme_default_base_scale();
  1574. }
  1575. Size2 Tree::_get_cell_icon_size(const TreeItem::Cell &p_cell) const {
  1576. Size2i icon_size = p_cell.get_icon_size();
  1577. int max_width = 0;
  1578. if (theme_cache.icon_max_width > 0) {
  1579. max_width = theme_cache.icon_max_width;
  1580. }
  1581. if (p_cell.icon_max_w > 0 && (max_width == 0 || p_cell.icon_max_w < max_width)) {
  1582. max_width = p_cell.icon_max_w;
  1583. }
  1584. if (max_width > 0 && icon_size.width > max_width) {
  1585. icon_size.height = icon_size.height * max_width / icon_size.width;
  1586. icon_size.width = max_width;
  1587. }
  1588. return icon_size;
  1589. }
  1590. int Tree::compute_item_height(TreeItem *p_item) const {
  1591. if ((p_item == root && hide_root) || !p_item->is_visible_in_tree()) {
  1592. return 0;
  1593. }
  1594. ERR_FAIL_COND_V(theme_cache.font.is_null(), 0);
  1595. int height = 0;
  1596. for (int i = 0; i < columns.size(); i++) {
  1597. height = MAX(height, p_item->get_minimum_size(i).y);
  1598. }
  1599. int item_min_height = MAX(theme_cache.font->get_height(theme_cache.font_size), p_item->get_custom_minimum_height());
  1600. if (height < item_min_height) {
  1601. height = item_min_height;
  1602. }
  1603. height += theme_cache.v_separation;
  1604. return height;
  1605. }
  1606. int Tree::get_item_height(TreeItem *p_item) const {
  1607. if (!p_item->is_visible_in_tree()) {
  1608. return 0;
  1609. }
  1610. int height = compute_item_height(p_item);
  1611. height += theme_cache.v_separation;
  1612. if (!p_item->collapsed) { // If not collapsed, check the children.
  1613. TreeItem *c = p_item->first_child;
  1614. while (c) {
  1615. height += get_item_height(c);
  1616. c = c->next;
  1617. }
  1618. }
  1619. return height;
  1620. }
  1621. void Tree::draw_item_rect(TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color, int p_ol_size, const Color &p_ol_color) {
  1622. ERR_FAIL_COND(theme_cache.font.is_null());
  1623. Rect2i rect = p_rect.grow_individual(-theme_cache.inner_item_margin_left, -theme_cache.inner_item_margin_top, -theme_cache.inner_item_margin_right, -theme_cache.inner_item_margin_bottom);
  1624. Size2 ts = p_cell.text_buf->get_size();
  1625. bool rtl = is_layout_rtl();
  1626. Size2i icon_size;
  1627. if (p_cell.icon.is_valid()) {
  1628. icon_size = _get_cell_icon_size(p_cell);
  1629. }
  1630. int displayed_width = 0;
  1631. if (p_cell.icon.is_valid()) {
  1632. displayed_width += icon_size.width + theme_cache.h_separation;
  1633. }
  1634. if (displayed_width + ts.width > rect.size.width) {
  1635. ts.width = rect.size.width - displayed_width;
  1636. }
  1637. displayed_width += ts.width;
  1638. int empty_width = rect.size.width - displayed_width;
  1639. switch (p_cell.text_alignment) {
  1640. case HORIZONTAL_ALIGNMENT_FILL:
  1641. case HORIZONTAL_ALIGNMENT_LEFT: {
  1642. if (rtl) {
  1643. rect.position.x += empty_width;
  1644. }
  1645. } break;
  1646. case HORIZONTAL_ALIGNMENT_CENTER:
  1647. rect.position.x += empty_width / 2;
  1648. break;
  1649. case HORIZONTAL_ALIGNMENT_RIGHT:
  1650. if (!rtl) {
  1651. rect.position.x += empty_width;
  1652. }
  1653. break;
  1654. }
  1655. RID ci = get_canvas_item();
  1656. if (rtl) {
  1657. if (ts.width > 0) {
  1658. Point2 draw_pos = rect.position;
  1659. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
  1660. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1661. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1662. }
  1663. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1664. }
  1665. rect.position.x += ts.width + theme_cache.h_separation;
  1666. }
  1667. if (p_cell.icon.is_valid()) {
  1668. p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - icon_size.y) / 2)), icon_size, p_icon_color);
  1669. rect.position.x += icon_size.x + theme_cache.h_separation;
  1670. }
  1671. if (!rtl && ts.width > 0) {
  1672. Point2 draw_pos = rect.position;
  1673. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
  1674. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1675. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1676. }
  1677. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1678. }
  1679. }
  1680. void Tree::update_column(int p_col) {
  1681. columns.write[p_col].text_buf->clear();
  1682. if (columns[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1683. columns.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1684. } else {
  1685. columns.write[p_col].text_buf->set_direction((TextServer::Direction)columns[p_col].text_direction);
  1686. }
  1687. columns.write[p_col].xl_title = atr(columns[p_col].title);
  1688. columns.write[p_col].text_buf->add_string(columns[p_col].xl_title, theme_cache.tb_font, theme_cache.tb_font_size, columns[p_col].language);
  1689. columns.write[p_col].cached_minimum_width_dirty = true;
  1690. }
  1691. void Tree::update_item_cell(TreeItem *p_item, int p_col) const {
  1692. String valtext;
  1693. p_item->cells.write[p_col].text_buf->clear();
  1694. if (p_item->cells[p_col].mode == TreeItem::CELL_MODE_RANGE) {
  1695. if (!p_item->cells[p_col].text.is_empty()) {
  1696. if (!p_item->cells[p_col].editable) {
  1697. return;
  1698. }
  1699. int option = (int)p_item->cells[p_col].val;
  1700. valtext = p_item->atr(p_col, ETR("(Other)"));
  1701. Vector<String> strings = p_item->cells[p_col].text.split(",");
  1702. for (int j = 0; j < strings.size(); j++) {
  1703. int value = j;
  1704. if (!strings[j].get_slicec(':', 1).is_empty()) {
  1705. value = strings[j].get_slicec(':', 1).to_int();
  1706. }
  1707. if (option == value) {
  1708. valtext = p_item->atr(p_col, strings[j].get_slicec(':', 0));
  1709. break;
  1710. }
  1711. }
  1712. } else {
  1713. valtext = String::num(p_item->cells[p_col].val, Math::range_step_decimals(p_item->cells[p_col].step));
  1714. }
  1715. } else {
  1716. // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
  1717. if (!p_item->cells[p_col].editable || p_item->cells[p_col].mode != TreeItem::CELL_MODE_STRING) {
  1718. p_item->cells.write[p_col].xl_text = p_item->atr(p_col, p_item->cells[p_col].text);
  1719. } else {
  1720. p_item->cells.write[p_col].xl_text = p_item->cells[p_col].text;
  1721. }
  1722. valtext = p_item->cells[p_col].xl_text;
  1723. }
  1724. if (!p_item->cells[p_col].suffix.is_empty()) {
  1725. if (!valtext.is_empty()) {
  1726. valtext += " ";
  1727. }
  1728. valtext += p_item->cells[p_col].suffix;
  1729. }
  1730. if (p_item->cells[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1731. p_item->cells.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1732. } else {
  1733. p_item->cells.write[p_col].text_buf->set_direction((TextServer::Direction)p_item->cells[p_col].text_direction);
  1734. }
  1735. Ref<Font> font;
  1736. if (p_item->cells[p_col].custom_font.is_valid()) {
  1737. font = p_item->cells[p_col].custom_font;
  1738. } else {
  1739. font = theme_cache.font;
  1740. }
  1741. int font_size;
  1742. if (p_item->cells[p_col].custom_font_size > 0) {
  1743. font_size = p_item->cells[p_col].custom_font_size;
  1744. } else {
  1745. font_size = theme_cache.font_size;
  1746. }
  1747. p_item->cells.write[p_col].text_buf->add_string(valtext, font, font_size, p_item->cells[p_col].language);
  1748. BitField<TextServer::LineBreakFlag> break_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES;
  1749. switch (p_item->cells.write[p_col].autowrap_mode) {
  1750. case TextServer::AUTOWRAP_OFF:
  1751. break;
  1752. case TextServer::AUTOWRAP_ARBITRARY:
  1753. break_flags.set_flag(TextServer::BREAK_GRAPHEME_BOUND);
  1754. break;
  1755. case TextServer::AUTOWRAP_WORD:
  1756. break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
  1757. break;
  1758. case TextServer::AUTOWRAP_WORD_SMART:
  1759. break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
  1760. break_flags.set_flag(TextServer::BREAK_ADAPTIVE);
  1761. break;
  1762. }
  1763. p_item->cells.write[p_col].text_buf->set_break_flags(break_flags);
  1764. TS->shaped_text_set_bidi_override(p_item->cells[p_col].text_buf->get_rid(), structured_text_parser(p_item->cells[p_col].st_parser, p_item->cells[p_col].st_args, valtext));
  1765. p_item->cells.write[p_col].dirty = false;
  1766. }
  1767. void Tree::update_item_cache(TreeItem *p_item) const {
  1768. for (int i = 0; i < p_item->cells.size(); i++) {
  1769. update_item_cell(p_item, i);
  1770. }
  1771. TreeItem *c = p_item->first_child;
  1772. while (c) {
  1773. update_item_cache(c);
  1774. c = c->next;
  1775. }
  1776. }
  1777. int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item, int &r_self_height) {
  1778. if (p_pos.y - theme_cache.offset.y > (p_draw_size.height)) {
  1779. return -1; // Draw no more!
  1780. }
  1781. if (!p_item->is_visible_in_tree()) {
  1782. return 0;
  1783. }
  1784. RID ci = get_canvas_item();
  1785. int htotal = 0;
  1786. int label_h = 0;
  1787. bool rtl = cache.rtl;
  1788. // Draw label, if height fits.
  1789. bool skip = (p_item == root && hide_root);
  1790. if (!skip) {
  1791. // Draw separation.
  1792. ERR_FAIL_COND_V(theme_cache.font.is_null(), -1);
  1793. int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin);
  1794. int skip2 = 0;
  1795. bool is_row_hovered = (!cache.hover_header_row && cache.hover_item == p_item);
  1796. for (int i = 0; i < columns.size(); i++) {
  1797. if (skip2) {
  1798. skip2--;
  1799. continue;
  1800. }
  1801. bool is_col_hovered = cache.hover_column == i;
  1802. bool is_cell_hovered = is_row_hovered && is_col_hovered;
  1803. bool is_cell_button_hovered = is_cell_hovered && cache.hover_button_index_in_column != -1;
  1804. int item_width = get_column_width(i);
  1805. if (i == 0) {
  1806. item_width -= ofs;
  1807. if (item_width <= 0) {
  1808. ofs = get_column_width(0);
  1809. continue;
  1810. }
  1811. } else {
  1812. ofs += theme_cache.h_separation;
  1813. item_width -= theme_cache.h_separation;
  1814. }
  1815. if (p_item->cells[i].expand_right) {
  1816. int plus = 1;
  1817. while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].xl_text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
  1818. item_width += get_column_width(i + plus);
  1819. plus++;
  1820. skip2++;
  1821. }
  1822. }
  1823. if (!p_item->cells[i].buttons.is_empty()) {
  1824. int buttons_width = 0;
  1825. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1826. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  1827. buttons_width += button_texture->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
  1828. }
  1829. double total_ofs = ofs - theme_cache.offset.x;
  1830. // If part of the column is beyond the right side of the control due to scrolling, clamp the label width
  1831. // so that all buttons attached to the cell remain within view.
  1832. if (total_ofs + item_width > p_draw_size.width) {
  1833. item_width = MAX(buttons_width, p_draw_size.width - total_ofs);
  1834. }
  1835. }
  1836. int item_width_with_buttons = item_width; // Used later for drawing buttons.
  1837. int buttons_width = 0;
  1838. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1839. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  1840. Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
  1841. item_width -= button_size.width + theme_cache.button_margin;
  1842. buttons_width += button_size.width + theme_cache.button_margin;
  1843. }
  1844. int text_width = item_width - theme_cache.inner_item_margin_left - theme_cache.inner_item_margin_right;
  1845. if (p_item->cells[i].icon.is_valid()) {
  1846. text_width -= _get_cell_icon_size(p_item->cells[i]).x + theme_cache.h_separation;
  1847. }
  1848. p_item->cells.write[i].text_buf->set_width(text_width);
  1849. r_self_height = compute_item_height(p_item);
  1850. label_h = r_self_height + theme_cache.v_separation;
  1851. if (p_pos.y + label_h - theme_cache.offset.y < 0) {
  1852. continue; // No need to draw.
  1853. }
  1854. Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - theme_cache.offset + p_draw_ofs, Size2i(item_width, label_h));
  1855. Rect2i cell_rect = item_rect;
  1856. if (i != 0) {
  1857. cell_rect.position.x -= theme_cache.h_separation;
  1858. cell_rect.size.x += theme_cache.h_separation;
  1859. }
  1860. if (theme_cache.draw_guides) {
  1861. Rect2 r = cell_rect;
  1862. if (rtl) {
  1863. r.position.x = get_size().width - r.position.x - r.size.x;
  1864. }
  1865. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2i(r.position.x, r.position.y + r.size.height), r.position + r.size, theme_cache.guide_color, 1);
  1866. }
  1867. if (i == 0 && select_mode == SELECT_ROW) {
  1868. if (p_item->cells[0].selected || is_row_hovered) {
  1869. const Rect2 content_rect = _get_content_rect();
  1870. Rect2i row_rect = Rect2i(Point2i(content_rect.position.x, item_rect.position.y), Size2i(content_rect.size.x, item_rect.size.y));
  1871. if (rtl) {
  1872. row_rect.position.x = get_size().width - row_rect.position.x - row_rect.size.x;
  1873. }
  1874. if (p_item->cells[0].selected) {
  1875. if (is_row_hovered) {
  1876. if (has_focus()) {
  1877. theme_cache.hovered_selected_focus->draw(ci, row_rect);
  1878. } else {
  1879. theme_cache.hovered_selected->draw(ci, row_rect);
  1880. }
  1881. } else {
  1882. if (has_focus()) {
  1883. theme_cache.selected_focus->draw(ci, row_rect);
  1884. } else {
  1885. theme_cache.selected->draw(ci, row_rect);
  1886. }
  1887. }
  1888. } else if (!drop_mode_flags) {
  1889. if (is_cell_button_hovered) {
  1890. theme_cache.hovered_dimmed->draw(ci, row_rect);
  1891. } else {
  1892. theme_cache.hovered->draw(ci, row_rect);
  1893. }
  1894. }
  1895. }
  1896. }
  1897. if (select_mode != SELECT_ROW) {
  1898. Rect2i r = cell_rect;
  1899. if (rtl) {
  1900. r.position.x = get_size().width - r.position.x - r.size.x;
  1901. }
  1902. // Cell hover.
  1903. if (is_cell_hovered && !p_item->cells[i].selected && !drop_mode_flags) {
  1904. if (is_cell_button_hovered) {
  1905. theme_cache.hovered_dimmed->draw(ci, r);
  1906. } else {
  1907. theme_cache.hovered->draw(ci, r);
  1908. }
  1909. }
  1910. }
  1911. if ((select_mode == SELECT_ROW && selected_item == p_item) || p_item->cells[i].selected || !p_item->has_meta("__focus_rect")) {
  1912. Rect2i r = cell_rect;
  1913. if (select_mode != SELECT_ROW) {
  1914. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  1915. if (rtl) {
  1916. r.position.x = get_size().width - r.position.x - r.size.x;
  1917. }
  1918. if (p_item->cells[i].selected) {
  1919. if (is_cell_hovered) {
  1920. if (has_focus()) {
  1921. theme_cache.hovered_selected_focus->draw(ci, r);
  1922. } else {
  1923. theme_cache.hovered_selected->draw(ci, r);
  1924. }
  1925. } else {
  1926. if (has_focus()) {
  1927. theme_cache.selected_focus->draw(ci, r);
  1928. } else {
  1929. theme_cache.selected->draw(ci, r);
  1930. }
  1931. }
  1932. }
  1933. } else {
  1934. p_item->set_meta("__focus_col_" + itos(i), Rect2(r.position, r.size));
  1935. }
  1936. }
  1937. if (p_item->cells[i].custom_bg_color) {
  1938. Rect2 r = cell_rect;
  1939. if (i == 0) {
  1940. r.position.x = p_draw_ofs.x;
  1941. r.size.x = item_width + ofs;
  1942. }
  1943. if (rtl) {
  1944. r.position.x = get_size().width - r.position.x - r.size.x;
  1945. }
  1946. if (p_item->cells[i].custom_bg_outline) {
  1947. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), p_item->cells[i].bg_color);
  1948. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y - 1, r.size.x, 1), p_item->cells[i].bg_color);
  1949. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
  1950. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
  1951. } else {
  1952. RenderingServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  1953. }
  1954. }
  1955. if (drop_mode_flags && drop_mode_over) {
  1956. Rect2 r = cell_rect;
  1957. if (rtl) {
  1958. r.position.x = get_size().width - r.position.x - r.size.x;
  1959. }
  1960. if (drop_mode_over == p_item) {
  1961. if (drop_mode_section == 0 || drop_mode_section == -1) {
  1962. // Line above.
  1963. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
  1964. }
  1965. if (drop_mode_section == 0) {
  1966. // Side lines.
  1967. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), theme_cache.drop_position_color);
  1968. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x + r.size.x - 1, r.position.y, 1, r.size.y), theme_cache.drop_position_color);
  1969. }
  1970. if (drop_mode_section == 0 || (drop_mode_section == 1 && (!p_item->get_first_child() || p_item->is_collapsed()))) {
  1971. // Line below.
  1972. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y + r.size.y, r.size.x, 1), theme_cache.drop_position_color);
  1973. }
  1974. } else if (drop_mode_over == p_item->get_parent()) {
  1975. if (drop_mode_section == 1 && !p_item->get_prev()) {
  1976. // Line above.
  1977. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
  1978. }
  1979. }
  1980. }
  1981. Color cell_color;
  1982. if (p_item->cells[i].custom_color) {
  1983. cell_color = p_item->cells[i].color;
  1984. } else {
  1985. bool draw_as_hover = !drop_mode_flags && (select_mode == SELECT_ROW ? is_row_hovered : is_cell_hovered);
  1986. bool draw_as_hover_dim = draw_as_hover && is_cell_button_hovered;
  1987. cell_color = p_item->cells[i].selected && draw_as_hover ? theme_cache.font_hovered_selected_color : (p_item->cells[i].selected ? theme_cache.font_selected_color : (draw_as_hover_dim ? theme_cache.font_hovered_dimmed_color : (draw_as_hover ? theme_cache.font_hovered_color : theme_cache.font_color)));
  1988. }
  1989. Color font_outline_color = theme_cache.font_outline_color;
  1990. int outline_size = theme_cache.font_outline_size;
  1991. Color icon_col = p_item->cells[i].icon_color;
  1992. if (p_item->cells[i].dirty) {
  1993. update_item_cell(p_item, i);
  1994. }
  1995. if (rtl) {
  1996. item_rect.position.x = get_size().width - item_rect.position.x - item_rect.size.x;
  1997. }
  1998. Point2i text_pos = item_rect.position;
  1999. text_pos.y += Math::floor(p_draw_ofs.y) - _get_title_button_height();
  2000. switch (p_item->cells[i].mode) {
  2001. case TreeItem::CELL_MODE_STRING: {
  2002. draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  2003. } break;
  2004. case TreeItem::CELL_MODE_CHECK: {
  2005. Point2i check_ofs = item_rect.position;
  2006. check_ofs.y += Math::floor((real_t)(item_rect.size.y - theme_cache.checked->get_height()) / 2);
  2007. if (p_item->cells[i].editable) {
  2008. if (p_item->cells[i].indeterminate) {
  2009. theme_cache.indeterminate->draw(ci, check_ofs);
  2010. } else if (p_item->cells[i].checked) {
  2011. theme_cache.checked->draw(ci, check_ofs);
  2012. } else {
  2013. theme_cache.unchecked->draw(ci, check_ofs);
  2014. }
  2015. } else {
  2016. if (p_item->cells[i].indeterminate) {
  2017. theme_cache.indeterminate_disabled->draw(ci, check_ofs);
  2018. } else if (p_item->cells[i].checked) {
  2019. theme_cache.checked_disabled->draw(ci, check_ofs);
  2020. } else {
  2021. theme_cache.unchecked_disabled->draw(ci, check_ofs);
  2022. }
  2023. }
  2024. int check_w = theme_cache.checked->get_width() + theme_cache.h_separation;
  2025. text_pos.x += check_w;
  2026. item_rect.size.x -= check_w;
  2027. item_rect.position.x += check_w;
  2028. if (!p_item->cells[i].editable) {
  2029. cell_color = theme_cache.font_disabled_color;
  2030. }
  2031. draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  2032. } break;
  2033. case TreeItem::CELL_MODE_RANGE: {
  2034. if (!p_item->cells[i].text.is_empty()) {
  2035. if (!p_item->cells[i].editable) {
  2036. break;
  2037. }
  2038. Ref<Texture2D> downarrow = theme_cache.select_arrow;
  2039. int cell_width = item_rect.size.x - downarrow->get_width();
  2040. if (rtl) {
  2041. if (outline_size > 0 && font_outline_color.a > 0) {
  2042. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  2043. }
  2044. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
  2045. } else {
  2046. if (outline_size > 0 && font_outline_color.a > 0) {
  2047. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  2048. }
  2049. p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
  2050. }
  2051. Point2i arrow_pos = item_rect.position;
  2052. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  2053. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  2054. downarrow->draw(ci, arrow_pos);
  2055. } else {
  2056. Ref<Texture2D> updown = theme_cache.updown;
  2057. int cell_width = item_rect.size.x - updown->get_width();
  2058. if (rtl) {
  2059. if (outline_size > 0 && font_outline_color.a > 0) {
  2060. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  2061. }
  2062. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
  2063. } else {
  2064. if (outline_size > 0 && font_outline_color.a > 0) {
  2065. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  2066. }
  2067. p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
  2068. }
  2069. if (!p_item->cells[i].editable) {
  2070. break;
  2071. }
  2072. Point2i updown_pos = item_rect.position;
  2073. updown_pos.x += item_rect.size.x - updown->get_width();
  2074. updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
  2075. updown->draw(ci, updown_pos);
  2076. }
  2077. } break;
  2078. case TreeItem::CELL_MODE_ICON: {
  2079. if (p_item->cells[i].icon.is_null()) {
  2080. break;
  2081. }
  2082. Size2i icon_size = _get_cell_icon_size(p_item->cells[i]);
  2083. Point2i icon_ofs = (item_rect.size - icon_size) / 2;
  2084. icon_ofs += item_rect.position;
  2085. draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size), false, icon_col);
  2086. } break;
  2087. case TreeItem::CELL_MODE_CUSTOM: {
  2088. if (p_item->cells[i].custom_draw_callback.is_valid()) {
  2089. Variant args[] = { p_item, Rect2(item_rect) };
  2090. const Variant *argptrs[] = { &args[0], &args[1] };
  2091. Callable::CallError ce;
  2092. Variant ret;
  2093. p_item->cells[i].custom_draw_callback.callp(argptrs, 2, ret, ce);
  2094. if (ce.error != Callable::CallError::CALL_OK) {
  2095. ERR_PRINT("Error calling custom draw method: " + Variant::get_callable_error_text(p_item->cells[i].custom_draw_callback, argptrs, 2, ce) + ".");
  2096. }
  2097. }
  2098. if (!p_item->cells[i].editable) {
  2099. draw_item_rect(p_item->cells.write[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  2100. break;
  2101. }
  2102. Ref<Texture2D> downarrow = theme_cache.select_arrow;
  2103. Rect2i ir = item_rect;
  2104. Point2i arrow_pos = item_rect.position;
  2105. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  2106. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  2107. ir.size.width -= downarrow->get_width();
  2108. if (p_item->cells[i].custom_button) {
  2109. if (cache.hover_item == p_item && cache.hover_column == i) {
  2110. if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  2111. draw_style_box(theme_cache.custom_button_pressed, ir);
  2112. } else {
  2113. draw_style_box(theme_cache.custom_button_hover, ir);
  2114. cell_color = theme_cache.custom_button_font_highlight;
  2115. }
  2116. } else {
  2117. draw_style_box(theme_cache.custom_button, ir);
  2118. }
  2119. ir.size -= theme_cache.custom_button->get_minimum_size();
  2120. ir.position += theme_cache.custom_button->get_offset();
  2121. }
  2122. draw_item_rect(p_item->cells.write[i], ir, cell_color, icon_col, outline_size, font_outline_color);
  2123. downarrow->draw(ci, arrow_pos);
  2124. } break;
  2125. }
  2126. // Draw the buttons inside the cell.
  2127. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  2128. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  2129. Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
  2130. Point2i button_ofs = Point2i(ofs + item_width_with_buttons - button_size.width, p_pos.y) - theme_cache.offset + p_draw_ofs;
  2131. bool should_draw_pressed = cache.click_type == Cache::CLICK_BUTTON && cache.click_item == p_item && cache.click_column == i && cache.click_index == j && !p_item->cells[i].buttons[j].disabled;
  2132. bool should_draw_hovered = !drop_mode_flags && cache.hover_item == p_item && cache.hover_column == i && cache.hover_button_index_in_column == j && !p_item->cells[i].buttons[j].disabled;
  2133. if (should_draw_pressed || should_draw_hovered) {
  2134. Point2 od = button_ofs;
  2135. if (rtl) {
  2136. od.x = get_size().width - od.x - button_size.x;
  2137. }
  2138. if (should_draw_pressed && should_draw_hovered) {
  2139. theme_cache.button_pressed->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
  2140. } else {
  2141. theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
  2142. }
  2143. }
  2144. if (selected_item == p_item && selected_col == i && selected_button == j) {
  2145. Point2 od = button_ofs;
  2146. if (rtl) {
  2147. od.x = get_size().width - od.x - button_size.x;
  2148. }
  2149. theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
  2150. }
  2151. button_ofs.y += (label_h - button_size.height) / 2;
  2152. button_ofs += theme_cache.button_pressed->get_offset();
  2153. if (rtl) {
  2154. button_ofs.x = get_size().width - button_ofs.x - button_texture->get_width();
  2155. }
  2156. button_texture->draw(ci, button_ofs, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
  2157. item_width_with_buttons -= button_size.width + theme_cache.button_margin;
  2158. }
  2159. if (i == 0) {
  2160. ofs = get_column_width(0);
  2161. } else {
  2162. ofs += item_width + buttons_width;
  2163. }
  2164. if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
  2165. if (is_layout_rtl()) {
  2166. cell_rect.position.x = get_size().width - cell_rect.position.x - cell_rect.size.x;
  2167. }
  2168. if (has_focus()) {
  2169. theme_cache.cursor->draw(ci, cell_rect);
  2170. } else {
  2171. theme_cache.cursor_unfocus->draw(ci, cell_rect);
  2172. }
  2173. }
  2174. }
  2175. // Draw the folding arrow.
  2176. if (!p_item->disable_folding && !hide_folding && p_item->first_child && p_item->get_visible_child_count() != 0) { // Has visible children, draw the guide box.
  2177. Ref<Texture2D> arrow;
  2178. if (p_item->collapsed) {
  2179. if (is_layout_rtl()) {
  2180. arrow = theme_cache.arrow_collapsed_mirrored;
  2181. } else {
  2182. arrow = theme_cache.arrow_collapsed;
  2183. }
  2184. } else {
  2185. arrow = theme_cache.arrow;
  2186. }
  2187. Size2 arrow_full_size = arrow->get_size();
  2188. Point2 apos = p_pos + Point2i(0, (label_h - arrow_full_size.height) / 2) - theme_cache.offset + p_draw_ofs;
  2189. apos.x += theme_cache.item_margin - arrow_full_size.width;
  2190. Size2 arrow_draw_size = arrow_full_size;
  2191. int out_width = p_pos.x + theme_cache.item_margin - get_column_width(0);
  2192. if (out_width > 0) {
  2193. arrow_draw_size.width -= out_width;
  2194. }
  2195. if (arrow_draw_size.width > 0) {
  2196. Point2 src_pos = Point2();
  2197. if (rtl) {
  2198. apos.x = get_size().width - apos.x - arrow_draw_size.width;
  2199. src_pos = Point2(arrow_full_size.width - arrow_draw_size.width, 0);
  2200. }
  2201. Rect2 arrow_rect = Rect2(apos, arrow_draw_size);
  2202. Rect2 arrow_src_rect = Rect2(src_pos, arrow_draw_size);
  2203. arrow->draw_rect_region(ci, arrow_rect, arrow_src_rect);
  2204. }
  2205. }
  2206. }
  2207. Point2 children_pos = p_pos;
  2208. if (!skip) {
  2209. children_pos.x += theme_cache.item_margin;
  2210. htotal += label_h;
  2211. children_pos.y += htotal;
  2212. }
  2213. if (!p_item->collapsed) { // If not collapsed, check the children.
  2214. TreeItem *c = p_item->first_child;
  2215. int base_ofs = children_pos.y - theme_cache.offset.y + p_draw_ofs.y;
  2216. float prev_ofs = base_ofs;
  2217. float prev_hl_ofs = base_ofs;
  2218. while (c) {
  2219. int child_h = -1;
  2220. int child_self_height = 0;
  2221. if (htotal >= 0) {
  2222. child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c, child_self_height);
  2223. child_self_height += theme_cache.v_separation;
  2224. }
  2225. // Draw relationship lines.
  2226. if (theme_cache.draw_relationship_lines > 0 && (!hide_root || c->parent != root) && c->is_visible_in_tree()) {
  2227. int parent_ofs = p_pos.x + theme_cache.item_margin;
  2228. Point2i parent_pos = Point2i(parent_ofs - theme_cache.arrow->get_width() * 0.5, p_pos.y + label_h * 0.5 + theme_cache.arrow->get_height() * 0.5) - theme_cache.offset + p_draw_ofs;
  2229. int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin);
  2230. Point2i root_pos = Point2i(root_ofs, children_pos.y + child_self_height * 0.5) - theme_cache.offset + p_draw_ofs;
  2231. if (!hide_folding && !p_item->disable_folding && c->get_visible_child_count() > 0) {
  2232. root_pos -= Point2i(theme_cache.arrow->get_width(), 0);
  2233. }
  2234. root_pos.x = MIN(root_pos.x, get_column_width(0) + p_draw_ofs.x);
  2235. bool is_no_space = root_pos.x <= parent_pos.x;
  2236. float line_width = theme_cache.relationship_line_width * Math::round(theme_cache.base_scale);
  2237. float parent_line_width = theme_cache.parent_hl_line_width * Math::round(theme_cache.base_scale);
  2238. float children_line_width = theme_cache.children_hl_line_width * Math::round(theme_cache.base_scale);
  2239. float line_pixel_shift = int(Math::round(line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
  2240. float parent_line_pixel_shift = int(Math::round(parent_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
  2241. float children_line_pixel_shift = int(Math::round(children_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
  2242. int more_prev_ofs = 0;
  2243. if (root_pos.y + line_width >= 0) {
  2244. if (rtl) {
  2245. root_pos.x = get_size().width - root_pos.x;
  2246. parent_pos.x = get_size().width - parent_pos.x;
  2247. }
  2248. float parent_bottom_y = root_pos.y + parent_line_width * 0.5 + parent_line_pixel_shift;
  2249. // Order of parts on this bend: the horizontal line first, then the vertical line.
  2250. if (_is_branch_selected(c)) {
  2251. // If this item or one of its children is selected, we draw the line using parent highlight style.
  2252. if (!is_no_space) {
  2253. if (htotal >= 0) {
  2254. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + parent_line_pixel_shift), Point2(parent_pos.x + parent_line_width * 0.5 + parent_line_pixel_shift, root_pos.y + parent_line_pixel_shift), theme_cache.parent_hl_line_color, parent_line_width);
  2255. }
  2256. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
  2257. }
  2258. more_prev_ofs = theme_cache.parent_hl_line_margin;
  2259. prev_hl_ofs = parent_bottom_y;
  2260. } else if (p_item->is_selected(0)) {
  2261. // If parent item is selected (but this item is not), we draw the line using children highlight style.
  2262. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  2263. if (_is_sibling_branch_selected(c)) {
  2264. if (!is_no_space) {
  2265. if (htotal >= 0) {
  2266. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2i(parent_pos.x + parent_line_width * 0.5 + children_line_pixel_shift, root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
  2267. }
  2268. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
  2269. }
  2270. prev_hl_ofs = parent_bottom_y;
  2271. } else {
  2272. if (!is_no_space) {
  2273. if (htotal >= 0) {
  2274. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2(parent_pos.x + children_line_width * 0.5 + children_line_pixel_shift, root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
  2275. }
  2276. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + children_line_pixel_shift, root_pos.y + children_line_width * 0.5 + children_line_pixel_shift), Point2(parent_pos.x + children_line_pixel_shift, prev_ofs + children_line_width * 0.5), theme_cache.children_hl_line_color, children_line_width);
  2277. }
  2278. }
  2279. } else {
  2280. // If nothing of the above is true, we draw the line using normal style.
  2281. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  2282. if (_is_sibling_branch_selected(c)) {
  2283. if (!is_no_space) {
  2284. if (htotal >= 0) {
  2285. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + theme_cache.parent_hl_line_margin, root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
  2286. }
  2287. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
  2288. }
  2289. prev_hl_ofs = parent_bottom_y;
  2290. } else {
  2291. if (!is_no_space) {
  2292. if (htotal >= 0) {
  2293. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + line_width * 0.5 + line_pixel_shift, root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
  2294. }
  2295. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + line_pixel_shift, root_pos.y + line_width * 0.5 + line_pixel_shift), Point2(parent_pos.x + line_pixel_shift, prev_ofs + line_width * 0.5), theme_cache.relationship_line_color, line_width);
  2296. }
  2297. }
  2298. }
  2299. }
  2300. prev_ofs = root_pos.y + more_prev_ofs + line_pixel_shift;
  2301. }
  2302. if (child_h < 0) {
  2303. if (htotal == -1) {
  2304. break; // Last loop done, stop.
  2305. }
  2306. if (theme_cache.draw_relationship_lines == 0) {
  2307. return -1; // No need to draw anymore, full stop.
  2308. }
  2309. htotal = -1;
  2310. children_pos.y = theme_cache.offset.y + p_draw_size.height;
  2311. } else {
  2312. htotal += child_h;
  2313. children_pos.y += child_h;
  2314. }
  2315. c = c->next;
  2316. }
  2317. }
  2318. return htotal;
  2319. }
  2320. int Tree::_count_selected_items(TreeItem *p_from) const {
  2321. int count = 0;
  2322. for (int i = 0; i < columns.size(); i++) {
  2323. if (p_from->is_selected(i)) {
  2324. count++;
  2325. }
  2326. }
  2327. for (TreeItem *c = p_from->get_first_child(); c; c = c->get_next()) {
  2328. count += _count_selected_items(c);
  2329. }
  2330. return count;
  2331. }
  2332. bool Tree::_is_branch_selected(TreeItem *p_from) const {
  2333. for (int i = 0; i < columns.size(); i++) {
  2334. if (p_from->is_selected(i)) {
  2335. return true;
  2336. }
  2337. }
  2338. TreeItem *child_item = p_from->get_first_child();
  2339. while (child_item) {
  2340. if (_is_branch_selected(child_item)) {
  2341. return true;
  2342. }
  2343. child_item = child_item->get_next();
  2344. }
  2345. return false;
  2346. }
  2347. bool Tree::_is_sibling_branch_selected(TreeItem *p_from) const {
  2348. TreeItem *sibling_item = p_from->get_next();
  2349. while (sibling_item) {
  2350. if (_is_branch_selected(sibling_item)) {
  2351. return true;
  2352. }
  2353. sibling_item = sibling_item->get_next();
  2354. }
  2355. return false;
  2356. }
  2357. void Tree::select_single_item(TreeItem *p_selected, TreeItem *p_current, int p_col, TreeItem *p_prev, bool *r_in_range, bool p_force_deselect) {
  2358. popup_editor->hide();
  2359. TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
  2360. bool switched = false;
  2361. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  2362. *r_in_range = true;
  2363. switched = true;
  2364. }
  2365. bool emitted_row = false;
  2366. for (int i = 0; i < columns.size(); i++) {
  2367. TreeItem::Cell &c = p_current->cells.write[i];
  2368. if (!c.selectable) {
  2369. continue;
  2370. }
  2371. if (select_mode == SELECT_ROW) {
  2372. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  2373. c.selected = true;
  2374. selected_item = p_selected;
  2375. if (!emitted_row) {
  2376. emit_signal(SceneStringName(item_selected));
  2377. emitted_row = true;
  2378. }
  2379. } else if (c.selected) {
  2380. if (p_selected != p_current) {
  2381. // Deselect other rows.
  2382. c.selected = false;
  2383. }
  2384. }
  2385. if (&selected_cell == &c) {
  2386. selected_col = i;
  2387. }
  2388. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  2389. if (!r_in_range && &selected_cell == &c) {
  2390. if (!selected_cell.selected || allow_reselect) {
  2391. selected_cell.selected = true;
  2392. selected_item = p_selected;
  2393. selected_col = i;
  2394. selected_button = -1;
  2395. emit_signal(SNAME("cell_selected"));
  2396. if (select_mode == SELECT_MULTI) {
  2397. emit_signal(SNAME("multi_selected"), p_current, i, true);
  2398. } else if (select_mode == SELECT_SINGLE) {
  2399. emit_signal(SceneStringName(item_selected));
  2400. }
  2401. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  2402. selected_item = p_selected;
  2403. selected_col = i;
  2404. selected_button = -1;
  2405. emit_signal(SNAME("cell_selected"));
  2406. }
  2407. } else {
  2408. if (r_in_range && *r_in_range && !p_force_deselect) {
  2409. if (!c.selected && c.selectable) {
  2410. c.selected = true;
  2411. emit_signal(SNAME("multi_selected"), p_current, i, true);
  2412. }
  2413. } else if (!r_in_range || p_force_deselect) {
  2414. if (select_mode == SELECT_MULTI && c.selected) {
  2415. emit_signal(SNAME("multi_selected"), p_current, i, false);
  2416. }
  2417. c.selected = false;
  2418. }
  2419. }
  2420. }
  2421. }
  2422. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  2423. *r_in_range = false;
  2424. }
  2425. TreeItem *c = p_current->first_child;
  2426. while (c) {
  2427. if (c->is_visible()) {
  2428. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  2429. }
  2430. c = c->next;
  2431. }
  2432. queue_accessibility_update();
  2433. }
  2434. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  2435. return Rect2();
  2436. }
  2437. void Tree::_range_click_timeout() {
  2438. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  2439. Point2 pos = get_local_mouse_position() - theme_cache.panel_style->get_offset();
  2440. if (show_column_titles) {
  2441. pos.y -= _get_title_button_height();
  2442. if (pos.y < 0) {
  2443. range_click_timer->stop();
  2444. return;
  2445. }
  2446. }
  2447. if (!root) {
  2448. return;
  2449. }
  2450. click_handled = false;
  2451. Ref<InputEventMouseButton> mb;
  2452. mb.instantiate();
  2453. int x_limit = _get_content_rect().size.x;
  2454. cache.rtl = is_layout_rtl();
  2455. propagate_mouse_activated = false; // Done from outside, so signal handler can't clear the tree in the middle of emit (which is a common case).
  2456. blocked++;
  2457. propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, false, root, MouseButton::LEFT, mb);
  2458. blocked--;
  2459. if (range_click_timer->is_one_shot()) {
  2460. range_click_timer->set_wait_time(0.05);
  2461. range_click_timer->set_one_shot(false);
  2462. range_click_timer->start();
  2463. }
  2464. if (!click_handled) {
  2465. range_click_timer->stop();
  2466. }
  2467. if (propagate_mouse_activated) {
  2468. emit_signal(SNAME("item_activated"));
  2469. propagate_mouse_activated = false;
  2470. }
  2471. } else {
  2472. range_click_timer->stop();
  2473. }
  2474. }
  2475. int Tree::propagate_mouse_event(const Point2i &p_pos, int x_ofs, int y_ofs, int x_limit, bool p_double_click, TreeItem *p_item, MouseButton p_button, const Ref<InputEventWithModifiers> &p_mod) {
  2476. if (p_item && !p_item->is_visible_in_tree()) {
  2477. // Skip any processing of invisible items.
  2478. return 0;
  2479. }
  2480. if (p_pos.x > x_limit) {
  2481. // Inside the scroll area.
  2482. return -1;
  2483. }
  2484. int item_h = compute_item_height(p_item) + theme_cache.v_separation;
  2485. bool skip = (p_item == root && hide_root);
  2486. if (!skip && p_pos.y < item_h) {
  2487. // Check event!
  2488. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  2489. return -1;
  2490. }
  2491. if (!p_item->disable_folding && !hide_folding && p_item->first_child && (p_pos.x < (x_ofs + theme_cache.item_margin))) {
  2492. if (enable_recursive_folding && p_mod->is_shift_pressed()) {
  2493. p_item->set_collapsed_recursive(!p_item->is_collapsed());
  2494. } else {
  2495. p_item->set_collapsed(!p_item->is_collapsed());
  2496. }
  2497. return -1;
  2498. }
  2499. FindColumnButtonResult find_result = _find_column_and_button_at_pos(p_pos.x, p_item, x_ofs, x_limit);
  2500. if (find_result.column_index == -1) {
  2501. return -1;
  2502. }
  2503. int col = find_result.column_index;
  2504. int col_width = find_result.column_width;
  2505. int col_ofs = find_result.column_offset;
  2506. int x = find_result.pos_x;
  2507. const TreeItem::Cell &c = p_item->cells[col];
  2508. if (find_result.button_index >= 0) {
  2509. if (c.buttons[find_result.button_index].disabled) {
  2510. pressed_button = -1;
  2511. cache.click_type = Cache::CLICK_NONE;
  2512. return -1;
  2513. }
  2514. // Make sure the click is correct.
  2515. const Point2 click_pos = get_local_mouse_position();
  2516. if (!get_item_at_position(click_pos)) {
  2517. pressed_button = -1;
  2518. cache.click_type = Cache::CLICK_NONE;
  2519. return -1;
  2520. }
  2521. pressed_button = find_result.button_index;
  2522. cache.click_type = Cache::CLICK_BUTTON;
  2523. cache.click_index = find_result.button_index;
  2524. cache.click_id = c.buttons[find_result.button_index].id;
  2525. cache.click_item = p_item;
  2526. cache.click_column = col;
  2527. cache.click_pos = click_pos;
  2528. queue_redraw();
  2529. return -1;
  2530. }
  2531. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_first_child()) {
  2532. if (enable_recursive_folding && p_mod->is_shift_pressed()) {
  2533. p_item->set_collapsed_recursive(!p_item->is_collapsed());
  2534. } else {
  2535. p_item->set_collapsed(!p_item->is_collapsed());
  2536. }
  2537. return -1; // Collapse/uncollapse, because nothing can be done with the item.
  2538. }
  2539. bool already_selected = c.selected;
  2540. bool already_cursor = (p_item == selected_item) && col == selected_col;
  2541. if (p_button == MouseButton::LEFT || (p_button == MouseButton::RIGHT && allow_rmb_select)) {
  2542. // Process selection.
  2543. if (p_double_click && (!c.editable || c.mode == TreeItem::CELL_MODE_CUSTOM || c.mode == TreeItem::CELL_MODE_ICON)) {
  2544. // Emits the `item_activated` signal.
  2545. propagate_mouse_activated = true;
  2546. incr_search.clear();
  2547. return -1;
  2548. }
  2549. if (c.selectable) {
  2550. if (select_mode == SELECT_MULTI && p_mod->is_command_or_control_pressed()) {
  2551. if (c.selected && p_button == MouseButton::LEFT) {
  2552. p_item->deselect(col);
  2553. emit_signal(SNAME("multi_selected"), p_item, col, false);
  2554. } else {
  2555. p_item->select(col);
  2556. emit_signal(SNAME("multi_selected"), p_item, col, true);
  2557. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2558. }
  2559. } else {
  2560. if (select_mode == SELECT_MULTI && p_mod->is_shift_pressed() && selected_item && selected_item != p_item) {
  2561. bool inrange = false;
  2562. select_single_item(p_item, root, col, selected_item, &inrange);
  2563. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2564. } else {
  2565. int icount = _count_selected_items(root);
  2566. if (select_mode == SELECT_MULTI && icount > 1 && p_button != MouseButton::RIGHT) {
  2567. if (!already_selected) {
  2568. select_single_item(p_item, root, col);
  2569. }
  2570. single_select_defer = p_item;
  2571. single_select_defer_column = col;
  2572. } else {
  2573. if (p_button != MouseButton::RIGHT || !c.selected) {
  2574. select_single_item(p_item, root, col);
  2575. }
  2576. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2577. }
  2578. }
  2579. queue_accessibility_update();
  2580. queue_redraw();
  2581. }
  2582. }
  2583. }
  2584. if (!c.editable) {
  2585. return -1; // If cell is not editable, don't bother.
  2586. }
  2587. // Editing.
  2588. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  2589. String editor_text = c.text;
  2590. switch (c.mode) {
  2591. case TreeItem::CELL_MODE_STRING: {
  2592. // Nothing in particular.
  2593. if (select_mode == SELECT_MULTI && (get_viewport()->get_processed_events_count() == focus_in_id || !already_cursor)) {
  2594. bring_up_editor = false;
  2595. }
  2596. } break;
  2597. case TreeItem::CELL_MODE_CHECK: {
  2598. bring_up_editor = false; // Checkboxes are not edited with editor.
  2599. if (force_edit_checkbox_only_on_checkbox) {
  2600. if (x < theme_cache.checked->get_width()) {
  2601. p_item->set_checked(col, !c.checked);
  2602. item_edited(col, p_item, p_button);
  2603. }
  2604. } else {
  2605. p_item->set_checked(col, !c.checked);
  2606. item_edited(col, p_item, p_button);
  2607. }
  2608. click_handled = true;
  2609. } break;
  2610. case TreeItem::CELL_MODE_RANGE: {
  2611. if (!c.text.is_empty()) {
  2612. _update_popup_menu(c);
  2613. popup_menu->set_size(Size2(col_width, 0));
  2614. popup_menu->set_position(get_screen_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - theme_cache.offset);
  2615. popup_menu->popup();
  2616. popup_edited_item = p_item;
  2617. popup_edited_item_col = col;
  2618. bring_up_editor = false;
  2619. } else {
  2620. if (x >= (col_width - item_h / 2)) {
  2621. // Touching the combo.
  2622. bool up = p_pos.y < (item_h / 2);
  2623. if (p_button == MouseButton::LEFT) {
  2624. if (range_click_timer->get_time_left() == 0) {
  2625. range_item_last = p_item;
  2626. range_up_last = up;
  2627. range_click_timer->set_wait_time(0.6);
  2628. range_click_timer->set_one_shot(true);
  2629. range_click_timer->start();
  2630. } else if (up != range_up_last) {
  2631. return -1; // Break. Avoid changing direction on mouse held.
  2632. }
  2633. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  2634. item_edited(col, p_item, p_button);
  2635. } else if (p_button == MouseButton::RIGHT) {
  2636. p_item->set_range(col, (up ? c.max : c.min));
  2637. item_edited(col, p_item, p_button);
  2638. } else if (p_button == MouseButton::WHEEL_UP) {
  2639. p_item->set_range(col, c.val + c.step);
  2640. item_edited(col, p_item, p_button);
  2641. } else if (p_button == MouseButton::WHEEL_DOWN) {
  2642. p_item->set_range(col, c.val - c.step);
  2643. item_edited(col, p_item, p_button);
  2644. }
  2645. bring_up_editor = false;
  2646. } else {
  2647. editor_text = String::num(p_item->cells[col].val, Math::range_step_decimals(p_item->cells[col].step));
  2648. if (select_mode == SELECT_MULTI && get_viewport()->get_processed_events_count() == focus_in_id) {
  2649. bring_up_editor = false;
  2650. }
  2651. }
  2652. }
  2653. click_handled = true;
  2654. } break;
  2655. case TreeItem::CELL_MODE_ICON: {
  2656. bring_up_editor = false;
  2657. } break;
  2658. case TreeItem::CELL_MODE_CUSTOM: {
  2659. edited_item = p_item;
  2660. edited_col = col;
  2661. bool on_arrow = x > col_width - theme_cache.select_arrow->get_width();
  2662. custom_popup_rect = Rect2i(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h - theme_cache.offset.y), Size2(get_column_width(col), item_h));
  2663. if (on_arrow || !p_item->cells[col].custom_button) {
  2664. emit_signal(SNAME("custom_popup_edited"), ((bool)(x >= (col_width - item_h / 2))));
  2665. }
  2666. if (!p_item->cells[col].custom_button || !on_arrow) {
  2667. item_edited(col, p_item, p_button);
  2668. }
  2669. click_handled = true;
  2670. return -1;
  2671. } break;
  2672. };
  2673. if (!bring_up_editor || p_button != MouseButton::LEFT) {
  2674. return -1;
  2675. }
  2676. click_handled = true;
  2677. popup_pressing_edited_item = p_item;
  2678. popup_pressing_edited_item_column = col;
  2679. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - theme_cache.offset, Size2(col_width, item_h));
  2680. pressing_for_editor_text = editor_text;
  2681. pressing_for_editor = true;
  2682. return -1; // Select.
  2683. } else {
  2684. Point2i new_pos = p_pos;
  2685. if (!skip) {
  2686. x_ofs += theme_cache.item_margin;
  2687. y_ofs += item_h;
  2688. new_pos.y -= item_h;
  2689. }
  2690. if (!p_item->collapsed) { // If not collapsed, check the children.
  2691. TreeItem *c = p_item->first_child;
  2692. while (c) {
  2693. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, x_limit, p_double_click, c, p_button, p_mod);
  2694. if (child_h < 0) {
  2695. return -1; // Break, stop propagating, no need to anymore.
  2696. }
  2697. new_pos.y -= child_h;
  2698. y_ofs += child_h;
  2699. c = c->next;
  2700. item_h += child_h;
  2701. }
  2702. }
  2703. if (p_item == root) {
  2704. emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), p_button);
  2705. }
  2706. }
  2707. return item_h; // Nothing found.
  2708. }
  2709. void Tree::_text_editor_popup_modal_close() {
  2710. if (popup_edit_committed) {
  2711. return; // Already processed by `LineEdit` / `TextEdit` commit.
  2712. }
  2713. if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
  2714. return; // ESC pressed, app focus lost, or forced close from code.
  2715. }
  2716. if (value_editor && value_editor->has_point(value_editor->get_local_mouse_position())) {
  2717. return;
  2718. }
  2719. if (!popup_edited_item) {
  2720. return;
  2721. }
  2722. if (popup_edited_item->is_edit_multiline(popup_edited_item_col) && popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_STRING) {
  2723. _apply_multiline_edit();
  2724. } else {
  2725. _line_editor_submit(line_editor->get_text());
  2726. }
  2727. }
  2728. void Tree::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  2729. if (popup_edit_committed) {
  2730. return; // Already processed by `_text_editor_popup_modal_close`.
  2731. }
  2732. if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
  2733. return; // ESC pressed, app focus lost, or forced close from code.
  2734. }
  2735. if (p_event->is_action_pressed("ui_text_newline_blank", true)) {
  2736. accept_event();
  2737. } else if (p_event->is_action_pressed("ui_text_newline")) {
  2738. popup_edit_committed = true; // End edit popup processing.
  2739. popup_editor->hide();
  2740. _apply_multiline_edit();
  2741. accept_event();
  2742. }
  2743. }
  2744. void Tree::_apply_multiline_edit() {
  2745. if (!popup_edited_item) {
  2746. return;
  2747. }
  2748. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2749. return;
  2750. }
  2751. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2752. switch (c.mode) {
  2753. case TreeItem::CELL_MODE_STRING: {
  2754. c.text = text_editor->get_text();
  2755. } break;
  2756. default: {
  2757. ERR_FAIL();
  2758. }
  2759. }
  2760. item_edited(popup_edited_item_col, popup_edited_item);
  2761. queue_redraw();
  2762. }
  2763. void Tree::_line_editor_submit(String p_text) {
  2764. if (popup_edit_committed) {
  2765. return; // Already processed by _text_editor_popup_modal_close.
  2766. }
  2767. if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
  2768. return; // ESC pressed, app focus lost, or forced close from code.
  2769. }
  2770. popup_edit_committed = true; // End edit popup processing.
  2771. popup_editor->hide();
  2772. if (!popup_edited_item) {
  2773. return;
  2774. }
  2775. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2776. return;
  2777. }
  2778. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2779. switch (c.mode) {
  2780. case TreeItem::CELL_MODE_STRING: {
  2781. c.text = p_text;
  2782. } break;
  2783. case TreeItem::CELL_MODE_RANGE: {
  2784. c.val = p_text.to_float();
  2785. if (c.step > 0) {
  2786. c.val = Math::snapped(c.val, c.step);
  2787. }
  2788. if (c.val < c.min) {
  2789. c.val = c.min;
  2790. } else if (c.val > c.max) {
  2791. c.val = c.max;
  2792. }
  2793. } break;
  2794. default: {
  2795. ERR_FAIL();
  2796. }
  2797. }
  2798. item_edited(popup_edited_item_col, popup_edited_item);
  2799. queue_redraw();
  2800. }
  2801. void Tree::value_editor_changed(double p_value) {
  2802. if (updating_value_editor) {
  2803. return;
  2804. }
  2805. if (!popup_edited_item) {
  2806. return;
  2807. }
  2808. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  2809. line_editor->set_text(String::num(p_value, Math::range_step_decimals(c.step)));
  2810. queue_redraw();
  2811. }
  2812. void Tree::_update_popup_menu(const TreeItem::Cell &p_cell) {
  2813. if (popup_menu == nullptr) {
  2814. popup_menu = memnew(PopupMenu);
  2815. popup_menu->hide();
  2816. add_child(popup_menu, false, INTERNAL_MODE_FRONT);
  2817. popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &Tree::popup_select));
  2818. }
  2819. popup_menu->clear();
  2820. for (int i = 0; i < p_cell.text.get_slice_count(","); i++) {
  2821. String s = p_cell.text.get_slicec(',', i);
  2822. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).is_empty() ? i : s.get_slicec(':', 1).to_int());
  2823. }
  2824. }
  2825. void Tree::_update_value_editor(const TreeItem::Cell &p_cell) {
  2826. if (value_editor == nullptr) {
  2827. value_editor = memnew(HSlider);
  2828. value_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  2829. value_editor->hide();
  2830. popup_editor_vb->add_child(value_editor);
  2831. value_editor->connect(SceneStringName(value_changed), callable_mp(this, &Tree::value_editor_changed));
  2832. }
  2833. updating_value_editor = true;
  2834. value_editor->set_min(p_cell.min);
  2835. value_editor->set_max(p_cell.max);
  2836. value_editor->set_step(p_cell.step);
  2837. value_editor->set_value(p_cell.val);
  2838. value_editor->set_exp_ratio(p_cell.expr);
  2839. updating_value_editor = false;
  2840. }
  2841. void Tree::popup_select(int p_option) {
  2842. if (!popup_edited_item) {
  2843. return;
  2844. }
  2845. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2846. return;
  2847. }
  2848. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  2849. queue_redraw();
  2850. item_edited(popup_edited_item_col, popup_edited_item);
  2851. }
  2852. void Tree::_go_left() {
  2853. if (get_tree()->is_accessibility_enabled() && selected_button >= 0) {
  2854. selected_button--;
  2855. } else if (selected_col == 0) {
  2856. selected_button = -1;
  2857. if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
  2858. selected_item->set_collapsed(true);
  2859. } else {
  2860. if (columns.size() == 1) { // Goto parent with one column.
  2861. TreeItem *parent = selected_item->get_parent();
  2862. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  2863. select_single_item(parent, get_root(), selected_col);
  2864. }
  2865. } else if (selected_item->get_prev_visible()) {
  2866. selected_col = columns.size() - 1;
  2867. _go_up(); // Go to upper column if possible.
  2868. }
  2869. }
  2870. } else {
  2871. selected_button = -1;
  2872. if (select_mode == SELECT_MULTI) {
  2873. selected_col--;
  2874. emit_signal(SNAME("cell_selected"));
  2875. } else {
  2876. selected_item->select(selected_col - 1);
  2877. }
  2878. }
  2879. queue_accessibility_update();
  2880. queue_redraw();
  2881. accept_event();
  2882. ensure_cursor_is_visible();
  2883. }
  2884. void Tree::_go_right() {
  2885. int buttons = (selected_item && selected_col >= 0 && selected_col < columns.size()) ? selected_item->cells[selected_col].buttons.size() : 0;
  2886. if (get_tree()->is_accessibility_enabled() && selected_button < buttons - 1) {
  2887. selected_button++;
  2888. } else if (selected_col == (columns.size() - 1)) {
  2889. selected_button = -1;
  2890. if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
  2891. selected_item->set_collapsed(false);
  2892. } else if (selected_item->get_next_visible()) {
  2893. selected_col = 0;
  2894. _go_down();
  2895. }
  2896. } else {
  2897. selected_button = -1;
  2898. if (select_mode == SELECT_MULTI) {
  2899. selected_col++;
  2900. emit_signal(SNAME("cell_selected"));
  2901. } else {
  2902. selected_item->select(selected_col + 1);
  2903. }
  2904. }
  2905. queue_accessibility_update();
  2906. queue_redraw();
  2907. ensure_cursor_is_visible();
  2908. accept_event();
  2909. }
  2910. void Tree::_go_up() {
  2911. TreeItem *prev = nullptr;
  2912. if (!selected_item) {
  2913. prev = get_last_item();
  2914. selected_col = 0;
  2915. selected_button = -1;
  2916. } else {
  2917. prev = selected_item->get_prev_visible();
  2918. }
  2919. int col = MAX(selected_col, 0);
  2920. if (select_mode == SELECT_MULTI) {
  2921. if (!prev) {
  2922. return;
  2923. }
  2924. selected_item = prev;
  2925. emit_signal(SNAME("cell_selected"));
  2926. queue_redraw();
  2927. } else {
  2928. while (prev && !prev->cells[col].selectable) {
  2929. prev = prev->get_prev_visible();
  2930. }
  2931. if (!prev) {
  2932. return; // Do nothing.
  2933. }
  2934. prev->select(col);
  2935. }
  2936. queue_accessibility_update();
  2937. ensure_cursor_is_visible();
  2938. accept_event();
  2939. }
  2940. void Tree::_shift_select_range(TreeItem *new_item) {
  2941. if (!new_item) {
  2942. new_item = selected_item;
  2943. }
  2944. int s_col = selected_col;
  2945. bool in_range = false;
  2946. TreeItem *item = root;
  2947. if (!shift_anchor) {
  2948. shift_anchor = selected_item;
  2949. }
  2950. while (item) {
  2951. bool at_range_edge = item == shift_anchor || item == new_item;
  2952. if (at_range_edge) {
  2953. in_range = !in_range;
  2954. }
  2955. if (new_item == shift_anchor) {
  2956. in_range = false;
  2957. }
  2958. if (item->is_visible_in_tree()) {
  2959. if (in_range || at_range_edge) {
  2960. if (!item->is_selected(selected_col) && item->is_selectable(selected_col)) {
  2961. item->select(selected_col);
  2962. emit_signal(SNAME("multi_selected"), item, selected_col, true);
  2963. }
  2964. } else if (item->is_selected(selected_col)) {
  2965. item->deselect(selected_col);
  2966. emit_signal(SNAME("multi_selected"), item, selected_col, false);
  2967. }
  2968. }
  2969. item = item->get_next_in_tree(false);
  2970. }
  2971. selected_item = new_item;
  2972. selected_col = s_col;
  2973. ensure_cursor_is_visible();
  2974. queue_redraw();
  2975. accept_event();
  2976. }
  2977. void Tree::_go_down() {
  2978. TreeItem *next = nullptr;
  2979. if (!selected_item) {
  2980. if (root) {
  2981. next = hide_root ? root->get_next_visible() : root;
  2982. }
  2983. } else {
  2984. next = selected_item->get_next_visible();
  2985. }
  2986. int col = MAX(selected_col, 0);
  2987. if (select_mode == SELECT_MULTI) {
  2988. if (!next) {
  2989. return;
  2990. }
  2991. selected_item = next;
  2992. emit_signal(SNAME("cell_selected"));
  2993. queue_redraw();
  2994. } else {
  2995. while (next && !next->cells[col].selectable) {
  2996. next = next->get_next_visible();
  2997. }
  2998. if (!next) {
  2999. return; // Do nothing.
  3000. }
  3001. next->select(col);
  3002. }
  3003. queue_accessibility_update();
  3004. ensure_cursor_is_visible();
  3005. accept_event();
  3006. }
  3007. bool Tree::_scroll(bool p_horizontal, float p_pages) {
  3008. ScrollBar *scroll = p_horizontal ? (ScrollBar *)h_scroll : (ScrollBar *)v_scroll;
  3009. double prev_value = scroll->get_value();
  3010. scroll->set_value(scroll->get_value() + scroll->get_page() * p_pages);
  3011. bool scroll_happened = scroll->get_value() != prev_value;
  3012. if (scroll_happened) {
  3013. _determine_hovered_item();
  3014. }
  3015. return scroll_happened;
  3016. }
  3017. Rect2 Tree::_get_scrollbar_layout_rect() const {
  3018. const Size2 control_size = get_size();
  3019. const Ref<StyleBox> background = theme_cache.panel_style;
  3020. // This is the background stylebox's content rect.
  3021. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  3022. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  3023. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  3024. // Use the stylebox's margins by default. Can be overridden by `scrollbar_margin_*`.
  3025. const real_t top = theme_cache.scrollbar_margin_top < 0 ? content_rect.get_position().y : theme_cache.scrollbar_margin_top;
  3026. const real_t right = theme_cache.scrollbar_margin_right < 0 ? content_rect.get_end().x : (control_size.x - theme_cache.scrollbar_margin_right);
  3027. const real_t bottom = theme_cache.scrollbar_margin_bottom < 0 ? content_rect.get_end().y : (control_size.y - theme_cache.scrollbar_margin_bottom);
  3028. const real_t left = theme_cache.scrollbar_margin_left < 0 ? content_rect.get_position().x : theme_cache.scrollbar_margin_left;
  3029. return Rect2(left, top, right - left, bottom - top);
  3030. }
  3031. Rect2 Tree::_get_content_rect() const {
  3032. const Size2 control_size = get_size();
  3033. const Ref<StyleBox> background = theme_cache.panel_style;
  3034. // This is the background stylebox's content rect.
  3035. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  3036. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  3037. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  3038. // Scrollbars won't affect Tree's content rect if they're not visible or placed inside the stylebox margin area.
  3039. const real_t v_size = v_scroll->is_visible() ? (v_scroll->get_combined_minimum_size().x + theme_cache.scrollbar_h_separation) : 0;
  3040. const real_t h_size = h_scroll->is_visible() ? (h_scroll->get_combined_minimum_size().y + theme_cache.scrollbar_v_separation) : 0;
  3041. const Point2 scroll_begin = _get_scrollbar_layout_rect().get_end() - Vector2(v_size, h_size);
  3042. const Size2 offset = (content_rect.get_end() - scroll_begin).maxf(0);
  3043. return content_rect.grow_individual(0, 0, -offset.x, -offset.y);
  3044. }
  3045. void Tree::gui_input(const Ref<InputEvent> &p_event) {
  3046. ERR_FAIL_COND(p_event.is_null());
  3047. Ref<InputEventKey> k = p_event;
  3048. if (k.is_valid() && k->get_keycode() == Key::SHIFT && !k->is_pressed()) {
  3049. shift_anchor = nullptr;
  3050. }
  3051. bool is_command = k.is_valid() && k->is_command_or_control_pressed();
  3052. if (p_event->is_action(cache.rtl ? "ui_left" : "ui_right") && p_event->is_pressed()) {
  3053. if (!cursor_can_exit_tree) {
  3054. accept_event();
  3055. }
  3056. if (!selected_item || selected_col > (columns.size() - 1)) {
  3057. return;
  3058. }
  3059. if (k.is_valid() && k->is_shift_pressed()) {
  3060. selected_item->set_collapsed_recursive(false);
  3061. } else if (select_mode != SELECT_ROW) {
  3062. _go_right();
  3063. } else if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
  3064. selected_item->set_collapsed(false);
  3065. } else {
  3066. _go_down();
  3067. }
  3068. } else if (p_event->is_action(cache.rtl ? "ui_right" : "ui_left") && p_event->is_pressed()) {
  3069. if (!cursor_can_exit_tree) {
  3070. accept_event();
  3071. }
  3072. if (!selected_item || selected_col < 0) {
  3073. return;
  3074. }
  3075. if (k.is_valid() && k->is_shift_pressed()) {
  3076. selected_item->set_collapsed_recursive(true);
  3077. } else if (select_mode != SELECT_ROW) {
  3078. _go_left();
  3079. } else if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
  3080. selected_item->set_collapsed(true);
  3081. } else {
  3082. _go_up();
  3083. }
  3084. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !is_command) {
  3085. if (!cursor_can_exit_tree) {
  3086. accept_event();
  3087. }
  3088. // Shift Up Selection.
  3089. if (k.is_valid() && k->is_shift_pressed() && selected_item && select_mode == SELECT_MULTI) {
  3090. TreeItem *new_item = selected_item->get_prev_visible(false);
  3091. _shift_select_range(new_item);
  3092. } else {
  3093. _go_up();
  3094. }
  3095. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !is_command) {
  3096. if (!cursor_can_exit_tree) {
  3097. accept_event();
  3098. }
  3099. // Shift Down Selection.
  3100. if (k.is_valid() && k->is_shift_pressed() && selected_item && select_mode == SELECT_MULTI) {
  3101. TreeItem *new_item = selected_item->get_next_visible(false);
  3102. _shift_select_range(new_item);
  3103. } else {
  3104. _go_down();
  3105. }
  3106. } else if (p_event->is_action("ui_menu") && p_event->is_pressed()) {
  3107. if (allow_rmb_select && selected_item) {
  3108. emit_signal(SNAME("item_mouse_selected"), get_item_rect(selected_item).position, MouseButton::RIGHT);
  3109. }
  3110. accept_event();
  3111. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  3112. if (!cursor_can_exit_tree) {
  3113. accept_event();
  3114. }
  3115. TreeItem *next = nullptr;
  3116. if (!selected_item) {
  3117. return;
  3118. }
  3119. next = selected_item;
  3120. for (int i = 0; i < 10; i++) {
  3121. TreeItem *_n = next->get_next_visible();
  3122. if (_n) {
  3123. next = _n;
  3124. } else {
  3125. break;
  3126. }
  3127. }
  3128. if (next == selected_item) {
  3129. return;
  3130. }
  3131. if (select_mode == SELECT_MULTI) {
  3132. selected_item = next;
  3133. emit_signal(SNAME("cell_selected"));
  3134. queue_accessibility_update();
  3135. queue_redraw();
  3136. } else {
  3137. while (next && !next->cells[selected_col].selectable) {
  3138. next = next->get_next_visible();
  3139. }
  3140. if (!next) {
  3141. return; // Do nothing.
  3142. }
  3143. next->select(selected_col);
  3144. }
  3145. ensure_cursor_is_visible();
  3146. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  3147. if (!cursor_can_exit_tree) {
  3148. accept_event();
  3149. }
  3150. TreeItem *prev = nullptr;
  3151. if (!selected_item) {
  3152. return;
  3153. }
  3154. prev = selected_item;
  3155. for (int i = 0; i < 10; i++) {
  3156. TreeItem *_n = prev->get_prev_visible();
  3157. if (_n) {
  3158. prev = _n;
  3159. } else {
  3160. break;
  3161. }
  3162. }
  3163. if (prev == selected_item) {
  3164. return;
  3165. }
  3166. if (select_mode == SELECT_MULTI) {
  3167. selected_item = prev;
  3168. emit_signal(SNAME("cell_selected"));
  3169. queue_accessibility_update();
  3170. queue_redraw();
  3171. } else {
  3172. while (prev && !prev->cells[selected_col].selectable) {
  3173. prev = prev->get_prev_visible();
  3174. }
  3175. if (!prev) {
  3176. return; // Do nothing.
  3177. }
  3178. prev->select(selected_col);
  3179. }
  3180. ensure_cursor_is_visible();
  3181. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  3182. if (select_mode == SELECT_MULTI) {
  3183. if (!selected_item) {
  3184. return;
  3185. }
  3186. if (selected_item && selected_col != -1 && selected_button != -1) {
  3187. const TreeItem::Cell &c = selected_item->cells[selected_col];
  3188. emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
  3189. } else if (selected_item->is_selected(selected_col)) {
  3190. selected_item->deselect(selected_col);
  3191. emit_signal(SNAME("multi_selected"), selected_item, selected_col, false);
  3192. } else if (selected_item->is_selectable(selected_col)) {
  3193. selected_item->select(selected_col);
  3194. emit_signal(SNAME("multi_selected"), selected_item, selected_col, true);
  3195. }
  3196. } else if (selected_item && selected_col != -1 && selected_button != -1) {
  3197. const TreeItem::Cell &c = selected_item->cells[selected_col];
  3198. emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
  3199. }
  3200. accept_event();
  3201. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  3202. if (selected_item) {
  3203. // Bring up editor if possible.
  3204. if (selected_item && selected_col != -1 && selected_button != -1) {
  3205. const TreeItem::Cell &c = selected_item->cells[selected_col];
  3206. emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
  3207. } else if (!edit_selected()) {
  3208. emit_signal(SNAME("item_activated"));
  3209. incr_search.clear();
  3210. }
  3211. }
  3212. accept_event();
  3213. }
  3214. if (allow_search && k.is_valid()) { // Incremental search.
  3215. if (!k->is_pressed()) {
  3216. return;
  3217. }
  3218. if (k->is_command_or_control_pressed() || (k->is_shift_pressed() && k->get_unicode() == 0) || k->is_meta_pressed()) {
  3219. return;
  3220. }
  3221. if (!root) {
  3222. return;
  3223. }
  3224. if (hide_root && !root->get_next_visible()) {
  3225. return;
  3226. }
  3227. if (k->get_unicode() > 0) {
  3228. _do_incr_search(String::chr(k->get_unicode()));
  3229. accept_event();
  3230. return;
  3231. } else {
  3232. if (k->get_keycode() != Key::SHIFT) {
  3233. last_keypress = 0;
  3234. }
  3235. }
  3236. }
  3237. Ref<InputEventMouseMotion> mm = p_event;
  3238. if (mm.is_valid()) {
  3239. hovered_pos = mm->get_position();
  3240. _determine_hovered_item();
  3241. bool rtl = is_layout_rtl();
  3242. if (pressing_for_editor && popup_pressing_edited_item && !popup_pressing_edited_item->cells.is_empty() && (popup_pressing_edited_item->get_cell_mode(popup_pressing_edited_item_column) == TreeItem::CELL_MODE_RANGE)) {
  3243. // This needs to happen now, because the popup can be closed when pressing another item, and must remain the popup edited item until it actually closes.
  3244. popup_edited_item = popup_pressing_edited_item;
  3245. popup_edited_item_col = popup_pressing_edited_item_column;
  3246. popup_pressing_edited_item = nullptr;
  3247. popup_pressing_edited_item_column = -1;
  3248. if (!range_drag_enabled) {
  3249. // Range drag.
  3250. Vector2 cpos = mm->get_position();
  3251. if (rtl) {
  3252. cpos.x = get_size().width - cpos.x;
  3253. }
  3254. if (cpos.distance_to(pressing_pos) > 2) {
  3255. range_drag_enabled = true;
  3256. range_drag_capture_pos = cpos;
  3257. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  3258. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  3259. }
  3260. } else {
  3261. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  3262. float diff_y = -mm->get_relative().y;
  3263. diff_y = Math::pow(Math::abs(diff_y), 1.8f) * SIGN(diff_y);
  3264. diff_y *= 0.1;
  3265. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  3266. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  3267. item_edited(popup_edited_item_col, popup_edited_item);
  3268. }
  3269. }
  3270. if (drag_touching && !drag_touching_deaccel) {
  3271. drag_accum -= mm->get_relative().y;
  3272. v_scroll->set_value(drag_from + drag_accum);
  3273. drag_speed = -mm->get_velocity().y;
  3274. }
  3275. }
  3276. Ref<InputEventMouseButton> mb = p_event;
  3277. if (mb.is_valid()) {
  3278. bool rtl = is_layout_rtl();
  3279. if (!mb->is_pressed()) {
  3280. if (mb->get_button_index() == MouseButton::LEFT ||
  3281. mb->get_button_index() == MouseButton::RIGHT) {
  3282. Point2 pos = mb->get_position();
  3283. if (rtl) {
  3284. pos.x = get_size().width - pos.x - 1;
  3285. }
  3286. pos -= theme_cache.panel_style->get_offset();
  3287. if (show_column_titles) {
  3288. pos.y -= _get_title_button_height();
  3289. if (pos.y < 0) {
  3290. pos.x += theme_cache.offset.x;
  3291. int len = 0;
  3292. for (int i = 0; i < columns.size(); i++) {
  3293. len += get_column_width(i);
  3294. if (pos.x < static_cast<real_t>(len)) {
  3295. emit_signal(SNAME("column_title_clicked"), i, mb->get_button_index());
  3296. break;
  3297. }
  3298. }
  3299. }
  3300. }
  3301. }
  3302. if (mb->get_button_index() == MouseButton::LEFT) {
  3303. if (single_select_defer) {
  3304. select_single_item(single_select_defer, root, single_select_defer_column);
  3305. single_select_defer = nullptr;
  3306. }
  3307. range_click_timer->stop();
  3308. if (pressing_for_editor) {
  3309. if (range_drag_enabled) {
  3310. range_drag_enabled = false;
  3311. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  3312. warp_mouse(range_drag_capture_pos);
  3313. } else {
  3314. Rect2 rect = _get_item_focus_rect(get_selected());
  3315. Point2 mpos = mb->get_position();
  3316. int icon_size_x = 0;
  3317. Ref<Texture2D> icon = get_selected()->get_icon(selected_col);
  3318. if (icon.is_valid()) {
  3319. Rect2i icon_region = get_selected()->get_icon_region(selected_col);
  3320. if (icon_region == Rect2i()) {
  3321. icon_size_x = icon->get_width();
  3322. } else {
  3323. icon_size_x = icon_region.size.width;
  3324. }
  3325. }
  3326. // Icon is treated as if it is outside of the rect so that double clicking on it will emit the `item_icon_double_clicked` signal.
  3327. if (rtl) {
  3328. mpos.x = get_size().width - (mpos.x + icon_size_x);
  3329. } else {
  3330. mpos.x -= icon_size_x;
  3331. }
  3332. if (rect.has_point(mpos)) {
  3333. if (!edit_selected()) {
  3334. emit_signal(SNAME("item_icon_double_clicked"));
  3335. }
  3336. } else {
  3337. emit_signal(SNAME("item_icon_double_clicked"));
  3338. }
  3339. }
  3340. pressing_for_editor = false;
  3341. }
  3342. if (drag_touching) {
  3343. if (drag_speed == 0) {
  3344. drag_touching_deaccel = false;
  3345. drag_touching = false;
  3346. set_process_internal(false);
  3347. } else {
  3348. drag_touching_deaccel = true;
  3349. }
  3350. }
  3351. }
  3352. if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item != nullptr) {
  3353. // Make sure the reference is current in case TreeItems is reconstructed.
  3354. cache.click_item = get_item_at_position(cache.click_pos);
  3355. // Only emit the event if the click is still within the button rect.
  3356. TreeItem *current_item;
  3357. int current_column, current_index, current_section;
  3358. _find_button_at_pos(mb->get_position(), current_item, current_column, current_index, current_section);
  3359. if (current_item == cache.click_item && current_column == cache.click_column && current_index == cache.click_index) {
  3360. emit_signal("button_clicked", cache.click_item, cache.click_column, cache.click_id, mb->get_button_index());
  3361. }
  3362. }
  3363. cache.click_type = Cache::CLICK_NONE;
  3364. cache.click_index = -1;
  3365. cache.click_id = -1;
  3366. cache.click_item = nullptr;
  3367. cache.click_column = 0;
  3368. queue_redraw();
  3369. return;
  3370. }
  3371. if (range_drag_enabled) {
  3372. return;
  3373. }
  3374. switch (mb->get_button_index()) {
  3375. case MouseButton::RIGHT:
  3376. case MouseButton::LEFT: {
  3377. Ref<StyleBox> bg = theme_cache.panel_style;
  3378. Point2 pos = mb->get_position();
  3379. if (rtl) {
  3380. pos.x = get_size().width - pos.x - 1;
  3381. }
  3382. pos -= bg->get_offset();
  3383. cache.click_type = Cache::CLICK_NONE;
  3384. if (show_column_titles) {
  3385. pos.y -= _get_title_button_height();
  3386. if (pos.y < 0) {
  3387. pos.x += theme_cache.offset.x;
  3388. int len = 0;
  3389. for (int i = 0; i < columns.size(); i++) {
  3390. len += get_column_width(i);
  3391. if (pos.x < static_cast<real_t>(len)) {
  3392. cache.click_type = Cache::CLICK_TITLE;
  3393. cache.click_index = i;
  3394. queue_redraw();
  3395. break;
  3396. }
  3397. }
  3398. break;
  3399. }
  3400. }
  3401. if (!root || (!root->get_first_child() && hide_root)) {
  3402. emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), mb->get_button_index());
  3403. break;
  3404. }
  3405. click_handled = false;
  3406. pressing_for_editor = false;
  3407. propagate_mouse_activated = false;
  3408. int x_limit = _get_content_rect().size.x;
  3409. cache.rtl = is_layout_rtl();
  3410. blocked++;
  3411. propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, mb->is_double_click(), root, mb->get_button_index(), mb);
  3412. blocked--;
  3413. if (pressing_for_editor) {
  3414. pressing_pos = mb->get_position();
  3415. if (rtl) {
  3416. pressing_pos.x = get_size().width - pressing_pos.x;
  3417. }
  3418. }
  3419. if (mb->get_button_index() == MouseButton::RIGHT) {
  3420. break;
  3421. }
  3422. if (drag_touching) {
  3423. set_process_internal(false);
  3424. drag_touching_deaccel = false;
  3425. drag_touching = false;
  3426. drag_speed = 0;
  3427. drag_from = 0;
  3428. }
  3429. if (!click_handled) {
  3430. drag_speed = 0;
  3431. drag_accum = 0;
  3432. drag_from = v_scroll->get_value();
  3433. drag_touching = DisplayServer::get_singleton()->is_touchscreen_available();
  3434. drag_touching_deaccel = false;
  3435. if (drag_touching) {
  3436. set_process_internal(true);
  3437. }
  3438. if (mb->get_button_index() == MouseButton::LEFT) {
  3439. if (get_item_at_position(mb->get_position()) == nullptr && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed()) {
  3440. emit_signal(SNAME("nothing_selected"));
  3441. }
  3442. }
  3443. }
  3444. if (propagate_mouse_activated) {
  3445. emit_signal(SNAME("item_activated"));
  3446. propagate_mouse_activated = false;
  3447. }
  3448. } break;
  3449. case MouseButton::WHEEL_UP: {
  3450. if (_scroll(mb->is_shift_pressed(), -mb->get_factor() / 8)) {
  3451. accept_event();
  3452. }
  3453. } break;
  3454. case MouseButton::WHEEL_DOWN: {
  3455. if (_scroll(mb->is_shift_pressed(), mb->get_factor() / 8)) {
  3456. accept_event();
  3457. }
  3458. } break;
  3459. case MouseButton::WHEEL_LEFT: {
  3460. if (_scroll(!mb->is_shift_pressed(), -mb->get_factor() / 8)) {
  3461. accept_event();
  3462. }
  3463. } break;
  3464. case MouseButton::WHEEL_RIGHT: {
  3465. if (_scroll(!mb->is_shift_pressed(), mb->get_factor() / 8)) {
  3466. accept_event();
  3467. }
  3468. } break;
  3469. default:
  3470. break;
  3471. }
  3472. }
  3473. Ref<InputEventPanGesture> pan_gesture = p_event;
  3474. if (pan_gesture.is_valid()) {
  3475. double prev_v = v_scroll->get_value();
  3476. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  3477. double prev_h = h_scroll->get_value();
  3478. if (is_layout_rtl()) {
  3479. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * -pan_gesture->get_delta().x / 8);
  3480. } else {
  3481. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  3482. }
  3483. if (v_scroll->get_value() != prev_v || h_scroll->get_value() != prev_h) {
  3484. accept_event();
  3485. }
  3486. }
  3487. }
  3488. void Tree::_determine_hovered_item() {
  3489. Ref<StyleBox> bg = theme_cache.panel_style;
  3490. bool rtl = is_layout_rtl();
  3491. Point2 pos = hovered_pos;
  3492. if (rtl) {
  3493. pos.x = get_size().width - pos.x - 1;
  3494. }
  3495. pos -= theme_cache.panel_style->get_offset();
  3496. bool old_header_row = cache.hover_header_row;
  3497. int old_header_column = cache.hover_header_column;
  3498. TreeItem *old_item = cache.hover_item;
  3499. int old_column = cache.hover_column;
  3500. int old_button_index_in_column = cache.hover_button_index_in_column;
  3501. // Determine hover on column headers.
  3502. cache.hover_header_row = false;
  3503. cache.hover_header_column = 0;
  3504. if (show_column_titles && is_mouse_hovering) {
  3505. pos.y -= _get_title_button_height();
  3506. if (pos.y < 0) {
  3507. pos.x += theme_cache.offset.x;
  3508. int len = 0;
  3509. for (int i = 0; i < columns.size(); i++) {
  3510. len += get_column_width(i);
  3511. if (pos.x < len) {
  3512. cache.hover_header_row = true;
  3513. cache.hover_header_column = i;
  3514. cache.hover_button_index_in_column = -1;
  3515. break;
  3516. }
  3517. }
  3518. }
  3519. }
  3520. // Determine hover on rows and items.
  3521. if (root && is_mouse_hovering && hovered_pos.y >= 0) {
  3522. TreeItem *it;
  3523. int col, section, col_button_index;
  3524. _find_button_at_pos(hovered_pos, it, col, col_button_index, section);
  3525. if (drop_mode_flags) {
  3526. if (it != drop_mode_over) {
  3527. drop_mode_over = it;
  3528. queue_redraw();
  3529. }
  3530. if (it && section != drop_mode_section) {
  3531. drop_mode_section = section;
  3532. queue_redraw();
  3533. }
  3534. }
  3535. cache.hover_item = it;
  3536. cache.hover_column = col;
  3537. cache.hover_button_index_in_column = col_button_index;
  3538. if (it != old_item || col != old_column) {
  3539. if (old_item && old_column >= old_item->cells.size()) {
  3540. // Columns may have changed since last `redraw()`.
  3541. queue_redraw();
  3542. } else {
  3543. // Only need to update if mouse enters/exits a button.
  3544. bool was_over_button = old_item && old_item->cells[old_column].custom_button;
  3545. bool is_over_button = it && it->cells[col].custom_button;
  3546. if (was_over_button || is_over_button) {
  3547. queue_redraw();
  3548. }
  3549. }
  3550. }
  3551. }
  3552. // Reduce useless redraw calls.
  3553. bool hovered_cell_button_changed = (cache.hover_button_index_in_column != old_button_index_in_column);
  3554. bool hovered_column_changed = (cache.hover_column != old_column);
  3555. // Mouse has moved from row to row, or from cell to cell within same row unless selection mode is full row which saves a useless redraw.
  3556. bool item_hover_needs_redraw = !cache.hover_header_row && (cache.hover_item != old_item || hovered_cell_button_changed || (select_mode != SELECT_ROW && hovered_column_changed));
  3557. // Mouse has moved between two different column header sections.
  3558. bool header_hover_needs_redraw = cache.hover_header_row && cache.hover_header_column != old_header_column;
  3559. // Mouse has moved between header and "main" areas.
  3560. bool whole_needs_redraw = cache.hover_header_row != old_header_row;
  3561. if (whole_needs_redraw || header_hover_needs_redraw || item_hover_needs_redraw) {
  3562. queue_redraw();
  3563. }
  3564. }
  3565. bool Tree::edit_selected(bool p_force_edit) {
  3566. TreeItem *s = get_selected();
  3567. ERR_FAIL_NULL_V_MSG(s, false, "No item selected.");
  3568. ensure_cursor_is_visible();
  3569. int col = get_selected_column();
  3570. ERR_FAIL_INDEX_V_MSG(col, columns.size(), false, "No item column selected.");
  3571. if (!s->cells[col].editable && !p_force_edit) {
  3572. return false;
  3573. }
  3574. Size2 scale = popup_editor->get_parent_viewport()->get_popup_base_transform().get_scale() * get_global_transform_with_canvas().get_scale();
  3575. real_t popup_scale = MIN(scale.x, scale.y);
  3576. Rect2 rect = _get_item_focus_rect(s);
  3577. rect.position *= popup_scale;
  3578. popup_edited_item = s;
  3579. popup_edited_item_col = col;
  3580. const TreeItem::Cell &c = s->cells[col];
  3581. if (c.mode == TreeItem::CELL_MODE_CHECK) {
  3582. s->set_checked(col, !c.checked);
  3583. item_edited(col, s);
  3584. return true;
  3585. } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
  3586. edited_item = s;
  3587. edited_col = col;
  3588. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  3589. emit_signal(SNAME("custom_popup_edited"), false);
  3590. item_edited(col, s);
  3591. return true;
  3592. } else if (c.mode == TreeItem::CELL_MODE_RANGE && !c.text.is_empty()) {
  3593. _update_popup_menu(c);
  3594. popup_menu->set_size(Size2(rect.size.width, 0));
  3595. popup_menu->set_position(get_screen_position() + rect.position + Point2i(0, rect.size.height));
  3596. popup_menu->popup();
  3597. popup_edited_item = s;
  3598. popup_edited_item_col = col;
  3599. return true;
  3600. } else if ((c.mode == TreeItem::CELL_MODE_STRING && !c.edit_multiline) || c.mode == TreeItem::CELL_MODE_RANGE) {
  3601. int value_editor_height = 0;
  3602. if (c.mode == TreeItem::CELL_MODE_RANGE) {
  3603. _update_value_editor(c);
  3604. value_editor_height = value_editor->get_minimum_size().height;
  3605. value_editor->show();
  3606. } else if (value_editor) {
  3607. value_editor->hide();
  3608. }
  3609. Rect2 popup_rect;
  3610. // `floor()` centers vertically.
  3611. Vector2 ofs(0, Math::floor((MAX(line_editor->get_minimum_size().height, rect.size.height - value_editor_height) - rect.size.height) / 2));
  3612. // Account for icon.
  3613. real_t icon_ofs = 0;
  3614. if (c.icon.is_valid()) {
  3615. icon_ofs = _get_cell_icon_size(c).x * popup_scale + theme_cache.h_separation;
  3616. }
  3617. popup_rect.size = rect.size + Vector2(-icon_ofs, value_editor_height);
  3618. popup_rect.position = rect.position - ofs;
  3619. popup_rect.position.x += icon_ofs;
  3620. if (cache.rtl) {
  3621. popup_rect.position.x = get_size().width - popup_rect.position.x - popup_rect.size.x;
  3622. }
  3623. popup_rect.position += get_screen_position();
  3624. line_editor->clear();
  3625. line_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::range_step_decimals(c.step)));
  3626. line_editor->select_all();
  3627. line_editor->show();
  3628. text_editor->hide();
  3629. popup_editor->set_position(popup_rect.position);
  3630. popup_editor->set_size(popup_rect.size * popup_scale);
  3631. if (!popup_editor->is_embedded()) {
  3632. popup_editor->set_content_scale_factor(popup_scale);
  3633. }
  3634. popup_edit_committed = false; // Start edit popup processing.
  3635. popup_editor->popup();
  3636. popup_editor->child_controls_changed();
  3637. line_editor->grab_focus();
  3638. return true;
  3639. } else if (c.mode == TreeItem::CELL_MODE_STRING && c.edit_multiline) {
  3640. line_editor->hide();
  3641. text_editor->clear();
  3642. text_editor->set_text(c.text);
  3643. text_editor->select_all();
  3644. text_editor->show();
  3645. popup_editor->set_position(get_screen_position() + rect.position);
  3646. popup_editor->set_size(rect.size * popup_scale);
  3647. if (!popup_editor->is_embedded()) {
  3648. popup_editor->set_content_scale_factor(popup_scale);
  3649. }
  3650. popup_edit_committed = false; // Start edit popup processing.
  3651. popup_editor->popup();
  3652. popup_editor->child_controls_changed();
  3653. text_editor->grab_focus();
  3654. return true;
  3655. }
  3656. return false;
  3657. }
  3658. Rect2 Tree::_get_item_focus_rect(const TreeItem *p_item) const {
  3659. Rect2 rect;
  3660. if (select_mode == SELECT_ROW) {
  3661. rect = p_item->get_meta("__focus_col_" + itos(selected_col));
  3662. } else {
  3663. rect = p_item->get_meta("__focus_rect");
  3664. }
  3665. return rect;
  3666. }
  3667. bool Tree::is_editing() {
  3668. return popup_editor->is_visible();
  3669. }
  3670. void Tree::set_editor_selection(int p_from_line, int p_to_line, int p_from_column, int p_to_column, int p_caret) {
  3671. if (p_from_column == -1 || p_to_column == -1) {
  3672. line_editor->select(p_from_line, p_to_line);
  3673. } else {
  3674. text_editor->select(p_from_line, p_from_column, p_to_line, p_to_column, p_caret);
  3675. }
  3676. }
  3677. Size2 Tree::get_internal_min_size() const {
  3678. Size2i size;
  3679. if (root) {
  3680. size.height += get_item_height(root);
  3681. }
  3682. for (int i = 0; i < columns.size(); i++) {
  3683. size.width += get_column_minimum_width(i);
  3684. }
  3685. return size;
  3686. }
  3687. void Tree::update_scrollbars() {
  3688. const Size2 control_size = get_size();
  3689. const Ref<StyleBox> background = theme_cache.panel_style;
  3690. // This is the background stylebox's content rect.
  3691. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  3692. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  3693. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  3694. const Size2 hmin = h_scroll->get_combined_minimum_size();
  3695. const Size2 vmin = v_scroll->get_combined_minimum_size();
  3696. const Size2 internal_min_size = get_internal_min_size();
  3697. const int title_button_height = _get_title_button_height();
  3698. Size2 tree_content_size = content_rect.get_size() - Vector2(0, title_button_height);
  3699. bool display_vscroll = internal_min_size.height > tree_content_size.height;
  3700. bool display_hscroll = internal_min_size.width > tree_content_size.width;
  3701. for (int i = 0; i < 2; i++) {
  3702. // Check twice, as both values are dependent on each other.
  3703. if (display_hscroll) {
  3704. tree_content_size.height = content_rect.get_size().height - title_button_height - hmin.height;
  3705. display_vscroll = internal_min_size.height > tree_content_size.height;
  3706. }
  3707. if (display_vscroll) {
  3708. tree_content_size.width = content_rect.get_size().width - vmin.width;
  3709. display_hscroll = internal_min_size.width > tree_content_size.width;
  3710. }
  3711. }
  3712. if (display_vscroll) {
  3713. v_scroll->show();
  3714. v_scroll->set_max(internal_min_size.height);
  3715. v_scroll->set_page(tree_content_size.height);
  3716. theme_cache.offset.y = v_scroll->get_value();
  3717. } else {
  3718. v_scroll->hide();
  3719. theme_cache.offset.y = 0;
  3720. }
  3721. if (display_hscroll) {
  3722. h_scroll->show();
  3723. h_scroll->set_max(internal_min_size.width);
  3724. h_scroll->set_page(tree_content_size.width);
  3725. theme_cache.offset.x = h_scroll->get_value();
  3726. } else {
  3727. h_scroll->hide();
  3728. theme_cache.offset.x = 0;
  3729. }
  3730. const Rect2 scroll_rect = _get_scrollbar_layout_rect();
  3731. v_scroll->set_begin(scroll_rect.get_position() + Vector2(scroll_rect.get_size().x - vmin.width, 0));
  3732. v_scroll->set_end(scroll_rect.get_end() - Vector2(0, display_hscroll ? hmin.height : 0));
  3733. h_scroll->set_begin(scroll_rect.get_position() + Vector2(0, scroll_rect.get_size().y - hmin.height));
  3734. h_scroll->set_end(scroll_rect.get_end() - Vector2(display_vscroll ? vmin.width : 0, 0));
  3735. }
  3736. int Tree::_get_title_button_height() const {
  3737. ERR_FAIL_COND_V(theme_cache.tb_font.is_null() || theme_cache.title_button.is_null(), 0);
  3738. int h = 0;
  3739. if (show_column_titles) {
  3740. for (int i = 0; i < columns.size(); i++) {
  3741. h = MAX(h, columns[i].text_buf->get_size().y + theme_cache.title_button->get_minimum_size().height);
  3742. }
  3743. }
  3744. return h;
  3745. }
  3746. void Tree::_check_item_accessibility(TreeItem *p_item, PackedStringArray &r_warnings, int &r_row) const {
  3747. for (int i = 0; i < p_item->cells.size(); i++) {
  3748. const TreeItem::Cell &cell = p_item->cells[i];
  3749. if (cell.alt_text.strip_edges().is_empty() && cell.text.strip_edges().is_empty()) {
  3750. r_warnings.push_back(vformat(RTR("Cell %d x %d: either text or alternative text must not be empty."), r_row, i));
  3751. }
  3752. for (int j = 0; j < cell.buttons.size(); j++) {
  3753. if (cell.buttons[j].alt_text.strip_edges().is_empty()) {
  3754. r_warnings.push_back(vformat(RTR("Button %d in %d x %d: alternative text must not be empty."), j, r_row, i));
  3755. }
  3756. }
  3757. }
  3758. r_row++;
  3759. // Children.
  3760. if (!p_item->collapsed) {
  3761. TreeItem *c = p_item->first_child;
  3762. while (c) {
  3763. _check_item_accessibility(c, r_warnings, r_row);
  3764. c = c->next;
  3765. }
  3766. }
  3767. }
  3768. PackedStringArray Tree::get_accessibility_configuration_warnings() const {
  3769. PackedStringArray warnings = Control::get_accessibility_configuration_warnings();
  3770. if (root) {
  3771. int row = 1;
  3772. _check_item_accessibility(root, warnings, row);
  3773. }
  3774. return warnings;
  3775. }
  3776. void Tree::_accessibility_action_scroll_down(const Variant &p_data) {
  3777. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 4);
  3778. }
  3779. void Tree::_accessibility_action_scroll_left(const Variant &p_data) {
  3780. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 4);
  3781. }
  3782. void Tree::_accessibility_action_scroll_right(const Variant &p_data) {
  3783. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 4);
  3784. }
  3785. void Tree::_accessibility_action_scroll_up(const Variant &p_data) {
  3786. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 4);
  3787. }
  3788. void Tree::_accessibility_action_scroll_set(const Variant &p_data) {
  3789. const Point2 &pos = p_data;
  3790. h_scroll->set_value(pos.x);
  3791. v_scroll->set_value(pos.y);
  3792. }
  3793. void Tree::_accessibility_action_scroll_into_view(const Variant &p_data, TreeItem *p_item, int p_col) {
  3794. scroll_to_item(p_item);
  3795. }
  3796. void Tree::_accessibility_action_focus(const Variant &p_data, TreeItem *p_item, int p_col) {
  3797. p_item->select(p_col);
  3798. }
  3799. void Tree::_accessibility_action_blur(const Variant &p_data, TreeItem *p_item, int p_col) {
  3800. p_item->deselect(p_col);
  3801. }
  3802. void Tree::_accessibility_action_collapse(const Variant &p_data, TreeItem *p_item) {
  3803. p_item->set_collapsed(true);
  3804. }
  3805. void Tree::_accessibility_action_expand(const Variant &p_data, TreeItem *p_item) {
  3806. p_item->set_collapsed(false);
  3807. }
  3808. void Tree::_accessibility_action_set_text_value(const Variant &p_data, TreeItem *p_item, int p_col) {
  3809. p_item->set_text(p_col, p_data);
  3810. }
  3811. void Tree::_accessibility_action_set_num_value(const Variant &p_data, TreeItem *p_item, int p_col) {
  3812. p_item->set_range(p_col, p_data);
  3813. }
  3814. void Tree::_accessibility_action_set_bool_value(const Variant &p_data, TreeItem *p_item, int p_col) {
  3815. p_item->set_checked(p_col, !p_item->cells[p_col].checked);
  3816. }
  3817. void Tree::_accessibility_action_edit_custom(const Variant &p_data, TreeItem *p_item, int p_col) {
  3818. float popup_scale = popup_editor->is_embedded() ? 1.0 : popup_editor->get_parent_visible_window()->get_content_scale_factor();
  3819. Rect2 rect;
  3820. if (select_mode == SELECT_ROW) {
  3821. rect = p_item->get_meta("__focus_col_" + itos(p_col));
  3822. } else {
  3823. rect = p_item->get_meta("__focus_rect");
  3824. }
  3825. rect.position *= popup_scale;
  3826. edited_item = p_item;
  3827. edited_col = p_col;
  3828. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  3829. emit_signal(SNAME("custom_popup_edited"), false);
  3830. item_edited(p_col, p_item);
  3831. }
  3832. void Tree::_accessibility_action_set_inc(const Variant &p_data, TreeItem *p_item, int p_col) {
  3833. p_item->set_range(p_col, p_item->cells[p_col].val + p_item->cells[p_col].step);
  3834. }
  3835. void Tree::_accessibility_action_set_dec(const Variant &p_data, TreeItem *p_item, int p_col) {
  3836. p_item->set_range(p_col, p_item->cells[p_col].val - p_item->cells[p_col].step);
  3837. }
  3838. void Tree::_accessibility_action_button_press(const Variant &p_data, TreeItem *p_item, int p_col, int p_btn) {
  3839. emit_signal("button_clicked", p_item, p_col, p_btn, MouseButton::LEFT);
  3840. }
  3841. RID Tree::get_focused_accessibility_element() const {
  3842. if (selected_item) {
  3843. if (selected_col >= 0) {
  3844. if (selected_button >= 0) {
  3845. return selected_item->cells[selected_col].buttons[selected_button].accessibility_button_element;
  3846. } else {
  3847. return selected_item->cells[selected_col].accessibility_cell_element;
  3848. }
  3849. } else {
  3850. return selected_item->accessibility_row_element;
  3851. }
  3852. } else {
  3853. return get_accessibility_element();
  3854. }
  3855. }
  3856. void Tree::_accessibility_clean_info(TreeItem *p_item) {
  3857. p_item->accessibility_row_element = RID();
  3858. for (TreeItem::Cell &cell : p_item->cells) {
  3859. cell.accessibility_cell_element = RID();
  3860. for (TreeItem::Cell::Button &btn : cell.buttons) {
  3861. btn.accessibility_button_element = RID();
  3862. }
  3863. }
  3864. // Children.
  3865. TreeItem *c = p_item->first_child;
  3866. while (c) {
  3867. _accessibility_clean_info(c);
  3868. c = c->next;
  3869. }
  3870. }
  3871. void Tree::_accessibility_update_item(Point2 &r_ofs, TreeItem *p_item, int &r_row, int p_level) {
  3872. // Row.
  3873. if ((p_item != root || !hide_root) && p_item->is_visible()) {
  3874. if (p_item->accessibility_row_element.is_null()) {
  3875. p_item->accessibility_row_element = DisplayServer::get_singleton()->accessibility_create_sub_element(accessibility_scroll_element, DisplayServer::AccessibilityRole::ROLE_TREE_ITEM);
  3876. p_item->accessibility_row_dirty = true;
  3877. }
  3878. DisplayServer::get_singleton()->accessibility_update_set_table_row_index(p_item->accessibility_row_element, r_row);
  3879. DisplayServer::get_singleton()->accessibility_update_set_list_item_level(p_item->accessibility_row_element, p_level);
  3880. DisplayServer::get_singleton()->accessibility_update_set_list_item_expanded(p_item->accessibility_row_element, !p_item->collapsed);
  3881. DisplayServer::get_singleton()->accessibility_update_set_flag(p_item->accessibility_row_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !(p_item->visible && !p_item->parent_visible_in_tree));
  3882. DisplayServer::get_singleton()->accessibility_update_add_action(p_item->accessibility_row_element, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &Tree::_accessibility_action_collapse).bind(p_item));
  3883. DisplayServer::get_singleton()->accessibility_update_add_action(p_item->accessibility_row_element, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &Tree::_accessibility_action_expand).bind(p_item));
  3884. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(p_item->accessibility_row_element, selected_item == p_item);
  3885. if (p_item == root && is_root_hidden()) {
  3886. DisplayServer::get_singleton()->accessibility_update_set_flag(p_item->accessibility_row_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, true);
  3887. }
  3888. Transform2D row_xform;
  3889. row_xform.set_origin(r_ofs);
  3890. DisplayServer::get_singleton()->accessibility_update_set_transform(p_item->accessibility_row_element, row_xform);
  3891. Size2 item_size = Size2(get_size().width, compute_item_height(p_item));
  3892. DisplayServer::get_singleton()->accessibility_update_set_bounds(p_item->accessibility_row_element, Rect2(Vector2(), item_size));
  3893. if (p_item->accessibility_row_dirty) {
  3894. // Cells.
  3895. int col_offset = 0;
  3896. for (int i = 0; i < p_item->cells.size(); i++) {
  3897. TreeItem::Cell &cell = p_item->cells.write[i];
  3898. if (cell.accessibility_cell_element.is_null()) {
  3899. cell.accessibility_cell_element = DisplayServer::get_singleton()->accessibility_create_sub_element(p_item->accessibility_row_element, DisplayServer::AccessibilityRole::ROLE_CELL);
  3900. }
  3901. float cw = get_column_width(i);
  3902. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &Tree::_accessibility_action_scroll_into_view).bind(p_item, i));
  3903. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Tree::_accessibility_action_focus).bind(p_item, i));
  3904. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &Tree::_accessibility_action_blur).bind(p_item, i));
  3905. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &Tree::_accessibility_action_collapse).bind(p_item));
  3906. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &Tree::_accessibility_action_expand).bind(p_item));
  3907. DisplayServer::get_singleton()->accessibility_update_set_table_cell_position(cell.accessibility_cell_element, r_row, i);
  3908. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(cell.accessibility_cell_element, cell.selected);
  3909. if (cell.alt_text.is_empty()) {
  3910. DisplayServer::get_singleton()->accessibility_update_set_name(cell.accessibility_cell_element, cell.xl_text);
  3911. } else {
  3912. DisplayServer::get_singleton()->accessibility_update_set_name(cell.accessibility_cell_element, cell.alt_text);
  3913. }
  3914. DisplayServer::get_singleton()->accessibility_update_set_text_align(cell.accessibility_cell_element, cell.text_alignment);
  3915. DisplayServer::get_singleton()->accessibility_update_set_flag(cell.accessibility_cell_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !(p_item->visible && !p_item->parent_visible_in_tree));
  3916. DisplayServer::get_singleton()->accessibility_update_set_flag(cell.accessibility_cell_element, DisplayServer::AccessibilityFlags::FLAG_READONLY, !cell.editable);
  3917. DisplayServer::get_singleton()->accessibility_update_set_tooltip(cell.accessibility_cell_element, cell.tooltip);
  3918. switch (cell.mode) {
  3919. case TreeItem::CELL_MODE_STRING: {
  3920. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Tree::_accessibility_action_set_text_value).bind(p_item, i));
  3921. DisplayServer::get_singleton()->accessibility_update_set_value(cell.accessibility_cell_element, cell.xl_text);
  3922. } break;
  3923. case TreeItem::CELL_MODE_CHECK: {
  3924. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_set_bool_value).bind(p_item, i));
  3925. DisplayServer::get_singleton()->accessibility_update_set_checked(cell.accessibility_cell_element, cell.checked);
  3926. } break;
  3927. case TreeItem::CELL_MODE_RANGE: {
  3928. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &Tree::_accessibility_action_set_dec).bind(p_item, i));
  3929. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &Tree::_accessibility_action_set_inc).bind(p_item, i));
  3930. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Tree::_accessibility_action_set_num_value).bind(p_item, i));
  3931. DisplayServer::get_singleton()->accessibility_update_set_num_value(cell.accessibility_cell_element, cell.val);
  3932. DisplayServer::get_singleton()->accessibility_update_set_num_range(cell.accessibility_cell_element, cell.min, cell.max);
  3933. if (cell.step > 0) {
  3934. DisplayServer::get_singleton()->accessibility_update_set_num_step(cell.accessibility_cell_element, cell.step);
  3935. } else {
  3936. DisplayServer::get_singleton()->accessibility_update_set_num_step(cell.accessibility_cell_element, 1);
  3937. }
  3938. } break;
  3939. case TreeItem::CELL_MODE_ICON: {
  3940. // NOP
  3941. } break;
  3942. case TreeItem::CELL_MODE_CUSTOM: {
  3943. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_edit_custom).bind(p_item, i));
  3944. } break;
  3945. }
  3946. DisplayServer::get_singleton()->accessibility_update_set_background_color(cell.accessibility_cell_element, cell.color);
  3947. DisplayServer::get_singleton()->accessibility_update_set_foreground_color(cell.accessibility_cell_element, cell.bg_color);
  3948. DisplayServer::get_singleton()->accessibility_update_set_bounds(cell.accessibility_cell_element, Rect2(Point2(col_offset, 0), Size2(cw, item_size.y)));
  3949. Vector2 ofst = Vector2(col_offset + cw, 0);
  3950. for (int j = cell.buttons.size() - 1; j >= 0; j--) {
  3951. if (cell.buttons[j].accessibility_button_element.is_null()) {
  3952. cell.buttons[j].accessibility_button_element = DisplayServer::get_singleton()->accessibility_create_sub_element(cell.accessibility_cell_element, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  3953. }
  3954. DisplayServer::get_singleton()->accessibility_update_add_action(cell.buttons[j].accessibility_button_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_button_press).bind(p_item, i, j));
  3955. DisplayServer::get_singleton()->accessibility_update_set_flag(cell.buttons[j].accessibility_button_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, cell.buttons[j].disabled);
  3956. DisplayServer::get_singleton()->accessibility_update_set_tooltip(cell.buttons[j].accessibility_button_element, cell.buttons[j].tooltip);
  3957. if (cell.buttons[j].alt_text.is_empty()) {
  3958. DisplayServer::get_singleton()->accessibility_update_set_name(cell.buttons[j].accessibility_button_element, cell.buttons[j].tooltip);
  3959. } else {
  3960. DisplayServer::get_singleton()->accessibility_update_set_name(cell.buttons[j].accessibility_button_element, cell.buttons[j].alt_text);
  3961. }
  3962. Ref<Texture2D> b = cell.buttons[j].texture;
  3963. Size2 b_size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  3964. ofst.x -= b_size.x;
  3965. DisplayServer::get_singleton()->accessibility_update_set_bounds(cell.buttons[j].accessibility_button_element, Rect2(ofst, b_size));
  3966. }
  3967. col_offset += cw;
  3968. }
  3969. }
  3970. r_ofs.y += item_size.y;
  3971. r_ofs.y += theme_cache.v_separation;
  3972. p_item->accessibility_row_dirty = false;
  3973. r_row++;
  3974. }
  3975. // Children.
  3976. if (!p_item->collapsed) {
  3977. TreeItem *c = p_item->first_child;
  3978. while (c) {
  3979. _accessibility_update_item(r_ofs, c, r_row, p_level + 1);
  3980. c = c->next;
  3981. }
  3982. }
  3983. }
  3984. void Tree::_notification(int p_what) {
  3985. switch (p_what) {
  3986. case NOTIFICATION_EXIT_TREE:
  3987. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  3988. if (root) {
  3989. _accessibility_clean_info(root);
  3990. }
  3991. for (ColumnInfo &col : columns) {
  3992. col.accessibility_col_element = RID();
  3993. }
  3994. accessibility_scroll_element = RID();
  3995. } break;
  3996. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  3997. RID ae = get_accessibility_element();
  3998. ERR_FAIL_COND(ae.is_null());
  3999. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_TREE);
  4000. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &Tree::_accessibility_action_scroll_down));
  4001. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &Tree::_accessibility_action_scroll_left));
  4002. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &Tree::_accessibility_action_scroll_right));
  4003. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &Tree::_accessibility_action_scroll_up));
  4004. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &Tree::_accessibility_action_scroll_set));
  4005. Ref<StyleBox> bg = theme_cache.panel_style;
  4006. int tbh = _get_title_button_height();
  4007. // Columns.
  4008. int ofs = theme_cache.panel_style->get_margin(SIDE_LEFT);
  4009. int cs = columns.size();
  4010. for (int i = 0; i < cs; i++) {
  4011. ColumnInfo &column = columns.write[i];
  4012. if (column.accessibility_col_element.is_null()) {
  4013. column.accessibility_col_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_COLUMN_HEADER);
  4014. }
  4015. DisplayServer::get_singleton()->accessibility_update_set_table_column_index(column.accessibility_col_element, i);
  4016. DisplayServer::get_singleton()->accessibility_update_set_name(column.accessibility_col_element, column.xl_title);
  4017. DisplayServer::get_singleton()->accessibility_update_set_text_align(column.accessibility_col_element, column.title_alignment);
  4018. Rect2 tbrect = Rect2(ofs - theme_cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
  4019. if (cache.rtl) {
  4020. tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
  4021. }
  4022. ofs += tbrect.size.width;
  4023. DisplayServer::get_singleton()->accessibility_update_set_bounds(column.accessibility_col_element, Rect2(tbrect.position, tbrect.size));
  4024. }
  4025. DisplayServer::get_singleton()->accessibility_update_set_table_column_count(ae, cs);
  4026. // Scroll container.
  4027. if (accessibility_scroll_element.is_null()) {
  4028. accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
  4029. }
  4030. Transform2D scroll_xform;
  4031. scroll_xform.set_origin(Vector2i(-h_scroll->get_value(), -v_scroll->get_value()));
  4032. DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform);
  4033. DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, Rect2(0, 0, h_scroll->get_max(), v_scroll->get_max()));
  4034. // Rows (and cells).
  4035. Point2 origin = Point2(theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.offset.x, bg->get_margin(SIDE_TOP) + tbh);
  4036. int rows = 0;
  4037. if (root) {
  4038. _accessibility_update_item(origin, root, rows, 0);
  4039. }
  4040. DisplayServer::get_singleton()->accessibility_update_set_table_row_count(ae, rows);
  4041. } break;
  4042. case NOTIFICATION_FOCUS_ENTER: {
  4043. if (get_viewport()) {
  4044. focus_in_id = get_viewport()->get_processed_events_count();
  4045. }
  4046. } break;
  4047. case NOTIFICATION_MOUSE_ENTER: {
  4048. is_mouse_hovering = true;
  4049. _determine_hovered_item();
  4050. } break;
  4051. case NOTIFICATION_MOUSE_EXIT: {
  4052. is_mouse_hovering = false;
  4053. // Clear hovered item cache.
  4054. if (cache.hover_header_row || cache.hover_item != nullptr) {
  4055. cache.hover_header_row = false;
  4056. cache.hover_header_column = -1;
  4057. cache.hover_item = nullptr;
  4058. cache.hover_column = -1;
  4059. cache.hover_button_index_in_column = -1;
  4060. queue_redraw();
  4061. }
  4062. } break;
  4063. case NOTIFICATION_VISIBILITY_CHANGED: {
  4064. drag_touching = false;
  4065. } break;
  4066. case NOTIFICATION_DRAG_END: {
  4067. drop_mode_flags = 0;
  4068. scrolling = false;
  4069. set_process_internal(false);
  4070. queue_redraw();
  4071. } break;
  4072. case NOTIFICATION_DRAG_BEGIN: {
  4073. single_select_defer = nullptr;
  4074. if (theme_cache.scroll_speed > 0) {
  4075. scrolling = true;
  4076. set_process_internal(true);
  4077. }
  4078. } break;
  4079. case NOTIFICATION_INTERNAL_PROCESS: {
  4080. if (drag_touching) {
  4081. if (drag_touching_deaccel) {
  4082. float pos = v_scroll->get_value();
  4083. pos += drag_speed * get_process_delta_time();
  4084. bool turnoff = false;
  4085. if (pos < 0) {
  4086. pos = 0;
  4087. turnoff = true;
  4088. set_process_internal(false);
  4089. drag_touching = false;
  4090. drag_touching_deaccel = false;
  4091. }
  4092. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  4093. pos = v_scroll->get_max() - v_scroll->get_page();
  4094. turnoff = true;
  4095. }
  4096. v_scroll->set_value(pos);
  4097. float sgn = drag_speed < 0 ? -1 : 1;
  4098. float val = Math::abs(drag_speed);
  4099. val -= 1000 * get_process_delta_time();
  4100. if (val < 0) {
  4101. turnoff = true;
  4102. }
  4103. drag_speed = sgn * val;
  4104. if (turnoff) {
  4105. set_process_internal(false);
  4106. drag_touching = false;
  4107. drag_touching_deaccel = false;
  4108. }
  4109. }
  4110. }
  4111. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  4112. if (scrolling && get_rect().grow(theme_cache.scroll_border).has_point(mouse_position)) {
  4113. Point2 point;
  4114. if ((Math::abs(mouse_position.x) < Math::abs(mouse_position.x - get_size().width)) && (Math::abs(mouse_position.x) < theme_cache.scroll_border)) {
  4115. point.x = mouse_position.x - theme_cache.scroll_border;
  4116. } else if (Math::abs(mouse_position.x - get_size().width) < theme_cache.scroll_border) {
  4117. point.x = mouse_position.x - (get_size().width - theme_cache.scroll_border);
  4118. }
  4119. if ((Math::abs(mouse_position.y) < Math::abs(mouse_position.y - get_size().height)) && (Math::abs(mouse_position.y) < theme_cache.scroll_border)) {
  4120. point.y = mouse_position.y - theme_cache.scroll_border;
  4121. } else if (Math::abs(mouse_position.y - get_size().height) < theme_cache.scroll_border) {
  4122. point.y = mouse_position.y - (get_size().height - theme_cache.scroll_border);
  4123. }
  4124. point *= theme_cache.scroll_speed * get_process_delta_time();
  4125. point += get_scroll();
  4126. h_scroll->set_value(point.x);
  4127. v_scroll->set_value(point.y);
  4128. }
  4129. } break;
  4130. case NOTIFICATION_DRAW: {
  4131. v_scroll->set_custom_step(theme_cache.font->get_height(theme_cache.font_size));
  4132. update_scrollbars();
  4133. RID ci = get_canvas_item();
  4134. Ref<StyleBox> bg = theme_cache.panel_style;
  4135. const Rect2 content_rect = _get_content_rect();
  4136. Point2 draw_ofs = content_rect.position;
  4137. Size2 draw_size = content_rect.size;
  4138. bg->draw(ci, Rect2(Point2(), get_size()));
  4139. int tbh = _get_title_button_height();
  4140. draw_ofs.y += tbh;
  4141. draw_size.y -= tbh;
  4142. cache.rtl = is_layout_rtl();
  4143. content_scale_factor = popup_editor->is_embedded() ? 1.0 : popup_editor->get_parent_visible_window()->get_content_scale_factor();
  4144. if (root && get_size().x > 0 && get_size().y > 0) {
  4145. int self_height = 0; // Just to pass a reference, we don't need the root's `self_height`.
  4146. draw_item(Point2(), draw_ofs, draw_size, root, self_height);
  4147. }
  4148. if (show_column_titles) {
  4149. // Title buttons.
  4150. int ofs2 = theme_cache.panel_style->get_margin(SIDE_LEFT);
  4151. for (int i = 0; i < columns.size(); i++) {
  4152. Ref<StyleBox> sb = (cache.click_type == Cache::CLICK_TITLE && cache.click_index == i) ? theme_cache.title_button_pressed : ((cache.hover_header_row && cache.hover_header_column == i) ? theme_cache.title_button_hover : theme_cache.title_button);
  4153. Rect2 tbrect = Rect2(ofs2 - theme_cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
  4154. if (cache.rtl) {
  4155. tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
  4156. }
  4157. sb->draw(ci, tbrect);
  4158. ofs2 += tbrect.size.width;
  4159. // Text.
  4160. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  4161. columns.write[i].text_buf->set_width(clip_w);
  4162. columns.write[i].cached_minimum_width_dirty = true;
  4163. Vector2 text_pos = Point2i(tbrect.position.x, tbrect.position.y + (tbrect.size.height - columns[i].text_buf->get_size().y) / 2);
  4164. switch (columns[i].title_alignment) {
  4165. case HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT: {
  4166. text_pos.x += cache.rtl ? tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x) : sb->get_offset().x;
  4167. break;
  4168. }
  4169. case HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT: {
  4170. text_pos.x += cache.rtl ? sb->get_offset().x : tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x);
  4171. break;
  4172. }
  4173. default: {
  4174. text_pos.x += (tbrect.size.width - columns[i].text_buf->get_size().x) / 2;
  4175. break;
  4176. }
  4177. }
  4178. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  4179. columns[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  4180. }
  4181. columns[i].text_buf->draw(ci, text_pos, theme_cache.title_button_color);
  4182. }
  4183. }
  4184. // Draw the focus outline last, so that it is drawn in front of the section headings.
  4185. // Otherwise, section heading backgrounds can appear to be in front of the focus outline when scrolling.
  4186. if (has_focus()) {
  4187. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  4188. theme_cache.focus_style->draw(ci, Rect2(Point2(), get_size()));
  4189. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  4190. }
  4191. } break;
  4192. case NOTIFICATION_THEME_CHANGED:
  4193. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  4194. case NOTIFICATION_TRANSLATION_CHANGED: {
  4195. _update_all();
  4196. } break;
  4197. case NOTIFICATION_RESIZED:
  4198. case NOTIFICATION_TRANSFORM_CHANGED: {
  4199. if (popup_edited_item != nullptr) {
  4200. Rect2 rect = _get_item_focus_rect(popup_edited_item);
  4201. popup_editor->set_position(get_global_position() + rect.position);
  4202. popup_editor->set_size(rect.size);
  4203. popup_editor->child_controls_changed();
  4204. }
  4205. } break;
  4206. }
  4207. }
  4208. void Tree::_update_all() {
  4209. for (int i = 0; i < columns.size(); i++) {
  4210. update_column(i);
  4211. }
  4212. if (root) {
  4213. update_item_cache(root);
  4214. }
  4215. }
  4216. Size2 Tree::get_minimum_size() const {
  4217. Vector2 min_size = Vector2(0, _get_title_button_height());
  4218. if (theme_cache.panel_style.is_valid()) {
  4219. min_size += theme_cache.panel_style->get_minimum_size();
  4220. }
  4221. Vector2 content_min_size = get_internal_min_size();
  4222. if (h_scroll_enabled) {
  4223. content_min_size.x = 0;
  4224. min_size.y += h_scroll->get_combined_minimum_size().height;
  4225. }
  4226. if (v_scroll_enabled) {
  4227. min_size.x += v_scroll->get_combined_minimum_size().width;
  4228. content_min_size.y = 0;
  4229. }
  4230. return min_size + content_min_size;
  4231. }
  4232. TreeItem *Tree::create_item(TreeItem *p_parent, int p_index) {
  4233. ERR_FAIL_COND_V(blocked > 0, nullptr);
  4234. TreeItem *ti = nullptr;
  4235. if (p_parent) {
  4236. ERR_FAIL_COND_V_MSG(p_parent->tree != this, nullptr, "A different tree owns the given parent");
  4237. ti = p_parent->create_child(p_index);
  4238. } else {
  4239. if (!root) {
  4240. // No root exists, make the given item the new root.
  4241. ti = memnew(TreeItem(this));
  4242. ERR_FAIL_NULL_V(ti, nullptr);
  4243. ti->cells.resize(columns.size());
  4244. ti->is_root = true;
  4245. root = ti;
  4246. } else {
  4247. // Root exists, append or insert to root.
  4248. ti = create_item(root, p_index);
  4249. }
  4250. }
  4251. _determine_hovered_item();
  4252. queue_accessibility_update();
  4253. return ti;
  4254. }
  4255. TreeItem *Tree::get_root() const {
  4256. return root;
  4257. }
  4258. TreeItem *Tree::get_last_item() const {
  4259. TreeItem *last = root;
  4260. while (last && last->last_child && !last->collapsed) {
  4261. last = last->last_child;
  4262. }
  4263. return last;
  4264. }
  4265. void Tree::item_edited(int p_column, TreeItem *p_item, MouseButton p_custom_mouse_index) {
  4266. edited_item = p_item;
  4267. edited_col = p_column;
  4268. if (p_item != nullptr && p_column >= 0 && p_column < p_item->cells.size()) {
  4269. edited_item->cells.write[p_column].dirty = true;
  4270. edited_item->cells.write[p_column].cached_minimum_size_dirty = true;
  4271. }
  4272. emit_signal(SNAME("item_edited"));
  4273. if (p_custom_mouse_index != MouseButton::NONE) {
  4274. emit_signal(SNAME("custom_item_clicked"), p_custom_mouse_index);
  4275. }
  4276. queue_accessibility_update();
  4277. }
  4278. void Tree::item_changed(int p_column, TreeItem *p_item) {
  4279. if (p_item != nullptr) {
  4280. if (p_column >= 0 && p_column < p_item->cells.size()) {
  4281. p_item->cells.write[p_column].dirty = true;
  4282. columns.write[p_column].cached_minimum_width_dirty = true;
  4283. } else if (p_column == -1) {
  4284. for (int i = 0; i < p_item->cells.size(); i++) {
  4285. p_item->cells.write[i].dirty = true;
  4286. columns.write[i].cached_minimum_width_dirty = true;
  4287. }
  4288. }
  4289. p_item->accessibility_row_dirty = true;
  4290. }
  4291. queue_accessibility_update();
  4292. queue_redraw();
  4293. }
  4294. void Tree::item_selected(int p_column, TreeItem *p_item) {
  4295. if (select_mode == SELECT_MULTI) {
  4296. if (!p_item->cells[p_column].selectable) {
  4297. return;
  4298. }
  4299. p_item->cells.write[p_column].selected = true;
  4300. //emit_signal(SNAME("multi_selected"),p_item,p_column,true); - NO this is for `TreeItem::select`
  4301. selected_col = p_column;
  4302. selected_item = p_item;
  4303. selected_button = -1;
  4304. } else {
  4305. select_single_item(p_item, root, p_column);
  4306. }
  4307. p_item->accessibility_row_dirty = true;
  4308. queue_accessibility_update();
  4309. queue_redraw();
  4310. }
  4311. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  4312. if (select_mode == SELECT_SINGLE && selected_item == p_item && selected_col == p_column) {
  4313. selected_item = nullptr;
  4314. selected_col = -1;
  4315. } else {
  4316. if (select_mode == SELECT_ROW && selected_item == p_item) {
  4317. selected_item = nullptr;
  4318. selected_col = -1;
  4319. } else {
  4320. if (select_mode == SELECT_MULTI) {
  4321. selected_item = p_item;
  4322. selected_col = p_column;
  4323. }
  4324. }
  4325. }
  4326. selected_button = -1;
  4327. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  4328. p_item->cells.write[p_column].selected = false;
  4329. } else if (select_mode == SELECT_ROW) {
  4330. for (int i = 0; i < p_item->cells.size(); i++) {
  4331. p_item->cells.write[i].selected = false;
  4332. }
  4333. }
  4334. p_item->accessibility_row_dirty = true;
  4335. queue_accessibility_update();
  4336. queue_redraw();
  4337. }
  4338. void Tree::set_select_mode(SelectMode p_mode) {
  4339. select_mode = p_mode;
  4340. }
  4341. Tree::SelectMode Tree::get_select_mode() const {
  4342. return select_mode;
  4343. }
  4344. void Tree::deselect_all() {
  4345. if (root) {
  4346. TreeItem *item = root;
  4347. while (item) {
  4348. if (select_mode == SELECT_ROW) {
  4349. item->deselect(0);
  4350. } else {
  4351. for (int i = 0; i < columns.size(); i++) {
  4352. item->deselect(i);
  4353. }
  4354. }
  4355. TreeItem *prev_item = item;
  4356. item = get_next_selected(root);
  4357. ERR_FAIL_COND(item == prev_item);
  4358. }
  4359. }
  4360. selected_item = nullptr;
  4361. selected_col = -1;
  4362. selected_button = -1;
  4363. queue_accessibility_update();
  4364. queue_redraw();
  4365. }
  4366. bool Tree::is_anything_selected() {
  4367. return (selected_item != nullptr);
  4368. }
  4369. void Tree::clear() {
  4370. ERR_FAIL_COND(blocked > 0);
  4371. if (pressing_for_editor) {
  4372. if (range_drag_enabled) {
  4373. range_drag_enabled = false;
  4374. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  4375. warp_mouse(range_drag_capture_pos);
  4376. }
  4377. pressing_for_editor = false;
  4378. }
  4379. if (root) {
  4380. memdelete(root);
  4381. root = nullptr;
  4382. };
  4383. selected_item = nullptr;
  4384. edited_item = nullptr;
  4385. popup_edited_item = nullptr;
  4386. popup_pressing_edited_item = nullptr;
  4387. _determine_hovered_item();
  4388. queue_accessibility_update();
  4389. queue_redraw();
  4390. }
  4391. void Tree::set_hide_root(bool p_enabled) {
  4392. if (hide_root == p_enabled) {
  4393. return;
  4394. }
  4395. hide_root = p_enabled;
  4396. queue_accessibility_update();
  4397. queue_redraw();
  4398. update_minimum_size();
  4399. }
  4400. bool Tree::is_root_hidden() const {
  4401. return hide_root;
  4402. }
  4403. void Tree::set_column_custom_minimum_width(int p_column, int p_min_width) {
  4404. ERR_FAIL_INDEX(p_column, columns.size());
  4405. if (columns[p_column].custom_min_width == p_min_width) {
  4406. return;
  4407. }
  4408. if (p_min_width < 0) {
  4409. return;
  4410. }
  4411. columns.write[p_column].custom_min_width = p_min_width;
  4412. columns.write[p_column].cached_minimum_width_dirty = true;
  4413. queue_accessibility_update();
  4414. queue_redraw();
  4415. }
  4416. void Tree::set_column_expand(int p_column, bool p_expand) {
  4417. ERR_FAIL_INDEX(p_column, columns.size());
  4418. if (columns[p_column].expand == p_expand) {
  4419. return;
  4420. }
  4421. columns.write[p_column].expand = p_expand;
  4422. columns.write[p_column].cached_minimum_width_dirty = true;
  4423. queue_accessibility_update();
  4424. queue_redraw();
  4425. }
  4426. void Tree::set_column_expand_ratio(int p_column, int p_ratio) {
  4427. ERR_FAIL_INDEX(p_column, columns.size());
  4428. if (columns[p_column].expand_ratio == p_ratio) {
  4429. return;
  4430. }
  4431. columns.write[p_column].expand_ratio = p_ratio;
  4432. columns.write[p_column].cached_minimum_width_dirty = true;
  4433. queue_accessibility_update();
  4434. queue_redraw();
  4435. }
  4436. void Tree::set_column_clip_content(int p_column, bool p_fit) {
  4437. ERR_FAIL_INDEX(p_column, columns.size());
  4438. if (columns[p_column].clip_content == p_fit) {
  4439. return;
  4440. }
  4441. columns.write[p_column].clip_content = p_fit;
  4442. columns.write[p_column].cached_minimum_width_dirty = true;
  4443. queue_accessibility_update();
  4444. queue_redraw();
  4445. }
  4446. bool Tree::is_column_expanding(int p_column) const {
  4447. ERR_FAIL_INDEX_V(p_column, columns.size(), false);
  4448. return columns[p_column].expand;
  4449. }
  4450. int Tree::get_column_expand_ratio(int p_column) const {
  4451. ERR_FAIL_INDEX_V(p_column, columns.size(), 1);
  4452. return columns[p_column].expand_ratio;
  4453. }
  4454. bool Tree::is_column_clipping_content(int p_column) const {
  4455. ERR_FAIL_INDEX_V(p_column, columns.size(), false);
  4456. return columns[p_column].clip_content;
  4457. }
  4458. TreeItem *Tree::get_selected() const {
  4459. return selected_item;
  4460. }
  4461. void Tree::set_selected(TreeItem *p_item, int p_column) {
  4462. ERR_FAIL_INDEX(p_column, columns.size());
  4463. ERR_FAIL_NULL(p_item);
  4464. ERR_FAIL_COND_MSG(p_item->get_tree() != this, "The provided TreeItem does not belong to this Tree. Ensure that the TreeItem is a part of the Tree before setting it as selected.");
  4465. select_single_item(p_item, get_root(), p_column);
  4466. }
  4467. int Tree::get_selected_column() const {
  4468. return selected_col;
  4469. }
  4470. TreeItem *Tree::get_edited() const {
  4471. return edited_item;
  4472. }
  4473. int Tree::get_edited_column() const {
  4474. return edited_col;
  4475. }
  4476. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  4477. if (!root) {
  4478. return nullptr;
  4479. }
  4480. while (true) {
  4481. if (!p_item) {
  4482. p_item = root;
  4483. } else {
  4484. if (p_item->first_child) {
  4485. p_item = p_item->first_child;
  4486. } else if (p_item->next) {
  4487. p_item = p_item->next;
  4488. } else {
  4489. while (!p_item->next) {
  4490. p_item = p_item->parent;
  4491. if (p_item == nullptr) {
  4492. return nullptr;
  4493. }
  4494. }
  4495. p_item = p_item->next;
  4496. }
  4497. }
  4498. for (int i = 0; i < columns.size(); i++) {
  4499. if (p_item->cells[i].selected) {
  4500. return p_item;
  4501. }
  4502. }
  4503. }
  4504. return nullptr;
  4505. }
  4506. int Tree::get_column_minimum_width(int p_column) const {
  4507. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  4508. if (columns[p_column].cached_minimum_width_dirty) {
  4509. // Use the custom minimum width.
  4510. int min_width = columns[p_column].custom_min_width;
  4511. // Check if the visible title of the column is wider.
  4512. if (show_column_titles) {
  4513. const float padding = theme_cache.title_button->get_margin(SIDE_LEFT) + theme_cache.title_button->get_margin(SIDE_RIGHT);
  4514. min_width = MAX(theme_cache.font->get_string_size(columns[p_column].xl_title, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + padding, min_width);
  4515. }
  4516. if (root && !columns[p_column].clip_content) {
  4517. int depth = 1;
  4518. TreeItem *last = nullptr;
  4519. TreeItem *first = hide_root ? root->get_next_visible() : root;
  4520. for (TreeItem *item = first; item; last = item, item = item->get_next_visible()) {
  4521. // Get column indentation.
  4522. int indent;
  4523. if (p_column == 0) {
  4524. if (last) {
  4525. if (item->parent == last) {
  4526. depth += 1;
  4527. } else if (item->parent != last->parent) {
  4528. depth = hide_root ? 0 : 1;
  4529. for (TreeItem *iter = item->parent; iter; iter = iter->parent) {
  4530. depth += 1;
  4531. }
  4532. }
  4533. }
  4534. indent = theme_cache.item_margin * depth;
  4535. } else {
  4536. indent = theme_cache.h_separation;
  4537. }
  4538. // Get the item minimum size.
  4539. Size2 item_size = item->get_minimum_size(p_column);
  4540. item_size.width += indent;
  4541. // Check if the item is wider.
  4542. min_width = MAX(min_width, item_size.width);
  4543. }
  4544. }
  4545. columns.get(p_column).cached_minimum_width = min_width;
  4546. columns.get(p_column).cached_minimum_width_dirty = false;
  4547. }
  4548. return columns[p_column].cached_minimum_width;
  4549. }
  4550. int Tree::get_column_width(int p_column) const {
  4551. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  4552. int column_width = get_column_minimum_width(p_column);
  4553. if (columns[p_column].expand) {
  4554. int expand_area = _get_content_rect().size.width;
  4555. int expanding_total = 0;
  4556. for (int i = 0; i < columns.size(); i++) {
  4557. expand_area -= get_column_minimum_width(i);
  4558. if (columns[i].expand) {
  4559. expanding_total += columns[i].expand_ratio;
  4560. }
  4561. }
  4562. if (expand_area >= expanding_total && expanding_total > 0) {
  4563. column_width += expand_area * columns[p_column].expand_ratio / expanding_total;
  4564. }
  4565. }
  4566. return column_width;
  4567. }
  4568. void Tree::propagate_set_columns(TreeItem *p_item) {
  4569. p_item->cells.resize(columns.size());
  4570. p_item->accessibility_row_dirty = true;
  4571. for (TreeItem::Cell &cell : p_item->cells) {
  4572. if (cell.accessibility_cell_element.is_valid()) {
  4573. DisplayServer::get_singleton()->accessibility_free_element(cell.accessibility_cell_element);
  4574. cell.accessibility_cell_element = RID();
  4575. }
  4576. for (TreeItem::Cell::Button &btn : cell.buttons) {
  4577. if (btn.accessibility_button_element.is_valid()) {
  4578. DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
  4579. btn.accessibility_button_element = RID();
  4580. }
  4581. }
  4582. }
  4583. TreeItem *c = p_item->get_first_child();
  4584. while (c) {
  4585. propagate_set_columns(c);
  4586. c = c->next;
  4587. }
  4588. }
  4589. void Tree::set_columns(int p_columns) {
  4590. ERR_FAIL_COND(p_columns < 1);
  4591. ERR_FAIL_COND(blocked > 0);
  4592. if (columns.size() > p_columns) {
  4593. for (int i = p_columns; i < columns.size(); i++) {
  4594. if (columns[i].accessibility_col_element.is_valid()) {
  4595. DisplayServer::get_singleton()->accessibility_free_element(columns[i].accessibility_col_element);
  4596. columns.write[i].accessibility_col_element = RID();
  4597. }
  4598. }
  4599. }
  4600. columns.resize(p_columns);
  4601. if (root) {
  4602. propagate_set_columns(root);
  4603. }
  4604. if (selected_col >= p_columns) {
  4605. selected_col = p_columns - 1;
  4606. selected_button = -1;
  4607. }
  4608. queue_accessibility_update();
  4609. queue_redraw();
  4610. }
  4611. int Tree::get_columns() const {
  4612. return columns.size();
  4613. }
  4614. void Tree::_scroll_moved(float) {
  4615. _determine_hovered_item();
  4616. queue_redraw();
  4617. }
  4618. Rect2 Tree::get_custom_popup_rect() const {
  4619. return custom_popup_rect;
  4620. }
  4621. int Tree::get_item_offset(TreeItem *p_item) const {
  4622. TreeItem *it = root;
  4623. int ofs = _get_title_button_height();
  4624. if (!it) {
  4625. return 0;
  4626. }
  4627. while (true) {
  4628. if (it == p_item) {
  4629. return ofs;
  4630. }
  4631. if ((it != root || !hide_root) && it->is_visible_in_tree()) {
  4632. ofs += compute_item_height(it);
  4633. ofs += theme_cache.v_separation;
  4634. }
  4635. if (it->first_child && !it->collapsed) {
  4636. it = it->first_child;
  4637. } else if (it->next) {
  4638. it = it->next;
  4639. } else {
  4640. while (!it->next) {
  4641. it = it->parent;
  4642. if (it == nullptr) {
  4643. return 0;
  4644. }
  4645. }
  4646. it = it->next;
  4647. }
  4648. }
  4649. return -1; // Not found.
  4650. }
  4651. void Tree::ensure_cursor_is_visible() {
  4652. if (!is_inside_tree()) {
  4653. return;
  4654. }
  4655. if (!selected_item || (selected_col == -1)) {
  4656. return; // Nothing under cursor.
  4657. }
  4658. // Note: Code below similar to `Tree::scroll_to_item()`, in case of bug fix both.
  4659. const Size2 area_size = _get_content_rect().size;
  4660. int y_offset = get_item_offset(selected_item);
  4661. if (y_offset != -1) {
  4662. const int tbh = _get_title_button_height();
  4663. y_offset -= tbh;
  4664. const int cell_h = compute_item_height(selected_item) + theme_cache.v_separation;
  4665. int screen_h = area_size.height - tbh;
  4666. if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
  4667. v_scroll->set_value(y_offset);
  4668. } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
  4669. callable_mp((Range *)v_scroll, &Range::set_value).call_deferred(y_offset - screen_h + cell_h);
  4670. } else if (y_offset < v_scroll->get_value()) {
  4671. v_scroll->set_value(y_offset);
  4672. }
  4673. }
  4674. if (select_mode != SELECT_ROW) { // Cursor always at column 0 in this mode.
  4675. int x_offset = 0;
  4676. for (int i = 0; i < selected_col; i++) {
  4677. x_offset += get_column_width(i);
  4678. }
  4679. const int cell_w = get_column_width(selected_col);
  4680. const int screen_w = area_size.width;
  4681. if (cell_w > screen_w) {
  4682. h_scroll->set_value(x_offset);
  4683. } else if (x_offset + cell_w > h_scroll->get_value() + screen_w) {
  4684. callable_mp((Range *)h_scroll, &Range::set_value).call_deferred(x_offset - screen_w + cell_w);
  4685. } else if (x_offset < h_scroll->get_value()) {
  4686. h_scroll->set_value(x_offset);
  4687. }
  4688. }
  4689. queue_accessibility_update();
  4690. }
  4691. int Tree::get_pressed_button() const {
  4692. return pressed_button;
  4693. }
  4694. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column, int p_button) const {
  4695. ERR_FAIL_NULL_V(p_item, Rect2());
  4696. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  4697. if (p_column != -1) {
  4698. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  4699. }
  4700. if (p_button != -1) {
  4701. ERR_FAIL_COND_V(p_column == -1, Rect2()); // Pass a column if you want to pass a button.
  4702. ERR_FAIL_INDEX_V(p_button, p_item->cells[p_column].buttons.size(), Rect2());
  4703. }
  4704. int ofs = get_item_offset(p_item);
  4705. int height = compute_item_height(p_item) + theme_cache.v_separation;
  4706. Rect2 r;
  4707. r.position.y = ofs - theme_cache.offset.y + theme_cache.panel_style->get_offset().y;
  4708. r.size.height = height;
  4709. bool rtl = is_layout_rtl();
  4710. const Rect2 content_rect = _get_content_rect();
  4711. if (p_column == -1) {
  4712. r.position.x = 0;
  4713. r.size.x = get_size().width;
  4714. } else {
  4715. int accum = 0;
  4716. for (int i = 0; i < p_column; i++) {
  4717. accum += get_column_width(i);
  4718. }
  4719. r.position.x = (rtl) ? get_size().x - (accum - theme_cache.offset.x) - get_column_width(p_column) - theme_cache.panel_style->get_margin(SIDE_LEFT) : accum - theme_cache.offset.x + theme_cache.panel_style->get_margin(SIDE_LEFT);
  4720. r.size.x = get_column_width(p_column);
  4721. if (p_button != -1) {
  4722. // RTL direction support for button rect is different due to buttons not
  4723. // having same behavior as they do in LTR when tree is scrolled.
  4724. const TreeItem::Cell &c = p_item->cells[p_column];
  4725. Vector2 ofst = Vector2(r.position.x + r.size.x + theme_cache.button_margin, r.position.y);
  4726. // Compute total width of buttons block including spacings.
  4727. int buttons_width = 0;
  4728. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  4729. Ref<Texture2D> b = c.buttons[j].texture;
  4730. Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  4731. buttons_width += size.width + theme_cache.button_margin;
  4732. }
  4733. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  4734. Ref<Texture2D> b = c.buttons[j].texture;
  4735. Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  4736. ofst.x -= size.x + theme_cache.button_margin;
  4737. if (rtl) {
  4738. if (j == p_button) {
  4739. return Rect2(r.position, Size2(size.x, r.size.y));
  4740. }
  4741. r.position.x += size.x + theme_cache.button_margin;
  4742. continue;
  4743. }
  4744. if (j == p_button) {
  4745. float content_rect_end_x = content_rect.position.x + content_rect.size.width;
  4746. if (r.position.x + r.size.x < content_rect_end_x) {
  4747. return Rect2(ofst, Size2(size.x, r.size.y));
  4748. }
  4749. // Button block can be outside of `content_rect`.
  4750. if (content_rect_end_x - (r.position.x + theme_cache.h_separation) < buttons_width) {
  4751. return Rect2(r.position + Point2(theme_cache.h_separation + (buttons_width - ((r.position.x + r.size.x) - ofst.x)), 0), Size2(size.x, r.size.y));
  4752. }
  4753. return Rect2(ofst - Vector2((r.position.x + r.size.x) - content_rect_end_x, 0), Size2(size.x, r.size.y));
  4754. }
  4755. }
  4756. }
  4757. }
  4758. return r;
  4759. }
  4760. void Tree::set_column_titles_visible(bool p_show) {
  4761. if (show_column_titles == p_show) {
  4762. return;
  4763. }
  4764. show_column_titles = p_show;
  4765. queue_accessibility_update();
  4766. queue_redraw();
  4767. update_minimum_size();
  4768. }
  4769. bool Tree::are_column_titles_visible() const {
  4770. return show_column_titles;
  4771. }
  4772. void Tree::set_column_title(int p_column, const String &p_title) {
  4773. ERR_FAIL_INDEX(p_column, columns.size());
  4774. if (columns[p_column].title == p_title) {
  4775. return;
  4776. }
  4777. columns.write[p_column].title = p_title;
  4778. columns.write[p_column].xl_title = atr(p_title);
  4779. update_column(p_column);
  4780. queue_accessibility_update();
  4781. queue_redraw();
  4782. }
  4783. String Tree::get_column_title(int p_column) const {
  4784. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  4785. return columns[p_column].title;
  4786. }
  4787. void Tree::set_column_title_alignment(int p_column, HorizontalAlignment p_alignment) {
  4788. ERR_FAIL_INDEX(p_column, columns.size());
  4789. if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  4790. WARN_PRINT("HORIZONTAL_ALIGNMENT_FILL is not supported for column titles.");
  4791. }
  4792. if (columns[p_column].title_alignment == p_alignment) {
  4793. return;
  4794. }
  4795. columns.write[p_column].title_alignment = p_alignment;
  4796. update_column(p_column);
  4797. queue_accessibility_update();
  4798. queue_redraw();
  4799. }
  4800. HorizontalAlignment Tree::get_column_title_alignment(int p_column) const {
  4801. ERR_FAIL_INDEX_V(p_column, columns.size(), HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  4802. return columns[p_column].title_alignment;
  4803. }
  4804. void Tree::set_column_title_direction(int p_column, Control::TextDirection p_text_direction) {
  4805. ERR_FAIL_INDEX(p_column, columns.size());
  4806. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  4807. if (columns[p_column].text_direction != p_text_direction) {
  4808. columns.write[p_column].text_direction = p_text_direction;
  4809. update_column(p_column);
  4810. queue_accessibility_update();
  4811. queue_redraw();
  4812. }
  4813. }
  4814. Control::TextDirection Tree::get_column_title_direction(int p_column) const {
  4815. ERR_FAIL_INDEX_V(p_column, columns.size(), TEXT_DIRECTION_INHERITED);
  4816. return columns[p_column].text_direction;
  4817. }
  4818. void Tree::set_column_title_language(int p_column, const String &p_language) {
  4819. ERR_FAIL_INDEX(p_column, columns.size());
  4820. if (columns[p_column].language != p_language) {
  4821. columns.write[p_column].language = p_language;
  4822. update_column(p_column);
  4823. queue_accessibility_update();
  4824. queue_redraw();
  4825. }
  4826. }
  4827. String Tree::get_column_title_language(int p_column) const {
  4828. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  4829. return columns[p_column].language;
  4830. }
  4831. Point2 Tree::get_scroll() const {
  4832. Point2 ofs;
  4833. if (h_scroll->is_visible_in_tree()) {
  4834. ofs.x = h_scroll->get_value();
  4835. }
  4836. if (v_scroll->is_visible_in_tree()) {
  4837. ofs.y = v_scroll->get_value();
  4838. }
  4839. return ofs;
  4840. }
  4841. void Tree::scroll_to_item(TreeItem *p_item, bool p_center_on_item) {
  4842. ERR_FAIL_NULL(p_item);
  4843. update_scrollbars();
  4844. // Note: Code below similar to `Tree::ensure_cursor_is_visible()`, in case of bug fix both.
  4845. const Size2 area_size = _get_content_rect().size;
  4846. int y_offset = get_item_offset(p_item);
  4847. if (y_offset != -1) {
  4848. const int title_button_height = _get_title_button_height();
  4849. y_offset -= title_button_height;
  4850. const int cell_h = compute_item_height(p_item) + theme_cache.v_separation;
  4851. int screen_h = area_size.height - title_button_height;
  4852. if (p_center_on_item) {
  4853. // This makes sure that centering the offset doesn't overflow.
  4854. const double v_scroll_value = y_offset - MAX((screen_h - cell_h) / 2.0, 0.0);
  4855. v_scroll->set_value(v_scroll_value);
  4856. } else {
  4857. if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
  4858. v_scroll->set_value(y_offset);
  4859. } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
  4860. v_scroll->set_value(y_offset - screen_h + cell_h);
  4861. } else if (y_offset < v_scroll->get_value()) {
  4862. v_scroll->set_value(y_offset);
  4863. }
  4864. }
  4865. }
  4866. queue_accessibility_update();
  4867. }
  4868. void Tree::set_h_scroll_enabled(bool p_enable) {
  4869. if (h_scroll_enabled == p_enable) {
  4870. return;
  4871. }
  4872. h_scroll_enabled = p_enable;
  4873. update_minimum_size();
  4874. }
  4875. bool Tree::is_h_scroll_enabled() const {
  4876. return h_scroll_enabled;
  4877. }
  4878. void Tree::set_v_scroll_enabled(bool p_enable) {
  4879. if (v_scroll_enabled == p_enable) {
  4880. return;
  4881. }
  4882. v_scroll_enabled = p_enable;
  4883. update_minimum_size();
  4884. }
  4885. bool Tree::is_v_scroll_enabled() const {
  4886. return v_scroll_enabled;
  4887. }
  4888. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  4889. TreeItem *from = p_at;
  4890. TreeItem *loop = nullptr; // Safe-guard against infinite loop.
  4891. while (p_at) {
  4892. for (int i = 0; i < columns.size(); i++) {
  4893. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  4894. if (r_col) {
  4895. *r_col = i;
  4896. }
  4897. return p_at;
  4898. }
  4899. }
  4900. if (p_backwards) {
  4901. p_at = p_at->get_prev_visible(true);
  4902. } else {
  4903. p_at = p_at->get_next_visible(true);
  4904. }
  4905. if ((p_at) == from) {
  4906. break;
  4907. }
  4908. if (!loop) {
  4909. loop = p_at;
  4910. } else if (loop == p_at) {
  4911. break;
  4912. }
  4913. }
  4914. return nullptr;
  4915. }
  4916. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  4917. TreeItem *from = get_selected();
  4918. if (!from) {
  4919. from = root;
  4920. }
  4921. if (!from) {
  4922. return nullptr;
  4923. }
  4924. return _search_item_text(from->get_next_visible(true), p_find, r_col, p_selectable);
  4925. }
  4926. TreeItem *Tree::get_item_with_text(const String &p_find) const {
  4927. for (TreeItem *current = root; current; current = current->get_next_visible()) {
  4928. for (int i = 0; i < columns.size(); i++) {
  4929. if (current->get_text(i) == p_find) {
  4930. return current;
  4931. }
  4932. }
  4933. }
  4934. return nullptr;
  4935. }
  4936. TreeItem *Tree::get_item_with_metadata(const Variant &p_find, int p_column) const {
  4937. if (p_column < 0) {
  4938. for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
  4939. for (int i = 0; i < columns.size(); i++) {
  4940. if (current->get_metadata(i) == p_find) {
  4941. return current;
  4942. }
  4943. }
  4944. }
  4945. return nullptr;
  4946. }
  4947. for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
  4948. if (current->get_metadata(p_column) == p_find) {
  4949. return current;
  4950. }
  4951. }
  4952. return nullptr;
  4953. }
  4954. void Tree::_do_incr_search(const String &p_add) {
  4955. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // Convert to msec.
  4956. uint64_t diff = time - last_keypress;
  4957. if (diff > uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec"))) {
  4958. incr_search = p_add;
  4959. } else if (incr_search != p_add) {
  4960. incr_search += p_add;
  4961. }
  4962. last_keypress = time;
  4963. int col;
  4964. TreeItem *item = search_item_text(incr_search, &col, true);
  4965. if (!item) {
  4966. return;
  4967. }
  4968. if (select_mode == SELECT_MULTI) {
  4969. item->set_as_cursor(col);
  4970. } else {
  4971. item->select(col);
  4972. }
  4973. ensure_cursor_is_visible();
  4974. }
  4975. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &r_height, int &r_section) const {
  4976. r_column = -1;
  4977. r_height = 0;
  4978. r_section = -100;
  4979. if (!root) {
  4980. return nullptr;
  4981. }
  4982. Point2 pos = p_pos;
  4983. if ((root != p_item || !hide_root) && p_item->is_visible_in_tree()) {
  4984. r_height = compute_item_height(p_item) + theme_cache.v_separation;
  4985. if (pos.y < r_height) {
  4986. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  4987. r_section = 0;
  4988. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  4989. r_section = pos.y < r_height / 2 ? -1 : 1;
  4990. } else if (pos.y < r_height / 4) {
  4991. r_section = -1;
  4992. } else if (pos.y >= (r_height * 3 / 4)) {
  4993. r_section = 1;
  4994. } else {
  4995. r_section = 0;
  4996. }
  4997. for (int i = 0; i < columns.size(); i++) {
  4998. int w = get_column_width(i);
  4999. if (pos.x < w) {
  5000. r_column = i;
  5001. return p_item;
  5002. }
  5003. pos.x -= w;
  5004. }
  5005. return nullptr;
  5006. } else {
  5007. pos.y -= r_height;
  5008. }
  5009. } else {
  5010. r_height = 0;
  5011. }
  5012. if (p_item->is_collapsed() || !p_item->is_visible_in_tree()) {
  5013. return nullptr; // Do not try children, it's collapsed.
  5014. }
  5015. TreeItem *n = p_item->get_first_child();
  5016. while (n) {
  5017. int ch;
  5018. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, r_section);
  5019. pos.y -= ch;
  5020. r_height += ch;
  5021. if (r) {
  5022. return r;
  5023. }
  5024. n = n->get_next();
  5025. }
  5026. return nullptr;
  5027. }
  5028. void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index, int &r_section) const {
  5029. // When on a button, `r_index` is valid.
  5030. // When on an item, both `r_item` and `r_column` are valid.
  5031. // Otherwise, all output arguments are invalid.
  5032. r_item = nullptr;
  5033. r_column = -1;
  5034. r_index = -1;
  5035. r_section = -1;
  5036. if (!root) {
  5037. return;
  5038. }
  5039. Point2 pos = p_pos;
  5040. if (cache.rtl) {
  5041. pos.x = get_size().width - pos.x - 1;
  5042. }
  5043. pos -= theme_cache.panel_style->get_offset();
  5044. pos.y -= _get_title_button_height();
  5045. if (pos.y < 0) {
  5046. return;
  5047. }
  5048. pos += theme_cache.offset; // Scrolling.
  5049. int x_limit = _get_content_rect().size.x + theme_cache.offset.width;
  5050. if (pos.x > x_limit) {
  5051. // Inside the scroll area.
  5052. return;
  5053. }
  5054. int col, h, section;
  5055. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5056. if (!it) {
  5057. return;
  5058. }
  5059. r_item = it;
  5060. r_column = col;
  5061. r_section = section;
  5062. const TreeItem::Cell &c = it->cells[col];
  5063. if (c.buttons.is_empty()) {
  5064. return;
  5065. }
  5066. // Search the button index.
  5067. FindColumnButtonResult result = _find_column_and_button_at_pos(pos.x, it, 0, x_limit);
  5068. r_index = result.button_index;
  5069. }
  5070. Tree::FindColumnButtonResult Tree::_find_column_and_button_at_pos(int p_x, const TreeItem *p_item, int p_x_ofs, int p_x_limit) const {
  5071. int x = p_x;
  5072. int col = -1;
  5073. int col_ofs = 0;
  5074. int col_width = 0;
  5075. int limit_w = p_x_limit;
  5076. FindColumnButtonResult result;
  5077. for (int i = 0; i < columns.size(); i++) {
  5078. col_width = get_column_width(i);
  5079. if (p_item->cells[i].expand_right) {
  5080. int plus = 1;
  5081. while (i + plus < columns.size() && !p_item->cells[i + plus].editable && p_item->cells[i + plus].mode == TreeItem::CELL_MODE_STRING && p_item->cells[i + plus].text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
  5082. col_width += theme_cache.h_separation;
  5083. col_width += get_column_width(i + plus);
  5084. plus++;
  5085. }
  5086. }
  5087. if (x < col_width) {
  5088. col = i;
  5089. break;
  5090. }
  5091. col_ofs += col_width;
  5092. x -= col_width;
  5093. limit_w -= col_width;
  5094. }
  5095. if (col >= 0) {
  5096. if (col == 0) {
  5097. int margin = p_x_ofs + theme_cache.item_margin;
  5098. col_width -= margin;
  5099. limit_w -= margin;
  5100. col_ofs += margin;
  5101. x -= margin;
  5102. } else {
  5103. col_width -= theme_cache.h_separation;
  5104. limit_w -= theme_cache.h_separation;
  5105. x -= theme_cache.h_separation;
  5106. }
  5107. if (!cache.rtl && !p_item->cells[col].buttons.is_empty()) {
  5108. int button_w = 0;
  5109. for (int j = p_item->cells[col].buttons.size() - 1; j >= 0; j--) {
  5110. const Ref<Texture2D> &b = p_item->cells[col].buttons[j].texture;
  5111. button_w += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
  5112. }
  5113. col_width = MAX(button_w, MIN(limit_w, col_width));
  5114. }
  5115. // Cell button detection code.
  5116. // The first half of the button margin will be applied to the left button,
  5117. // and the other half to the right button.
  5118. const int offset_button_width = theme_cache.button_margin / 2;
  5119. const TreeItem::Cell &c = p_item->cells[col];
  5120. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  5121. const Ref<Texture2D> &b = c.buttons[j].texture;
  5122. int w = b->get_size().width + theme_cache.button_pressed->get_minimum_size().width;
  5123. if (x >= col_width - w - offset_button_width - 1) {
  5124. result.button_index = j;
  5125. break;
  5126. }
  5127. col_width -= w + theme_cache.button_margin;
  5128. }
  5129. }
  5130. result.column_index = col;
  5131. result.column_offset = col_ofs;
  5132. result.column_width = col_width;
  5133. result.pos_x = x;
  5134. return result;
  5135. }
  5136. int Tree::get_column_at_position(const Point2 &p_pos) const {
  5137. if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5138. return -1;
  5139. }
  5140. Point2 pos = p_pos;
  5141. if (is_layout_rtl()) {
  5142. pos.x = get_size().width - pos.x - 1;
  5143. }
  5144. pos -= theme_cache.panel_style->get_offset();
  5145. pos.y -= _get_title_button_height();
  5146. if (pos.y < 0) {
  5147. return -1;
  5148. }
  5149. if (h_scroll->is_visible_in_tree()) {
  5150. pos.x += h_scroll->get_value();
  5151. }
  5152. if (v_scroll->is_visible_in_tree()) {
  5153. pos.y += v_scroll->get_value();
  5154. }
  5155. int col, h, section;
  5156. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5157. if (it) {
  5158. return col;
  5159. }
  5160. return -1;
  5161. }
  5162. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  5163. if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5164. return -100;
  5165. }
  5166. Point2 pos = p_pos;
  5167. if (is_layout_rtl()) {
  5168. pos.x = get_size().width - pos.x - 1;
  5169. }
  5170. pos -= theme_cache.panel_style->get_offset();
  5171. pos.y -= _get_title_button_height();
  5172. if (pos.y < 0) {
  5173. return -100;
  5174. }
  5175. if (h_scroll->is_visible_in_tree()) {
  5176. pos.x += h_scroll->get_value();
  5177. }
  5178. if (v_scroll->is_visible_in_tree()) {
  5179. pos.y += v_scroll->get_value();
  5180. }
  5181. int col, h, section;
  5182. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5183. if (it) {
  5184. return section;
  5185. }
  5186. return -100;
  5187. }
  5188. bool Tree::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  5189. if (drag_touching) {
  5190. // Disable data drag & drop when touch dragging.
  5191. return false;
  5192. }
  5193. return Control::can_drop_data(p_point, p_data);
  5194. }
  5195. Variant Tree::get_drag_data(const Point2 &p_point) {
  5196. if (drag_touching) {
  5197. // Disable data drag & drop when touch dragging.
  5198. return Variant();
  5199. }
  5200. return Control::get_drag_data(p_point);
  5201. }
  5202. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  5203. if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5204. return nullptr;
  5205. }
  5206. Point2 pos = p_pos;
  5207. if (is_layout_rtl()) {
  5208. pos.x = get_size().width - pos.x - 1;
  5209. }
  5210. pos -= theme_cache.panel_style->get_offset();
  5211. pos.y -= _get_title_button_height();
  5212. if (pos.y < 0) {
  5213. return nullptr;
  5214. }
  5215. if (h_scroll->is_visible_in_tree()) {
  5216. pos.x += h_scroll->get_value();
  5217. }
  5218. if (v_scroll->is_visible_in_tree()) {
  5219. pos.y += v_scroll->get_value();
  5220. }
  5221. int col, h, section;
  5222. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5223. if (it) {
  5224. return it;
  5225. }
  5226. return nullptr;
  5227. }
  5228. int Tree::get_button_id_at_position(const Point2 &p_pos) const {
  5229. if (!root) {
  5230. return -1;
  5231. }
  5232. if (p_pos == Vector2(Math::INF, Math::INF)) {
  5233. if (selected_item && selected_button >= 0) {
  5234. return selected_item->cells[selected_col].buttons[selected_button].id;
  5235. }
  5236. } else {
  5237. if (!Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5238. return -1;
  5239. }
  5240. TreeItem *it;
  5241. int col, index, section;
  5242. _find_button_at_pos(p_pos, it, col, index, section);
  5243. if (index == -1) {
  5244. return -1;
  5245. }
  5246. return it->cells[col].buttons[index].id;
  5247. }
  5248. return -1;
  5249. }
  5250. String Tree::get_tooltip(const Point2 &p_pos) const {
  5251. Point2 pos = p_pos - theme_cache.panel_style->get_offset();
  5252. pos.y -= _get_title_button_height();
  5253. if (pos.y < 0) {
  5254. return Control::get_tooltip(p_pos);
  5255. }
  5256. TreeItem *it;
  5257. int col, index, section;
  5258. _find_button_at_pos(p_pos, it, col, index, section);
  5259. if (index != -1) {
  5260. return it->cells[col].buttons[index].tooltip;
  5261. }
  5262. if (it) {
  5263. const String item_tooltip = it->get_tooltip_text(col);
  5264. if (enable_auto_tooltip && item_tooltip.is_empty()) {
  5265. return it->get_text(col);
  5266. }
  5267. return item_tooltip;
  5268. }
  5269. return Control::get_tooltip(p_pos);
  5270. }
  5271. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  5272. cursor_can_exit_tree = p_enable;
  5273. }
  5274. void Tree::set_hide_folding(bool p_hide) {
  5275. if (hide_folding == p_hide) {
  5276. return;
  5277. }
  5278. hide_folding = p_hide;
  5279. queue_redraw();
  5280. }
  5281. bool Tree::is_folding_hidden() const {
  5282. return hide_folding;
  5283. }
  5284. void Tree::set_enable_recursive_folding(bool p_enable) {
  5285. enable_recursive_folding = p_enable;
  5286. }
  5287. bool Tree::is_recursive_folding_enabled() const {
  5288. return enable_recursive_folding;
  5289. }
  5290. void Tree::set_drop_mode_flags(int p_flags) {
  5291. if (drop_mode_flags == p_flags) {
  5292. return;
  5293. }
  5294. drop_mode_flags = p_flags;
  5295. if (drop_mode_flags == 0) {
  5296. drop_mode_over = nullptr;
  5297. }
  5298. queue_redraw();
  5299. }
  5300. int Tree::get_drop_mode_flags() const {
  5301. return drop_mode_flags;
  5302. }
  5303. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  5304. force_edit_checkbox_only_on_checkbox = p_enable;
  5305. }
  5306. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  5307. return force_edit_checkbox_only_on_checkbox;
  5308. }
  5309. void Tree::set_allow_rmb_select(bool p_allow) {
  5310. allow_rmb_select = p_allow;
  5311. }
  5312. bool Tree::get_allow_rmb_select() const {
  5313. return allow_rmb_select;
  5314. }
  5315. void Tree::set_allow_reselect(bool p_allow) {
  5316. allow_reselect = p_allow;
  5317. }
  5318. bool Tree::get_allow_reselect() const {
  5319. return allow_reselect;
  5320. }
  5321. void Tree::set_allow_search(bool p_allow) {
  5322. allow_search = p_allow;
  5323. }
  5324. bool Tree::get_allow_search() const {
  5325. return allow_search;
  5326. }
  5327. void Tree::set_auto_tooltip(bool p_enable) {
  5328. enable_auto_tooltip = p_enable;
  5329. }
  5330. bool Tree::is_auto_tooltip_enabled() const {
  5331. return enable_auto_tooltip;
  5332. }
  5333. void Tree::_bind_methods() {
  5334. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  5335. ClassDB::bind_method(D_METHOD("create_item", "parent", "index"), &Tree::create_item, DEFVAL(Variant()), DEFVAL(-1));
  5336. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  5337. ClassDB::bind_method(D_METHOD("set_column_custom_minimum_width", "column", "min_width"), &Tree::set_column_custom_minimum_width);
  5338. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  5339. ClassDB::bind_method(D_METHOD("set_column_expand_ratio", "column", "ratio"), &Tree::set_column_expand_ratio);
  5340. ClassDB::bind_method(D_METHOD("set_column_clip_content", "column", "enable"), &Tree::set_column_clip_content);
  5341. ClassDB::bind_method(D_METHOD("is_column_expanding", "column"), &Tree::is_column_expanding);
  5342. ClassDB::bind_method(D_METHOD("is_column_clipping_content", "column"), &Tree::is_column_clipping_content);
  5343. ClassDB::bind_method(D_METHOD("get_column_expand_ratio", "column"), &Tree::get_column_expand_ratio);
  5344. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  5345. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  5346. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  5347. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::get_next_selected);
  5348. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  5349. ClassDB::bind_method(D_METHOD("set_selected", "item", "column"), &Tree::set_selected);
  5350. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  5351. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  5352. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  5353. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  5354. ClassDB::bind_method(D_METHOD("deselect_all"), &Tree::deselect_all);
  5355. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  5356. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  5357. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  5358. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  5359. ClassDB::bind_method(D_METHOD("edit_selected", "force_edit"), &Tree::edit_selected, DEFVAL(false));
  5360. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  5361. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column", "button_index"), &Tree::get_item_rect, DEFVAL(-1), DEFVAL(-1));
  5362. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  5363. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  5364. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  5365. ClassDB::bind_method(D_METHOD("get_button_id_at_position", "position"), &Tree::get_button_id_at_position);
  5366. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  5367. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  5368. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  5369. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  5370. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  5371. ClassDB::bind_method(D_METHOD("set_column_title_alignment", "column", "title_alignment"), &Tree::set_column_title_alignment);
  5372. ClassDB::bind_method(D_METHOD("get_column_title_alignment", "column"), &Tree::get_column_title_alignment);
  5373. ClassDB::bind_method(D_METHOD("set_column_title_direction", "column", "direction"), &Tree::set_column_title_direction);
  5374. ClassDB::bind_method(D_METHOD("get_column_title_direction", "column"), &Tree::get_column_title_direction);
  5375. ClassDB::bind_method(D_METHOD("set_column_title_language", "column", "language"), &Tree::set_column_title_language);
  5376. ClassDB::bind_method(D_METHOD("get_column_title_language", "column"), &Tree::get_column_title_language);
  5377. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  5378. ClassDB::bind_method(D_METHOD("scroll_to_item", "item", "center_on_item"), &Tree::scroll_to_item, DEFVAL(false));
  5379. ClassDB::bind_method(D_METHOD("set_h_scroll_enabled", "h_scroll"), &Tree::set_h_scroll_enabled);
  5380. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &Tree::is_h_scroll_enabled);
  5381. ClassDB::bind_method(D_METHOD("set_v_scroll_enabled", "h_scroll"), &Tree::set_v_scroll_enabled);
  5382. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &Tree::is_v_scroll_enabled);
  5383. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  5384. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  5385. ClassDB::bind_method(D_METHOD("set_enable_recursive_folding", "enable"), &Tree::set_enable_recursive_folding);
  5386. ClassDB::bind_method(D_METHOD("is_recursive_folding_enabled"), &Tree::is_recursive_folding_enabled);
  5387. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  5388. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  5389. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  5390. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  5391. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  5392. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  5393. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &Tree::set_allow_search);
  5394. ClassDB::bind_method(D_METHOD("get_allow_search"), &Tree::get_allow_search);
  5395. ClassDB::bind_method(D_METHOD("set_auto_tooltip", "enable"), &Tree::set_auto_tooltip);
  5396. ClassDB::bind_method(D_METHOD("is_auto_tooltip_enabled"), &Tree::is_auto_tooltip_enabled);
  5397. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  5398. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "column_titles_visible"), "set_column_titles_visible", "are_column_titles_visible");
  5399. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  5400. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  5401. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  5402. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  5403. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_recursive_folding"), "set_enable_recursive_folding", "is_recursive_folding_enabled");
  5404. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  5405. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In Between"), "set_drop_mode_flags", "get_drop_mode_flags");
  5406. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  5407. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_h_scroll_enabled", "is_h_scroll_enabled");
  5408. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_v_scroll_enabled", "is_v_scroll_enabled");
  5409. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_tooltip"), "set_auto_tooltip", "is_auto_tooltip_enabled");
  5410. ADD_SIGNAL(MethodInfo("item_selected"));
  5411. ADD_SIGNAL(MethodInfo("cell_selected"));
  5412. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  5413. ADD_SIGNAL(MethodInfo("item_mouse_selected", PropertyInfo(Variant::VECTOR2, "mouse_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  5414. ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "click_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  5415. ADD_SIGNAL(MethodInfo("item_edited"));
  5416. ADD_SIGNAL(MethodInfo("custom_item_clicked", PropertyInfo(Variant::INT, "mouse_button_index")));
  5417. ADD_SIGNAL(MethodInfo("item_icon_double_clicked"));
  5418. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  5419. ADD_SIGNAL(MethodInfo("check_propagated_to_item", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column")));
  5420. ADD_SIGNAL(MethodInfo("button_clicked", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::INT, "mouse_button_index")));
  5421. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  5422. ADD_SIGNAL(MethodInfo("item_activated"));
  5423. ADD_SIGNAL(MethodInfo("column_title_clicked", PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "mouse_button_index")));
  5424. ADD_SIGNAL(MethodInfo("nothing_selected"));
  5425. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  5426. BIND_ENUM_CONSTANT(SELECT_ROW);
  5427. BIND_ENUM_CONSTANT(SELECT_MULTI);
  5428. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  5429. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  5430. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  5431. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, panel_style, "panel");
  5432. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, focus_style, "focus");
  5433. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Tree, font);
  5434. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Tree, font_size);
  5435. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, Tree, tb_font, "title_button_font");
  5436. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, Tree, tb_font_size, "title_button_font_size");
  5437. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered);
  5438. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_dimmed);
  5439. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_selected);
  5440. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_selected_focus);
  5441. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected);
  5442. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected_focus);
  5443. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor);
  5444. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor_unfocus, "cursor_unfocused");
  5445. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, button_hover);
  5446. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, button_pressed);
  5447. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked);
  5448. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked);
  5449. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked_disabled);
  5450. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked_disabled);
  5451. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate);
  5452. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate_disabled);
  5453. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow);
  5454. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed);
  5455. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed_mirrored);
  5456. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, select_arrow);
  5457. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, updown);
  5458. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button);
  5459. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_hover);
  5460. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_pressed);
  5461. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, custom_button_font_highlight);
  5462. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_color);
  5463. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_color);
  5464. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_dimmed_color);
  5465. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_selected_color);
  5466. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_selected_color);
  5467. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_disabled_color);
  5468. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, drop_position_color);
  5469. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, h_separation);
  5470. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, v_separation);
  5471. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_bottom);
  5472. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_left);
  5473. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_right);
  5474. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_top);
  5475. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, item_margin);
  5476. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, button_margin);
  5477. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, icon_max_width);
  5478. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_outline_color);
  5479. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Tree, font_outline_size, "outline_size");
  5480. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_guides);
  5481. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, guide_color);
  5482. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_relationship_lines);
  5483. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, relationship_line_width);
  5484. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_width);
  5485. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, children_hl_line_width);
  5486. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_margin);
  5487. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, relationship_line_color);
  5488. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, parent_hl_line_color);
  5489. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, children_hl_line_color);
  5490. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_border);
  5491. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_speed);
  5492. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_top);
  5493. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_right);
  5494. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_bottom);
  5495. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_left);
  5496. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_h_separation);
  5497. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_v_separation);
  5498. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button, "title_button_normal");
  5499. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_pressed);
  5500. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_hover);
  5501. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, title_button_color);
  5502. }
  5503. Tree::Tree() {
  5504. columns.resize(1);
  5505. set_focus_mode(FOCUS_ALL);
  5506. popup_editor = memnew(Popup);
  5507. add_child(popup_editor, false, INTERNAL_MODE_FRONT);
  5508. popup_editor_vb = memnew(VBoxContainer);
  5509. popup_editor_vb->add_theme_constant_override("separation", 0);
  5510. popup_editor_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  5511. popup_editor->add_child(popup_editor_vb);
  5512. line_editor = memnew(LineEdit);
  5513. line_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  5514. line_editor->hide();
  5515. popup_editor_vb->add_child(line_editor);
  5516. text_editor = memnew(TextEdit);
  5517. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  5518. text_editor->hide();
  5519. popup_editor_vb->add_child(text_editor);
  5520. h_scroll = memnew(HScrollBar);
  5521. v_scroll = memnew(VScrollBar);
  5522. add_child(h_scroll, false, INTERNAL_MODE_FRONT);
  5523. add_child(v_scroll, false, INTERNAL_MODE_FRONT);
  5524. range_click_timer = memnew(Timer);
  5525. range_click_timer->connect("timeout", callable_mp(this, &Tree::_range_click_timeout));
  5526. add_child(range_click_timer, false, INTERNAL_MODE_FRONT);
  5527. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Tree::_scroll_moved));
  5528. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Tree::_scroll_moved));
  5529. line_editor->connect(SceneStringName(text_submitted), callable_mp(this, &Tree::_line_editor_submit));
  5530. text_editor->connect(SceneStringName(gui_input), callable_mp(this, &Tree::_text_editor_gui_input));
  5531. popup_editor->connect("popup_hide", callable_mp(this, &Tree::_text_editor_popup_modal_close));
  5532. set_notify_transform(true);
  5533. set_mouse_filter(MOUSE_FILTER_STOP);
  5534. set_clip_contents(true);
  5535. }
  5536. Tree::~Tree() {
  5537. if (root) {
  5538. memdelete(root);
  5539. }
  5540. }