tree.cpp 150 KB

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