Skip to content

Commit

Permalink
use BIViewLayer for LSVL
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Dec 17, 2023
1 parent b23ed2e commit bc8d229
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 146 deletions.
166 changes: 40 additions & 126 deletions src/layers/LevelFiltering/LevelSearchViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,78 +39,19 @@ bool LevelSearchViewLayer::init(GJSearchObject* gjSearchObj, BISearchObject sear
}

bool LevelSearchViewLayer::init(BISearchObject searchObj) {
BIViewLayer::init();

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

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

setTouchEnabled(true);
setKeypadEnabled(true);

//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);

m_statusText = CCLabelBMFont::create("Waiting", "goldFont.fnt");
m_statusText->setPosition({winSize.width / 2, winSize.height / 2 - 147});
m_statusText->setScale(.7f);
addChild(m_statusText);

auto prevSprite = CCSprite::createWithSpriteFrameName(controllerConnected ? "controllerBtn_DPad_Left_001.png" : "GJ_arrow_03_001.png");
m_prevBtn = CCMenuItemSpriteExtra::create(
prevSprite,
this,
menu_selector(LevelSearchViewLayer::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(LevelSearchViewLayer::onNext)
);
m_nextBtn->setPosition({+ (winSize.width / 2) - 25, 0});
menu->addChild(m_nextBtn);

m_counter = CCLabelBMFont::create("0 to 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);

m_searchObj = searchObj;

auto menu = CCMenu::create();

auto buttonSprite = ButtonSprite::create("Filters", (int)(90*0.5), true, "bigFont.fnt", "GJ_button_01.png", 44*0.5f, 0.5f);
auto buttonButton = CCMenuItemSpriteExtra::create(
buttonSprite,
Expand All @@ -131,6 +72,8 @@ bool LevelSearchViewLayer::init(BISearchObject searchObj) {
infoBtn->setPosition({+ (winSize.width / 2) - 25, - (winSize.height / 2) + 25});
menu->addChild(infoBtn);

addChild(menu);

reload();

if(m_gjSearchObj && !(Mod::get()->getSavedValue<bool>("lsvl_seen_info"))) showInfoDialog();
Expand All @@ -155,33 +98,30 @@ void LevelSearchViewLayer::unload() {

if(m_gjSearchObj) m_gjSearchObj->m_page = 0;

if(!m_loadedLevels) return;

for(size_t i = 0; i < m_loadedLevels->count(); i++) {
auto level = static_cast<GJGameLevel*>(m_loadedLevels->objectAtIndex(i));
level->release();
}
if(!m_data) return;

m_loadedLevels->release();
m_loadedLevels = nullptr;
m_data->release();
m_data = nullptr;
}

void LevelSearchViewLayer::reload() {
unload();

resetUnloadedLevels();

m_loadedLevels = CCArray::create();
m_loadedLevels->retain();
m_data = CCArray::create();
m_data->retain();

loadPage(true);
startLoading();
}

void LevelSearchViewLayer::startLoading(){
if(!m_data) return;

if(!m_gjSearchObjOptimized && m_gjSearchObj) optimizeSearchObject();

if((m_unloadedLevels.empty() && !m_gjSearchObjOptimized) || (m_page + 2) * 10 < m_loadedLevels->count() || (m_gjSearchObjOptimized && m_gjSearchObjOptimized->m_searchType == SearchType::MapPackOnClick && m_gjSearchObjOptimized->m_page > 0)) {
if((m_unloadedLevels.empty() && !m_gjSearchObjOptimized) || (m_page + 2) * 10 < m_data->count() || (m_gjSearchObjOptimized && m_gjSearchObjOptimized->m_searchType == SearchType::MapPackOnClick && m_gjSearchObjOptimized->m_page > 0)) {
setTextStatus(true);
return;
}
Expand Down Expand Up @@ -234,45 +174,43 @@ void LevelSearchViewLayer::queueLoad(float dt) {
}

void LevelSearchViewLayer::loadPage(bool reload){
if(!m_loadedLevels) return;
if(!m_data) return;

auto currentPage = CCArray::create();
auto end = (m_page + 1) * 10;
end = m_loadedLevels->count() < end ? m_loadedLevels->count() : end;
for(size_t i = m_page * 10; i < end; i++) {
currentPage->addObject(m_loadedLevels->objectAtIndex(i));
}
auto currentPage = trimData();

m_firstIndex = (m_page * 10) + 1;
m_lastIndex = (m_page * 10) + currentPage->count();
m_totalAmount = m_unloadedLevels.size() + m_loadedLevels->count();
m_counter->setCString(fmt::format("{} to {} of {} / {}", m_firstIndex, m_lastIndex, m_loadedLevels->count(), m_totalAmount).c_str());
m_totalAmount = m_unloadedLevels.size() + m_data->count();

if(m_page == 0) m_prevBtn->setVisible(false);
else m_prevBtn->setVisible(true);

if(m_loadedLevels->count() > m_lastIndex) m_nextBtn->setVisible(true);
else m_nextBtn->setVisible(false);
updateCounter();

if(!reload && m_shownLevels == currentPage->count()) return;
m_shownLevels = currentPage->count();

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

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

m_listView = CustomListView::create(currentPage, BoomListType::Level, 220.f, 356.f);
m_listLayer = GJListLayer::create(m_listView, "Levels", {191, 114, 62, 255}, 356.f, 220.f);
m_listLayer->setPosition(winSize / 2 - m_listLayer->getScaledContentSize() / 2 - CCPoint(0,1));
addChild(m_listLayer);
BIViewLayer::loadPage();
}

void LevelSearchViewLayer::updateCounter() {
if(!m_data) return;

BIViewLayer::updateCounter();

m_counter->setCString(fmt::format("{} to {} of {} / {}", m_firstIndex, m_lastIndex, m_data->count(), m_totalAmount).c_str());
}

void LevelSearchViewLayer::loadPage() {
loadPage(true);
startLoading();
}

void LevelSearchViewLayer::keyBackClicked() {
setTouchEnabled(false);
setKeypadEnabled(false);
unload();
if(m_gjSearchObj) m_gjSearchObj->release();
CCDirector::sharedDirector()->popSceneWithTransition(0.5f, PopTransition::kPopTransitionFade);

BIViewLayer::keyBackClicked();
}


Expand All @@ -295,14 +233,13 @@ CCScene* LevelSearchViewLayer::scene(GJSearchObject* gjSearchObj, BISearchObject
}

void LevelSearchViewLayer::loadListFinished(cocos2d::CCArray* levels, const char*) {
if(!m_loadedLevels) return;
if(!m_data) return;

for(size_t i = 0; i < levels->count(); i++) {
auto level = static_cast<GJGameLevel*>(levels->objectAtIndex(i));
if(level == nullptr) continue;

level->retain();
if(BetterInfo::levelMatchesObject(level, m_searchObj)) m_loadedLevels->addObject(level);
if(BetterInfo::levelMatchesObject(level, m_searchObj)) m_data->addObject(level);
}

loadPage(false);
Expand All @@ -326,22 +263,10 @@ void LevelSearchViewLayer::setTextStatus(bool finished) {
if(m_statusText) m_statusText->setString(
finished ? "Finished" :
m_gjSearchObjOptimized ? fmt::format("Loading (online page {})", m_gjSearchObjOptimized->m_page).c_str() :
(m_loadedLevels->count() > m_lastIndex ? "Loading (next page)" : "Loading (current page)")
(m_data->count() > m_lastIndex ? "Loading (next page)" : "Loading (current page)")
);
}

void LevelSearchViewLayer::onPrev(cocos2d::CCObject*) {
if(m_page == 0) return;
m_page--;
loadPage(true);
}

void LevelSearchViewLayer::onNext(cocos2d::CCObject*) {
m_page++;
loadPage(true);
startLoading();
}

void LevelSearchViewLayer::onFilters(cocos2d::CCObject*) {
auto searchOptions = ProfileSearchOptions::create(nullptr, "", this);
searchOptions->setSearchObject(m_searchObj);
Expand All @@ -352,21 +277,6 @@ void LevelSearchViewLayer::onInfo(cocos2d::CCObject*) {
LevelSearchViewLayer::showInfoDialog();
}

void LevelSearchViewLayer::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);
}
}

void LevelSearchViewLayer::onSearchObjectFinished(const BISearchObject& searchObj) {
m_searchObj = searchObj;
reload();
Expand Down Expand Up @@ -457,4 +367,8 @@ void LevelSearchViewLayer::resetUnloadedLevels() {
m_unloadedLevels.push_back(level);
}
}
}

int LevelSearchViewLayer::resultsPerPage() const {
return 10;
}
16 changes: 5 additions & 11 deletions src/layers/LevelFiltering/LevelSearchViewLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@
#include <deque>
#include "../../delegates/BISearchObjectDelegate.h"
#include "../../objects/BISearchObject.h"
#include "../_bases/BIViewLayer.h"
#include <Geode/Bindings.hpp>

using namespace geode::prelude;

class LevelSearchViewLayer : public cocos2d::CCLayer, public OnlineListDelegate, public BISearchObjectDelegate {
class LevelSearchViewLayer : public BIViewLayer, public OnlineListDelegate, public BISearchObjectDelegate {
BISearchObject m_searchObj;
GJSearchObject* m_gjSearchObj = nullptr;
GJSearchObject* m_gjSearchObjOptimized = nullptr;
GJSearchObject* m_gjSearchObjLoaded = nullptr;
CustomListView* m_listView = nullptr;
GJListLayer* m_listLayer = nullptr;
CCMenuItemSpriteExtra* m_prevBtn = nullptr;
CCMenuItemSpriteExtra* m_nextBtn = nullptr;
cocos2d::CCLabelBMFont* m_statusText = nullptr;
cocos2d::CCLabelBMFont* m_counter = nullptr;
cocos2d::CCArray* m_loadedLevels = nullptr;
std::deque<GJGameLevel*> m_unloadedLevels;
std::deque<GJGameLevel*> m_allLevels;
size_t m_page = 0;
size_t m_shownLevels = 0;
size_t m_firstIndex = 0;
size_t m_lastIndex = 0;
Expand All @@ -30,23 +24,23 @@ class LevelSearchViewLayer : public cocos2d::CCLayer, public OnlineListDelegate,
virtual bool init(GJSearchObject* gjSearchObj, BISearchObject searchObj = BISearchObject());
virtual bool init(BISearchObject searchObj = BISearchObject());
virtual void keyBackClicked();
virtual void updateCounter();
void onBack(cocos2d::CCObject*);
void unload();
void reload();
void startLoading();
void setTextStatus(bool finished);
int resultsPerPage() const;
public:
void loadPage(bool reload);
void loadPage();
static LevelSearchViewLayer* create(std::deque<GJGameLevel*> allLevels, BISearchObject searchObj = BISearchObject());
static LevelSearchViewLayer* create(GJSearchObject* gjSearchObj, BISearchObject searchObj = BISearchObject());
static cocos2d::CCScene* scene(std::deque<GJGameLevel*> allLevels, BISearchObject searchObj = BISearchObject());
static cocos2d::CCScene* scene(GJSearchObject* gjSearchObj, BISearchObject searchObj = BISearchObject());

void onPrev(cocos2d::CCObject*);
void onNext(cocos2d::CCObject*);
void onFilters(cocos2d::CCObject*);
void onInfo(cocos2d::CCObject*);
void keyDown(cocos2d::enumKeyCodes key);

void loadListFinished(cocos2d::CCArray*, const char*);
void loadListFailed(const char*);
Expand Down
24 changes: 15 additions & 9 deletions src/layers/_bases/BIViewLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ void BIViewLayer::loadPage(){

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

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

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

m_listLayer = GJListLayer::create(m_listView, m_title.c_str(), {191, 114, 62, 255}, 356.f, 220.f);
m_listLayer->setPosition(winSize / 2 - m_listLayer->getScaledContentSize() / 2 - CCPoint(0,5));
addChild(m_listLayer);

updateCounter();
}

void BIViewLayer::updateCounter(){
if(!m_data) return;

const unsigned int count = resultsPerPage();
unsigned int firstIndex = m_page * count;
unsigned int lastIndex = (m_page+1) * count;
Expand All @@ -96,14 +110,6 @@ void BIViewLayer::loadPage(){
if(m_data->count() > lastIndex) m_nextBtn->setVisible(true);
else m_nextBtn->setVisible(false);

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

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

m_listLayer = GJListLayer::create(m_listView, m_title.c_str(), {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());
}

Expand All @@ -116,7 +122,7 @@ void BIViewLayer::keyBackClicked() {
setTouchEnabled(false);
setKeypadEnabled(false);

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

Expand Down
1 change: 1 addition & 0 deletions src/layers/_bases/BIViewLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BIViewLayer : public BIBaseLayer, public PageNumberDelegate {

virtual bool init(bool paginated = true);
virtual void keyBackClicked();
virtual void updateCounter();
void onBack(cocos2d::CCObject*);
void onPrev(cocos2d::CCObject*);
void onNext(cocos2d::CCObject*);
Expand Down

0 comments on commit bc8d229

Please sign in to comment.