diff --git a/src/layers/DailyHistory/DailyViewLayer.cpp b/src/layers/DailyHistory/DailyViewLayer.cpp index 27479e5..20a8c1a 100644 --- a/src/layers/DailyHistory/DailyViewLayer.cpp +++ b/src/layers/DailyHistory/DailyViewLayer.cpp @@ -20,38 +20,13 @@ bool DailyViewLayer::compareDailies(const void* l1, const void* l2){ } bool DailyViewLayer::init(bool isWeekly) { - m_isWeekly = isWeekly; - + // initialize data auto GLM = GameLevelManager::sharedState(); auto winSize = CCDirector::sharedDirector()->getWinSize(); - auto backgroundSprite = CCSprite::create("game_bg_14_001.png"); - bool controllerConnected = BetterInfo::controllerConnected(); - - backgroundSprite->setScale(winSize.width / backgroundSprite->getContentSize().width); - backgroundSprite->setAnchorPoint({0, 0}); - backgroundSprite->setPosition({0,-75}); - backgroundSprite->setColor({164, 0, 255}); //purple - backgroundSprite->setZOrder(-2); - addChild(backgroundSprite); - - auto menu = CCMenu::create(); - addChild(menu); - - auto backBtn = CCMenuItemSpriteExtra::create( - CCSprite::createWithSpriteFrameName("GJ_arrow_01_001.png"), - this, - menu_selector(DailyViewLayer::onBack) - ); - - backBtn->setPosition({(-winSize.width / 2) + 25, (winSize.height / 2) - 25}); - menu->addChild(backBtn); - - setTouchEnabled(true); - setKeypadEnabled(true); auto dailyLevels = GLM->m_dailyLevels; - m_sortedLevels = CCArray::create(); - m_sortedLevels->retain(); + m_data = CCArray::create(); + m_data->retain(); CCDictElement* obj; CCDICT_FOREACH(dailyLevels, obj){ auto currentLvl = static_cast(obj->getObject()); @@ -59,69 +34,16 @@ bool DailyViewLayer::init(bool isWeekly) { currentLvl != nullptr && ((isWeekly && currentLvl->m_dailyID >= 100000) || (!isWeekly && currentLvl->m_dailyID < 100000)) ){ - m_sortedLevels->addObject(currentLvl); + m_data->addObject(currentLvl); } } - std::sort(m_sortedLevels->data->arr, m_sortedLevels->data->arr + m_sortedLevels->data->num, DailyViewLayer::compareDailies); + std::sort(m_data->data->arr, m_data->data->arr + m_data->data->num, DailyViewLayer::compareDailies); - auto prevSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Left_001.png" : "GJ_arrow_03_001.png"); - m_prevBtn = CCMenuItemSpriteExtra::create( - prevSprite, - this, - menu_selector(DailyViewLayer::onPrev) - ); - m_prevBtn->setPosition({- (winSize.width / 2) + 25, 0}); - menu->addChild(m_prevBtn); + // init the layer + BIViewLayer::init(); - auto nextSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Right_001.png" : "GJ_arrow_03_001.png"); - if(!controllerConnected) nextSprite->setFlipX(true); - m_nextBtn = CCMenuItemSpriteExtra::create( - nextSprite, - this, - menu_selector(DailyViewLayer::onNext) - ); - m_nextBtn->setPosition({+ (winSize.width / 2) - 25, 0}); - menu->addChild(m_nextBtn); - - 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"); - cornerBL->setPosition({0,0}); - cornerBL->setAnchorPoint({0,0}); - addChild(cornerBL, -1); - - auto cornerBR = CCSprite::createWithSpriteFrameName("GJ_sideArt_001.png"); - cornerBR->setPosition({winSize.width,0}); - cornerBR->setAnchorPoint({0,0}); - cornerBR->setRotation(270); - addChild(cornerBR, -1); - - //navigation buttons - m_pageBtnSprite = ButtonSprite::create("1", 23, true, "bigFont.fnt", "GJ_button_02.png", 40, .7f); - m_pageBtnSprite->setScale(0.7f); - auto pageBtn = CCMenuItemSpriteExtra::create( - m_pageBtnSprite, - this, - menu_selector(DailyViewLayer::onJumpToPageLayer) - ); - pageBtn->setSizeMult(1.2f); - pageBtn->setPosition({+ (winSize.width / 2) - 23, (winSize.height / 2) - 37}); - menu->addChild(pageBtn); - - auto randomSprite = BetterInfo::createWithBISpriteFrameName("BI_randomBtn_001.png"); - randomSprite->setScale(0.9f); - auto randomBtn = CCMenuItemSpriteExtra::create( - randomSprite, - this, - menu_selector(DailyViewLayer::onRandom) - ); - randomBtn->setPosition({ (winSize.width / 2) - 23, (winSize.height / 2) - 72}); - menu->addChild(randomBtn); + // add more btn + auto menu = CCMenu::create(); auto buttonSprite = ButtonSprite::create("More", (int)(90*0.5), true, "bigFont.fnt", "GJ_button_01.png", 44*0.5f, 0.5f); auto buttonButton = CCMenuItemSpriteExtra::create( @@ -134,6 +56,8 @@ bool DailyViewLayer::init(bool isWeekly) { buttonButton->setAnchorPoint({0,0}); menu->addChild(buttonButton); + addChild(menu); + loadPage(0); return true; } @@ -142,64 +66,28 @@ void DailyViewLayer::loadPage(unsigned int page){ auto winSize = CCDirector::sharedDirector()->getWinSize(); - if(m_listLayer != nullptr) m_listLayer->removeFromParentAndCleanup(true); - - 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(); + + const unsigned int levelCount = resultsPerPage(); unsigned int firstIndex = page * levelCount; unsigned int lastIndex = (page+1) * levelCount; for(unsigned int i = firstIndex; i < lastIndex; i++){ - auto levelObject = m_sortedLevels->objectAtIndex(i); - if(i >= m_sortedLevels->count() || levelObject == nullptr) break; + auto levelObject = m_data->objectAtIndex(i); + if(i >= m_data->count() || levelObject == nullptr) break; displayedLevels->addObject(levelObject); } - 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); + m_listView = CvoltonListView::create(displayedLevels, 356.f, 220.f); + m_title = m_isWeekly ? "Weekly Demons" : "Daily Levels"; - if(page == 0) m_prevBtn->setVisible(false); - else m_prevBtn->setVisible(true); - - if(m_sortedLevels->count() > lastIndex) m_nextBtn->setVisible(true); - else m_nextBtn->setVisible(false); - - m_pageBtnSprite->setString(std::to_string(page+1).c_str()); - - m_counter->setCString(CCString::createWithFormat("%i to %i of %i", firstIndex+1, (m_sortedLevels->count() >= lastIndex) ? lastIndex : m_sortedLevels->count(), m_sortedLevels->count())->getCString()); + BIViewLayer::loadPage(page); } void DailyViewLayer::keyBackClicked() { - setTouchEnabled(false); - setKeypadEnabled(false); - m_sortedLevels->release(); - CCDirector::sharedDirector()->popSceneWithTransition(0.5f, PopTransition::kPopTransitionFade); -} - - -void DailyViewLayer::onBack(CCObject* object) { - keyBackClicked(); -} - -void DailyViewLayer::onPrev(CCObject* object) { - loadPage(--m_page); -} - -void DailyViewLayer::onNext(CCObject* object) { - loadPage(++m_page); -} - -void DailyViewLayer::onJumpToPageLayer(CCObject* sender){ - JumpToPageLayer::create(this)->show(); -} - -void DailyViewLayer::onRandom(CCObject* sender){ - loadPage(BetterInfo::randomNumber(0, m_sortedLevels->count() / levelsPerPage())); + BIViewLayer::keyBackClicked(); + m_data->release(); } void DailyViewLayer::onMore(CCObject* object) { @@ -216,27 +104,4 @@ CCScene* DailyViewLayer::scene(bool isWeekly) { auto scene = CCScene::create(); scene->addChild(layer); return scene; -} - -int DailyViewLayer::getPage() const{ - return m_page; -} - -int DailyViewLayer::levelsPerPage() const{ - return (GameManager::sharedState()->getGameVariable("0093")) ? 20 : 10; -} - -void DailyViewLayer::keyDown(enumKeyCodes key){ - switch(key){ - case KEY_Left: - case CONTROLLER_Left: - if(m_prevBtn->isVisible() == true) onPrev(nullptr); - break; - case KEY_Right: - case CONTROLLER_Right: - if(m_nextBtn->isVisible() == true) onNext(nullptr); - break; - default: - CCLayer::keyDown(key); - } } \ No newline at end of file diff --git a/src/layers/DailyHistory/DailyViewLayer.h b/src/layers/DailyHistory/DailyViewLayer.h index 2636da9..eca8f70 100644 --- a/src/layers/DailyHistory/DailyViewLayer.h +++ b/src/layers/DailyHistory/DailyViewLayer.h @@ -1,33 +1,18 @@ #pragma once -#include "../_bases/CvoltonListView.h" +#include "../_bases/BIViewLayer.h" #include "DailyCell.h" #include "../../delegates/PageNumberDelegate.h" -class DailyViewLayer : public cocos2d::CCLayer, public PageNumberDelegate { - 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; +class DailyViewLayer : public BIViewLayer { bool m_isWeekly = false; protected: virtual bool init(bool isWeekly); virtual void keyBackClicked(); - void onBack(cocos2d::CCObject*); - void onPrev(cocos2d::CCObject*); - void onNext(cocos2d::CCObject*); - void onJumpToPageLayer(cocos2d::CCObject*); - void onRandom(cocos2d::CCObject*); void onMore(cocos2d::CCObject*); - int levelsPerPage() const; public: virtual void loadPage(unsigned int page); static DailyViewLayer* create(bool isWeekly); static bool compareDailies(const void* l1, const void* l2); static cocos2d::CCScene* scene(bool isWeekly); - virtual int getPage() const; - void keyDown(cocos2d::enumKeyCodes key); }; \ No newline at end of file diff --git a/src/layers/_bases/BIViewLayer.cpp b/src/layers/_bases/BIViewLayer.cpp new file mode 100644 index 0000000..492bfc6 --- /dev/null +++ b/src/layers/_bases/BIViewLayer.cpp @@ -0,0 +1,158 @@ +#include "BIViewLayer.h" +#include "../JumpToPageLayer.h" +#include "../../utils.hpp" + +BIViewLayer* BIViewLayer::create() { + auto ret = new BIViewLayer(); + if (ret && ret->init()) { + ret->autorelease(); + } else { + delete ret; + ret = nullptr; + } + return ret; +} + +bool BIViewLayer::init() { + BIBaseLayer::init(); + + auto controllerConnected = BetterInfo::controllerConnected(); + auto winSize = CCDirector::sharedDirector()->getWinSize(); + + auto menu = CCMenu::create(); + auto prevSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Left_001.png" : "GJ_arrow_03_001.png"); + m_prevBtn = CCMenuItemSpriteExtra::create( + prevSprite, + this, + menu_selector(BIViewLayer::onPrev) + ); + 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); + m_nextBtn = CCMenuItemSpriteExtra::create( + nextSprite, + this, + menu_selector(BIViewLayer::onNext) + ); + m_nextBtn->setPosition({+ (winSize.width / 2) - 25, 0}); + menu->addChild(m_nextBtn); + + 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); + + //navigation buttons + m_pageBtnSprite = ButtonSprite::create("1", 23, true, "bigFont.fnt", "GJ_button_02.png", 40, .7f); + m_pageBtnSprite->setScale(0.7f); + auto pageBtn = CCMenuItemSpriteExtra::create( + m_pageBtnSprite, + this, + menu_selector(BIViewLayer::onJumpToPageLayer) + ); + pageBtn->setSizeMult(1.2f); + pageBtn->setPosition({+ (winSize.width / 2) - 23, (winSize.height / 2) - 37}); + menu->addChild(pageBtn); + + auto randomSprite = BetterInfo::createWithBISpriteFrameName("BI_randomBtn_001.png"); + randomSprite->setScale(0.9f); + auto randomBtn = CCMenuItemSpriteExtra::create( + randomSprite, + this, + menu_selector(BIViewLayer::onRandom) + ); + randomBtn->setPosition({ (winSize.width / 2) - 23, (winSize.height / 2) - 72}); + menu->addChild(randomBtn); + + addChild(menu); + + loadPage(0); + return true; +} + +void BIViewLayer::loadPage(unsigned int page){ + auto winSize = CCDirector::sharedDirector()->getWinSize(); + + const unsigned int count = resultsPerPage(); + unsigned int firstIndex = page * count; + unsigned int lastIndex = (page+1) * count; + + m_page = page; + + if(page == 0) m_prevBtn->setVisible(false); + else m_prevBtn->setVisible(true); + + if(m_data->count() > lastIndex) m_nextBtn->setVisible(true); + else m_nextBtn->setVisible(false); + + m_pageBtnSprite->setString(std::to_string(page+1).c_str()); + + if(m_listLayer != nullptr) m_listLayer->removeFromParentAndCleanup(true); + + m_listLayer = GJListLayer::create(m_listView, m_title, {191, 114, 62, 255}, 356.f, 220.f); + m_listLayer->setPosition(winSize / 2 - m_listLayer->getScaledContentSize() / 2 - CCPoint(0,5)); + addChild(m_listLayer); + + m_counter->setCString(CCString::createWithFormat("%i to %i of %i", firstIndex+1, (m_data->count() >= lastIndex) ? lastIndex : m_data->count(), m_data->count())->getCString()); +} + +void BIViewLayer::keyBackClicked() { + setTouchEnabled(false); + setKeypadEnabled(false); + m_data->release(); + CCDirector::sharedDirector()->popSceneWithTransition(0.5f, PopTransition::kPopTransitionFade); +} + + +void BIViewLayer::onBack(CCObject* object) { + keyBackClicked(); +} + +void BIViewLayer::onPrev(CCObject* object) { + loadPage(--m_page); +} + +void BIViewLayer::onNext(CCObject* object) { + loadPage(++m_page); +} + +void BIViewLayer::onJumpToPageLayer(CCObject* sender){ + JumpToPageLayer::create(this)->show(); +} + +void BIViewLayer::onRandom(CCObject* sender){ + loadPage(BetterInfo::randomNumber(0, m_data->count() / resultsPerPage())); +} + +CCScene* BIViewLayer::scene() { + auto layer = BIViewLayer::create(); + auto scene = CCScene::create(); + scene->addChild(layer); + return scene; +} + +int BIViewLayer::getPage() const{ + return m_page; +} + +int BIViewLayer::resultsPerPage() const{ + return (GameManager::sharedState()->getGameVariable("0093")) ? 20 : 10; +} + +void BIViewLayer::keyDown(enumKeyCodes key){ + switch(key){ + case KEY_Left: + case CONTROLLER_Left: + if(m_prevBtn->isVisible() == true) onPrev(nullptr); + break; + case KEY_Right: + case CONTROLLER_Right: + if(m_nextBtn->isVisible() == true) onNext(nullptr); + break; + default: + CCLayer::keyDown(key); + } +} \ No newline at end of file diff --git a/src/layers/_bases/BIViewLayer.h b/src/layers/_bases/BIViewLayer.h new file mode 100644 index 0000000..266266b --- /dev/null +++ b/src/layers/_bases/BIViewLayer.h @@ -0,0 +1,33 @@ +#pragma once + +#include "CvoltonListView.h" +#include "BIBaseLayer.h" +#include "../../delegates/PageNumberDelegate.h" + +class BIViewLayer : public BIBaseLayer, public PageNumberDelegate { +protected: + const char* m_title = nullptr; + BoomListView* m_listView = nullptr; + GJListLayer* m_listLayer = nullptr; + cocos2d::CCArray* m_data = nullptr; + CCMenuItemSpriteExtra* m_prevBtn = nullptr; + CCMenuItemSpriteExtra* m_nextBtn = nullptr; + cocos2d::CCLabelBMFont* m_counter = nullptr; + ButtonSprite* m_pageBtnSprite = nullptr; + unsigned int m_page = 0; + + virtual bool init(); + virtual void keyBackClicked(); + void onBack(cocos2d::CCObject*); + void onPrev(cocos2d::CCObject*); + void onNext(cocos2d::CCObject*); + void onJumpToPageLayer(cocos2d::CCObject*); + void onRandom(cocos2d::CCObject*); + virtual int resultsPerPage() const; +public: + virtual void loadPage(unsigned int page); + static BIViewLayer* create(); + static cocos2d::CCScene* scene(); + virtual int getPage() const; + virtual void keyDown(cocos2d::enumKeyCodes key); +}; \ No newline at end of file