tabs.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*************************************************************************/
  2. /* tabs.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "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. // h+=MIN( get_constant("label_valign_fg"), get_constant("label_valign_bg") );
  37. for(int i=0;i<tabs.size();i++) {
  38. Ref<Texture> tex = tabs[i].icon;
  39. if (tex.is_valid()) {
  40. ms.height = MAX( ms.height, tex->get_size().height );
  41. if (tabs[i].text!="")
  42. ms.width+=get_constant("hseparation");
  43. }
  44. ms.width+=font->get_string_size(tabs[i].text).width;
  45. if (current==i)
  46. ms.width+=tab_fg->get_minimum_size().width;
  47. else
  48. ms.width+=tab_bg->get_minimum_size().width;
  49. }
  50. return ms;
  51. }
  52. void Tabs::_input_event(const InputEvent& p_event) {
  53. if (p_event.type==InputEvent::MOUSE_BUTTON &&
  54. p_event.mouse_button.pressed &&
  55. p_event.mouse_button.button_index==BUTTON_LEFT) {
  56. // clicks
  57. Point2 pos( p_event.mouse_button.x, p_event.mouse_button.y );
  58. int found=-1;
  59. for(int i=0;i<tabs.size();i++) {
  60. int ofs=tabs[i].ofs_cache;
  61. if (pos.x < ofs) {
  62. found=i;
  63. break;
  64. }
  65. }
  66. if (found!=-1) {
  67. set_current_tab(found);
  68. }
  69. }
  70. }
  71. void Tabs::_notification(int p_what) {
  72. switch(p_what) {
  73. case NOTIFICATION_DRAW: {
  74. RID ci = get_canvas_item();
  75. Ref<StyleBox> tab_bg = get_stylebox("tab_bg");
  76. Ref<StyleBox> tab_fg = get_stylebox("tab_fg");
  77. Ref<Font> font = get_font("font");
  78. Color color_fg = get_color("font_color_fg");
  79. Color color_bg = get_color("font_color_bg");
  80. int h = get_size().height;
  81. int label_valign_fg = get_constant("label_valign_fg");
  82. int label_valign_bg = get_constant("label_valign_bg");
  83. int w=0;
  84. for(int i=0;i<tabs.size();i++) {
  85. String s = tabs[i].text;
  86. int lsize=0;
  87. int slen=font->get_string_size(s).width;;
  88. lsize+=slen;
  89. Ref<Texture> icon;
  90. if (tabs[i].icon.is_valid()) {
  91. Ref<Texture> icon = tabs[i].icon;
  92. if (icon.is_valid()) {
  93. lsize+=icon->get_width();
  94. if (s!="")
  95. lsize+=get_constant("hseparation");
  96. }
  97. }
  98. Ref<StyleBox> sb;
  99. int va;
  100. Color col;
  101. if (i==current) {
  102. sb=tab_fg;
  103. va=label_valign_fg;
  104. col=color_fg;
  105. } else {
  106. sb=tab_bg;
  107. va=label_valign_bg;
  108. col=color_bg;
  109. }
  110. Size2i sb_ms = sb->get_minimum_size();
  111. Rect2 sb_rect = Rect2( w, 0, lsize+sb_ms.width, h);
  112. sb->draw(ci, sb_rect );
  113. w+=sb->get_margin(MARGIN_LEFT);
  114. if (icon.is_valid()) {
  115. icon->draw(ci, Point2i( w, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-icon->get_height())/2 ) );
  116. if (s!="")
  117. w+=icon->get_width()+get_constant("hseparation");
  118. }
  119. font->draw(ci, Point2i( w, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-font->get_height())/2+font->get_ascent() ), s, col );
  120. w+=slen+sb->get_margin(MARGIN_RIGHT);
  121. tabs[i].ofs_cache=w;
  122. }
  123. } break;
  124. }
  125. }
  126. int Tabs::get_tab_count() const {
  127. return tabs.size();
  128. }
  129. void Tabs::set_current_tab(int p_current) {
  130. ERR_FAIL_INDEX( p_current, get_tab_count() );
  131. //printf("DEBUG %p: set_current_tab to %i\n", this, p_current);
  132. current=p_current;
  133. _change_notify("current_tab");
  134. emit_signal("tab_changed",current);
  135. update();
  136. }
  137. int Tabs::get_current_tab() const {
  138. return current;
  139. }
  140. void Tabs::set_tab_title(int p_tab,const String& p_title) {
  141. ERR_FAIL_INDEX(p_tab,tabs.size());
  142. tabs[p_tab].text=p_title;
  143. update();
  144. minimum_size_changed();
  145. }
  146. String Tabs::get_tab_title(int p_tab) const{
  147. ERR_FAIL_INDEX_V(p_tab,tabs.size(),"");
  148. return tabs[p_tab].text;
  149. }
  150. void Tabs::set_tab_icon(int p_tab,const Ref<Texture>& p_icon){
  151. ERR_FAIL_INDEX(p_tab,tabs.size());
  152. tabs[p_tab].icon=p_icon;
  153. update();
  154. minimum_size_changed();
  155. }
  156. Ref<Texture> Tabs::get_tab_icon(int p_tab) const{
  157. ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref<Texture>());
  158. return tabs[p_tab].icon;
  159. }
  160. void Tabs::add_tab(const String& p_str,const Ref<Texture>& p_icon) {
  161. Tab t;
  162. t.text=p_str;
  163. t.icon=p_icon;
  164. tabs.push_back(t);
  165. update();
  166. minimum_size_changed();
  167. }
  168. void Tabs::remove_tab(int p_idx) {
  169. ERR_FAIL_INDEX(p_idx,tabs.size());
  170. tabs.remove(p_idx);
  171. if (current>=p_idx)
  172. current--;
  173. update();
  174. minimum_size_changed();
  175. if (current<0)
  176. current=0;
  177. if (current>=tabs.size())
  178. current=tabs.size()-1;
  179. emit_signal("tab_changed",current);
  180. }
  181. void Tabs::_bind_methods() {
  182. ObjectTypeDB::bind_method(_MD("_input_event"),&Tabs::_input_event);
  183. ObjectTypeDB::bind_method(_MD("get_tab_count"),&Tabs::get_tab_count);
  184. ObjectTypeDB::bind_method(_MD("set_current_tab","tab_idx"),&Tabs::set_current_tab);
  185. ObjectTypeDB::bind_method(_MD("get_current_tab"),&Tabs::get_current_tab);
  186. ObjectTypeDB::bind_method(_MD("set_tab_title","tab_idx","title"),&Tabs::set_tab_title);
  187. ObjectTypeDB::bind_method(_MD("get_tab_title","tab_idx"),&Tabs::get_tab_title);
  188. ObjectTypeDB::bind_method(_MD("set_tab_icon","tab_idx","icon:Texture"),&Tabs::set_tab_icon);
  189. ObjectTypeDB::bind_method(_MD("get_tab_icon:Texture","tab_idx"),&Tabs::get_tab_icon);
  190. ObjectTypeDB::bind_method(_MD("remove_tab","tab_idx","icon:Texture"),&Tabs::remove_tab);
  191. ADD_SIGNAL(MethodInfo("tab_changed",PropertyInfo(Variant::INT,"tab")));
  192. ADD_PROPERTY( PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE,"-1,4096,1",PROPERTY_USAGE_EDITOR), _SCS("set_current_tab"), _SCS("get_current_tab") );
  193. }
  194. Tabs::Tabs() {
  195. current=0;
  196. }