ScrollView.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "UIElement.h"
  24. namespace Atomic
  25. {
  26. namespace SystemUI
  27. {
  28. class BorderImage;
  29. class ScrollBar;
  30. /// Scrollable %UI element for showing a (possibly large) child element.
  31. class ATOMIC_API ScrollView : public UIElement
  32. {
  33. OBJECT(ScrollView);
  34. public:
  35. /// Construct.
  36. ScrollView(Context* context);
  37. /// Destruct.
  38. virtual ~ScrollView();
  39. /// Register object factory.
  40. static void RegisterObject(Context* context);
  41. /// Perform UI element update.
  42. virtual void Update(float timeStep);
  43. /// Apply attribute changes that can not be applied immediately.
  44. virtual void ApplyAttributes();
  45. /// React to mouse wheel.
  46. virtual void OnWheel(int delta, int buttons, int qualifiers);
  47. /// React to a key press.
  48. virtual void OnKey(int key, int buttons, int qualifiers);
  49. /// React to resize.
  50. virtual void OnResize();
  51. /// Set content element.
  52. void SetContentElement(UIElement* element);
  53. /// Set view offset from the top-left corner.
  54. void SetViewPosition(const IntVector2& position);
  55. /// Set view offset from the top-left corner.
  56. void SetViewPosition(int x, int y);
  57. /// Set scrollbars' visibility manually. Disables scrollbar autoshow/hide.
  58. void SetScrollBarsVisible(bool horizontal, bool vertical);
  59. /// Set whether to automatically show/hide scrollbars. Default true.
  60. void SetScrollBarsAutoVisible(bool enable);
  61. /// Set arrow key scroll step. Also sets it on the scrollbars.
  62. void SetScrollStep(float step);
  63. /// Set arrow key page step.
  64. void SetPageStep(float step);
  65. /// Set scroll deceleration.
  66. void SetScrollDeceleration(float deceleration) { scrollDeceleration_ = deceleration; }
  67. /// Set scroll snap epsilon
  68. void SetScrollSnapEpsilon(float snap) { scrollSnapEpsilon_ = snap; }
  69. /// Set whether child elements should be disabled while touch scrolling.
  70. void SetAutoDisableChildren(bool disable) { autoDisableChildren_ = disable; };
  71. /// Set how much touch movement is needed to trigger child element disabling.
  72. void SetAutoDisableThreshold(float amount) { autoDisableThreshold_ = amount; };
  73. /// Return view offset from the top-left corner.
  74. const IntVector2& GetViewPosition() const { return viewPosition_; }
  75. /// Return content element.
  76. UIElement* GetContentElement() const { return contentElement_; }
  77. /// Return horizontal scroll bar.
  78. ScrollBar* GetHorizontalScrollBar() const { return horizontalScrollBar_; }
  79. /// Return vertical scroll bar.
  80. ScrollBar* GetVerticalScrollBar() const { return verticalScrollBar_; }
  81. /// Return scroll panel.
  82. BorderImage* GetScrollPanel() const { return scrollPanel_; }
  83. /// Return whether scrollbars are automatically shown/hidden.
  84. bool GetScrollBarsAutoVisible() const { return scrollBarsAutoVisible_; }
  85. /// Return arrow key scroll step.
  86. float GetScrollStep() const;
  87. /// Return arrow key page step.
  88. float GetPageStep() const { return pageStep_; }
  89. /// Return scroll deceleration.
  90. float GetScrollDeceleration() const { return scrollDeceleration_; }
  91. /// Return scroll snap epsilon
  92. float GetScrollSnapEpsilon() const { return scrollSnapEpsilon_; }
  93. /// Return whether child element will be disabled while touch scrolling.
  94. bool GetAutoDisableChildren() const { return autoDisableChildren_; }
  95. /// Return how much touch movement is needed to trigger child element disabling.
  96. float GetAutoDisableThreshold() const { return autoDisableThreshold_; }
  97. /// Set view position attribute.
  98. void SetViewPositionAttr(const IntVector2& value);
  99. protected:
  100. /// Filter implicit attributes in serialization process.
  101. virtual bool FilterImplicitAttributes(XMLElement& dest) const;
  102. /// Filter implicit attributes in serialization process for internal scroll bar.
  103. bool FilterScrollBarImplicitAttributes(XMLElement& dest, const String& name) const;
  104. /// Resize panel based on scrollbar visibility.
  105. void UpdatePanelSize();
  106. /// Recalculate view size, validate view position and update scrollbars.
  107. void UpdateViewSize();
  108. /// Update the scrollbars' ranges and positions.
  109. void UpdateScrollBars();
  110. /// Limit and update the view with a new position.
  111. void UpdateView(const IntVector2& position);
  112. /// Content element.
  113. SharedPtr<UIElement> contentElement_;
  114. /// Horizontal scroll bar.
  115. SharedPtr<ScrollBar> horizontalScrollBar_;
  116. /// Vertical scroll bar.
  117. SharedPtr<ScrollBar> verticalScrollBar_;
  118. /// Scroll panel element.
  119. SharedPtr<BorderImage> scrollPanel_;
  120. /// Current view offset from the top-left corner.
  121. IntVector2 viewPosition_;
  122. /// Total view size.
  123. IntVector2 viewSize_;
  124. /// View offset attribute.
  125. IntVector2 viewPositionAttr_;
  126. /// Accumulated touch scroll speed.
  127. Vector2 touchScrollSpeed_;
  128. /// Max touch scroll speed.
  129. Vector2 touchScrollSpeedMax_;
  130. /// Arrow key page step.
  131. float pageStep_;
  132. /// Automatically show/hide scrollbars flag.
  133. bool scrollBarsAutoVisible_;
  134. /// Ignore scrollbar events flag. Used to prevent possible endless loop when resizing.
  135. bool ignoreEvents_;
  136. /// Resize content widget width to match panel. Internal flag, used by the ListView class.
  137. bool resizeContentWidth_;
  138. /// Scroll deceleration
  139. float scrollDeceleration_;
  140. /// Scroll snap epsilon
  141. float scrollSnapEpsilon_;
  142. /// Used to trigger scroll smoothing when false;
  143. bool scrollTouchDown_;
  144. /// Used to prevent touch scroll - scroll bar conflict
  145. bool barScrolling_;
  146. /// Used to determine if child elements should be disabled while touch scrolling, to prevent their trigger.
  147. bool autoDisableChildren_;
  148. /// Used to determine if children have been disabled.
  149. bool scrollChildrenDisable_;
  150. /// Distance moved with touch scrolling
  151. float touchDistanceSum_;
  152. /// Threshold to trigger auto disable children
  153. float autoDisableThreshold_;
  154. private:
  155. /// Handle scrollbar value changed.
  156. void HandleScrollBarChanged(StringHash eventType, VariantMap& eventData);
  157. /// Handle scrollbar visibility changed.
  158. void HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData);
  159. /// Handle content element resized.
  160. void HandleElementResized(StringHash eventType, VariantMap& eventData);
  161. /// Handle touch move event for scrolling.
  162. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  163. /// Handle the scroll smoothing.
  164. void ScrollSmooth(float timeStep);
  165. };
  166. }
  167. }