From 418499159b6bd72811ffd3238ad94959dda94c43 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 11 Jan 2019 04:25:12 -0500 Subject: [PATCH] Add snapping to Knob --- include/app/Knob.hpp | 3 +++ include/app/ParamWidget.hpp | 1 - include/componentlibrary.hpp | 8 ++------ src/app/Knob.cpp | 17 ++++++++++++++--- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/app/Knob.hpp b/include/app/Knob.hpp index 8c48561e..ea14c02d 100644 --- a/include/app/Knob.hpp +++ b/include/app/Knob.hpp @@ -12,6 +12,9 @@ struct Knob : ParamWidget { /** Multiplier for mouse movement to adjust knob value */ float speed = 1.0; float oldValue = 0.f; + /** Enable snapping at integer values */ + bool snap = false; + float snapValue = NAN; void onButton(const event::Button &e) override; void onDragStart(const event::DragStart &e) override; diff --git a/include/app/ParamWidget.hpp b/include/app/ParamWidget.hpp index f28d24d3..58de6d64 100644 --- a/include/app/ParamWidget.hpp +++ b/include/app/ParamWidget.hpp @@ -13,7 +13,6 @@ struct ParamWidget : OpaqueWidget { ParamQuantity *paramQuantity = NULL; float dirtyValue = NAN; Tooltip *tooltip = NULL; - bool snap = false; ~ParamWidget(); void step() override; diff --git a/include/componentlibrary.hpp b/include/componentlibrary.hpp index a7ccb829..571587ae 100644 --- a/include/componentlibrary.hpp +++ b/include/componentlibrary.hpp @@ -59,9 +59,7 @@ struct RoundHugeBlackKnob : RoundKnob { struct RoundBlackSnapKnob : RoundBlackKnob { RoundBlackSnapKnob() { - // TODO - // quantity.snap = true; - // quantity.smooth = false; + snap = true; } }; @@ -309,9 +307,7 @@ struct BefacoBigKnob : SVGKnob { struct BefacoBigSnapKnob : BefacoBigKnob { BefacoBigSnapKnob() { - // TODO - // quantity.snap = true; - // quantity.smooth = false; + snap = true; } }; diff --git a/src/app/Knob.cpp b/src/app/Knob.cpp index e19c0ec5..b47c8226 100644 --- a/src/app/Knob.cpp +++ b/src/app/Knob.cpp @@ -20,8 +20,12 @@ void Knob::onButton(const event::Button &e) { } void Knob::onDragStart(const event::DragStart &e) { - if (paramQuantity) + if (paramQuantity) { oldValue = paramQuantity->getValue(); + if (snap) { + snapValue = oldValue; + } + } app()->window->cursorLock(); } @@ -58,8 +62,15 @@ void Knob::onDragMove(const event::DragMove &e) { // Drag slower if Mod is held if (app()->window->isModPressed()) delta /= 16.f; - float oldValue = paramQuantity->getSmoothValue(); - paramQuantity->setSmoothValue(oldValue + delta); + + if (snap) { + snapValue += delta; + snapValue = math::clamp(snapValue, paramQuantity->getMinValue(), paramQuantity->getMaxValue()); + paramQuantity->setValue(std::round(snapValue)); + } + else { + paramQuantity->setSmoothValue(paramQuantity->getSmoothValue() + delta); + } } ParamWidget::onDragMove(e);