PurchaseBannerCtrl.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // PurchaseBannerCtrl.cpp
  3. // merge6
  4. //
  5. // Created by Black Homles on 2024/9/24.
  6. //
  7. #include "PurchaseBannerCtrl.hpp"
  8. #include "PBConfigData.hpp"
  9. PurchaseBannerCtrl* PurchaseBannerCtrl::tempPBCtrlInst = nullptr;
  10. PurchaseBannerCtrl::PurchaseBannerCtrl() {
  11. }
  12. PurchaseBannerCtrl::~PurchaseBannerCtrl() {
  13. }
  14. PurchaseBannerCtrl* PurchaseBannerCtrl::getInstance() {
  15. if (tempPBCtrlInst == nullptr || !tempPBCtrlInst) {
  16. tempPBCtrlInst = new (std::nothrow) PurchaseBannerCtrl();
  17. }
  18. return tempPBCtrlInst;
  19. }
  20. void PurchaseBannerCtrl::init(PurchaseBannerDelegate* pDelegate) {
  21. _pBannerDelegate = pDelegate;
  22. PBConfigData::getInstance()->initData();
  23. SpriteFrameCache::getInstance()->addSpriteFramesWithFile("内购版位_UI.plist");
  24. }
  25. void PurchaseBannerCtrl::showBanner(Node* outerNode,vector<string> bNameList,float autoTime) {
  26. if (outerNode == nullptr || bNameList.size() <= 0) {
  27. return;
  28. }
  29. _parentNode = outerNode;
  30. //640,340
  31. Size tmpsize = Size(640,340);
  32. auto vDotCfg = PBConfigData::getInstance()->getViewDot();
  33. _gDotCfg.sepWidth = vDotCfg.sepWidth;
  34. _gDotCfg.yStart = vDotCfg.yStart;
  35. _gDotCfg.highSp = vDotCfg.highSp;
  36. _gDotCfg.normalSp = vDotCfg.normalSp;
  37. _pviewNode = PurchaseBannerView::create(tmpsize,_gDotCfg,true);
  38. _pviewNode->setDelegate(this);
  39. _parentNode->addChild(_pviewNode);
  40. _pviewNode->setAnchorPoint(Vec2(.5,.5));
  41. _pviewNode->setIgnoreAnchorPointForPosition(false);
  42. _pviewCellNameMap.clear();
  43. for (int lopp = 0; lopp < bNameList.size(); lopp++) {
  44. string tmpbName = bNameList[lopp];
  45. auto tmppbcell = PurchaseBannerCell::create(tmpbName);
  46. _pviewNode->addBanner(tmppbcell);
  47. tmppbcell->setBtnClickCallBack(std::bind(&PurchaseBannerCtrl::pageViewBtnClick,this,std::placeholders::_1,std::placeholders::_2));
  48. tmppbcell->setBannerNO(lopp);
  49. _pviewCellNameMap[lopp] = tmppbcell;
  50. }
  51. this->startShowNextPage(autoTime);
  52. }
  53. void PurchaseBannerCtrl::pageViewBtnClick(int bNO,string bName) {
  54. if (bName == "noTime") {
  55. this->showTimeOutTip();
  56. return;
  57. }
  58. auto bannerinfo = PBConfigData::getInstance()->getBannerInfo(bName);
  59. if (_pBannerDelegate == nullptr) {
  60. return;
  61. }
  62. auto thisobj = this;
  63. if (bannerinfo.bannerType == "礼包") {
  64. _pBannerDelegate->onPurchaseClick(bannerinfo.bannerPID,[=](bool isok){
  65. thisobj->purchaseOverCallBack(bNO,bName,isok);
  66. });
  67. } else if (bannerinfo.bannerType == "计时") {
  68. _pBannerDelegate->onPurchaseClick(bannerinfo.bannerPID,[=](bool isok){
  69. thisobj->purchaseOverCallBack(bNO,bName,isok);
  70. });
  71. } else if (bannerinfo.bannerType == "活动") {
  72. _pBannerDelegate->onActBtnClick(bannerinfo.bannerName);
  73. }
  74. }
  75. void PurchaseBannerCtrl::purchaseOverCallBack(int bNO,string bName,bool isok) {
  76. if (isok) {
  77. if (_pviewCellNameMap.find(bNO) != _pviewCellNameMap.end()) {
  78. _pviewCellNameMap[bNO]->setCellStatus();
  79. }
  80. }
  81. }
  82. void PurchaseBannerCtrl::showTimeOutTip() {
  83. auto tmpTipLayer = RedreamLoader::Layer("内购版位_浮层_提示文本.redream");
  84. auto pNode = Director::getInstance()->getRunningScene();
  85. pNode->addChild(tmpTipLayer);
  86. float dtt = tmpTipLayer->playAnim("优惠已过期");
  87. tmpTipLayer->scheduleOnce([=](float dt){
  88. tmpTipLayer->removeFromParent();
  89. },dtt,"PurchaseBannerCtrl::showTimeOutTip");
  90. }
  91. void PurchaseBannerCtrl::startShowNextPage(float dtt) {
  92. if (dtt <= 0 || _pviewNode == nullptr) {
  93. return;
  94. }
  95. if (_pviewNode->getPageCount() <= 1) {
  96. return;
  97. }
  98. auto thisNode = this;
  99. _moveNextTime = 0;
  100. string schname = "PurchaseBannerCtrl::startShowNextPage";
  101. _pviewNode->unschedule(schname);
  102. _pviewNode->schedule([=](float dt){
  103. if (thisNode->_moveNextTime >= dtt) {
  104. int curridx = thisNode->_pviewNode->getCurrentPageIdx();
  105. int pagecount = thisNode->_pviewNode->getPageCount();
  106. if (curridx >= pagecount - 1) {
  107. thisNode->_pviewNode->moveToFirstPage();
  108. } else {
  109. thisNode->_pviewNode->moveToNextPage();
  110. }
  111. thisNode->_moveNextTime = 0;
  112. } else {
  113. thisNode->_moveNextTime++;
  114. }
  115. },1,schname);
  116. }
  117. PurchaseBannerCell* PurchaseBannerCtrl::pageCellAtIndex(PurchaseBannerView* pageView, int index) {
  118. return nullptr;
  119. }
  120. void PurchaseBannerCtrl::pageCellTouched(Touch* touch, PurchaseBannerCell* page) {
  121. }
  122. void PurchaseBannerCtrl::onPageChanged(int currentPageIndex, bool manualSet) {
  123. }
  124. void PurchaseBannerCtrl::onPageTouchBegan(Touch* touch) {
  125. _moveNextTime = 0;
  126. }