tree.cpp 110 KB

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