tree.cpp 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934
  1. /*************************************************************************/
  2. /* tree.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "core/math/math_funcs.h"
  33. #include "core/os/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/print_string.h"
  37. #include "core/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->children == this)
  44. return; //already on top
  45. TreeItem *prev = get_prev();
  46. prev->next = next;
  47. next = parent->children;
  48. parent->children = 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->children = 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.write[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.write[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.write[p_column].text = p_text;
  127. if (cells[p_column].mode == TreeItem::CELL_MODE_RANGE) {
  128. Vector<String> strings = p_text.split(",");
  129. cells.write[p_column].min = INT_MAX;
  130. cells.write[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.write[p_column].min = MIN(cells[p_column].min, value);
  137. cells.write[p_column].max = MAX(cells[p_column].max, value);
  138. }
  139. cells.write[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.write[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.write[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.write[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.write[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.write[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.write[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.write[p_column].min = p_min;
  215. cells.write[p_column].max = p_max;
  216. cells.write[p_column].step = p_step;
  217. cells.write[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.write[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.write[p_column].custom_draw_obj = p_object->get_instance_id();
  238. cells.write[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->children == this)
  278. return NULL;
  279. TreeItem *prev = parent->children;
  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 children;
  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->children) {
  300. //go to the very end
  301. current = current->children;
  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->children) {
  311. current = current->children;
  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 = &children;
  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.write[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.write[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.write[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.write[p_column].buttons.write[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.write[p_column].buttons.write[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.write[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.write[p_column].custom_color = true;
  440. cells.write[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.write[p_column].custom_color = false;
  452. cells.write[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.write[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.write[p_column].custom_bg_color = true;
  466. cells.write[p_column].custom_bg_outline = p_bg_outline;
  467. cells.write[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.write[p_column].custom_bg_color = false;
  473. cells.write[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.write[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.write[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.write[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. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  577. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
  578. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
  579. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  580. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  581. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  582. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  583. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  584. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  585. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  586. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  587. }
  588. void TreeItem::clear_children() {
  589. TreeItem *c = children;
  590. while (c) {
  591. TreeItem *aux = c;
  592. c = c->get_next();
  593. aux->parent = 0; // so it won't try to recursively autoremove from me in here
  594. memdelete(aux);
  595. }
  596. children = 0;
  597. };
  598. TreeItem::TreeItem(Tree *p_tree) {
  599. tree = p_tree;
  600. collapsed = false;
  601. disable_folding = false;
  602. custom_min_height = 0;
  603. parent = 0; // parent item
  604. next = 0; // next in list
  605. children = 0; //child items
  606. }
  607. TreeItem::~TreeItem() {
  608. clear_children();
  609. if (parent)
  610. parent->remove_child(this);
  611. if (tree && tree->root == this) {
  612. tree->root = 0;
  613. }
  614. if (tree && tree->popup_edited_item == this) {
  615. tree->popup_edited_item = NULL;
  616. tree->pressing_for_editor = false;
  617. }
  618. if (tree && tree->cache.hover_item == this) {
  619. tree->cache.hover_item = NULL;
  620. }
  621. if (tree && tree->selected_item == this)
  622. tree->selected_item = NULL;
  623. if (tree && tree->drop_mode_over == this)
  624. tree->drop_mode_over = NULL;
  625. if (tree && tree->single_select_defer == this)
  626. tree->single_select_defer = NULL;
  627. if (tree && tree->edited_item == this) {
  628. tree->edited_item = NULL;
  629. tree->pressing_for_editor = false;
  630. }
  631. }
  632. /**********************************************/
  633. /**********************************************/
  634. /**********************************************/
  635. /**********************************************/
  636. /**********************************************/
  637. /**********************************************/
  638. void Tree::update_cache() {
  639. cache.font = get_font("font");
  640. cache.tb_font = get_font("title_button_font");
  641. cache.bg = get_stylebox("bg");
  642. cache.selected = get_stylebox("selected");
  643. cache.selected_focus = get_stylebox("selected_focus");
  644. cache.cursor = get_stylebox("cursor");
  645. cache.cursor_unfocus = get_stylebox("cursor_unfocused");
  646. cache.button_pressed = get_stylebox("button_pressed");
  647. cache.checked = get_icon("checked");
  648. cache.unchecked = get_icon("unchecked");
  649. cache.arrow_collapsed = get_icon("arrow_collapsed");
  650. cache.arrow = get_icon("arrow");
  651. cache.select_arrow = get_icon("select_arrow");
  652. cache.select_option = get_icon("select_option");
  653. cache.updown = get_icon("updown");
  654. cache.custom_button = get_stylebox("custom_button");
  655. cache.custom_button_hover = get_stylebox("custom_button_hover");
  656. cache.custom_button_pressed = get_stylebox("custom_button_pressed");
  657. cache.custom_button_font_highlight = get_color("custom_button_font_highlight");
  658. cache.font_color = get_color("font_color");
  659. cache.font_color_selected = get_color("font_color_selected");
  660. cache.guide_color = get_color("guide_color");
  661. cache.drop_position_color = get_color("drop_position_color");
  662. cache.hseparation = get_constant("hseparation");
  663. cache.vseparation = get_constant("vseparation");
  664. cache.item_margin = get_constant("item_margin");
  665. cache.button_margin = get_constant("button_margin");
  666. cache.guide_width = get_constant("guide_width");
  667. cache.draw_relationship_lines = get_constant("draw_relationship_lines");
  668. cache.relationship_line_color = get_color("relationship_line_color");
  669. cache.scroll_border = get_constant("scroll_border");
  670. cache.scroll_speed = get_constant("scroll_speed");
  671. cache.title_button = get_stylebox("title_button_normal");
  672. cache.title_button_pressed = get_stylebox("title_button_pressed");
  673. cache.title_button_hover = get_stylebox("title_button_hover");
  674. cache.title_button_color = get_color("title_button_color");
  675. v_scroll->set_custom_step(cache.font->get_height());
  676. }
  677. int Tree::compute_item_height(TreeItem *p_item) const {
  678. if (p_item == root && hide_root)
  679. return 0;
  680. ERR_FAIL_COND_V(cache.font.is_null(), 0);
  681. int height = cache.font->get_height();
  682. for (int i = 0; i < columns.size(); i++) {
  683. for (int j = 0; j < p_item->cells[i].buttons.size(); j++) {
  684. Size2i s; // = cache.button_pressed->get_minimum_size();
  685. s += p_item->cells[i].buttons[j].texture->get_size();
  686. if (s.height > height)
  687. height = s.height;
  688. }
  689. switch (p_item->cells[i].mode) {
  690. case TreeItem::CELL_MODE_CHECK: {
  691. int check_icon_h = cache.checked->get_height();
  692. if (height < check_icon_h)
  693. height = check_icon_h;
  694. }
  695. case TreeItem::CELL_MODE_STRING:
  696. case TreeItem::CELL_MODE_CUSTOM:
  697. case TreeItem::CELL_MODE_ICON: {
  698. Ref<Texture> icon = p_item->cells[i].icon;
  699. if (!icon.is_null()) {
  700. Size2i s = p_item->cells[i].get_icon_size();
  701. if (p_item->cells[i].icon_max_w > 0 && s.width > p_item->cells[i].icon_max_w) {
  702. s.height = s.height * p_item->cells[i].icon_max_w / s.width;
  703. }
  704. if (s.height > height)
  705. height = s.height;
  706. }
  707. if (p_item->cells[i].mode == TreeItem::CELL_MODE_CUSTOM && p_item->cells[i].custom_button) {
  708. height += cache.custom_button->get_minimum_size().height;
  709. }
  710. } break;
  711. default: {}
  712. }
  713. }
  714. int item_min_height = p_item->get_custom_minimum_height();
  715. if (height < item_min_height)
  716. height = item_min_height;
  717. height += cache.vseparation;
  718. return height;
  719. }
  720. int Tree::get_item_height(TreeItem *p_item) const {
  721. int height = compute_item_height(p_item);
  722. height += cache.vseparation;
  723. if (!p_item->collapsed) { /* if not collapsed, check the children */
  724. TreeItem *c = p_item->children;
  725. while (c) {
  726. height += get_item_height(c);
  727. c = c->next;
  728. }
  729. }
  730. return height;
  731. }
  732. void Tree::draw_item_rect(const TreeItem::Cell &p_cell, const Rect2i &p_rect, const Color &p_color, const Color &p_icon_color) {
  733. ERR_FAIL_COND(cache.font.is_null());
  734. Rect2i rect = p_rect;
  735. Ref<Font> font = cache.font;
  736. String text = p_cell.text;
  737. if (p_cell.suffix != String())
  738. text += " " + p_cell.suffix;
  739. int w = 0;
  740. if (!p_cell.icon.is_null()) {
  741. Size2i bmsize = p_cell.get_icon_size();
  742. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  743. bmsize.width = p_cell.icon_max_w;
  744. }
  745. w += bmsize.width + cache.hseparation;
  746. }
  747. w += font->get_string_size(text).width;
  748. switch (p_cell.text_align) {
  749. case TreeItem::ALIGN_LEFT:
  750. break; //do none
  751. case TreeItem::ALIGN_CENTER:
  752. rect.position.x += MAX(0, (rect.size.width - w) / 2);
  753. break; //do none
  754. case TreeItem::ALIGN_RIGHT:
  755. rect.position.x += MAX(0, (rect.size.width - w));
  756. break; //do none
  757. }
  758. RID ci = get_canvas_item();
  759. if (!p_cell.icon.is_null()) {
  760. Size2i bmsize = p_cell.get_icon_size();
  761. if (p_cell.icon_max_w > 0 && bmsize.width > p_cell.icon_max_w) {
  762. bmsize.height = bmsize.height * p_cell.icon_max_w / bmsize.width;
  763. bmsize.width = p_cell.icon_max_w;
  764. }
  765. p_cell.draw_icon(ci, rect.position + Size2i(0, Math::floor((real_t)(rect.size.y - bmsize.y) / 2)), bmsize, p_icon_color);
  766. rect.position.x += bmsize.x + cache.hseparation;
  767. rect.size.x -= bmsize.x + cache.hseparation;
  768. }
  769. rect.position.y += Math::floor((rect.size.y - font->get_height()) / 2.0) + font->get_ascent();
  770. font->draw(ci, rect.position, text, p_color, rect.size.x);
  771. }
  772. int Tree::draw_item(const Point2i &p_pos, const Point2 &p_draw_ofs, const Size2 &p_draw_size, TreeItem *p_item) {
  773. if (p_pos.y - cache.offset.y > (p_draw_size.height))
  774. return -1; //draw no more!
  775. RID ci = get_canvas_item();
  776. int htotal = 0;
  777. int label_h = compute_item_height(p_item);
  778. /* Calculate height of the label part */
  779. label_h += cache.vseparation;
  780. /* Draw label, if height fits */
  781. bool skip = (p_item == root && hide_root);
  782. if (!skip && (p_pos.y + label_h - cache.offset.y) > 0) {
  783. //draw separation.
  784. //if (p_item->get_parent()!=root || !hide_root)
  785. ERR_FAIL_COND_V(cache.font.is_null(), -1);
  786. Ref<Font> font = cache.font;
  787. int font_ascent = font->get_ascent();
  788. int ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  789. int skip = 0;
  790. for (int i = 0; i < columns.size(); i++) {
  791. if (skip) {
  792. skip--;
  793. continue;
  794. }
  795. int w = get_column_width(i);
  796. if (i == 0) {
  797. w -= ofs;
  798. if (w <= 0) {
  799. ofs = get_column_width(0);
  800. continue;
  801. }
  802. } else {
  803. ofs += cache.hseparation;
  804. w -= cache.hseparation;
  805. }
  806. if (p_item->cells[i].expand_right) {
  807. int plus = 1;
  808. 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()) {
  809. w += get_column_width(i + plus);
  810. plus++;
  811. skip++;
  812. }
  813. }
  814. int bw = 0;
  815. for (int j = p_item->cells[i].buttons.size() - 1; j >= 0; j--) {
  816. Ref<Texture> b = p_item->cells[i].buttons[j].texture;
  817. Size2 s = b->get_size() + cache.button_pressed->get_minimum_size();
  818. Point2i o = Point2i(ofs + w - s.width, p_pos.y) - cache.offset + p_draw_ofs;
  819. 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) {
  820. //being pressed
  821. cache.button_pressed->draw(get_canvas_item(), Rect2(o, s));
  822. }
  823. o.y += (label_h - s.height) / 2;
  824. o += cache.button_pressed->get_offset();
  825. b->draw(ci, o, p_item->cells[i].buttons[j].disabled ? Color(1, 1, 1, 0.5) : p_item->cells[i].buttons[j].color);
  826. w -= s.width + cache.button_margin;
  827. bw += s.width + cache.button_margin;
  828. }
  829. Rect2i item_rect = Rect2i(Point2i(ofs, p_pos.y) - cache.offset + p_draw_ofs, Size2i(w, label_h));
  830. Rect2i cell_rect = item_rect;
  831. if (i != 0) {
  832. cell_rect.position.x -= cache.hseparation;
  833. cell_rect.size.x += cache.hseparation;
  834. }
  835. 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);
  836. if (i == 0) {
  837. if (p_item->cells[0].selected && select_mode == SELECT_ROW) {
  838. 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));
  839. //Rect2 r = Rect2i(row_rect.pos,row_rect.size);
  840. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  841. if (has_focus())
  842. cache.selected_focus->draw(ci, row_rect);
  843. else
  844. cache.selected->draw(ci, row_rect);
  845. }
  846. }
  847. if (p_item->cells[i].selected && select_mode != SELECT_ROW) {
  848. Rect2i r(cell_rect.position, cell_rect.size);
  849. if (p_item->cells[i].text.size() > 0) {
  850. float icon_width = p_item->cells[i].get_icon_size().width;
  851. r.position.x += icon_width;
  852. r.size.x -= icon_width;
  853. }
  854. //r.grow(cache.selected->get_margin(MARGIN_LEFT));
  855. if (has_focus()) {
  856. cache.selected_focus->draw(ci, r);
  857. p_item->set_meta("__focus_rect", Rect2(r.position, r.size));
  858. } else {
  859. cache.selected->draw(ci, r);
  860. }
  861. if (text_editor->is_visible_in_tree()) {
  862. Vector2 ofs(0, (text_editor->get_size().height - r.size.height) / 2);
  863. text_editor->set_position(get_global_position() + r.position - ofs);
  864. }
  865. }
  866. if (p_item->cells[i].custom_bg_color) {
  867. Rect2 r = cell_rect;
  868. if (i == 0) {
  869. r.position.x = p_draw_ofs.x;
  870. r.size.x = w + ofs;
  871. } else {
  872. r.position.x -= cache.hseparation;
  873. r.size.x += cache.hseparation;
  874. }
  875. if (p_item->cells[i].custom_bg_outline) {
  876. 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);
  877. 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);
  878. 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);
  879. 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);
  880. } else {
  881. VisualServer::get_singleton()->canvas_item_add_rect(ci, r, p_item->cells[i].bg_color);
  882. }
  883. }
  884. if (drop_mode_flags && drop_mode_over == p_item) {
  885. Rect2 r = cell_rect;
  886. if (drop_mode_section == -1 || drop_mode_section == 0) {
  887. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, r.size.x, 1), cache.drop_position_color);
  888. }
  889. if (drop_mode_section == 0) {
  890. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(r.position.x, r.position.y, 1, r.size.y), cache.drop_position_color);
  891. 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);
  892. }
  893. if (drop_mode_section == 1 || drop_mode_section == 0) {
  894. 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);
  895. }
  896. }
  897. 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");
  898. Color icon_col = p_item->cells[i].icon_color;
  899. Point2i text_pos = item_rect.position;
  900. text_pos.y += Math::floor((item_rect.size.y - font->get_height()) / 2) + font_ascent;
  901. switch (p_item->cells[i].mode) {
  902. case TreeItem::CELL_MODE_STRING: {
  903. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  904. } break;
  905. case TreeItem::CELL_MODE_CHECK: {
  906. Ref<Texture> checked = cache.checked;
  907. Ref<Texture> unchecked = cache.unchecked;
  908. Point2i check_ofs = item_rect.position;
  909. check_ofs.y += Math::floor((real_t)(item_rect.size.y - checked->get_height()) / 2);
  910. if (p_item->cells[i].checked) {
  911. checked->draw(ci, check_ofs, icon_col);
  912. } else {
  913. unchecked->draw(ci, check_ofs, icon_col);
  914. }
  915. int check_w = checked->get_width() + cache.hseparation;
  916. text_pos.x += check_w;
  917. item_rect.size.x -= check_w;
  918. item_rect.position.x += check_w;
  919. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  920. //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w );
  921. } break;
  922. case TreeItem::CELL_MODE_RANGE: {
  923. if (p_item->cells[i].text != "") {
  924. if (!p_item->cells[i].editable)
  925. break;
  926. int option = (int)p_item->cells[i].val;
  927. String s = RTR("(Other)");
  928. Vector<String> strings = p_item->cells[i].text.split(",");
  929. for (int i = 0; i < strings.size(); i++) {
  930. int value = i;
  931. if (!strings[i].get_slicec(':', 1).empty()) {
  932. value = strings[i].get_slicec(':', 1).to_int();
  933. }
  934. if (option == value) {
  935. s = strings[i].get_slicec(':', 0);
  936. break;
  937. }
  938. }
  939. if (p_item->cells[i].suffix != String())
  940. s += " " + p_item->cells[i].suffix;
  941. Ref<Texture> downarrow = cache.select_arrow;
  942. font->draw(ci, text_pos, s, col, item_rect.size.x - downarrow->get_width());
  943. //?
  944. Point2i arrow_pos = item_rect.position;
  945. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  946. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  947. downarrow->draw(ci, arrow_pos, icon_col);
  948. } else {
  949. Ref<Texture> updown = cache.updown;
  950. String valtext = String::num(p_item->cells[i].val, Math::step_decimals(p_item->cells[i].step));
  951. //String valtext = rtos( p_item->cells[i].val );
  952. if (p_item->cells[i].suffix != String())
  953. valtext += " " + p_item->cells[i].suffix;
  954. font->draw(ci, text_pos, valtext, col, item_rect.size.x - updown->get_width());
  955. if (!p_item->cells[i].editable)
  956. break;
  957. Point2i updown_pos = item_rect.position;
  958. updown_pos.x += item_rect.size.x - updown->get_width();
  959. updown_pos.y += Math::floor(((item_rect.size.y - updown->get_height())) / 2.0);
  960. updown->draw(ci, updown_pos, icon_col);
  961. }
  962. } break;
  963. case TreeItem::CELL_MODE_ICON: {
  964. if (p_item->cells[i].icon.is_null())
  965. break;
  966. Size2i icon_size = p_item->cells[i].get_icon_size();
  967. if (p_item->cells[i].icon_max_w > 0 && icon_size.width > p_item->cells[i].icon_max_w) {
  968. icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width;
  969. icon_size.width = p_item->cells[i].icon_max_w;
  970. }
  971. Point2i icon_ofs = (item_rect.size - icon_size) / 2;
  972. icon_ofs += item_rect.position;
  973. draw_texture_rect(p_item->cells[i].icon, Rect2(icon_ofs, icon_size), false, icon_col);
  974. //p_item->cells[i].icon->draw(ci, icon_ofs);
  975. } break;
  976. case TreeItem::CELL_MODE_CUSTOM: {
  977. //int option = (int)p_item->cells[i].val;
  978. if (p_item->cells[i].custom_draw_obj) {
  979. Object *cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
  980. if (cdo)
  981. cdo->call(p_item->cells[i].custom_draw_callback, p_item, Rect2(item_rect));
  982. }
  983. if (!p_item->cells[i].editable) {
  984. draw_item_rect(p_item->cells[i], item_rect, col, icon_col);
  985. break;
  986. }
  987. Ref<Texture> downarrow = cache.select_arrow;
  988. Rect2i ir = item_rect;
  989. Point2i arrow_pos = item_rect.position;
  990. arrow_pos.x += item_rect.size.x - downarrow->get_width();
  991. arrow_pos.y += Math::floor(((item_rect.size.y - downarrow->get_height())) / 2.0);
  992. ir.size.width -= downarrow->get_width();
  993. if (p_item->cells[i].custom_button) {
  994. if (cache.hover_item == p_item && cache.hover_cell == i) {
  995. if (Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  996. draw_style_box(cache.custom_button_pressed, ir);
  997. } else {
  998. draw_style_box(cache.custom_button_hover, ir);
  999. col = cache.custom_button_font_highlight;
  1000. }
  1001. } else {
  1002. draw_style_box(cache.custom_button, ir);
  1003. }
  1004. ir.size -= cache.custom_button->get_minimum_size();
  1005. ir.position += cache.custom_button->get_offset();
  1006. }
  1007. draw_item_rect(p_item->cells[i], ir, col, icon_col);
  1008. downarrow->draw(ci, arrow_pos);
  1009. } break;
  1010. }
  1011. if (i == 0) {
  1012. ofs = get_column_width(0);
  1013. } else {
  1014. ofs += w + bw;
  1015. }
  1016. if (select_mode == SELECT_MULTI && selected_item == p_item && selected_col == i) {
  1017. if (has_focus())
  1018. cache.cursor->draw(ci, cell_rect);
  1019. else
  1020. cache.cursor_unfocus->draw(ci, cell_rect);
  1021. }
  1022. }
  1023. if (!p_item->disable_folding && !hide_folding && p_item->children) { //has children, draw the guide box
  1024. Ref<Texture> arrow;
  1025. if (p_item->collapsed) {
  1026. arrow = cache.arrow_collapsed;
  1027. } else {
  1028. arrow = cache.arrow;
  1029. }
  1030. arrow->draw(ci, p_pos + p_draw_ofs + Point2i(0, (label_h - arrow->get_height()) / 2) - cache.offset);
  1031. }
  1032. //separator
  1033. //get_painter()->draw_fill_rect( Point2i(0,pos.y),Size2i(get_size().width,1),color( COLOR_TREE_GRID) );
  1034. //pos=p_pos; //reset pos
  1035. }
  1036. Point2 children_pos = p_pos;
  1037. if (!skip) {
  1038. children_pos.x += cache.item_margin;
  1039. htotal += label_h;
  1040. children_pos.y += htotal;
  1041. }
  1042. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1043. TreeItem *c = p_item->children;
  1044. while (c) {
  1045. if (cache.draw_relationship_lines == 1 && (c->get_parent() != root || !hide_root)) {
  1046. int root_ofs = children_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1047. int parent_ofs = p_pos.x + ((p_item->disable_folding || hide_folding) ? cache.hseparation : cache.item_margin);
  1048. Point2i root_pos = Point2i(root_ofs, children_pos.y + label_h / 2) - cache.offset + p_draw_ofs;
  1049. if (c->get_children() != NULL)
  1050. root_pos -= Point2i(cache.arrow->get_width(), 0);
  1051. float line_width = 1.0;
  1052. #ifdef TOOLS_ENABLED
  1053. line_width *= EDSCALE;
  1054. #endif
  1055. 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;
  1056. if (root_pos.y + line_width >= 0) {
  1057. 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);
  1058. VisualServer::get_singleton()->canvas_item_add_line(ci, Point2i(parent_pos.x, root_pos.y), parent_pos, cache.relationship_line_color, line_width);
  1059. }
  1060. if (htotal < 0) {
  1061. return -1;
  1062. }
  1063. }
  1064. if (htotal >= 0) {
  1065. int child_h = draw_item(children_pos, p_draw_ofs, p_draw_size, c);
  1066. if (child_h < 0) {
  1067. if (cache.draw_relationship_lines == 0) {
  1068. return -1; // break, stop drawing, no need to anymore
  1069. } else {
  1070. htotal = -1;
  1071. children_pos.y = cache.offset.y + p_draw_size.height;
  1072. }
  1073. } else {
  1074. htotal += child_h;
  1075. children_pos.y += child_h;
  1076. }
  1077. }
  1078. c = c->next;
  1079. }
  1080. }
  1081. return htotal;
  1082. }
  1083. int Tree::_count_selected_items(TreeItem *p_from) const {
  1084. int count = 0;
  1085. for (int i = 0; i < columns.size(); i++) {
  1086. if (p_from->is_selected(i))
  1087. count++;
  1088. }
  1089. if (p_from->get_children()) {
  1090. count += _count_selected_items(p_from->get_children());
  1091. }
  1092. if (p_from->get_next()) {
  1093. count += _count_selected_items(p_from->get_next());
  1094. }
  1095. return count;
  1096. }
  1097. 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) {
  1098. TreeItem::Cell &selected_cell = p_selected->cells.write[p_col];
  1099. bool switched = false;
  1100. if (r_in_range && !*r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1101. *r_in_range = true;
  1102. switched = true;
  1103. }
  1104. bool emitted_row = false;
  1105. for (int i = 0; i < columns.size(); i++) {
  1106. TreeItem::Cell &c = p_current->cells.write[i];
  1107. if (!c.selectable)
  1108. continue;
  1109. if (select_mode == SELECT_ROW) {
  1110. if (p_selected == p_current && (!c.selected || allow_reselect)) {
  1111. c.selected = true;
  1112. selected_item = p_selected;
  1113. selected_col = 0;
  1114. if (!emitted_row) {
  1115. emit_signal("item_selected");
  1116. emitted_row = true;
  1117. }
  1118. /*
  1119. if (p_col==i)
  1120. p_current->selected_signal.call(p_col);
  1121. */
  1122. } else if (c.selected) {
  1123. c.selected = false;
  1124. //p_current->deselected_signal.call(p_col);
  1125. }
  1126. } else if (select_mode == SELECT_SINGLE || select_mode == SELECT_MULTI) {
  1127. if (!r_in_range && &selected_cell == &c) {
  1128. if (!selected_cell.selected || allow_reselect) {
  1129. selected_cell.selected = true;
  1130. selected_item = p_selected;
  1131. selected_col = i;
  1132. emit_signal("cell_selected");
  1133. if (select_mode == SELECT_MULTI)
  1134. emit_signal("multi_selected", p_current, i, true);
  1135. else if (select_mode == SELECT_SINGLE)
  1136. emit_signal("item_selected");
  1137. } else if (select_mode == SELECT_MULTI && (selected_item != p_selected || selected_col != i)) {
  1138. selected_item = p_selected;
  1139. selected_col = i;
  1140. emit_signal("cell_selected");
  1141. }
  1142. } else {
  1143. if (r_in_range && *r_in_range && !p_force_deselect) {
  1144. if (!c.selected && c.selectable) {
  1145. c.selected = true;
  1146. emit_signal("multi_selected", p_current, i, true);
  1147. }
  1148. } else if (!r_in_range || p_force_deselect) {
  1149. if (select_mode == SELECT_MULTI && c.selected)
  1150. emit_signal("multi_selected", p_current, i, false);
  1151. c.selected = false;
  1152. }
  1153. //p_current->deselected_signal.call(p_col);
  1154. }
  1155. }
  1156. }
  1157. if (!switched && r_in_range && *r_in_range && (p_current == p_selected || p_current == p_prev)) {
  1158. *r_in_range = false;
  1159. }
  1160. TreeItem *c = p_current->children;
  1161. while (c) {
  1162. select_single_item(p_selected, c, p_col, p_prev, r_in_range, p_current->is_collapsed() || p_force_deselect);
  1163. c = c->next;
  1164. }
  1165. }
  1166. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  1167. return Rect2();
  1168. }
  1169. void Tree::_range_click_timeout() {
  1170. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  1171. Point2 pos = get_local_mouse_position() - cache.bg->get_offset();
  1172. if (show_column_titles) {
  1173. pos.y -= _get_title_button_height();
  1174. if (pos.y < 0) {
  1175. range_click_timer->stop();
  1176. return;
  1177. }
  1178. }
  1179. click_handled = false;
  1180. Ref<InputEventMouseButton> mb;
  1181. mb.instance();
  1182. ;
  1183. blocked++;
  1184. propagate_mouse_event(pos + cache.offset, 0, 0, false, root, BUTTON_LEFT, mb);
  1185. blocked--;
  1186. if (range_click_timer->is_one_shot()) {
  1187. range_click_timer->set_wait_time(0.05);
  1188. range_click_timer->set_one_shot(false);
  1189. range_click_timer->start();
  1190. }
  1191. if (!click_handled)
  1192. range_click_timer->stop();
  1193. } else {
  1194. range_click_timer->stop();
  1195. }
  1196. }
  1197. 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) {
  1198. int item_h = compute_item_height(p_item) + cache.vseparation;
  1199. bool skip = (p_item == root && hide_root);
  1200. if (!skip && p_pos.y < item_h) {
  1201. // check event!
  1202. if (range_click_timer->get_time_left() > 0 && p_item != range_item_last) {
  1203. return -1;
  1204. }
  1205. if (!p_item->disable_folding && !hide_folding && (p_pos.x >= x_ofs && p_pos.x < (x_ofs + cache.item_margin))) {
  1206. if (p_item->children)
  1207. p_item->set_collapsed(!p_item->is_collapsed());
  1208. return -1; //handled!
  1209. }
  1210. int x = p_pos.x;
  1211. /* find clicked column */
  1212. int col = -1;
  1213. int col_ofs = 0;
  1214. int col_width = 0;
  1215. for (int i = 0; i < columns.size(); i++) {
  1216. col_width = get_column_width(i);
  1217. if (p_item->cells[i].expand_right) {
  1218. int plus = 1;
  1219. 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()) {
  1220. col_width += cache.hseparation;
  1221. col_width += get_column_width(i + plus);
  1222. plus++;
  1223. }
  1224. }
  1225. if (x > col_width) {
  1226. col_ofs += col_width;
  1227. x -= col_width;
  1228. continue;
  1229. }
  1230. col = i;
  1231. break;
  1232. }
  1233. if (col == -1)
  1234. return -1;
  1235. else if (col == 0) {
  1236. int margin = x_ofs + cache.item_margin; //-cache.hseparation;
  1237. //int lm = cache.bg->get_margin(MARGIN_LEFT);
  1238. col_width -= margin;
  1239. col_ofs += margin;
  1240. x -= margin;
  1241. } else {
  1242. col_width -= cache.hseparation;
  1243. x -= cache.hseparation;
  1244. }
  1245. if (!p_item->disable_folding && !hide_folding && !p_item->cells[col].editable && !p_item->cells[col].selectable && p_item->get_children()) {
  1246. p_item->set_collapsed(!p_item->is_collapsed());
  1247. return -1; //collapse/uncollapse because nothing can be done with item
  1248. }
  1249. const TreeItem::Cell &c = p_item->cells[col];
  1250. bool already_selected = c.selected;
  1251. bool already_cursor = (p_item == selected_item) && col == selected_col;
  1252. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  1253. Ref<Texture> b = c.buttons[j].texture;
  1254. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  1255. if (x > col_width - w) {
  1256. if (c.buttons[j].disabled) {
  1257. pressed_button = -1;
  1258. cache.click_type = Cache::CLICK_NONE;
  1259. return -1;
  1260. }
  1261. pressed_button = j;
  1262. cache.click_type = Cache::CLICK_BUTTON;
  1263. cache.click_index = j;
  1264. cache.click_id = c.buttons[j].id;
  1265. cache.click_item = p_item;
  1266. cache.click_column = col;
  1267. cache.click_pos = get_global_mouse_position() - get_global_position();
  1268. update();
  1269. //emit_signal("button_pressed");
  1270. return -1;
  1271. }
  1272. col_width -= w + cache.button_margin;
  1273. }
  1274. if (p_button == BUTTON_LEFT || (p_button == BUTTON_RIGHT && allow_rmb_select)) {
  1275. /* process selection */
  1276. 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
  1277. emit_signal("item_activated");
  1278. incr_search.clear();
  1279. return -1;
  1280. }
  1281. if (select_mode == SELECT_MULTI && p_mod->get_command() && c.selectable) {
  1282. if (!c.selected || p_button == BUTTON_RIGHT) {
  1283. p_item->select(col);
  1284. emit_signal("multi_selected", p_item, col, true);
  1285. if (p_button == BUTTON_RIGHT) {
  1286. emit_signal("item_rmb_selected", get_local_mouse_position());
  1287. }
  1288. //p_item->selected_signal.call(col);
  1289. } else {
  1290. p_item->deselect(col);
  1291. emit_signal("multi_selected", p_item, col, false);
  1292. //p_item->deselected_signal.call(col);
  1293. }
  1294. } else {
  1295. if (c.selectable) {
  1296. if (select_mode == SELECT_MULTI && p_mod->get_shift() && selected_item && selected_item != p_item) {
  1297. bool inrange = false;
  1298. select_single_item(p_item, root, col, selected_item, &inrange);
  1299. if (p_button == BUTTON_RIGHT) {
  1300. emit_signal("item_rmb_selected", get_local_mouse_position());
  1301. }
  1302. } else {
  1303. int icount = _count_selected_items(root);
  1304. if (select_mode == SELECT_MULTI && icount > 1 && p_button != BUTTON_RIGHT) {
  1305. single_select_defer = p_item;
  1306. single_select_defer_column = col;
  1307. } else {
  1308. if (p_button != BUTTON_RIGHT || !c.selected) {
  1309. select_single_item(p_item, root, col);
  1310. }
  1311. if (p_button == BUTTON_RIGHT) {
  1312. emit_signal("item_rmb_selected", get_local_mouse_position());
  1313. }
  1314. }
  1315. }
  1316. /*
  1317. if (!c.selected && select_mode==SELECT_MULTI) {
  1318. emit_signal("multi_selected",p_item,col,true);
  1319. }
  1320. */
  1321. update();
  1322. }
  1323. }
  1324. }
  1325. if (!c.editable)
  1326. return -1; // if cell is not editable, don't bother
  1327. /* editing */
  1328. bool bring_up_editor = allow_reselect ? (c.selected && already_selected) : c.selected;
  1329. String editor_text = c.text;
  1330. switch (c.mode) {
  1331. case TreeItem::CELL_MODE_STRING: {
  1332. //nothing in particular
  1333. if (select_mode == SELECT_MULTI && (get_tree()->get_event_count() == focus_in_id || !already_cursor)) {
  1334. bring_up_editor = false;
  1335. }
  1336. } break;
  1337. case TreeItem::CELL_MODE_CHECK: {
  1338. bring_up_editor = false; //checkboxes are not edited with editor
  1339. if (force_edit_checkbox_only_on_checkbox) {
  1340. if (x < cache.checked->get_width()) {
  1341. p_item->set_checked(col, !c.checked);
  1342. item_edited(col, p_item);
  1343. }
  1344. } else {
  1345. p_item->set_checked(col, !c.checked);
  1346. item_edited(col, p_item);
  1347. }
  1348. click_handled = true;
  1349. //p_item->edited_signal.call(col);
  1350. } break;
  1351. case TreeItem::CELL_MODE_RANGE: {
  1352. if (c.text != "") {
  1353. //if (x >= (get_column_width(col)-item_h/2)) {
  1354. popup_menu->clear();
  1355. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  1356. String s = c.text.get_slicec(',', i);
  1357. popup_menu->add_item(s.get_slicec(':', 0), s.get_slicec(':', 1).empty() ? i : s.get_slicec(':', 1).to_int());
  1358. }
  1359. popup_menu->set_size(Size2(col_width, 0));
  1360. popup_menu->set_position(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs + item_h) - cache.offset);
  1361. popup_menu->popup();
  1362. popup_edited_item = p_item;
  1363. popup_edited_item_col = col;
  1364. //}
  1365. bring_up_editor = false;
  1366. } else {
  1367. if (x >= (col_width - item_h / 2)) {
  1368. /* touching the combo */
  1369. bool up = p_pos.y < (item_h / 2);
  1370. if (p_button == BUTTON_LEFT) {
  1371. if (range_click_timer->get_time_left() == 0) {
  1372. range_item_last = p_item;
  1373. range_up_last = up;
  1374. range_click_timer->set_wait_time(0.6);
  1375. range_click_timer->set_one_shot(true);
  1376. range_click_timer->start();
  1377. } else if (up != range_up_last) {
  1378. return -1; // break. avoid changing direction on mouse held
  1379. }
  1380. p_item->set_range(col, c.val + (up ? 1.0 : -1.0) * c.step);
  1381. item_edited(col, p_item);
  1382. } else if (p_button == BUTTON_RIGHT) {
  1383. p_item->set_range(col, (up ? c.max : c.min));
  1384. item_edited(col, p_item);
  1385. } else if (p_button == BUTTON_WHEEL_UP) {
  1386. p_item->set_range(col, c.val + c.step);
  1387. item_edited(col, p_item);
  1388. } else if (p_button == BUTTON_WHEEL_DOWN) {
  1389. p_item->set_range(col, c.val - c.step);
  1390. item_edited(col, p_item);
  1391. }
  1392. //p_item->edited_signal.call(col);
  1393. bring_up_editor = false;
  1394. } else {
  1395. editor_text = String::num(p_item->cells[col].val, Math::step_decimals(p_item->cells[col].step));
  1396. if (select_mode == SELECT_MULTI && get_tree()->get_event_count() == focus_in_id)
  1397. bring_up_editor = false;
  1398. }
  1399. }
  1400. click_handled = true;
  1401. } break;
  1402. case TreeItem::CELL_MODE_ICON: {
  1403. bring_up_editor = false;
  1404. } break;
  1405. case TreeItem::CELL_MODE_CUSTOM: {
  1406. edited_item = p_item;
  1407. edited_col = col;
  1408. bool on_arrow = x > col_width - cache.select_arrow->get_width();
  1409. bring_up_editor = false;
  1410. 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));
  1411. if (on_arrow || !p_item->cells[col].custom_button) {
  1412. emit_signal("custom_popup_edited", ((bool)(x >= (col_width - item_h / 2))));
  1413. }
  1414. if (!p_item->cells[col].custom_button || !on_arrow) {
  1415. item_edited(col, p_item, p_button == BUTTON_LEFT);
  1416. }
  1417. click_handled = true;
  1418. return -1;
  1419. } break;
  1420. };
  1421. if (!bring_up_editor || p_button != BUTTON_LEFT)
  1422. return -1;
  1423. click_handled = true;
  1424. popup_edited_item = p_item;
  1425. popup_edited_item_col = col;
  1426. pressing_item_rect = Rect2(get_global_position() + Point2i(col_ofs, _get_title_button_height() + y_ofs) - cache.offset, Size2(col_width, item_h));
  1427. pressing_for_editor_text = editor_text;
  1428. pressing_for_editor = true;
  1429. return -1; //select
  1430. } else {
  1431. Point2i new_pos = p_pos;
  1432. if (!skip) {
  1433. x_ofs += cache.item_margin;
  1434. //new_pos.x-=cache.item_margin;
  1435. y_ofs += item_h;
  1436. new_pos.y -= item_h;
  1437. }
  1438. if (!p_item->collapsed) { /* if not collapsed, check the children */
  1439. TreeItem *c = p_item->children;
  1440. while (c) {
  1441. int child_h = propagate_mouse_event(new_pos, x_ofs, y_ofs, p_doubleclick, c, p_button, p_mod);
  1442. if (child_h < 0)
  1443. return -1; // break, stop propagating, no need to anymore
  1444. new_pos.y -= child_h;
  1445. y_ofs += child_h;
  1446. c = c->next;
  1447. item_h += child_h;
  1448. }
  1449. }
  1450. }
  1451. return item_h; // nothing found
  1452. }
  1453. void Tree::_text_editor_modal_close() {
  1454. if (Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  1455. Input::get_singleton()->is_key_pressed(KEY_KP_ENTER) ||
  1456. Input::get_singleton()->is_key_pressed(KEY_ENTER)) {
  1457. return;
  1458. }
  1459. if (value_editor->has_point(value_editor->get_local_mouse_position()))
  1460. return;
  1461. text_editor_enter(text_editor->get_text());
  1462. }
  1463. void Tree::text_editor_enter(String p_text) {
  1464. text_editor->hide();
  1465. value_editor->hide();
  1466. if (!popup_edited_item)
  1467. return;
  1468. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1469. return;
  1470. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1471. switch (c.mode) {
  1472. case TreeItem::CELL_MODE_STRING: {
  1473. c.text = p_text;
  1474. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1475. } break;
  1476. case TreeItem::CELL_MODE_RANGE: {
  1477. c.val = p_text.to_double();
  1478. if (c.step > 0)
  1479. c.val = Math::stepify(c.val, c.step);
  1480. if (c.val < c.min)
  1481. c.val = c.min;
  1482. else if (c.val > c.max)
  1483. c.val = c.max;
  1484. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1485. } break;
  1486. default: { ERR_FAIL(); }
  1487. }
  1488. item_edited(popup_edited_item_col, popup_edited_item);
  1489. update();
  1490. }
  1491. void Tree::value_editor_changed(double p_value) {
  1492. if (updating_value_editor) {
  1493. return;
  1494. }
  1495. if (!popup_edited_item) {
  1496. return;
  1497. }
  1498. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1499. c.val = p_value;
  1500. item_edited(popup_edited_item_col, popup_edited_item);
  1501. update();
  1502. }
  1503. void Tree::popup_select(int p_option) {
  1504. if (!popup_edited_item)
  1505. return;
  1506. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1507. return;
  1508. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  1509. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1510. update();
  1511. item_edited(popup_edited_item_col, popup_edited_item);
  1512. }
  1513. void Tree::_go_left() {
  1514. if (selected_col == 0) {
  1515. if (selected_item->get_children() != NULL && !selected_item->is_collapsed()) {
  1516. selected_item->set_collapsed(true);
  1517. } else {
  1518. if (columns.size() == 1) { // goto parent with one column
  1519. TreeItem *parent = selected_item->get_parent();
  1520. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  1521. select_single_item(parent, get_root(), selected_col);
  1522. }
  1523. } else if (selected_item->get_prev_visible()) {
  1524. selected_col = columns.size() - 1;
  1525. _go_up(); // go to upper column if possible
  1526. }
  1527. }
  1528. } else {
  1529. if (select_mode == SELECT_MULTI) {
  1530. selected_col--;
  1531. emit_signal("cell_selected");
  1532. } else {
  1533. selected_item->select(selected_col - 1);
  1534. }
  1535. }
  1536. update();
  1537. accept_event();
  1538. ensure_cursor_is_visible();
  1539. }
  1540. void Tree::_go_right() {
  1541. if (selected_col == (columns.size() - 1)) {
  1542. if (selected_item->get_children() != NULL && selected_item->is_collapsed()) {
  1543. selected_item->set_collapsed(false);
  1544. } else if (selected_item->get_next_visible()) {
  1545. selected_item->select(0);
  1546. _go_down();
  1547. return;
  1548. }
  1549. } else {
  1550. if (select_mode == SELECT_MULTI) {
  1551. selected_col++;
  1552. emit_signal("cell_selected");
  1553. } else {
  1554. selected_item->select(selected_col + 1);
  1555. }
  1556. }
  1557. update();
  1558. ensure_cursor_is_visible();
  1559. accept_event();
  1560. }
  1561. void Tree::_go_up() {
  1562. TreeItem *prev = NULL;
  1563. if (!selected_item) {
  1564. prev = get_last_item();
  1565. selected_col = 0;
  1566. } else {
  1567. prev = selected_item->get_prev_visible();
  1568. if (last_keypress != 0) {
  1569. //incr search next
  1570. int col;
  1571. prev = _search_item_text(prev, incr_search, &col, true, true);
  1572. if (!prev) {
  1573. accept_event();
  1574. return;
  1575. }
  1576. }
  1577. }
  1578. if (select_mode == SELECT_MULTI) {
  1579. if (!prev)
  1580. return;
  1581. selected_item = prev;
  1582. emit_signal("cell_selected");
  1583. update();
  1584. } else {
  1585. int col = selected_col < 0 ? 0 : selected_col;
  1586. while (prev && !prev->cells[col].selectable)
  1587. prev = prev->get_prev_visible();
  1588. if (!prev)
  1589. return; // do nothing..
  1590. prev->select(col);
  1591. }
  1592. ensure_cursor_is_visible();
  1593. accept_event();
  1594. }
  1595. void Tree::_go_down() {
  1596. TreeItem *next = NULL;
  1597. if (!selected_item) {
  1598. next = hide_root ? root->get_next_visible() : root;
  1599. selected_item = 0;
  1600. } else {
  1601. next = selected_item->get_next_visible();
  1602. if (last_keypress != 0) {
  1603. //incr search next
  1604. int col;
  1605. next = _search_item_text(next, incr_search, &col, true);
  1606. if (!next) {
  1607. accept_event();
  1608. return;
  1609. }
  1610. }
  1611. }
  1612. if (select_mode == SELECT_MULTI) {
  1613. if (!next) {
  1614. return;
  1615. }
  1616. selected_item = next;
  1617. emit_signal("cell_selected");
  1618. update();
  1619. } else {
  1620. int col = selected_col < 0 ? 0 : selected_col;
  1621. while (next && !next->cells[col].selectable)
  1622. next = next->get_next_visible();
  1623. if (!next) {
  1624. return; // do nothing..
  1625. }
  1626. next->select(col);
  1627. }
  1628. ensure_cursor_is_visible();
  1629. accept_event();
  1630. }
  1631. void Tree::_gui_input(Ref<InputEvent> p_event) {
  1632. Ref<InputEventKey> k = p_event;
  1633. if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  1634. if (!cursor_can_exit_tree) accept_event();
  1635. if (!selected_item || select_mode == SELECT_ROW || selected_col > (columns.size() - 1)) {
  1636. return;
  1637. }
  1638. if (k.is_valid() && k->get_alt()) {
  1639. selected_item->set_collapsed(false);
  1640. TreeItem *next = selected_item->get_children();
  1641. while (next && next != selected_item->next) {
  1642. next->set_collapsed(false);
  1643. next = next->get_next_visible();
  1644. }
  1645. } else {
  1646. _go_right();
  1647. }
  1648. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  1649. if (!cursor_can_exit_tree) accept_event();
  1650. if (!selected_item || select_mode == SELECT_ROW || selected_col < 0) {
  1651. return;
  1652. }
  1653. if (k.is_valid() && k->get_alt()) {
  1654. selected_item->set_collapsed(true);
  1655. TreeItem *next = selected_item->get_children();
  1656. while (next && next != selected_item->next) {
  1657. next->set_collapsed(true);
  1658. next = next->get_next_visible();
  1659. }
  1660. } else {
  1661. _go_left();
  1662. }
  1663. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !k->get_command()) {
  1664. if (!cursor_can_exit_tree) accept_event();
  1665. _go_up();
  1666. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !k->get_command()) {
  1667. if (!cursor_can_exit_tree) accept_event();
  1668. _go_down();
  1669. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  1670. if (!cursor_can_exit_tree) accept_event();
  1671. TreeItem *next = NULL;
  1672. if (!selected_item)
  1673. return;
  1674. next = selected_item;
  1675. for (int i = 0; i < 10; i++) {
  1676. TreeItem *_n = next->get_next_visible();
  1677. if (_n) {
  1678. next = _n;
  1679. } else {
  1680. return;
  1681. }
  1682. }
  1683. if (next == selected_item)
  1684. return;
  1685. if (select_mode == SELECT_MULTI) {
  1686. selected_item = next;
  1687. emit_signal("cell_selected");
  1688. update();
  1689. } else {
  1690. while (next && !next->cells[selected_col].selectable)
  1691. next = next->get_next_visible();
  1692. if (!next) {
  1693. return; // do nothing..
  1694. }
  1695. next->select(selected_col);
  1696. }
  1697. ensure_cursor_is_visible();
  1698. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  1699. if (!cursor_can_exit_tree) accept_event();
  1700. TreeItem *prev = NULL;
  1701. if (!selected_item)
  1702. return;
  1703. prev = selected_item;
  1704. for (int i = 0; i < 10; i++) {
  1705. TreeItem *_n = prev->get_prev_visible();
  1706. if (_n) {
  1707. prev = _n;
  1708. } else {
  1709. return;
  1710. }
  1711. }
  1712. if (prev == selected_item)
  1713. return;
  1714. if (select_mode == SELECT_MULTI) {
  1715. selected_item = prev;
  1716. emit_signal("cell_selected");
  1717. update();
  1718. } else {
  1719. while (prev && !prev->cells[selected_col].selectable)
  1720. prev = prev->get_prev_visible();
  1721. if (!prev) {
  1722. return; // do nothing..
  1723. }
  1724. prev->select(selected_col);
  1725. }
  1726. ensure_cursor_is_visible();
  1727. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  1728. if (selected_item) {
  1729. //bring up editor if possible
  1730. if (!edit_selected()) {
  1731. emit_signal("item_activated");
  1732. incr_search.clear();
  1733. }
  1734. }
  1735. accept_event();
  1736. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  1737. if (select_mode == SELECT_MULTI) {
  1738. if (!selected_item)
  1739. return;
  1740. if (selected_item->is_selected(selected_col)) {
  1741. selected_item->deselect(selected_col);
  1742. emit_signal("multi_selected", selected_item, selected_col, false);
  1743. } else if (selected_item->is_selectable(selected_col)) {
  1744. selected_item->select(selected_col);
  1745. emit_signal("multi_selected", selected_item, selected_col, true);
  1746. }
  1747. }
  1748. accept_event();
  1749. }
  1750. if (k.is_valid()) { // Incremental search
  1751. if (!k->is_pressed())
  1752. return;
  1753. if (k->get_command() || (k->get_shift() && k->get_unicode() == 0) || k->get_metakey())
  1754. return;
  1755. if (!root)
  1756. return;
  1757. if (hide_root && !root->get_next_visible())
  1758. return;
  1759. if (k->get_unicode() > 0) {
  1760. _do_incr_search(String::chr(k->get_unicode()));
  1761. accept_event();
  1762. return;
  1763. } else {
  1764. if (k->get_scancode() != KEY_SHIFT)
  1765. last_keypress = 0;
  1766. }
  1767. }
  1768. Ref<InputEventMouseMotion> mm = p_event;
  1769. if (mm.is_valid()) {
  1770. if (cache.font.is_null()) // avoid a strange case that may corrupt stuff
  1771. update_cache();
  1772. Ref<StyleBox> bg = cache.bg;
  1773. Point2 pos = mm->get_position() - bg->get_offset();
  1774. Cache::ClickType old_hover = cache.hover_type;
  1775. int old_index = cache.hover_index;
  1776. cache.hover_type = Cache::CLICK_NONE;
  1777. cache.hover_index = 0;
  1778. if (show_column_titles) {
  1779. pos.y -= _get_title_button_height();
  1780. if (pos.y < 0) {
  1781. pos.x += cache.offset.x;
  1782. int len = 0;
  1783. for (int i = 0; i < columns.size(); i++) {
  1784. len += get_column_width(i);
  1785. if (pos.x < len) {
  1786. cache.hover_type = Cache::CLICK_TITLE;
  1787. cache.hover_index = i;
  1788. update();
  1789. break;
  1790. }
  1791. }
  1792. }
  1793. }
  1794. if (root) {
  1795. Point2 mpos = mm->get_position();
  1796. mpos -= cache.bg->get_offset();
  1797. mpos.y -= _get_title_button_height();
  1798. if (mpos.y >= 0) {
  1799. if (h_scroll->is_visible_in_tree())
  1800. mpos.x += h_scroll->get_value();
  1801. if (v_scroll->is_visible_in_tree())
  1802. mpos.y += v_scroll->get_value();
  1803. int col, h, section;
  1804. TreeItem *it = _find_item_at_pos(root, mpos, col, h, section);
  1805. if (drop_mode_flags) {
  1806. if (it != drop_mode_over) {
  1807. drop_mode_over = it;
  1808. update();
  1809. }
  1810. if (it && section != drop_mode_section) {
  1811. drop_mode_section = section;
  1812. update();
  1813. }
  1814. }
  1815. if (it != cache.hover_item) {
  1816. cache.hover_item = it;
  1817. update();
  1818. }
  1819. if (it && col != cache.hover_cell) {
  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)) {
  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. const 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 corrupt 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_internal(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_internal(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_internal(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. const 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.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) {
  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) {
  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. ERR_FAIL_COND_V(cache.font.is_null() || cache.title_button.is_null(), 0);
  2106. return show_column_titles ? cache.font->get_height() + cache.title_button->get_minimum_size().height : 0;
  2107. }
  2108. void Tree::_notification(int p_what) {
  2109. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  2110. focus_in_id = get_tree()->get_event_count();
  2111. }
  2112. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  2113. if (cache.hover_type != Cache::CLICK_NONE) {
  2114. cache.hover_type = Cache::CLICK_NONE;
  2115. update();
  2116. }
  2117. }
  2118. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2119. drag_touching = false;
  2120. }
  2121. if (p_what == NOTIFICATION_ENTER_TREE) {
  2122. update_cache();
  2123. }
  2124. if (p_what == NOTIFICATION_DRAG_END) {
  2125. drop_mode_flags = 0;
  2126. scrolling = false;
  2127. set_physics_process_internal(false);
  2128. update();
  2129. }
  2130. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2131. single_select_defer = NULL;
  2132. if (cache.scroll_speed > 0 && get_rect().has_point(get_viewport()->get_mouse_position() - get_global_position())) {
  2133. scrolling = true;
  2134. set_physics_process_internal(true);
  2135. }
  2136. }
  2137. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  2138. if (drag_touching) {
  2139. if (drag_touching_deaccel) {
  2140. float pos = v_scroll->get_value();
  2141. pos += drag_speed * get_physics_process_delta_time();
  2142. bool turnoff = false;
  2143. if (pos < 0) {
  2144. pos = 0;
  2145. turnoff = true;
  2146. set_physics_process_internal(false);
  2147. drag_touching = false;
  2148. drag_touching_deaccel = false;
  2149. }
  2150. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  2151. pos = v_scroll->get_max() - v_scroll->get_page();
  2152. turnoff = true;
  2153. }
  2154. v_scroll->set_value(pos);
  2155. float sgn = drag_speed < 0 ? -1 : 1;
  2156. float val = Math::abs(drag_speed);
  2157. val -= 1000 * get_physics_process_delta_time();
  2158. if (val < 0) {
  2159. turnoff = true;
  2160. }
  2161. drag_speed = sgn * val;
  2162. if (turnoff) {
  2163. set_physics_process_internal(false);
  2164. drag_touching = false;
  2165. drag_touching_deaccel = false;
  2166. }
  2167. } else {
  2168. }
  2169. }
  2170. if (scrolling) {
  2171. Point2 point = get_viewport()->get_mouse_position() - get_global_position();
  2172. if (point.x < cache.scroll_border) {
  2173. point.x -= cache.scroll_border;
  2174. } else if (point.x > get_size().width - cache.scroll_border) {
  2175. point.x -= get_size().width - cache.scroll_border;
  2176. } else {
  2177. point.x = 0;
  2178. }
  2179. if (point.y < cache.scroll_border) {
  2180. point.y -= cache.scroll_border;
  2181. } else if (point.y > get_size().height - cache.scroll_border) {
  2182. point.y -= get_size().height - cache.scroll_border;
  2183. } else {
  2184. point.y = 0;
  2185. }
  2186. point *= cache.scroll_speed * get_physics_process_delta_time();
  2187. point += get_scroll();
  2188. h_scroll->set_value(point.x);
  2189. v_scroll->set_value(point.y);
  2190. }
  2191. }
  2192. if (p_what == NOTIFICATION_DRAW) {
  2193. update_cache();
  2194. update_scrollbars();
  2195. RID ci = get_canvas_item();
  2196. Ref<StyleBox> bg = cache.bg;
  2197. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  2198. Point2 draw_ofs;
  2199. draw_ofs += bg->get_offset();
  2200. Size2 draw_size = get_size() - bg->get_minimum_size();
  2201. bg->draw(ci, Rect2(Point2(), get_size()));
  2202. if (has_focus()) {
  2203. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  2204. bg_focus->draw(ci, Rect2(Point2(), get_size()));
  2205. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  2206. }
  2207. int tbh = _get_title_button_height();
  2208. draw_ofs.y += tbh;
  2209. draw_size.y -= tbh;
  2210. if (root) {
  2211. draw_item(Point2(), draw_ofs, draw_size, root);
  2212. }
  2213. int ofs = 0;
  2214. for (int i = 0; i < (columns.size() - 1 - 1); i++) {
  2215. ofs += get_column_width(i);
  2216. }
  2217. if (show_column_titles) {
  2218. //title butons
  2219. int ofs = cache.bg->get_margin(MARGIN_LEFT);
  2220. for (int i = 0; i < columns.size(); i++) {
  2221. 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);
  2222. Ref<Font> f = cache.tb_font;
  2223. Rect2 tbrect = Rect2(ofs - cache.offset.x, bg->get_margin(MARGIN_TOP), get_column_width(i), tbh);
  2224. sb->draw(ci, tbrect);
  2225. ofs += tbrect.size.width;
  2226. //text
  2227. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  2228. 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);
  2229. }
  2230. }
  2231. }
  2232. if (p_what == NOTIFICATION_THEME_CHANGED) {
  2233. update_cache();
  2234. }
  2235. }
  2236. Size2 Tree::get_minimum_size() const {
  2237. return Size2(1, 1);
  2238. }
  2239. TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
  2240. ERR_FAIL_COND_V(blocked > 0, NULL);
  2241. TreeItem *ti = NULL;
  2242. if (p_parent) {
  2243. // Append or insert a new item to the given parent.
  2244. ti = memnew(TreeItem(this));
  2245. ERR_FAIL_COND_V(!ti, NULL);
  2246. ti->cells.resize(columns.size());
  2247. TreeItem *prev = NULL;
  2248. TreeItem *c = p_parent->children;
  2249. int idx = 0;
  2250. while (c) {
  2251. if (idx++ == p_idx) {
  2252. ti->next = c;
  2253. break;
  2254. }
  2255. prev = c;
  2256. c = c->next;
  2257. }
  2258. if (prev)
  2259. prev->next = ti;
  2260. else
  2261. p_parent->children = ti;
  2262. ti->parent = p_parent;
  2263. } else {
  2264. if (!root) {
  2265. // No root exists, make the given item the new root.
  2266. ti = memnew(TreeItem(this));
  2267. ERR_FAIL_COND_V(!ti, NULL);
  2268. ti->cells.resize(columns.size());
  2269. root = ti;
  2270. } else {
  2271. // Root exists, append or insert to root.
  2272. ti = create_item(root, p_idx);
  2273. }
  2274. }
  2275. return ti;
  2276. }
  2277. TreeItem *Tree::get_root() {
  2278. return root;
  2279. }
  2280. TreeItem *Tree::get_last_item() {
  2281. TreeItem *last = root;
  2282. while (last) {
  2283. if (last->next)
  2284. last = last->next;
  2285. else if (last->children)
  2286. last = last->children;
  2287. else
  2288. break;
  2289. }
  2290. return last;
  2291. }
  2292. void Tree::item_edited(int p_column, TreeItem *p_item, bool p_lmb) {
  2293. edited_item = p_item;
  2294. edited_col = p_column;
  2295. if (p_lmb)
  2296. emit_signal("item_edited");
  2297. else
  2298. emit_signal("item_rmb_edited");
  2299. }
  2300. void Tree::item_changed(int p_column, TreeItem *p_item) {
  2301. update();
  2302. }
  2303. void Tree::item_selected(int p_column, TreeItem *p_item) {
  2304. if (select_mode == SELECT_MULTI) {
  2305. if (!p_item->cells[p_column].selectable)
  2306. return;
  2307. p_item->cells.write[p_column].selected = true;
  2308. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  2309. selected_col = p_column;
  2310. } else {
  2311. select_single_item(p_item, root, p_column);
  2312. }
  2313. update();
  2314. }
  2315. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  2316. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  2317. p_item->cells.write[p_column].selected = false;
  2318. }
  2319. update();
  2320. }
  2321. void Tree::set_select_mode(SelectMode p_mode) {
  2322. select_mode = p_mode;
  2323. }
  2324. Tree::SelectMode Tree::get_select_mode() const {
  2325. return select_mode;
  2326. }
  2327. void Tree::deselect_all() {
  2328. TreeItem *item = get_next_selected(get_root());
  2329. while (item) {
  2330. item->deselect(selected_col);
  2331. TreeItem *prev_item = item;
  2332. item = get_next_selected(get_root());
  2333. ERR_FAIL_COND(item == prev_item);
  2334. }
  2335. selected_item = NULL;
  2336. selected_col = -1;
  2337. update();
  2338. }
  2339. bool Tree::is_anything_selected() {
  2340. return (selected_item != NULL);
  2341. }
  2342. void Tree::clear() {
  2343. if (blocked > 0) {
  2344. ERR_FAIL_COND(blocked > 0);
  2345. }
  2346. if (pressing_for_editor) {
  2347. if (range_drag_enabled) {
  2348. range_drag_enabled = false;
  2349. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  2350. warp_mouse(range_drag_capture_pos);
  2351. }
  2352. pressing_for_editor = false;
  2353. }
  2354. if (root) {
  2355. memdelete(root);
  2356. root = NULL;
  2357. };
  2358. selected_item = NULL;
  2359. edited_item = NULL;
  2360. popup_edited_item = NULL;
  2361. update();
  2362. };
  2363. void Tree::set_hide_root(bool p_enabled) {
  2364. hide_root = p_enabled;
  2365. update();
  2366. }
  2367. bool Tree::is_root_hidden() const {
  2368. return hide_root;
  2369. }
  2370. void Tree::set_column_min_width(int p_column, int p_min_width) {
  2371. ERR_FAIL_INDEX(p_column, columns.size());
  2372. if (p_min_width < 1)
  2373. return;
  2374. columns.write[p_column].min_width = p_min_width;
  2375. update();
  2376. }
  2377. void Tree::set_column_expand(int p_column, bool p_expand) {
  2378. ERR_FAIL_INDEX(p_column, columns.size());
  2379. columns.write[p_column].expand = p_expand;
  2380. update();
  2381. }
  2382. TreeItem *Tree::get_selected() const {
  2383. return selected_item;
  2384. }
  2385. int Tree::get_selected_column() const {
  2386. return selected_col;
  2387. }
  2388. TreeItem *Tree::get_edited() const {
  2389. return edited_item;
  2390. }
  2391. int Tree::get_edited_column() const {
  2392. return edited_col;
  2393. }
  2394. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  2395. /*
  2396. if (!p_item)
  2397. return NULL;
  2398. */
  2399. if (!root)
  2400. return NULL;
  2401. while (true) {
  2402. if (!p_item) {
  2403. p_item = root;
  2404. } else {
  2405. if (p_item->children) {
  2406. p_item = p_item->children;
  2407. } else if (p_item->next) {
  2408. p_item = p_item->next;
  2409. } else {
  2410. while (!p_item->next) {
  2411. p_item = p_item->parent;
  2412. if (p_item == NULL)
  2413. return NULL;
  2414. }
  2415. p_item = p_item->next;
  2416. }
  2417. }
  2418. for (int i = 0; i < columns.size(); i++)
  2419. if (p_item->cells[i].selected)
  2420. return p_item;
  2421. }
  2422. return NULL;
  2423. }
  2424. int Tree::get_column_width(int p_column) const {
  2425. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  2426. if (!columns[p_column].expand)
  2427. return columns[p_column].min_width;
  2428. Ref<StyleBox> bg = cache.bg;
  2429. int expand_area = get_size().width - (bg->get_margin(MARGIN_LEFT) + bg->get_margin(MARGIN_RIGHT));
  2430. if (v_scroll->is_visible_in_tree())
  2431. expand_area -= v_scroll->get_combined_minimum_size().width;
  2432. int expanding_columns = 0;
  2433. int expanding_total = 0;
  2434. for (int i = 0; i < columns.size(); i++) {
  2435. if (!columns[i].expand) {
  2436. expand_area -= columns[i].min_width;
  2437. } else {
  2438. expanding_total += columns[i].min_width;
  2439. expanding_columns++;
  2440. }
  2441. }
  2442. if (expand_area < expanding_total)
  2443. return columns[p_column].min_width;
  2444. ERR_FAIL_COND_V(expanding_columns == 0, -1); // shouldn't happen
  2445. return expand_area * columns[p_column].min_width / expanding_total;
  2446. }
  2447. void Tree::propagate_set_columns(TreeItem *p_item) {
  2448. p_item->cells.resize(columns.size());
  2449. TreeItem *c = p_item->get_children();
  2450. while (c) {
  2451. propagate_set_columns(c);
  2452. c = c->get_next();
  2453. }
  2454. }
  2455. void Tree::set_columns(int p_columns) {
  2456. ERR_FAIL_COND(p_columns < 1);
  2457. ERR_FAIL_COND(blocked > 0);
  2458. columns.resize(p_columns);
  2459. if (root)
  2460. propagate_set_columns(root);
  2461. if (selected_col >= p_columns)
  2462. selected_col = p_columns - 1;
  2463. update();
  2464. }
  2465. int Tree::get_columns() const {
  2466. return columns.size();
  2467. }
  2468. void Tree::_scroll_moved(float) {
  2469. update();
  2470. }
  2471. Rect2 Tree::get_custom_popup_rect() const {
  2472. return custom_popup_rect;
  2473. }
  2474. int Tree::get_item_offset(TreeItem *p_item) const {
  2475. TreeItem *it = root;
  2476. int ofs = _get_title_button_height();
  2477. if (!it)
  2478. return 0;
  2479. while (true) {
  2480. if (it == p_item)
  2481. return ofs;
  2482. ofs += compute_item_height(it) + cache.vseparation;
  2483. if (it->children && !it->collapsed) {
  2484. it = it->children;
  2485. } else if (it->next) {
  2486. it = it->next;
  2487. } else {
  2488. while (!it->next) {
  2489. it = it->parent;
  2490. if (it == NULL)
  2491. return 0;
  2492. }
  2493. it = it->next;
  2494. }
  2495. }
  2496. return -1; //not found
  2497. }
  2498. void Tree::ensure_cursor_is_visible() {
  2499. if (!is_inside_tree())
  2500. return;
  2501. TreeItem *selected = get_selected();
  2502. if (!selected)
  2503. return;
  2504. int ofs = get_item_offset(selected);
  2505. if (ofs == -1)
  2506. return;
  2507. int h = compute_item_height(selected) + cache.vseparation;
  2508. int screenh = get_size().height - h_scroll->get_combined_minimum_size().height;
  2509. if (ofs + h > v_scroll->get_value() + screenh)
  2510. v_scroll->call_deferred("set_value", ofs - screenh + h);
  2511. else if (ofs < v_scroll->get_value())
  2512. v_scroll->set_value(ofs);
  2513. }
  2514. int Tree::get_pressed_button() const {
  2515. return pressed_button;
  2516. }
  2517. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const {
  2518. ERR_FAIL_NULL_V(p_item, Rect2());
  2519. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  2520. if (p_column != -1) {
  2521. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  2522. }
  2523. int ofs = get_item_offset(p_item);
  2524. int height = compute_item_height(p_item);
  2525. Rect2 r;
  2526. r.position.y = ofs;
  2527. r.size.height = height;
  2528. if (p_column == -1) {
  2529. r.position.x = 0;
  2530. r.size.x = get_size().width;
  2531. } else {
  2532. int accum = 0;
  2533. for (int i = 0; i < p_column; i++) {
  2534. accum += get_column_width(i);
  2535. }
  2536. r.position.x = accum;
  2537. r.size.x = get_column_width(p_column);
  2538. }
  2539. return r;
  2540. }
  2541. void Tree::set_column_titles_visible(bool p_show) {
  2542. show_column_titles = p_show;
  2543. update();
  2544. }
  2545. bool Tree::are_column_titles_visible() const {
  2546. return show_column_titles;
  2547. }
  2548. void Tree::set_column_title(int p_column, const String &p_title) {
  2549. ERR_FAIL_INDEX(p_column, columns.size());
  2550. columns.write[p_column].title = p_title;
  2551. update();
  2552. }
  2553. String Tree::get_column_title(int p_column) const {
  2554. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  2555. return columns[p_column].title;
  2556. }
  2557. Point2 Tree::get_scroll() const {
  2558. Point2 ofs;
  2559. if (h_scroll->is_visible_in_tree())
  2560. ofs.x = h_scroll->get_value();
  2561. if (v_scroll->is_visible_in_tree())
  2562. ofs.y = v_scroll->get_value();
  2563. return ofs;
  2564. }
  2565. void Tree::scroll_to_item(TreeItem *p_item) {
  2566. if (!is_visible_in_tree()) {
  2567. // hack to work around crash in get_item_rect() if Tree is not in tree.
  2568. return;
  2569. }
  2570. // make sure the scrollbar min and max are up to date with latest changes.
  2571. update_scrollbars();
  2572. const Rect2 r = get_item_rect(p_item);
  2573. if (r.position.y < v_scroll->get_value()) {
  2574. v_scroll->set_value(r.position.y);
  2575. } else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) {
  2576. v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y);
  2577. }
  2578. }
  2579. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  2580. while (p_at) {
  2581. for (int i = 0; i < columns.size(); i++) {
  2582. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  2583. if (r_col)
  2584. *r_col = i;
  2585. return p_at;
  2586. }
  2587. }
  2588. if (p_backwards)
  2589. p_at = p_at->get_prev_visible();
  2590. else
  2591. p_at = p_at->get_next_visible();
  2592. }
  2593. return NULL;
  2594. }
  2595. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  2596. if (!root)
  2597. return NULL;
  2598. return _search_item_text(root, p_find, r_col, p_selectable);
  2599. }
  2600. void Tree::_do_incr_search(const String &p_add) {
  2601. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2602. uint64_t diff = time - last_keypress;
  2603. if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000)))
  2604. incr_search = p_add;
  2605. else
  2606. incr_search += p_add;
  2607. last_keypress = time;
  2608. int col;
  2609. TreeItem *item = search_item_text(incr_search, &col, true);
  2610. if (!item)
  2611. return;
  2612. item->select(col);
  2613. ensure_cursor_is_visible();
  2614. }
  2615. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
  2616. Point2 pos = p_pos;
  2617. if (root != p_item || !hide_root) {
  2618. h = compute_item_height(p_item) + cache.vseparation;
  2619. if (pos.y < h) {
  2620. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  2621. section = 0;
  2622. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  2623. section = pos.y < h / 2 ? -1 : 1;
  2624. } else if (pos.y < h / 4) {
  2625. section = -1;
  2626. } else if (pos.y >= (h * 3 / 4)) {
  2627. section = 1;
  2628. } else {
  2629. section = 0;
  2630. }
  2631. for (int i = 0; i < columns.size(); i++) {
  2632. int w = get_column_width(i);
  2633. if (pos.x < w) {
  2634. r_column = i;
  2635. return p_item;
  2636. }
  2637. pos.x -= w;
  2638. }
  2639. return NULL;
  2640. } else {
  2641. pos.y -= h;
  2642. }
  2643. } else {
  2644. h = 0;
  2645. }
  2646. if (p_item->is_collapsed())
  2647. return NULL; // do not try children, it's collapsed
  2648. TreeItem *n = p_item->get_children();
  2649. while (n) {
  2650. int ch;
  2651. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
  2652. pos.y -= ch;
  2653. h += ch;
  2654. if (r)
  2655. return r;
  2656. n = n->get_next();
  2657. }
  2658. return NULL;
  2659. }
  2660. int Tree::get_column_at_position(const Point2 &p_pos) const {
  2661. if (root) {
  2662. Point2 pos = p_pos;
  2663. pos -= cache.bg->get_offset();
  2664. pos.y -= _get_title_button_height();
  2665. if (pos.y < 0)
  2666. return -1;
  2667. if (h_scroll->is_visible_in_tree())
  2668. pos.x += h_scroll->get_value();
  2669. if (v_scroll->is_visible_in_tree())
  2670. pos.y += v_scroll->get_value();
  2671. int col, h, section;
  2672. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2673. if (it) {
  2674. return col;
  2675. }
  2676. }
  2677. return -1;
  2678. }
  2679. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  2680. if (root) {
  2681. Point2 pos = p_pos;
  2682. pos -= cache.bg->get_offset();
  2683. pos.y -= _get_title_button_height();
  2684. if (pos.y < 0)
  2685. return -100;
  2686. if (h_scroll->is_visible_in_tree())
  2687. pos.x += h_scroll->get_value();
  2688. if (v_scroll->is_visible_in_tree())
  2689. pos.y += v_scroll->get_value();
  2690. int col, h, section;
  2691. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2692. if (it) {
  2693. return section;
  2694. }
  2695. }
  2696. return -100;
  2697. }
  2698. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  2699. if (root) {
  2700. Point2 pos = p_pos;
  2701. pos -= cache.bg->get_offset();
  2702. pos.y -= _get_title_button_height();
  2703. if (pos.y < 0)
  2704. return NULL;
  2705. if (h_scroll->is_visible_in_tree())
  2706. pos.x += h_scroll->get_value();
  2707. if (v_scroll->is_visible_in_tree())
  2708. pos.y += v_scroll->get_value();
  2709. int col, h, section;
  2710. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2711. if (it) {
  2712. return it;
  2713. }
  2714. }
  2715. return NULL;
  2716. }
  2717. String Tree::get_tooltip(const Point2 &p_pos) const {
  2718. if (root) {
  2719. Point2 pos = p_pos;
  2720. pos -= cache.bg->get_offset();
  2721. pos.y -= _get_title_button_height();
  2722. if (pos.y < 0)
  2723. return Control::get_tooltip(p_pos);
  2724. if (h_scroll->is_visible_in_tree())
  2725. pos.x += h_scroll->get_value();
  2726. if (v_scroll->is_visible_in_tree())
  2727. pos.y += v_scroll->get_value();
  2728. int col, h, section;
  2729. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2730. if (it) {
  2731. const TreeItem::Cell &c = it->cells[col];
  2732. int col_width = get_column_width(col);
  2733. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  2734. Ref<Texture> b = c.buttons[j].texture;
  2735. Size2 size = b->get_size() + cache.button_pressed->get_minimum_size();
  2736. if (pos.x > col_width - size.width) {
  2737. String tooltip = c.buttons[j].tooltip;
  2738. if (tooltip != "") {
  2739. return tooltip;
  2740. }
  2741. }
  2742. col_width -= size.width;
  2743. }
  2744. String ret;
  2745. if (it->get_tooltip(col) == "")
  2746. ret = it->get_text(col);
  2747. else
  2748. ret = it->get_tooltip(col);
  2749. return ret;
  2750. }
  2751. }
  2752. return Control::get_tooltip(p_pos);
  2753. }
  2754. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2755. cursor_can_exit_tree = p_enable;
  2756. }
  2757. bool Tree::can_cursor_exit_tree() const {
  2758. return cursor_can_exit_tree;
  2759. }
  2760. void Tree::set_hide_folding(bool p_hide) {
  2761. hide_folding = p_hide;
  2762. update();
  2763. }
  2764. bool Tree::is_folding_hidden() const {
  2765. return hide_folding;
  2766. }
  2767. void Tree::set_drop_mode_flags(int p_flags) {
  2768. if (drop_mode_flags == p_flags)
  2769. return;
  2770. drop_mode_flags = p_flags;
  2771. if (drop_mode_flags == 0) {
  2772. drop_mode_over = NULL;
  2773. }
  2774. update();
  2775. }
  2776. int Tree::get_drop_mode_flags() const {
  2777. return drop_mode_flags;
  2778. }
  2779. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  2780. force_edit_checkbox_only_on_checkbox = p_enable;
  2781. }
  2782. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  2783. return force_edit_checkbox_only_on_checkbox;
  2784. }
  2785. void Tree::set_allow_rmb_select(bool p_allow) {
  2786. allow_rmb_select = p_allow;
  2787. }
  2788. bool Tree::get_allow_rmb_select() const {
  2789. return allow_rmb_select;
  2790. }
  2791. void Tree::set_allow_reselect(bool p_allow) {
  2792. allow_reselect = p_allow;
  2793. }
  2794. bool Tree::get_allow_reselect() const {
  2795. return allow_reselect;
  2796. }
  2797. void Tree::_bind_methods() {
  2798. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &Tree::_range_click_timeout);
  2799. ClassDB::bind_method(D_METHOD("_gui_input"), &Tree::_gui_input);
  2800. ClassDB::bind_method(D_METHOD("_popup_select"), &Tree::popup_select);
  2801. ClassDB::bind_method(D_METHOD("_text_editor_enter"), &Tree::text_editor_enter);
  2802. ClassDB::bind_method(D_METHOD("_text_editor_modal_close"), &Tree::_text_editor_modal_close);
  2803. ClassDB::bind_method(D_METHOD("_value_editor_changed"), &Tree::value_editor_changed);
  2804. ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved);
  2805. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  2806. ClassDB::bind_method(D_METHOD("create_item", "parent", "idx"), &Tree::_create_item, DEFVAL(Variant()), DEFVAL(-1));
  2807. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  2808. ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width);
  2809. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  2810. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  2811. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  2812. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  2813. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::_get_next_selected);
  2814. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  2815. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  2816. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  2817. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  2818. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  2819. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  2820. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  2821. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  2822. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  2823. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  2824. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column"), &Tree::_get_item_rect, DEFVAL(-1));
  2825. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  2826. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  2827. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  2828. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  2829. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  2830. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  2831. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  2832. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  2833. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  2834. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  2835. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  2836. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  2837. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  2838. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  2839. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  2840. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  2841. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  2842. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  2843. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  2844. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  2845. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  2846. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  2847. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In between"), "set_drop_mode_flags", "get_drop_mode_flags");
  2848. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  2849. ADD_SIGNAL(MethodInfo("item_selected"));
  2850. ADD_SIGNAL(MethodInfo("cell_selected"));
  2851. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  2852. ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2853. ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2854. ADD_SIGNAL(MethodInfo("item_edited"));
  2855. ADD_SIGNAL(MethodInfo("item_rmb_edited"));
  2856. ADD_SIGNAL(MethodInfo("item_custom_button_pressed"));
  2857. ADD_SIGNAL(MethodInfo("item_double_clicked"));
  2858. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  2859. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2860. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id")));
  2861. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  2862. ADD_SIGNAL(MethodInfo("item_activated"));
  2863. ADD_SIGNAL(MethodInfo("column_title_pressed", PropertyInfo(Variant::INT, "column")));
  2864. ADD_SIGNAL(MethodInfo("nothing_selected"));
  2865. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  2866. BIND_ENUM_CONSTANT(SELECT_ROW);
  2867. BIND_ENUM_CONSTANT(SELECT_MULTI);
  2868. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  2869. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  2870. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  2871. }
  2872. Tree::Tree() {
  2873. selected_col = 0;
  2874. columns.resize(1);
  2875. selected_item = NULL;
  2876. edited_item = NULL;
  2877. selected_col = -1;
  2878. edited_col = -1;
  2879. hide_root = false;
  2880. select_mode = SELECT_SINGLE;
  2881. root = 0;
  2882. popup_menu = NULL;
  2883. popup_edited_item = NULL;
  2884. text_editor = NULL;
  2885. set_focus_mode(FOCUS_ALL);
  2886. popup_menu = memnew(PopupMenu);
  2887. popup_menu->hide();
  2888. add_child(popup_menu);
  2889. popup_menu->set_as_toplevel(true);
  2890. text_editor = memnew(LineEdit);
  2891. add_child(text_editor);
  2892. text_editor->set_as_toplevel(true);
  2893. text_editor->hide();
  2894. value_editor = memnew(HSlider);
  2895. add_child(value_editor);
  2896. value_editor->set_as_toplevel(true);
  2897. value_editor->hide();
  2898. h_scroll = memnew(HScrollBar);
  2899. v_scroll = memnew(VScrollBar);
  2900. add_child(h_scroll);
  2901. add_child(v_scroll);
  2902. range_click_timer = memnew(Timer);
  2903. range_click_timer->connect("timeout", this, "_range_click_timeout");
  2904. add_child(range_click_timer);
  2905. h_scroll->connect("value_changed", this, "_scroll_moved");
  2906. v_scroll->connect("value_changed", this, "_scroll_moved");
  2907. text_editor->connect("text_entered", this, "_text_editor_enter");
  2908. text_editor->connect("modal_closed", this, "_text_editor_modal_close");
  2909. popup_menu->connect("id_pressed", this, "_popup_select");
  2910. value_editor->connect("value_changed", this, "_value_editor_changed");
  2911. value_editor->set_as_toplevel(true);
  2912. text_editor->set_as_toplevel(true);
  2913. updating_value_editor = false;
  2914. pressed_button = -1;
  2915. show_column_titles = false;
  2916. cache.click_type = Cache::CLICK_NONE;
  2917. cache.hover_type = Cache::CLICK_NONE;
  2918. cache.hover_index = -1;
  2919. cache.click_index = -1;
  2920. cache.click_id = -1;
  2921. cache.click_item = NULL;
  2922. cache.click_column = 0;
  2923. cache.hover_cell = -1;
  2924. cache.hover_index = -1;
  2925. last_keypress = 0;
  2926. focus_in_id = 0;
  2927. blocked = 0;
  2928. cursor_can_exit_tree = true;
  2929. set_mouse_filter(MOUSE_FILTER_STOP);
  2930. drag_speed = 0;
  2931. drag_touching = false;
  2932. drag_touching_deaccel = false;
  2933. pressing_for_editor = false;
  2934. range_drag_enabled = false;
  2935. hide_folding = false;
  2936. drop_mode_flags = 0;
  2937. drop_mode_over = NULL;
  2938. drop_mode_section = 0;
  2939. single_select_defer = NULL;
  2940. allow_rmb_select = false;
  2941. force_edit_checkbox_only_on_checkbox = false;
  2942. set_clip_contents(true);
  2943. cache.hover_item = NULL;
  2944. cache.hover_cell = -1;
  2945. allow_reselect = false;
  2946. }
  2947. Tree::~Tree() {
  2948. if (root) {
  2949. memdelete(root);
  2950. }
  2951. }