Skip to content

Commit

Permalink
begin CvoltonOptionsLayer refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Jan 16, 2024
1 parent 181a56f commit 807aa3b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 107 deletions.
32 changes: 14 additions & 18 deletions src/layers/LevelFiltering/CvoltonSearchOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,18 @@ void CvoltonSearchOptions::createToggle(const char* option, const char* name, fl
menu_selector(CvoltonSearchOptions::onToggle)
);
button->setID(Mod::get()->expandSpriteName(fmt::format("{}-toggle", option).c_str()));
m_buttonMenu->addChild(button);
button->setPosition({x, y});
m_buttonMenu->addChild(button);
m_toggles.push_back(button);

auto optionString = CCString::create(option);
optionString->retain();
button->setUserData(optionString);
button->setSizeMult(1.2f);
button->setUserObject(optionString);

auto label = createTextLabel(name, {x + 20, y}, 0.5f, m_buttonMenu);
label->setAnchorPoint({0,0.5f});
label->limitLabelWidth(80, 0.5f, 0);
label->setID(Mod::get()->expandSpriteName(fmt::format("{}-label", option).c_str()));
}

void CvoltonSearchOptions::destroyToggles(){
//starting at 1 because 0 is the close button and 1 is the prev button
unsigned int totalMembers = m_buttonMenu->getChildrenCount();
for(unsigned int i = 3; i < totalMembers; i++){
//static index 1 because we're actively moving the elements
auto object = static_cast<CCNode*>(m_buttonMenu->getChildren()->objectAtIndex(3));
auto userData = object->getUserData();
if(userData != nullptr) static_cast<CCString*>(userData)->release();
//m_buttonMenu->removeChild(object, false);
object->removeFromParent();
}
m_toggleCount = 0;
m_toggles.push_back(label);
}

void CvoltonSearchOptions::drawToggles(){
Expand All @@ -99,16 +86,25 @@ void CvoltonSearchOptions::drawToggles(){

auto completedMode = createTextLabel("Completed Mode:", {0, -95}, 0.5f, m_buttonMenu, "goldFont.fnt");
completedMode->setID("completed-mode"_spr);
m_toggles.push_back(completedMode);

auto completedLeft = createButton("edit_leftBtn_001.png", {-120, -120}, menu_selector(CvoltonSearchOptions::onCompletedPrev), 1.2f);
completedLeft->setID("completed-left-button"_spr);
m_toggles.push_back(completedLeft);

auto label = createTextLabel(getCompletedString(), {0, -120}, 1, m_buttonMenu, "bigFont.fnt");
label->limitLabelWidth(200, 0.8f, 0);
label->setID("completed-mode-label"_spr);
m_toggles.push_back(label);

auto completedRight = createButton("edit_rightBtn_001.png", {120, -120}, menu_selector(CvoltonSearchOptions::onCompletedNext), 1.2f);
completedRight->setID("completed-right-button"_spr);
m_toggles.push_back(completedRight);

if(static_cast<CompleteMode>(getOptionInt("search_completed")) == CompleteMode::percentage) {
auto plus = createButton("GJ_plusBtn_001.png", {196, -120}, menu_selector(CvoltonSearchOptions::onPercentageRange), .75f);
plus->setID("percentage-plus-button"_spr);
m_toggles.push_back(plus);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/layers/LevelFiltering/CvoltonSearchOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class CvoltonSearchOptions : public CvoltonOptionsLayer, public IDRangeDelegate
void onPrev(cocos2d::CCObject* sender);
void onInfo(cocos2d::CCObject* sender);
bool init();
void destroyToggles();
void drawToggles();
void createToggle(const char* option, const char* name, float x, float y);
void onCompletedPrev(cocos2d::CCObject* sender);
Expand Down
2 changes: 1 addition & 1 deletion src/layers/LevelFiltering/IDRangePopup.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once
#include "../_bases/CvoltonOptionsLayer.h"
#include "../_bases/CvoltonAlertLayerStub.h"
#include "../../delegates/IDRangeDelegate.h"

class IDRangePopup : public CvoltonAlertLayerStub {
Expand Down
53 changes: 12 additions & 41 deletions src/layers/LevelFiltering/ProfileSearchOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,6 @@ void ProfileSearchOptions::reloadBrowser(){
if(m_levelBrowserLayer != nullptr) m_levelBrowserLayer->loadPage(m_levelBrowserLayer->m_searchObject);
}

void ProfileSearchOptions::createToggle(const char* option, const char* name, float x, float y){
auto buttonSprite = CCSprite::createWithSpriteFrameName(getOption(option) ? "GJ_checkOn_001.png" : "GJ_checkOff_001.png");
buttonSprite->setScale(0.8f);
auto button = CCMenuItemSpriteExtra::create(
buttonSprite,
this,
menu_selector(ProfileSearchOptions::onToggle)
);
button->setID(Mod::get()->expandSpriteName(fmt::format("{}-toggle", option).c_str()));
m_buttonMenu->addChild(button);
button->setPosition({x, y});
auto optionString = CCString::create(option);
optionString->retain();
button->setUserData(optionString);
button->setSizeMult(1.2f);

auto label = createTextLabel(name, {x + 20, y}, 0.5f, m_buttonMenu);
label->setAnchorPoint({0,0.5f});
label->limitLabelWidth(80, 0.5f, 0);
label->setID(Mod::get()->expandSpriteName(fmt::format("{}-label", option).c_str()));
}

void ProfileSearchOptions::createToggle(const char* option, const char* name, float x, float y, SEL_MenuHandler additional){
auto buttonSprite = CCSprite::createWithSpriteFrameName(getOption(option) ? "GJ_checkOn_001.png" : "GJ_checkOff_001.png");
buttonSprite->setScale(0.8f);
Expand All @@ -174,33 +152,23 @@ void ProfileSearchOptions::createToggle(const char* option, const char* name, fl
menu_selector(ProfileSearchOptions::onToggle)
);
button->setID(Mod::get()->expandSpriteName(fmt::format("{}-toggle", option).c_str()));
m_buttonMenu->addChild(button);
button->setPosition({x, y});
m_buttonMenu->addChild(button);
m_toggles.push_back(button);

auto optionString = CCString::create(option);
optionString->retain();
button->setUserData(optionString);
button->setSizeMult(1.2f);
button->setUserObject(optionString);

auto label = createTextLabel(name, {x + 20, y}, 0.5f, m_buttonMenu);
label->setAnchorPoint({0,0.5f});
label->limitLabelWidth(60, 0.5f, 0);
label->setID(Mod::get()->expandSpriteName(fmt::format("{}-label", option).c_str()));
m_toggles.push_back(label);

if(getOption(option)) createButton("GJ_plusBtn_001.png", {x + 98, y}, additional, .65f);
}

void ProfileSearchOptions::destroyToggles(){
//starting at 1 because 0 is the close button and 1 is the prev button
unsigned int totalMembers = m_buttonMenu->getChildrenCount();
for(unsigned int i = 6; i < totalMembers; i++){
//static index 1 because we're actively moving the elements
auto object = static_cast<CCNode*>(m_buttonMenu->getChildren()->objectAtIndex(6));
auto userData = object->getUserData();
if(userData != nullptr) static_cast<CCString*>(userData)->release();
//m_buttonMenu->removeChild(object, false);
object->removeFromParent();
if(getOption(option) && additional) {
auto button = createButton("GJ_plusBtn_001.png", {x + 98, y}, additional, .65f);
m_toggles.push_back(button);
}
m_toggleCount = 0;
}

void ProfileSearchOptions::drawToggles(){
Expand Down Expand Up @@ -231,8 +199,10 @@ void ProfileSearchOptions::drawTogglesPrimary(){

auto timeIcon = CCSprite::createWithSpriteFrameName("GJ_timeIcon_001.png");
timeIcon->setID("time-icon"_spr);
m_buttonMenu->addChild(timeIcon);
timeIcon->setPosition({-193, -120});
m_buttonMenu->addChild(timeIcon);
m_toggles.push_back(timeIcon);

createButtonToggle("len_00", CCLabelBMFont::create("Tiny", "bigFont.fnt"), -142, -119, 0.6f);
createButtonToggle("len_01", CCLabelBMFont::create("Short", "bigFont.fnt"), -69, -119, 0.6f);
createButtonToggle("len_02", CCLabelBMFont::create("Medium", "bigFont.fnt"), 16, -119, 0.6f);
Expand Down Expand Up @@ -287,6 +257,7 @@ void ProfileSearchOptions::drawTogglesSecondary(){

auto infoBtn = createButton("GJ_infoIcon_001.png", {203, 128}, menu_selector(ProfileSearchOptions::onSecondaryInfo));
infoBtn->setID("info-button"_spr);
m_toggles.push_back(infoBtn);

createToggle("copied", "Copied", -170, 80);
createToggle("downloaded", "Downloaded", -40, 80);
Expand Down
8 changes: 3 additions & 5 deletions src/layers/LevelFiltering/ProfileSearchOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
using namespace geode::prelude;

class ProfileSearchOptions : public CvoltonOptionsLayer, public SongDialogCloseDelegate, public IDRangeDelegate {
std::map<std::string, bool> m_options;
std::map<std::string, int> m_optionInts;
std::unordered_map<std::string, bool> m_options;
std::unordered_map<std::string, int> m_optionInts;
LevelBrowserLayer* m_levelBrowserLayer = nullptr;
CCMenuItemSpriteExtra* m_prevBtn = nullptr;
CCMenuItemSpriteExtra* m_nextBtn = nullptr;
Expand All @@ -35,13 +35,11 @@ class ProfileSearchOptions : public CvoltonOptionsLayer, public SongDialogCloseD
void onSecondaryInfo(cocos2d::CCObject* sender);
bool init(LevelBrowserLayer* levelBrowserLayer = nullptr, const std::string& prefix = "", BISearchObjectDelegate* searchObjDelegate = nullptr);
void reloadBrowser();
void destroyToggles();
void drawToggles();
void drawTogglesPrimary();
void drawTogglesSecondary();
void drawTogglesTerciary();
void createToggle(const char* option, const char* name, float x, float y);
void createToggle(const char* option, const char* name, float x, float y, cocos2d::SEL_MenuHandler additional);
void createToggle(const char* option, const char* name, float x, float y, cocos2d::SEL_MenuHandler additional = nullptr);
void onSongDialogClosed(bool custom, int songID);
void onIDRangeFinished(int min, int max, int additional);
bool getOption(const std::string& option) const;
Expand Down
28 changes: 7 additions & 21 deletions src/layers/LevelFiltering/ProfileSearchOptionsSongSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ bool ProfileSearchOptionsSongSelect::init(SongDialogCloseDelegate* delegate){
auto infoBg = cocos2d::extension::CCScale9Sprite::create("square02b_001.png", { 0.0f, 0.0f, 80.0f, 80.0f });
infoBg->setContentSize({200,60});
infoBg->setColor({123,60,31});
m_buttonMenu->addChild(infoBg, -1);
infoBg->setPosition({0,-37});
infoBg->setScale(0.6f);
infoBg->setID("song-id-bg"_spr);
//createTextLabel("Advanced Options", {(winSize.width / 2), (winSize.height / 2) + 125}, 1.f, m_pLayer, "bigFont.fnt");
m_buttonMenu->addChild(infoBg, -1);

drawToggles();

Expand All @@ -64,31 +63,18 @@ void ProfileSearchOptionsSongSelect::createToggle(const char* option, const char
menu_selector(ProfileSearchOptionsSongSelect::onToggle)
);
button->setID(Mod::get()->expandSpriteName(fmt::format("{}-toggle", option).c_str()));
m_buttonMenu->addChild(button);
button->setPosition({x, y});
m_buttonMenu->addChild(button);
m_toggles.push_back(button);

auto optionString = CCString::create(option);
optionString->retain();
button->setUserData(optionString);
button->setSizeMult(1.2f);
button->setUserObject(optionString);

auto label = createTextLabel(name, {x + 20, y}, 0.5f, m_buttonMenu);
label->setAnchorPoint({0,0.5f});
label->limitLabelWidth(80, 0.5f, 0);
label->setID(Mod::get()->expandSpriteName(fmt::format("{}-label", option).c_str()));
}

void ProfileSearchOptionsSongSelect::destroyToggles(){
//starting at 1 because 0 is the close button and 1 is the prev button
unsigned int totalMembers = m_buttonMenu->getChildrenCount();
for(unsigned int i = 5; i < totalMembers; i++){
//static index 1 because we're actively moving the elements
auto object = static_cast<CCNode*>(m_buttonMenu->getChildren()->objectAtIndex(5));
auto userData = object->getUserData();
if(userData != nullptr) static_cast<CCString*>(userData)->release();
//m_buttonMenu->removeChild(object, false);
object->removeFromParent();
}
m_toggleCount = 0;
m_toggles.push_back(label);
}

void ProfileSearchOptionsSongSelect::drawToggles(){
Expand All @@ -106,8 +92,8 @@ void ProfileSearchOptionsSongSelect::drawToggles(){
m_savedBtn->setPosition({88, -39});
m_savedBtn->setID("saved-button"_spr);
m_savedBtn->setVisible(getOption("user_search_song_custom"));
m_savedBtn->setVisible(getOption("user_search_song_custom"));
m_buttonMenu->addChild(m_savedBtn);
m_toggles.push_back(m_savedBtn);
}

int ProfileSearchOptionsSongSelect::songID(){
Expand Down
1 change: 0 additions & 1 deletion src/layers/LevelFiltering/ProfileSearchOptionsSongSelect.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class ProfileSearchOptionsSongSelect : public CvoltonOptionsLayer, public GJDrop
static ProfileSearchOptionsSongSelect* create(SongDialogCloseDelegate* delegate);
void onClose(cocos2d::CCObject* sender);
bool init(SongDialogCloseDelegate* delegate);
void destroyToggles();
void drawToggles();
int songID();
void createToggle(const char* option, const char* name, float x, float y);
Expand Down
33 changes: 14 additions & 19 deletions src/layers/_bases/CvoltonOptionsLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ void CvoltonOptionsLayer::onToggle(cocos2d::CCObject* sender)
{
sender->retain();
auto button = static_cast<CCMenuItemSpriteExtra*>(sender);
toggleOption(static_cast<CCString*>(button->getUserData())->getCString());
toggleOption(static_cast<CCString*>(button->getUserObject())->getCString());

destroyToggles();
drawToggles();
sender->release();
}

void CvoltonOptionsLayer::createToggle(const char* option, const char* name){
float y = 85.f - (m_toggleCount++ * 40.f);

auto buttonSprite = CCSprite::createWithSpriteFrameName(getOption(option) ? "GJ_checkOn_001.png" : "GJ_checkOff_001.png");
buttonSprite->setScale(0.8f);
Expand All @@ -60,17 +61,17 @@ void CvoltonOptionsLayer::createToggle(const char* option, const char* name){
menu_selector(CvoltonOptionsLayer::onToggle)
);
button->setID(Mod::get()->expandSpriteName(fmt::format("{}-toggle", option).c_str()));
m_buttonMenu->addChild(button);
float y = 85.f - (m_toggleCount++ * 40.f);
button->setPosition({-157, y});
m_buttonMenu->addChild(button);
m_toggles.push_back(button);

auto optionString = CCString::create(option);
optionString->retain();
button->setUserData(optionString);
button->setSizeMult(1.2f);
button->setUserObject(optionString);

auto label = createTextLabel(name, {-137, y}, 0.5f, m_buttonMenu);
label->setAnchorPoint({0,0.5f});
label->setID(Mod::get()->expandSpriteName(fmt::format("{}-label", option).c_str()));
m_toggles.push_back(label);
}

void CvoltonOptionsLayer::createButtonToggle(const char* option, CCNode* sprite, float x, float y, float scale){
Expand All @@ -82,26 +83,20 @@ void CvoltonOptionsLayer::createButtonToggle(const char* option, CCNode* sprite,
menu_selector(CvoltonOptionsLayer::onToggle)
);
button->setID(Mod::get()->expandSpriteName(fmt::format("{}-toggle", option).c_str()));
m_buttonMenu->addChild(button);
button->setPosition({x, y});
if(!getOption(option)) button->setColor({125,125,125});
m_buttonMenu->addChild(button);
m_toggles.push_back(button);

auto optionString = CCString::create(option);
optionString->retain();
button->setUserData(optionString);
button->setSizeMult(1.2f);
button->setUserObject(optionString);
}

void CvoltonOptionsLayer::destroyToggles(){
//starting at 1 because 0 is the close button
unsigned int totalMembers = m_buttonMenu->getChildrenCount();
for(unsigned int i = 1; i < totalMembers; i++){
//static index 1 because we're actively moving the elements
auto object = static_cast<CCNode*>(m_buttonMenu->getChildren()->objectAtIndex(1));
auto userData = object->getUserData();
if(userData != nullptr) static_cast<CCString*>(userData)->release();
//m_buttonMenu->removeChild(object, false);
object->removeFromParent();
for(auto toggle : m_toggles) {
toggle->removeFromParent();
}
m_toggles.clear();
m_toggleCount = 0;
}

Expand Down
1 change: 1 addition & 0 deletions src/layers/_bases/CvoltonOptionsLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class CvoltonOptionsLayer : public CvoltonAlertLayerStub {
protected:
int m_toggleCount = 0;
std::vector<CCNode*> m_toggles;
public:
static CvoltonOptionsLayer* create();
void onClose(cocos2d::CCObject* sender);
Expand Down

0 comments on commit 807aa3b

Please sign in to comment.