diff --git a/CHANGELOG.md b/CHANGELOG.md index d14b2909..16c4e800 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,9 @@ - widgets - expose widgets unique identifiers with property name "uuid" - - xy/multixy: add `ephemeral` property + - xy/multixy: add `ephemeral` property + - fader/knob/xy/range/multixy: remove `touchAddress` property + - fader/knob/xy/range/multixy: expose touch state in `script` property (`touchAddress` usages will be converted automatically) ## 1.3.0 diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index 63829cc6..34958b8a 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -16,6 +16,7 @@ Depending on what triggered the script's execution, a few variables are availabl - `id` (`string`): id of the widget that's reponsible for the value update - `value`: widget's value +- `touch`: see [Touch state](#touch-state) **Event: keyboard** (script widget only) @@ -27,10 +28,31 @@ Depending on what triggered the script's execution, a few variables are availabl - `alt` (`boolean`): `true` if alt key is pressed - `meta` (`boolean`): `true` if meta key is pressed +## Touch state + +When some widgets are touched or released, a special event can be catched to trigger custom actions. + +If the variable `touch` is not `undefined`, this means it holds the widget's cuurent touch state: + +- `0/1` if the widget is single touch (`fader`, `knob` and `xy`) +- `[i, 0/1]` if the widget is multi touch (`range` and `multixy`). In this case `i` is the touched handle's index, starting with `0`. + +Example script: + +```js +if (touch !== undefined) { + // send touch state + send('/touch_address', touch) +} else { + // do something with the value ? +} +``` + +To avoid unwanted script executions, touch state events will only be catched if the script contains the word `touch`. ## Available variables -Besides the variables available in the [javascript property syntax ](./advanced-syntaxes.md#available-variables), the following variables and functions are also accessible in this context. +Besides the variables available in the [javascript property syntax ](./advanced-syntaxes.md#available-variables) and the ones listed above, the following variables and functions are accessible in this context. ---- diff --git a/src/client/managers/session/session.js b/src/client/managers/session/session.js index 2f0b9404..aa91414d 100644 --- a/src/client/managers/session/session.js +++ b/src/client/managers/session/session.js @@ -211,6 +211,28 @@ var converters = [ } + } + }, + { + version: '1.3.1', + widget: (data)=>{ + + switch (data.type) { + + case 'fader': + case 'knob': + case 'xy': + case 'range': + case 'multixy': + if (data.touchAddress && String(data.touchAddress).length) { + let touchVal = data.type.match(/range|multixy/) ? 'touch[0], touch[1]' : 'touch', + originalScript = data.script ? ` else {\n // original script\n ${data.script}\n // -----------\n}` : '' + data.script = `\nif (touch !== undefined) {\n // generated automatically\n // from touchAddress\n send("${data.touchAddress}", ${touchVal})\n}${originalScript}` + } + break + + } + } } ] diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index 77c2aadf..006fd613 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -213,7 +213,11 @@ class Widget extends EventEmitter { this.on('change', (e)=>{ if (e.widget === this && this.mounted && !e.options.fromEdit) this.script.setValue(this.value, {...e.options, id: e.options.id || e.id}) }) - + if (String(this.getProp('script')).includes('touch')) { + this.on('touch', (e)=>{ + this.script.setValue(this.value, {id: this.getProp('id'), touch: e.touch, sync: false, send: true}) + }) + } } this.container = html` diff --git a/src/client/widgets/mixins/touch_state.js b/src/client/widgets/mixins/touch_state.js index ce3f857d..991810a1 100644 --- a/src/client/widgets/mixins/touch_state.js +++ b/src/client/widgets/mixins/touch_state.js @@ -1,18 +1,13 @@ module.exports = (self, options)=>{ - var sendTouchState = self.getProp('touchAddress') && self.getProp('touchAddress').length - self.touched = 0 self.setValueTouchedQueue = undefined self.on('draginit',(e)=>{ self.touched += 1 - if (self.touched == 1 && sendTouchState) - self.sendValue({ - address:self.getProp('touchAddress'), - v:1, - split:false - }) + if (self.touched == 1) { + self.trigger('touch', {stopPropagation :0, touch: 1}) + } }, options) self.on('dragend', (e)=>{ @@ -24,13 +19,7 @@ module.exports = (self, options)=>{ self.setValueTouchedQueue = undefined } - if (sendTouchState) { - self.sendValue({ - address:self.getProp('touchAddress'), - v:0, - split:false - }) - } + self.trigger('touch', {stopPropagation :true, touch: 0}) } else if (self.touched > 1){ self.touched -= 1 @@ -38,12 +27,8 @@ module.exports = (self, options)=>{ }, options) self.onRemove = ()=>{ - if (sendTouchState && self.touched > 0) { - self.sendValue({ - address:self.getProp('touchAddress'), - v:0, - split:false - }) + if (self.touched > 0) { + self.trigger('touch', {stopPropagation :true, touch: 0}) } self.__proto__.onRemove.call(self) } diff --git a/src/client/widgets/pads/multixy.js b/src/client/widgets/pads/multixy.js index 8136d7f6..cf75c5ba 100644 --- a/src/client/widgets/pads/multixy.js +++ b/src/client/widgets/pads/multixy.js @@ -43,11 +43,7 @@ module.exports = class MultiXy extends Pad { logScaleY: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the y axis. Set to `-1` for exponential scale.'}, sensitivity: {type: 'number', value: 1, help: 'Defines the pad\'s sensitivity when `snap` is `false` '}, - }, [], { - - touchAddress: {type: 'string', value:'', help: 'OSC address for touched state messages: `/touchAddress [preArgs] 0/1`'}, - - }) + }, [], {}) } @@ -120,12 +116,7 @@ module.exports = class MultiXy extends Pad { e.stopPropagation = true - if (this.getProp('touchAddress')) { - this.sendValue({ - address: this.getProp('touchAddress'), - v: [parseInt(id), 1], - }) - } + this.trigger('touch', {stopPropagation: true, touch: [parseInt(id), 1]}) this.pads[id].trigger('draginit', e) @@ -151,12 +142,7 @@ module.exports = class MultiXy extends Pad { this.pads[i].trigger('dragend', e) - if (this.getProp('touchAddress')) { - this.sendValue({ - address: this.getProp('touchAddress'), - v: [parseInt(this.touchMap[e.pointerId]), 0], - }) - } + this.trigger('touch', {stopPropagation: true, touch: [parseInt(this.touchMap[e.pointerId]), 0]}) delete this.touchMap[e.pointerId] diff --git a/src/client/widgets/pads/xy.js b/src/client/widgets/pads/xy.js index f2eb5a57..bb140449 100644 --- a/src/client/widgets/pads/xy.js +++ b/src/client/widgets/pads/xy.js @@ -37,11 +37,7 @@ module.exports = class Xy extends Pad { ]}, sensitivity: {type: 'number', value: 1, help: 'Defines the pad\'s sensitivity when `snap` is `false` '}, - }, [], { - - touchAddress: {type: 'string', value:'', help: 'OSC address for touched state messages: `/touchAddress [preArgs] 0/1`)'}, - - }) + }, [], {}) } diff --git a/src/client/widgets/scripts/script.js b/src/client/widgets/scripts/script.js index a7bc8d56..a3440959 100644 --- a/src/client/widgets/scripts/script.js +++ b/src/client/widgets/scripts/script.js @@ -56,7 +56,8 @@ class Script extends Widget { this.compile({ id: '', - value: 0 + value: 0, + touch: undefined }) } else if (this.getProp('event') === 'keyboard' && this.getProp('keyBinding')) { @@ -128,6 +129,7 @@ class Script extends Widget { this.run({ value: v, id: options.id, + touch: options.touch }) scriptVm.setValueOptions(options) diff --git a/src/client/widgets/sliders/fader.js b/src/client/widgets/sliders/fader.js index d3689971..08d1ad13 100644 --- a/src/client/widgets/sliders/fader.js +++ b/src/client/widgets/sliders/fader.js @@ -49,11 +49,7 @@ class Fader extends Slider { ]}, origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the fader\'s gauge'}, - }, [], { - - touchAddress: {type: 'string', value:'', help: 'OSC address for touched state messages: `/touchAddress [preArgs] 0/1`'}, - - }, Fader) + }, [], {}, Fader) } diff --git a/src/client/widgets/sliders/knob.js b/src/client/widgets/sliders/knob.js index b6ebb3c5..37c42d73 100644 --- a/src/client/widgets/sliders/knob.js +++ b/src/client/widgets/sliders/knob.js @@ -48,11 +48,7 @@ module.exports = class Knob extends Slider { ]}, origin: {type: 'number', value: 'auto', help: 'Defines the starting point\'s value of the knob\'s gauge'}, - }, [], { - - touchAddress: {type: 'string', value:'', help: 'OSC address for touched state messages: `/touchAddress [preArgs] 0/1`'}, - - }, Knob) + }, [], {}, Knob) } diff --git a/src/client/widgets/sliders/range.js b/src/client/widgets/sliders/range.js index d2b54dc6..b4051c24 100644 --- a/src/client/widgets/sliders/range.js +++ b/src/client/widgets/sliders/range.js @@ -141,12 +141,7 @@ class Range extends Fader { if (!i) return - if (this.getProp('touchAddress')) { - this.sendValue({ - address: this.getProp('touchAddress'), - v: [parseInt(i), 1], - }) - } + this.trigger('touch', {stopPropagation: true, touch: [parseInt(i), 1]}) if (e.shiftKey) { this.faders[0].trigger('drag', e) @@ -167,12 +162,7 @@ class Range extends Fader { this.faders[i].trigger('dragend', e) - if (this.getProp('touchAddress')) { - this.sendValue({ - address: this.getProp('touchAddress'), - v: [parseInt(i), 0], - }) - } + this.trigger('touch', {stopPropagation: true, touch: [parseInt(i), 0]}) delete this.touchMap[e.pointerId]