tree.cpp 105 KB

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