From 12b76207512869cd33b3eb87c07b0b0df1568aee Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sat, 3 Nov 2018 09:24:24 +0100 Subject: [PATCH] sliders: add `steps` option (#412) --- CHANGELOG.md | 1 + src/client/app/widgets/sliders/fader.js | 11 +++++++++-- src/client/app/widgets/sliders/knob.js | 13 ++++++++++--- src/client/app/widgets/sliders/slider.js | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ec7cc76..17c2e0bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - widgets - multixy: added `.pad-N` class to inner xy pads to help with css customization + - sliders: new `steps` option ## 0.39.3 diff --git a/src/client/app/widgets/sliders/fader.js b/src/client/app/widgets/sliders/fader.js index 84365e3d..fd483122 100644 --- a/src/client/app/widgets/sliders/fader.js +++ b/src/client/app/widgets/sliders/fader.js @@ -34,7 +34,13 @@ module.exports = class Fader extends Slider { '- values can be `number` or `object` if a custom label is needed', 'Example: (`{min:{"-inf": 0}, "50%": 0.25, max: {"+inf": 1}}`)' ]}, - logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis'}, + steps: {type: 'boolean|number|array', value: '', help: [ + 'Restricts the widget\'s value:', + '- `true`: use values defined in `range`', + '- `number`: define a number of evenly spaced steps', + '- `array`: use arbitrary values', + ]}, + logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale'}, unit: {type: 'string', value: '', help: 'Unit will be appended to the displayed widget\'s value (it doesn\'t affect osc messages)'}, origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the fader\'s gauge'}, @@ -221,7 +227,8 @@ module.exports = class Fader extends Slider { var width = this.getProp('horizontal') ? this.height : this.width, height = !this.getProp('horizontal') ? this.height : this.width - var d = Math.round(this.percentToCoord(this.percent)), + var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent, + d = Math.round(this.percentToCoord(percent)), o = Math.round(this.percentToCoord(this.valueToPercent(this.originValue))), m = Math.round(this.getProp('horizontal') ? this.height / 2 : this.width / 2) diff --git a/src/client/app/widgets/sliders/knob.js b/src/client/app/widgets/sliders/knob.js index fb6010a3..ed446dbb 100644 --- a/src/client/app/widgets/sliders/knob.js +++ b/src/client/app/widgets/sliders/knob.js @@ -25,7 +25,13 @@ module.exports = class Knob extends Slider { '- values can be `number` or `object` if a custom label is needed', 'Example: (`{min:{"-inf": 0}, "50%": 0.25, max: {"+inf": 1}}`)' ]}, - logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale for the y axis'}, + steps: {type: 'boolean|array', value: false, help: [ + 'Restricts the widget\'s value:', + '- `true`: use values defined in `range`', + '- `number`: define a number of evenly spaced steps', + '- `array`: use arbitrary values', + ]}, + logScale: {type: 'boolean', value: false, help: 'Set to `true` to use logarithmic scale'}, unit: {type: 'string', value: '', help: 'Unit will be appended to the displayed widget\'s value (it doesn\'t affect osc messages)'}, origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the knob\'s gauge'}, @@ -135,8 +141,9 @@ module.exports = class Knob extends Slider { draw() { - var o = this.percentToAngle(this.valueToPercent(this.originValue)), - d = this.percentToAngle(this.percent), + var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent, + o = this.percentToAngle(this.valueToPercent(this.originValue)), + d = this.percentToAngle(percent), min = this.percentToAngle(0), max = this.percentToAngle(100), dashed = this.getProp('dashed'), diff --git a/src/client/app/widgets/sliders/slider.js b/src/client/app/widgets/sliders/slider.js index 62d496dc..dd6816a8 100644 --- a/src/client/app/widgets/sliders/slider.js +++ b/src/client/app/widgets/sliders/slider.js @@ -100,6 +100,15 @@ class Slider extends Canvas { } + if (this.getProp('steps')) { + var steps = this.getProp('steps') + this.steps = Array.isArray(steps) ? + steps : typeof steps === 'number' ? + Array(steps).fill(0).map((x, i) => i / (steps - 1) * this.rangeValsMax + this.rangeValsMin) + : this.rangeVals + } + + this.setValue(this.springValue) } @@ -210,6 +219,16 @@ class Slider extends Canvas { this.value = value + if (this.getProp('steps')) { + + var diff = this.steps.map(x => Math.abs(x - this.value)), + index = diff.indexOf(Math.min(...diff)), + val = this.steps[index] + + if (!isNaN(val)) this.value = clip(val, [this.rangeValsMin,this.rangeValsMax]) + + } + if (!options.dragged) this.percent = this.valueToPercent(this.value) if (!this.noDraw) this.batchDraw()