1
0

UIPageViewIndicator.cpp 7.0 KB

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