widgets: script proto (#340)

This commit is contained in:
jean-emmanuel 2018-08-04 12:43:03 +02:00
parent 369f8d4cec
commit aadfee0a5d
4 changed files with 136 additions and 1 deletions

View file

@ -71,3 +71,31 @@
- type: `boolean`
- default: `true`
- usage: set to `false` to prevent `keydown` repeats when holding the key combo pressed.
## Script
```js
{
type:'script',
// etc
}
```
### `script`
- type: `string`
- default: `''`
- usage: this property is evaluated each time the widgets receives a non-falsy value. [Formulas](../extras/advanced-property-syntax/#formulas) are given extras variables in this context:
- `value`: the value received by the widget
- `send`: `function` for sending osc messages
- `set`: `function` for setting a widget's value (triggers osc sendings)
#### `send(target, address, arg1, arg2, ...)`
- `target` (`string` or `array` or `false`): one or several osc targets. Default targets (`--send` and the script's `target` property) are ignored unless `target` is `false`
- `address` (`string`): a valid osc address
- `arg`: one or several osc arguments to be sent. The script's `preArgs` property is ignored
#### `set(id, value)`
- `id` (`string`): a widget's id
- `value`: the widget's new value

View file

@ -51,6 +51,7 @@ module.exports.widgets = {
// inputs
input: require('./inputs/input'),
keys: require('./inputs/keys'),
script: require('./inputs/script'),
// maths
// deprecated / hidden
@ -66,5 +67,5 @@ module.exports.categories = {
'Plots':['plot','eq','visualizer','led', 'rgbled','meter','text', 'image', 'svg'],
'Containers':['panel','strip','modal', 'clone'],
'Switchers':['switcher','crossfader'],
'Inputs':['input', 'keys']
'Inputs':['input', 'keys', 'script']
}

View file

@ -0,0 +1,99 @@
var Widget = require('../common/widget'),
widgetManager = require('../../managers/widgets')
class Script extends Widget {
static defaults() {
return {
type:'script',
id:'auto',
linkId:'',
_geometry:'geometry',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
_style:'style',
label:'auto',
color:'auto',
css:'',
_script:'script',
script:'',
_osc:'osc',
address:'auto',
preArgs:[],
target: []
}
}
constructor(options) {
var html = `
<div class="script">
</div>
`
super({...options, html: html})
}
static scriptSet(id, args) {
var widgets = widgetManager.getWidgetById(id)
for (var i = widgets.length - 1; i >= 0; i--) {
return widgets[i].setValue(value, {
sync: true,
send: true
})
}
}
scriptSend(target, address, ...args) {
var overrides = {
address,
v: args,
preArgs: []
}
if (target) overrides.target = Array.isArray(target) ? target : [target]
this.sendValue(overrides)
}
setValue(v, options={} ) {
if (v) this.resolveProp('script', undefined, false, false, false, {value: v, send: this.scriptSend.bind(this), set: Script.scriptSet, log: console.log})
// if (options.send) this.sendValue()
if (options.sync) this.changed(options)
}
}
Script.dynamicProps = Script.prototype.constructor.dynamicProps.concat(
'script'
)
module.exports = Script

View file

@ -121,3 +121,10 @@
border: 1rem solid var(--color-bg);
}
}
.script-container {
width: 100rem;
height:60rem;
.script {
}
}