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.

117 lines
2.3KB

  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. if (pq->isMax()) {
  67. // Reset value back to minimum
  68. pq->setMin();
  69. }
  70. else {
  71. // Increment value by 1
  72. pq->setValue(std::round(pq->getValue()) + 1.f);
  73. }
  74. float newValue = pq->getValue();
  75. if (oldValue != newValue) {
  76. // Push ParamChange history action
  77. history::ParamChange* h = new history::ParamChange;
  78. h->name = "move switch";
  79. h->moduleId = module->id;
  80. h->paramId = paramId;
  81. h->oldValue = oldValue;
  82. h->newValue = newValue;
  83. APP->history->push(h);
  84. }
  85. }
  86. }
  87. }
  88. void Switch::onDragEnd(const DragEndEvent& e) {
  89. ParamWidget::onDragEnd(e);
  90. if (e.button != GLFW_MOUSE_BUTTON_LEFT)
  91. return;
  92. if (momentary) {
  93. internal->momentaryReleased = true;
  94. }
  95. }
  96. } // namespace app
  97. } // namespace rack