- generic synchronization between widgets

- added basic maptoscale function
- xy-pad and rgb-pad fully operationnal
 - rgb pad has a tiny glitch (only visual) when synchronizing with another one, due to hsbtorgb conversion choosing wrong hue (if saturation and/or lightness equals zero)
- still improving css (atom style...)
This commit is contained in:
jean-emmanuel 2015-08-18 16:33:42 +02:00 • committed by bordun
parent a83f17c1e7
commit e767fd8af6
3 changed files with 236 additions and 122 deletions

View file

@ -43,15 +43,10 @@ function parsetabs(TABS,parent,sub){
var $tabContent = $(document.createElement('div')).addClass('tab').attr('id',id);
if (tabData.tabs) {
parsetabs(tabData.tabs,parent=$tabContent,sub=true);
} else {
parsewidgets(tabData.widgets,$tabContent)
}
$content.append($tabContent);
@ -65,21 +60,24 @@ function parsewidgets(widgets,parent) {
var color = widgetData.color?'style="background-color:'+widgetData.color+'"':'';
var title = widgetData.title || widgetId;
var type = widgetData.type || 'fader'
widgetData.path = widgetData.path || '/' + widgetId
var $widget = $('\
<div class="widget" widgetId="'+widgetId+'" path="'+widgetData.path+'">\
<div class="widget" widgetType="'+type+'" widgetId="'+widgetId+'" path="'+widgetData.path+'">\
<div class="title" '+color+'>'+title+'</div>\
</div>\
');
var type = widgetData.type || 'fader'
var widgetInner = createWidget[type](widgetData,parent)
widgetInner.type = widgetData.type || 'fader'
$widget.append(widgetInner)
__widgets__[widgetId] = widgetInner
if (__widgets__[widgetId]==undefined) {
__widgets__[widgetId] = []
}
__widgets__[widgetId].push(widgetInner)
parent.append($widget);
@ -151,7 +149,10 @@ createWidget.xy = function(widgetData) {
<div class="xy">\
<div class="handle"><span></span></div>\
</div>\
<div class="val"></div>\
<div class="value">\
<input disabled value="X"></input><input disabled value="Y"></input>\
<input type="number" class="x"></input><input type="number" class="y"></input>\
</div>\
</div>\
');
$widget.xy = $widget.find('.xy')
@ -161,11 +162,13 @@ createWidget.xy = function(widgetData) {
containment:'parent'
});
$widget.xy.value = {x:$widget.find('.x'),y:$widget.find('.y')}
$widget.xy.on('drag',function(e, data){
var v = $widget.getValue();
$widget.sendValue(v);
$widget.find('.val').text(v)
$widget.showValue(v)
})
widgetData.range = widgetData.range || {x:{min:0,max:1},y:{min:0,max:1}}
@ -181,9 +184,9 @@ createWidget.xy = function(widgetData) {
var x = (v[0]-widgetData.range.x.min)/(widgetData.range.x.max-widgetData.range.x.min)*$widget.xy.innerWidth(),
y = $widget.xy.innerHeight()-(v[1]-widgetData.range.y.min)/(widgetData.range.y.max-widgetData.range.y.min)*$widget.xy.innerHeight()
$widget.xy.handle.css({'top':y+'px','left':x+'px'})
$widget.find('.val').text($widget.getValue())
if (send) $widget.xy.sendValue(v);
$widget.showValue(v)
if (send) $widget.sendValue(v);
}
$widget.sendValue = function(v) {
var t = widgetData.target?widgetData.target:defaultTarget,
@ -191,6 +194,10 @@ createWidget.xy = function(widgetData) {
sendOsc([t,p,v]);
}
$widget.showValue = function(v) {
$widget.xy.value.x.val(v[0])
$widget.xy.value.y.val(v[1])
}
return $widget;
}
@ -198,26 +205,26 @@ createWidget.xy = function(widgetData) {
var hsbToRgb = function (hsb) {
var rgb = {};
var h = hsb.h;
var s = hsb.s*255/100;
var v = hsb.b*255/100;
if(s == 0) {
rgb.r = rgb.g = rgb.b = v;
} else {
var t1 = v;
var t2 = (255-s)*v/255;
var t3 = (t1-t2)*(h%60)/60;
if(h==360) h = 0;
if(h<60) {rgb.r=t1; rgb.b=t2; rgb.g=t2+t3}
else if(h<120) {rgb.g=t1; rgb.b=t2; rgb.r=t1-t3}
else if(h<180) {rgb.g=t1; rgb.r=t2; rgb.b=t2+t3}
else if(h<240) {rgb.b=t1; rgb.r=t2; rgb.g=t1-t3}
else if(h<300) {rgb.b=t1; rgb.g=t2; rgb.r=t2+t3}
else if(h<360) {rgb.r=t1; rgb.g=t2; rgb.b=t1-t3}
else {rgb.r=0; rgb.g=0; rgb.b=0}
}
return {r:Math.round(rgb.r), g:Math.round(rgb.g), b:Math.round(rgb.b)};
var rgb = {};
var h = hsb.h;
var s = hsb.s*255/100;
var v = hsb.b*255/100;
if(s == 0) {
rgb.r = rgb.g = rgb.b = v;
} else {
var t1 = v;
var t2 = (255-s)*v/255;
var t3 = (t1-t2)*(h%60)/60;
if(h==360) h = 0;
if(h<60) {rgb.r=t1; rgb.b=t2; rgb.g=t2+t3}
else if(h<120) {rgb.g=t1; rgb.b=t2; rgb.r=t1-t3}
else if(h<180) {rgb.g=t1; rgb.r=t2; rgb.b=t2+t3}
else if(h<240) {rgb.b=t1; rgb.r=t2; rgb.g=t1-t3}
else if(h<300) {rgb.b=t1; rgb.g=t2; rgb.r=t2+t3}
else if(h<360) {rgb.r=t1; rgb.g=t2; rgb.b=t1-t3}
else {rgb.r=0; rgb.g=0; rgb.b=0}
}
return {r:Math.round(rgb.r), g:Math.round(rgb.g), b:Math.round(rgb.b)};
};
var rgbToHsb = function (rgb) {
var hsb = {h: 0, s: 0, b: 0};
@ -237,6 +244,37 @@ var rgbToHsb = function (rgb) {
hsb.b *= 100/255;
return hsb;
};
// map a value from a scale to another input and output must be range arrays
var mapToScale = function(value,rangeIn,rangeOut) {
var reverse = false;
if (rangeIn[0]>rangeIn[1]) {
reverse = true;
rangeIn = [rangeIn[1],rangeIn[0]]
}
var value = Math.max(rangeIn[0],Math.min(parseFloat(value),rangeIn[1]))
value = (value/rangeIn[1]-rangeIn[0]) * (rangeOut[1]-rangeOut[0]) + rangeOut[0]
if (reverse) value = Math.abs(rangeOut[1] - value)
value = Math.max(rangeOut[0],Math.min(value,rangeOut[1]))
value = Math.round(value)
return value
}
createWidget.rgb = function(widgetData) {
var $widget = $('\
<div class="xy-wrapper rgb-wrapper">\
@ -267,20 +305,33 @@ createWidget.rgb = function(widgetData) {
$widget.xy.value = {r:$widget.find('input.r'),g:$widget.find('input.g'),b:$widget.find('input.b')}
$widget.xy.hue.on('drag',function(e, data){
var h = parseFloat($widget.xy.hue.css('left'))/$widget.xy.innerWidth()*360,
rgb = hsbToRgb({h:h,s:100,b:100}),
v = $widget.getValue();
console.log(h)
$widget.xy.css('background-color','rgb('+rgb.r+','+rgb.g+','+rgb.b+')')
$widget.sendValue(v);
$widget.showValue(v);
$widget.trigger('sync')
})
$widget.xy.saturation.on('drag',function(e, data){
var v = $widget.getValue();
$widget.sendValue(v);
$widget.showValue(v);
$widget.trigger('sync')
})
$widget.xy.saturation.on('stop',function(e, data){
var v = $widget.getValue();
$widget.sendValue(v);
$widget.showValue(v);
//$widget.trigger('sync')
})
@ -289,16 +340,13 @@ createWidget.rgb = function(widgetData) {
$widget.getValue = function() {
var s = parseFloat($widget.xy.saturation.css('left'))/$widget.xy.innerWidth(),
s = widgetData.range.x.min + s *(widgetData.range.x.max - widgetData.range.x.min),
l = 1 - (parseFloat($widget.xy.saturation.css('top'))/$widget.xy.innerHeight()),
l = widgetData.range.y.min + l *(widgetData.range.y.max - widgetData.range.y.min),
h = parseFloat($widget.xy.hue.css('left'))/$widget.xy.innerWidth()*360,
var s = mapToScale($widget.xy.saturation.css('left'),[0,$widget.xy.innerWidth()],[0,100]),
l = mapToScale($widget.xy.saturation.css('top'),[$widget.xy.innerHeight(),0],[0,100]),
h = mapToScale($widget.xy.hue.css('left'),[0,$widget.xy.innerWidth()],[0,360]),
rgb = hsbToRgb({h:h,s:s,b:l})
return [rgb.r,rgb.g,rgb.b]
}
$widget.setValue = function(v,send) {
$widget.setValue = function(v,send,sync) {
if (v[1]==undefined && v[2]==undefined) var v = [v,v,v]
if (v[2]==undefined) var v = [v[0],v[1],v[1]]
@ -309,18 +357,21 @@ createWidget.rgb = function(widgetData) {
var hsb = rgbToHsb({r:v[0],g:v[1],b:v[2]})
var s = (hsb.s-widgetData.range.x.min)/(widgetData.range.x.max-widgetData.range.x.min)*$widget.xy.innerWidth(),
l = $widget.xy.innerHeight()-(hsb.b-widgetData.range.y.min)/(widgetData.range.y.max-widgetData.range.y.min)*$widget.xy.innerHeight()
h = hsb.h/360*$widget.xy.innerWidth()
var left = mapToScale(hsb.s,[0,100],[0,$widget.xy.innerWidth()]),
top = mapToScale(hsb.b,[100,0],[0,$widget.xy.innerHeight()]),
leftHue = mapToScale(hsb.h,[0,360],[0,$widget.xy.innerWidth()])
$widget.xy.saturation.css({'top':top+'px','left':left+'px'})
$widget.xy.hue.css({'left':leftHue+'px'})
$widget.xy.saturation.css({'top':l+'px','left':s+'px'})
$widget.xy.hue.css({'left':h+'px'})
var rgb = hsbToRgb({h:h,s:100,b:100})
var rgb = hsbToRgb({h:hsb.h,s:100,b:100})
$widget.xy.css('background-color','rgb('+rgb.r+','+rgb.g+','+rgb.b+')')
$widget.showValue(v);
if (sync) $widget.trigger('sync');
if (send) $widget.sendValue(v);
}
$widget.sendValue = function(v) {
@ -339,17 +390,17 @@ createWidget.rgb = function(widgetData) {
$widget.xy.value.r.change(function(){
if (!0<=$(this).val()<=255) {$(this).val(Math.min(255,Math.max(0,$(this).val())))}
var v = $widget.getValue()
$widget.setValue([$(this).val(),v[1],v[2]],true)
$widget.setValue([$(this).val(),v[1],v[2]],true,true)
})
$widget.xy.value.g.change(function(){
if (!0<=$(this).val()<=255) {$(this).val(Math.min(255,Math.max(0,$(this).val())))}
var v = $widget.getValue()
$widget.setValue([v[0],$(this).val(),v[2]],true)
$widget.setValue([v[0],$(this).val(),v[2]],true,true)
})
$widget.xy.value.b.change(function(){
if (!0<=$(this).val()<=255) {$(this).val(Math.min(255,Math.max(0,$(this).val())))}
var v = $widget.getValue()
$widget.setValue([v[0],v[1],$(this).val()],true)
$widget.setValue([v[0],v[1],$(this).val()],true,true)
})
@ -420,20 +471,28 @@ createWidget.fader = function(widgetData,parent) {
})
.Link('lower').to($widget.find('input'))
.on('slide',function(){
if (!$widget.lock) {$widget.sendValue()}
if (!$widget.lock) {
$widget.sendValue();
}
})
.on('set',function(){
if (!$widget.lock) {$widget.sendValue()}
if (!$widget.lock) {
$widget.sendValue();
$widget.trigger('sync')
}
});
$widget.getValue = function(v,send){
return $widget.fader.val();
}
$widget.setValue = function(v,send){
$widget.setValue = function(v,send,sync){
if (!send) {$widget.lock=true}
$widget.fader.val(v);
if (sync) $widget.trigger('sync')
$widget.lock=false;
}
$widget.sendValue = function(){
var t = widgetData.target?widgetData.target:defaultTarget,
@ -452,34 +511,37 @@ createWidget.fader = function(widgetData,parent) {
parsetabs(TABS,false,false)
// Widget Synchronization : widget that share the same id will update each other
// without sending any extra osc message (they must be of the same type as well)
$.each(__widgets__,function(i,widget) {
if (widget.length>1) {
var script ='';
for (j in widget) {
// Oh dear this looks hacky
// I didn't manage to cross-bind all widgets together the usual way
// ( getValue() always returns the last element's value, maybe a scope problem)
// Synchronize faders that share the same widgetId
$(".fader").each(function(){
$(".fader[widgetId='"+$(this).attr('widgetId')+"']").not(this).Link('lower').to($(this), crossUpdate);
}).removeClass('locked');
function crossUpdate ( value, handle, slider ) {
$(this).val( value );
}
script += ' \n\
widget['+j+'].on(\'sync\',function(){ \n\
var v = widget['+j+'].getValue() \n\
for (k=0;k<widget.length;k++) { \n\
if ('+j+'!=k) { \n\
widget[k].setValue(v,false,false) \n\
} \n\
} \n\
}) \n\
';
}
eval(script)
}
})
// Scrollbar
@ -490,15 +552,18 @@ $('.content .tab').niceScroll({
cursorborder:0,
cursorborderradius:0,
cursoropacitymin:1,
cursorwidth:'4px',
cursorwidth:'6px',
zindex:999999,
autohidemode:false,
cursorminheight:0,
background:'rgba(0,0,0,.2)',
railpadding: { top: 0, right: 0, left: 0, bottom: 1 },
});
// Tabs...
$('.tablist a').click(function(e){
$('.tab.on').getNiceScroll().hide();
$('.tab').getNiceScroll().hide();
e.preventDefault();
var id = $(this).attr('href');
@ -519,12 +584,14 @@ $('.tablist a').click(function(e){
function updateScrollbar(){
// update scrollbar
$('.content .tab').each(function(){
$(this).getNiceScroll().resize();
$('.tab.on').getNiceScroll().resize();
})
}
// Activate first tabs
$('.tablist li:first-child a').click();
setTimeout(function(){
$('.tablist li:first-child a').click();
},1500)
@ -554,6 +621,7 @@ $('.goFullscreen').click(function(e){
} else {
ipc.send('fullscreen',false)
}
updateScrollbar();
})
@ -588,7 +656,7 @@ function saveState() {
function getState(){
var data = []
$.each(__widgets__,function(i,widget) {
data.push(widget.type+':'+i+':'+widget.getValue())
data.push(widget[0].type+':'+i+':'+widget[0].getValue())
})
return data
}
@ -599,14 +667,16 @@ function loadState() {
function sendState(){
var data = getState().join('\n')
setState(data)
}
function setState(preset){
$.each(preset.split('\n'),function(i,d) {
var data = d.split(':')
setTimeout(function(){
if (__widgets__[data[1]]!=undefined) {
__widgets__[data[1]].setValue(data[2].split(','))
__widgets__[data[1]][0].setValue(data[2].split(','),true,true)
}
},i)
})
@ -644,7 +714,7 @@ ipc.on('receiveOsc',function(data){
var id = $('[path="'+path+'"]').attr('widgetId');
// update
if (__widgets__[id]!=undefined) __widgets__[id].setValue(data.args,false)
if (__widgets__[id]!=undefined) __widgets__[id][0].setValue(data.args,false,true)
})