tree.cpp 154 KB

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