| @@ -40,7 +40,7 @@ struct Quantity { | |||||
| return 0.f; | return 0.f; | ||||
| } | } | ||||
| /** Returns the value, possibly transformed for displaying. | |||||
| /** Returns the value, 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(); | virtual float getDisplayValue(); | ||||
| @@ -90,16 +90,21 @@ struct Quantity { | |||||
| void setMax(); | void setMax(); | ||||
| /** Sets the value to max if the current value is min, otherwise sets the value to min. */ | /** Sets the value to max if the current value is min, otherwise sets the value to min. */ | ||||
| void toggle(); | void toggle(); | ||||
| /** Sets value from the range 0 to 1. */ | |||||
| void setScaledValue(float scaledValue); | |||||
| /** Returns the value rescaled to the range 0 to 1. */ | |||||
| float getScaledValue(); | |||||
| /** Adds an amount to the value. */ | |||||
| void moveValue(float deltaValue); | |||||
| /** The difference between the max and min values. */ | /** The difference between the max and min values. */ | ||||
| float getRange(); | float getRange(); | ||||
| /** Checks whether the bounds are finite. */ | /** Checks whether the bounds are finite. */ | ||||
| bool isBounded(); | bool isBounded(); | ||||
| /** Adds an amount to the value. */ | |||||
| void moveValue(float deltaValue); | |||||
| /** Transforms a value to the range 0 to 1. */ | |||||
| float toScaled(float value); | |||||
| /** Transforms a value from the range 0 to 1. */ | |||||
| float fromScaled(float scaledValue); | |||||
| /** Sets value from the range 0 to 1. */ | |||||
| void setScaledValue(float scaledValue); | |||||
| /** Returns the value scaled to the range 0 to 1. */ | |||||
| float getScaledValue(); | |||||
| /** Adds an amount to the value scaled to the range 0 to 1. */ | /** Adds an amount to the value scaled to the range 0 to 1. */ | ||||
| void moveScaledValue(float deltaScaledValue); | void moveScaledValue(float deltaScaledValue); | ||||
| }; | }; | ||||
| @@ -64,11 +64,9 @@ struct ParamQuantity : Quantity { | |||||
| bool snapEnabled = false; | bool snapEnabled = false; | ||||
| Param* getParam(); | Param* getParam(); | ||||
| /** Request to the engine to smoothly set the value */ | |||||
| /** If smoothEnabled is true, requests to the engine to smoothly set the value. */ | |||||
| void setSmoothValue(float value); | void setSmoothValue(float value); | ||||
| float getSmoothValue(); | float getSmoothValue(); | ||||
| void setSmoothScaledValue(float scaledValue); | |||||
| float getSmoothScaledValue(); | |||||
| void setValue(float value) override; | void setValue(float value) override; | ||||
| float getValue() override; | float getValue() override; | ||||
| @@ -87,32 +87,40 @@ void Quantity::toggle() { | |||||
| setValue(isMin() ? getMaxValue() : getMinValue()); | setValue(isMin() ? getMaxValue() : getMinValue()); | ||||
| } | } | ||||
| void Quantity::setScaledValue(float scaledValue) { | |||||
| if (!isBounded()) | |||||
| setValue(scaledValue); | |||||
| else | |||||
| setValue(math::rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue())); | |||||
| void Quantity::moveValue(float deltaValue) { | |||||
| setValue(getValue() + deltaValue); | |||||
| } | } | ||||
| float Quantity::getScaledValue() { | |||||
| float Quantity::getRange() { | |||||
| return getMaxValue() - getMinValue(); | |||||
| } | |||||
| bool Quantity::isBounded() { | |||||
| return std::isfinite(getMinValue()) && std::isfinite(getMaxValue()); | |||||
| } | |||||
| float Quantity::toScaled(float value) { | |||||
| if (!isBounded()) | if (!isBounded()) | ||||
| return getValue(); | |||||
| return value; | |||||
| else if (getMinValue() == getMaxValue()) | else if (getMinValue() == getMaxValue()) | ||||
| return 0.f; | return 0.f; | ||||
| else | else | ||||
| return math::rescale(getValue(), getMinValue(), getMaxValue(), 0.f, 1.f); | |||||
| return math::rescale(value, getMinValue(), getMaxValue(), 0.f, 1.f); | |||||
| } | } | ||||
| float Quantity::getRange() { | |||||
| return getMaxValue() - getMinValue(); | |||||
| float Quantity::fromScaled(float scaledValue) { | |||||
| if (!isBounded()) | |||||
| return scaledValue; | |||||
| else | |||||
| return math::rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue()); | |||||
| } | } | ||||
| bool Quantity::isBounded() { | |||||
| return std::isfinite(getMinValue()) && std::isfinite(getMaxValue()); | |||||
| void Quantity::setScaledValue(float scaledValue) { | |||||
| setValue(fromScaled(scaledValue)); | |||||
| } | } | ||||
| void Quantity::moveValue(float deltaValue) { | |||||
| setValue(getValue() + deltaValue); | |||||
| float Quantity::getScaledValue() { | |||||
| return toScaled(getValue()); | |||||
| } | } | ||||
| void Quantity::moveScaledValue(float deltaScaledValue) { | void Quantity::moveScaledValue(float deltaScaledValue) { | ||||
| @@ -11,8 +11,10 @@ namespace engine { | |||||
| engine::Param* ParamQuantity::getParam() { | engine::Param* ParamQuantity::getParam() { | ||||
| assert(module); | |||||
| assert(0 <= paramId && paramId < (int) module->params.size()); | |||||
| if (!module) | |||||
| return NULL; | |||||
| if (!(0 <= paramId && paramId < (int) module->params.size())) | |||||
| return NULL; | |||||
| return &module->params[paramId]; | return &module->params[paramId]; | ||||
| } | } | ||||
| @@ -37,22 +39,6 @@ float ParamQuantity::getSmoothValue() { | |||||
| return APP->engine->getParamValue(module, paramId); | return APP->engine->getParamValue(module, paramId); | ||||
| } | } | ||||
| void ParamQuantity::setSmoothScaledValue(float scaledValue) { | |||||
| if (!isBounded()) | |||||
| setSmoothValue(scaledValue); | |||||
| else | |||||
| setSmoothValue(math::rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue())); | |||||
| } | |||||
| float ParamQuantity::getSmoothScaledValue() { | |||||
| if (!isBounded()) | |||||
| return getSmoothValue(); | |||||
| else if (getMinValue() == getMaxValue()) | |||||
| return 0.f; | |||||
| else | |||||
| return math::rescale(getSmoothValue(), getMinValue(), getMaxValue(), 0.f, 1.f); | |||||
| } | |||||
| void ParamQuantity::setValue(float value) { | void ParamQuantity::setValue(float value) { | ||||
| if (!module) | if (!module) | ||||
| return; | return; | ||||
| @@ -81,8 +67,7 @@ float ParamQuantity::getDefaultValue() { | |||||
| } | } | ||||
| float ParamQuantity::getDisplayValue() { | float ParamQuantity::getDisplayValue() { | ||||
| if (!module) | |||||
| return Quantity::getDisplayValue(); | |||||
| // We don't want the text to be smoothed (animated), so get the smooth target value. | |||||
| float v = getSmoothValue(); | float v = getSmoothValue(); | ||||
| if (displayBase == 0.f) { | if (displayBase == 0.f) { | ||||
| // Linear | // Linear | ||||
| @@ -100,9 +85,6 @@ float ParamQuantity::getDisplayValue() { | |||||
| } | } | ||||
| void ParamQuantity::setDisplayValue(float displayValue) { | void ParamQuantity::setDisplayValue(float displayValue) { | ||||
| if (!module) | |||||
| return; | |||||
| // Handle displayOffset | // Handle displayOffset | ||||
| float v = displayValue - displayOffset; | float v = displayValue - displayOffset; | ||||
| @@ -129,6 +111,7 @@ void ParamQuantity::setDisplayValue(float displayValue) { | |||||
| if (std::isnan(v)) | if (std::isnan(v)) | ||||
| return; | return; | ||||
| // Set the value directly without smoothing | |||||
| setValue(v); | setValue(v); | ||||
| } | } | ||||
| @@ -201,6 +184,7 @@ std::string SwitchQuantity::getDisplayValueString() { | |||||
| } | } | ||||
| void SwitchQuantity::setDisplayValueString(std::string s) { | void SwitchQuantity::setDisplayValueString(std::string s) { | ||||
| // Find label that matches string, case insensitive. | |||||
| auto it = std::find_if(labels.begin(), labels.end(), [&](const std::string& a) { | auto it = std::find_if(labels.begin(), labels.end(), [&](const std::string& a) { | ||||
| return string::lowercase(a) == string::lowercase(s); | return string::lowercase(a) == string::lowercase(s); | ||||
| }); | }); | ||||