tree.cpp 87 KB

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