RUTableView.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #ifndef RED_TABLE_VIEW_hpp
  2. #define RED_TABLE_VIEW_hpp
  3. #include <stdio.h>
  4. #include <vector>
  5. #include <tuple>
  6. #include <math/CCGeometry.h>
  7. #include <math/Vec2.h>
  8. #include "cocos2d.h"
  9. #include <extensions/GUI/CCScrollView/CCTableViewSmooth.h>
  10. #include "RUDefine.h"
  11. USING_NS_CC_EXT ;
  12. USING_NS_CC ;
  13. using namespace std ;
  14. NS_RU_BEGIN
  15. class RUTableDataModel
  16. : public TableViewDataSourceSmooth
  17. ,public TableViewDelegateSmooth
  18. {
  19. protected:
  20. unordered_map<int, Node*> _cellMap ;
  21. bool _cascadeOpacity = false ;
  22. protected:
  23. virtual void scrollViewDidScroll(ScrollViewSmooth* view) override;
  24. public:
  25. ///重写
  26. virtual void tableCellTouched(TableViewSmooth* table, TableViewCell* cell) override;
  27. virtual void tableCellTouchedHandle(TableViewSmooth* table, TableViewCell* cell,Touch *pTouch);
  28. virtual ssize_t numberOfCellsInTableView(TableViewSmooth* table) override;
  29. cocos2d::Size cellSizeForTable(TableViewSmooth* table) override ;
  30. cocos2d::Size tableCellSizeForIndex(TableViewSmooth* table, ssize_t idx) override ;
  31. virtual TableViewCell* tableCellAtIndex(TableViewSmooth* table, ssize_t idx) override;
  32. ///Yjz_add
  33. virtual ~RUTableDataModel();
  34. ///ScrollView
  35. function<void(ScrollViewSmooth*)> _scrollViewDidScroll = NULL ;
  36. ///TableDataSource
  37. function<ssize_t(TableViewSmooth* table)> _numberOfCellsInTableView = NULL ;
  38. function<Size(TableViewSmooth* table)> _cellSizeForTable = NULL ;
  39. function<Size(TableViewSmooth* table, ssize_t idx)> _tableCellSizeForIndex = NULL ;
  40. function<TableViewCell*(TableViewSmooth* table, ssize_t idx)> _tableCellAtIndex = NULL ;
  41. ///TableViewDelegate
  42. function<void(TableViewSmooth* table, TableViewCell* cell)> _tableCellTouched = NULL ;
  43. bool cascadeOpacity() ;
  44. void setCascadeOpacity(bool cascadeOpacity) ;
  45. Node* getCell(int index);
  46. void insertCell(int index,Node* cell);
  47. void clear() ;
  48. public:
  49. enum class LayOut
  50. {
  51. HORIZONTAL,
  52. VERTICAL,
  53. FREE
  54. } ;
  55. //protected:
  56. public:
  57. struct CellInfo
  58. {
  59. int frame_count ;
  60. std::string frameType ;
  61. cocos2d::Size cell_size ;
  62. std::string cellCCB ;
  63. std::unordered_map<std::string, std::string> userData ;
  64. };
  65. struct TableConfig
  66. {
  67. bool limitSize = false ;
  68. int minCellCount = 1 ;
  69. int level_count = 0 ;
  70. LayOut layOut = LayOut::HORIZONTAL ;
  71. } ;
  72. std::vector<std::tuple<std::vector<CellInfo>,int>> table_regular ;
  73. TableConfig _config ;
  74. bool _dirty = false;
  75. void checkLegality() ;
  76. bool checkFrameExist(int frameIndex) ;
  77. bool checkCellExist(int cellIndex) ;
  78. std::vector<CellInfo> cell_array ;
  79. std::vector<CellInfo>& CA = cell_array ;
  80. public:
  81. void expandConfig() ;
  82. void OffsetByCellIndex(float& out_offsetMin,float& out_offsetMax,int in_cellIndex) ;
  83. void OffsetByFrameGlobalIndex(float& out_offsetMin,float& out_offsetMax,int in_frameGlobalIndex);
  84. void OffsetByFrameLocalIndex(float& out_offsetMin,float& out_offsetMax,int in_frameLocalIndex,int cellIndex);
  85. int CellIndex(int in_frameGlobalIndex);
  86. int FrameGlobalIndex(int in_frameLocalIndex,int cellIndex);
  87. int CellCount(int in_frameGlobalIndex) ;
  88. cocos2d::Size TableSize() ;
  89. int FrameCount() ;
  90. int CellCount() ;
  91. /// 保持cell填充满,还需几个图框
  92. /// @param in_showJigsaws 展示的相框数
  93. /// @return out_add2FullCell 还需填充的相框数
  94. int FrameNeedCountForFullCell(int in_visibleFrames);
  95. /// 通过全局相框的索引值计算相框所在的cell索引
  96. /// @param in_jigsawIndex 相框的全局索引
  97. /// @return out_cellIndex cell的索引
  98. void getCellIndexByFrameGlobalIndex(int in_frameGlobalIndex,int& out_cellIndex) ;
  99. /// 获取指定cell的首相框的全局索引
  100. /// @param idx cell的索引
  101. /// @return out_startIdx 该cell首个相框的全局索引
  102. void getGlobalIndexForFirstFrameInCell(int idx,int& out_startIdx) ;
  103. /// 获取cell的宽
  104. /// @param cellGlobalIndex cell的索引
  105. /// @return out_cellWidth cell的宽
  106. void getCellWidth(int cellGlobalIndex,float& out_cellWidth) ;
  107. /// 获取cell在X轴偏移量,即右边界的偏移量 ; 其左边界的偏移量为前一个celll的右偏移量
  108. /// @param cellGlobalIndex cell的索引
  109. /// @param out_offsetX 右边界偏移量
  110. void getOffsetByCellGlobalIndex(int cellGlobalIndex,float& out_offsetX) ;
  111. /// 获取cell在X轴偏移量,即右边界的偏移量 ; 其左边界的偏移量为前一个celll的右偏移量
  112. /// @param cellGlobalIndex cell的索引
  113. /// @param out_offsetX 右边界偏移量
  114. void get_cell_and_localFrameIndex_by_globalFrameIndex(int& cellIndex,int& localFrameIndex,int globalFrameIndex) ;
  115. std::string getCellCCB(int cellGlobalIndex) ;
  116. void get_type_and_count_by_cellIdx(int cellGlobalIndex,int& out_frameCount,std::string& out_frameType) ;
  117. void singleCycle(int i_max,std::function<bool(int i)> callback);
  118. void mutiCycle(int i_max,std::function<bool(int i,int cumulate_i)> callback) ;
  119. };
  120. class RUTableView
  121. :public TableViewSmooth
  122. {
  123. #pragma mark - 吸附部分变量
  124. protected:
  125. bool _absorb = false;
  126. bool _absordBounced = false ;
  127. float _AbsorbDistanceFactor = 0.122f ;
  128. float _AbsorbDuration = 0.04f;
  129. float _AbsorbBounceDuration = 0.3f ;
  130. int _fixedIndex4Absorb = -1;
  131. bool _bScrollable = true;
  132. bool _recycle = false ;
  133. int _recycleScrollIndex = INT_MIN ;
  134. float _RecycleScrollDuration = 0.3f ;
  135. ///
  136. bool _cascadeOpacity = false ;
  137. /// 点击时的回调
  138. std::function<void()> _cbWhileTouchedBegin = nullptr;
  139. std::function<void(bool)> _cbWhileTouchedMoved = nullptr;
  140. std::function<void(bool)> _cbWhileTouchedEnd = nullptr;
  141. ///TableView原生代码
  142. public:
  143. ///yjz_fix:reloadData或设置填充顺序时,子cell将被cleanUp;
  144. void reloadData(bool cleanUp);
  145. void setVerticalFillOrder(VerticalFillOrder fillOrder,bool cleanUp) ;
  146. ///yjz_fix:设置偏移后,调用ScrollViewDidScroll刷新控件
  147. void safeSetContentOffset(Vec2 offset);
  148. void safeScrollViewDidScroll();
  149. // 设置开始点击时的回调
  150. void setCBWhileTouchBegin(std::function<void()> cb) { _cbWhileTouchedBegin = cb; }
  151. void setCBWhileTouchMoved(std::function<void(bool)> cb) { _cbWhileTouchedMoved = cb; }
  152. void setCBWhileTouchEnd(std::function<void(bool)> cb) { _cbWhileTouchedEnd = cb; }
  153. protected:
  154. virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override;
  155. virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override;
  156. virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override;
  157. virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override;
  158. public:
  159. //sdk有bug,将崩溃
  160. // static view* create();
  161. // static view* create(Size size);
  162. //
  163. static RUTableView* create(TableViewDataSourceSmooth* dataSource, Size size);
  164. static RUTableView* create(TableViewDataSourceSmooth* dataSource, Size size, Node *container);
  165. ~RUTableView() ;
  166. #pragma mark -参数
  167. float getAbsorbDuration(){return _AbsorbDuration;};
  168. void setAbsorbDistanceFactor(float f) {_AbsorbDistanceFactor = f; };
  169. void setAbsorbDuration(float f) {_AbsorbDuration = f; };
  170. void setAbsorbBounceDuration(float f) {_AbsorbBounceDuration = f; };
  171. void setRecycleScrollDuration(float f) {_RecycleScrollDuration = f; };
  172. #pragma mark - 吸附
  173. void setAbsorb(bool absorb, bool bounced = true, bool bFixedIndex = -1);
  174. void absorbCell(bool byAni);
  175. bool isAbsorbAniRun();
  176. #pragma mark - 循环
  177. void setRecycle(bool recycle);
  178. bool isRecycleScrollAniRun() ;
  179. #pragma mark - 自制
  180. void updateCellAtIndex(int cellIndex) ;
  181. float scrollToNodeInView(int cellIndex,Node* node,Vec2 percentInView,float duration,bool strict=false) ;
  182. float scrollToNodeInView(int cellIndex,Vec2 posInCell,Vec2 percentInView,float duration);
  183. bool scrollToPosInView(int cellIndex,Vec2 percentInCell,Vec2 percentInView,float duration,bool bounced) ;
  184. void setContentOffsetInDurationByBounce(Vec2 offset, float dt,float bounceDuration);
  185. #pragma mark - 其他
  186. int getVisibleCellIndex() ;
  187. Size tableSize() ;
  188. Size maxCellSize() ;
  189. RUTableView* clone() ;
  190. int recycleIndex() ;
  191. Vec2 localDistanceToPercentViewSize(Vec2 worldPos,float percentX,float percentY);
  192. void setScrollable(bool bEnabled = true) {_bScrollable = bEnabled;};
  193. //修复scrollVeiw机制的API
  194. bool scrollToCellIndex(int index,float duration,bool bounced) ;
  195. void recycleScrollTo(int index) ;
  196. void recycleScrollTo(int index,float perCellDuration,bool bounced);
  197. void recycleScrollToInDuration(int index,float duration,bool bounced);
  198. Vec2 scrollDistanceFbs() ;
  199. void stopDeaccelerateScrolling();
  200. #pragma mark - 访问受保护TableView/ScorllView变量
  201. Action* animatedScrollAction();
  202. void scrollDistance(Vec2 customDistance) ;
  203. Vec2 offsetFromIndex(ssize_t cellIndex) ;
  204. bool isDraging();
  205. bool isScrolling() ;
  206. Vector<TableViewCell*> getUsedCell();
  207. Vector<TableViewCell*> getFreeCell();
  208. //复刻
  209. void update(float dt) override;
  210. };
  211. NS_RU_END
  212. #endif /* yjz_table_hpp */