CCTableView.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /****************************************************************************
  2. Copyright (c) 2012 cocos2d-x.org
  3. Copyright (c) 2010 Sangwoo Im
  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 __CCTABLEVIEW_H__
  22. #define __CCTABLEVIEW_H__
  23. #include "CCScrollView.h"
  24. #include "CCTableViewCell.h"
  25. #include "extensions/ExtensionExport.h"
  26. #include <set>
  27. #include <vector>
  28. /**
  29. * @addtogroup ui
  30. * @{
  31. */
  32. NS_CC_EXT_BEGIN
  33. class TableView;
  34. /**
  35. * Sole purpose of this delegate is to single touch event in this version.
  36. */
  37. class CC_EX_DLL TableViewDelegate : public ScrollViewDelegate
  38. {
  39. public:
  40. /**
  41. * Delegate to respond touch event
  42. *
  43. * @param table table contains the given cell
  44. * @param cell cell that is touched
  45. * @js NA
  46. * @lua NA
  47. */
  48. virtual void tableCellTouched(TableView* table, TableViewCell* cell) = 0;
  49. /**
  50. * Delegate to respond a table cell press event.
  51. *
  52. * @param table table contains the given cell
  53. * @param cell cell that is pressed
  54. * @js NA
  55. * @lua NA
  56. */
  57. virtual void tableCellHighlight(TableView* table, TableViewCell* cell);
  58. /**
  59. * Delegate to respond a table cell release event
  60. *
  61. * @param table table contains the given cell
  62. * @param cell cell that is pressed
  63. * @js NA
  64. * @lua NA
  65. */
  66. virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell);
  67. /**
  68. * Delegate called when the cell is about to be recycled. Immediately
  69. * after this call the cell will be removed from the scene graph and
  70. * recycled.
  71. *
  72. * @param table table contains the given cell
  73. * @param cell cell that is pressed
  74. * @js NA
  75. * @lua NA
  76. */
  77. virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell);
  78. };
  79. /**
  80. * Data source that governs table backend data.
  81. */
  82. class CC_EX_DLL TableViewDataSource
  83. {
  84. public:
  85. /**
  86. * @js NA
  87. * @lua NA
  88. */
  89. virtual ~TableViewDataSource() {}
  90. /**
  91. * cell size for a given index
  92. *
  93. * @param idx the index of a cell to get a size
  94. * @return size of a cell at given index
  95. */
  96. virtual Size tableCellSizeForIndex(TableView* table, ssize_t idx);
  97. /**
  98. * cell height for a given table.
  99. *
  100. * @param table table to hold the instances of Class
  101. * @return cell size
  102. */
  103. virtual Size cellSizeForTable(TableView* table);
  104. /**
  105. * a cell instance at a given index
  106. *
  107. * @param idx index to search for a cell
  108. * @return cell found at idx
  109. */
  110. virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx) = 0;
  111. /**
  112. * Returns number of cells in a given table view.
  113. *
  114. * @return number of cells
  115. */
  116. virtual ssize_t numberOfCellsInTableView(TableView *table) = 0;
  117. };
  118. /**
  119. * UITableView support for cocos2d-x.
  120. *
  121. * This is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
  122. */
  123. class CC_EX_DLL TableView : public ScrollView, public ScrollViewDelegate
  124. {
  125. public:
  126. enum class VerticalFillOrder
  127. {
  128. TOP_DOWN,
  129. BOTTOM_UP
  130. };
  131. /** Empty constructor of TableView */
  132. static TableView* create();
  133. /**
  134. * An initialized table view object
  135. *
  136. * @param dataSource data source
  137. * @param size view size
  138. * @return table view
  139. * @code
  140. * when this function bound to js or lua,the input params are changed
  141. * in js:var create(var jsObject,var size)
  142. * in lua:local create(var size)
  143. * in lua:
  144. * @endcode
  145. */
  146. static TableView* create(TableViewDataSource* dataSource, Size size);
  147. /**
  148. * An initialized table view object
  149. *
  150. * @param dataSource data source;
  151. * @param size view size
  152. * @param container parent object for cells
  153. * @return table view
  154. * @code
  155. * when this function bound to js or lua,the input params are changed
  156. * in js:var create(var jsObject,var size,var container)
  157. * in lua:local create(var size, var container)
  158. * in lua:
  159. * @endcode
  160. */
  161. static TableView* create(TableViewDataSource* dataSource, Size size, Node *container);
  162. /**
  163. * @js ctor
  164. * @lua new
  165. */
  166. TableView();
  167. /**
  168. * @js NA
  169. * @lua NA
  170. */
  171. virtual ~TableView();
  172. bool initWithViewSize(Size size, Node* container = NULL);
  173. /**
  174. * data source
  175. * @js NA
  176. * @lua NA
  177. */
  178. TableViewDataSource* getDataSource() { return _dataSource; }
  179. /**
  180. * @code
  181. * when this function bound to js or lua,the input params are changed
  182. * in js:var setDataSource(var jsSource)
  183. * in lua:local setDataSource()
  184. * @endcode
  185. */
  186. void setDataSource(TableViewDataSource* source) { _dataSource = source; }
  187. /**
  188. * delegate
  189. * @js NA
  190. * @lua NA
  191. */
  192. TableViewDelegate* getDelegate() { return _tableViewDelegate; }
  193. /**
  194. * @code
  195. * when this function bound to js or lua,the input params are changed
  196. * in js:var setDelegate(var jsDelegate)
  197. * in lua:local setDelegate()
  198. * @endcode
  199. */
  200. void setDelegate(TableViewDelegate* pDelegate) { _tableViewDelegate = pDelegate; }
  201. /**
  202. * determines how cell is ordered and filled in the view.
  203. */
  204. void setVerticalFillOrder(VerticalFillOrder order);
  205. VerticalFillOrder getVerticalFillOrder();
  206. /**
  207. * Updates the content of the cell at a given index.
  208. *
  209. * @param idx index to find a cell
  210. */
  211. void updateCellAtIndex(ssize_t idx);
  212. /**
  213. * Inserts a new cell at a given index
  214. *
  215. * @param idx location to insert
  216. */
  217. void insertCellAtIndex(ssize_t idx);
  218. /**
  219. * Removes a cell at a given index
  220. *
  221. * @param idx index to find a cell
  222. */
  223. void removeCellAtIndex(ssize_t idx);
  224. /**
  225. * reloads data from data source. the view will be refreshed.
  226. */
  227. void reloadData();
  228. /**
  229. * Dequeues a free cell if available. nil if not.
  230. *
  231. * @return free cell
  232. */
  233. TableViewCell *dequeueCell();
  234. /**
  235. * Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query.
  236. *
  237. * @param idx index
  238. * @return a cell at a given index
  239. */
  240. TableViewCell *cellAtIndex(ssize_t idx);
  241. // Overrides
  242. virtual void scrollViewDidScroll(ScrollView* view) override;
  243. virtual void scrollViewDidZoom(ScrollView* view) override {}
  244. virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override;
  245. virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override;
  246. virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override;
  247. virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override;
  248. protected:
  249. long __indexFromOffset(Vec2 offset);
  250. long _indexFromOffset(Vec2 offset);
  251. Vec2 __offsetFromIndex(ssize_t index);
  252. Vec2 _offsetFromIndex(ssize_t index);
  253. void _moveCellOutOfSight(TableViewCell *cell);
  254. void _setIndexForCell(ssize_t index, TableViewCell *cell);
  255. void _addCellIfNecessary(TableViewCell * cell);
  256. void _updateCellPositions();
  257. TableViewCell *_touchedCell;
  258. /**
  259. * vertical direction of cell filling
  260. */
  261. VerticalFillOrder _vordering;
  262. /**
  263. * index set to query the indexes of the cells used.
  264. */
  265. std::set<ssize_t>* _indices;
  266. /**
  267. * vector with all cell positions
  268. */
  269. std::vector<float> _vCellsPositions;
  270. //NSMutableIndexSet *indices_;
  271. /**
  272. * cells that are currently in the table
  273. */
  274. Vector<TableViewCell*> _cellsUsed;
  275. /**
  276. * free list of cells
  277. */
  278. Vector<TableViewCell*> _cellsFreed;
  279. /**
  280. * weak link to the data source object
  281. */
  282. TableViewDataSource* _dataSource;
  283. /**
  284. * weak link to the delegate object
  285. */
  286. TableViewDelegate* _tableViewDelegate;
  287. Direction _oldDirection;
  288. bool _isUsedCellsDirty;
  289. public:
  290. void _updateContentSize();
  291. };
  292. NS_CC_EXT_END
  293. // end of ui group
  294. /// @}
  295. #endif /* __CCTABLEVIEW_H__ */