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.

97 lines
1.8KB

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