tree.cpp 171 KB

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