Skip to content

Commit eb54f4e

Browse files
committed
Updated behaviour of the Knob for better quantization support
1 parent 65d79bb commit eb54f4e

5 files changed

Lines changed: 42 additions & 30 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
=== 1.0.33 ===
66
* Fixed widget redrawing atrifacts for some use cases.
7+
* Updated behaviour of the Knob for better quantization support.
78

89
=== 1.0.32 ===
910
* Implemented tk::Image widget that can load and hold raster images.

include/lsp-plug.in/tk/prop/multi/RangeFloat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace lsp
6868
virtual void push() override;
6969
virtual void commit(atom_t property) override;
7070

71-
float climited(float v) const;
71+
float climited(float v, bool write) const;
7272
float change(float k, float step);
7373
float do_limit(float v, bool write) const;
7474
float transform(float v, bool write) const;
@@ -95,7 +95,7 @@ namespace lsp
9595
inline bool range_locked() const { return nFlags & F_RANGE_LOCK; }
9696
inline bool auto_limit() const { return nFlags & F_AUTO_LIMIT; }
9797

98-
float set(float v);
98+
float set(float v, bool cyclic = false);
9999
float set_all(float v, float min, float max);
100100
float set_min(float v);
101101
float set_max(float v);

include/lsp-plug.in/tk/widgets/simple/Knob.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ namespace lsp
103103

104104
protected:
105105
ssize_t nLastY;
106+
float fLastValue;
107+
float fDelta;
106108
size_t nState;
107109
size_t nButtons;
108110

@@ -132,7 +134,6 @@ namespace lsp
132134

133135
protected:
134136
size_t check_mouse_over(ssize_t x, ssize_t y);
135-
void update_value(float delta);
136137
void on_click(ssize_t x, ssize_t y);
137138
style::KnobColors *select_colors();
138139

src/main/prop/multi/RangeFloat.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ namespace lsp
177177
return old;
178178
}
179179

180-
float RangeFloat::set(float value)
180+
float RangeFloat::set(float value, bool cyclic)
181181
{
182182
float old = fValue;
183-
value = do_limit(value, true);
183+
value = (cyclic) ? climited(value, true) : do_limit(value, true);
184184
if (value == old)
185185
return old;
186186

@@ -218,7 +218,7 @@ namespace lsp
218218
value;
219219
}
220220

221-
float RangeFloat::climited(float value) const
221+
float RangeFloat::climited(float value, bool write) const
222222
{
223223
if (!(nFlags & F_AUTO_LIMIT))
224224
return value;
@@ -239,7 +239,7 @@ namespace lsp
239239
value += delta;
240240
}
241241

242-
return do_limit(value, false);
242+
return do_limit(value, write);
243243
}
244244

245245
float RangeFloat::change(float k, float step)
@@ -257,7 +257,7 @@ namespace lsp
257257
float RangeFloat::add(float v, bool cyclic)
258258
{
259259
float old = fValue;
260-
v = (cyclic) ? climited(old + v) : do_limit(old + v, true);
260+
v = (cyclic) ? climited(old + v, true) : do_limit(old + v, true);
261261
if (old == v)
262262
return old;
263263

@@ -269,7 +269,7 @@ namespace lsp
269269
float RangeFloat::sub(float v, bool cyclic)
270270
{
271271
float old = fValue;
272-
v = (cyclic) ? climited(old - v) : do_limit(old - v, true);
272+
v = (cyclic) ? climited(old - v, true) : do_limit(old - v, true);
273273
if (old == v)
274274
return old;
275275

src/main/widgets/simple/Knob.cpp

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ namespace lsp
157157
sInvertMouseVScroll(&sProperties)
158158
{
159159
nLastY = -1;
160+
fLastValue = 0.0f;
161+
fDelta = 0.0f;
160162
nState = 0;
161163
nButtons = 0;
162164

@@ -263,16 +265,6 @@ namespace lsp
263265
return (self != NULL) ? self->on_end_edit() : STATUS_BAD_ARGUMENTS;
264266
}
265267

266-
void Knob::update_value(float delta)
267-
{
268-
// lsp_trace("value=%f, delta=%f", sValue.get(), delta);
269-
270-
// Check that value is in range
271-
float old = sValue.add(delta, sCycling.get());
272-
if (old != sValue.get())
273-
sSlots.execute(SLOT_CHANGE, this);
274-
}
275-
276268
void Knob::on_click(ssize_t x, ssize_t y)
277269
{
278270
x -= sSize.nLeft;
@@ -397,10 +389,11 @@ namespace lsp
397389
if (nState != S_NONE)
398390
sSlots.execute(SLOT_BEGIN_EDIT, this);
399391
}
392+
nLastY = e->nTop;
393+
fLastValue = sValue.get();
400394
}
401395

402396
nButtons |= (size_t(1) << e->nCode);
403-
nLastY = e->nTop;
404397

405398
return STATUS_OK;
406399
}
@@ -417,6 +410,8 @@ namespace lsp
417410
if (nState != S_NONE)
418411
sSlots.execute(SLOT_END_EDIT, this);
419412
nState = S_NONE;
413+
414+
fDelta = 0.0f;
420415
}
421416

422417
return STATUS_OK;
@@ -431,13 +426,15 @@ namespace lsp
431426
return STATUS_OK;
432427

433428
// Update value
434-
float scaling = lsp_max(0.0f, sScaling.get());
435-
bool accel = (e->nState & ws::MCF_CONTROL);
436-
bool decel = bool(e->nState & ws::MCF_SHIFT) != bool(nButtons & ws::MCF_RIGHT);
429+
const float scaling = lsp_max(0.0f, sScaling.get());
430+
const bool accel = (e->nState & ws::MCF_CONTROL);
431+
const bool decel = bool(e->nState & ws::MCF_SHIFT) != bool(nButtons & ws::MCF_RIGHT);
432+
const float step = sStep.get(accel, decel);
437433

438-
float step = sStep.get(accel, decel);
439-
update_value(step * (nLastY - e->nTop) / scaling);
440-
nLastY = e->nTop;
434+
// Update value
435+
const float old = sValue.set(fLastValue + step * (nLastY - e->nTop) / scaling, sCycling.get());
436+
if (old != sValue.get())
437+
sSlots.execute(SLOT_CHANGE, this);
441438
}
442439
else if (nState == S_CLICK)
443440
{
@@ -455,13 +452,15 @@ namespace lsp
455452
// lsp_trace("x=%d, y=%d, state=%x, code=%x", int(e->nLeft), int(e->nTop), int(e->nState), int(e->nCode));
456453
if (!sEditable.get())
457454
return STATUS_OK;
455+
if (nButtons != 0)
456+
return STATUS_OK;
458457

459458
float step = sStep.get(e->nState & ws::MCF_CONTROL, e->nState & ws::MCF_SHIFT);
460459
if (sInvertMouseVScroll.get())
461460
step = -step;
462461

463462
// Compute delta
464-
float delta = 0.0;
463+
float delta = 0.0f;
465464
if (e->nCode == ws::MCD_UP)
466465
delta = step;
467466
else if (e->nCode == ws::MCD_DOWN)
@@ -470,9 +469,20 @@ namespace lsp
470469
return STATUS_OK;
471470

472471
// Update value
473-
sSlots.execute(SLOT_BEGIN_EDIT, this);
474-
update_value(delta);
475-
sSlots.execute(SLOT_END_EDIT, this);
472+
if (fDelta == 0.0f)
473+
fLastValue = sValue.get();
474+
fDelta += delta;
475+
476+
const float old = sValue.set(fLastValue + fDelta, sCycling.get());
477+
if (old != sValue.get())
478+
{
479+
fLastValue = sValue.get();
480+
fDelta = 0.0f;
481+
482+
sSlots.execute(SLOT_BEGIN_EDIT, this);
483+
sSlots.execute(SLOT_CHANGE, this);
484+
sSlots.execute(SLOT_END_EDIT, this);
485+
}
476486

477487
return STATUS_OK;
478488
}

0 commit comments

Comments
 (0)