encoder: tweaking the beast

This commit is contained in:
jean-emmanuel 2017-04-26 22:53:05 +02:00
parent 1875370376
commit 02c0b1a7dc
2 changed files with 105 additions and 16 deletions

View file

@ -19,8 +19,12 @@ module.exports = class Encoder extends _widgets_base {
width:'auto',
height:'auto',
color:'auto',
compact:true,
css:'',
_behaviour:'behaviour',
snap:false,
_osc:'osc',
@ -53,44 +57,73 @@ module.exports = class Encoder extends _widgets_base {
angle:360,
snap:true,
range:{min:0,max:this.ticks},
value:this.ticks/2,
compact:true,
noPip:true,
precision:0
}, true)
this.wrapper.append(this.knob.widget)
this.knob.noDraw = true
this.display = new Knob({
label:false,
angle:360,
range:{min:0,max:this.ticks},
compact:widgetData.compact,
noPip:true,
}, true)
this.wrapper.append(this.knob.widget.addClass('drag-knob'))
this.wrapper.append(this.display.widget.addClass('display-knob'))
this.knob.setValue(this.ticks/2)
this.display.setValue(this.ticks/2)
this.previousValue = this.ticks/2
this.knob.draginitHandle= function(e, data, traversing) {
this.percent = clip(this.percent,[0,100])
this.lastOffsetX = data.offsetX
this.lastOffsetY = data.offsetY
if (!(traversing || this.widgetData.snap)) return
this.percent = this.angleToPercent(this.coordsToAngle(data.offsetX, data.offsetY))
this.setValue(this.percentToValue(this.percent), {send:true,sync:true,dragged:true, draginit:true})
}
this.wrapper.on('sync',(e)=>{
e.stopPropagation()
var value = this.knob.getValue()
if (Math.round(value) == Math.round(this.previousValue)) return
var direction
if (value < this.previousValue) direction = widgetData.back
if (value > this.previousValue) direction = widgetData.forth
if (value < this.previousValue ||
((this.ticks * .75 < value && value < this.ticks) && (0 < this.previousValue && this.previousValue < this.ticks / 4))
) {
direction = widgetData.back
}
if ((0 < value && value < this.ticks / 4) && (this.ticks * .75 < this.previousValue && this.previousValue < this.ticks)) direction = widgetData.forth
if ((this.ticks * .75 < value && value < this.ticks) && (0 < this.previousValue && this.previousValue < this.ticks / 4)) direction = widgetData.back
if ((value > this.previousValue) ||
((0 < value && value < this.ticks / 4) && (this.ticks * .75 < this.previousValue && this.previousValue < this.ticks))
) {
direction = widgetData.forth
}
if (direction) this.setValue(direction, {sync:true, send:true})
if (direction && (Math.round(value) != Math.round(this.previousValue))) this.setValue(direction, {sync:true, send:true, dragged: e.options.dragged, draginit: e.options.draginit})
this.previousValue = value
})
this.wrapper.on('dragend', (e)=>{
this.knob.setValue(this.ticks/2)
this.previousValue = this.ticks/2
if (widgetData.release !== '') {
this.setValue(widgetData.release, {sync:true, send:true})
this.knob.setValue(this.ticks/2)
this.display.setValue(this.ticks/2)
this.setValue(widgetData.release, {sync:true, send:true, dragged:false})
}
})
@ -105,14 +138,57 @@ module.exports = class Encoder extends _widgets_base {
this.value = this.widgetData.back
} else if (v === this.widgetData.forth) {
this.value = this.widgetData.forth
} else if (v === this.widgetData.release && this.widgetData.release != '') {
} else if (v === this.widgetData.release && this.widgetData.release !== '') {
this.value = this.widgetData.release
} else {
match = false
}
if (options.dragged || options.draginit) this.updateDisplay(options.draginit)
if (options.sync && match) this.widget.trigger({type:'sync',id:this.widgetData.id,widget:this.widget, linkId:this.widgetData.linkId, options:options})
if (options.send && match) this.sendValue()
}
updateDisplay(init){
if (this.widgetData.snap) {
this.display.setValue(this.knob.value)
return
}
if (init) {
this.offset = this.knob.value - this.display.value
} else {
var v = this.knob.value - this.offset,
updateOffset
if (v > this.ticks) {
v = this.ticks - v
updateOffset = true
} else if (v < 0) {
v = v + this.ticks
updateOffset = true
}
this.display.setValue(v)
if (updateOffset) {
this.offset = this.knob.value - this.display.value
}
}
if (this.offset > this.ticks) this.offset = this.ticks - this.offset
if (this.offset < 0) this.offset = this.offset + this.ticks
}
}

View file

@ -78,4 +78,17 @@
.input {
display: none;
}
.display-knob {
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
pointer-events: none;
}
.drag-knob {
opacity: 0;
}
}