mod items now use layouts (Wayouts imo)

This commit is contained in:
HJfod 2024-02-29 16:43:23 +02:00
parent da2bf3e0d3
commit 16fdddb06e
3 changed files with 118 additions and 47 deletions

View file

@ -10,18 +10,61 @@ bool BaseModItem::init() {
m_logo = this->createModLogo();
this->addChild(m_logo);
m_title = CCLabelBMFont::create(meta.getName().c_str(), "bigFont.fnt");
m_title->setAnchorPoint({ .0f, .5f });
this->addChild(m_title);
m_infoContainer = CCNode::create();
m_infoContainer->setScale(.4f);
m_infoContainer->setAnchorPoint({ .0f, .5f });
m_infoContainer->setLayout(
ColumnLayout::create()
->setAxisReverse(true)
->setAxisAlignment(AxisAlignment::Even)
->setCrossAxisLineAlignment(AxisAlignment::Start)
->setGap(0)
);
m_infoContainer->getLayout()->ignoreInvisibleChildren(true);
m_titleContainer = CCNode::create();
m_titleContainer->setAnchorPoint({ .0f, .5f });
m_titleContainer->setLayout(
RowLayout::create()
->setAxisAlignment(AxisAlignment::Start)
);
auto title = CCLabelBMFont::create(meta.getName().c_str(), "bigFont.fnt");
title->setAnchorPoint({ .0f, .5f });
title->setLayoutOptions(
AxisLayoutOptions::create()
->setMinScale(.1f)
);
m_titleContainer->addChild(title);
m_infoContainer->addChild(m_titleContainer);
m_developers = CCMenu::create();
m_developers->ignoreAnchorPointForPosition(false);
m_developers->setAnchorPoint({ .0f, .5f });
auto by = "By " + ModMetadata::formatDeveloperDisplayString(this->getMetadata().getDevelopers());
auto developersBtn = CCMenuItemSpriteExtra::create(
CCLabelBMFont::create(by.c_str(), "goldFont.fnt"),
this, nullptr
);
m_developers->addChild(developersBtn);
m_developers->setLayout(
RowLayout::create()
->setAxisAlignment(AxisAlignment::Start)
);
this->addChild(m_developers);
m_infoContainer->addChild(m_developers);
m_restartRequiredLabel = ButtonSprite::create("Restart Required", "bigFont.fnt", "black-square.png"_spr, .8f);
m_restartRequiredLabel->m_label->setColor({ 55, 255, 155 });
m_restartRequiredLabel->setLayoutOptions(
AxisLayoutOptions::create()
->setMaxScale(.75f)
);
m_infoContainer->addChild(m_restartRequiredLabel);
this->addChild(m_infoContainer);
m_viewMenu = CCMenu::create();
m_viewMenu->setAnchorPoint({ 1.f, .5f });
@ -37,50 +80,44 @@ bool BaseModItem::init() {
RowLayout::create()
->setAxisReverse(true)
->setAxisAlignment(AxisAlignment::End)
->setGap(10)
);
this->addChildAtPosition(m_viewMenu, Anchor::Right, ccp(-10, 0));
this->updateState();
return true;
}
void BaseModItem::updateState() {
m_developers->removeAllChildren();
if (this->wantsRestart()) {
m_developers->addChild(ButtonSprite::create("Restart Required", "goldFont.fnt", "black-square"_spr, .8f));
}
else {
auto by = "By " + ModMetadata::formatDeveloperDisplayString(this->getMetadata().getDevelopers());
auto developersBtn = CCMenuItemSpriteExtra::create(
CCLabelBMFont::create(by.c_str(), "goldFont.fnt"),
this, nullptr
);
m_developers->addChild(developersBtn);
}
m_developers->updateLayout();
auto wantsRestart = this->wantsRestart();
m_restartRequiredLabel->setVisible(wantsRestart);
m_developers->setVisible(!wantsRestart);
m_infoContainer->updateLayout();
}
void BaseModItem::updateSize(float width, bool big) {
this->setContentSize({ width, big ? 40.f : 25.f });
this->setContentSize({ width, big ? 40.f : 30.f });
auto logoSize = m_obContentSize.height - 5;
auto logoSize = m_obContentSize.height - 10;
limitNodeSize(m_logo, { logoSize, logoSize }, 999, .1f);
m_logo->setPosition(m_obContentSize.height / 2 + 5, m_obContentSize.height / 2);
CCSize titleSpace {
m_obContentSize.width / 2 - m_obContentSize.height,
m_obContentSize.height / 2
logoSize + 5
};
m_title->setPosition(m_obContentSize.height + 10, m_obContentSize.height * .7f);
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->setContentWidth(titleSpace.width / m_developers->getScale());
// Divide by scale of info container since that actually determines the size
// (Since the scale of m_titleContainer and m_developers is managed by its layout)
m_titleContainer->setContentWidth(titleSpace.width / m_infoContainer->getScale());
m_titleContainer->updateLayout();
m_developers->setContentWidth(titleSpace.width / m_infoContainer->getScale());
m_developers->updateLayout();
m_infoContainer->setPosition(m_obContentSize.height + 10, m_obContentSize.height / 2);
m_infoContainer->setContentSize(ccp(titleSpace.width, titleSpace.height) / m_infoContainer->getScale());
m_infoContainer->updateLayout();
m_viewMenu->setContentWidth(m_obContentSize.width / 2 - 20);
m_viewMenu->updateLayout();
@ -93,9 +130,46 @@ bool InstalledModItem::init(Mod* mod) {
if (!BaseModItem::init())
return false;
// Add an enable button if the mod is enablable
if (!mod->isInternal()) {
m_enableToggle = CCMenuItemToggler::createWithStandardSprites(
this, menu_selector(InstalledModItem::onEnable), 1.f
);
// Manually handle toggle state
m_enableToggle->m_notClickable = true;
m_viewMenu->addChild(m_enableToggle);
m_viewMenu->updateLayout();
}
this->updateState();
return true;
}
void InstalledModItem::updateState() {
BaseModItem::updateState();
// Update enable toggle state
if (m_enableToggle) {
m_enableToggle->toggle(m_mod->isEnabled());
}
}
void InstalledModItem::onEnable(CCObject*) {
// Toggle the mod state
auto res = m_mod->isEnabled() ? m_mod->disable() : m_mod->enable();
if (!res) {
FLAlertLayer::create(
"Error Toggling Mod",
res.unwrapErr(),
"OK"
)->show();
}
// Update whole state of the mod item
this->updateState();
}
InstalledModItem* InstalledModItem::create(Mod* mod) {
auto ret = new InstalledModItem();
if (ret && ret->init(mod)) {
@ -132,22 +206,15 @@ bool ServerModItem::init(server::ServerModMetadata const& metadata) {
auto tick = CCSprite::createWithSpriteFrameName("GJ_starsIcon_001.png");
m_checkmark->addChildAtPosition(tick, Anchor::Center);
this->addChild(m_checkmark);
m_titleContainer->addChild(m_checkmark);
}
this->updateState();
return true;
}
void ServerModItem::updateSize(float width, bool big) {
BaseModItem::updateSize(width, big);
if (m_checkmark) {
auto size = m_title->getScaledContentSize();
limitNodeSize(m_checkmark, ccp(100, size.height), 1.f, .1f);
m_checkmark->setPosition(m_title->getPosition() + ccp(size.width + 10, 0));
}
}
ServerModItem* ServerModItem::create(server::ServerModMetadata const& metadata) {
auto ret = new ServerModItem();
if (ret && ret->init(metadata)) {

View file

@ -7,9 +7,11 @@ using namespace geode::prelude;
class BaseModItem : public CCNode {
protected:
CCNode* m_logo = nullptr;
CCNode* m_title = nullptr;
CCNode* m_developers = nullptr;
CCNode* m_logo;
CCNode* m_infoContainer;
CCNode* m_titleContainer;
CCNode* m_developers;
ButtonSprite* m_restartRequiredLabel = nullptr;
CCMenu* m_viewMenu;
/**
@ -30,9 +32,12 @@ public:
class InstalledModItem : public BaseModItem {
protected:
Mod* m_mod;
CCMenuItemToggler* m_enableToggle = nullptr;
bool init(Mod* mod);
void onEnable(CCObject*);
public:
/**
* @note Make sure to call `updateSize` afterwards
@ -42,6 +47,8 @@ public:
ModMetadata getMetadata() const override;
CCNode* createModLogo() const override;
bool wantsRestart() const override;
void updateState() override;
};
class ServerModItem : public BaseModItem {
@ -60,6 +67,4 @@ public:
ModMetadata getMetadata() const override;
CCNode* createModLogo() const override;
bool wantsRestart() const override;
void updateSize(float width, bool big) override;
};

View file

@ -19,8 +19,7 @@ bool ModList::init(ModListSource* src, CCSize const& size) {
->setAxisReverse(true)
->setAxisAlignment(AxisAlignment::End)
->setAutoGrowAxis(size.height)
// This is half the normal size for separators
->setGap(2.5f)
->setGap(0)
);
this->addChildAtPosition(m_list, Anchor::Bottom, ccp(-m_list->getScaledContentSize().width / 2, 0));
@ -274,7 +273,7 @@ void ModList::activateSearch(bool activate) {
// (giving a little bit of extra padding for it, the same size as gap)
m_list->setContentHeight(
activate ?
this->getContentHeight() - m_searchMenu->getContentHeight() - 2.5f :
this->getContentHeight() - m_searchMenu->getContentHeight() :
this->getContentHeight()
);