You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.4KB

  1. #pragma once
  2. #include "string.hpp"
  3. namespace rack {
  4. /** A controller for manipulating a float value (which subclasses must store somehow) with limits and labels
  5. Often used as a decorator component for Widgets that read or write a quantity.
  6. */
  7. struct Quantity {
  8. virtual ~Quantity() {}
  9. /** Sets the value directly
  10. Override this to change the state of your subclass to represent the new value.
  11. */
  12. virtual void setValue(float value) {}
  13. /** Returns the value
  14. Override this to return the state of your subclass.
  15. */
  16. virtual float getValue() {return 0.f;}
  17. /** Returns the minimum allowed value */
  18. virtual float getMinValue() {return 0.f;}
  19. /** Returns the maximum allowed value */
  20. virtual float getMaxValue() {return 1.f;}
  21. /** Returns the default value, for resetting */
  22. virtual float getDefaultValue() {return 0.f;}
  23. /** Returns the value, possibly transformed for displaying
  24. Useful for logarithmic scaling, multiplying by 100 for percentages, etc.
  25. */
  26. virtual float getDisplayValue() {return getValue();}
  27. /** Inversely transforms the display value and sets the value */
  28. virtual void setDisplayValue(float displayValue) {setValue(displayValue);}
  29. /** The number of decimal places for displaying
  30. A precision of 2 will display as "1.00" for example.
  31. */
  32. virtual int getDisplayPrecision() {return 2;}
  33. /** Returns a string representation of the display value */
  34. virtual std::string getDisplayValueString() {
  35. return string::f("%.*f", getDisplayPrecision(), getDisplayValue());
  36. }
  37. /** The name of the quantity */
  38. virtual std::string getLabel() {return "";}
  39. /** The unit abbreviation of the quantity
  40. Include an initial space character if you want a space after the number, e.g. "440 Hz". This allows space-less units, like "100%".
  41. */
  42. virtual std::string getUnit() {return "";}
  43. /** Returns a string representation of the quantity */
  44. virtual std::string getString() {
  45. std::string s;
  46. std::string label = getLabel();
  47. if (!label.empty())
  48. s += label + ": ";
  49. s += getDisplayValueString() + getUnit();
  50. return s;
  51. }
  52. // Helper methods
  53. /** Resets the value to the default value */
  54. void reset() {
  55. setValue(getDefaultValue());
  56. }
  57. /** Checks whether the value is at the min value */
  58. bool isMin() {
  59. return getValue() <= getMinValue();
  60. }
  61. /** Checks whether the value is at the max value */
  62. bool isMax() {
  63. return getValue() >= getMaxValue();
  64. }
  65. /** Sets the value to the min value */
  66. void setMin() {
  67. setValue(getMinValue());
  68. }
  69. /** Sets the value to the max value */
  70. void setMax() {
  71. setValue(getMaxValue());
  72. }
  73. /** Sets value from the range 0 to 1 */
  74. void setScaledValue(float scaledValue) {
  75. setValue(rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue()));
  76. }
  77. /** Returns the value rescaled to the range 0 to 1 */
  78. float getScaledValue() {
  79. return rescale(getValue(), getMinValue(), getMaxValue(), 0.f, 1.f);
  80. }
  81. /** The difference between the max and min values */
  82. float getRange() {
  83. return getMaxValue() - getMinValue();
  84. }
  85. /** Checks whether the bounds are finite */
  86. bool isBounded() {
  87. return std::isfinite(getMinValue()) && std::isfinite(getMaxValue());
  88. }
  89. /** Adds an amount to the value */
  90. void moveValue(float deltaValue) {
  91. setValue(getValue() + deltaValue);
  92. }
  93. /** Adds an amount to the value scaled to the range 0 to 1 */
  94. void moveScaledValue(float deltaScaledValue) {
  95. moveValue(deltaScaledValue * getRange());
  96. }
  97. };
  98. } // namespace rack