ScrollView.h 7.6 KB

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