BsGUIScrollArea.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsGUIElementContainer.h"
  6. namespace bs
  7. {
  8. /** @addtogroup GUI
  9. * @{
  10. */
  11. /** Scroll bar options for a GUI scroll area. */
  12. enum class ScrollBarType
  13. {
  14. ShowIfDoesntFit,
  15. AlwaysShow,
  16. NeverShow
  17. };
  18. /** A GUI element container with support for vertical & horizontal scrolling. */
  19. class BS_EXPORT GUIScrollArea : public GUIElementContainer
  20. {
  21. public:
  22. /** Returns type name of the GUI element used for finding GUI element styles. */
  23. static const String& getGUITypeName();
  24. /**
  25. * Creates a new empty scroll area.
  26. *
  27. * @param[in] vertBarType Vertical scrollbar options.
  28. * @param[in] horzBarType Horizontal scrollbar options.
  29. * @param[in] scrollBarStyle Style used by the scroll bars.
  30. * @param[in] scrollAreaStyle Style used by the scroll content area.
  31. */
  32. static GUIScrollArea* create(ScrollBarType vertBarType, ScrollBarType horzBarType,
  33. const String& scrollBarStyle = StringUtil::BLANK, const String& scrollAreaStyle = StringUtil::BLANK);
  34. /**
  35. * Creates a new empty scroll area.
  36. *
  37. * @param[in] vertBarType Vertical scrollbar options.
  38. * @param[in] horzBarType Horizontal scrollbar options.
  39. * @param[in] options Options that allow you to control how is the element positioned and sized. This will
  40. * override any similar options set by style.
  41. * @param[in] scrollBarStyle Style used by the scroll bars.
  42. * @param[in] scrollAreaStyle Style used by the scroll content area.
  43. */
  44. static GUIScrollArea* create(ScrollBarType vertBarType, ScrollBarType horzBarType,
  45. const GUIOptions& options, const String& scrollBarStyle = StringUtil::BLANK,
  46. const String& scrollAreaStyle = StringUtil::BLANK);
  47. /**
  48. * Creates a new empty scroll area. Scroll bars will be show if needed and hidden otherwise.
  49. *
  50. * @param[in] scrollBarStyle Style used by the scroll bars.
  51. * @param[in] scrollAreaStyle Style used by the scroll content area.
  52. */
  53. static GUIScrollArea* create(const String& scrollBarStyle = StringUtil::BLANK,
  54. const String& scrollAreaStyle = StringUtil::BLANK);
  55. /**
  56. * Creates a new empty scroll area. Scroll bars will be show if needed and hidden otherwise.
  57. *
  58. * @param[in] options Options that allow you to control how is the element positioned and sized. This will
  59. * override any similar options set by style.
  60. * @param[in] scrollBarStyle Style used by the scroll bars.
  61. * @param[in] scrollAreaStyle Style used by the scroll content area.
  62. */
  63. static GUIScrollArea* create(const GUIOptions& options, const String& scrollBarStyle = StringUtil::BLANK,
  64. const String& scrollAreaStyle = StringUtil::BLANK);
  65. /** Returns the scroll area layout that you may use to add elements inside the scroll area. */
  66. GUILayout& getLayout() const { return *mContentLayout; }
  67. /** Scrolls the area up by specified amount of pixels, if possible. */
  68. void scrollUpPx(UINT32 pixels);
  69. /** Scrolls the area down by specified amount of pixels, if possible. */
  70. void scrollDownPx(UINT32 pixels);
  71. /** Scrolls the area left by specified amount of pixels, if possible. */
  72. void scrollLeftPx(UINT32 pixels);
  73. /** Scrolls the area right by specified amount of pixels, if possible. */
  74. void scrollRightPx(UINT32 pixels);
  75. /** Scrolls the area up by specified percentage (ranging [0, 1]), if possible. */
  76. void scrollUpPct(float percent);
  77. /** Scrolls the area down by specified percentage (ranging [0, 1]), if possible. */
  78. void scrollDownPct(float percent);
  79. /** Scrolls the area left by specified percentage (ranging [0, 1]), if possible. */
  80. void scrollLeftPct(float percent);
  81. /** Scrolls the area right by specified percentage (ranging [0, 1]), if possible. */
  82. void scrollRightPct(float percent);
  83. /**
  84. * Scrolls the contents to the specified position (0 meaning top-most part of the content is visible, and 1 meaning
  85. * bottom-most part is visible).
  86. */
  87. void scrollToVertical(float pct);
  88. /**
  89. * Scrolls the contents to the specified position (0 meaning left-most part of the content is visible, and 1 meaning
  90. * right-most part is visible)
  91. */
  92. void scrollToHorizontal(float pct);
  93. /**
  94. * Returns how much is the scroll area scrolled in the vertical direction. Returned value represents percentage
  95. * where 0 means no scrolling is happening, and 1 means area is fully scrolled to the bottom.
  96. */
  97. float getVerticalScroll() const;
  98. /**
  99. * Returns how much is the scroll area scrolled in the horizontal direction. Returned value represents percentage
  100. * where 0 means no scrolling is happening, and 1 means area is fully scrolled to the right.
  101. */
  102. float getHorizontalScroll() const;
  103. /**
  104. * Returns the bounds of the scroll area not including the scroll bars (meaning only the portion that contains the
  105. * contents).
  106. */
  107. Rect2I getContentBounds();
  108. /**
  109. * Number of pixels the scroll bar will occupy when active. This is width for vertical scrollbar, and height for
  110. * horizontal scrollbar.
  111. */
  112. static const UINT32 ScrollBarWidth;
  113. /** @name Internal
  114. * @{
  115. */
  116. /** @copydoc GUIElementContainer::_getElementType */
  117. ElementType _getElementType() const override { return ElementType::ScrollArea; }
  118. /** @} */
  119. protected:
  120. ~GUIScrollArea();
  121. /** @copydoc GUIElementContainer::_getLayoutSizeRange */
  122. LayoutSizeRange _getLayoutSizeRange() const override;
  123. /** @copydoc GUIElementContainer::updateClippedBounds */
  124. void updateClippedBounds() override;
  125. /** @copydoc GUIElementBase::_calculateLayoutSizeRange */
  126. LayoutSizeRange _calculateLayoutSizeRange() const override;
  127. /** @copydoc GUIElementBase::_updateOptimalLayoutSizes */
  128. void _updateOptimalLayoutSizes() override;
  129. /** @copydoc GUIElementContainer::_getOptimalSize */
  130. Vector2I _getOptimalSize() const override;
  131. private:
  132. GUIScrollArea(ScrollBarType vertBarType, ScrollBarType horzBarType,
  133. const String& scrollBarStyle, const String& scrollAreaStyle, const GUIDimensions& dimensions);
  134. /** @copydoc GUIElementContainer::mouseEvent */
  135. bool _mouseEvent(const GUIMouseEvent& ev) override;
  136. /**
  137. * Called when the vertical scrollbar moves.
  138. *
  139. * @param[in] pct Scrollbar position ranging [0, 1].
  140. */
  141. void vertScrollUpdate(float pct);
  142. /**
  143. * Called when the horizontal scrollbar moves.
  144. *
  145. * @param[in] pct Scrollbar position ranging [0, 1].
  146. */
  147. void horzScrollUpdate(float pct);
  148. /** @copydoc GUIElementContainer::_updateLayoutInternal */
  149. void _updateLayoutInternal(const GUILayoutData& data) override;
  150. /** @copydoc GUIElementContainer::_getElementAreas */
  151. void _getElementAreas(const Rect2I& layoutArea, Rect2I* elementAreas, UINT32 numElements,
  152. const Vector<LayoutSizeRange>& sizeRanges, const LayoutSizeRange& mySizeRange) const override;
  153. /**
  154. * @copydoc GUIElementContainer::_getElementAreas
  155. *
  156. * @note Also calculates some scroll area specific values.
  157. */
  158. void _getElementAreas(const Rect2I& layoutArea, Rect2I* elementAreas, UINT32 numElements,
  159. const Vector<LayoutSizeRange>& sizeRanges, Vector2I& visibleSize, Vector2I& contentSize) const;
  160. ScrollBarType mVertBarType;
  161. ScrollBarType mHorzBarType;
  162. String mScrollBarStyle;
  163. GUILayout* mContentLayout;
  164. GUIScrollBarVert* mVertScroll;
  165. GUIScrollBarHorz* mHorzScroll;
  166. float mVertOffset;
  167. float mHorzOffset;
  168. bool mRecalculateVertOffset;
  169. bool mRecalculateHorzOffset;
  170. Vector2I mVisibleSize;
  171. Vector2I mContentSize;
  172. Vector<LayoutSizeRange> mChildSizeRanges;
  173. LayoutSizeRange mSizeRange;
  174. static const UINT32 MinHandleSize;
  175. static const UINT32 WheelScrollAmount;
  176. };
  177. /** @} */
  178. }