tree.cpp 158 KB

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