tree.cpp 97 KB

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