diff --git a/resources/docs/user-guide/widgets-specifics/maths.md b/resources/docs/user-guide/widgets-specifics/maths.md new file mode 100644 index 00000000..0b0f0fe2 --- /dev/null +++ b/resources/docs/user-guide/widgets-specifics/maths.md @@ -0,0 +1,24 @@ +# Maths + +Maths do the maths ! + +---- + +## Math + +Math evaluates mathematical expressions over other widgets' values each one of these widgets changes. + +```js +{ + type:'math', + // etc +} +``` + + +#### `formula` +- type: `string` +- default: `''` +- usage: a javascript expression in which + - `${id}` will be replaced the `id`'s value +- example: `${fader_1} * 2 + ${fader_2}` diff --git a/src/browser/js/app/widgets.js b/src/browser/js/app/widgets.js index e37f4293..a1fee09e 100644 --- a/src/browser/js/app/widgets.js +++ b/src/browser/js/app/widgets.js @@ -35,6 +35,9 @@ var widgets = { // state switchers switcher: require('./widgets/switchers/switcher'), crossfader: require('./widgets/switchers/crossfader'), + + // maths + math: require('./widgets/maths/math') } module.exports.categories = { @@ -44,7 +47,8 @@ module.exports.categories = { 'Matrices':['multifader','multitoggle','multipush', 'keyboard'], 'Plots':['plot','eq','visualizer','led','meter','text'], 'Containers':['panel','strip','modal'], - 'Switchers':['switcher','crossfader'] + 'Switchers':['switcher','crossfader'], + 'Maths':['math'] } module.exports.widgetOptions = function(){ diff --git a/src/browser/js/app/widgets/maths/math.js b/src/browser/js/app/widgets/maths/math.js new file mode 100644 index 00000000..ff332c02 --- /dev/null +++ b/src/browser/js/app/widgets/maths/math.js @@ -0,0 +1,97 @@ +var _widgets_base = require('../common/_widgets_base'), + {widgetManager} = require('../../managers') + + +module.exports = class Math extends _widgets_base { + + static options() { + + return { + type:'math', + id:'auto', + linkId:'', + + _math: 'math', + + formula: '', + + _style:'style', + + label:'auto', + left:'auto', + top:'auto', + width:'auto', + height:'auto', + color:'auto', + css:'', + + _osc:'osc', + + precision:2, + address:'auto', + preArgs:[], + target:[] + } + + } + + constructor(widgetData) { + + var widgetHtml = ` +
+
+
+ ` + + super(...arguments, widgetHtml) + + 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)}) + + $('body').on(`sync.${this.hash}`,this.syncHandle.bind(this)) + + } + + onRemove() { + + $('body').off(`sync.${this.hash}`) + + } + + syncHandle(e) { + + if (this.linkedWidgets.indexOf(e.id)==-1 || !widgetManager.getWidgetById(e.id).length) return + this.updateValue(e) + + } + + updateValue(e){ + + var formula = this.widgetData.formula, + id + + for (id of this.linkedWidgets) { + if (id !== undefined) { + formula = formula.replace('${' + id + '}', widgetManager.getWidgetById(id)[0].getValue()) + } + } + + try { + + this.value = eval(formula) + + this.input.val(typeof this.value != 'object' ? this.value.toFixed(this.widgetData.precision) : this.value.map((a)=>{return a.toFixed(this.widgetData.precision)})) + + 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(this.value) + + } catch(err) { + + throw 'Error parsing formula "' + formula + '"' + + } + + } + +} diff --git a/src/browser/scss/_widgets.scss b/src/browser/scss/_widgets.scss index 5121090f..f902e495 100644 --- a/src/browser/scss/_widgets.scss +++ b/src/browser/scss/_widgets.scss @@ -108,3 +108,5 @@ @import 'widgets/strip'; @import 'widgets/panel'; + +@import 'widgets/math'; diff --git a/src/browser/scss/widgets/_math.scss b/src/browser/scss/widgets/_math.scss new file mode 100644 index 00000000..b808da3a --- /dev/null +++ b/src/browser/scss/widgets/_math.scss @@ -0,0 +1,16 @@ +.math-container { + width: 100rem; + height:60rem; + .math { + display:flex; + height:100%; + background: var(--color-faded); + .input { + height:auto; + width: auto; + } + } + .input { + --color-text: var(--color-custom); + } +}