2
0

tree.cpp 145 KB

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