widgets: prop: add OSC{/path, default_value} syntax (#191)

This commit is contained in:
jean-emmanuel 2018-03-12 21:35:56 +01:00
parent 4e079a2048
commit 76e51df03d
4 changed files with 108 additions and 5 deletions

View file

@ -1,4 +1,6 @@
# Property inheritance
# Advanced property syntax
## Inheritance: `@{}`
Widgets can use each other's property values by using the following syntaxes:
@ -21,20 +23,61 @@ If the retreived property is an object (`[] / {}`), a subset can be defined by a
!!! note ""
The root panel's `id` is `root`.
## Using the value
### Using the value
The special property name `value`<i class="md-icon" title="dynamic">flash_on</i> refers to a widget's current value (which can be affected by its `value` *property*).
When omitted, the property name defaults to `value` : `@{widgetId}` => `@{widgetId.value}`
## Dynamic properties
### Dynamic properties
Some properties, when changed, trigger a complete widget recreation that ends any ongoing user interaction. Also, updating these properties continuously (e.g. when linked to a slider's dynamic value) can be very cpu expensive.
Some properties have much cheaper update routines and can be considered as `dynamic`, as in performance safe. These properties are marked in the documentation with a ` `<i class="md-icon" title="dynamic">flash_on</i>.
## Circular references cases
### Circular references cases
- container widgets can inherit their children's properties only to define `dynamic` properties
- widgets can inherit their own `value`<i class="md-icon" title="dynamic">flash_on</i> property only to define `dynamic` properties
----
## OSC listeners: `OSC{}`
The following syntax allows listening on an osc address to define a property, with an optionnal default value :
```
OSC{/address, 1}
```
Will return `1` at first and listen for osc messages on address `/address`. Each time a value is received, the property will be updated.
----
## Formulas: `#{}`
The following syntax allow writing mathematical formulas in widgets' properties:
```
#{FORMULA}
```
Where FORMULA is a valid [MathJS](http://mathjs.org/docs/expressions/syntax.html) expression:
- [syntax documentation](http://mathjs.org/docs/expressions/syntax.html)
- [available functions](http://mathjs.org/docs/reference/functions.html)
- [available constants](http://mathjs.org/docs/reference/constants.html)
- formulas can be [multiline](http://mathjs.org/docs/expressions/syntax.html#multiline-expressions)
- property inheritance calls (`@{...}`) are always resolved before formulas
- arrays / matrices indexes are **zero-based** (ie `["a","b"][0]` returns `"a"`)
- strings can be multiline when enclosed in backticks instead of double quotes (``` ` ` ```)
- inner closing brackets (`}`) must be escaped with a backslash (`\}`) except for `@{}` and `OSC{}` calls which are always resolved before formulas
Additionnal functions:
- `unpack(x)`: remove an array's brackets (`unpack([1,2])` returns `"1, 2"`)
- `pad(x, padding)`: add leading zeros if the length the integer part of `x` is smaller than `padding` (`pad(1,2)` returns `"01"`)
- `length(x)`: returns the length of an array or string
!!! tip ""
A single widget property can contain multiple formulas. Variables and functions declared in a formula are available to subsequent formulas in the same property definition.

View file

@ -1,10 +1,13 @@
var widgetManager = require('./managers/widgets'),
EventEmitter = require('./events/event-emitter'),
ipc = require('./ipc/')
var Osc = class Osc {
var Osc = class Osc extends EventEmitter {
constructor() {
super()
this.syncOnly = false
this.remoteControl = ()=>{console.error('remote-control module not loaded')}
@ -47,6 +50,8 @@ var Osc = class Osc {
}
}
this.trigger(new RegExp('^' + data.address + '\\..*'), [data.args])
}
}

View file

@ -0,0 +1,29 @@
var osc = require('../../osc')
module.exports = class OscReceiver {
constructor(address, value, parent, propName) {
this.value = value
this.parent = parent
osc.on(address + '.' + parent.hash, (args)=>{
if (typeof args !== 'object') args = [args]
var preArgs = parent.getProp('preArgs') || []
if (args.length >= preArgs.length) {
for (var i in preArgs) {
if (preArgs[i] !== args[i]) return
}
var val = args.slice(preArgs.length)
if (val.length < 2) val = val[0]
if (val !== this.value) {
this.value = val
parent.updateProps([propName], parent)
}
}
})
}
}

View file

@ -6,6 +6,7 @@ var EventEmitter = require('../../events/event-emitter'),
scopeCss = require('scope-css'),
{iconify} = require('../../ui/utils'),
resize = require('../../events/resize'),
OscReceiver = require('./osc-receiver'),
updateWidget = ()=>{}
@ -61,6 +62,9 @@ class Widget extends EventEmitter {
this.linkedProps = {}
this.linkedPropsValue = {}
// OSC{/path} receivers
this.oscReceivers = {}
// cache props (resolve @{props})
this.cachedProps = {}
@ -243,6 +247,27 @@ class Widget extends EventEmitter {
varnumber = 0
if (typeof propValue == 'string') {
propValue = propValue.replace(/OSC\{([^\}]+)\}/g, (m)=>{
let [address, value] = m.substr(4, m.length - 5).split(',')
address = address.trim()
value = value.trim()
if (!this.oscReceivers[address]) this.oscReceivers[address] = new OscReceiver(address, value, this, propName)
var r = this.oscReceivers[address].value
r = typeof r != 'string' ? JSON.stringify(r) : r
var varname = 'VAR_' + varnumber
varnumber++
variables[varname] = r
mathscope[varname] = r
return varname
})
propValue = propValue.replace(/@\{([^\}]+)\}/g, (m)=>{
let id = m.substr(2, m.length - 3).split('.'),
k, subk
@ -564,6 +589,7 @@ class Widget extends EventEmitter {
widgetManager.off(`widget-created.${this.hash}`)
widgetManager.off(`prop-changed.${this.hash}`)
widgetManager.off(`change.${this.hash}`)
osc.off(new RegExp('.*\\.' + this.hash))
}
}