From 24db0ed98421e922374cd1e22d4771bc625d1a08 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 12 Jan 2022 14:42:39 +0100 Subject: [PATCH] scrpting: restore legacy touch state feature, deprecate it and replace with a more generic onTouch handler --- CHANGELOG.md | 1 + .../docs/docs/widgets/properties-reference.md | 12 ++ resources/docs/docs/widgets/scripting.md | 30 +++-- src/client/ui/ui-inspector-field.js | 4 +- src/client/widgets/common/widget.js | 26 +++++ src/client/widgets/pads/canvas.js | 109 +++++++++--------- src/client/widgets/pads/multixy.js | 6 +- src/client/widgets/pads/rgb.js | 6 +- src/client/widgets/pads/xy.js | 6 +- src/client/widgets/sliders/fader.js | 6 +- src/client/widgets/sliders/knob.js | 6 +- 11 files changed, 147 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 544607fd..fa60d83f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - widgets - renamed `script` to `onValue`, `draw` to `onDraw`, `touch` to `onTouch` - added `onCreate` script property to all widgets + - added `onTouch` script to widgets that supported the touch state variable in scripts (now deprecated) - canvas: expose additional touch event informations (`radiusX`, `radiusY`, `rotationAngle` and iOS-only `altitudeAngle`, `azimuthAngle` and `touchType`) diff --git a/resources/docs/docs/widgets/properties-reference.md b/resources/docs/docs/widgets/properties-reference.md index fbc7cb7e..37c81763 100644 --- a/resources/docs/docs/widgets/properties-reference.md +++ b/resources/docs/docs/widgets/properties-reference.md @@ -696,6 +696,7 @@ | property | type |default | description | | --- | --- | --- | --- | |
onValue#
| `script` | "" | Script executed when the widget's value updates. See documentation.

Canvas-based widget have their computed width and height available as local variables:
- `locals.width`
- `locals.height` | + |
onTouch#
| `script` | "" | Script executed when the widget is touched. See documentation. | ??? api "
rgb#
" Color picker with optional alpha slider. @@ -710,6 +711,12 @@ |
alpha#
| `boolean` | false | Set to `true` to enable alpha channel | |
rangeAlpha#
| `object` | \{
 "min": 0,
 "max": 1
}
| Defines the widget's output scale for the alpha channel. | + === "scripting" + + | property | type |default | description | + | --- | --- | --- | --- | + |
onTouch#
| `script` | "" | Script executed when the widget is touched. See documentation. | + ??? api "
multixy#
" Multi-point XY pad. @@ -740,6 +747,7 @@ | property | type |default | description | | --- | --- | --- | --- | |
onValue#
| `script` | "" | Script executed when the widget's value updates. See documentation.

Canvas-based widget have their computed width and height available as local variables:
- `locals.width`
- `locals.height` | + |
onTouch#
| `script` | "" | Script executed when the widget is touched. See documentation. | ??? api "
canvas#
" Multitouch canvas widget with user-defined drawing functions and touch reactions. @@ -800,6 +808,7 @@ | property | type |default | description | | --- | --- | --- | --- | |
onValue#
| `script` | "" | Script executed when the widget's value updates. See documentation.

Canvas-based widget have their computed width and height available as local variables:
- `locals.width`
- `locals.height` | + |
onTouch#
| `script` | "" | Script executed when the widget is touched. See documentation. | ??? api "
knob#
" Rotative knob slider. @@ -832,6 +841,7 @@ | property | type |default | description | | --- | --- | --- | --- | |
onValue#
| `script` | "" | Script executed when the widget's value updates. See documentation.

Canvas-based widget have their computed width and height available as local variables:
- `locals.width`
- `locals.height` | + |
onTouch#
| `script` | "" | Script executed when the widget is touched. See documentation. | ??? api "
encoder#
" A knob that sends a relative direction infomation instead of an absolute value. @@ -861,6 +871,7 @@ | property | type |default | description | | --- | --- | --- | --- | |
onValue#
| `script` | "" | Script executed when the widget's value updates. See documentation.

Canvas-based widget have their computed width and height available as local variables:
- `locals.width`
- `locals.height`

Additionnal variables:
- `locals.speed`: encoder's speed (reduce `sensitivity` to increase averaging)
- `locals.angle`: encoder's angle in degrees | + |
onTouch#
| `script` | "" | Script executed when the widget is touched. See documentation. | ??? api "
range#
" A fader with two heads for setting a range. @@ -894,6 +905,7 @@ | property | type |default | description | | --- | --- | --- | --- | |
onValue#
| `script` | "" | Script executed when the widget's value updates. See documentation.

Canvas-based widget have their computed width and height available as local variables:
- `locals.width`
- `locals.height` | + |
onTouch#
| `script` | "" | Script executed when the widget is touched. See documentation. | ## Scripts ??? api "
script#
" diff --git a/resources/docs/docs/widgets/scripting.md b/resources/docs/docs/widgets/scripting.md index fcbeea20..3eaa8404 100644 --- a/resources/docs/docs/widgets/scripting.md +++ b/resources/docs/docs/widgets/scripting.md @@ -27,7 +27,9 @@ The following variables are available in this context: value[getIndex(id)] ``` -??? infos "Touch state" +??? infos "Touch state" (deprecated) + + *This feature is deprecated, use `onTouch` instead* When some widgets are touched or released, a special value event can be caught to trigger custom actions. @@ -47,11 +49,30 @@ The following variables are available in this context: } ``` - To avoid unwanted script executions, touch state events will only be caught if the script contains the word `touch`. + To avoid unwanted script executions, touch state events will only be caught if the script contains the word `touch` and if `onTouch` is empty. +### `onTouch` + +This script is executed when the widget is touched, and when it is released. + +This script has access to the same variables and functions as the `script` property (except the event-specific ones), plus the following: + +- `value`: widget value +- `event`: object containing the following: + - `type`: `"start"` or `"stop"` + - `handle`: `undefined` if the event concerns the widget, `integer` if it concerns one of it's handles (`multixy` and `range` widgets only) + +Note: the [canvas](../canvas/) widgets exposes more informations in its `onTouch` script. + +### `onDraw` + +See [canvas](../canvas/). + ### `onEvent` +*Script widget only* + This script is executed when the event specified in the `event` property occurs. === "Event: keyboard" @@ -75,11 +96,6 @@ This script is executed when the event specified in the `event` property occurs. See `onValue`. -### `onTouch` / `onDraw` - -See [canvas](../canvas/). - - ## Available variables The following variables and functions are accessible in this context. diff --git a/src/client/ui/ui-inspector-field.js b/src/client/ui/ui-inspector-field.js index b6c5aea9..351bfd5e 100644 --- a/src/client/ui/ui-inspector-field.js +++ b/src/client/ui/ui-inspector-field.js @@ -224,7 +224,9 @@ class UiInspectorField extends UiWidget { } else if (this.name === 'onDraw') { globals = {value: true, height: true, width: true, ctx: true, cssVars: true} } else if (this.name === 'onTouch') { - globals = {value: true, height: true, width: true, event: true} + globals = this.widget.getProp('type') === 'canvas' ? + {value: true, height: true, width: true, event: true} : + {value: true, event: true, handle: true} } globals.locals = true globals.console = true diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index f3745048..2acc711e 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -240,6 +240,32 @@ class Widget extends EventEmitter { } }) } + + if (this.getProp('onTouch')) { + this.scripts.onTouch = new Script({props:{ + id: this.getProp('id') + '.onTouch', + onEvent: this.getProp('onTouch'), + }, builtIn: true, parent: this, context: { + event: {}, + value: 0 + }}) + this.on('touch', (e)=>{ + var multi = Array.isArray(e.touch), + state = multi ? e.touch[1] : e.touch + this.scripts.onTouch.run({ + event: {type: state ? 'start' : 'stop', handle: multi ? e.touch[0] : undefined}, + value: this.value + }, {sync: true, send: true}) + }) + } + + // legacy touch state + if (String(this.getProp('onValue')).includes('touch') && !this.getProp('onTouch')) { + this.on('touch', (e)=>{ + this.scripts.onValue.setValue(this.value, {id: this.getProp('id'), touch: e.touch, sync: true, send: true}) + }) + } + } this.style = null diff --git a/src/client/widgets/pads/canvas.js b/src/client/widgets/pads/canvas.js index e7a9aae0..d7bb9606 100644 --- a/src/client/widgets/pads/canvas.js +++ b/src/client/widgets/pads/canvas.js @@ -49,62 +49,67 @@ class CanvasWidget extends Canvas { this.valueLength = Math.max(1, parseInt(this.getProp('valueLength')) || 1) this.value = this.valueLength > 1 ? Array(this.valueLength).fill(0) : 0 - this.scripts.onDraw = new Script({props:{ - id: this.getProp('id') + '.onDraw', - onEvent: this.getProp('onDraw'), - }, builtIn: true, parent: this, context: { - value: 0, - width: 100, - height: 100, - ctx: {}, - cssVars: {} - }}) - this.scripts.onDraw._not_editable = true - - - this.scripts.onTouch = new Script({props:{ - id: this.getProp('id') + '.onTouch', - onEvent: this.getProp('onTouch'), - }, builtIn: true, parent: this, context: { - value: 0, - width: 100, - height: 100, - event: {} - }}) - this.scripts.onTouch._not_editable = true - - var touchCb = (e, type)=>{ - - var event = {...e} - delete event.firstTarget - event.targetName = event.target.getAttribute('name') - event.targetTagName = event.target.tagName - var w = widgetManager.getWidgetByElement(event.target) - if (w) event.target = w === this ? 'this' : w.getProp('id') - else event.target = null - event.type = type - - this.scripts.onTouch.run({ - value: this.value, - width: this.width, - height: this.height, - event: event, - }, {sync: true, send: true}) - - this.batchDraw() + if (this.getProp('onDraw')) { + this.scripts.onDraw = new Script({props:{ + id: this.getProp('id') + '.onDraw', + onEvent: this.getProp('onDraw'), + }, builtIn: true, parent: this, context: { + value: 0, + width: 100, + height: 100, + ctx: {}, + cssVars: {} + }}) + this.scripts.onDraw._not_editable = true } - this.on('draginit',(e)=>{ - touchCb(e, 'start') - }, {element: this.container, multitouch: true}) + if (this.getProp('onTouch')) { - this.on('drag',(e)=>{ - touchCb(e, 'move') - }, {element: this.container, multitouch: true}) + this.scripts.onTouch = new Script({props:{ + id: this.getProp('id') + '.onTouch', + onEvent: this.getProp('onTouch'), + }, builtIn: true, parent: this, context: { + value: 0, + width: 100, + height: 100, + event: {} + }}) + this.scripts.onTouch._not_editable = true - this.on('dragend',(e)=>{ - touchCb(e, 'stop') - }, {element: this.container, multitouch: true}) + var touchCb = (e, type)=>{ + + var event = {...e} + delete event.firstTarget + event.targetName = event.target.getAttribute('name') + event.targetTagName = event.target.tagName + var w = widgetManager.getWidgetByElement(event.target) + if (w) event.target = w === this ? 'this' : w.getProp('id') + else event.target = null + event.type = type + + this.scripts.onTouch.run({ + value: this.value, + width: this.width, + height: this.height, + event: event, + }, {sync: true, send: true}) + + this.batchDraw() + } + + this.on('draginit',(e)=>{ + touchCb(e, 'start') + }, {element: this.container, multitouch: true}) + + this.on('drag',(e)=>{ + touchCb(e, 'move') + }, {element: this.container, multitouch: true}) + + this.on('dragend',(e)=>{ + touchCb(e, 'stop') + }, {element: this.container, multitouch: true}) + + } if (this.getProp('continuous')) { var freq = parseInt(this.getProp('continuous')) || 30 diff --git a/src/client/widgets/pads/multixy.js b/src/client/widgets/pads/multixy.js index c5752aec..5184b99e 100644 --- a/src/client/widgets/pads/multixy.js +++ b/src/client/widgets/pads/multixy.js @@ -18,7 +18,7 @@ module.exports = class MultiXy extends Pad { static defaults() { - return super.defaults().extend({ + var defaults = super.defaults().extend({ style: { _separator_xy_style: 'Xy style', pointSize: {type: 'integer', value: 20, help: 'Defines the points\' size'}, @@ -43,6 +43,10 @@ module.exports = class MultiXy extends Pad { } }) + defaults.scripting.onTouch = {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is touched. See documentation.',]} + + return defaults + } constructor(options) { diff --git a/src/client/widgets/pads/rgb.js b/src/client/widgets/pads/rgb.js index b53368f2..0d80f68b 100644 --- a/src/client/widgets/pads/rgb.js +++ b/src/client/widgets/pads/rgb.js @@ -19,7 +19,7 @@ module.exports = class Rgb extends Widget { static defaults() { - return super.defaults().extend({ + var defaults = super.defaults().extend({ class_specific: { snap: {type: 'boolean', value: false, help: [ 'By default, the points are dragged from their initial position.', @@ -32,6 +32,10 @@ module.exports = class Rgb extends Widget { } }) + defaults.scripting.onTouch = {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is touched. See documentation.',]} + + return defaults + } constructor(options) { diff --git a/src/client/widgets/pads/xy.js b/src/client/widgets/pads/xy.js index e55beac1..f00a051e 100644 --- a/src/client/widgets/pads/xy.js +++ b/src/client/widgets/pads/xy.js @@ -15,7 +15,7 @@ module.exports = class Xy extends Pad { static defaults() { - return super.defaults().extend({ + var defaults = super.defaults().extend({ style: { _separator_xy_style: 'Xy style', pointSize: {type: 'integer', value: 20, help: 'Defines the points\' size'}, @@ -40,6 +40,10 @@ module.exports = class Xy extends Pad { } }) + defaults.scripting.onTouch = {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is touched. See documentation.',]} + + return defaults + } constructor(options) { diff --git a/src/client/widgets/sliders/fader.js b/src/client/widgets/sliders/fader.js index fa741cc6..66fb7b40 100644 --- a/src/client/widgets/sliders/fader.js +++ b/src/client/widgets/sliders/fader.js @@ -11,7 +11,7 @@ class Fader extends Slider { static defaults() { - return super.defaults(Fader).extend({ + var defaults = super.defaults(Fader).extend({ style: { _separator_fader_style: 'Fader style', design: {type: 'string', value: 'default', choices: ['default', 'round', 'compact'], help: 'Design style'}, @@ -52,6 +52,10 @@ class Fader extends Slider { } }) + defaults.scripting.onTouch = {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is touched. See documentation.',]} + + return defaults + } constructor(options) { diff --git a/src/client/widgets/sliders/knob.js b/src/client/widgets/sliders/knob.js index 060cc2a4..53b22e20 100644 --- a/src/client/widgets/sliders/knob.js +++ b/src/client/widgets/sliders/knob.js @@ -11,7 +11,7 @@ module.exports = class Knob extends Slider { static defaults() { - return super.defaults(Knob).extend({ + var defaults = super.defaults(Knob).extend({ style: { _separator_knob_style: 'Knob style', design: {type: 'string', value: 'default', choices: ['default', 'solid', 'line'], help: [ @@ -51,6 +51,10 @@ module.exports = class Knob extends Slider { } }) + defaults.scripting.onTouch = {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is touched. See documentation.',]} + + return defaults + } constructor(options) {