1
0

UIScrollViewBar.h 5.2 KB

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