Add linkId for widget linking (=state sync + osc send) (#5)

(also fixed some quirks with toggle/push)
This commit is contained in:
jean-emmanuel 2016-02-23 12:52:01 +01:00
parent 336daa2232
commit a8ab78e071
3 changed files with 47 additions and 17 deletions

View file

@ -92,12 +92,12 @@ init = function(session,callback) {
}
sidepanel()
// Widget Synchronization : widget that share the same id will update each other
// without sending any extra osc message
// Widget Synchronization
sync = function() {
// Widget that share the same id will update each other
// without sending any extra osc message
$.each(__widgets__,function(i,widget) {
if (widget.length>1) {
var closureSync = function(x) {
return function() {
var v = widget[x].getValue()
@ -109,12 +109,29 @@ init = function(session,callback) {
}
}
}
for (j in widget) {
widget[j].off('sync').on('sync',closureSync(j))
widget[j].off('sync.id').on('sync.id',closureSync(j))
}
}
})
// widgets that share the same linkId will update each other.
// Updated widgets will send osc messages normally
$.each(__widgetsLinks__,function(i,widget) {
if (widget.length>1) {
var closureSync = function(x) {
return function() {
var v = widget[x].getValue()
for (k=0;k<widget.length;k++) {
if (x!=k) {
if (document.body.contains(widget[k][0].parentNode))
widget[k].setValue(v,true,false)
}
}
}
}
for (j in widget) {
widget[j].off('sync.link').on('sync.link',closureSync(j))
}
}
})
}

View file

@ -1,4 +1,5 @@
__widgets__ = {}
__widgetsLinks__ = {}
__widgetsIds__ = {}
@ -23,6 +24,7 @@ parsetabs = function(tabs,parent,main){
if (main) {
__widgets__ = {}
__widgetsLinks__ = {}
__widgetsIds__ = {}
tabIterator = {}
@ -137,15 +139,18 @@ parsewidgets = function(widgets,parent) {
widgetContainer.append(widgetInner)
// store widget reference for cross widget sync
if (__widgets__[widgetData.id]==undefined) {
__widgets__[widgetData.id] = []
}
if (__widgets__[widgetData.id]==undefined) __widgets__[widgetData.id] = []
__widgets__[widgetData.id].push(widgetInner)
// store widget with linkId for widgte linking
if (widgetData.linkId && widgetData.linkId.length) {
if (__widgetsLinks__[widgetData.linkId]==undefined) __widgetsLinks__[widgetData.linkId] = []
__widgetsLinks__[widgetData.linkId].push(widgetInner)
}
// store path vs widget id for faster cross-app sync
__widgetsIds__[widgetData.path ] = widgetData.id
__widgetsIds__[widgetData.path] = widgetData.id
parent.append(widgetContainer)
}

View file

@ -1,4 +1,5 @@
createWidget = {}
widgetOptions = {
strip: {
type:'strip',
@ -59,6 +60,7 @@ widgetOptions = {
toggle: {
type:'toggle',
id:'auto',
linkId:'',
separator1:'style',
@ -79,6 +81,7 @@ widgetOptions = {
push: {
type:'push',
id:'auto',
linkId:'',
separator1:'style',
@ -99,6 +102,7 @@ widgetOptions = {
switch: {
type:'switch',
id:'auto',
linkId:'',
separator1:'style',
@ -119,6 +123,7 @@ widgetOptions = {
xy: {
type:'xy',
id:'auto',
linkId:'',
separator1:'style',
@ -144,6 +149,7 @@ widgetOptions = {
rgb: {
type:'rgb',
id:'auto',
linkId:'',
separator1:'style',
@ -167,6 +173,7 @@ widgetOptions = {
fader: {
type:'fader',
id:'auto',
linkId:'',
separator1:'style',
@ -192,6 +199,7 @@ widgetOptions = {
knob: {
type:'knob',
id:'auto',
linkId:'',
separator1:'style',
@ -294,7 +302,7 @@ createWidget.toggle = function(widgetData) {
widget.fakeclick = function(){
var newVal = widget.hasClass('on')?widgetData.off:widgetData.on
widget.setValue(newVal,true)
widget.setValue(newVal,true,true)
$document.on('dragend.toggle',function(){
$document.off('dragend.toggle')
widget.on('draginit.toggle',function(){
@ -317,7 +325,7 @@ createWidget.toggle = function(widgetData) {
if (send) widget.sendValue(widgetData.off)
}
if (send) widget.trigger('sync')
if (sync) widget.trigger('sync')
}
widget.sendValue = function(v) {
@ -350,9 +358,9 @@ createWidget.push = function(widgetData) {
})
widget.fakeclick = function(){
widget.setValue(widgetData.on,true)
widget.setValue(widgetData.on,true,true)
$document.on('dragend.push',function(){
widget.setValue(widgetData.off,true)
widget.setValue(widgetData.off,true,true)
$document.off('dragend.push')
widget.on('draginit.push',function(){
widget.off('draginit.push')
@ -375,7 +383,7 @@ createWidget.push = function(widgetData) {
if (send) widget.sendValue(v)
}
if (send) widget.trigger('sync')
if (sync) widget.trigger('sync')
}
widget.sendValue = function(v) {