tree.cpp 163 KB

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