tree.cpp 166 KB

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