UIPageView.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. #ifndef __UIPAGEVIEW_H__
  22. #define __UIPAGEVIEW_H__
  23. #include "ui/UIListView.h"
  24. #include "ui/GUIExport.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. namespace ui {
  31. class PageViewIndicator;
  32. /**
  33. *PageView page turn event type.
  34. *@deprecated Use `PageView::EventType` instead.
  35. */
  36. typedef enum
  37. {
  38. PAGEVIEW_EVENT_TURNING,
  39. }PageViewEventType;
  40. /**
  41. *A callback which would be called when a PageView turning event is happening.
  42. *@deprecated Use `PageView::ccPageViewCallback` instead.
  43. */
  44. typedef void (Ref::*SEL_PageViewEvent)(Ref*, PageViewEventType);
  45. #define pagevieweventselector(_SELECTOR)(SEL_PageViewEvent)(&_SELECTOR)
  46. /**
  47. *@brief Layout manager that allows the user to flip left & right and up & down through pages of data.
  48. *
  49. */
  50. class CC_GUI_DLL PageView : public ListView
  51. {
  52. DECLARE_CLASS_GUI_INFO
  53. public:
  54. /**
  55. * Page turn event type.
  56. */
  57. enum class EventType
  58. {
  59. TURNING
  60. };
  61. /**
  62. * Touch direction type.
  63. */
  64. enum class TouchDirection
  65. {
  66. LEFT,
  67. RIGHT,
  68. UP,
  69. DOWN
  70. };
  71. /**
  72. * PageView page turn event callback.
  73. */
  74. typedef std::function<void(Ref*, EventType)> ccPageViewCallback;
  75. /**
  76. * Default constructor
  77. * @js ctor
  78. * @lua new
  79. */
  80. PageView();
  81. /**
  82. * Default destructor
  83. * @js NA
  84. * @lua NA
  85. */
  86. virtual ~PageView();
  87. /**
  88. * Create an empty PageView.
  89. *@return A PageView instance.
  90. */
  91. static PageView* create();
  92. /**
  93. * Changes direction
  94. * Direction Direction::VERTICAL means vertical scroll, Direction::HORIZONTAL means horizontal scroll.
  95. * @param direction Set the page view's scroll direction.
  96. */
  97. virtual void setDirection(Direction direction) override;
  98. /**
  99. * Add a widget as a page of PageView in a given index.
  100. *
  101. * @param widget Widget to be added to pageview.
  102. * @param pageIdx A given index.
  103. * @param forceCreate If `forceCreate` is true and `widget` isn't exists, pageview would create a default page and add it.
  104. *
  105. * Since v3.9, this is deprecated. Use `insertPage(Widget* page, int idx)` instead.
  106. */
  107. CC_DEPRECATED_ATTRIBUTE void addWidgetToPage(Widget* widget, ssize_t pageIdx, bool forceCreate);
  108. /**
  109. * Insert a page into the end of PageView.
  110. *
  111. * @param page Page to be inserted.
  112. */
  113. void addPage(Widget* page);
  114. /**
  115. * Insert a page into PageView at a given index.
  116. *
  117. * @param page Page to be inserted.
  118. * @param idx A given index.
  119. */
  120. void insertPage(Widget* page, int idx);
  121. /**
  122. * Remove a page of PageView.
  123. *
  124. * @param page Page to be removed.
  125. */
  126. void removePage(Widget* page);
  127. /**
  128. * Remove a page at a given index of PageView.
  129. *
  130. * @param index A given index.
  131. */
  132. void removePageAtIndex(ssize_t index);
  133. /**
  134. * @brief Remove all pages of the PageView.
  135. */
  136. void removeAllPages();
  137. /**
  138. * Scroll to a page with a given index.
  139. *
  140. * @param idx A given index in the PageView. Index start from 0 to pageCount -1.
  141. */
  142. void scrollToPage(ssize_t idx);
  143. /**
  144. * Scroll to a page with a given index and with a given scroll time.
  145. *
  146. * @param idx A given index in the PageView. Index start from 0 to pageCount -1.
  147. * @param time Scroll time must be >= 0. Otherwise last set scroll time will be used.
  148. */
  149. void scrollToPage(ssize_t idx, float time);
  150. /**
  151. * Scroll to a page with a given index.
  152. *
  153. * @param itemIndex A given index in the PageView. Index start from 0 to pageCount -1.
  154. */
  155. void scrollToItem(ssize_t itemIndex);
  156. /**
  157. * Scroll to a item with a given index and with a given scroll time.
  158. *
  159. * @param idx A given index in the PageView. Index start from 0 to pageCount -1.
  160. * @param time Scroll time must be >= 0. Otherwise last set scrolltime will be used.
  161. */
  162. void scrollToItem(ssize_t idx, float time);
  163. /**
  164. * Gets current displayed page index.
  165. * @return current page index.
  166. *
  167. * Since v3.9, this is deprecated. Use `getCurrentPageIndex()` instead.
  168. */
  169. CC_DEPRECATED_ATTRIBUTE ssize_t getCurPageIndex() const;
  170. /**
  171. * Gets current displayed page index.
  172. * @return current page index.
  173. */
  174. ssize_t getCurrentPageIndex();
  175. /**
  176. * Jump to a page with a given index without scrolling.
  177. * This is the different between scrollToPage.
  178. *
  179. * @param index A given index in PageView. Index start from 0 to pageCount -1.
  180. *
  181. * Since v3.9, this is deprecated. Use `setCurrentPageIndex()` instead.
  182. */
  183. CC_DEPRECATED_ATTRIBUTE void setCurPageIndex(ssize_t index);
  184. /**
  185. * Jump to a page with a given index without scrolling.
  186. * This is the different between scrollToPage.
  187. *
  188. * @param index A given index in PageView. Index start from 0 to pageCount -1.
  189. */
  190. void setCurrentPageIndex(ssize_t index);
  191. /**
  192. * @brief Get all the pages in the PageView.
  193. * @return A vector of Layout pointers.
  194. *
  195. * Since v3.9, this is obsolete. Use `Vector<Widget*>& ListView::getItems()` instead.
  196. */
  197. CC_DEPRECATED_ATTRIBUTE Vector<Layout*>& getPages();
  198. /**
  199. * @brief Get a page at a given index
  200. *
  201. * @param index A given index.
  202. * @return A layout pointer in PageView container.
  203. *
  204. * Since v3.9, this is obsolete. Use `Widget* ListView::getItem(index)` instead.
  205. */
  206. CC_DEPRECATED_ATTRIBUTE Layout* getPage(ssize_t index);
  207. /**
  208. * Add a page turn callback to PageView, then when one page is turning, the callback will be called.
  209. *@deprecated Use `PageView::addEventListener` instead.
  210. *@param target A pointer of `Ref*` type.
  211. *@param selector A member function pointer with signature of `SEL_PageViewEvent`.
  212. */
  213. CC_DEPRECATED_ATTRIBUTE void addEventListenerPageView(Ref *target, SEL_PageViewEvent selector);
  214. /**
  215. * @brief Add a page turn callback to PageView, then when one page is turning, the callback will be called.
  216. *
  217. * @param callback A page turning callback.
  218. */
  219. void addEventListener(const ccPageViewCallback& callback);
  220. using ScrollView::addEventListener;
  221. //override methods
  222. virtual std::string getDescription() const override;
  223. /**
  224. * @brief Toggle page indicator enabled.
  225. *
  226. * @param enabled True if enable page indicator, false otherwise.
  227. */
  228. void setIndicatorEnabled(bool enabled);
  229. /**
  230. * @brief Query page indicator state.
  231. *
  232. * @return True if page indicator is enabled, false otherwise.
  233. */
  234. bool getIndicatorEnabled() const { return _indicator != nullptr; }
  235. /**
  236. * @brief Set the page indicator's position using anchor point.
  237. *
  238. * @param positionAsAnchorPoint The position as anchor point.
  239. */
  240. void setIndicatorPositionAsAnchorPoint(const Vec2& positionAsAnchorPoint);
  241. /**
  242. * @brief Get the page indicator's position as anchor point.
  243. *
  244. * @return positionAsAnchorPoint
  245. */
  246. const Vec2& getIndicatorPositionAsAnchorPoint() const;
  247. /**
  248. * @brief Set the page indicator's position in page view.
  249. *
  250. * @param position The position in page view
  251. */
  252. void setIndicatorPosition(const Vec2& position);
  253. /**
  254. * @brief Get the page indicator's position.
  255. *
  256. * @return positionAsAnchorPoint
  257. */
  258. const Vec2& getIndicatorPosition() const;
  259. /**
  260. * @brief Set space between page indicator's index nodes.
  261. *
  262. * @param spaceBetweenIndexNodes Space between nodes in pixel.
  263. */
  264. void setIndicatorSpaceBetweenIndexNodes(float spaceBetweenIndexNodes);
  265. /**
  266. * @brief Get the space between page indicator's index nodes.
  267. *
  268. * @return spaceBetweenIndexNodes
  269. */
  270. float getIndicatorSpaceBetweenIndexNodes() const;
  271. /**
  272. * @brief Set color of page indicator's selected index.
  273. *
  274. * @param color New color for selected (current) index.
  275. */
  276. void setIndicatorSelectedIndexColor(const Color3B& color);
  277. /**
  278. * @brief Get the color of page indicator's selected index.
  279. *
  280. * @return color
  281. */
  282. const Color3B& getIndicatorSelectedIndexColor() const;
  283. /**
  284. * @brief Set color of page indicator's index nodes.
  285. *
  286. * @param color New indicator node color.
  287. */
  288. void setIndicatorIndexNodesColor(const Color3B& color);
  289. /**
  290. * @brief Get the color of page indicator's index nodes.
  291. *
  292. * @return color
  293. */
  294. const Color3B& getIndicatorIndexNodesColor() const;
  295. /**
  296. * @brief Set opacity of page indicator's selected index.
  297. *
  298. * @param color New opacity for selected (current) index.
  299. */
  300. void setIndicatorSelectedIndexOpacity(GLubyte opacity);
  301. /**
  302. * @brief Get the opacity of page indicator's selected index.
  303. *
  304. * @return opacity
  305. */
  306. GLubyte getIndicatorSelectedIndexOpacity() const;
  307. /**
  308. * @brief Set opacity of page indicator's index nodes.
  309. *
  310. * @param opacity New indicator node opacity.
  311. */
  312. void setIndicatorIndexNodesOpacity(GLubyte opacity);
  313. /**
  314. * @brief Get the opacity of page indicator's index nodes.
  315. *
  316. * @return opacity
  317. */
  318. GLubyte getIndicatorIndexNodesOpacity() const;
  319. /**
  320. * @brief Set scale of page indicator's index nodes.
  321. *
  322. * @param indexNodesScale Scale of index nodes.
  323. */
  324. void setIndicatorIndexNodesScale(float indexNodesScale);
  325. /**
  326. * sets texture for index nodes.
  327. *
  328. * @param fileName File name of texture.
  329. * @param resType @see TextureResType .
  330. */
  331. void setIndicatorIndexNodesTexture(const std::string& texName,Widget::TextureResType texType = Widget::TextureResType::LOCAL);
  332. /**
  333. * @brief Get scale of page indicator's index nodes.
  334. *
  335. * @return indexNodesScale
  336. */
  337. float getIndicatorIndexNodesScale() const;
  338. /**
  339. *@brief If you don't specify the value, the pageView will turn page when scrolling at the half width of a page.
  340. *@param threshold A threshold in float.
  341. *@deprecated Since v3.9, this method has no effect.
  342. */
  343. CC_DEPRECATED_ATTRIBUTE void setCustomScrollThreshold(float threshold);
  344. /**
  345. *@brief Query the custom scroll threshold of the PageView.
  346. *@return Custom scroll threshold in float.
  347. *@deprecated Since v3.9, this method always returns 0.
  348. */
  349. CC_DEPRECATED_ATTRIBUTE float getCustomScrollThreshold()const;
  350. /**
  351. *@brief Set using user defined scroll page threshold or not.
  352. * If you set it to false, then the default scroll threshold is pageView.width / 2
  353. *@param flag True if using custom scroll threshold, false otherwise.
  354. *@deprecated Since v3.9, this method has no effect.
  355. */
  356. CC_DEPRECATED_ATTRIBUTE void setUsingCustomScrollThreshold(bool flag);
  357. /**
  358. *@brief Query whether use user defined scroll page threshold or not.
  359. *@return True if using custom scroll threshold, false otherwise.
  360. *@deprecated Since v3.9, this method always returns false.
  361. */
  362. CC_DEPRECATED_ATTRIBUTE bool isUsingCustomScrollThreshold()const;
  363. void setAutoScrollStopEpsilon(float epsilon);
  364. CC_CONSTRUCTOR_ACCESS:
  365. virtual bool init() override;
  366. //override methods
  367. virtual void doLayout() override;
  368. protected:
  369. void pageTurningEvent();
  370. virtual float getAutoScrollStopEpsilon() const override;
  371. virtual void remedyLayoutParameter(Widget* item)override;
  372. virtual void moveInnerContainer(const Vec2& deltaMove, bool canStartBounceBack) override;
  373. virtual void onItemListChanged() override;
  374. virtual void onSizeChanged() override;
  375. virtual void handleReleaseLogic(Touch *touch) override;
  376. virtual void handlePressLogic(Touch *touch) override;
  377. virtual Widget* createCloneInstance() override;
  378. virtual void copySpecialProperties(Widget* model) override;
  379. void refreshIndicatorPosition();
  380. protected:
  381. PageViewIndicator* _indicator;
  382. Vec2 _indicatorPositionAsAnchorPoint;
  383. ssize_t _currentPageIndex;
  384. float _childFocusCancelOffset;
  385. Ref* _pageViewEventListener;
  386. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  387. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  388. #elif _MSC_VER >= 1400 //vs 2005 or higher
  389. #pragma warning (push)
  390. #pragma warning (disable: 4996)
  391. #endif
  392. SEL_PageViewEvent _pageViewEventSelector;
  393. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  394. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  395. #elif _MSC_VER >= 1400 //vs 2005 or higher
  396. #pragma warning (pop)
  397. #endif
  398. ccPageViewCallback _eventCallback;
  399. float _autoScrollStopEpsilon;
  400. ssize_t _previousPageIndex;
  401. bool _isTouchBegin;
  402. };
  403. }
  404. NS_CC_END
  405. // end of ui group
  406. /// @}
  407. #endif /* defined(__PageView__) */