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.

48 lines
982B

  1. #pragma once
  2. #include "app/common.hpp"
  3. #include "app/ParamWidget.hpp"
  4. #include "context.hpp"
  5. namespace rack {
  6. static const float KNOB_SENSITIVITY = 0.0015f;
  7. /** Implements vertical dragging behavior for ParamWidgets */
  8. struct Knob : ParamWidget {
  9. /** Multiplier for mouse movement to adjust knob value */
  10. float speed = 1.0;
  11. void onDragStart(event::DragStart &e) override {
  12. context()->window->cursorLock();
  13. }
  14. void onDragEnd(event::DragEnd &e) override {
  15. context()->window->cursorUnlock();
  16. }
  17. void onDragMove(event::DragMove &e) override {
  18. if (quantity) {
  19. float range;
  20. if (quantity->isBounded()) {
  21. range = quantity->getRange();
  22. }
  23. else {
  24. // Continuous encoders scale as if their limits are +/-1
  25. range = 2.f;
  26. }
  27. float delta = KNOB_SENSITIVITY * -e.mouseDelta.y * speed * range;
  28. // Drag slower if Mod is held
  29. if (context()->window->isModPressed())
  30. delta /= 16.f;
  31. quantity->moveValue(delta);
  32. }
  33. }
  34. };
  35. } // namespace rack