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.

94 lines
1.9KB

  1. #include <app/Switch.hpp>
  2. #include <app.hpp>
  3. #include <app/Scene.hpp>
  4. #include <random.hpp>
  5. #include <history.hpp>
  6. namespace rack {
  7. namespace app {
  8. void Switch::step() {
  9. if (momentaryPressed) {
  10. momentaryPressed = false;
  11. // Wait another frame.
  12. }
  13. else if (momentaryReleased) {
  14. momentaryReleased = false;
  15. if (paramQuantity) {
  16. // Set to minimum value
  17. paramQuantity->setMin();
  18. }
  19. }
  20. ParamWidget::step();
  21. }
  22. void Switch::onDoubleClick(const event::DoubleClick& e) {
  23. // Don't reset parameter on double-click
  24. }
  25. void Switch::onDragStart(const event::DragStart& e) {
  26. if (e.button != GLFW_MOUSE_BUTTON_LEFT)
  27. return;
  28. if (momentary) {
  29. if (paramQuantity) {
  30. // Set to maximum value
  31. paramQuantity->setMax();
  32. momentaryPressed = true;
  33. }
  34. }
  35. else {
  36. if (paramQuantity) {
  37. float oldValue = paramQuantity->getValue();
  38. if (paramQuantity->isMax()) {
  39. // Reset value back to minimum
  40. paramQuantity->setMin();
  41. }
  42. else {
  43. // Increment value by 1
  44. paramQuantity->setValue(std::round(paramQuantity->getValue()) + 1.f);
  45. }
  46. float newValue = paramQuantity->getValue();
  47. if (oldValue != newValue) {
  48. // Push ParamChange history action
  49. history::ParamChange* h = new history::ParamChange;
  50. h->name = "move switch";
  51. h->moduleId = paramQuantity->module->id;
  52. h->paramId = paramQuantity->paramId;
  53. h->oldValue = oldValue;
  54. h->newValue = newValue;
  55. APP->history->push(h);
  56. }
  57. }
  58. }
  59. }
  60. void Switch::onDragEnd(const event::DragEnd& e) {
  61. if (e.button != GLFW_MOUSE_BUTTON_LEFT)
  62. return;
  63. if (momentary) {
  64. momentaryReleased = true;
  65. }
  66. }
  67. void Switch::reset() {
  68. if (paramQuantity && !momentary) {
  69. paramQuantity->reset();
  70. }
  71. }
  72. void Switch::randomize() {
  73. if (paramQuantity && !momentary) {
  74. float value = paramQuantity->getMinValue() + std::floor(random::uniform() * (paramQuantity->getRange() + 1));
  75. paramQuantity->setValue(value);
  76. }
  77. }
  78. } // namespace app
  79. } // namespace rack