widgets: add input widget to replace former jquery implementation

This commit is contained in:
jean-emmanuel 2017-07-10 15:41:42 +02:00
parent 2d4c4588a7
commit 53c3ed7a35
20 changed files with 442 additions and 253 deletions

View file

@ -5,14 +5,16 @@
- ui
- general design improved, heavily inspired by [budislavTVP](http://budislavtvp.deviantart.com/) works; color schemes are much simpler and easier to customize (try `--theme light` !)
- widgets
- fader / meter: added `dashed` option;
- meter: added `alignRight` option;
- knob: removed `compact` option; added `dashed` option;
- fader: added `dashed` option; added `input` option;
- meter: added `alignRight` option; added `dashed` option;
- knob: removed `compact` option; added `dashed` option; added `input` option;
- xy/rgb: added `input` option;
- panel: remove inner widgets dark borders
- buttons: new `dropdown` widget
- new `input` widget
- editor
- option fields are now multiline (`ctrl + enter` to add a new line), and pretty printed;
- added checkboxes for boolean options
- added checkboxes for boolean options
- midi
- replaced `pyo` dependency with [`pyrtmidi`](https://github.com/patrickkidd/pyrtmidi): lighter, dedicated to midi, and handling virtual ports (no `mididings` dependency anymore)
- pitchbend value is now a single integer between 0 and 16383

View file

@ -0,0 +1,27 @@
# inputs
## Input
```js
{
type:'input',
// etc
}
```
### `widgetId`
- type: `object`
- default: `empty`
- usage: a widget's `id` whose value will be linked to the input
### `horizontal`
- type: `boolean`
- default: `false`
- usage: set to `true` to display the input horizontally
### `unit`
- type: `string`
- default: `empty`
- usage: `unit` will be appended to the displayed widget's value (it doesn't affect osc messages)

View file

@ -13,6 +13,10 @@ Pads are multidimensional widgets that output multiple values.
}
```
### `input`
- type: `boolean`
- default: `true`
- usage: set to `false` to hide the built-in input
### `rangeX`
- type: `object`
@ -131,6 +135,10 @@ RGB is a variant of XY, it outputs rgb values between 0 and 255.
}
```
### `input`
- type: `boolean`
- default: `true`
- usage: set to `false` to hide the built-in input
### `precision`
- default:`0`

View file

@ -27,6 +27,11 @@ Sliders are unidimensional widgets that output one value at a time.
- default: `true`
- usage: set to `false` to hide the scale
### `input`
- type: `boolean`
- default: `true`
- usage: set to `false` to hide the built-in input
### `compact`
- type: `boolean`
- default: `false`
@ -121,6 +126,11 @@ range: {
- default: `true`
- usage: set to `false` to hide the scale
### `input`
- type: `boolean`
- default: `true`
- usage: set to `false` to hide the built-in input
### `compact`
- type: `boolean`
- default: `false`

View file

@ -20,7 +20,7 @@ var scrolls = function(){
}
})
$(document).on('keydown.resetzoom', function(e){
if (e.keyCode==96||e.keyCode==48) {
if (e.ctrlKey && (e.keyCode==96||e.keyCode==48)) {
PXSCALE = INITIALZOOM
document.documentElement.style.setProperty('font-size', PXSCALE + 'px')

View file

@ -52,13 +52,17 @@ module.exports = class _canvas_base extends _widgets_base {
this.clearRect = []
if (ratio != 1) this.ctx.scale(ratio, ratio)
if (ratio != 1) {
this.ctx.setTransform(1, 0, 0, 1, 0, 0)
this.ctx.scale(ratio, ratio)
}
}
var style = getComputedStyle(this.widget[0])
if (!this.visible || checkColors) {
this.visible = true
var style = getComputedStyle(this.widget[0])
this.colors.custom = style.getPropertyValue('--color-custom')
this.colors.text = style.getPropertyValue('--color-text')
this.colors.raised = style.getPropertyValue('--color-raised')
@ -66,14 +70,15 @@ module.exports = class _canvas_base extends _widgets_base {
this.colors.fg = style.getPropertyValue('--color-fg')
this.colors.faded = style.getPropertyValue('--color-faded')
this.colors.light = style.getPropertyValue('--color-light')
this.fontSize = parseFloat(style.getPropertyValue("font-size"))
this.fontFamily = style.getPropertyValue("font-family")
this.textAlign = style.getPropertyValue("text-align")
this.ctx.textBaseline = "middle"
this.ctx.font = this.fontSize + 'px ' + this.fontFamily
this.ctx.textAlign = this.textAlign
}
this.fontSize = parseFloat(style.getPropertyValue("font-size"))
this.ctx.font = this.fontSize + 'px ' + this.fontFamily
this.ctx.textBaseline = "middle"
this.ctx.textAlign = this.textAlign
requestAnimationFrame(this.draw.bind(this))
}

View file

@ -0,0 +1,200 @@
var {iconify} = require('../../utils'),
_canvas_base = require('../common/_canvas_base'),
{widgetManager} = require('../../managers')
module.exports = class Input extends _canvas_base {
static defaults() {
return {
type:'input',
id:'auto',
linkId:'',
_style:'style',
label:'auto',
unit: '',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
horizontal:false,
color:'auto',
css:'',
_plot:'input',
widgetId:'',
_osc:'osc',
value:'',
precision:2,
address:'auto',
preArgs:[],
target:[]
}
}
constructor(options) {
var html = `
<div class="input">
<canvas tabindex="0"></canvas>
</div>
`
super({...options, html: html})
this.scalin
this.value = ''
this.stringValue = ''
this.canvas.on('focus',()=>{
this.canvas.attr('tabindex','-1')
let v = this.stringValue
let i = $('<input></input>')
.prependTo(this.widget)
.val(v)
.focus()
.change(()=>{
this.setValue(i.val(), {sync:true, send:true})
i.blur()
})
i.blur(()=>{
this.canvas.attr('tabindex','0')
i.remove()
$(document).off('.fakeInput')
})
$(document).on('fake-click.fakeInput',function(e, d){
if (d.originalEvent.target!=i[0]) {
i.blur()
}
}).on('keydown.fakeInput', function(e){
if (e.keyCode==13) i.change()
if (e.keyCode==27) i.off('change').blur()
})
})
if (this.getProp('widgetId').length) $('body').on(`change.${this.hash}`,this.syncHandle.bind(this))
}
onRemove() {
$('body').off(`change.${this.hash}`)
super.onRemove()
}
syncHandle(e) {
if (this.getProp('widgetId')!=e.id || !widgetManager.getWidgetById(e.id).length) return
var widget = widgetManager.getWidgetById(e.id),
value
for (var i=widget.length-1; i>=0; i--) {
if (widget[i].getValue) {
this.setValue(widget[i].getValue(), e.options)
return
}
}
}
resizeHandle(){
super.resizeHandle(...arguments)
if (this.getProp('horizontal')){
this.ctx.setTransform(1, 0, 0, 1, 0, 0)
this.ctx.rotate(-Math.PI/2)
this.ctx.translate(-this.height, 0)
var ratio = CANVAS_SCALING * this.scaling
if (ratio != 1) this.ctx.scale(ratio, ratio)
}
}
setValue(v, options={} ) {
try {
this.value = JSON.parse(v)
} catch (err) {
this.value = v
}
this.stringValue = this.getStringValue()
this.draw()
if (this.getProp('widgetId').length && !options.sync) {
var widget = widgetManager.getWidgetById(this.getProp('widgetId'))
for (var i=widget.length-1; i>=0; i--) {
if (widget[i].setValue) {
widget[i].setValue(this.value, options)
}
}
}
if (options.sync) this.widget.trigger({type:'change',id:this.getProp('id'),widget:this.widget, linkId:this.getProp('linkId'), options:options})
if (options.send) this.sendValue(v)
}
draw() {
var v = this.stringValue,
width = this.getProp('horizontal') ? this.height : this.width,
height = !this.getProp('horizontal') ? this.height : this.width
if (this.getProp('unit')) v += ' ' + this.getProp('unit')
this.clear()
this.ctx.fillStyle = this.colors.text
if (this.textAlign == 'center') {
this.ctx.fillText(v, Math.round(width/2), Math.round(height/2))
} else if (this.textAlign == 'right') {
this.ctx.fillText(v, width, Math.round(height/2))
} else {
this.ctx.fillText(v, 0, Math.round(height/2))
}
this.clearRect = [0, 0, width, height]
}
static deepCopyWithPrecision(obj, precision) {
var copy = obj,
key
if (typeof obj === 'object') {
copy = Array.isArray(obj) ? [] : {}
for (key in obj) {
copy[key] = Input.deepCopyWithPrecision(obj[key], precision)
}
} else if (typeof obj == 'number') {
return parseFloat(copy.toFixed(precision))
}
return copy
}
getStringValue() {
return typeof this.value != 'string' ?
JSON.stringify(Input.deepCopyWithPrecision(this.value, this.precision)).replace(/,/g, ', ') :
this.value
}
}

View file

@ -1,6 +1,7 @@
var _pads_base = require('./_pads_base'),
Fader = require('./_fake_fader'),
{clip, hsbToRgb, rgbToHsb} = require('../utils')
{clip, hsbToRgb, rgbToHsb} = require('../utils'),
Input = require('../inputs/input')
var faderDefaults = Fader.defaults()
@ -49,21 +50,7 @@ module.exports = class Rgb extends _pads_base {
: [this.getProp('address') + '/r', this.getProp('address') + '/g', this.getProp('address') + '/b']
: false
this.widget.append(`
<div class="hue-wrapper"></div>
<div class="labels row">
<span class="input">R</span>
<span class="input">G</span>
<span class="input">B</span>
</div>
<div class="row">
<div class="input r"></div>
<div class="input g"></div>
<div class="input b"></div>
</div>
`)
this.hueWrapper = this.widget.find('.hue-wrapper')
this.hueWrapper = $(`<div class="hue-wrapper"></div>`).appendTo(this.widget)
this.faders = {
h: new Fader({props:{
@ -98,12 +85,6 @@ module.exports = class Rgb extends _pads_base {
}, cancelDraw: true})
}
this.inputs = [
this.widget.find('.r').fakeInput({align:'center'}),
this.widget.find('.g').fakeInput({align:'center'}),
this.widget.find('.b').fakeInput({align:'center'})
]
this.value = []
this.hsb = {h:0,s:0,b:0}
@ -132,14 +113,20 @@ module.exports = class Rgb extends _pads_base {
this.dragHandle()
})
if (this.getProp('input')) {
this.inputs.forEach(function(input,i){
input.change(()=>{
this.value[i] = parseFloat(clip(this.inputs[i].val(),[0,255]))
this.setValue(this.value,{send:true,sync:true})
this.input = new Input({
props:{...Input.defaults(), precision:this.getProp('precision'), unit:this.getProp('unit')},
parent:this, parentNode:this.widget
})
this.widget.append(this.input.widget)
this.input.widget.on('change', (e)=>{
e.preventDefault()
this.setValue(this.input.getValue())
this.showValue()
})
},this)
}
this.setValue([0,0,0])
@ -200,12 +187,11 @@ module.exports = class Rgb extends _pads_base {
this.faders.h.setValue(this.hsb.h, {sync: false, send:false, dragged:false})
}
for (let i in this.inputs) [
this.inputs[i].val(this.value[i].toFixed(this.precision))
]
this.draw()
this.showValue()
}
draw() {
@ -248,4 +234,10 @@ module.exports = class Rgb extends _pads_base {
]
}
showValue() {
if (this.getProp('input')) this.input.setValue(this.value)
}
}

View file

@ -1,7 +1,8 @@
var _pads_base = require('./_pads_base'),
Fader = require('./_fake_fader'),
{clip} = require('../utils'),
doubletabreset = require('../mixins/double_tap_reset')
doubletabreset = require('../mixins/double_tap_reset'),
Input = require('../inputs/input')
var faderDefaults = Fader.defaults()
@ -21,6 +22,7 @@ module.exports = class Xy extends _pads_base {
top:'auto',
width:'auto',
height:'auto',
input: true,
color:'auto',
css:'',
@ -57,17 +59,6 @@ module.exports = class Xy extends _pads_base {
: [this.getProp('address') + '/x', this.getProp('address') + '/y']
: false
this.widget.append(`
<div class="labels row">
<span class="input">X</span>
<span class="input">Y</span>
</div>
<div class="row">
<div class="input x"></div>
<div class="input y"></div>
</div>
`)
this.faders = {
x: new Fader({props:{
...faderDefaults,
@ -101,11 +92,6 @@ module.exports = class Xy extends _pads_base {
}, cancelDraw: true}),
}
this.inputs = [
this.widget.find('.x').replaceWith(this.faders.x.input),
this.widget.find('.y').replaceWith(this.faders.y.input)
]
this.value = [this.faders.x.value, this.faders.y.value]
this.wrapper.append(this.faders.x.widget)
@ -138,14 +124,22 @@ module.exports = class Xy extends _pads_base {
})
}
this.faders.x.input.change(()=>{
var v = [this.faders.x.value, this.faders.y.value]
this.setValue(v,{send:true,sync:true})
})
this.faders.y.input.change(()=>{
var v = [this.faders.x.value, this.faders.y.value]
this.setValue(v,{send:true,sync:true})
})
if (this.getProp('input')) {
this.input = new Input({
props:{...Input.defaults(), precision:this.getProp('precision'), unit:this.getProp('unit')},
parent:this, parentNode:this.widget
})
this.widget.append(this.input.widget)
this.input.widget.on('change', (e)=>{
e.preventDefault()
this.setValue(this.input.getValue())
this.showValue()
})
}
this.setValue([0,0])
}
@ -181,6 +175,8 @@ module.exports = class Xy extends _pads_base {
this.draw()
this.showValue()
}
draw() {
@ -221,4 +217,10 @@ module.exports = class Xy extends _pads_base {
]
}
showValue() {
if (this.getProp('input')) this.input.setValue(this.value)
}
}

View file

@ -45,11 +45,10 @@ module.exports = class Meter extends Fader {
constructor(options) {
options.props.compact = true
options.props.input = false
super(options)
this.input.hide()
this.widget.addClass('meter')
.off('mousewheel')

View file

@ -1,7 +1,8 @@
var {clip, mapToScale} = require('../utils'),
_canvas_base = require('../common/_canvas_base'),
osctouchstate = require('../mixins/osc_touch_state'),
doubletabreset = require('../mixins/double_tap_reset')
doubletabreset = require('../mixins/double_tap_reset'),
Input = require('../inputs/input')
module.exports = class _sliders_base extends _canvas_base {
@ -12,14 +13,13 @@ module.exports = class _sliders_base extends _canvas_base {
<div class="wrapper">
<canvas></canvas>
</div>
<div class="input"></div>
</div>
`
super({...options, html: html})
this.wrapper = this.widget.find('.wrapper')
this.input = this.widget.find('.input').fakeInput({align:'center'})
this.value = undefined
this.percent = 0
@ -60,14 +60,6 @@ module.exports = class _sliders_base extends _canvas_base {
}
this.widget.on('fake-right-click',function(e){
if (!EDITING) {
e.stopPropagation()
e.preventDefault()
this.input.focus()
}
}.bind(this))
if (this.getProp('touchAddress') && this.getProp('touchAddress').length)
osctouchstate(this, this.wrapper)
@ -76,11 +68,35 @@ module.exports = class _sliders_base extends _canvas_base {
this.wrapper.on('drag',this.dragHandleProxy.bind(this))
this.wrapper.on('dragend',this.dragendHandleProxy.bind(this))
this.input.change(()=>{
this.setValue(parseFloat(this.input.val()),{sync:true,send:true})
if (this.getProp('input')) {
})
this.input = new Input({
props:{
...Input.defaults(),
precision:this.getProp('precision'),
unit:this.getProp('unit'),
horizontal: this.getProp('type') == 'fader' && this.getProp('compact') && !this.getProp('horizontal')
},
parent:this, parentNode:this.widget
})
this.widget.append(this.input.widget)
this.input.widget.on('change', (e)=>{
e.preventDefault()
this.setValue(this.input.getValue())
this.showValue()
})
this.widget.on('fake-right-click',function(e){
if (!EDITING) {
e.stopPropagation()
e.preventDefault()
this.input.canvas.focus()
}
}.bind(this))
}
this.setValue(this.originValue)
@ -200,7 +216,7 @@ module.exports = class _sliders_base extends _canvas_base {
showValue() {
this.input.val(this.value.toFixed(this.precision) + this.unit)
if (this.getProp('input')) this.input.setValue(this.value)
}

View file

@ -22,6 +22,7 @@ module.exports = class Fader extends _sliders_base {
alignRight:false,
horizontal:false,
pips:true,
input: true,
compact:false,
dashed:false,
color:'auto',

View file

@ -20,6 +20,7 @@ module.exports = class Knob extends _sliders_base {
height:'auto',
color:'auto',
pips:true,
input: true,
dashed:false,
angle:270,
css:'',

View file

@ -57,7 +57,6 @@ window.$ = require('jquery/dist/jquery.slim.min.js')
require('./libs/jquery.ui')
require('./libs/jquery.drag')
require('./libs/jquery.resize')
require('./libs/jquery.fake-input')
///////////////////////

View file

@ -1,103 +0,0 @@
;(function ($) {
$.fn.fakeInput = function(options) {
var settings = $.extend({
align:'left'
}, options )
var self= this,
canvas = $('<canvas></canvas>').appendTo(self),
ctx = canvas[0].getContext('2d')
self.value = undefined
self.height = undefined
self.width = undefined
self.color = undefined
self.center = settings.align == 'center'
self.visible = false
self.attr('tabindex',"0")
canvas.resize(function(e,width,height,checkColors){
var width = width,
height = height
if (width && height) {
self.height=height
self.width=width
canvas[0].setAttribute('width',width * 2)
canvas[0].setAttribute('height',height * 2)
ctx.scale(2,2)
}
if (!self.visible || checkColors) {
self.color = getComputedStyle(canvas[0]).getPropertyValue("--color-text")
self.fontSize = parseFloat(getComputedStyle(canvas[0]).getPropertyValue("font-size"))
}
ctx.font = self.fontSize + 'px Droid Sans'
ctx.textBaseline = "middle"
ctx.fillStyle = self.color
ctx.textAlign = settings.align
self.val(self.value)
})
self.val = function(newVal) {
if (newVal==undefined) return self.value
self.value = newVal
if (!self.height || !self.width || (self.height==100 && self.width==100)) return
ctx.clearRect(0,0,self.width,self.height)
if (settings.align == 'center') {
ctx.fillText(newVal,self.width/2,this.height/2)
} else if (settings.align == 'left') {
ctx.fillText(newVal,0,this.height/2)
} else if (settings.align == 'right') {
ctx.fillText(newVal,this.width,this.height/2)
}
}
if (!options.disabled) {
self.on('focus',function(){
self.attr('tabindex','-1')
var i = $('<input></input>')
.prependTo(self)
.val(self.value)
.focus()
.change(function(){
self.val($(this).val())
i.blur()
})
i.blur(function(){
self.attr('tabindex','0')
i.remove()
$(document).off('.fakeInput')
})
$(document).on('touchstart.fakeInput mousedown.fakeInput',function(e){
if (e.originalEvent.target!=i[0] && !(e.originalEvent.sourceCapabilities && e.originalEvent.sourceCapabilities.firesTouchEvents)) {
i.blur()
}
}).on('keydown.fakeInput', function(e){
if (e.keyCode==13) i.change()
if (e.keyCode==27) i.off('change').blur()
})
})
}
return self
}
})($)

View file

@ -48,53 +48,6 @@
color:var(--color-accent);
}
}
.Xinput {
width:100%;
height:24rem;
border:0;
background:transparent;
color:var(--color-text-fade);
display:block;
padding:4rem 5rem;
position: relative;
display: var(--display-input);
// background: var(--color-track);
// border: 1rem solid var(--color-bg);
input {
position: absolute;
top:0;
left:0;
right:0;
bottom:0;
padding:4rem 5rem;
width: 100%;
font: inherit;
text-align:center;
border:0;
background: transparent;
color:var(--color-accent);
border-radius: 0;
-webkit-appearance:none;
// font-weight: bold;
}
canvas {
width:100%;
height:100%;
opacity: 0.8;
}
input + canvas {
visibility: hidden;
}
&:focus {
color:var(--color-accent);
}
&[disabled] {
color:var(--color-text-fade);
}
}
}

View file

@ -32,6 +32,15 @@
flex-direction: column;
position: relative;
> .input {
height: 25rem;
line-height: 25rem;
flex:initial!important;
display: block;
background: transparent!important;
border: 0!important;
}
.wrapper {
overflow: hidden;
flex:1;
@ -240,6 +249,10 @@
position: absolute;
border:0;
background:transparent;
overflow: hidden;
position: absolute;
top:50%;
left:50%;
}
&.horizontal {
@ -255,8 +268,10 @@
}
.input {
top:50%;
margin-top: calc(var(--input-margin) - 12rem );
width: 100rem;
height: 24rem;
margin-left: -50rem;
margin-top: calc(-1 * 12rem + var(--input-margin));
}
.pips {
@ -274,16 +289,15 @@
&:not(.horizontal) {
.input {
overflow: hidden;
position: absolute;
top:50%;
transform: rotate(-90deg);
width: 100rem;
margin-top: -12rem;
margin-left: calc(-1 * 50rem + var(--input-margin));
left:50%;
height: 100rem;
width: 24rem;
margin-top: -50rem;
margin-left: calc(-1 * 12rem + var(--input-margin));
z-index: 3;
input {
transform: rotate(-90deg);
width: 100rem;
}
}
&.align-right {

View file

@ -0,0 +1,55 @@
.input-container {
width: 100rem;
height: 60rem;
.input {
}
}
.widget {
.input {
display: flex;
flex:1;
background:transparent;
color:var(--color-text-fade);
padding:4rem 5rem;
display: var(--display-input);
overflow: hidden;
// background: var(--color-track);
// border: 1rem solid var(--color-bg);
text-align:center;
margin:1rem;
padding:1rem;
border: 1rem solid var(--color-bg);
background: var(--color-track);
input {
height: 100%;
width: 100%;
font: inherit;
border:0;
padding:0;
background: transparent;
color:var(--color-accent);
border-radius: 0;
-webkit-appearance:none;
text-align:inherit;
vertical-align: top;
// font-weight: bold;
}
canvas {
height: 100%;
width: 100%;
}
input + canvas {
// visibility:hidden;
// position: absolute;
display: none;
}
&:focus {
color:var(--color-accent);
}
}
}

View file

@ -20,6 +20,15 @@
flex-direction: column;
position: relative;
> .input {
height: 25rem;
line-height: 25rem;
flex:initial!important;
display: block;
background: transparent!important;
border: 0!important;
}
.wrapper {
overflow: hidden;
flex:1;

View file

@ -30,13 +30,12 @@
overflow: hidden;
display: flex;
flex-direction: column;
.row {
display: flex;
text-align: center;
flex-shrink: 0;
.input {
flex:1;
}
> .input {
height: 25rem;
line-height: 25rem;
flex:initial!important;
display: block;
flex-shrink: 0!important;
}
> .wrapper {
position: relative;
@ -166,7 +165,7 @@
canvas {
border:0;
}
.row {
.input {
display: none;
}
&:not(:nth-child(2)) {