tree.cpp 149 KB

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