tree.cpp 177 KB

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