diff --git a/CHANGELOG.md b/CHANGELOG.md index 293d9ead..51dd9152 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - bug fixes - regression breaking widgets' `split` option (0.17.6) - regression breaking the state export button (0.17.7) +- widgets + - maths: add `condition` option to [`formula`]http://osc.ammd.net/user-guide/widgets-specifics/maths/#formula) ## 0.17.7 diff --git a/resources/docs/user-guide/widgets-specifics/maths.md b/resources/docs/user-guide/widgets-specifics/maths.md index 999552a5..909f8b46 100644 --- a/resources/docs/user-guide/widgets-specifics/maths.md +++ b/resources/docs/user-guide/widgets-specifics/maths.md @@ -24,6 +24,13 @@ Formula evaluates mathematical expressions over other widgets' values each one o - example: `${fader_1} * 2 + ${fader_2}` - **important note**: contrary to what is stated in *mathjs*' documentation, **matrix indexes are zero-based** here +#### `condition` +- type: `string` +- default: `''` +- usage: + - a [mathjs](http://mathjs.org/docs/expressions/syntax.html) comparison expression that bypasses widget's osc sending when returning`false` + - example: `${fader_1} > 0.5` + #### `split` - type: `object` - default: `[]` diff --git a/src/browser/js/app/widgets/maths/formula.js b/src/browser/js/app/widgets/maths/formula.js index 17cffe54..b1292920 100644 --- a/src/browser/js/app/widgets/maths/formula.js +++ b/src/browser/js/app/widgets/maths/formula.js @@ -15,6 +15,7 @@ module.exports = class Formula extends _widgets_base { _math: 'math', formula: '', + condition: '', _style:'style', @@ -47,14 +48,35 @@ module.exports = class Formula extends _widgets_base { super(...arguments, widgetHtml) + widgetData.formula = String(widgetData.formula) + widgetData.condition = String(widgetData.condition) + this.split = typeof widgetData.split == 'object' && widgetData.split.length ? widgetData.split : false this.input = this.widget.find('.input').fakeInput({align:'center', disabled:true}) - this.linkedWidgets = widgetData.formula.match(/\$\{([^\}]*)\}/g) == null ? [] : widgetData.formula.match(/\$\{([^\}]*)\}/g).map((a)=>{return a.substr(2, a.length - 3)}) + this.linkedWidgets = [] + + if (widgetData.formula.length) { + + if (widgetData.formula.match(/\$\{([^\}]*)\}/g) != null) this.linkedWidgets = this.linkedWidgets.concat(widgetData.formula.match(/\$\{([^\}]*)\}/g).map((a)=>{return a.substr(2, a.length - 3)})) + + } + + if (widgetData.condition.length) { + + if (widgetData.condition.match(/\$\{([^\}]*)\}/g) != null) this.linkedWidgets = this.linkedWidgets.concat(widgetData.condition.match(/\$\{([^\}]*)\}/g).map((a)=>{return a.substr(2, a.length - 3)})) + + } + this.formula = math.compile(widgetData.formula.replace(/\$\{([^\}]*)\}/g, '_$1')) + this.condition = math.compile(widgetData.condition.replace(/\$\{([^\}]*)\}/g, '_$1')) + + this.conditionState = true + + $('body').on(`sync.${this.hash}`,this.syncHandle.bind(this)) this.input.val('...') @@ -85,6 +107,22 @@ module.exports = class Formula extends _widgets_base { } } + if (this.widgetData.condition.length) { + + try { + + var s = this.condition.eval(variables).valueOf() + + this.conditionState = s.data !== undefined ? s.data : s + + } catch(err) { + + throw 'Error parsing formula "' + this.condition + '" (' + err + ')' + + } + + } + try { var v = this.formula.eval(variables).valueOf() @@ -93,12 +131,12 @@ module.exports = class Formula extends _widgets_base { this.showValue() - if (e.options.sync) this.widget.trigger({type: 'sync',id: this.widgetData.id,widget: this.widget, linkId: this.widgetData.linkId, options: e.options}) - if (e.options.send) this.sendValue() + if (e.options.sync && this.conditionState) this.widget.trigger({type: 'sync',id: this.widgetData.id,widget: this.widget, linkId: this.widgetData.linkId, options: e.options}) + if (e.options.send && this.conditionState) this.sendValue() } catch(err) { - throw 'Error parsing formula "' + formula + '" (' + err + ')' + throw 'Error parsing formula "' + this.formula + '" (' + err + ')' } @@ -124,7 +162,10 @@ module.exports = class Formula extends _widgets_base { showValue() { - this.input.val(JSON.stringify(Formula.deepCopyWithPrecision(this.value, this.widgetData.precision))) + this.input.val( + (this.widgetData.condition.length && !this.conditionState ? '* ' : '') + + JSON.stringify(Formula.deepCopyWithPrecision(this.value, this.widgetData.precision)) + ) }