ScrollView.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../UI/UIElement.h"
  5. namespace Urho3D
  6. {
  7. class BorderImage;
  8. class ScrollBar;
  9. /// Scrollable %UI element for showing a (possibly large) child element.
  10. class URHO3D_API ScrollView : public UIElement
  11. {
  12. URHO3D_OBJECT(ScrollView, UIElement);
  13. public:
  14. /// Construct.
  15. explicit ScrollView(Context* context);
  16. /// Destruct.
  17. ~ScrollView() override;
  18. /// Register object factory.
  19. /// @nobind
  20. static void RegisterObject(Context* context);
  21. /// Perform UI element update.
  22. void Update(float timeStep) override;
  23. /// Apply attribute changes that can not be applied immediately.
  24. void ApplyAttributes() override;
  25. /// React to mouse wheel.
  26. void OnWheel(int delta, MouseButtonFlags buttons, QualifierFlags qualifiers) override;
  27. /// React to a key press.
  28. void OnKey(Key key, MouseButtonFlags buttons, QualifierFlags qualifiers) override;
  29. /// React to resize.
  30. void OnResize(const IntVector2& newSize, const IntVector2& delta) override;
  31. /// Return whether the element could handle wheel input.
  32. bool IsWheelHandler() const override { return true; }
  33. /// Set content element.
  34. /// @property
  35. void SetContentElement(UIElement* element);
  36. /// Set view offset from the top-left corner.
  37. /// @property
  38. void SetViewPosition(const IntVector2& position);
  39. /// Set view offset from the top-left corner.
  40. void SetViewPosition(int x, int y);
  41. /// Set scrollbars' visibility manually. Disables scrollbar autoshow/hide.
  42. void SetScrollBarsVisible(bool horizontal, bool vertical);
  43. /// Set horizontal scrollbar visibility manually. Disables scrollbar autoshow/hide.
  44. /// @property
  45. void SetHorizontalScrollBarVisible(bool visible);
  46. /// Set vertical scrollbar visibility manually. Disables scrollbar autoshow/hide.
  47. /// @property
  48. void SetVerticalScrollBarVisible(bool visible);
  49. /// Set whether to automatically show/hide scrollbars. Default true.
  50. /// @property
  51. void SetScrollBarsAutoVisible(bool enable);
  52. /// Set arrow key scroll step. Also sets it on the scrollbars.
  53. /// @property
  54. void SetScrollStep(float step);
  55. /// Set arrow key page step.
  56. /// @property
  57. void SetPageStep(float step);
  58. /// Set scroll deceleration.
  59. /// @property
  60. void SetScrollDeceleration(float deceleration) { scrollDeceleration_ = deceleration; }
  61. /// Set scroll snap epsilon.
  62. /// @property
  63. void SetScrollSnapEpsilon(float snap) { scrollSnapEpsilon_ = snap; }
  64. /// Set whether child elements should be disabled while touch scrolling.
  65. /// @property
  66. void SetAutoDisableChildren(bool disable) { autoDisableChildren_ = disable; };
  67. /// Set how much touch movement is needed to trigger child element disabling.
  68. /// @property
  69. void SetAutoDisableThreshold(float amount) { autoDisableThreshold_ = amount; };
  70. /// Return view offset from the top-left corner.
  71. /// @property
  72. const IntVector2& GetViewPosition() const { return viewPosition_; }
  73. /// Return content element.
  74. /// @property
  75. UIElement* GetContentElement() const { return contentElement_; }
  76. /// Return horizontal scroll bar.
  77. /// @property
  78. ScrollBar* GetHorizontalScrollBar() const { return horizontalScrollBar_; }
  79. /// Return vertical scroll bar.
  80. /// @property
  81. ScrollBar* GetVerticalScrollBar() const { return verticalScrollBar_; }
  82. /// Return scroll panel.
  83. /// @property
  84. BorderImage* GetScrollPanel() const { return scrollPanel_; }
  85. /// Return whether scrollbars are automatically shown/hidden.
  86. /// @property
  87. bool GetScrollBarsAutoVisible() const { return scrollBarsAutoVisible_; }
  88. /// Return whether the horizontal scrollbar is visible.
  89. /// @property
  90. bool GetHorizontalScrollBarVisible() const;
  91. /// Return whether the vertical scrollbar is visible.
  92. /// @property
  93. bool GetVerticalScrollBarVisible() const;
  94. /// Return arrow key scroll step.
  95. /// @property
  96. float GetScrollStep() const;
  97. /// Return arrow key page step.
  98. /// @property
  99. float GetPageStep() const { return pageStep_; }
  100. /// Return scroll deceleration.
  101. /// @property
  102. float GetScrollDeceleration() const { return scrollDeceleration_; }
  103. /// Return scroll snap epsilon.
  104. /// @property
  105. float GetScrollSnapEpsilon() const { return scrollSnapEpsilon_; }
  106. /// Return whether child element will be disabled while touch scrolling.
  107. /// @property
  108. bool GetAutoDisableChildren() const { return autoDisableChildren_; }
  109. /// Return how much touch movement is needed to trigger child element disabling.
  110. /// @property
  111. float GetAutoDisableThreshold() const { return autoDisableThreshold_; }
  112. /// Set view position attribute.
  113. void SetViewPositionAttr(const IntVector2& value);
  114. protected:
  115. /// Filter implicit attributes in serialization process.
  116. bool FilterImplicitAttributes(XMLElement& dest) const override;
  117. /// Filter implicit attributes in serialization process for internal scroll bar.
  118. bool FilterScrollBarImplicitAttributes(XMLElement& dest, const String& name) const;
  119. /// Resize panel based on scrollbar visibility.
  120. void UpdatePanelSize();
  121. /// Recalculate view size, validate view position and update scrollbars.
  122. void UpdateViewSize();
  123. /// Update the scrollbars' ranges and positions.
  124. void UpdateScrollBars();
  125. /// Limit and update the view with a new position.
  126. void UpdateView(const IntVector2& position);
  127. /// Content element.
  128. SharedPtr<UIElement> contentElement_;
  129. /// Horizontal scroll bar.
  130. SharedPtr<ScrollBar> horizontalScrollBar_;
  131. /// Vertical scroll bar.
  132. SharedPtr<ScrollBar> verticalScrollBar_;
  133. /// Scroll panel element.
  134. SharedPtr<BorderImage> scrollPanel_;
  135. /// Current view offset from the top-left corner.
  136. IntVector2 viewPosition_;
  137. /// Total view size.
  138. IntVector2 viewSize_;
  139. /// View offset attribute.
  140. IntVector2 viewPositionAttr_;
  141. /// Accumulated touch scroll speed.
  142. Vector2 touchScrollSpeed_;
  143. /// Max touch scroll speed.
  144. Vector2 touchScrollSpeedMax_;
  145. /// Arrow key page step.
  146. float pageStep_;
  147. /// Automatically show/hide scrollbars flag.
  148. bool scrollBarsAutoVisible_;
  149. /// Ignore scrollbar events flag. Used to prevent possible endless loop when resizing.
  150. bool ignoreEvents_;
  151. /// Resize content widget width to match panel. Internal flag, used by the ListView class.
  152. bool resizeContentWidth_;
  153. /// Scroll deceleration.
  154. float scrollDeceleration_;
  155. /// Scroll snap epsilon.
  156. float scrollSnapEpsilon_;
  157. /// Used to trigger scroll smoothing when false.
  158. bool scrollTouchDown_;
  159. /// Used to prevent touch scroll - scroll bar conflict.
  160. bool barScrolling_;
  161. /// Used to determine if child elements should be disabled while touch scrolling, to prevent their trigger.
  162. bool autoDisableChildren_;
  163. /// Used to determine if children have been disabled.
  164. bool scrollChildrenDisable_;
  165. /// Distance moved with touch scrolling.
  166. float touchDistanceSum_;
  167. /// Threshold to trigger auto disable children.
  168. float autoDisableThreshold_;
  169. private:
  170. /// Handle scrollbar value changed.
  171. void HandleScrollBarChanged(StringHash eventType, VariantMap& eventData);
  172. /// Handle scrollbar visibility changed.
  173. void HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData);
  174. /// Handle content element resized.
  175. void HandleElementResized(StringHash eventType, VariantMap& eventData);
  176. /// Handle touch move event for scrolling.
  177. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  178. /// Handle the scroll smoothing.
  179. void ScrollSmooth(float timeStep);
  180. };
  181. }