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 */
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;


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

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

~ParamWidget();
void step() override;


+ 2
- 6
include/componentlibrary.hpp View File

@@ -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;
}
};



+ 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) {
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);


Loading…
Cancel
Save