tab_bar.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/resources/text_line.h"
  34. class TabBar : public Control {
  35. GDCLASS(TabBar, Control);
  36. public:
  37. enum AlignmentMode {
  38. ALIGNMENT_LEFT,
  39. ALIGNMENT_CENTER,
  40. ALIGNMENT_RIGHT,
  41. ALIGNMENT_MAX,
  42. };
  43. enum CloseButtonDisplayPolicy {
  44. CLOSE_BUTTON_SHOW_NEVER,
  45. CLOSE_BUTTON_SHOW_ACTIVE_ONLY,
  46. CLOSE_BUTTON_SHOW_ALWAYS,
  47. CLOSE_BUTTON_MAX
  48. };
  49. private:
  50. struct Tab {
  51. String text;
  52. String language;
  53. Control::TextDirection text_direction = Control::TEXT_DIRECTION_INHERITED;
  54. Ref<TextLine> text_buf;
  55. Ref<Texture2D> icon;
  56. int icon_max_width = 0;
  57. bool disabled = false;
  58. bool hidden = false;
  59. Variant metadata;
  60. int ofs_cache = 0;
  61. int size_cache = 0;
  62. int size_text = 0;
  63. Ref<Texture2D> right_button;
  64. Rect2 rb_rect;
  65. Rect2 cb_rect;
  66. Tab() {
  67. text_buf.instantiate();
  68. }
  69. };
  70. int offset = 0;
  71. int max_drawn_tab = 0;
  72. int highlight_arrow = -1;
  73. bool buttons_visible = false;
  74. bool missing_right = false;
  75. Vector<Tab> tabs;
  76. int current = 0;
  77. int previous = 0;
  78. AlignmentMode tab_alignment = ALIGNMENT_LEFT;
  79. bool clip_tabs = true;
  80. int rb_hover = -1;
  81. bool rb_pressing = false;
  82. bool select_with_rmb = false;
  83. int cb_hover = -1;
  84. bool cb_pressing = false;
  85. CloseButtonDisplayPolicy cb_displaypolicy = CLOSE_BUTTON_SHOW_NEVER;
  86. int hover = -1; // Hovered tab.
  87. int max_width = 0;
  88. bool scrolling_enabled = true;
  89. bool drag_to_rearrange_enabled = false;
  90. bool dragging_valid_tab = false;
  91. bool scroll_to_selected = true;
  92. int tabs_rearrange_group = -1;
  93. const float DEFAULT_GAMEPAD_EVENT_DELAY_MS = 0.5;
  94. const float GAMEPAD_EVENT_REPEAT_RATE_MS = 1.0 / 20;
  95. float gamepad_event_delay_ms = DEFAULT_GAMEPAD_EVENT_DELAY_MS;
  96. struct ThemeCache {
  97. int h_separation = 0;
  98. int icon_max_width = 0;
  99. Ref<StyleBox> tab_unselected_style;
  100. Ref<StyleBox> tab_hovered_style;
  101. Ref<StyleBox> tab_selected_style;
  102. Ref<StyleBox> tab_disabled_style;
  103. Ref<StyleBox> tab_focus_style;
  104. Ref<Texture2D> increment_icon;
  105. Ref<Texture2D> increment_hl_icon;
  106. Ref<Texture2D> decrement_icon;
  107. Ref<Texture2D> decrement_hl_icon;
  108. Ref<Texture2D> drop_mark_icon;
  109. Color drop_mark_color;
  110. Ref<Font> font;
  111. int font_size;
  112. int outline_size = 0;
  113. Color font_selected_color;
  114. Color font_hovered_color;
  115. Color font_unselected_color;
  116. Color font_disabled_color;
  117. Color font_outline_color;
  118. Ref<Texture2D> close_icon;
  119. Ref<StyleBox> button_pressed_style;
  120. Ref<StyleBox> button_hl_style;
  121. } theme_cache;
  122. int get_tab_width(int p_idx) const;
  123. Size2 _get_tab_icon_size(int p_idx) const;
  124. void _ensure_no_over_offset();
  125. void _update_hover();
  126. void _update_cache(bool p_update_hover = true);
  127. void _on_mouse_exited();
  128. void _shape(int p_tab);
  129. void _draw_tab(Ref<StyleBox> &p_tab_style, Color &p_font_color, int p_index, float p_x, bool p_focus);
  130. protected:
  131. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  132. bool _set(const StringName &p_name, const Variant &p_value);
  133. bool _get(const StringName &p_name, Variant &r_ret) const;
  134. void _get_property_list(List<PropertyInfo> *p_list) const;
  135. void _notification(int p_what);
  136. static void _bind_methods();
  137. Variant get_drag_data(const Point2 &p_point) override;
  138. bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  139. void drop_data(const Point2 &p_point, const Variant &p_data) override;
  140. void _move_tab_from(TabBar *p_from_tabbar, int p_from_index, int p_to_index);
  141. public:
  142. Variant _handle_get_drag_data(const String &p_type, const Point2 &p_point);
  143. bool _handle_can_drop_data(const String &p_type, const Point2 &p_point, const Variant &p_data) const;
  144. 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);
  145. void add_tab(const String &p_str = "", const Ref<Texture2D> &p_icon = Ref<Texture2D>());
  146. void set_tab_title(int p_tab, const String &p_title);
  147. String get_tab_title(int p_tab) const;
  148. void set_tab_text_direction(int p_tab, TextDirection p_text_direction);
  149. TextDirection get_tab_text_direction(int p_tab) const;
  150. void set_tab_language(int p_tab, const String &p_language);
  151. String get_tab_language(int p_tab) const;
  152. void set_tab_icon(int p_tab, const Ref<Texture2D> &p_icon);
  153. Ref<Texture2D> get_tab_icon(int p_tab) const;
  154. void set_tab_icon_max_width(int p_tab, int p_width);
  155. int get_tab_icon_max_width(int p_tab) const;
  156. void set_tab_disabled(int p_tab, bool p_disabled);
  157. bool is_tab_disabled(int p_tab) const;
  158. void set_tab_hidden(int p_tab, bool p_hidden);
  159. bool is_tab_hidden(int p_tab) const;
  160. void set_tab_metadata(int p_tab, const Variant &p_metadata);
  161. Variant get_tab_metadata(int p_tab) const;
  162. void set_tab_button_icon(int p_tab, const Ref<Texture2D> &p_icon);
  163. Ref<Texture2D> get_tab_button_icon(int p_tab) const;
  164. int get_tab_idx_at_point(const Point2 &p_point) const;
  165. void set_tab_alignment(AlignmentMode p_alignment);
  166. AlignmentMode get_tab_alignment() const;
  167. void set_clip_tabs(bool p_clip_tabs);
  168. bool get_clip_tabs() const;
  169. void move_tab(int p_from, int p_to);
  170. void set_tab_close_display_policy(CloseButtonDisplayPolicy p_policy);
  171. CloseButtonDisplayPolicy get_tab_close_display_policy() const;
  172. void set_tab_count(int p_count);
  173. int get_tab_count() const;
  174. void set_current_tab(int p_current);
  175. int get_current_tab() const;
  176. int get_previous_tab() const;
  177. int get_hovered_tab() const;
  178. bool select_previous_available();
  179. bool select_next_available();
  180. int get_tab_offset() const;
  181. bool get_offset_buttons_visible() const;
  182. void remove_tab(int p_idx);
  183. void clear_tabs();
  184. void set_scrolling_enabled(bool p_enabled);
  185. bool get_scrolling_enabled() const;
  186. void set_drag_to_rearrange_enabled(bool p_enabled);
  187. bool get_drag_to_rearrange_enabled() const;
  188. void set_tabs_rearrange_group(int p_group_id);
  189. int get_tabs_rearrange_group() const;
  190. void set_scroll_to_selected(bool p_enabled);
  191. bool get_scroll_to_selected() const;
  192. void set_select_with_rmb(bool p_enabled);
  193. bool get_select_with_rmb() const;
  194. void ensure_tab_visible(int p_idx);
  195. void set_max_tab_width(int p_width);
  196. int get_max_tab_width() const;
  197. Rect2 get_tab_rect(int p_tab) const;
  198. Size2 get_minimum_size() const override;
  199. TabBar();
  200. };
  201. VARIANT_ENUM_CAST(TabBar::AlignmentMode);
  202. VARIANT_ENUM_CAST(TabBar::CloseButtonDisplayPolicy);
  203. #endif // TAB_BAR_H