tree.cpp 87 KB

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