tree.cpp 215 KB

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