scrpting: restore legacy touch state feature, deprecate it and replace with a more generic onTouch handler

This commit is contained in:
jean-emmanuel 2022-01-12 14:42:39 +01:00
parent d478de49f6
commit 24db0ed984
11 changed files with 147 additions and 65 deletions

View file

@ -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`)

View file

@ -696,6 +696,7 @@
| property | type |default | description |
| --- | --- | --- | --- |
| <h6 id="xy_onValue">onValue<a class="headerlink" href="#xy_onValue" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget's value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.<br/><br/>Canvas-based widget have their computed width and height available as local variables:<br/>- `locals.width`<br/>- `locals.height` |
| <h6 id="xy_onTouch">onTouch<a class="headerlink" href="#xy_onTouch" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>. |
??? api "<div id="rgb">rgb<a class="headerlink" href="#rgb" title="Permanent link">#</a></div>"
Color picker with optional alpha slider.
@ -710,6 +711,12 @@
| <h6 id="rgb_alpha">alpha<a class="headerlink" href="#rgb_alpha" title="Permanent link">#</a></h6> | `boolean` | <code>false</code> | Set to `true` to enable alpha channel |
| <h6 id="rgb_rangeAlpha">rangeAlpha<a class="headerlink" href="#rgb_rangeAlpha" title="Permanent link">#</a></h6> | `object` | <code>\{<br/>&nbsp;"min": 0,<br/>&nbsp;"max": 1<br/>}</code> | Defines the widget's output scale for the alpha channel. |
=== "scripting"
| property | type |default | description |
| --- | --- | --- | --- |
| <h6 id="rgb_onTouch">onTouch<a class="headerlink" href="#rgb_onTouch" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>. |
??? api "<div id="multixy">multixy<a class="headerlink" href="#multixy" title="Permanent link">#</a></div>"
Multi-point XY pad.
@ -740,6 +747,7 @@
| property | type |default | description |
| --- | --- | --- | --- |
| <h6 id="multixy_onValue">onValue<a class="headerlink" href="#multixy_onValue" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget's value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.<br/><br/>Canvas-based widget have their computed width and height available as local variables:<br/>- `locals.width`<br/>- `locals.height` |
| <h6 id="multixy_onTouch">onTouch<a class="headerlink" href="#multixy_onTouch" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>. |
??? api "<div id="canvas">canvas<a class="headerlink" href="#canvas" title="Permanent link">#</a></div>"
Multitouch canvas widget with user-defined drawing functions and touch reactions.
@ -800,6 +808,7 @@
| property | type |default | description |
| --- | --- | --- | --- |
| <h6 id="fader_onValue">onValue<a class="headerlink" href="#fader_onValue" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget's value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.<br/><br/>Canvas-based widget have their computed width and height available as local variables:<br/>- `locals.width`<br/>- `locals.height` |
| <h6 id="fader_onTouch">onTouch<a class="headerlink" href="#fader_onTouch" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>. |
??? api "<div id="knob">knob<a class="headerlink" href="#knob" title="Permanent link">#</a></div>"
Rotative knob slider.
@ -832,6 +841,7 @@
| property | type |default | description |
| --- | --- | --- | --- |
| <h6 id="knob_onValue">onValue<a class="headerlink" href="#knob_onValue" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget's value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.<br/><br/>Canvas-based widget have their computed width and height available as local variables:<br/>- `locals.width`<br/>- `locals.height` |
| <h6 id="knob_onTouch">onTouch<a class="headerlink" href="#knob_onTouch" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>. |
??? api "<div id="encoder">encoder<a class="headerlink" href="#encoder" title="Permanent link">#</a></div>"
A knob that sends a relative direction infomation instead of an absolute value.
@ -861,6 +871,7 @@
| property | type |default | description |
| --- | --- | --- | --- |
| <h6 id="encoder_onValue">onValue<a class="headerlink" href="#encoder_onValue" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget's value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.<br/><br/>Canvas-based widget have their computed width and height available as local variables:<br/>- `locals.width`<br/>- `locals.height`<br/><br/>Additionnal variables:<br/>- `locals.speed`: encoder's speed (reduce `sensitivity` to increase averaging)<br/>- `locals.angle`: encoder's angle in degrees |
| <h6 id="encoder_onTouch">onTouch<a class="headerlink" href="#encoder_onTouch" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>. |
??? api "<div id="range">range<a class="headerlink" href="#range" title="Permanent link">#</a></div>"
A fader with two heads for setting a range.
@ -894,6 +905,7 @@
| property | type |default | description |
| --- | --- | --- | --- |
| <h6 id="range_onValue">onValue<a class="headerlink" href="#range_onValue" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget's value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.<br/><br/>Canvas-based widget have their computed width and height available as local variables:<br/>- `locals.width`<br/>- `locals.height` |
| <h6 id="range_onTouch">onTouch<a class="headerlink" href="#range_onTouch" title="Permanent link">#</a></h6> | `script` | <code>""</code> | Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>. |
## Scripts
??? api "<div id="script">script<a class="headerlink" href="#script" title="Permanent link">#</a></div>"

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.',]}
return defaults
}
constructor(options) {

View file

@ -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 <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.',]}
return defaults
}
constructor(options) {

View file

@ -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 <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.',]}
return defaults
}
constructor(options) {

View file

@ -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 <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.',]}
return defaults
}
constructor(options) {

View file

@ -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 <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.',]}
return defaults
}
constructor(options) {