123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- //
- // IAPCtlShopUI.cpp
- // TileManor
- //
- // Created by 徐炼新 on 2024/1/16.
- //
- #include "IAPCtlShopUI.hpp"
- #include "IAPCtlShopItem.hpp"
- #include "RUUtils.h"
- #include "RUPlatform.h"
- IAPCtlShopUI* IAPCtlShopUI::_instance = nullptr;
- IAPCtlShopUI* IAPCtlShopUI::getInstance() {
- if (_instance == nullptr) {
- _instance = new IAPCtlShopUI();
- _instance->autorelease();
- _instance->retain();
- }
- return _instance;
- }
- IAPCtlShopUI::IAPCtlShopUI() {
- }
- IAPCtlShopUI::~IAPCtlShopUI() {
-
- }
- bool IAPCtlShopUI::isShowing() {
- return _bUIShowing;
- }
- void IAPCtlShopUI::clear() {
- _goodsInfo.clear();
- for (auto& gi : _goodsItems) {
- gi->release();
- }
- _goodsItems.clear();
- _allSizes.clear();
- _cb = nullptr;
- }
- bool IAPCtlShopUI::create(Node* pNode, IAPConf* cfg, int placementId) {
- redutils::Platform::getInstance()->reportLog("open iap");
- _cfg = cfg;
- _bUIShowing = true;
- _placementId = placementId;
- clear();
-
- _layer = redutils::RUReboltLayer::createReboltLayer("shop_interface.redream");
- pNode->addChild(_layer, 3);
- _layer->registerOnNotify([this](const redutils::ReboltNotifyData& data){
- _onNotifyDevelopment(data);
- });
- _layer->runBehaviacWhitFunName("初始化");
-
- // _igCoin->doOwnInit();
-
- if (_goodsInfo.size() == 0) {
- IAPConf::getInstance()->getAllGoods(_goodsInfo);
- }
-
- // 构建 list 区域
- constructShopItem(false);
- cocos2d::Size size = _ndScrollArea->getContentSize();
- _tableView = redutils::RUTableView::create(&_tableData, cocos2d::Size(size.width, size.height));
- _tableView->setCascadeOpacityEnabled(true);
- _tableView->setCascadeColorEnabled(true);
- _tableView->setDirection(ScrollViewSmooth::Direction::VERTICAL);
- _tableView->setVerticalFillOrder(TableViewSmooth::VerticalFillOrder::TOP_DOWN, false);
- _tableView->setDelegate(&_tableData);
- _tableView->setBounceable(true);
- // _tableView->setOutSightClean(false);
- _ndScrollArea->addChild(_tableView);
-
- return true;
- }
- void IAPCtlShopUI::_onNotifyDevelopment(const redutils::ReboltNotifyData& data) {
- if (data.notify == "绑定显示区域") {
- _ndScrollArea = data.outNode;
- } else if (data.notify == "绑定coin节点") {
- // _igCoin = new MapIGCoin(data.reboltRedManager);
- } else if (data.notify == "退出") {
- auto rm = RemoveSelf::create();
- auto cf = CallFunc::create([=]() {
- _layer->removeFromParent();
- _layer = nullptr;
- _bUIShowing = false;
- redutils::Platform::getInstance()->reportLog("iap closed");
- });
- _layer->runAction(Sequence::create(rm, cf, NULL));
- if (_cb) {
- _cb();
- }
- _cb = nullptr;
- }
- }
- void IAPCtlShopUI::constructShopItem(bool bShowAll) {
- int index = 0;
- _tableData.setCascadeOpacity(true);
- _tableData.clear();
- cocos2d::Size size = _ndScrollArea->getContentSize();
- auto delayIdx = 0;
- cocos2d::Vec2 offset(size.width, 0);
- float dur = 0.4;
- int height = 0;
-
- // 加入一个表头
- {
- Node* nd = Node::create();
- _tableData.insertCell(index++, nd);
- int h = size.height*0.08;
- _allSizes.push_back(cocos2d::Size(size.width, h));
- height += h;
- }
-
- // 当前有效的区域
- for (const auto& gi : _goodsInfo) {
- if (bShowAll || gi.always) {
- bool bNeedAnim = height <= size.height;
- Node* nd = Node::create();
- nd->setCascadeOpacityEnabled(true);
- nd->setCascadeColorEnabled(true);
- auto si = IAPCtlShopItem::create(gi, _placementId);
- auto sz = si->getSize();
- cocos2d::Vec2 pos(size.width/2.0, sz.height/2.0);
- nd->addChild(si);
- si->setPosition(bNeedAnim ? pos+offset : pos);
- _goodsItems.push_back(si);
- _tableData.insertCell(index++, nd);
- _allSizes.push_back(sz);
- height += sz.height;
-
- if (bNeedAnim) {
- // 加入动画
- auto d = DelayTime::create(delayIdx*dur/8.0);
- auto m = EaseBackOut::create(MoveBy::create(dur, cocos2d::Vec2(-offset.x, offset.y)));
- si->runAction(Sequence::create(d, m, NULL));
- delayIdx ++;
- }
- }
- }
-
- if (!bShowAll) {
- // 加入show All btn, 这个是一定会有动画的
- auto sz = cocos2d::Size(584, 130);
- Node* nd = Node::create();
- nd->setCascadeOpacityEnabled(true);
- nd->setCascadeColorEnabled(true);
- _btnShowAll = redutils::RUReboltLayer::createReboltLayer("shop_more_btn.redream");
- nd->addChild(_btnShowAll);
- _btnShowAll->setPosition(Vec2(size.width/2.0, sz.height/2.0) + offset);
- _tableData.insertCell(index++, nd);
- _btnShowAll->registerOnNotify([this](const redutils::ReboltNotifyData& data){
- if (data.notify == "点击") {
- auto rm = RemoveSelf::create();
- auto cf = CallFunc::create([=](){
- _btnShowAll = NULL;
- _tableData.clear();
- _goodsItems.clear();
- _allSizes.clear();
- constructShopItem(true);
- _tableView->reloadData(true);
- });
- _btnShowAll->runAction(Sequence::create(rm, cf, NULL));
- }
- });
- redutils::iterateNode(nd, [](Node* nd){
- nd->setCascadeOpacityEnabled(true);
- nd->setCascadeColorEnabled(true);
- });
- _btnShowAll->runBehaviacWhitFunName("初始化");
- _allSizes.push_back(sz);
-
- // 加入动画
- auto d = DelayTime::create(delayIdx*dur/8.0);
- auto m = EaseBackOut::create(MoveBy::create(dur, cocos2d::Vec2(-offset.x, offset.y)));
- _btnShowAll->runAction(Sequence::create(d, m, NULL));
- delayIdx ++;
- } else {
- // 底部添加一个额外节点(一个是底部的广告,还有就是美观一些)
- _tableData.insertCell(index++, Node::create());
- _allSizes.push_back(cocos2d::Size(584, 150));
- }
-
- _tableData._numberOfCellsInTableView = [=](cocos2d::extension::TableViewSmooth*)->ssize_t{
- return _allSizes.size();
- };
- _tableData._tableCellSizeForIndex = [=](cocos2d::extension::TableViewSmooth*, ssize_t idx)->Size{
- if (idx < _allSizes.size()) {
- return _allSizes[idx];
- } else {
- return Size(0,0);
- }
- };
-
- // 防止拉动的时候才创建
- _layer->scheduleOnce([=](float){
- for (int i=0; i<_allSizes.size(); i++) {
- _tableView->updateCellAtIndex(i);
- }
- }, 0.05, "SCH_Update_Cells");
- }
- void IAPCtlShopUI::quit() {
- if (_layer) {
- _layer->runBehaviacWhitFunName("退出");
- }
- }
|