Skip to content

Commit

Permalink
button setting test
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Sep 8, 2024
1 parent 76347e1 commit d9aa1ca
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ project(GeodeTestMod VERSION 1.0.0)
add_library(${PROJECT_NAME} SHARED
src/main.cpp
src/MyCustomSettingV3.cpp
src/MyButtonSettingV3.cpp
)

if (NOT DEFINED ENV{GEODE_SDK})
Expand Down
113 changes: 113 additions & 0 deletions src/MyButtonSettingV3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <Geode/loader/SettingV3.hpp>
#include <Geode/loader/Mod.hpp>

using namespace geode::prelude;

class MyButtonSettingV3 : public SettingV3 {
public:
static Result<std::shared_ptr<MyButtonSettingV3>> parse(std::string const& key, std::string const& modID, matjson::Value const& json) {
auto res = std::make_shared<MyButtonSettingV3>();
auto root = checkJson(json, "MyButtonSettingV3");
res->init(key, modID, root);
res->parseNameAndDescription(root);
res->parseEnableIf(root);
root.checkUnknownKeys();
return root.ok(res);
}

bool load(matjson::Value const& json) override {
return true;
}
bool save(matjson::Value& json) const override {
return true;
}

bool isDefaultValue() const override {
return true;
}

void reset() override {}

SettingNodeV3* createNode(float width) override;
};

class MyButtonSettingNodeV3 : public SettingNodeV3 {
protected:
ButtonSprite* m_buttonSprite;
CCMenuItemSpriteExtra* m_button;

bool init(std::shared_ptr<MyButtonSettingV3> setting, float width) {
if (!SettingNodeV3::init(setting, width))
return false;

m_buttonSprite = ButtonSprite::create("Click Me!", "goldFont.fnt", "GJ_button_01.png", .8f);
m_buttonSprite->setScale(.5f);
m_button = CCMenuItemSpriteExtra::create(
m_buttonSprite, this, menu_selector(MyButtonSettingNodeV3::onButton)
);
this->getButtonMenu()->addChildAtPosition(m_button, Anchor::Center);
this->getButtonMenu()->setContentWidth(60);
this->getButtonMenu()->updateLayout();

this->updateState(nullptr);

return true;
}

void updateState(CCNode* invoker) override {
SettingNodeV3::updateState(invoker);

auto shouldEnable = this->getSetting()->shouldEnable();
m_button->setEnabled(shouldEnable);
m_buttonSprite->setCascadeColorEnabled(true);
m_buttonSprite->setCascadeOpacityEnabled(true);
m_buttonSprite->setOpacity(shouldEnable ? 255 : 155);
m_buttonSprite->setColor(shouldEnable ? ccWHITE : ccGRAY);
}
void onButton(CCObject*) {
FLAlertLayer::create(
"Hello",
fmt::format(
"You clicked the button for setting <co>{}</c>!",
this->getSetting()->getDisplayName()
),
"OK"
)->show();
}

void onCommit() override {}
void onResetToDefault() override {}

public:
static MyButtonSettingNodeV3* create(std::shared_ptr<MyButtonSettingV3> setting, float width) {
auto ret = new MyButtonSettingNodeV3();
if (ret && ret->init(setting, width)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}

bool hasUncommittedChanges() const override {
return false;
}
bool hasNonDefaultValue() const override {
return false;
}

std::shared_ptr<MyButtonSettingV3> getSetting() const {
return std::static_pointer_cast<MyButtonSettingV3>(SettingNodeV3::getSetting());
}
};

SettingNodeV3* MyButtonSettingV3::createNode(float width) {
return MyButtonSettingNodeV3::create(
std::static_pointer_cast<MyButtonSettingV3>(shared_from_this()),
width
);
}

$execute {
(void)Mod::get()->registerCustomSettingType("my-button-type", &MyButtonSettingV3::parse);
}

0 comments on commit d9aa1ca

Please sign in to comment.