-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |