UIScrollViewBar.cpp 9.7 KB

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