tabs.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*************************************************************************/
  2. /* tabs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "tabs.h"
  30. #include "message_queue.h"
  31. Size2 Tabs::get_minimum_size() const {
  32. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  33. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  34. Ref<Font> font = get_font("font");
  35. Size2 ms(0, MAX(tab_bg->get_minimum_size().height, tab_fg->get_minimum_size().height)+font->get_height());
  36. for(int i=0;i<tabs.size();i++) {
  37. Ref<Texture> tex = tabs[i].icon;
  38. if (tex.is_valid()) {
  39. ms.height = MAX(ms.height, tex->get_size().height);
  40. if (tabs[i].text!="")
  41. ms.width+=get_constant("hseparation");
  42. }
  43. ms.width+=font->get_string_size(tabs[i].text).width;
  44. if (current==i)
  45. ms.width+=tab_fg->get_minimum_size().width;
  46. else
  47. ms.width+=tab_bg->get_minimum_size().width;
  48. if (tabs[i].right_button.is_valid()) {
  49. Ref<Texture> rb=tabs[i].right_button;
  50. Size2 bms = rb->get_size();
  51. bms.width+=get_constant("hseparation");
  52. ms.width+=bms.width;
  53. ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height);
  54. }
  55. if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) {
  56. Ref<Texture> cb=get_icon("close");
  57. Size2 bms = cb->get_size();
  58. bms.width+=get_constant("hseparation");
  59. ms.width+=bms.width;
  60. ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height);
  61. }
  62. }
  63. ms.width=0; //TODO: should make this optional
  64. return ms;
  65. }
  66. void Tabs::_input_event(const InputEvent& p_event) {
  67. if (p_event.type==InputEvent::MOUSE_MOTION) {
  68. Point2 pos( p_event.mouse_motion.x, p_event.mouse_motion.y );
  69. hilite_arrow=-1;
  70. if (buttons_visible) {
  71. Ref<Texture> incr = get_icon("increment");
  72. Ref<Texture> decr = get_icon("decrement");
  73. int limit=get_size().width-incr->get_width()-decr->get_width();
  74. if (pos.x>limit+decr->get_width()) {
  75. hilite_arrow=1;
  76. } else if (pos.x>limit) {
  77. hilite_arrow=0;
  78. }
  79. }
  80. // test hovering to display right or close button
  81. int hover_buttons=-1;
  82. hover=-1;
  83. for(int i=0;i<tabs.size();i++) {
  84. if (i<offset)
  85. continue;
  86. if (tabs[i].rb_rect.has_point(pos)) {
  87. rb_hover=i;
  88. cb_hover=-1;
  89. hover_buttons = i;
  90. break;
  91. }
  92. else if (tabs[i].cb_rect.has_point(pos)) {
  93. cb_hover=i;
  94. rb_hover=-1;
  95. hover_buttons = i;
  96. break;
  97. }
  98. }
  99. if (hover_buttons == -1) { // no hover
  100. rb_hover= hover_buttons;
  101. cb_hover= hover_buttons;
  102. }
  103. update();
  104. return;
  105. }
  106. if (rb_pressing && p_event.type==InputEvent::MOUSE_BUTTON &&
  107. !p_event.mouse_button.pressed &&
  108. p_event.mouse_button.button_index==BUTTON_LEFT) {
  109. if (rb_hover!=-1) {
  110. //pressed
  111. emit_signal("right_button_pressed",rb_hover);
  112. }
  113. rb_pressing=false;
  114. update();
  115. }
  116. if (cb_pressing && p_event.type==InputEvent::MOUSE_BUTTON &&
  117. !p_event.mouse_button.pressed &&
  118. p_event.mouse_button.button_index==BUTTON_LEFT) {
  119. if (cb_hover!=-1) {
  120. //pressed
  121. emit_signal("tab_close",cb_hover);
  122. }
  123. cb_pressing=false;
  124. update();
  125. }
  126. if (p_event.type==InputEvent::MOUSE_BUTTON &&
  127. p_event.mouse_button.pressed &&
  128. p_event.mouse_button.button_index==BUTTON_LEFT) {
  129. // clicks
  130. Point2 pos( p_event.mouse_button.x, p_event.mouse_button.y );
  131. if (buttons_visible) {
  132. Ref<Texture> incr = get_icon("increment");
  133. Ref<Texture> decr = get_icon("decrement");
  134. int limit=get_size().width-incr->get_width()-decr->get_width();
  135. if (pos.x>limit+decr->get_width()) {
  136. if (missing_right) {
  137. offset++;
  138. update();
  139. }
  140. return;
  141. } else if (pos.x>limit) {
  142. if (offset>0) {
  143. offset--;
  144. update();
  145. }
  146. return;
  147. }
  148. }
  149. int found=-1;
  150. for(int i=0;i<tabs.size();i++) {
  151. if (i<offset)
  152. continue;
  153. if (tabs[i].rb_rect.has_point(pos)) {
  154. rb_pressing=true;
  155. update();
  156. return;
  157. }
  158. if (tabs[i].cb_rect.has_point(pos)) {
  159. cb_pressing=true;
  160. update();
  161. return;
  162. }
  163. if (pos.x >=tabs[i].ofs_cache && pos.x<tabs[i].ofs_cache+tabs[i].size_cache) {
  164. found=i;
  165. break;
  166. }
  167. }
  168. if (found!=-1) {
  169. set_current_tab(found);
  170. emit_signal("tab_changed",found);
  171. }
  172. }
  173. }
  174. void Tabs::_notification(int p_what) {
  175. switch(p_what) {
  176. case NOTIFICATION_MOUSE_EXIT: {
  177. rb_hover=-1;
  178. cb_hover=-1;
  179. hover=-1;
  180. update();
  181. } break;
  182. case NOTIFICATION_RESIZED: {
  183. _ensure_no_over_offset();
  184. } break;
  185. case NOTIFICATION_DRAW: {
  186. RID ci = get_canvas_item();
  187. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  188. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  189. Ref<Font> font = get_font("font");
  190. Color color_fg = get_color("font_color_fg");
  191. Color color_bg = get_color("font_color_bg");
  192. Ref<Texture> close=get_icon("close");
  193. int h = get_size().height;
  194. int w = 0;
  195. int mw = 0;
  196. for(int i=0;i<tabs.size();i++) {
  197. tabs[i].ofs_cache = mw;
  198. mw += get_tab_width(i);
  199. }
  200. if (tab_align==ALIGN_CENTER) {
  201. w=(get_size().width-mw)/2;
  202. } else if (tab_align==ALIGN_RIGHT) {
  203. w=get_size().width-mw;
  204. }
  205. if (w<0) {
  206. w=0;
  207. }
  208. Ref<Texture> incr = get_icon("increment");
  209. Ref<Texture> decr = get_icon("decrement");
  210. Ref<Texture> incr_hl = get_icon("increment_hilite");
  211. Ref<Texture> decr_hl = get_icon("decrement_hilite");
  212. int limit=get_size().width - incr->get_size().width - decr->get_size().width;
  213. missing_right=false;
  214. for(int i=0;i<tabs.size();i++) {
  215. if (i<offset)
  216. continue;
  217. tabs[i].ofs_cache=w;
  218. int lsize = get_tab_width(i);
  219. String text = tabs[i].text;
  220. int slen = font->get_string_size(text).width;
  221. if (w+lsize > limit) {
  222. max_drawn_tab=i-1;
  223. missing_right=true;
  224. break;
  225. } else {
  226. max_drawn_tab=i;
  227. }
  228. Ref<StyleBox> sb;
  229. Color col;
  230. if (i==current) {
  231. sb=tab_fg;
  232. col=color_fg;
  233. } else {
  234. sb=tab_bg;
  235. col=color_bg;
  236. }
  237. Rect2 sb_rect = Rect2(w, 0, lsize, h);
  238. sb->draw(ci, sb_rect);
  239. w+=sb->get_margin(MARGIN_LEFT);
  240. Size2i sb_ms = sb->get_minimum_size();
  241. Ref<Texture> icon = tabs[i].icon;
  242. if (icon.is_valid()) {
  243. icon->draw(ci, Point2i( w, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-icon->get_height())/2 ) );
  244. if (text!="")
  245. w+=icon->get_width()+get_constant("hseparation");
  246. }
  247. font->draw(ci, Point2i( w, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-font->get_height())/2+font->get_ascent() ), text, col );
  248. w+=slen;
  249. if (tabs[i].right_button.is_valid()) {
  250. Ref<StyleBox> style = get_stylebox("button");
  251. Ref<Texture> rb=tabs[i].right_button;
  252. w+=get_constant("hseparation");
  253. Rect2 rb_rect;
  254. rb_rect.size=style->get_minimum_size()+rb->get_size();
  255. rb_rect.pos.x=w;
  256. rb_rect.pos.y=sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-(rb_rect.size.y))/2;
  257. if (rb_hover==i) {
  258. if (rb_pressing)
  259. get_stylebox("button_pressed")->draw(ci,rb_rect);
  260. else
  261. style->draw(ci,rb_rect);
  262. }
  263. rb->draw(ci,Point2i( w+style->get_margin(MARGIN_LEFT), rb_rect.pos.y+style->get_margin(MARGIN_TOP) ));
  264. w+=rb->get_width();
  265. tabs[i].rb_rect=rb_rect;
  266. }
  267. if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && i==current)) {
  268. Ref<StyleBox> style = get_stylebox("button");
  269. Ref<Texture> cb=close;
  270. w+=get_constant("hseparation");
  271. Rect2 cb_rect;
  272. cb_rect.size=style->get_minimum_size()+cb->get_size();
  273. cb_rect.pos.x=w;
  274. cb_rect.pos.y=sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-(cb_rect.size.y))/2;
  275. if (cb_hover==i) {
  276. if (cb_pressing)
  277. get_stylebox("button_pressed")->draw(ci,cb_rect);
  278. else
  279. style->draw(ci,cb_rect);
  280. }
  281. cb->draw(ci,Point2i( w+style->get_margin(MARGIN_LEFT), cb_rect.pos.y+style->get_margin(MARGIN_TOP) ));
  282. w+=cb->get_width();
  283. tabs[i].cb_rect=cb_rect;
  284. }
  285. w+=sb->get_margin(MARGIN_RIGHT);
  286. tabs[i].size_cache=w-tabs[i].ofs_cache;
  287. }
  288. if (offset>0 || missing_right) {
  289. int vofs = (get_size().height-incr->get_size().height)/2;
  290. if (offset>0)
  291. draw_texture(hilite_arrow==0 ? decr_hl : decr, Point2(limit,vofs));
  292. else
  293. draw_texture(decr,Point2(limit,vofs), Color(1,1,1,0.5));
  294. if (missing_right)
  295. draw_texture(hilite_arrow==1 ? incr_hl : incr, Point2(limit+decr->get_size().width,vofs));
  296. else
  297. draw_texture(incr,Point2(limit+decr->get_size().width,vofs), Color(1,1,1,0.5));
  298. buttons_visible=true;
  299. } else {
  300. buttons_visible=false;
  301. }
  302. } break;
  303. }
  304. }
  305. int Tabs::get_tab_count() const {
  306. return tabs.size();
  307. }
  308. void Tabs::set_current_tab(int p_current) {
  309. ERR_FAIL_INDEX( p_current, get_tab_count() );
  310. current=p_current;
  311. _change_notify("current_tab");
  312. update();
  313. }
  314. int Tabs::get_current_tab() const {
  315. return current;
  316. }
  317. void Tabs::set_tab_title(int p_tab,const String& p_title) {
  318. ERR_FAIL_INDEX(p_tab,tabs.size());
  319. tabs[p_tab].text=p_title;
  320. update();
  321. minimum_size_changed();
  322. }
  323. String Tabs::get_tab_title(int p_tab) const{
  324. ERR_FAIL_INDEX_V(p_tab,tabs.size(),"");
  325. return tabs[p_tab].text;
  326. }
  327. void Tabs::set_tab_icon(int p_tab,const Ref<Texture>& p_icon){
  328. ERR_FAIL_INDEX(p_tab,tabs.size());
  329. tabs[p_tab].icon=p_icon;
  330. update();
  331. minimum_size_changed();
  332. }
  333. Ref<Texture> Tabs::get_tab_icon(int p_tab) const{
  334. ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref<Texture>());
  335. return tabs[p_tab].icon;
  336. }
  337. void Tabs::set_tab_right_button(int p_tab,const Ref<Texture>& p_right_button){
  338. ERR_FAIL_INDEX(p_tab,tabs.size());
  339. tabs[p_tab].right_button=p_right_button;
  340. update();
  341. minimum_size_changed();
  342. }
  343. Ref<Texture> Tabs::get_tab_right_button(int p_tab) const{
  344. ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref<Texture>());
  345. return tabs[p_tab].right_button;
  346. }
  347. void Tabs::add_tab(const String& p_str,const Ref<Texture>& p_icon) {
  348. Tab t;
  349. t.text=p_str;
  350. t.icon=p_icon;
  351. tabs.push_back(t);
  352. update();
  353. minimum_size_changed();
  354. }
  355. void Tabs::clear_tabs() {
  356. tabs.clear();
  357. current=0;
  358. update();
  359. }
  360. void Tabs::remove_tab(int p_idx) {
  361. ERR_FAIL_INDEX(p_idx,tabs.size());
  362. tabs.remove(p_idx);
  363. if (current>=p_idx)
  364. current--;
  365. update();
  366. minimum_size_changed();
  367. if (current<0)
  368. current=0;
  369. if (current>=tabs.size())
  370. current=tabs.size()-1;
  371. _ensure_no_over_offset();
  372. }
  373. void Tabs::set_tab_align(TabAlign p_align) {
  374. tab_align=p_align;
  375. update();
  376. }
  377. Tabs::TabAlign Tabs::get_tab_align() const {
  378. return tab_align;
  379. }
  380. int Tabs::get_tab_width(int p_idx) const {
  381. ERR_FAIL_INDEX_V(p_idx,tabs.size(),0);
  382. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  383. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  384. Ref<Font> font = get_font("font");
  385. int x=0;
  386. Ref<Texture> tex = tabs[p_idx].icon;
  387. if (tex.is_valid()) {
  388. x+=tex->get_width();
  389. if (tabs[p_idx].text!="")
  390. x+=get_constant("hseparation");
  391. }
  392. x+=font->get_string_size(tabs[p_idx].text).width;
  393. if (current==p_idx)
  394. x+=tab_fg->get_minimum_size().width;
  395. else
  396. x+=tab_bg->get_minimum_size().width;
  397. if (tabs[p_idx].right_button.is_valid()) {
  398. Ref<Texture> rb=tabs[p_idx].right_button;
  399. x+=rb->get_width();
  400. x+=get_constant("hseparation");
  401. }
  402. if (cb_displaypolicy==CLOSE_BUTTON_SHOW_ALWAYS || (cb_displaypolicy==CLOSE_BUTTON_SHOW_ACTIVE_ONLY && p_idx==current)) {
  403. Ref<Texture> cb=get_icon("close");
  404. x+=cb->get_width();
  405. x+=get_constant("hseparation");
  406. }
  407. return x;
  408. }
  409. void Tabs::_ensure_no_over_offset() {
  410. if (!is_inside_tree())
  411. return;
  412. Ref<Texture> incr = get_icon("increment");
  413. Ref<Texture> decr = get_icon("decrement");
  414. int limit=get_size().width-incr->get_width()-decr->get_width();
  415. while(offset>0) {
  416. int total_w=0;
  417. for(int i=0;i<tabs.size();i++) {
  418. if (i<offset-1)
  419. continue;
  420. total_w+=get_tab_width(i);
  421. }
  422. if (total_w < limit) {
  423. offset--;
  424. update();
  425. } else {
  426. break;
  427. }
  428. }
  429. }
  430. void Tabs::ensure_tab_visible(int p_idx) {
  431. if (!is_inside_tree())
  432. return;
  433. ERR_FAIL_INDEX(p_idx,tabs.size());
  434. _ensure_no_over_offset();
  435. if (p_idx<=offset) {
  436. offset=p_idx;
  437. update();
  438. return;
  439. }
  440. Ref<Texture> incr = get_icon("increment");
  441. Ref<Texture> decr = get_icon("decrement");
  442. int limit=get_size().width-incr->get_width()-decr->get_width();
  443. int x=0;
  444. for(int i=0;i<tabs.size();i++) {
  445. if (i<offset)
  446. continue;
  447. int sz = get_tab_width(i);
  448. tabs[i].x_cache=x;
  449. tabs[i].x_size_cache=sz;
  450. x+=sz;
  451. }
  452. while(offset<tabs.size() && ( (tabs[p_idx].x_cache + tabs[p_idx].x_size_cache) - tabs[offset].x_cache) > limit) {
  453. offset++;
  454. }
  455. update();
  456. }
  457. void Tabs::set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy) {
  458. cb_displaypolicy=p_policy;
  459. update();
  460. }
  461. void Tabs::_bind_methods() {
  462. ObjectTypeDB::bind_method(_MD("_input_event"),&Tabs::_input_event);
  463. ObjectTypeDB::bind_method(_MD("get_tab_count"),&Tabs::get_tab_count);
  464. ObjectTypeDB::bind_method(_MD("set_current_tab","tab_idx"),&Tabs::set_current_tab);
  465. ObjectTypeDB::bind_method(_MD("get_current_tab"),&Tabs::get_current_tab);
  466. ObjectTypeDB::bind_method(_MD("set_tab_title","tab_idx","title"),&Tabs::set_tab_title);
  467. ObjectTypeDB::bind_method(_MD("get_tab_title","tab_idx"),&Tabs::get_tab_title);
  468. ObjectTypeDB::bind_method(_MD("set_tab_icon","tab_idx","icon:Texture"),&Tabs::set_tab_icon);
  469. ObjectTypeDB::bind_method(_MD("get_tab_icon:Texture","tab_idx"),&Tabs::get_tab_icon);
  470. ObjectTypeDB::bind_method(_MD("remove_tab","tab_idx"),&Tabs::remove_tab);
  471. ObjectTypeDB::bind_method(_MD("add_tab","title","icon:Texture"),&Tabs::add_tab);
  472. ObjectTypeDB::bind_method(_MD("set_tab_align","align"),&Tabs::set_tab_align);
  473. ObjectTypeDB::bind_method(_MD("get_tab_align"),&Tabs::get_tab_align);
  474. ObjectTypeDB::bind_method(_MD("ensure_tab_visible","idx"),&Tabs::ensure_tab_visible);
  475. ADD_SIGNAL(MethodInfo("tab_changed",PropertyInfo(Variant::INT,"tab")));
  476. ADD_SIGNAL(MethodInfo("right_button_pressed",PropertyInfo(Variant::INT,"tab")));
  477. ADD_SIGNAL(MethodInfo("tab_close",PropertyInfo(Variant::INT,"tab")));
  478. ADD_PROPERTY( PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE,"-1,4096,1",PROPERTY_USAGE_EDITOR), _SCS("set_current_tab"), _SCS("get_current_tab") );
  479. BIND_CONSTANT( ALIGN_LEFT );
  480. BIND_CONSTANT( ALIGN_CENTER );
  481. BIND_CONSTANT( ALIGN_RIGHT );
  482. BIND_CONSTANT( CLOSE_BUTTON_SHOW_ACTIVE_ONLY );
  483. BIND_CONSTANT( CLOSE_BUTTON_SHOW_ALWAYS );
  484. BIND_CONSTANT( CLOSE_BUTTON_SHOW_NEVER );
  485. }
  486. Tabs::Tabs() {
  487. current=0;
  488. tab_align=ALIGN_CENTER;
  489. rb_hover=-1;
  490. rb_pressing=false;
  491. hilite_arrow=-1;
  492. cb_hover=-1;
  493. cb_pressing=false;
  494. cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
  495. offset=0;
  496. max_drawn_tab=0;
  497. }