From c57463adb97815e278ed698c5fb0c92bde30c43e Mon Sep 17 00:00:00 2001 From: Cvolton Date: Sat, 16 Dec 2023 02:37:14 +0100 Subject: [PATCH] introduce templated CvoltonListView --- src/layers/DailyHistory/DailyCell.cpp | 3 +- src/layers/DailyHistory/DailyCell.h | 2 +- src/layers/DailyHistory/DailyListView.cpp | 39 --------- src/layers/DailyHistory/DailyListView.h | 13 --- src/layers/DailyHistory/DailyViewLayer.cpp | 83 ++++++++++---------- src/layers/DailyHistory/DailyViewLayer.h | 20 ++--- src/layers/RewardHistory/RewardCell.cpp | 4 +- src/layers/RewardHistory/RewardCell.h | 2 +- src/layers/RewardHistory/RewardListView.cpp | 39 --------- src/layers/RewardHistory/RewardListView.h | 13 --- src/layers/RewardHistory/RewardViewLayer.cpp | 81 ++++++++++--------- src/layers/RewardHistory/RewardViewLayer.h | 20 ++--- src/layers/_bases/CvoltonListView.cpp | 61 ++++++++++++-- src/layers/_bases/CvoltonListView.h | 13 ++- 14 files changed, 172 insertions(+), 221 deletions(-) delete mode 100644 src/layers/DailyHistory/DailyListView.cpp delete mode 100644 src/layers/DailyHistory/DailyListView.h delete mode 100644 src/layers/RewardHistory/RewardListView.cpp delete mode 100644 src/layers/RewardHistory/RewardListView.h diff --git a/src/layers/DailyHistory/DailyCell.cpp b/src/layers/DailyHistory/DailyCell.cpp index 18b433a..a8dcabd 100644 --- a/src/layers/DailyHistory/DailyCell.cpp +++ b/src/layers/DailyHistory/DailyCell.cpp @@ -2,7 +2,8 @@ #include "../../utils.hpp" #include "../../managers/BetterInfoCache.h" -void DailyCell::loadFromLevel(GJGameLevel* level) { +void DailyCell::loadFromData(CCObject* object) { + auto level = static_cast(object); this->m_level = level; auto biCache = BetterInfoCache::sharedState(); diff --git a/src/layers/DailyHistory/DailyCell.h b/src/layers/DailyHistory/DailyCell.h index d7dfcef..9a56494 100644 --- a/src/layers/DailyHistory/DailyCell.h +++ b/src/layers/DailyHistory/DailyCell.h @@ -16,6 +16,6 @@ class DailyCell : public GenericListCell { public: DailyCell(const char* name, cocos2d::CCSize size); - void loadFromLevel(GJGameLevel* level); + void loadFromData(CCObject* object); static DailyCell* create(const char* key, cocos2d::CCSize size); }; \ No newline at end of file diff --git a/src/layers/DailyHistory/DailyListView.cpp b/src/layers/DailyHistory/DailyListView.cpp deleted file mode 100644 index b3c9a72..0000000 --- a/src/layers/DailyHistory/DailyListView.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "DailyListView.h" -#include "DailyCell.h" - -DailyListView* DailyListView::create(cocos2d::CCArray* levels, float width, float height) { - auto ret = new DailyListView(); - if (ret && ret->init(levels, 0x420, width, height)) { - ret->autorelease(); - } else { - delete ret; - ret = nullptr; - } - return ret; -} - -void DailyListView::setupList() { - this->m_itemSeparation = 55.0f; - - this->m_tableView->reloadData(); - - auto coverage = calculateNodeCoverage(m_tableView->m_contentLayer->getChildren()); - if (this->m_entries->count() > 4) - m_tableView->m_contentLayer->setContentSize({-coverage.origin.x + coverage.size.width, -coverage.origin.y + coverage.size.height}); - - this->m_tableView->moveToTop(); - - if (this->m_entries->count() == 1) - this->m_tableView->moveToTopWithOffset(this->m_itemSeparation); -} - -void DailyListView::loadCell(TableViewCell* cell, int index) { - static_cast(cell)->loadFromLevel( - static_cast(this->m_entries->objectAtIndex(index)) - ); - static_cast(cell)->updateBGColor(index); -} - -TableViewCell* DailyListView::getListCell(const char* key) { - return new DailyCell(key, { this->m_width, 55.0f }); -} \ No newline at end of file diff --git a/src/layers/DailyHistory/DailyListView.h b/src/layers/DailyHistory/DailyListView.h deleted file mode 100644 index a2d34e3..0000000 --- a/src/layers/DailyHistory/DailyListView.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include "../_bases/CvoltonListView.h" - -class DailyListView : public CvoltonListView { -protected: - void setupList() override; - TableViewCell* getListCell(const char* key) override; - void loadCell(TableViewCell* cell, int index) override; - -public: - static DailyListView* create(cocos2d::CCArray* levels, float width, float height); -}; \ No newline at end of file diff --git a/src/layers/DailyHistory/DailyViewLayer.cpp b/src/layers/DailyHistory/DailyViewLayer.cpp index be6972a..27479e5 100644 --- a/src/layers/DailyHistory/DailyViewLayer.cpp +++ b/src/layers/DailyHistory/DailyViewLayer.cpp @@ -1,5 +1,4 @@ #include "DailyViewLayer.h" -#include "DailyListView.h" #include "../JumpToPageLayer.h" #include "../../utils.hpp" @@ -21,7 +20,7 @@ bool DailyViewLayer::compareDailies(const void* l1, const void* l2){ } bool DailyViewLayer::init(bool isWeekly) { - this->isWeekly = isWeekly; + m_isWeekly = isWeekly; auto GLM = GameLevelManager::sharedState(); auto winSize = CCDirector::sharedDirector()->getWinSize(); @@ -51,8 +50,8 @@ bool DailyViewLayer::init(bool isWeekly) { setKeypadEnabled(true); auto dailyLevels = GLM->m_dailyLevels; - sortedLevels = CCArray::create(); - sortedLevels->retain(); + m_sortedLevels = CCArray::create(); + m_sortedLevels->retain(); CCDictElement* obj; CCDICT_FOREACH(dailyLevels, obj){ auto currentLvl = static_cast(obj->getObject()); @@ -60,35 +59,35 @@ bool DailyViewLayer::init(bool isWeekly) { currentLvl != nullptr && ((isWeekly && currentLvl->m_dailyID >= 100000) || (!isWeekly && currentLvl->m_dailyID < 100000)) ){ - sortedLevels->addObject(currentLvl); + m_sortedLevels->addObject(currentLvl); } } - std::sort(sortedLevels->data->arr, sortedLevels->data->arr + sortedLevels->data->num, DailyViewLayer::compareDailies); + std::sort(m_sortedLevels->data->arr, m_sortedLevels->data->arr + m_sortedLevels->data->num, DailyViewLayer::compareDailies); auto prevSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Left_001.png" : "GJ_arrow_03_001.png"); - prevBtn = CCMenuItemSpriteExtra::create( + m_prevBtn = CCMenuItemSpriteExtra::create( prevSprite, this, menu_selector(DailyViewLayer::onPrev) ); - prevBtn->setPosition({- (winSize.width / 2) + 25, 0}); - menu->addChild(prevBtn); + m_prevBtn->setPosition({- (winSize.width / 2) + 25, 0}); + menu->addChild(m_prevBtn); auto nextSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Right_001.png" : "GJ_arrow_03_001.png"); if(!controllerConnected) nextSprite->setFlipX(true); - nextBtn = CCMenuItemSpriteExtra::create( + m_nextBtn = CCMenuItemSpriteExtra::create( nextSprite, this, menu_selector(DailyViewLayer::onNext) ); - nextBtn->setPosition({+ (winSize.width / 2) - 25, 0}); - menu->addChild(nextBtn); + m_nextBtn->setPosition({+ (winSize.width / 2) - 25, 0}); + menu->addChild(m_nextBtn); - counter = CCLabelBMFont::create("0 to 0 of 0", "goldFont.fnt"); - counter->setAnchorPoint({ 1.f, 1.f }); - counter->setPosition(winSize - CCPoint(7,3)); - counter->setScale(0.5f); - addChild(counter); + m_counter = CCLabelBMFont::create("0 to 0 of 0", "goldFont.fnt"); + m_counter->setAnchorPoint({ 1.f, 1.f }); + m_counter->setPosition(winSize - CCPoint(7,3)); + m_counter->setScale(0.5f); + addChild(m_counter); //corners auto cornerBL = CCSprite::createWithSpriteFrameName("GJ_sideArt_001.png"); @@ -103,10 +102,10 @@ bool DailyViewLayer::init(bool isWeekly) { addChild(cornerBR, -1); //navigation buttons - pageBtnSprite = ButtonSprite::create("1", 23, true, "bigFont.fnt", "GJ_button_02.png", 40, .7f); - pageBtnSprite->setScale(0.7f); + m_pageBtnSprite = ButtonSprite::create("1", 23, true, "bigFont.fnt", "GJ_button_02.png", 40, .7f); + m_pageBtnSprite->setScale(0.7f); auto pageBtn = CCMenuItemSpriteExtra::create( - pageBtnSprite, + m_pageBtnSprite, this, menu_selector(DailyViewLayer::onJumpToPageLayer) ); @@ -143,9 +142,9 @@ void DailyViewLayer::loadPage(unsigned int page){ auto winSize = CCDirector::sharedDirector()->getWinSize(); - if(listLayer != nullptr) listLayer->removeFromParentAndCleanup(true); + if(m_listLayer != nullptr) m_listLayer->removeFromParentAndCleanup(true); - this->page = page; + m_page = page; CCArray* displayedLevels = CCArray::create(); //TODO: can we clone this by passing an iterator or something like that const unsigned int levelCount = levelsPerPage(); @@ -153,32 +152,32 @@ void DailyViewLayer::loadPage(unsigned int page){ unsigned int lastIndex = (page+1) * levelCount; for(unsigned int i = firstIndex; i < lastIndex; i++){ - auto levelObject = sortedLevels->objectAtIndex(i); - if(i >= sortedLevels->count() || levelObject == nullptr) break; + auto levelObject = m_sortedLevels->objectAtIndex(i); + if(i >= m_sortedLevels->count() || levelObject == nullptr) break; displayedLevels->addObject(levelObject); } - dailyView = DailyListView::create(displayedLevels, 356.f, 220.f); - listLayer = GJListLayer::create(dailyView, isWeekly ? "Weekly Demons" : "Daily Levels", {191, 114, 62, 255}, 356.f, 220.f); - listLayer->setPosition(winSize / 2 - listLayer->getScaledContentSize() / 2 - CCPoint(0,5)); - addChild(listLayer); + auto listView = CvoltonListView::create(displayedLevels, 356.f, 220.f); + m_listLayer = GJListLayer::create(listView, m_isWeekly ? "Weekly Demons" : "Daily Levels", {191, 114, 62, 255}, 356.f, 220.f); + m_listLayer->setPosition(winSize / 2 - m_listLayer->getScaledContentSize() / 2 - CCPoint(0,5)); + addChild(m_listLayer); - if(page == 0) prevBtn->setVisible(false); - else prevBtn->setVisible(true); + if(page == 0) m_prevBtn->setVisible(false); + else m_prevBtn->setVisible(true); - if(sortedLevels->count() > lastIndex) nextBtn->setVisible(true); - else nextBtn->setVisible(false); + if(m_sortedLevels->count() > lastIndex) m_nextBtn->setVisible(true); + else m_nextBtn->setVisible(false); - pageBtnSprite->setString(std::to_string(page+1).c_str()); + m_pageBtnSprite->setString(std::to_string(page+1).c_str()); - counter->setCString(CCString::createWithFormat("%i to %i of %i", firstIndex+1, (sortedLevels->count() >= lastIndex) ? lastIndex : sortedLevels->count(), sortedLevels->count())->getCString()); + m_counter->setCString(CCString::createWithFormat("%i to %i of %i", firstIndex+1, (m_sortedLevels->count() >= lastIndex) ? lastIndex : m_sortedLevels->count(), m_sortedLevels->count())->getCString()); } void DailyViewLayer::keyBackClicked() { setTouchEnabled(false); setKeypadEnabled(false); - sortedLevels->release(); + m_sortedLevels->release(); CCDirector::sharedDirector()->popSceneWithTransition(0.5f, PopTransition::kPopTransitionFade); } @@ -188,11 +187,11 @@ void DailyViewLayer::onBack(CCObject* object) { } void DailyViewLayer::onPrev(CCObject* object) { - loadPage(--page); + loadPage(--m_page); } void DailyViewLayer::onNext(CCObject* object) { - loadPage(++page); + loadPage(++m_page); } void DailyViewLayer::onJumpToPageLayer(CCObject* sender){ @@ -200,11 +199,11 @@ void DailyViewLayer::onJumpToPageLayer(CCObject* sender){ } void DailyViewLayer::onRandom(CCObject* sender){ - loadPage(BetterInfo::randomNumber(0, sortedLevels->count() / levelsPerPage())); + loadPage(BetterInfo::randomNumber(0, m_sortedLevels->count() / levelsPerPage())); } void DailyViewLayer::onMore(CCObject* object) { - auto searchObject = GJSearchObject::create(isWeekly ? SearchType::WeeklyVault : SearchType::DailyVault); + auto searchObject = GJSearchObject::create(m_isWeekly ? SearchType::WeeklyVault : SearchType::DailyVault); auto browserLayer = LevelBrowserLayer::scene(searchObject); auto transitionFade = CCTransitionFade::create(0.5, browserLayer); @@ -220,7 +219,7 @@ CCScene* DailyViewLayer::scene(bool isWeekly) { } int DailyViewLayer::getPage() const{ - return page; + return m_page; } int DailyViewLayer::levelsPerPage() const{ @@ -231,11 +230,11 @@ void DailyViewLayer::keyDown(enumKeyCodes key){ switch(key){ case KEY_Left: case CONTROLLER_Left: - if(prevBtn->isVisible() == true) onPrev(nullptr); + if(m_prevBtn->isVisible() == true) onPrev(nullptr); break; case KEY_Right: case CONTROLLER_Right: - if(nextBtn->isVisible() == true) onNext(nullptr); + if(m_nextBtn->isVisible() == true) onNext(nullptr); break; default: CCLayer::keyDown(key); diff --git a/src/layers/DailyHistory/DailyViewLayer.h b/src/layers/DailyHistory/DailyViewLayer.h index 8ae01ef..2636da9 100644 --- a/src/layers/DailyHistory/DailyViewLayer.h +++ b/src/layers/DailyHistory/DailyViewLayer.h @@ -1,18 +1,18 @@ #pragma once -#include "DailyListView.h" +#include "../_bases/CvoltonListView.h" +#include "DailyCell.h" #include "../../delegates/PageNumberDelegate.h" class DailyViewLayer : public cocos2d::CCLayer, public PageNumberDelegate { - DailyListView* dailyView = nullptr; - GJListLayer* listLayer = nullptr; - cocos2d::CCArray* sortedLevels = nullptr; - CCMenuItemSpriteExtra* prevBtn = nullptr; - CCMenuItemSpriteExtra* nextBtn = nullptr; - cocos2d::CCLabelBMFont* counter = nullptr; - ButtonSprite* pageBtnSprite = nullptr; - unsigned int page = 0; - bool isWeekly = false; + GJListLayer* m_listLayer = nullptr; + cocos2d::CCArray* m_sortedLevels = nullptr; + CCMenuItemSpriteExtra* m_prevBtn = nullptr; + CCMenuItemSpriteExtra* m_nextBtn = nullptr; + cocos2d::CCLabelBMFont* m_counter = nullptr; + ButtonSprite* m_pageBtnSprite = nullptr; + unsigned int m_page = 0; + bool m_isWeekly = false; protected: virtual bool init(bool isWeekly); virtual void keyBackClicked(); diff --git a/src/layers/RewardHistory/RewardCell.cpp b/src/layers/RewardHistory/RewardCell.cpp index 073da4e..0639ccb 100644 --- a/src/layers/RewardHistory/RewardCell.cpp +++ b/src/layers/RewardHistory/RewardCell.cpp @@ -1,7 +1,9 @@ #include "RewardCell.h" #include "../utils.hpp" -void RewardCell::loadFromReward(GJRewardItem* reward) { +void RewardCell::loadFromData(CCObject* object) { + auto reward = static_cast(object); + const float rowX = 48.f; const float rowY = 10.5f; diff --git a/src/layers/RewardHistory/RewardCell.h b/src/layers/RewardHistory/RewardCell.h index d999ced..0794946 100644 --- a/src/layers/RewardHistory/RewardCell.h +++ b/src/layers/RewardHistory/RewardCell.h @@ -9,6 +9,6 @@ class RewardCell : public GenericListCell { public: RewardCell(const char* name, cocos2d::CCSize size); - void loadFromReward(GJRewardItem* reward); + void loadFromData(CCObject* object); static RewardCell* create(const char* key, cocos2d::CCSize size); }; \ No newline at end of file diff --git a/src/layers/RewardHistory/RewardListView.cpp b/src/layers/RewardHistory/RewardListView.cpp deleted file mode 100644 index 3336385..0000000 --- a/src/layers/RewardHistory/RewardListView.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include "RewardListView.h" -#include "RewardCell.h" - -RewardListView* RewardListView::create(cocos2d::CCArray* rewards, float width, float height) { - auto ret = new RewardListView(); - if (ret && ret->init(rewards, 0x420, width, height)) { - ret->autorelease(); - } else { - delete ret; - ret = nullptr; - } - return ret; -} - -void RewardListView::setupList() { - this->m_itemSeparation = 45.0f; - - this->m_tableView->reloadData(); - - auto coverage = calculateNodeCoverage(m_tableView->m_contentLayer->getChildren()); - if (this->m_entries->count() > 4) - m_tableView->m_contentLayer->setContentSize({-coverage.origin.x + coverage.size.width, -coverage.origin.y + coverage.size.height}); - - this->m_tableView->moveToTop(); - - if (this->m_entries->count() == 1) - this->m_tableView->moveToTopWithOffset(this->m_itemSeparation); -} - -void RewardListView::loadCell(TableViewCell* cell, int index) { - static_cast(cell)->loadFromReward( - static_cast(this->m_entries->objectAtIndex(index)) - ); - static_cast(cell)->updateBGColor(index); -} - -TableViewCell* RewardListView::getListCell(const char* key) { - return new RewardCell(key, { this->m_width, this->m_itemSeparation }); -} \ No newline at end of file diff --git a/src/layers/RewardHistory/RewardListView.h b/src/layers/RewardHistory/RewardListView.h deleted file mode 100644 index 91724a4..0000000 --- a/src/layers/RewardHistory/RewardListView.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include "../_bases/CvoltonListView.h" - -class RewardListView : public CvoltonListView { -protected: - void setupList() override; - TableViewCell* getListCell(const char* key) override; - void loadCell(TableViewCell* cell, int index) override; - -public: - static RewardListView* create(cocos2d::CCArray* rewards, float width, float height); -}; \ No newline at end of file diff --git a/src/layers/RewardHistory/RewardViewLayer.cpp b/src/layers/RewardHistory/RewardViewLayer.cpp index d41453c..93683ee 100644 --- a/src/layers/RewardHistory/RewardViewLayer.cpp +++ b/src/layers/RewardHistory/RewardViewLayer.cpp @@ -1,5 +1,4 @@ #include "RewardViewLayer.h" -#include "RewardListView.h" #include "../JumpToPageLayer.h" #include "../../utils.hpp" @@ -25,7 +24,7 @@ bool RewardViewLayer::compareRewards(const void* i1, const void* i2){ bool RewardViewLayer::init(CCDictionary* chests, const char* title) { - this->title = title; + m_title = title; Mod::get()->setSavedValue("reward-cell-title", title); @@ -62,39 +61,39 @@ bool RewardViewLayer::init(CCDictionary* chests, const char* title) { setTouchEnabled(true); setKeypadEnabled(true); - sortedRewards = CCArray::create(); - sortedRewards->retain(); + m_sortedRewards = CCArray::create(); + m_sortedRewards->retain(); CCDictElement* obj; CCDICT_FOREACH(chests, obj){ auto currentReward = static_cast(obj->getObject()); - if(currentReward != nullptr) sortedRewards->addObject(currentReward); + if(currentReward != nullptr) m_sortedRewards->addObject(currentReward); } - std::sort(sortedRewards->data->arr, sortedRewards->data->arr + sortedRewards->data->num, RewardViewLayer::compareRewards); + std::sort(m_sortedRewards->data->arr, m_sortedRewards->data->arr + m_sortedRewards->data->num, RewardViewLayer::compareRewards); auto prevSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Left_001.png" : "GJ_arrow_03_001.png"); - prevBtn = CCMenuItemSpriteExtra::create( + m_prevBtn = CCMenuItemSpriteExtra::create( prevSprite, this, menu_selector(RewardViewLayer::onPrev) ); - prevBtn->setPosition({- (winSize.width / 2) + 25, 0}); - menu->addChild(prevBtn); + m_prevBtn->setPosition({- (winSize.width / 2) + 25, 0}); + menu->addChild(m_prevBtn); auto nextSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Right_001.png" : "GJ_arrow_03_001.png"); if(!controllerConnected) nextSprite->setFlipX(true); - nextBtn = CCMenuItemSpriteExtra::create( + m_nextBtn = CCMenuItemSpriteExtra::create( nextSprite, this, menu_selector(RewardViewLayer::onNext) ); - nextBtn->setPosition({+ (winSize.width / 2) - 25, 0}); - menu->addChild(nextBtn); + m_nextBtn->setPosition({+ (winSize.width / 2) - 25, 0}); + menu->addChild(m_nextBtn); - counter = CCLabelBMFont::create("0 to 0 of 0", "goldFont.fnt"); - counter->setAnchorPoint({ 1.f, 1.f }); - counter->setPosition(winSize - CCPoint(7,3)); - counter->setScale(0.5f); - addChild(counter); + m_counter = CCLabelBMFont::create("0 to 0 of 0", "goldFont.fnt"); + m_counter->setAnchorPoint({ 1.f, 1.f }); + m_counter->setPosition(winSize - CCPoint(7,3)); + m_counter->setScale(0.5f); + addChild(m_counter); //corners auto cornerBL = CCSprite::createWithSpriteFrameName("GJ_sideArt_001.png"); @@ -109,10 +108,10 @@ bool RewardViewLayer::init(CCDictionary* chests, const char* title) { addChild(cornerBR, -1); //navigation buttons - pageBtnSprite = ButtonSprite::create("1", 23, true, "bigFont.fnt", "GJ_button_02.png", 40, .7f); - pageBtnSprite->setScale(0.7f); + m_pageBtnSprite = ButtonSprite::create("1", 23, true, "bigFont.fnt", "GJ_button_02.png", 40, .7f); + m_pageBtnSprite->setScale(0.7f); auto pageBtn = CCMenuItemSpriteExtra::create( - pageBtnSprite, + m_pageBtnSprite, this, menu_selector(RewardViewLayer::onJumpToPageLayer) ); @@ -138,9 +137,9 @@ void RewardViewLayer::loadPage(unsigned int page){ auto winSize = CCDirector::sharedDirector()->getWinSize(); - if(listLayer != nullptr) listLayer->removeFromParentAndCleanup(true); + if(m_listLayer != nullptr) m_listLayer->removeFromParentAndCleanup(true); - this->page = page; + m_page = page; CCArray* displayedLevels = CCArray::create(); //TODO: can we clone this by passing an iterator or something like that const unsigned int levelCount = rewardsPerPage(); @@ -148,32 +147,32 @@ void RewardViewLayer::loadPage(unsigned int page){ unsigned int lastIndex = (page+1) * levelCount; for(unsigned int i = firstIndex; i < lastIndex; i++){ - auto levelObject = sortedRewards->objectAtIndex(i); - if(i >= sortedRewards->count() || levelObject == nullptr) break; + auto levelObject = m_sortedRewards->objectAtIndex(i); + if(i >= m_sortedRewards->count() || levelObject == nullptr) break; displayedLevels->addObject(levelObject); } - rewardView = RewardListView::create(displayedLevels, 356.f, 220.f); - listLayer = GJListLayer::create(rewardView, CCString::createWithFormat("%s Chests", title)->getCString(), {191, 114, 62, 255}, 356.f, 220.f); - listLayer->setPosition(winSize / 2 - listLayer->getScaledContentSize() / 2 - CCPoint(0,5)); - addChild(listLayer); + auto rewardView = CvoltonListView::create(displayedLevels, 356.f, 220.f, 45.f); + m_listLayer = GJListLayer::create(rewardView, CCString::createWithFormat("%s Chests", m_title)->getCString(), {191, 114, 62, 255}, 356.f, 220.f); + m_listLayer->setPosition(winSize / 2 - m_listLayer->getScaledContentSize() / 2 - CCPoint(0,5)); + addChild(m_listLayer); - if(page == 0) prevBtn->setVisible(false); - else prevBtn->setVisible(true); + if(page == 0) m_prevBtn->setVisible(false); + else m_prevBtn->setVisible(true); - if(sortedRewards->count() > lastIndex) nextBtn->setVisible(true); - else nextBtn->setVisible(false); + if(m_sortedRewards->count() > lastIndex) m_nextBtn->setVisible(true); + else m_nextBtn->setVisible(false); - pageBtnSprite->setString(std::to_string(page+1).c_str()); + m_pageBtnSprite->setString(std::to_string(page+1).c_str()); - counter->setCString(CCString::createWithFormat("%i to %i of %i", firstIndex+1, (sortedRewards->count() >= lastIndex) ? lastIndex : sortedRewards->count(), sortedRewards->count())->getCString()); + m_counter->setCString(CCString::createWithFormat("%i to %i of %i", firstIndex+1, (m_sortedRewards->count() >= lastIndex) ? lastIndex : m_sortedRewards->count(), m_sortedRewards->count())->getCString()); } void RewardViewLayer::keyBackClicked() { setTouchEnabled(false); setKeypadEnabled(false); - sortedRewards->release(); + m_sortedRewards->release(); CCDirector::sharedDirector()->popSceneWithTransition(0.5f, PopTransition::kPopTransitionFade); } @@ -183,11 +182,11 @@ void RewardViewLayer::onBack(CCObject* object) { } void RewardViewLayer::onPrev(CCObject* object) { - loadPage(--page); + loadPage(--m_page); } void RewardViewLayer::onNext(CCObject* object) { - loadPage(++page); + loadPage(++m_page); } void RewardViewLayer::onJumpToPageLayer(CCObject* sender){ @@ -195,7 +194,7 @@ void RewardViewLayer::onJumpToPageLayer(CCObject* sender){ } void RewardViewLayer::onRandom(CCObject* sender){ - loadPage(BetterInfo::randomNumber(0, sortedRewards->count() / rewardsPerPage())); + loadPage(BetterInfo::randomNumber(0, m_sortedRewards->count() / rewardsPerPage())); } CCScene* RewardViewLayer::scene(CCDictionary* chests, const char* title) { @@ -206,7 +205,7 @@ CCScene* RewardViewLayer::scene(CCDictionary* chests, const char* title) { } int RewardViewLayer::getPage() const{ - return page; + return m_page; } int RewardViewLayer::rewardsPerPage() const{ @@ -217,11 +216,11 @@ void RewardViewLayer::keyDown(enumKeyCodes key){ switch(key){ case KEY_Left: case CONTROLLER_Left: - if(prevBtn->isVisible() == true) onPrev(nullptr); + if(m_prevBtn->isVisible() == true) onPrev(nullptr); break; case KEY_Right: case CONTROLLER_Right: - if(nextBtn->isVisible() == true) onNext(nullptr); + if(m_nextBtn->isVisible() == true) onNext(nullptr); break; default: CCLayer::keyDown(key); diff --git a/src/layers/RewardHistory/RewardViewLayer.h b/src/layers/RewardHistory/RewardViewLayer.h index 74860dd..87db00d 100644 --- a/src/layers/RewardHistory/RewardViewLayer.h +++ b/src/layers/RewardHistory/RewardViewLayer.h @@ -1,18 +1,18 @@ #pragma once -#include "RewardListView.h" +#include "../_bases/CvoltonListView.h" +#include "RewardCell.h" #include "../../delegates/PageNumberDelegate.h" class RewardViewLayer : public cocos2d::CCLayer, public PageNumberDelegate { - RewardListView* rewardView = nullptr; - GJListLayer* listLayer = nullptr; - cocos2d::CCArray* sortedRewards = nullptr; - CCMenuItemSpriteExtra* prevBtn = nullptr; - CCMenuItemSpriteExtra* nextBtn = nullptr; - cocos2d::CCLabelBMFont* counter = nullptr; - ButtonSprite* pageBtnSprite = nullptr; - const char* title = nullptr; - unsigned int page = 0; + GJListLayer* m_listLayer = nullptr; + cocos2d::CCArray* m_sortedRewards = nullptr; + CCMenuItemSpriteExtra* m_prevBtn = nullptr; + CCMenuItemSpriteExtra* m_nextBtn = nullptr; + cocos2d::CCLabelBMFont* m_counter = nullptr; + ButtonSprite* m_pageBtnSprite = nullptr; + const char* m_title = nullptr; + unsigned int m_page = 0; protected: virtual bool init(cocos2d::CCDictionary* chests, const char* title); virtual void keyBackClicked(); diff --git a/src/layers/_bases/CvoltonListView.cpp b/src/layers/_bases/CvoltonListView.cpp index c52f909..b2508de 100644 --- a/src/layers/_bases/CvoltonListView.cpp +++ b/src/layers/_bases/CvoltonListView.cpp @@ -1,15 +1,64 @@ #include "CvoltonListView.h" +#include -using namespace gd; -using namespace cocos2d; +#include "../DailyHistory/DailyCell.h" +#include "../RewardHistory/RewardCell.h" -bool CvoltonListView::init(cocos2d::CCArray* entries, int btype, float width, float height) { - if(!CustomListView::init(entries, (BoomListType) btype, width, height)) return false; +using namespace geode::prelude; + +template +bool CvoltonListView::init(cocos2d::CCArray* entries, float width, float height, float separation) { + m_itemSeparation = separation; + + if(!CustomListView::init(entries, (BoomListType) 0x420, width, height)) return false; m_tableView->retain(); return true; } -CvoltonListView::~CvoltonListView() { +template +CvoltonListView::~CvoltonListView() { m_tableView->release(); -} \ No newline at end of file +} + +template +CvoltonListView* CvoltonListView::create(cocos2d::CCArray* data, float width, float height, float separation) { + auto ret = new CvoltonListView(); + if (ret && ret->init(data, width, height, separation)) { + ret->autorelease(); + } else { + delete ret; + ret = nullptr; + } + return ret; +} + +template +void CvoltonListView::setupList() { + this->m_tableView->reloadData(); + + auto coverage = calculateNodeCoverage(m_tableView->m_contentLayer->getChildren()); + if (this->m_entries->count() > 4) + m_tableView->m_contentLayer->setContentSize({-coverage.origin.x + coverage.size.width, -coverage.origin.y + coverage.size.height}); + + this->m_tableView->moveToTop(); + + if (this->m_entries->count() == 1) + this->m_tableView->moveToTopWithOffset(this->m_itemSeparation); +} + +template +void CvoltonListView::loadCell(TableViewCell* cell, int index) { + static_cast(cell)->loadFromData( + this->m_entries->objectAtIndex(index) + ); + static_cast(cell)->updateBGColor(index); +} + +template +TableViewCell* CvoltonListView::getListCell(const char* key) { + return new Cell(key, { this->m_width, m_itemSeparation }); +} + +template CvoltonListView; +template CvoltonListView; \ No newline at end of file diff --git a/src/layers/_bases/CvoltonListView.h b/src/layers/_bases/CvoltonListView.h index 8191e98..31e8764 100644 --- a/src/layers/_bases/CvoltonListView.h +++ b/src/layers/_bases/CvoltonListView.h @@ -2,11 +2,16 @@ #include -/** - * @brief This class only exists as a bugfix to CustomListView - */ +template class CvoltonListView : public CustomListView { protected: - bool init(cocos2d::CCArray* entries, int btype, float width, float height); + bool init(cocos2d::CCArray* entries, float width, float height, float separation); virtual ~CvoltonListView(); + + void setupList() override; + TableViewCell* getListCell(const char* key) override; + void loadCell(TableViewCell* cell, int index) override; + +public: + static CvoltonListView* create(cocos2d::CCArray* data, float width, float height, float separation = 55.0f); }; \ No newline at end of file