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

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