tree.cpp 152 KB

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