split_container.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**************************************************************************/
  2. /* split_container.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. #pragma once
  31. #include "scene/gui/container.h"
  32. class TextureRect;
  33. class SplitContainerDragger : public Control {
  34. GDCLASS(SplitContainerDragger, Control);
  35. friend class SplitContainer;
  36. Rect2 split_bar_rect;
  37. TextureRect *touch_dragger = nullptr;
  38. void _touch_dragger_mouse_exited();
  39. void _touch_dragger_gui_input(const Ref<InputEvent> &p_event);
  40. protected:
  41. void _notification(int p_what);
  42. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  43. void _accessibility_action_inc(const Variant &p_data);
  44. void _accessibility_action_dec(const Variant &p_data);
  45. void _accessibility_action_set_value(const Variant &p_data);
  46. private:
  47. bool dragging = false;
  48. int drag_from = 0;
  49. int start_drag_split_offset = 0;
  50. bool mouse_inside = false;
  51. public:
  52. int dragger_index = -1;
  53. virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
  54. void set_touch_dragger_enabled(bool p_enabled);
  55. void update_touch_dragger();
  56. bool is_touch_dragger_enabled() const;
  57. SplitContainerDragger();
  58. };
  59. class SplitContainer : public Container {
  60. GDCLASS(SplitContainer, Container);
  61. friend class SplitContainerDragger;
  62. public:
  63. enum DraggerVisibility {
  64. DRAGGER_VISIBLE,
  65. DRAGGER_HIDDEN,
  66. DRAGGER_HIDDEN_COLLAPSED
  67. };
  68. private:
  69. bool show_drag_area = false;
  70. int drag_area_margin_begin = 0;
  71. int drag_area_margin_end = 0;
  72. int drag_area_offset = 0;
  73. PackedInt32Array split_offsets;
  74. LocalVector<int> default_dragger_positions;
  75. LocalVector<int> dragger_positions;
  76. LocalVector<Control *> valid_children;
  77. LocalVector<SplitContainerDragger *> dragging_area_controls;
  78. bool vertical = false;
  79. bool collapsed = false;
  80. DraggerVisibility dragger_visibility = DRAGGER_VISIBLE;
  81. bool dragging_enabled = true;
  82. bool split_offset_pending = false;
  83. bool can_use_desired_sizes = false;
  84. bool initialized = false;
  85. bool touch_dragger_enabled = false;
  86. struct ThemeCache {
  87. Color touch_dragger_color;
  88. Color touch_dragger_pressed_color;
  89. Color touch_dragger_hover_color;
  90. int separation = 0;
  91. int minimum_grab_thickness = 0;
  92. bool autohide = false;
  93. Ref<Texture2D> touch_dragger_icon;
  94. Ref<Texture2D> touch_dragger_icon_h;
  95. Ref<Texture2D> touch_dragger_icon_v;
  96. Ref<Texture2D> grabber_icon;
  97. Ref<Texture2D> grabber_icon_h;
  98. Ref<Texture2D> grabber_icon_v;
  99. float base_scale = 1.0;
  100. Ref<StyleBox> split_bar_background;
  101. } theme_cache;
  102. Ref<Texture2D> _get_grabber_icon() const;
  103. Ref<Texture2D> _get_touch_dragger_icon() const;
  104. Point2i _get_valid_range(int p_dragger_index) const;
  105. PackedInt32Array _get_desired_sizes() const;
  106. void _set_desired_sizes(const PackedInt32Array &p_desired_sizes, int p_priority_index = -1);
  107. void _update_default_dragger_positions();
  108. void _update_dragger_positions(int p_clamp_index = -1);
  109. int _get_separation() const;
  110. void _resort();
  111. void _update_draggers();
  112. void _on_child_visibility_changed(Control *p_control);
  113. void _add_valid_child(Control *p_control);
  114. void _remove_valid_child(Control *p_control);
  115. protected:
  116. bool is_fixed = false;
  117. void _notification(int p_what);
  118. void _validate_property(PropertyInfo &p_property) const;
  119. virtual void add_child_notify(Node *p_child) override;
  120. virtual void remove_child_notify(Node *p_child) override;
  121. virtual void move_child_notify(Node *p_child) override;
  122. static void _bind_methods();
  123. #ifndef DISABLE_DEPRECATED
  124. void _clamp_split_offset_compat_90411();
  125. static void _bind_compatibility_methods();
  126. #endif // DISABLE_DEPRECATED
  127. public:
  128. void set_split_offset(int p_offset, int p_index = 0);
  129. int get_split_offset(int p_index = 0) const;
  130. void set_split_offsets(const PackedInt32Array &p_offsets);
  131. PackedInt32Array get_split_offsets() const;
  132. void clamp_split_offset(int p_priority_index = 0);
  133. void set_collapsed(bool p_collapsed);
  134. bool is_collapsed() const;
  135. void set_dragger_visibility(DraggerVisibility p_visibility);
  136. DraggerVisibility get_dragger_visibility() const;
  137. void set_vertical(bool p_vertical);
  138. bool is_vertical() const;
  139. void set_dragging_enabled(bool p_enabled);
  140. bool is_dragging_enabled() const;
  141. virtual Size2 get_minimum_size() const override;
  142. virtual Vector<int> get_allowed_size_flags_horizontal() const override;
  143. virtual Vector<int> get_allowed_size_flags_vertical() const override;
  144. void set_drag_area_margin_begin(int p_margin);
  145. int get_drag_area_margin_begin() const;
  146. void set_drag_area_margin_end(int p_margin);
  147. int get_drag_area_margin_end() const;
  148. void set_drag_area_offset(int p_offset);
  149. int get_drag_area_offset() const;
  150. void set_show_drag_area_enabled(bool p_enabled);
  151. bool is_show_drag_area_enabled() const;
  152. TypedArray<Control> get_drag_area_controls();
  153. void set_touch_dragger_enabled(bool p_enabled);
  154. bool is_touch_dragger_enabled() const;
  155. #ifndef DISABLE_DEPRECATED
  156. Control *get_drag_area_control() { return dragging_area_controls[0]; }
  157. void _set_split_offset_first(int p_offset) { set_split_offset(p_offset); }
  158. int _get_split_offset_first() const { return get_split_offset(); }
  159. #endif
  160. SplitContainer(bool p_vertical = false);
  161. };
  162. VARIANT_ENUM_CAST(SplitContainer::DraggerVisibility);
  163. class HSplitContainer : public SplitContainer {
  164. GDCLASS(HSplitContainer, SplitContainer);
  165. public:
  166. HSplitContainer() :
  167. SplitContainer(false) { is_fixed = true; }
  168. };
  169. class VSplitContainer : public SplitContainer {
  170. GDCLASS(VSplitContainer, SplitContainer);
  171. public:
  172. VSplitContainer() :
  173. SplitContainer(true) { is_fixed = true; }
  174. };