Skip to content

Commit

Permalink
add custom setting test
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Sep 7, 2024
1 parent 27e13c6 commit e3384f0
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 1 deletion.
16 changes: 15 additions & 1 deletion mod.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"geode": "3.4.0",
"geode": "3.5.0",
"gd": {
"win": "2.206",
"android": "2.206",
Expand Down Expand Up @@ -310,6 +310,15 @@
"default": "#000",
"name": "Longer ass name for color setting how does it cope"
},
"section-custom": {
"type": "title",
"name": "Custom Setting Tests"
},
"test-custom-setting": {
"type": "custom:my-awesome-type",
"default": "valid-enum-value",
"name": "My Awesome Enum!"
},
"section-enable-if": {
"type": "title",
"name": "Enable If Tests"
Expand Down Expand Up @@ -407,6 +416,11 @@
"type": "rgba",
"default": "#0000",
"enable-if": "test-enable-if"
},
"test-should-be-enabled-custom": {
"type": "custom:my-awesome-type",
"default": "valid-enum-value",
"enable-if": "test-enable-if"
}
}
}
128 changes: 128 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#include <Geode/loader/SettingV3.hpp>
#include <Geode/loader/Mod.hpp>

using namespace geode::prelude;

enum class MyCustomEnum {
ValidEnumValue,
OtherValidEnumValue,
};

template <>
struct matjson::Serialize<MyCustomEnum> {
static matjson::Value to_json(MyCustomEnum const& value) {
switch (value) {
default:
case MyCustomEnum::ValidEnumValue: return "valid-enum-value";
case MyCustomEnum::OtherValidEnumValue: return "other-valid-enum-value";
}
}
static MyCustomEnum from_json(matjson::Value const& value) {
switch (hash(value.as_string())) {
case hash("valid-enum-value"): return MyCustomEnum::ValidEnumValue;
case hash("other-valid-enum-value"): return MyCustomEnum::OtherValidEnumValue;
default: throw matjson::JsonException(fmt::format("invalid MyCustomEnum value '{}'", value));
}
}
static bool is_json(matjson::Value const& json) {
return json.is_string();
}
};

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

SettingNodeV3* createNode(float width) override;
};

template <>
struct geode::SettingTypeForValueType<MyCustomEnum> {
using SettingType = MyCustomSettingV3;
};

class MyCustomSettingNodeV3 : public SettingValueNodeV3<MyCustomSettingV3> {
protected:
MyCustomEnum m_value;
std::vector<CCMenuItemToggler*> m_toggles;

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

m_value = setting->getValue();

for (auto value : {
std::make_pair(MyCustomEnum::ValidEnumValue, "GJ_starsIcon_001.png"),
std::make_pair(MyCustomEnum::OtherValidEnumValue, "currencyOrbIcon_001.png"),
}) {
auto offSpr = CCSprite::createWithSpriteFrameName(value.second);
offSpr->setOpacity(90);
auto onSpr = CCSprite::createWithSpriteFrameName(value.second);
auto toggle = CCMenuItemToggler::create(
offSpr, onSpr, this, menu_selector(MyCustomSettingNodeV3::onToggle)
);
toggle->m_notClickable = true;
toggle->setTag(static_cast<int>(value.first));
m_toggles.push_back(toggle);
this->getButtonMenu()->addChild(toggle);
}
this->getButtonMenu()->setContentWidth(40);
this->getButtonMenu()->setLayout(RowLayout::create());

this->updateState(nullptr);

return true;
}

void updateState(CCNode* invoker) override {
SettingValueNodeV3::updateState(invoker);
auto shouldEnable = this->getSetting()->shouldEnable();
for (auto toggle : m_toggles) {
toggle->toggle(toggle->getTag() == static_cast<int>(m_value));
toggle->setEnabled(shouldEnable);
toggle->setCascadeColorEnabled(true);
toggle->setCascadeOpacityEnabled(true);
toggle->setOpacity(shouldEnable ? 255 : 155);
toggle->setColor(shouldEnable ? ccWHITE : ccGRAY);
}
}
void onToggle(CCObject* sender) {
this->setValue(static_cast<MyCustomEnum>(sender->getTag()), static_cast<CCNode*>(sender));
}
void onSetValue(MyCustomEnum value) override {
m_value = value;
}

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

MyCustomEnum getValue() const override {
return m_value;
}
};

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

$execute {
auto ret = Mod::get()->registerCustomSettingType("my-awesome-type", &MyCustomSettingV3::parse);
if (!ret) {
log::error("Unable to register setting type: {}", ret.unwrapErr());
}
}

0 comments on commit e3384f0

Please sign in to comment.