| @@ -30,27 +30,56 @@ struct ParamQuantity : Quantity { | |||||
| // TODO Snap | // TODO Snap | ||||
| getParam()->value = value; | getParam()->value = value; | ||||
| } | } | ||||
| float getValue() override { | float getValue() override { | ||||
| return getParam()->value; | return getParam()->value; | ||||
| } | } | ||||
| float getMinValue() override { | float getMinValue() override { | ||||
| return getParam()->minValue; | return getParam()->minValue; | ||||
| } | } | ||||
| float getMaxValue() override { | float getMaxValue() override { | ||||
| return getParam()->maxValue; | return getParam()->maxValue; | ||||
| } | } | ||||
| float getDefaultValue() override { | float getDefaultValue() override { | ||||
| return getParam()->defaultValue; | return getParam()->defaultValue; | ||||
| } | } | ||||
| float getDisplayValue() override { | |||||
| if (getParam()->displayBase == 0.f) { | |||||
| // Linear | |||||
| return getParam()->value * getParam()->displayMultiplier; | |||||
| } | |||||
| else { | |||||
| // Exponential | |||||
| return std::pow(getParam()->displayBase, getParam()->value) * getParam()->displayMultiplier; | |||||
| } | |||||
| } | |||||
| void setDisplayValue(float displayValue) override { | |||||
| if (getParam()->displayBase == 0.f) { | |||||
| // Linear | |||||
| getParam()->value = displayValue / getParam()->displayMultiplier; | |||||
| } | |||||
| else { | |||||
| // Exponential | |||||
| getParam()->value = std::log(displayValue / getParam()->displayMultiplier) / std::log(getParam()->displayBase); | |||||
| } | |||||
| } | |||||
| int getDisplayPrecision() override { | |||||
| return getParam()->displayPrecision; | |||||
| } | |||||
| std::string getLabel() override { | std::string getLabel() override { | ||||
| return getParam()->label; | return getParam()->label; | ||||
| } | } | ||||
| std::string getUnit() override { | std::string getUnit() override { | ||||
| return getParam()->unit; | return getParam()->unit; | ||||
| } | } | ||||
| int getPrecision() override { | |||||
| return getParam()->precision; | |||||
| } | |||||
| }; | }; | ||||
| @@ -12,19 +12,24 @@ struct Param { | |||||
| float minValue = 0.f; | float minValue = 0.f; | ||||
| float maxValue = 1.f; | float maxValue = 1.f; | ||||
| float defaultValue = 0.f; | float defaultValue = 0.f; | ||||
| // For formatting/displaying the value | |||||
| /** Set to 0 for linear, nonzero for exponential */ | |||||
| float displayBase = 0.f; | |||||
| float displayMultiplier = 1.f; | |||||
| int displayPrecision = 2; | |||||
| std::string label; | std::string label; | ||||
| std::string unit; | std::string unit; | ||||
| int precision = 2; | |||||
| // TODO Change this horrible method name | // TODO Change this horrible method name | ||||
| void setup(float minValue, float maxValue, float defaultValue, std::string label = "", std::string unit = "", int precision = 2) { | |||||
| void setup(float minValue, float maxValue, float defaultValue, std::string label = "", std::string unit = "", int displayPrecision = 2) { | |||||
| this->value = defaultValue; | this->value = defaultValue; | ||||
| this->minValue = minValue; | this->minValue = minValue; | ||||
| this->maxValue = maxValue; | this->maxValue = maxValue; | ||||
| this->defaultValue = defaultValue; | this->defaultValue = defaultValue; | ||||
| this->label = label; | this->label = label; | ||||
| this->unit = unit; | this->unit = unit; | ||||
| this->precision = precision; | |||||
| this->displayPrecision = displayPrecision; | |||||
| } | } | ||||
| json_t *toJson(); | json_t *toJson(); | ||||
| @@ -30,22 +30,22 @@ struct Quantity { | |||||
| /** Returns the default value, for resetting */ | /** Returns the default value, for resetting */ | ||||
| virtual float getDefaultValue() {return 0.f;} | virtual float getDefaultValue() {return 0.f;} | ||||
| /** Returns the value, possibly transformed | |||||
| /** Returns the value, possibly transformed for displaying | |||||
| Useful for logarithmic scaling, multiplying by 100 for percentages, etc. | Useful for logarithmic scaling, multiplying by 100 for percentages, etc. | ||||
| */ | */ | ||||
| virtual float getDisplayValue() {return getValue();} | virtual float getDisplayValue() {return getValue();} | ||||
| /** Sets the value by the transformed display value */ | |||||
| /** Inversely transforms the display value and sets the value */ | |||||
| virtual void setDisplayValue(float displayValue) {setValue(displayValue);} | virtual void setDisplayValue(float displayValue) {setValue(displayValue);} | ||||
| /** The number of decimal places for display | |||||
| /** The number of decimal places for displaying | |||||
| A precision of 2 will display as "1.00" for example. | A precision of 2 will display as "1.00" for example. | ||||
| */ | */ | ||||
| virtual int getPrecision() {return 2;} | |||||
| virtual int getDisplayPrecision() {return 2;} | |||||
| /** Returns a string representation of the display value */ | /** Returns a string representation of the display value */ | ||||
| virtual std::string getDisplayValueString() { | virtual std::string getDisplayValueString() { | ||||
| return string::f("%.*f", getPrecision(), getDisplayValue()); | |||||
| return string::f("%.*f", getDisplayPrecision(), getDisplayValue()); | |||||
| } | } | ||||
| /** The name of the quantity */ | /** The name of the quantity */ | ||||
| @@ -117,12 +117,6 @@ void ModuleWidget::fromJson(json_t *rootJ) { | |||||
| } | } | ||||
| } | } | ||||
| // legacy | |||||
| int legacy = 0; | |||||
| json_t *legacyJ = json_object_get(rootJ, "legacy"); | |||||
| if (legacyJ) | |||||
| legacy = json_integer_value(legacyJ); | |||||
| // params | // params | ||||
| json_t *paramsJ = json_object_get(rootJ, "params"); | json_t *paramsJ = json_object_get(rootJ, "params"); | ||||
| size_t i; | size_t i; | ||||
| @@ -112,7 +112,7 @@ struct DownloadQuantity : Quantity { | |||||
| return getValue() * 100.f; | return getValue() * 100.f; | ||||
| } | } | ||||
| int getPrecision() override {return 0;} | |||||
| int getDisplayPrecision() override {return 0;} | |||||
| std::string getLabel() override { | std::string getLabel() override { | ||||
| return "Downloading " + pluginGetDownloadName(); | return "Downloading " + pluginGetDownloadName(); | ||||
| @@ -160,7 +160,7 @@ struct WireOpacityQuantity : Quantity { | |||||
| } | } | ||||
| float getDefaultValue() override {return 0.5;} | float getDefaultValue() override {return 0.5;} | ||||
| std::string getLabel() override {return "Cable opacity";} | std::string getLabel() override {return "Cable opacity";} | ||||
| int getPrecision() override {return 0;} | |||||
| int getDisplayPrecision() override {return 0;} | |||||
| }; | }; | ||||
| @@ -173,7 +173,7 @@ struct WireTensionQuantity : Quantity { | |||||
| } | } | ||||
| float getDefaultValue() override {return 0.5;} | float getDefaultValue() override {return 0.5;} | ||||
| std::string getLabel() override {return "Cable tension";} | std::string getLabel() override {return "Cable tension";} | ||||
| int getPrecision() override {return 0;} | |||||
| int getDisplayPrecision() override {return 0;} | |||||
| }; | }; | ||||
| @@ -189,7 +189,7 @@ struct ZoomQuantity : Quantity { | |||||
| float getDefaultValue() override {return 100;} | float getDefaultValue() override {return 100;} | ||||
| std::string getLabel() override {return "Zoom";} | std::string getLabel() override {return "Zoom";} | ||||
| std::string getUnit() override {return "%";} | std::string getUnit() override {return "%";} | ||||
| int getPrecision() override {return 0;} | |||||
| int getDisplayPrecision() override {return 0;} | |||||
| }; | }; | ||||