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.

131 lines
2.6KB

  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. struct Switch::Internal {
  9. /** Whether momentary switch was pressed this frame. */
  10. bool momentaryPressed = false;
  11. /** Whether momentary switch was released this frame. */
  12. bool momentaryReleased = false;
  13. };
  14. Switch::Switch() {
  15. internal = new Internal;
  16. }
  17. Switch::~Switch() {
  18. delete internal;
  19. }
  20. void Switch::initParamQuantity() {
  21. ParamWidget::initParamQuantity();
  22. engine::ParamQuantity* pq = getParamQuantity();
  23. if (pq) {
  24. pq->snapEnabled = true;
  25. pq->smoothEnabled = false;
  26. if (momentary) {
  27. pq->resetEnabled = false;
  28. pq->randomizeEnabled = false;
  29. }
  30. }
  31. }
  32. void Switch::step() {
  33. engine::ParamQuantity* pq = getParamQuantity();
  34. if (internal->momentaryPressed) {
  35. internal->momentaryPressed = false;
  36. // Wait another frame.
  37. }
  38. else if (internal->momentaryReleased) {
  39. internal->momentaryReleased = false;
  40. if (pq) {
  41. // Set to minimum value
  42. pq->setMin();
  43. }
  44. }
  45. ParamWidget::step();
  46. }
  47. void Switch::onDoubleClick(const DoubleClickEvent& e) {
  48. // Don't reset parameter on double-click
  49. OpaqueWidget::onDoubleClick(e);
  50. }
  51. void Switch::onDragStart(const DragStartEvent& e) {
  52. ParamWidget::onDragStart(e);
  53. if (e.button != GLFW_MOUSE_BUTTON_LEFT)
  54. return;
  55. engine::ParamQuantity* pq = getParamQuantity();
  56. if (momentary) {
  57. internal->momentaryPressed = true;
  58. if (pq) {
  59. // Set to maximum value
  60. pq->setMax();
  61. }
  62. }
  63. else {
  64. if (pq) {
  65. float oldValue = pq->getValue();
  66. int mods = APP->window->getMods();
  67. if ((mods & RACK_MOD_MASK) == 0) {
  68. if (pq->isMax()) {
  69. // Reset value back to minimum
  70. pq->setMin();
  71. }
  72. else {
  73. // Increment value by 1
  74. pq->setValue(std::round(pq->getValue()) + 1.f);
  75. }
  76. }
  77. else if ((mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  78. if (pq->isMin()) {
  79. // Reset value back to maximum
  80. pq->setMax();
  81. }
  82. else {
  83. // Decrement value by 1
  84. pq->setValue(std::round(pq->getValue()) - 1.f);
  85. }
  86. }
  87. float newValue = pq->getValue();
  88. if (oldValue != newValue) {
  89. // Push ParamChange history action
  90. history::ParamChange* h = new history::ParamChange;
  91. h->name = string::translate("Switch.history.move");
  92. h->moduleId = module->id;
  93. h->paramId = paramId;
  94. h->oldValue = oldValue;
  95. h->newValue = newValue;
  96. APP->history->push(h);
  97. }
  98. }
  99. }
  100. }
  101. void Switch::onDragEnd(const DragEndEvent& e) {
  102. ParamWidget::onDragEnd(e);
  103. if (e.button != GLFW_MOUSE_BUTTON_LEFT)
  104. return;
  105. if (momentary) {
  106. internal->momentaryReleased = true;
  107. }
  108. }
  109. } // namespace app
  110. } // namespace rack