tree.cpp 173 KB

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