foldable_container.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**************************************************************************/
  2. /* foldable_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 FoldableGroup;
  33. class TextLine;
  34. class FoldableContainer : public Container {
  35. GDCLASS(FoldableContainer, Container);
  36. public:
  37. enum TitlePosition {
  38. POSITION_TOP,
  39. POSITION_BOTTOM,
  40. POSITION_MAX
  41. };
  42. private:
  43. bool folded = false;
  44. String title;
  45. Ref<FoldableGroup> foldable_group;
  46. String language;
  47. TextDirection title_text_direction = TEXT_DIRECTION_AUTO;
  48. HorizontalAlignment title_alignment = HORIZONTAL_ALIGNMENT_LEFT;
  49. TextServer::OverrunBehavior overrun_behavior = TextServer::OVERRUN_NO_TRIMMING;
  50. TitlePosition title_position = POSITION_TOP;
  51. Ref<TextLine> text_buf;
  52. bool changing_group = false;
  53. bool is_hovering = false;
  54. mutable Vector2 title_minimum_size;
  55. LocalVector<Control *> title_controls;
  56. struct ThemeCache {
  57. Ref<StyleBox> title_style;
  58. Ref<StyleBox> title_hover_style;
  59. Ref<StyleBox> title_collapsed_style;
  60. Ref<StyleBox> title_collapsed_hover_style;
  61. Ref<StyleBox> panel_style;
  62. Ref<StyleBox> focus_style;
  63. Color title_font_color;
  64. Color title_hovered_font_color;
  65. Color title_collapsed_font_color;
  66. Color title_font_outline_color;
  67. Ref<Font> title_font;
  68. int title_font_size = 0;
  69. int title_font_outline_size = 0;
  70. Ref<Texture2D> expanded_arrow;
  71. Ref<Texture2D> expanded_arrow_mirrored;
  72. Ref<Texture2D> folded_arrow;
  73. Ref<Texture2D> folded_arrow_mirrored;
  74. int h_separation = 0;
  75. } theme_cache;
  76. Ref<StyleBox> _get_title_style() const;
  77. Ref<Texture2D> _get_title_icon() const;
  78. int _get_h_separation() const { return MAX(theme_cache.h_separation, 0); }
  79. real_t _get_title_controls_width() const;
  80. void _update_title_min_size() const;
  81. void _shape();
  82. HorizontalAlignment _get_actual_alignment() const;
  83. void _update_group();
  84. void _draw_flippable_stylebox(const Ref<StyleBox> p_stylebox, const Rect2 &p_rect);
  85. protected:
  86. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  87. virtual String get_tooltip(const Point2 &p_pos) const override;
  88. void _notification(int p_what);
  89. static void _bind_methods();
  90. public:
  91. void fold();
  92. void expand();
  93. void set_folded(bool p_folded);
  94. bool is_folded() const;
  95. void set_foldable_group(const Ref<FoldableGroup> &p_group);
  96. Ref<FoldableGroup> get_foldable_group() const;
  97. void set_title(const String &p_text);
  98. String get_title() const;
  99. void set_title_alignment(HorizontalAlignment p_alignment);
  100. HorizontalAlignment get_title_alignment() const;
  101. void set_title_text_direction(TextDirection p_text_direction);
  102. TextDirection get_title_text_direction() const;
  103. void set_title_text_overrun_behavior(TextServer::OverrunBehavior p_overrun_behavior);
  104. TextServer::OverrunBehavior get_title_text_overrun_behavior() const;
  105. void set_language(const String &p_language);
  106. String get_language() const;
  107. void set_title_position(TitlePosition p_title_position);
  108. TitlePosition get_title_position() const;
  109. void add_title_bar_control(Control *p_control);
  110. void remove_title_bar_control(Control *p_control);
  111. virtual Size2 get_minimum_size() const override;
  112. virtual Vector<int> get_allowed_size_flags_horizontal() const override { return { SIZE_FILL, SIZE_SHRINK_BEGIN, SIZE_SHRINK_CENTER, SIZE_SHRINK_END }; }
  113. virtual Vector<int> get_allowed_size_flags_vertical() const override { return { SIZE_FILL, SIZE_SHRINK_BEGIN, SIZE_SHRINK_CENTER, SIZE_SHRINK_END }; }
  114. FoldableContainer(const String &p_text = String());
  115. ~FoldableContainer();
  116. };
  117. VARIANT_ENUM_CAST(FoldableContainer::TitlePosition);
  118. class FoldableGroup : public Resource {
  119. GDCLASS(FoldableGroup, Resource);
  120. friend class FoldableContainer;
  121. HashSet<FoldableContainer *> containers;
  122. bool allow_folding_all = false;
  123. bool updating_group = false;
  124. protected:
  125. static void _bind_methods();
  126. public:
  127. FoldableContainer *get_expanded_container() const;
  128. void get_containers(List<FoldableContainer *> *r_containers) const;
  129. TypedArray<FoldableContainer> _get_containers() const;
  130. void set_allow_folding_all(bool p_enabled);
  131. bool is_allow_folding_all() const;
  132. FoldableGroup();
  133. };