tree.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330
  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","button_idx"),&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 (!hide_folding && 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 + (hide_folding?cache.hseparation: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. Rect2 r=cell_rect;
  718. r.pos.x-=cache.hseparation;
  719. r.size.x+=cache.hseparation;
  720. VisualServer::get_singleton()->canvas_item_add_rect(ci,r,p_item->cells[i].bg_color);
  721. }
  722. 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");
  723. Point2i text_pos=item_rect.pos;
  724. text_pos.y+=Math::floor((item_rect.size.y-font->get_height())/2) + font_ascent;
  725. switch (p_item->cells[i].mode) {
  726. case TreeItem::CELL_MODE_STRING: {
  727. draw_item_rect(p_item->cells[i],item_rect,col);
  728. } break;
  729. case TreeItem::CELL_MODE_CHECK: {
  730. Ref<Texture> checked = cache.checked;
  731. Ref<Texture> unchecked = cache.unchecked;
  732. Point2i check_ofs=item_rect.pos;
  733. check_ofs.y+=Math::floor((item_rect.size.y-checked->get_height())/2);
  734. if (p_item->cells[i].checked) {
  735. checked->draw( ci, check_ofs );
  736. } else {
  737. unchecked->draw( ci, check_ofs );
  738. }
  739. int check_w = checked->get_width()+cache.hseparation;
  740. text_pos.x+=check_w;
  741. item_rect.size.x-=check_w;
  742. item_rect.pos.x+=check_w;
  743. draw_item_rect(p_item->cells[i],item_rect,col);
  744. //font->draw( ci, text_pos, p_item->cells[i].text, col,item_rect.size.x-check_w );
  745. } break;
  746. case TreeItem::CELL_MODE_RANGE: {
  747. if (p_item->cells[i].text!="") {
  748. if (!p_item->cells[i].editable)
  749. break;
  750. int option = (int)p_item->cells[i].val;
  751. String s = p_item->cells[i].text;
  752. s=s.get_slicec(',',option);
  753. Ref<Texture> downarrow = cache.select_arrow;
  754. font->draw(ci, text_pos, s, col,item_rect.size.x-downarrow->get_width() );
  755. //?
  756. Point2i arrow_pos=item_rect.pos;
  757. arrow_pos.x+=item_rect.size.x-downarrow->get_width();
  758. arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0);
  759. downarrow->draw( ci, arrow_pos );
  760. } else {
  761. Ref<Texture> updown = cache.updown;
  762. String valtext = String::num( p_item->cells[i].val, Math::decimals( p_item->cells[i].step ) );
  763. font->draw( ci, text_pos, valtext, col, item_rect.size.x-updown->get_width());
  764. if (!p_item->cells[i].editable)
  765. break;
  766. Point2i updown_pos=item_rect.pos;
  767. updown_pos.x+=item_rect.size.x-updown->get_width();
  768. updown_pos.y+=Math::floor(((item_rect.size.y-updown->get_height()))/2.0);
  769. updown->draw( ci, updown_pos );
  770. }
  771. } break;
  772. case TreeItem::CELL_MODE_ICON: {
  773. if (p_item->cells[i].icon.is_null())
  774. break;
  775. Size2i icon_size = p_item->cells[i].get_icon_size();
  776. if (p_item->cells[i].icon_max_w>0 && icon_size.width >p_item->cells[i].icon_max_w) {
  777. icon_size.height = icon_size.height * p_item->cells[i].icon_max_w / icon_size.width;
  778. icon_size.width=p_item->cells[i].icon_max_w;
  779. }
  780. Point2i icon_ofs = (item_rect.size-icon_size)/2;
  781. icon_ofs+=item_rect.pos;
  782. draw_texture_rect(p_item->cells[i].icon,Rect2(icon_ofs,icon_size));;
  783. //p_item->cells[i].icon->draw(ci, icon_ofs);
  784. } break;
  785. case TreeItem::CELL_MODE_CUSTOM: {
  786. // int option = (int)p_item->cells[i].val;
  787. if (p_item->cells[i].custom_draw_obj) {
  788. Object* cdo = ObjectDB::get_instance(p_item->cells[i].custom_draw_obj);
  789. if (cdo)
  790. cdo->call(p_item->cells[i].custom_draw_callback,p_item,Rect2(item_rect));
  791. }
  792. if (!p_item->cells[i].editable) {
  793. draw_item_rect(p_item->cells[i],item_rect,col);
  794. break;
  795. }
  796. Ref<Texture> downarrow = cache.select_arrow;
  797. Rect2i ir=item_rect;
  798. ir.size.width-=downarrow->get_width();
  799. draw_item_rect(p_item->cells[i],ir,col);
  800. Point2i arrow_pos=item_rect.pos;
  801. arrow_pos.x+=item_rect.size.x-downarrow->get_width();
  802. arrow_pos.y+=Math::floor(((item_rect.size.y-downarrow->get_height()))/2.0);
  803. downarrow->draw( ci, arrow_pos );
  804. } break;
  805. }
  806. if (i==0) {
  807. ofs=get_column_width(0);
  808. } else {
  809. ofs+=w+bw;
  810. }
  811. if (select_mode==SELECT_MULTI && selected_item==p_item && selected_col==i) {
  812. if (has_focus())
  813. cache.cursor->draw(ci,cell_rect);
  814. else
  815. cache.cursor_unfocus->draw(ci,cell_rect);
  816. }
  817. }
  818. //separator
  819. //get_painter()->draw_fill_rect( Point2i(0,pos.y),Size2i(get_size().width,1),color( COLOR_TREE_GRID) );
  820. //pos=p_pos; //reset pos
  821. }
  822. Point2 children_pos=p_pos;
  823. if (!skip) {
  824. children_pos.x+=cache.item_margin;
  825. htotal+=label_h;
  826. children_pos.y+=htotal;
  827. }
  828. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  829. TreeItem *c=p_item->childs;
  830. while (c) {
  831. int child_h=draw_item(children_pos, p_draw_ofs, p_draw_size, c );
  832. if (child_h<0)
  833. return -1; // break, stop drawing, no need to anymore
  834. htotal+=child_h;
  835. children_pos.y+=child_h;
  836. c=c->next;
  837. }
  838. }
  839. return htotal;
  840. }
  841. void Tree::select_single_item(TreeItem *p_selected,TreeItem *p_current,int p_col,TreeItem *p_prev,bool *r_in_range) {
  842. TreeItem::Cell &selected_cell=p_selected->cells[p_col];
  843. bool switched=false;
  844. if (r_in_range && !*r_in_range && (p_current==p_selected || p_current==p_prev)) {
  845. *r_in_range=true;
  846. switched=true;
  847. }
  848. for (int i=0;i<columns.size();i++) {
  849. TreeItem::Cell &c=p_current->cells[i];
  850. if (!c.selectable)
  851. continue;
  852. if (select_mode==SELECT_ROW) {
  853. if (p_selected==p_current) {
  854. if (!c.selected) {
  855. c.selected=true;
  856. selected_item=p_selected;
  857. selected_col=0;
  858. selected_item=p_selected;
  859. emit_signal("item_selected");
  860. //if (p_col==i)
  861. // p_current->selected_signal.call(p_col);
  862. }
  863. } else {
  864. if (c.selected) {
  865. c.selected=false;
  866. //p_current->deselected_signal.call(p_col);
  867. }
  868. }
  869. } else if (select_mode==SELECT_SINGLE || select_mode==SELECT_MULTI) {
  870. if (!r_in_range && &selected_cell==&c) {
  871. if (!selected_cell.selected) {
  872. selected_cell.selected=true;
  873. selected_item=p_selected;
  874. selected_col=i;
  875. emit_signal("cell_selected");
  876. if (select_mode==SELECT_MULTI)
  877. emit_signal("multi_selected",p_current,i,true);
  878. } else if (select_mode==SELECT_MULTI && (selected_item!=p_selected || selected_col!=i)) {
  879. selected_item=p_selected;
  880. selected_col=i;
  881. emit_signal("cell_selected");
  882. }
  883. } else {
  884. if (r_in_range && *r_in_range) {
  885. if (!c.selected && c.selectable) {
  886. c.selected=true;
  887. emit_signal("multi_selected",p_current,i,true);
  888. }
  889. } else if (!r_in_range){
  890. if (select_mode==SELECT_MULTI && c.selected)
  891. emit_signal("multi_selected",p_current,i,false);
  892. c.selected=false;
  893. }
  894. //p_current->deselected_signal.call(p_col);
  895. }
  896. }
  897. }
  898. if (!switched && r_in_range && *r_in_range && (p_current==p_selected || p_current==p_prev)) {
  899. *r_in_range=false;
  900. }
  901. TreeItem *c=p_current->childs;
  902. while (c) {
  903. select_single_item(p_selected,c,p_col,p_prev,r_in_range);
  904. c=c->next;
  905. }
  906. }
  907. Rect2 Tree::search_item_rect(TreeItem *p_from, TreeItem *p_item) {
  908. return Rect2();
  909. }
  910. void Tree::_range_click_timeout() {
  911. if (range_item_last && !range_drag_enabled && Input::get_singleton()->is_mouse_button_pressed(BUTTON_LEFT)) {
  912. Point2 pos = (Input::get_singleton()->get_mouse_pos()-get_global_pos())-cache.bg->get_offset();
  913. if (show_column_titles) {
  914. pos.y-=_get_title_button_height();
  915. if (pos.y<0) {
  916. range_click_timer->stop();
  917. return;
  918. }
  919. }
  920. click_handled=false;
  921. InputModifierState mod = {}; // should be irrelevant..
  922. blocked++;
  923. propagate_mouse_event(pos+cache.offset, 0, 0, false, root, BUTTON_LEFT, mod);
  924. blocked--;
  925. if (range_click_timer->is_one_shot()) {
  926. range_click_timer->set_wait_time(0.05);
  927. range_click_timer->set_one_shot(false);
  928. range_click_timer->start();
  929. }
  930. if (!click_handled)
  931. range_click_timer->stop();
  932. } else {
  933. range_click_timer->stop();
  934. }
  935. }
  936. 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) {
  937. int item_h=compute_item_height( p_item )+cache.vseparation;
  938. bool skip=(p_item==root && hide_root);
  939. if (!skip && p_pos.y<item_h) {
  940. // check event!
  941. if (!hide_folding && (p_pos.x >=x_ofs && p_pos.x < (x_ofs+cache.item_margin) )) {
  942. if (p_item->childs)
  943. p_item->set_collapsed( ! p_item->is_collapsed() );
  944. return -1; //handled!
  945. }
  946. int x=p_pos.x;
  947. /* find clicked column */
  948. int col=-1;
  949. int col_ofs=0;
  950. int col_width=0;
  951. for (int i=0;i<columns.size();i++) {
  952. col_width=get_column_width(i);
  953. if (x>col_width) {
  954. col_ofs+=col_width;
  955. x-=col_width;
  956. continue;
  957. }
  958. col=i;
  959. break;
  960. }
  961. if (col==-1)
  962. return -1;
  963. else if (col==0) {
  964. int margin=x_ofs+cache.item_margin;//-cache.hseparation;
  965. //int lm = cache.bg->get_margin(MARGIN_LEFT);
  966. col_width-=margin;
  967. col_ofs+=margin;
  968. x-=margin;
  969. } else {
  970. col_width-=cache.hseparation;
  971. x-=cache.hseparation;
  972. }
  973. TreeItem::Cell &c = p_item->cells[col];
  974. bool already_selected=c.selected;
  975. bool already_cursor=(p_item==selected_item) && col == selected_col;
  976. for(int j=c.buttons.size()-1;j>=0;j--) {
  977. Ref<Texture> b=c.buttons[j].texture;
  978. int w = b->get_size().width + cache.button_pressed->get_minimum_size().width;
  979. if (x>col_width-w) {
  980. pressed_button=j;
  981. cache.click_type=Cache::CLICK_BUTTON;
  982. cache.click_index=j;
  983. cache.click_id=c.buttons[j].id;
  984. cache.click_item=p_item;
  985. cache.click_column=col;
  986. update();
  987. //emit_signal("button_pressed");
  988. return -1;
  989. }
  990. col_width-=w+cache.button_margin;
  991. }
  992. if (p_button==BUTTON_LEFT) {
  993. /* process selection */
  994. if (p_doubleclick && (!c.editable || c.mode==TreeItem::CELL_MODE_CUSTOM || c.mode==TreeItem::CELL_MODE_ICON || c.mode==TreeItem::CELL_MODE_CHECK)) {
  995. emit_signal("item_activated");
  996. return -1;
  997. }
  998. if (select_mode==SELECT_MULTI && p_mod.command && c.selectable) {
  999. if (!c.selected) {
  1000. p_item->select(col);
  1001. emit_signal("multi_selected",p_item,col,true);
  1002. //p_item->selected_signal.call(col);
  1003. } else {
  1004. p_item->deselect(col);
  1005. emit_signal("multi_selected",p_item,col,false);
  1006. //p_item->deselected_signal.call(col);
  1007. }
  1008. } else {
  1009. if (c.selectable) {
  1010. if (select_mode==SELECT_MULTI && p_mod.shift && selected_item && selected_item!=p_item) {
  1011. bool inrange=false;
  1012. select_single_item( p_item, root, col,selected_item,&inrange );
  1013. } else {
  1014. select_single_item( p_item, root, col );
  1015. }
  1016. //if (!c.selected && select_mode==SELECT_MULTI) {
  1017. // emit_signal("multi_selected",p_item,col,true);
  1018. //}
  1019. update();
  1020. }
  1021. }
  1022. }
  1023. if (!c.editable)
  1024. return -1; // if cell is not editable, don't bother
  1025. /* editing */
  1026. bool bring_up_editor=c.selected;// && already_selected;
  1027. bool bring_up_value_editor=false;
  1028. String editor_text=c.text;
  1029. switch (c.mode) {
  1030. case TreeItem::CELL_MODE_STRING: {
  1031. //nothing in particular
  1032. if (select_mode==SELECT_MULTI && (get_tree()->get_last_event_id() == focus_in_id || !already_cursor)) {
  1033. bring_up_editor=false;
  1034. }
  1035. } break;
  1036. case TreeItem::CELL_MODE_CHECK: {
  1037. Ref<Texture> checked = cache.checked;
  1038. bring_up_editor=false; //checkboxes are not edited with editor
  1039. if (x>=0 && x<= checked->get_width()+cache.hseparation ) {
  1040. p_item->set_checked(col,!c.checked);
  1041. item_edited(col,p_item);
  1042. click_handled=true;
  1043. //p_item->edited_signal.call(col);
  1044. }
  1045. } break;
  1046. case TreeItem::CELL_MODE_RANGE: {
  1047. if (c.text!="") {
  1048. //if (x >= (get_column_width(col)-item_h/2)) {
  1049. popup_menu->clear();
  1050. for (int i=0;i<c.text.get_slice_count(",");i++) {
  1051. String s = c.text.get_slicec(',',i);
  1052. popup_menu->add_item(s,i);
  1053. }
  1054. popup_menu->set_size(Size2(col_width,0));
  1055. popup_menu->set_pos( get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs+item_h)-cache.offset );
  1056. popup_menu->popup();
  1057. popup_edited_item=p_item;
  1058. popup_edited_item_col=col;
  1059. //}
  1060. bring_up_editor=false;
  1061. } else {
  1062. Ref<Texture> updown = cache.updown;
  1063. if (x >= (col_width-item_h/2)) {
  1064. /* touching the combo */
  1065. bool up=p_pos.y < (item_h /2);
  1066. if (p_button==BUTTON_LEFT) {
  1067. if (range_click_timer->get_time_left() == 0) {
  1068. range_item_last=p_item;
  1069. range_up_last=up;
  1070. range_click_timer->set_wait_time(0.6);
  1071. range_click_timer->set_one_shot(true);
  1072. range_click_timer->start();
  1073. } else if (up != range_up_last) {
  1074. return -1; // break. avoid changing direction on mouse held
  1075. }
  1076. p_item->set_range( col, c.val + (up?1.0:-1.0) * c.step );
  1077. item_edited(col,p_item);
  1078. } else if (p_button==BUTTON_RIGHT) {
  1079. p_item->set_range( col, (up?c.max:c.min) );
  1080. item_edited(col,p_item);
  1081. } else if (p_button==BUTTON_WHEEL_UP) {
  1082. p_item->set_range( col, c.val + c.step );
  1083. item_edited(col,p_item);
  1084. } else if (p_button==BUTTON_WHEEL_DOWN) {
  1085. p_item->set_range( col, c.val - c.step );
  1086. item_edited(col,p_item);
  1087. }
  1088. //p_item->edited_signal.call(col);
  1089. bring_up_editor=false;
  1090. } else {
  1091. editor_text=String::num( p_item->cells[col].val, Math::decimals( p_item->cells[col].step ) );
  1092. bring_up_value_editor=false;
  1093. if (select_mode==SELECT_MULTI && get_tree()->get_last_event_id() == focus_in_id)
  1094. bring_up_editor=false;
  1095. }
  1096. }
  1097. click_handled=true;
  1098. } break;
  1099. case TreeItem::CELL_MODE_ICON: {
  1100. bring_up_editor=false;
  1101. } break;
  1102. case TreeItem::CELL_MODE_CUSTOM: {
  1103. edited_item=p_item;
  1104. edited_col=col;
  1105. 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));
  1106. emit_signal("custom_popup_edited",((bool)(x >= (col_width-item_h/2))));
  1107. bring_up_editor=false;
  1108. item_edited(col,p_item);
  1109. click_handled=true;
  1110. return -1;
  1111. } break;
  1112. };
  1113. if (!bring_up_editor || p_button!=BUTTON_LEFT)
  1114. return -1;
  1115. click_handled=true;
  1116. popup_edited_item=p_item;
  1117. popup_edited_item_col=col;
  1118. pressing_item_rect=Rect2(get_global_pos() + Point2i(col_ofs,_get_title_button_height()+y_ofs)-cache.offset,Size2(col_width,item_h));
  1119. pressing_for_editor_text=editor_text;
  1120. pressing_for_editor=true;
  1121. return -1; //select
  1122. } else {
  1123. Point2i new_pos=p_pos;
  1124. if (!skip) {
  1125. x_ofs+=cache.item_margin;
  1126. //new_pos.x-=cache.item_margin;
  1127. y_ofs+=item_h;
  1128. new_pos.y-=item_h;
  1129. }
  1130. if (!p_item->collapsed) { /* if not collapsed, check the childs */
  1131. TreeItem *c=p_item->childs;
  1132. while (c) {
  1133. int child_h=propagate_mouse_event( new_pos,x_ofs,y_ofs,p_doubleclick,c,p_button,p_mod);
  1134. if (child_h<0)
  1135. return -1; // break, stop propagating, no need to anymore
  1136. new_pos.y-=child_h;
  1137. y_ofs+=child_h;
  1138. c=c->next;
  1139. item_h+=child_h;
  1140. }
  1141. }
  1142. }
  1143. return item_h; // nothing found
  1144. }
  1145. void Tree::text_editor_enter(String p_text) {
  1146. text_editor->hide();
  1147. value_editor->hide();
  1148. if (!popup_edited_item)
  1149. return;
  1150. if (popup_edited_item_col<0 || popup_edited_item_col>columns.size())
  1151. return;
  1152. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1153. switch( c.mode ) {
  1154. case TreeItem::CELL_MODE_STRING: {
  1155. c.text=p_text;
  1156. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1157. } break;
  1158. case TreeItem::CELL_MODE_RANGE: {
  1159. c.val=p_text.to_double();
  1160. if (c.step>0)
  1161. c.val=Math::stepify(c.val,c.step);
  1162. if (c.val<c.min)
  1163. c.val=c.min;
  1164. else if (c.val>c.max)
  1165. c.val=c.max;
  1166. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1167. } break;
  1168. default: { ERR_FAIL(); }
  1169. }
  1170. item_edited(popup_edited_item_col,popup_edited_item);
  1171. update();
  1172. }
  1173. void Tree::value_editor_changed(double p_value) {
  1174. if (updating_value_editor) {
  1175. return;
  1176. }
  1177. if (!popup_edited_item) {
  1178. return;
  1179. }
  1180. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1181. c.val=p_value;
  1182. item_edited(popup_edited_item_col,popup_edited_item);
  1183. update();
  1184. }
  1185. void Tree::popup_select(int p_option) {
  1186. if (!popup_edited_item)
  1187. return;
  1188. if (popup_edited_item_col<0 || popup_edited_item_col>columns.size())
  1189. return;
  1190. popup_edited_item->cells[popup_edited_item_col].val=p_option;
  1191. //popup_edited_item->edited_signal.call( popup_edited_item_col );
  1192. update();
  1193. item_edited(popup_edited_item_col,popup_edited_item);
  1194. }
  1195. void Tree::_input_event(InputEvent p_event) {
  1196. switch (p_event.type) {
  1197. case InputEvent::KEY: {
  1198. if (!p_event.key.pressed)
  1199. break;
  1200. 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)
  1201. break;
  1202. if (!root)
  1203. return;
  1204. if (hide_root && !root->get_next_visible())
  1205. return;
  1206. switch(p_event.key.scancode) {
  1207. #define EXIT_BREAK { if (!cursor_can_exit_tree) accept_event(); break; }
  1208. case KEY_RIGHT: {
  1209. //TreeItem *next = NULL;
  1210. if (!selected_item)
  1211. break;
  1212. if (select_mode==SELECT_ROW)
  1213. EXIT_BREAK;
  1214. if (selected_col>=(columns.size()-1))
  1215. EXIT_BREAK;
  1216. if (select_mode==SELECT_MULTI) {
  1217. selected_col++;
  1218. emit_signal("cell_selected");
  1219. } else {
  1220. selected_item->select(selected_col+1);
  1221. }
  1222. update();
  1223. ensure_cursor_is_visible();
  1224. accept_event();
  1225. } break;
  1226. case KEY_LEFT: {
  1227. // TreeItem *next = NULL;
  1228. if (!selected_item)
  1229. break;
  1230. if (select_mode==SELECT_ROW)
  1231. EXIT_BREAK;
  1232. if (selected_col<=0)
  1233. EXIT_BREAK;
  1234. if (select_mode==SELECT_MULTI) {
  1235. selected_col--;
  1236. emit_signal("cell_selected");
  1237. } else {
  1238. selected_item->select(selected_col-1);
  1239. }
  1240. update();
  1241. accept_event();
  1242. } break;
  1243. case KEY_DOWN: {
  1244. TreeItem *next = NULL;
  1245. if (!selected_item) {
  1246. next=hide_root?root->get_next_visible():root;
  1247. selected_item=0;
  1248. } else {
  1249. next=selected_item->get_next_visible();
  1250. // if (diff < uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000))) {
  1251. if (last_keypress!=0) {
  1252. //incr search next
  1253. int col;
  1254. next=_search_item_text(next,incr_search,&col,true);
  1255. if (!next) {
  1256. accept_event();
  1257. return;
  1258. }
  1259. }
  1260. }
  1261. if (select_mode==SELECT_MULTI) {
  1262. if (!next)
  1263. EXIT_BREAK;
  1264. selected_item=next;
  1265. emit_signal("cell_selected");
  1266. update();
  1267. } else {
  1268. int col=selected_col<0?0:selected_col;
  1269. while (next && !next->cells[col].selectable)
  1270. next=next->get_next_visible();
  1271. if (!next)
  1272. EXIT_BREAK; // do nothing..
  1273. next->select(col);
  1274. }
  1275. ensure_cursor_is_visible();
  1276. accept_event();
  1277. } break;
  1278. case KEY_UP: {
  1279. TreeItem *prev = NULL;
  1280. if (!selected_item) {
  1281. prev = get_last_item();
  1282. selected_col=0;
  1283. } else {
  1284. prev=selected_item->get_prev_visible();
  1285. if (last_keypress!=0) {
  1286. //incr search next
  1287. int col;
  1288. prev=_search_item_text(prev,incr_search,&col,true,true);
  1289. if (!prev) {
  1290. accept_event();
  1291. return;
  1292. }
  1293. }
  1294. }
  1295. if (select_mode==SELECT_MULTI) {
  1296. if (!prev)
  1297. break;
  1298. selected_item=prev;
  1299. emit_signal("cell_selected");
  1300. update();
  1301. } else {
  1302. int col=selected_col<0?0:selected_col;
  1303. while (prev && !prev->cells[col].selectable)
  1304. prev=prev->get_prev_visible();
  1305. if (!prev)
  1306. break; // do nothing..
  1307. prev->select(col);
  1308. }
  1309. ensure_cursor_is_visible();
  1310. accept_event();
  1311. } break;
  1312. case KEY_PAGEDOWN: {
  1313. TreeItem *next = NULL;
  1314. if (!selected_item)
  1315. break;
  1316. next=selected_item;
  1317. for(int i=0;i<10;i++) {
  1318. TreeItem *_n = next->get_next_visible();
  1319. if (_n) {
  1320. next=_n;
  1321. } else {
  1322. break;
  1323. }
  1324. }
  1325. if (next==selected_item)
  1326. break;
  1327. if (select_mode==SELECT_MULTI) {
  1328. selected_item=next;
  1329. emit_signal("cell_selected");
  1330. update();
  1331. } else {
  1332. while (next && !next->cells[selected_col].selectable)
  1333. next=next->get_next_visible();
  1334. if (!next)
  1335. EXIT_BREAK; // do nothing..
  1336. next->select(selected_col);
  1337. }
  1338. ensure_cursor_is_visible();
  1339. } break;
  1340. case KEY_PAGEUP: {
  1341. TreeItem *prev = NULL;
  1342. if (!selected_item)
  1343. break;
  1344. prev=selected_item;
  1345. for(int i=0;i<10;i++) {
  1346. TreeItem *_n = prev->get_prev_visible();
  1347. if (_n) {
  1348. prev=_n;
  1349. } else {
  1350. break;
  1351. }
  1352. }
  1353. if (prev==selected_item)
  1354. break;
  1355. if (select_mode==SELECT_MULTI) {
  1356. selected_item=prev;
  1357. emit_signal("cell_selected");
  1358. update();
  1359. } else {
  1360. while (prev && !prev->cells[selected_col].selectable)
  1361. prev=prev->get_prev_visible();
  1362. if (!prev)
  1363. EXIT_BREAK; // do nothing..
  1364. prev->select(selected_col);
  1365. }
  1366. ensure_cursor_is_visible();
  1367. } break;
  1368. case KEY_F2:
  1369. case KEY_RETURN:
  1370. case KEY_ENTER: {
  1371. if (selected_item) {
  1372. //bring up editor if possible
  1373. if (!edit_selected()) {
  1374. emit_signal("item_activated");
  1375. }
  1376. }
  1377. accept_event();
  1378. } break;
  1379. case KEY_SPACE: {
  1380. if (select_mode==SELECT_MULTI) {
  1381. if (!selected_item)
  1382. break;
  1383. if (selected_item->is_selected(selected_col)) {
  1384. selected_item->deselect(selected_col);
  1385. emit_signal("multi_selected",selected_item,selected_col,false);
  1386. } else if (selected_item->is_selectable(selected_col)) {
  1387. selected_item->select(selected_col);
  1388. emit_signal("multi_selected",selected_item,selected_col,true);
  1389. }
  1390. }
  1391. accept_event();
  1392. } break;
  1393. default: {
  1394. if (p_event.key.unicode>0) {
  1395. _do_incr_search(String::chr(p_event.key.unicode));
  1396. accept_event();
  1397. return;
  1398. } else {
  1399. if (p_event.key.scancode!=KEY_SHIFT)
  1400. last_keypress=0;
  1401. }
  1402. } break;
  1403. last_keypress=0;
  1404. }
  1405. } break;
  1406. case InputEvent::MOUSE_MOTION: {
  1407. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1408. update_cache();
  1409. const InputEventMouseMotion& b=p_event.mouse_motion;
  1410. range_click_timer->stop();
  1411. Ref<StyleBox> bg = cache.bg;
  1412. Point2 pos = Point2(b.x,b.y) - bg->get_offset();
  1413. Cache::ClickType old_hover = cache.hover_type;
  1414. int old_index = cache.hover_index;
  1415. cache.hover_type=Cache::CLICK_NONE;
  1416. cache.hover_index=0;
  1417. if (show_column_titles) {
  1418. pos.y-=_get_title_button_height();
  1419. if (pos.y<0) {
  1420. pos.x+=cache.offset.x;
  1421. int len=0;
  1422. for(int i=0;i<columns.size();i++) {
  1423. len+=get_column_width(i);
  1424. if (pos.x<len) {
  1425. cache.hover_type=Cache::CLICK_TITLE;
  1426. cache.hover_index=i;
  1427. update();
  1428. break;
  1429. }
  1430. }
  1431. }
  1432. }
  1433. if (cache.hover_type!=old_hover || cache.hover_index!=old_index) {
  1434. update();
  1435. }
  1436. if (pressing_for_editor && popup_edited_item && popup_edited_item->get_cell_mode(popup_edited_item_col)==TreeItem::CELL_MODE_RANGE) {
  1437. //range drag
  1438. if (!range_drag_enabled) {
  1439. Vector2 cpos = Vector2(b.x,b.y);
  1440. if (cpos.distance_to(pressing_pos)>2) {
  1441. range_drag_enabled=true;
  1442. range_drag_capture_pos=cpos;
  1443. range_drag_base=popup_edited_item->get_range(popup_edited_item_col);
  1444. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1445. }
  1446. } else {
  1447. TreeItem::Cell &c=popup_edited_item->cells[popup_edited_item_col];
  1448. float diff_y = -b.relative_y;
  1449. diff_y=Math::pow(ABS(diff_y),1.8)*SGN(diff_y);
  1450. diff_y*=0.1;
  1451. range_drag_base=CLAMP(range_drag_base + c.step * diff_y, c.min, c.max);
  1452. popup_edited_item->set_range(popup_edited_item_col,range_drag_base);
  1453. item_edited(popup_edited_item_col,popup_edited_item);
  1454. }
  1455. }
  1456. if (drag_touching && ! drag_touching_deaccel) {
  1457. drag_accum-=b.relative_y;
  1458. v_scroll->set_val(drag_from+drag_accum);
  1459. drag_speed=-b.speed_y;
  1460. }
  1461. } break;
  1462. case InputEvent::MOUSE_BUTTON: {
  1463. if (cache.font.is_null()) // avoid a strange case that may fuckup stuff
  1464. update_cache();
  1465. const InputEventMouseButton& b=p_event.mouse_button;
  1466. if (!b.pressed) {
  1467. if (b.button_index==BUTTON_LEFT) {
  1468. range_click_timer->stop();
  1469. if (pressing_for_editor) {
  1470. if (range_drag_enabled) {
  1471. range_drag_enabled=false;
  1472. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1473. warp_mouse(range_drag_capture_pos);
  1474. } else
  1475. edit_selected();
  1476. pressing_for_editor=false;
  1477. }
  1478. if (cache.click_type==Cache::CLICK_BUTTON) {
  1479. emit_signal("button_pressed",cache.click_item,cache.click_column,cache.click_id);
  1480. }
  1481. cache.click_type=Cache::CLICK_NONE;
  1482. cache.click_index=-1;
  1483. cache.click_id=-1;
  1484. cache.click_item=NULL;
  1485. cache.click_column=0;
  1486. if (drag_touching) {
  1487. if (drag_speed==0) {
  1488. drag_touching_deaccel=false;
  1489. drag_touching=false;
  1490. set_fixed_process(false);
  1491. } else {
  1492. drag_touching_deaccel=true;
  1493. }
  1494. }
  1495. update();
  1496. }
  1497. break;
  1498. }
  1499. switch(b.button_index) {
  1500. case BUTTON_LEFT: {
  1501. Ref<StyleBox> bg = cache.bg;
  1502. Point2 pos = Point2(b.x,b.y) - bg->get_offset();
  1503. cache.click_type=Cache::CLICK_NONE;
  1504. if (show_column_titles) {
  1505. pos.y-=_get_title_button_height();
  1506. if (pos.y<0) {
  1507. pos.x+=cache.offset.x;
  1508. int len=0;
  1509. for(int i=0;i<columns.size();i++) {
  1510. len+=get_column_width(i);
  1511. if (pos.x<len) {
  1512. cache.click_type=Cache::CLICK_TITLE;
  1513. cache.click_index=i;
  1514. //cache.click_id=;
  1515. update();
  1516. break;
  1517. }
  1518. }
  1519. break;
  1520. }
  1521. }
  1522. if (!root)
  1523. break;
  1524. click_handled=false;
  1525. pressing_for_editor=false;
  1526. blocked++;
  1527. bool handled = propagate_mouse_event(pos+cache.offset,0,0,b.doubleclick,root,b.button_index,b.mod);
  1528. blocked--;
  1529. if (pressing_for_editor) {
  1530. pressing_pos=Point2(b.x,b.y);
  1531. }
  1532. if (drag_touching) {
  1533. set_fixed_process(false);
  1534. drag_touching_deaccel=false;
  1535. drag_touching=false;
  1536. drag_speed=0;
  1537. drag_from=0;
  1538. }
  1539. if (!click_handled) {
  1540. drag_speed=0;
  1541. drag_accum=0;
  1542. // last_drag_accum=0;
  1543. drag_from=v_scroll->get_val();
  1544. drag_touching=OS::get_singleton()->has_touchscreen_ui_hint();
  1545. drag_touching_deaccel=false;
  1546. if (drag_touching) {
  1547. set_fixed_process(true);
  1548. }
  1549. }
  1550. } break;
  1551. case BUTTON_WHEEL_UP: {
  1552. range_click_timer->stop();
  1553. v_scroll->set_val( v_scroll->get_val()-v_scroll->get_page()/8 );
  1554. } break;
  1555. case BUTTON_WHEEL_DOWN: {
  1556. range_click_timer->stop();
  1557. v_scroll->set_val( v_scroll->get_val()+v_scroll->get_page()/8 );
  1558. } break;
  1559. }
  1560. } break;
  1561. }
  1562. }
  1563. bool Tree::edit_selected() {
  1564. TreeItem *s = get_selected();
  1565. ERR_EXPLAIN("No item selected!");
  1566. ERR_FAIL_COND_V(!s,false);
  1567. ensure_cursor_is_visible();
  1568. int col = get_selected_column();
  1569. ERR_EXPLAIN("No item column selected!");
  1570. ERR_FAIL_INDEX_V(col,columns.size(),false);
  1571. if (!s->cells[col].editable)
  1572. return false;
  1573. Rect2 rect;
  1574. rect.pos.y = get_item_offset(s) - v_scroll->get_val();
  1575. for(int i=0;i<col;i++) {
  1576. rect.pos.x+=get_column_width(i);
  1577. }
  1578. rect.size.width=get_column_width(col);
  1579. rect.size.height=compute_item_height(s)+cache.vseparation;
  1580. popup_edited_item=s;
  1581. popup_edited_item_col=col;
  1582. TreeItem::Cell &c = s->cells[col];
  1583. if (c.mode==TreeItem::CELL_MODE_CHECK) {
  1584. s->set_checked(col, !c.checked);
  1585. item_edited(col,s);
  1586. return true;
  1587. } else if (c.mode==TreeItem::CELL_MODE_CUSTOM) {
  1588. edited_item=s;
  1589. edited_col=col;
  1590. custom_popup_rect=Rect2i( get_global_pos() + rect.pos, rect.size );
  1591. emit_signal("custom_popup_edited",false);
  1592. item_edited(col,s);
  1593. return true;
  1594. } else if (c.mode==TreeItem::CELL_MODE_RANGE && c.text!="") {
  1595. popup_menu->clear();
  1596. for (int i=0;i<c.text.get_slice_count(",");i++) {
  1597. String s = c.text.get_slicec(',',i);
  1598. popup_menu->add_item(s,i);
  1599. }
  1600. popup_menu->set_size(Size2(rect.size.width,0));
  1601. popup_menu->set_pos( get_global_pos() + rect.pos + Point2i(0,rect.size.height) );
  1602. popup_menu->popup();
  1603. popup_edited_item=s;
  1604. popup_edited_item_col=col;
  1605. return true;
  1606. } else if (c.mode==TreeItem::CELL_MODE_STRING || c.mode==TreeItem::CELL_MODE_RANGE) {
  1607. Point2i textedpos=get_global_pos() + rect.pos;
  1608. text_editor->set_pos( textedpos );
  1609. text_editor->set_size( rect.size);
  1610. text_editor->clear();
  1611. text_editor->set_text( c.mode==TreeItem::CELL_MODE_STRING?c.text:rtos(c.val) );
  1612. text_editor->select_all();
  1613. if (c.mode==TreeItem::CELL_MODE_RANGE) {
  1614. value_editor->set_pos(textedpos + Point2i(0,text_editor->get_size().height) );
  1615. value_editor->set_size( Size2(rect.size.width,1));
  1616. value_editor->show_modal();
  1617. updating_value_editor=true;
  1618. value_editor->set_min( c.min );
  1619. value_editor->set_max( c.max );
  1620. value_editor->set_step( c.step );
  1621. value_editor->set_val( c.val );
  1622. value_editor->set_exp_unit_value( c.expr );
  1623. updating_value_editor=false;
  1624. }
  1625. text_editor->show_modal();
  1626. text_editor->grab_focus();
  1627. return true;
  1628. }
  1629. return false;
  1630. }
  1631. Size2 Tree::get_internal_min_size() const {
  1632. Size2i size=cache.bg->get_offset();
  1633. if (root)
  1634. size.height+=get_item_height(root);
  1635. for (int i=0;i<columns.size();i++) {
  1636. size.width+=columns[i].min_width;
  1637. }
  1638. return size;
  1639. }
  1640. void Tree::update_scrollbars() {
  1641. Size2 size = get_size();
  1642. int tbh;
  1643. if (show_column_titles) {
  1644. tbh=_get_title_button_height();
  1645. } else {
  1646. tbh=0;
  1647. }
  1648. Size2 hmin = h_scroll->get_combined_minimum_size();
  1649. Size2 vmin = v_scroll->get_combined_minimum_size();
  1650. v_scroll->set_begin( Point2(size.width - vmin.width , cache.bg->get_margin(MARGIN_TOP)) );
  1651. v_scroll->set_end( Point2(size.width, size.height-cache.bg->get_margin(MARGIN_TOP)-cache.bg->get_margin(MARGIN_BOTTOM)) );
  1652. h_scroll->set_begin( Point2( 0, size.height - hmin.height) );
  1653. h_scroll->set_end( Point2(size.width-vmin.width, size.height) );
  1654. Size2 min = get_internal_min_size();
  1655. if (min.height < size.height - hmin.height) {
  1656. v_scroll->hide();
  1657. cache.offset.y=0;
  1658. } else {
  1659. v_scroll->show();
  1660. v_scroll->set_max(min.height);
  1661. v_scroll->set_page(size.height - hmin.height - tbh);
  1662. cache.offset.y=v_scroll->get_val();
  1663. }
  1664. if (min.width < size.width - vmin.width) {
  1665. h_scroll->hide();
  1666. cache.offset.x=0;
  1667. } else {
  1668. h_scroll->show();
  1669. h_scroll->set_max(min.width);
  1670. h_scroll->set_page(size.width - vmin.width);
  1671. cache.offset.x=h_scroll->get_val();
  1672. }
  1673. }
  1674. int Tree::_get_title_button_height() const {
  1675. return show_column_titles?cache.font->get_height() + cache.title_button->get_minimum_size().height:0;
  1676. }
  1677. void Tree::_notification(int p_what) {
  1678. if (p_what==NOTIFICATION_FOCUS_ENTER) {
  1679. focus_in_id=get_tree()->get_last_event_id();
  1680. }
  1681. if (p_what==NOTIFICATION_MOUSE_EXIT) {
  1682. if (cache.hover_type!=Cache::CLICK_NONE) {
  1683. cache.hover_type=Cache::CLICK_NONE;
  1684. update();
  1685. }
  1686. }
  1687. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  1688. drag_touching=false;
  1689. }
  1690. if (p_what==NOTIFICATION_ENTER_TREE) {
  1691. update_cache();;
  1692. }
  1693. if (p_what==NOTIFICATION_FIXED_PROCESS) {
  1694. if (drag_touching) {
  1695. if (drag_touching_deaccel) {
  1696. float pos = v_scroll->get_val();
  1697. pos+=drag_speed*get_fixed_process_delta_time();
  1698. bool turnoff=false;
  1699. if (pos<0) {
  1700. pos=0;
  1701. turnoff=true;
  1702. set_fixed_process(false);
  1703. drag_touching=false;
  1704. drag_touching_deaccel=false;
  1705. }
  1706. if (pos > (v_scroll->get_max()-v_scroll->get_page())) {
  1707. pos=v_scroll->get_max()-v_scroll->get_page();
  1708. turnoff=true;
  1709. }
  1710. v_scroll->set_val(pos);
  1711. float sgn = drag_speed<0? -1 : 1;
  1712. float val = Math::abs(drag_speed);
  1713. val-=1000*get_fixed_process_delta_time();
  1714. if (val<0) {
  1715. turnoff=true;
  1716. }
  1717. drag_speed=sgn*val;
  1718. if (turnoff) {
  1719. set_fixed_process(false);
  1720. drag_touching=false;
  1721. drag_touching_deaccel=false;
  1722. }
  1723. } else {
  1724. }
  1725. }
  1726. }
  1727. if (p_what==NOTIFICATION_DRAW) {
  1728. update_cache();
  1729. update_scrollbars();
  1730. RID ci = get_canvas_item();
  1731. VisualServer::get_singleton()->canvas_item_set_clip(ci,true);
  1732. Ref<StyleBox> bg = cache.bg;
  1733. Ref<StyleBox> bg_focus = get_stylebox("bg_focus");
  1734. Point2 draw_ofs;
  1735. draw_ofs+=bg->get_offset();
  1736. Size2 draw_size=get_size()-bg->get_minimum_size();
  1737. bg->draw( ci, Rect2( Point2(), get_size()) );
  1738. if (has_focus()) {
  1739. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,true);
  1740. bg_focus->draw( ci, Rect2( Point2(), get_size()) );
  1741. VisualServer::get_singleton()->canvas_item_add_clip_ignore(ci,false);
  1742. }
  1743. int tbh = _get_title_button_height();
  1744. draw_ofs.y+=tbh;
  1745. draw_size.y-=tbh;
  1746. if (root) {
  1747. draw_item( Point2(),draw_ofs,draw_size,root);
  1748. }
  1749. int ofs=0;
  1750. // int from_y=exposed.pos.y+bg->get_margin(MARGIN_TOP);
  1751. // int size_y=exposed.size.height-bg->get_minimum_size().height;
  1752. for (int i=0;i<(columns.size()-1-1);i++) {
  1753. ofs+=get_column_width(i);
  1754. //get_painter()->draw_fill_rect( Point2(ofs+cache.hseparation/2, from_y), Size2( 1, size_y ),color( COLOR_TREE_GRID) );
  1755. }
  1756. if (show_column_titles) {
  1757. //title butons
  1758. int ofs=cache.bg->get_margin(MARGIN_LEFT);
  1759. for(int i=0;i<columns.size();i++) {
  1760. 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);
  1761. Ref<Font> f = cache.tb_font;
  1762. Rect2 tbrect = Rect2(ofs - cache.offset.x,bg->get_margin(MARGIN_TOP),get_column_width(i),tbh);
  1763. sb->draw(ci,tbrect);
  1764. ofs+=tbrect.size.width;
  1765. //text
  1766. int clip_w = tbrect.size.width - sb->get_minimum_size().width;
  1767. 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);
  1768. }
  1769. }
  1770. }
  1771. if (p_what==NOTIFICATION_THEME_CHANGED) {
  1772. update_cache();
  1773. }
  1774. }
  1775. Size2 Tree::get_minimum_size() const {
  1776. return Size2(1,1);
  1777. }
  1778. TreeItem *Tree::create_item(TreeItem *p_parent) {
  1779. ERR_FAIL_COND_V(blocked>0,NULL);
  1780. TreeItem *ti = memnew( TreeItem(this) );
  1781. ti->cells.resize( columns.size() );
  1782. ERR_FAIL_COND_V(!ti,NULL);
  1783. if (p_parent) {
  1784. /* Always append at the end */
  1785. TreeItem *last=0;
  1786. TreeItem *c=p_parent->childs;
  1787. while(c) {
  1788. last=c;
  1789. c=c->next;
  1790. }
  1791. if (last) {
  1792. last->next=ti;
  1793. } else {
  1794. p_parent->childs=ti;
  1795. }
  1796. ti->parent=p_parent;
  1797. } else {
  1798. if (root)
  1799. ti->childs=root;
  1800. root=ti;
  1801. }
  1802. return ti;
  1803. }
  1804. TreeItem* Tree::get_root() {
  1805. return root;
  1806. }
  1807. TreeItem* Tree::get_last_item() {
  1808. TreeItem *last=root;
  1809. while(last) {
  1810. if (last->next)
  1811. last=last->next;
  1812. else if (last->childs)
  1813. last=last->childs;
  1814. else
  1815. break;
  1816. }
  1817. return last;
  1818. }
  1819. void Tree::item_edited(int p_column,TreeItem *p_item) {
  1820. edited_item=p_item;
  1821. edited_col=p_column;
  1822. emit_signal("item_edited");
  1823. }
  1824. void Tree::item_changed(int p_column,TreeItem *p_item) {
  1825. update();
  1826. }
  1827. void Tree::item_selected(int p_column,TreeItem *p_item) {
  1828. if (select_mode==SELECT_MULTI) {
  1829. if (!p_item->cells[p_column].selectable)
  1830. return;
  1831. p_item->cells[p_column].selected=true;
  1832. //emit_signal("multi_selected",p_item,p_column,true); - NO this is for TreeItem::select
  1833. } else {
  1834. select_single_item(p_item,root,p_column);
  1835. }
  1836. update();
  1837. }
  1838. void Tree::item_deselected(int p_column,TreeItem *p_item) {
  1839. if (select_mode==SELECT_MULTI) {
  1840. p_item->cells[p_column].selected=false;
  1841. }
  1842. update();
  1843. }
  1844. void Tree::set_select_mode(SelectMode p_mode) {
  1845. select_mode=p_mode;
  1846. }
  1847. void Tree::clear() {
  1848. if (blocked>0) {
  1849. ERR_FAIL_COND(blocked>0);
  1850. }
  1851. if (root) {
  1852. memdelete( root );
  1853. root = NULL;
  1854. };
  1855. selected_item=NULL;
  1856. edited_item=NULL;
  1857. popup_edited_item=NULL;
  1858. selected_item=NULL;
  1859. pressing_for_editor=false;
  1860. update();
  1861. };
  1862. void Tree::set_hide_root(bool p_enabled) {
  1863. hide_root=p_enabled;
  1864. update();
  1865. }
  1866. void Tree::set_column_min_width(int p_column,int p_min_width) {
  1867. ERR_FAIL_INDEX(p_column,columns.size());
  1868. if (p_min_width<1)
  1869. return;
  1870. columns[p_column].min_width=p_min_width;
  1871. update();
  1872. }
  1873. void Tree::set_column_expand(int p_column,bool p_expand) {
  1874. ERR_FAIL_INDEX(p_column,columns.size());
  1875. columns[p_column].expand=p_expand;
  1876. update();
  1877. }
  1878. TreeItem *Tree::get_selected() const {
  1879. return selected_item;
  1880. }
  1881. int Tree::get_selected_column() const {
  1882. return selected_col;
  1883. }
  1884. TreeItem *Tree::get_edited() const {
  1885. return edited_item;
  1886. }
  1887. int Tree::get_edited_column() const {
  1888. return edited_col;
  1889. }
  1890. TreeItem* Tree::get_next_selected( TreeItem* p_item) {
  1891. //if (!p_item)
  1892. // return NULL;
  1893. if (!root)
  1894. return NULL;
  1895. while(true) {
  1896. if (!p_item) {
  1897. p_item=root;
  1898. } else {
  1899. if (p_item->childs) {
  1900. p_item=p_item->childs;
  1901. } else if (p_item->next) {
  1902. p_item=p_item->next;
  1903. } else {
  1904. while(!p_item->next) {
  1905. p_item=p_item->parent;
  1906. if (p_item==NULL)
  1907. return NULL;
  1908. }
  1909. p_item=p_item->next;
  1910. }
  1911. }
  1912. for (int i=0;i<columns.size();i++)
  1913. if (p_item->cells[i].selected)
  1914. return p_item;
  1915. }
  1916. return NULL;
  1917. }
  1918. int Tree::get_column_width(int p_column) const {
  1919. ERR_FAIL_INDEX_V(p_column,columns.size(),-1);
  1920. if (!columns[p_column].expand)
  1921. return columns[p_column].min_width;
  1922. Ref<StyleBox> bg = cache.bg;
  1923. int expand_area=get_size().width-(bg->get_margin(MARGIN_LEFT)+bg->get_margin(MARGIN_RIGHT));
  1924. if (v_scroll->is_visible())
  1925. expand_area-=v_scroll->get_combined_minimum_size().width;
  1926. int expanding_columns=0;
  1927. int expanding_total=0;
  1928. for (int i=0;i<columns.size();i++) {
  1929. if (!columns[i].expand) {
  1930. expand_area-=columns[i].min_width;
  1931. } else {
  1932. expanding_total+=columns[i].min_width;
  1933. expanding_columns++;
  1934. }
  1935. }
  1936. if (expand_area<expanding_total)
  1937. return columns[p_column].min_width;
  1938. ERR_FAIL_COND_V(expanding_columns==0,-1); // shouldnt happen
  1939. return expand_area * columns[p_column].min_width / expanding_total;
  1940. }
  1941. void Tree::propagate_set_columns(TreeItem *p_item) {
  1942. p_item->cells.resize( columns.size() );
  1943. TreeItem *c = p_item->get_children();
  1944. while(c) {
  1945. propagate_set_columns(c);
  1946. c=c->get_next();
  1947. }
  1948. }
  1949. void Tree::set_columns(int p_columns) {
  1950. ERR_FAIL_COND(p_columns<1);
  1951. ERR_FAIL_COND(blocked>0);
  1952. columns.resize(p_columns);
  1953. if (root)
  1954. propagate_set_columns(root);
  1955. if (selected_col>=p_columns)
  1956. selected_col=p_columns-1;
  1957. update();
  1958. }
  1959. int Tree::get_columns() const {
  1960. return columns.size();
  1961. }
  1962. void Tree::_scroll_moved(float) {
  1963. update();
  1964. }
  1965. Rect2 Tree::get_custom_popup_rect() const {
  1966. return custom_popup_rect;
  1967. }
  1968. int Tree::get_item_offset(TreeItem *p_item) const {
  1969. TreeItem *it=root;
  1970. int ofs=_get_title_button_height();
  1971. if (!it)
  1972. return 0;
  1973. while(true) {
  1974. if (it==p_item)
  1975. return ofs;
  1976. ofs+=compute_item_height(it)+cache.vseparation;
  1977. if (it->childs && !it->collapsed) {
  1978. it=it->childs;
  1979. } else if (it->next) {
  1980. it=it->next;
  1981. } else {
  1982. while(!it->next) {
  1983. it=it->parent;
  1984. if (it==NULL)
  1985. return 0;
  1986. }
  1987. it=it->next;
  1988. }
  1989. }
  1990. return -1; //not found
  1991. }
  1992. void Tree::ensure_cursor_is_visible() {
  1993. if (!is_inside_tree())
  1994. return;
  1995. TreeItem *selected = get_selected();
  1996. if (!selected)
  1997. return;
  1998. int ofs = get_item_offset(selected);
  1999. if (ofs==-1)
  2000. return;
  2001. int h = compute_item_height(selected)+cache.vseparation;
  2002. int screenh=get_size().height-h_scroll->get_combined_minimum_size().height;
  2003. if (ofs+h>v_scroll->get_val()+screenh)
  2004. v_scroll->set_val(ofs-screenh+h);
  2005. else if (ofs < v_scroll->get_val())
  2006. v_scroll->set_val(ofs);
  2007. }
  2008. int Tree::get_pressed_button() const {
  2009. return pressed_button;
  2010. }
  2011. Rect2 Tree::get_item_rect(TreeItem *p_item,int p_column) const {
  2012. ERR_FAIL_NULL_V(p_item,Rect2());
  2013. ERR_FAIL_COND_V(p_item->tree!=this,Rect2());
  2014. if (p_column!=-1) {
  2015. ERR_FAIL_INDEX_V(p_column,columns.size(),Rect2());
  2016. }
  2017. int ofs = get_item_offset(p_item);
  2018. int height = compute_item_height(p_item);
  2019. Rect2 r;
  2020. r.pos.y=ofs;
  2021. r.size.height=height;
  2022. if (p_column==-1) {
  2023. r.pos.x=0;
  2024. r.size.x=get_size().width;
  2025. } else {
  2026. int accum=0;
  2027. for(int i=0;i<p_column;i++) {
  2028. accum+=get_column_width(i);
  2029. }
  2030. r.pos.x=accum;
  2031. r.size.x=get_column_width(p_column);
  2032. }
  2033. return r;
  2034. }
  2035. void Tree::set_column_titles_visible(bool p_show) {
  2036. show_column_titles=p_show;
  2037. update();
  2038. }
  2039. bool Tree::are_column_titles_visible() const {
  2040. return show_column_titles;
  2041. }
  2042. void Tree::set_column_title(int p_column,const String& p_title) {
  2043. ERR_FAIL_INDEX(p_column,columns.size());
  2044. columns[p_column].title=p_title;
  2045. update();
  2046. }
  2047. String Tree::get_column_title(int p_column) const {
  2048. ERR_FAIL_INDEX_V(p_column,columns.size(),"");
  2049. return columns[p_column].title;
  2050. }
  2051. Point2 Tree::get_scroll() const {
  2052. Point2 ofs;
  2053. if (h_scroll->is_visible())
  2054. ofs.x=h_scroll->get_val();
  2055. if (v_scroll->is_visible())
  2056. ofs.y=v_scroll->get_val();
  2057. return ofs;
  2058. }
  2059. TreeItem* Tree::_search_item_text(TreeItem *p_at, const String& p_find,int *r_col,bool p_selectable,bool p_backwards) {
  2060. while(p_at) {
  2061. for(int i=0;i<columns.size();i++) {
  2062. if (p_at->get_text(i).findn(p_find)==0 && (!p_selectable || p_at->is_selectable(i))) {
  2063. if (r_col)
  2064. *r_col=i;
  2065. return p_at;
  2066. }
  2067. }
  2068. if (p_backwards)
  2069. p_at=p_at->get_prev_visible();
  2070. else
  2071. p_at=p_at->get_next_visible();
  2072. }
  2073. return NULL;
  2074. }
  2075. TreeItem* Tree::search_item_text(const String& p_find,int *r_col,bool p_selectable) {
  2076. if (!root)
  2077. return NULL;
  2078. return _search_item_text(root,p_find,r_col,p_selectable);
  2079. }
  2080. void Tree::_do_incr_search(const String& p_add) {
  2081. uint64_t time = OS::get_singleton()->get_ticks_usec() / 1000; // convert to msec
  2082. uint64_t diff = time - last_keypress;
  2083. if (diff > uint64_t(GLOBAL_DEF("gui/incr_search_max_interval_msec",2000)))
  2084. incr_search=p_add;
  2085. else
  2086. incr_search+=p_add;
  2087. last_keypress=time;
  2088. int col;
  2089. TreeItem *item = search_item_text(incr_search,&col,true);
  2090. if (!item)
  2091. return;
  2092. item->select(col);
  2093. ensure_cursor_is_visible();
  2094. }
  2095. TreeItem* Tree::_find_item_at_pos(TreeItem*p_item, const Point2& p_pos,int& r_column,int &h) const {
  2096. Point2 pos = p_pos;
  2097. if (root!=p_item || ! hide_root) {
  2098. h = compute_item_height(p_item)+cache.vseparation;;
  2099. if (pos.y<h) {
  2100. for(int i=0;i<columns.size();i++) {
  2101. int w = get_column_width(i);
  2102. if (pos.x < w) {
  2103. r_column=i;
  2104. return p_item;
  2105. }
  2106. pos.x-=w;
  2107. }
  2108. return NULL;
  2109. } else {
  2110. pos.y-=h;
  2111. }
  2112. } else {
  2113. h=0;
  2114. }
  2115. if (p_item->is_collapsed())
  2116. return NULL; // do not try childs, it's collapsed
  2117. TreeItem *n = p_item->get_children();
  2118. while(n) {
  2119. int ch;
  2120. TreeItem *r = _find_item_at_pos(n,pos,r_column,ch);
  2121. pos.y-=ch;
  2122. h+=ch;
  2123. if (r)
  2124. return r;
  2125. n=n->get_next();
  2126. }
  2127. return NULL;
  2128. }
  2129. String Tree::get_tooltip(const Point2& p_pos) const {
  2130. if (root) {
  2131. Point2 pos=p_pos;
  2132. pos -= cache.bg->get_offset();
  2133. pos.y-=_get_title_button_height();
  2134. if (pos.y<0)
  2135. return Control::get_tooltip(p_pos);
  2136. if (h_scroll->is_visible())
  2137. pos.x+=h_scroll->get_val();
  2138. if (v_scroll->is_visible())
  2139. pos.y+=v_scroll->get_val();
  2140. int col,h;
  2141. TreeItem *it = _find_item_at_pos(root,pos,col,h);
  2142. if (it) {
  2143. String ret;
  2144. if (it->get_tooltip(col)=="")
  2145. ret=it->get_text(col);
  2146. else
  2147. ret=it->get_tooltip(col);
  2148. return ret;
  2149. }
  2150. }
  2151. return Control::get_tooltip(p_pos);
  2152. }
  2153. void Tree::set_cursor_can_exit_tree(bool p_enable) {
  2154. cursor_can_exit_tree=p_enable;
  2155. }
  2156. bool Tree::can_cursor_exit_tree() const {
  2157. return cursor_can_exit_tree;
  2158. }
  2159. void Tree::set_hide_folding(bool p_hide) {
  2160. hide_folding=p_hide;
  2161. update();
  2162. }
  2163. bool Tree::is_folding_hidden() const {
  2164. return hide_folding;
  2165. }
  2166. void Tree::_bind_methods() {
  2167. ObjectTypeDB::bind_method(_MD("_range_click_timeout"),&Tree::_range_click_timeout);
  2168. ObjectTypeDB::bind_method(_MD("_input_event"),&Tree::_input_event);
  2169. ObjectTypeDB::bind_method(_MD("_popup_select"),&Tree::popup_select);
  2170. ObjectTypeDB::bind_method(_MD("_text_editor_enter"),&Tree::text_editor_enter);
  2171. ObjectTypeDB::bind_method(_MD("_value_editor_changed"),&Tree::value_editor_changed);
  2172. ObjectTypeDB::bind_method(_MD("_scroll_moved"),&Tree::_scroll_moved);
  2173. ObjectTypeDB::bind_method(_MD("clear"),&Tree::clear);
  2174. ObjectTypeDB::bind_method(_MD("create_item:TreeItem","parent:TreeItem"),&Tree::_create_item,DEFVAL((Object*)NULL));
  2175. ObjectTypeDB::bind_method(_MD("get_root:TreeItem"),&Tree::get_root);
  2176. ObjectTypeDB::bind_method(_MD("set_column_min_width","column","min_width"),&Tree::set_column_min_width);
  2177. ObjectTypeDB::bind_method(_MD("set_column_expand","column","expand"),&Tree::set_column_expand);
  2178. ObjectTypeDB::bind_method(_MD("get_column_width","column"),&Tree::get_column_width);
  2179. ObjectTypeDB::bind_method(_MD("set_hide_root","enable"),&Tree::set_hide_root);
  2180. ObjectTypeDB::bind_method(_MD("get_next_selected:TreeItem","from:TreeItem"),&Tree::_get_next_selected);
  2181. ObjectTypeDB::bind_method(_MD("get_selected:TreeItem"),&Tree::get_selected);
  2182. ObjectTypeDB::bind_method(_MD("get_selected_column"),&Tree::get_selected_column);
  2183. ObjectTypeDB::bind_method(_MD("get_pressed_button"),&Tree::get_pressed_button);
  2184. ObjectTypeDB::bind_method(_MD("set_select_mode","mode"),&Tree::set_select_mode);
  2185. ObjectTypeDB::bind_method(_MD("set_columns","amount"),&Tree::set_columns);
  2186. ObjectTypeDB::bind_method(_MD("get_columns"),&Tree::get_columns);
  2187. ObjectTypeDB::bind_method(_MD("get_edited:TreeItem"),&Tree::get_edited);
  2188. ObjectTypeDB::bind_method(_MD("get_edited_column"),&Tree::get_edited_column);
  2189. ObjectTypeDB::bind_method(_MD("get_custom_popup_rect"),&Tree::get_custom_popup_rect);
  2190. ObjectTypeDB::bind_method(_MD("get_item_area_rect","item:TreeItem","column"),&Tree::_get_item_rect,DEFVAL(-1));
  2191. ObjectTypeDB::bind_method(_MD("ensure_cursor_is_visible"),&Tree::ensure_cursor_is_visible);
  2192. ObjectTypeDB::bind_method(_MD("set_column_titles_visible","visible"),&Tree::set_column_titles_visible);
  2193. ObjectTypeDB::bind_method(_MD("are_column_titles_visible"),&Tree::are_column_titles_visible);
  2194. ObjectTypeDB::bind_method(_MD("set_column_title","column","title"),&Tree::set_column_title);
  2195. ObjectTypeDB::bind_method(_MD("get_column_title","column"),&Tree::get_column_title);
  2196. ObjectTypeDB::bind_method(_MD("get_scroll"),&Tree::get_scroll);
  2197. ObjectTypeDB::bind_method(_MD("set_hide_folding","hide"),&Tree::set_hide_folding);
  2198. ObjectTypeDB::bind_method(_MD("is_folding_hidden"),&Tree::is_folding_hidden);
  2199. ADD_SIGNAL( MethodInfo("item_selected"));
  2200. ADD_SIGNAL( MethodInfo("cell_selected"));
  2201. ADD_SIGNAL( MethodInfo("multi_selected",PropertyInfo(Variant::OBJECT,"item"),PropertyInfo(Variant::INT,"column"),PropertyInfo(Variant::BOOL,"selected")) );
  2202. ADD_SIGNAL( MethodInfo("item_edited"));
  2203. ADD_SIGNAL( MethodInfo("item_collapsed",PropertyInfo(Variant::OBJECT,"item")));
  2204. //ADD_SIGNAL( MethodInfo("item_doubleclicked" ) );
  2205. ADD_SIGNAL( MethodInfo("button_pressed",PropertyInfo(Variant::OBJECT,"item"),PropertyInfo(Variant::INT,"column"),PropertyInfo(Variant::INT,"id")));
  2206. ADD_SIGNAL( MethodInfo("custom_popup_edited",PropertyInfo(Variant::BOOL,"arrow_clicked") ) );
  2207. ADD_SIGNAL( MethodInfo("item_activated"));
  2208. BIND_CONSTANT( SELECT_SINGLE );
  2209. BIND_CONSTANT( SELECT_ROW );
  2210. BIND_CONSTANT( SELECT_MULTI );
  2211. }
  2212. Tree::Tree() {
  2213. selected_col=0;
  2214. columns.resize(1);
  2215. selected_item=NULL;
  2216. edited_item=NULL;
  2217. selected_col=-1;
  2218. edited_col=-1;
  2219. hide_root=false;
  2220. select_mode=SELECT_SINGLE;
  2221. root=0;
  2222. popup_menu=NULL;
  2223. popup_edited_item=NULL;
  2224. text_editor=NULL;
  2225. set_focus_mode(FOCUS_ALL);
  2226. popup_menu = memnew( PopupMenu );
  2227. popup_menu->hide();
  2228. add_child(popup_menu);
  2229. popup_menu->set_as_toplevel(true);
  2230. text_editor = memnew( LineEdit );
  2231. add_child(text_editor);
  2232. text_editor->set_as_toplevel(true);
  2233. text_editor->hide();
  2234. value_editor = memnew( HSlider );
  2235. add_child(value_editor);
  2236. value_editor->set_as_toplevel(true);
  2237. value_editor->hide();
  2238. h_scroll = memnew( HScrollBar );
  2239. v_scroll = memnew( VScrollBar );
  2240. add_child(h_scroll);
  2241. add_child(v_scroll);
  2242. range_click_timer = memnew( Timer );
  2243. range_click_timer->connect("timeout",this,"_range_click_timeout");
  2244. add_child(range_click_timer);
  2245. h_scroll->connect("value_changed", this,"_scroll_moved");
  2246. v_scroll->connect("value_changed", this,"_scroll_moved");
  2247. text_editor->connect("text_entered", this,"_text_editor_enter");
  2248. popup_menu->connect("item_pressed", this,"_popup_select");
  2249. value_editor->connect("value_changed", this,"_value_editor_changed");
  2250. value_editor->set_as_toplevel(true);
  2251. text_editor->set_as_toplevel(true);
  2252. updating_value_editor=false;
  2253. pressed_button=-1;
  2254. show_column_titles=false;
  2255. cache.click_type = Cache::CLICK_NONE;
  2256. cache.hover_type = Cache::CLICK_NONE;
  2257. cache.hover_index = -1;
  2258. cache.click_index=-1;
  2259. cache.click_id=-1;
  2260. cache.click_item=NULL;
  2261. cache.click_column=0;
  2262. last_keypress=0;
  2263. focus_in_id=0;
  2264. blocked=0;
  2265. cursor_can_exit_tree=true;
  2266. set_stop_mouse(true);
  2267. drag_speed=0;
  2268. drag_touching=false;
  2269. drag_touching_deaccel=false;
  2270. pressing_for_editor=false;
  2271. range_drag_enabled=false;
  2272. hide_folding=false;
  2273. }
  2274. Tree::~Tree() {
  2275. if (root) {
  2276. memdelete( root );
  2277. }
  2278. }