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.

Quantity.hpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include <math.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 widget::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 total decimal places for generating the display value string
  30. */
  31. virtual int getDisplayPrecision();
  32. /** Returns a string representation of the display value */
  33. virtual std::string getDisplayValueString();
  34. virtual void setDisplayValueString(std::string s);
  35. /** The name of the quantity */
  36. virtual std::string getLabel() {return "";}
  37. /** The unit abbreviation of the quantity
  38. Include an initial space character if you want a space after the number, e.g. "440 Hz". This allows space-less units, like "100%".
  39. */
  40. virtual std::string getUnit() {return "";}
  41. /** Returns a string representation of the quantity */
  42. virtual std::string getString();
  43. // Helper methods
  44. /** Resets the value to the default value */
  45. void reset() {
  46. setValue(getDefaultValue());
  47. }
  48. /** Checks whether the value is at the min value */
  49. bool isMin() {
  50. return getValue() <= getMinValue();
  51. }
  52. /** Checks whether the value is at the max value */
  53. bool isMax() {
  54. return getValue() >= getMaxValue();
  55. }
  56. /** Sets the value to the min value */
  57. void setMin() {
  58. setValue(getMinValue());
  59. }
  60. /** Sets the value to the max value */
  61. void setMax() {
  62. setValue(getMaxValue());
  63. }
  64. /** Sets value from the range 0 to 1 */
  65. void setScaledValue(float scaledValue) {
  66. setValue(math::rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue()));
  67. }
  68. /** Returns the value rescaled to the range 0 to 1 */
  69. float getScaledValue() {
  70. return math::rescale(getValue(), getMinValue(), getMaxValue(), 0.f, 1.f);
  71. }
  72. /** The difference between the max and min values */
  73. float getRange() {
  74. return getMaxValue() - getMinValue();
  75. }
  76. /** Checks whether the bounds are finite */
  77. bool isBounded() {
  78. return std::isfinite(getMinValue()) && std::isfinite(getMaxValue());
  79. }
  80. /** Adds an amount to the value */
  81. void moveValue(float deltaValue) {
  82. setValue(getValue() + deltaValue);
  83. }
  84. /** Adds an amount to the value scaled to the range 0 to 1 */
  85. void moveScaledValue(float deltaScaledValue) {
  86. moveValue(deltaScaledValue * getRange());
  87. }
  88. };
  89. } // namespace rack