tree.cpp 125 KB

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