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.

124 lines
3.2KB

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