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.

78 lines
1.5KB

  1. #include <app/SvgSlider.hpp>
  2. namespace rack {
  3. namespace app {
  4. SvgSlider::SvgSlider() {
  5. fb = new widget::FramebufferWidget;
  6. addChild(fb);
  7. background = new widget::SvgWidget;
  8. fb->addChild(background);
  9. handle = new widget::SvgWidget;
  10. fb->addChild(handle);
  11. speed = 2.0;
  12. }
  13. void SvgSlider::setBackgroundSvg(std::shared_ptr<window::Svg> svg) {
  14. if (svg == background->svg)
  15. return;
  16. background->setSvg(svg);
  17. box.size = background->box.size;
  18. fb->box.size = background->box.size;
  19. fb->setDirty();
  20. }
  21. void SvgSlider::setHandleSvg(std::shared_ptr<window::Svg> svg) {
  22. if (svg == handle->svg)
  23. return;
  24. handle->setSvg(svg);
  25. handle->box.pos = maxHandlePos;
  26. fb->setDirty();
  27. }
  28. void SvgSlider::setHandlePos(math::Vec minHandlePos, math::Vec maxHandlePos) {
  29. this->minHandlePos = minHandlePos;
  30. this->maxHandlePos = maxHandlePos;
  31. // Set handle pos to maximum by default
  32. handle->box.pos = maxHandlePos;
  33. }
  34. void SvgSlider::setHandlePosCentered(math::Vec minHandlePosCentered, math::Vec maxHandlePosCentered) {
  35. setHandlePos(
  36. minHandlePosCentered.minus(handle->box.size.div(2)),
  37. maxHandlePosCentered.minus(handle->box.size.div(2))
  38. );
  39. }
  40. void SvgSlider::onChange(const ChangeEvent& e) {
  41. // Default position is max value
  42. float v = 1.f;
  43. engine::ParamQuantity* pq = getParamQuantity();
  44. if (pq) {
  45. v = pq->getScaledValue();
  46. }
  47. // Interpolate handle position
  48. handle->box.pos = minHandlePos.crossfade(maxHandlePos, v);
  49. fb->setDirty();
  50. ParamWidget::onChange(e);
  51. }
  52. } // namespace app
  53. } // namespace rack