tree.cpp 145 KB

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