UIScrollViewBar.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /****************************************************************************
  2. Copyright (c) 2015 Neo Kim (neo.kim@neofect.com)
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __UISCROLLVIEWBAR_H__
  22. #define __UISCROLLVIEWBAR_H__
  23. #include "ui/UIScrollView.h"
  24. NS_CC_BEGIN
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. class Sprite;
  30. namespace ui {
  31. /**
  32. * Scroll bar being attached to ScrollView layout container.
  33. */
  34. class CC_GUI_DLL ScrollViewBar : public ProtectedNode
  35. {
  36. public:
  37. /**
  38. * Default constructor
  39. * @js ctor
  40. * @lua new
  41. */
  42. ScrollViewBar(ScrollView* parent, ScrollView::Direction direction);
  43. /**
  44. * Default destructor
  45. * @js NA
  46. * @lua NA
  47. */
  48. virtual ~ScrollViewBar();
  49. /**
  50. * Create a scroll bar with its parent scroll view and direction.
  51. * @return A scroll bar instance.
  52. */
  53. static ScrollViewBar* create(ScrollView* parent, ScrollView::Direction direction);
  54. /**
  55. * @brief Set the scroll bar position from the left-bottom corner (horizontal) or right-top corner (vertical).
  56. *
  57. * @param positionFromCorner The position from the left-bottom corner (horizontal) or right-top corner (vertical).
  58. */
  59. void setPositionFromCorner(const Vec2& positionFromCorner);
  60. /**
  61. * @brief Get the scroll bar position from the left-bottom corner (horizontal) or right-top corner (vertical).
  62. *
  63. * @return positionFromCorner
  64. */
  65. Vec2 getPositionFromCorner() const;
  66. /**
  67. * @brief Set the scroll bar's width
  68. *
  69. * @param width The scroll bar's width
  70. */
  71. void setWidth(float width);
  72. /**
  73. * @brief Get the scroll bar's width
  74. *
  75. * @return the scroll bar's width
  76. */
  77. float getWidth() const;
  78. /**
  79. * @brief Set scroll bar auto hide state
  80. *
  81. * @param scroll bar auto hide state
  82. */
  83. void setAutoHideEnabled(bool autoHideEnabled);
  84. /**
  85. * @brief Query scroll bar auto hide state
  86. *
  87. * @return True if scroll bar auto hide is enabled, false otherwise.
  88. */
  89. bool isAutoHideEnabled() const { return _autoHideEnabled; }
  90. /**
  91. * @brief Set scroll bar auto hide time
  92. *
  93. * @param scroll bar auto hide time
  94. */
  95. void setAutoHideTime(float autoHideTime) { _autoHideTime = autoHideTime; }
  96. /**
  97. * @brief Get the scroll bar's auto hide time
  98. *
  99. * @return the scroll bar's auto hide time
  100. */
  101. float getAutoHideTime() const { return _autoHideTime; }
  102. /**
  103. * @brief This is called by parent ScrollView when the parent is scrolled. Don't call this directly.
  104. *
  105. * @param amount how much the inner container of ScrollView is out of boundary
  106. */
  107. virtual void onScrolled(const Vec2& outOfBoundary);
  108. /**
  109. * @lua NA
  110. */
  111. virtual void setOpacity(GLubyte opacity) override { _opacity = opacity; }
  112. virtual GLubyte getOpacity() const override { return _opacity; }
  113. virtual void onEnter() override;
  114. virtual void update(float deltaTime) override;
  115. /**
  116. * @brief This is called by parent ScrollView when a touch is began. Don't call this directly.
  117. */
  118. void onTouchBegan();
  119. /**
  120. * @brief This is called by parent ScrollView when a touch is ended. Don't call this directly.
  121. */
  122. void onTouchEnded();
  123. CC_CONSTRUCTOR_ACCESS:
  124. virtual bool init() override;
  125. private:
  126. float calculateLength(float innerContainerMeasure, float scrollViewMeasure, float outOfBoundaryValue);
  127. Vec2 calculatePosition(float innerContainerMeasure, float scrollViewMeasure, float innerContainerPosition, float outOfBoundaryValue, float actualLength);
  128. void updateLength(float length);
  129. void processAutoHide(float deltaTime);
  130. ScrollView* _parent;
  131. ScrollView::Direction _direction;
  132. Sprite* _upperHalfCircle;
  133. Sprite* _lowerHalfCircle;
  134. Sprite* _body;
  135. GLubyte _opacity;
  136. float _marginFromBoundary;
  137. float _marginForLength;
  138. bool _touching;
  139. bool _autoHideEnabled;
  140. float _autoHideTime;
  141. float _autoHideRemainingTime;
  142. };
  143. }
  144. // end of ui group
  145. /// @}
  146. NS_CC_END
  147. #endif /* defined(__UISCROLLVIEWBAR_H__) */