CCTableView.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. public:
  79. bool isNeedReleaseByTableView() { return _isNeedReleaseByTableView; }
  80. void setNeedReleaseByTableView(bool b) { _isNeedReleaseByTableView = b; }
  81. private:
  82. // 默认由开发者管理,设为true则交由TableView负责delete
  83. // 首次使用: TableView从redream文件中加载时需要一个默认的Datasource,此时再修改为共享指针成本较高
  84. bool _isNeedReleaseByTableView = false;
  85. };
  86. /**
  87. * Data source that governs table backend data.
  88. */
  89. class CC_EX_DLL TableViewDataSource
  90. {
  91. public:
  92. /**
  93. * @js NA
  94. * @lua NA
  95. */
  96. virtual ~TableViewDataSource() {}
  97. /**
  98. * cell size for a given index
  99. *
  100. * @param idx the index of a cell to get a size
  101. * @return size of a cell at given index
  102. */
  103. virtual Size tableCellSizeForIndex(TableView* table, ssize_t idx);
  104. /**
  105. * cell height for a given table.
  106. *
  107. * @param table table to hold the instances of Class
  108. * @return cell size
  109. */
  110. virtual Size cellSizeForTable(TableView* table);
  111. /**
  112. * a cell instance at a given index
  113. *
  114. * @param idx index to search for a cell
  115. * @return cell found at idx
  116. */
  117. virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx) = 0;
  118. /**
  119. * Returns number of cells in a given table view.
  120. *
  121. * @return number of cells
  122. */
  123. virtual ssize_t numberOfCellsInTableView(TableView *table) = 0;
  124. public:
  125. bool isNeedReleaseByTableView() { return _isNeedReleaseByTableView; }
  126. void setNeedReleaseByTableView(bool b) { _isNeedReleaseByTableView = b; }
  127. private:
  128. // DataSource默认由使用者自己delete, 设置为true时,TableView会负责delete
  129. // 添加原因: 从Redream中读取一个TableView时,需要设置一个默认的DataSource,而这个DataSource无人管理其生命周期
  130. bool _isNeedReleaseByTableView = false;
  131. };
  132. /**
  133. * UITableView support for cocos2d-x.
  134. *
  135. * This is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
  136. */
  137. class CC_EX_DLL TableView : public ScrollView, public ScrollViewDelegate
  138. {
  139. public:
  140. enum class VerticalFillOrder
  141. {
  142. TOP_DOWN,
  143. BOTTOM_UP
  144. };
  145. /** Empty constructor of TableView */
  146. static TableView* create();
  147. /**
  148. * An initialized table view object
  149. *
  150. * @param dataSource data source
  151. * @param size view size
  152. * @return table view
  153. * @code
  154. * when this function bound to js or lua,the input params are changed
  155. * in js:var create(var jsObject,var size)
  156. * in lua:local create(var size)
  157. * in lua:
  158. * @endcode
  159. */
  160. static TableView* create(TableViewDataSource* dataSource, Size size);
  161. /**
  162. * An initialized table view object
  163. *
  164. * @param dataSource data source;
  165. * @param size view size
  166. * @param container parent object for cells
  167. * @return table view
  168. * @code
  169. * when this function bound to js or lua,the input params are changed
  170. * in js:var create(var jsObject,var size,var container)
  171. * in lua:local create(var size, var container)
  172. * in lua:
  173. * @endcode
  174. */
  175. static TableView* create(TableViewDataSource* dataSource, Size size, Node *container);
  176. /**
  177. * @js ctor
  178. * @lua new
  179. */
  180. TableView();
  181. /**
  182. * @js NA
  183. * @lua NA
  184. */
  185. virtual ~TableView();
  186. bool initWithViewSize(Size size, Node* container = NULL);
  187. /**
  188. * data source
  189. * @js NA
  190. * @lua NA
  191. */
  192. TableViewDataSource* getDataSource() { return _dataSource; }
  193. /**
  194. * @code
  195. * when this function bound to js or lua,the input params are changed
  196. * in js:var setDataSource(var jsSource)
  197. * in lua:local setDataSource()
  198. * @endcode
  199. */
  200. void setDataSource(TableViewDataSource* source);
  201. /**
  202. * delegate
  203. * @js NA
  204. * @lua NA
  205. */
  206. TableViewDelegate* getDelegate() { return _tableViewDelegate; }
  207. /**
  208. * @code
  209. * when this function bound to js or lua,the input params are changed
  210. * in js:var setDelegate(var jsDelegate)
  211. * in lua:local setDelegate()
  212. * @endcode
  213. */
  214. void setDelegate(TableViewDelegate* pDelegate) { _tableViewDelegate = pDelegate; }
  215. /**
  216. * determines how cell is ordered and filled in the view.
  217. */
  218. void setVerticalFillOrder(VerticalFillOrder order);
  219. VerticalFillOrder getVerticalFillOrder();
  220. /**
  221. * Updates the content of the cell at a given index.
  222. *
  223. * @param idx index to find a cell
  224. */
  225. void updateCellAtIndex(ssize_t idx);
  226. /**
  227. * Inserts a new cell at a given index
  228. *
  229. * @param idx location to insert
  230. */
  231. void insertCellAtIndex(ssize_t idx);
  232. /**
  233. * Removes a cell at a given index
  234. *
  235. * @param idx index to find a cell
  236. */
  237. void removeCellAtIndex(ssize_t idx);
  238. /**
  239. * reloads data from data source. the view will be refreshed.
  240. */
  241. void reloadData();
  242. /**
  243. * Dequeues a free cell if available. nil if not.
  244. *
  245. * @return free cell
  246. */
  247. TableViewCell *dequeueCell();
  248. /**
  249. * Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query.
  250. *
  251. * @param idx index
  252. * @return a cell at a given index
  253. */
  254. TableViewCell *cellAtIndex(ssize_t idx);
  255. // Overrides
  256. virtual void scrollViewDidScroll(ScrollView* view) override;
  257. virtual void scrollViewDidZoom(ScrollView* view) override {}
  258. virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override;
  259. virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override;
  260. virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override;
  261. virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override;
  262. protected:
  263. long __indexFromOffset(Vec2 offset);
  264. long _indexFromOffset(Vec2 offset);
  265. Vec2 __offsetFromIndex(ssize_t index);
  266. Vec2 _offsetFromIndex(ssize_t index);
  267. void _moveCellOutOfSight(TableViewCell *cell);
  268. void _setIndexForCell(ssize_t index, TableViewCell *cell);
  269. void _addCellIfNecessary(TableViewCell * cell);
  270. void _updateCellPositions();
  271. TableViewCell *_touchedCell;
  272. /**
  273. * vertical direction of cell filling
  274. */
  275. VerticalFillOrder _vordering;
  276. /**
  277. * index set to query the indexes of the cells used.
  278. */
  279. std::set<ssize_t>* _indices;
  280. /**
  281. * vector with all cell positions
  282. */
  283. std::vector<float> _vCellsPositions;
  284. //NSMutableIndexSet *indices_;
  285. /**
  286. * cells that are currently in the table
  287. */
  288. Vector<TableViewCell*> _cellsUsed;
  289. /**
  290. * free list of cells
  291. */
  292. Vector<TableViewCell*> _cellsFreed;
  293. /**
  294. * weak link to the data source object
  295. */
  296. TableViewDataSource* _dataSource;
  297. /**
  298. * weak link to the delegate object
  299. */
  300. TableViewDelegate* _tableViewDelegate;
  301. Direction _oldDirection;
  302. bool _isUsedCellsDirty;
  303. public:
  304. void _updateContentSize();
  305. };
  306. NS_CC_EXT_END
  307. // end of ui group
  308. /// @}
  309. #endif /* __CCTABLEVIEW_H__ */