tree.cpp 104 KB

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