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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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() {
  17. return 0.f;
  18. }
  19. /** Returns the minimum allowed value */
  20. virtual float getMinValue() {
  21. return 0.f;
  22. }
  23. /** Returns the maximum allowed value */
  24. virtual float getMaxValue() {
  25. return 1.f;
  26. }
  27. /** Returns the default value, for resetting */
  28. virtual float getDefaultValue() {
  29. return 0.f;
  30. }
  31. /** Returns the value, possibly transformed for displaying
  32. Useful for logarithmic scaling, multiplying by 100 for percentages, etc.
  33. */
  34. virtual float getDisplayValue() {
  35. return getValue();
  36. }
  37. /** Inversely transforms the display value and sets the value */
  38. virtual void setDisplayValue(float displayValue) {
  39. setValue(displayValue);
  40. }
  41. /** The number of total decimal places for generating the display value string
  42. */
  43. virtual int getDisplayPrecision();
  44. /** Returns a string representation of the display value */
  45. virtual std::string getDisplayValueString();
  46. virtual void setDisplayValueString(std::string s);
  47. /** The name of the quantity */
  48. virtual std::string getLabel() {
  49. return "";
  50. }
  51. /** The unit abbreviation of the quantity
  52. Include an initial space character if you want a space after the number, e.g. "440 Hz". This allows space-less units, like "100%".
  53. */
  54. virtual std::string getUnit() {
  55. return "";
  56. }
  57. /** Returns a string representation of the quantity */
  58. virtual std::string getString();
  59. // Helper methods
  60. /** Resets the value to the default value */
  61. void reset() {
  62. setValue(getDefaultValue());
  63. }
  64. /** Checks whether the value is at the min value */
  65. bool isMin() {
  66. return getValue() <= getMinValue();
  67. }
  68. /** Checks whether the value is at the max value */
  69. bool isMax() {
  70. return getValue() >= getMaxValue();
  71. }
  72. /** Sets the value to the min value */
  73. void setMin() {
  74. setValue(getMinValue());
  75. }
  76. /** Sets the value to the max value */
  77. void setMax() {
  78. setValue(getMaxValue());
  79. }
  80. /** Sets value from the range 0 to 1 */
  81. void setScaledValue(float scaledValue) {
  82. setValue(math::rescale(scaledValue, 0.f, 1.f, getMinValue(), getMaxValue()));
  83. }
  84. /** Returns the value rescaled to the range 0 to 1 */
  85. float getScaledValue() {
  86. return math::rescale(getValue(), getMinValue(), getMaxValue(), 0.f, 1.f);
  87. }
  88. /** The difference between the max and min values */
  89. float getRange() {
  90. return getMaxValue() - getMinValue();
  91. }
  92. /** Checks whether the bounds are finite */
  93. bool isBounded() {
  94. return std::isfinite(getMinValue()) && std::isfinite(getMaxValue());
  95. }
  96. /** Adds an amount to the value */
  97. void moveValue(float deltaValue) {
  98. setValue(getValue() + deltaValue);
  99. }
  100. /** Adds an amount to the value scaled to the range 0 to 1 */
  101. void moveScaledValue(float deltaScaledValue) {
  102. moveValue(deltaScaledValue * getRange());
  103. }
  104. };
  105. } // namespace rack