Skip to content

Commit

Permalink
add BIViewLayer and upgrade DailyViewLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Dec 16, 2023
1 parent c375978 commit 5756355
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 172 deletions.
175 changes: 20 additions & 155 deletions src/layers/DailyHistory/DailyViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,108 +20,30 @@ 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<GJGameLevel*>(obj->getObject());
if(
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(
Expand All @@ -134,6 +56,8 @@ bool DailyViewLayer::init(bool isWeekly) {
buttonButton->setAnchorPoint({0,0});
menu->addChild(buttonButton);

addChild(menu);

loadPage(0);
return true;
}
Expand All @@ -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<DailyCell>::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<DailyCell>::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) {
Expand All @@ -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);
}
}
19 changes: 2 additions & 17 deletions src/layers/DailyHistory/DailyViewLayer.h
Original file line number Diff line number Diff line change
@@ -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);
};
Loading

0 comments on commit 5756355

Please sign in to comment.