UIPageViewIndicator.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/UIPageViewIndicator.h"
  22. #include "2d/CCSprite.h"
  23. #include "base/ccUtils.h"
  24. static const char* CIRCLE_IMAGE = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAQAAADZc7J/AAAA8ElEQVRIx62VyRGCQBBF+6gWRCEmYDIQkhiBCgHhSclC8YqWzOV5oVzKAYZp3r1/9fpbxAIBMTsKrjx5cqVgR0wgLhCRUWOjJiPqD56xoaGPhpRZV/iSEy6crHmw5oIrF9b/lVeMofrJgjlnxlIy/wik+JB+mme8BExbBhm+5CJC2LE2LtSEQoyGWDioBA5CoRIohJtK4CYDxzNEM4GAugR1E9VjVC+SZpXvhCJCrjomESLvc17pDGX7bWmlh6UtpjPVCWy9zaJ0TD7qfm3pwERMz2trRVZk3K3BD/L34AY+dEDCniMVBkPFkT2J/b2/AIV+dRpFLOYoAAAAAElFTkSuQmCC";
  25. static const char* CIRCLE_IMAGE_KEY = "/__circleImage";
  26. NS_CC_BEGIN
  27. namespace {
  28. static const float SPACE_BETWEEN_INDEX_NODES_DEFAULT = 23;
  29. static const GLubyte INDEX_NODES_OPACITY_DEFAULT = 0.3*255;
  30. }
  31. namespace ui {
  32. PageViewIndicator* PageViewIndicator::create()
  33. {
  34. PageViewIndicator* node = new (std::nothrow) PageViewIndicator();
  35. if (node && node->init())
  36. {
  37. node->autorelease();
  38. return node;
  39. }
  40. CC_SAFE_DELETE(node);
  41. return nullptr;
  42. }
  43. PageViewIndicator::PageViewIndicator()
  44. : _direction(PageView::Direction::HORIZONTAL)
  45. , _currentIndexNode(nullptr)
  46. , _currentOverlappingIndexNode(nullptr)
  47. , _spaceBetweenIndexNodes(SPACE_BETWEEN_INDEX_NODES_DEFAULT)
  48. , _indexNodesScale(1.0f)
  49. , _indexNodesColor(Color3B::WHITE)
  50. , _indexNodesOpacity(INDEX_NODES_OPACITY_DEFAULT)
  51. , _useDefaultTexture(true)
  52. , _indexNodesTextureFile("")
  53. , _indexNodesTexType(Widget::TextureResType::LOCAL)
  54. {
  55. }
  56. PageViewIndicator::~PageViewIndicator()
  57. {
  58. }
  59. bool PageViewIndicator::init()
  60. {
  61. _currentIndexNode = utils::createSpriteFromBase64Cached(CIRCLE_IMAGE, CIRCLE_IMAGE_KEY);
  62. _currentIndexNode->setVisible(false);
  63. addProtectedChild(_currentIndexNode, 1);
  64. return true;
  65. }
  66. void PageViewIndicator::setDirection(PageView::Direction direction)
  67. {
  68. _direction = direction;
  69. rearrange();
  70. }
  71. void PageViewIndicator::reset(ssize_t numberOfTotalPages)
  72. {
  73. while(_indexNodes.size() < numberOfTotalPages)
  74. {
  75. increaseNumberOfPages();
  76. }
  77. while(_indexNodes.size() > numberOfTotalPages)
  78. {
  79. decreaseNumberOfPages();
  80. }
  81. rearrange();
  82. _currentIndexNode->setVisible(!_indexNodes.empty());
  83. }
  84. void PageViewIndicator::indicate(ssize_t index)
  85. {
  86. if (index < 0 || index >= _indexNodes.size())
  87. {
  88. return;
  89. }
  90. Sprite* oldOverlappingNode = _currentOverlappingIndexNode;
  91. _currentOverlappingIndexNode = _indexNodes.at(index);
  92. if ( oldOverlappingNode != _currentOverlappingIndexNode ) {
  93. if ( oldOverlappingNode )
  94. oldOverlappingNode->setVisible(true);
  95. _currentOverlappingIndexNode->setVisible(false);
  96. _currentIndexNode->setPosition(_currentOverlappingIndexNode->getPosition());
  97. }
  98. }
  99. void PageViewIndicator::rearrange()
  100. {
  101. if(_indexNodes.empty())
  102. {
  103. return;
  104. }
  105. bool horizontal = (_direction == PageView::Direction::HORIZONTAL);
  106. // Calculate total size
  107. Size indexNodeSize = _indexNodes.at(0)->getContentSize();
  108. float sizeValue = (horizontal ? indexNodeSize.width : indexNodeSize.height);
  109. ssize_t numberOfItems = _indexNodes.size();
  110. float totalSizeValue = sizeValue * numberOfItems + _spaceBetweenIndexNodes * (numberOfItems - 1);
  111. float posValue = -(totalSizeValue / 2) + (sizeValue / 2);
  112. for(auto& indexNode : _indexNodes) {
  113. Vec2 position;
  114. if(horizontal)
  115. {
  116. position = Vec2(posValue, indexNodeSize.height / 2.0f);
  117. }
  118. else
  119. {
  120. position = Vec2(indexNodeSize.width / 2.0f, -posValue);
  121. }
  122. indexNode->setPosition(position);
  123. posValue += sizeValue + _spaceBetweenIndexNodes;
  124. }
  125. }
  126. void PageViewIndicator::setSpaceBetweenIndexNodes(float spaceBetweenIndexNodes)
  127. {
  128. if(_spaceBetweenIndexNodes == spaceBetweenIndexNodes)
  129. {
  130. return;
  131. }
  132. _spaceBetweenIndexNodes = spaceBetweenIndexNodes;
  133. rearrange();
  134. }
  135. void PageViewIndicator::setIndexNodesColor(const Color3B& indexNodesColor)
  136. {
  137. _indexNodesColor = indexNodesColor;
  138. for(auto& indexNode : _indexNodes) {
  139. indexNode->setColor(indexNodesColor);
  140. }
  141. }
  142. void PageViewIndicator::setIndexNodesOpacity(GLubyte opacity) {
  143. _indexNodesOpacity = opacity;
  144. for ( auto& indexNode : _indexNodes )
  145. indexNode->setOpacity(opacity);
  146. }
  147. void PageViewIndicator::setIndexNodesScale(float indexNodesScale)
  148. {
  149. if(_indexNodesScale == indexNodesScale)
  150. {
  151. return;
  152. }
  153. _indexNodesScale = indexNodesScale;
  154. _currentIndexNode->setScale(indexNodesScale);
  155. for(auto& indexNode : _indexNodes) {
  156. indexNode->setScale(_indexNodesScale);
  157. }
  158. rearrange();
  159. }
  160. void PageViewIndicator::setIndexNodesTexture(const std::string& texName, Widget::TextureResType texType)
  161. {
  162. _useDefaultTexture = false;
  163. _indexNodesTextureFile = texName;
  164. _indexNodesTexType = texType;
  165. switch (texType)
  166. {
  167. case Widget::TextureResType::LOCAL:
  168. _currentIndexNode->setTexture(texName);
  169. for(auto& indexNode : _indexNodes) {
  170. indexNode->setTexture(texName);
  171. }
  172. break;
  173. case Widget::TextureResType::PLIST:
  174. _currentIndexNode->setSpriteFrame(texName);
  175. for(auto& indexNode : _indexNodes) {
  176. indexNode->setSpriteFrame(texName);
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. rearrange();
  183. }
  184. void PageViewIndicator::increaseNumberOfPages()
  185. {
  186. if ( _currentOverlappingIndexNode ) {
  187. _currentOverlappingIndexNode->setVisible(true);
  188. _currentOverlappingIndexNode = nullptr;
  189. }
  190. Sprite* indexNode;
  191. if(_useDefaultTexture)
  192. {
  193. indexNode = utils::createSpriteFromBase64(CIRCLE_IMAGE);
  194. }
  195. else
  196. {
  197. switch (_indexNodesTexType)
  198. {
  199. case Widget::TextureResType::LOCAL:
  200. indexNode = Sprite::create(_indexNodesTextureFile);
  201. break;
  202. case Widget::TextureResType::PLIST:
  203. indexNode = Sprite::createWithSpriteFrameName(_indexNodesTextureFile);
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. indexNode->setColor(_indexNodesColor);
  210. indexNode->setScale(_indexNodesScale);
  211. indexNode->setOpacity(_indexNodesOpacity);
  212. addProtectedChild(indexNode);
  213. _indexNodes.pushBack(indexNode);
  214. }
  215. void PageViewIndicator::decreaseNumberOfPages()
  216. {
  217. if ( _currentOverlappingIndexNode ) {
  218. _currentOverlappingIndexNode->setVisible(true);
  219. _currentOverlappingIndexNode = nullptr;
  220. }
  221. if(_indexNodes.empty())
  222. {
  223. return;
  224. }
  225. removeProtectedChild(*_indexNodes.begin());
  226. _indexNodes.erase(_indexNodes.begin());
  227. }
  228. void PageViewIndicator::clear()
  229. {
  230. if ( _currentOverlappingIndexNode ) {
  231. _currentOverlappingIndexNode->setVisible(true);
  232. _currentOverlappingIndexNode = nullptr;
  233. }
  234. for(auto& indexNode : _indexNodes)
  235. {
  236. removeProtectedChild(indexNode);
  237. }
  238. _indexNodes.clear();
  239. _currentIndexNode->setVisible(false);
  240. }
  241. }
  242. NS_CC_END