tree.cpp 187 KB

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