preserve settings order (note: still need to change all uses of json to

ordered_json) + preliminary work on file setting
This commit is contained in:
HJfod 2022-09-23 22:25:20 +03:00
parent faa269fe8a
commit 8e629cb2cf
7 changed files with 85 additions and 9 deletions

View file

@ -127,7 +127,7 @@ namespace geode {
/**
* Mod settings
*/
std::unordered_map<std::string, std::shared_ptr<Setting>> m_settings;
std::vector<std::pair<std::string, std::shared_ptr<Setting>>> m_settings;
/**
* Whether the mod can be disabled or not
*/
@ -323,18 +323,21 @@ namespace geode {
bool hasSettings() const;
decltype(ModInfo::m_settings) getSettings() const;
bool hasSetting(std::string const& key) const;
std::shared_ptr<Setting> getSetting(std::string const& key) const;
template<class T>
T getSettingValue(std::string const& key) const {
if (m_info.m_settings.count(key)) {
return geode::getBuiltInSettingValue<T>(m_info.m_settings.at(key));
if (this->hasSetting(key)) {
return geode::getBuiltInSettingValue<T>(
this->getSetting(key)
);
}
return T();
}
template<class T>
bool setSettingValue(std::string const& key, T const& value) {
if (m_info.m_settings.count(key)) {
geode::setBuiltInSettingValue<T>(m_info.m_settings[key], value);
if (this->hasSetting(key)) {
geode::setBuiltInSettingValue<T>(this->getSetting(key), value);
return true;
}
return false;

View file

@ -395,6 +395,14 @@ namespace geode {
SettingNode* createNode(float width) override;
};
class GEODE_DLL FileSetting :
public GeodeSetting<FileSetting, ghc::filesystem::path, SettingType::File>,
public std::enable_shared_from_this<FileSetting>
{
public:
SettingNode* createNode(float width) override;
};
class GEODE_DLL ColorSetting :
public GeodeSetting<ColorSetting, cocos2d::ccColor3B, SettingType::Color>,
public std::enable_shared_from_this<ColorSetting>

View file

@ -558,12 +558,23 @@ decltype(ModInfo::m_settings) Mod::getSettings() const {
}
std::shared_ptr<Setting> Mod::getSetting(std::string const& key) const {
if (m_info.m_settings.count(key)) {
return m_info.m_settings.at(key);
for (auto& sett : m_info.m_settings) {
if (sett.first == key) {
return sett.second;
}
}
return nullptr;
}
bool Mod::hasSetting(std::string const& key) const {
for (auto& sett : m_info.m_settings) {
if (sett.first == key) {
return true;
}
}
return false;
}
std::string Mod::getLoadErrorInfo() const {
return m_loadErrorInfo;
}

View file

@ -22,9 +22,11 @@ Result<std::shared_ptr<Setting>> Setting::parse(
case hash("int"): return IntSetting::parse(key, obj);
case hash("float"): return FloatSetting::parse(key, obj);
case hash("string"): return StringSetting::parse(key, obj);
case hash("rgb"): case hash("color"):
return ColorSetting::parse(key, obj);
case hash("rgb"):
case hash("color"): return ColorSetting::parse(key, obj);
case hash("rgba"): return ColorAlphaSetting::parse(key, obj);
case hash("path"):
case hash("file"): return FileSetting::parse(key, obj);
default: return Err(
"Setting \"" + key + "\" has unknown type \"" + type + "\""
);
@ -68,6 +70,10 @@ SettingNode* StringSetting::createNode(float width) {
return StringSettingNode::create(shared_from_this(), width);
}
SettingNode* FileSetting::createNode(float width) {
return FileSettingNode::create(shared_from_this(), width);
}
SettingNode* ColorSetting::createNode(float width) {
return ColorSettingNode::create(shared_from_this(), width);
}

View file

@ -91,6 +91,35 @@ bool StringSettingNode::setup(std::shared_ptr<StringSetting> setting, float widt
return true;
}
// FileSettingNode
void FileSettingNode::updateLabel() {
// hacky way to make setString not called textChanged
m_input->getInput()->setDelegate(nullptr);
m_input->setString(m_uncommittedValue.string());
m_input->getInput()->setDelegate(this);
}
void FileSettingNode::textChanged(CCTextInputNode* input) {
m_uncommittedValue = input->getString();
this->valueChanged(false);
}
void FileSettingNode::valueChanged(bool updateText) {
GeodeSettingNode::valueChanged(updateText);
this->updateLabel();
}
bool FileSettingNode::setup(std::shared_ptr<FileSetting> setting, float width) {
m_input = InputNode::create(width / 2 - 10.f, "Path to File", "chatFont.fnt");
m_input->setPosition({ -(width / 2 - 70.f) / 2, .0f });
m_input->setScale(.65f);
m_input->getInput()->setDelegate(this);
m_menu->addChild(m_input);
return true;
}
// ColorSettingNode
void ColorSettingNode::valueChanged(bool updateText) {

View file

@ -494,6 +494,20 @@ protected:
bool setup(std::shared_ptr<StringSetting> setting, float width) override;
};
class FileSettingNode :
public GeodeSettingNode<FileSettingNode, FileSetting>,
public TextInputDelegate
{
protected:
InputNode* m_input;
void textChanged(CCTextInputNode* input) override;
void valueChanged(bool updateText) override;
void updateLabel();
bool setup(std::shared_ptr<FileSetting> setting, float width) override;
};
class ColorSettingNode :
public GeodeSettingNode<ColorSettingNode, ColorSetting>,
public ColorPickPopupDelegate

View file

@ -60,6 +60,11 @@
"faithful-dog-hachi": {
"type": "rgba",
"default": [ 255, 150, 55, 180 ]
},
"im-getting-to-the-bus-to-the-other-world-see-ya": {
"type": "file",
"name": "Bus",
"default": ""
}
}
}