refactor UI code to be more dry + move restart to developer rather than replacing the view menu

This commit is contained in:
HJfod 2024-02-29 11:21:18 +02:00
parent 1eab0bcb27
commit da2bf3e0d3
7 changed files with 103 additions and 55 deletions

View file

@ -0,0 +1,49 @@
#include "GeodeStyle.hpp"
#include <Geode/utils/cocos.hpp>
bool GeodeButtonSprite::init(CCSprite* top, bool* state) {
if (!CCSprite::initWithFile("GE_button_05.png"_spr))
return false;
m_stateSrc = state;
limitNodeSize(top, m_obContentSize * .65f, 2.f, .1f);
this->addChildAtPosition(top, Anchor::Center);
// Only schedule update if there is a need to do so
if (state) {
this->scheduleUpdate();
}
return true;
}
void GeodeButtonSprite::update(float dt) {
CCSprite::update(dt);
if (m_stateSrc && m_state != *m_stateSrc) {
m_state = *m_stateSrc;
this->setTexture(CCTextureCache::get()->addImage(
(m_state ? "GJ_button_02.png" : "GE_button_05.png"_spr), true
));
}
}
GeodeButtonSprite* GeodeButtonSprite::create(const char* top, bool* state) {
auto ret = new GeodeButtonSprite();
if (ret && ret->init(CCSprite::create(top), state)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
GeodeButtonSprite* GeodeButtonSprite::createWithSpriteFrameName(const char* top, bool* state) {
auto ret = new GeodeButtonSprite();
if (ret && ret->init(CCSprite::createWithSpriteFrameName(top), state)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}

View file

@ -0,0 +1,19 @@
#pragma once
#include <Geode/DefaultInclude.hpp>
using namespace geode::prelude;
class GeodeButtonSprite : public CCSprite {
protected:
bool* m_stateSrc = nullptr;
bool m_state = false;
bool init(CCSprite* top, bool* state);
void update(float dt) override;
public:
static GeodeButtonSprite* create(const char* top, bool* state = nullptr);
static GeodeButtonSprite* createWithSpriteFrameName(const char* top, bool* state = nullptr);
};

View file

@ -14,20 +14,25 @@ bool BaseModItem::init() {
m_title->setAnchorPoint({ .0f, .5f }); m_title->setAnchorPoint({ .0f, .5f });
this->addChild(m_title); this->addChild(m_title);
auto by = "By " + ModMetadata::formatDeveloperDisplayString(meta.getDevelopers());
auto developersBtn = CCMenuItemSpriteExtra::create(
CCLabelBMFont::create(by.c_str(), "goldFont.fnt"),
this, nullptr
);
m_developers = CCMenu::create(); m_developers = CCMenu::create();
m_developers->ignoreAnchorPointForPosition(false); m_developers->ignoreAnchorPointForPosition(false);
m_developers->setContentSize(developersBtn->getScaledContentSize());
m_developers->addChildAtPosition(developersBtn, Anchor::Center);
m_developers->setAnchorPoint({ .0f, .5f }); m_developers->setAnchorPoint({ .0f, .5f });
m_developers->setLayout(
RowLayout::create()
->setAxisAlignment(AxisAlignment::Start)
);
this->addChild(m_developers); this->addChild(m_developers);
m_viewMenu = CCMenu::create(); m_viewMenu = CCMenu::create();
m_viewMenu->setAnchorPoint({ 1.f, .5f }); m_viewMenu->setAnchorPoint({ 1.f, .5f });
m_viewMenu->setScale(.55f);
auto viewBtn = CCMenuItemSpriteExtra::create(
ButtonSprite::create("View", "bigFont.fnt", "GE_button_05.png"_spr, .8f),
this, nullptr
);
m_viewMenu->addChild(viewBtn);
m_viewMenu->setLayout( m_viewMenu->setLayout(
RowLayout::create() RowLayout::create()
->setAxisReverse(true) ->setAxisReverse(true)
@ -41,24 +46,19 @@ bool BaseModItem::init() {
} }
void BaseModItem::updateState() { void BaseModItem::updateState() {
m_viewMenu->removeAllChildren(); m_developers->removeAllChildren();
if (this->wantsRestart()) { if (this->wantsRestart()) {
auto restartSpr = ButtonSprite::create("Restart", "bigFont.fnt", "GE_button_02.png"_spr, .8f); m_developers->addChild(ButtonSprite::create("Restart Required", "goldFont.fnt", "black-square"_spr, .8f));
restartSpr->setScale(.55f);
auto restartBtn = CCMenuItemSpriteExtra::create(
restartSpr, this, nullptr
);
m_viewMenu->addChild(restartBtn);
} }
else { else {
auto viewSpr = ButtonSprite::create("View", "bigFont.fnt", "GE_button_05.png"_spr, .8f); auto by = "By " + ModMetadata::formatDeveloperDisplayString(this->getMetadata().getDevelopers());
viewSpr->setScale(.55f); auto developersBtn = CCMenuItemSpriteExtra::create(
auto viewBtn = CCMenuItemSpriteExtra::create( CCLabelBMFont::create(by.c_str(), "goldFont.fnt"),
viewSpr, this, nullptr this, nullptr
); );
m_viewMenu->addChild(viewBtn); m_developers->addChild(developersBtn);
} }
m_viewMenu->updateLayout(); m_developers->updateLayout();
} }
void BaseModItem::updateSize(float width, bool big) { void BaseModItem::updateSize(float width, bool big) {
@ -74,8 +74,12 @@ void BaseModItem::updateSize(float width, bool big) {
}; };
m_title->setPosition(m_obContentSize.height + 10, m_obContentSize.height * .7f); m_title->setPosition(m_obContentSize.height + 10, m_obContentSize.height * .7f);
limitNodeSize(m_title, titleSpace, 1.f, .1f); limitNodeSize(m_title, titleSpace, 1.f, .1f);
// Only limit developer size by height since we're setting the content width manually
limitNodeSize(m_developers, ccp(9999, titleSpace.height * .8f), 1.f, .1f);
m_developers->setPosition(m_obContentSize.height + 10, m_obContentSize.height * .3f); m_developers->setPosition(m_obContentSize.height + 10, m_obContentSize.height * .3f);
limitNodeSize(m_developers, titleSpace, .4f, .1f); m_developers->setContentWidth(titleSpace.width / m_developers->getScale());
m_developers->updateLayout();
m_viewMenu->setContentWidth(m_obContentSize.width / 2 - 20); m_viewMenu->setContentWidth(m_obContentSize.width / 2 - 20);
m_viewMenu->updateLayout(); m_viewMenu->updateLayout();

View file

@ -1,6 +1,7 @@
#include "ModList.hpp" #include "ModList.hpp"
#include <Geode/utils/ColorProvider.hpp> #include <Geode/utils/ColorProvider.hpp>
#include "TagsPopup.hpp" #include "TagsPopup.hpp"
#include "GeodeStyle.hpp"
bool ModList::init(ModListSource* src, CCSize const& size) { bool ModList::init(ModListSource* src, CCSize const& size) {
if (!CCNode::init()) if (!CCNode::init())
@ -71,21 +72,15 @@ bool ModList::init(ModListSource* src, CCSize const& size) {
// todo: sort button // todo: sort button
m_filterBtnSpr = CCSprite::create("GE_button_05.png"_spr);
auto filterBtnTop = CCSprite::createWithSpriteFrameName("GJ_filterIcon_001.png");
limitNodeSize(filterBtnTop, m_filterBtnSpr->getContentSize() * .65f, 2.f, .1f);
m_filterBtnSpr->addChildAtPosition(filterBtnTop, Anchor::Center);
auto filterBtn = CCMenuItemSpriteExtra::create( auto filterBtn = CCMenuItemSpriteExtra::create(
m_filterBtnSpr, this, menu_selector(ModList::onFilters) GeodeButtonSprite::createWithSpriteFrameName("GJ_filterIcon_001.png"),
this, menu_selector(ModList::onFilters)
); );
searchFiltersMenu->addChild(filterBtn); searchFiltersMenu->addChild(filterBtn);
auto clearFiltersBtnSpr = CCSprite::create("GE_button_05.png"_spr);
auto clearFiltersBtnTop = CCSprite::createWithSpriteFrameName("GJ_deleteIcon_001.png");
limitNodeSize(clearFiltersBtnTop, clearFiltersBtnSpr->getContentSize() * .65f, 2.f, .1f);
clearFiltersBtnSpr->addChildAtPosition(clearFiltersBtnTop, Anchor::Center);
auto clearFiltersBtn = CCMenuItemSpriteExtra::create( auto clearFiltersBtn = CCMenuItemSpriteExtra::create(
clearFiltersBtnSpr, this, menu_selector(ModList::onClearFilters) GeodeButtonSprite::createWithSpriteFrameName("GJ_deleteIcon_001.png"),
this, menu_selector(ModList::onClearFilters)
); );
searchFiltersMenu->addChild(clearFiltersBtn); searchFiltersMenu->addChild(clearFiltersBtn);

View file

@ -33,7 +33,6 @@ protected:
CCMenuItemSpriteExtra* m_pagePrevBtn; CCMenuItemSpriteExtra* m_pagePrevBtn;
CCMenuItemSpriteExtra* m_pageNextBtn; CCMenuItemSpriteExtra* m_pageNextBtn;
Ref<CCNode> m_searchMenu; Ref<CCNode> m_searchMenu;
CCSprite* m_filterBtnSpr;
TextInput* m_searchInput; TextInput* m_searchInput;
ModListPageUpdated m_pageUpdated = nullptr; ModListPageUpdated m_pageUpdated = nullptr;
bool m_bigSize = false; bool m_bigSize = false;

View file

@ -2,6 +2,7 @@
#include "SwelvyBG.hpp" #include "SwelvyBG.hpp"
#include <Geode/ui/TextInput.hpp> #include <Geode/ui/TextInput.hpp>
#include <Geode/utils/ColorProvider.hpp> #include <Geode/utils/ColorProvider.hpp>
#include "GeodeStyle.hpp"
bool ModsLayer::init() { bool ModsLayer::init() {
if (!CCLayer::init()) if (!CCLayer::init())
@ -137,21 +138,15 @@ bool ModsLayer::init() {
listActionsMenu->setAnchorPoint({ 1, 0 }); listActionsMenu->setAnchorPoint({ 1, 0 });
listActionsMenu->setScale(.65f); listActionsMenu->setScale(.65f);
m_bigSizeBtnSpr = CCSprite::create("GE_button_05.png"_spr);
auto bigSizeBtnTop = CCSprite::createWithSpriteFrameName("GJ_smallModeIcon_001.png");
limitNodeSize(bigSizeBtnTop, m_bigSizeBtnSpr->getContentSize() * .65f, 2.f, .1f);
m_bigSizeBtnSpr->addChildAtPosition(bigSizeBtnTop, Anchor::Center);
auto bigSizeBtn = CCMenuItemSpriteExtra::create( auto bigSizeBtn = CCMenuItemSpriteExtra::create(
m_bigSizeBtnSpr, this, menu_selector(ModsLayer::onBigView) GeodeButtonSprite::createWithSpriteFrameName("GJ_smallModeIcon_001.png", &m_bigView),
this, menu_selector(ModsLayer::onBigView)
); );
listActionsMenu->addChild(bigSizeBtn); listActionsMenu->addChild(bigSizeBtn);
m_searchBtnSpr = CCSprite::create("GE_button_05.png"_spr);
auto searchBtnTop = CCSprite::createWithSpriteFrameName("search.png"_spr);
limitNodeSize(searchBtnTop, m_searchBtnSpr->getContentSize() * .65f, 2.f, .1f);
m_searchBtnSpr->addChildAtPosition(searchBtnTop, Anchor::Center);
auto searchBtn = CCMenuItemSpriteExtra::create( auto searchBtn = CCMenuItemSpriteExtra::create(
m_searchBtnSpr, this, menu_selector(ModsLayer::onSearch) GeodeButtonSprite::createWithSpriteFrameName("search.png"_spr, &m_showSearch),
this, menu_selector(ModsLayer::onSearch)
); );
listActionsMenu->addChild(searchBtn); listActionsMenu->addChild(searchBtn);
@ -285,29 +280,18 @@ void ModsLayer::onGoToPage(CCObject*) {
void ModsLayer::onBigView(CCObject*) { void ModsLayer::onBigView(CCObject*) {
m_bigView = !m_bigView; m_bigView = !m_bigView;
// Make sure to avoid a crash // Make sure to avoid a crash
if (m_currentSource) { if (m_currentSource) {
m_lists.at(m_currentSource)->updateSize(m_bigView); m_lists.at(m_currentSource)->updateSize(m_bigView);
} }
// Update the background on the size button
m_bigSizeBtnSpr->setTexture(CCTextureCache::get()->addImage(
(m_bigView ? "GJ_button_02.png" : "GE_button_05.png"_spr), true
));
} }
void ModsLayer::onSearch(CCObject*) { void ModsLayer::onSearch(CCObject*) {
m_showSearch = !m_showSearch; m_showSearch = !m_showSearch;
// Make sure to avoid a crash // Make sure to avoid a crash
if (m_currentSource) { if (m_currentSource) {
m_lists.at(m_currentSource)->activateSearch(m_showSearch); m_lists.at(m_currentSource)->activateSearch(m_showSearch);
} }
// Update the background on the search button
m_searchBtnSpr->setTexture(CCTextureCache::get()->addImage(
(m_showSearch ? "GJ_button_02.png" : "GE_button_05.png"_spr), true
));
} }
ModsLayer* ModsLayer::create() { ModsLayer* ModsLayer::create() {

View file

@ -15,8 +15,6 @@ protected:
std::vector<CCMenuItemSpriteExtra*> m_tabs; std::vector<CCMenuItemSpriteExtra*> m_tabs;
ModListSource* m_currentSource = nullptr; ModListSource* m_currentSource = nullptr;
std::unordered_map<ModListSource*, Ref<ModList>> m_lists; std::unordered_map<ModListSource*, Ref<ModList>> m_lists;
CCSprite* m_bigSizeBtnSpr;
CCSprite* m_searchBtnSpr;
CCMenu* m_pageMenu; CCMenu* m_pageMenu;
CCLabelBMFont* m_pageLabel; CCLabelBMFont* m_pageLabel;
CCMenuItemSpriteExtra* m_goToPageBtn; CCMenuItemSpriteExtra* m_goToPageBtn;