tree.cpp 99 KB

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