tree.cpp 90 KB

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