tree.cpp 217 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752
  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 "tree.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/os/keyboard.h"
  36. #include "core/os/os.h"
  37. #include "scene/gui/box_container.h"
  38. #include "scene/gui/line_edit.h"
  39. #include "scene/gui/popup_menu.h"
  40. #include "scene/gui/scroll_bar.h"
  41. #include "scene/gui/slider.h"
  42. #include "scene/gui/text_edit.h"
  43. #include "scene/main/window.h"
  44. #include "scene/theme/theme_db.h"
  45. #include <climits>
  46. Size2 TreeItem::Cell::get_icon_size() const {
  47. if (icon.is_null()) {
  48. return Size2();
  49. }
  50. if (icon_region == Rect2i()) {
  51. return icon->get_size();
  52. } else {
  53. return icon_region.size;
  54. }
  55. }
  56. void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Color &p_color) const {
  57. if (icon.is_null()) {
  58. return;
  59. }
  60. Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size;
  61. if (icon_region == Rect2i()) {
  62. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
  63. if (icon_overlay.is_valid()) {
  64. Vector2 offset = icon->get_size() - icon_overlay->get_size();
  65. icon_overlay->draw_rect_region(p_where, Rect2(p_pos + offset, dsize), Rect2(Point2(), icon_overlay->get_size()), p_color);
  66. }
  67. } else {
  68. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  69. if (icon_overlay.is_valid()) {
  70. icon_overlay->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  71. }
  72. }
  73. }
  74. void TreeItem::_changed_notify(int p_cell) {
  75. if (tree) {
  76. tree->item_changed(p_cell, this);
  77. }
  78. }
  79. void TreeItem::_changed_notify() {
  80. if (tree) {
  81. tree->item_changed(-1, this);
  82. }
  83. }
  84. void TreeItem::_cell_selected(int p_cell) {
  85. if (tree) {
  86. tree->item_selected(p_cell, this);
  87. }
  88. }
  89. void TreeItem::_cell_deselected(int p_cell) {
  90. if (tree) {
  91. tree->item_deselected(p_cell, this);
  92. }
  93. }
  94. void TreeItem::_change_tree(Tree *p_tree) {
  95. if (p_tree == tree) {
  96. return;
  97. }
  98. accessibility_row_dirty = true;
  99. TreeItem *c = first_child;
  100. while (c) {
  101. c->_change_tree(p_tree);
  102. c = c->next;
  103. }
  104. if (tree) {
  105. if (tree->root == this) {
  106. tree->root = nullptr;
  107. }
  108. if (tree->popup_edited_item == this) {
  109. tree->popup_edited_item = nullptr;
  110. tree->popup_pressing_edited_item = nullptr;
  111. tree->pressing_for_editor = false;
  112. }
  113. if (tree->cache.hover_item == this) {
  114. tree->cache.hover_item = nullptr;
  115. }
  116. if (tree->selected_item == this) {
  117. for (int i = 0; i < tree->selected_item->cells.size(); i++) {
  118. tree->selected_item->cells.write[i].selected = false;
  119. }
  120. tree->selected_item = nullptr;
  121. }
  122. if (tree->drop_mode_over == this) {
  123. tree->drop_mode_over = nullptr;
  124. }
  125. if (tree->single_select_defer == this) {
  126. tree->single_select_defer = nullptr;
  127. }
  128. if (tree->edited_item == this) {
  129. tree->edited_item = nullptr;
  130. tree->pressing_for_editor = false;
  131. }
  132. tree->queue_accessibility_update();
  133. tree->queue_redraw();
  134. }
  135. tree = p_tree;
  136. if (accessibility_row_element.is_valid()) {
  137. DisplayServer::get_singleton()->accessibility_free_element(accessibility_row_element);
  138. accessibility_row_element = RID();
  139. }
  140. for (Cell &cell : cells) {
  141. if (cell.accessibility_cell_element.is_valid()) {
  142. DisplayServer::get_singleton()->accessibility_free_element(cell.accessibility_cell_element);
  143. cell.accessibility_cell_element = RID();
  144. }
  145. for (Cell::Button &btn : cell.buttons) {
  146. if (btn.accessibility_button_element.is_valid()) {
  147. DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
  148. btn.accessibility_button_element = RID();
  149. }
  150. }
  151. }
  152. if (tree) {
  153. tree->queue_accessibility_update();
  154. tree->queue_redraw();
  155. cells.resize(tree->columns.size());
  156. }
  157. }
  158. void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) {
  159. ERR_FAIL_INDEX(p_column, cells.size());
  160. if (cells[p_column].mode == p_mode) {
  161. return;
  162. }
  163. Cell &c = cells.write[p_column];
  164. c.mode = p_mode;
  165. c.min = 0;
  166. c.max = 100;
  167. c.step = 1;
  168. c.val = 0;
  169. c.checked = false;
  170. c.icon = Ref<Texture2D>();
  171. c.text = "";
  172. c.dirty = true;
  173. c.icon_max_w = 0;
  174. c.cached_minimum_size_dirty = true;
  175. _changed_notify(p_column);
  176. }
  177. TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
  178. ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING);
  179. return cells[p_column].mode;
  180. }
  181. void TreeItem::set_auto_translate_mode(int p_column, Node::AutoTranslateMode p_mode) {
  182. ERR_FAIL_INDEX(p_column, cells.size());
  183. if (cells[p_column].auto_translate_mode == p_mode) {
  184. return;
  185. }
  186. cells.write[p_column].auto_translate_mode = p_mode;
  187. cells.write[p_column].dirty = true;
  188. cells.write[p_column].cached_minimum_size_dirty = true;
  189. _changed_notify(p_column);
  190. }
  191. Node::AutoTranslateMode TreeItem::get_auto_translate_mode(int p_column) const {
  192. ERR_FAIL_INDEX_V(p_column, cells.size(), Node::AUTO_TRANSLATE_MODE_INHERIT);
  193. return cells[p_column].auto_translate_mode;
  194. }
  195. void TreeItem::set_edit_multiline(int p_column, bool p_multiline) {
  196. ERR_FAIL_INDEX(p_column, cells.size());
  197. cells.write[p_column].edit_multiline = p_multiline;
  198. _changed_notify(p_column);
  199. }
  200. bool TreeItem::is_edit_multiline(int p_column) const {
  201. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  202. return cells[p_column].edit_multiline;
  203. }
  204. void TreeItem::set_checked(int p_column, bool p_checked) {
  205. ERR_FAIL_INDEX(p_column, cells.size());
  206. if (cells[p_column].checked == p_checked) {
  207. return;
  208. }
  209. cells.write[p_column].checked = p_checked;
  210. cells.write[p_column].indeterminate = false;
  211. cells.write[p_column].cached_minimum_size_dirty = true;
  212. _changed_notify(p_column);
  213. }
  214. void TreeItem::set_indeterminate(int p_column, bool p_indeterminate) {
  215. ERR_FAIL_INDEX(p_column, cells.size());
  216. // Prevent uncheck if indeterminate set to false twice.
  217. if (p_indeterminate == cells[p_column].indeterminate) {
  218. return;
  219. }
  220. cells.write[p_column].indeterminate = p_indeterminate;
  221. cells.write[p_column].checked = false;
  222. cells.write[p_column].cached_minimum_size_dirty = true;
  223. _changed_notify(p_column);
  224. }
  225. bool TreeItem::is_checked(int p_column) const {
  226. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  227. return cells[p_column].checked;
  228. }
  229. bool TreeItem::is_indeterminate(int p_column) const {
  230. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  231. return cells[p_column].indeterminate;
  232. }
  233. void TreeItem::propagate_check(int p_column, bool p_emit_signal) {
  234. bool ch = cells[p_column].checked;
  235. if (p_emit_signal) {
  236. tree->emit_signal(SNAME("check_propagated_to_item"), this, p_column);
  237. }
  238. _propagate_check_through_children(p_column, ch, p_emit_signal);
  239. _propagate_check_through_parents(p_column, p_emit_signal);
  240. }
  241. String TreeItem::atr(int p_column, const String &p_text) const {
  242. ERR_FAIL_INDEX_V(p_column, cells.size(), tree->atr(p_text));
  243. switch (cells[p_column].auto_translate_mode) {
  244. case Node::AUTO_TRANSLATE_MODE_INHERIT: {
  245. return tree->atr(p_text);
  246. } break;
  247. case Node::AUTO_TRANSLATE_MODE_ALWAYS: {
  248. return tree->tr(p_text);
  249. } break;
  250. case Node::AUTO_TRANSLATE_MODE_DISABLED: {
  251. return p_text;
  252. } break;
  253. }
  254. ERR_FAIL_V_MSG(tree->atr(p_text), "Unexpected auto translate mode: " + itos(cells[p_column].auto_translate_mode));
  255. }
  256. void TreeItem::_propagate_check_through_children(int p_column, bool p_checked, bool p_emit_signal) {
  257. TreeItem *current = get_first_child();
  258. while (current) {
  259. current->set_checked(p_column, p_checked);
  260. if (p_emit_signal) {
  261. current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
  262. }
  263. current->_propagate_check_through_children(p_column, p_checked, p_emit_signal);
  264. current = current->get_next();
  265. }
  266. }
  267. void TreeItem::_propagate_check_through_parents(int p_column, bool p_emit_signal) {
  268. TreeItem *current = get_parent();
  269. if (!current) {
  270. return;
  271. }
  272. bool any_checked = false;
  273. bool any_unchecked = false;
  274. bool any_indeterminate = false;
  275. TreeItem *child_item = current->get_first_child();
  276. while (child_item) {
  277. if (!child_item->is_checked(p_column)) {
  278. any_unchecked = true;
  279. if (child_item->is_indeterminate(p_column)) {
  280. any_indeterminate = true;
  281. break;
  282. }
  283. } else {
  284. any_checked = true;
  285. }
  286. child_item = child_item->get_next();
  287. }
  288. if (any_indeterminate || (any_checked && any_unchecked)) {
  289. current->set_indeterminate(p_column, true);
  290. } else if (current->is_indeterminate(p_column) && !any_checked) {
  291. current->set_indeterminate(p_column, false);
  292. } else {
  293. current->set_checked(p_column, any_checked);
  294. }
  295. if (p_emit_signal) {
  296. current->tree->emit_signal(SNAME("check_propagated_to_item"), current, p_column);
  297. }
  298. current->_propagate_check_through_parents(p_column, p_emit_signal);
  299. }
  300. void TreeItem::set_text(int p_column, String p_text) {
  301. ERR_FAIL_INDEX(p_column, cells.size());
  302. if (cells[p_column].text == p_text) {
  303. return;
  304. }
  305. cells.write[p_column].text = p_text;
  306. cells.write[p_column].dirty = true;
  307. if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE) {
  308. Vector<String> strings = p_text.split(",");
  309. cells.write[p_column].min = INT_MAX;
  310. cells.write[p_column].max = INT_MIN;
  311. for (int i = 0; i < strings.size(); i++) {
  312. int value = i;
  313. if (!strings[i].get_slicec(':', 1).is_empty()) {
  314. value = strings[i].get_slicec(':', 1).to_int();
  315. }
  316. cells.write[p_column].min = MIN(cells[p_column].min, value);
  317. cells.write[p_column].max = MAX(cells[p_column].max, value);
  318. }
  319. cells.write[p_column].step = 0;
  320. } else {
  321. // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
  322. if (tree && (!cells[p_column].editable || cells[p_column].mode != TreeItem::CELL_MODE_STRING)) {
  323. cells.write[p_column].xl_text = atr(p_column, p_text);
  324. } else {
  325. cells.write[p_column].xl_text = p_text;
  326. }
  327. }
  328. cells.write[p_column].cached_minimum_size_dirty = true;
  329. _changed_notify(p_column);
  330. if (get_tree()) {
  331. get_tree()->update_configuration_warnings();
  332. }
  333. }
  334. String TreeItem::get_text(int p_column) const {
  335. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  336. return cells[p_column].text;
  337. }
  338. void TreeItem::set_description(int p_column, String p_text) {
  339. ERR_FAIL_INDEX(p_column, cells.size());
  340. if (cells[p_column].description == p_text) {
  341. return;
  342. }
  343. cells.write[p_column].description = p_text;
  344. _changed_notify(p_column);
  345. if (get_tree()) {
  346. get_tree()->update_configuration_warnings();
  347. }
  348. }
  349. String TreeItem::get_description(int p_column) const {
  350. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  351. return cells[p_column].description;
  352. }
  353. void TreeItem::set_text_direction(int p_column, Control::TextDirection p_text_direction) {
  354. ERR_FAIL_INDEX(p_column, cells.size());
  355. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  356. if (cells[p_column].text_direction == p_text_direction) {
  357. return;
  358. }
  359. cells.write[p_column].text_direction = p_text_direction;
  360. cells.write[p_column].dirty = true;
  361. _changed_notify(p_column);
  362. cells.write[p_column].cached_minimum_size_dirty = true;
  363. }
  364. Control::TextDirection TreeItem::get_text_direction(int p_column) const {
  365. ERR_FAIL_INDEX_V(p_column, cells.size(), Control::TEXT_DIRECTION_INHERITED);
  366. return cells[p_column].text_direction;
  367. }
  368. void TreeItem::set_autowrap_mode(int p_column, TextServer::AutowrapMode p_mode) {
  369. ERR_FAIL_INDEX(p_column, cells.size());
  370. ERR_FAIL_COND(p_mode < TextServer::AUTOWRAP_OFF || p_mode > TextServer::AUTOWRAP_WORD_SMART);
  371. if (cells[p_column].autowrap_mode == p_mode) {
  372. return;
  373. }
  374. cells.write[p_column].autowrap_mode = p_mode;
  375. cells.write[p_column].dirty = true;
  376. _changed_notify(p_column);
  377. cells.write[p_column].cached_minimum_size_dirty = true;
  378. }
  379. TextServer::AutowrapMode TreeItem::get_autowrap_mode(int p_column) const {
  380. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::AUTOWRAP_OFF);
  381. return cells[p_column].autowrap_mode;
  382. }
  383. void TreeItem::set_text_overrun_behavior(int p_column, TextServer::OverrunBehavior p_behavior) {
  384. ERR_FAIL_INDEX(p_column, cells.size());
  385. if (cells[p_column].text_buf->get_text_overrun_behavior() == p_behavior) {
  386. return;
  387. }
  388. cells.write[p_column].text_buf->set_text_overrun_behavior(p_behavior);
  389. cells.write[p_column].dirty = true;
  390. cells.write[p_column].cached_minimum_size_dirty = true;
  391. _changed_notify(p_column);
  392. }
  393. TextServer::OverrunBehavior TreeItem::get_text_overrun_behavior(int p_column) const {
  394. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::OVERRUN_TRIM_ELLIPSIS);
  395. return cells[p_column].text_buf->get_text_overrun_behavior();
  396. }
  397. void TreeItem::set_structured_text_bidi_override(int p_column, TextServer::StructuredTextParser p_parser) {
  398. ERR_FAIL_INDEX(p_column, cells.size());
  399. if (cells[p_column].st_parser != p_parser) {
  400. cells.write[p_column].st_parser = p_parser;
  401. cells.write[p_column].dirty = true;
  402. cells.write[p_column].cached_minimum_size_dirty = true;
  403. _changed_notify(p_column);
  404. }
  405. }
  406. TextServer::StructuredTextParser TreeItem::get_structured_text_bidi_override(int p_column) const {
  407. ERR_FAIL_INDEX_V(p_column, cells.size(), TextServer::STRUCTURED_TEXT_DEFAULT);
  408. return cells[p_column].st_parser;
  409. }
  410. void TreeItem::set_structured_text_bidi_override_options(int p_column, Array p_args) {
  411. ERR_FAIL_INDEX(p_column, cells.size());
  412. if (cells[p_column].st_args == p_args) {
  413. return;
  414. }
  415. cells.write[p_column].st_args = p_args;
  416. cells.write[p_column].dirty = true;
  417. cells.write[p_column].cached_minimum_size_dirty = true;
  418. _changed_notify(p_column);
  419. }
  420. Array TreeItem::get_structured_text_bidi_override_options(int p_column) const {
  421. ERR_FAIL_INDEX_V(p_column, cells.size(), Array());
  422. return cells[p_column].st_args;
  423. }
  424. void TreeItem::set_language(int p_column, const String &p_language) {
  425. ERR_FAIL_INDEX(p_column, cells.size());
  426. if (cells[p_column].language != p_language) {
  427. cells.write[p_column].language = p_language;
  428. cells.write[p_column].dirty = true;
  429. cells.write[p_column].cached_minimum_size_dirty = true;
  430. _changed_notify(p_column);
  431. }
  432. }
  433. String TreeItem::get_language(int p_column) const {
  434. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  435. return cells[p_column].language;
  436. }
  437. void TreeItem::set_suffix(int p_column, String p_suffix) {
  438. ERR_FAIL_INDEX(p_column, cells.size());
  439. if (cells[p_column].suffix == p_suffix) {
  440. return;
  441. }
  442. cells.write[p_column].suffix = p_suffix;
  443. cells.write[p_column].cached_minimum_size_dirty = true;
  444. _changed_notify(p_column);
  445. }
  446. String TreeItem::get_suffix(int p_column) const {
  447. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  448. return cells[p_column].suffix;
  449. }
  450. void TreeItem::set_icon(int p_column, const Ref<Texture2D> &p_icon) {
  451. ERR_FAIL_INDEX(p_column, cells.size());
  452. if (cells[p_column].icon == p_icon) {
  453. return;
  454. }
  455. cells.write[p_column].icon = p_icon;
  456. cells.write[p_column].cached_minimum_size_dirty = true;
  457. _changed_notify(p_column);
  458. }
  459. Ref<Texture2D> TreeItem::get_icon(int p_column) const {
  460. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  461. return cells[p_column].icon;
  462. }
  463. void TreeItem::set_icon_overlay(int p_column, const Ref<Texture2D> &p_icon_overlay) {
  464. ERR_FAIL_INDEX(p_column, cells.size());
  465. if (cells[p_column].icon_overlay == p_icon_overlay) {
  466. return;
  467. }
  468. cells.write[p_column].icon_overlay = p_icon_overlay;
  469. cells.write[p_column].cached_minimum_size_dirty = true;
  470. _changed_notify(p_column);
  471. }
  472. Ref<Texture2D> TreeItem::get_icon_overlay(int p_column) const {
  473. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  474. return cells[p_column].icon_overlay;
  475. }
  476. void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
  477. ERR_FAIL_INDEX(p_column, cells.size());
  478. if (cells[p_column].icon_region == p_icon_region) {
  479. return;
  480. }
  481. cells.write[p_column].icon_region = p_icon_region;
  482. cells.write[p_column].cached_minimum_size_dirty = true;
  483. _changed_notify(p_column);
  484. }
  485. Rect2 TreeItem::get_icon_region(int p_column) const {
  486. ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2());
  487. return cells[p_column].icon_region;
  488. }
  489. void TreeItem::set_icon_modulate(int p_column, const Color &p_modulate) {
  490. ERR_FAIL_INDEX(p_column, cells.size());
  491. if (cells[p_column].icon_color == p_modulate) {
  492. return;
  493. }
  494. cells.write[p_column].icon_color = p_modulate;
  495. _changed_notify(p_column);
  496. }
  497. Color TreeItem::get_icon_modulate(int p_column) const {
  498. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  499. return cells[p_column].icon_color;
  500. }
  501. void TreeItem::set_icon_max_width(int p_column, int p_max) {
  502. ERR_FAIL_INDEX(p_column, cells.size());
  503. if (cells[p_column].icon_max_w == p_max) {
  504. return;
  505. }
  506. cells.write[p_column].icon_max_w = p_max;
  507. cells.write[p_column].cached_minimum_size_dirty = true;
  508. _changed_notify(p_column);
  509. }
  510. int TreeItem::get_icon_max_width(int p_column) const {
  511. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  512. return cells[p_column].icon_max_w;
  513. }
  514. void TreeItem::set_range(int p_column, double p_value) {
  515. ERR_FAIL_INDEX(p_column, cells.size());
  516. if (cells[p_column].step > 0) {
  517. p_value = Math::snapped(p_value, cells[p_column].step);
  518. }
  519. if (p_value < cells[p_column].min) {
  520. p_value = cells[p_column].min;
  521. }
  522. if (p_value > cells[p_column].max) {
  523. p_value = cells[p_column].max;
  524. }
  525. if (cells[p_column].val == p_value) {
  526. return;
  527. }
  528. cells.write[p_column].val = p_value;
  529. cells.write[p_column].dirty = true;
  530. _changed_notify(p_column);
  531. }
  532. double TreeItem::get_range(int p_column) const {
  533. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  534. return cells[p_column].val;
  535. }
  536. bool TreeItem::is_range_exponential(int p_column) const {
  537. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  538. return cells[p_column].expr;
  539. }
  540. void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) {
  541. ERR_FAIL_INDEX(p_column, cells.size());
  542. 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) {
  543. return;
  544. }
  545. cells.write[p_column].min = p_min;
  546. cells.write[p_column].max = p_max;
  547. cells.write[p_column].step = p_step;
  548. cells.write[p_column].expr = p_exp;
  549. _changed_notify(p_column);
  550. }
  551. void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const {
  552. ERR_FAIL_INDEX(p_column, cells.size());
  553. r_min = cells[p_column].min;
  554. r_max = cells[p_column].max;
  555. r_step = cells[p_column].step;
  556. }
  557. void TreeItem::set_metadata(int p_column, const Variant &p_meta) {
  558. ERR_FAIL_INDEX(p_column, cells.size());
  559. cells.write[p_column].meta = p_meta;
  560. }
  561. Variant TreeItem::get_metadata(int p_column) const {
  562. ERR_FAIL_INDEX_V(p_column, cells.size(), Variant());
  563. return cells[p_column].meta;
  564. }
  565. #ifndef DISABLE_DEPRECATED
  566. void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
  567. WARN_DEPRECATED_MSG(R"*(The "set_custom_draw()" method is deprecated, use "set_custom_draw_callback()" instead.)*");
  568. ERR_FAIL_INDEX(p_column, cells.size());
  569. ERR_FAIL_NULL(p_object);
  570. cells.write[p_column].custom_draw_callback = Callable(p_object, p_callback);
  571. _changed_notify(p_column);
  572. }
  573. #endif // DISABLE_DEPRECATED
  574. void TreeItem::set_custom_draw_callback(int p_column, const Callable &p_callback) {
  575. ERR_FAIL_INDEX(p_column, cells.size());
  576. cells.write[p_column].custom_draw_callback = p_callback;
  577. _changed_notify(p_column);
  578. }
  579. Callable TreeItem::get_custom_draw_callback(int p_column) const {
  580. ERR_FAIL_INDEX_V(p_column, cells.size(), Callable());
  581. return cells[p_column].custom_draw_callback;
  582. }
  583. void TreeItem::set_collapsed(bool p_collapsed) {
  584. if (collapsed == p_collapsed || !tree) {
  585. return;
  586. }
  587. collapsed = p_collapsed;
  588. TreeItem *ci = tree->selected_item;
  589. if (ci) {
  590. while (ci && ci != this) {
  591. ci = ci->parent;
  592. }
  593. if (ci) { // Collapsing cursor/selected, move it!
  594. if (tree->select_mode == Tree::SELECT_MULTI) {
  595. tree->selected_item = this;
  596. emit_signal(SNAME("cell_selected"));
  597. } else {
  598. select(tree->selected_col);
  599. }
  600. tree->queue_accessibility_update();
  601. tree->queue_redraw();
  602. }
  603. }
  604. _changed_notify();
  605. tree->emit_signal(SNAME("item_collapsed"), this);
  606. }
  607. bool TreeItem::is_collapsed() {
  608. return collapsed;
  609. }
  610. void TreeItem::set_collapsed_recursive(bool p_collapsed) {
  611. if (!tree) {
  612. return;
  613. }
  614. set_collapsed(p_collapsed);
  615. TreeItem *child = get_first_child();
  616. while (child) {
  617. child->set_collapsed_recursive(p_collapsed);
  618. child = child->get_next();
  619. }
  620. }
  621. bool TreeItem::_is_any_collapsed(bool p_only_visible) {
  622. TreeItem *child = get_first_child();
  623. // Check on children directly first (avoid recursing if possible).
  624. while (child) {
  625. if (child->get_first_child() && child->is_collapsed() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count()))) {
  626. return true;
  627. }
  628. child = child->get_next();
  629. }
  630. child = get_first_child();
  631. // Otherwise recurse on children.
  632. while (child) {
  633. if (child->get_first_child() && (!p_only_visible || (child->is_visible() && child->get_visible_child_count())) && child->_is_any_collapsed(p_only_visible)) {
  634. return true;
  635. }
  636. child = child->get_next();
  637. }
  638. return false;
  639. }
  640. bool TreeItem::is_any_collapsed(bool p_only_visible) {
  641. if (p_only_visible && !is_visible_in_tree()) {
  642. return false;
  643. }
  644. // Collapsed if this is collapsed and it has children (only considers visible if only visible is set).
  645. if (is_collapsed() && get_first_child() && (!p_only_visible || get_visible_child_count())) {
  646. return true;
  647. }
  648. return _is_any_collapsed(p_only_visible);
  649. }
  650. void TreeItem::set_visible(bool p_visible) {
  651. if (visible == p_visible) {
  652. return;
  653. }
  654. visible = p_visible;
  655. if (tree) {
  656. if (!visible) {
  657. for (int i = 0; i < tree->columns.size(); i++) {
  658. if (cells[i].selected) {
  659. deselect(i);
  660. }
  661. }
  662. }
  663. tree->queue_accessibility_update();
  664. tree->queue_redraw();
  665. _changed_notify();
  666. }
  667. _handle_visibility_changed(p_visible);
  668. }
  669. bool TreeItem::is_visible() {
  670. return visible;
  671. }
  672. bool TreeItem::is_visible_in_tree() const {
  673. return visible && parent_visible_in_tree;
  674. }
  675. void TreeItem::_handle_visibility_changed(bool p_visible) {
  676. TreeItem *child = get_first_child();
  677. while (child) {
  678. child->_propagate_visibility_changed(p_visible);
  679. child = child->get_next();
  680. }
  681. }
  682. void TreeItem::_propagate_visibility_changed(bool p_parent_visible_in_tree) {
  683. parent_visible_in_tree = p_parent_visible_in_tree;
  684. _handle_visibility_changed(p_parent_visible_in_tree);
  685. }
  686. void TreeItem::uncollapse_tree() {
  687. TreeItem *t = this;
  688. while (t) {
  689. t->set_collapsed(false);
  690. t = t->parent;
  691. }
  692. }
  693. void TreeItem::set_custom_minimum_height(int p_height) {
  694. if (custom_min_height == p_height) {
  695. return;
  696. }
  697. custom_min_height = p_height;
  698. for (Cell &c : cells) {
  699. c.cached_minimum_size_dirty = true;
  700. }
  701. _changed_notify();
  702. }
  703. int TreeItem::get_custom_minimum_height() const {
  704. return custom_min_height;
  705. }
  706. TreeItem *TreeItem::create_child(int p_index) {
  707. TreeItem *ti = memnew(TreeItem(tree));
  708. if (tree) {
  709. ti->cells.resize(tree->columns.size());
  710. tree->queue_accessibility_update();
  711. tree->queue_redraw();
  712. }
  713. TreeItem *item_prev = nullptr;
  714. TreeItem *item_next = first_child;
  715. if (p_index < 0 && last_child) {
  716. item_prev = last_child;
  717. } else {
  718. int idx = 0;
  719. if (!children_cache.is_empty()) {
  720. idx = MIN(children_cache.size() - 1, p_index);
  721. item_next = children_cache[idx];
  722. item_prev = item_next->prev;
  723. }
  724. while (item_next) {
  725. if (idx == p_index) {
  726. item_next->prev = ti;
  727. ti->next = item_next;
  728. break;
  729. }
  730. item_prev = item_next;
  731. item_next = item_next->next;
  732. idx++;
  733. }
  734. }
  735. if (item_prev) {
  736. item_prev->next = ti;
  737. ti->prev = item_prev;
  738. if (!children_cache.is_empty()) {
  739. if (ti->next) {
  740. children_cache.insert(p_index, ti);
  741. } else {
  742. children_cache.append(ti);
  743. }
  744. }
  745. } else {
  746. first_child = ti;
  747. children_cache.insert(0, ti);
  748. }
  749. if (item_prev == last_child) {
  750. last_child = ti;
  751. }
  752. ti->parent = this;
  753. ti->parent_visible_in_tree = is_visible_in_tree();
  754. return ti;
  755. }
  756. void TreeItem::add_child(TreeItem *p_item) {
  757. ERR_FAIL_NULL(p_item);
  758. ERR_FAIL_COND(p_item->tree);
  759. ERR_FAIL_COND(p_item->parent);
  760. p_item->_change_tree(tree);
  761. p_item->parent = this;
  762. p_item->parent_visible_in_tree = is_visible_in_tree();
  763. p_item->_handle_visibility_changed(p_item->parent_visible_in_tree);
  764. if (last_child) {
  765. last_child->next = p_item;
  766. p_item->prev = last_child;
  767. } else {
  768. first_child = p_item;
  769. }
  770. last_child = p_item;
  771. if (!children_cache.is_empty()) {
  772. children_cache.append(p_item);
  773. }
  774. validate_cache();
  775. }
  776. void TreeItem::remove_child(TreeItem *p_item) {
  777. ERR_FAIL_NULL(p_item);
  778. ERR_FAIL_COND(p_item->parent != this);
  779. p_item->_unlink_from_tree();
  780. p_item->_change_tree(nullptr);
  781. p_item->prev = nullptr;
  782. p_item->next = nullptr;
  783. p_item->parent = nullptr;
  784. validate_cache();
  785. }
  786. Tree *TreeItem::get_tree() const {
  787. return tree;
  788. }
  789. TreeItem *TreeItem::get_next() const {
  790. return next;
  791. }
  792. TreeItem *TreeItem::get_prev() {
  793. if (prev) {
  794. return prev;
  795. }
  796. if (!parent || parent->first_child == this) {
  797. return nullptr;
  798. }
  799. // This is an edge case.
  800. TreeItem *l_prev = parent->first_child;
  801. while (l_prev && l_prev->next != this) {
  802. l_prev = l_prev->next;
  803. }
  804. prev = l_prev;
  805. return prev;
  806. }
  807. TreeItem *TreeItem::get_parent() const {
  808. return parent;
  809. }
  810. TreeItem *TreeItem::get_first_child() const {
  811. return first_child;
  812. }
  813. TreeItem *TreeItem::_get_prev_in_tree(bool p_wrap, bool p_include_invisible) {
  814. TreeItem *current = this;
  815. TreeItem *prev_item = current->get_prev();
  816. if (!prev_item) {
  817. current = current->parent;
  818. if (!current || (current == tree->root && tree->hide_root)) {
  819. if (!p_wrap) {
  820. return nullptr;
  821. }
  822. // Wrap around to the last visible item.
  823. current = this;
  824. TreeItem *temp = get_next_visible();
  825. while (temp) {
  826. current = temp;
  827. temp = temp->get_next_visible();
  828. }
  829. }
  830. } else {
  831. current = prev_item;
  832. while ((!current->collapsed || p_include_invisible) && current->last_child) {
  833. current = current->last_child;
  834. }
  835. }
  836. return current;
  837. }
  838. TreeItem *TreeItem::get_prev_visible(bool p_wrap) {
  839. TreeItem *loop = this;
  840. TreeItem *prev_item = _get_prev_in_tree(p_wrap);
  841. while (prev_item && !prev_item->is_visible_in_tree()) {
  842. prev_item = prev_item->_get_prev_in_tree(p_wrap);
  843. if (prev_item == loop) {
  844. // Check that we haven't looped all the way around to the start.
  845. prev_item = nullptr;
  846. break;
  847. }
  848. }
  849. return prev_item;
  850. }
  851. TreeItem *TreeItem::_get_next_in_tree(bool p_wrap, bool p_include_invisible) {
  852. TreeItem *current = this;
  853. if ((!current->collapsed || p_include_invisible) && current->first_child) {
  854. current = current->first_child;
  855. } else if (current->next) {
  856. current = current->next;
  857. } else {
  858. while (current && !current->next) {
  859. current = current->parent;
  860. }
  861. if (!current) {
  862. if (p_wrap) {
  863. return tree->root;
  864. } else {
  865. return nullptr;
  866. }
  867. } else {
  868. current = current->next;
  869. }
  870. }
  871. return current;
  872. }
  873. TreeItem *TreeItem::get_next_visible(bool p_wrap) {
  874. TreeItem *loop = this;
  875. TreeItem *next_item = _get_next_in_tree(p_wrap);
  876. while (next_item && !next_item->is_visible_in_tree()) {
  877. next_item = next_item->_get_next_in_tree(p_wrap);
  878. if (next_item == loop) {
  879. // Check that we haven't looped all the way around to the start.
  880. next_item = nullptr;
  881. break;
  882. }
  883. }
  884. return next_item;
  885. }
  886. TreeItem *TreeItem::get_prev_in_tree(bool p_wrap) {
  887. TreeItem *prev_item = _get_prev_in_tree(p_wrap, true);
  888. return prev_item;
  889. }
  890. TreeItem *TreeItem::get_next_in_tree(bool p_wrap) {
  891. TreeItem *next_item = _get_next_in_tree(p_wrap, true);
  892. return next_item;
  893. }
  894. TreeItem *TreeItem::get_child(int p_index) {
  895. _create_children_cache();
  896. if (p_index < 0) {
  897. p_index += children_cache.size();
  898. }
  899. ERR_FAIL_INDEX_V(p_index, children_cache.size(), nullptr);
  900. return children_cache.get(p_index);
  901. }
  902. int TreeItem::get_visible_child_count() {
  903. _create_children_cache();
  904. int visible_count = 0;
  905. for (int i = 0; i < children_cache.size(); i++) {
  906. if (children_cache[i]->is_visible()) {
  907. visible_count += 1;
  908. }
  909. }
  910. return visible_count;
  911. }
  912. int TreeItem::get_child_count() {
  913. _create_children_cache();
  914. return children_cache.size();
  915. }
  916. TypedArray<TreeItem> TreeItem::get_children() {
  917. // Don't need to explicitly create children cache, because get_child_count creates it.
  918. int size = get_child_count();
  919. TypedArray<TreeItem> arr;
  920. arr.resize(size);
  921. for (int i = 0; i < size; i++) {
  922. arr[i] = children_cache[i];
  923. }
  924. return arr;
  925. }
  926. void TreeItem::clear_children() {
  927. TreeItem *c = first_child;
  928. while (c) {
  929. TreeItem *aux = c;
  930. c = c->get_next();
  931. aux->parent = nullptr; // So it won't try to recursively auto-remove from me in here.
  932. memdelete(aux);
  933. }
  934. first_child = nullptr;
  935. last_child = nullptr;
  936. children_cache.clear();
  937. }
  938. int TreeItem::get_index() {
  939. int idx = 0;
  940. TreeItem *c = this;
  941. while (c) {
  942. c = c->get_prev();
  943. idx++;
  944. }
  945. return idx - 1;
  946. }
  947. #ifdef DEV_ENABLED
  948. void TreeItem::validate_cache() const {
  949. if (!parent || parent->children_cache.is_empty()) {
  950. return;
  951. }
  952. TreeItem *scan = parent->first_child;
  953. int index = 0;
  954. while (scan) {
  955. DEV_ASSERT(parent->children_cache[index] == scan);
  956. ++index;
  957. scan = scan->get_next();
  958. }
  959. DEV_ASSERT(index == parent->children_cache.size());
  960. }
  961. #endif
  962. void TreeItem::move_before(TreeItem *p_item) {
  963. ERR_FAIL_NULL(p_item);
  964. ERR_FAIL_COND(is_root);
  965. ERR_FAIL_NULL(p_item->parent);
  966. if (p_item == this) {
  967. return;
  968. }
  969. TreeItem *p = p_item->parent;
  970. while (p) {
  971. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  972. p = p->parent;
  973. }
  974. Tree *old_tree = tree;
  975. _unlink_from_tree();
  976. _change_tree(p_item->tree);
  977. parent = p_item->parent;
  978. TreeItem *item_prev = p_item->get_prev();
  979. if (item_prev) {
  980. item_prev->next = this;
  981. parent->children_cache.clear();
  982. } else {
  983. parent->first_child = this;
  984. // If the cache is empty, it has not been built but there
  985. // are items in the tree (note p_item != nullptr) so we cannot update it.
  986. if (!parent->children_cache.is_empty()) {
  987. parent->children_cache.insert(0, this);
  988. }
  989. }
  990. prev = item_prev;
  991. next = p_item;
  992. p_item->prev = this;
  993. if (tree && old_tree == tree) {
  994. tree->queue_accessibility_update();
  995. tree->queue_redraw();
  996. }
  997. validate_cache();
  998. }
  999. void TreeItem::move_after(TreeItem *p_item) {
  1000. ERR_FAIL_NULL(p_item);
  1001. ERR_FAIL_COND(is_root);
  1002. ERR_FAIL_NULL(p_item->parent);
  1003. if (p_item == this) {
  1004. return;
  1005. }
  1006. TreeItem *p = p_item->parent;
  1007. while (p) {
  1008. ERR_FAIL_COND_MSG(p == this, "Can't move to a descendant");
  1009. p = p->parent;
  1010. }
  1011. Tree *old_tree = tree;
  1012. _unlink_from_tree();
  1013. _change_tree(p_item->tree);
  1014. if (p_item->next) {
  1015. p_item->next->prev = this;
  1016. }
  1017. parent = p_item->parent;
  1018. prev = p_item;
  1019. next = p_item->next;
  1020. p_item->next = this;
  1021. if (next) {
  1022. parent->children_cache.clear();
  1023. } else {
  1024. parent->last_child = this;
  1025. // If the cache is empty, it has not been built but there
  1026. // are items in the tree (note p_item != nullptr,) so we cannot update it.
  1027. if (!parent->children_cache.is_empty()) {
  1028. parent->children_cache.append(this);
  1029. }
  1030. }
  1031. if (tree && old_tree == tree) {
  1032. tree->queue_accessibility_update();
  1033. tree->queue_redraw();
  1034. }
  1035. validate_cache();
  1036. }
  1037. void TreeItem::set_selectable(int p_column, bool p_selectable) {
  1038. ERR_FAIL_INDEX(p_column, cells.size());
  1039. cells.write[p_column].selectable = p_selectable;
  1040. }
  1041. bool TreeItem::is_selectable(int p_column) const {
  1042. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1043. return cells[p_column].selectable;
  1044. }
  1045. bool TreeItem::is_selected(int p_column) {
  1046. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1047. return cells[p_column].selectable && cells[p_column].selected;
  1048. }
  1049. bool TreeItem::is_any_column_selected() const {
  1050. for (const Cell &cell : cells) {
  1051. if (cell.selectable && cell.selected) {
  1052. return true;
  1053. }
  1054. }
  1055. return false;
  1056. }
  1057. void TreeItem::set_as_cursor(int p_column) {
  1058. ERR_FAIL_INDEX(p_column, cells.size());
  1059. if (!tree) {
  1060. return;
  1061. }
  1062. if (tree->select_mode != Tree::SELECT_MULTI) {
  1063. return;
  1064. }
  1065. if (tree->selected_item == this && tree->selected_col == p_column) {
  1066. return;
  1067. }
  1068. tree->selected_item = this;
  1069. tree->selected_col = p_column;
  1070. tree->selected_button = -1;
  1071. tree->queue_accessibility_update();
  1072. tree->queue_redraw();
  1073. }
  1074. void TreeItem::select(int p_column) {
  1075. ERR_FAIL_INDEX(p_column, cells.size());
  1076. _cell_selected(p_column);
  1077. }
  1078. void TreeItem::deselect(int p_column) {
  1079. ERR_FAIL_INDEX(p_column, cells.size());
  1080. _cell_deselected(p_column);
  1081. }
  1082. void TreeItem::clear_buttons() {
  1083. int i = 0;
  1084. for (Cell &cell : cells) {
  1085. if (!cell.buttons.is_empty()) {
  1086. for (Cell::Button &btn : cell.buttons) {
  1087. if (btn.accessibility_button_element.is_valid()) {
  1088. DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
  1089. }
  1090. }
  1091. cell.buttons.clear();
  1092. cell.cached_minimum_size_dirty = true;
  1093. _changed_notify(i);
  1094. }
  1095. ++i;
  1096. }
  1097. }
  1098. void TreeItem::add_button(int p_column, const Ref<Texture2D> &p_button, int p_id, bool p_disabled, const String &p_tooltip, const String &p_description) {
  1099. ERR_FAIL_INDEX(p_column, cells.size());
  1100. ERR_FAIL_COND(p_button.is_null());
  1101. TreeItem::Cell::Button button;
  1102. button.texture = p_button;
  1103. if (p_id < 0) {
  1104. p_id = cells[p_column].buttons.size();
  1105. }
  1106. button.id = p_id;
  1107. button.disabled = p_disabled;
  1108. button.tooltip = p_tooltip;
  1109. button.description = p_description;
  1110. cells.write[p_column].buttons.push_back(button);
  1111. cells.write[p_column].cached_minimum_size_dirty = true;
  1112. _changed_notify(p_column);
  1113. if (get_tree()) {
  1114. get_tree()->update_configuration_warnings();
  1115. }
  1116. }
  1117. int TreeItem::get_button_count(int p_column) const {
  1118. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1119. return cells[p_column].buttons.size();
  1120. }
  1121. Ref<Texture2D> TreeItem::get_button(int p_column, int p_index) const {
  1122. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture2D>());
  1123. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Ref<Texture2D>());
  1124. return cells[p_column].buttons[p_index].texture;
  1125. }
  1126. String TreeItem::get_button_tooltip_text(int p_column, int p_index) const {
  1127. ERR_FAIL_INDEX_V(p_column, cells.size(), String());
  1128. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), String());
  1129. return cells[p_column].buttons[p_index].tooltip;
  1130. }
  1131. int TreeItem::get_button_id(int p_column, int p_index) const {
  1132. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1133. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), -1);
  1134. return cells[p_column].buttons[p_index].id;
  1135. }
  1136. void TreeItem::erase_button(int p_column, int p_index) {
  1137. ERR_FAIL_INDEX(p_column, cells.size());
  1138. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1139. if (cells[p_column].buttons[p_index].accessibility_button_element.is_valid()) {
  1140. DisplayServer::get_singleton()->accessibility_free_element(cells.write[p_column].buttons.write[p_index].accessibility_button_element);
  1141. }
  1142. cells.write[p_column].buttons.remove_at(p_index);
  1143. _changed_notify(p_column);
  1144. if (get_tree()) {
  1145. get_tree()->update_configuration_warnings();
  1146. }
  1147. }
  1148. int TreeItem::get_button_by_id(int p_column, int p_id) const {
  1149. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1150. for (int i = 0; i < cells[p_column].buttons.size(); i++) {
  1151. if (cells[p_column].buttons[i].id == p_id) {
  1152. return i;
  1153. }
  1154. }
  1155. return -1;
  1156. }
  1157. Color TreeItem::get_button_color(int p_column, int p_index) const {
  1158. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1159. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), Color());
  1160. return cells[p_column].buttons[p_index].color;
  1161. }
  1162. void TreeItem::set_button_tooltip_text(int p_column, int p_index, const String &p_tooltip) {
  1163. ERR_FAIL_INDEX(p_column, cells.size());
  1164. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1165. cells.write[p_column].buttons.write[p_index].tooltip = p_tooltip;
  1166. _changed_notify(p_column);
  1167. }
  1168. void TreeItem::set_button(int p_column, int p_index, const Ref<Texture2D> &p_button) {
  1169. ERR_FAIL_COND(p_button.is_null());
  1170. ERR_FAIL_INDEX(p_column, cells.size());
  1171. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1172. if (cells[p_column].buttons[p_index].texture == p_button) {
  1173. return;
  1174. }
  1175. cells.write[p_column].buttons.write[p_index].texture = p_button;
  1176. cells.write[p_column].cached_minimum_size_dirty = true;
  1177. _changed_notify(p_column);
  1178. }
  1179. void TreeItem::set_button_description(int p_column, int p_index, const String &p_description) {
  1180. ERR_FAIL_INDEX(p_column, cells.size());
  1181. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1182. if (cells[p_column].buttons[p_index].description == p_description) {
  1183. return;
  1184. }
  1185. cells.write[p_column].buttons.write[p_index].description = p_description;
  1186. _changed_notify(p_column);
  1187. if (get_tree()) {
  1188. get_tree()->update_configuration_warnings();
  1189. }
  1190. }
  1191. void TreeItem::set_button_color(int p_column, int p_index, const Color &p_color) {
  1192. ERR_FAIL_INDEX(p_column, cells.size());
  1193. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1194. if (cells[p_column].buttons[p_index].color == p_color) {
  1195. return;
  1196. }
  1197. cells.write[p_column].buttons.write[p_index].color = p_color;
  1198. _changed_notify(p_column);
  1199. }
  1200. void TreeItem::set_button_disabled(int p_column, int p_index, bool p_disabled) {
  1201. ERR_FAIL_INDEX(p_column, cells.size());
  1202. ERR_FAIL_INDEX(p_index, cells[p_column].buttons.size());
  1203. if (cells[p_column].buttons[p_index].disabled == p_disabled) {
  1204. return;
  1205. }
  1206. cells.write[p_column].buttons.write[p_index].disabled = p_disabled;
  1207. cells.write[p_column].cached_minimum_size_dirty = true;
  1208. _changed_notify(p_column);
  1209. }
  1210. bool TreeItem::is_button_disabled(int p_column, int p_index) const {
  1211. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1212. ERR_FAIL_INDEX_V(p_index, cells[p_column].buttons.size(), false);
  1213. return cells[p_column].buttons[p_index].disabled;
  1214. }
  1215. void TreeItem::set_editable(int p_column, bool p_editable) {
  1216. ERR_FAIL_INDEX(p_column, cells.size());
  1217. if (cells[p_column].editable == p_editable) {
  1218. return;
  1219. }
  1220. cells.write[p_column].editable = p_editable;
  1221. cells.write[p_column].cached_minimum_size_dirty = true;
  1222. _changed_notify(p_column);
  1223. }
  1224. bool TreeItem::is_editable(int p_column) {
  1225. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1226. return cells[p_column].editable;
  1227. }
  1228. void TreeItem::set_custom_color(int p_column, const Color &p_color) {
  1229. ERR_FAIL_INDEX(p_column, cells.size());
  1230. if (cells[p_column].custom_color && cells[p_column].color == p_color) {
  1231. return;
  1232. }
  1233. cells.write[p_column].custom_color = true;
  1234. cells.write[p_column].color = p_color;
  1235. _changed_notify(p_column);
  1236. }
  1237. Color TreeItem::get_custom_color(int p_column) const {
  1238. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1239. if (!cells[p_column].custom_color) {
  1240. return Color();
  1241. }
  1242. return cells[p_column].color;
  1243. }
  1244. void TreeItem::clear_custom_color(int p_column) {
  1245. ERR_FAIL_INDEX(p_column, cells.size());
  1246. cells.write[p_column].custom_color = false;
  1247. cells.write[p_column].color = Color();
  1248. _changed_notify(p_column);
  1249. }
  1250. void TreeItem::set_custom_font(int p_column, const Ref<Font> &p_font) {
  1251. ERR_FAIL_INDEX(p_column, cells.size());
  1252. if (cells[p_column].custom_font == p_font) {
  1253. return;
  1254. }
  1255. cells.write[p_column].custom_font = p_font;
  1256. cells.write[p_column].cached_minimum_size_dirty = true;
  1257. _changed_notify(p_column);
  1258. }
  1259. Ref<Font> TreeItem::get_custom_font(int p_column) const {
  1260. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Font>());
  1261. return cells[p_column].custom_font;
  1262. }
  1263. void TreeItem::set_custom_font_size(int p_column, int p_font_size) {
  1264. ERR_FAIL_INDEX(p_column, cells.size());
  1265. if (cells[p_column].custom_font_size == p_font_size) {
  1266. return;
  1267. }
  1268. cells.write[p_column].custom_font_size = p_font_size;
  1269. cells.write[p_column].cached_minimum_size_dirty = true;
  1270. _changed_notify(p_column);
  1271. }
  1272. int TreeItem::get_custom_font_size(int p_column) const {
  1273. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  1274. return cells[p_column].custom_font_size;
  1275. }
  1276. void TreeItem::set_tooltip_text(int p_column, const String &p_tooltip) {
  1277. ERR_FAIL_INDEX(p_column, cells.size());
  1278. cells.write[p_column].tooltip = p_tooltip;
  1279. }
  1280. String TreeItem::get_tooltip_text(int p_column) const {
  1281. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  1282. return cells[p_column].tooltip;
  1283. }
  1284. void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
  1285. ERR_FAIL_INDEX(p_column, cells.size());
  1286. if (cells[p_column].custom_bg_color && cells[p_column].custom_bg_outline == p_bg_outline && cells[p_column].bg_color == p_color) {
  1287. return;
  1288. }
  1289. cells.write[p_column].custom_bg_color = true;
  1290. cells.write[p_column].custom_bg_outline = p_bg_outline;
  1291. cells.write[p_column].bg_color = p_color;
  1292. _changed_notify(p_column);
  1293. }
  1294. void TreeItem::clear_custom_bg_color(int p_column) {
  1295. ERR_FAIL_INDEX(p_column, cells.size());
  1296. cells.write[p_column].custom_bg_color = false;
  1297. cells.write[p_column].bg_color = Color();
  1298. _changed_notify(p_column);
  1299. }
  1300. Color TreeItem::get_custom_bg_color(int p_column) const {
  1301. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  1302. if (!cells[p_column].custom_bg_color) {
  1303. return Color();
  1304. }
  1305. return cells[p_column].bg_color;
  1306. }
  1307. void TreeItem::set_custom_as_button(int p_column, bool p_button) {
  1308. ERR_FAIL_INDEX(p_column, cells.size());
  1309. if (cells[p_column].custom_button == p_button) {
  1310. return;
  1311. }
  1312. cells.write[p_column].custom_button = p_button;
  1313. cells.write[p_column].cached_minimum_size_dirty = true;
  1314. _changed_notify(p_column);
  1315. }
  1316. bool TreeItem::is_custom_set_as_button(int p_column) const {
  1317. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1318. return cells[p_column].custom_button;
  1319. }
  1320. void TreeItem::set_text_alignment(int p_column, HorizontalAlignment p_alignment) {
  1321. ERR_FAIL_INDEX(p_column, cells.size());
  1322. if (cells[p_column].text_alignment == p_alignment) {
  1323. return;
  1324. }
  1325. cells.write[p_column].text_alignment = p_alignment;
  1326. cells.write[p_column].cached_minimum_size_dirty = true;
  1327. _changed_notify(p_column);
  1328. }
  1329. HorizontalAlignment TreeItem::get_text_alignment(int p_column) const {
  1330. ERR_FAIL_INDEX_V(p_column, cells.size(), HORIZONTAL_ALIGNMENT_LEFT);
  1331. return cells[p_column].text_alignment;
  1332. }
  1333. void TreeItem::set_expand_right(int p_column, bool p_enable) {
  1334. ERR_FAIL_INDEX(p_column, cells.size());
  1335. if (cells[p_column].expand_right == p_enable) {
  1336. return;
  1337. }
  1338. cells.write[p_column].expand_right = p_enable;
  1339. cells.write[p_column].cached_minimum_size_dirty = true;
  1340. _changed_notify(p_column);
  1341. }
  1342. bool TreeItem::get_expand_right(int p_column) const {
  1343. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  1344. return cells[p_column].expand_right;
  1345. }
  1346. void TreeItem::set_disable_folding(bool p_disable) {
  1347. if (disable_folding == p_disable) {
  1348. return;
  1349. }
  1350. disable_folding = p_disable;
  1351. for (Cell &c : cells) {
  1352. c.cached_minimum_size_dirty = true;
  1353. }
  1354. _changed_notify(0);
  1355. }
  1356. bool TreeItem::is_folding_disabled() const {
  1357. return disable_folding;
  1358. }
  1359. Size2 TreeItem::get_minimum_size(int p_column) {
  1360. ERR_FAIL_INDEX_V(p_column, cells.size(), Size2());
  1361. Tree *parent_tree = get_tree();
  1362. ERR_FAIL_NULL_V(parent_tree, Size2());
  1363. const TreeItem::Cell &cell = cells[p_column];
  1364. if (cell.cached_minimum_size_dirty || cell.text_buf->is_dirty() || cell.dirty) {
  1365. Size2 size = Size2(
  1366. parent_tree->theme_cache.inner_item_margin_left + parent_tree->theme_cache.inner_item_margin_right,
  1367. parent_tree->theme_cache.inner_item_margin_top + parent_tree->theme_cache.inner_item_margin_bottom);
  1368. // Text.
  1369. if (!cell.text.is_empty()) {
  1370. if (cell.dirty) {
  1371. parent_tree->update_item_cell(this, p_column);
  1372. }
  1373. Size2 text_size = cell.text_buf->get_size();
  1374. if (get_text_overrun_behavior(p_column) == TextServer::OVERRUN_NO_TRIMMING) {
  1375. size.width += text_size.width;
  1376. }
  1377. size.height = MAX(size.height, text_size.height);
  1378. }
  1379. // Icon.
  1380. if (cell.mode == CELL_MODE_CHECK) {
  1381. Size2i check_size = parent_tree->theme_cache.checked->get_size();
  1382. size.width += check_size.width + parent_tree->theme_cache.h_separation;
  1383. size.height = MAX(size.height, check_size.height);
  1384. }
  1385. if (cell.icon.is_valid()) {
  1386. Size2i icon_size = parent_tree->_get_cell_icon_size(cell);
  1387. size.width += icon_size.width + parent_tree->theme_cache.h_separation;
  1388. size.height = MAX(size.height, icon_size.height);
  1389. }
  1390. // Buttons.
  1391. for (int i = 0; i < cell.buttons.size(); i++) {
  1392. Ref<Texture2D> texture = cell.buttons[i].texture;
  1393. if (texture.is_valid()) {
  1394. Size2 button_size = texture->get_size();
  1395. button_size.width += parent_tree->theme_cache.button_pressed->get_minimum_size().width;
  1396. size.width += button_size.width + parent_tree->theme_cache.button_margin;
  1397. size.height = MAX(size.height, button_size.height);
  1398. }
  1399. }
  1400. cells.write[p_column].cached_minimum_size = size;
  1401. cells.write[p_column].cached_minimum_size_dirty = false;
  1402. }
  1403. return cell.cached_minimum_size;
  1404. }
  1405. void TreeItem::_call_recursive_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1406. if (p_argcount < 1) {
  1407. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1408. r_error.expected = 1;
  1409. return;
  1410. }
  1411. if (!p_args[0]->is_string()) {
  1412. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1413. r_error.argument = 0;
  1414. r_error.expected = Variant::STRING_NAME;
  1415. return;
  1416. }
  1417. StringName method = *p_args[0];
  1418. call_recursive(method, &p_args[1], p_argcount - 1, r_error);
  1419. }
  1420. void recursive_call_aux(TreeItem *p_item, const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1421. if (!p_item) {
  1422. return;
  1423. }
  1424. p_item->callp(p_method, p_args, p_argcount, r_error);
  1425. TreeItem *c = p_item->get_first_child();
  1426. while (c) {
  1427. recursive_call_aux(c, p_method, p_args, p_argcount, r_error);
  1428. c = c->get_next();
  1429. }
  1430. }
  1431. void TreeItem::call_recursive(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1432. recursive_call_aux(this, p_method, p_args, p_argcount, r_error);
  1433. }
  1434. void TreeItem::_bind_methods() {
  1435. ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
  1436. ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
  1437. ClassDB::bind_method(D_METHOD("set_auto_translate_mode", "column", "mode"), &TreeItem::set_auto_translate_mode);
  1438. ClassDB::bind_method(D_METHOD("get_auto_translate_mode", "column"), &TreeItem::get_auto_translate_mode);
  1439. ClassDB::bind_method(D_METHOD("set_edit_multiline", "column", "multiline"), &TreeItem::set_edit_multiline);
  1440. ClassDB::bind_method(D_METHOD("is_edit_multiline", "column"), &TreeItem::is_edit_multiline);
  1441. ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
  1442. ClassDB::bind_method(D_METHOD("set_indeterminate", "column", "indeterminate"), &TreeItem::set_indeterminate);
  1443. ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
  1444. ClassDB::bind_method(D_METHOD("is_indeterminate", "column"), &TreeItem::is_indeterminate);
  1445. ClassDB::bind_method(D_METHOD("propagate_check", "column", "emit_signal"), &TreeItem::propagate_check, DEFVAL(true));
  1446. ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
  1447. ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
  1448. ClassDB::bind_method(D_METHOD("set_description", "column", "description"), &TreeItem::set_description);
  1449. ClassDB::bind_method(D_METHOD("get_description", "column"), &TreeItem::get_description);
  1450. ClassDB::bind_method(D_METHOD("set_text_direction", "column", "direction"), &TreeItem::set_text_direction);
  1451. ClassDB::bind_method(D_METHOD("get_text_direction", "column"), &TreeItem::get_text_direction);
  1452. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "column", "autowrap_mode"), &TreeItem::set_autowrap_mode);
  1453. ClassDB::bind_method(D_METHOD("get_autowrap_mode", "column"), &TreeItem::get_autowrap_mode);
  1454. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "column", "overrun_behavior"), &TreeItem::set_text_overrun_behavior);
  1455. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior", "column"), &TreeItem::get_text_overrun_behavior);
  1456. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "column", "parser"), &TreeItem::set_structured_text_bidi_override);
  1457. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override", "column"), &TreeItem::get_structured_text_bidi_override);
  1458. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "column", "args"), &TreeItem::set_structured_text_bidi_override_options);
  1459. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options", "column"), &TreeItem::get_structured_text_bidi_override_options);
  1460. ClassDB::bind_method(D_METHOD("set_language", "column", "language"), &TreeItem::set_language);
  1461. ClassDB::bind_method(D_METHOD("get_language", "column"), &TreeItem::get_language);
  1462. ClassDB::bind_method(D_METHOD("set_suffix", "column", "text"), &TreeItem::set_suffix);
  1463. ClassDB::bind_method(D_METHOD("get_suffix", "column"), &TreeItem::get_suffix);
  1464. ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
  1465. ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
  1466. ClassDB::bind_method(D_METHOD("set_icon_overlay", "column", "texture"), &TreeItem::set_icon_overlay);
  1467. ClassDB::bind_method(D_METHOD("get_icon_overlay", "column"), &TreeItem::get_icon_overlay);
  1468. ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
  1469. ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
  1470. ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
  1471. ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
  1472. ClassDB::bind_method(D_METHOD("set_icon_modulate", "column", "modulate"), &TreeItem::set_icon_modulate);
  1473. ClassDB::bind_method(D_METHOD("get_icon_modulate", "column"), &TreeItem::get_icon_modulate);
  1474. ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
  1475. ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
  1476. ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
  1477. ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
  1478. ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
  1479. ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
  1480. #ifndef DISABLE_DEPRECATED
  1481. ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
  1482. #endif // DISABLE_DEPRECATED
  1483. ClassDB::bind_method(D_METHOD("set_custom_draw_callback", "column", "callback"), &TreeItem::set_custom_draw_callback);
  1484. ClassDB::bind_method(D_METHOD("get_custom_draw_callback", "column"), &TreeItem::get_custom_draw_callback);
  1485. ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
  1486. ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
  1487. ClassDB::bind_method(D_METHOD("set_collapsed_recursive", "enable"), &TreeItem::set_collapsed_recursive);
  1488. ClassDB::bind_method(D_METHOD("is_any_collapsed", "only_visible"), &TreeItem::is_any_collapsed, DEFVAL(false));
  1489. ClassDB::bind_method(D_METHOD("set_visible", "enable"), &TreeItem::set_visible);
  1490. ClassDB::bind_method(D_METHOD("is_visible"), &TreeItem::is_visible);
  1491. ClassDB::bind_method(D_METHOD("is_visible_in_tree"), &TreeItem::is_visible_in_tree);
  1492. ClassDB::bind_method(D_METHOD("uncollapse_tree"), &TreeItem::uncollapse_tree);
  1493. ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
  1494. ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
  1495. ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
  1496. ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
  1497. ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
  1498. ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
  1499. ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
  1500. ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
  1501. ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
  1502. ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
  1503. ClassDB::bind_method(D_METHOD("get_custom_color", "column"), &TreeItem::get_custom_color);
  1504. ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
  1505. ClassDB::bind_method(D_METHOD("set_custom_font", "column", "font"), &TreeItem::set_custom_font);
  1506. ClassDB::bind_method(D_METHOD("get_custom_font", "column"), &TreeItem::get_custom_font);
  1507. ClassDB::bind_method(D_METHOD("set_custom_font_size", "column", "font_size"), &TreeItem::set_custom_font_size);
  1508. ClassDB::bind_method(D_METHOD("get_custom_font_size", "column"), &TreeItem::get_custom_font_size);
  1509. ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
  1510. ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
  1511. ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
  1512. ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
  1513. ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
  1514. ClassDB::bind_method(D_METHOD("clear_buttons"), &TreeItem::clear_buttons);
  1515. ClassDB::bind_method(D_METHOD("add_button", "column", "button", "id", "disabled", "tooltip_text", "description"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""), DEFVAL(""));
  1516. ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
  1517. ClassDB::bind_method(D_METHOD("get_button_tooltip_text", "column", "button_index"), &TreeItem::get_button_tooltip_text);
  1518. ClassDB::bind_method(D_METHOD("get_button_id", "column", "button_index"), &TreeItem::get_button_id);
  1519. ClassDB::bind_method(D_METHOD("get_button_by_id", "column", "id"), &TreeItem::get_button_by_id);
  1520. ClassDB::bind_method(D_METHOD("get_button_color", "column", "id"), &TreeItem::get_button_color);
  1521. ClassDB::bind_method(D_METHOD("get_button", "column", "button_index"), &TreeItem::get_button);
  1522. ClassDB::bind_method(D_METHOD("set_button_tooltip_text", "column", "button_index", "tooltip"), &TreeItem::set_button_tooltip_text);
  1523. ClassDB::bind_method(D_METHOD("set_button", "column", "button_index", "button"), &TreeItem::set_button);
  1524. ClassDB::bind_method(D_METHOD("erase_button", "column", "button_index"), &TreeItem::erase_button);
  1525. ClassDB::bind_method(D_METHOD("set_button_description", "column", "button_index", "description"), &TreeItem::set_button_description);
  1526. ClassDB::bind_method(D_METHOD("set_button_disabled", "column", "button_index", "disabled"), &TreeItem::set_button_disabled);
  1527. ClassDB::bind_method(D_METHOD("set_button_color", "column", "button_index", "color"), &TreeItem::set_button_color);
  1528. ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_index"), &TreeItem::is_button_disabled);
  1529. ClassDB::bind_method(D_METHOD("set_tooltip_text", "column", "tooltip"), &TreeItem::set_tooltip_text);
  1530. ClassDB::bind_method(D_METHOD("get_tooltip_text", "column"), &TreeItem::get_tooltip_text);
  1531. ClassDB::bind_method(D_METHOD("set_text_alignment", "column", "text_alignment"), &TreeItem::set_text_alignment);
  1532. ClassDB::bind_method(D_METHOD("get_text_alignment", "column"), &TreeItem::get_text_alignment);
  1533. ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
  1534. ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
  1535. ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
  1536. ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
  1537. ClassDB::bind_method(D_METHOD("create_child", "index"), &TreeItem::create_child, DEFVAL(-1));
  1538. ClassDB::bind_method(D_METHOD("add_child", "child"), &TreeItem::add_child);
  1539. ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::remove_child);
  1540. ClassDB::bind_method(D_METHOD("get_tree"), &TreeItem::get_tree);
  1541. ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
  1542. ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
  1543. ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
  1544. ClassDB::bind_method(D_METHOD("get_first_child"), &TreeItem::get_first_child);
  1545. ClassDB::bind_method(D_METHOD("get_next_in_tree", "wrap"), &TreeItem::get_next_in_tree, DEFVAL(false));
  1546. ClassDB::bind_method(D_METHOD("get_prev_in_tree", "wrap"), &TreeItem::get_prev_in_tree, DEFVAL(false));
  1547. ClassDB::bind_method(D_METHOD("get_next_visible", "wrap"), &TreeItem::get_next_visible, DEFVAL(false));
  1548. ClassDB::bind_method(D_METHOD("get_prev_visible", "wrap"), &TreeItem::get_prev_visible, DEFVAL(false));
  1549. ClassDB::bind_method(D_METHOD("get_child", "index"), &TreeItem::get_child);
  1550. ClassDB::bind_method(D_METHOD("get_child_count"), &TreeItem::get_child_count);
  1551. ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
  1552. ClassDB::bind_method(D_METHOD("get_index"), &TreeItem::get_index);
  1553. ClassDB::bind_method(D_METHOD("move_before", "item"), &TreeItem::move_before);
  1554. ClassDB::bind_method(D_METHOD("move_after", "item"), &TreeItem::move_after);
  1555. {
  1556. MethodInfo mi;
  1557. mi.name = "call_recursive";
  1558. mi.arguments.push_back(PropertyInfo(Variant::STRING_NAME, "method"));
  1559. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_recursive", &TreeItem::_call_recursive_bind, mi);
  1560. }
  1561. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  1562. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "visible"), "set_visible", "is_visible");
  1563. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
  1564. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
  1565. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  1566. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  1567. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  1568. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  1569. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  1570. }
  1571. TreeItem::TreeItem(Tree *p_tree) {
  1572. tree = p_tree;
  1573. }
  1574. TreeItem::~TreeItem() {
  1575. _unlink_from_tree();
  1576. _change_tree(nullptr);
  1577. validate_cache();
  1578. prev = nullptr;
  1579. clear_children();
  1580. }
  1581. /*************************** END of TreeItem ***************************/
  1582. void Tree::_update_theme_item_cache() {
  1583. Control::_update_theme_item_cache();
  1584. theme_cache.base_scale = get_theme_default_base_scale();
  1585. }
  1586. Size2 Tree::_get_cell_icon_size(const TreeItem::Cell &p_cell) const {
  1587. Size2i icon_size = p_cell.get_icon_size();
  1588. int max_width = 0;
  1589. if (theme_cache.icon_max_width > 0) {
  1590. max_width = theme_cache.icon_max_width;
  1591. }
  1592. if (p_cell.icon_max_w > 0 && (max_width == 0 || p_cell.icon_max_w < max_width)) {
  1593. max_width = p_cell.icon_max_w;
  1594. }
  1595. if (max_width > 0 && icon_size.width > max_width) {
  1596. icon_size.height = icon_size.height * max_width / icon_size.width;
  1597. icon_size.width = max_width;
  1598. }
  1599. return icon_size;
  1600. }
  1601. int Tree::compute_item_height(TreeItem *p_item) const {
  1602. if ((p_item == root && hide_root) || !p_item->is_visible_in_tree()) {
  1603. return 0;
  1604. }
  1605. ERR_FAIL_COND_V(theme_cache.font.is_null(), 0);
  1606. int height = 0;
  1607. for (int i = 0; i < columns.size(); i++) {
  1608. height = MAX(height, p_item->get_minimum_size(i).y);
  1609. }
  1610. int item_min_height = MAX(theme_cache.font->get_height(theme_cache.font_size), p_item->get_custom_minimum_height());
  1611. if (height < item_min_height) {
  1612. height = item_min_height;
  1613. }
  1614. height += theme_cache.v_separation;
  1615. return height;
  1616. }
  1617. int Tree::get_item_height(TreeItem *p_item) const {
  1618. if (!p_item->is_visible_in_tree()) {
  1619. return 0;
  1620. }
  1621. int height = compute_item_height(p_item);
  1622. height += theme_cache.v_separation;
  1623. if (!p_item->collapsed) { // If not collapsed, check the children.
  1624. TreeItem *c = p_item->first_child;
  1625. while (c) {
  1626. height += get_item_height(c);
  1627. c = c->next;
  1628. }
  1629. }
  1630. return height;
  1631. }
  1632. void Tree::draw_item_rect(const 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) const {
  1633. ERR_FAIL_COND(theme_cache.font.is_null());
  1634. Rect2i rect = p_rect.grow_individual(-theme_cache.inner_item_margin_left, -theme_cache.inner_item_margin_top, -theme_cache.inner_item_margin_right, -theme_cache.inner_item_margin_bottom);
  1635. Size2 ts = p_cell.text_buf->get_size();
  1636. bool rtl = is_layout_rtl();
  1637. Size2i icon_size;
  1638. if (p_cell.icon.is_valid()) {
  1639. icon_size = _get_cell_icon_size(p_cell);
  1640. }
  1641. int displayed_width = 0;
  1642. if (p_cell.icon.is_valid()) {
  1643. displayed_width += icon_size.width + theme_cache.h_separation;
  1644. }
  1645. if (displayed_width + ts.width > rect.size.width) {
  1646. ts.width = rect.size.width - displayed_width;
  1647. }
  1648. displayed_width += ts.width;
  1649. int empty_width = rect.size.width - displayed_width;
  1650. switch (p_cell.text_alignment) {
  1651. case HORIZONTAL_ALIGNMENT_FILL:
  1652. case HORIZONTAL_ALIGNMENT_LEFT: {
  1653. if (rtl) {
  1654. rect.position.x += empty_width;
  1655. }
  1656. } break;
  1657. case HORIZONTAL_ALIGNMENT_CENTER:
  1658. rect.position.x += empty_width / 2;
  1659. break;
  1660. case HORIZONTAL_ALIGNMENT_RIGHT:
  1661. if (!rtl) {
  1662. rect.position.x += empty_width;
  1663. }
  1664. break;
  1665. }
  1666. RID ci = get_canvas_item();
  1667. if (rtl) {
  1668. if (ts.width > 0) {
  1669. Point2 draw_pos = rect.position;
  1670. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
  1671. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1672. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1673. }
  1674. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1675. }
  1676. rect.position.x += ts.width + theme_cache.h_separation;
  1677. }
  1678. if (p_cell.icon.is_valid()) {
  1679. p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - icon_size.y) / 2)), icon_size, p_icon_color);
  1680. rect.position.x += icon_size.x + theme_cache.h_separation;
  1681. }
  1682. if (!rtl && ts.width > 0) {
  1683. Point2 draw_pos = rect.position;
  1684. draw_pos.y += Math::floor((rect.size.y - p_cell.text_buf->get_size().y) * 0.5);
  1685. if (p_ol_size > 0 && p_ol_color.a > 0) {
  1686. p_cell.text_buf->draw_outline(ci, draw_pos, p_ol_size, p_ol_color);
  1687. }
  1688. p_cell.text_buf->draw(ci, draw_pos, p_color);
  1689. }
  1690. }
  1691. void Tree::update_column(int p_col) {
  1692. columns.write[p_col].text_buf->clear();
  1693. if (columns[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1694. columns.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1695. } else {
  1696. columns.write[p_col].text_buf->set_direction((TextServer::Direction)columns[p_col].text_direction);
  1697. }
  1698. columns.write[p_col].xl_title = atr(columns[p_col].title);
  1699. columns.write[p_col].text_buf->add_string(columns[p_col].xl_title, theme_cache.tb_font, theme_cache.tb_font_size, columns[p_col].language);
  1700. columns.write[p_col].cached_minimum_width_dirty = true;
  1701. }
  1702. void Tree::update_item_cell(TreeItem *p_item, int p_col) const {
  1703. String valtext;
  1704. p_item->cells.write[p_col].text_buf->clear();
  1705. if (p_item->cells[p_col].mode == TreeItem::CELL_MODE_RANGE) {
  1706. if (!p_item->cells[p_col].text.is_empty()) {
  1707. if (!p_item->cells[p_col].editable) {
  1708. return;
  1709. }
  1710. int option = (int)p_item->cells[p_col].val;
  1711. valtext = p_item->atr(p_col, ETR("(Other)"));
  1712. Vector<String> strings = p_item->cells[p_col].text.split(",");
  1713. for (int j = 0; j < strings.size(); j++) {
  1714. int value = j;
  1715. if (!strings[j].get_slicec(':', 1).is_empty()) {
  1716. value = strings[j].get_slicec(':', 1).to_int();
  1717. }
  1718. if (option == value) {
  1719. valtext = p_item->atr(p_col, strings[j].get_slicec(':', 0));
  1720. break;
  1721. }
  1722. }
  1723. } else {
  1724. valtext = String::num(p_item->cells[p_col].val, Math::range_step_decimals(p_item->cells[p_col].step));
  1725. }
  1726. } else {
  1727. // Don't auto translate if it's in string mode and editable, as the text can be changed to anything by the user.
  1728. if (!p_item->cells[p_col].editable || p_item->cells[p_col].mode != TreeItem::CELL_MODE_STRING) {
  1729. p_item->cells.write[p_col].xl_text = p_item->atr(p_col, p_item->cells[p_col].text);
  1730. } else {
  1731. p_item->cells.write[p_col].xl_text = p_item->cells[p_col].text;
  1732. }
  1733. valtext = p_item->cells[p_col].xl_text;
  1734. }
  1735. if (!p_item->cells[p_col].suffix.is_empty()) {
  1736. if (!valtext.is_empty()) {
  1737. valtext += " ";
  1738. }
  1739. valtext += p_item->cells[p_col].suffix;
  1740. }
  1741. if (p_item->cells[p_col].text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1742. p_item->cells.write[p_col].text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1743. } else {
  1744. p_item->cells.write[p_col].text_buf->set_direction((TextServer::Direction)p_item->cells[p_col].text_direction);
  1745. }
  1746. Ref<Font> font;
  1747. if (p_item->cells[p_col].custom_font.is_valid()) {
  1748. font = p_item->cells[p_col].custom_font;
  1749. } else {
  1750. font = theme_cache.font;
  1751. }
  1752. int font_size;
  1753. if (p_item->cells[p_col].custom_font_size > 0) {
  1754. font_size = p_item->cells[p_col].custom_font_size;
  1755. } else {
  1756. font_size = theme_cache.font_size;
  1757. }
  1758. p_item->cells.write[p_col].text_buf->add_string(valtext, font, font_size, p_item->cells[p_col].language);
  1759. BitField<TextServer::LineBreakFlag> break_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES;
  1760. switch (p_item->cells.write[p_col].autowrap_mode) {
  1761. case TextServer::AUTOWRAP_OFF:
  1762. break;
  1763. case TextServer::AUTOWRAP_ARBITRARY:
  1764. break_flags.set_flag(TextServer::BREAK_GRAPHEME_BOUND);
  1765. break;
  1766. case TextServer::AUTOWRAP_WORD:
  1767. break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
  1768. break;
  1769. case TextServer::AUTOWRAP_WORD_SMART:
  1770. break_flags.set_flag(TextServer::BREAK_WORD_BOUND);
  1771. break_flags.set_flag(TextServer::BREAK_ADAPTIVE);
  1772. break;
  1773. }
  1774. p_item->cells.write[p_col].text_buf->set_break_flags(break_flags);
  1775. 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));
  1776. p_item->cells.write[p_col].dirty = false;
  1777. }
  1778. void Tree::update_item_cache(TreeItem *p_item) const {
  1779. for (int i = 0; i < p_item->cells.size(); i++) {
  1780. update_item_cell(p_item, i);
  1781. }
  1782. TreeItem *c = p_item->first_child;
  1783. while (c) {
  1784. update_item_cache(c);
  1785. c = c->next;
  1786. }
  1787. }
  1788. int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item, int &r_self_height) {
  1789. if (p_pos.y - theme_cache.offset.y > (p_draw_size.height)) {
  1790. return -1; // Draw no more!
  1791. }
  1792. if (!p_item->is_visible_in_tree()) {
  1793. return 0;
  1794. }
  1795. RID ci = get_canvas_item();
  1796. int htotal = 0;
  1797. int label_h = 0;
  1798. bool rtl = cache.rtl;
  1799. // Draw label, if height fits.
  1800. bool skip = (p_item == root && hide_root);
  1801. if (!skip) {
  1802. // Draw separation.
  1803. ERR_FAIL_COND_V(theme_cache.font.is_null(), -1);
  1804. int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin);
  1805. int skip2 = 0;
  1806. bool is_row_hovered = (!cache.hover_header_row && cache.hover_item == p_item);
  1807. for (int i = 0; i < columns.size(); i++) {
  1808. if (skip2) {
  1809. skip2--;
  1810. continue;
  1811. }
  1812. bool is_col_hovered = cache.hover_column == i;
  1813. bool is_cell_hovered = is_row_hovered && is_col_hovered;
  1814. bool is_cell_button_hovered = is_cell_hovered && cache.hover_button_index_in_column != -1;
  1815. int item_width = get_column_width(i);
  1816. if (i == 0) {
  1817. item_width -= ofs;
  1818. if (item_width <= 0) {
  1819. ofs = get_column_width(0);
  1820. continue;
  1821. }
  1822. } else {
  1823. ofs += theme_cache.h_separation;
  1824. item_width -= theme_cache.h_separation;
  1825. }
  1826. if (p_item->cells[i].expand_right) {
  1827. int plus = 1;
  1828. 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].xl_text.is_empty() && p_item->cells[i + plus].icon.is_null()) {
  1829. item_width += get_column_width(i + plus);
  1830. plus++;
  1831. skip2++;
  1832. }
  1833. }
  1834. if (!p_item->cells[i].buttons.is_empty()) {
  1835. int buttons_width = 0;
  1836. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1837. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  1838. buttons_width += button_texture->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
  1839. }
  1840. double total_ofs = ofs - theme_cache.offset.x;
  1841. // If part of the column is beyond the right side of the control due to scrolling, clamp the label width
  1842. // so that all buttons attached to the cell remain within view.
  1843. if (total_ofs + item_width > p_draw_size.width) {
  1844. item_width = MAX(buttons_width, p_draw_size.width - total_ofs);
  1845. }
  1846. }
  1847. int item_width_with_buttons = item_width; // Used later for drawing buttons.
  1848. int buttons_width = 0;
  1849. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  1850. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  1851. Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
  1852. item_width -= button_size.width + theme_cache.button_margin;
  1853. buttons_width += button_size.width + theme_cache.button_margin;
  1854. }
  1855. int text_width = item_width - theme_cache.inner_item_margin_left - theme_cache.inner_item_margin_right;
  1856. if (p_item->cells[i].icon.is_valid()) {
  1857. text_width -= _get_cell_icon_size(p_item->cells[i]).x + theme_cache.h_separation;
  1858. }
  1859. p_item->cells.write[i].text_buf->set_width(text_width);
  1860. r_self_height = compute_item_height(p_item);
  1861. label_h = r_self_height + theme_cache.v_separation;
  1862. if (p_pos.y + label_h - theme_cache.offset.y < 0) {
  1863. continue; // No need to draw.
  1864. }
  1865. Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - theme_cache.offset + p_draw_ofs, Size2i(item_width, label_h));
  1866. Rect2i cell_rect = item_rect;
  1867. if (i != 0) {
  1868. cell_rect.position.x -= theme_cache.h_separation;
  1869. cell_rect.size.x += theme_cache.h_separation;
  1870. }
  1871. if (i == 0 && select_mode == SELECT_ROW) {
  1872. if (p_item->cells[0].selected || is_row_hovered) {
  1873. const Rect2 content_rect = _get_content_rect();
  1874. Rect2i row_rect = Rect2i(Point2i(content_rect.position.x, item_rect.position.y), Size2i(content_rect.size.x, item_rect.size.y));
  1875. if (rtl) {
  1876. row_rect.position.x = get_size().width - row_rect.position.x - row_rect.size.x;
  1877. }
  1878. if (p_item->cells[0].selected) {
  1879. if (is_row_hovered) {
  1880. if (has_focus(true)) {
  1881. theme_cache.hovered_selected_focus->draw(ci, row_rect);
  1882. } else {
  1883. theme_cache.hovered_selected->draw(ci, row_rect);
  1884. }
  1885. } else {
  1886. if (has_focus(true)) {
  1887. theme_cache.selected_focus->draw(ci, row_rect);
  1888. } else {
  1889. theme_cache.selected->draw(ci, row_rect);
  1890. }
  1891. }
  1892. } else if (!drop_mode_flags) {
  1893. if (is_cell_button_hovered) {
  1894. theme_cache.hovered_dimmed->draw(ci, row_rect);
  1895. } else {
  1896. theme_cache.hovered->draw(ci, row_rect);
  1897. }
  1898. }
  1899. }
  1900. }
  1901. if (select_mode != SELECT_ROW) {
  1902. Rect2i r = cell_rect;
  1903. if (rtl) {
  1904. r.position.x = get_size().width - r.position.x - r.size.x;
  1905. }
  1906. // Cell hover.
  1907. if (is_cell_hovered && !p_item->cells[i].selected && !drop_mode_flags) {
  1908. if (is_cell_button_hovered) {
  1909. theme_cache.hovered_dimmed->draw(ci, r);
  1910. } else {
  1911. theme_cache.hovered->draw(ci, r);
  1912. }
  1913. }
  1914. }
  1915. if ((select_mode == SELECT_ROW && selected_item == p_item) || (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) || p_item->cells[i].selected || !p_item->has_meta("__focus_rect")) {
  1916. Rect2i r = cell_rect;
  1917. if (select_mode != SELECT_ROW) {
  1918. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  1919. if (rtl) {
  1920. r.position.x = get_size().width - r.position.x - r.size.x;
  1921. }
  1922. if (p_item->cells[i].selected) {
  1923. if (is_cell_hovered) {
  1924. if (has_focus(true)) {
  1925. theme_cache.hovered_selected_focus->draw(ci, r);
  1926. } else {
  1927. theme_cache.hovered_selected->draw(ci, r);
  1928. }
  1929. } else {
  1930. if (has_focus(true)) {
  1931. theme_cache.selected_focus->draw(ci, r);
  1932. } else {
  1933. theme_cache.selected->draw(ci, r);
  1934. }
  1935. }
  1936. }
  1937. } else {
  1938. p_item->set_meta("__focus_col_" + itos(i), Rect2(r.position, r.size));
  1939. }
  1940. }
  1941. if (theme_cache.draw_guides) {
  1942. Rect2 r = cell_rect;
  1943. if (rtl) {
  1944. r.position.x = get_size().width - r.position.x - r.size.x;
  1945. }
  1946. 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);
  1947. }
  1948. if (p_item->cells[i].custom_bg_color) {
  1949. Rect2 r = cell_rect;
  1950. if (i == 0) {
  1951. r.position.x = p_draw_ofs.x;
  1952. r.size.x = item_width + ofs;
  1953. }
  1954. if (rtl) {
  1955. r.position.x = get_size().width - r.position.x - r.size.x;
  1956. }
  1957. if (p_item->cells[i].custom_bg_outline) {
  1958. 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);
  1959. 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);
  1960. 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);
  1961. 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);
  1962. } else {
  1963. RenderingServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  1964. }
  1965. }
  1966. if (drop_mode_flags && drop_mode_over) {
  1967. Rect2 r = cell_rect;
  1968. if (rtl) {
  1969. r.position.x = get_size().width - r.position.x - r.size.x;
  1970. }
  1971. if (drop_mode_over == p_item) {
  1972. if (drop_mode_section == 0 || drop_mode_section == -1) {
  1973. // Line above.
  1974. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
  1975. }
  1976. if (drop_mode_section == 0) {
  1977. // Side lines.
  1978. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), theme_cache.drop_position_color);
  1979. 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);
  1980. }
  1981. if (drop_mode_section == 0 || (drop_mode_section == 1 && (!p_item->get_first_child() || p_item->is_collapsed()))) {
  1982. // Line below.
  1983. 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);
  1984. }
  1985. } else if (drop_mode_over == p_item->get_parent()) {
  1986. if (drop_mode_section == 1 && !p_item->get_prev()) {
  1987. // Line above.
  1988. RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), theme_cache.drop_position_color);
  1989. }
  1990. }
  1991. }
  1992. Color cell_color;
  1993. if (p_item->cells[i].custom_color) {
  1994. cell_color = p_item->cells[i].color;
  1995. } else {
  1996. bool draw_as_hover = !drop_mode_flags && (select_mode == SELECT_ROW ? is_row_hovered : is_cell_hovered);
  1997. bool draw_as_hover_dim = draw_as_hover && is_cell_button_hovered;
  1998. cell_color = p_item->cells[i].selected && draw_as_hover ? theme_cache.font_hovered_selected_color : (p_item->cells[i].selected ? theme_cache.font_selected_color : (draw_as_hover_dim ? theme_cache.font_hovered_dimmed_color : (draw_as_hover ? theme_cache.font_hovered_color : theme_cache.font_color)));
  1999. }
  2000. Color font_outline_color = theme_cache.font_outline_color;
  2001. int outline_size = theme_cache.font_outline_size;
  2002. Color icon_col = p_item->cells[i].icon_color;
  2003. if (p_item->cells[i].dirty) {
  2004. update_item_cell(p_item, i);
  2005. }
  2006. if (rtl) {
  2007. item_rect.position.x = get_size().width - item_rect.position.x - item_rect.size.x;
  2008. }
  2009. Point2i text_pos = item_rect.position;
  2010. text_pos.y += Math::floor((item_rect.size.y - p_item->cells[i].text_buf->get_size().y) * 0.5);
  2011. switch (p_item->cells[i].mode) {
  2012. case TreeItem::CELL_MODE_STRING: {
  2013. draw_item_rect(p_item->cells[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  2014. } break;
  2015. case TreeItem::CELL_MODE_CHECK: {
  2016. Point2i check_ofs = item_rect.position;
  2017. check_ofs.y += Math::floor((real_t)(item_rect.size.y - theme_cache.checked->get_height()) / 2);
  2018. if (p_item->cells[i].editable) {
  2019. if (p_item->cells[i].indeterminate) {
  2020. theme_cache.indeterminate->draw(ci, check_ofs);
  2021. } else if (p_item->cells[i].checked) {
  2022. theme_cache.checked->draw(ci, check_ofs);
  2023. } else {
  2024. theme_cache.unchecked->draw(ci, check_ofs);
  2025. }
  2026. } else {
  2027. if (p_item->cells[i].indeterminate) {
  2028. theme_cache.indeterminate_disabled->draw(ci, check_ofs);
  2029. } else if (p_item->cells[i].checked) {
  2030. theme_cache.checked_disabled->draw(ci, check_ofs);
  2031. } else {
  2032. theme_cache.unchecked_disabled->draw(ci, check_ofs);
  2033. }
  2034. }
  2035. int check_w = theme_cache.checked->get_width() + theme_cache.h_separation;
  2036. text_pos.x += check_w;
  2037. item_rect.size.x -= check_w;
  2038. item_rect.position.x += check_w;
  2039. if (!p_item->cells[i].editable) {
  2040. cell_color = theme_cache.font_disabled_color;
  2041. }
  2042. draw_item_rect(p_item->cells[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  2043. } break;
  2044. case TreeItem::CELL_MODE_RANGE: {
  2045. if (!p_item->cells[i].text.is_empty()) {
  2046. if (!p_item->cells[i].editable) {
  2047. break;
  2048. }
  2049. Ref<Texture2D> downarrow = theme_cache.select_arrow;
  2050. int cell_width = item_rect.size.x - downarrow->get_width();
  2051. if (rtl) {
  2052. if (outline_size > 0 && font_outline_color.a > 0) {
  2053. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  2054. }
  2055. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
  2056. } else {
  2057. if (outline_size > 0 && font_outline_color.a > 0) {
  2058. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  2059. }
  2060. p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
  2061. }
  2062. Point2i arrow_pos = item_rect.position;
  2063. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  2064. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  2065. downarrow->draw(ci, arrow_pos);
  2066. } else {
  2067. Ref<Texture2D> updown = theme_cache.updown;
  2068. int cell_width = item_rect.size.x - updown->get_width();
  2069. if (rtl) {
  2070. if (outline_size > 0 && font_outline_color.a > 0) {
  2071. p_item->cells[i].text_buf->draw_outline(ci, text_pos + Vector2(cell_width - text_width, 0), outline_size, font_outline_color);
  2072. }
  2073. p_item->cells[i].text_buf->draw(ci, text_pos + Vector2(cell_width - text_width, 0), cell_color);
  2074. } else {
  2075. if (outline_size > 0 && font_outline_color.a > 0) {
  2076. p_item->cells[i].text_buf->draw_outline(ci, text_pos, outline_size, font_outline_color);
  2077. }
  2078. p_item->cells[i].text_buf->draw(ci, text_pos, cell_color);
  2079. }
  2080. if (!p_item->cells[i].editable) {
  2081. break;
  2082. }
  2083. Point2i updown_pos = item_rect.position;
  2084. updown_pos.x += item_rect.size.x - updown->get_width();
  2085. updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
  2086. updown->draw(ci, updown_pos);
  2087. }
  2088. } break;
  2089. case TreeItem::CELL_MODE_ICON: {
  2090. if (p_item->cells[i].icon.is_null()) {
  2091. break;
  2092. }
  2093. Size2i icon_size = _get_cell_icon_size(p_item->cells[i]);
  2094. Point2i icon_ofs = (item_rect.size - icon_size) / 2;
  2095. icon_ofs += item_rect.position;
  2096. draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size), false, icon_col);
  2097. } break;
  2098. case TreeItem::CELL_MODE_CUSTOM: {
  2099. if (p_item->cells[i].custom_draw_callback.is_valid()) {
  2100. Variant args[] = { p_item, Rect2(item_rect) };
  2101. const Variant *argptrs[] = { &args[0], &args[1] };
  2102. Callable::CallError ce;
  2103. Variant ret;
  2104. p_item->cells[i].custom_draw_callback.callp(argptrs, 2, ret, ce);
  2105. if (ce.error != Callable::CallError::CALL_OK) {
  2106. ERR_PRINT("Error calling custom draw method: " + Variant::get_callable_error_text(p_item->cells[i].custom_draw_callback, argptrs, 2, ce) + ".");
  2107. }
  2108. }
  2109. if (!p_item->cells[i].editable) {
  2110. draw_item_rect(p_item->cells[i], item_rect, cell_color, icon_col, outline_size, font_outline_color);
  2111. break;
  2112. }
  2113. Ref<Texture2D> downarrow = theme_cache.select_arrow;
  2114. Rect2i ir = item_rect;
  2115. Point2i arrow_pos = item_rect.position;
  2116. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  2117. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  2118. ir.size.width -= downarrow->get_width();
  2119. if (p_item->cells[i].custom_button) {
  2120. if (cache.hover_item == p_item && cache.hover_column == i) {
  2121. if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  2122. draw_style_box(theme_cache.custom_button_pressed, ir);
  2123. } else {
  2124. draw_style_box(theme_cache.custom_button_hover, ir);
  2125. cell_color = theme_cache.custom_button_font_highlight;
  2126. }
  2127. } else {
  2128. draw_style_box(theme_cache.custom_button, ir);
  2129. }
  2130. ir.size -= theme_cache.custom_button->get_minimum_size();
  2131. ir.position += theme_cache.custom_button->get_offset();
  2132. }
  2133. draw_item_rect(p_item->cells[i], ir, cell_color, icon_col, outline_size, font_outline_color);
  2134. downarrow->draw(ci, arrow_pos);
  2135. } break;
  2136. }
  2137. // Draw the buttons inside the cell.
  2138. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  2139. Ref<Texture2D> button_texture = p_item->cells[i].buttons[j].texture;
  2140. Size2 button_size = button_texture->get_size() + theme_cache.button_pressed->get_minimum_size();
  2141. Point2i button_ofs = Point2i(ofs + item_width_with_buttons - button_size.width, p_pos.y) - theme_cache.offset + p_draw_ofs;
  2142. bool should_draw_pressed = 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;
  2143. bool should_draw_hovered = !drop_mode_flags && cache.hover_item == p_item && cache.hover_column == i && cache.hover_button_index_in_column == j && !p_item->cells[i].buttons[j].disabled;
  2144. if (should_draw_pressed || should_draw_hovered) {
  2145. Point2 od = button_ofs;
  2146. if (rtl) {
  2147. od.x = get_size().width - od.x - button_size.x;
  2148. }
  2149. if (should_draw_pressed && should_draw_hovered) {
  2150. theme_cache.button_pressed->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
  2151. } else {
  2152. theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
  2153. }
  2154. }
  2155. if (selected_item == p_item && selected_col == i && selected_button == j) {
  2156. Point2 od = button_ofs;
  2157. if (rtl) {
  2158. od.x = get_size().width - od.x - button_size.x;
  2159. }
  2160. theme_cache.button_hover->draw(get_canvas_item(), Rect2(od.x, od.y, button_size.width, MAX(button_size.height, label_h)));
  2161. }
  2162. button_ofs.y += (label_h - button_size.height) / 2;
  2163. button_ofs += theme_cache.button_pressed->get_offset();
  2164. if (rtl) {
  2165. button_ofs.x = get_size().width - button_ofs.x - button_texture->get_width();
  2166. }
  2167. 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);
  2168. item_width_with_buttons -= button_size.width + theme_cache.button_margin;
  2169. }
  2170. if (i == 0) {
  2171. ofs = get_column_width(0);
  2172. } else {
  2173. ofs += item_width + buttons_width;
  2174. }
  2175. if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
  2176. if (is_layout_rtl()) {
  2177. cell_rect.position.x = get_size().width - cell_rect.position.x - cell_rect.size.x;
  2178. }
  2179. if (has_focus(true)) {
  2180. theme_cache.cursor->draw(ci, cell_rect);
  2181. } else {
  2182. theme_cache.cursor_unfocus->draw(ci, cell_rect);
  2183. }
  2184. }
  2185. }
  2186. // Draw the folding arrow.
  2187. 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.
  2188. Ref<Texture2D> arrow;
  2189. if (p_item->collapsed) {
  2190. if (is_layout_rtl()) {
  2191. arrow = theme_cache.arrow_collapsed_mirrored;
  2192. } else {
  2193. arrow = theme_cache.arrow_collapsed;
  2194. }
  2195. } else {
  2196. arrow = theme_cache.arrow;
  2197. }
  2198. Size2 arrow_full_size = arrow->get_size();
  2199. Point2 apos = p_pos + Point2i(0, (label_h - arrow_full_size.height) / 2) - theme_cache.offset + p_draw_ofs;
  2200. apos.x += theme_cache.item_margin - arrow_full_size.width;
  2201. Size2 arrow_draw_size = arrow_full_size;
  2202. int out_width = p_pos.x + theme_cache.item_margin - get_column_width(0);
  2203. if (out_width > 0) {
  2204. arrow_draw_size.width -= out_width;
  2205. }
  2206. if (arrow_draw_size.width > 0) {
  2207. Point2 src_pos = Point2();
  2208. if (rtl) {
  2209. apos.x = get_size().width - apos.x - arrow_draw_size.width;
  2210. src_pos = Point2(arrow_full_size.width - arrow_draw_size.width, 0);
  2211. }
  2212. Rect2 arrow_rect = Rect2(apos, arrow_draw_size);
  2213. Rect2 arrow_src_rect = Rect2(src_pos, arrow_draw_size);
  2214. arrow->draw_rect_region(ci, arrow_rect, arrow_src_rect);
  2215. }
  2216. }
  2217. }
  2218. Point2 children_pos = p_pos;
  2219. if (!skip) {
  2220. children_pos.x += theme_cache.item_margin;
  2221. htotal += label_h;
  2222. children_pos.y += htotal;
  2223. }
  2224. if (!p_item->collapsed) { // If not collapsed, check the children.
  2225. TreeItem *c = p_item->first_child;
  2226. int base_ofs = children_pos.y - theme_cache.offset.y + p_draw_ofs.y;
  2227. float prev_ofs = base_ofs;
  2228. float prev_hl_ofs = base_ofs;
  2229. bool has_sibling_selection = c && _is_sibling_branch_selected(c);
  2230. while (c) {
  2231. int child_h = -1;
  2232. int child_self_height = 0;
  2233. if (htotal >= 0) {
  2234. child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c, child_self_height);
  2235. child_self_height += theme_cache.v_separation;
  2236. }
  2237. // Draw relationship lines.
  2238. if (theme_cache.draw_relationship_lines > 0 && (!hide_root || c->parent != root) && c->is_visible_in_tree()) {
  2239. int parent_ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin - theme_cache.arrow->get_width() * 0.5);
  2240. Point2i parent_pos = Point2i(parent_ofs, p_pos.y + label_h * 0.5 + theme_cache.arrow->get_height() * 0.5) - theme_cache.offset + p_draw_ofs;
  2241. int root_ofs = children_pos.x + ((c->disable_folding || hide_folding) ? theme_cache.h_separation : theme_cache.item_margin - (c->get_visible_child_count() > 0 ? theme_cache.arrow->get_width() : 0));
  2242. root_ofs = MIN(root_ofs, get_column_width(0) + p_draw_ofs.x);
  2243. Point2i root_pos = Point2i(root_ofs, children_pos.y + child_self_height * 0.5) - theme_cache.offset + p_draw_ofs;
  2244. bool is_no_space = root_pos.x <= parent_pos.x;
  2245. float line_width = theme_cache.relationship_line_width * Math::round(theme_cache.base_scale);
  2246. float parent_line_width = theme_cache.parent_hl_line_width * Math::round(theme_cache.base_scale);
  2247. float children_line_width = theme_cache.children_hl_line_width * Math::round(theme_cache.base_scale);
  2248. float line_pixel_shift = int(Math::round(line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
  2249. float parent_line_pixel_shift = int(Math::round(parent_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
  2250. float children_line_pixel_shift = int(Math::round(children_line_width * content_scale_factor)) % 2 == 0 ? 0.0 : 0.5;
  2251. int more_prev_ofs = 0;
  2252. if (root_pos.y + line_width >= 0) {
  2253. if (rtl) {
  2254. root_pos.x = get_size().width - root_pos.x;
  2255. parent_pos.x = get_size().width - parent_pos.x;
  2256. }
  2257. float parent_bottom_y = root_pos.y + parent_line_width * 0.5 + parent_line_pixel_shift;
  2258. // Order of parts on this bend: the horizontal line first, then the vertical line.
  2259. if (_is_branch_selected(c)) {
  2260. // If this item or one of its children is selected, we draw the line using parent highlight style.
  2261. if (!is_no_space) {
  2262. if (htotal >= 0) {
  2263. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + parent_line_pixel_shift), Point2(parent_pos.x + parent_line_width * 0.5 + parent_line_pixel_shift, root_pos.y + parent_line_pixel_shift), theme_cache.parent_hl_line_color, parent_line_width);
  2264. }
  2265. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
  2266. }
  2267. more_prev_ofs = theme_cache.parent_hl_line_margin;
  2268. prev_hl_ofs = parent_bottom_y;
  2269. has_sibling_selection = _is_sibling_branch_selected(c);
  2270. } else if (p_item->is_any_column_selected()) {
  2271. // If parent item is selected (but this item is not), we draw the line using children highlight style.
  2272. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  2273. if (has_sibling_selection) {
  2274. if (!is_no_space) {
  2275. if (htotal >= 0) {
  2276. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2i(parent_pos.x + parent_line_width * 0.5 + children_line_pixel_shift, root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
  2277. }
  2278. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
  2279. }
  2280. prev_hl_ofs = parent_bottom_y;
  2281. } else {
  2282. if (!is_no_space) {
  2283. if (htotal >= 0) {
  2284. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + children_line_pixel_shift), Point2(parent_pos.x + children_line_width * 0.5 + children_line_pixel_shift, root_pos.y + children_line_pixel_shift), theme_cache.children_hl_line_color, children_line_width);
  2285. }
  2286. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + children_line_pixel_shift, root_pos.y + children_line_width * 0.5 + children_line_pixel_shift), Point2(parent_pos.x + children_line_pixel_shift, prev_ofs + children_line_width * 0.5), theme_cache.children_hl_line_color, children_line_width);
  2287. }
  2288. }
  2289. } else {
  2290. // If nothing of the above is true, we draw the line using normal style.
  2291. // Siblings of the selected branch can be drawn with a slight offset and their vertical line must appear as highlighted.
  2292. if (has_sibling_selection) {
  2293. if (!is_no_space) {
  2294. if (htotal >= 0) {
  2295. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + theme_cache.parent_hl_line_margin, root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
  2296. }
  2297. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + parent_line_pixel_shift, parent_bottom_y), Point2(parent_pos.x + parent_line_pixel_shift, prev_hl_ofs), theme_cache.parent_hl_line_color, parent_line_width);
  2298. }
  2299. prev_hl_ofs = parent_bottom_y;
  2300. } else {
  2301. if (!is_no_space) {
  2302. if (htotal >= 0) {
  2303. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(root_pos.x, root_pos.y + line_pixel_shift), Point2(parent_pos.x + line_width * 0.5 + line_pixel_shift, root_pos.y + line_pixel_shift), theme_cache.relationship_line_color, line_width);
  2304. }
  2305. RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(parent_pos.x + line_pixel_shift, root_pos.y + line_width * 0.5 + line_pixel_shift), Point2(parent_pos.x + line_pixel_shift, prev_ofs + line_width * 0.5), theme_cache.relationship_line_color, line_width);
  2306. }
  2307. }
  2308. }
  2309. }
  2310. prev_ofs = root_pos.y + more_prev_ofs + line_pixel_shift;
  2311. }
  2312. if (child_h < 0) {
  2313. if (htotal == -1) {
  2314. break; // Last loop done, stop.
  2315. }
  2316. if (theme_cache.draw_relationship_lines == 0) {
  2317. return -1; // No need to draw anymore, full stop.
  2318. }
  2319. htotal = -1;
  2320. children_pos.y = theme_cache.offset.y + p_draw_size.height;
  2321. } else {
  2322. htotal += child_h;
  2323. children_pos.y += child_h;
  2324. }
  2325. c = c->next;
  2326. }
  2327. }
  2328. return htotal;
  2329. }
  2330. int Tree::_count_selected_items(TreeItem *p_from) const {
  2331. int count = 0;
  2332. for (int i = 0; i < columns.size(); i++) {
  2333. if (p_from->is_selected(i)) {
  2334. count++;
  2335. }
  2336. }
  2337. for (TreeItem *c = p_from->get_first_child(); c; c = c->get_next()) {
  2338. count += _count_selected_items(c);
  2339. }
  2340. return count;
  2341. }
  2342. bool Tree::_is_branch_selected(TreeItem *p_from) const {
  2343. if (p_from->is_any_column_selected()) {
  2344. return true;
  2345. }
  2346. TreeItem *child_item = p_from->get_first_child();
  2347. while (child_item) {
  2348. if (_is_branch_selected(child_item)) {
  2349. return true;
  2350. }
  2351. child_item = child_item->get_next();
  2352. }
  2353. return false;
  2354. }
  2355. bool Tree::_is_sibling_branch_selected(TreeItem *p_from) const {
  2356. TreeItem *sibling_item = p_from->get_next();
  2357. while (sibling_item) {
  2358. if (_is_branch_selected(sibling_item)) {
  2359. return true;
  2360. }
  2361. sibling_item = sibling_item->get_next();
  2362. }
  2363. return false;
  2364. }
  2365. 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) {
  2366. popup_editor->hide();
  2367. TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
  2368. bool switched = false;
  2369. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  2370. *r_in_range = true;
  2371. switched = true;
  2372. }
  2373. bool emitted_row = false;
  2374. for (int i = 0; i < columns.size(); i++) {
  2375. TreeItem::Cell &c = p_current->cells.write[i];
  2376. if (!c.selectable) {
  2377. continue;
  2378. }
  2379. if (select_mode == SELECT_ROW) {
  2380. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  2381. c.selected = true;
  2382. selected_item = p_selected;
  2383. if (!emitted_row) {
  2384. emit_signal(SceneStringName(item_selected));
  2385. emitted_row = true;
  2386. }
  2387. } else if (c.selected) {
  2388. if (p_selected != p_current) {
  2389. // Deselect other rows.
  2390. c.selected = false;
  2391. }
  2392. }
  2393. if (&selected_cell == &c) {
  2394. selected_col = i;
  2395. }
  2396. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  2397. if (!r_in_range && &selected_cell == &c) {
  2398. if (!selected_cell.selected || allow_reselect) {
  2399. selected_cell.selected = true;
  2400. selected_item = p_selected;
  2401. selected_col = i;
  2402. selected_button = -1;
  2403. emit_signal(SNAME("cell_selected"));
  2404. if (select_mode == SELECT_MULTI) {
  2405. emit_signal(SNAME("multi_selected"), p_current, i, true);
  2406. } else if (select_mode == SELECT_SINGLE) {
  2407. emit_signal(SceneStringName(item_selected));
  2408. }
  2409. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  2410. selected_item = p_selected;
  2411. selected_col = i;
  2412. selected_button = -1;
  2413. emit_signal(SNAME("cell_selected"));
  2414. }
  2415. } else {
  2416. if (r_in_range && *r_in_range && !p_force_deselect) {
  2417. if (!c.selected && c.selectable) {
  2418. c.selected = true;
  2419. emit_signal(SNAME("multi_selected"), p_current, i, true);
  2420. }
  2421. } else if (!r_in_range || p_force_deselect) {
  2422. if (select_mode == SELECT_MULTI && c.selected) {
  2423. emit_signal(SNAME("multi_selected"), p_current, i, false);
  2424. }
  2425. c.selected = false;
  2426. }
  2427. }
  2428. }
  2429. }
  2430. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  2431. *r_in_range = false;
  2432. }
  2433. TreeItem *c = p_current->first_child;
  2434. while (c) {
  2435. if (c->is_visible()) {
  2436. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  2437. }
  2438. c = c->next;
  2439. }
  2440. queue_accessibility_update();
  2441. }
  2442. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  2443. return Rect2();
  2444. }
  2445. void Tree::_range_click_timeout() {
  2446. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  2447. Point2 pos = get_local_mouse_position() - theme_cache.panel_style->get_offset();
  2448. if (show_column_titles) {
  2449. pos.y -= _get_title_button_height();
  2450. if (pos.y < 0) {
  2451. range_click_timer->stop();
  2452. return;
  2453. }
  2454. }
  2455. if (!root) {
  2456. return;
  2457. }
  2458. click_handled = false;
  2459. Ref<InputEventMouseButton> mb;
  2460. mb.instantiate();
  2461. int x_limit = _get_content_rect().size.x;
  2462. cache.rtl = is_layout_rtl();
  2463. 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).
  2464. blocked++;
  2465. propagate_mouse_event(pos + theme_cache.offset, 0, 0, x_limit + theme_cache.offset.width, false, root, MouseButton::LEFT, mb);
  2466. blocked--;
  2467. if (range_click_timer->is_one_shot()) {
  2468. range_click_timer->set_wait_time(0.05);
  2469. range_click_timer->set_one_shot(false);
  2470. range_click_timer->start();
  2471. }
  2472. if (!click_handled) {
  2473. range_click_timer->stop();
  2474. }
  2475. if (propagate_mouse_activated) {
  2476. emit_signal(SNAME("item_activated"));
  2477. propagate_mouse_activated = false;
  2478. }
  2479. } else {
  2480. range_click_timer->stop();
  2481. }
  2482. }
  2483. 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) {
  2484. if (p_item && !p_item->is_visible_in_tree()) {
  2485. // Skip any processing of invisible items.
  2486. return 0;
  2487. }
  2488. if (p_pos.x > x_limit) {
  2489. // Inside the scroll area.
  2490. return -1;
  2491. }
  2492. int item_h = compute_item_height(p_item) + theme_cache.v_separation;
  2493. bool skip = (p_item == root && hide_root);
  2494. if (!skip && p_pos.y < item_h) {
  2495. // Check event!
  2496. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  2497. return -1;
  2498. }
  2499. if (!p_item->disable_folding && !hide_folding && p_item->first_child && (p_pos.x < (x_ofs + theme_cache.item_margin))) {
  2500. if (enable_recursive_folding && p_mod->is_shift_pressed()) {
  2501. p_item->set_collapsed_recursive(!p_item->is_collapsed());
  2502. } else {
  2503. p_item->set_collapsed(!p_item->is_collapsed());
  2504. }
  2505. return -1;
  2506. }
  2507. FindColumnButtonResult find_result = _find_column_and_button_at_pos(p_pos.x, p_item, x_ofs, x_limit);
  2508. if (find_result.column_index == -1) {
  2509. return -1;
  2510. }
  2511. int col = find_result.column_index;
  2512. int col_width = find_result.column_width;
  2513. int col_ofs = find_result.column_offset;
  2514. int x = find_result.pos_x;
  2515. const TreeItem::Cell &c = p_item->cells[col];
  2516. if (find_result.button_index >= 0) {
  2517. if (c.buttons[find_result.button_index].disabled) {
  2518. pressed_button = -1;
  2519. cache.click_type = Cache::CLICK_NONE;
  2520. return -1;
  2521. }
  2522. // Make sure the click is correct.
  2523. const Point2 click_pos = get_local_mouse_position();
  2524. if (!get_item_at_position(click_pos)) {
  2525. pressed_button = -1;
  2526. cache.click_type = Cache::CLICK_NONE;
  2527. return -1;
  2528. }
  2529. pressed_button = find_result.button_index;
  2530. cache.click_type = Cache::CLICK_BUTTON;
  2531. cache.click_index = find_result.button_index;
  2532. cache.click_id = c.buttons[find_result.button_index].id;
  2533. cache.click_item = p_item;
  2534. cache.click_column = col;
  2535. cache.click_pos = click_pos;
  2536. queue_redraw();
  2537. return -1;
  2538. }
  2539. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_first_child()) {
  2540. if (enable_recursive_folding && p_mod->is_shift_pressed()) {
  2541. p_item->set_collapsed_recursive(!p_item->is_collapsed());
  2542. } else {
  2543. p_item->set_collapsed(!p_item->is_collapsed());
  2544. }
  2545. return -1; // Collapse/uncollapse, because nothing can be done with the item.
  2546. }
  2547. bool already_selected = c.selected;
  2548. bool already_cursor = (p_item == selected_item) && col == selected_col;
  2549. if (p_button == MouseButton::LEFT || (p_button == MouseButton::RIGHT && allow_rmb_select)) {
  2550. // Process selection.
  2551. if (p_double_click && (!c.editable || c.mode == TreeItem::CELL_MODE_CUSTOM || c.mode == TreeItem::CELL_MODE_ICON)) {
  2552. // Emits the `item_activated` signal.
  2553. propagate_mouse_activated = true;
  2554. incr_search.clear();
  2555. return -1;
  2556. }
  2557. if (c.selectable) {
  2558. if (select_mode == SELECT_MULTI && p_mod->is_command_or_control_pressed()) {
  2559. if (c.selected && p_button == MouseButton::LEFT) {
  2560. p_item->deselect(col);
  2561. emit_signal(SNAME("multi_selected"), p_item, col, false);
  2562. } else {
  2563. p_item->select(col);
  2564. emit_signal(SNAME("multi_selected"), p_item, col, true);
  2565. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2566. }
  2567. } else {
  2568. if (select_mode == SELECT_MULTI && p_mod->is_shift_pressed() && selected_item && selected_item != p_item) {
  2569. bool inrange = false;
  2570. select_single_item(p_item, root, col, selected_item, &inrange);
  2571. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2572. } else {
  2573. int icount = _count_selected_items(root);
  2574. if (select_mode == SELECT_MULTI && icount > 1 && p_button != MouseButton::RIGHT) {
  2575. if (!already_selected) {
  2576. select_single_item(p_item, root, col);
  2577. }
  2578. single_select_defer = p_item;
  2579. single_select_defer_column = col;
  2580. } else {
  2581. if (p_button != MouseButton::RIGHT || !c.selected) {
  2582. select_single_item(p_item, root, col);
  2583. }
  2584. emit_signal(SNAME("item_mouse_selected"), get_local_mouse_position(), p_button);
  2585. }
  2586. }
  2587. queue_accessibility_update();
  2588. queue_redraw();
  2589. }
  2590. }
  2591. }
  2592. if (!c.editable) {
  2593. return -1; // If cell is not editable, don't bother.
  2594. }
  2595. // Editing.
  2596. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  2597. String editor_text = c.text;
  2598. switch (c.mode) {
  2599. case TreeItem::CELL_MODE_STRING: {
  2600. // Nothing in particular.
  2601. if (select_mode == SELECT_MULTI && (get_viewport()->get_processed_events_count() == focus_in_id || !already_cursor)) {
  2602. bring_up_editor = false;
  2603. }
  2604. } break;
  2605. case TreeItem::CELL_MODE_CHECK: {
  2606. bring_up_editor = false; // Checkboxes are not edited with editor.
  2607. if (force_edit_checkbox_only_on_checkbox) {
  2608. if (x < theme_cache.checked->get_width()) {
  2609. p_item->set_checked(col, !c.checked);
  2610. item_edited(col, p_item, p_button);
  2611. }
  2612. } else {
  2613. p_item->set_checked(col, !c.checked);
  2614. item_edited(col, p_item, p_button);
  2615. }
  2616. click_handled = true;
  2617. } break;
  2618. case TreeItem::CELL_MODE_RANGE: {
  2619. if (!c.text.is_empty()) {
  2620. _update_popup_menu(c);
  2621. popup_menu->set_size(Size2(col_width, 0));
  2622. popup_menu->set_position(get_screen_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - theme_cache.offset);
  2623. popup_menu->popup();
  2624. popup_edited_item = p_item;
  2625. popup_edited_item_col = col;
  2626. bring_up_editor = false;
  2627. } else {
  2628. if (x >= (col_width - item_h / 2)) {
  2629. // Touching the combo.
  2630. bool up = p_pos.y < (item_h / 2);
  2631. if (p_button == MouseButton::LEFT) {
  2632. if (range_click_timer->get_time_left() == 0) {
  2633. range_item_last = p_item;
  2634. range_up_last = up;
  2635. range_click_timer->set_wait_time(0.6);
  2636. range_click_timer->set_one_shot(true);
  2637. range_click_timer->start();
  2638. } else if (up != range_up_last) {
  2639. return -1; // Break. Avoid changing direction on mouse held.
  2640. }
  2641. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  2642. item_edited(col, p_item, p_button);
  2643. } else if (p_button == MouseButton::RIGHT) {
  2644. p_item->set_range(col, (up ? c.max : c.min));
  2645. item_edited(col, p_item, p_button);
  2646. } else if (p_button == MouseButton::WHEEL_UP) {
  2647. p_item->set_range(col, c.val + c.step);
  2648. item_edited(col, p_item, p_button);
  2649. } else if (p_button == MouseButton::WHEEL_DOWN) {
  2650. p_item->set_range(col, c.val - c.step);
  2651. item_edited(col, p_item, p_button);
  2652. }
  2653. bring_up_editor = false;
  2654. } else {
  2655. editor_text = String::num(p_item->cells[col].val, Math::range_step_decimals(p_item->cells[col].step));
  2656. if (select_mode == SELECT_MULTI && get_viewport()->get_processed_events_count() == focus_in_id) {
  2657. bring_up_editor = false;
  2658. }
  2659. }
  2660. }
  2661. click_handled = true;
  2662. } break;
  2663. case TreeItem::CELL_MODE_ICON: {
  2664. bring_up_editor = false;
  2665. } break;
  2666. case TreeItem::CELL_MODE_CUSTOM: {
  2667. edited_item = p_item;
  2668. edited_col = col;
  2669. bool on_arrow = x > col_width - theme_cache.select_arrow->get_width();
  2670. 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));
  2671. if (on_arrow || !p_item->cells[col].custom_button) {
  2672. emit_signal(SNAME("custom_popup_edited"), ((bool)(x >= (col_width - item_h / 2))));
  2673. }
  2674. if (!p_item->cells[col].custom_button || !on_arrow) {
  2675. item_edited(col, p_item, p_button);
  2676. }
  2677. click_handled = true;
  2678. return -1;
  2679. } break;
  2680. };
  2681. if (!bring_up_editor || p_button != MouseButton::LEFT) {
  2682. return -1;
  2683. }
  2684. click_handled = true;
  2685. popup_pressing_edited_item = p_item;
  2686. popup_pressing_edited_item_column = col;
  2687. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - theme_cache.offset, Size2(col_width, item_h));
  2688. pressing_for_editor_text = editor_text;
  2689. pressing_for_editor = true;
  2690. return -1; // Select.
  2691. } else {
  2692. Point2i new_pos = p_pos;
  2693. if (!skip) {
  2694. x_ofs += theme_cache.item_margin;
  2695. y_ofs += item_h;
  2696. new_pos.y -= item_h;
  2697. }
  2698. if (!p_item->collapsed) { // If not collapsed, check the children.
  2699. TreeItem *c = p_item->first_child;
  2700. while (c) {
  2701. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, x_limit, p_double_click, c, p_button, p_mod);
  2702. if (child_h < 0) {
  2703. return -1; // Break, stop propagating, no need to anymore.
  2704. }
  2705. new_pos.y -= child_h;
  2706. y_ofs += child_h;
  2707. c = c->next;
  2708. item_h += child_h;
  2709. }
  2710. }
  2711. if (p_item == root) {
  2712. emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), p_button);
  2713. }
  2714. }
  2715. return item_h; // Nothing found.
  2716. }
  2717. void Tree::_text_editor_popup_modal_close() {
  2718. if (popup_edit_committed) {
  2719. return; // Already processed by `LineEdit` / `TextEdit` commit.
  2720. }
  2721. if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
  2722. return; // ESC pressed, app focus lost, or forced close from code.
  2723. }
  2724. if (value_editor && value_editor->has_point(value_editor->get_local_mouse_position())) {
  2725. return;
  2726. }
  2727. if (!popup_edited_item) {
  2728. return;
  2729. }
  2730. if (popup_edited_item->is_edit_multiline(popup_edited_item_col) && popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_STRING) {
  2731. _apply_multiline_edit();
  2732. } else {
  2733. _line_editor_submit(line_editor->get_text());
  2734. }
  2735. }
  2736. void Tree::_text_editor_gui_input(const Ref<InputEvent> &p_event) {
  2737. if (popup_edit_committed) {
  2738. return; // Already processed by `_text_editor_popup_modal_close`.
  2739. }
  2740. if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
  2741. return; // ESC pressed, app focus lost, or forced close from code.
  2742. }
  2743. if (p_event->is_action_pressed("ui_text_newline_blank", true)) {
  2744. accept_event();
  2745. } else if (p_event->is_action_pressed("ui_text_newline")) {
  2746. popup_edit_committed = true; // End edit popup processing.
  2747. popup_editor->hide();
  2748. _apply_multiline_edit();
  2749. accept_event();
  2750. }
  2751. }
  2752. void Tree::_apply_multiline_edit() {
  2753. if (!popup_edited_item) {
  2754. return;
  2755. }
  2756. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2757. return;
  2758. }
  2759. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2760. switch (c.mode) {
  2761. case TreeItem::CELL_MODE_STRING: {
  2762. c.text = text_editor->get_text();
  2763. } break;
  2764. default: {
  2765. ERR_FAIL();
  2766. }
  2767. }
  2768. item_edited(popup_edited_item_col, popup_edited_item);
  2769. queue_redraw();
  2770. }
  2771. void Tree::_line_editor_submit(String p_text) {
  2772. if (popup_edit_committed) {
  2773. return; // Already processed by _text_editor_popup_modal_close.
  2774. }
  2775. if (popup_editor->get_hide_reason() == Popup::HIDE_REASON_CANCELED) {
  2776. return; // ESC pressed, app focus lost, or forced close from code.
  2777. }
  2778. popup_edit_committed = true; // End edit popup processing.
  2779. popup_editor->hide();
  2780. if (!popup_edited_item) {
  2781. return;
  2782. }
  2783. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2784. return;
  2785. }
  2786. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  2787. switch (c.mode) {
  2788. case TreeItem::CELL_MODE_STRING: {
  2789. c.text = p_text;
  2790. } break;
  2791. case TreeItem::CELL_MODE_RANGE: {
  2792. c.val = p_text.to_float();
  2793. if (c.step > 0) {
  2794. c.val = Math::snapped(c.val, c.step);
  2795. }
  2796. if (c.val < c.min) {
  2797. c.val = c.min;
  2798. } else if (c.val > c.max) {
  2799. c.val = c.max;
  2800. }
  2801. } break;
  2802. default: {
  2803. ERR_FAIL();
  2804. }
  2805. }
  2806. item_edited(popup_edited_item_col, popup_edited_item);
  2807. queue_redraw();
  2808. }
  2809. void Tree::value_editor_changed(double p_value) {
  2810. if (updating_value_editor) {
  2811. return;
  2812. }
  2813. if (!popup_edited_item) {
  2814. return;
  2815. }
  2816. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  2817. line_editor->set_text(String::num(p_value, Math::range_step_decimals(c.step)));
  2818. queue_redraw();
  2819. }
  2820. void Tree::_update_popup_menu(const TreeItem::Cell &p_cell) {
  2821. if (popup_menu == nullptr) {
  2822. popup_menu = memnew(PopupMenu);
  2823. popup_menu->hide();
  2824. add_child(popup_menu, false, INTERNAL_MODE_FRONT);
  2825. popup_menu->connect(SceneStringName(id_pressed), callable_mp(this, &Tree::popup_select));
  2826. }
  2827. popup_menu->clear();
  2828. for (int i = 0; i < p_cell.text.get_slice_count(","); i++) {
  2829. String s = p_cell.text.get_slicec(',', i);
  2830. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).is_empty() ? i : s.get_slicec(':', 1).to_int());
  2831. }
  2832. }
  2833. void Tree::_update_value_editor(const TreeItem::Cell &p_cell) {
  2834. if (value_editor == nullptr) {
  2835. value_editor = memnew(HSlider);
  2836. value_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  2837. value_editor->hide();
  2838. popup_editor_vb->add_child(value_editor);
  2839. value_editor->connect(SceneStringName(value_changed), callable_mp(this, &Tree::value_editor_changed));
  2840. }
  2841. updating_value_editor = true;
  2842. value_editor->set_min(p_cell.min);
  2843. value_editor->set_max(p_cell.max);
  2844. value_editor->set_step(p_cell.step);
  2845. value_editor->set_value(p_cell.val);
  2846. value_editor->set_exp_ratio(p_cell.expr);
  2847. updating_value_editor = false;
  2848. }
  2849. void Tree::popup_select(int p_option) {
  2850. if (!popup_edited_item) {
  2851. return;
  2852. }
  2853. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size()) {
  2854. return;
  2855. }
  2856. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  2857. queue_redraw();
  2858. item_edited(popup_edited_item_col, popup_edited_item);
  2859. }
  2860. void Tree::_go_left() {
  2861. if (get_tree()->is_accessibility_enabled() && selected_button >= 0) {
  2862. selected_button--;
  2863. } else if (selected_col == 0) {
  2864. selected_button = -1;
  2865. if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
  2866. selected_item->set_collapsed(true);
  2867. } else {
  2868. if (columns.size() == 1) { // Goto parent with one column.
  2869. TreeItem *parent = selected_item->get_parent();
  2870. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  2871. select_single_item(parent, get_root(), selected_col);
  2872. }
  2873. } else if (selected_item->get_prev_visible()) {
  2874. selected_col = columns.size() - 1;
  2875. _go_up(); // Go to upper column if possible.
  2876. }
  2877. }
  2878. } else {
  2879. selected_button = -1;
  2880. if (select_mode == SELECT_MULTI) {
  2881. selected_col--;
  2882. emit_signal(SNAME("cell_selected"));
  2883. } else {
  2884. selected_item->select(selected_col - 1);
  2885. }
  2886. }
  2887. queue_accessibility_update();
  2888. queue_redraw();
  2889. accept_event();
  2890. ensure_cursor_is_visible();
  2891. }
  2892. void Tree::_go_right() {
  2893. int buttons = (selected_item && selected_col >= 0 && selected_col < columns.size()) ? selected_item->cells[selected_col].buttons.size() : 0;
  2894. if (get_tree()->is_accessibility_enabled() && selected_button < buttons - 1) {
  2895. selected_button++;
  2896. } else if (selected_col == (columns.size() - 1)) {
  2897. selected_button = -1;
  2898. if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
  2899. selected_item->set_collapsed(false);
  2900. } else if (selected_item->get_next_visible()) {
  2901. selected_col = 0;
  2902. _go_down();
  2903. }
  2904. } else {
  2905. selected_button = -1;
  2906. if (select_mode == SELECT_MULTI) {
  2907. selected_col++;
  2908. emit_signal(SNAME("cell_selected"));
  2909. } else {
  2910. selected_item->select(selected_col + 1);
  2911. }
  2912. }
  2913. queue_accessibility_update();
  2914. queue_redraw();
  2915. ensure_cursor_is_visible();
  2916. accept_event();
  2917. }
  2918. void Tree::_go_up() {
  2919. TreeItem *prev = nullptr;
  2920. if (!selected_item) {
  2921. prev = get_last_item();
  2922. selected_col = 0;
  2923. selected_button = -1;
  2924. } else {
  2925. prev = selected_item->get_prev_visible();
  2926. }
  2927. int col = MAX(selected_col, 0);
  2928. if (select_mode == SELECT_MULTI) {
  2929. if (!prev) {
  2930. return;
  2931. }
  2932. selected_item = prev;
  2933. emit_signal(SNAME("cell_selected"));
  2934. queue_redraw();
  2935. } else {
  2936. while (prev && !prev->cells[col].selectable) {
  2937. prev = prev->get_prev_visible();
  2938. }
  2939. if (!prev) {
  2940. return; // Do nothing.
  2941. }
  2942. prev->select(col);
  2943. }
  2944. queue_accessibility_update();
  2945. ensure_cursor_is_visible();
  2946. accept_event();
  2947. }
  2948. void Tree::_shift_select_range(TreeItem *new_item) {
  2949. if (!new_item) {
  2950. new_item = selected_item;
  2951. }
  2952. int s_col = selected_col;
  2953. bool in_range = false;
  2954. TreeItem *item = root;
  2955. if (!shift_anchor) {
  2956. shift_anchor = selected_item;
  2957. }
  2958. while (item) {
  2959. bool at_range_edge = item == shift_anchor || item == new_item;
  2960. if (at_range_edge) {
  2961. in_range = !in_range;
  2962. }
  2963. if (new_item == shift_anchor) {
  2964. in_range = false;
  2965. }
  2966. if (item->is_visible_in_tree()) {
  2967. if (in_range || at_range_edge) {
  2968. if (!item->is_selected(selected_col) && item->is_selectable(selected_col)) {
  2969. item->select(selected_col);
  2970. emit_signal(SNAME("multi_selected"), item, selected_col, true);
  2971. }
  2972. } else if (item->is_selected(selected_col)) {
  2973. item->deselect(selected_col);
  2974. emit_signal(SNAME("multi_selected"), item, selected_col, false);
  2975. }
  2976. }
  2977. item = item->get_next_in_tree(false);
  2978. }
  2979. selected_item = new_item;
  2980. selected_col = s_col;
  2981. ensure_cursor_is_visible();
  2982. queue_redraw();
  2983. accept_event();
  2984. }
  2985. void Tree::_go_down() {
  2986. TreeItem *next = nullptr;
  2987. if (!selected_item) {
  2988. if (root) {
  2989. next = hide_root ? root->get_next_visible() : root;
  2990. }
  2991. } else {
  2992. next = selected_item->get_next_visible();
  2993. }
  2994. int col = MAX(selected_col, 0);
  2995. if (select_mode == SELECT_MULTI) {
  2996. if (!next) {
  2997. return;
  2998. }
  2999. selected_item = next;
  3000. emit_signal(SNAME("cell_selected"));
  3001. queue_redraw();
  3002. } else {
  3003. while (next && !next->cells[col].selectable) {
  3004. next = next->get_next_visible();
  3005. }
  3006. if (!next) {
  3007. return; // Do nothing.
  3008. }
  3009. next->select(col);
  3010. }
  3011. queue_accessibility_update();
  3012. ensure_cursor_is_visible();
  3013. accept_event();
  3014. }
  3015. bool Tree::_scroll(bool p_horizontal, float p_pages) {
  3016. ScrollBar *scroll = p_horizontal ? (ScrollBar *)h_scroll : (ScrollBar *)v_scroll;
  3017. double prev_value = scroll->get_value();
  3018. scroll->set_value(scroll->get_value() + scroll->get_page() * p_pages);
  3019. bool scroll_happened = scroll->get_value() != prev_value;
  3020. if (scroll_happened) {
  3021. _determine_hovered_item();
  3022. }
  3023. return scroll_happened;
  3024. }
  3025. Rect2 Tree::_get_scrollbar_layout_rect() const {
  3026. const Size2 control_size = get_size();
  3027. const Ref<StyleBox> background = theme_cache.panel_style;
  3028. // This is the background stylebox's content rect.
  3029. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  3030. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  3031. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  3032. // Use the stylebox's margins by default. Can be overridden by `scrollbar_margin_*`.
  3033. const real_t top = theme_cache.scrollbar_margin_top < 0 ? content_rect.get_position().y : theme_cache.scrollbar_margin_top;
  3034. const real_t right = theme_cache.scrollbar_margin_right < 0 ? content_rect.get_end().x : (control_size.x - theme_cache.scrollbar_margin_right);
  3035. const real_t bottom = theme_cache.scrollbar_margin_bottom < 0 ? content_rect.get_end().y : (control_size.y - theme_cache.scrollbar_margin_bottom);
  3036. const real_t left = theme_cache.scrollbar_margin_left < 0 ? content_rect.get_position().x : theme_cache.scrollbar_margin_left;
  3037. return Rect2(left, top, right - left, bottom - top);
  3038. }
  3039. Rect2 Tree::_get_content_rect() const {
  3040. const Size2 control_size = get_size();
  3041. const Ref<StyleBox> background = theme_cache.panel_style;
  3042. // This is the background stylebox's content rect.
  3043. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  3044. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  3045. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  3046. // Scrollbars won't affect Tree's content rect if they're not visible or placed inside the stylebox margin area.
  3047. const real_t v_size = v_scroll->is_visible() ? (v_scroll->get_combined_minimum_size().x + theme_cache.scrollbar_h_separation) : 0;
  3048. const real_t h_size = h_scroll->is_visible() ? (h_scroll->get_combined_minimum_size().y + theme_cache.scrollbar_v_separation) : 0;
  3049. const Point2 scroll_begin = _get_scrollbar_layout_rect().get_end() - Vector2(v_size, h_size);
  3050. const Size2 offset = (content_rect.get_end() - scroll_begin).maxf(0);
  3051. return content_rect.grow_individual(0, 0, -offset.x, -offset.y);
  3052. }
  3053. void Tree::gui_input(const Ref<InputEvent> &p_event) {
  3054. ERR_FAIL_COND(p_event.is_null());
  3055. Ref<InputEventKey> k = p_event;
  3056. if (k.is_valid() && k->get_keycode() == Key::SHIFT && !k->is_pressed()) {
  3057. shift_anchor = nullptr;
  3058. }
  3059. bool is_command = k.is_valid() && k->is_command_or_control_pressed();
  3060. if (p_event->is_action(cache.rtl ? "ui_left" : "ui_right") && p_event->is_pressed()) {
  3061. if (!cursor_can_exit_tree) {
  3062. accept_event();
  3063. }
  3064. if (!selected_item || selected_col > (columns.size() - 1)) {
  3065. return;
  3066. }
  3067. if (k.is_valid() && k->is_shift_pressed()) {
  3068. selected_item->set_collapsed_recursive(false);
  3069. } else if (select_mode != SELECT_ROW) {
  3070. _go_right();
  3071. } else if (selected_item->get_first_child() != nullptr && selected_item->is_collapsed()) {
  3072. selected_item->set_collapsed(false);
  3073. } else {
  3074. _go_down();
  3075. }
  3076. } else if (p_event->is_action(cache.rtl ? "ui_right" : "ui_left") && p_event->is_pressed()) {
  3077. if (!cursor_can_exit_tree) {
  3078. accept_event();
  3079. }
  3080. if (!selected_item || selected_col < 0) {
  3081. return;
  3082. }
  3083. if (k.is_valid() && k->is_shift_pressed()) {
  3084. selected_item->set_collapsed_recursive(true);
  3085. } else if (select_mode != SELECT_ROW) {
  3086. _go_left();
  3087. } else if (selected_item->get_first_child() != nullptr && !selected_item->is_collapsed()) {
  3088. selected_item->set_collapsed(true);
  3089. } else {
  3090. _go_up();
  3091. }
  3092. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !is_command) {
  3093. if (!cursor_can_exit_tree) {
  3094. accept_event();
  3095. }
  3096. // Shift Up Selection.
  3097. if (k.is_valid() && k->is_shift_pressed() && selected_item && select_mode == SELECT_MULTI) {
  3098. TreeItem *new_item = selected_item->get_prev_visible(false);
  3099. _shift_select_range(new_item);
  3100. } else {
  3101. _go_up();
  3102. }
  3103. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !is_command) {
  3104. if (!cursor_can_exit_tree) {
  3105. accept_event();
  3106. }
  3107. // Shift Down Selection.
  3108. if (k.is_valid() && k->is_shift_pressed() && selected_item && select_mode == SELECT_MULTI) {
  3109. TreeItem *new_item = selected_item->get_next_visible(false);
  3110. _shift_select_range(new_item);
  3111. } else {
  3112. _go_down();
  3113. }
  3114. } else if (p_event->is_action("ui_menu") && p_event->is_pressed()) {
  3115. if (allow_rmb_select && selected_item) {
  3116. emit_signal(SNAME("item_mouse_selected"), get_item_rect(selected_item).position, MouseButton::RIGHT);
  3117. }
  3118. accept_event();
  3119. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  3120. if (!cursor_can_exit_tree) {
  3121. accept_event();
  3122. }
  3123. TreeItem *next = nullptr;
  3124. if (!selected_item) {
  3125. return;
  3126. }
  3127. next = selected_item;
  3128. for (int i = 0; i < 10; i++) {
  3129. TreeItem *_n = next->get_next_visible();
  3130. if (_n) {
  3131. next = _n;
  3132. } else {
  3133. break;
  3134. }
  3135. }
  3136. if (next == selected_item) {
  3137. return;
  3138. }
  3139. if (select_mode == SELECT_MULTI) {
  3140. selected_item = next;
  3141. emit_signal(SNAME("cell_selected"));
  3142. queue_accessibility_update();
  3143. queue_redraw();
  3144. } else {
  3145. while (next && !next->cells[selected_col].selectable) {
  3146. next = next->get_next_visible();
  3147. }
  3148. if (!next) {
  3149. return; // Do nothing.
  3150. }
  3151. next->select(selected_col);
  3152. }
  3153. ensure_cursor_is_visible();
  3154. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  3155. if (!cursor_can_exit_tree) {
  3156. accept_event();
  3157. }
  3158. TreeItem *prev = nullptr;
  3159. if (!selected_item) {
  3160. return;
  3161. }
  3162. prev = selected_item;
  3163. for (int i = 0; i < 10; i++) {
  3164. TreeItem *_n = prev->get_prev_visible();
  3165. if (_n) {
  3166. prev = _n;
  3167. } else {
  3168. break;
  3169. }
  3170. }
  3171. if (prev == selected_item) {
  3172. return;
  3173. }
  3174. if (select_mode == SELECT_MULTI) {
  3175. selected_item = prev;
  3176. emit_signal(SNAME("cell_selected"));
  3177. queue_accessibility_update();
  3178. queue_redraw();
  3179. } else {
  3180. while (prev && !prev->cells[selected_col].selectable) {
  3181. prev = prev->get_prev_visible();
  3182. }
  3183. if (!prev) {
  3184. return; // Do nothing.
  3185. }
  3186. prev->select(selected_col);
  3187. }
  3188. ensure_cursor_is_visible();
  3189. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  3190. if (select_mode == SELECT_MULTI) {
  3191. if (!selected_item) {
  3192. return;
  3193. }
  3194. if (selected_item && selected_col != -1 && selected_button != -1) {
  3195. const TreeItem::Cell &c = selected_item->cells[selected_col];
  3196. emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
  3197. } else if (selected_item->is_selected(selected_col)) {
  3198. selected_item->deselect(selected_col);
  3199. emit_signal(SNAME("multi_selected"), selected_item, selected_col, false);
  3200. } else if (selected_item->is_selectable(selected_col)) {
  3201. selected_item->select(selected_col);
  3202. emit_signal(SNAME("multi_selected"), selected_item, selected_col, true);
  3203. }
  3204. } else if (selected_item && selected_col != -1 && selected_button != -1) {
  3205. const TreeItem::Cell &c = selected_item->cells[selected_col];
  3206. emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
  3207. }
  3208. accept_event();
  3209. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  3210. if (selected_item) {
  3211. // Bring up editor if possible.
  3212. if (selected_item && selected_col != -1 && selected_button != -1) {
  3213. const TreeItem::Cell &c = selected_item->cells[selected_col];
  3214. emit_signal("button_clicked", selected_item, selected_col, c.buttons[selected_button].id, MouseButton::LEFT);
  3215. } else if (!edit_selected()) {
  3216. emit_signal(SNAME("item_activated"));
  3217. incr_search.clear();
  3218. }
  3219. }
  3220. accept_event();
  3221. }
  3222. if (allow_search && k.is_valid()) { // Incremental search.
  3223. if (!k->is_pressed()) {
  3224. return;
  3225. }
  3226. if (k->is_command_or_control_pressed() || (k->is_shift_pressed() && k->get_unicode() == 0) || k->is_meta_pressed()) {
  3227. return;
  3228. }
  3229. if (!root) {
  3230. return;
  3231. }
  3232. if (hide_root && !root->get_next_visible()) {
  3233. return;
  3234. }
  3235. if (k->get_unicode() > 0) {
  3236. _do_incr_search(String::chr(k->get_unicode()));
  3237. accept_event();
  3238. return;
  3239. } else {
  3240. if (k->get_keycode() != Key::SHIFT) {
  3241. last_keypress = 0;
  3242. }
  3243. }
  3244. }
  3245. Ref<InputEventMouseMotion> mm = p_event;
  3246. if (mm.is_valid()) {
  3247. hovered_pos = mm->get_position();
  3248. _determine_hovered_item();
  3249. bool rtl = is_layout_rtl();
  3250. if (pressing_for_editor && popup_pressing_edited_item && !popup_pressing_edited_item->cells.is_empty() && (popup_pressing_edited_item->get_cell_mode(popup_pressing_edited_item_column) == TreeItem::CELL_MODE_RANGE)) {
  3251. // 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.
  3252. popup_edited_item = popup_pressing_edited_item;
  3253. popup_edited_item_col = popup_pressing_edited_item_column;
  3254. popup_pressing_edited_item = nullptr;
  3255. popup_pressing_edited_item_column = -1;
  3256. if (!range_drag_enabled) {
  3257. // Range drag.
  3258. Vector2 cpos = mm->get_position();
  3259. if (rtl) {
  3260. cpos.x = get_size().width - cpos.x;
  3261. }
  3262. if (cpos.distance_to(pressing_pos) > 2) {
  3263. range_drag_enabled = true;
  3264. range_drag_capture_pos = cpos;
  3265. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  3266. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  3267. }
  3268. } else {
  3269. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  3270. float diff_y = -mm->get_relative().y;
  3271. diff_y = Math::pow(Math::abs(diff_y), 1.8f) * SIGN(diff_y);
  3272. diff_y *= 0.1;
  3273. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  3274. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  3275. item_edited(popup_edited_item_col, popup_edited_item);
  3276. }
  3277. }
  3278. if (drag_touching && !drag_touching_deaccel) {
  3279. drag_accum -= mm->get_relative().y;
  3280. v_scroll->set_value(drag_from + drag_accum);
  3281. drag_speed = -mm->get_velocity().y;
  3282. }
  3283. }
  3284. Ref<InputEventMouseButton> mb = p_event;
  3285. if (mb.is_valid()) {
  3286. bool rtl = is_layout_rtl();
  3287. if (!mb->is_pressed()) {
  3288. if (mb->get_button_index() == MouseButton::LEFT ||
  3289. mb->get_button_index() == MouseButton::RIGHT) {
  3290. Point2 pos = mb->get_position();
  3291. if (rtl) {
  3292. pos.x = get_size().width - pos.x - 1;
  3293. }
  3294. pos -= theme_cache.panel_style->get_offset();
  3295. if (show_column_titles) {
  3296. pos.y -= _get_title_button_height();
  3297. if (pos.y < 0) {
  3298. pos.x += theme_cache.offset.x;
  3299. int len = 0;
  3300. for (int i = 0; i < columns.size(); i++) {
  3301. len += get_column_width(i);
  3302. if (pos.x < static_cast<real_t>(len)) {
  3303. emit_signal(SNAME("column_title_clicked"), i, mb->get_button_index());
  3304. break;
  3305. }
  3306. }
  3307. }
  3308. }
  3309. }
  3310. if (mb->get_button_index() == MouseButton::LEFT) {
  3311. if (single_select_defer) {
  3312. select_single_item(single_select_defer, root, single_select_defer_column);
  3313. single_select_defer = nullptr;
  3314. }
  3315. range_click_timer->stop();
  3316. if (pressing_for_editor) {
  3317. if (range_drag_enabled) {
  3318. range_drag_enabled = false;
  3319. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  3320. warp_mouse(range_drag_capture_pos);
  3321. } else {
  3322. Rect2 rect = _get_item_focus_rect(get_selected());
  3323. Point2 mpos = mb->get_position();
  3324. int icon_size_x = 0;
  3325. Ref<Texture2D> icon = get_selected()->get_icon(selected_col);
  3326. if (icon.is_valid()) {
  3327. Rect2i icon_region = get_selected()->get_icon_region(selected_col);
  3328. if (icon_region == Rect2i()) {
  3329. icon_size_x = icon->get_width();
  3330. } else {
  3331. icon_size_x = icon_region.size.width;
  3332. }
  3333. }
  3334. // 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.
  3335. if (rtl) {
  3336. mpos.x = get_size().width - (mpos.x + icon_size_x);
  3337. } else {
  3338. mpos.x -= icon_size_x;
  3339. }
  3340. if (rect.has_point(mpos)) {
  3341. if (!edit_selected()) {
  3342. emit_signal(SNAME("item_icon_double_clicked"));
  3343. }
  3344. } else {
  3345. emit_signal(SNAME("item_icon_double_clicked"));
  3346. }
  3347. }
  3348. pressing_for_editor = false;
  3349. }
  3350. if (drag_touching) {
  3351. if (drag_speed == 0) {
  3352. drag_touching_deaccel = false;
  3353. drag_touching = false;
  3354. set_process_internal(false);
  3355. } else {
  3356. drag_touching_deaccel = true;
  3357. }
  3358. }
  3359. }
  3360. if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item != nullptr) {
  3361. // Make sure the reference is current in case TreeItems is reconstructed.
  3362. cache.click_item = get_item_at_position(cache.click_pos);
  3363. // Only emit the event if the click is still within the button rect.
  3364. TreeItem *current_item;
  3365. int current_column, current_index, current_section;
  3366. _find_button_at_pos(mb->get_position(), current_item, current_column, current_index, current_section);
  3367. if (current_item == cache.click_item && current_column == cache.click_column && current_index == cache.click_index) {
  3368. emit_signal("button_clicked", cache.click_item, cache.click_column, cache.click_id, mb->get_button_index());
  3369. }
  3370. }
  3371. cache.click_type = Cache::CLICK_NONE;
  3372. cache.click_index = -1;
  3373. cache.click_id = -1;
  3374. cache.click_item = nullptr;
  3375. cache.click_column = 0;
  3376. queue_redraw();
  3377. return;
  3378. }
  3379. if (range_drag_enabled) {
  3380. return;
  3381. }
  3382. switch (mb->get_button_index()) {
  3383. case MouseButton::RIGHT:
  3384. case MouseButton::LEFT: {
  3385. Ref<StyleBox> bg = theme_cache.panel_style;
  3386. Point2 pos = mb->get_position();
  3387. if (rtl) {
  3388. pos.x = get_size().width - pos.x - 1;
  3389. }
  3390. pos -= bg->get_offset();
  3391. cache.click_type = Cache::CLICK_NONE;
  3392. if (show_column_titles) {
  3393. pos.y -= _get_title_button_height();
  3394. if (pos.y < 0) {
  3395. pos.x += theme_cache.offset.x;
  3396. int len = 0;
  3397. for (int i = 0; i < columns.size(); i++) {
  3398. len += get_column_width(i);
  3399. if (pos.x < static_cast<real_t>(len)) {
  3400. cache.click_type = Cache::CLICK_TITLE;
  3401. cache.click_index = i;
  3402. queue_redraw();
  3403. break;
  3404. }
  3405. }
  3406. break;
  3407. }
  3408. }
  3409. if (!root || (!root->get_first_child() && hide_root)) {
  3410. emit_signal(SNAME("empty_clicked"), get_local_mouse_position(), mb->get_button_index());
  3411. break;
  3412. }
  3413. click_handled = false;
  3414. pressing_for_editor = false;
  3415. propagate_mouse_activated = false;
  3416. int x_limit = _get_content_rect().size.x;
  3417. cache.rtl = is_layout_rtl();
  3418. blocked++;
  3419. 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);
  3420. blocked--;
  3421. if (pressing_for_editor) {
  3422. pressing_pos = mb->get_position();
  3423. if (rtl) {
  3424. pressing_pos.x = get_size().width - pressing_pos.x;
  3425. }
  3426. }
  3427. if (mb->get_button_index() == MouseButton::RIGHT) {
  3428. break;
  3429. }
  3430. if (drag_touching) {
  3431. set_process_internal(false);
  3432. drag_touching_deaccel = false;
  3433. drag_touching = false;
  3434. drag_speed = 0;
  3435. drag_from = 0;
  3436. }
  3437. if (!click_handled) {
  3438. drag_speed = 0;
  3439. drag_accum = 0;
  3440. drag_from = v_scroll->get_value();
  3441. drag_touching = DisplayServer::get_singleton()->is_touchscreen_available();
  3442. drag_touching_deaccel = false;
  3443. if (drag_touching) {
  3444. set_process_internal(true);
  3445. }
  3446. if (mb->get_button_index() == MouseButton::LEFT) {
  3447. if (get_item_at_position(mb->get_position()) == nullptr && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed()) {
  3448. emit_signal(SNAME("nothing_selected"));
  3449. }
  3450. }
  3451. }
  3452. if (propagate_mouse_activated) {
  3453. emit_signal(SNAME("item_activated"));
  3454. propagate_mouse_activated = false;
  3455. }
  3456. } break;
  3457. case MouseButton::WHEEL_UP: {
  3458. if (_scroll(mb->is_shift_pressed(), -mb->get_factor() / 8)) {
  3459. accept_event();
  3460. }
  3461. } break;
  3462. case MouseButton::WHEEL_DOWN: {
  3463. if (_scroll(mb->is_shift_pressed(), mb->get_factor() / 8)) {
  3464. accept_event();
  3465. }
  3466. } break;
  3467. case MouseButton::WHEEL_LEFT: {
  3468. if (_scroll(!mb->is_shift_pressed(), -mb->get_factor() / 8)) {
  3469. accept_event();
  3470. }
  3471. } break;
  3472. case MouseButton::WHEEL_RIGHT: {
  3473. if (_scroll(!mb->is_shift_pressed(), mb->get_factor() / 8)) {
  3474. accept_event();
  3475. }
  3476. } break;
  3477. default:
  3478. break;
  3479. }
  3480. }
  3481. Ref<InputEventPanGesture> pan_gesture = p_event;
  3482. if (pan_gesture.is_valid()) {
  3483. double prev_v = v_scroll->get_value();
  3484. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  3485. double prev_h = h_scroll->get_value();
  3486. if (is_layout_rtl()) {
  3487. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * -pan_gesture->get_delta().x / 8);
  3488. } else {
  3489. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
  3490. }
  3491. if (v_scroll->get_value() != prev_v || h_scroll->get_value() != prev_h) {
  3492. accept_event();
  3493. }
  3494. }
  3495. }
  3496. void Tree::_determine_hovered_item() {
  3497. hovered_update_queued = false;
  3498. Ref<StyleBox> bg = theme_cache.panel_style;
  3499. bool rtl = is_layout_rtl();
  3500. Point2 pos = hovered_pos;
  3501. if (rtl) {
  3502. pos.x = get_size().width - pos.x - 1;
  3503. }
  3504. pos -= theme_cache.panel_style->get_offset();
  3505. bool old_header_row = cache.hover_header_row;
  3506. int old_header_column = cache.hover_header_column;
  3507. TreeItem *old_item = cache.hover_item;
  3508. int old_column = cache.hover_column;
  3509. int old_button_index_in_column = cache.hover_button_index_in_column;
  3510. // Determine hover on column headers.
  3511. cache.hover_header_row = false;
  3512. cache.hover_header_column = 0;
  3513. if (show_column_titles && is_mouse_hovering) {
  3514. pos.y -= _get_title_button_height();
  3515. if (pos.y < 0) {
  3516. pos.x += theme_cache.offset.x;
  3517. int len = 0;
  3518. for (int i = 0; i < columns.size(); i++) {
  3519. len += get_column_width(i);
  3520. if (pos.x < len) {
  3521. cache.hover_header_row = true;
  3522. cache.hover_header_column = i;
  3523. cache.hover_button_index_in_column = -1;
  3524. break;
  3525. }
  3526. }
  3527. }
  3528. }
  3529. // Determine hover on rows and items.
  3530. if (root && is_mouse_hovering && hovered_pos.y >= 0) {
  3531. TreeItem *it;
  3532. int col, section, col_button_index;
  3533. _find_button_at_pos(hovered_pos, it, col, col_button_index, section);
  3534. if (drop_mode_flags) {
  3535. if (it != drop_mode_over) {
  3536. drop_mode_over = it;
  3537. queue_redraw();
  3538. }
  3539. if (it && section != drop_mode_section) {
  3540. drop_mode_section = section;
  3541. queue_redraw();
  3542. }
  3543. }
  3544. cache.hover_item = it;
  3545. cache.hover_column = col;
  3546. cache.hover_button_index_in_column = col_button_index;
  3547. if (it != old_item || col != old_column) {
  3548. if (old_item && old_column >= old_item->cells.size()) {
  3549. // Columns may have changed since last `redraw()`.
  3550. queue_redraw();
  3551. } else {
  3552. // Only need to update if mouse enters/exits a button.
  3553. bool was_over_button = old_item && old_item->cells[old_column].custom_button;
  3554. bool is_over_button = it && it->cells[col].custom_button;
  3555. if (was_over_button || is_over_button) {
  3556. queue_redraw();
  3557. }
  3558. }
  3559. }
  3560. }
  3561. // Reduce useless redraw calls.
  3562. bool hovered_cell_button_changed = (cache.hover_button_index_in_column != old_button_index_in_column);
  3563. bool hovered_column_changed = (cache.hover_column != old_column);
  3564. // Mouse has moved from row to row, or from cell to cell within same row unless selection mode is full row which saves a useless redraw.
  3565. bool item_hover_needs_redraw = !cache.hover_header_row && (cache.hover_item != old_item || hovered_cell_button_changed || (select_mode != SELECT_ROW && hovered_column_changed));
  3566. // Mouse has moved between two different column header sections.
  3567. bool header_hover_needs_redraw = cache.hover_header_row && cache.hover_header_column != old_header_column;
  3568. // Mouse has moved between header and "main" areas.
  3569. bool whole_needs_redraw = cache.hover_header_row != old_header_row;
  3570. if (whole_needs_redraw || header_hover_needs_redraw || item_hover_needs_redraw) {
  3571. queue_redraw();
  3572. }
  3573. }
  3574. void Tree::_queue_update_hovered_item() {
  3575. if (hovered_update_queued) {
  3576. return;
  3577. }
  3578. hovered_update_queued = true;
  3579. callable_mp(this, &Tree::_determine_hovered_item).call_deferred();
  3580. }
  3581. bool Tree::edit_selected(bool p_force_edit) {
  3582. TreeItem *s = get_selected();
  3583. ERR_FAIL_NULL_V_MSG(s, false, "No item selected.");
  3584. ensure_cursor_is_visible();
  3585. int col = get_selected_column();
  3586. ERR_FAIL_INDEX_V_MSG(col, columns.size(), false, "No item column selected.");
  3587. if (!s->cells[col].editable && !p_force_edit) {
  3588. return false;
  3589. }
  3590. Size2 scale = popup_editor->get_parent_viewport()->get_popup_base_transform().get_scale() * get_global_transform_with_canvas().get_scale();
  3591. real_t popup_scale = MIN(scale.x, scale.y);
  3592. Rect2 rect = _get_item_focus_rect(s);
  3593. rect.position *= popup_scale;
  3594. popup_edited_item = s;
  3595. popup_edited_item_col = col;
  3596. const TreeItem::Cell &c = s->cells[col];
  3597. if (c.mode == TreeItem::CELL_MODE_CHECK) {
  3598. s->set_checked(col, !c.checked);
  3599. item_edited(col, s);
  3600. return true;
  3601. } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
  3602. edited_item = s;
  3603. edited_col = col;
  3604. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  3605. emit_signal(SNAME("custom_popup_edited"), false);
  3606. item_edited(col, s);
  3607. return true;
  3608. } else if (c.mode == TreeItem::CELL_MODE_RANGE && !c.text.is_empty()) {
  3609. _update_popup_menu(c);
  3610. popup_menu->set_size(Size2(rect.size.width, 0));
  3611. popup_menu->set_position(get_screen_position() + rect.position + Point2i(0, rect.size.height));
  3612. popup_menu->popup();
  3613. popup_edited_item = s;
  3614. popup_edited_item_col = col;
  3615. return true;
  3616. } else if ((c.mode == TreeItem::CELL_MODE_STRING && !c.edit_multiline) || c.mode == TreeItem::CELL_MODE_RANGE) {
  3617. int value_editor_height = 0;
  3618. if (c.mode == TreeItem::CELL_MODE_RANGE) {
  3619. _update_value_editor(c);
  3620. value_editor_height = value_editor->get_minimum_size().height;
  3621. value_editor->show();
  3622. } else if (value_editor) {
  3623. value_editor->hide();
  3624. }
  3625. Rect2 popup_rect;
  3626. // `floor()` centers vertically.
  3627. Vector2 ofs(0, Math::floor((MAX(line_editor->get_minimum_size().height, rect.size.height - value_editor_height) - rect.size.height) / 2));
  3628. // Account for icon.
  3629. real_t icon_ofs = 0;
  3630. if (c.icon.is_valid()) {
  3631. icon_ofs = _get_cell_icon_size(c).x * popup_scale + theme_cache.h_separation;
  3632. }
  3633. popup_rect.size = rect.size + Vector2(-icon_ofs, value_editor_height);
  3634. popup_rect.position = rect.position - ofs;
  3635. popup_rect.position.x += icon_ofs;
  3636. if (cache.rtl) {
  3637. popup_rect.position.x = get_size().width - popup_rect.position.x - popup_rect.size.x;
  3638. }
  3639. popup_rect.position += get_screen_position();
  3640. line_editor->clear();
  3641. line_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::range_step_decimals(c.step)));
  3642. line_editor->select_all();
  3643. line_editor->show();
  3644. text_editor->hide();
  3645. popup_editor->set_position(popup_rect.position);
  3646. popup_editor->set_size(popup_rect.size * popup_scale);
  3647. if (!popup_editor->is_embedded()) {
  3648. popup_editor->set_content_scale_factor(popup_scale);
  3649. }
  3650. popup_edit_committed = false; // Start edit popup processing.
  3651. popup_editor->popup();
  3652. popup_editor->child_controls_changed();
  3653. line_editor->grab_focus();
  3654. return true;
  3655. } else if (c.mode == TreeItem::CELL_MODE_STRING && c.edit_multiline) {
  3656. line_editor->hide();
  3657. text_editor->clear();
  3658. text_editor->set_text(c.text);
  3659. text_editor->select_all();
  3660. text_editor->show();
  3661. popup_editor->set_position(get_screen_position() + rect.position);
  3662. popup_editor->set_size(rect.size * popup_scale);
  3663. if (!popup_editor->is_embedded()) {
  3664. popup_editor->set_content_scale_factor(popup_scale);
  3665. }
  3666. popup_edit_committed = false; // Start edit popup processing.
  3667. popup_editor->popup();
  3668. popup_editor->child_controls_changed();
  3669. text_editor->grab_focus();
  3670. return true;
  3671. }
  3672. return false;
  3673. }
  3674. Rect2 Tree::_get_item_focus_rect(const TreeItem *p_item) const {
  3675. Rect2 rect;
  3676. if (select_mode == SELECT_ROW) {
  3677. rect = p_item->get_meta("__focus_col_" + itos(selected_col));
  3678. } else {
  3679. rect = p_item->get_meta("__focus_rect");
  3680. }
  3681. return rect;
  3682. }
  3683. bool Tree::is_editing() {
  3684. return popup_editor->is_visible();
  3685. }
  3686. void Tree::set_editor_selection(int p_from_line, int p_to_line, int p_from_column, int p_to_column, int p_caret) {
  3687. if (p_from_column == -1 || p_to_column == -1) {
  3688. line_editor->select(p_from_line, p_to_line);
  3689. } else {
  3690. text_editor->select(p_from_line, p_from_column, p_to_line, p_to_column, p_caret);
  3691. }
  3692. }
  3693. Size2 Tree::get_internal_min_size() const {
  3694. Size2i size;
  3695. if (root) {
  3696. size.height += get_item_height(root);
  3697. }
  3698. for (int i = 0; i < columns.size(); i++) {
  3699. size.width += get_column_minimum_width(i);
  3700. }
  3701. return size;
  3702. }
  3703. void Tree::update_scrollbars() {
  3704. const Size2 control_size = get_size();
  3705. const Ref<StyleBox> background = theme_cache.panel_style;
  3706. // This is the background stylebox's content rect.
  3707. const real_t width = control_size.x - background->get_margin(SIDE_LEFT) - background->get_margin(SIDE_RIGHT);
  3708. const real_t height = control_size.y - background->get_margin(SIDE_TOP) - background->get_margin(SIDE_BOTTOM);
  3709. const Rect2 content_rect = Rect2(background->get_offset(), Size2(width, height));
  3710. const Size2 hmin = h_scroll->get_combined_minimum_size();
  3711. const Size2 vmin = v_scroll->get_combined_minimum_size();
  3712. const Size2 internal_min_size = get_internal_min_size();
  3713. const int title_button_height = _get_title_button_height();
  3714. Size2 tree_content_size = content_rect.get_size() - Vector2(0, title_button_height);
  3715. bool display_vscroll = internal_min_size.height > tree_content_size.height;
  3716. bool display_hscroll = internal_min_size.width > tree_content_size.width;
  3717. for (int i = 0; i < 2; i++) {
  3718. // Check twice, as both values are dependent on each other.
  3719. if (display_hscroll) {
  3720. tree_content_size.height = content_rect.get_size().height - title_button_height - hmin.height;
  3721. display_vscroll = internal_min_size.height > tree_content_size.height;
  3722. }
  3723. if (display_vscroll) {
  3724. tree_content_size.width = content_rect.get_size().width - vmin.width;
  3725. display_hscroll = internal_min_size.width > tree_content_size.width;
  3726. }
  3727. }
  3728. if (display_vscroll) {
  3729. v_scroll->show();
  3730. v_scroll->set_max(internal_min_size.height);
  3731. v_scroll->set_page(tree_content_size.height);
  3732. theme_cache.offset.y = v_scroll->get_value();
  3733. } else {
  3734. v_scroll->hide();
  3735. theme_cache.offset.y = 0;
  3736. }
  3737. if (display_hscroll) {
  3738. h_scroll->show();
  3739. h_scroll->set_max(internal_min_size.width);
  3740. h_scroll->set_page(tree_content_size.width);
  3741. theme_cache.offset.x = h_scroll->get_value();
  3742. } else {
  3743. h_scroll->hide();
  3744. theme_cache.offset.x = 0;
  3745. }
  3746. const Rect2 scroll_rect = _get_scrollbar_layout_rect();
  3747. v_scroll->set_begin(scroll_rect.get_position() + Vector2(scroll_rect.get_size().x - vmin.width, 0));
  3748. v_scroll->set_end(scroll_rect.get_end() - Vector2(0, display_hscroll ? hmin.height : 0));
  3749. h_scroll->set_begin(scroll_rect.get_position() + Vector2(0, scroll_rect.get_size().y - hmin.height));
  3750. h_scroll->set_end(scroll_rect.get_end() - Vector2(display_vscroll ? vmin.width : 0, 0));
  3751. }
  3752. int Tree::_get_title_button_height() const {
  3753. ERR_FAIL_COND_V(theme_cache.tb_font.is_null() || theme_cache.title_button.is_null(), 0);
  3754. int h = 0;
  3755. if (show_column_titles) {
  3756. for (int i = 0; i < columns.size(); i++) {
  3757. h = MAX(h, columns[i].text_buf->get_size().y + theme_cache.title_button->get_minimum_size().height);
  3758. }
  3759. }
  3760. return h;
  3761. }
  3762. void Tree::_check_item_accessibility(TreeItem *p_item, PackedStringArray &r_warnings, int &r_row) const {
  3763. for (int i = 0; i < p_item->cells.size(); i++) {
  3764. const TreeItem::Cell &cell = p_item->cells[i];
  3765. if (cell.description.strip_edges().is_empty() && cell.text.strip_edges().is_empty()) {
  3766. r_warnings.push_back(vformat(RTR("Cell %d x %d: either text or alternative text must not be empty."), r_row, i));
  3767. }
  3768. for (int j = 0; j < cell.buttons.size(); j++) {
  3769. if (cell.buttons[j].description.strip_edges().is_empty()) {
  3770. r_warnings.push_back(vformat(RTR("Button %d in %d x %d: alternative text must not be empty."), j, r_row, i));
  3771. }
  3772. }
  3773. }
  3774. r_row++;
  3775. // Children.
  3776. if (!p_item->collapsed) {
  3777. TreeItem *c = p_item->first_child;
  3778. while (c) {
  3779. _check_item_accessibility(c, r_warnings, r_row);
  3780. c = c->next;
  3781. }
  3782. }
  3783. }
  3784. PackedStringArray Tree::get_accessibility_configuration_warnings() const {
  3785. PackedStringArray warnings = Control::get_accessibility_configuration_warnings();
  3786. if (root) {
  3787. int row = 1;
  3788. _check_item_accessibility(root, warnings, row);
  3789. }
  3790. return warnings;
  3791. }
  3792. void Tree::_accessibility_action_scroll_down(const Variant &p_data) {
  3793. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  3794. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 4);
  3795. } else {
  3796. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page());
  3797. }
  3798. }
  3799. void Tree::_accessibility_action_scroll_left(const Variant &p_data) {
  3800. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  3801. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 4);
  3802. } else {
  3803. h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page());
  3804. }
  3805. }
  3806. void Tree::_accessibility_action_scroll_right(const Variant &p_data) {
  3807. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  3808. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 4);
  3809. } else {
  3810. h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page());
  3811. }
  3812. }
  3813. void Tree::_accessibility_action_scroll_up(const Variant &p_data) {
  3814. if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) {
  3815. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 4);
  3816. } else {
  3817. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page());
  3818. }
  3819. }
  3820. void Tree::_accessibility_action_scroll_set(const Variant &p_data) {
  3821. const Point2 &pos = p_data;
  3822. h_scroll->set_value(pos.x);
  3823. v_scroll->set_value(pos.y);
  3824. }
  3825. void Tree::_accessibility_action_scroll_into_view(const Variant &p_data, TreeItem *p_item, int p_col) {
  3826. scroll_to_item(p_item);
  3827. }
  3828. void Tree::_accessibility_action_focus(const Variant &p_data, TreeItem *p_item, int p_col) {
  3829. p_item->select(p_col);
  3830. }
  3831. void Tree::_accessibility_action_blur(const Variant &p_data, TreeItem *p_item, int p_col) {
  3832. p_item->deselect(p_col);
  3833. }
  3834. void Tree::_accessibility_action_collapse(const Variant &p_data, TreeItem *p_item) {
  3835. p_item->set_collapsed(true);
  3836. }
  3837. void Tree::_accessibility_action_expand(const Variant &p_data, TreeItem *p_item) {
  3838. p_item->set_collapsed(false);
  3839. }
  3840. void Tree::_accessibility_action_set_text_value(const Variant &p_data, TreeItem *p_item, int p_col) {
  3841. p_item->set_text(p_col, p_data);
  3842. }
  3843. void Tree::_accessibility_action_set_num_value(const Variant &p_data, TreeItem *p_item, int p_col) {
  3844. p_item->set_range(p_col, p_data);
  3845. }
  3846. void Tree::_accessibility_action_set_bool_value(const Variant &p_data, TreeItem *p_item, int p_col) {
  3847. p_item->set_checked(p_col, !p_item->cells[p_col].checked);
  3848. }
  3849. void Tree::_accessibility_action_edit_custom(const Variant &p_data, TreeItem *p_item, int p_col) {
  3850. float popup_scale = popup_editor->is_embedded() ? 1.0 : popup_editor->get_parent_visible_window()->get_content_scale_factor();
  3851. Rect2 rect;
  3852. if (select_mode == SELECT_ROW) {
  3853. rect = p_item->get_meta("__focus_col_" + itos(p_col));
  3854. } else {
  3855. rect = p_item->get_meta("__focus_rect");
  3856. }
  3857. rect.position *= popup_scale;
  3858. edited_item = p_item;
  3859. edited_col = p_col;
  3860. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  3861. emit_signal(SNAME("custom_popup_edited"), false);
  3862. item_edited(p_col, p_item);
  3863. }
  3864. void Tree::_accessibility_action_set_inc(const Variant &p_data, TreeItem *p_item, int p_col) {
  3865. p_item->set_range(p_col, p_item->cells[p_col].val + p_item->cells[p_col].step);
  3866. }
  3867. void Tree::_accessibility_action_set_dec(const Variant &p_data, TreeItem *p_item, int p_col) {
  3868. p_item->set_range(p_col, p_item->cells[p_col].val - p_item->cells[p_col].step);
  3869. }
  3870. void Tree::_accessibility_action_button_press(const Variant &p_data, TreeItem *p_item, int p_col, int p_btn) {
  3871. emit_signal("button_clicked", p_item, p_col, p_btn, MouseButton::LEFT);
  3872. }
  3873. RID Tree::get_focused_accessibility_element() const {
  3874. if (selected_item) {
  3875. if (selected_col >= 0) {
  3876. if (selected_button >= 0) {
  3877. return selected_item->cells[selected_col].buttons[selected_button].accessibility_button_element;
  3878. } else {
  3879. return selected_item->cells[selected_col].accessibility_cell_element;
  3880. }
  3881. } else {
  3882. return selected_item->accessibility_row_element;
  3883. }
  3884. } else {
  3885. return get_accessibility_element();
  3886. }
  3887. }
  3888. void Tree::_accessibility_clean_info(TreeItem *p_item) {
  3889. p_item->accessibility_row_element = RID();
  3890. for (TreeItem::Cell &cell : p_item->cells) {
  3891. cell.accessibility_cell_element = RID();
  3892. for (TreeItem::Cell::Button &btn : cell.buttons) {
  3893. btn.accessibility_button_element = RID();
  3894. }
  3895. }
  3896. // Children.
  3897. TreeItem *c = p_item->first_child;
  3898. while (c) {
  3899. _accessibility_clean_info(c);
  3900. c = c->next;
  3901. }
  3902. }
  3903. void Tree::_accessibility_update_item(Point2 &r_ofs, TreeItem *p_item, int &r_row, int p_level) {
  3904. // Row.
  3905. if ((p_item != root || !hide_root) && p_item->is_visible()) {
  3906. if (p_item->accessibility_row_element.is_null()) {
  3907. p_item->accessibility_row_element = DisplayServer::get_singleton()->accessibility_create_sub_element(accessibility_scroll_element, DisplayServer::AccessibilityRole::ROLE_TREE_ITEM);
  3908. p_item->accessibility_row_dirty = true;
  3909. }
  3910. DisplayServer::get_singleton()->accessibility_update_set_table_row_index(p_item->accessibility_row_element, r_row);
  3911. DisplayServer::get_singleton()->accessibility_update_set_list_item_level(p_item->accessibility_row_element, p_level);
  3912. DisplayServer::get_singleton()->accessibility_update_set_list_item_expanded(p_item->accessibility_row_element, !p_item->collapsed);
  3913. DisplayServer::get_singleton()->accessibility_update_set_flag(p_item->accessibility_row_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !(p_item->visible && !p_item->parent_visible_in_tree));
  3914. DisplayServer::get_singleton()->accessibility_update_add_action(p_item->accessibility_row_element, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &Tree::_accessibility_action_collapse).bind(p_item));
  3915. DisplayServer::get_singleton()->accessibility_update_add_action(p_item->accessibility_row_element, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &Tree::_accessibility_action_expand).bind(p_item));
  3916. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(p_item->accessibility_row_element, selected_item == p_item);
  3917. if (p_item == root && is_root_hidden()) {
  3918. DisplayServer::get_singleton()->accessibility_update_set_flag(p_item->accessibility_row_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, true);
  3919. }
  3920. Transform2D row_xform;
  3921. row_xform.set_origin(r_ofs);
  3922. DisplayServer::get_singleton()->accessibility_update_set_transform(p_item->accessibility_row_element, row_xform);
  3923. Size2 item_size = Size2(get_size().width, compute_item_height(p_item));
  3924. DisplayServer::get_singleton()->accessibility_update_set_bounds(p_item->accessibility_row_element, Rect2(Vector2(), item_size));
  3925. if (p_item->accessibility_row_dirty) {
  3926. // Cells.
  3927. int col_offset = 0;
  3928. for (int i = 0; i < p_item->cells.size(); i++) {
  3929. TreeItem::Cell &cell = p_item->cells.write[i];
  3930. if (cell.accessibility_cell_element.is_null()) {
  3931. cell.accessibility_cell_element = DisplayServer::get_singleton()->accessibility_create_sub_element(p_item->accessibility_row_element, DisplayServer::AccessibilityRole::ROLE_CELL);
  3932. }
  3933. float cw = get_column_width(i);
  3934. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &Tree::_accessibility_action_scroll_into_view).bind(p_item, i));
  3935. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &Tree::_accessibility_action_focus).bind(p_item, i));
  3936. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &Tree::_accessibility_action_blur).bind(p_item, i));
  3937. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_COLLAPSE, callable_mp(this, &Tree::_accessibility_action_collapse).bind(p_item));
  3938. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_EXPAND, callable_mp(this, &Tree::_accessibility_action_expand).bind(p_item));
  3939. DisplayServer::get_singleton()->accessibility_update_set_table_cell_position(cell.accessibility_cell_element, r_row, i);
  3940. DisplayServer::get_singleton()->accessibility_update_set_list_item_selected(cell.accessibility_cell_element, cell.selected);
  3941. if (cell.description.is_empty()) {
  3942. DisplayServer::get_singleton()->accessibility_update_set_name(cell.accessibility_cell_element, cell.xl_text);
  3943. } else {
  3944. DisplayServer::get_singleton()->accessibility_update_set_name(cell.accessibility_cell_element, cell.description);
  3945. }
  3946. DisplayServer::get_singleton()->accessibility_update_set_text_align(cell.accessibility_cell_element, cell.text_alignment);
  3947. DisplayServer::get_singleton()->accessibility_update_set_flag(cell.accessibility_cell_element, DisplayServer::AccessibilityFlags::FLAG_HIDDEN, !(p_item->visible && !p_item->parent_visible_in_tree));
  3948. DisplayServer::get_singleton()->accessibility_update_set_flag(cell.accessibility_cell_element, DisplayServer::AccessibilityFlags::FLAG_READONLY, !cell.editable);
  3949. DisplayServer::get_singleton()->accessibility_update_set_tooltip(cell.accessibility_cell_element, cell.tooltip);
  3950. switch (cell.mode) {
  3951. case TreeItem::CELL_MODE_STRING: {
  3952. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Tree::_accessibility_action_set_text_value).bind(p_item, i));
  3953. DisplayServer::get_singleton()->accessibility_update_set_value(cell.accessibility_cell_element, cell.xl_text);
  3954. } break;
  3955. case TreeItem::CELL_MODE_CHECK: {
  3956. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_set_bool_value).bind(p_item, i));
  3957. DisplayServer::get_singleton()->accessibility_update_set_checked(cell.accessibility_cell_element, cell.checked);
  3958. } break;
  3959. case TreeItem::CELL_MODE_RANGE: {
  3960. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_DECREMENT, callable_mp(this, &Tree::_accessibility_action_set_dec).bind(p_item, i));
  3961. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_INCREMENT, callable_mp(this, &Tree::_accessibility_action_set_inc).bind(p_item, i));
  3962. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &Tree::_accessibility_action_set_num_value).bind(p_item, i));
  3963. DisplayServer::get_singleton()->accessibility_update_set_num_value(cell.accessibility_cell_element, cell.val);
  3964. DisplayServer::get_singleton()->accessibility_update_set_num_range(cell.accessibility_cell_element, cell.min, cell.max);
  3965. if (cell.step > 0) {
  3966. DisplayServer::get_singleton()->accessibility_update_set_num_step(cell.accessibility_cell_element, cell.step);
  3967. } else {
  3968. DisplayServer::get_singleton()->accessibility_update_set_num_step(cell.accessibility_cell_element, 1);
  3969. }
  3970. } break;
  3971. case TreeItem::CELL_MODE_ICON: {
  3972. // NOP
  3973. } break;
  3974. case TreeItem::CELL_MODE_CUSTOM: {
  3975. DisplayServer::get_singleton()->accessibility_update_add_action(cell.accessibility_cell_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_edit_custom).bind(p_item, i));
  3976. } break;
  3977. }
  3978. DisplayServer::get_singleton()->accessibility_update_set_background_color(cell.accessibility_cell_element, cell.color);
  3979. DisplayServer::get_singleton()->accessibility_update_set_foreground_color(cell.accessibility_cell_element, cell.bg_color);
  3980. DisplayServer::get_singleton()->accessibility_update_set_bounds(cell.accessibility_cell_element, Rect2(Point2(col_offset, 0), Size2(cw, item_size.y)));
  3981. Vector2 ofst = Vector2(col_offset + cw, 0);
  3982. for (int j = cell.buttons.size() - 1; j >= 0; j--) {
  3983. if (cell.buttons[j].accessibility_button_element.is_null()) {
  3984. cell.buttons[j].accessibility_button_element = DisplayServer::get_singleton()->accessibility_create_sub_element(cell.accessibility_cell_element, DisplayServer::AccessibilityRole::ROLE_BUTTON);
  3985. }
  3986. DisplayServer::get_singleton()->accessibility_update_add_action(cell.buttons[j].accessibility_button_element, DisplayServer::AccessibilityAction::ACTION_CLICK, callable_mp(this, &Tree::_accessibility_action_button_press).bind(p_item, i, j));
  3987. DisplayServer::get_singleton()->accessibility_update_set_flag(cell.buttons[j].accessibility_button_element, DisplayServer::AccessibilityFlags::FLAG_DISABLED, cell.buttons[j].disabled);
  3988. DisplayServer::get_singleton()->accessibility_update_set_tooltip(cell.buttons[j].accessibility_button_element, cell.buttons[j].tooltip);
  3989. if (cell.buttons[j].description.is_empty()) {
  3990. DisplayServer::get_singleton()->accessibility_update_set_name(cell.buttons[j].accessibility_button_element, cell.buttons[j].tooltip);
  3991. } else {
  3992. DisplayServer::get_singleton()->accessibility_update_set_name(cell.buttons[j].accessibility_button_element, cell.buttons[j].description);
  3993. }
  3994. Ref<Texture2D> b = cell.buttons[j].texture;
  3995. Size2 b_size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  3996. ofst.x -= b_size.x;
  3997. DisplayServer::get_singleton()->accessibility_update_set_bounds(cell.buttons[j].accessibility_button_element, Rect2(ofst, b_size));
  3998. }
  3999. col_offset += cw;
  4000. }
  4001. }
  4002. r_ofs.y += item_size.y;
  4003. r_ofs.y += theme_cache.v_separation;
  4004. p_item->accessibility_row_dirty = false;
  4005. r_row++;
  4006. }
  4007. // Children.
  4008. if (!p_item->collapsed) {
  4009. TreeItem *c = p_item->first_child;
  4010. while (c) {
  4011. _accessibility_update_item(r_ofs, c, r_row, p_level + 1);
  4012. c = c->next;
  4013. }
  4014. }
  4015. }
  4016. void Tree::_notification(int p_what) {
  4017. switch (p_what) {
  4018. case NOTIFICATION_EXIT_TREE:
  4019. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  4020. if (root) {
  4021. _accessibility_clean_info(root);
  4022. }
  4023. for (ColumnInfo &col : columns) {
  4024. col.accessibility_col_element = RID();
  4025. }
  4026. accessibility_scroll_element = RID();
  4027. } break;
  4028. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  4029. RID ae = get_accessibility_element();
  4030. ERR_FAIL_COND(ae.is_null());
  4031. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_TREE);
  4032. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &Tree::_accessibility_action_scroll_down));
  4033. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &Tree::_accessibility_action_scroll_left));
  4034. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &Tree::_accessibility_action_scroll_right));
  4035. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &Tree::_accessibility_action_scroll_up));
  4036. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &Tree::_accessibility_action_scroll_set));
  4037. Ref<StyleBox> bg = theme_cache.panel_style;
  4038. int tbh = _get_title_button_height();
  4039. // Columns.
  4040. int ofs = theme_cache.panel_style->get_margin(SIDE_LEFT);
  4041. int cs = columns.size();
  4042. for (int i = 0; i < cs; i++) {
  4043. ColumnInfo &column = columns.write[i];
  4044. if (column.accessibility_col_element.is_null()) {
  4045. column.accessibility_col_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_COLUMN_HEADER);
  4046. }
  4047. DisplayServer::get_singleton()->accessibility_update_set_table_column_index(column.accessibility_col_element, i);
  4048. DisplayServer::get_singleton()->accessibility_update_set_name(column.accessibility_col_element, column.xl_title);
  4049. DisplayServer::get_singleton()->accessibility_update_set_text_align(column.accessibility_col_element, column.title_alignment);
  4050. Rect2 tbrect = Rect2(ofs - theme_cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
  4051. if (cache.rtl) {
  4052. tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
  4053. }
  4054. ofs += tbrect.size.width;
  4055. DisplayServer::get_singleton()->accessibility_update_set_bounds(column.accessibility_col_element, Rect2(tbrect.position, tbrect.size));
  4056. }
  4057. DisplayServer::get_singleton()->accessibility_update_set_table_column_count(ae, cs);
  4058. // Scroll container.
  4059. if (accessibility_scroll_element.is_null()) {
  4060. accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
  4061. }
  4062. Transform2D scroll_xform;
  4063. scroll_xform.set_origin(Vector2i(-h_scroll->get_value(), -v_scroll->get_value()));
  4064. DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform);
  4065. DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, Rect2(0, 0, h_scroll->get_max(), v_scroll->get_max()));
  4066. // Rows (and cells).
  4067. Point2 origin = Point2(theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.offset.x, bg->get_margin(SIDE_TOP) + tbh);
  4068. int rows = 0;
  4069. if (root) {
  4070. _accessibility_update_item(origin, root, rows, 0);
  4071. }
  4072. DisplayServer::get_singleton()->accessibility_update_set_table_row_count(ae, rows);
  4073. } break;
  4074. case NOTIFICATION_FOCUS_ENTER: {
  4075. if (get_viewport()) {
  4076. focus_in_id = get_viewport()->get_processed_events_count();
  4077. }
  4078. } break;
  4079. case NOTIFICATION_MOUSE_ENTER: {
  4080. is_mouse_hovering = true;
  4081. _determine_hovered_item();
  4082. } break;
  4083. case NOTIFICATION_MOUSE_EXIT: {
  4084. is_mouse_hovering = false;
  4085. // Clear hovered item cache.
  4086. if (cache.hover_header_row || cache.hover_item != nullptr) {
  4087. cache.hover_header_row = false;
  4088. cache.hover_header_column = -1;
  4089. cache.hover_item = nullptr;
  4090. cache.hover_column = -1;
  4091. cache.hover_button_index_in_column = -1;
  4092. queue_redraw();
  4093. }
  4094. } break;
  4095. case NOTIFICATION_VISIBILITY_CHANGED: {
  4096. drag_touching = false;
  4097. } break;
  4098. case NOTIFICATION_DRAG_END: {
  4099. drop_mode_flags = 0;
  4100. scrolling = false;
  4101. set_process_internal(false);
  4102. queue_redraw();
  4103. } break;
  4104. case NOTIFICATION_DRAG_BEGIN: {
  4105. single_select_defer = nullptr;
  4106. if (theme_cache.scroll_speed > 0) {
  4107. scrolling = true;
  4108. set_process_internal(true);
  4109. }
  4110. } break;
  4111. case NOTIFICATION_INTERNAL_PROCESS: {
  4112. if (drag_touching) {
  4113. if (drag_touching_deaccel) {
  4114. float pos = v_scroll->get_value();
  4115. pos += drag_speed * get_process_delta_time();
  4116. bool turnoff = false;
  4117. if (pos < 0) {
  4118. pos = 0;
  4119. turnoff = true;
  4120. set_process_internal(false);
  4121. drag_touching = false;
  4122. drag_touching_deaccel = false;
  4123. }
  4124. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  4125. pos = v_scroll->get_max() - v_scroll->get_page();
  4126. turnoff = true;
  4127. }
  4128. v_scroll->set_value(pos);
  4129. float sgn = drag_speed < 0 ? -1 : 1;
  4130. float val = Math::abs(drag_speed);
  4131. val -= 1000 * get_process_delta_time();
  4132. if (val < 0) {
  4133. turnoff = true;
  4134. }
  4135. drag_speed = sgn * val;
  4136. if (turnoff) {
  4137. set_process_internal(false);
  4138. drag_touching = false;
  4139. drag_touching_deaccel = false;
  4140. }
  4141. }
  4142. }
  4143. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  4144. if (scrolling && get_rect().grow(theme_cache.scroll_border).has_point(mouse_position)) {
  4145. Point2 point;
  4146. if ((Math::abs(mouse_position.x) < Math::abs(mouse_position.x - get_size().width)) && (Math::abs(mouse_position.x) < theme_cache.scroll_border)) {
  4147. point.x = mouse_position.x - theme_cache.scroll_border;
  4148. } else if (Math::abs(mouse_position.x - get_size().width) < theme_cache.scroll_border) {
  4149. point.x = mouse_position.x - (get_size().width - theme_cache.scroll_border);
  4150. }
  4151. if ((Math::abs(mouse_position.y) < Math::abs(mouse_position.y - get_size().height)) && (Math::abs(mouse_position.y) < theme_cache.scroll_border)) {
  4152. point.y = mouse_position.y - theme_cache.scroll_border;
  4153. } else if (Math::abs(mouse_position.y - get_size().height) < theme_cache.scroll_border) {
  4154. point.y = mouse_position.y - (get_size().height - theme_cache.scroll_border);
  4155. }
  4156. point *= theme_cache.scroll_speed * get_process_delta_time();
  4157. point += get_scroll();
  4158. h_scroll->set_value(point.x);
  4159. v_scroll->set_value(point.y);
  4160. }
  4161. } break;
  4162. case NOTIFICATION_DRAW: {
  4163. v_scroll->set_custom_step(theme_cache.font->get_height(theme_cache.font_size));
  4164. update_scrollbars();
  4165. RID ci = get_canvas_item();
  4166. Ref<StyleBox> bg = theme_cache.panel_style;
  4167. const Rect2 content_rect = _get_content_rect();
  4168. Point2 draw_ofs = content_rect.position;
  4169. Size2 draw_size = content_rect.size;
  4170. bg->draw(ci, Rect2(Point2(), get_size()));
  4171. int tbh = _get_title_button_height();
  4172. draw_ofs.y += tbh;
  4173. draw_size.y -= tbh;
  4174. cache.rtl = is_layout_rtl();
  4175. content_scale_factor = popup_editor->is_embedded() ? 1.0 : popup_editor->get_parent_visible_window()->get_content_scale_factor();
  4176. if (root && get_size().x > 0 && get_size().y > 0) {
  4177. int self_height = 0; // Just to pass a reference, we don't need the root's `self_height`.
  4178. draw_item(Point2(), draw_ofs, draw_size, root, self_height);
  4179. }
  4180. if (show_column_titles) {
  4181. // Title buttons.
  4182. int ofs2 = theme_cache.panel_style->get_margin(SIDE_LEFT);
  4183. for (int i = 0; i < columns.size(); i++) {
  4184. Ref<StyleBox> sb = (cache.click_type == Cache::CLICK_TITLE && cache.click_index == i) ? theme_cache.title_button_pressed : ((cache.hover_header_row && cache.hover_header_column == i) ? theme_cache.title_button_hover : theme_cache.title_button);
  4185. Rect2 tbrect = Rect2(ofs2 - theme_cache.offset.x, bg->get_margin(SIDE_TOP), get_column_width(i), tbh);
  4186. if (cache.rtl) {
  4187. tbrect.position.x = get_size().width - tbrect.size.x - tbrect.position.x;
  4188. }
  4189. sb->draw(ci, tbrect);
  4190. ofs2 += tbrect.size.width;
  4191. // Text.
  4192. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  4193. columns.write[i].text_buf->set_width(clip_w);
  4194. columns.write[i].cached_minimum_width_dirty = true;
  4195. Vector2 text_pos = Point2i(tbrect.position.x, tbrect.position.y + (tbrect.size.height - columns[i].text_buf->get_size().y) / 2);
  4196. switch (columns[i].title_alignment) {
  4197. case HorizontalAlignment::HORIZONTAL_ALIGNMENT_LEFT: {
  4198. text_pos.x += cache.rtl ? tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x) : sb->get_offset().x;
  4199. break;
  4200. }
  4201. case HorizontalAlignment::HORIZONTAL_ALIGNMENT_RIGHT: {
  4202. text_pos.x += cache.rtl ? sb->get_offset().x : tbrect.size.width - (sb->get_offset().x + columns[i].text_buf->get_size().x);
  4203. break;
  4204. }
  4205. default: {
  4206. text_pos.x += (tbrect.size.width - columns[i].text_buf->get_size().x) / 2;
  4207. break;
  4208. }
  4209. }
  4210. if (theme_cache.font_outline_size > 0 && theme_cache.font_outline_color.a > 0) {
  4211. columns[i].text_buf->draw_outline(ci, text_pos, theme_cache.font_outline_size, theme_cache.font_outline_color);
  4212. }
  4213. columns[i].text_buf->draw(ci, text_pos, theme_cache.title_button_color);
  4214. }
  4215. }
  4216. // Draw the focus outline last, so that it is drawn in front of the section headings.
  4217. // Otherwise, section heading backgrounds can appear to be in front of the focus outline when scrolling.
  4218. if (has_focus(true)) {
  4219. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  4220. theme_cache.focus_style->draw(ci, Rect2(Point2(), get_size()));
  4221. RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  4222. }
  4223. } break;
  4224. case NOTIFICATION_THEME_CHANGED:
  4225. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  4226. case NOTIFICATION_TRANSLATION_CHANGED: {
  4227. _update_all();
  4228. } break;
  4229. case NOTIFICATION_RESIZED:
  4230. case NOTIFICATION_TRANSFORM_CHANGED: {
  4231. if (popup_edited_item != nullptr) {
  4232. Rect2 rect = _get_item_focus_rect(popup_edited_item);
  4233. popup_editor->set_position(get_global_position() + rect.position);
  4234. popup_editor->set_size(rect.size);
  4235. popup_editor->child_controls_changed();
  4236. }
  4237. } break;
  4238. }
  4239. }
  4240. void Tree::_update_all() {
  4241. for (int i = 0; i < columns.size(); i++) {
  4242. update_column(i);
  4243. }
  4244. if (root) {
  4245. update_item_cache(root);
  4246. }
  4247. }
  4248. Size2 Tree::get_minimum_size() const {
  4249. Vector2 min_size = Vector2(0, _get_title_button_height());
  4250. if (theme_cache.panel_style.is_valid()) {
  4251. min_size += theme_cache.panel_style->get_minimum_size();
  4252. }
  4253. Vector2 content_min_size = get_internal_min_size();
  4254. if (h_scroll_enabled) {
  4255. content_min_size.x = 0;
  4256. min_size.y += h_scroll->get_combined_minimum_size().height;
  4257. }
  4258. if (v_scroll_enabled) {
  4259. min_size.x += v_scroll->get_combined_minimum_size().width;
  4260. content_min_size.y = 0;
  4261. }
  4262. return min_size + content_min_size;
  4263. }
  4264. TreeItem *Tree::create_item(TreeItem *p_parent, int p_index) {
  4265. ERR_FAIL_COND_V(blocked > 0, nullptr);
  4266. TreeItem *ti = nullptr;
  4267. if (p_parent) {
  4268. ERR_FAIL_COND_V_MSG(p_parent->tree != this, nullptr, "A different tree owns the given parent");
  4269. ti = p_parent->create_child(p_index);
  4270. } else {
  4271. if (!root) {
  4272. // No root exists, make the given item the new root.
  4273. ti = memnew(TreeItem(this));
  4274. ERR_FAIL_NULL_V(ti, nullptr);
  4275. ti->cells.resize(columns.size());
  4276. ti->is_root = true;
  4277. root = ti;
  4278. } else {
  4279. // Root exists, append or insert to root.
  4280. ti = create_item(root, p_index);
  4281. }
  4282. }
  4283. _queue_update_hovered_item();
  4284. queue_accessibility_update();
  4285. return ti;
  4286. }
  4287. TreeItem *Tree::get_root() const {
  4288. return root;
  4289. }
  4290. TreeItem *Tree::get_last_item() const {
  4291. TreeItem *last = root;
  4292. while (last && last->last_child && !last->collapsed) {
  4293. last = last->last_child;
  4294. }
  4295. return last;
  4296. }
  4297. void Tree::item_edited(int p_column, TreeItem *p_item, MouseButton p_custom_mouse_index) {
  4298. edited_item = p_item;
  4299. edited_col = p_column;
  4300. if (p_item != nullptr && p_column >= 0 && p_column < p_item->cells.size()) {
  4301. edited_item->cells.write[p_column].dirty = true;
  4302. edited_item->cells.write[p_column].cached_minimum_size_dirty = true;
  4303. }
  4304. emit_signal(SNAME("item_edited"));
  4305. if (p_custom_mouse_index != MouseButton::NONE) {
  4306. emit_signal(SNAME("custom_item_clicked"), p_custom_mouse_index);
  4307. }
  4308. queue_accessibility_update();
  4309. }
  4310. void Tree::item_changed(int p_column, TreeItem *p_item) {
  4311. if (p_item != nullptr) {
  4312. if (p_column >= 0 && p_column < p_item->cells.size()) {
  4313. p_item->cells.write[p_column].dirty = true;
  4314. columns.write[p_column].cached_minimum_width_dirty = true;
  4315. } else if (p_column == -1) {
  4316. for (int i = 0; i < p_item->cells.size(); i++) {
  4317. p_item->cells.write[i].dirty = true;
  4318. columns.write[i].cached_minimum_width_dirty = true;
  4319. }
  4320. }
  4321. p_item->accessibility_row_dirty = true;
  4322. }
  4323. queue_accessibility_update();
  4324. queue_redraw();
  4325. }
  4326. void Tree::item_selected(int p_column, TreeItem *p_item) {
  4327. if (select_mode == SELECT_MULTI) {
  4328. if (!p_item->cells[p_column].selectable) {
  4329. return;
  4330. }
  4331. p_item->cells.write[p_column].selected = true;
  4332. //emit_signal(SNAME("multi_selected"),p_item,p_column,true); - NO this is for `TreeItem::select`
  4333. selected_col = p_column;
  4334. selected_item = p_item;
  4335. selected_button = -1;
  4336. } else {
  4337. select_single_item(p_item, root, p_column);
  4338. }
  4339. p_item->accessibility_row_dirty = true;
  4340. queue_accessibility_update();
  4341. queue_redraw();
  4342. }
  4343. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  4344. if (select_mode == SELECT_SINGLE && selected_item == p_item && selected_col == p_column) {
  4345. selected_item = nullptr;
  4346. selected_col = -1;
  4347. } else {
  4348. if (select_mode == SELECT_ROW && selected_item == p_item) {
  4349. selected_item = nullptr;
  4350. selected_col = -1;
  4351. } else {
  4352. if (select_mode == SELECT_MULTI) {
  4353. selected_item = p_item;
  4354. selected_col = p_column;
  4355. }
  4356. }
  4357. }
  4358. selected_button = -1;
  4359. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  4360. p_item->cells.write[p_column].selected = false;
  4361. } else if (select_mode == SELECT_ROW) {
  4362. for (int i = 0; i < p_item->cells.size(); i++) {
  4363. p_item->cells.write[i].selected = false;
  4364. }
  4365. }
  4366. p_item->accessibility_row_dirty = true;
  4367. queue_accessibility_update();
  4368. queue_redraw();
  4369. }
  4370. void Tree::set_select_mode(SelectMode p_mode) {
  4371. select_mode = p_mode;
  4372. }
  4373. Tree::SelectMode Tree::get_select_mode() const {
  4374. return select_mode;
  4375. }
  4376. void Tree::deselect_all() {
  4377. if (root) {
  4378. TreeItem *item = root;
  4379. while (item) {
  4380. if (select_mode == SELECT_ROW) {
  4381. item->deselect(0);
  4382. } else {
  4383. for (int i = 0; i < columns.size(); i++) {
  4384. item->deselect(i);
  4385. }
  4386. }
  4387. TreeItem *prev_item = item;
  4388. item = get_next_selected(root);
  4389. ERR_FAIL_COND(item == prev_item);
  4390. }
  4391. }
  4392. selected_item = nullptr;
  4393. selected_col = -1;
  4394. selected_button = -1;
  4395. queue_accessibility_update();
  4396. queue_redraw();
  4397. }
  4398. bool Tree::is_anything_selected() {
  4399. return (selected_item != nullptr);
  4400. }
  4401. void Tree::clear() {
  4402. ERR_FAIL_COND(blocked > 0);
  4403. if (pressing_for_editor) {
  4404. if (range_drag_enabled) {
  4405. range_drag_enabled = false;
  4406. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  4407. warp_mouse(range_drag_capture_pos);
  4408. }
  4409. pressing_for_editor = false;
  4410. }
  4411. if (root) {
  4412. memdelete(root);
  4413. root = nullptr;
  4414. };
  4415. selected_item = nullptr;
  4416. edited_item = nullptr;
  4417. popup_edited_item = nullptr;
  4418. popup_pressing_edited_item = nullptr;
  4419. _determine_hovered_item();
  4420. queue_accessibility_update();
  4421. queue_redraw();
  4422. }
  4423. void Tree::set_hide_root(bool p_enabled) {
  4424. if (hide_root == p_enabled) {
  4425. return;
  4426. }
  4427. hide_root = p_enabled;
  4428. queue_accessibility_update();
  4429. queue_redraw();
  4430. update_minimum_size();
  4431. }
  4432. bool Tree::is_root_hidden() const {
  4433. return hide_root;
  4434. }
  4435. void Tree::set_column_custom_minimum_width(int p_column, int p_min_width) {
  4436. ERR_FAIL_INDEX(p_column, columns.size());
  4437. if (columns[p_column].custom_min_width == p_min_width) {
  4438. return;
  4439. }
  4440. if (p_min_width < 0) {
  4441. return;
  4442. }
  4443. columns.write[p_column].custom_min_width = p_min_width;
  4444. columns.write[p_column].cached_minimum_width_dirty = true;
  4445. queue_accessibility_update();
  4446. queue_redraw();
  4447. }
  4448. void Tree::set_column_expand(int p_column, bool p_expand) {
  4449. ERR_FAIL_INDEX(p_column, columns.size());
  4450. if (columns[p_column].expand == p_expand) {
  4451. return;
  4452. }
  4453. columns.write[p_column].expand = p_expand;
  4454. columns.write[p_column].cached_minimum_width_dirty = true;
  4455. queue_accessibility_update();
  4456. queue_redraw();
  4457. }
  4458. void Tree::set_column_expand_ratio(int p_column, int p_ratio) {
  4459. ERR_FAIL_INDEX(p_column, columns.size());
  4460. if (columns[p_column].expand_ratio == p_ratio) {
  4461. return;
  4462. }
  4463. columns.write[p_column].expand_ratio = p_ratio;
  4464. columns.write[p_column].cached_minimum_width_dirty = true;
  4465. queue_accessibility_update();
  4466. queue_redraw();
  4467. }
  4468. void Tree::set_column_clip_content(int p_column, bool p_fit) {
  4469. ERR_FAIL_INDEX(p_column, columns.size());
  4470. if (columns[p_column].clip_content == p_fit) {
  4471. return;
  4472. }
  4473. columns.write[p_column].clip_content = p_fit;
  4474. columns.write[p_column].cached_minimum_width_dirty = true;
  4475. queue_accessibility_update();
  4476. queue_redraw();
  4477. }
  4478. bool Tree::is_column_expanding(int p_column) const {
  4479. ERR_FAIL_INDEX_V(p_column, columns.size(), false);
  4480. return columns[p_column].expand;
  4481. }
  4482. int Tree::get_column_expand_ratio(int p_column) const {
  4483. ERR_FAIL_INDEX_V(p_column, columns.size(), 1);
  4484. return columns[p_column].expand_ratio;
  4485. }
  4486. bool Tree::is_column_clipping_content(int p_column) const {
  4487. ERR_FAIL_INDEX_V(p_column, columns.size(), false);
  4488. return columns[p_column].clip_content;
  4489. }
  4490. TreeItem *Tree::get_selected() const {
  4491. return selected_item;
  4492. }
  4493. void Tree::set_selected(TreeItem *p_item, int p_column) {
  4494. ERR_FAIL_INDEX(p_column, columns.size());
  4495. ERR_FAIL_NULL(p_item);
  4496. ERR_FAIL_COND_MSG(p_item->get_tree() != this, "The provided TreeItem does not belong to this Tree. Ensure that the TreeItem is a part of the Tree before setting it as selected.");
  4497. select_single_item(p_item, get_root(), p_column);
  4498. }
  4499. int Tree::get_selected_column() const {
  4500. return selected_col;
  4501. }
  4502. TreeItem *Tree::get_edited() const {
  4503. return edited_item;
  4504. }
  4505. int Tree::get_edited_column() const {
  4506. return edited_col;
  4507. }
  4508. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  4509. if (!root) {
  4510. return nullptr;
  4511. }
  4512. while (true) {
  4513. if (!p_item) {
  4514. p_item = root;
  4515. } else {
  4516. if (p_item->first_child) {
  4517. p_item = p_item->first_child;
  4518. } else if (p_item->next) {
  4519. p_item = p_item->next;
  4520. } else {
  4521. while (!p_item->next) {
  4522. p_item = p_item->parent;
  4523. if (p_item == nullptr) {
  4524. return nullptr;
  4525. }
  4526. }
  4527. p_item = p_item->next;
  4528. }
  4529. }
  4530. for (int i = 0; i < columns.size(); i++) {
  4531. if (p_item->cells[i].selected) {
  4532. return p_item;
  4533. }
  4534. }
  4535. }
  4536. return nullptr;
  4537. }
  4538. int Tree::get_column_minimum_width(int p_column) const {
  4539. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  4540. if (columns[p_column].cached_minimum_width_dirty) {
  4541. // Use the custom minimum width.
  4542. int min_width = columns[p_column].custom_min_width;
  4543. // Check if the visible title of the column is wider.
  4544. if (show_column_titles) {
  4545. const float padding = theme_cache.title_button->get_margin(SIDE_LEFT) + theme_cache.title_button->get_margin(SIDE_RIGHT);
  4546. min_width = MAX(theme_cache.font->get_string_size(columns[p_column].xl_title, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + padding, min_width);
  4547. }
  4548. if (root && !columns[p_column].clip_content) {
  4549. int depth = 1;
  4550. TreeItem *last = nullptr;
  4551. TreeItem *first = hide_root ? root->get_next_visible() : root;
  4552. for (TreeItem *item = first; item; last = item, item = item->get_next_visible()) {
  4553. // Get column indentation.
  4554. int indent;
  4555. if (p_column == 0) {
  4556. if (last) {
  4557. if (item->parent == last) {
  4558. depth += 1;
  4559. } else if (item->parent != last->parent) {
  4560. depth = hide_root ? 0 : 1;
  4561. for (TreeItem *iter = item->parent; iter; iter = iter->parent) {
  4562. depth += 1;
  4563. }
  4564. }
  4565. }
  4566. indent = theme_cache.item_margin * depth;
  4567. } else {
  4568. indent = theme_cache.h_separation;
  4569. }
  4570. // Get the item minimum size.
  4571. Size2 item_size = item->get_minimum_size(p_column);
  4572. item_size.width += indent;
  4573. // Check if the item is wider.
  4574. min_width = MAX(min_width, item_size.width);
  4575. }
  4576. }
  4577. columns.get(p_column).cached_minimum_width = min_width;
  4578. columns.get(p_column).cached_minimum_width_dirty = false;
  4579. }
  4580. return columns[p_column].cached_minimum_width;
  4581. }
  4582. int Tree::get_column_width(int p_column) const {
  4583. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  4584. int column_width = get_column_minimum_width(p_column);
  4585. if (columns[p_column].expand) {
  4586. int expand_area = _get_content_rect().size.width;
  4587. int expanding_total = 0;
  4588. for (int i = 0; i < columns.size(); i++) {
  4589. expand_area -= get_column_minimum_width(i);
  4590. if (columns[i].expand) {
  4591. expanding_total += columns[i].expand_ratio;
  4592. }
  4593. }
  4594. if (expand_area >= expanding_total && expanding_total > 0) {
  4595. column_width += expand_area * columns[p_column].expand_ratio / expanding_total;
  4596. }
  4597. }
  4598. return column_width;
  4599. }
  4600. void Tree::propagate_set_columns(TreeItem *p_item) {
  4601. p_item->cells.resize(columns.size());
  4602. p_item->accessibility_row_dirty = true;
  4603. for (TreeItem::Cell &cell : p_item->cells) {
  4604. if (cell.accessibility_cell_element.is_valid()) {
  4605. DisplayServer::get_singleton()->accessibility_free_element(cell.accessibility_cell_element);
  4606. cell.accessibility_cell_element = RID();
  4607. }
  4608. for (TreeItem::Cell::Button &btn : cell.buttons) {
  4609. if (btn.accessibility_button_element.is_valid()) {
  4610. DisplayServer::get_singleton()->accessibility_free_element(btn.accessibility_button_element);
  4611. btn.accessibility_button_element = RID();
  4612. }
  4613. }
  4614. }
  4615. TreeItem *c = p_item->get_first_child();
  4616. while (c) {
  4617. propagate_set_columns(c);
  4618. c = c->next;
  4619. }
  4620. }
  4621. void Tree::set_columns(int p_columns) {
  4622. ERR_FAIL_COND(p_columns < 1);
  4623. ERR_FAIL_COND(blocked > 0);
  4624. if (columns.size() > p_columns) {
  4625. for (int i = p_columns; i < columns.size(); i++) {
  4626. if (columns[i].accessibility_col_element.is_valid()) {
  4627. DisplayServer::get_singleton()->accessibility_free_element(columns[i].accessibility_col_element);
  4628. columns.write[i].accessibility_col_element = RID();
  4629. }
  4630. }
  4631. }
  4632. columns.resize(p_columns);
  4633. if (root) {
  4634. propagate_set_columns(root);
  4635. }
  4636. if (selected_col >= p_columns) {
  4637. selected_col = p_columns - 1;
  4638. selected_button = -1;
  4639. }
  4640. queue_accessibility_update();
  4641. queue_redraw();
  4642. }
  4643. int Tree::get_columns() const {
  4644. return columns.size();
  4645. }
  4646. void Tree::_scroll_moved(float) {
  4647. _determine_hovered_item();
  4648. queue_redraw();
  4649. }
  4650. Rect2 Tree::get_custom_popup_rect() const {
  4651. return custom_popup_rect;
  4652. }
  4653. int Tree::get_item_offset(TreeItem *p_item) const {
  4654. TreeItem *it = root;
  4655. int ofs = _get_title_button_height();
  4656. if (!it) {
  4657. return 0;
  4658. }
  4659. while (true) {
  4660. if (it == p_item) {
  4661. return ofs;
  4662. }
  4663. if ((it != root || !hide_root) && it->is_visible_in_tree()) {
  4664. ofs += compute_item_height(it);
  4665. ofs += theme_cache.v_separation;
  4666. }
  4667. if (it->first_child && !it->collapsed) {
  4668. it = it->first_child;
  4669. } else if (it->next) {
  4670. it = it->next;
  4671. } else {
  4672. while (!it->next) {
  4673. it = it->parent;
  4674. if (it == nullptr) {
  4675. return 0;
  4676. }
  4677. }
  4678. it = it->next;
  4679. }
  4680. }
  4681. return -1; // Not found.
  4682. }
  4683. void Tree::ensure_cursor_is_visible() {
  4684. if (!is_inside_tree()) {
  4685. return;
  4686. }
  4687. if (!selected_item || (selected_col == -1)) {
  4688. return; // Nothing under cursor.
  4689. }
  4690. // Note: Code below similar to `Tree::scroll_to_item()`, in case of bug fix both.
  4691. const Size2 area_size = _get_content_rect().size;
  4692. int y_offset = get_item_offset(selected_item);
  4693. if (y_offset != -1) {
  4694. const int tbh = _get_title_button_height();
  4695. y_offset -= tbh;
  4696. const int cell_h = compute_item_height(selected_item) + theme_cache.v_separation;
  4697. int screen_h = area_size.height - tbh;
  4698. if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
  4699. v_scroll->set_value(y_offset);
  4700. } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
  4701. callable_mp((Range *)v_scroll, &Range::set_value).call_deferred(y_offset - screen_h + cell_h);
  4702. } else if (y_offset < v_scroll->get_value()) {
  4703. v_scroll->set_value(y_offset);
  4704. }
  4705. }
  4706. if (select_mode != SELECT_ROW) { // Cursor always at column 0 in this mode.
  4707. int x_offset = 0;
  4708. for (int i = 0; i < selected_col; i++) {
  4709. x_offset += get_column_width(i);
  4710. }
  4711. const int cell_w = get_column_width(selected_col);
  4712. const int screen_w = area_size.width;
  4713. if (cell_w > screen_w) {
  4714. h_scroll->set_value(x_offset);
  4715. } else if (x_offset + cell_w > h_scroll->get_value() + screen_w) {
  4716. callable_mp((Range *)h_scroll, &Range::set_value).call_deferred(x_offset - screen_w + cell_w);
  4717. } else if (x_offset < h_scroll->get_value()) {
  4718. h_scroll->set_value(x_offset);
  4719. }
  4720. }
  4721. queue_accessibility_update();
  4722. }
  4723. int Tree::get_pressed_button() const {
  4724. return pressed_button;
  4725. }
  4726. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column, int p_button) const {
  4727. ERR_FAIL_NULL_V(p_item, Rect2());
  4728. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  4729. if (p_column != -1) {
  4730. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  4731. }
  4732. if (p_button != -1) {
  4733. ERR_FAIL_COND_V(p_column == -1, Rect2()); // Pass a column if you want to pass a button.
  4734. ERR_FAIL_INDEX_V(p_button, p_item->cells[p_column].buttons.size(), Rect2());
  4735. }
  4736. int ofs = get_item_offset(p_item);
  4737. int height = compute_item_height(p_item) + theme_cache.v_separation;
  4738. Rect2 r;
  4739. r.position.y = ofs - theme_cache.offset.y + theme_cache.panel_style->get_offset().y;
  4740. r.size.height = height;
  4741. bool rtl = is_layout_rtl();
  4742. const Rect2 content_rect = _get_content_rect();
  4743. if (p_column == -1) {
  4744. r.position.x = 0;
  4745. r.size.x = get_size().width;
  4746. } else {
  4747. int accum = 0;
  4748. for (int i = 0; i < p_column; i++) {
  4749. accum += get_column_width(i);
  4750. }
  4751. r.position.x = (rtl) ? get_size().x - (accum - theme_cache.offset.x) - get_column_width(p_column) - theme_cache.panel_style->get_margin(SIDE_LEFT) : accum - theme_cache.offset.x + theme_cache.panel_style->get_margin(SIDE_LEFT);
  4752. r.size.x = get_column_width(p_column);
  4753. if (p_button != -1) {
  4754. // RTL direction support for button rect is different due to buttons not
  4755. // having same behavior as they do in LTR when tree is scrolled.
  4756. const TreeItem::Cell &c = p_item->cells[p_column];
  4757. Vector2 ofst = Vector2(r.position.x + r.size.x + theme_cache.button_margin, r.position.y);
  4758. // Compute total width of buttons block including spacings.
  4759. int buttons_width = 0;
  4760. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  4761. Ref<Texture2D> b = c.buttons[j].texture;
  4762. Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  4763. buttons_width += size.width + theme_cache.button_margin;
  4764. }
  4765. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  4766. Ref<Texture2D> b = c.buttons[j].texture;
  4767. Size2 size = b->get_size() + theme_cache.button_pressed->get_minimum_size();
  4768. ofst.x -= size.x + theme_cache.button_margin;
  4769. if (rtl) {
  4770. if (j == p_button) {
  4771. return Rect2(r.position, Size2(size.x, r.size.y));
  4772. }
  4773. r.position.x += size.x + theme_cache.button_margin;
  4774. continue;
  4775. }
  4776. if (j == p_button) {
  4777. float content_rect_end_x = content_rect.position.x + content_rect.size.width;
  4778. if (r.position.x + r.size.x < content_rect_end_x) {
  4779. return Rect2(ofst, Size2(size.x, r.size.y));
  4780. }
  4781. // Button block can be outside of `content_rect`.
  4782. if (content_rect_end_x - (r.position.x + theme_cache.h_separation) < buttons_width) {
  4783. return Rect2(r.position + Point2(theme_cache.h_separation + (buttons_width - ((r.position.x + r.size.x) - ofst.x)), 0), Size2(size.x, r.size.y));
  4784. }
  4785. return Rect2(ofst - Vector2((r.position.x + r.size.x) - content_rect_end_x, 0), Size2(size.x, r.size.y));
  4786. }
  4787. }
  4788. }
  4789. }
  4790. return r;
  4791. }
  4792. void Tree::set_column_titles_visible(bool p_show) {
  4793. if (show_column_titles == p_show) {
  4794. return;
  4795. }
  4796. show_column_titles = p_show;
  4797. queue_accessibility_update();
  4798. queue_redraw();
  4799. update_minimum_size();
  4800. }
  4801. bool Tree::are_column_titles_visible() const {
  4802. return show_column_titles;
  4803. }
  4804. void Tree::set_column_title(int p_column, const String &p_title) {
  4805. ERR_FAIL_INDEX(p_column, columns.size());
  4806. if (columns[p_column].title == p_title) {
  4807. return;
  4808. }
  4809. columns.write[p_column].title = p_title;
  4810. columns.write[p_column].xl_title = atr(p_title);
  4811. update_column(p_column);
  4812. queue_accessibility_update();
  4813. queue_redraw();
  4814. }
  4815. String Tree::get_column_title(int p_column) const {
  4816. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  4817. return columns[p_column].title;
  4818. }
  4819. void Tree::set_column_title_alignment(int p_column, HorizontalAlignment p_alignment) {
  4820. ERR_FAIL_INDEX(p_column, columns.size());
  4821. if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  4822. WARN_PRINT("HORIZONTAL_ALIGNMENT_FILL is not supported for column titles.");
  4823. }
  4824. if (columns[p_column].title_alignment == p_alignment) {
  4825. return;
  4826. }
  4827. columns.write[p_column].title_alignment = p_alignment;
  4828. update_column(p_column);
  4829. queue_accessibility_update();
  4830. queue_redraw();
  4831. }
  4832. HorizontalAlignment Tree::get_column_title_alignment(int p_column) const {
  4833. ERR_FAIL_INDEX_V(p_column, columns.size(), HorizontalAlignment::HORIZONTAL_ALIGNMENT_CENTER);
  4834. return columns[p_column].title_alignment;
  4835. }
  4836. void Tree::set_column_title_direction(int p_column, Control::TextDirection p_text_direction) {
  4837. ERR_FAIL_INDEX(p_column, columns.size());
  4838. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  4839. if (columns[p_column].text_direction != p_text_direction) {
  4840. columns.write[p_column].text_direction = p_text_direction;
  4841. update_column(p_column);
  4842. queue_accessibility_update();
  4843. queue_redraw();
  4844. }
  4845. }
  4846. Control::TextDirection Tree::get_column_title_direction(int p_column) const {
  4847. ERR_FAIL_INDEX_V(p_column, columns.size(), TEXT_DIRECTION_INHERITED);
  4848. return columns[p_column].text_direction;
  4849. }
  4850. void Tree::set_column_title_language(int p_column, const String &p_language) {
  4851. ERR_FAIL_INDEX(p_column, columns.size());
  4852. if (columns[p_column].language != p_language) {
  4853. columns.write[p_column].language = p_language;
  4854. update_column(p_column);
  4855. queue_accessibility_update();
  4856. queue_redraw();
  4857. }
  4858. }
  4859. String Tree::get_column_title_language(int p_column) const {
  4860. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  4861. return columns[p_column].language;
  4862. }
  4863. Point2 Tree::get_scroll() const {
  4864. Point2 ofs;
  4865. if (h_scroll->is_visible_in_tree()) {
  4866. ofs.x = h_scroll->get_value();
  4867. }
  4868. if (v_scroll->is_visible_in_tree()) {
  4869. ofs.y = v_scroll->get_value();
  4870. }
  4871. return ofs;
  4872. }
  4873. void Tree::scroll_to_item(TreeItem *p_item, bool p_center_on_item) {
  4874. ERR_FAIL_NULL(p_item);
  4875. update_scrollbars();
  4876. // Note: Code below similar to `Tree::ensure_cursor_is_visible()`, in case of bug fix both.
  4877. const Size2 area_size = _get_content_rect().size;
  4878. int y_offset = get_item_offset(p_item);
  4879. if (y_offset != -1) {
  4880. const int title_button_height = _get_title_button_height();
  4881. y_offset -= title_button_height;
  4882. const int cell_h = compute_item_height(p_item) + theme_cache.v_separation;
  4883. int screen_h = area_size.height - title_button_height;
  4884. if (p_center_on_item) {
  4885. // This makes sure that centering the offset doesn't overflow.
  4886. const double v_scroll_value = y_offset - MAX((screen_h - cell_h) / 2.0, 0.0);
  4887. v_scroll->set_value(v_scroll_value);
  4888. } else {
  4889. if (cell_h > screen_h) { // Screen size is too small, maybe it was not resized yet.
  4890. v_scroll->set_value(y_offset);
  4891. } else if (y_offset + cell_h > v_scroll->get_value() + screen_h) {
  4892. v_scroll->set_value(y_offset - screen_h + cell_h);
  4893. } else if (y_offset < v_scroll->get_value()) {
  4894. v_scroll->set_value(y_offset);
  4895. }
  4896. }
  4897. }
  4898. queue_accessibility_update();
  4899. }
  4900. void Tree::set_h_scroll_enabled(bool p_enable) {
  4901. if (h_scroll_enabled == p_enable) {
  4902. return;
  4903. }
  4904. h_scroll_enabled = p_enable;
  4905. update_minimum_size();
  4906. }
  4907. bool Tree::is_h_scroll_enabled() const {
  4908. return h_scroll_enabled;
  4909. }
  4910. void Tree::set_v_scroll_enabled(bool p_enable) {
  4911. if (v_scroll_enabled == p_enable) {
  4912. return;
  4913. }
  4914. v_scroll_enabled = p_enable;
  4915. update_minimum_size();
  4916. }
  4917. bool Tree::is_v_scroll_enabled() const {
  4918. return v_scroll_enabled;
  4919. }
  4920. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  4921. TreeItem *from = p_at;
  4922. TreeItem *loop = nullptr; // Safe-guard against infinite loop.
  4923. while (p_at) {
  4924. for (int i = 0; i < columns.size(); i++) {
  4925. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  4926. if (r_col) {
  4927. *r_col = i;
  4928. }
  4929. return p_at;
  4930. }
  4931. }
  4932. if (p_backwards) {
  4933. p_at = p_at->get_prev_visible(true);
  4934. } else {
  4935. p_at = p_at->get_next_visible(true);
  4936. }
  4937. if ((p_at) == from) {
  4938. break;
  4939. }
  4940. if (!loop) {
  4941. loop = p_at;
  4942. } else if (loop == p_at) {
  4943. break;
  4944. }
  4945. }
  4946. return nullptr;
  4947. }
  4948. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  4949. TreeItem *from = get_selected();
  4950. if (!from) {
  4951. from = root;
  4952. }
  4953. if (!from) {
  4954. return nullptr;
  4955. }
  4956. return _search_item_text(from->get_next_visible(true), p_find, r_col, p_selectable);
  4957. }
  4958. TreeItem *Tree::get_item_with_text(const String &p_find) const {
  4959. for (TreeItem *current = root; current; current = current->get_next_visible()) {
  4960. for (int i = 0; i < columns.size(); i++) {
  4961. if (current->get_text(i) == p_find) {
  4962. return current;
  4963. }
  4964. }
  4965. }
  4966. return nullptr;
  4967. }
  4968. TreeItem *Tree::get_item_with_metadata(const Variant &p_find, int p_column) const {
  4969. if (p_column < 0) {
  4970. for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
  4971. for (int i = 0; i < columns.size(); i++) {
  4972. if (current->get_metadata(i) == p_find) {
  4973. return current;
  4974. }
  4975. }
  4976. }
  4977. return nullptr;
  4978. }
  4979. for (TreeItem *current = root; current; current = current->get_next_in_tree()) {
  4980. if (current->get_metadata(p_column) == p_find) {
  4981. return current;
  4982. }
  4983. }
  4984. return nullptr;
  4985. }
  4986. void Tree::_do_incr_search(const String &p_add) {
  4987. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // Convert to msec.
  4988. uint64_t diff = time - last_keypress;
  4989. if (diff > uint64_t(GLOBAL_GET_CACHED(uint64_t, "gui/timers/incremental_search_max_interval_msec"))) {
  4990. incr_search = p_add;
  4991. } else if (incr_search != p_add) {
  4992. incr_search += p_add;
  4993. }
  4994. last_keypress = time;
  4995. int col;
  4996. TreeItem *item = search_item_text(incr_search, &col, true);
  4997. if (!item) {
  4998. return;
  4999. }
  5000. if (select_mode == SELECT_MULTI) {
  5001. item->set_as_cursor(col);
  5002. } else {
  5003. item->select(col);
  5004. }
  5005. ensure_cursor_is_visible();
  5006. }
  5007. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &r_height, int &r_section) const {
  5008. r_column = -1;
  5009. r_height = 0;
  5010. r_section = -100;
  5011. if (!root) {
  5012. return nullptr;
  5013. }
  5014. Point2 pos = p_pos;
  5015. if ((root != p_item || !hide_root) && p_item->is_visible_in_tree()) {
  5016. r_height = compute_item_height(p_item) + theme_cache.v_separation;
  5017. if (pos.y < r_height) {
  5018. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  5019. r_section = 0;
  5020. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  5021. r_section = pos.y < r_height / 2 ? -1 : 1;
  5022. } else if (pos.y < r_height / 4) {
  5023. r_section = -1;
  5024. } else if (pos.y >= (r_height * 3 / 4)) {
  5025. r_section = 1;
  5026. } else {
  5027. r_section = 0;
  5028. }
  5029. for (int i = 0; i < columns.size(); i++) {
  5030. int col_width = get_column_width(i);
  5031. if (p_item->cells[i].expand_right) {
  5032. int plus = 1;
  5033. 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() && p_item->cells[i + plus].buttons.is_empty()) {
  5034. col_width += theme_cache.h_separation;
  5035. col_width += get_column_width(i + plus);
  5036. plus++;
  5037. }
  5038. }
  5039. if (pos.x < col_width) {
  5040. r_column = i;
  5041. return p_item;
  5042. }
  5043. pos.x -= col_width;
  5044. }
  5045. return nullptr;
  5046. } else {
  5047. pos.y -= r_height;
  5048. }
  5049. } else {
  5050. r_height = 0;
  5051. }
  5052. if (p_item->is_collapsed() || !p_item->is_visible_in_tree()) {
  5053. return nullptr; // Do not try children, it's collapsed.
  5054. }
  5055. TreeItem *n = p_item->get_first_child();
  5056. while (n) {
  5057. int ch;
  5058. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, r_section);
  5059. pos.y -= ch;
  5060. r_height += ch;
  5061. if (r) {
  5062. return r;
  5063. }
  5064. n = n->get_next();
  5065. }
  5066. return nullptr;
  5067. }
  5068. void Tree::_find_button_at_pos(const Point2 &p_pos, TreeItem *&r_item, int &r_column, int &r_index, int &r_section) const {
  5069. // When on a button, `r_index` is valid.
  5070. // When on an item, both `r_item` and `r_column` are valid.
  5071. // Otherwise, all output arguments are invalid.
  5072. r_item = nullptr;
  5073. r_column = -1;
  5074. r_index = -1;
  5075. r_section = -1;
  5076. if (!root) {
  5077. return;
  5078. }
  5079. Point2 pos = p_pos;
  5080. if (cache.rtl) {
  5081. pos.x = get_size().width - pos.x - 1;
  5082. }
  5083. pos -= theme_cache.panel_style->get_offset();
  5084. pos.y -= _get_title_button_height();
  5085. if (pos.y < 0) {
  5086. return;
  5087. }
  5088. pos += theme_cache.offset; // Scrolling.
  5089. int x_limit = _get_content_rect().size.x + theme_cache.offset.width;
  5090. if (pos.x > x_limit) {
  5091. // Inside the scroll area.
  5092. return;
  5093. }
  5094. int col, h, section;
  5095. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5096. if (!it) {
  5097. return;
  5098. }
  5099. r_item = it;
  5100. r_column = col;
  5101. r_section = section;
  5102. const TreeItem::Cell &c = it->cells[col];
  5103. if (c.buttons.is_empty()) {
  5104. return;
  5105. }
  5106. // Search the button index.
  5107. FindColumnButtonResult result = _find_column_and_button_at_pos(pos.x, it, 0, x_limit);
  5108. r_index = result.button_index;
  5109. }
  5110. Tree::FindColumnButtonResult Tree::_find_column_and_button_at_pos(int p_x, const TreeItem *p_item, int p_x_ofs, int p_x_limit) const {
  5111. int x = p_x;
  5112. int col = -1;
  5113. int col_ofs = 0;
  5114. int col_width = 0;
  5115. int limit_w = p_x_limit;
  5116. FindColumnButtonResult result;
  5117. for (int i = 0; i < columns.size(); i++) {
  5118. col_width = get_column_width(i);
  5119. if (p_item->cells[i].expand_right) {
  5120. int plus = 1;
  5121. 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()) {
  5122. col_width += theme_cache.h_separation;
  5123. col_width += get_column_width(i + plus);
  5124. plus++;
  5125. }
  5126. }
  5127. if (x < col_width) {
  5128. col = i;
  5129. break;
  5130. }
  5131. col_ofs += col_width;
  5132. x -= col_width;
  5133. limit_w -= col_width;
  5134. }
  5135. if (col >= 0) {
  5136. if (col == 0) {
  5137. int margin = p_x_ofs + theme_cache.item_margin;
  5138. col_width -= margin;
  5139. limit_w -= margin;
  5140. col_ofs += margin;
  5141. x -= margin;
  5142. } else {
  5143. col_width -= theme_cache.h_separation;
  5144. limit_w -= theme_cache.h_separation;
  5145. x -= theme_cache.h_separation;
  5146. }
  5147. if (!cache.rtl && !p_item->cells[col].buttons.is_empty()) {
  5148. int button_w = 0;
  5149. for (int j = p_item->cells[col].buttons.size() - 1; j >= 0; j--) {
  5150. const Ref<Texture2D> &b = p_item->cells[col].buttons[j].texture;
  5151. button_w += b->get_size().width + theme_cache.button_pressed->get_minimum_size().width + theme_cache.button_margin;
  5152. }
  5153. col_width = MAX(button_w, MIN(limit_w, col_width));
  5154. }
  5155. // Cell button detection code.
  5156. // The first half of the button margin will be applied to the left button,
  5157. // and the other half to the right button.
  5158. const int offset_button_width = theme_cache.button_margin / 2;
  5159. const TreeItem::Cell &c = p_item->cells[col];
  5160. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  5161. const Ref<Texture2D> &b = c.buttons[j].texture;
  5162. int w = b->get_size().width + theme_cache.button_pressed->get_minimum_size().width;
  5163. if (x >= col_width - w - offset_button_width - 1) {
  5164. result.button_index = j;
  5165. break;
  5166. }
  5167. col_width -= w + theme_cache.button_margin;
  5168. }
  5169. }
  5170. result.column_index = col;
  5171. result.column_offset = col_ofs;
  5172. result.column_width = col_width;
  5173. result.pos_x = x;
  5174. return result;
  5175. }
  5176. int Tree::get_column_at_position(const Point2 &p_pos) const {
  5177. if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5178. return -1;
  5179. }
  5180. Point2 pos = p_pos;
  5181. if (is_layout_rtl()) {
  5182. pos.x = get_size().width - pos.x - 1;
  5183. }
  5184. pos -= theme_cache.panel_style->get_offset();
  5185. pos.y -= _get_title_button_height();
  5186. if (pos.y < 0) {
  5187. return -1;
  5188. }
  5189. if (h_scroll->is_visible_in_tree()) {
  5190. pos.x += h_scroll->get_value();
  5191. }
  5192. if (v_scroll->is_visible_in_tree()) {
  5193. pos.y += v_scroll->get_value();
  5194. }
  5195. int col, h, section;
  5196. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5197. if (it) {
  5198. return col;
  5199. }
  5200. return -1;
  5201. }
  5202. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  5203. if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5204. return -100;
  5205. }
  5206. Point2 pos = p_pos;
  5207. if (is_layout_rtl()) {
  5208. pos.x = get_size().width - pos.x - 1;
  5209. }
  5210. pos -= theme_cache.panel_style->get_offset();
  5211. pos.y -= _get_title_button_height();
  5212. if (pos.y < 0) {
  5213. return -100;
  5214. }
  5215. if (h_scroll->is_visible_in_tree()) {
  5216. pos.x += h_scroll->get_value();
  5217. }
  5218. if (v_scroll->is_visible_in_tree()) {
  5219. pos.y += v_scroll->get_value();
  5220. }
  5221. int col, h, section;
  5222. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5223. if (it) {
  5224. return section;
  5225. }
  5226. return -100;
  5227. }
  5228. bool Tree::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  5229. if (drag_touching) {
  5230. // Disable data drag & drop when touch dragging.
  5231. return false;
  5232. }
  5233. return Control::can_drop_data(p_point, p_data);
  5234. }
  5235. Variant Tree::get_drag_data(const Point2 &p_point) {
  5236. if (drag_touching) {
  5237. // Disable data drag & drop when touch dragging.
  5238. return Variant();
  5239. }
  5240. return Control::get_drag_data(p_point);
  5241. }
  5242. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  5243. if (!root || !Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5244. return nullptr;
  5245. }
  5246. Point2 pos = p_pos;
  5247. if (is_layout_rtl()) {
  5248. pos.x = get_size().width - pos.x - 1;
  5249. }
  5250. pos -= theme_cache.panel_style->get_offset();
  5251. pos.y -= _get_title_button_height();
  5252. if (pos.y < 0) {
  5253. return nullptr;
  5254. }
  5255. if (h_scroll->is_visible_in_tree()) {
  5256. pos.x += h_scroll->get_value();
  5257. }
  5258. if (v_scroll->is_visible_in_tree()) {
  5259. pos.y += v_scroll->get_value();
  5260. }
  5261. int col, h, section;
  5262. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  5263. if (it) {
  5264. return it;
  5265. }
  5266. return nullptr;
  5267. }
  5268. int Tree::get_button_id_at_position(const Point2 &p_pos) const {
  5269. if (!root) {
  5270. return -1;
  5271. }
  5272. if (p_pos == Vector2(Math::INF, Math::INF)) {
  5273. if (selected_item && selected_button >= 0) {
  5274. return selected_item->cells[selected_col].buttons[selected_button].id;
  5275. }
  5276. } else {
  5277. if (!Rect2(Vector2(), get_size()).has_point(p_pos)) {
  5278. return -1;
  5279. }
  5280. TreeItem *it;
  5281. int col, index, section;
  5282. _find_button_at_pos(p_pos, it, col, index, section);
  5283. if (index == -1) {
  5284. return -1;
  5285. }
  5286. return it->cells[col].buttons[index].id;
  5287. }
  5288. return -1;
  5289. }
  5290. String Tree::get_tooltip(const Point2 &p_pos) const {
  5291. Point2 pos = p_pos - theme_cache.panel_style->get_offset();
  5292. pos.y -= _get_title_button_height();
  5293. if (pos.y < 0) {
  5294. return Control::get_tooltip(p_pos);
  5295. }
  5296. TreeItem *it;
  5297. int col, index, section;
  5298. _find_button_at_pos(p_pos, it, col, index, section);
  5299. if (index != -1) {
  5300. return it->cells[col].buttons[index].tooltip;
  5301. }
  5302. if (it) {
  5303. const String item_tooltip = it->get_tooltip_text(col);
  5304. if (enable_auto_tooltip && item_tooltip.is_empty()) {
  5305. return it->get_text(col);
  5306. }
  5307. return item_tooltip;
  5308. }
  5309. return Control::get_tooltip(p_pos);
  5310. }
  5311. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  5312. cursor_can_exit_tree = p_enable;
  5313. }
  5314. void Tree::set_hide_folding(bool p_hide) {
  5315. if (hide_folding == p_hide) {
  5316. return;
  5317. }
  5318. hide_folding = p_hide;
  5319. queue_redraw();
  5320. }
  5321. bool Tree::is_folding_hidden() const {
  5322. return hide_folding;
  5323. }
  5324. void Tree::set_enable_recursive_folding(bool p_enable) {
  5325. enable_recursive_folding = p_enable;
  5326. }
  5327. bool Tree::is_recursive_folding_enabled() const {
  5328. return enable_recursive_folding;
  5329. }
  5330. void Tree::set_drop_mode_flags(int p_flags) {
  5331. if (drop_mode_flags == p_flags) {
  5332. return;
  5333. }
  5334. drop_mode_flags = p_flags;
  5335. if (drop_mode_flags == 0) {
  5336. drop_mode_over = nullptr;
  5337. }
  5338. queue_redraw();
  5339. }
  5340. int Tree::get_drop_mode_flags() const {
  5341. return drop_mode_flags;
  5342. }
  5343. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  5344. force_edit_checkbox_only_on_checkbox = p_enable;
  5345. }
  5346. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  5347. return force_edit_checkbox_only_on_checkbox;
  5348. }
  5349. void Tree::set_allow_rmb_select(bool p_allow) {
  5350. allow_rmb_select = p_allow;
  5351. }
  5352. bool Tree::get_allow_rmb_select() const {
  5353. return allow_rmb_select;
  5354. }
  5355. void Tree::set_allow_reselect(bool p_allow) {
  5356. allow_reselect = p_allow;
  5357. }
  5358. bool Tree::get_allow_reselect() const {
  5359. return allow_reselect;
  5360. }
  5361. void Tree::set_allow_search(bool p_allow) {
  5362. allow_search = p_allow;
  5363. }
  5364. bool Tree::get_allow_search() const {
  5365. return allow_search;
  5366. }
  5367. void Tree::set_auto_tooltip(bool p_enable) {
  5368. enable_auto_tooltip = p_enable;
  5369. }
  5370. bool Tree::is_auto_tooltip_enabled() const {
  5371. return enable_auto_tooltip;
  5372. }
  5373. void Tree::_bind_methods() {
  5374. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  5375. ClassDB::bind_method(D_METHOD("create_item", "parent", "index"), &Tree::create_item, DEFVAL(Variant()), DEFVAL(-1));
  5376. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  5377. ClassDB::bind_method(D_METHOD("set_column_custom_minimum_width", "column", "min_width"), &Tree::set_column_custom_minimum_width);
  5378. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  5379. ClassDB::bind_method(D_METHOD("set_column_expand_ratio", "column", "ratio"), &Tree::set_column_expand_ratio);
  5380. ClassDB::bind_method(D_METHOD("set_column_clip_content", "column", "enable"), &Tree::set_column_clip_content);
  5381. ClassDB::bind_method(D_METHOD("is_column_expanding", "column"), &Tree::is_column_expanding);
  5382. ClassDB::bind_method(D_METHOD("is_column_clipping_content", "column"), &Tree::is_column_clipping_content);
  5383. ClassDB::bind_method(D_METHOD("get_column_expand_ratio", "column"), &Tree::get_column_expand_ratio);
  5384. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  5385. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  5386. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  5387. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::get_next_selected);
  5388. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  5389. ClassDB::bind_method(D_METHOD("set_selected", "item", "column"), &Tree::set_selected);
  5390. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  5391. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  5392. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  5393. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  5394. ClassDB::bind_method(D_METHOD("deselect_all"), &Tree::deselect_all);
  5395. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  5396. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  5397. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  5398. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  5399. ClassDB::bind_method(D_METHOD("edit_selected", "force_edit"), &Tree::edit_selected, DEFVAL(false));
  5400. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  5401. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column", "button_index"), &Tree::get_item_rect, DEFVAL(-1), DEFVAL(-1));
  5402. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  5403. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  5404. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  5405. ClassDB::bind_method(D_METHOD("get_button_id_at_position", "position"), &Tree::get_button_id_at_position);
  5406. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  5407. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  5408. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  5409. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  5410. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  5411. ClassDB::bind_method(D_METHOD("set_column_title_alignment", "column", "title_alignment"), &Tree::set_column_title_alignment);
  5412. ClassDB::bind_method(D_METHOD("get_column_title_alignment", "column"), &Tree::get_column_title_alignment);
  5413. ClassDB::bind_method(D_METHOD("set_column_title_direction", "column", "direction"), &Tree::set_column_title_direction);
  5414. ClassDB::bind_method(D_METHOD("get_column_title_direction", "column"), &Tree::get_column_title_direction);
  5415. ClassDB::bind_method(D_METHOD("set_column_title_language", "column", "language"), &Tree::set_column_title_language);
  5416. ClassDB::bind_method(D_METHOD("get_column_title_language", "column"), &Tree::get_column_title_language);
  5417. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  5418. ClassDB::bind_method(D_METHOD("scroll_to_item", "item", "center_on_item"), &Tree::scroll_to_item, DEFVAL(false));
  5419. ClassDB::bind_method(D_METHOD("set_h_scroll_enabled", "h_scroll"), &Tree::set_h_scroll_enabled);
  5420. ClassDB::bind_method(D_METHOD("is_h_scroll_enabled"), &Tree::is_h_scroll_enabled);
  5421. ClassDB::bind_method(D_METHOD("set_v_scroll_enabled", "h_scroll"), &Tree::set_v_scroll_enabled);
  5422. ClassDB::bind_method(D_METHOD("is_v_scroll_enabled"), &Tree::is_v_scroll_enabled);
  5423. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  5424. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  5425. ClassDB::bind_method(D_METHOD("set_enable_recursive_folding", "enable"), &Tree::set_enable_recursive_folding);
  5426. ClassDB::bind_method(D_METHOD("is_recursive_folding_enabled"), &Tree::is_recursive_folding_enabled);
  5427. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  5428. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  5429. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  5430. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  5431. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  5432. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  5433. ClassDB::bind_method(D_METHOD("set_allow_search", "allow"), &Tree::set_allow_search);
  5434. ClassDB::bind_method(D_METHOD("get_allow_search"), &Tree::get_allow_search);
  5435. ClassDB::bind_method(D_METHOD("set_auto_tooltip", "enable"), &Tree::set_auto_tooltip);
  5436. ClassDB::bind_method(D_METHOD("is_auto_tooltip_enabled"), &Tree::is_auto_tooltip_enabled);
  5437. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  5438. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "column_titles_visible"), "set_column_titles_visible", "are_column_titles_visible");
  5439. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  5440. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  5441. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_search"), "set_allow_search", "get_allow_search");
  5442. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  5443. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enable_recursive_folding"), "set_enable_recursive_folding", "is_recursive_folding_enabled");
  5444. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  5445. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In Between"), "set_drop_mode_flags", "get_drop_mode_flags");
  5446. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  5447. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_horizontal_enabled"), "set_h_scroll_enabled", "is_h_scroll_enabled");
  5448. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_vertical_enabled"), "set_v_scroll_enabled", "is_v_scroll_enabled");
  5449. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_tooltip"), "set_auto_tooltip", "is_auto_tooltip_enabled");
  5450. ADD_SIGNAL(MethodInfo("item_selected"));
  5451. ADD_SIGNAL(MethodInfo("cell_selected"));
  5452. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  5453. ADD_SIGNAL(MethodInfo("item_mouse_selected", PropertyInfo(Variant::VECTOR2, "mouse_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  5454. ADD_SIGNAL(MethodInfo("empty_clicked", PropertyInfo(Variant::VECTOR2, "click_position"), PropertyInfo(Variant::INT, "mouse_button_index")));
  5455. ADD_SIGNAL(MethodInfo("item_edited"));
  5456. ADD_SIGNAL(MethodInfo("custom_item_clicked", PropertyInfo(Variant::INT, "mouse_button_index")));
  5457. ADD_SIGNAL(MethodInfo("item_icon_double_clicked"));
  5458. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  5459. ADD_SIGNAL(MethodInfo("check_propagated_to_item", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column")));
  5460. 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")));
  5461. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  5462. ADD_SIGNAL(MethodInfo("item_activated"));
  5463. ADD_SIGNAL(MethodInfo("column_title_clicked", PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "mouse_button_index")));
  5464. ADD_SIGNAL(MethodInfo("nothing_selected"));
  5465. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  5466. BIND_ENUM_CONSTANT(SELECT_ROW);
  5467. BIND_ENUM_CONSTANT(SELECT_MULTI);
  5468. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  5469. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  5470. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  5471. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, panel_style, "panel");
  5472. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, focus_style, "focus");
  5473. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Tree, font);
  5474. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Tree, font_size);
  5475. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, Tree, tb_font, "title_button_font");
  5476. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, Tree, tb_font_size, "title_button_font_size");
  5477. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered);
  5478. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_dimmed);
  5479. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_selected);
  5480. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, hovered_selected_focus);
  5481. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected);
  5482. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, selected_focus);
  5483. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor);
  5484. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, cursor_unfocus, "cursor_unfocused");
  5485. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, button_hover);
  5486. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, button_pressed);
  5487. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked);
  5488. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked);
  5489. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, checked_disabled);
  5490. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, unchecked_disabled);
  5491. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate);
  5492. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, indeterminate_disabled);
  5493. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow);
  5494. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed);
  5495. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, arrow_collapsed_mirrored);
  5496. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, select_arrow);
  5497. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Tree, updown);
  5498. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button);
  5499. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_hover);
  5500. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, custom_button_pressed);
  5501. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, custom_button_font_highlight);
  5502. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_color);
  5503. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_color);
  5504. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_dimmed_color);
  5505. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_hovered_selected_color);
  5506. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_selected_color);
  5507. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_disabled_color);
  5508. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, drop_position_color);
  5509. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, h_separation);
  5510. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, v_separation);
  5511. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_bottom);
  5512. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_left);
  5513. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_right);
  5514. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, inner_item_margin_top);
  5515. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, item_margin);
  5516. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, button_margin);
  5517. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, icon_max_width);
  5518. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, font_outline_color);
  5519. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Tree, font_outline_size, "outline_size");
  5520. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_guides);
  5521. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, guide_color);
  5522. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, draw_relationship_lines);
  5523. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, relationship_line_width);
  5524. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_width);
  5525. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, children_hl_line_width);
  5526. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, parent_hl_line_margin);
  5527. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, relationship_line_color);
  5528. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, parent_hl_line_color);
  5529. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, children_hl_line_color);
  5530. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_border);
  5531. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scroll_speed);
  5532. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_top);
  5533. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_right);
  5534. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_bottom);
  5535. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_margin_left);
  5536. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_h_separation);
  5537. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Tree, scrollbar_v_separation);
  5538. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button, "title_button_normal");
  5539. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_pressed);
  5540. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Tree, title_button_hover);
  5541. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Tree, title_button_color);
  5542. ADD_CLASS_DEPENDENCY("HScrollBar");
  5543. ADD_CLASS_DEPENDENCY("HSlider");
  5544. ADD_CLASS_DEPENDENCY("LineEdit");
  5545. ADD_CLASS_DEPENDENCY("Popup");
  5546. ADD_CLASS_DEPENDENCY("TextEdit");
  5547. ADD_CLASS_DEPENDENCY("Timer");
  5548. ADD_CLASS_DEPENDENCY("VScrollBar");
  5549. }
  5550. Tree::Tree() {
  5551. columns.resize(1);
  5552. set_focus_mode(FOCUS_ALL);
  5553. popup_editor = memnew(Popup);
  5554. add_child(popup_editor, false, INTERNAL_MODE_FRONT);
  5555. popup_editor_vb = memnew(VBoxContainer);
  5556. popup_editor_vb->add_theme_constant_override("separation", 0);
  5557. popup_editor_vb->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  5558. popup_editor->add_child(popup_editor_vb);
  5559. line_editor = memnew(LineEdit);
  5560. line_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  5561. line_editor->hide();
  5562. popup_editor_vb->add_child(line_editor);
  5563. text_editor = memnew(TextEdit);
  5564. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  5565. text_editor->hide();
  5566. popup_editor_vb->add_child(text_editor);
  5567. h_scroll = memnew(HScrollBar);
  5568. v_scroll = memnew(VScrollBar);
  5569. add_child(h_scroll, false, INTERNAL_MODE_FRONT);
  5570. add_child(v_scroll, false, INTERNAL_MODE_FRONT);
  5571. range_click_timer = memnew(Timer);
  5572. range_click_timer->connect("timeout", callable_mp(this, &Tree::_range_click_timeout));
  5573. add_child(range_click_timer, false, INTERNAL_MODE_FRONT);
  5574. h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Tree::_scroll_moved));
  5575. v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Tree::_scroll_moved));
  5576. line_editor->connect(SceneStringName(text_submitted), callable_mp(this, &Tree::_line_editor_submit));
  5577. text_editor->connect(SceneStringName(gui_input), callable_mp(this, &Tree::_text_editor_gui_input));
  5578. popup_editor->connect("popup_hide", callable_mp(this, &Tree::_text_editor_popup_modal_close));
  5579. set_notify_transform(true);
  5580. set_mouse_filter(MOUSE_FILTER_STOP);
  5581. set_clip_contents(true);
  5582. }
  5583. Tree::~Tree() {
  5584. if (root) {
  5585. memdelete(root);
  5586. }
  5587. }