tree.cpp 73 KB

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