tree.cpp 187 KB

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