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.

122 lines
3.2KB

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