tree.cpp 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982
  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 || !tree)
  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. tree->emit_signal("item_collapsed", this);
  261. }
  262. bool TreeItem::is_collapsed() {
  263. return collapsed;
  264. }
  265. void TreeItem::set_custom_minimum_height(int p_height) {
  266. custom_min_height = p_height;
  267. _changed_notify();
  268. }
  269. int TreeItem::get_custom_minimum_height() const {
  270. return custom_min_height;
  271. }
  272. TreeItem *TreeItem::get_next() {
  273. return next;
  274. }
  275. TreeItem *TreeItem::get_prev() {
  276. if (!parent || parent->children == this)
  277. return NULL;
  278. TreeItem *prev = parent->children;
  279. while (prev && prev->next != this)
  280. prev = prev->next;
  281. return prev;
  282. }
  283. TreeItem *TreeItem::get_parent() {
  284. return parent;
  285. }
  286. TreeItem *TreeItem::get_children() {
  287. return children;
  288. }
  289. TreeItem *TreeItem::get_prev_visible() {
  290. TreeItem *current = this;
  291. TreeItem *prev = current->get_prev();
  292. if (!prev) {
  293. current = current->parent;
  294. if (!current || (current == tree->root && tree->hide_root))
  295. return NULL;
  296. } else {
  297. current = prev;
  298. while (!current->collapsed && current->children) {
  299. //go to the very end
  300. current = current->children;
  301. while (current->next)
  302. current = current->next;
  303. }
  304. }
  305. return current;
  306. }
  307. TreeItem *TreeItem::get_next_visible() {
  308. TreeItem *current = this;
  309. if (!current->collapsed && current->children) {
  310. current = current->children;
  311. } else if (current->next) {
  312. current = current->next;
  313. } else {
  314. while (current && !current->next) {
  315. current = current->parent;
  316. }
  317. if (current == NULL)
  318. return NULL;
  319. else
  320. current = current->next;
  321. }
  322. return current;
  323. }
  324. void TreeItem::remove_child(TreeItem *p_item) {
  325. ERR_FAIL_NULL(p_item);
  326. TreeItem **c = &children;
  327. while (*c) {
  328. if ((*c) == p_item) {
  329. TreeItem *aux = *c;
  330. *c = (*c)->next;
  331. aux->parent = NULL;
  332. return;
  333. }
  334. c = &(*c)->next;
  335. }
  336. ERR_FAIL();
  337. }
  338. void TreeItem::set_selectable(int p_column, bool p_selectable) {
  339. ERR_FAIL_INDEX(p_column, cells.size());
  340. cells.write[p_column].selectable = p_selectable;
  341. }
  342. bool TreeItem::is_selectable(int p_column) const {
  343. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  344. return cells[p_column].selectable;
  345. }
  346. bool TreeItem::is_selected(int p_column) {
  347. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  348. return cells[p_column].selectable && cells[p_column].selected;
  349. }
  350. void TreeItem::set_as_cursor(int p_column) {
  351. ERR_FAIL_INDEX(p_column, cells.size());
  352. if (!tree)
  353. return;
  354. if (tree->select_mode != Tree::SELECT_MULTI)
  355. return;
  356. tree->selected_item = this;
  357. tree->selected_col = p_column;
  358. tree->update();
  359. }
  360. void TreeItem::select(int p_column) {
  361. ERR_FAIL_INDEX(p_column, cells.size());
  362. _cell_selected(p_column);
  363. }
  364. void TreeItem::deselect(int p_column) {
  365. ERR_FAIL_INDEX(p_column, cells.size());
  366. _cell_deselected(p_column);
  367. }
  368. void TreeItem::add_button(int p_column, const Ref<Texture> &p_button, int p_id, bool p_disabled, const String &p_tooltip) {
  369. ERR_FAIL_INDEX(p_column, cells.size());
  370. ERR_FAIL_COND(!p_button.is_valid());
  371. TreeItem::Cell::Button button;
  372. button.texture = p_button;
  373. if (p_id < 0)
  374. p_id = cells[p_column].buttons.size();
  375. button.id = p_id;
  376. button.disabled = p_disabled;
  377. button.tooltip = p_tooltip;
  378. cells.write[p_column].buttons.push_back(button);
  379. _changed_notify(p_column);
  380. }
  381. int TreeItem::get_button_count(int p_column) const {
  382. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  383. return cells[p_column].buttons.size();
  384. }
  385. Ref<Texture> TreeItem::get_button(int p_column, int p_idx) const {
  386. ERR_FAIL_INDEX_V(p_column, cells.size(), Ref<Texture>());
  387. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), Ref<Texture>());
  388. return cells[p_column].buttons[p_idx].texture;
  389. }
  390. int TreeItem::get_button_id(int p_column, int p_idx) const {
  391. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  392. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), -1);
  393. return cells[p_column].buttons[p_idx].id;
  394. }
  395. void TreeItem::erase_button(int p_column, int p_idx) {
  396. ERR_FAIL_INDEX(p_column, cells.size());
  397. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  398. cells.write[p_column].buttons.remove(p_idx);
  399. _changed_notify(p_column);
  400. }
  401. int TreeItem::get_button_by_id(int p_column, int p_id) const {
  402. ERR_FAIL_INDEX_V(p_column, cells.size(), -1);
  403. for (int i = 0; i < cells[p_column].buttons.size(); i++) {
  404. if (cells[p_column].buttons[i].id == p_id)
  405. return i;
  406. }
  407. return -1;
  408. }
  409. bool TreeItem::is_button_disabled(int p_column, int p_idx) const {
  410. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  411. ERR_FAIL_INDEX_V(p_idx, cells[p_column].buttons.size(), false);
  412. return cells[p_column].buttons[p_idx].disabled;
  413. }
  414. void TreeItem::set_button(int p_column, int p_idx, const Ref<Texture> &p_button) {
  415. ERR_FAIL_COND(p_button.is_null());
  416. ERR_FAIL_INDEX(p_column, cells.size());
  417. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  418. cells.write[p_column].buttons.write[p_idx].texture = p_button;
  419. _changed_notify(p_column);
  420. }
  421. void TreeItem::set_button_color(int p_column, int p_idx, const Color &p_color) {
  422. ERR_FAIL_INDEX(p_column, cells.size());
  423. ERR_FAIL_INDEX(p_idx, cells[p_column].buttons.size());
  424. cells.write[p_column].buttons.write[p_idx].color = p_color;
  425. _changed_notify(p_column);
  426. }
  427. void TreeItem::set_editable(int p_column, bool p_editable) {
  428. ERR_FAIL_INDEX(p_column, cells.size());
  429. cells.write[p_column].editable = p_editable;
  430. _changed_notify(p_column);
  431. }
  432. bool TreeItem::is_editable(int p_column) {
  433. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  434. return cells[p_column].editable;
  435. }
  436. void TreeItem::set_custom_color(int p_column, const Color &p_color) {
  437. ERR_FAIL_INDEX(p_column, cells.size());
  438. cells.write[p_column].custom_color = true;
  439. cells.write[p_column].color = p_color;
  440. _changed_notify(p_column);
  441. }
  442. Color TreeItem::get_custom_color(int p_column) const {
  443. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  444. if (!cells[p_column].custom_color)
  445. return Color();
  446. return cells[p_column].color;
  447. }
  448. void TreeItem::clear_custom_color(int p_column) {
  449. ERR_FAIL_INDEX(p_column, cells.size());
  450. cells.write[p_column].custom_color = false;
  451. cells.write[p_column].color = Color();
  452. _changed_notify(p_column);
  453. }
  454. void TreeItem::set_tooltip(int p_column, const String &p_tooltip) {
  455. ERR_FAIL_INDEX(p_column, cells.size());
  456. cells.write[p_column].tooltip = p_tooltip;
  457. }
  458. String TreeItem::get_tooltip(int p_column) const {
  459. ERR_FAIL_INDEX_V(p_column, cells.size(), "");
  460. return cells[p_column].tooltip;
  461. }
  462. void TreeItem::set_custom_bg_color(int p_column, const Color &p_color, bool p_bg_outline) {
  463. ERR_FAIL_INDEX(p_column, cells.size());
  464. cells.write[p_column].custom_bg_color = true;
  465. cells.write[p_column].custom_bg_outline = p_bg_outline;
  466. cells.write[p_column].bg_color = p_color;
  467. _changed_notify(p_column);
  468. }
  469. void TreeItem::clear_custom_bg_color(int p_column) {
  470. ERR_FAIL_INDEX(p_column, cells.size());
  471. cells.write[p_column].custom_bg_color = false;
  472. cells.write[p_column].bg_color = Color();
  473. _changed_notify(p_column);
  474. }
  475. Color TreeItem::get_custom_bg_color(int p_column) const {
  476. ERR_FAIL_INDEX_V(p_column, cells.size(), Color());
  477. if (!cells[p_column].custom_bg_color)
  478. return Color();
  479. return cells[p_column].bg_color;
  480. }
  481. void TreeItem::set_custom_as_button(int p_column, bool p_button) {
  482. ERR_FAIL_INDEX(p_column, cells.size());
  483. cells.write[p_column].custom_button = p_button;
  484. }
  485. bool TreeItem::is_custom_set_as_button(int p_column) const {
  486. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  487. return cells[p_column].custom_button;
  488. }
  489. void TreeItem::set_text_align(int p_column, TextAlign p_align) {
  490. ERR_FAIL_INDEX(p_column, cells.size());
  491. cells.write[p_column].text_align = p_align;
  492. _changed_notify(p_column);
  493. }
  494. TreeItem::TextAlign TreeItem::get_text_align(int p_column) const {
  495. ERR_FAIL_INDEX_V(p_column, cells.size(), ALIGN_LEFT);
  496. return cells[p_column].text_align;
  497. }
  498. void TreeItem::set_expand_right(int p_column, bool p_enable) {
  499. ERR_FAIL_INDEX(p_column, cells.size());
  500. cells.write[p_column].expand_right = p_enable;
  501. _changed_notify(p_column);
  502. }
  503. bool TreeItem::get_expand_right(int p_column) const {
  504. ERR_FAIL_INDEX_V(p_column, cells.size(), false);
  505. return cells[p_column].expand_right;
  506. }
  507. void TreeItem::set_disable_folding(bool p_disable) {
  508. disable_folding = p_disable;
  509. _changed_notify(0);
  510. }
  511. bool TreeItem::is_folding_disabled() const {
  512. return disable_folding;
  513. }
  514. void TreeItem::_bind_methods() {
  515. ClassDB::bind_method(D_METHOD("set_cell_mode", "column", "mode"), &TreeItem::set_cell_mode);
  516. ClassDB::bind_method(D_METHOD("get_cell_mode", "column"), &TreeItem::get_cell_mode);
  517. ClassDB::bind_method(D_METHOD("set_checked", "column", "checked"), &TreeItem::set_checked);
  518. ClassDB::bind_method(D_METHOD("is_checked", "column"), &TreeItem::is_checked);
  519. ClassDB::bind_method(D_METHOD("set_text", "column", "text"), &TreeItem::set_text);
  520. ClassDB::bind_method(D_METHOD("get_text", "column"), &TreeItem::get_text);
  521. ClassDB::bind_method(D_METHOD("set_icon", "column", "texture"), &TreeItem::set_icon);
  522. ClassDB::bind_method(D_METHOD("get_icon", "column"), &TreeItem::get_icon);
  523. ClassDB::bind_method(D_METHOD("set_icon_region", "column", "region"), &TreeItem::set_icon_region);
  524. ClassDB::bind_method(D_METHOD("get_icon_region", "column"), &TreeItem::get_icon_region);
  525. ClassDB::bind_method(D_METHOD("set_icon_max_width", "column", "width"), &TreeItem::set_icon_max_width);
  526. ClassDB::bind_method(D_METHOD("get_icon_max_width", "column"), &TreeItem::get_icon_max_width);
  527. ClassDB::bind_method(D_METHOD("set_range", "column", "value"), &TreeItem::set_range);
  528. ClassDB::bind_method(D_METHOD("get_range", "column"), &TreeItem::get_range);
  529. ClassDB::bind_method(D_METHOD("set_range_config", "column", "min", "max", "step", "expr"), &TreeItem::set_range_config, DEFVAL(false));
  530. ClassDB::bind_method(D_METHOD("get_range_config", "column"), &TreeItem::_get_range_config);
  531. ClassDB::bind_method(D_METHOD("set_metadata", "column", "meta"), &TreeItem::set_metadata);
  532. ClassDB::bind_method(D_METHOD("get_metadata", "column"), &TreeItem::get_metadata);
  533. ClassDB::bind_method(D_METHOD("set_custom_draw", "column", "object", "callback"), &TreeItem::set_custom_draw);
  534. ClassDB::bind_method(D_METHOD("set_collapsed", "enable"), &TreeItem::set_collapsed);
  535. ClassDB::bind_method(D_METHOD("is_collapsed"), &TreeItem::is_collapsed);
  536. ClassDB::bind_method(D_METHOD("set_custom_minimum_height", "height"), &TreeItem::set_custom_minimum_height);
  537. ClassDB::bind_method(D_METHOD("get_custom_minimum_height"), &TreeItem::get_custom_minimum_height);
  538. ClassDB::bind_method(D_METHOD("get_next"), &TreeItem::get_next);
  539. ClassDB::bind_method(D_METHOD("get_prev"), &TreeItem::get_prev);
  540. ClassDB::bind_method(D_METHOD("get_parent"), &TreeItem::get_parent);
  541. ClassDB::bind_method(D_METHOD("get_children"), &TreeItem::get_children);
  542. ClassDB::bind_method(D_METHOD("get_next_visible"), &TreeItem::get_next_visible);
  543. ClassDB::bind_method(D_METHOD("get_prev_visible"), &TreeItem::get_prev_visible);
  544. ClassDB::bind_method(D_METHOD("remove_child", "child"), &TreeItem::_remove_child);
  545. ClassDB::bind_method(D_METHOD("set_selectable", "column", "selectable"), &TreeItem::set_selectable);
  546. ClassDB::bind_method(D_METHOD("is_selectable", "column"), &TreeItem::is_selectable);
  547. ClassDB::bind_method(D_METHOD("is_selected", "column"), &TreeItem::is_selected);
  548. ClassDB::bind_method(D_METHOD("select", "column"), &TreeItem::select);
  549. ClassDB::bind_method(D_METHOD("deselect", "column"), &TreeItem::deselect);
  550. ClassDB::bind_method(D_METHOD("set_editable", "column", "enabled"), &TreeItem::set_editable);
  551. ClassDB::bind_method(D_METHOD("is_editable", "column"), &TreeItem::is_editable);
  552. ClassDB::bind_method(D_METHOD("set_custom_color", "column", "color"), &TreeItem::set_custom_color);
  553. ClassDB::bind_method(D_METHOD("clear_custom_color", "column"), &TreeItem::clear_custom_color);
  554. ClassDB::bind_method(D_METHOD("set_custom_bg_color", "column", "color", "just_outline"), &TreeItem::set_custom_bg_color, DEFVAL(false));
  555. ClassDB::bind_method(D_METHOD("clear_custom_bg_color", "column"), &TreeItem::clear_custom_bg_color);
  556. ClassDB::bind_method(D_METHOD("get_custom_bg_color", "column"), &TreeItem::get_custom_bg_color);
  557. ClassDB::bind_method(D_METHOD("set_custom_as_button", "column", "enable"), &TreeItem::set_custom_as_button);
  558. ClassDB::bind_method(D_METHOD("is_custom_set_as_button", "column"), &TreeItem::is_custom_set_as_button);
  559. ClassDB::bind_method(D_METHOD("add_button", "column", "button", "button_idx", "disabled", "tooltip"), &TreeItem::add_button, DEFVAL(-1), DEFVAL(false), DEFVAL(""));
  560. ClassDB::bind_method(D_METHOD("get_button_count", "column"), &TreeItem::get_button_count);
  561. ClassDB::bind_method(D_METHOD("get_button", "column", "button_idx"), &TreeItem::get_button);
  562. ClassDB::bind_method(D_METHOD("set_button", "column", "button_idx", "button"), &TreeItem::set_button);
  563. ClassDB::bind_method(D_METHOD("erase_button", "column", "button_idx"), &TreeItem::erase_button);
  564. ClassDB::bind_method(D_METHOD("is_button_disabled", "column", "button_idx"), &TreeItem::is_button_disabled);
  565. ClassDB::bind_method(D_METHOD("set_expand_right", "column", "enable"), &TreeItem::set_expand_right);
  566. ClassDB::bind_method(D_METHOD("get_expand_right", "column"), &TreeItem::get_expand_right);
  567. ClassDB::bind_method(D_METHOD("set_tooltip", "column", "tooltip"), &TreeItem::set_tooltip);
  568. ClassDB::bind_method(D_METHOD("get_tooltip", "column"), &TreeItem::get_tooltip);
  569. ClassDB::bind_method(D_METHOD("set_text_align", "column", "text_align"), &TreeItem::set_text_align);
  570. ClassDB::bind_method(D_METHOD("get_text_align", "column"), &TreeItem::get_text_align);
  571. ClassDB::bind_method(D_METHOD("move_to_top"), &TreeItem::move_to_top);
  572. ClassDB::bind_method(D_METHOD("move_to_bottom"), &TreeItem::move_to_bottom);
  573. ClassDB::bind_method(D_METHOD("set_disable_folding", "disable"), &TreeItem::set_disable_folding);
  574. ClassDB::bind_method(D_METHOD("is_folding_disabled"), &TreeItem::is_folding_disabled);
  575. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collapsed"), "set_collapsed", "is_collapsed");
  576. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_folding"), "set_disable_folding", "is_folding_disabled");
  577. ADD_PROPERTY(PropertyInfo(Variant::INT, "custom_minimum_height", PROPERTY_HINT_RANGE, "0,1000,1"), "set_custom_minimum_height", "get_custom_minimum_height");
  578. BIND_ENUM_CONSTANT(CELL_MODE_STRING);
  579. BIND_ENUM_CONSTANT(CELL_MODE_CHECK);
  580. BIND_ENUM_CONSTANT(CELL_MODE_RANGE);
  581. BIND_ENUM_CONSTANT(CELL_MODE_ICON);
  582. BIND_ENUM_CONSTANT(CELL_MODE_CUSTOM);
  583. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  584. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  585. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  586. }
  587. void TreeItem::clear_children() {
  588. TreeItem *c = children;
  589. while (c) {
  590. TreeItem *aux = c;
  591. c = c->get_next();
  592. aux->parent = 0; // so it won't try to recursively autoremove from me in here
  593. memdelete(aux);
  594. }
  595. children = 0;
  596. };
  597. TreeItem::TreeItem(Tree *p_tree) {
  598. tree = p_tree;
  599. collapsed = false;
  600. disable_folding = false;
  601. custom_min_height = 0;
  602. parent = 0; // parent item
  603. next = 0; // next in list
  604. children = 0; //child items
  605. }
  606. TreeItem::~TreeItem() {
  607. clear_children();
  608. if (parent)
  609. parent->remove_child(this);
  610. if (tree && tree->root == this) {
  611. tree->root = 0;
  612. }
  613. if (tree && tree->popup_edited_item == this) {
  614. tree->popup_edited_item = NULL;
  615. tree->pressing_for_editor = false;
  616. }
  617. if (tree && tree->cache.hover_item == this) {
  618. tree->cache.hover_item = NULL;
  619. }
  620. if (tree && tree->selected_item == this)
  621. tree->selected_item = NULL;
  622. if (tree && tree->drop_mode_over == this)
  623. tree->drop_mode_over = NULL;
  624. if (tree && tree->single_select_defer == this)
  625. tree->single_select_defer = NULL;
  626. if (tree && tree->edited_item == this) {
  627. tree->edited_item = NULL;
  628. tree->pressing_for_editor = false;
  629. }
  630. }
  631. /**********************************************/
  632. /**********************************************/
  633. /**********************************************/
  634. /**********************************************/
  635. /**********************************************/
  636. /**********************************************/
  637. void Tree::update_cache() {
  638. cache.font = get_font("font");
  639. cache.tb_font = get_font("title_button_font");
  640. cache.bg = get_stylebox("bg");
  641. cache.selected = get_stylebox("selected");
  642. cache.selected_focus = get_stylebox("selected_focus");
  643. cache.cursor = get_stylebox("cursor");
  644. cache.cursor_unfocus = get_stylebox("cursor_unfocused");
  645. cache.button_pressed = get_stylebox("button_pressed");
  646. cache.checked = get_icon("checked");
  647. cache.unchecked = get_icon("unchecked");
  648. cache.arrow_collapsed = get_icon("arrow_collapsed");
  649. cache.arrow = get_icon("arrow");
  650. cache.select_arrow = get_icon("select_arrow");
  651. cache.select_option = get_icon("select_option");
  652. cache.updown = get_icon("updown");
  653. cache.custom_button = get_stylebox("custom_button");
  654. cache.custom_button_hover = get_stylebox("custom_button_hover");
  655. cache.custom_button_pressed = get_stylebox("custom_button_pressed");
  656. cache.custom_button_font_highlight = get_color("custom_button_font_highlight");
  657. cache.font_color = get_color("font_color");
  658. cache.font_color_selected = get_color("font_color_selected");
  659. cache.guide_color = get_color("guide_color");
  660. cache.drop_position_color = get_color("drop_position_color");
  661. cache.hseparation = get_constant("hseparation");
  662. cache.vseparation = get_constant("vseparation");
  663. cache.item_margin = get_constant("item_margin");
  664. cache.button_margin = get_constant("button_margin");
  665. cache.guide_width = get_constant("guide_width");
  666. cache.draw_guides = get_constant("draw_guides");
  667. cache.draw_relationship_lines = get_constant("draw_relationship_lines");
  668. cache.relationship_line_color = get_color("relationship_line_color");
  669. cache.scroll_border = get_constant("scroll_border");
  670. cache.scroll_speed = get_constant("scroll_speed");
  671. cache.title_button = get_stylebox("title_button_normal");
  672. cache.title_button_pressed = get_stylebox("title_button_pressed");
  673. cache.title_button_hover = get_stylebox("title_button_hover");
  674. cache.title_button_color = get_color("title_button_color");
  675. v_scroll->set_custom_step(cache.font->get_height());
  676. }
  677. int Tree::compute_item_height(TreeItem *p_item) const {
  678. if (p_item == root && hide_root)
  679. return 0;
  680. ERR_FAIL_COND_V(cache.font.is_null(), 0);
  681. int height = cache.font->get_height();
  682. for (int i = 0; i < columns.size(); i++) {
  683. for (int j = 0; j < p_item->cells[i].buttons.size(); j++) {
  684. Size2i s; // = cache.button_pressed->get_minimum_size();
  685. s += p_item->cells[i].buttons[j].texture->get_size();
  686. if (s.height > height)
  687. height = s.height;
  688. }
  689. switch (p_item->cells[i].mode) {
  690. case TreeItem::CELL_MODE_CHECK: {
  691. int check_icon_h = cache.checked->get_height();
  692. if (height < check_icon_h)
  693. height = check_icon_h;
  694. FALLTHROUGH;
  695. }
  696. case TreeItem::CELL_MODE_STRING:
  697. case TreeItem::CELL_MODE_CUSTOM:
  698. case TreeItem::CELL_MODE_ICON: {
  699. Ref<Texture> icon = p_item->cells[i].icon;
  700. if (!icon.is_null()) {
  701. Size2i s = p_item->cells[i].get_icon_size();
  702. if (p_item->cells[i].icon_max_w > 0 && s.width > p_item->cells[i].icon_max_w) {
  703. s.height = s.height * p_item->cells[i].icon_max_w / s.width;
  704. }
  705. if (s.height > height)
  706. height = s.height;
  707. }
  708. if (p_item->cells[i].mode == TreeItem::CELL_MODE_CUSTOM && p_item->cells[i].custom_button) {
  709. height += cache.custom_button->get_minimum_size().height;
  710. }
  711. } break;
  712. default: {
  713. }
  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. if (p_item == root && p_button == BUTTON_RIGHT) {
  1461. emit_signal("empty_rmb", get_local_mouse_position());
  1462. }
  1463. }
  1464. return item_h; // nothing found
  1465. }
  1466. void Tree::_text_editor_modal_close() {
  1467. if (Input::get_singleton()->is_key_pressed(KEY_ESCAPE) ||
  1468. Input::get_singleton()->is_key_pressed(KEY_KP_ENTER) ||
  1469. Input::get_singleton()->is_key_pressed(KEY_ENTER)) {
  1470. return;
  1471. }
  1472. if (value_editor->has_point(value_editor->get_local_mouse_position()))
  1473. return;
  1474. text_editor_enter(text_editor->get_text());
  1475. }
  1476. void Tree::text_editor_enter(String p_text) {
  1477. text_editor->hide();
  1478. value_editor->hide();
  1479. if (!popup_edited_item)
  1480. return;
  1481. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1482. return;
  1483. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1484. switch (c.mode) {
  1485. case TreeItem::CELL_MODE_STRING: {
  1486. c.text = p_text;
  1487. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1488. } break;
  1489. case TreeItem::CELL_MODE_RANGE: {
  1490. c.val = p_text.to_double();
  1491. if (c.step > 0)
  1492. c.val = Math::stepify(c.val, c.step);
  1493. if (c.val < c.min)
  1494. c.val = c.min;
  1495. else if (c.val > c.max)
  1496. c.val = c.max;
  1497. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1498. } break;
  1499. default: {
  1500. ERR_FAIL();
  1501. }
  1502. }
  1503. item_edited(popup_edited_item_col, popup_edited_item);
  1504. update();
  1505. }
  1506. void Tree::value_editor_changed(double p_value) {
  1507. if (updating_value_editor) {
  1508. return;
  1509. }
  1510. if (!popup_edited_item) {
  1511. return;
  1512. }
  1513. TreeItem::Cell &c = popup_edited_item->cells.write[popup_edited_item_col];
  1514. c.val = p_value;
  1515. item_edited(popup_edited_item_col, popup_edited_item);
  1516. update();
  1517. }
  1518. void Tree::popup_select(int p_option) {
  1519. if (!popup_edited_item)
  1520. return;
  1521. if (popup_edited_item_col < 0 || popup_edited_item_col > columns.size())
  1522. return;
  1523. popup_edited_item->cells.write[popup_edited_item_col].val = p_option;
  1524. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1525. update();
  1526. item_edited(popup_edited_item_col, popup_edited_item);
  1527. }
  1528. void Tree::_go_left() {
  1529. if (selected_col == 0) {
  1530. if (selected_item->get_children() != NULL && !selected_item->is_collapsed()) {
  1531. selected_item->set_collapsed(true);
  1532. } else {
  1533. if (columns.size() == 1) { // goto parent with one column
  1534. TreeItem *parent = selected_item->get_parent();
  1535. if (selected_item != get_root() && parent && parent->is_selectable(selected_col) && !(hide_root && parent == get_root())) {
  1536. select_single_item(parent, get_root(), selected_col);
  1537. }
  1538. } else if (selected_item->get_prev_visible()) {
  1539. selected_col = columns.size() - 1;
  1540. _go_up(); // go to upper column if possible
  1541. }
  1542. }
  1543. } else {
  1544. if (select_mode == SELECT_MULTI) {
  1545. selected_col--;
  1546. emit_signal("cell_selected");
  1547. } else {
  1548. selected_item->select(selected_col - 1);
  1549. }
  1550. }
  1551. update();
  1552. accept_event();
  1553. ensure_cursor_is_visible();
  1554. }
  1555. void Tree::_go_right() {
  1556. if (selected_col == (columns.size() - 1)) {
  1557. if (selected_item->get_children() != NULL && selected_item->is_collapsed()) {
  1558. selected_item->set_collapsed(false);
  1559. } else if (selected_item->get_next_visible()) {
  1560. selected_item->select(0);
  1561. _go_down();
  1562. return;
  1563. }
  1564. } else {
  1565. if (select_mode == SELECT_MULTI) {
  1566. selected_col++;
  1567. emit_signal("cell_selected");
  1568. } else {
  1569. selected_item->select(selected_col + 1);
  1570. }
  1571. }
  1572. update();
  1573. ensure_cursor_is_visible();
  1574. accept_event();
  1575. }
  1576. void Tree::_go_up() {
  1577. TreeItem *prev = NULL;
  1578. if (!selected_item) {
  1579. prev = get_last_item();
  1580. selected_col = 0;
  1581. } else {
  1582. prev = selected_item->get_prev_visible();
  1583. if (last_keypress != 0) {
  1584. //incr search next
  1585. int col;
  1586. prev = _search_item_text(prev, incr_search, &col, true, true);
  1587. if (!prev) {
  1588. accept_event();
  1589. return;
  1590. }
  1591. }
  1592. }
  1593. if (select_mode == SELECT_MULTI) {
  1594. if (!prev)
  1595. return;
  1596. selected_item = prev;
  1597. emit_signal("cell_selected");
  1598. update();
  1599. } else {
  1600. int col = selected_col < 0 ? 0 : selected_col;
  1601. while (prev && !prev->cells[col].selectable)
  1602. prev = prev->get_prev_visible();
  1603. if (!prev)
  1604. return; // do nothing..
  1605. prev->select(col);
  1606. }
  1607. ensure_cursor_is_visible();
  1608. accept_event();
  1609. }
  1610. void Tree::_go_down() {
  1611. TreeItem *next = NULL;
  1612. if (!selected_item) {
  1613. next = hide_root ? root->get_next_visible() : root;
  1614. selected_item = 0;
  1615. } else {
  1616. next = selected_item->get_next_visible();
  1617. if (last_keypress != 0) {
  1618. //incr search next
  1619. int col;
  1620. next = _search_item_text(next, incr_search, &col, true);
  1621. if (!next) {
  1622. accept_event();
  1623. return;
  1624. }
  1625. }
  1626. }
  1627. if (select_mode == SELECT_MULTI) {
  1628. if (!next) {
  1629. return;
  1630. }
  1631. selected_item = next;
  1632. emit_signal("cell_selected");
  1633. update();
  1634. } else {
  1635. int col = selected_col < 0 ? 0 : selected_col;
  1636. while (next && !next->cells[col].selectable)
  1637. next = next->get_next_visible();
  1638. if (!next) {
  1639. return; // do nothing..
  1640. }
  1641. next->select(col);
  1642. }
  1643. ensure_cursor_is_visible();
  1644. accept_event();
  1645. }
  1646. void Tree::_gui_input(Ref<InputEvent> p_event) {
  1647. Ref<InputEventKey> k = p_event;
  1648. bool is_command = k.is_valid() && k->get_command();
  1649. if (p_event->is_action("ui_right") && p_event->is_pressed()) {
  1650. if (!cursor_can_exit_tree) accept_event();
  1651. if (!selected_item || select_mode == SELECT_ROW || selected_col > (columns.size() - 1)) {
  1652. return;
  1653. }
  1654. if (k.is_valid() && k->get_alt()) {
  1655. selected_item->set_collapsed(false);
  1656. TreeItem *next = selected_item->get_children();
  1657. while (next && next != selected_item->next) {
  1658. next->set_collapsed(false);
  1659. next = next->get_next_visible();
  1660. }
  1661. } else {
  1662. _go_right();
  1663. }
  1664. } else if (p_event->is_action("ui_left") && p_event->is_pressed()) {
  1665. if (!cursor_can_exit_tree) accept_event();
  1666. if (!selected_item || select_mode == SELECT_ROW || selected_col < 0) {
  1667. return;
  1668. }
  1669. if (k.is_valid() && k->get_alt()) {
  1670. selected_item->set_collapsed(true);
  1671. TreeItem *next = selected_item->get_children();
  1672. while (next && next != selected_item->next) {
  1673. next->set_collapsed(true);
  1674. next = next->get_next_visible();
  1675. }
  1676. } else {
  1677. _go_left();
  1678. }
  1679. } else if (p_event->is_action("ui_up") && p_event->is_pressed() && !is_command) {
  1680. if (!cursor_can_exit_tree) accept_event();
  1681. _go_up();
  1682. } else if (p_event->is_action("ui_down") && p_event->is_pressed() && !is_command) {
  1683. if (!cursor_can_exit_tree) accept_event();
  1684. _go_down();
  1685. } else if (p_event->is_action("ui_page_down") && p_event->is_pressed()) {
  1686. if (!cursor_can_exit_tree) accept_event();
  1687. TreeItem *next = NULL;
  1688. if (!selected_item)
  1689. return;
  1690. next = selected_item;
  1691. for (int i = 0; i < 10; i++) {
  1692. TreeItem *_n = next->get_next_visible();
  1693. if (_n) {
  1694. next = _n;
  1695. } else {
  1696. break;
  1697. }
  1698. }
  1699. if (next == selected_item)
  1700. return;
  1701. if (select_mode == SELECT_MULTI) {
  1702. selected_item = next;
  1703. emit_signal("cell_selected");
  1704. update();
  1705. } else {
  1706. while (next && !next->cells[selected_col].selectable)
  1707. next = next->get_next_visible();
  1708. if (!next) {
  1709. return; // do nothing..
  1710. }
  1711. next->select(selected_col);
  1712. }
  1713. ensure_cursor_is_visible();
  1714. } else if (p_event->is_action("ui_page_up") && p_event->is_pressed()) {
  1715. if (!cursor_can_exit_tree) accept_event();
  1716. TreeItem *prev = NULL;
  1717. if (!selected_item)
  1718. return;
  1719. prev = selected_item;
  1720. for (int i = 0; i < 10; i++) {
  1721. TreeItem *_n = prev->get_prev_visible();
  1722. if (_n) {
  1723. prev = _n;
  1724. } else {
  1725. break;
  1726. }
  1727. }
  1728. if (prev == selected_item)
  1729. return;
  1730. if (select_mode == SELECT_MULTI) {
  1731. selected_item = prev;
  1732. emit_signal("cell_selected");
  1733. update();
  1734. } else {
  1735. while (prev && !prev->cells[selected_col].selectable)
  1736. prev = prev->get_prev_visible();
  1737. if (!prev) {
  1738. return; // do nothing..
  1739. }
  1740. prev->select(selected_col);
  1741. }
  1742. ensure_cursor_is_visible();
  1743. } else if (p_event->is_action("ui_accept") && p_event->is_pressed()) {
  1744. if (selected_item) {
  1745. //bring up editor if possible
  1746. if (!edit_selected()) {
  1747. emit_signal("item_activated");
  1748. incr_search.clear();
  1749. }
  1750. }
  1751. accept_event();
  1752. } else if (p_event->is_action("ui_select") && p_event->is_pressed()) {
  1753. if (select_mode == SELECT_MULTI) {
  1754. if (!selected_item)
  1755. return;
  1756. if (selected_item->is_selected(selected_col)) {
  1757. selected_item->deselect(selected_col);
  1758. emit_signal("multi_selected", selected_item, selected_col, false);
  1759. } else if (selected_item->is_selectable(selected_col)) {
  1760. selected_item->select(selected_col);
  1761. emit_signal("multi_selected", selected_item, selected_col, true);
  1762. }
  1763. }
  1764. accept_event();
  1765. }
  1766. if (k.is_valid()) { // Incremental search
  1767. if (!k->is_pressed())
  1768. return;
  1769. if (k->get_command() || (k->get_shift() && k->get_unicode() == 0) || k->get_metakey())
  1770. return;
  1771. if (!root)
  1772. return;
  1773. if (hide_root && !root->get_next_visible())
  1774. return;
  1775. if (k->get_unicode() > 0) {
  1776. _do_incr_search(String::chr(k->get_unicode()));
  1777. accept_event();
  1778. return;
  1779. } else {
  1780. if (k->get_scancode() != KEY_SHIFT)
  1781. last_keypress = 0;
  1782. }
  1783. }
  1784. Ref<InputEventMouseMotion> mm = p_event;
  1785. if (mm.is_valid()) {
  1786. if (cache.font.is_null()) // avoid a strange case that may corrupt stuff
  1787. update_cache();
  1788. Ref<StyleBox> bg = cache.bg;
  1789. Point2 pos = mm->get_position() - bg->get_offset();
  1790. Cache::ClickType old_hover = cache.hover_type;
  1791. int old_index = cache.hover_index;
  1792. cache.hover_type = Cache::CLICK_NONE;
  1793. cache.hover_index = 0;
  1794. if (show_column_titles) {
  1795. pos.y -= _get_title_button_height();
  1796. if (pos.y < 0) {
  1797. pos.x += cache.offset.x;
  1798. int len = 0;
  1799. for (int i = 0; i < columns.size(); i++) {
  1800. len += get_column_width(i);
  1801. if (pos.x < len) {
  1802. cache.hover_type = Cache::CLICK_TITLE;
  1803. cache.hover_index = i;
  1804. update();
  1805. break;
  1806. }
  1807. }
  1808. }
  1809. }
  1810. if (root) {
  1811. Point2 mpos = mm->get_position();
  1812. mpos -= cache.bg->get_offset();
  1813. mpos.y -= _get_title_button_height();
  1814. if (mpos.y >= 0) {
  1815. if (h_scroll->is_visible_in_tree())
  1816. mpos.x += h_scroll->get_value();
  1817. if (v_scroll->is_visible_in_tree())
  1818. mpos.y += v_scroll->get_value();
  1819. int col, h, section;
  1820. TreeItem *it = _find_item_at_pos(root, mpos, col, h, section);
  1821. if (drop_mode_flags) {
  1822. if (it != drop_mode_over) {
  1823. drop_mode_over = it;
  1824. update();
  1825. }
  1826. if (it && section != drop_mode_section) {
  1827. drop_mode_section = section;
  1828. update();
  1829. }
  1830. }
  1831. if (it != cache.hover_item) {
  1832. cache.hover_item = it;
  1833. update();
  1834. }
  1835. if (it && col != cache.hover_cell) {
  1836. cache.hover_cell = col;
  1837. update();
  1838. }
  1839. }
  1840. }
  1841. if (cache.hover_type != old_hover || cache.hover_index != old_index) {
  1842. update();
  1843. }
  1844. if (pressing_for_editor && popup_edited_item && (popup_edited_item->get_cell_mode(popup_edited_item_col) == TreeItem::CELL_MODE_RANGE)) {
  1845. //range drag
  1846. if (!range_drag_enabled) {
  1847. Vector2 cpos = mm->get_position();
  1848. if (cpos.distance_to(pressing_pos) > 2) {
  1849. range_drag_enabled = true;
  1850. range_drag_capture_pos = cpos;
  1851. range_drag_base = popup_edited_item->get_range(popup_edited_item_col);
  1852. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1853. }
  1854. } else {
  1855. const TreeItem::Cell &c = popup_edited_item->cells[popup_edited_item_col];
  1856. float diff_y = -mm->get_relative().y;
  1857. diff_y = Math::pow(ABS(diff_y), 1.8f) * SGN(diff_y);
  1858. diff_y *= 0.1;
  1859. range_drag_base = CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  1860. popup_edited_item->set_range(popup_edited_item_col, range_drag_base);
  1861. item_edited(popup_edited_item_col, popup_edited_item);
  1862. }
  1863. }
  1864. if (drag_touching && !drag_touching_deaccel) {
  1865. drag_accum -= mm->get_relative().y;
  1866. v_scroll->set_value(drag_from + drag_accum);
  1867. drag_speed = -mm->get_speed().y;
  1868. }
  1869. }
  1870. Ref<InputEventMouseButton> b = p_event;
  1871. if (b.is_valid()) {
  1872. if (cache.font.is_null()) // avoid a strange case that may corrupt stuff
  1873. update_cache();
  1874. if (!b->is_pressed()) {
  1875. if (b->get_button_index() == BUTTON_LEFT) {
  1876. Point2 pos = b->get_position() - cache.bg->get_offset();
  1877. if (show_column_titles) {
  1878. pos.y -= _get_title_button_height();
  1879. if (pos.y < 0) {
  1880. pos.x += cache.offset.x;
  1881. int len = 0;
  1882. for (int i = 0; i < columns.size(); i++) {
  1883. len += get_column_width(i);
  1884. if (pos.x < len) {
  1885. emit_signal("column_title_pressed", i);
  1886. break;
  1887. }
  1888. }
  1889. }
  1890. }
  1891. if (single_select_defer) {
  1892. select_single_item(single_select_defer, root, single_select_defer_column);
  1893. single_select_defer = NULL;
  1894. }
  1895. range_click_timer->stop();
  1896. if (pressing_for_editor) {
  1897. if (range_drag_enabled) {
  1898. range_drag_enabled = false;
  1899. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1900. warp_mouse(range_drag_capture_pos);
  1901. } else {
  1902. Rect2 rect = get_selected()->get_meta("__focus_rect");
  1903. if (rect.has_point(Point2(b->get_position().x, b->get_position().y))) {
  1904. edit_selected();
  1905. } else {
  1906. emit_signal("item_double_clicked");
  1907. }
  1908. }
  1909. pressing_for_editor = false;
  1910. }
  1911. if (cache.click_type == Cache::CLICK_BUTTON && cache.click_item != NULL) {
  1912. // make sure in case of wrong reference after reconstructing whole TreeItems
  1913. cache.click_item = get_item_at_position(cache.click_pos);
  1914. emit_signal("button_pressed", cache.click_item, cache.click_column, cache.click_id);
  1915. }
  1916. cache.click_type = Cache::CLICK_NONE;
  1917. cache.click_index = -1;
  1918. cache.click_id = -1;
  1919. cache.click_item = NULL;
  1920. cache.click_column = 0;
  1921. if (drag_touching) {
  1922. if (drag_speed == 0) {
  1923. drag_touching_deaccel = false;
  1924. drag_touching = false;
  1925. set_physics_process_internal(false);
  1926. } else {
  1927. drag_touching_deaccel = true;
  1928. }
  1929. }
  1930. update();
  1931. }
  1932. return;
  1933. }
  1934. if (range_drag_enabled)
  1935. return;
  1936. switch (b->get_button_index()) {
  1937. case BUTTON_RIGHT:
  1938. case BUTTON_LEFT: {
  1939. Ref<StyleBox> bg = cache.bg;
  1940. Point2 pos = b->get_position() - bg->get_offset();
  1941. cache.click_type = Cache::CLICK_NONE;
  1942. if (show_column_titles) {
  1943. pos.y -= _get_title_button_height();
  1944. if (pos.y < 0) {
  1945. if (b->get_button_index() == BUTTON_LEFT) {
  1946. pos.x += cache.offset.x;
  1947. int len = 0;
  1948. for (int i = 0; i < columns.size(); i++) {
  1949. len += get_column_width(i);
  1950. if (pos.x < len) {
  1951. cache.click_type = Cache::CLICK_TITLE;
  1952. cache.click_index = i;
  1953. //cache.click_id=;
  1954. update();
  1955. break;
  1956. }
  1957. }
  1958. }
  1959. break;
  1960. }
  1961. }
  1962. if (!root || (!root->get_children() && hide_root)) {
  1963. if (b->get_button_index() == BUTTON_RIGHT && allow_rmb_select) {
  1964. emit_signal("empty_tree_rmb_selected", get_local_mouse_position());
  1965. }
  1966. break;
  1967. }
  1968. click_handled = false;
  1969. pressing_for_editor = false;
  1970. propagate_mouse_activated = false;
  1971. blocked++;
  1972. propagate_mouse_event(pos + cache.offset, 0, 0, b->is_doubleclick(), root, b->get_button_index(), b);
  1973. blocked--;
  1974. if (pressing_for_editor) {
  1975. pressing_pos = b->get_position();
  1976. }
  1977. if (b->get_button_index() == BUTTON_RIGHT)
  1978. break;
  1979. if (drag_touching) {
  1980. set_physics_process_internal(false);
  1981. drag_touching_deaccel = false;
  1982. drag_touching = false;
  1983. drag_speed = 0;
  1984. drag_from = 0;
  1985. }
  1986. if (!click_handled) {
  1987. drag_speed = 0;
  1988. drag_accum = 0;
  1989. //last_drag_accum=0;
  1990. drag_from = v_scroll->get_value();
  1991. drag_touching = OS::get_singleton()->has_touchscreen_ui_hint();
  1992. drag_touching_deaccel = false;
  1993. if (drag_touching) {
  1994. set_physics_process_internal(true);
  1995. }
  1996. if (b->get_button_index() == BUTTON_LEFT) {
  1997. if (get_item_at_position(b->get_position()) == NULL && !b->get_shift() && !b->get_control() && !b->get_command())
  1998. emit_signal("nothing_selected");
  1999. }
  2000. }
  2001. if (propagate_mouse_activated) {
  2002. emit_signal("item_activated");
  2003. propagate_mouse_activated = false;
  2004. }
  2005. } break;
  2006. case BUTTON_WHEEL_UP: {
  2007. v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() * b->get_factor() / 8);
  2008. } break;
  2009. case BUTTON_WHEEL_DOWN: {
  2010. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * b->get_factor() / 8);
  2011. } break;
  2012. }
  2013. }
  2014. Ref<InputEventPanGesture> pan_gesture = p_event;
  2015. if (pan_gesture.is_valid()) {
  2016. v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
  2017. }
  2018. }
  2019. bool Tree::edit_selected() {
  2020. TreeItem *s = get_selected();
  2021. ERR_EXPLAIN("No item selected!");
  2022. ERR_FAIL_COND_V(!s, false);
  2023. ensure_cursor_is_visible();
  2024. int col = get_selected_column();
  2025. ERR_EXPLAIN("No item column selected!");
  2026. ERR_FAIL_INDEX_V(col, columns.size(), false);
  2027. if (!s->cells[col].editable)
  2028. return false;
  2029. Rect2 rect = s->get_meta("__focus_rect");
  2030. popup_edited_item = s;
  2031. popup_edited_item_col = col;
  2032. const TreeItem::Cell &c = s->cells[col];
  2033. if (c.mode == TreeItem::CELL_MODE_CHECK) {
  2034. s->set_checked(col, !c.checked);
  2035. item_edited(col, s);
  2036. return true;
  2037. } else if (c.mode == TreeItem::CELL_MODE_CUSTOM) {
  2038. edited_item = s;
  2039. edited_col = col;
  2040. custom_popup_rect = Rect2i(get_global_position() + rect.position, rect.size);
  2041. emit_signal("custom_popup_edited", false);
  2042. item_edited(col, s);
  2043. return true;
  2044. } else if (c.mode == TreeItem::CELL_MODE_RANGE && c.text != "") {
  2045. popup_menu->clear();
  2046. for (int i = 0; i < c.text.get_slice_count(","); i++) {
  2047. String s2 = c.text.get_slicec(',', i);
  2048. popup_menu->add_item(s2.get_slicec(':', 0), s2.get_slicec(':', 1).empty() ? i : s2.get_slicec(':', 1).to_int());
  2049. }
  2050. popup_menu->set_size(Size2(rect.size.width, 0));
  2051. popup_menu->set_position(get_global_position() + rect.position + Point2i(0, rect.size.height));
  2052. popup_menu->popup();
  2053. popup_edited_item = s;
  2054. popup_edited_item_col = col;
  2055. return true;
  2056. } else if (c.mode == TreeItem::CELL_MODE_STRING || c.mode == TreeItem::CELL_MODE_RANGE) {
  2057. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2058. Point2i textedpos = get_global_position() + rect.position - ofs;
  2059. cache.text_editor_position = textedpos;
  2060. text_editor->set_position(textedpos);
  2061. text_editor->set_size(rect.size);
  2062. text_editor->clear();
  2063. text_editor->set_text(c.mode == TreeItem::CELL_MODE_STRING ? c.text : String::num(c.val, Math::step_decimals(c.step)));
  2064. text_editor->select_all();
  2065. if (c.mode == TreeItem::CELL_MODE_RANGE) {
  2066. value_editor->set_position(textedpos + Point2i(0, text_editor->get_size().height));
  2067. value_editor->set_size(Size2(rect.size.width, 1));
  2068. value_editor->show_modal();
  2069. updating_value_editor = true;
  2070. value_editor->set_min(c.min);
  2071. value_editor->set_max(c.max);
  2072. value_editor->set_step(c.step);
  2073. value_editor->set_value(c.val);
  2074. value_editor->set_exp_ratio(c.expr);
  2075. updating_value_editor = false;
  2076. }
  2077. text_editor->show_modal();
  2078. text_editor->grab_focus();
  2079. return true;
  2080. }
  2081. return false;
  2082. }
  2083. Size2 Tree::get_internal_min_size() const {
  2084. Size2i size = cache.bg->get_offset();
  2085. if (root)
  2086. size.height += get_item_height(root);
  2087. for (int i = 0; i < columns.size(); i++) {
  2088. size.width += columns[i].min_width;
  2089. }
  2090. return size;
  2091. }
  2092. void Tree::update_scrollbars() {
  2093. Size2 size = get_size();
  2094. int tbh;
  2095. if (show_column_titles) {
  2096. tbh = _get_title_button_height();
  2097. } else {
  2098. tbh = 0;
  2099. }
  2100. Size2 hmin = h_scroll->get_combined_minimum_size();
  2101. Size2 vmin = v_scroll->get_combined_minimum_size();
  2102. v_scroll->set_begin(Point2(size.width - vmin.width, cache.bg->get_margin(MARGIN_TOP)));
  2103. v_scroll->set_end(Point2(size.width, size.height - cache.bg->get_margin(MARGIN_TOP) - cache.bg->get_margin(MARGIN_BOTTOM)));
  2104. h_scroll->set_begin(Point2(0, size.height - hmin.height));
  2105. h_scroll->set_end(Point2(size.width - vmin.width, size.height));
  2106. Size2 min = get_internal_min_size();
  2107. if (min.height < size.height - hmin.height) {
  2108. v_scroll->hide();
  2109. cache.offset.y = 0;
  2110. } else {
  2111. v_scroll->show();
  2112. v_scroll->set_max(min.height);
  2113. v_scroll->set_page(size.height - hmin.height - tbh);
  2114. cache.offset.y = v_scroll->get_value();
  2115. }
  2116. if (min.width < size.width - vmin.width) {
  2117. h_scroll->hide();
  2118. cache.offset.x = 0;
  2119. } else {
  2120. h_scroll->show();
  2121. h_scroll->set_max(min.width);
  2122. h_scroll->set_page(size.width - vmin.width);
  2123. cache.offset.x = h_scroll->get_value();
  2124. }
  2125. }
  2126. int Tree::_get_title_button_height() const {
  2127. ERR_FAIL_COND_V(cache.font.is_null() || cache.title_button.is_null(), 0);
  2128. return show_column_titles ? cache.font->get_height() + cache.title_button->get_minimum_size().height : 0;
  2129. }
  2130. void Tree::_notification(int p_what) {
  2131. if (p_what == NOTIFICATION_FOCUS_ENTER) {
  2132. focus_in_id = get_tree()->get_event_count();
  2133. }
  2134. if (p_what == NOTIFICATION_MOUSE_EXIT) {
  2135. if (cache.hover_type != Cache::CLICK_NONE) {
  2136. cache.hover_type = Cache::CLICK_NONE;
  2137. update();
  2138. }
  2139. }
  2140. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  2141. drag_touching = false;
  2142. }
  2143. if (p_what == NOTIFICATION_ENTER_TREE) {
  2144. update_cache();
  2145. }
  2146. if (p_what == NOTIFICATION_DRAG_END) {
  2147. drop_mode_flags = 0;
  2148. scrolling = false;
  2149. set_physics_process_internal(false);
  2150. update();
  2151. }
  2152. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  2153. single_select_defer = NULL;
  2154. if (cache.scroll_speed > 0) {
  2155. scrolling = true;
  2156. set_physics_process_internal(true);
  2157. }
  2158. }
  2159. if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  2160. if (drag_touching) {
  2161. if (drag_touching_deaccel) {
  2162. float pos = v_scroll->get_value();
  2163. pos += drag_speed * get_physics_process_delta_time();
  2164. bool turnoff = false;
  2165. if (pos < 0) {
  2166. pos = 0;
  2167. turnoff = true;
  2168. set_physics_process_internal(false);
  2169. drag_touching = false;
  2170. drag_touching_deaccel = false;
  2171. }
  2172. if (pos > (v_scroll->get_max() - v_scroll->get_page())) {
  2173. pos = v_scroll->get_max() - v_scroll->get_page();
  2174. turnoff = true;
  2175. }
  2176. v_scroll->set_value(pos);
  2177. float sgn = drag_speed < 0 ? -1 : 1;
  2178. float val = Math::abs(drag_speed);
  2179. val -= 1000 * get_physics_process_delta_time();
  2180. if (val < 0) {
  2181. turnoff = true;
  2182. }
  2183. drag_speed = sgn * val;
  2184. if (turnoff) {
  2185. set_physics_process_internal(false);
  2186. drag_touching = false;
  2187. drag_touching_deaccel = false;
  2188. }
  2189. } else {
  2190. }
  2191. }
  2192. Point2 mouse_position = get_viewport()->get_mouse_position() - get_global_position();
  2193. if (scrolling && get_rect().grow(cache.scroll_border).has_point(mouse_position)) {
  2194. Point2 point;
  2195. if ((ABS(mouse_position.x) < ABS(mouse_position.x - get_size().width)) && (ABS(mouse_position.x) < cache.scroll_border)) {
  2196. point.x = mouse_position.x - cache.scroll_border;
  2197. } else if (ABS(mouse_position.x - get_size().width) < cache.scroll_border) {
  2198. point.x = mouse_position.x - (get_size().width - cache.scroll_border);
  2199. }
  2200. if ((ABS(mouse_position.y) < ABS(mouse_position.y - get_size().height)) && (ABS(mouse_position.y) < cache.scroll_border)) {
  2201. point.y = mouse_position.y - cache.scroll_border;
  2202. } else if (ABS(mouse_position.y - get_size().height) < cache.scroll_border) {
  2203. point.y = mouse_position.y - (get_size().height - cache.scroll_border);
  2204. }
  2205. point *= cache.scroll_speed * get_physics_process_delta_time();
  2206. point += get_scroll();
  2207. h_scroll->set_value(point.x);
  2208. v_scroll->set_value(point.y);
  2209. }
  2210. }
  2211. if (p_what == NOTIFICATION_DRAW) {
  2212. update_cache();
  2213. update_scrollbars();
  2214. RID ci = get_canvas_item();
  2215. Ref<StyleBox> bg = cache.bg;
  2216. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  2217. Point2 draw_ofs;
  2218. draw_ofs += bg->get_offset();
  2219. Size2 draw_size = get_size() - bg->get_minimum_size();
  2220. bg->draw(ci, Rect2(Point2(), get_size()));
  2221. if (has_focus()) {
  2222. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
  2223. bg_focus->draw(ci, Rect2(Point2(), get_size()));
  2224. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
  2225. }
  2226. int tbh = _get_title_button_height();
  2227. draw_ofs.y += tbh;
  2228. draw_size.y -= tbh;
  2229. if (root) {
  2230. draw_item(Point2(), draw_ofs, draw_size, root);
  2231. }
  2232. int ofs = 0;
  2233. for (int i = 0; i < (columns.size() - 1 - 1); i++) {
  2234. ofs += get_column_width(i);
  2235. }
  2236. if (show_column_titles) {
  2237. //title buttons
  2238. int ofs2 = cache.bg->get_margin(MARGIN_LEFT);
  2239. for (int i = 0; i < columns.size(); i++) {
  2240. 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);
  2241. Ref<Font> f = cache.tb_font;
  2242. Rect2 tbrect = Rect2(ofs2 - cache.offset.x, bg->get_margin(MARGIN_TOP), get_column_width(i), tbh);
  2243. sb->draw(ci, tbrect);
  2244. ofs2 += tbrect.size.width;
  2245. //text
  2246. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  2247. 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);
  2248. }
  2249. }
  2250. }
  2251. if (p_what == NOTIFICATION_THEME_CHANGED) {
  2252. update_cache();
  2253. }
  2254. if (p_what == NOTIFICATION_RESIZED || p_what == NOTIFICATION_TRANSFORM_CHANGED) {
  2255. if (popup_edited_item != NULL) {
  2256. Rect2 rect = popup_edited_item->get_meta("__focus_rect");
  2257. Vector2 ofs(0, (text_editor->get_size().height - rect.size.height) / 2);
  2258. Point2i textedpos = get_global_position() + rect.position - ofs;
  2259. if (cache.text_editor_position != textedpos) {
  2260. cache.text_editor_position = textedpos;
  2261. text_editor->set_position(textedpos);
  2262. value_editor->set_position(textedpos + Point2i(0, text_editor->get_size().height));
  2263. }
  2264. }
  2265. }
  2266. }
  2267. Size2 Tree::get_minimum_size() const {
  2268. return Size2(1, 1);
  2269. }
  2270. TreeItem *Tree::create_item(TreeItem *p_parent, int p_idx) {
  2271. ERR_FAIL_COND_V(blocked > 0, NULL);
  2272. TreeItem *ti = NULL;
  2273. if (p_parent) {
  2274. // Append or insert a new item to the given parent.
  2275. ti = memnew(TreeItem(this));
  2276. ERR_FAIL_COND_V(!ti, NULL);
  2277. ti->cells.resize(columns.size());
  2278. TreeItem *prev = NULL;
  2279. TreeItem *c = p_parent->children;
  2280. int idx = 0;
  2281. while (c) {
  2282. if (idx++ == p_idx) {
  2283. ti->next = c;
  2284. break;
  2285. }
  2286. prev = c;
  2287. c = c->next;
  2288. }
  2289. if (prev)
  2290. prev->next = ti;
  2291. else
  2292. p_parent->children = ti;
  2293. ti->parent = p_parent;
  2294. } else {
  2295. if (!root) {
  2296. // No root exists, make the given item the new root.
  2297. ti = memnew(TreeItem(this));
  2298. ERR_FAIL_COND_V(!ti, NULL);
  2299. ti->cells.resize(columns.size());
  2300. root = ti;
  2301. } else {
  2302. // Root exists, append or insert to root.
  2303. ti = create_item(root, p_idx);
  2304. }
  2305. }
  2306. return ti;
  2307. }
  2308. TreeItem *Tree::get_root() {
  2309. return root;
  2310. }
  2311. TreeItem *Tree::get_last_item() {
  2312. TreeItem *last = root;
  2313. while (last) {
  2314. if (last->next)
  2315. last = last->next;
  2316. else if (last->children)
  2317. last = last->children;
  2318. else
  2319. break;
  2320. }
  2321. return last;
  2322. }
  2323. void Tree::item_edited(int p_column, TreeItem *p_item, bool p_lmb) {
  2324. edited_item = p_item;
  2325. edited_col = p_column;
  2326. if (p_lmb)
  2327. emit_signal("item_edited");
  2328. else
  2329. emit_signal("item_rmb_edited");
  2330. }
  2331. void Tree::item_changed(int p_column, TreeItem *p_item) {
  2332. update();
  2333. }
  2334. void Tree::item_selected(int p_column, TreeItem *p_item) {
  2335. if (select_mode == SELECT_MULTI) {
  2336. if (!p_item->cells[p_column].selectable)
  2337. return;
  2338. p_item->cells.write[p_column].selected = true;
  2339. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  2340. selected_col = p_column;
  2341. } else {
  2342. select_single_item(p_item, root, p_column);
  2343. }
  2344. update();
  2345. }
  2346. void Tree::item_deselected(int p_column, TreeItem *p_item) {
  2347. if (select_mode == SELECT_MULTI || select_mode == SELECT_SINGLE) {
  2348. p_item->cells.write[p_column].selected = false;
  2349. }
  2350. update();
  2351. }
  2352. void Tree::set_select_mode(SelectMode p_mode) {
  2353. select_mode = p_mode;
  2354. }
  2355. Tree::SelectMode Tree::get_select_mode() const {
  2356. return select_mode;
  2357. }
  2358. void Tree::deselect_all() {
  2359. TreeItem *item = get_next_selected(get_root());
  2360. while (item) {
  2361. item->deselect(selected_col);
  2362. TreeItem *prev_item = item;
  2363. item = get_next_selected(get_root());
  2364. ERR_FAIL_COND(item == prev_item);
  2365. }
  2366. selected_item = NULL;
  2367. selected_col = -1;
  2368. update();
  2369. }
  2370. bool Tree::is_anything_selected() {
  2371. return (selected_item != NULL);
  2372. }
  2373. void Tree::clear() {
  2374. if (blocked > 0) {
  2375. ERR_FAIL_COND(blocked > 0);
  2376. }
  2377. if (pressing_for_editor) {
  2378. if (range_drag_enabled) {
  2379. range_drag_enabled = false;
  2380. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  2381. warp_mouse(range_drag_capture_pos);
  2382. }
  2383. pressing_for_editor = false;
  2384. }
  2385. if (root) {
  2386. memdelete(root);
  2387. root = NULL;
  2388. };
  2389. selected_item = NULL;
  2390. edited_item = NULL;
  2391. popup_edited_item = NULL;
  2392. update();
  2393. };
  2394. void Tree::set_hide_root(bool p_enabled) {
  2395. hide_root = p_enabled;
  2396. update();
  2397. }
  2398. bool Tree::is_root_hidden() const {
  2399. return hide_root;
  2400. }
  2401. void Tree::set_column_min_width(int p_column, int p_min_width) {
  2402. ERR_FAIL_INDEX(p_column, columns.size());
  2403. if (p_min_width < 1)
  2404. return;
  2405. columns.write[p_column].min_width = p_min_width;
  2406. update();
  2407. }
  2408. void Tree::set_column_expand(int p_column, bool p_expand) {
  2409. ERR_FAIL_INDEX(p_column, columns.size());
  2410. columns.write[p_column].expand = p_expand;
  2411. update();
  2412. }
  2413. TreeItem *Tree::get_selected() const {
  2414. return selected_item;
  2415. }
  2416. int Tree::get_selected_column() const {
  2417. return selected_col;
  2418. }
  2419. TreeItem *Tree::get_edited() const {
  2420. return edited_item;
  2421. }
  2422. int Tree::get_edited_column() const {
  2423. return edited_col;
  2424. }
  2425. TreeItem *Tree::get_next_selected(TreeItem *p_item) {
  2426. /*
  2427. if (!p_item)
  2428. return NULL;
  2429. */
  2430. if (!root)
  2431. return NULL;
  2432. while (true) {
  2433. if (!p_item) {
  2434. p_item = root;
  2435. } else {
  2436. if (p_item->children) {
  2437. p_item = p_item->children;
  2438. } else if (p_item->next) {
  2439. p_item = p_item->next;
  2440. } else {
  2441. while (!p_item->next) {
  2442. p_item = p_item->parent;
  2443. if (p_item == NULL)
  2444. return NULL;
  2445. }
  2446. p_item = p_item->next;
  2447. }
  2448. }
  2449. for (int i = 0; i < columns.size(); i++)
  2450. if (p_item->cells[i].selected)
  2451. return p_item;
  2452. }
  2453. return NULL;
  2454. }
  2455. int Tree::get_column_width(int p_column) const {
  2456. ERR_FAIL_INDEX_V(p_column, columns.size(), -1);
  2457. if (!columns[p_column].expand)
  2458. return columns[p_column].min_width;
  2459. Ref<StyleBox> bg = cache.bg;
  2460. int expand_area = get_size().width - (bg->get_margin(MARGIN_LEFT) + bg->get_margin(MARGIN_RIGHT));
  2461. if (v_scroll->is_visible_in_tree())
  2462. expand_area -= v_scroll->get_combined_minimum_size().width;
  2463. int expanding_columns = 0;
  2464. int expanding_total = 0;
  2465. for (int i = 0; i < columns.size(); i++) {
  2466. if (!columns[i].expand) {
  2467. expand_area -= columns[i].min_width;
  2468. } else {
  2469. expanding_total += columns[i].min_width;
  2470. expanding_columns++;
  2471. }
  2472. }
  2473. if (expand_area < expanding_total)
  2474. return columns[p_column].min_width;
  2475. ERR_FAIL_COND_V(expanding_columns == 0, -1); // shouldn't happen
  2476. return expand_area * columns[p_column].min_width / expanding_total;
  2477. }
  2478. void Tree::propagate_set_columns(TreeItem *p_item) {
  2479. p_item->cells.resize(columns.size());
  2480. TreeItem *c = p_item->get_children();
  2481. while (c) {
  2482. propagate_set_columns(c);
  2483. c = c->get_next();
  2484. }
  2485. }
  2486. void Tree::set_columns(int p_columns) {
  2487. ERR_FAIL_COND(p_columns < 1);
  2488. ERR_FAIL_COND(blocked > 0);
  2489. columns.resize(p_columns);
  2490. if (root)
  2491. propagate_set_columns(root);
  2492. if (selected_col >= p_columns)
  2493. selected_col = p_columns - 1;
  2494. update();
  2495. }
  2496. int Tree::get_columns() const {
  2497. return columns.size();
  2498. }
  2499. void Tree::_scroll_moved(float) {
  2500. update();
  2501. }
  2502. Rect2 Tree::get_custom_popup_rect() const {
  2503. return custom_popup_rect;
  2504. }
  2505. int Tree::get_item_offset(TreeItem *p_item) const {
  2506. TreeItem *it = root;
  2507. int ofs = _get_title_button_height();
  2508. if (!it)
  2509. return 0;
  2510. while (true) {
  2511. if (it == p_item)
  2512. return ofs;
  2513. ofs += compute_item_height(it) + cache.vseparation;
  2514. if (it->children && !it->collapsed) {
  2515. it = it->children;
  2516. } else if (it->next) {
  2517. it = it->next;
  2518. } else {
  2519. while (!it->next) {
  2520. it = it->parent;
  2521. if (it == NULL)
  2522. return 0;
  2523. }
  2524. it = it->next;
  2525. }
  2526. }
  2527. return -1; //not found
  2528. }
  2529. void Tree::ensure_cursor_is_visible() {
  2530. if (!is_inside_tree())
  2531. return;
  2532. TreeItem *selected = get_selected();
  2533. if (!selected)
  2534. return;
  2535. int ofs = get_item_offset(selected);
  2536. if (ofs == -1)
  2537. return;
  2538. int h = compute_item_height(selected) + cache.vseparation;
  2539. int screenh = get_size().height - h_scroll->get_combined_minimum_size().height;
  2540. if (h > screenh) { //screen size is too small, maybe it was not resized yet.
  2541. v_scroll->set_value(ofs);
  2542. } else if (ofs + h > v_scroll->get_value() + screenh) {
  2543. v_scroll->call_deferred("set_value", ofs - screenh + h);
  2544. } else if (ofs < v_scroll->get_value()) {
  2545. v_scroll->set_value(ofs);
  2546. }
  2547. }
  2548. int Tree::get_pressed_button() const {
  2549. return pressed_button;
  2550. }
  2551. Rect2 Tree::get_item_rect(TreeItem *p_item, int p_column) const {
  2552. ERR_FAIL_NULL_V(p_item, Rect2());
  2553. ERR_FAIL_COND_V(p_item->tree != this, Rect2());
  2554. if (p_column != -1) {
  2555. ERR_FAIL_INDEX_V(p_column, columns.size(), Rect2());
  2556. }
  2557. int ofs = get_item_offset(p_item);
  2558. int height = compute_item_height(p_item);
  2559. Rect2 r;
  2560. r.position.y = ofs;
  2561. r.size.height = height;
  2562. if (p_column == -1) {
  2563. r.position.x = 0;
  2564. r.size.x = get_size().width;
  2565. } else {
  2566. int accum = 0;
  2567. for (int i = 0; i < p_column; i++) {
  2568. accum += get_column_width(i);
  2569. }
  2570. r.position.x = accum;
  2571. r.size.x = get_column_width(p_column);
  2572. }
  2573. return r;
  2574. }
  2575. void Tree::set_column_titles_visible(bool p_show) {
  2576. show_column_titles = p_show;
  2577. update();
  2578. }
  2579. bool Tree::are_column_titles_visible() const {
  2580. return show_column_titles;
  2581. }
  2582. void Tree::set_column_title(int p_column, const String &p_title) {
  2583. ERR_FAIL_INDEX(p_column, columns.size());
  2584. columns.write[p_column].title = p_title;
  2585. update();
  2586. }
  2587. String Tree::get_column_title(int p_column) const {
  2588. ERR_FAIL_INDEX_V(p_column, columns.size(), "");
  2589. return columns[p_column].title;
  2590. }
  2591. Point2 Tree::get_scroll() const {
  2592. Point2 ofs;
  2593. if (h_scroll->is_visible_in_tree())
  2594. ofs.x = h_scroll->get_value();
  2595. if (v_scroll->is_visible_in_tree())
  2596. ofs.y = v_scroll->get_value();
  2597. return ofs;
  2598. }
  2599. void Tree::scroll_to_item(TreeItem *p_item) {
  2600. if (!is_visible_in_tree()) {
  2601. // hack to work around crash in get_item_rect() if Tree is not in tree.
  2602. return;
  2603. }
  2604. // make sure the scrollbar min and max are up to date with latest changes.
  2605. update_scrollbars();
  2606. const Rect2 r = get_item_rect(p_item);
  2607. if (r.position.y < v_scroll->get_value()) {
  2608. v_scroll->set_value(r.position.y);
  2609. } else if (r.position.y + r.size.y + 2 * cache.vseparation > v_scroll->get_value() + get_size().y) {
  2610. v_scroll->set_value(r.position.y + r.size.y + 2 * cache.vseparation - get_size().y);
  2611. }
  2612. }
  2613. TreeItem *Tree::_search_item_text(TreeItem *p_at, const String &p_find, int *r_col, bool p_selectable, bool p_backwards) {
  2614. while (p_at) {
  2615. for (int i = 0; i < columns.size(); i++) {
  2616. if (p_at->get_text(i).findn(p_find) == 0 && (!p_selectable || p_at->is_selectable(i))) {
  2617. if (r_col)
  2618. *r_col = i;
  2619. return p_at;
  2620. }
  2621. }
  2622. if (p_backwards)
  2623. p_at = p_at->get_prev_visible();
  2624. else
  2625. p_at = p_at->get_next_visible();
  2626. }
  2627. return NULL;
  2628. }
  2629. TreeItem *Tree::search_item_text(const String &p_find, int *r_col, bool p_selectable) {
  2630. if (!root)
  2631. return NULL;
  2632. return _search_item_text(root, p_find, r_col, p_selectable);
  2633. }
  2634. void Tree::_do_incr_search(const String &p_add) {
  2635. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2636. uint64_t diff = time - last_keypress;
  2637. if (diff > uint64_t(GLOBAL_DEF("gui/timers/incremental_search_max_interval_msec", 2000)))
  2638. incr_search = p_add;
  2639. else
  2640. incr_search += p_add;
  2641. last_keypress = time;
  2642. int col;
  2643. TreeItem *item = search_item_text(incr_search, &col, true);
  2644. if (!item)
  2645. return;
  2646. item->select(col);
  2647. ensure_cursor_is_visible();
  2648. }
  2649. TreeItem *Tree::_find_item_at_pos(TreeItem *p_item, const Point2 &p_pos, int &r_column, int &h, int &section) const {
  2650. Point2 pos = p_pos;
  2651. if (root != p_item || !hide_root) {
  2652. h = compute_item_height(p_item) + cache.vseparation;
  2653. if (pos.y < h) {
  2654. if (drop_mode_flags == DROP_MODE_ON_ITEM) {
  2655. section = 0;
  2656. } else if (drop_mode_flags == DROP_MODE_INBETWEEN) {
  2657. section = pos.y < h / 2 ? -1 : 1;
  2658. } else if (pos.y < h / 4) {
  2659. section = -1;
  2660. } else if (pos.y >= (h * 3 / 4)) {
  2661. section = 1;
  2662. } else {
  2663. section = 0;
  2664. }
  2665. for (int i = 0; i < columns.size(); i++) {
  2666. int w = get_column_width(i);
  2667. if (pos.x < w) {
  2668. r_column = i;
  2669. return p_item;
  2670. }
  2671. pos.x -= w;
  2672. }
  2673. return NULL;
  2674. } else {
  2675. pos.y -= h;
  2676. }
  2677. } else {
  2678. h = 0;
  2679. }
  2680. if (p_item->is_collapsed())
  2681. return NULL; // do not try children, it's collapsed
  2682. TreeItem *n = p_item->get_children();
  2683. while (n) {
  2684. int ch;
  2685. TreeItem *r = _find_item_at_pos(n, pos, r_column, ch, section);
  2686. pos.y -= ch;
  2687. h += ch;
  2688. if (r)
  2689. return r;
  2690. n = n->get_next();
  2691. }
  2692. return NULL;
  2693. }
  2694. int Tree::get_column_at_position(const Point2 &p_pos) const {
  2695. if (root) {
  2696. Point2 pos = p_pos;
  2697. pos -= cache.bg->get_offset();
  2698. pos.y -= _get_title_button_height();
  2699. if (pos.y < 0)
  2700. return -1;
  2701. if (h_scroll->is_visible_in_tree())
  2702. pos.x += h_scroll->get_value();
  2703. if (v_scroll->is_visible_in_tree())
  2704. pos.y += v_scroll->get_value();
  2705. int col, h, section;
  2706. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2707. if (it) {
  2708. return col;
  2709. }
  2710. }
  2711. return -1;
  2712. }
  2713. int Tree::get_drop_section_at_position(const Point2 &p_pos) const {
  2714. if (root) {
  2715. Point2 pos = p_pos;
  2716. pos -= cache.bg->get_offset();
  2717. pos.y -= _get_title_button_height();
  2718. if (pos.y < 0)
  2719. return -100;
  2720. if (h_scroll->is_visible_in_tree())
  2721. pos.x += h_scroll->get_value();
  2722. if (v_scroll->is_visible_in_tree())
  2723. pos.y += v_scroll->get_value();
  2724. int col, h, section;
  2725. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2726. if (it) {
  2727. return section;
  2728. }
  2729. }
  2730. return -100;
  2731. }
  2732. TreeItem *Tree::get_item_at_position(const Point2 &p_pos) const {
  2733. if (root) {
  2734. Point2 pos = p_pos;
  2735. pos -= cache.bg->get_offset();
  2736. pos.y -= _get_title_button_height();
  2737. if (pos.y < 0)
  2738. return NULL;
  2739. if (h_scroll->is_visible_in_tree())
  2740. pos.x += h_scroll->get_value();
  2741. if (v_scroll->is_visible_in_tree())
  2742. pos.y += v_scroll->get_value();
  2743. int col, h, section;
  2744. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2745. if (it) {
  2746. return it;
  2747. }
  2748. }
  2749. return NULL;
  2750. }
  2751. String Tree::get_tooltip(const Point2 &p_pos) const {
  2752. if (root) {
  2753. Point2 pos = p_pos;
  2754. pos -= cache.bg->get_offset();
  2755. pos.y -= _get_title_button_height();
  2756. if (pos.y < 0)
  2757. return Control::get_tooltip(p_pos);
  2758. if (h_scroll->is_visible_in_tree())
  2759. pos.x += h_scroll->get_value();
  2760. if (v_scroll->is_visible_in_tree())
  2761. pos.y += v_scroll->get_value();
  2762. int col, h, section;
  2763. TreeItem *it = _find_item_at_pos(root, pos, col, h, section);
  2764. if (it) {
  2765. const TreeItem::Cell &c = it->cells[col];
  2766. int col_width = get_column_width(col);
  2767. for (int j = c.buttons.size() - 1; j >= 0; j--) {
  2768. Ref<Texture> b = c.buttons[j].texture;
  2769. Size2 size = b->get_size() + cache.button_pressed->get_minimum_size();
  2770. if (pos.x > col_width - size.width) {
  2771. String tooltip = c.buttons[j].tooltip;
  2772. if (tooltip != "") {
  2773. return tooltip;
  2774. }
  2775. }
  2776. col_width -= size.width;
  2777. }
  2778. String ret;
  2779. if (it->get_tooltip(col) == "")
  2780. ret = it->get_text(col);
  2781. else
  2782. ret = it->get_tooltip(col);
  2783. return ret;
  2784. }
  2785. }
  2786. return Control::get_tooltip(p_pos);
  2787. }
  2788. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2789. cursor_can_exit_tree = p_enable;
  2790. }
  2791. bool Tree::can_cursor_exit_tree() const {
  2792. return cursor_can_exit_tree;
  2793. }
  2794. void Tree::set_hide_folding(bool p_hide) {
  2795. hide_folding = p_hide;
  2796. update();
  2797. }
  2798. bool Tree::is_folding_hidden() const {
  2799. return hide_folding;
  2800. }
  2801. void Tree::set_drop_mode_flags(int p_flags) {
  2802. if (drop_mode_flags == p_flags)
  2803. return;
  2804. drop_mode_flags = p_flags;
  2805. if (drop_mode_flags == 0) {
  2806. drop_mode_over = NULL;
  2807. }
  2808. update();
  2809. }
  2810. int Tree::get_drop_mode_flags() const {
  2811. return drop_mode_flags;
  2812. }
  2813. void Tree::set_edit_checkbox_cell_only_when_checkbox_is_pressed(bool p_enable) {
  2814. force_edit_checkbox_only_on_checkbox = p_enable;
  2815. }
  2816. bool Tree::get_edit_checkbox_cell_only_when_checkbox_is_pressed() const {
  2817. return force_edit_checkbox_only_on_checkbox;
  2818. }
  2819. void Tree::set_allow_rmb_select(bool p_allow) {
  2820. allow_rmb_select = p_allow;
  2821. }
  2822. bool Tree::get_allow_rmb_select() const {
  2823. return allow_rmb_select;
  2824. }
  2825. void Tree::set_allow_reselect(bool p_allow) {
  2826. allow_reselect = p_allow;
  2827. }
  2828. bool Tree::get_allow_reselect() const {
  2829. return allow_reselect;
  2830. }
  2831. void Tree::_bind_methods() {
  2832. ClassDB::bind_method(D_METHOD("_range_click_timeout"), &Tree::_range_click_timeout);
  2833. ClassDB::bind_method(D_METHOD("_gui_input"), &Tree::_gui_input);
  2834. ClassDB::bind_method(D_METHOD("_popup_select"), &Tree::popup_select);
  2835. ClassDB::bind_method(D_METHOD("_text_editor_enter"), &Tree::text_editor_enter);
  2836. ClassDB::bind_method(D_METHOD("_text_editor_modal_close"), &Tree::_text_editor_modal_close);
  2837. ClassDB::bind_method(D_METHOD("_value_editor_changed"), &Tree::value_editor_changed);
  2838. ClassDB::bind_method(D_METHOD("_scroll_moved"), &Tree::_scroll_moved);
  2839. ClassDB::bind_method(D_METHOD("clear"), &Tree::clear);
  2840. ClassDB::bind_method(D_METHOD("create_item", "parent", "idx"), &Tree::_create_item, DEFVAL(Variant()), DEFVAL(-1));
  2841. ClassDB::bind_method(D_METHOD("get_root"), &Tree::get_root);
  2842. ClassDB::bind_method(D_METHOD("set_column_min_width", "column", "min_width"), &Tree::set_column_min_width);
  2843. ClassDB::bind_method(D_METHOD("set_column_expand", "column", "expand"), &Tree::set_column_expand);
  2844. ClassDB::bind_method(D_METHOD("get_column_width", "column"), &Tree::get_column_width);
  2845. ClassDB::bind_method(D_METHOD("set_hide_root", "enable"), &Tree::set_hide_root);
  2846. ClassDB::bind_method(D_METHOD("is_root_hidden"), &Tree::is_root_hidden);
  2847. ClassDB::bind_method(D_METHOD("get_next_selected", "from"), &Tree::_get_next_selected);
  2848. ClassDB::bind_method(D_METHOD("get_selected"), &Tree::get_selected);
  2849. ClassDB::bind_method(D_METHOD("get_selected_column"), &Tree::get_selected_column);
  2850. ClassDB::bind_method(D_METHOD("get_pressed_button"), &Tree::get_pressed_button);
  2851. ClassDB::bind_method(D_METHOD("set_select_mode", "mode"), &Tree::set_select_mode);
  2852. ClassDB::bind_method(D_METHOD("get_select_mode"), &Tree::get_select_mode);
  2853. ClassDB::bind_method(D_METHOD("set_columns", "amount"), &Tree::set_columns);
  2854. ClassDB::bind_method(D_METHOD("get_columns"), &Tree::get_columns);
  2855. ClassDB::bind_method(D_METHOD("get_edited"), &Tree::get_edited);
  2856. ClassDB::bind_method(D_METHOD("get_edited_column"), &Tree::get_edited_column);
  2857. ClassDB::bind_method(D_METHOD("get_custom_popup_rect"), &Tree::get_custom_popup_rect);
  2858. ClassDB::bind_method(D_METHOD("get_item_area_rect", "item", "column"), &Tree::_get_item_rect, DEFVAL(-1));
  2859. ClassDB::bind_method(D_METHOD("get_item_at_position", "position"), &Tree::get_item_at_position);
  2860. ClassDB::bind_method(D_METHOD("get_column_at_position", "position"), &Tree::get_column_at_position);
  2861. ClassDB::bind_method(D_METHOD("get_drop_section_at_position", "position"), &Tree::get_drop_section_at_position);
  2862. ClassDB::bind_method(D_METHOD("ensure_cursor_is_visible"), &Tree::ensure_cursor_is_visible);
  2863. ClassDB::bind_method(D_METHOD("set_column_titles_visible", "visible"), &Tree::set_column_titles_visible);
  2864. ClassDB::bind_method(D_METHOD("are_column_titles_visible"), &Tree::are_column_titles_visible);
  2865. ClassDB::bind_method(D_METHOD("set_column_title", "column", "title"), &Tree::set_column_title);
  2866. ClassDB::bind_method(D_METHOD("get_column_title", "column"), &Tree::get_column_title);
  2867. ClassDB::bind_method(D_METHOD("get_scroll"), &Tree::get_scroll);
  2868. ClassDB::bind_method(D_METHOD("set_hide_folding", "hide"), &Tree::set_hide_folding);
  2869. ClassDB::bind_method(D_METHOD("is_folding_hidden"), &Tree::is_folding_hidden);
  2870. ClassDB::bind_method(D_METHOD("set_drop_mode_flags", "flags"), &Tree::set_drop_mode_flags);
  2871. ClassDB::bind_method(D_METHOD("get_drop_mode_flags"), &Tree::get_drop_mode_flags);
  2872. ClassDB::bind_method(D_METHOD("set_allow_rmb_select", "allow"), &Tree::set_allow_rmb_select);
  2873. ClassDB::bind_method(D_METHOD("get_allow_rmb_select"), &Tree::get_allow_rmb_select);
  2874. ClassDB::bind_method(D_METHOD("set_allow_reselect", "allow"), &Tree::set_allow_reselect);
  2875. ClassDB::bind_method(D_METHOD("get_allow_reselect"), &Tree::get_allow_reselect);
  2876. ADD_PROPERTY(PropertyInfo(Variant::INT, "columns"), "set_columns", "get_columns");
  2877. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_reselect"), "set_allow_reselect", "get_allow_reselect");
  2878. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_rmb_select"), "set_allow_rmb_select", "get_allow_rmb_select");
  2879. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_folding"), "set_hide_folding", "is_folding_hidden");
  2880. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hide_root"), "set_hide_root", "is_root_hidden");
  2881. ADD_PROPERTY(PropertyInfo(Variant::INT, "drop_mode_flags", PROPERTY_HINT_FLAGS, "On Item,In between"), "set_drop_mode_flags", "get_drop_mode_flags");
  2882. ADD_PROPERTY(PropertyInfo(Variant::INT, "select_mode", PROPERTY_HINT_ENUM, "Single,Row,Multi"), "set_select_mode", "get_select_mode");
  2883. ADD_SIGNAL(MethodInfo("item_selected"));
  2884. ADD_SIGNAL(MethodInfo("cell_selected"));
  2885. ADD_SIGNAL(MethodInfo("multi_selected", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::BOOL, "selected")));
  2886. ADD_SIGNAL(MethodInfo("item_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2887. ADD_SIGNAL(MethodInfo("empty_rmb", PropertyInfo(Variant::VECTOR2, "position")));
  2888. ADD_SIGNAL(MethodInfo("empty_tree_rmb_selected", PropertyInfo(Variant::VECTOR2, "position")));
  2889. ADD_SIGNAL(MethodInfo("item_edited"));
  2890. ADD_SIGNAL(MethodInfo("item_rmb_edited"));
  2891. ADD_SIGNAL(MethodInfo("item_custom_button_pressed"));
  2892. ADD_SIGNAL(MethodInfo("item_double_clicked"));
  2893. ADD_SIGNAL(MethodInfo("item_collapsed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem")));
  2894. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2895. ADD_SIGNAL(MethodInfo("button_pressed", PropertyInfo(Variant::OBJECT, "item", PROPERTY_HINT_RESOURCE_TYPE, "TreeItem"), PropertyInfo(Variant::INT, "column"), PropertyInfo(Variant::INT, "id")));
  2896. ADD_SIGNAL(MethodInfo("custom_popup_edited", PropertyInfo(Variant::BOOL, "arrow_clicked")));
  2897. ADD_SIGNAL(MethodInfo("item_activated"));
  2898. ADD_SIGNAL(MethodInfo("column_title_pressed", PropertyInfo(Variant::INT, "column")));
  2899. ADD_SIGNAL(MethodInfo("nothing_selected"));
  2900. BIND_ENUM_CONSTANT(SELECT_SINGLE);
  2901. BIND_ENUM_CONSTANT(SELECT_ROW);
  2902. BIND_ENUM_CONSTANT(SELECT_MULTI);
  2903. BIND_ENUM_CONSTANT(DROP_MODE_DISABLED);
  2904. BIND_ENUM_CONSTANT(DROP_MODE_ON_ITEM);
  2905. BIND_ENUM_CONSTANT(DROP_MODE_INBETWEEN);
  2906. }
  2907. Tree::Tree() {
  2908. selected_col = 0;
  2909. columns.resize(1);
  2910. selected_item = NULL;
  2911. edited_item = NULL;
  2912. selected_col = -1;
  2913. edited_col = -1;
  2914. hide_root = false;
  2915. select_mode = SELECT_SINGLE;
  2916. root = 0;
  2917. popup_menu = NULL;
  2918. popup_edited_item = NULL;
  2919. text_editor = NULL;
  2920. set_focus_mode(FOCUS_ALL);
  2921. popup_menu = memnew(PopupMenu);
  2922. popup_menu->hide();
  2923. add_child(popup_menu);
  2924. popup_menu->set_as_toplevel(true);
  2925. text_editor = memnew(LineEdit);
  2926. add_child(text_editor);
  2927. text_editor->set_as_toplevel(true);
  2928. text_editor->hide();
  2929. value_editor = memnew(HSlider);
  2930. add_child(value_editor);
  2931. value_editor->set_as_toplevel(true);
  2932. value_editor->hide();
  2933. h_scroll = memnew(HScrollBar);
  2934. v_scroll = memnew(VScrollBar);
  2935. add_child(h_scroll);
  2936. add_child(v_scroll);
  2937. range_click_timer = memnew(Timer);
  2938. range_click_timer->connect("timeout", this, "_range_click_timeout");
  2939. add_child(range_click_timer);
  2940. h_scroll->connect("value_changed", this, "_scroll_moved");
  2941. v_scroll->connect("value_changed", this, "_scroll_moved");
  2942. text_editor->connect("text_entered", this, "_text_editor_enter");
  2943. text_editor->connect("modal_closed", this, "_text_editor_modal_close");
  2944. popup_menu->connect("id_pressed", this, "_popup_select");
  2945. value_editor->connect("value_changed", this, "_value_editor_changed");
  2946. value_editor->set_as_toplevel(true);
  2947. text_editor->set_as_toplevel(true);
  2948. set_notify_transform(true);
  2949. updating_value_editor = false;
  2950. pressed_button = -1;
  2951. show_column_titles = false;
  2952. cache.click_type = Cache::CLICK_NONE;
  2953. cache.hover_type = Cache::CLICK_NONE;
  2954. cache.hover_index = -1;
  2955. cache.click_index = -1;
  2956. cache.click_id = -1;
  2957. cache.click_item = NULL;
  2958. cache.click_column = 0;
  2959. cache.hover_cell = -1;
  2960. last_keypress = 0;
  2961. focus_in_id = 0;
  2962. blocked = 0;
  2963. cursor_can_exit_tree = true;
  2964. set_mouse_filter(MOUSE_FILTER_STOP);
  2965. drag_speed = 0;
  2966. drag_touching = false;
  2967. drag_touching_deaccel = false;
  2968. pressing_for_editor = false;
  2969. range_drag_enabled = false;
  2970. hide_folding = false;
  2971. drop_mode_flags = 0;
  2972. drop_mode_over = NULL;
  2973. drop_mode_section = 0;
  2974. single_select_defer = NULL;
  2975. allow_rmb_select = false;
  2976. force_edit_checkbox_only_on_checkbox = false;
  2977. set_clip_contents(true);
  2978. cache.hover_item = NULL;
  2979. cache.hover_cell = -1;
  2980. allow_reselect = false;
  2981. propagate_mouse_activated = false;
  2982. }
  2983. Tree::~Tree() {
  2984. if (root) {
  2985. memdelete(root);
  2986. }
  2987. }