UIListView.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 __UILISTVIEW_H__
  22. #define __UILISTVIEW_H__
  23. #include "ui/UIScrollView.h"
  24. #include "ui/GUIExport.h"
  25. /**
  26. * @addtogroup ui
  27. * @{
  28. */
  29. NS_CC_BEGIN
  30. namespace ui{
  31. /**
  32. * ListView click item event type.
  33. */
  34. typedef enum
  35. {
  36. LISTVIEW_ONSELECTEDITEM_START,
  37. LISTVIEW_ONSELECTEDITEM_END
  38. }ListViewEventType;
  39. /**
  40. * A callback which would be called when a ListView item is clicked.
  41. *@deprecated Use `ccListViewCallback` instead.
  42. */
  43. typedef void (Ref::*SEL_ListViewEvent)(Ref*,ListViewEventType);
  44. #define listvieweventselector(_SELECTOR) (SEL_ListViewEvent)(&_SELECTOR)
  45. /**
  46. *@brief ListView is a view group that displays a list of scrollable items.
  47. *The list items are inserted to the list by using `addChild` or `insertDefaultItem`.
  48. * @warning The list item in ListView doesn't support cell reuse at the moment, if you have a large amount of data need to be displayed, use `TableView` instead.
  49. * ListView is a subclass of `ScrollView`, so it shares many features of ScrollView.
  50. */
  51. class CC_GUI_DLL ListView : public ScrollView
  52. {
  53. DECLARE_CLASS_GUI_INFO
  54. public:
  55. /**
  56. * Gravity for docking elements in ListView.
  57. */
  58. enum class Gravity
  59. {
  60. LEFT,
  61. RIGHT,
  62. CENTER_HORIZONTAL,
  63. TOP,
  64. BOTTOM,
  65. CENTER_VERTICAL
  66. };
  67. /**
  68. * ListView element item click event.
  69. */
  70. enum class EventType
  71. {
  72. ON_SELECTED_ITEM_START,
  73. ON_SELECTED_ITEM_END
  74. };
  75. /**
  76. * ListView supports magnetic scroll.
  77. * With CENTER type, ListView tries to align its items in center of current view.
  78. * With BOTH_END type, ListView tries to align its items in left or right end if it is horizontal, top or bottom in vertical. The aligning side (left or right, top or bottom) is determined by user's scroll direction.
  79. */
  80. enum class MagneticType
  81. {
  82. NONE,
  83. CENTER,
  84. BOTH_END,
  85. LEFT,
  86. RIGHT,
  87. TOP,
  88. BOTTOM,
  89. };
  90. /**
  91. * ListView item click callback.
  92. */
  93. typedef std::function<void(Ref*, EventType)> ccListViewCallback;
  94. /**
  95. * Default constructor
  96. * @js ctor
  97. * @lua new
  98. */
  99. ListView();
  100. /**
  101. * Default destructor
  102. * @js NA
  103. * @lua NA
  104. */
  105. virtual ~ListView();
  106. /**
  107. * Create an empty ListView.
  108. *@return A ListView instance.
  109. */
  110. static ListView* create();
  111. /**
  112. * Set an item model for listview.
  113. *
  114. * When calling `pushBackDefaultItem`, the model will be used as a blueprint and new model copy will be inserted into ListView.
  115. * @param model Model in `Widget*`.
  116. */
  117. void setItemModel(Widget* model);
  118. /**
  119. * Insert a default item(create by a cloned model) at the end of the listview.
  120. */
  121. void pushBackDefaultItem();
  122. /**
  123. * Insert a default item(create by cloning model) into listview at a give index.
  124. *@param index An index in ssize_t.
  125. */
  126. void insertDefaultItem(ssize_t index);
  127. /**
  128. * Insert a custom item into the end of ListView.
  129. *@param item An item in `Widget*`.
  130. */
  131. void pushBackCustomItem(Widget* item);
  132. /**
  133. * @brief Insert a custom widget into ListView at a given index.
  134. *
  135. * @param item A widget pointer to be inserted.
  136. * @param index A given index in ssize_t.
  137. */
  138. void insertCustomItem(Widget* item, ssize_t index);
  139. /**
  140. * Removes the last item of ListView.
  141. */
  142. void removeLastItem();
  143. /**
  144. * Remove an item at given index.
  145. *
  146. * @param index A given index in ssize_t.
  147. */
  148. void removeItem(ssize_t index);
  149. /**
  150. * @brief Remove all items in current ListView.
  151. *
  152. */
  153. void removeAllItems();
  154. /**
  155. * Return an item at a given index.
  156. *
  157. * @param index A given index in ssize_t.
  158. * @return A widget instance.
  159. */
  160. Widget* getItem(ssize_t index)const;
  161. /**
  162. * Return all items in a ListView.
  163. *@returns A vector of widget pointers.
  164. */
  165. Vector<Widget*>& getItems();
  166. /**
  167. * Return the index of specified widget.
  168. *
  169. * @param item A widget pointer.
  170. * @return The index of a given widget in ListView.
  171. */
  172. ssize_t getIndex(Widget* item) const;
  173. /**
  174. * Set the gravity of ListView.
  175. * @see `ListViewGravity`
  176. */
  177. void setGravity(Gravity gravity);
  178. /**
  179. * Set magnetic type of ListView.
  180. * @see `MagneticType`
  181. */
  182. void setMagneticType(MagneticType magneticType);
  183. /**
  184. * Get magnetic type of ListView.
  185. */
  186. MagneticType getMagneticType() const;
  187. /**
  188. * Set magnetic allowed out of boundary.
  189. */
  190. void setMagneticAllowedOutOfBoundary(bool magneticAllowedOutOfBoundary);
  191. /**
  192. * Query whether the magnetic out of boundary is allowed.
  193. */
  194. bool getMagneticAllowedOutOfBoundary() const;
  195. /**
  196. * Set the margin between each item in ListView.
  197. *
  198. * @param margin A margin in float.
  199. */
  200. void setItemsMargin(float margin);
  201. /**
  202. * @brief Query margin between each item in ListView.
  203. *
  204. * @return A margin in float.
  205. */
  206. float getItemsMargin()const;
  207. /**
  208. * Change padding with left, top, right, and bottom padding.
  209. * @param l Left padding in float.
  210. * @param t Top margin in float.
  211. * @param r Right margin in float.
  212. * @param b Bottom margin in float.
  213. */
  214. void setPadding(float l, float t, float r, float b);
  215. /**
  216. * Change padding with left padding
  217. * @param l Left padding in float.
  218. */
  219. void setLeftPadding(float l);
  220. /**
  221. * Change padding with top padding
  222. * @param t Top padding in float
  223. */
  224. void setTopPadding(float t);
  225. /**
  226. * Change padding with right padding
  227. * @param r Right padding in float
  228. */
  229. void setRightPadding(float r);
  230. /**
  231. * Change padding with bottom padding
  232. * @param b Bottom padding in float
  233. */
  234. void setBottomPadding(float b);
  235. /**
  236. * Get the left padding in ListView
  237. * @return Left padding in float
  238. */
  239. float getLeftPadding() const;
  240. /**
  241. * Get the top padding in ListView
  242. * @return Top padding in float
  243. */
  244. float getTopPadding() const;
  245. /**
  246. * Get the right padding in ListView
  247. * @return Right padding in float
  248. */
  249. float getRightPadding() const;
  250. /**
  251. * Get the bottom padding in ListView
  252. * @return Bottom padding in float
  253. */
  254. float getBottomPadding() const;
  255. /**
  256. * Set the time in seconds to scroll between items.
  257. * Subsequent calls of function 'scrollToItem', will take 'time' seconds for scrolling.
  258. * @param time The seconds needed to scroll between two items. 'time' must be >= 0
  259. * @see scrollToItem(ssize_t, const Vec2&, const Vec2&)
  260. */
  261. void setScrollDuration(float time);
  262. /**
  263. * Get the time in seconds to scroll between items.
  264. * @return The time in seconds to scroll between items
  265. * @see setScrollDuration(float)
  266. */
  267. float getScrollDuration() const;
  268. //override methods
  269. virtual void doLayout() override;
  270. virtual void requestDoLayout() override;
  271. virtual void addChild(Node* child)override;
  272. virtual void addChild(Node * child, int localZOrder)override;
  273. virtual void addChild(Node* child, int zOrder, int tag) override;
  274. virtual void addChild(Node* child, int zOrder, const std::string &name) override;
  275. virtual void removeAllChildren() override;
  276. virtual void removeAllChildrenWithCleanup(bool cleanup) override;
  277. virtual void removeChild(Node* child, bool cleanup = true) override;
  278. /**
  279. * @brief Query the closest item to a specific position in inner container.
  280. *
  281. * @param targetPosition Specifies the target position in inner container's coordinates.
  282. * @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.
  283. * @return An item instance if list view is not empty. Otherwise, returns null.
  284. */
  285. Widget* getClosestItemToPosition(const Vec2& targetPosition, const Vec2& itemAnchorPoint) const;
  286. /**
  287. * @brief Query the closest item to a specific position in current view.
  288. * For instance, to find the item in the center of view, call 'getClosestItemToPositionInCurrentView(Vec2::ANCHOR_MIDDLE, Vec2::ANCHOR_MIDDLE)'.
  289. *
  290. * @param positionRatioInView Specifies the target position with ratio in list view's content size.
  291. * @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.
  292. * @return An item instance if list view is not empty. Otherwise, returns null.
  293. */
  294. Widget* getClosestItemToPositionInCurrentView(const Vec2& positionRatioInView, const Vec2& itemAnchorPoint) const;
  295. /**
  296. * @brief Query the center item
  297. * @return An item instance.
  298. */
  299. Widget* getCenterItemInCurrentView() const;
  300. /**
  301. * @brief Query the leftmost item in horizontal list
  302. * @return An item instance.
  303. */
  304. Widget* getLeftmostItemInCurrentView() const;
  305. /**
  306. * @brief Query the rightmost item in horizontal list
  307. * @return An item instance.
  308. */
  309. Widget* getRightmostItemInCurrentView() const;
  310. /**
  311. * @brief Query the topmost item in horizontal list
  312. * @return An item instance.
  313. */
  314. Widget* getTopmostItemInCurrentView() const;
  315. /**
  316. * @brief Query the bottommost item in horizontal list
  317. * @return An item instance.
  318. */
  319. Widget* getBottommostItemInCurrentView() const;
  320. /**
  321. * Override functions
  322. */
  323. virtual void jumpToBottom() override;
  324. virtual void jumpToTop() override;
  325. virtual void jumpToLeft() override;
  326. virtual void jumpToRight() override;
  327. virtual void jumpToTopLeft() override;
  328. virtual void jumpToTopRight() override;
  329. virtual void jumpToBottomLeft() override;
  330. virtual void jumpToBottomRight() override;
  331. virtual void jumpToPercentVertical(float percent) override;
  332. virtual void jumpToPercentHorizontal(float percent) override;
  333. virtual void jumpToPercentBothDirection(const Vec2& percent) override;
  334. /**
  335. * @brief Jump to specific item
  336. * @param itemIndex Specifies the item's index
  337. * @param positionRatioInView Specifies the position with ratio in list view's content size.
  338. * @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.
  339. */
  340. void jumpToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint);
  341. /**
  342. * @brief Scroll to specific item
  343. * @param positionRatioInView Specifies the position with ratio in list view's content size.
  344. * @param itemAnchorPoint Specifies an anchor point of each item for position to calculate distance.
  345. * @param timeInSec Scroll time
  346. */
  347. void scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint);
  348. void scrollToItem(ssize_t itemIndex, const Vec2& positionRatioInView, const Vec2& itemAnchorPoint, float timeInSec);
  349. /**
  350. * @brief Query current selected widget's index.
  351. *
  352. * @return An index of a selected item.
  353. */
  354. ssize_t getCurSelectedIndex() const;
  355. /**
  356. * @brief Set current selected widget's index and call TouchEventType::ENDED event.
  357. * @param itemIndex A index of a selected item.
  358. */
  359. void setCurSelectedIndex(int itemIndex);
  360. /**
  361. * Add an event click callback to ListView, then one item of Listview is clicked, the callback will be called.
  362. *@deprecated Use `addEventListener` instead.
  363. *@param target A pointer of `Ref*` type.
  364. *@param selector A member function pointer with type of `SEL_ListViewEvent`.
  365. */
  366. CC_DEPRECATED_ATTRIBUTE void addEventListenerListView(Ref* target, SEL_ListViewEvent selector);
  367. /**
  368. * Add an event click callback to ListView, then one item of Listview is clicked, the callback will be called.
  369. *@param callback A callback function with type of `ccListViewCallback`.
  370. */
  371. void addEventListener(const ccListViewCallback& callback);
  372. using ScrollView::addEventListener;
  373. /**
  374. * Changes scroll direction of scrollview.
  375. *
  376. * Direction Direction::VERTICAL means vertical scroll, Direction::HORIZONTAL means horizontal scroll.
  377. * @param dir Set the list view's scroll direction.
  378. */
  379. virtual void setDirection(Direction dir) override;
  380. virtual std::string getDescription() const override;
  381. /**
  382. * @brief Refresh view and layout of ListView manually.
  383. * This method will mark ListView content as dirty and the content view will be refreshed in the next frame.
  384. * @deprecated Use method requestDoLayout() instead
  385. */
  386. CC_DEPRECATED_ATTRIBUTE void requestRefreshView();
  387. /**
  388. * @brief Refresh content view of ListView.
  389. * @deprecated Use method forceDoLayout() instead
  390. */
  391. CC_DEPRECATED_ATTRIBUTE void refreshView();
  392. CC_CONSTRUCTOR_ACCESS:
  393. virtual bool init() override;
  394. protected:
  395. virtual void handleReleaseLogic(Touch *touch) override;
  396. virtual void onItemListChanged();
  397. virtual void remedyLayoutParameter(Widget* item);
  398. void updateInnerContainerSize();
  399. void remedyVerticalLayoutParameter(LinearLayoutParameter* layoutParameter, ssize_t itemIndex);
  400. void remedyHorizontalLayoutParameter(LinearLayoutParameter* layoutParameter,ssize_t itemIndex);
  401. virtual void onSizeChanged() override;
  402. virtual Widget* createCloneInstance() override;
  403. virtual void copySpecialProperties(Widget* model) override;
  404. virtual void copyClonedWidgetChildren(Widget* model) override;
  405. void selectedItemEvent(TouchEventType event);
  406. virtual void interceptTouchEvent(Widget::TouchEventType event,Widget* sender,Touch* touch) override;
  407. virtual Vec2 getHowMuchOutOfBoundary(const Vec2& addition = Vec2::ZERO) override;
  408. virtual void startAttenuatingAutoScroll(const Vec2& deltaMove, const Vec2& initialVelocity) override;
  409. void startMagneticScroll();
  410. Vec2 calculateItemDestination(const Vec2& positionRatioInView, Widget* item, const Vec2& itemAnchorPoint);
  411. protected:
  412. Widget* _model;
  413. Vector<Widget*> _items;
  414. Gravity _gravity;
  415. MagneticType _magneticType;
  416. bool _magneticAllowedOutOfBoundary;
  417. float _itemsMargin;
  418. float _leftPadding;
  419. float _topPadding;
  420. float _rightPadding;
  421. float _bottomPadding;
  422. float _scrollTime;
  423. ssize_t _curSelectedIndex;
  424. bool _innerContainerDoLayoutDirty;
  425. Ref* _listViewEventListener;
  426. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  427. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  428. #elif _MSC_VER >= 1400 //vs 2005 or higher
  429. #pragma warning (push)
  430. #pragma warning (disable: 4996)
  431. #endif
  432. SEL_ListViewEvent _listViewEventSelector;
  433. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  434. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  435. #elif _MSC_VER >= 1400 //vs 2005 or higher
  436. #pragma warning (pop)
  437. #endif
  438. ccListViewCallback _eventCallback;
  439. };
  440. }
  441. NS_CC_END
  442. // end of ui group
  443. /// @}
  444. #endif /* defined(__ListView__) */