tree.cpp 228 KB

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