tree.cpp 109 KB

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