123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- #ifndef RED_TABLE_VIEW_hpp
- #define RED_TABLE_VIEW_hpp
- #include <stdio.h>
- #include <vector>
- #include <tuple>
- #include <math/CCGeometry.h>
- #include <math/Vec2.h>
- #include "cocos2d.h"
- #include <extensions/GUI/CCScrollView/CCTableViewSmooth.h>
- #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<int, Node*> _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<void(ScrollViewSmooth*)> _scrollViewDidScroll = NULL ;
-
- ///TableDataSource
- function<ssize_t(TableViewSmooth* table)> _numberOfCellsInTableView = NULL ;
- function<Size(TableViewSmooth* table)> _cellSizeForTable = NULL ;
- function<Size(TableViewSmooth* table, ssize_t idx)> _tableCellSizeForIndex = NULL ;
- function<TableViewCell*(TableViewSmooth* table, ssize_t idx)> _tableCellAtIndex = NULL ;
-
- ///TableViewDelegate
- function<void(TableViewSmooth* table, TableViewCell* cell)> _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<std::string, std::string> userData ;
- };
-
- struct TableConfig
- {
- bool limitSize = false ;
- int minCellCount = 1 ;
- int level_count = 0 ;
- LayOut layOut = LayOut::HORIZONTAL ;
- } ;
-
- std::vector<std::tuple<std::vector<CellInfo>,int>> table_regular ;
- TableConfig _config ;
- bool _dirty = false;
-
- void checkLegality() ;
- bool checkFrameExist(int frameIndex) ;
- bool checkCellExist(int cellIndex) ;
-
- std::vector<CellInfo> cell_array ;
- std::vector<CellInfo>& 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<bool(int i)> callback);
- void mutiCycle(int i_max,std::function<bool(int i,int cumulate_i)> 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<void()> _cbWhileTouchedBegin = nullptr;
- std::function<void(bool)> _cbWhileTouchedMoved = nullptr;
- std::function<void(bool)> _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<void()> cb) { _cbWhileTouchedBegin = cb; }
- void setCBWhileTouchMoved(std::function<void(bool)> cb) { _cbWhileTouchedMoved = cb; }
- void setCBWhileTouchEnd(std::function<void(bool)> 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<TableViewCell*> getUsedCell();
- Vector<TableViewCell*> getFreeCell();
-
- //复刻
- void update(float dt) override;
- };
- NS_RU_END
- #endif /* yjz_table_hpp */
|