tree.cpp 125 KB

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