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.

98 lines
1.9KB

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