tree.cpp 111 KB

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