tree.cpp 217 KB

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