UIPageView.cpp 14 KB

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