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.

104 lines
2.8KB

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