-
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
114 additions
and
0 deletions.
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,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); | ||
} |