tree.cpp 213 KB

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