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.

96 lines
2.1KB

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