2
0

tree.cpp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898
  1. /*************************************************************************/
  2. /* tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 <limits.h>
  32. #include "math_funcs.h"
  33. #include "os/input.h"
  34. #include "os/keyboard.h"
  35. #include "os/os.h"
  36. #include "print_string.h"
  37. #include "project_settings.h"
  38. #include "scene/main/viewport.h"
  39. #ifdef TOOLS_ENABLED
  40. #include "editor/editor_node.h"
  41. #endif
  42. void TreeItem::move_to_top() {
  43. if (!parent || parent->childs == this)
  44. return; //already on top
  45. TreeItem *prev = get_prev();
  46. prev->next = next;
  47. next = parent->childs;
  48. parent->childs = this;
  49. }
  50. void TreeItem::move_to_bottom() {
  51. if (!parent || !next)
  52. return;
  53. TreeItem *prev = get_prev();
  54. TreeItem *last = next;
  55. while (last->next)
  56. last = last->next;
  57. if (prev) {
  58. prev->next = next;
  59. } else {
  60. parent->childs = next;
  61. }
  62. last->next = this;
  63. next = NULL;
  64. }
  65. Size2 TreeItem::Cell::get_icon_size() const {
  66. if (icon.is_null())
  67. return Size2();
  68. if (icon_region == Rect2i())
  69. return icon->get_size();
  70. else
  71. return icon_region.size;
  72. }
  73. void TreeItem::Cell::draw_icon(const RID &p_where, const Point2 &p_pos, const Size2 &p_size, const Color &p_color) const {
  74. if (icon.is_null())
  75. return;
  76. Size2i dsize = (p_size == Size2()) ? icon->get_size() : p_size;
  77. if (icon_region == Rect2i()) {
  78. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), Rect2(Point2(), icon->get_size()), p_color);
  79. } else {
  80. icon->draw_rect_region(p_where, Rect2(p_pos, dsize), icon_region, p_color);
  81. }
  82. }
  83. void TreeItem::_changed_notify(int p_cell) {
  84. tree->item_changed(p_cell, this);
  85. }
  86. void TreeItem::_changed_notify() {
  87. tree->item_changed(-1, this);
  88. }
  89. void TreeItem::_cell_selected(int p_cell) {
  90. tree->item_selected(p_cell, this);
  91. }
  92. void TreeItem::_cell_deselected(int p_cell) {
  93. tree->item_deselected(p_cell, this);
  94. }
  95. /* cell mode */
  96. void TreeItem::set_cell_mode(int p_column, TreeCellMode p_mode) {
  97. ERR_FAIL_INDEX(p_column, cells.size());
  98. Cell &c = cells[p_column];
  99. c.mode = p_mode;
  100. c.min = 0;
  101. c.max = 100;
  102. c.step = 1;
  103. c.val = 0;
  104. c.checked = false;
  105. c.icon = Ref<Texture>();
  106. c.text = "";
  107. c.icon_max_w = 0;
  108. _changed_notify(p_column);
  109. }
  110. TreeItem::TreeCellMode TreeItem::get_cell_mode(int p_column) const {
  111. ERR_FAIL_INDEX_V(p_column, cells.size(), TreeItem::CELL_MODE_STRING);
  112. return cells[p_column].mode;
  113. }
  114. /* check mode */
  115. void TreeItem::set_checked(int p_column, bool p_checked) {
  116. ERR_FAIL_INDEX(p_column, cells.size());
  117. cells[p_column].checked = p_checked;
  118. _changed_notify(p_column);
  119. }
  120. bool TreeItem::is_checked(int p_column) const {
  121. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  122. return cells[p_column].checked;
  123. }
  124. void TreeItem::set_text(int p_column, String p_text) {
  125. ERR_FAIL_INDEX(p_column, cells.size());
  126. cells[p_column].text = p_text;
  127. if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE || cells[p_column].mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) {
  128. Vector<String> strings = p_text.split(",");
  129. cells[p_column].min = INT_MAX;
  130. cells[p_column].max = INT_MIN;
  131. for (int i = 0; i < strings.size(); i++) {
  132. int value = i;
  133. if (!strings[i].get_slicec(':', 1).empty()) {
  134. value = strings[i].get_slicec(':', 1).to_int();
  135. }
  136. cells[p_column].min = MIN(cells[p_column].min, value);
  137. cells[p_column].max = MAX(cells[p_column].max, value);
  138. }
  139. cells[p_column].step = 0;
  140. }
  141. _changed_notify(p_column);
  142. }
  143. String TreeItem::get_text(int p_column) const {
  144. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  145. return cells[p_column].text;
  146. }
  147. void TreeItem::set_suffix(int p_column, String p_suffix) {
  148. ERR_FAIL_INDEX(p_column, cells.size());
  149. cells[p_column].suffix = p_suffix;
  150. _changed_notify(p_column);
  151. }
  152. String TreeItem::get_suffix(int p_column) const {
  153. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  154. return cells[p_column].suffix;
  155. }
  156. void TreeItem::set_icon(int p_column, const Ref<Texture> &p_icon) {
  157. ERR_FAIL_INDEX(p_column, cells.size());
  158. cells[p_column].icon = p_icon;
  159. _changed_notify(p_column);
  160. }
  161. Ref<Texture> TreeItem::get_icon(int p_column) const {
  162. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture>());
  163. return cells[p_column].icon;
  164. }
  165. void TreeItem::set_icon_region(int p_column, const Rect2 &p_icon_region) {
  166. ERR_FAIL_INDEX(p_column, cells.size());
  167. cells[p_column].icon_region = p_icon_region;
  168. _changed_notify(p_column);
  169. }
  170. Rect2 TreeItem::get_icon_region(int p_column) const {
  171. ERR_FAIL_INDEX_V(p_column, cells.size(), Rect2());
  172. return cells[p_column].icon_region;
  173. }
  174. void TreeItem::set_icon_color(int p_column, const Color &p_icon_color) {
  175. ERR_FAIL_INDEX(p_column, cells.size());
  176. cells[p_column].icon_color = p_icon_color;
  177. _changed_notify(p_column);
  178. }
  179. Color TreeItem::get_icon_color(int p_column) const {
  180. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  181. return cells[p_column].icon_color;
  182. }
  183. void TreeItem::set_icon_max_width(int p_column, int p_max) {
  184. ERR_FAIL_INDEX(p_column, cells.size());
  185. cells[p_column].icon_max_w = p_max;
  186. _changed_notify(p_column);
  187. }
  188. int TreeItem::get_icon_max_width(int p_column) const {
  189. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  190. return cells[p_column].icon_max_w;
  191. }
  192. /* range works for mode number or mode combo */
  193. void TreeItem::set_range(int p_column, double p_value) {
  194. ERR_FAIL_INDEX(p_column, cells.size());
  195. if (cells[p_column].step > 0)
  196. p_value = Math::stepify(p_value, cells[p_column].step);
  197. if (p_value < cells[p_column].min)
  198. p_value = cells[p_column].min;
  199. if (p_value > cells[p_column].max)
  200. p_value = cells[p_column].max;
  201. cells[p_column].val = p_value;
  202. _changed_notify(p_column);
  203. }
  204. double TreeItem::get_range(int p_column) const {
  205. ERR_FAIL_INDEX_V(p_column, cells.size(), 0);
  206. return cells[p_column].val;
  207. }
  208. bool TreeItem::is_range_exponential(int p_column) const {
  209. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  210. return cells[p_column].expr;
  211. }
  212. void TreeItem::set_range_config(int p_column, double p_min, double p_max, double p_step, bool p_exp) {
  213. ERR_FAIL_INDEX(p_column, cells.size());
  214. cells[p_column].min = p_min;
  215. cells[p_column].max = p_max;
  216. cells[p_column].step = p_step;
  217. cells[p_column].expr = p_exp;
  218. _changed_notify(p_column);
  219. }
  220. void TreeItem::get_range_config(int p_column, double &r_min, double &r_max, double &r_step) const {
  221. ERR_FAIL_INDEX(p_column, cells.size());
  222. r_min = cells[p_column].min;
  223. r_max = cells[p_column].max;
  224. r_step = cells[p_column].step;
  225. }
  226. void TreeItem::set_metadata(int p_column, const Variant &p_meta) {
  227. ERR_FAIL_INDEX(p_column, cells.size());
  228. cells[p_column].meta = p_meta;
  229. }
  230. Variant TreeItem::get_metadata(int p_column) const {
  231. ERR_FAIL_INDEX_V(p_column, cells.size(), Variant());
  232. return cells[p_column].meta;
  233. }
  234. void TreeItem::set_custom_draw(int p_column, Object *p_object, const StringName &p_callback) {
  235. ERR_FAIL_INDEX(p_column, cells.size());
  236. ERR_FAIL_NULL(p_object);
  237. cells[p_column].custom_draw_obj = p_object->get_instance_id();
  238. cells[p_column].custom_draw_callback = p_callback;
  239. }
  240. void TreeItem::set_collapsed(bool p_collapsed) {
  241. if (collapsed == p_collapsed)
  242. return;
  243. collapsed = p_collapsed;
  244. TreeItem *ci = tree->selected_item;
  245. if (ci) {
  246. while (ci && ci != this) {
  247. ci = ci->parent;
  248. }
  249. if (ci) { // collapsing cursor/selectd, move it!
  250. if (tree->select_mode == Tree::SELECT_MULTI) {
  251. tree->selected_item = this;
  252. emit_signal("cell_selected");
  253. } else {
  254. select(tree->selected_col);
  255. }
  256. tree->update();
  257. }
  258. }
  259. _changed_notify();
  260. if (tree)
  261. tree->emit_signal("item_collapsed", this);
  262. }
  263. bool TreeItem::is_collapsed() {
  264. return collapsed;
  265. }
  266. void TreeItem::set_custom_minimum_height(int p_height) {
  267. custom_min_height = p_height;
  268. _changed_notify();
  269. }
  270. int TreeItem::get_custom_minimum_height() const {
  271. return custom_min_height;
  272. }
  273. TreeItem *TreeItem::get_next() {
  274. return next;
  275. }
  276. TreeItem *TreeItem::get_prev() {
  277. if (!parent || parent->childs == this)
  278. return NULL;
  279. TreeItem *prev = parent->childs;
  280. while (prev && prev->next != this)
  281. prev = prev->next;
  282. return prev;
  283. }
  284. TreeItem *TreeItem::get_parent() {
  285. return parent;
  286. }
  287. TreeItem *TreeItem::get_children() {
  288. return childs;
  289. }
  290. TreeItem *TreeItem::get_prev_visible() {
  291. TreeItem *current = this;
  292. TreeItem *prev = current->get_prev();
  293. if (!prev) {
  294. current = current->parent;
  295. if (!current || (current == tree->root && tree->hide_root))
  296. return NULL;
  297. } else {
  298. current = prev;
  299. while (!current->collapsed && current->childs) {
  300. //go to the very end
  301. current = current->childs;
  302. while (current->next)
  303. current = current->next;
  304. }
  305. }
  306. return current;
  307. }
  308. TreeItem *TreeItem::get_next_visible() {
  309. TreeItem *current = this;
  310. if (!current->collapsed && current->childs) {
  311. current = current->childs;
  312. } else if (current->next) {
  313. current = current->next;
  314. } else {
  315. while (current && !current->next) {
  316. current = current->parent;
  317. }
  318. if (current == NULL)
  319. return NULL;
  320. else
  321. current = current->next;
  322. }
  323. return current;
  324. }
  325. void TreeItem::remove_child(TreeItem *p_item) {
  326. ERR_FAIL_NULL(p_item);
  327. TreeItem **c = &childs;
  328. while (*c) {
  329. if ((*c) == p_item) {
  330. TreeItem *aux = *c;
  331. *c = (*c)->next;
  332. aux->parent = NULL;
  333. return;
  334. }
  335. c = &(*c)->next;
  336. }
  337. ERR_FAIL();
  338. }
  339. void TreeItem::set_selectable(int p_column, bool p_selectable) {
  340. ERR_FAIL_INDEX(p_column, cells.size());
  341. cells[p_column].selectable = p_selectable;
  342. }
  343. bool TreeItem::is_selectable(int p_column) const {
  344. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  345. return cells[p_column].selectable;
  346. }
  347. bool TreeItem::is_selected(int p_column) {
  348. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  349. return cells[p_column].selectable && cells[p_column].selected;
  350. }
  351. void TreeItem::set_as_cursor(int p_column) {
  352. ERR_FAIL_INDEX(p_column, cells.size());
  353. if (!tree)
  354. return;
  355. if (tree->select_mode != Tree::SELECT_MULTI)
  356. return;
  357. tree->selected_item = this;
  358. tree->selected_col = p_column;
  359. tree->update();
  360. }
  361. void TreeItem::select(int p_column) {
  362. ERR_FAIL_INDEX(p_column, cells.size());
  363. _cell_selected(p_column);
  364. }
  365. void TreeItem::deselect(int p_column) {
  366. ERR_FAIL_INDEX(p_column, cells.size());
  367. _cell_deselected(p_column);
  368. }
  369. void TreeItem::add_button(int p_column, const Ref<Texture> &p_button, int p_id, bool p_disabled, const String &p_tooltip) {
  370. ERR_FAIL_INDEX(p_column, cells.size());
  371. ERR_FAIL_COND(!p_button.is_valid());
  372. TreeItem::Cell::Button button;
  373. button.texture = p_button;
  374. if (p_id < 0)
  375. p_id = cells[p_column].buttons.size();
  376. button.id = p_id;
  377. button.disabled = p_disabled;
  378. button.tooltip = p_tooltip;
  379. cells[p_column].buttons.push_back(button);
  380. _changed_notify(p_column);
  381. }
  382. int TreeItem::get_button_count(int p_column) const {
  383. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  384. return cells[p_column].buttons.size();
  385. }
  386. Ref<Texture> TreeItem::get_button(int p_column, int p_idx) const {
  387. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture>());
  388. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), Ref<Texture>());
  389. return cells[p_column].buttons[p_idx].texture;
  390. }
  391. int TreeItem::get_button_id(int p_column, int p_idx) const {
  392. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  393. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), -1);
  394. return cells[p_column].buttons[p_idx].id;
  395. }
  396. void TreeItem::erase_button(int p_column, int p_idx) {
  397. ERR_FAIL_INDEX(p_column, cells.size());
  398. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  399. cells[p_column].buttons.remove(p_idx);
  400. _changed_notify(p_column);
  401. }
  402. int TreeItem::get_button_by_id(int p_column, int p_id) const {
  403. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  404. for (int i = 0; i < cells[p_column].buttons.size(); i++) {
  405. if (cells[p_column].buttons[i].id == p_id)
  406. return i;
  407. }
  408. return -1;
  409. }
  410. bool TreeItem::is_button_disabled(int p_column, int p_idx) const {
  411. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  412. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), false);
  413. return cells[p_column].buttons[p_idx].disabled;
  414. }
  415. void TreeItem::set_button(int p_column, int p_idx, const Ref<Texture> &p_button) {
  416. ERR_FAIL_COND(p_button.is_null());
  417. ERR_FAIL_INDEX(p_column, cells.size());
  418. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  419. cells[p_column].buttons[p_idx].texture = p_button;
  420. _changed_notify(p_column);
  421. }
  422. void TreeItem::set_button_color(int p_column, int p_idx, const Color &p_color) {
  423. ERR_FAIL_INDEX(p_column, cells.size());
  424. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  425. cells[p_column].buttons[p_idx].color = p_color;
  426. _changed_notify(p_column);
  427. }
  428. void TreeItem::set_editable(int p_column, bool p_editable) {
  429. ERR_FAIL_INDEX(p_column, cells.size());
  430. cells[p_column].editable = p_editable;
  431. _changed_notify(p_column);
  432. }
  433. bool TreeItem::is_editable(int p_column) {
  434. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  435. return cells[p_column].editable;
  436. }
  437. void TreeItem::set_custom_color(int p_column, const Color &p_color) {
  438. ERR_FAIL_INDEX(p_column, cells.size());
  439. cells[p_column].custom_color = true;
  440. cells[p_column].color = p_color;
  441. _changed_notify(p_column);
  442. }
  443. Color TreeItem::get_custom_color(int p_column) const {
  444. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  445. if (!cells[p_column].custom_color)
  446. return Color();
  447. return cells[p_column].color;
  448. }
  449. void TreeItem::clear_custom_color(int p_column) {
  450. ERR_FAIL_INDEX(p_column, cells.size());
  451. cells[p_column].custom_color = false;
  452. cells[p_column].color = Color();
  453. _changed_notify(p_column);
  454. }
  455. void TreeItem::set_tooltip(int p_column, const String &p_tooltip) {
  456. ERR_FAIL_INDEX(p_column, cells.size());
  457. cells[p_column].tooltip = p_tooltip;
  458. }
  459. String TreeItem::get_tooltip(int p_column) const {
  460. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  461. return cells[p_column].tooltip;
  462. }
  463. void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
  464. ERR_FAIL_INDEX(p_column, cells.size());
  465. cells[p_column].custom_bg_color = true;
  466. cells[p_column].custom_bg_outline = p_bg_outline;
  467. cells[p_column].bg_color = p_color;
  468. _changed_notify(p_column);
  469. }
  470. void TreeItem::clear_custom_bg_color(int p_column) {
  471. ERR_FAIL_INDEX(p_column, cells.size());
  472. cells[p_column].custom_bg_color = false;
  473. cells[p_column].bg_color = Color();
  474. _changed_notify(p_column);
  475. }
  476. Color TreeItem::get_custom_bg_color(int p_column) const {
  477. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  478. if (!cells[p_column].custom_bg_color)
  479. return Color();
  480. return cells[p_column].bg_color;
  481. }
  482. void TreeItem::set_custom_as_button(int p_column, bool p_button) {
  483. ERR_FAIL_INDEX(p_column, cells.size());
  484. cells[p_column].custom_button = p_button;
  485. }
  486. bool TreeItem::is_custom_set_as_button(int p_column) const {
  487. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  488. return cells[p_column].custom_button;
  489. }
  490. void TreeItem::set_text_align(int p_column, TextAlign p_align) {
  491. ERR_FAIL_INDEX(p_column, cells.size());
  492. cells[p_column].text_align = p_align;
  493. _changed_notify(p_column);
  494. }
  495. TreeItem::TextAlign TreeItem::get_text_align(int p_column) const {
  496. ERR_FAIL_INDEX_V(p_column, cells.size(), ALIGN_LEFT);
  497. return cells[p_column].text_align;
  498. }
  499. void TreeItem::set_expand_right(int p_column, bool p_enable) {
  500. ERR_FAIL_INDEX(p_column, cells.size());
  501. cells[p_column].expand_right = p_enable;
  502. _changed_notify(p_column);
  503. }
  504. bool TreeItem::get_expand_right(int p_column) const {
  505. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  506. return cells[p_column].expand_right;
  507. }
  508. void TreeItem::set_disable_folding(bool p_disable) {
  509. disable_folding = p_disable;
  510. _changed_notify(0);
  511. }
  512. bool TreeItem::is_folding_disabled() const {
  513. return disable_folding;
  514. }
  515. void TreeItem::_bind_methods() {
  516. ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
  517. ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
  518. ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
  519. ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
  520. ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
  521. ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
  522. ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
  523. ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
  524. ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
  525. ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
  526. ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
  527. ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
  528. ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
  529. ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
  530. ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
  531. ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
  532. ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
  533. ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
  534. ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
  535. ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
  536. ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
  537. ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
  538. ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
  539. ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
  540. ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
  541. ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
  542. ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
  543. ClassDB::bind_method(D_METHOD("get_next_visible"), &TreeItem::get_next_visible);
  544. ClassDB::bind_method(D_METHOD("get_prev_visible"), &TreeItem::get_prev_visible);
  545. ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::_remove_child);
  546. ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
  547. ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
  548. ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
  549. ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
  550. ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
  551. ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
  552. ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
  553. ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
  554. ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
  555. ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
  556. ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
  557. ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
  558. ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
  559. ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
  560. ClassDB::bind_method(D_METHOD("add_button", "column", "button", "button_idx", "disabled", "tooltip"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""));
  561. ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
  562. ClassDB::bind_method(D_METHOD("get_button", "column", "button_idx"), &TreeItem::get_button);
  563. ClassDB::bind_method(D_METHOD("set_button", "column", "button_idx", "button"), &TreeItem::set_button);
  564. ClassDB::bind_method(D_METHOD("erase_button", "column", "button_idx"), &TreeItem::erase_button);
  565. ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_idx"), &TreeItem::is_button_disabled);
  566. ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
  567. ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
  568. ClassDB::bind_method(D_METHOD("set_tooltip", "column", "tooltip"), &TreeItem::set_tooltip);
  569. ClassDB::bind_method(D_METHOD("get_tooltip", "column"), &TreeItem::get_tooltip);
  570. ClassDB::bind_method(D_METHOD("set_text_align", "column", "text_align"), &TreeItem::set_text_align);
  571. ClassDB::bind_method(D_METHOD("get_text_align", "column"), &TreeItem::get_text_align);
  572. ClassDB::bind_method(D_METHOD("move_to_top"), &TreeItem::move_to_top);
  573. ClassDB::bind_method(D_METHOD("move_to_bottom"), &TreeItem::move_to_bottom);
  574. ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
  575. ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
  576. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  577. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  578. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  579. BIND_ENUM_CONSTANT(CELL_MODE_RANGE_EXPRESSION);
  580. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  581. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  582. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  583. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  584. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  585. }
  586. void TreeItem::clear_children() {
  587. TreeItem *c = childs;
  588. while (c) {
  589. TreeItem *aux = c;
  590. c = c->get_next();
  591. aux->parent = 0; // so it wont try to recursively autoremove from me in here
  592. memdelete(aux);
  593. }
  594. childs = 0;
  595. };
  596. TreeItem::TreeItem(Tree *p_tree) {
  597. tree = p_tree;
  598. collapsed = false;
  599. disable_folding = false;
  600. custom_min_height = 0;
  601. parent = 0; // parent item
  602. next = 0; // next in list
  603. childs = 0; //child items
  604. }
  605. TreeItem::~TreeItem() {
  606. clear_children();
  607. if (parent)
  608. parent->remove_child(this);
  609. if (tree && tree->root == this) {
  610. tree->root = 0;
  611. }
  612. if (tree && tree->popup_edited_item == this) {
  613. tree->popup_edited_item = NULL;
  614. tree->pressing_for_editor = false;
  615. }
  616. if (tree && tree->cache.hover_item == this) {
  617. tree->cache.hover_item = NULL;
  618. }
  619. if (tree && tree->selected_item == this)
  620. tree->selected_item = NULL;
  621. if (tree && tree->drop_mode_over == this)
  622. tree->drop_mode_over = NULL;
  623. if (tree && tree->single_select_defer == this)
  624. tree->single_select_defer = NULL;
  625. if (tree && tree->edited_item == this) {
  626. tree->edited_item = NULL;
  627. tree->pressing_for_editor = false;
  628. }
  629. }
  630. /**********************************************/
  631. /**********************************************/
  632. /**********************************************/
  633. /**********************************************/
  634. /**********************************************/
  635. /**********************************************/
  636. void Tree::update_cache() {
  637. cache.font = get_font("font");
  638. cache.tb_font = get_font("title_button_font");
  639. cache.bg = get_stylebox("bg");
  640. cache.selected = get_stylebox("selected");
  641. cache.selected_focus = get_stylebox("selected_focus");
  642. cache.cursor = get_stylebox("cursor");
  643. cache.cursor_unfocus = get_stylebox("cursor_unfocused");
  644. cache.button_pressed = get_stylebox("button_pressed");
  645. cache.checked = get_icon("checked");
  646. cache.unchecked = get_icon("unchecked");
  647. cache.arrow_collapsed = get_icon("arrow_collapsed");
  648. cache.arrow = get_icon("arrow");
  649. cache.select_arrow = get_icon("select_arrow");
  650. cache.select_option = get_icon("select_option");
  651. cache.updown = get_icon("updown");
  652. cache.custom_button = get_stylebox("custom_button");
  653. cache.custom_button_hover = get_stylebox("custom_button_hover");
  654. cache.custom_button_pressed = get_stylebox("custom_button_pressed");
  655. cache.custom_button_font_highlight = get_color("custom_button_font_highlight");
  656. cache.font_color = get_color("font_color");
  657. cache.font_color_selected = get_color("font_color_selected");
  658. cache.guide_color = get_color("guide_color");
  659. cache.drop_position_color = get_color("drop_position_color");
  660. cache.hseparation = get_constant("hseparation");
  661. cache.vseparation = get_constant("vseparation");
  662. cache.item_margin = get_constant("item_margin");
  663. cache.button_margin = get_constant("button_margin");
  664. cache.guide_width = get_constant("guide_width");
  665. cache.draw_relationship_lines = get_constant("draw_relationship_lines");
  666. cache.relationship_line_color = get_color("relationship_line_color");
  667. cache.scroll_border = get_constant("scroll_border");
  668. cache.scroll_speed = get_constant("scroll_speed");
  669. cache.title_button = get_stylebox("title_button_normal");
  670. cache.title_button_pressed = get_stylebox("title_button_pressed");
  671. cache.title_button_hover = get_stylebox("title_button_hover");
  672. cache.title_button_color = get_color("title_button_color");
  673. v_scroll->set_custom_step(cache.font->get_height());
  674. }
  675. int Tree::compute_item_height(TreeItem *p_item) const {
  676. if (p_item == root && hide_root)
  677. return 0;
  678. int height = cache.font->get_height();
  679. for (int i = 0; i < columns.size(); i++) {
  680. for (int j = 0; j < p_item->cells[i].buttons.size(); j++) {
  681. Size2i s; // = cache.button_pressed->get_minimum_size();
  682. s += p_item->cells[i].buttons[j].texture->get_size();
  683. if (s.height > height)
  684. height = s.height;
  685. }
  686. switch (p_item->cells[i].mode) {
  687. case TreeItem::CELL_MODE_CHECK: {
  688. int check_icon_h = cache.checked->get_height();
  689. if (height < check_icon_h)
  690. height = check_icon_h;
  691. }
  692. case TreeItem::CELL_MODE_STRING:
  693. case TreeItem::CELL_MODE_CUSTOM:
  694. case TreeItem::CELL_MODE_ICON: {
  695. Ref<Texture> icon = p_item->cells[i].icon;
  696. if (!icon.is_null()) {
  697. Size2i s = p_item->cells[i].get_icon_size();
  698. if (p_item->cells[i].icon_max_w > 0 && s.width > p_item->cells[i].icon_max_w) {
  699. s.height = s.height * p_item->cells[i].icon_max_w / s.width;
  700. }
  701. if (s.height > height)
  702. height = s.height;
  703. }
  704. if (p_item->cells[i].mode == TreeItem::CELL_MODE_CUSTOM && p_item->cells[i].custom_button) {
  705. height += cache.custom_button->get_minimum_size().height;
  706. }
  707. } break;
  708. default: {}
  709. }
  710. }
  711. int item_min_height = p_item->get_custom_minimum_height();
  712. if (height < item_min_height)
  713. height = item_min_height;
  714. height += cache.vseparation;
  715. return height;
  716. }
  717. int Tree::get_item_height(TreeItem *p_item) const {
  718. int height = compute_item_height(p_item);
  719. height += cache.vseparation;
  720. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  721. TreeItem *c = p_item->childs;
  722. while (c) {
  723. height += get_item_height(c);
  724. c = c->next;
  725. }
  726. }
  727. return height;
  728. }
  729. void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color) {
  730. Rect2i rect = p_rect;
  731. Ref<Font> font = cache.font;
  732. String text = p_cell.text;
  733. if (p_cell.suffix != String())
  734. text += " " + p_cell.suffix;
  735. int w = 0;
  736. if (!p_cell.icon.is_null()) {
  737. Size2i bmsize = p_cell.get_icon_size();
  738. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  739. bmsize.width = p_cell.icon_max_w;
  740. }
  741. w += bmsize.width + cache.hseparation;
  742. }
  743. w += font->get_string_size(text).width;
  744. switch (p_cell.text_align) {
  745. case TreeItem::ALIGN_LEFT:
  746. break; //do none
  747. case TreeItem::ALIGN_CENTER:
  748. rect.position.x = MAX(0, (rect.size.width - w) / 2);
  749. break; //do none
  750. case TreeItem::ALIGN_RIGHT:
  751. rect.position.x = MAX(0, (rect.size.width - w));
  752. break; //do none
  753. }
  754. RID ci = get_canvas_item();
  755. if (!p_cell.icon.is_null()) {
  756. Size2i bmsize = p_cell.get_icon_size();
  757. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  758. bmsize.height = bmsize.height * p_cell.icon_max_w / bmsize.width;
  759. bmsize.width = p_cell.icon_max_w;
  760. }
  761. p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize, p_icon_color);
  762. rect.position.x += bmsize.x + cache.hseparation;
  763. rect.size.x -= bmsize.x + cache.hseparation;
  764. }
  765. rect.position.y += Math::floor((rect.size.y - font->get_height()) / 2.0) + font->get_ascent();
  766. font->draw(ci, rect.position, text, p_color, rect.size.x);
  767. }
  768. int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item) {
  769. if (p_pos.y - cache.offset.y > (p_draw_size.height))
  770. return -1; //draw no more!
  771. RID ci = get_canvas_item();
  772. int htotal = 0;
  773. int label_h = compute_item_height(p_item);
  774. /* Calculate height of the label part */
  775. label_h += cache.vseparation;
  776. /* Draw label, if height fits */
  777. bool skip = (p_item == root && hide_root);
  778. if (!skip && (p_pos.y + label_h - cache.offset.y) > 0) {
  779. //draw separation.
  780. //if (p_item->get_parent()!=root || !hide_root)
  781. Ref<Font> font = cache.font;
  782. int font_ascent = font->get_ascent();
  783. int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  784. int skip = 0;
  785. for (int i = 0; i < columns.size(); i++) {
  786. if (skip) {
  787. skip--;
  788. continue;
  789. }
  790. int w = get_column_width(i);
  791. if (i == 0) {
  792. w -= ofs;
  793. if (w <= 0) {
  794. ofs = get_column_width(0);
  795. continue;
  796. }
  797. } else {
  798. ofs += cache.hseparation;
  799. w -= cache.hseparation;
  800. }
  801. if (p_item->cells[i].expand_right) {
  802. int plus = 1;
  803. 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()) {
  804. w += get_column_width(i + plus);
  805. plus++;
  806. skip++;
  807. }
  808. }
  809. int bw = 0;
  810. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  811. Ref<Texture> b = p_item->cells[i].buttons[j].texture;
  812. Size2 s = b->get_size() + cache.button_pressed->get_minimum_size();
  813. Point2i o = Point2i(ofs + w - s.width, p_pos.y) - cache.offset + p_draw_ofs;
  814. 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) {
  815. //being pressed
  816. cache.button_pressed->draw(get_canvas_item(), Rect2(o, s));
  817. }
  818. o.y += (label_h - s.height) / 2;
  819. o += cache.button_pressed->get_offset();
  820. b->draw(ci, o, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
  821. w -= s.width + cache.button_margin;
  822. bw += s.width + cache.button_margin;
  823. }
  824. Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - cache.offset + p_draw_ofs, Size2i(w, label_h));
  825. Rect2i cell_rect = item_rect;
  826. if (i != 0) {
  827. cell_rect.position.x -= cache.hseparation;
  828. cell_rect.size.x += cache.hseparation;
  829. }
  830. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(cell_rect.position.x, cell_rect.position.y + cell_rect.size.height), cell_rect.position + cell_rect.size, cache.guide_color, 1);
  831. if (i == 0) {
  832. if (p_item->cells[0].selected && select_mode == SELECT_ROW) {
  833. Rect2i row_rect = Rect2i(Point2i(cache.bg->get_margin(MARGIN_LEFT), item_rect.position.y), Size2i(get_size().width - cache.bg->get_minimum_size().width, item_rect.size.y));
  834. //Rect2 r = Rect2i(row_rect.pos,row_rect.size);
  835. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  836. if (has_focus())
  837. cache.selected_focus->draw(ci, row_rect);
  838. else
  839. cache.selected->draw(ci, row_rect);
  840. }
  841. }
  842. if (p_item->cells[i].selected && select_mode != SELECT_ROW) {
  843. Rect2i r(cell_rect.position, cell_rect.size);
  844. if (p_item->cells[i].text.size() > 0) {
  845. float icon_width = p_item->cells[i].get_icon_size().width;
  846. r.position.x += icon_width;
  847. r.size.x -= icon_width;
  848. }
  849. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  850. if (has_focus()) {
  851. cache.selected_focus->draw(ci, r);
  852. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  853. } else {
  854. cache.selected->draw(ci, r);
  855. }
  856. if (text_editor->is_visible_in_tree()) {
  857. Vector2 ofs(0, (text_editor->get_size().height - r.size.height) / 2);
  858. text_editor->set_position(get_global_position() + r.position - ofs);
  859. }
  860. }
  861. if (p_item->cells[i].custom_bg_color) {
  862. Rect2 r = cell_rect;
  863. if (i == 0) {
  864. r.position.x = p_draw_ofs.x;
  865. r.size.x = w + ofs;
  866. } else {
  867. r.position.x -= cache.hseparation;
  868. r.size.x += cache.hseparation;
  869. }
  870. if (p_item->cells[i].custom_bg_outline) {
  871. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), p_item->cells[i].bg_color);
  872. VisualServer::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);
  873. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), p_item->cells[i].bg_color);
  874. VisualServer::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);
  875. } else {
  876. VisualServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  877. }
  878. }
  879. if (drop_mode_flags && drop_mode_over == p_item) {
  880. Rect2 r = cell_rect;
  881. if (drop_mode_section == -1 || drop_mode_section == 0) {
  882. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), cache.drop_position_color);
  883. }
  884. if (drop_mode_section == 0) {
  885. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), cache.drop_position_color);
  886. VisualServer::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);
  887. }
  888. if (drop_mode_section == 1 || drop_mode_section == 0) {
  889. VisualServer::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);
  890. }
  891. }
  892. Color col = p_item->cells[i].custom_color ? p_item->cells[i].color : get_color(p_item->cells[i].selected ? "font_color_selected" : "font_color");
  893. Color icon_col = p_item->cells[i].icon_color;
  894. Point2i text_pos = item_rect.position;
  895. text_pos.y += Math::floor((item_rect.size.y - font->get_height()) / 2) + font_ascent;
  896. switch (p_item->cells[i].mode) {
  897. case TreeItem::CELL_MODE_STRING: {
  898. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  899. } break;
  900. case TreeItem::CELL_MODE_CHECK: {
  901. Ref<Texture> checked = cache.checked;
  902. Ref<Texture> unchecked = cache.unchecked;
  903. Point2i check_ofs = item_rect.position;
  904. check_ofs.y += Math::floor((real_t)(item_rect.size.y - checked->get_height()) / 2);
  905. if (p_item->cells[i].checked) {
  906. checked->draw(ci, check_ofs, icon_col);
  907. } else {
  908. unchecked->draw(ci, check_ofs, icon_col);
  909. }
  910. int check_w = checked->get_width() + cache.hseparation;
  911. text_pos.x += check_w;
  912. item_rect.size.x -= check_w;
  913. item_rect.position.x += check_w;
  914. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  915. //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w );
  916. } break;
  917. case TreeItem::CELL_MODE_RANGE:
  918. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  919. if (p_item->cells[i].text != "") {
  920. if (!p_item->cells[i].editable)
  921. break;
  922. int option = (int)p_item->cells[i].val;
  923. String s = RTR("(Other)");
  924. Vector<String> strings = p_item->cells[i].text.split(",");
  925. for (int i = 0; i < strings.size(); i++) {
  926. int value = i;
  927. if (!strings[i].get_slicec(':', 1).empty()) {
  928. value = strings[i].get_slicec(':', 1).to_int();
  929. }
  930. if (option == value) {
  931. s = strings[i].get_slicec(':', 0);
  932. break;
  933. }
  934. }
  935. if (p_item->cells[i].suffix != String())
  936. s += " " + p_item->cells[i].suffix;
  937. Ref<Texture> downarrow = cache.select_arrow;
  938. font->draw(ci, text_pos, s, col, item_rect.size.x - downarrow->get_width());
  939. //?
  940. Point2i arrow_pos = item_rect.position;
  941. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  942. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  943. downarrow->draw(ci, arrow_pos, icon_col);
  944. } else {
  945. Ref<Texture> updown = cache.updown;
  946. String valtext = String::num(p_item->cells[i].val, Math::step_decimals(p_item->cells[i].step));
  947. //String valtext = rtos( p_item->cells[i].val );
  948. if (p_item->cells[i].suffix != String())
  949. valtext += " " + p_item->cells[i].suffix;
  950. font->draw(ci, text_pos, valtext, col, item_rect.size.x - updown->get_width());
  951. if (!p_item->cells[i].editable)
  952. break;
  953. Point2i updown_pos = item_rect.position;
  954. updown_pos.x += item_rect.size.x - updown->get_width();
  955. updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
  956. updown->draw(ci, updown_pos, icon_col);
  957. }
  958. } break;
  959. case TreeItem::CELL_MODE_ICON: {
  960. if (p_item->cells[i].icon.is_null())
  961. break;
  962. Size2i icon_size = p_item->cells[i].get_icon_size();
  963. if (p_item->cells[i].icon_max_w > 0 && icon_size.width > p_item->cells[i].icon_max_w) {
  964. icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width;
  965. icon_size.width = p_item->cells[i].icon_max_w;
  966. }
  967. Point2i icon_ofs = (item_rect.size - icon_size) / 2;
  968. icon_ofs += item_rect.position;
  969. draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size), false, icon_col);
  970. //p_item->cells[i].icon->draw(ci, icon_ofs);
  971. } break;
  972. case TreeItem::CELL_MODE_CUSTOM: {
  973. //int option = (int)p_item->cells[i].val;
  974. if (p_item->cells[i].custom_draw_obj) {
  975. Object *cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
  976. if (cdo)
  977. cdo->call(p_item->cells[i].custom_draw_callback, p_item, Rect2(item_rect));
  978. }
  979. if (!p_item->cells[i].editable) {
  980. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  981. break;
  982. }
  983. Ref<Texture> downarrow = cache.select_arrow;
  984. Rect2i ir = item_rect;
  985. Point2i arrow_pos = item_rect.position;
  986. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  987. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  988. ir.size.width -= downarrow->get_width();
  989. if (p_item->cells[i].custom_button) {
  990. if (cache.hover_item == p_item && cache.hover_cell == i) {
  991. if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  992. draw_style_box(cache.custom_button_pressed, ir);
  993. } else {
  994. draw_style_box(cache.custom_button_hover, ir);
  995. col = cache.custom_button_font_highlight;
  996. }
  997. } else {
  998. draw_style_box(cache.custom_button, ir);
  999. }
  1000. ir.size -= cache.custom_button->get_minimum_size();
  1001. ir.position += cache.custom_button->get_offset();
  1002. }
  1003. draw_item_rect(p_item->cells[i], ir, col, icon_col);
  1004. downarrow->draw(ci, arrow_pos);
  1005. } break;
  1006. }
  1007. if (i == 0) {
  1008. ofs = get_column_width(0);
  1009. } else {
  1010. ofs += w + bw;
  1011. }
  1012. if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
  1013. if (has_focus())
  1014. cache.cursor->draw(ci, cell_rect);
  1015. else
  1016. cache.cursor_unfocus->draw(ci, cell_rect);
  1017. }
  1018. }
  1019. if (!p_item->disable_folding && !hide_folding && p_item->childs) { //has childs, draw the guide box
  1020. Ref<Texture> arrow;
  1021. if (p_item->collapsed) {
  1022. arrow = cache.arrow_collapsed;
  1023. } else {
  1024. arrow = cache.arrow;
  1025. }
  1026. arrow->draw(ci, p_pos + p_draw_ofs + Point2i(0, (label_h - arrow->get_height()) / 2) - cache.offset);
  1027. }
  1028. //separator
  1029. //get_painter()->draw_fill_rect( Point2i(0,pos.y),Size2i(get_size().width,1),color( COLOR_TREE_GRID) );
  1030. //pos=p_pos; //reset pos
  1031. }
  1032. Point2 children_pos = p_pos;
  1033. if (!skip) {
  1034. children_pos.x += cache.item_margin;
  1035. htotal += label_h;
  1036. children_pos.y += htotal;
  1037. }
  1038. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  1039. TreeItem *c = p_item->childs;
  1040. while (c) {
  1041. if (cache.draw_relationship_lines == 1) {
  1042. int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1043. int parent_ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1044. Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h / 2) - cache.offset + p_draw_ofs;
  1045. if (c->get_children() != NULL)
  1046. root_pos -= Point2i(cache.arrow->get_width(), 0);
  1047. float line_width = 1.0;
  1048. #ifdef TOOLS_ENABLED
  1049. line_width *= EDSCALE;
  1050. #endif
  1051. 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;
  1052. VisualServer::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);
  1053. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), parent_pos, cache.relationship_line_color, line_width);
  1054. }
  1055. int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c);
  1056. if (child_h < 0 && cache.draw_relationship_lines == 0)
  1057. return -1; // break, stop drawing, no need to anymore
  1058. htotal += child_h;
  1059. children_pos.y += child_h;
  1060. c = c->next;
  1061. }
  1062. }
  1063. return htotal;
  1064. }
  1065. int Tree::_count_selected_items(TreeItem *p_from) const {
  1066. int count = 0;
  1067. for (int i = 0; i < columns.size(); i++) {
  1068. if (p_from->is_selected(i))
  1069. count++;
  1070. }
  1071. if (p_from->get_children()) {
  1072. count += _count_selected_items(p_from->get_children());
  1073. }
  1074. if (p_from->get_next()) {
  1075. count += _count_selected_items(p_from->get_next());
  1076. }
  1077. return count;
  1078. }
  1079. 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) {
  1080. TreeItem::Cell &selected_cell = p_selected->cells[p_col];
  1081. bool switched = false;
  1082. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1083. *r_in_range = true;
  1084. switched = true;
  1085. }
  1086. bool emitted_row = false;
  1087. for (int i = 0; i < columns.size(); i++) {
  1088. TreeItem::Cell &c = p_current->cells[i];
  1089. if (!c.selectable)
  1090. continue;
  1091. if (select_mode == SELECT_ROW) {
  1092. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  1093. c.selected = true;
  1094. selected_item = p_selected;
  1095. selected_col = 0;
  1096. if (!emitted_row) {
  1097. emit_signal("item_selected");
  1098. emitted_row = true;
  1099. }
  1100. /*
  1101. if (p_col==i)
  1102. p_current->selected_signal.call(p_col);
  1103. */
  1104. } else if (c.selected) {
  1105. c.selected = false;
  1106. //p_current->deselected_signal.call(p_col);
  1107. }
  1108. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  1109. if (!r_in_range && &selected_cell == &c) {
  1110. if (!selected_cell.selected || allow_reselect) {
  1111. selected_cell.selected = true;
  1112. selected_item = p_selected;
  1113. selected_col = i;
  1114. emit_signal("cell_selected");
  1115. if (select_mode == SELECT_MULTI)
  1116. emit_signal("multi_selected", p_current, i, true);
  1117. else if (select_mode == SELECT_SINGLE)
  1118. emit_signal("item_selected");
  1119. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  1120. selected_item = p_selected;
  1121. selected_col = i;
  1122. emit_signal("cell_selected");
  1123. }
  1124. } else {
  1125. if (r_in_range && *r_in_range && !p_force_deselect) {
  1126. if (!c.selected && c.selectable) {
  1127. c.selected = true;
  1128. emit_signal("multi_selected", p_current, i, true);
  1129. }
  1130. } else if (!r_in_range || p_force_deselect) {
  1131. if (select_mode == SELECT_MULTI && c.selected)
  1132. emit_signal("multi_selected", p_current, i, false);
  1133. c.selected = false;
  1134. }
  1135. //p_current->deselected_signal.call(p_col);
  1136. }
  1137. }
  1138. }
  1139. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1140. *r_in_range = false;
  1141. }
  1142. TreeItem *c = p_current->childs;
  1143. while (c) {
  1144. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  1145. c = c->next;
  1146. }
  1147. }
  1148. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  1149. return Rect2();
  1150. }
  1151. void Tree::_range_click_timeout() {
  1152. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  1153. Point2 pos = get_local_mouse_position() - cache.bg->get_offset();
  1154. if (show_column_titles) {
  1155. pos.y -= _get_title_button_height();
  1156. if (pos.y < 0) {
  1157. range_click_timer->stop();
  1158. return;
  1159. }
  1160. }
  1161. click_handled = false;
  1162. Ref<InputEventMouseButton> mb;
  1163. mb.instance();
  1164. ;
  1165. blocked++;
  1166. propagate_mouse_event(pos + cache.offset, 0, 0, false, root, BUTTON_LEFT, mb);
  1167. blocked--;
  1168. if (range_click_timer->is_one_shot()) {
  1169. range_click_timer->set_wait_time(0.05);
  1170. range_click_timer->set_one_shot(false);
  1171. range_click_timer->start();
  1172. }
  1173. if (!click_handled)
  1174. range_click_timer->stop();
  1175. } else {
  1176. range_click_timer->stop();
  1177. }
  1178. }
  1179. 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) {
  1180. int item_h = compute_item_height(p_item) + cache.vseparation;
  1181. bool skip = (p_item == root && hide_root);
  1182. if (!skip && p_pos.y < item_h) {
  1183. // check event!
  1184. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  1185. return -1;
  1186. }
  1187. if (!p_item->disable_folding && !hide_folding && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + cache.item_margin))) {
  1188. if (p_item->childs)
  1189. p_item->set_collapsed(!p_item->is_collapsed());
  1190. return -1; //handled!
  1191. }
  1192. int x = p_pos.x;
  1193. /* find clicked column */
  1194. int col = -1;
  1195. int col_ofs = 0;
  1196. int col_width = 0;
  1197. for (int i = 0; i < columns.size(); i++) {
  1198. col_width = get_column_width(i);
  1199. if (p_item->cells[i].expand_right) {
  1200. int plus = 1;
  1201. 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()) {
  1202. col_width += cache.hseparation;
  1203. col_width += get_column_width(i + plus);
  1204. plus++;
  1205. }
  1206. }
  1207. if (x > col_width) {
  1208. col_ofs += col_width;
  1209. x -= col_width;
  1210. continue;
  1211. }
  1212. col = i;
  1213. break;
  1214. }
  1215. if (col == -1)
  1216. return -1;
  1217. else if (col == 0) {
  1218. int margin = x_ofs + cache.item_margin; //-cache.hseparation;
  1219. //int lm = cache.bg->get_margin(MARGIN_LEFT);
  1220. col_width -= margin;
  1221. col_ofs += margin;
  1222. x -= margin;
  1223. } else {
  1224. col_width -= cache.hseparation;
  1225. x -= cache.hseparation;
  1226. }
  1227. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_children()) {
  1228. p_item->set_collapsed(!p_item->is_collapsed());
  1229. return -1; //collapse/uncollapse because nothing can be done with item
  1230. }
  1231. TreeItem::Cell &c = p_item->cells[col];
  1232. bool already_selected = c.selected;
  1233. bool already_cursor = (p_item == selected_item) && col == selected_col;
  1234. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  1235. Ref<Texture> b = c.buttons[j].texture;
  1236. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  1237. if (x > col_width - w) {
  1238. if (c.buttons[j].disabled) {
  1239. pressed_button = -1;
  1240. cache.click_type = Cache::CLICK_NONE;
  1241. return -1;
  1242. }
  1243. pressed_button = j;
  1244. cache.click_type = Cache::CLICK_BUTTON;
  1245. cache.click_index = j;
  1246. cache.click_id = c.buttons[j].id;
  1247. cache.click_item = p_item;
  1248. cache.click_column = col;
  1249. cache.click_pos = get_global_mouse_position() - get_global_position();
  1250. update();
  1251. //emit_signal("button_pressed");
  1252. return -1;
  1253. }
  1254. col_width -= w + cache.button_margin;
  1255. }
  1256. if (p_button == BUTTON_LEFT || (p_button == BUTTON_RIGHT && allow_rmb_select)) {
  1257. /* process selection */
  1258. 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
  1259. emit_signal("item_activated");
  1260. incr_search.clear();
  1261. return -1;
  1262. }
  1263. if (select_mode == SELECT_MULTI && p_mod->get_command() && c.selectable) {
  1264. if (!c.selected || p_button == BUTTON_RIGHT) {
  1265. p_item->select(col);
  1266. emit_signal("multi_selected", p_item, col, true);
  1267. if (p_button == BUTTON_RIGHT) {
  1268. emit_signal("item_rmb_selected", get_local_mouse_position());
  1269. }
  1270. //p_item->selected_signal.call(col);
  1271. } else {
  1272. p_item->deselect(col);
  1273. emit_signal("multi_selected", p_item, col, false);
  1274. //p_item->deselected_signal.call(col);
  1275. }
  1276. } else {
  1277. if (c.selectable) {
  1278. if (select_mode == SELECT_MULTI && p_mod->get_shift() && selected_item && selected_item != p_item) {
  1279. bool inrange = false;
  1280. select_single_item(p_item, root, col, selected_item, &inrange);
  1281. if (p_button == BUTTON_RIGHT) {
  1282. emit_signal("item_rmb_selected", get_local_mouse_position());
  1283. }
  1284. } else {
  1285. int icount = _count_selected_items(root);
  1286. if (select_mode == SELECT_MULTI && icount > 1 && p_button != BUTTON_RIGHT) {
  1287. single_select_defer = p_item;
  1288. single_select_defer_column = col;
  1289. } else {
  1290. if (p_button != BUTTON_RIGHT || !c.selected) {
  1291. select_single_item(p_item, root, col);
  1292. }
  1293. if (p_button == BUTTON_RIGHT) {
  1294. emit_signal("item_rmb_selected", get_local_mouse_position());
  1295. }
  1296. }
  1297. }
  1298. /*
  1299. if (!c.selected && select_mode==SELECT_MULTI) {
  1300. emit_signal("multi_selected",p_item,col,true);
  1301. }
  1302. */
  1303. update();
  1304. }
  1305. }
  1306. }
  1307. if (!c.editable)
  1308. return -1; // if cell is not editable, don't bother
  1309. /* editing */
  1310. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  1311. String editor_text = c.text;
  1312. switch (c.mode) {
  1313. case TreeItem::CELL_MODE_STRING: {
  1314. //nothing in particular
  1315. if (select_mode == SELECT_MULTI && (get_tree()->get_last_event_id() == focus_in_id || !already_cursor)) {
  1316. bring_up_editor = false;
  1317. }
  1318. } break;
  1319. case TreeItem::CELL_MODE_CHECK: {
  1320. bring_up_editor = false; //checkboxes are not edited with editor
  1321. if (force_edit_checkbox_only_on_checkbox) {
  1322. if (x < cache.checked->get_width()) {
  1323. p_item->set_checked(col, !c.checked);
  1324. item_edited(col, p_item);
  1325. }
  1326. } else {
  1327. p_item->set_checked(col, !c.checked);
  1328. item_edited(col, p_item);
  1329. }
  1330. click_handled = true;
  1331. //p_item->edited_signal.call(col);
  1332. } break;
  1333. case TreeItem::CELL_MODE_RANGE:
  1334. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1335. if (c.text != "") {
  1336. //if (x >= (get_column_width(col)-item_h/2)) {
  1337. popup_menu->clear();
  1338. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  1339. String s = c.text.get_slicec(',', i);
  1340. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).empty() ? i : s.get_slicec(':', 1).to_int());
  1341. }
  1342. popup_menu->set_size(Size2(col_width, 0));
  1343. popup_menu->set_position(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - cache.offset);
  1344. popup_menu->popup();
  1345. popup_edited_item = p_item;
  1346. popup_edited_item_col = col;
  1347. //}
  1348. bring_up_editor = false;
  1349. } else {
  1350. if (x >= (col_width - item_h / 2)) {
  1351. /* touching the combo */
  1352. bool up = p_pos.y < (item_h / 2);
  1353. if (p_button == BUTTON_LEFT) {
  1354. if (range_click_timer->get_time_left() == 0) {
  1355. range_item_last = p_item;
  1356. range_up_last = up;
  1357. range_click_timer->set_wait_time(0.6);
  1358. range_click_timer->set_one_shot(true);
  1359. range_click_timer->start();
  1360. } else if (up != range_up_last) {
  1361. return -1; // break. avoid changing direction on mouse held
  1362. }
  1363. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  1364. item_edited(col, p_item);
  1365. } else if (p_button == BUTTON_RIGHT) {
  1366. p_item->set_range(col, (up ? c.max : c.min));
  1367. item_edited(col, p_item);
  1368. } else if (p_button == BUTTON_WHEEL_UP) {
  1369. p_item->set_range(col, c.val + c.step);
  1370. item_edited(col, p_item);
  1371. } else if (p_button == BUTTON_WHEEL_DOWN) {
  1372. p_item->set_range(col, c.val - c.step);
  1373. item_edited(col, p_item);
  1374. }
  1375. //p_item->edited_signal.call(col);
  1376. bring_up_editor = false;
  1377. } else {
  1378. editor_text = String::num(p_item->cells[col].val, Math::step_decimals(p_item->cells[col].step));
  1379. if (select_mode == SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id)
  1380. bring_up_editor = false;
  1381. }
  1382. }
  1383. click_handled = true;
  1384. } break;
  1385. case TreeItem::CELL_MODE_ICON: {
  1386. bring_up_editor = false;
  1387. } break;
  1388. case TreeItem::CELL_MODE_CUSTOM: {
  1389. edited_item = p_item;
  1390. edited_col = col;
  1391. bool on_arrow = x > col_width - cache.select_arrow->get_width();
  1392. bring_up_editor = false;
  1393. 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));
  1394. if (on_arrow || !p_item->cells[col].custom_button) {
  1395. emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2))));
  1396. }
  1397. if (!p_item->cells[col].custom_button || !on_arrow) {
  1398. item_edited(col, p_item, p_button == BUTTON_LEFT);
  1399. }
  1400. click_handled = true;
  1401. return -1;
  1402. } break;
  1403. };
  1404. if (!bring_up_editor || p_button != BUTTON_LEFT)
  1405. return -1;
  1406. click_handled = true;
  1407. popup_edited_item = p_item;
  1408. popup_edited_item_col = col;
  1409. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - cache.offset, Size2(col_width, item_h));
  1410. pressing_for_editor_text = editor_text;
  1411. pressing_for_editor = true;
  1412. return -1; //select
  1413. } else {
  1414. Point2i new_pos = p_pos;
  1415. if (!skip) {
  1416. x_ofs += cache.item_margin;
  1417. //new_pos.x-=cache.item_margin;
  1418. y_ofs += item_h;
  1419. new_pos.y -= item_h;
  1420. }
  1421. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  1422. TreeItem *c = p_item->childs;
  1423. while (c) {
  1424. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, p_doubleclick, c, p_button, p_mod);
  1425. if (child_h < 0)
  1426. return -1; // break, stop propagating, no need to anymore
  1427. new_pos.y -= child_h;
  1428. y_ofs += child_h;
  1429. c = c->next;
  1430. item_h += child_h;
  1431. }
  1432. }
  1433. }
  1434. return item_h; // nothing found
  1435. }
  1436. void Tree::_text_editor_modal_close() {
  1437. if (Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  1438. Input::get_singleton()->is_key_pressed(KEY_KP_ENTER) ||
  1439. Input::get_singleton()->is_key_pressed(KEY_ENTER)) {
  1440. return;
  1441. }
  1442. if (value_editor->has_point(value_editor->get_local_mouse_position()))
  1443. return;
  1444. text_editor_enter(text_editor->get_text());
  1445. }
  1446. void Tree::text_editor_enter(String p_text) {
  1447. text_editor->hide();
  1448. value_editor->hide();
  1449. if (!popup_edited_item)
  1450. return;
  1451. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1452. return;
  1453. TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  1454. switch (c.mode) {
  1455. case TreeItem::CELL_MODE_STRING: {
  1456. c.text = p_text;
  1457. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1458. } break;
  1459. case TreeItem::CELL_MODE_RANGE: {
  1460. c.val = p_text.to_double();
  1461. if (c.step > 0)
  1462. c.val = Math::stepify(c.val, c.step);
  1463. if (c.val < c.min)
  1464. c.val = c.min;
  1465. else if (c.val > c.max)
  1466. c.val = c.max;
  1467. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1468. } break;
  1469. case TreeItem::CELL_MODE_RANGE_EXPRESSION: {
  1470. if (evaluator)
  1471. c.val = evaluator->eval(p_text);
  1472. else
  1473. c.val = p_text.to_double();
  1474. if (c.step > 0)
  1475. c.val = Math::stepify(c.val, c.step);
  1476. if (c.val < c.min)
  1477. c.val = c.min;
  1478. else if (c.val > c.max)
  1479. c.val = c.max;
  1480. } break;
  1481. default: { ERR_FAIL(); }
  1482. }
  1483. item_edited(popup_edited_item_col, popup_edited_item);
  1484. update();
  1485. }
  1486. void Tree::value_editor_changed(double p_value) {
  1487. if (updating_value_editor) {
  1488. return;
  1489. }
  1490. if (!popup_edited_item) {
  1491. return;
  1492. }
  1493. TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  1494. c.val = p_value;
  1495. item_edited(popup_edited_item_col, popup_edited_item);
  1496. update();
  1497. }
  1498. void Tree::popup_select(int p_option) {
  1499. if (!popup_edited_item)
  1500. return;
  1501. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1502. return;
  1503. popup_edited_item->cells[popup_edited_item_col].val = p_option;
  1504. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1505. update();
  1506. item_edited(popup_edited_item_col, popup_edited_item);
  1507. }
  1508. void Tree::_gui_input(Ref<InputEvent> p_event) {
  1509. Ref<InputEventKey> k = p_event;
  1510. if (k.is_valid()) {
  1511. if (!k->is_pressed())
  1512. return;
  1513. if (k->get_command() || (k->get_shift() && k->get_unicode() == 0) || k->get_metakey())
  1514. return;
  1515. if (!root)
  1516. return;
  1517. if (hide_root && !root->get_next_visible())
  1518. return;
  1519. switch (k->get_scancode()) {
  1520. #define EXIT_BREAK \
  1521. { \
  1522. if (!cursor_can_exit_tree) accept_event(); \
  1523. break; \
  1524. }
  1525. case KEY_RIGHT: {
  1526. bool dobreak = true;
  1527. //TreeItem *next = NULL;
  1528. if (!selected_item)
  1529. break;
  1530. if (select_mode == SELECT_ROW) {
  1531. EXIT_BREAK;
  1532. }
  1533. if (selected_col > (columns.size() - 1)) {
  1534. EXIT_BREAK;
  1535. }
  1536. if (k->get_alt()) {
  1537. selected_item->set_collapsed(false);
  1538. TreeItem *next = selected_item->get_children();
  1539. while (next && next != selected_item->next) {
  1540. next->set_collapsed(false);
  1541. next = next->get_next_visible();
  1542. }
  1543. } else if (selected_col == (columns.size() - 1)) {
  1544. if (selected_item->get_children() != NULL && selected_item->is_collapsed()) {
  1545. selected_item->set_collapsed(false);
  1546. } else {
  1547. selected_col = 0;
  1548. dobreak = false; // fall through to key_down
  1549. }
  1550. } else {
  1551. if (select_mode == SELECT_MULTI) {
  1552. selected_col++;
  1553. emit_signal("cell_selected");
  1554. } else {
  1555. selected_item->select(selected_col + 1);
  1556. }
  1557. }
  1558. update();
  1559. ensure_cursor_is_visible();
  1560. accept_event();
  1561. if (dobreak) {
  1562. break;
  1563. }
  1564. }
  1565. case KEY_DOWN: {
  1566. TreeItem *next = NULL;
  1567. if (!selected_item) {
  1568. next = hide_root ? root->get_next_visible() : root;
  1569. selected_item = 0;
  1570. } else {
  1571. next = selected_item->get_next_visible();
  1572. //if (diff < uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000))) {
  1573. if (last_keypress != 0) {
  1574. //incr search next
  1575. int col;
  1576. next = _search_item_text(next, incr_search, &col, true);
  1577. if (!next) {
  1578. accept_event();
  1579. return;
  1580. }
  1581. }
  1582. }
  1583. if (select_mode == SELECT_MULTI) {
  1584. if (!next)
  1585. EXIT_BREAK;
  1586. selected_item = next;
  1587. emit_signal("cell_selected");
  1588. update();
  1589. } else {
  1590. int col = selected_col < 0 ? 0 : selected_col;
  1591. while (next && !next->cells[col].selectable)
  1592. next = next->get_next_visible();
  1593. if (!next)
  1594. EXIT_BREAK; // do nothing..
  1595. next->select(col);
  1596. }
  1597. ensure_cursor_is_visible();
  1598. accept_event();
  1599. } break;
  1600. case KEY_LEFT: {
  1601. bool dobreak = true;
  1602. //TreeItem *next = NULL;
  1603. if (!selected_item)
  1604. break;
  1605. if (select_mode == SELECT_ROW) {
  1606. EXIT_BREAK;
  1607. }
  1608. if (selected_col < 0) {
  1609. EXIT_BREAK;
  1610. }
  1611. if (k->get_alt()) {
  1612. selected_item->set_collapsed(true);
  1613. TreeItem *next = selected_item->get_children();
  1614. while (next && next != selected_item->next) {
  1615. next->set_collapsed(true);
  1616. next = next->get_next_visible();
  1617. }
  1618. } else if (selected_col == 0) {
  1619. if (selected_item->get_children() != NULL && !selected_item->is_collapsed()) {
  1620. selected_item->set_collapsed(true);
  1621. } else {
  1622. if (columns.size() == 1) { // goto parent with one column
  1623. TreeItem *parent = selected_item->get_parent();
  1624. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  1625. select_single_item(parent, get_root(), selected_col);
  1626. }
  1627. } else {
  1628. selected_col = columns.size() - 1;
  1629. dobreak = false; // fall through to key_up
  1630. }
  1631. }
  1632. } else {
  1633. if (select_mode == SELECT_MULTI) {
  1634. selected_col--;
  1635. emit_signal("cell_selected");
  1636. } else {
  1637. selected_item->select(selected_col - 1);
  1638. }
  1639. }
  1640. update();
  1641. accept_event();
  1642. ensure_cursor_is_visible();
  1643. if (dobreak) {
  1644. break;
  1645. }
  1646. }
  1647. case KEY_UP: {
  1648. TreeItem *prev = NULL;
  1649. if (!selected_item) {
  1650. prev = get_last_item();
  1651. selected_col = 0;
  1652. } else {
  1653. prev = selected_item->get_prev_visible();
  1654. if (last_keypress != 0) {
  1655. //incr search next
  1656. int col;
  1657. prev = _search_item_text(prev, incr_search, &col, true, true);
  1658. if (!prev) {
  1659. accept_event();
  1660. return;
  1661. }
  1662. }
  1663. }
  1664. if (select_mode == SELECT_MULTI) {
  1665. if (!prev)
  1666. break;
  1667. selected_item = prev;
  1668. emit_signal("cell_selected");
  1669. update();
  1670. } else {
  1671. int col = selected_col < 0 ? 0 : selected_col;
  1672. while (prev && !prev->cells[col].selectable)
  1673. prev = prev->get_prev_visible();
  1674. if (!prev)
  1675. break; // do nothing..
  1676. prev->select(col);
  1677. }
  1678. ensure_cursor_is_visible();
  1679. accept_event();
  1680. } break;
  1681. case KEY_PAGEDOWN: {
  1682. TreeItem *next = NULL;
  1683. if (!selected_item)
  1684. break;
  1685. next = selected_item;
  1686. for (int i = 0; i < 10; i++) {
  1687. TreeItem *_n = next->get_next_visible();
  1688. if (_n) {
  1689. next = _n;
  1690. } else {
  1691. break;
  1692. }
  1693. }
  1694. if (next == selected_item)
  1695. break;
  1696. if (select_mode == SELECT_MULTI) {
  1697. selected_item = next;
  1698. emit_signal("cell_selected");
  1699. update();
  1700. } else {
  1701. while (next && !next->cells[selected_col].selectable)
  1702. next = next->get_next_visible();
  1703. if (!next)
  1704. EXIT_BREAK; // do nothing..
  1705. next->select(selected_col);
  1706. }
  1707. ensure_cursor_is_visible();
  1708. } break;
  1709. case KEY_PAGEUP: {
  1710. TreeItem *prev = NULL;
  1711. if (!selected_item)
  1712. break;
  1713. prev = selected_item;
  1714. for (int i = 0; i < 10; i++) {
  1715. TreeItem *_n = prev->get_prev_visible();
  1716. if (_n) {
  1717. prev = _n;
  1718. } else {
  1719. break;
  1720. }
  1721. }
  1722. if (prev == selected_item)
  1723. break;
  1724. if (select_mode == SELECT_MULTI) {
  1725. selected_item = prev;
  1726. emit_signal("cell_selected");
  1727. update();
  1728. } else {
  1729. while (prev && !prev->cells[selected_col].selectable)
  1730. prev = prev->get_prev_visible();
  1731. if (!prev)
  1732. EXIT_BREAK; // do nothing..
  1733. prev->select(selected_col);
  1734. }
  1735. ensure_cursor_is_visible();
  1736. } break;
  1737. case KEY_F2:
  1738. case KEY_ENTER:
  1739. case KEY_KP_ENTER: {
  1740. if (selected_item) {
  1741. //bring up editor if possible
  1742. if (!edit_selected()) {
  1743. emit_signal("item_activated");
  1744. incr_search.clear();
  1745. }
  1746. }
  1747. accept_event();
  1748. } break;
  1749. case KEY_SPACE: {
  1750. if (select_mode == SELECT_MULTI) {
  1751. if (!selected_item)
  1752. break;
  1753. if (selected_item->is_selected(selected_col)) {
  1754. selected_item->deselect(selected_col);
  1755. emit_signal("multi_selected", selected_item, selected_col, false);
  1756. } else if (selected_item->is_selectable(selected_col)) {
  1757. selected_item->select(selected_col);
  1758. emit_signal("multi_selected", selected_item, selected_col, true);
  1759. }
  1760. }
  1761. accept_event();
  1762. } break;
  1763. default: {
  1764. if (k->get_unicode() > 0) {
  1765. _do_incr_search(String::chr(k->get_unicode()));
  1766. accept_event();
  1767. return;
  1768. } else {
  1769. if (k->get_scancode() != KEY_SHIFT)
  1770. last_keypress = 0;
  1771. }
  1772. } break;
  1773. last_keypress = 0;
  1774. }
  1775. }
  1776. Ref<InputEventMouseMotion> mm = p_event;
  1777. if (mm.is_valid()) {
  1778. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1779. update_cache();
  1780. Ref<StyleBox> bg = cache.bg;
  1781. Point2 pos = mm->get_position() - bg->get_offset();
  1782. Cache::ClickType old_hover = cache.hover_type;
  1783. int old_index = cache.hover_index;
  1784. cache.hover_type = Cache::CLICK_NONE;
  1785. cache.hover_index = 0;
  1786. if (show_column_titles) {
  1787. pos.y -= _get_title_button_height();
  1788. if (pos.y < 0) {
  1789. pos.x += cache.offset.x;
  1790. int len = 0;
  1791. for (int i = 0; i < columns.size(); i++) {
  1792. len += get_column_width(i);
  1793. if (pos.x < len) {
  1794. cache.hover_type = Cache::CLICK_TITLE;
  1795. cache.hover_index = i;
  1796. update();
  1797. break;
  1798. }
  1799. }
  1800. }
  1801. }
  1802. if (root) {
  1803. Point2 mpos = mm->get_position();
  1804. mpos -= cache.bg->get_offset();
  1805. mpos.y -= _get_title_button_height();
  1806. if (mpos.y >= 0) {
  1807. if (h_scroll->is_visible_in_tree())
  1808. mpos.x += h_scroll->get_value();
  1809. if (v_scroll->is_visible_in_tree())
  1810. mpos.y += v_scroll->get_value();
  1811. int col, h, section;
  1812. TreeItem *it = _find_item_at_pos(root, mpos, col, h, section);
  1813. if ((drop_mode_flags && it != drop_mode_over) || section != drop_mode_section) {
  1814. drop_mode_over = it;
  1815. drop_mode_section = section;
  1816. update();
  1817. }
  1818. if (it != cache.hover_item || col != cache.hover_cell) {
  1819. cache.hover_item = it;
  1820. cache.hover_cell = col;
  1821. update();
  1822. }
  1823. }
  1824. }
  1825. if (cache.hover_type != old_hover || cache.hover_index != old_index) {
  1826. update();
  1827. }
  1828. if (pressing_for_editor && popup_edited_item && (popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE || popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE_EXPRESSION)) {
  1829. //range drag
  1830. if (!range_drag_enabled) {
  1831. Vector2 cpos = mm->get_position();
  1832. if (cpos.distance_to(pressing_pos) > 2) {
  1833. range_drag_enabled = true;
  1834. range_drag_capture_pos = cpos;
  1835. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  1836. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1837. }
  1838. } else {
  1839. TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  1840. float diff_y = -mm->get_relative().y;
  1841. diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y);
  1842. diff_y *= 0.1;
  1843. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  1844. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  1845. item_edited(popup_edited_item_col, popup_edited_item);
  1846. }
  1847. }
  1848. if (drag_touching && !drag_touching_deaccel) {
  1849. drag_accum -= mm->get_relative().y;
  1850. v_scroll->set_value(drag_from + drag_accum);
  1851. drag_speed = -mm->get_speed().y;
  1852. }
  1853. }
  1854. Ref<InputEventMouseButton> b = p_event;
  1855. if (b.is_valid()) {
  1856. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1857. update_cache();
  1858. if (!b->is_pressed()) {
  1859. if (b->get_button_index() == BUTTON_LEFT) {
  1860. Point2 pos = b->get_position() - cache.bg->get_offset();
  1861. if (show_column_titles) {
  1862. pos.y -= _get_title_button_height();
  1863. if (pos.y < 0) {
  1864. pos.x += cache.offset.x;
  1865. int len = 0;
  1866. for (int i = 0; i < columns.size(); i++) {
  1867. len += get_column_width(i);
  1868. if (pos.x < len) {
  1869. emit_signal("column_title_pressed", i);
  1870. break;
  1871. }
  1872. }
  1873. }
  1874. }
  1875. if (single_select_defer) {
  1876. select_single_item(single_select_defer, root, single_select_defer_column);
  1877. single_select_defer = NULL;
  1878. }
  1879. range_click_timer->stop();
  1880. if (pressing_for_editor) {
  1881. if (range_drag_enabled) {
  1882. range_drag_enabled = false;
  1883. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1884. warp_mouse(range_drag_capture_pos);
  1885. } else {
  1886. Rect2 rect = get_selected()->get_meta("__focus_rect");
  1887. if (rect.has_point(Point2(b->get_position().x, b->get_position().y))) {
  1888. edit_selected();
  1889. } else {
  1890. emit_signal("item_double_clicked");
  1891. }
  1892. }
  1893. pressing_for_editor = false;
  1894. }
  1895. if (cache.click_type == Cache::CLICK_BUTTON) {
  1896. // make sure in case of wrong reference after reconstructing whole TreeItems
  1897. cache.click_item = get_item_at_position(cache.click_pos);
  1898. emit_signal("button_pressed", cache.click_item, cache.click_column, cache.click_id);
  1899. }
  1900. cache.click_type = Cache::CLICK_NONE;
  1901. cache.click_index = -1;
  1902. cache.click_id = -1;
  1903. cache.click_item = NULL;
  1904. cache.click_column = 0;
  1905. if (drag_touching) {
  1906. if (drag_speed == 0) {
  1907. drag_touching_deaccel = false;
  1908. drag_touching = false;
  1909. set_physics_process(false);
  1910. } else {
  1911. drag_touching_deaccel = true;
  1912. }
  1913. }
  1914. update();
  1915. }
  1916. return;
  1917. }
  1918. if (range_drag_enabled)
  1919. return;
  1920. switch (b->get_button_index()) {
  1921. case BUTTON_RIGHT:
  1922. case BUTTON_LEFT: {
  1923. Ref<StyleBox> bg = cache.bg;
  1924. Point2 pos = b->get_position() - bg->get_offset();
  1925. cache.click_type = Cache::CLICK_NONE;
  1926. if (show_column_titles) {
  1927. pos.y -= _get_title_button_height();
  1928. if (pos.y < 0) {
  1929. if (b->get_button_index() == BUTTON_LEFT) {
  1930. pos.x += cache.offset.x;
  1931. int len = 0;
  1932. for (int i = 0; i < columns.size(); i++) {
  1933. len += get_column_width(i);
  1934. if (pos.x < len) {
  1935. cache.click_type = Cache::CLICK_TITLE;
  1936. cache.click_index = i;
  1937. //cache.click_id=;
  1938. update();
  1939. break;
  1940. }
  1941. }
  1942. }
  1943. break;
  1944. }
  1945. }
  1946. if (!root || (!root->get_children() && hide_root)) {
  1947. if (b->get_button_index() == BUTTON_RIGHT && allow_rmb_select) {
  1948. emit_signal("empty_tree_rmb_selected", get_local_mouse_position());
  1949. }
  1950. break;
  1951. }
  1952. click_handled = false;
  1953. pressing_for_editor = false;
  1954. blocked++;
  1955. propagate_mouse_event(pos + cache.offset, 0, 0, b->is_doubleclick(), root, b->get_button_index(), b);
  1956. blocked--;
  1957. if (pressing_for_editor) {
  1958. pressing_pos = b->get_position();
  1959. }
  1960. if (b->get_button_index() == BUTTON_RIGHT)
  1961. break;
  1962. if (drag_touching) {
  1963. set_physics_process(false);
  1964. drag_touching_deaccel = false;
  1965. drag_touching = false;
  1966. drag_speed = 0;
  1967. drag_from = 0;
  1968. }
  1969. if (!click_handled) {
  1970. drag_speed = 0;
  1971. drag_accum = 0;
  1972. //last_drag_accum=0;
  1973. drag_from = v_scroll->get_value();
  1974. drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  1975. drag_touching_deaccel = false;
  1976. if (drag_touching) {
  1977. set_physics_process(true);
  1978. }
  1979. if (b->get_button_index() == BUTTON_LEFT) {
  1980. if (get_item_at_position(b->get_position()) == NULL && !b->get_shift() && !b->get_control() && !b->get_command())
  1981. emit_signal("nothing_selected");
  1982. }
  1983. }
  1984. } break;
  1985. case BUTTON_WHEEL_UP: {
  1986. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  1987. } break;
  1988. case BUTTON_WHEEL_DOWN: {
  1989. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  1990. } break;
  1991. }
  1992. }
  1993. Ref<InputEventPanGesture> pan_gesture = p_event;
  1994. if (pan_gesture.is_valid()) {
  1995. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  1996. }
  1997. }
  1998. bool Tree::edit_selected() {
  1999. TreeItem *s = get_selected();
  2000. ERR_EXPLAIN("No item selected!");
  2001. ERR_FAIL_COND_V(!s, false);
  2002. ensure_cursor_is_visible();
  2003. int col = get_selected_column();
  2004. ERR_EXPLAIN("No item column selected!");
  2005. ERR_FAIL_INDEX_V(col, columns.size(), false);
  2006. if (!s->cells[col].editable)
  2007. return false;
  2008. Rect2 rect = s->get_meta("__focus_rect");
  2009. popup_edited_item = s;
  2010. popup_edited_item_col = col;
  2011. TreeItem::Cell &c = s->cells[col];
  2012. if (c.mode == TreeItem::CELL_MODE_CHECK) {
  2013. s->set_checked(col, !c.checked);
  2014. item_edited(col, s);
  2015. return true;
  2016. } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
  2017. edited_item = s;
  2018. edited_col = col;
  2019. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  2020. emit_signal("custom_popup_edited", false);
  2021. item_edited(col, s);
  2022. return true;
  2023. } else if ((c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) && c.text != "") {
  2024. popup_menu->clear();
  2025. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  2026. String s = c.text.get_slicec(',', i);
  2027. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).empty() ? i : s.get_slicec(':', 1).to_int());
  2028. }
  2029. popup_menu->set_size(Size2(rect.size.width, 0));
  2030. popup_menu->set_position(get_global_position() + rect.position + Point2i(0, rect.size.height));
  2031. popup_menu->popup();
  2032. popup_edited_item = s;
  2033. popup_edited_item_col = col;
  2034. return true;
  2035. } else if (c.mode == TreeItem::CELL_MODE_STRING || c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) {
  2036. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2037. Point2i textedpos = get_global_position() + rect.position - ofs;
  2038. text_editor->set_position(textedpos);
  2039. text_editor->set_size(rect.size);
  2040. text_editor->clear();
  2041. text_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::step_decimals(c.step)));
  2042. text_editor->select_all();
  2043. if (c.mode == TreeItem::CELL_MODE_RANGE || c.mode == TreeItem::CELL_MODE_RANGE_EXPRESSION) {
  2044. value_editor->set_position(textedpos + Point2i(0, text_editor->get_size().height));
  2045. value_editor->set_size(Size2(rect.size.width, 1));
  2046. value_editor->show_modal();
  2047. updating_value_editor = true;
  2048. value_editor->set_min(c.min);
  2049. value_editor->set_max(c.max);
  2050. value_editor->set_step(c.step);
  2051. value_editor->set_value(c.val);
  2052. value_editor->set_exp_ratio(c.expr);
  2053. updating_value_editor = false;
  2054. }
  2055. text_editor->show_modal();
  2056. text_editor->grab_focus();
  2057. return true;
  2058. }
  2059. return false;
  2060. }
  2061. Size2 Tree::get_internal_min_size() const {
  2062. Size2i size = cache.bg->get_offset();
  2063. if (root)
  2064. size.height += get_item_height(root);
  2065. for (int i = 0; i < columns.size(); i++) {
  2066. size.width += columns[i].min_width;
  2067. }
  2068. return size;
  2069. }
  2070. void Tree::update_scrollbars() {
  2071. Size2 size = get_size();
  2072. int tbh;
  2073. if (show_column_titles) {
  2074. tbh = _get_title_button_height();
  2075. } else {
  2076. tbh = 0;
  2077. }
  2078. Size2 hmin = h_scroll->get_combined_minimum_size();
  2079. Size2 vmin = v_scroll->get_combined_minimum_size();
  2080. v_scroll->set_begin(Point2(size.width - vmin.width, cache.bg->get_margin(MARGIN_TOP)));
  2081. v_scroll->set_end(Point2(size.width, size.height - cache.bg->get_margin(MARGIN_TOP) - cache.bg->get_margin(MARGIN_BOTTOM)));
  2082. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  2083. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  2084. Size2 min = get_internal_min_size();
  2085. if (min.height < size.height - hmin.height) {
  2086. v_scroll->hide();
  2087. cache.offset.y = 0;
  2088. } else {
  2089. v_scroll->show();
  2090. v_scroll->set_max(min.height);
  2091. v_scroll->set_page(size.height - hmin.height - tbh);
  2092. cache.offset.y = v_scroll->get_value();
  2093. }
  2094. if (min.width < size.width - vmin.width) {
  2095. h_scroll->hide();
  2096. cache.offset.x = 0;
  2097. } else {
  2098. h_scroll->show();
  2099. h_scroll->set_max(min.width);
  2100. h_scroll->set_page(size.width - vmin.width);
  2101. cache.offset.x = h_scroll->get_value();
  2102. }
  2103. }
  2104. int Tree::_get_title_button_height() const {
  2105. return show_column_titles ? cache.font->get_height() + cache.title_button->get_minimum_size().height : 0;
  2106. }
  2107. void Tree::_notification(int p_what) {
  2108. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  2109. focus_in_id = get_tree()->get_last_event_id();
  2110. }
  2111. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  2112. if (cache.hover_type != Cache::CLICK_NONE) {
  2113. cache.hover_type = Cache::CLICK_NONE;
  2114. update();
  2115. }
  2116. }
  2117. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2118. drag_touching = false;
  2119. }
  2120. if (p_what == NOTIFICATION_ENTER_TREE) {
  2121. update_cache();
  2122. }
  2123. if (p_what == NOTIFICATION_DRAG_END) {
  2124. drop_mode_flags = 0;
  2125. scrolling = false;
  2126. set_physics_process(false);
  2127. update();
  2128. }
  2129. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2130. single_select_defer = NULL;
  2131. if (cache.scroll_speed > 0 && get_rect().has_point(get_viewport()->get_mouse_position() - get_global_position())) {
  2132. scrolling = true;
  2133. set_physics_process(true);
  2134. }
  2135. }
  2136. if (p_what == NOTIFICATION_PHYSICS_PROCESS) {
  2137. if (drag_touching) {
  2138. if (drag_touching_deaccel) {
  2139. float pos = v_scroll->get_value();
  2140. pos += drag_speed * get_physics_process_delta_time();
  2141. bool turnoff = false;
  2142. if (pos < 0) {
  2143. pos = 0;
  2144. turnoff = true;
  2145. set_physics_process(false);
  2146. drag_touching = false;
  2147. drag_touching_deaccel = false;
  2148. }
  2149. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  2150. pos = v_scroll->get_max() - v_scroll->get_page();
  2151. turnoff = true;
  2152. }
  2153. v_scroll->set_value(pos);
  2154. float sgn = drag_speed < 0 ? -1 : 1;
  2155. float val = Math::abs(drag_speed);
  2156. val -= 1000 * get_physics_process_delta_time();
  2157. if (val < 0) {
  2158. turnoff = true;
  2159. }
  2160. drag_speed = sgn * val;
  2161. if (turnoff) {
  2162. set_physics_process(false);
  2163. drag_touching = false;
  2164. drag_touching_deaccel = false;
  2165. }
  2166. } else {
  2167. }
  2168. }
  2169. if (scrolling) {
  2170. Point2 point = get_viewport()->get_mouse_position() - get_global_position();
  2171. if (point.x < cache.scroll_border) {
  2172. point.x -= cache.scroll_border;
  2173. } else if (point.x > get_size().width - cache.scroll_border) {
  2174. point.x -= get_size().width - cache.scroll_border;
  2175. } else {
  2176. point.x = 0;
  2177. }
  2178. if (point.y < cache.scroll_border) {
  2179. point.y -= cache.scroll_border;
  2180. } else if (point.y > get_size().height - cache.scroll_border) {
  2181. point.y -= get_size().height - cache.scroll_border;
  2182. } else {
  2183. point.y = 0;
  2184. }
  2185. point *= cache.scroll_speed * get_physics_process_delta_time();
  2186. point += get_scroll();
  2187. h_scroll->set_value(point.x);
  2188. v_scroll->set_value(point.y);
  2189. }
  2190. }
  2191. if (p_what == NOTIFICATION_DRAW) {
  2192. update_cache();
  2193. update_scrollbars();
  2194. RID ci = get_canvas_item();
  2195. Ref<StyleBox> bg = cache.bg;
  2196. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  2197. Point2 draw_ofs;
  2198. draw_ofs += bg->get_offset();
  2199. Size2 draw_size = get_size() - bg->get_minimum_size();
  2200. bg->draw(ci, Rect2(Point2(), get_size()));
  2201. if (has_focus()) {
  2202. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  2203. bg_focus->draw(ci, Rect2(Point2(), get_size()));
  2204. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  2205. }
  2206. int tbh = _get_title_button_height();
  2207. draw_ofs.y += tbh;
  2208. draw_size.y -= tbh;
  2209. if (root) {
  2210. draw_item(Point2(), draw_ofs, draw_size, root);
  2211. }
  2212. int ofs = 0;
  2213. for (int i = 0; i < (columns.size() - 1 - 1); i++) {
  2214. ofs += get_column_width(i);
  2215. }
  2216. if (show_column_titles) {
  2217. //title butons
  2218. int ofs = cache.bg->get_margin(MARGIN_LEFT);
  2219. for (int i = 0; i < columns.size(); i++) {
  2220. 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);
  2221. Ref<Font> f = cache.tb_font;
  2222. Rect2 tbrect = Rect2(ofs - cache.offset.x, bg->get_margin(MARGIN_TOP), get_column_width(i), tbh);
  2223. sb->draw(ci, tbrect);
  2224. ofs += tbrect.size.width;
  2225. //text
  2226. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  2227. f->draw_halign(ci, tbrect.position + Point2i(sb->get_offset().x, (tbrect.size.height - f->get_height()) / 2 + f->get_ascent()), HALIGN_CENTER, clip_w, columns[i].title, cache.title_button_color);
  2228. }
  2229. }
  2230. }
  2231. if (p_what == NOTIFICATION_THEME_CHANGED) {
  2232. update_cache();
  2233. }
  2234. }
  2235. Size2 Tree::get_minimum_size() const {
  2236. return Size2(1, 1);
  2237. }
  2238. TreeItem *Tree::create_item(TreeItem *p_parent) {
  2239. ERR_FAIL_COND_V(blocked > 0, NULL);
  2240. TreeItem *ti = memnew(TreeItem(this));
  2241. ERR_FAIL_COND_V(!ti, NULL);
  2242. ti->cells.resize(columns.size());
  2243. if (p_parent) {
  2244. /* Always append at the end */
  2245. TreeItem *last = 0;
  2246. TreeItem *c = p_parent->childs;
  2247. while (c) {
  2248. last = c;
  2249. c = c->next;
  2250. }
  2251. if (last) {
  2252. last->next = ti;
  2253. } else {
  2254. p_parent->childs = ti;
  2255. }
  2256. ti->parent = p_parent;
  2257. } else {
  2258. if (root)
  2259. ti->childs = root;
  2260. root = ti;
  2261. }
  2262. return ti;
  2263. }
  2264. TreeItem *Tree::get_root() {
  2265. return root;
  2266. }
  2267. TreeItem *Tree::get_last_item() {
  2268. TreeItem *last = root;
  2269. while (last) {
  2270. if (last->next)
  2271. last = last->next;
  2272. else if (last->childs)
  2273. last = last->childs;
  2274. else
  2275. break;
  2276. }
  2277. return last;
  2278. }
  2279. void Tree::item_edited(int p_column, TreeItem *p_item, bool p_lmb) {
  2280. edited_item = p_item;
  2281. edited_col = p_column;
  2282. if (p_lmb)
  2283. emit_signal("item_edited");
  2284. else
  2285. emit_signal("item_rmb_edited");
  2286. }
  2287. void Tree::item_changed(int p_column, TreeItem *p_item) {
  2288. update();
  2289. }
  2290. void Tree::item_selected(int p_column, TreeItem *p_item) {
  2291. if (select_mode == SELECT_MULTI) {
  2292. if (!p_item->cells[p_column].selectable)
  2293. return;
  2294. p_item->cells[p_column].selected = true;
  2295. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  2296. } else {
  2297. select_single_item(p_item, root, p_column);
  2298. }
  2299. update();
  2300. }
  2301. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  2302. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  2303. p_item->cells[p_column].selected = false;
  2304. }
  2305. update();
  2306. }
  2307. void Tree::set_select_mode(SelectMode p_mode) {
  2308. select_mode = p_mode;
  2309. }
  2310. void Tree::deselect_all() {
  2311. TreeItem *item = get_next_selected(get_root());
  2312. while (item) {
  2313. item->deselect(selected_col);
  2314. item = get_next_selected(get_root());
  2315. }
  2316. selected_item = NULL;
  2317. selected_col = -1;
  2318. update();
  2319. }
  2320. bool Tree::is_anything_selected() {
  2321. return (selected_item != NULL);
  2322. }
  2323. void Tree::clear() {
  2324. if (blocked > 0) {
  2325. ERR_FAIL_COND(blocked > 0);
  2326. }
  2327. if (pressing_for_editor) {
  2328. if (range_drag_enabled) {
  2329. range_drag_enabled = false;
  2330. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  2331. warp_mouse(range_drag_capture_pos);
  2332. }
  2333. pressing_for_editor = false;
  2334. }
  2335. if (root) {
  2336. memdelete(root);
  2337. root = NULL;
  2338. };
  2339. selected_item = NULL;
  2340. edited_item = NULL;
  2341. popup_edited_item = NULL;
  2342. update();
  2343. };
  2344. void Tree::set_hide_root(bool p_enabled) {
  2345. hide_root = p_enabled;
  2346. update();
  2347. }
  2348. void Tree::set_column_min_width(int p_column, int p_min_width) {
  2349. ERR_FAIL_INDEX(p_column, columns.size());
  2350. if (p_min_width < 1)
  2351. return;
  2352. columns[p_column].min_width = p_min_width;
  2353. update();
  2354. }
  2355. void Tree::set_column_expand(int p_column, bool p_expand) {
  2356. ERR_FAIL_INDEX(p_column, columns.size());
  2357. columns[p_column].expand = p_expand;
  2358. update();
  2359. }
  2360. TreeItem *Tree::get_selected() const {
  2361. return selected_item;
  2362. }
  2363. int Tree::get_selected_column() const {
  2364. return selected_col;
  2365. }
  2366. TreeItem *Tree::get_edited() const {
  2367. return edited_item;
  2368. }
  2369. int Tree::get_edited_column() const {
  2370. return edited_col;
  2371. }
  2372. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  2373. /*
  2374. if (!p_item)
  2375. return NULL;
  2376. */
  2377. if (!root)
  2378. return NULL;
  2379. while (true) {
  2380. if (!p_item) {
  2381. p_item = root;
  2382. } else {
  2383. if (p_item->childs) {
  2384. p_item = p_item->childs;
  2385. } else if (p_item->next) {
  2386. p_item = p_item->next;
  2387. } else {
  2388. while (!p_item->next) {
  2389. p_item = p_item->parent;
  2390. if (p_item == NULL)
  2391. return NULL;
  2392. }
  2393. p_item = p_item->next;
  2394. }
  2395. }
  2396. for (int i = 0; i < columns.size(); i++)
  2397. if (p_item->cells[i].selected)
  2398. return p_item;
  2399. }
  2400. return NULL;
  2401. }
  2402. int Tree::get_column_width(int p_column) const {
  2403. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  2404. if (!columns[p_column].expand)
  2405. return columns[p_column].min_width;
  2406. Ref<StyleBox> bg = cache.bg;
  2407. int expand_area = get_size().width - (bg->get_margin(MARGIN_LEFT) + bg->get_margin(MARGIN_RIGHT));
  2408. if (v_scroll->is_visible_in_tree())
  2409. expand_area -= v_scroll->get_combined_minimum_size().width;
  2410. int expanding_columns = 0;
  2411. int expanding_total = 0;
  2412. for (int i = 0; i < columns.size(); i++) {
  2413. if (!columns[i].expand) {
  2414. expand_area -= columns[i].min_width;
  2415. } else {
  2416. expanding_total += columns[i].min_width;
  2417. expanding_columns++;
  2418. }
  2419. }
  2420. if (expand_area < expanding_total)
  2421. return columns[p_column].min_width;
  2422. ERR_FAIL_COND_V(expanding_columns == 0, -1); // shouldn't happen
  2423. return expand_area * columns[p_column].min_width / expanding_total;
  2424. }
  2425. void Tree::propagate_set_columns(TreeItem *p_item) {
  2426. p_item->cells.resize(columns.size());
  2427. TreeItem *c = p_item->get_children();
  2428. while (c) {
  2429. propagate_set_columns(c);
  2430. c = c->get_next();
  2431. }
  2432. }
  2433. void Tree::set_columns(int p_columns) {
  2434. ERR_FAIL_COND(p_columns < 1);
  2435. ERR_FAIL_COND(blocked > 0);
  2436. columns.resize(p_columns);
  2437. if (root)
  2438. propagate_set_columns(root);
  2439. if (selected_col >= p_columns)
  2440. selected_col = p_columns - 1;
  2441. update();
  2442. }
  2443. int Tree::get_columns() const {
  2444. return columns.size();
  2445. }
  2446. void Tree::_scroll_moved(float) {
  2447. update();
  2448. }
  2449. Rect2 Tree::get_custom_popup_rect() const {
  2450. return custom_popup_rect;
  2451. }
  2452. int Tree::get_item_offset(TreeItem *p_item) const {
  2453. TreeItem *it = root;
  2454. int ofs = _get_title_button_height();
  2455. if (!it)
  2456. return 0;
  2457. while (true) {
  2458. if (it == p_item)
  2459. return ofs;
  2460. ofs += compute_item_height(it) + cache.vseparation;
  2461. if (it->childs && !it->collapsed) {
  2462. it = it->childs;
  2463. } else if (it->next) {
  2464. it = it->next;
  2465. } else {
  2466. while (!it->next) {
  2467. it = it->parent;
  2468. if (it == NULL)
  2469. return 0;
  2470. }
  2471. it = it->next;
  2472. }
  2473. }
  2474. return -1; //not found
  2475. }
  2476. void Tree::ensure_cursor_is_visible() {
  2477. if (!is_inside_tree())
  2478. return;
  2479. TreeItem *selected = get_selected();
  2480. if (!selected)
  2481. return;
  2482. int ofs = get_item_offset(selected);
  2483. if (ofs == -1)
  2484. return;
  2485. int h = compute_item_height(selected) + cache.vseparation;
  2486. int screenh = get_size().height - h_scroll->get_combined_minimum_size().height;
  2487. if (ofs + h > v_scroll->get_value() + screenh)
  2488. v_scroll->call_deferred("set_value", ofs - screenh + h);
  2489. else if (ofs < v_scroll->get_value())
  2490. v_scroll->set_value(ofs);
  2491. }
  2492. int Tree::get_pressed_button() const {
  2493. return pressed_button;
  2494. }
  2495. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const {
  2496. ERR_FAIL_NULL_V(p_item, Rect2());
  2497. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  2498. if (p_column != -1) {
  2499. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  2500. }
  2501. int ofs = get_item_offset(p_item);
  2502. int height = compute_item_height(p_item);
  2503. Rect2 r;
  2504. r.position.y = ofs;
  2505. r.size.height = height;
  2506. if (p_column == -1) {
  2507. r.position.x = 0;
  2508. r.size.x = get_size().width;
  2509. } else {
  2510. int accum = 0;
  2511. for (int i = 0; i < p_column; i++) {
  2512. accum += get_column_width(i);
  2513. }
  2514. r.position.x = accum;
  2515. r.size.x = get_column_width(p_column);
  2516. }
  2517. return r;
  2518. }
  2519. void Tree::set_column_titles_visible(bool p_show) {
  2520. show_column_titles = p_show;
  2521. update();
  2522. }
  2523. bool Tree::are_column_titles_visible() const {
  2524. return show_column_titles;
  2525. }
  2526. void Tree::set_column_title(int p_column, const String &p_title) {
  2527. ERR_FAIL_INDEX(p_column, columns.size());
  2528. columns[p_column].title = p_title;
  2529. update();
  2530. }
  2531. String Tree::get_column_title(int p_column) const {
  2532. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  2533. return columns[p_column].title;
  2534. }
  2535. Point2 Tree::get_scroll() const {
  2536. Point2 ofs;
  2537. if (h_scroll->is_visible_in_tree())
  2538. ofs.x = h_scroll->get_value();
  2539. if (v_scroll->is_visible_in_tree())
  2540. ofs.y = v_scroll->get_value();
  2541. return ofs;
  2542. }
  2543. void Tree::scroll_to_item(TreeItem *p_item) {
  2544. if (!is_visible_in_tree()) {
  2545. // hack to work around crash in get_item_rect() if Tree is not in tree.
  2546. return;
  2547. }
  2548. // make sure the scrollbar min and max are up to date with latest changes.
  2549. update_scrollbars();
  2550. const Rect2 r = get_item_rect(p_item);
  2551. if (r.position.y < v_scroll->get_value()) {
  2552. v_scroll->set_value(r.position.y);
  2553. } else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) {
  2554. v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y);
  2555. }
  2556. }
  2557. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  2558. while (p_at) {
  2559. for (int i = 0; i < columns.size(); i++) {
  2560. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  2561. if (r_col)
  2562. *r_col = i;
  2563. return p_at;
  2564. }
  2565. }
  2566. if (p_backwards)
  2567. p_at = p_at->get_prev_visible();
  2568. else
  2569. p_at = p_at->get_next_visible();
  2570. }
  2571. return NULL;
  2572. }
  2573. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  2574. if (!root)
  2575. return NULL;
  2576. return _search_item_text(root, p_find, r_col, p_selectable);
  2577. }
  2578. void Tree::_do_incr_search(const String &p_add) {
  2579. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2580. uint64_t diff = time - last_keypress;
  2581. if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000)))
  2582. incr_search = p_add;
  2583. else
  2584. incr_search += p_add;
  2585. last_keypress = time;
  2586. int col;
  2587. TreeItem *item = search_item_text(incr_search, &col, true);
  2588. if (!item)
  2589. return;
  2590. item->select(col);
  2591. ensure_cursor_is_visible();
  2592. }
  2593. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
  2594. Point2 pos = p_pos;
  2595. if (root != p_item || !hide_root) {
  2596. h = compute_item_height(p_item) + cache.vseparation;
  2597. if (pos.y < h) {
  2598. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  2599. section = 0;
  2600. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  2601. section = pos.y < h / 2 ? -1 : 1;
  2602. } else if (pos.y < h / 4) {
  2603. section = -1;
  2604. } else if (pos.y >= (h * 3 / 4)) {
  2605. section = 1;
  2606. } else {
  2607. section = 0;
  2608. }
  2609. for (int i = 0; i < columns.size(); i++) {
  2610. int w = get_column_width(i);
  2611. if (pos.x < w) {
  2612. r_column = i;
  2613. return p_item;
  2614. }
  2615. pos.x -= w;
  2616. }
  2617. return NULL;
  2618. } else {
  2619. pos.y -= h;
  2620. }
  2621. } else {
  2622. h = 0;
  2623. }
  2624. if (p_item->is_collapsed())
  2625. return NULL; // do not try childs, it's collapsed
  2626. TreeItem *n = p_item->get_children();
  2627. while (n) {
  2628. int ch;
  2629. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
  2630. pos.y -= ch;
  2631. h += ch;
  2632. if (r)
  2633. return r;
  2634. n = n->get_next();
  2635. }
  2636. return NULL;
  2637. }
  2638. int Tree::get_column_at_position(const Point2 &p_pos) const {
  2639. if (root) {
  2640. Point2 pos = p_pos;
  2641. pos -= cache.bg->get_offset();
  2642. pos.y -= _get_title_button_height();
  2643. if (pos.y < 0)
  2644. return -1;
  2645. if (h_scroll->is_visible_in_tree())
  2646. pos.x += h_scroll->get_value();
  2647. if (v_scroll->is_visible_in_tree())
  2648. pos.y += v_scroll->get_value();
  2649. int col, h, section;
  2650. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2651. if (it) {
  2652. return col;
  2653. }
  2654. }
  2655. return -1;
  2656. }
  2657. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  2658. if (root) {
  2659. Point2 pos = p_pos;
  2660. pos -= cache.bg->get_offset();
  2661. pos.y -= _get_title_button_height();
  2662. if (pos.y < 0)
  2663. return -100;
  2664. if (h_scroll->is_visible_in_tree())
  2665. pos.x += h_scroll->get_value();
  2666. if (v_scroll->is_visible_in_tree())
  2667. pos.y += v_scroll->get_value();
  2668. int col, h, section;
  2669. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2670. if (it) {
  2671. return section;
  2672. }
  2673. }
  2674. return -100;
  2675. }
  2676. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  2677. if (root) {
  2678. Point2 pos = p_pos;
  2679. pos -= cache.bg->get_offset();
  2680. pos.y -= _get_title_button_height();
  2681. if (pos.y < 0)
  2682. return NULL;
  2683. if (h_scroll->is_visible_in_tree())
  2684. pos.x += h_scroll->get_value();
  2685. if (v_scroll->is_visible_in_tree())
  2686. pos.y += v_scroll->get_value();
  2687. int col, h, section;
  2688. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2689. if (it) {
  2690. return it;
  2691. }
  2692. }
  2693. return NULL;
  2694. }
  2695. String Tree::get_tooltip(const Point2 &p_pos) const {
  2696. if (root) {
  2697. Point2 pos = p_pos;
  2698. pos -= cache.bg->get_offset();
  2699. pos.y -= _get_title_button_height();
  2700. if (pos.y < 0)
  2701. return Control::get_tooltip(p_pos);
  2702. if (h_scroll->is_visible_in_tree())
  2703. pos.x += h_scroll->get_value();
  2704. if (v_scroll->is_visible_in_tree())
  2705. pos.y += v_scroll->get_value();
  2706. int col, h, section;
  2707. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2708. if (it) {
  2709. TreeItem::Cell &c = it->cells[col];
  2710. int col_width = get_column_width(col);
  2711. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  2712. Ref<Texture> b = c.buttons[j].texture;
  2713. Size2 size = b->get_size() + cache.button_pressed->get_minimum_size();
  2714. if (pos.x > col_width - size.width) {
  2715. String tooltip = c.buttons[j].tooltip;
  2716. if (tooltip != "") {
  2717. return tooltip;
  2718. }
  2719. }
  2720. col_width -= size.width;
  2721. }
  2722. String ret;
  2723. if (it->get_tooltip(col) == "")
  2724. ret = it->get_text(col);
  2725. else
  2726. ret = it->get_tooltip(col);
  2727. return ret;
  2728. }
  2729. }
  2730. return Control::get_tooltip(p_pos);
  2731. }
  2732. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2733. cursor_can_exit_tree = p_enable;
  2734. }
  2735. bool Tree::can_cursor_exit_tree() const {
  2736. return cursor_can_exit_tree;
  2737. }
  2738. void Tree::set_hide_folding(bool p_hide) {
  2739. hide_folding = p_hide;
  2740. update();
  2741. }
  2742. bool Tree::is_folding_hidden() const {
  2743. return hide_folding;
  2744. }
  2745. void Tree::set_value_evaluator(ValueEvaluator *p_evaluator) {
  2746. evaluator = p_evaluator;
  2747. }
  2748. void Tree::set_drop_mode_flags(int p_flags) {
  2749. if (drop_mode_flags == p_flags)
  2750. return;
  2751. drop_mode_flags = p_flags;
  2752. if (drop_mode_flags == 0) {
  2753. drop_mode_over = NULL;
  2754. }
  2755. update();
  2756. }
  2757. int Tree::get_drop_mode_flags() const {
  2758. return drop_mode_flags;
  2759. }
  2760. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  2761. force_edit_checkbox_only_on_checkbox = p_enable;
  2762. }
  2763. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  2764. return force_edit_checkbox_only_on_checkbox;
  2765. }
  2766. void Tree::set_allow_rmb_select(bool p_allow) {
  2767. allow_rmb_select = p_allow;
  2768. }
  2769. bool Tree::get_allow_rmb_select() const {
  2770. return allow_rmb_select;
  2771. }
  2772. void Tree::set_allow_reselect(bool p_allow) {
  2773. allow_reselect = p_allow;
  2774. }
  2775. bool Tree::get_allow_reselect() const {
  2776. return allow_reselect;
  2777. }
  2778. void Tree::_bind_methods() {
  2779. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &Tree::_range_click_timeout);
  2780. ClassDB::bind_method(D_METHOD("_gui_input"), &Tree::_gui_input);
  2781. ClassDB::bind_method(D_METHOD("_popup_select"), &Tree::popup_select);
  2782. ClassDB::bind_method(D_METHOD("_text_editor_enter"), &Tree::text_editor_enter);
  2783. ClassDB::bind_method(D_METHOD("_text_editor_modal_close"), &Tree::_text_editor_modal_close);
  2784. ClassDB::bind_method(D_METHOD("_value_editor_changed"), &Tree::value_editor_changed);
  2785. ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved);
  2786. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  2787. ClassDB::bind_method(D_METHOD("create_item", "parent"), &Tree::_create_item, DEFVAL(Variant()));
  2788. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  2789. ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width);
  2790. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  2791. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  2792. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  2793. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::_get_next_selected);
  2794. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  2795. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  2796. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  2797. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  2798. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  2799. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  2800. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  2801. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  2802. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  2803. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column"), &Tree::_get_item_rect, DEFVAL(-1));
  2804. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  2805. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  2806. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  2807. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  2808. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  2809. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  2810. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  2811. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  2812. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  2813. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  2814. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  2815. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  2816. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  2817. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  2818. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  2819. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  2820. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  2821. ADD_SIGNAL(MethodInfo("item_selected"));
  2822. ADD_SIGNAL(MethodInfo("cell_selected"));
  2823. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  2824. ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2825. ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2826. ADD_SIGNAL(MethodInfo("item_edited"));
  2827. ADD_SIGNAL(MethodInfo("item_rmb_edited"));
  2828. ADD_SIGNAL(MethodInfo("item_custom_button_pressed"));
  2829. ADD_SIGNAL(MethodInfo("item_double_clicked"));
  2830. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item")));
  2831. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2832. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::OBJECT, "item"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id")));
  2833. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  2834. ADD_SIGNAL(MethodInfo("item_activated"));
  2835. ADD_SIGNAL(MethodInfo("column_title_pressed", PropertyInfo(Variant::INT, "column")));
  2836. ADD_SIGNAL(MethodInfo("nothing_selected"));
  2837. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  2838. BIND_ENUM_CONSTANT(SELECT_ROW);
  2839. BIND_ENUM_CONSTANT(SELECT_MULTI);
  2840. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  2841. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  2842. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  2843. }
  2844. Tree::Tree() {
  2845. selected_col = 0;
  2846. columns.resize(1);
  2847. selected_item = NULL;
  2848. edited_item = NULL;
  2849. selected_col = -1;
  2850. edited_col = -1;
  2851. hide_root = false;
  2852. select_mode = SELECT_SINGLE;
  2853. root = 0;
  2854. popup_menu = NULL;
  2855. popup_edited_item = NULL;
  2856. text_editor = NULL;
  2857. set_focus_mode(FOCUS_ALL);
  2858. popup_menu = memnew(PopupMenu);
  2859. popup_menu->hide();
  2860. add_child(popup_menu);
  2861. popup_menu->set_as_toplevel(true);
  2862. text_editor = memnew(LineEdit);
  2863. add_child(text_editor);
  2864. text_editor->set_as_toplevel(true);
  2865. text_editor->hide();
  2866. value_editor = memnew(HSlider);
  2867. add_child(value_editor);
  2868. value_editor->set_as_toplevel(true);
  2869. value_editor->hide();
  2870. h_scroll = memnew(HScrollBar);
  2871. v_scroll = memnew(VScrollBar);
  2872. add_child(h_scroll);
  2873. add_child(v_scroll);
  2874. range_click_timer = memnew(Timer);
  2875. range_click_timer->connect("timeout", this, "_range_click_timeout");
  2876. add_child(range_click_timer);
  2877. h_scroll->connect("value_changed", this, "_scroll_moved");
  2878. v_scroll->connect("value_changed", this, "_scroll_moved");
  2879. text_editor->connect("text_entered", this, "_text_editor_enter");
  2880. text_editor->connect("modal_closed", this, "_text_editor_modal_close");
  2881. popup_menu->connect("id_pressed", this, "_popup_select");
  2882. value_editor->connect("value_changed", this, "_value_editor_changed");
  2883. value_editor->set_as_toplevel(true);
  2884. text_editor->set_as_toplevel(true);
  2885. updating_value_editor = false;
  2886. pressed_button = -1;
  2887. show_column_titles = false;
  2888. cache.click_type = Cache::CLICK_NONE;
  2889. cache.hover_type = Cache::CLICK_NONE;
  2890. cache.hover_index = -1;
  2891. cache.click_index = -1;
  2892. cache.click_id = -1;
  2893. cache.click_item = NULL;
  2894. cache.click_column = 0;
  2895. last_keypress = 0;
  2896. focus_in_id = 0;
  2897. blocked = 0;
  2898. cursor_can_exit_tree = true;
  2899. set_mouse_filter(MOUSE_FILTER_STOP);
  2900. drag_speed = 0;
  2901. drag_touching = false;
  2902. drag_touching_deaccel = false;
  2903. pressing_for_editor = false;
  2904. range_drag_enabled = false;
  2905. hide_folding = false;
  2906. evaluator = NULL;
  2907. drop_mode_flags = 0;
  2908. drop_mode_over = NULL;
  2909. drop_mode_section = 0;
  2910. single_select_defer = NULL;
  2911. allow_rmb_select = false;
  2912. force_edit_checkbox_only_on_checkbox = false;
  2913. set_clip_contents(true);
  2914. cache.hover_item = NULL;
  2915. cache.hover_cell = -1;
  2916. allow_reselect = false;
  2917. }
  2918. Tree::~Tree() {
  2919. if (root) {
  2920. memdelete(root);
  2921. }
  2922. }