widgets: (wip) add switchers widgets

This commit is contained in:
jean-emmanuel 2017-01-02 19:02:49 +01:00
parent baf338d1ec
commit 177af78c4b
5 changed files with 328 additions and 1 deletions

View file

@ -31,6 +31,10 @@ var widgets = {
strip: require('./widgets/containers/strip'),
panel: require('./widgets/containers/panel'),
modal: require('./widgets/containers/modal'),
// state switchers
switcher: require('./widgets/switchers/switcher'),
crossfader: require('./widgets/switchers/crossfader'),
}
module.exports.categories = {
@ -39,7 +43,8 @@ module.exports.categories = {
'Pads':['xy','rgb','multixy'],
'Matrices':['multifader','multitoggle','multipush', 'keyboard'],
'Plots':['plot','eq','visualizer','led','meter','text'],
'Containers':['panel','strip','modal']
'Containers':['panel','strip','modal'],
'Switchers':['switcher','crossfader']
}
module.exports.widgetOptions = function(){

View file

@ -0,0 +1,101 @@
var _widgets_base = require('../common/_widgets_base'),
{widgetManager} = require('../../managers'),
{stateSet} = require('../../actions')
module.exports = class _switchers_base extends _widgets_base {
constructor(widgetData) {
super(...arguments)
this._isSwitcher = true
this.value = {_selected:false}
this.linkedWidgets = typeof widgetData.linkedWidgets == 'object' ? widgetData.linkedWidgets : [widgetData.linkedWidgets]
for (var i in widgetData.values) {
this.value[widgetData.values[i]] = {}
}
$('body').on('sync',(e)=>{
var {id, widget, linkId, fromExternal, options} = e
if (this.value._selected !== false && widget.abstract._isSwitcher !== true && this.isWatching(id, widget)) {
this.value[this.value._selected][id] = JSON.parse(JSON.stringify(widget.abstract.getValue()))
}
})
}
isWatching(id, widget) {
if (this.linkedWidgets.indexOf(id) != -1) return true
for (var k in this.linkedWidgets) {
let widgets = widgetManager.getWidgetById(this.linkedWidgets[k])
for (var i in widgets) {
if (widgets[i].widget[0].contains(widget[0])) return true
}
}
}
setValue(v, options={}) {
let apply = false
if (typeof v == 'object') {
for (var k in this.value) {
if (v.hasOwnProperty(k)) {
this.value[k] = v[k]
apply = true
}
}
} else if (this.value[v] && v != '_selected') {
this.value._selected = v
apply = true
}
if (apply) this.applyState(this.value[this.value._selected], options)
}
applyState(state, options) {
for (var id in state) {
var value = state[id],
widgets = widgetManager.getWidgetById(id)
if (widgets.length) {
for (var i=widgets.length-1;i>=0;i--) {
if (widgets[i].setValue) {
widgets[i].setValue(value,{sync:options.sync, send:options.send})
}
}
}
}
if (options.sync) this.widget.trigger({type:'sync',id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId, options:options})
}
}

View file

@ -0,0 +1,107 @@
var Switcher = require('./switcher'),
Fader = require('../sliders/fader'),
{mapToScale, clip} = require('../utils')
module.exports = class Crossfader extends Switcher {
static options(){
var o = super.options()
delete o.values
return o
}
constructor(widgetData) {
widgetData.values = widgetData.horizontal ? ['A', 'B'] : ['B', 'A']
super(...arguments)
var o = Fader.options()
o.horizontal = widgetData.horizontal
o.range = {min:{'A':0}, '50%':{" ":0.5},max:{'B':1}}
this.fader = new Fader(o, 0)
this.widget.append($('<div class="fader-container"></div>').append(this.fader.widget))
this.fader.widget.on('sync', (e)=>{
e.stopPropagation()
if (this.fader.value == 0) {
this.setValue('A', e.options)
} else if (this.fader.value == 1) {
this.setValue('B', e.options)
} else {
this.setValue(this.fader.getValue(), e.options)
}
})
}
setValue(v, options={}) {
super.setValue(...arguments)
if (typeof v == 'object') {
this.fader.setValue(this.value._fader)
}
if (v == 'A' || v <= 0) {
this.fader.setValue(0)
this.value._fader = 0
} else if (v == 'B' || v >= 1) {
this.fader.setValue(1)
this.value._fader = 1
} else if (!isNaN(v)) {
this.fader.setValue(v)
this.value._fader = this.fader.getValue()
this.value._selected = false
this.switch.setValue()
var s = this.getValue(),
a = {}
for (var i in s['A']) {
if (s['B'][i] === undefined) continue
if (typeof s['A'][i] == 'object') {
for (var k in s['A'][i]) {
if (!isNaN(s['A'][i][k])) {
a[i][k] = mapToScale(v, [0,1], [s['A'][i][k], s['B'][i][k]], false)
}
}
} else if (!isNaN(s['A'][i])) {
a[i] = mapToScale(v, [0,1], [s['A'][i], s['B'][i]], false)
}
}
this.applyState(a, options)
}
}
}

View file

@ -0,0 +1,82 @@
var _switchers_base = require('./_switchers_base'),
Switch = require('../buttons/switch')
module.exports = class Switcher extends _switchers_base {
static options() {
return {
type:'switcher',
id:'auto',
linkId:'',
_switcher:'Switcher',
linkedWidgets:'',
_style:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
horizontal:false,
color:'auto',
css:'',
_osc:'osc',
values:['A', 'B'],
value:'A',
address:'auto',
preArgs:[],
target:[]
}
}
constructor(widgetData) {
var widgetHtml = `
<div class="switcher switch-container ${widgetData.horizontal ? 'horizontal' : ''}"></div>
`
super(...arguments, widgetHtml)
this.switch = new Switch({
label:false,
values:widgetData.values,
value: widgetData.value,
horizontal:widgetData.horizontal
},0)
this.switch.sendValue = ()=>{}
this.widget.append(this.switch.widget)
this.switch.widget.on('sync', (e)=>{
e.stopPropagation()
var {widget, options} = e
options.fromSelf = true
this.setValue(this.switch.getValue(), options)
})
}
setValue(v, options={}) {
super.setValue(...arguments)
if (!options.fromSelf) this.switch.setValue(this.value._selected)
}
}

View file

@ -73,3 +73,35 @@
}
}
}
.switcher-container, .crossfader-container {
.switcher {
width: 100%;
flex:1;
}
}
.crossfader-container {
.switcher {
display:flex;
&.horizontal {
flex-direction: column;
.fader-container {
width: 100%;
height: auto
}
}
}
.switch {
height: auto;
flex:1;
}
.fader-container {
height: auto;
flex:1;
.input {
display: none;
}
}
}