2
0

tab_bar.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**************************************************************************/
  2. /* tab_bar.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TAB_BAR_H
  31. #define TAB_BAR_H
  32. #include "scene/gui/control.h"
  33. #include "scene/property_list_helper.h"
  34. #include "scene/resources/text_line.h"
  35. class TabBar : public Control {
  36. GDCLASS(TabBar, Control);
  37. public:
  38. enum AlignmentMode {
  39. ALIGNMENT_LEFT,
  40. ALIGNMENT_CENTER,
  41. ALIGNMENT_RIGHT,
  42. ALIGNMENT_MAX,
  43. };
  44. enum CloseButtonDisplayPolicy {
  45. CLOSE_BUTTON_SHOW_NEVER,
  46. CLOSE_BUTTON_SHOW_ACTIVE_ONLY,
  47. CLOSE_BUTTON_SHOW_ALWAYS,
  48. CLOSE_BUTTON_MAX
  49. };
  50. private:
  51. struct Tab {
  52. String text;
  53. String tooltip;
  54. String language;
  55. Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
  56. Ref<TextLine> text_buf;
  57. Ref<Texture2D> icon;
  58. int icon_max_width = 0;
  59. bool disabled = false;
  60. bool hidden = false;
  61. bool truncated = false;
  62. Variant metadata;
  63. int ofs_cache = 0;
  64. int size_cache = 0;
  65. int size_text = 0;
  66. Ref<Texture2D> right_button;
  67. Rect2 rb_rect;
  68. Rect2 cb_rect;
  69. Tab() {
  70. text_buf.instantiate();
  71. }
  72. Tab(bool p_dummy) {}
  73. };
  74. static inline PropertyListHelper base_property_helper;
  75. PropertyListHelper property_helper;
  76. int offset = 0;
  77. int max_drawn_tab = 0;
  78. int highlight_arrow = -1;
  79. bool buttons_visible = false;
  80. bool missing_right = false;
  81. Vector<Tab> tabs;
  82. int current = -1;
  83. int previous = -1;
  84. AlignmentMode tab_alignment = ALIGNMENT_LEFT;
  85. bool clip_tabs = true;
  86. int rb_hover = -1;
  87. bool rb_pressing = false;
  88. bool tab_style_v_flip = false;
  89. bool select_with_rmb = false;
  90. bool deselect_enabled = false;
  91. int cb_hover = -1;
  92. bool cb_pressing = false;
  93. CloseButtonDisplayPolicy cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
  94. int hover = -1; // Hovered tab.
  95. int max_width = 0;
  96. bool scrolling_enabled = true;
  97. bool drag_to_rearrange_enabled = false;
  98. bool dragging_valid_tab = false;
  99. bool scroll_to_selected = true;
  100. int tabs_rearrange_group = -1;
  101. static const int CURRENT_TAB_UNINITIALIZED = -2;
  102. bool initialized = false;
  103. int queued_current = CURRENT_TAB_UNINITIALIZED;
  104. const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
  105. const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
  106. float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  107. struct ThemeCache {
  108. int h_separation = 0;
  109. int icon_max_width = 0;
  110. Ref<StyleBox> tab_unselected_style;
  111. Ref<StyleBox> tab_hovered_style;
  112. Ref<StyleBox> tab_selected_style;
  113. Ref<StyleBox> tab_disabled_style;
  114. Ref<StyleBox> tab_focus_style;
  115. Ref<Texture2D> increment_icon;
  116. Ref<Texture2D> increment_hl_icon;
  117. Ref<Texture2D> decrement_icon;
  118. Ref<Texture2D> decrement_hl_icon;
  119. Ref<Texture2D> drop_mark_icon;
  120. Color drop_mark_color;
  121. Ref<Font> font;
  122. int font_size;
  123. int outline_size = 0;
  124. Color font_selected_color;
  125. Color font_hovered_color;
  126. Color font_unselected_color;
  127. Color font_disabled_color;
  128. Color font_outline_color;
  129. Ref<Texture2D> close_icon;
  130. Ref<StyleBox> button_pressed_style;
  131. Ref<StyleBox> button_hl_style;
  132. } theme_cache;
  133. int get_tab_width(int p_idx) const;
  134. Size2 _get_tab_icon_size(int p_idx) const;
  135. void _ensure_no_over_offset();
  136. bool _can_deselect() const;
  137. void _update_hover();
  138. void _update_cache(bool p_update_hover = true);
  139. void _on_mouse_exited();
  140. void _shape(int p_tab);
  141. void _draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x, bool p_focus);
  142. protected:
  143. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  144. virtual String get_tooltip(const Point2 &p_pos) const override;
  145. bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
  146. bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
  147. void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
  148. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  149. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  150. void _notification(int p_what);
  151. static void _bind_methods();
  152. Variant get_drag_data(const Point2 &p_point) override;
  153. bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  154. void drop_data(const Point2 &p_point, const Variant &p_data) override;
  155. void _move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
  156. public:
  157. Variant _handle_get_drag_data(const String &p_type, const Point2 &p_point);
  158. bool _handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const;
  159. void _handle_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data, const Callable &p_move_tab_callback, const Callable &p_move_tab_from_other_callback);
  160. void add_tab(const String &p_str = "", const Ref<Texture2D> &p_icon = Ref<Texture2D>());
  161. void set_tab_title(int p_tab, const String &p_title);
  162. String get_tab_title(int p_tab) const;
  163. void set_tab_tooltip(int p_tab, const String &p_tooltip);
  164. String get_tab_tooltip(int p_tab) const;
  165. void set_tab_text_direction(int p_tab, TextDirection p_text_direction);
  166. TextDirection get_tab_text_direction(int p_tab) const;
  167. void set_tab_language(int p_tab, const String &p_language);
  168. String get_tab_language(int p_tab) const;
  169. void set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon);
  170. Ref<Texture2D> get_tab_icon(int p_tab) const;
  171. void set_tab_icon_max_width(int p_tab, int p_width);
  172. int get_tab_icon_max_width(int p_tab) const;
  173. void set_tab_disabled(int p_tab, bool p_disabled);
  174. bool is_tab_disabled(int p_tab) const;
  175. void set_tab_hidden(int p_tab, bool p_hidden);
  176. bool is_tab_hidden(int p_tab) const;
  177. void set_tab_metadata(int p_tab, const Variant &p_metadata);
  178. Variant get_tab_metadata(int p_tab) const;
  179. void set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon);
  180. Ref<Texture2D> get_tab_button_icon(int p_tab) const;
  181. int get_tab_idx_at_point(const Point2 &p_point) const;
  182. void set_tab_alignment(AlignmentMode p_alignment);
  183. AlignmentMode get_tab_alignment() const;
  184. void set_clip_tabs(bool p_clip_tabs);
  185. bool get_clip_tabs() const;
  186. void set_tab_style_v_flip(bool p_tab_style_v_flip);
  187. void move_tab(int p_from, int p_to);
  188. void set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy);
  189. CloseButtonDisplayPolicy get_tab_close_display_policy() const;
  190. void set_tab_count(int p_count);
  191. int get_tab_count() const;
  192. void set_current_tab(int p_current);
  193. int get_current_tab() const;
  194. int get_previous_tab() const;
  195. int get_hovered_tab() const;
  196. bool select_previous_available();
  197. bool select_next_available();
  198. void set_tab_offset(int p_offset);
  199. int get_tab_offset() const;
  200. bool get_offset_buttons_visible() const;
  201. void remove_tab(int p_idx);
  202. void clear_tabs();
  203. void set_scrolling_enabled(bool p_enabled);
  204. bool get_scrolling_enabled() const;
  205. void set_drag_to_rearrange_enabled(bool p_enabled);
  206. bool get_drag_to_rearrange_enabled() const;
  207. void set_tabs_rearrange_group(int p_group_id);
  208. int get_tabs_rearrange_group() const;
  209. void set_scroll_to_selected(bool p_enabled);
  210. bool get_scroll_to_selected() const;
  211. void set_select_with_rmb(bool p_enabled);
  212. bool get_select_with_rmb() const;
  213. void set_deselect_enabled(bool p_enabled);
  214. bool get_deselect_enabled() const;
  215. void ensure_tab_visible(int p_idx);
  216. void set_max_tab_width(int p_width);
  217. int get_max_tab_width() const;
  218. Rect2 get_tab_rect(int p_tab) const;
  219. Size2 get_minimum_size() const override;
  220. TabBar();
  221. };
  222. VARIANT_ENUM_CAST(TabBar::AlignmentMode);
  223. VARIANT_ENUM_CAST(TabBar::CloseButtonDisplayPolicy);
  224. #endif // TAB_BAR_H