CCTableViewSmooth.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 __CCTABLEVIEWSMOOTH_H__
  22. #define __CCTABLEVIEWSMOOTH_H__
  23. #include "CCScrollViewSmooth.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 TableViewSmooth;
  34. /**
  35. * Sole purpose of this delegate is to single touch event in this version.
  36. */
  37. class CC_EX_DLL TableViewDelegateSmooth : public ScrollViewDelegateSmooth
  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(TableViewSmooth* 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(TableViewSmooth* 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(TableViewSmooth* 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(TableViewSmooth* table, TableViewCell* cell){};
  78. };
  79. /**
  80. * Data source that governs table backend data.
  81. */
  82. class CC_EX_DLL TableViewDataSourceSmooth
  83. {
  84. public:
  85. /**
  86. * @js NA
  87. * @lua NA
  88. */
  89. virtual ~TableViewDataSourceSmooth() {}
  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(TableViewSmooth *table, ssize_t idx) {
  97. return cellSizeForTable(table);
  98. };
  99. /**
  100. * cell height for a given table.
  101. *
  102. * @param table table to hold the instances of Class
  103. * @return cell size
  104. */
  105. virtual Size cellSizeForTable(TableViewSmooth *table) {
  106. return Size::ZERO;
  107. };
  108. /**
  109. * a cell instance at a given index
  110. *
  111. * @param idx index to search for a cell
  112. * @return cell found at idx
  113. */
  114. virtual TableViewCell* tableCellAtIndex(TableViewSmooth *table, ssize_t idx) = 0;
  115. /**
  116. * Returns number of cells in a given table view.
  117. *
  118. * @return number of cells
  119. */
  120. virtual ssize_t numberOfCellsInTableView(TableViewSmooth *table) = 0;
  121. };
  122. /**
  123. * UITableView support for cocos2d-x.
  124. *
  125. * This is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
  126. */
  127. class CC_EX_DLL TableViewSmooth : public ScrollViewSmooth, public ScrollViewDelegateSmooth
  128. {
  129. public:
  130. enum class VerticalFillOrder
  131. {
  132. TOP_DOWN,
  133. BOTTOM_UP
  134. };
  135. /** Empty constructor of TableView */
  136. static TableViewSmooth* create();
  137. /**
  138. * An initialized table view object
  139. *
  140. * @param dataSource data source
  141. * @param size view size
  142. * @return table view
  143. * @code
  144. * when this function bound to js or lua,the input params are changed
  145. * in js:var create(var jsObject,var size)
  146. * in lua:local create(var size)
  147. * in lua:
  148. * @endcode
  149. */
  150. static TableViewSmooth* create(TableViewDataSourceSmooth* dataSource, Size size);
  151. /**
  152. * An initialized table view object
  153. *
  154. * @param dataSource data source;
  155. * @param size view size
  156. * @param container parent object for cells
  157. * @return table view
  158. * @code
  159. * when this function bound to js or lua,the input params are changed
  160. * in js:var create(var jsObject,var size,var container)
  161. * in lua:local create(var size, var container)
  162. * in lua:
  163. * @endcode
  164. */
  165. static TableViewSmooth* create(TableViewDataSourceSmooth* dataSource, Size size, Node *container);
  166. /**
  167. * @js ctor
  168. * @lua new
  169. */
  170. TableViewSmooth();
  171. /**
  172. * @js NA
  173. * @lua NA
  174. */
  175. virtual ~TableViewSmooth();
  176. bool initWithViewSize(Size size, Node* container = NULL);
  177. /**
  178. * data source
  179. * @js NA
  180. * @lua NA
  181. */
  182. TableViewDataSourceSmooth* getDataSource() { return _dataSource; }
  183. /**
  184. * when this function bound to js or lua,the input params are changed
  185. * in js:var setDataSource(var jsSource)
  186. * in lua:local setDataSource()
  187. * @endcode
  188. */
  189. void setDataSource(TableViewDataSourceSmooth* source) { _dataSource = source; }
  190. /**
  191. * delegate
  192. * @js NA
  193. * @lua NA
  194. */
  195. TableViewDelegateSmooth* getDelegate() { return _tableViewDelegate; }
  196. /**
  197. * @code
  198. * when this function bound to js or lua,the input params are changed
  199. * in js:var setDelegate(var jsDelegate)
  200. * in lua:local setDelegate()
  201. * @endcode
  202. */
  203. void setDelegate(TableViewDelegateSmooth* pDelegate) { _tableViewDelegate = pDelegate; }
  204. /**
  205. * determines how cell is ordered and filled in the view.
  206. */
  207. void setVerticalFillOrder(VerticalFillOrder order);
  208. VerticalFillOrder getVerticalFillOrder();
  209. /**
  210. * Updates the content of the cell at a given index.
  211. *
  212. * @param idx index to find a cell
  213. */
  214. void updateCellAtIndex(ssize_t idx);
  215. /**
  216. * Inserts a new cell at a given index
  217. *
  218. * @param idx location to insert
  219. */
  220. void insertCellAtIndex(ssize_t idx);
  221. /**
  222. * Removes a cell at a given index
  223. *
  224. * @param idx index to find a cell
  225. */
  226. void removeCellAtIndex(ssize_t idx);
  227. /**
  228. * reloads data from data source. the view will be refreshed.
  229. */
  230. void reloadData();
  231. /**
  232. * Dequeues a free cell if available. nil if not.
  233. *
  234. * @return free cell
  235. */
  236. TableViewCell *dequeueCell();
  237. /**
  238. * Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query.
  239. *
  240. * @param idx index
  241. * @return a cell at a given index
  242. */
  243. TableViewCell *cellAtIndex(ssize_t idx);
  244. // Overrides
  245. virtual void scrollViewDidScroll(ScrollViewSmooth* view) override;
  246. virtual void scrollViewDidZoom(ScrollViewSmooth* view) override {}
  247. virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override;
  248. virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override;
  249. virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override;
  250. virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override;
  251. protected:
  252. long __indexFromOffset(Vec2 offset);
  253. long _indexFromOffset(Vec2 offset);
  254. Vec2 __offsetFromIndex(ssize_t index);
  255. Vec2 _offsetFromIndex(ssize_t index);
  256. void _moveCellOutOfSight(TableViewCell *cell);
  257. void _setIndexForCell(ssize_t index, TableViewCell *cell);
  258. void _addCellIfNecessary(TableViewCell * cell);
  259. void _updateCellPositions();
  260. TableViewCell *_touchedCell;
  261. /**
  262. * vertical direction of cell filling
  263. */
  264. VerticalFillOrder _vordering;
  265. /**
  266. * index set to query the indexes of the cells used.
  267. */
  268. std::set<ssize_t>* _indices;
  269. /**
  270. * vector with all cell positions
  271. */
  272. std::vector<float> _vCellsPositions;
  273. //NSMutableIndexSet *indices_;
  274. /**
  275. * cells that are currently in the table
  276. */
  277. Vector<TableViewCell*> _cellsUsed;
  278. /**
  279. * free list of cells
  280. */
  281. Vector<TableViewCell*> _cellsFreed;
  282. /**
  283. * weak link to the data source object
  284. */
  285. TableViewDataSourceSmooth* _dataSource;
  286. /**
  287. * weak link to the delegate object
  288. */
  289. TableViewDelegateSmooth* _tableViewDelegate;
  290. Direction _oldDirection;
  291. bool _isUsedCellsDirty;
  292. public:
  293. void _updateContentSize();
  294. };
  295. NS_CC_EXT_END
  296. // end of ui group
  297. /// @}
  298. #endif /* __CCTABLEVIEWSMOOTH_H__ */