UIScrollViewBar.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. #include "ui/UIScrollViewBar.h"
  22. #include "platform/CCImage.h"
  23. #include "2d/CCSprite.h"
  24. #include "base/ccUtils.h"
  25. NS_CC_BEGIN
  26. namespace ui {
  27. static const char* HALF_CIRCLE_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAAGCAMAAADAMI+zAAAAJ1BMVEX///////////////////////////////////////////////////9Ruv0SAAAADHRSTlMABgcbbW7Hz9Dz+PmlcJP5AAAAMElEQVR4AUXHwQ2AQAhFwYcLH1H6r1djzDK3ASxUpTBeK/uTCyz7dx54b44m4p5cD1MwAooEJyk3AAAAAElFTkSuQmCC";
  28. static const char* BODY_IMAGE_1_PIXEL_HEIGHT = "iVBORw0KGgoAAAANSUhEUgAAAAwAAAABCAMAAADdNb8LAAAAA1BMVEX///+nxBvIAAAACklEQVR4AWNABgAADQABYc2cpAAAAABJRU5ErkJggg==";
  29. static const char* HALF_CIRCLE_IMAGE_KEY = "/__halfCircleImage";
  30. static const char* BODY_IMAGE_1_PIXEL_HEIGHT_KEY = "/__bodyImage";
  31. static const Color3B DEFAULT_COLOR(52, 65, 87);
  32. static const float DEFAULT_MARGIN = 20;
  33. static const float DEFAULT_AUTO_HIDE_TIME = 0.2f;
  34. static const float DEFAULT_SCROLLBAR_OPACITY = 0.4f;
  35. ScrollViewBar::ScrollViewBar(ScrollView* parent, ScrollView::Direction direction):
  36. _parent(parent),
  37. _direction(direction),
  38. _upperHalfCircle(nullptr),
  39. _lowerHalfCircle(nullptr),
  40. _body(nullptr),
  41. _opacity(255 * DEFAULT_SCROLLBAR_OPACITY),
  42. _marginFromBoundary(DEFAULT_MARGIN),
  43. _marginForLength(DEFAULT_MARGIN),
  44. _touching(false),
  45. _autoHideEnabled(true),
  46. _autoHideTime(DEFAULT_AUTO_HIDE_TIME),
  47. _autoHideRemainingTime(0)
  48. {
  49. CCASSERT(parent != nullptr, "Parent scroll view must not be null!");
  50. CCASSERT(direction != ScrollView::Direction::BOTH, "Illegal scroll direction for scroll bar!");
  51. setCascadeColorEnabled(true);
  52. setCascadeOpacityEnabled(true);
  53. }
  54. ScrollViewBar::~ScrollViewBar()
  55. {
  56. }
  57. ScrollViewBar* ScrollViewBar::create(ScrollView* parent, ScrollView::Direction direction)
  58. {
  59. ScrollViewBar* node = new (std::nothrow) ScrollViewBar(parent, direction);
  60. if (node && node->init())
  61. {
  62. node->autorelease();
  63. return node;
  64. }
  65. CC_SAFE_DELETE(node);
  66. return nullptr;
  67. }
  68. bool ScrollViewBar::init()
  69. {
  70. if (!ProtectedNode::init())
  71. {
  72. return false;
  73. }
  74. _upperHalfCircle = utils::createSpriteFromBase64Cached(HALF_CIRCLE_IMAGE, HALF_CIRCLE_IMAGE_KEY);
  75. _upperHalfCircle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
  76. addProtectedChild(_upperHalfCircle);
  77. _lowerHalfCircle = Sprite::createWithTexture(_upperHalfCircle->getTexture(), _upperHalfCircle->getTextureRect(), _upperHalfCircle->isTextureRectRotated());
  78. _lowerHalfCircle->setScaleY(-1);
  79. _lowerHalfCircle->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
  80. addProtectedChild(_lowerHalfCircle);
  81. _body = utils::createSpriteFromBase64Cached(BODY_IMAGE_1_PIXEL_HEIGHT, BODY_IMAGE_1_PIXEL_HEIGHT_KEY);
  82. _body->setAnchorPoint(Vec2::ANCHOR_MIDDLE_BOTTOM);
  83. addProtectedChild(_body);
  84. setColor(DEFAULT_COLOR);
  85. onScrolled(Vec2::ZERO);
  86. ProtectedNode::setOpacity(0);
  87. _autoHideRemainingTime = 0.0f;
  88. if(_direction == ScrollView::Direction::HORIZONTAL)
  89. {
  90. setRotation(90);
  91. }
  92. return true;
  93. }
  94. void ScrollViewBar::setPositionFromCorner(const Vec2& positionFromCorner)
  95. {
  96. if(_direction == ScrollView::Direction::VERTICAL)
  97. {
  98. _marginForLength = positionFromCorner.y;
  99. _marginFromBoundary = positionFromCorner.x;
  100. }
  101. else
  102. {
  103. _marginForLength = positionFromCorner.x;
  104. _marginFromBoundary = positionFromCorner.y;
  105. }
  106. }
  107. Vec2 ScrollViewBar::getPositionFromCorner() const
  108. {
  109. if(_direction == ScrollView::Direction::VERTICAL)
  110. {
  111. return Vec2(_marginFromBoundary, _marginForLength);
  112. }
  113. else
  114. {
  115. return Vec2(_marginForLength, _marginFromBoundary);
  116. }
  117. }
  118. void ScrollViewBar::setWidth(float width)
  119. {
  120. float scale = width / _body->getContentSize().width;
  121. _body->setScaleX(scale);
  122. _upperHalfCircle->setScale(scale);
  123. _lowerHalfCircle->setScale(-scale);
  124. }
  125. void ScrollViewBar::setAutoHideEnabled(bool autoHideEnabled)
  126. {
  127. _autoHideEnabled = autoHideEnabled;
  128. if (!_autoHideEnabled && !_touching && _autoHideRemainingTime <= 0)
  129. {
  130. ProtectedNode::setOpacity(_opacity);
  131. }
  132. else
  133. ProtectedNode::setOpacity(0);
  134. }
  135. float ScrollViewBar::getWidth() const
  136. {
  137. return _body->getBoundingBox().size.width;
  138. }
  139. void ScrollViewBar::updateLength(float length)
  140. {
  141. float ratio = length / _body->getTextureRect().size.height;
  142. _body->setScaleY(ratio);
  143. _upperHalfCircle->setPositionY(_body->getPositionY() + length);
  144. }
  145. void ScrollViewBar::onEnter()
  146. {
  147. #if CC_ENABLE_SCRIPT_BINDING
  148. if (_scriptType == kScriptTypeJavascript)
  149. {
  150. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
  151. return;
  152. }
  153. #endif
  154. ProtectedNode::onEnter();
  155. scheduleUpdate();
  156. }
  157. void ScrollViewBar::update(float deltaTime)
  158. {
  159. processAutoHide(deltaTime);
  160. }
  161. void ScrollViewBar::processAutoHide(float deltaTime)
  162. {
  163. if(!_autoHideEnabled || _autoHideRemainingTime <= 0)
  164. {
  165. return;
  166. }
  167. else if(_touching)
  168. {
  169. // If it is touching, don't auto hide.
  170. return;
  171. }
  172. _autoHideRemainingTime -= deltaTime;
  173. if(_autoHideRemainingTime <= _autoHideTime)
  174. {
  175. _autoHideRemainingTime = MAX(0, _autoHideRemainingTime);
  176. ProtectedNode::setOpacity(_opacity * (_autoHideRemainingTime / _autoHideTime));
  177. }
  178. }
  179. void ScrollViewBar::onTouchBegan()
  180. {
  181. if(!_autoHideEnabled)
  182. {
  183. return;
  184. }
  185. _touching = true;
  186. }
  187. void ScrollViewBar::onTouchEnded()
  188. {
  189. if(!_autoHideEnabled)
  190. {
  191. return;
  192. }
  193. _touching = false;
  194. if(_autoHideRemainingTime <= 0)
  195. {
  196. // If the remaining time is 0, it means that it didn't moved after touch started so scroll bar is not showing.
  197. return;
  198. }
  199. _autoHideRemainingTime = _autoHideTime;
  200. }
  201. void ScrollViewBar::onScrolled(const Vec2& outOfBoundary)
  202. {
  203. if(_autoHideEnabled)
  204. {
  205. _autoHideRemainingTime = _autoHideTime;
  206. ProtectedNode::setOpacity(_opacity);
  207. }
  208. Layout* innerContainer = _parent->getInnerContainer();
  209. float innerContainerMeasure = 0;
  210. float scrollViewMeasure = 0;
  211. float outOfBoundaryValue = 0;
  212. float innerContainerPosition = 0;
  213. if(_direction == ScrollView::Direction::VERTICAL)
  214. {
  215. innerContainerMeasure = innerContainer->getContentSize().height;
  216. scrollViewMeasure = _parent->getContentSize().height;
  217. outOfBoundaryValue = outOfBoundary.y;
  218. innerContainerPosition = -innerContainer->getPositionY();
  219. }
  220. else if(_direction == ScrollView::Direction::HORIZONTAL)
  221. {
  222. innerContainerMeasure = innerContainer->getContentSize().width;
  223. scrollViewMeasure = _parent->getContentSize().width;
  224. outOfBoundaryValue = outOfBoundary.x;
  225. innerContainerPosition = -innerContainer->getPositionX();
  226. }
  227. float length = calculateLength(innerContainerMeasure, scrollViewMeasure, outOfBoundaryValue);
  228. Vec2 position = calculatePosition(innerContainerMeasure, scrollViewMeasure, innerContainerPosition, outOfBoundaryValue, length);
  229. updateLength(length);
  230. setPosition(position);
  231. }
  232. float ScrollViewBar::calculateLength(float innerContainerMeasure, float scrollViewMeasure, float outOfBoundaryValue)
  233. {
  234. float denominatorValue = innerContainerMeasure;
  235. if(outOfBoundaryValue != 0)
  236. {
  237. // If it is out of boundary, the length of scroll bar gets shorter quickly.
  238. static const float GETTING_SHORTER_FACTOR = 20;
  239. denominatorValue += (outOfBoundaryValue > 0 ? outOfBoundaryValue : -outOfBoundaryValue) * GETTING_SHORTER_FACTOR;
  240. }
  241. float lengthRatio = scrollViewMeasure / denominatorValue;
  242. return fabsf(scrollViewMeasure - 2 * _marginForLength) * lengthRatio;
  243. }
  244. Vec2 ScrollViewBar::calculatePosition(float innerContainerMeasure, float scrollViewMeasure, float innerContainerPosition, float outOfBoundaryValue, float length)
  245. {
  246. float denominatorValue = innerContainerMeasure - scrollViewMeasure;
  247. if(outOfBoundaryValue != 0)
  248. {
  249. denominatorValue += std::abs(outOfBoundaryValue);
  250. }
  251. float positionRatio = 0;
  252. if(denominatorValue != 0)
  253. {
  254. positionRatio = innerContainerPosition / denominatorValue;
  255. positionRatio = MAX(positionRatio, 0);
  256. positionRatio = MIN(positionRatio, 1);
  257. }
  258. float position = (scrollViewMeasure - length - 2 * _marginForLength) * positionRatio + _marginForLength;
  259. if(_direction == ScrollView::Direction::VERTICAL)
  260. {
  261. return Vec2(_parent->getContentSize().width - _marginFromBoundary, position);
  262. }
  263. else
  264. {
  265. return Vec2(position, _marginFromBoundary);
  266. }
  267. }
  268. }
  269. NS_CC_END