Browse Source

Add snapping to Knob

tags/v1.0.0
Andrew Belt 5 years ago
parent
commit
418499159b
4 changed files with 19 additions and 10 deletions
  1. +3
    -0
      include/app/Knob.hpp
  2. +0
    -1
      include/app/ParamWidget.hpp
  3. +2
    -6
      include/componentlibrary.hpp
  4. +14
    -3
      src/app/Knob.cpp

+ 3
- 0
include/app/Knob.hpp View File

@@ -12,6 +12,9 @@ struct Knob : ParamWidget {
/** Multiplier for mouse movement to adjust knob value */ /** Multiplier for mouse movement to adjust knob value */
float speed = 1.0; float speed = 1.0;
float oldValue = 0.f; float oldValue = 0.f;
/** Enable snapping at integer values */
bool snap = false;
float snapValue = NAN;


void onButton(const event::Button &e) override; void onButton(const event::Button &e) override;
void onDragStart(const event::DragStart &e) override; void onDragStart(const event::DragStart &e) override;


+ 0
- 1
include/app/ParamWidget.hpp View File

@@ -13,7 +13,6 @@ struct ParamWidget : OpaqueWidget {
ParamQuantity *paramQuantity = NULL; ParamQuantity *paramQuantity = NULL;
float dirtyValue = NAN; float dirtyValue = NAN;
Tooltip *tooltip = NULL; Tooltip *tooltip = NULL;
bool snap = false;


~ParamWidget(); ~ParamWidget();
void step() override; void step() override;


+ 2
- 6
include/componentlibrary.hpp View File

@@ -59,9 +59,7 @@ struct RoundHugeBlackKnob : RoundKnob {


struct RoundBlackSnapKnob : RoundBlackKnob { struct RoundBlackSnapKnob : RoundBlackKnob {
RoundBlackSnapKnob() { RoundBlackSnapKnob() {
// TODO
// quantity.snap = true;
// quantity.smooth = false;
snap = true;
} }
}; };


@@ -309,9 +307,7 @@ struct BefacoBigKnob : SVGKnob {


struct BefacoBigSnapKnob : BefacoBigKnob { struct BefacoBigSnapKnob : BefacoBigKnob {
BefacoBigSnapKnob() { BefacoBigSnapKnob() {
// TODO
// quantity.snap = true;
// quantity.smooth = false;
snap = true;
} }
}; };




+ 14
- 3
src/app/Knob.cpp View File

@@ -20,8 +20,12 @@ void Knob::onButton(const event::Button &e) {
} }


void Knob::onDragStart(const event::DragStart &e) { void Knob::onDragStart(const event::DragStart &e) {
if (paramQuantity)
if (paramQuantity) {
oldValue = paramQuantity->getValue(); oldValue = paramQuantity->getValue();
if (snap) {
snapValue = oldValue;
}
}


app()->window->cursorLock(); app()->window->cursorLock();
} }
@@ -58,8 +62,15 @@ void Knob::onDragMove(const event::DragMove &e) {
// Drag slower if Mod is held // Drag slower if Mod is held
if (app()->window->isModPressed()) if (app()->window->isModPressed())
delta /= 16.f; 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); ParamWidget::onDragMove(e);


Loading…
Cancel
Save