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.

50 lines
1.0KB

  1. #pragma once
  2. #include "widgets/EventWidget.hpp"
  3. namespace rack {
  4. /** A Widget representing a float value */
  5. struct QuantityWidget : virtual EventWidget {
  6. float value = 0.0;
  7. float minValue = 0.0;
  8. float maxValue = 1.0;
  9. float defaultValue = 0.0;
  10. std::string label;
  11. /** Include a space character if you want a space after the number, e.g. " Hz" */
  12. std::string unit;
  13. /** The decimal place to round for displaying values.
  14. A precision of 2 will display as "1.00" for example.
  15. */
  16. int precision = 2;
  17. void reset() {
  18. setValue(defaultValue);
  19. }
  20. void setValue(float value) {
  21. this->value = math::clampBetween(value, minValue, maxValue);
  22. event::Change e;
  23. on(e);
  24. }
  25. void setLimits(float minValue, float maxValue) {
  26. this->minValue = minValue;
  27. this->maxValue = maxValue;
  28. setValue(value);
  29. }
  30. void setDefaultValue(float defaultValue) {
  31. this->defaultValue = defaultValue;
  32. }
  33. /** Generates the display value */
  34. std::string getText() {
  35. return string::stringf("%s: %.*f%s", label.c_str(), precision, value, unit.c_str());
  36. }
  37. };
  38. } // namespace rack