UIPageView.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  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/UIPageView.h"
  22. #include "ui/UIPageViewIndicator.h"
  23. NS_CC_BEGIN
  24. namespace ui {
  25. IMPLEMENT_CLASS_GUI_INFO(PageView)
  26. PageView::PageView():
  27. _indicator(nullptr),
  28. _indicatorPositionAsAnchorPoint(Vec2(0.5f, 0.1f)),
  29. _currentPageIndex(-1),
  30. _childFocusCancelOffset(5.0f),
  31. _pageViewEventListener(nullptr),
  32. _pageViewEventSelector(nullptr),
  33. _eventCallback(nullptr),
  34. _autoScrollStopEpsilon(0.001f),
  35. _previousPageIndex(-1),
  36. _isTouchBegin(false)
  37. {
  38. }
  39. PageView::~PageView()
  40. {
  41. _pageViewEventListener = nullptr;
  42. _pageViewEventSelector = nullptr;
  43. }
  44. PageView* PageView::create()
  45. {
  46. PageView* widget = new (std::nothrow) PageView();
  47. if (widget && widget->init())
  48. {
  49. widget->autorelease();
  50. return widget;
  51. }
  52. CC_SAFE_DELETE(widget);
  53. return nullptr;
  54. }
  55. bool PageView::init()
  56. {
  57. if (ListView::init())
  58. {
  59. setDirection(Direction::HORIZONTAL);
  60. setMagneticType(MagneticType::CENTER);
  61. setScrollBarEnabled(false);
  62. return true;
  63. }
  64. return false;
  65. }
  66. void PageView::doLayout()
  67. {
  68. if(!_innerContainerDoLayoutDirty)
  69. {
  70. return;
  71. }
  72. ListView::doLayout();
  73. if(_indicator != nullptr)
  74. {
  75. _currentPageIndex = getIndex(getCenterItemInCurrentView());
  76. _indicator->indicate(_currentPageIndex);
  77. }
  78. _innerContainerDoLayoutDirty = false;
  79. }
  80. void PageView::setDirection(PageView::Direction direction)
  81. {
  82. ListView::setDirection(direction);
  83. if(direction == Direction::HORIZONTAL)
  84. {
  85. _indicatorPositionAsAnchorPoint = Vec2(0.5f, 0.1f);
  86. }
  87. else if(direction == Direction::VERTICAL)
  88. {
  89. _indicatorPositionAsAnchorPoint = Vec2(0.1f, 0.5f);
  90. }
  91. if(_indicator != nullptr)
  92. {
  93. _indicator->setDirection(direction);
  94. refreshIndicatorPosition();
  95. }
  96. }
  97. void PageView::addWidgetToPage(Widget *widget, ssize_t pageIdx, bool /*forceCreate*/)
  98. {
  99. insertCustomItem(widget, pageIdx);
  100. }
  101. void PageView::addPage(Widget* page)
  102. {
  103. pushBackCustomItem(page);
  104. }
  105. void PageView::insertPage(Widget* page, int idx)
  106. {
  107. insertCustomItem(page, idx);
  108. }
  109. void PageView::removePage(Widget* page)
  110. {
  111. removeItem(getIndex(page));
  112. }
  113. void PageView::removePageAtIndex(ssize_t index)
  114. {
  115. removeItem(index);
  116. }
  117. void PageView::removeAllPages()
  118. {
  119. removeAllItems();
  120. }
  121. void PageView::setCurPageIndex( ssize_t index )
  122. {
  123. setCurrentPageIndex(index);
  124. }
  125. ssize_t PageView::getCurrentPageIndex()
  126. {
  127. //The _currentPageIndex is lazy calculated
  128. if (_innerContainerDoLayoutDirty) {
  129. _currentPageIndex = getIndex(getCenterItemInCurrentView());
  130. }
  131. return _currentPageIndex;
  132. }
  133. void PageView::setCurrentPageIndex(ssize_t index)
  134. {
  135. jumpToItem(index, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
  136. }
  137. void PageView::scrollToPage(ssize_t idx)
  138. {
  139. scrollToItem(idx);
  140. }
  141. void PageView::scrollToPage(ssize_t idx, float time)
  142. {
  143. scrollToItem(idx, time);
  144. }
  145. void PageView::scrollToItem(ssize_t itemIndex)
  146. {
  147. if (_innerContainerDoLayoutDirty) {
  148. this->forceDoLayout();
  149. }
  150. ListView::scrollToItem(itemIndex, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE);
  151. }
  152. void PageView::scrollToItem(ssize_t itemIndex, float time)
  153. {
  154. if (_innerContainerDoLayoutDirty) {
  155. this->forceDoLayout();
  156. }
  157. ListView::scrollToItem(itemIndex, Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE, time >= 0 ? time : _scrollTime);
  158. }
  159. void PageView::setCustomScrollThreshold(float /*threshold*/)
  160. {
  161. CCLOG("PageView::setCustomScrollThreshold() has no effect!");
  162. }
  163. float PageView::getCustomScrollThreshold()const
  164. {
  165. return 0;
  166. }
  167. void PageView::setUsingCustomScrollThreshold(bool /*flag*/)
  168. {
  169. CCLOG("PageView::setUsingCustomScrollThreshold() has no effect!");
  170. }
  171. bool PageView::isUsingCustomScrollThreshold()const
  172. {
  173. return false;
  174. }
  175. void PageView::setAutoScrollStopEpsilon(float epsilon)
  176. {
  177. _autoScrollStopEpsilon = epsilon;
  178. }
  179. void PageView::moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack)
  180. {
  181. ListView::moveInnerContainer(deltaMove, canStartBounceBack);
  182. _currentPageIndex = getIndex(getCenterItemInCurrentView());
  183. if(_indicator != nullptr)
  184. {
  185. _indicator->indicate(_currentPageIndex);
  186. }
  187. }
  188. void PageView::onItemListChanged()
  189. {
  190. ListView::onItemListChanged();
  191. if(_indicator != nullptr)
  192. {
  193. _indicator->reset(_items.size());
  194. }
  195. }
  196. void PageView::onSizeChanged()
  197. {
  198. ListView::onSizeChanged();
  199. refreshIndicatorPosition();
  200. }
  201. void PageView::refreshIndicatorPosition()
  202. {
  203. if(_indicator != nullptr)
  204. {
  205. const Size& contentSize = getContentSize();
  206. float posX = contentSize.width * _indicatorPositionAsAnchorPoint.x;
  207. float posY = contentSize.height * _indicatorPositionAsAnchorPoint.y;
  208. _indicator->setPosition(Vec2(posX, posY));
  209. }
  210. }
  211. void PageView::handlePressLogic(Touch *touch)
  212. {
  213. ListView::handlePressLogic(touch);
  214. if (!_isTouchBegin) {
  215. _currentPageIndex = getIndex(getCenterItemInCurrentView());
  216. _previousPageIndex = _currentPageIndex;
  217. _isTouchBegin = true;
  218. }
  219. }
  220. void PageView::handleReleaseLogic(Touch *touch)
  221. {
  222. // Use `ScrollView` method in order to avoid `startMagneticScroll()` by `ListView`.
  223. ScrollView::handleReleaseLogic(touch);
  224. if(_items.empty())
  225. {
  226. return;
  227. }
  228. Vec2 touchMoveVelocity = flattenVectorByDirection(calculateTouchMoveVelocity());
  229. static const float INERTIA_THRESHOLD = 500;
  230. if(touchMoveVelocity.length() < INERTIA_THRESHOLD)
  231. {
  232. startMagneticScroll();
  233. }
  234. else
  235. {
  236. // Handle paging by inertia force.
  237. Widget* currentPage = getItem(_currentPageIndex);
  238. Vec2 destination = calculateItemDestination(Vec2::ANCHOR_MIDDLE, currentPage, Vec2::ANCHOR_MIDDLE);
  239. Vec2 deltaToCurrentpage = destination - getInnerContainerPosition();
  240. deltaToCurrentpage = flattenVectorByDirection(deltaToCurrentpage);
  241. // If the direction of displacement to current page and the direction of touch are same, just start magnetic scroll to the current page.
  242. // Otherwise, move to the next page of touch direction.
  243. if(touchMoveVelocity.x * deltaToCurrentpage.x > 0 || touchMoveVelocity.y * deltaToCurrentpage.y > 0)
  244. {
  245. startMagneticScroll();
  246. }
  247. else
  248. {
  249. if(touchMoveVelocity.x < 0 || touchMoveVelocity.y > 0)
  250. {
  251. ++_currentPageIndex;
  252. }
  253. else
  254. {
  255. --_currentPageIndex;
  256. }
  257. _currentPageIndex = MIN(_currentPageIndex, _items.size() - 1);
  258. _currentPageIndex = MAX(_currentPageIndex, 0);
  259. scrollToItem(_currentPageIndex);
  260. }
  261. }
  262. }
  263. float PageView::getAutoScrollStopEpsilon() const
  264. {
  265. return _autoScrollStopEpsilon;
  266. }
  267. void PageView::addEventListenerPageView(Ref *target, SEL_PageViewEvent selector)
  268. {
  269. _pageViewEventListener = target;
  270. _pageViewEventSelector = selector;
  271. ccScrollViewCallback scrollViewCallback = [=](Ref* /*ref*/, ScrollView::EventType type) -> void{
  272. if (type == ScrollView::EventType::AUTOSCROLL_ENDED && _previousPageIndex != _currentPageIndex) {
  273. pageTurningEvent();
  274. }
  275. };
  276. this->addEventListener(scrollViewCallback);
  277. }
  278. void PageView::pageTurningEvent()
  279. {
  280. this->retain();
  281. if (_pageViewEventListener && _pageViewEventSelector)
  282. {
  283. (_pageViewEventListener->*_pageViewEventSelector)(this, PAGEVIEW_EVENT_TURNING);
  284. }
  285. if (_eventCallback)
  286. {
  287. _eventCallback(this,EventType::TURNING);
  288. }
  289. if (_ccEventCallback)
  290. {
  291. _ccEventCallback(this, static_cast<int>(EventType::TURNING));
  292. }
  293. _isTouchBegin = false;
  294. this->release();
  295. }
  296. void PageView::addEventListener(const ccPageViewCallback& callback)
  297. {
  298. _eventCallback = callback;
  299. ccScrollViewCallback scrollViewCallback = [=](Ref* /*ref*/, ScrollView::EventType type) -> void{
  300. if (type == ScrollView::EventType::AUTOSCROLL_ENDED && _previousPageIndex != _currentPageIndex) {
  301. pageTurningEvent();
  302. }
  303. };
  304. this->addEventListener(scrollViewCallback);
  305. }
  306. ssize_t PageView::getCurPageIndex() const
  307. {
  308. Widget* widget = ListView::getCenterItemInCurrentView();
  309. return getIndex(widget);
  310. }
  311. Vector<Layout*>& PageView::getPages()
  312. {
  313. CCLOG("This method is obsolete!");
  314. // Temporary code to keep backward compatibility.
  315. static Vector<Layout*> pages;
  316. pages.clear();
  317. for(Widget* widget : getItems())
  318. {
  319. pages.pushBack(dynamic_cast<Layout*>(widget));
  320. }
  321. return pages;
  322. }
  323. Layout* PageView::getPage(ssize_t index)
  324. {
  325. if (index < 0 || index >= this->getItems().size())
  326. {
  327. return nullptr;
  328. }
  329. // Temporary code to keep backward compatibility.
  330. static Vector<Layout*> pages;
  331. pages.clear();
  332. for(Widget* widget : getItems())
  333. {
  334. pages.pushBack(dynamic_cast<Layout*>(widget));
  335. }
  336. return pages.at(index);
  337. }
  338. std::string PageView::getDescription() const
  339. {
  340. return "PageView";
  341. }
  342. Widget* PageView::createCloneInstance()
  343. {
  344. return PageView::create();
  345. }
  346. void PageView::copySpecialProperties(Widget *widget)
  347. {
  348. PageView* pageView = dynamic_cast<PageView*>(widget);
  349. if (pageView)
  350. {
  351. ListView::copySpecialProperties(widget);
  352. _eventCallback = pageView->_eventCallback;
  353. _ccEventCallback = pageView->_ccEventCallback;
  354. _pageViewEventListener = pageView->_pageViewEventListener;
  355. _pageViewEventSelector = pageView->_pageViewEventSelector;
  356. _currentPageIndex = pageView->_currentPageIndex;
  357. _previousPageIndex = pageView->_previousPageIndex;
  358. _childFocusCancelOffset = pageView->_childFocusCancelOffset;
  359. _autoScrollStopEpsilon = pageView->_autoScrollStopEpsilon;
  360. _indicatorPositionAsAnchorPoint = pageView->_indicatorPositionAsAnchorPoint;
  361. _isTouchBegin = pageView->_isTouchBegin;
  362. }
  363. }
  364. void PageView::setIndicatorEnabled(bool enabled)
  365. {
  366. if(enabled == (_indicator != nullptr))
  367. {
  368. return;
  369. }
  370. if(!enabled)
  371. {
  372. removeProtectedChild(_indicator);
  373. _indicator = nullptr;
  374. }
  375. else
  376. {
  377. _indicator = PageViewIndicator::create();
  378. _indicator->setDirection(getDirection());
  379. addProtectedChild(_indicator, 10000);
  380. setIndicatorSelectedIndexColor(Color3B(100, 100, 255));
  381. refreshIndicatorPosition();
  382. }
  383. }
  384. void PageView::setIndicatorPositionAsAnchorPoint(const Vec2& positionAsAnchorPoint)
  385. {
  386. _indicatorPositionAsAnchorPoint = positionAsAnchorPoint;
  387. refreshIndicatorPosition();
  388. }
  389. const Vec2& PageView::getIndicatorPositionAsAnchorPoint() const
  390. {
  391. return _indicatorPositionAsAnchorPoint;
  392. }
  393. void PageView::setIndicatorPosition(const Vec2& position)
  394. {
  395. if(_indicator != nullptr)
  396. {
  397. const Size& contentSize = getContentSize();
  398. _indicatorPositionAsAnchorPoint.x = position.x / contentSize.width;
  399. _indicatorPositionAsAnchorPoint.y = position.y / contentSize.height;
  400. _indicator->setPosition(position);
  401. }
  402. }
  403. const Vec2& PageView::getIndicatorPosition() const
  404. {
  405. CCASSERT(_indicator != nullptr, "");
  406. return _indicator->getPosition();
  407. }
  408. void PageView::setIndicatorSpaceBetweenIndexNodes(float spaceBetweenIndexNodes)
  409. {
  410. if(_indicator != nullptr)
  411. {
  412. _indicator->setSpaceBetweenIndexNodes(spaceBetweenIndexNodes);
  413. }
  414. }
  415. float PageView::getIndicatorSpaceBetweenIndexNodes() const
  416. {
  417. CCASSERT(_indicator != nullptr, "");
  418. return _indicator->getSpaceBetweenIndexNodes();
  419. }
  420. void PageView::setIndicatorSelectedIndexColor(const Color3B& color)
  421. {
  422. if(_indicator != nullptr)
  423. {
  424. _indicator->setSelectedIndexColor(color);
  425. }
  426. }
  427. const Color3B& PageView::getIndicatorSelectedIndexColor() const
  428. {
  429. CCASSERT(_indicator != nullptr, "");
  430. return _indicator->getSelectedIndexColor();
  431. }
  432. void PageView::setIndicatorIndexNodesColor(const Color3B& color)
  433. {
  434. if(_indicator != nullptr)
  435. {
  436. _indicator->setIndexNodesColor(color);
  437. }
  438. }
  439. const Color3B& PageView::getIndicatorIndexNodesColor() const
  440. {
  441. CCASSERT(_indicator != nullptr, "");
  442. return _indicator->getIndexNodesColor();
  443. }
  444. void PageView::setIndicatorSelectedIndexOpacity(GLubyte opacity)
  445. {
  446. if(_indicator != nullptr)
  447. {
  448. _indicator->setSelectedIndexOpacity(opacity);
  449. }
  450. }
  451. GLubyte PageView::getIndicatorSelectedIndexOpacity() const
  452. {
  453. CCASSERT(_indicator != nullptr, "");
  454. return _indicator->getSelectedIndexOpacity();
  455. }
  456. void PageView::setIndicatorIndexNodesOpacity(GLubyte opacity)
  457. {
  458. if(_indicator != nullptr)
  459. {
  460. _indicator->setIndexNodesOpacity(opacity);
  461. }
  462. }
  463. GLubyte PageView::getIndicatorIndexNodesOpacity() const
  464. {
  465. CCASSERT(_indicator != nullptr, "");
  466. return _indicator->getIndexNodesOpacity();
  467. }
  468. void PageView::setIndicatorIndexNodesScale(float indexNodesScale)
  469. {
  470. if(_indicator != nullptr)
  471. {
  472. _indicator->setIndexNodesScale(indexNodesScale);
  473. _indicator->indicate(_currentPageIndex);
  474. }
  475. }
  476. float PageView::getIndicatorIndexNodesScale() const
  477. {
  478. CCASSERT(_indicator != nullptr, "");
  479. return _indicator->getIndexNodesScale();
  480. }
  481. void PageView::setIndicatorIndexNodesTexture(const std::string& texName,Widget::TextureResType texType)
  482. {
  483. if(_indicator != nullptr)
  484. {
  485. _indicator->setIndexNodesTexture(texName, texType);
  486. _indicator->indicate(_currentPageIndex);
  487. }
  488. }
  489. void PageView::remedyLayoutParameter(Widget *item)
  490. {
  491. item->setContentSize(this->getContentSize());
  492. ListView::remedyLayoutParameter(item);
  493. }
  494. }
  495. NS_CC_END