diff --git a/CHANGELOG.md b/CHANGELOG.md index d98629ef..bcfcdd5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - regression causing osc receivers (`OSC{}`) to conflict with property listeners (`@{}`) - widgets - push: add `doubleTap` option + - osc listeners (`OSC{}`) now accept relative addresses (without leading '/'), automatically prefixed with the widget's address ## 0.40.3 diff --git a/resources/docs/extras/advanced-property-syntax.md b/resources/docs/extras/advanced-property-syntax.md index bd714e8f..eb82b85e 100644 --- a/resources/docs/extras/advanced-property-syntax.md +++ b/resources/docs/extras/advanced-property-syntax.md @@ -56,8 +56,17 @@ 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. + +If the leading slash (`/`) is omitted, the address will be prefixed with the widget's `address` property: + +``` +OSC{color, "auto"} +``` +Will listen for osc messages on address `/widget_address/color`. + ---- + ## Formulas: `#{}` The following syntax allow writing mathematical formulas in widgets' properties: diff --git a/src/client/app/widgets/common/osc-receiver.js b/src/client/app/widgets/common/osc-receiver.js index 1ed396a8..9a2a2516 100644 --- a/src/client/app/widgets/common/osc-receiver.js +++ b/src/client/app/widgets/common/osc-receiver.js @@ -2,7 +2,9 @@ var osc = require('../../osc') module.exports = class OscReceiver { - constructor(address, value, parent, propName) { + constructor(options) { + + var {prefix, address, value, parent, propName} = options try { this.value = JSON.parse(value) @@ -13,6 +15,7 @@ module.exports = class OscReceiver { this.parent = parent this.propNames = [propName] this.bindedCallback = this.callback.bind(this) + this.prefix = '' this.setAddress(address) } @@ -21,10 +24,16 @@ module.exports = class OscReceiver { if (this.address !== address) { - if (this.address) osc.off(this.address, this.bindedCallback, this.parent) + if (this.address) osc.off(this.prefix + this.address, this.bindedCallback, this.parent) - this.address = address - osc.on(this.address, this.bindedCallback, {context: this.parent}) + if (address) this.address = address + + if (this.address[0] !== '/') { + this.prefix = this.parent.getProp('address') || this.parent.resolveProp('address', undefined, false, this) + if (this.prefix[this.prefix.length - 1] !== '/') this.prefix += '/' + } + + osc.on(this.prefix + this.address, this.bindedCallback, {context: this.parent}) } diff --git a/src/client/app/widgets/common/widget.js b/src/client/app/widgets/common/widget.js index fb0e8e16..37f71961 100644 --- a/src/client/app/widgets/common/widget.js +++ b/src/client/app/widgets/common/widget.js @@ -526,7 +526,12 @@ class Widget extends EventEmitter { }) if (!this.oscReceivers[address]) { - this.oscReceivers[address] = new OscReceiver(resolvedAddress, value, this, propName) + this.oscReceivers[address] = new OscReceiver({ + address: resolvedAddress, + value: value, + parent: this, + propName: propName + }) if (oscReceiverState[address]) { this.oscReceivers[address].value = oscReceiverState[address] delete oscReceiverState[address] @@ -711,7 +716,15 @@ class Widget extends EventEmitter { case 'preArgs': case 'target': case 'noSync': - if (propName == 'precision') this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0)) + if (propName === 'address') { + for (var k in this.oscReceivers) { + var receiver = this.oscReceivers[k] + if (receiver.prefix !== '') { + receiver.setAddress() + } + } + } + if (propName === 'precision') this.precision = Math.min(20,Math.max(this.getProp('precision', undefined, false),0)) var data = {}, oldData = { preArgs: propName == 'preArgs' ? oldPropValue : this.getProp('preArgs'),