ScrollView.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. /// Return view offset from the top-left corner.
  64. const IntVector2& GetViewPosition() const { return viewPosition_; }
  65. /// Return content element.
  66. UIElement* GetContentElement() const { return contentElement_; }
  67. /// Return horizontal scroll bar.
  68. ScrollBar* GetHorizontalScrollBar() const { return horizontalScrollBar_; }
  69. /// Return vertical scroll bar.
  70. ScrollBar* GetVerticalScrollBar() const { return verticalScrollBar_; }
  71. /// Return scroll panel.
  72. BorderImage* GetScrollPanel() const { return scrollPanel_; }
  73. /// Return whether scrollbars are automatically shown/hidden.
  74. bool GetScrollBarsAutoVisible() const { return scrollBarsAutoVisible_; }
  75. /// Return arrow key scroll step.
  76. float GetScrollStep() const;
  77. /// Return arrow key page step.
  78. float GetPageStep() const { return pageStep_; }
  79. /// Set view position attribute.
  80. void SetViewPositionAttr(const IntVector2& value);
  81. protected:
  82. /// Filter implicit attributes in serialization process.
  83. virtual bool FilterImplicitAttributes(XMLElement& dest) const;
  84. /// Filter implicit attributes in serialization process for internal scroll bar.
  85. bool FilterScrollBarImplicitAttributes(XMLElement& dest, const String& name) const;
  86. /// Resize panel based on scrollbar visibility.
  87. void UpdatePanelSize();
  88. /// Recalculate view size, validate view position and update scrollbars.
  89. void UpdateViewSize();
  90. /// Update the scrollbars' ranges and positions.
  91. void UpdateScrollBars();
  92. /// Limit and update the view with a new position.
  93. void UpdateView(const IntVector2& position);
  94. /// Content element.
  95. SharedPtr<UIElement> contentElement_;
  96. /// Horizontal scroll bar.
  97. SharedPtr<ScrollBar> horizontalScrollBar_;
  98. /// Vertical scroll bar.
  99. SharedPtr<ScrollBar> verticalScrollBar_;
  100. /// Scroll panel element.
  101. SharedPtr<BorderImage> scrollPanel_;
  102. /// Current view offset from the top-left corner.
  103. IntVector2 viewPosition_;
  104. /// Total view size.
  105. IntVector2 viewSize_;
  106. /// View offset attribute.
  107. IntVector2 viewPositionAttr_;
  108. /// Accumulated touch scroll speed.
  109. IntVector2 touchScrollSpeed_;
  110. /// Arrow key page step.
  111. float pageStep_;
  112. /// Automatically show/hide scrollbars flag.
  113. bool scrollBarsAutoVisible_;
  114. /// Ignore scrollbar events flag. Used to prevent possible endless loop when resizing.
  115. bool ignoreEvents_;
  116. /// Resize content widget width to match panel. Internal flag, used by the ListView class.
  117. bool resizeContentWidth_;
  118. private:
  119. /// Handle scrollbar value changed.
  120. void HandleScrollBarChanged(StringHash eventType, VariantMap& eventData);
  121. /// Handle scrollbar visibility changed.
  122. void HandleScrollBarVisibleChanged(StringHash eventType, VariantMap& eventData);
  123. /// Handle content element resized.
  124. void HandleElementResized(StringHash eventType, VariantMap& eventData);
  125. /// Handle touch move event for scrolling.
  126. void HandleTouchMove(StringHash eventType, VariantMap& eventData);
  127. };
  128. }