Browse Source

Add Quantity::toScaled() and fromScaled(). Clean up Quantity and ParamQuantity.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
90d395ae8b
4 changed files with 42 additions and 47 deletions
  1. +12
    -7
      include/Quantity.hpp
  2. +1
    -3
      include/engine/ParamQuantity.hpp
  3. +22
    -14
      src/Quantity.cpp
  4. +7
    -23
      src/engine/ParamQuantity.cpp

+ 12
- 7
include/Quantity.hpp View File

@@ -40,7 +40,7 @@ struct Quantity {
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.
*/
virtual float getDisplayValue();
@@ -90,16 +90,21 @@ struct Quantity {
void setMax();
/** Sets the value to max if the current value is min, otherwise sets the value to min. */
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. */
float getRange();
/** Checks whether the bounds are finite. */
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. */
void moveScaledValue(float deltaScaledValue);
};


+ 1
- 3
include/engine/ParamQuantity.hpp View File

@@ -64,11 +64,9 @@ struct ParamQuantity : Quantity {
bool snapEnabled = false;

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);
float getSmoothValue();
void setSmoothScaledValue(float scaledValue);
float getSmoothScaledValue();

void setValue(float value) override;
float getValue() override;


+ 22
- 14
src/Quantity.cpp View File

@@ -87,32 +87,40 @@ void Quantity::toggle() {
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())
return getValue();
return value;
else if (getMinValue() == getMaxValue())
return 0.f;
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) {


+ 7
- 23
src/engine/ParamQuantity.cpp View File

@@ -11,8 +11,10 @@ namespace engine {


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];
}

@@ -37,22 +39,6 @@ float ParamQuantity::getSmoothValue() {
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) {
if (!module)
return;
@@ -81,8 +67,7 @@ float ParamQuantity::getDefaultValue() {
}

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();
if (displayBase == 0.f) {
// Linear
@@ -100,9 +85,6 @@ float ParamQuantity::getDisplayValue() {
}

void ParamQuantity::setDisplayValue(float displayValue) {
if (!module)
return;

// Handle displayOffset
float v = displayValue - displayOffset;

@@ -129,6 +111,7 @@ void ParamQuantity::setDisplayValue(float displayValue) {
if (std::isnan(v))
return;

// Set the value directly without smoothing
setValue(v);
}

@@ -201,6 +184,7 @@ std::string SwitchQuantity::getDisplayValueString() {
}

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) {
return string::lowercase(a) == string::lowercase(s);
});


Loading…
Cancel
Save