tree.cpp 157 KB

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