tree.cpp 215 KB

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