Skip to content

Commit

Permalink
use BIViewLayer for LeaderboardViewLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Dec 17, 2023
1 parent 455a2c1 commit a11606a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 111 deletions.
6 changes: 2 additions & 4 deletions src/layers/DailyHistory/DailyViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ bool DailyViewLayer::init(bool isWeekly) {
return true;
}

void DailyViewLayer::loadPage(unsigned int page){

m_page = page;
void DailyViewLayer::loadPage(){
auto displayedLevels = trimData();

m_listView = CvoltonListView<DailyCell>::create(displayedLevels, 356.f, 220.f);
m_title = m_isWeekly ? "Weekly Demons" : "Daily Levels";

BIViewLayer::loadPage(page);
BIViewLayer::loadPage();
}

void DailyViewLayer::onMore(CCObject* object) {
Expand Down
2 changes: 1 addition & 1 deletion src/layers/DailyHistory/DailyViewLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DailyViewLayer : public BIViewLayer {
virtual bool init(bool isWeekly);
void onMore(cocos2d::CCObject*);
public:
virtual void loadPage(unsigned int page);
virtual void loadPage();
static DailyViewLayer* create(bool isWeekly);
static bool compareDailies(const void* l1, const void* l2);
static cocos2d::CCScene* scene(bool isWeekly);
Expand Down
82 changes: 11 additions & 71 deletions src/layers/LeaderboardViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,59 +16,15 @@ LeaderboardViewLayer* LeaderboardViewLayer::create(int accountID) {
}

bool LeaderboardViewLayer::init(int accountID) {
BIViewLayer::init(false);

/*auto CM = CvoltonManager::sharedState();
CM->loadTextures();
CM->setOption("has_viewed_as", true);*/
m_accountID = accountID;

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(LeaderboardViewLayer::onBack)
);

backBtn->setPosition({(-winSize.width / 2) + 25, (winSize.height / 2) - 25});
menu->addChild(backBtn);

/*auto menuBack = CCMenu::create();
menuBack->addChild(backBtn);
menuBack->setPosition({25, winSize.height - 25});
addChild(menuBack);*/

setTouchEnabled(true);
setKeypadEnabled(true);

m_scores = CCArray::create();
m_scores->retain();

//corners
auto cornerBL = CCSprite::createWithSpriteFrameName("GJ_sideArt_001.png");
cornerBL->setPosition({0,0});
cornerBL->setAnchorPoint({0,0});
addChild(cornerBL, -1);
m_accountID = accountID;
m_title = "Global Leaderboards";

auto cornerBR = CCSprite::createWithSpriteFrameName("GJ_sideArt_001.png");
cornerBR->setPosition({winSize.width,0});
cornerBR->setAnchorPoint({0,0});
cornerBR->setRotation(270);
addChild(cornerBR, -1);
m_data = CCArray::create();
m_data->retain();

m_circle = LoadingCircle::create();
m_circle->retain();
Expand Down Expand Up @@ -97,35 +53,19 @@ bool LeaderboardViewLayer::init(int accountID) {
}

void LeaderboardViewLayer::loadPage(){
if(!m_scores) return;

auto winSize = CCDirector::sharedDirector()->getWinSize();
if(!m_data) return;

if(m_listLayer != nullptr) m_listLayer->removeFromParentAndCleanup(true);

if(m_leaderboardView) m_leaderboardView->release();
m_leaderboardView = CustomListView::create(m_scores, BoomListType::Score, 220.f, 356.f);
m_listLayer = GJListLayer::create(m_leaderboardView, "Global Leaderboards", {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 = CustomListView::create(m_data, BoomListType::Score, 220.f, 356.f);
BIViewLayer::loadPage();
}

void LeaderboardViewLayer::keyBackClicked() {
BetterInfoOnline::sharedState()->m_scoreDelegate = nullptr;

setTouchEnabled(false);
setKeypadEnabled(false);

if(m_scores) m_scores->release();
if(m_circle) m_circle->release();
m_scores = nullptr;
m_circle = nullptr;

CCDirector::sharedDirector()->popSceneWithTransition(0.5f, PopTransition::kPopTransitionFade);
}

void LeaderboardViewLayer::onBack(CCObject* object) {
keyBackClicked();
BIViewLayer::keyBackClicked();
}

void LeaderboardViewLayer::onRefresh(CCObject* object) {
Expand All @@ -150,8 +90,8 @@ CCScene* LeaderboardViewLayer::scene(int accountID) {
}

void LeaderboardViewLayer::onLeaderboardFinished(cocos2d::CCArray* scores) {
m_scores = scores;
m_scores->retain();
m_data = scores;
m_data->retain();
loadPage();
if(m_circle) m_circle->fadeAndRemove();
}
9 changes: 3 additions & 6 deletions src/layers/LeaderboardViewLayer.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
#pragma once

#include "../delegates/BILeaderboardDelegate.h"
#include "_bases/BIViewLayer.h"

class LeaderboardViewLayer : public cocos2d::CCLayer, public BILeaderboardDelegate {
CustomListView* m_leaderboardView = nullptr;
GJListLayer* m_listLayer = nullptr;
cocos2d::CCArray* m_scores = nullptr;
class LeaderboardViewLayer : public BIViewLayer, public BILeaderboardDelegate {
LoadingCircle* m_circle = nullptr;
int m_accountID = 0;
protected:
virtual bool init(int accountID);
virtual void keyBackClicked();
void onBack(cocos2d::CCObject*);
void onRefresh(cocos2d::CCObject*);
public:
void loadPage();
virtual void loadPage();
static LeaderboardViewLayer* create(int accountID);
static cocos2d::CCScene* scene(int accountID);

Expand Down
7 changes: 2 additions & 5 deletions src/layers/RewardHistory/RewardViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,12 @@ bool RewardViewLayer::init(CCDictionary* chests, const char* title) {
return true;
}

void RewardViewLayer::loadPage(unsigned int page){

m_page = page;
void RewardViewLayer::loadPage(){
auto displayedLevels = trimData();

m_listView = CvoltonListView<RewardCell>::create(displayedLevels, 356.f, 220.f, 45.f);

BIViewLayer::loadPage(page);

BIViewLayer::loadPage();
}

CCScene* RewardViewLayer::scene(CCDictionary* chests, const char* title) {
Expand Down
2 changes: 1 addition & 1 deletion src/layers/RewardHistory/RewardViewLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class RewardViewLayer : public BIViewLayer {
public:
static RewardViewLayer* create(cocos2d::CCDictionary* chests, const char* title);
static cocos2d::CCScene* scene(cocos2d::CCDictionary* chests, const char* title);
virtual void loadPage(unsigned int page);
virtual void loadPage();
static bool compareRewards(const void* l1, const void* l2);
};
43 changes: 24 additions & 19 deletions src/layers/_bases/BIViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include "../JumpToPageLayer.h"
#include "../../utils.hpp"

BIViewLayer* BIViewLayer::create() {
BIViewLayer* BIViewLayer::create(bool paginated) {
auto ret = new BIViewLayer();
if (ret && ret->init()) {
if (ret && ret->init(paginated)) {
ret->autorelease();
} else {
delete ret;
Expand All @@ -13,9 +13,11 @@ BIViewLayer* BIViewLayer::create() {
return ret;
}

bool BIViewLayer::init() {
bool BIViewLayer::init(bool paginated) {
BIBaseLayer::init();

m_paginated = paginated;

auto controllerConnected = BetterInfo::controllerConnected();
auto winSize = CCDirector::sharedDirector()->getWinSize();

Expand Down Expand Up @@ -67,34 +69,32 @@ bool BIViewLayer::init() {
m_randomBtn->setPosition({ (winSize.width / 2) - 23, (winSize.height / 2) - 72});
menu->addChild(m_randomBtn);

if(!paginated) {
m_pageBtn->setVisible(false);
m_randomBtn->setVisible(false);
m_counter->setVisible(false);
}

addChild(menu);

loadPage(0);
return true;
}

void BIViewLayer::loadPage(unsigned int page){
void BIViewLayer::loadPage(){
auto winSize = CCDirector::sharedDirector()->getWinSize();

const unsigned int count = resultsPerPage();
unsigned int firstIndex = page * count;
unsigned int lastIndex = (page+1) * count;

m_page = page;
unsigned int firstIndex = m_page * count;
unsigned int lastIndex = (m_page+1) * count;

if(page == 0) m_prevBtn->setVisible(false);
if(m_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);

if(m_data->count() > resultsPerPage()) m_pageBtn->setVisible(true);
else m_pageBtn->setVisible(false);

if(m_data->count() > resultsPerPage()) m_randomBtn->setVisible(true);
else m_randomBtn->setVisible(false);

m_pageBtnSprite->setString(std::to_string(page+1).c_str());
m_pageBtnSprite->setString(std::to_string(m_page+1).c_str());

if(m_listLayer != nullptr) m_listLayer->removeFromParentAndCleanup(true);

Expand All @@ -105,6 +105,11 @@ void BIViewLayer::loadPage(unsigned int page){
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::loadPage(unsigned int page){
m_page = page;
loadPage();
}

void BIViewLayer::keyBackClicked() {
setTouchEnabled(false);
setKeypadEnabled(false);
Expand Down Expand Up @@ -149,8 +154,8 @@ void BIViewLayer::onRandom(CCObject* sender){
loadPage(BetterInfo::randomNumber(0, m_data->count() / resultsPerPage()));
}

CCScene* BIViewLayer::scene() {
auto layer = BIViewLayer::create();
CCScene* BIViewLayer::scene(bool paginated) {
auto layer = BIViewLayer::create(paginated);
auto scene = CCScene::create();
scene->addChild(layer);
return scene;
Expand All @@ -161,7 +166,7 @@ int BIViewLayer::getPage() const{
}

int BIViewLayer::resultsPerPage() const{
return (GameManager::sharedState()->getGameVariable("0093")) ? 20 : 10;
return !m_paginated ? m_data->count() : ((GameManager::sharedState()->getGameVariable("0093")) ? 20 : 10);
}

void BIViewLayer::keyDown(enumKeyCodes key){
Expand Down
10 changes: 6 additions & 4 deletions src/layers/_bases/BIViewLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class BIViewLayer : public BIBaseLayer, public PageNumberDelegate {
cocos2d::CCLabelBMFont* m_counter = nullptr;
ButtonSprite* m_pageBtnSprite = nullptr;
unsigned int m_page = 0;
bool m_paginated = true;

virtual bool init();
virtual bool init(bool paginated = true);
virtual void keyBackClicked();
void onBack(cocos2d::CCObject*);
void onPrev(cocos2d::CCObject*);
Expand All @@ -28,9 +29,10 @@ class BIViewLayer : public BIBaseLayer, public PageNumberDelegate {
virtual int resultsPerPage() const;
CCArray* trimData();
public:
virtual void loadPage(unsigned int page);
static BIViewLayer* create();
static cocos2d::CCScene* scene();
virtual void loadPage();
void loadPage(unsigned int page);
static BIViewLayer* create(bool paginated = true);
static cocos2d::CCScene* scene(bool paginated = true);
virtual int getPage() const;
virtual void keyDown(cocos2d::enumKeyCodes key);
};

0 comments on commit a11606a

Please sign in to comment.