tree.cpp 187 KB

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