PurchaseBannerView.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // PurchaseBannerView.hpp
  3. // MyGameCpp
  4. //
  5. // Created by zhang yuntao on 2017/10/10.
  6. //
  7. //
  8. #ifndef PurchaseBannerView_hpp
  9. #define PurchaseBannerView_hpp
  10. #include "cocos2d.h"
  11. #include "extensions/cocos-ext.h"
  12. #include "PurchaseBannerCell.hpp"
  13. /*
  14. PageView类:
  15. 主要用途: 为了模仿iPhone的主屏幕page滑动页控件,使用场景:一些帮助页面使用。有的格子关卡页面等等。
  16. 使用说明:
  17. 1. 使用 PageView* create(cocos2d::Size& viewSize, sDotCfg & dotCfg, bool cachePages)来创建.
  18. 一般在ccb里设置好node的大小,然后得到大小传过来。cachePages:需不需要来缓存各个page,出于性能考虑,不缓存的话,通常只有三个页面存在
  19. 于内存中。来回迭代创建而已。
  20. 代码创建出来后一般设置 原点和锚点都为0, 再放在上面ccb设置的node上面。
  21. 然后设置delegate为自己类,且实现代理方法。
  22. //下方小圆圈这个功能是可选择的,如果不需要,上面dotCfg随便创建一个对象传进去就成。
  23. _initDotInd()
  24. //初始完毕后,使用下面函数刷新数据.
  25. reloadData(int pageCount, int defaultIndex = 0);
  26. 2. sDotCfg为iPhone主屏下方小圆圈配置。定义如下:
  27. struct sDotCfg {
  28. int sepWidth = 0; //每个小圆圈之间的间距
  29. int yStart = 0; //默认是在viewSize的底部.也可以设置偏移.
  30. string highSp = ""; //当前页高亮与普通对应的图片.
  31. string normalSp = "";
  32. };
  33. 3. 实现三个代理方法:
  34. //创建cell页面。
  35. virtual PageViewCell* pageCellAtIndex(PageView * pageView, int index) = 0;
  36. //点击到pagecell时的通知。
  37. virtual void pageCellTouched (Touch * touch, PageViewCell * page) = 0;
  38. //当前页面发生改变时通知。manualSet来区分是不是调用setCurrentPageIndex()手动使当前页面刷新。
  39. virtual void onPageChanged(int currentPageIndex, bool manualSet) = 0;
  40. 4. 自定义的 pageCell 必须承继于 PageViewCell,只是承继而已,别的不需要做什么。
  41. */
  42. class PurchaseBannerView;
  43. class PurchaseBannerViewDelegate {
  44. public:
  45. virtual PurchaseBannerCell* pageCellAtIndex(PurchaseBannerView* pageView, int index) = 0;
  46. virtual void pageCellTouched(Touch* touch, PurchaseBannerCell* page) = 0;
  47. virtual void onPageChanged(int currentPageIndex, bool manualSet) = 0;
  48. virtual void onPageTouchBegan(Touch* touch) = 0;
  49. };
  50. class PurchaseBannerView : public Layer, public cocos2d::extension::ScrollViewDelegate {
  51. public:
  52. struct sDotCfg {
  53. int sepWidth = 0;
  54. int yStart = 0;
  55. string highSp = "";
  56. string normalSp = "";
  57. };
  58. static PurchaseBannerView* create(Size& viewSize, sDotCfg& dotCfg, bool cachePages,bool isHor = true);
  59. void init(Size& viewSize, sDotCfg& dotCfg, bool cachePages, bool isHor);
  60. virtual void onEnter() override;
  61. virtual void onExit() override;
  62. void setDelegate(PurchaseBannerViewDelegate* delegate);
  63. void addBanner(PurchaseBannerCell* tmpNode);
  64. void reloadData(int pageCount, int defaultIndex = 0);
  65. void setCurrentPageIndex(int index);
  66. PurchaseBannerCell* getCurrentPage();
  67. int getCurrentPageIdx();
  68. void moveToPrePage();
  69. void moveToNextPage();
  70. void moveReferToPageIndex(int delta);
  71. void moveToFirstPage();
  72. int getPageCount();
  73. virtual void scrollViewDidScroll(cocos2d::extension::ScrollView* view) override;
  74. virtual bool onTouchBegan(Touch* touch, Event* unused_event) override;
  75. virtual void onTouchEnded(Touch* touch, Event* unused_event) override;
  76. private:
  77. PurchaseBannerViewDelegate* _pageViewDelegate = nullptr;
  78. int _currentPageIndex = 0;
  79. int _pageCount = 0;
  80. cocos2d::extension::ScrollView* _scrollView = nullptr;
  81. Size _viewSize = Size::ZERO;
  82. Vec2 _touchBeganPoint = Vec2::ZERO;
  83. int _touchedCount = 0;
  84. sDotCfg _dotCfg;
  85. bool _cachePages = false;
  86. bool _scrollViewMoved = false;
  87. bool _isHori = true;
  88. //dot
  89. bool _initedDot = false;
  90. vector<Sprite*> _dotSpArr = {};
  91. void _resizeView();
  92. void _generatePage();
  93. void _initDotInd();
  94. void _initDotAdded();
  95. void _updateIndSp();
  96. void _removeExpirePage();
  97. void _beginNewMoveToAnimation(float dt);
  98. void _notifyPageChanged(bool manualSet);
  99. void _autoSetBounceable();
  100. bool isTouchedInNode(Node* node, Touch* touch, bool isAnotherSize = false, Vec2 anotherSize = Vec2::ZERO);
  101. };
  102. #endif /* CPageView_hpp */