#ifndef RED_TABLE_VIEW_hpp #define RED_TABLE_VIEW_hpp #include #include #include #include #include #include "cocos2d.h" #include #include "RUDefine.h" USING_NS_CC_EXT ; USING_NS_CC ; using namespace std ; NS_RU_BEGIN class RUTableDataModel : public TableViewDataSourceSmooth ,public TableViewDelegateSmooth { protected: unordered_map _cellMap ; bool _cascadeOpacity = false ; protected: virtual void scrollViewDidScroll(ScrollViewSmooth* view) override; public: ///重写 virtual void tableCellTouched(TableViewSmooth* table, TableViewCell* cell) override; virtual void tableCellTouchedHandle(TableViewSmooth* table, TableViewCell* cell,Touch *pTouch); virtual ssize_t numberOfCellsInTableView(TableViewSmooth* table) override; cocos2d::Size cellSizeForTable(TableViewSmooth* table) override ; cocos2d::Size tableCellSizeForIndex(TableViewSmooth* table, ssize_t idx) override ; virtual TableViewCell* tableCellAtIndex(TableViewSmooth* table, ssize_t idx) override; ///Yjz_add virtual ~RUTableDataModel(); ///ScrollView function _scrollViewDidScroll = NULL ; ///TableDataSource function _numberOfCellsInTableView = NULL ; function _cellSizeForTable = NULL ; function _tableCellSizeForIndex = NULL ; function _tableCellAtIndex = NULL ; ///TableViewDelegate function _tableCellTouched = NULL ; bool cascadeOpacity() ; void setCascadeOpacity(bool cascadeOpacity) ; Node* getCell(int index); void insertCell(int index,Node* cell); void clear() ; public: enum class LayOut { HORIZONTAL, VERTICAL, FREE } ; //protected: public: struct CellInfo { int frame_count ; std::string frameType ; cocos2d::Size cell_size ; std::string cellCCB ; std::unordered_map userData ; }; struct TableConfig { bool limitSize = false ; int minCellCount = 1 ; int level_count = 0 ; LayOut layOut = LayOut::HORIZONTAL ; } ; std::vector,int>> table_regular ; TableConfig _config ; bool _dirty = false; void checkLegality() ; bool checkFrameExist(int frameIndex) ; bool checkCellExist(int cellIndex) ; std::vector cell_array ; std::vector& CA = cell_array ; public: void expandConfig() ; void OffsetByCellIndex(float& out_offsetMin,float& out_offsetMax,int in_cellIndex) ; void OffsetByFrameGlobalIndex(float& out_offsetMin,float& out_offsetMax,int in_frameGlobalIndex); void OffsetByFrameLocalIndex(float& out_offsetMin,float& out_offsetMax,int in_frameLocalIndex,int cellIndex); int CellIndex(int in_frameGlobalIndex); int FrameGlobalIndex(int in_frameLocalIndex,int cellIndex); int CellCount(int in_frameGlobalIndex) ; cocos2d::Size TableSize() ; int FrameCount() ; int CellCount() ; /// 保持cell填充满,还需几个图框 /// @param in_showJigsaws 展示的相框数 /// @return out_add2FullCell 还需填充的相框数 int FrameNeedCountForFullCell(int in_visibleFrames); /// 通过全局相框的索引值计算相框所在的cell索引 /// @param in_jigsawIndex 相框的全局索引 /// @return out_cellIndex cell的索引 void getCellIndexByFrameGlobalIndex(int in_frameGlobalIndex,int& out_cellIndex) ; /// 获取指定cell的首相框的全局索引 /// @param idx cell的索引 /// @return out_startIdx 该cell首个相框的全局索引 void getGlobalIndexForFirstFrameInCell(int idx,int& out_startIdx) ; /// 获取cell的宽 /// @param cellGlobalIndex cell的索引 /// @return out_cellWidth cell的宽 void getCellWidth(int cellGlobalIndex,float& out_cellWidth) ; /// 获取cell在X轴偏移量,即右边界的偏移量 ; 其左边界的偏移量为前一个celll的右偏移量 /// @param cellGlobalIndex cell的索引 /// @param out_offsetX 右边界偏移量 void getOffsetByCellGlobalIndex(int cellGlobalIndex,float& out_offsetX) ; /// 获取cell在X轴偏移量,即右边界的偏移量 ; 其左边界的偏移量为前一个celll的右偏移量 /// @param cellGlobalIndex cell的索引 /// @param out_offsetX 右边界偏移量 void get_cell_and_localFrameIndex_by_globalFrameIndex(int& cellIndex,int& localFrameIndex,int globalFrameIndex) ; std::string getCellCCB(int cellGlobalIndex) ; void get_type_and_count_by_cellIdx(int cellGlobalIndex,int& out_frameCount,std::string& out_frameType) ; void singleCycle(int i_max,std::function callback); void mutiCycle(int i_max,std::function callback) ; }; class RUTableView :public TableViewSmooth { #pragma mark - 吸附部分变量 protected: bool _absorb = false; bool _absordBounced = false ; float _AbsorbDistanceFactor = 0.122f ; float _AbsorbDuration = 0.04f; float _AbsorbBounceDuration = 0.3f ; int _fixedIndex4Absorb = -1; bool _bScrollable = true; bool _recycle = false ; int _recycleScrollIndex = INT_MIN ; float _RecycleScrollDuration = 0.3f ; /// bool _cascadeOpacity = false ; /// 点击时的回调 std::function _cbWhileTouchedBegin = nullptr; std::function _cbWhileTouchedMoved = nullptr; std::function _cbWhileTouchedEnd = nullptr; ///TableView原生代码 public: ///yjz_fix:reloadData或设置填充顺序时,子cell将被cleanUp; void reloadData(bool cleanUp); void setVerticalFillOrder(VerticalFillOrder fillOrder,bool cleanUp) ; ///yjz_fix:设置偏移后,调用ScrollViewDidScroll刷新控件 void safeSetContentOffset(Vec2 offset); void safeScrollViewDidScroll(); // 设置开始点击时的回调 void setCBWhileTouchBegin(std::function cb) { _cbWhileTouchedBegin = cb; } void setCBWhileTouchMoved(std::function cb) { _cbWhileTouchedMoved = cb; } void setCBWhileTouchEnd(std::function cb) { _cbWhileTouchedEnd = cb; } protected: virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override; virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override; virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override; virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override; public: //sdk有bug,将崩溃 // static view* create(); // static view* create(Size size); // static RUTableView* create(TableViewDataSourceSmooth* dataSource, Size size); static RUTableView* create(TableViewDataSourceSmooth* dataSource, Size size, Node *container); ~RUTableView() ; #pragma mark -参数 float getAbsorbDuration(){return _AbsorbDuration;}; void setAbsorbDistanceFactor(float f) {_AbsorbDistanceFactor = f; }; void setAbsorbDuration(float f) {_AbsorbDuration = f; }; void setAbsorbBounceDuration(float f) {_AbsorbBounceDuration = f; }; void setRecycleScrollDuration(float f) {_RecycleScrollDuration = f; }; #pragma mark - 吸附 void setAbsorb(bool absorb, bool bounced = true, bool bFixedIndex = -1); void absorbCell(bool byAni); bool isAbsorbAniRun(); #pragma mark - 循环 void setRecycle(bool recycle); bool isRecycleScrollAniRun() ; #pragma mark - 自制 void updateCellAtIndex(int cellIndex) ; float scrollToNodeInView(int cellIndex,Node* node,Vec2 percentInView,float duration,bool strict=false) ; float scrollToNodeInView(int cellIndex,Vec2 posInCell,Vec2 percentInView,float duration); bool scrollToPosInView(int cellIndex,Vec2 percentInCell,Vec2 percentInView,float duration,bool bounced) ; void setContentOffsetInDurationByBounce(Vec2 offset, float dt,float bounceDuration); #pragma mark - 其他 int getVisibleCellIndex() ; Size tableSize() ; Size maxCellSize() ; RUTableView* clone() ; int recycleIndex() ; Vec2 localDistanceToPercentViewSize(Vec2 worldPos,float percentX,float percentY); void setScrollable(bool bEnabled = true) {_bScrollable = bEnabled;}; //修复scrollVeiw机制的API bool scrollToCellIndex(int index,float duration,bool bounced) ; void recycleScrollTo(int index) ; void recycleScrollTo(int index,float perCellDuration,bool bounced); void recycleScrollToInDuration(int index,float duration,bool bounced); Vec2 scrollDistanceFbs() ; void stopDeaccelerateScrolling(); #pragma mark - 访问受保护TableView/ScorllView变量 Action* animatedScrollAction(); void scrollDistance(Vec2 customDistance) ; Vec2 offsetFromIndex(ssize_t cellIndex) ; bool isDraging(); bool isScrolling() ; Vector getUsedCell(); Vector getFreeCell(); //复刻 void update(float dt) override; }; NS_RU_END #endif /* yjz_table_hpp */