tree.cpp 216 KB

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