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.

109 lines
2.9KB

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