From 4219694389059ec938d479a4618a0de9c8baa6d0 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Thu, 27 Apr 2017 10:04:31 +0200 Subject: [PATCH] knob/encoder: some swag fix --- .../user-guide/widgets-specifics/sliders.md | 47 ++++++++++++++++ src/browser/js/app/widgets/sliders/encoder.js | 55 +++++++++++++------ src/browser/js/app/widgets/sliders/knob.js | 18 +++--- 3 files changed, 91 insertions(+), 29 deletions(-) diff --git a/resources/docs/user-guide/widgets-specifics/sliders.md b/resources/docs/user-guide/widgets-specifics/sliders.md index 8c71fd91..38681a97 100644 --- a/resources/docs/user-guide/widgets-specifics/sliders.md +++ b/resources/docs/user-guide/widgets-specifics/sliders.md @@ -166,3 +166,50 @@ range: { --color-knob:color; --color-pips:color; ``` + +## Encoder + +An endless knob that only responds to rotative gestures. + +```js +{ + type:'encoder', + // etc +} +``` + +#### `ticks` +- type: `number` +- default: `360` +- usage: defines the granularity / verbosity of the encoder (number of step for a 360° arc) + +#### `back` +- type: `string|number|object` +- default: `-1` +- usage: defines which value is sent when rotating the encoder anticlockwise + - can be an `object` if the type needs to be specified (see [preArgs](../widgets-generics.md#preargs)) + +#### `forth` +- type: `string|number|object` +- default: `1` +- usage: defines which value is sent when rotating the encoder clockwise + - can be an `object` if the type needs to be specified (see [preArgs](../widgets-generics.md#preargs)) + +#### `release` +- type: `string|number|object` +- default: `empty` +- usage: defines which value is sent when releasing the encoder + - set to `null` to send send no argument in the osc message + - can be an `object` if the type needs to be specified (see [preArgs](../widgets-generics.md#preargs)) + + +#### `snap` +- type: `boolean` +- default: `false` +- usage: by default, dragging the widget will modify it's value starting from its last value. Setting this to `true` will make it snap directly to the mouse/touch position. + + +#### `css` +``` +--color-knob:color; +``` diff --git a/src/browser/js/app/widgets/sliders/encoder.js b/src/browser/js/app/widgets/sliders/encoder.js index 36693d74..5314ce2c 100644 --- a/src/browser/js/app/widgets/sliders/encoder.js +++ b/src/browser/js/app/widgets/sliders/encoder.js @@ -2,6 +2,41 @@ var {clip, mapToScale} = require('../utils'), Knob = require('./knob'), _widgets_base = require('../common/_widgets_base') + + +var EncoderKnob = class extends Knob { + draginitHandle(e, data, traversing) { + + this.percent = clip(this.percent,[0,100]) + + this.lastOffsetX = data.offsetX + this.lastOffsetY = data.offsetY + + if (!(traversing || this.widgetData.snap)) return + + this.percent = this.angleToPercent(this.coordsToAngle(data.offsetX, data.offsetY)) + + this.setValue(this.percentToValue(this.percent), {send:true,sync:true,dragged:true, draginit:true}) + + } + mousewheelHandle(){} +} +var DisplayKnob = class extends Knob { + draw(){ + super.draw() + + this.ctx.beginPath() + this.ctx.fillStyle = this.colors.raised + this.ctx.strokeStyle = this.colors.raised + this.ctx.arc(this.width / 2, this.height / 2, this.minDimension / 2 - this.gaugeWidth * 1.5 * PXSCALE - 2 * PXSCALE, 0, Math.PI * 2) + this.ctx.fill() + this.ctx.globalAlpha = 0.3 + this.ctx.lineWidth = 1.1 * PXSCALE + this.ctx.stroke() + this.ctx.globalAlpha = 1 + } +} + module.exports = class Encoder extends _widgets_base { static options() { @@ -52,7 +87,7 @@ module.exports = class Encoder extends _widgets_base { this.wrapper = this.widget.find('.wrapper') this.ticks = Math.abs(parseInt(widgetData.ticks)) - this.knob = new Knob({ + this.knob = new EncoderKnob({ label:false, angle:360, snap:true, @@ -62,7 +97,7 @@ module.exports = class Encoder extends _widgets_base { this.knob.noDraw = true - this.display = new Knob({ + this.display = new DisplayKnob({ label:false, angle:360, range:{min:0,max:this.ticks}, @@ -78,22 +113,6 @@ module.exports = class Encoder extends _widgets_base { this.previousValue = this.ticks/2 - this.knob.mousewheelHandle = ()=>{} - this.knob.draginitHandle = function(e, data, traversing) { - - this.percent = clip(this.percent,[0,100]) - - this.lastOffsetX = data.offsetX - this.lastOffsetY = data.offsetY - - if (!(traversing || this.widgetData.snap)) return - - this.percent = this.angleToPercent(this.coordsToAngle(data.offsetX, data.offsetY)) - - this.setValue(this.percentToValue(this.percent), {send:true,sync:true,dragged:true, draginit:true}) - - } - this.wrapper.on('sync',(e)=>{ e.stopPropagation() diff --git a/src/browser/js/app/widgets/sliders/knob.js b/src/browser/js/app/widgets/sliders/knob.js index 8a1129c2..66220d23 100644 --- a/src/browser/js/app/widgets/sliders/knob.js +++ b/src/browser/js/app/widgets/sliders/knob.js @@ -212,19 +212,15 @@ module.exports = class Knob extends _sliders_base { this.ctx.beginPath() this.ctx.fillStyle = this.colors.raised + this.ctx.strokeStyle = this.colors.raised this.ctx.arc(this.width / 2, this.height / 2, r, 0, Math.PI * 2) this.ctx.fill() - - if ( this.colors.raised){ - var grd=this.ctx.createRadialGradient(this.width / 2, this.height / 2, Math.max(r - 4 * PXSCALE, PXSCALE), this.width / 2, this.height / 2, r) - grd.addColorStop(0, 'transparent') - grd.addColorStop(1, this.colors.raised) - } - - this.ctx.beginPath() - this.ctx.fillStyle = grd - this.ctx.arc(this.width / 2, this.height / 2, r, 0, Math.PI * 2) - this.ctx.fill() + this.ctx.lineWidth = 1 * PXSCALE + this.ctx.stroke() + this.ctx.globalAlpha = 0.3 + this.ctx.lineWidth = 1.1 * PXSCALE + this.ctx.stroke() + this.ctx.globalAlpha = 1 this.ctx.lineWidth = 2 * PXSCALE