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.

60 lines
1.2KB

  1. #pragma once
  2. #include "widgets/OpaqueWidget.hpp"
  3. #include "ui/Quantity.hpp"
  4. #include "ui/common.hpp"
  5. #include "context.hpp"
  6. namespace rack {
  7. static const float SLIDER_SENSITIVITY = 0.001f;
  8. struct Slider : OpaqueWidget {
  9. BNDwidgetState state = BND_DEFAULT;
  10. Quantity *quantity = NULL;
  11. Slider() {
  12. box.size.y = BND_WIDGET_HEIGHT;
  13. }
  14. ~Slider() {
  15. if (quantity)
  16. delete quantity;
  17. }
  18. void draw(NVGcontext *vg) override {
  19. float progress = quantity ? quantity->getScaledValue() : 0.f;
  20. std::string text = quantity ? quantity->getString() : "";
  21. bndSlider(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE, state, progress, text.c_str(), NULL);
  22. }
  23. void onDragStart(event::DragStart &e) override {
  24. state = BND_ACTIVE;
  25. context()->window->cursorLock();
  26. }
  27. void onDragMove(event::DragMove &e) override {
  28. if (quantity) {
  29. quantity->moveScaledValue(SLIDER_SENSITIVITY * e.mouseDelta.x);
  30. }
  31. }
  32. void onDragEnd(event::DragEnd &e) override {
  33. state = BND_DEFAULT;
  34. context()->window->cursorUnlock();
  35. }
  36. void onButton(event::Button &e) override {
  37. if (e.action == GLFW_PRESS && e.button == GLFW_MOUSE_BUTTON_RIGHT) {
  38. if (quantity)
  39. quantity->reset();
  40. }
  41. e.target = this;
  42. }
  43. };
  44. } // namespace rack