tree.cpp 188 KB

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