fix inputs not working & make slider snaps not optional

This commit is contained in:
HJfod 2024-09-09 23:54:14 +03:00
parent 77268271ee
commit 75003b9afa
4 changed files with 17 additions and 13 deletions

View file

@ -432,7 +432,7 @@ namespace geode {
size_t getArrowStepSize() const;
size_t getBigArrowStepSize() const;
bool isSliderEnabled() const;
std::optional<int64_t> getSliderSnap() const;
int64_t getSliderSnap() const;
bool isInputEnabled() const;
SettingNodeV3* createNode(float width) override;
@ -464,7 +464,7 @@ namespace geode {
double getArrowStepSize() const;
double getBigArrowStepSize() const;
bool isSliderEnabled() const;
std::optional<double> getSliderSnap() const;
double getSliderSnap() const;
bool isInputEnabled() const;
SettingNodeV3* createNode(float width) override;
@ -669,6 +669,9 @@ namespace geode {
void onCommit() override {
this->getSetting()->setValue(m_impl->currentValue);
// The value may be different, if the current value was an invalid
// value for the setting
this->setValue(this->getSetting()->getValue(), nullptr);
}
bool hasUncommittedChanges() const override {
return m_impl->currentValue != this->getSetting()->getValue();

View file

@ -311,8 +311,8 @@ bool StringSettingNodeV3::init(std::shared_ptr<StringSettingV3> setting, float w
return false;
m_input = TextInput::create(setting->getEnumOptions() ? width / 2 - 50 : width / 2, "Text");
m_input->setCallback([this](auto const&) {
this->markChanged(m_input);
m_input->setCallback([this](auto const& str) {
this->setValue(str, m_input);
});
m_input->setScale(.7f);
m_input->setString(this->getSetting()->getValue());

View file

@ -67,8 +67,9 @@ protected:
auto max = this->getSetting()->getMaxValue().value_or(+100);
auto range = max - min;
auto value = static_cast<ValueType>(num * range + min);
if (auto step = this->getSetting()->getSliderSnap()) {
value = static_cast<ValueType>(round(value / *step) * (*step));
auto step = this->getSetting()->getSliderSnap();
if (step > 0) {
value = static_cast<ValueType>(round(value / step) * step);
}
return value;
}
@ -106,8 +107,8 @@ protected:
m_input = TextInput::create(this->getButtonMenu()->getContentWidth() - 40, "Num");
m_input->setScale(.7f);
m_input->setCallback([this](auto const&) {
this->markChanged(m_input);
m_input->setCallback([this, setting](auto const& str) {
this->setValue(numFromString<ValueType>(str).unwrapOr(setting->getDefaultValue()), m_input);
});
if (!setting->isInputEnabled()) {
m_input->getBGSprite()->setVisible(false);

View file

@ -487,7 +487,7 @@ void SettingV3::init(std::string const& key, std::string const& modID, JsonExpec
}
void SettingV3::parseNameAndDescription(JsonExpectedValue& json) {
json.needs("name").into(m_impl->name);
json.has("name").into(m_impl->name);
json.has("description").into(m_impl->description);
}
void SettingV3::parseEnableIf(JsonExpectedValue& json) {
@ -711,7 +711,7 @@ public:
size_t arrowStepSize = 1;
size_t bigArrowStepSize = 5;
bool sliderEnabled = true;
std::optional<int64_t> sliderSnap;
int64_t sliderSnap = 1;
bool textInputEnabled = true;
} controls;
};
@ -797,7 +797,7 @@ size_t IntSettingV3::getBigArrowStepSize() const {
bool IntSettingV3::isSliderEnabled() const {
return m_impl->controls.sliderEnabled;
}
std::optional<int64_t> IntSettingV3::getSliderSnap() const {
int64_t IntSettingV3::getSliderSnap() const {
return m_impl->controls.sliderSnap;
}
bool IntSettingV3::isInputEnabled() const {
@ -842,7 +842,7 @@ public:
double arrowStepSize = 1;
double bigArrowStepSize = 5;
bool sliderEnabled = true;
std::optional<double> sliderSnap;
double sliderSnap = 0.1;
bool textInputEnabled = true;
} controls;
};
@ -926,7 +926,7 @@ double FloatSettingV3::getBigArrowStepSize() const {
bool FloatSettingV3::isSliderEnabled() const {
return m_impl->controls.sliderEnabled;
}
std::optional<double> FloatSettingV3::getSliderSnap() const {
double FloatSettingV3::getSliderSnap() const {
return m_impl->controls.sliderSnap;
}
bool FloatSettingV3::isInputEnabled() const {