tree.cpp 113 KB

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