split and clean code, wip...

This commit is contained in:
jean-emmanuel 2016-02-26 10:50:21 +01:00
parent 32696a7d46
commit 4648f6857a
22 changed files with 1699 additions and 1645 deletions

View file

@ -2,5 +2,4 @@ require('./parser.js') // session file parser
require('./utils.js') // state managing & inspector
require('./widgets.js') // widgets types and behaviour definition
require('./callbacks.js')// calls from mainProcess
require('./editor.js') // ...
require('./init.js') // ...

View file

@ -23,8 +23,9 @@ function getdata(obj){
}
enableEditor = function(){
disableEditor()
module.exports.enable = function(){
module.exports.disable()
var enableEditor = module.exports.enable
$('.widget').on('click.editor',function(e){
e.preventDefault()
e.stopPropagation()
@ -474,7 +475,7 @@ enableEditor = function(){
$('.disable-editor').removeClass('on')
}
disableEditor = function(){
module.exports.disable = function(){
$('.widget.ui-resizable').resizable('destroy')
$('.widget.ui-draggable').draggable('destroy').find('.ui-draggable-handle').remove()
$('.widget').off('click.editor')

View file

@ -1,3 +1,7 @@
var editor = require('./editor'),
enableEditor = editor.enable,
disableEditor = editor.disable
init = function(session,callback) {
$('#container').empty()

View file

@ -188,88 +188,3 @@ createPopup = function(title,content) {
return popup
}
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)}
}
rgbToHsb = function (rgb) {
var hsb = {h: 0, s: 0, b: 0}
var min = Math.min(rgb.r, rgb.g, rgb.b)
var max = Math.max(rgb.r, rgb.g, rgb.b)
var delta = max - min
hsb.b = max
hsb.s = max != 0 ? 255 * delta / max : 0
if (hsb.s != 0) {
if (rgb.r == max) hsb.h = (rgb.g - rgb.b) / delta
else if (rgb.g == max) hsb.h = 2 + (rgb.b - rgb.r) / delta
else hsb.h = 4 + (rgb.r - rgb.g) / delta
} else hsb.h = 0
hsb.h *= 60
if (hsb.h < 0) hsb.h += 360
hsb.s *= 100/255
hsb.b *= 100/255
return hsb
}
sizeToAngle = function(size) {
return clip(Math.acos((size/100))*180/Math.PI,[0,90])
}
clip = function(value,range) {
var value = parseFloat(value)
if (isNaN(value)) value = range[0]
return Math.max(Math.min(range[0],range[1]),Math.min(value,Math.max(range[0],range[1])))
}
// map a value from a scale to another input and output must be range arrays
mapToScale = function(value,rangeIn,rangeOut,precision) {
var value = clip(value,[rangeIn[0],rangeIn[1]]),
roundFactor = precision!=undefined?Math.pow(10,precision):100
value = ((value-rangeIn[0])/(rangeIn[1]-rangeIn[0])) * (rangeOut[1]-rangeOut[0]) + rangeOut[0]
value = Math.max(Math.min(rangeOut[0],rangeOut[1]),Math.min(value,Math.max(rangeOut[0],rangeOut[1])))
value = Math.round(value*roundFactor)/roundFactor
return value
}
function isSame(a1, a2){
if (a1.sort && a2.sort) {
return !(a1.sort() > a2.sort() || a1.sort() < a2.sort());
} else {
return a1==a2
}
}
$.fn.ripple = function() {
$(this).removeClass('ripple').addClass('ripple').delay(250).queue(function(next){
$(this).removeClass('ripple');
next();
});
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,201 @@
var utils = require('./utils'),
sizeToAngle = utils.sizeToAngle,
clip = utils.clip,
mapToScale = utils.mapToScale
module.exports.options = {
type:'fader',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
unit:'',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
horizontal:false,
css:'',
separator2:'behaviour',
absolute:false,
separator3:'osc',
range:{min:0,max:1},
precision:2,
path:'auto',
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="fader-wrapper-outer">
<div class="fader-wrapper">
<div class="fader">
<div class="handle"><span></span></div>
<div class="pips"></div>
</div>
</div>
<div class="input"></div>
</div>
`),
handle = widget.find('.handle'),
knob = widget.find('span')[0],
wrapper = widget.find('.fader-wrapper'),
fader = widget.find('.fader'),
pips = widget.find('.pips'),
input = widget.find('.input').fakeInput(),
unit = widgetData.unit?' '+widgetData.unit.trim(): '',
dimension = widgetData.horizontal?'width':'height',
axe = dimension=='height'?'X':'Y',
absolute = widgetData.absolute
if (widgetData.horizontal) container.addClass('horizontal')
handle.size = 0
fader.size = dimension=='height'?fader.outerHeight():fader.outerWidth()
wrapper.size = dimension=='height'?wrapper.outerHeight():wrapper.outerWidth()
fader.resize(function(){
fader.size= dimension=='height'?fader.outerHeight():fader.outerWidth()
wrapper.size = dimension=='height'?wrapper.outerHeight():wrapper.outerWidth()
})
var off = 0
wrapper.on('draginit',function(e,data){
if (absolute || data.ctrlKey || data.shiftKey) {
var d = (dimension=='height')?
((fader.size-data.offsetY+(wrapper.size-fader.size)/2) * 100 / fader.size):
(data.offsetX - (wrapper.size-fader.size)/2) * 100 / fader.size
var r = sizeToAngle(d)
handle[0].setAttribute('style','transform:rotate'+axe+'('+ r +'deg)')
knob.setAttribute('style','transform:rotate'+axe+'('+ (-r) +'deg)')
handle.size = d
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
}
off = handle.size
})
wrapper.on('drag',function(e,data){
e.stopPropagation()
if (data.shiftKey) return
var d = (dimension=='height')?-data.deltaY:data.deltaX
d = clip(d*100/fader.size+off,[0,100])
var r = sizeToAngle(d)
handle[0].setAttribute('style','transform:rotate'+axe+'('+ r +'deg)')
knob.setAttribute('style','transform:rotate'+axe+'('+ (-r) +'deg)')
handle.size = d
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
})
widgetData.range = widgetData.range || {'min':0,'max':1}
var range = {}
for (k in widgetData.range) {
if (k=='min') {
range[0]=widgetData.range[k]
} else if (k=='max') {
range[100]=widgetData.range[k]
} else {
range[parseInt(k)]=widgetData.range[k]
}
}
var scale = []
for (var i=0;i<=100;i++) {scale.push(i)}
for (i in scale) {
var pip = $('<div class="pip"></div>')
if (range[i]!=undefined) {
var piptext = Math.abs(range[i])>=1000?range[i]/1000+'k':range[i]
pip.addClass('val').append('<span>'+piptext+'</span>')
}
pips.append(pip)
}
if (dimension=='height') {
pips.append(pips.find('.pip').get().reverse())
}
var rangeKeys = Object.keys(range).map(function (key) {return parseInt(key)}),
rangeVals = Object.keys(range).map(function (key) {return parseFloat(range[key])})
widget.getValue = function(){
var h = clip(handle.size,[0,100])
for (var i=0;i<rangeKeys.length-1;i++) {
if (h <= rangeKeys[i+1] && h >= rangeKeys[i]) {
return mapToScale(h,[rangeKeys[i],rangeKeys[i+1]],[rangeVals[i],rangeVals[i+1]],widgetData.precision)
}
}
}
widget.setValue = function(v,send,sync) {
var h,
v=clip(v,[rangeVals[0],rangeVals.slice(-1)[0]])
for (var i=0;i<rangeVals.length-1;i++) {
if (v <= rangeVals[i+1] && v >= rangeVals[i]) {
h = mapToScale(v,[rangeVals[i],rangeVals[i+1]],[rangeKeys[i],rangeKeys[i+1]],widgetData.precision)
break
}
}
var r = sizeToAngle(h)
handle[0].setAttribute('style','transform:rotate'+axe+'('+ r +'deg)')
knob.setAttribute('style','transform:rotate'+axe+'('+ (-r) +'deg)')
handle.size = h
widget.showValue(v)
if (sync) widget.trigger('sync')
if (send) widget.sendValue(v)
}
widget.sendValue = function(v) {
sendOsc({
target:widgetData.target,
path:widgetData.path,
precision:widgetData.precision,
args:v
})
}
widget.showValue = function(v) {
input.val(v+unit)
}
input.change(function(){
widget.setValue(input.val(),true,true)
})
widget.setValue(rangeVals[0])
return widget
}

View file

@ -0,0 +1,191 @@
var utils = require('./utils'),
clip = utils.clip,
mapToScale = utils.mapToScale
module.exports.options = {
type:'knob',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
unit:'',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
css:'',
separator2:'behaviour',
absolute:false,
pan:false,
separator3:'osc',
range:{min:0,max:1},
precision:2,
path:'auto',
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="knob-wrapper-outer">
<div class="knob-wrapper">
<div class="knob-mask">
<div class="knob"><span></span></div>
<div class="handle"></div>
<div class="round"></div>
</div>
<div class="pip min"></div>
<div class="pip max"></div>
</div>
<div class="input"></div>
</div>
`),
wrapper = widget.find('.knob-wrapper'),
mask = widget.find('.knob-mask'),
knob = widget.find('.knob'),
handle = widget.find('.handle'),
input = widget.find('.input').fakeInput(),
range = widgetData.range || {min:0,max:1},
unit = widgetData.unit?' '+widgetData.unit.trim(): '',
absolute = widgetData.absolute,
pan = widgetData.pan
var pipmin = Math.abs(range.min)>=1000?range.min/1000+'k':range.min,
pipmax = Math.abs(range.max)>=1000?range.max/1000+'k':range.max
widget.find('.pip.min').text(pipmin)
widget.find('.pip.max').text(pipmax)
knob.rotation = 0
var offR = 0,
offX = 0,
offY = 0
wrapper.on('draginit',function(e,data){
if (absolute || data.ctrlKey || data.shiftKey) {
var w = data.target.clientWidth,
h = data.target.clientHeight,
x = data.offsetX-w/2,
y = data.offsetY-h/2,
angle = Math.atan2(-y, -x) * 180 / Math.PI +45,
r = angle<-90?angle+360:angle
r = (angle>=-90 && angle<-45)?270:r
r = clip(r,[0,270])
offX = x
offY = y
knob[0].setAttribute('style','transform:rotateZ('+r+'deg)')
handle[0].setAttribute('style','transform:rotateZ('+(r-45)+'deg)')
knob.rotation = r
if (pan && r<135) {mask.removeClass('pan-right').addClass('pan-left')}
else if (pan) {mask.removeClass('pan-left').addClass('pan-right')}
if (r>180) {knob.addClass('d3')}
else if (r>90) {knob.removeClass('d3').addClass('d2')}
else {knob.removeClass('d3 d2')}
var v = mapToScale(r,[0,270],[range.min,range.max],widgetData.precision)
widget.sendValue(v)
widget.trigger('sync')
widget.showValue(v)
}
offR = knob.rotation
})
wrapper.on('drag',function(e,data){
e.stopPropagation()
if (data.shiftKey) return
var r = clip(-data.deltaY*2+offR,[0,270])
if (absolute || data.ctrlKey || data.shiftKey) {
var w = data.target.clientWidth,
h = data.target.clientHeight,
x = data.deltaX + offX,
y = data.deltaY + offY,
angle = Math.atan2(-y, -x) * 180 / Math.PI +45,
r = angle<-90?angle+360:angle
r = (angle>=-90 && angle<-45)?270:r
r = clip(r,[0,270])
}
knob[0].setAttribute('style','transform:rotateZ('+r+'deg)')
handle[0].setAttribute('style','transform:rotateZ('+(r-45)+'deg)')
knob.rotation = r
if (pan && r<135) {mask.removeClass('pan-right').addClass('pan-left')}
else if (pan) {mask.removeClass('pan-left').addClass('pan-right')}
if (r>180) {knob.addClass('d3')}
else if (r>90) {knob.removeClass('d3').addClass('d2')}
else {knob.removeClass('d3 d2')}
var v = mapToScale(r,[0,270],[range.min,range.max],widgetData.precision)
widget.sendValue(v)
widget.trigger('sync')
widget.showValue(v)
})
widget.getValue = function() {
return mapToScale(knob.rotation,[0,270],[range.min,range.max],widgetData.precision)
}
widget.showValue = function(v) {
input.val(v+unit)
}
widget.sendValue = function(v) {
sendOsc({
target:widgetData.target,
path:widgetData.path,
precision:widgetData.precision,
args:v
})
}
widget.setValue = function(v,send,sync) {
var r = mapToScale(v,[range.min,range.max],[0,270],widgetData.precision)
knob.rotation = r
if (pan && r<135) {mask.removeClass('pan-right').addClass('pan-left')}
else if (pan) {mask.removeClass('pan-left').addClass('pan-right')}
if (r>180) {knob.addClass('d3')}
else if (r>90) {knob.removeClass('d3').addClass('d2')}
else {knob.removeClass('d3 d2')}
knob[0].setAttribute('style','transform:rotateZ('+r+'deg)')
handle[0].setAttribute('style','transform:rotateZ('+(r-45)+'deg)')
var v = widget.getValue()
widget.showValue(v)
if (sync) widget.trigger('sync')
if (send) widget.sendValue(v)
}
input.change(function(){
widget.setValue(input.val(),true,true)
})
widget.setValue(range.min)
return widget
}

View file

@ -0,0 +1,36 @@
module.exports.options = {
type:'led',
id:'auto',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
color:'green',
css:'',
separator2:'osc',
range:{min:0,max:1},
path:'auto'
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="led">
<div><span></span></div>
</div>
`),
led = widget.find('span'),
range = widgetData.range || {min:0,max:1}
if (widgetData.color) led.css('background-color',widgetData.color)
widget.setValue = function(v){
led.css('opacity',mapToScale(v,[range.min,range.max],[0,1],widgetData.precision))
}
widget.getValue = function(){return}
return widget
}

View file

@ -0,0 +1,36 @@
module.exports.options = {
type:'panel',
id:'auto',
separator1:'style',
label:'auto',
left:'0',
top:'0',
width:'auto',
height:'auto',
stretch:false,
css:'',
separator2:'chilren',
widgets:[],
tabs:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="panel">
</div>
`)
if (widgetData.stretch) widget.addClass('stretch')
if (widgetData.tabs.length) {
parsetabs(widgetData.tabs,widget)
} else {
parsewidgets(widgetData.widgets,widget)
}
widget.getValue = function(){return}
widget.setValue = function(){return}
return widget
}

View file

@ -0,0 +1,84 @@
module.exports.options = {
type:'push',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
css:'',
separator2:'osc',
on:1,
off:0,
precision:2,
path:'auto',
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="push toggle">
<div class="light"></div>
</div>\
`),
$document = $(document)
widget.value = widget.find('span')
widget.on('drag',function(e){e.stopPropagation()})
widget.on('draginit.push',function(){
widget.off('draginit.push')
widget.fakeclick()
})
widget.fakeclick = function(){
widget.setValue(widgetData.on,true,true)
$document.on('dragend.push',function(){
widget.setValue(widgetData.off,true,true)
$document.off('dragend.push')
widget.on('draginit.push',function(){
widget.off('draginit.push')
widget.fakeclick()
})
})
}
widget.getValue = function() {
return widget.hasClass('on')?widgetData.on:widgetData.off
}
widget.setValue = function(v,send,sync) {
var on = widgetData.on,
off= widgetData.off
if (v==on) {
widget.addClass('on').removeClass('ripple').addClass('ripple').delay(250).queue(function(next){
widget.removeClass('ripple')
next()
})
if (send) widget.sendValue(v)
} else if (v==off) {
widget.removeClass('on')
if (send) widget.sendValue(v)
}
if (sync) widget.trigger('sync')
}
widget.sendValue = function(v) {
if (v===false) return
sendOsc({
target:widgetData.target,
path:widgetData.path,
precision:widgetData.precision,
args:v
})
}
widget.setValue()
return widget
}

View file

@ -0,0 +1,272 @@
var utils = require('./utils'),
clip = utils.clip,
mapToScale = utils.mapToScale,
hsbToRgb = utils.hsbToRgb,
rgbToHsb = utils.rgbToHsb
module.exports.options = {
type:'rgb',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
css:'',
separator2:'behaviour',
absolute:false,
separator3:'osc',
precision:0,
path:'auto',
split:false,
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="xy-wrapper rgb-wrapper">
<div class="xy rgb">
<div class="bg"></div>
<div class="handle"></div>
</div>
<div class="hue">
<div class="bg"></div>
<div class="handle"></div>
</div>
<div class="value">
<div class="input disabled">R</div><div class="input disabled">G</div><div class="input disabled">B</div>
<div class="input r"></div><div class="input g"></div><div class="input b"></div>
</div>
</div>`),
rgbHandle = widget.find('.rgb .handle'),
hueHandle = widget.find('.hue .handle'),
pad = widget.find('.xy'),
huePad = widget.find('.hue'),
rgbBg = pad.find('.bg')[0],
value = {r:widget.find('.r').fakeInput({align:'center'}),g:widget.find('.g').fakeInput({align:'center'}),b:widget.find('.b').fakeInput({align:'center'})},
absolute = widgetData.absolute,
split = widgetData.split?
typeof widgetData.split == 'object'?
widgetData.split:{r:widgetData.path+'/r',g:widgetData.path+'/g',b:widgetData.path+'/b'}
:false
if (widgetData.height!='auto') widget.addClass('manual-height')
pad.width = pad.innerWidth()
pad.height = pad.innerHeight()
rgbHandle.height = 0
rgbHandle.width = 0
hueHandle.width = 0
pad.resize(function(){
var width = pad.innerWidth(),
height = pad.innerHeight()
if (!width || (pad.height==height && pad.width==width)) return
pad.width = width
pad.height = height
rgbHandle[0].setAttribute('style',`transform:translate3d(${pad.width*rgbHandle.width/100}px, -${pad.height*rgbHandle.height/100}px,0)`)
})
var rgbOff = {x:0,y:0}
pad.on('draginit',function(e,data){
if (absolute || data.ctrlKey || data.shiftKey) {
var h = ((pad.height-data.offsetY) * 100 / pad.height),
w = (data.offsetX * 100 / pad.width)
rgbHandle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px, -${pad.height*h/100}px,0)`)
rgbHandle.height = h
rgbHandle.width = w
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
}
rgbOff = {x:rgbHandle.width,y:rgbHandle.height}
})
pad.on('drag',function(e,data){
e.stopPropagation()
if (data.shiftKey) return
var h = clip((-data.deltaY)*100/pad.height+rgbOff.y,[0,100]),
w = clip(data.deltaX*100/pad.width+rgbOff.x,[0,100])
rgbHandle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px, -${pad.height*h/100}px,0)`)
rgbHandle.height = h
rgbHandle.width = w
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
})
var hueOff = 0
huePad.on('draginit',function(e,data){
if (absolute || data.ctrlKey || data.shiftKey) {
var d = (data.offsetX * 100 / pad.width)
hueHandle[0].setAttribute('style',`transform:translate3d(${pad.width*d/100}px,0,0)`)
hueHandle.width = d
var h = clip(hueHandle.width*3.6,[0,360]),
rgb = hsbToRgb({h:h,s:100,b:100}),
v = widget.getValue()
rgbBg.setAttribute('style','background-color:rgb('+rgb.r+','+rgb.g+','+rgb.b+')')
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
}
hueOff = hueHandle.width
})
huePad.on('drag',function(e,data){
e.stopPropagation()
if (data.shiftKey) return
var w = clip(data.deltaX*100/pad.width+hueOff,[0,100])
hueHandle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px,0,0)`)
hueHandle.width = w
var h = clip(hueHandle.width*3.6,[0,360]),
rgb = hsbToRgb({h:h,s:100,b:100}),
v = widget.getValue()
rgbBg.setAttribute('style','background-color:rgb('+rgb.r+','+rgb.g+','+rgb.b+')')
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
})
widget.getValue = function() {
var s = clip(rgbHandle.width,[0,100]),
l = clip(rgbHandle.height,[0,100]),
h = mapToScale(hueHandle.width,[0,100],[0,360],widgetData.precision),
rgb = hsbToRgb({h:h,s:s,b:l})
return [rgb.r,rgb.g,rgb.b]
}
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]]
for (i in [0,1,2]) {
v[i] = clip(v[i],[0,255])
}
var hsb = rgbToHsb({r:v[0],g:v[1],b:v[2]})
var w = mapToScale(hsb.s,[0,100],[0,100],widgetData.precision),
h = mapToScale(hsb.b,[0,100],[0,100],widgetData.precision),
hueW = mapToScale(hsb.h,[0,360],[0,100],widgetData.precision)
rgbHandle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px, -${pad.height*h/100}px,0)`)
hueHandle[0].setAttribute('style',`transform:translate3d(${pad.width*hueW/100}px,0,0)`)
rgbHandle.height = h
rgbHandle.width = w
hueHandle.width = hueW
var rgb = hsbToRgb({h:hsb.h,s:100,b:100})
rgbBg.setAttribute('style','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) {
sendOsc({
target:split?false:widgetData.target,
path:widgetData.path,
args:v,
precision:widgetData.precision,
})
if (split) {
sendOsc({
target:widgetData.target,
path:split.r,
args:v[0],
precision:widgetData.precision,
sync:false
})
sendOsc({
target:widgetData.target,
path:split.g,
args:v[1],
precision:widgetData.precision,
sync:false
})
sendOsc({
target:widgetData.target,
path:split.b,
args:v[2],
precision:widgetData.precision,
sync:false
})
}
}
widget.showValue = function(v) {
value.r.val(v[0])
value.g.val(v[1])
value.b.val(v[2])
}
value.r.change(function(){
var r = clip(value.r.val(),[0,255])
var v = widget.getValue()
widget.setValue([r,v[1],v[2]],true,true)
})
value.g.change(function(){
var g = clip(value.g.val(),[0,255])
var v = widget.getValue()
widget.setValue([v[0],g,v[2]],true,true)
})
value.b.change(function(){
var b = clip(value.b.val(),[0,255])
var v = widget.getValue()
widget.setValue([v[0],v[1],b],true,true)
})
widget.setValue(0)
return widget
}

View file

@ -0,0 +1,33 @@
module.exports.options = {
type:'strip',
id:'auto',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
horizontal:false,
css:'',
separator2:'children',
widgets:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="strip">
</div>
`)
if (widgetData.horizontal) {
container.addClass('horizontal')
} else {
container.addClass('vertical')
}
parsewidgets(widgetData.widgets,widget)
widget.getValue = function(){return}
widget.setValue = function(){return}
return widget
}

View file

@ -0,0 +1,79 @@
module.exports.options = {
type:'switch',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
horizontal:false,
css:'',
separator2:'osc',
values:{"Value 1":1,"Value 2":2},
precision:2,
path:'auto',
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="switch">
</div>
`),
$document = $(document)
if (widgetData.horizontal) {
widget.addClass('horizontal')
}
widget.values = []
for (k in widgetData.values) {
widget.values.push(widgetData.values[k])
$('<div class="value">'+k+'</div>').data({value:widgetData.values[k]}).appendTo(widget)
}
widget.value = undefined
widget.on('drag',function(e){e.stopPropagation()})
widget.on('draginit',function(e,dd){
var data = $(dd.target).data()
if (data.value!=widget.value || widget.value===undefined) widget.setValue(data.value,true,true)
})
widget.getValue = function() {
return widget.value
}
widget.setValue = function(v,send,sync) {
var i = widget.values.indexOf(v)
if (i!=-1) {
widget.value = widget.values[i]
widget.find('.on').removeClass('on')
widget.find('.value').eq(i).addClass('on').removeClass('ripple').addClass('ripple').delay(250).queue(function(next){
widget.removeClass('ripple')
next()
})
if (send) widget.sendValue(widget.value)
if (sync) widget.trigger('sync')
}
}
widget.sendValue = function(v) {
sendOsc({
target:widgetData.target,
path:widgetData.path,
precision:widgetData.precision,
args:v
})
}
return widget
}

View file

@ -0,0 +1,82 @@
module.exports.options = {
type:'toggle',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
css:'',
separator2:'osc',
on:1,
off:0,
precision:2,
path:'auto',
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="toggle">
<div class="light"></div>
</div>\
`),
$document = $(document)
widget.value = widget.find('span')
widget.on('drag',function(e){e.stopPropagation()})
widget.on('draginit.toggle',function(){
widget.off('draginit.toggle')
widget.fakeclick()
})
widget.fakeclick = function(){
var newVal = widget.hasClass('on')?widgetData.off:widgetData.on
widget.setValue(newVal,true,true)
$document.on('dragend.toggle',function(){
$document.off('dragend.toggle')
widget.on('draginit.toggle',function(){
widget.off('draginit.toggle')
widget.fakeclick()
})
})
}
widget.getValue = function() {
return widget.hasClass('on')?widgetData.on:widgetData.off
}
widget.setValue = function(v,send,sync) {
if (v==widgetData.on) {
widget.addClass('on').removeClass('ripple').addClass('ripple').delay(250).queue(function(next){
widget.removeClass('ripple')
next()
})
if (send) widget.sendValue(widgetData.on)
} else if (v==widgetData.off) {
widget.removeClass('on')
if (send) widget.sendValue(widgetData.off)
}
if (sync) widget.trigger('sync')
}
widget.sendValue = function(v) {
if (v===false) return
sendOsc({
target:widgetData.target,
path:widgetData.path,
precision:widgetData.precision,
args:v
})
}
widget.setValue()
return widget
}

View file

@ -0,0 +1,71 @@
module.exports = {
clip: function(value,range) {
var value = parseFloat(value)
if (isNaN(value)) value = range[0]
return Math.max(Math.min(range[0],range[1]),Math.min(value,Math.max(range[0],range[1])))
},
// map a value from a scale to another input and output must be range arrays
mapToScale: function(value,rangeIn,rangeOut,precision) {
var value = module.exports.clip(value,[rangeIn[0],rangeIn[1]]),
roundFactor = precision!=undefined?Math.pow(10,precision):100
value = ((value-rangeIn[0])/(rangeIn[1]-rangeIn[0])) * (rangeOut[1]-rangeOut[0]) + rangeOut[0]
value = Math.max(Math.min(rangeOut[0],rangeOut[1]),Math.min(value,Math.max(rangeOut[0],rangeOut[1])))
value = Math.round(value*roundFactor)/roundFactor
return value
},
sizeToAngle: function(size) {
return module.exports.clip(Math.acos((size/100))*180/Math.PI,[0,90])
},
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)}
},
rgbToHsb: function (rgb) {
var hsb = {h: 0, s: 0, b: 0}
var min = Math.min(rgb.r, rgb.g, rgb.b)
var max = Math.max(rgb.r, rgb.g, rgb.b)
var delta = max - min
hsb.b = max
hsb.s = max != 0 ? 255 * delta / max : 0
if (hsb.s != 0) {
if (rgb.r == max) hsb.h = (rgb.g - rgb.b) / delta
else if (rgb.g == max) hsb.h = 2 + (rgb.b - rgb.r) / delta
else hsb.h = 4 + (rgb.r - rgb.g) / delta
} else hsb.h = 0
hsb.h *= 60
if (hsb.h < 0) hsb.h += 360
hsb.s *= 100/255
hsb.b *= 100/255
return hsb
}
}

View file

@ -0,0 +1,181 @@
var utils = require('./utils'),
clip = utils.clip,
mapToScale = utils.mapToScale
module.exports.options = {
type:'xy',
id:'auto',
linkId:'',
separator1:'style',
label:'auto',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
css:'',
separator2:'behaviour',
absolute:false,
separator3:'osc',
rangeX:{min:0,max:1},
rangeY:{min:0,max:1},
precision:2,
path:'auto',
split:false,
target:[]
}
module.exports.create = function(widgetData,container) {
var widget = $(`
<div class="xy-wrapper">
<div class="xy">
<div class="handle"></div>
</div>
<div class="value">
<div class="input disabled">X</div><div class="input disabled">Y</div>
<div class="input x"></div><div class="input y"></div>
</div>
</div>
`),
handle = widget.find('.handle'),
pad = widget.find('.xy'),
value = {x:widget.find('.x').fakeInput({align:'center'}),y:widget.find('.y').fakeInput({align:'center'})},
range = {x:widgetData.rangeX,y:widgetData.rangeY},
absolute = widgetData.absolute,
split = widgetData.split?
typeof widgetData.split == 'object'?
widgetData.split:{x:widgetData.path+'/x',y:widgetData.path+'/y'}
:false
if (widgetData.height!='auto') widget.addClass('manual-height')
pad.width = pad.innerWidth()
pad.height = pad.innerHeight()
handle.height = 0
handle.width = 0
pad.resize(function(){
var width = pad.innerWidth(),
height = pad.innerHeight()
if (!width || (pad.height==height && pad.width==width)) return
pad.width = width
pad.height = height
handle[0].setAttribute('style',`transform:translate3d(${pad.width*handle.width/100}px, -${pad.height*handle.height/100}px,0)`)
})
var off = {x:0,y:0}
pad.on('draginit',function(e,data){
if (absolute || data.ctrlKey || data.shiftKey) {
var h = ((pad.height-data.offsetY) * 100 / pad.height),
w = (data.offsetX * 100 / pad.width)
handle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px, -${pad.height*h/100}px,0)`)
handle.height = h
handle.width = w
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
}
off = {x:handle.width,y:handle.height}
})
pad.on('drag',function(e,data){
e.stopPropagation()
if (data.shiftKey) return
var h = clip((-data.deltaY)*100/pad.height+off.y,[0,100]),
w = clip((data.deltaX)*100/pad.width+off.x,[0,100])
handle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px, -${pad.height*h/100}px,0)`)
handle.height = h
handle.width = w
var v = widget.getValue()
widget.sendValue(v)
widget.showValue(v)
widget.trigger('sync')
})
widget.getValue = function() {
var x = mapToScale(handle.width,[0,100],[range.x.min,range.x.max],widgetData.precision),
y = mapToScale(handle.height,[0,100],[range.y.min,range.y.max],widgetData.precision)
return [x,y]
}
widget.setValue = function(v,send,sync) {
if (v[1]==undefined) var v = [v,v]
for (i in [0,1]) {
v[i] = clip(v[i],[range[['x','y'][i]].min,range[['x','y'][i]].max])
}
var w = mapToScale(v[0],[range.x.min,range.x.max],[0,100],widgetData.precision)
h = mapToScale(v[1],[range.y.min,range.y.max],[0,100],widgetData.precision),
handle[0].setAttribute('style',`transform:translate3d(${pad.width*w/100}px, -${pad.height*h/100}px,0)`)
handle.height = h
handle.width = w
widget.showValue(v)
if (sync) widget.trigger('sync')
if (send) widget.sendValue(v)
}
widget.sendValue = function(v) {
sendOsc({
target:split?false:widgetData.target,
path:widgetData.path,
args:v,
precision:widgetData.precision,
})
if (split) {
sendOsc({
target:widgetData.target,
path:split.x,
args:v[0],
precision:widgetData.precision,
sync:false
})
sendOsc({
target:widgetData.target,
path:split.y,
args:v[1],
precision:widgetData.precision,
sync:false
})
}
}
widget.showValue = function(v) {
value.x.val(v[0])
value.y.val(v[1])
}
value.x.change(function(){
var v = widget.getValue()
v[0] = clip(value.x.val(),[range.x.min,range.x.max])
widget.setValue(v,true,true)
})
value.y.change(function(){
var v = widget.getValue()
v[1] = clip(value.y.val(),[range.y.min,range.y.max])
widget.setValue(v,true,true)
})
widget.setValue(range.x.min,range.y.min)
return widget
}