2
0

tree.cpp 187 KB

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