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.

102 lines
2.2KB

  1. #include "app/Knob.hpp"
  2. #include "app.hpp"
  3. #include "app/Scene.hpp"
  4. #include "history.hpp"
  5. namespace rack {
  6. namespace app {
  7. static const float KNOB_SENSITIVITY = 0.0015f;
  8. void Knob::onHover(const event::Hover &e) {
  9. math::Vec c = box.size.div(2);
  10. float dist = e.pos.minus(c).norm();
  11. if (dist <= c.x) {
  12. ParamWidget::onHover(e);
  13. }
  14. }
  15. void Knob::onButton(const event::Button &e) {
  16. math::Vec c = box.size.div(2);
  17. float dist = e.pos.minus(c).norm();
  18. if (dist <= c.x) {
  19. ParamWidget::onButton(e);
  20. }
  21. }
  22. void Knob::onDragStart(const event::DragStart &e) {
  23. if (paramQuantity) {
  24. oldValue = paramQuantity->getSmoothValue();
  25. if (snap) {
  26. snapValue = paramQuantity->getValue();
  27. }
  28. }
  29. APP->window->cursorLock();
  30. }
  31. void Knob::onDragEnd(const event::DragEnd &e) {
  32. APP->window->cursorUnlock();
  33. if (paramQuantity) {
  34. float newValue = paramQuantity->getSmoothValue();
  35. if (oldValue != newValue) {
  36. // Push ParamChange history action
  37. history::ParamChange *h = new history::ParamChange;
  38. h->moduleId = paramQuantity->module->id;
  39. h->paramId = paramQuantity->paramId;
  40. h->oldValue = oldValue;
  41. h->newValue = newValue;
  42. APP->history->push(h);
  43. }
  44. }
  45. }
  46. void Knob::onDragMove(const event::DragMove &e) {
  47. if (paramQuantity) {
  48. float range;
  49. if (paramQuantity->isBounded()) {
  50. range = paramQuantity->getRange();
  51. }
  52. else {
  53. // Continuous encoders scale as if their limits are +/-1
  54. range = 2.f;
  55. }
  56. float delta = (horizontal ? e.mouseDelta.x : -e.mouseDelta.y);
  57. delta *= KNOB_SENSITIVITY;
  58. delta *= speed;
  59. delta *= range;
  60. // Drag slower if mod is held
  61. int mods = APP->window->getMods();
  62. if ((mods & WINDOW_MOD_MASK) == WINDOW_MOD_CTRL) {
  63. delta /= 16.f;
  64. }
  65. // Drag even slower if mod+shift is held
  66. if ((mods & WINDOW_MOD_MASK) == (WINDOW_MOD_CTRL | GLFW_MOD_SHIFT)) {
  67. delta /= 256.f;
  68. }
  69. if (snap) {
  70. snapValue += delta;
  71. snapValue = math::clamp(snapValue, paramQuantity->getMinValue(), paramQuantity->getMaxValue());
  72. paramQuantity->setValue(std::round(snapValue));
  73. }
  74. else if (smooth) {
  75. paramQuantity->setSmoothValue(paramQuantity->getSmoothValue() + delta);
  76. }
  77. else {
  78. paramQuantity->setValue(paramQuantity->getValue() + delta);
  79. }
  80. }
  81. ParamWidget::onDragMove(e);
  82. }
  83. } // namespace app
  84. } // namespace rack