Setting update

This commit is contained in:
MihailRis
2024-03-29 20:02:58 +03:00
parent 079e8c1487
commit 73a460ddb6
7 changed files with 72 additions and 66 deletions
+8 -2
View File
@@ -2,13 +2,19 @@
#include "../util/stringutil.h"
std::string NumberSetting::toString() const {
template<class T>
std::string NumberSetting<T>::toString() const {
switch (getFormat()) {
case setting_format::simple:
return util::to_string(value);
case setting_format::percent:
return std::to_string(static_cast<int>(value * 100)) + "%";
return std::to_string(static_cast<int64_t>(value * 100)) + "%";
default:
return "invalid format";
}
}
template class NumberSetting<float>;
template class NumberSetting<double>;
template class NumberSetting<int>;
template class NumberSetting<uint>;
+21 -25
View File
@@ -7,29 +7,15 @@ enum class setting_format {
simple, percent
};
template<class T>
class Setting {
protected:
T value;
setting_format format;
public:
Setting(T value, setting_format format) : value(value), format(format) {
Setting(setting_format format) : format(format) {
}
virtual ~Setting() {}
T& operator*() {
return value;
}
virtual const T& get() const {
return value;
}
virtual void set(const T& value) {
this->value = value;
}
virtual setting_format getFormat() const {
return format;
}
@@ -37,33 +23,43 @@ public:
virtual std::string toString() const = 0;
};
class NumberSetting : public Setting<double> {
template<class T>
class NumberSetting : public Setting {
protected:
double min;
double max;
T value;
T min;
T max;
public:
NumberSetting(double value, double min, double max, setting_format format)
: Setting(value, format), min(min), max(max) {}
NumberSetting(T value, T min, T max, setting_format format)
: Setting(format), value(value), min(min), max(max) {}
double& operator*() {
T& operator*() {
return value;
}
double getMin() const {
T get() const {
return value;
}
void set(T value) {
this->value = value;
}
T getMin() const {
return min;
}
double getMax() const {
T getMax() const {
return max;
}
double getT() const {
T getT() const {
return (value - min) / (max - min);
}
virtual std::string toString() const override;
static inline NumberSetting createPercent(double def) {
static inline NumberSetting createPercent(T def) {
return NumberSetting(def, 0.0, 1.0, setting_format::percent);
}
};