tree.cpp 150 KB

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