widgets: add math widget, the ultimate helper (not safe yet)

This commit is contained in:
jean-emmanuel 2017-01-24 09:08:44 +01:00
parent 2403ad2336
commit 27a0f31ff4
5 changed files with 144 additions and 1 deletions

View file

@ -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}`

View file

@ -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(){

View file

@ -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 = `
<div class="math">
<div class="input"></div>
</div>
`
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 + '"'
}
}
}

View file

@ -108,3 +108,5 @@
@import 'widgets/strip';
@import 'widgets/panel';
@import 'widgets/math';

View file

@ -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);
}
}