- renamed script to onValue, draw to onDraw, touch to onTouch

- added `onCreate` script property to all widgets
This commit is contained in:
jean-emmanuel 2022-01-09 12:07:55 +01:00
parent e8ce280308
commit 467b2cd4c2
16 changed files with 194 additions and 133 deletions

View file

@ -2,7 +2,7 @@ var semver = require('semver'),
UiModal = require('../../ui/ui-modal'),
locales = require('../../locales')
module.exports = class Session {
class Session {
constructor(data, type) {
@ -18,7 +18,7 @@ module.exports = class Session {
var version = data.version || '0.0.0',
warning = false
for (var converter of converters) {
for (var converter of Session.converters) {
if (semver.lte(version, converter.version)) {
@ -102,7 +102,7 @@ module.exports = class Session {
}
var converters = [
Session.converters = [
{
version: '0.49.12',
warning: true,
@ -285,6 +285,31 @@ var converters = [
delete data.session
return data
}
},
{
version: '1.13.2',
widget: (data)=>{
if (data.script !== undefined) {
if (data.type === 'script') {
if (data.event === 'once') {
data.onCreate = data.script
data.event = 'value'
}
else data.onEvent = data.script
} else {
data.onValue = data.script
}
}
if (data.type === 'canvas') {
data.onDraw = data.draw
data.onTouch = data.touch
}
}
}
]
module.exports = Session

View file

@ -213,15 +213,17 @@ class UiInspectorField extends UiWidget {
})
if (this.default.editor === 'javascript') {
let globals = {}
if (this.name === 'script') {
if (this.widget.type !== 'script' || this.widget.getProp('event') === 'value') {
if (this.name === 'onEvent') {
if (this.widget.getProp('event') === 'value') {
globals = {id: true, value: true, touch: true}
} else if (this.widget.getProp('event') === 'keyboard') {
globals = {type: true, key: true, code: true, ctrl: true, shift: true, alt: true, meta: true}
}
} else if (this.name === 'draw') {
} else if (this.name === 'onValue') {
globals = {id: true, value: true, touch: true}
} else if (this.name === 'onDraw') {
globals = {value: true, height: true, width: true, ctx: true, cssVars: true}
} else if (this.name === 'touch') {
} else if (this.name === 'onTouch') {
globals = {value: true, height: true, width: true, event: true}
}
globals.locals = true

View file

@ -46,7 +46,7 @@ class Button extends Widget {
}
})
defaults.scripting.script.help.push(
defaults.scripting.onValue.help.push(
'Additionnal variables:',
'- `locals.touchCoords`: `[x, y]` array representing the touch coordinates, normalized between 0 and 1.'
)

View file

@ -14,7 +14,7 @@ class Canvas extends Widget {
'- `--widget-width`',
'- `--widget-height`',
)
defaults.scripting.script.help.push(
defaults.scripting.onValue.help.push(
'Canvas-based widget have their computed width and height available as local variables:',
'- `locals.width`',
'- `locals.height`',
@ -96,7 +96,7 @@ class Canvas extends Widget {
}
setVisibility(){
var visible = this.visible
super.setVisibility()
if (!visible && this.visible) this.batchDraw()

View file

@ -138,7 +138,8 @@ class Widget extends EventEmitter {
bypass: {type: 'boolean', value: false, help: 'Set to `true` to prevent the widget from sending any osc message'}
},
scripting: {
script: {type: 'script', value: '', editor: 'javascript', help: ['Script executed whenever the widget\'s value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
onCreate: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget is created. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
onValue: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget\'s value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
}
}
@ -216,25 +217,25 @@ class Widget extends EventEmitter {
this.decimals = Math.min(20, Math.max(this.getProp('decimals'), 0))
}
this.scripts = {}
Script = Script || require('../scripts/script')
if (this.getProp('script') && !(this instanceof Script)) {
if (typeof this.props.script == 'string' && this.props.script.match(/OSC\{|@\{|VAR\{/)) {
this.errorProp('script', 'syntax', 'using advanced syntaxes in the script property is strongly discouraged and may result in unexpected behaviors.')
if (!(this instanceof Script)) {
if (this.getProp('onCreate')) {
this.scripts.onCreate = new Script({props: {
id: this.getProp('id') + '.onCreate',
onEvent: this.getProp('onCreate')
}, builtIn: true, parent: this, context: {}})
}
this.script = new Script({props:{
id: this.getProp('id') + '/script',
script: this.getProp('script'),
event: 'value'
}, builtIn: true, parent: this})
this.script._not_editable = true
this.on('change', (e)=>{
if (e.widget === this && this.mounted && !e.options.fromEdit && e.options.script !== false) {
this.script.setValue(e.options.widget ? e.options.widget.value : 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: true, send: true})
if (this.getProp('onValue')) {
this.scripts.onValue = new Script({props:{
id: this.getProp('id') + '.onValue',
onEvent: this.getProp('onValue'),
event: 'value'
}, builtIn: true, parent: this})
this.on('change', (e)=>{
if (e.widget === this && this.mounted && !e.options.fromEdit && e.options.script !== false) {
this.scripts.onValue.setValue(e.options.widget ? e.options.widget.value : this.value, {...e.options, id: e.options.id || e.id})
}
})
}
}
@ -294,6 +295,8 @@ class Widget extends EventEmitter {
index: index
})
if (this.scripts.onCreate) this.scripts.onCreate.run({})
}
changed(options) {
@ -979,9 +982,9 @@ class Widget extends EventEmitter {
// ignore clone wrapper
if (widget.parent !== widgetManager && widget.parent.getProp('type') === 'clone') widget = widget.parent
console._error(`${id}.${name} ${type} error${line}: ${error}`)
console._error(`${id}${name ? '.' + name : ''} ${type} error${line}: ${error}`)
console.debug(error)
uiConsole.log('error', `<span class="edit-widget" data-widget="${widget.builtIn ? widget.parent.hash : widget.hash}">${id}</span>.${name} ${type} error${line}: ${error}`, true)
uiConsole.log('error', `<span class="edit-widget" data-widget="${widget.builtIn ? widget.parent.hash : widget.hash}">${id}</span>${name ? '.' + name : ''} ${type} error${line}: ${error}`, true)
}
setContainerStyles(styles = ['geometry', 'css', 'visibility']) {
@ -1177,8 +1180,8 @@ class Widget extends EventEmitter {
onRemove(){
if (this.getProp('script') && !(this instanceof Script)) {
this.script.onRemove()
for (var k in this.scripts) {
this.scripts[k].onRemove()
}
this.removed = true

View file

@ -4,7 +4,8 @@ var Container = require('../common/container'),
parser = require('../../parser'),
{deepCopy, deepEqual} = require('../../utils'),
{diff, diffToWidget} = require('../../editor/diff'),
html = require('nanohtml')
html = require('nanohtml'),
Session = require('../../managers/session/session')
var excludedCloneClasses = ['widget', 'absolute-position', 'not-editable', 'editing', 'flex-expand', 'no-interaction']
@ -155,6 +156,8 @@ class Clone extends Container() {
var data = {...deepCopy(this.cloneTarget.props), ...this.getProp('props')}
Session.converters['1.13.2'].widget(data)
parser.parse({
data: data,
parentNode: this.widget,
@ -219,6 +222,8 @@ class Clone extends Container() {
var data = {...deepCopy(this.cloneTarget.props), ...this.getProp('props')},
clone = this.children[0]
Session.converters['1.13.2'].widget(data)
var delta = diff.diff(clone.props, data) || {},
[widget, patch] = diffToWidget(clone, delta),
changedProps = Object.keys(patch)

View file

@ -5,7 +5,9 @@ var Container = require('../common/container'),
{deepCopy, deepEqual} = require('../../utils'),
{diff, diffToWidget} = require('../../editor/diff'),
html = require('nanohtml'),
sessionManager
sessionManager,
Session = require('../../managers/session/session')
var excludedfragmentClasses = ['widget', 'absolute-position', 'not-editable', 'editing', 'flex-expand', 'no-interaction']
@ -87,6 +89,8 @@ class Fragment extends Container() {
var data = {...deepCopy(fragment.getRoot()), ...this.getProp('props')}
Session.converters['1.13.2'].widget(data)
parser.parse({
data: data,
parentNode: this.widget,
@ -141,6 +145,8 @@ class Fragment extends Container() {
var data = {...deepCopy(fragment.getRoot()), ...this.getProp('props')},
fragmentWidget = this.children[0]
Session.converters['1.13.2'].widget(data)
var delta = diff.diff(fragmentWidget.props, data) || {},
[widget, patch] = diffToWidget(fragmentWidget, delta),
changedProps = Object.keys(patch)

View file

@ -1,7 +1,7 @@
var Panel = require('./panel'),
parser = require('../../parser'),
{deepCopy} = require('../../utils')
{deepCopy} = require('../../utils'),
Session = require('../../managers/session/session')
class Matrix extends Panel {
@ -73,6 +73,8 @@ class Matrix extends Panel {
data = JSON.parse(JSON.stringify(data).replace(/(JS|#|OSC|@|VAR)_\{/g, '$1{'))
Session.converters['1.13.2'].widget(data)
var widget = parser.parse({
data: data,
parentNode: this.widget,
@ -134,6 +136,8 @@ class Matrix extends Panel {
data = JSON.parse(JSON.stringify(data).replace(/(JS|#|OSC|@|VAR)_\{/g, '$1{'))
Session.converters['1.13.2'].widget(data)
if (typeof oldPropValue[i] === 'object' && oldPropValue[i] !== null) {
// if an overridden default props is removed, set it back to default
let overriddenDefaults,

View file

@ -13,7 +13,7 @@ class CanvasWidget extends Canvas {
static defaults() {
return super.defaults().extend({
var defaults = super.defaults().extend({
class_specific: {
valueLength: {type: 'number', value: 1, help:[
'Defines the number of values accepted by the widget (minimum 1). Incoming messages that don\'t comply will be ignored',
@ -26,14 +26,15 @@ class CanvasWidget extends Canvas {
'If set to `true`, `draw` will be called at each frame, otherwise it will be called only when the widget is touched and when it receives a value.',
'Can be a number between 1 and 60 to specify the framerate (default: 30 fps).'
]},
touch: {type: 'string', value: '', editor:'javascript', help: [
'Script executed when the widget is touched. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.',
]},
draw: {type: 'string', value: '', editor:'javascript', help: [
'Script executed when the widget is redrawn. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.'
]},
}
})
defaults.scripting = {
onCreate: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget is created. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
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>.',]},
onDraw: {type: 'script', value: '', editor:'javascript', help: ['Script executed when the widget is redrawn. See <a href="https://openstagecontrol.ammd.net/docs/widgets/canvas/">documentation</a>.']},
onValue: {type: 'script', value: '', editor: 'javascript', help: ['Script executed whenever the widget\'s value updates. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
}
return defaults
}
@ -48,28 +49,29 @@ 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.drawScript = new Script({props:{
id: this.getProp('id') + '/drawScript',
script: this.getProp('draw'),
event: {
value: 0,
width: 100,
height: 100,
ctx: {},
cssVars: {}
}
}, builtIn: true, parent: this})
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.touchScript = new Script({props:{
id: this.getProp('id') + '/touchScript',
script: this.getProp('touch'),
event: {
value: 0,
width: 100,
height: 100,
event: {}
}
}, builtIn: true, parent: this})
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)=>{
@ -82,7 +84,7 @@ class CanvasWidget extends Canvas {
else event.target = null
event.type = type
this.touchScript.run({
this.scripts.onTouch.run({
value: this.value,
width: this.width,
height: this.height,
@ -133,14 +135,14 @@ class CanvasWidget extends Canvas {
draw() {
if (!this.getProp('draw')) return
if (!this.getProp('onDraw')) return
if (this.getProp('autoClear')) {
this.clear()
this.ctx.beginPath()
}
this.drawScript.run({
this.scripts.onDraw.run({
value: this.value,
ctx: this.ctx,
width: this.width,

View file

@ -20,8 +20,9 @@ class Script extends Widget {
delete defaults.geometry
delete defaults.style
defaults.scripting = {
event: {type: 'string', value: 'value', choices: ['value', 'keyboard', 'once'], help: 'Define which events trigger the script\'s execution.'},
script: {type: 'script', value: '', editor:'javascript', help: 'Script executed whenever the widget receives the defined event. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.'},
event: {type: 'string', value: 'value', choices: ['value', 'keyboard'], help: 'Define which events trigger the script\'s execution.'},
onCreate: {type: 'script', value: '', editor: 'javascript', help: ['Script executed when the widget is created. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.']},
onEvent: {type: 'script', value: '', editor:'javascript', help: 'Script executed whenever the widget receives the defined event. See <a href="https://openstagecontrol.ammd.net/docs/widgets/scripting/">documentation</a>.'},
_separator: 'event: keyboard',
@ -44,13 +45,18 @@ class Script extends Widget {
this.noValueState = true
this.builtIn = options.builtIn
if (this.builtIn) this._not_editable = true
this.scriptLock = false
this.timeouts = {}
this.intervals = {}
if (this.getProp('event') === 'value') {
if (!this.getProp('onEvent')) {
this.script = ()=>{}
} else if (this.getProp('event') === 'value') {
this.compile({
id: '',
@ -78,16 +84,11 @@ class Script extends Widget {
meta: false,
})
} else if (this.getProp('event') === 'once') {
} else if (options.context) {
this.compile({})
setTimeout(()=>{
this.run({}, {send: true, sync: true})
})
// custom (internal/builtin) event
this.compile(options.context)
} else if (typeof this.getProp('event') === 'object'){
this.compile(this.getProp('event'))
}
}
@ -96,11 +97,11 @@ class Script extends Widget {
try {
this.script = scriptVm.compile(this.getProp('script'), context)
this.script = scriptVm.compile(this.getProp('onEvent'), context)
} catch(err) {
this.errorProp('script', 'javascript', err)
this.errorProp(this.builtIn ? null : 'onEvent', 'javascript', err)
this.script = ()=>{}
}
@ -119,7 +120,7 @@ class Script extends Widget {
try {
returnValue = this.script(context, this.builtIn ? this.parent.parsersLocalScope : this.parsersLocalScope)
} catch(err) {
this.errorProp('script', 'javascript', err)
this.errorProp(this.builtIn ? null : 'onEvent', 'javascript', err)
}
this.scriptLock = false

View file

@ -44,7 +44,7 @@ module.exports = class Encoder extends StaticProperties(Knob, {angle: 360, range
}
})
defaults.scripting.script.help.push(
defaults.scripting.onValue.help.push(
'Additionnal variables:',
'- `locals.speed`: encoder\'s speed (reduce `sensitivity` to increase averaging)',
'- `locals.angle`: encoder\'s angle in degrees'