tree.cpp 148 KB

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