tree.cpp 72 KB

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