inputs: add keys widget (wip) (issue #57)

This commit is contained in:
jean-emmanuel 2017-12-31 12:41:41 +01:00
parent 2239c06bbb
commit 1d221b7613
3 changed files with 204 additions and 1 deletions

View file

@ -48,6 +48,7 @@ module.exports.widgets = {
// inputs
input: require('./inputs/input'),
keys: require('./inputs/keys'),
// maths
formula: require('./maths/formula')
@ -62,6 +63,6 @@ module.exports.categories = {
'Plots':['plot','eq','visualizer','led', 'rgbled','meter','text', 'image'],
'Containers':['panel','strip','modal', 'clone'],
'Switchers':['switcher','crossfader'],
'Inputs':['input'],
'Inputs':['input', 'keys'],
'Maths':['formula']
}

View file

@ -0,0 +1,162 @@
var _widgets_base = require('../common/_widgets_base'),
widgetManager = require('../../managers/widgets'),
{math} = require('../utils'),
{iconify} = require('../../utils'),
keyboardJS = require('keyboardjs')
module.exports = class Keys extends _widgets_base {
static defaults() {
return {
type:'led',
id:'auto',
_geometry:'geometry',
left:'auto',
top:'auto',
width:'auto',
height:'auto',
_style:'style',
label:'auto',
color:'auto',
css:'',
_keys:'keys',
binding:'',
widgetId:'',
keydown:'',
keyup:'',
repeat:false,
}
}
constructor(options) {
var html = `
<div class="keys">
${iconify('^keyboard')}
</div>
`
super({...options, html: html})
this.keydownString = String(this.getProp('keydown'))
this.keyupString = String(this.getProp('keyup'))
if (this.getProp('binding') && (this.keydownString || this.keyupString)) {
this.widget.append(`<span>${this.getProp('binding')}</span>`)
this.keydownHandler = this.keydownString ? this.keydown.bind(this) : this.showKeydown.bind(this)
this.keyupHandler = this.keyupString ? this.keyup.bind(this) : this.showKeyup.bind(this)
this.keydownMath = math.compile(this.keydownString)
this.keyupMath = math.compile(this.keyupString)
keyboardJS.bind(this.getProp('binding'), this.keydownHandler, this.keyupHandler)
}
this.linkedWidget = null
}
onRemove() {
super.onRemove()
if (this.getProp('binding') && (this.getProp('keydown') || this.getProp('keyup'))) {
keyboardJS.unbind(this.getProp('binding'), this.keydownHandler, this.keyupHandler)
}
}
keydown(e) {
if (e.target.classList.contains('input')) return
if (!this.getProp('repeat') && e) e.preventRepeat()
var widget = this.getLinkedWidget()
if (widget) {
var v = this.keydownMath.eval({
value: widget.getValue(),
key: e.key,
ctrl: e.ctrlKey,
shift: e.shiftKey,
alt: e.altKey,
meta: e.metaKey
}).valueOf()
if (Array.isArray(v) && v.length == 1) v = v[0]
widget.setValue(v, {send:true, sync:true, fromExternal:false})
}
this.showKeydown()
}
showKeydown(){
this.widget[0].style.setProperty('--opacity', 1)
}
keyup(e) {
if (e.target.classList.contains('input')) return
var widget = this.getLinkedWidget()
if (widget) {
var v = this.keyupMath.eval({
value: widget.getValue(),
key: e.key,
ctrl: e.ctrlKey,
shift: e.shiftKey,
alt: e.altKey,
meta: e.metaKey
}).valueOf()
if (Array.isArray(v) && v.length == 1) v = v[0]
widget.setValue(v, {send:true, sync:true, fromExternal:false})
}
this.showKeyup()
}
showKeyup(){
this.widget[0].style.setProperty('--opacity', 0.75)
}
getLinkedWidget() {
if (this.getProp('widgetId')) {
return widgetManager.getWidgetById(this.getProp('widgetId')).pop() || null
}
}
setValue() {}
}

View file

@ -62,3 +62,43 @@
border: 1rem solid var(--color-bg);
}
}
.keys-container {
width: 100rem;
height:60rem;
flex-shrink: 0;
padding:1rem;
--opacity: .75;
.keys {
flex: 1 1 100%;
height:35rem;
padding: 10rem;
justify-content: center;
display: flex;
align-items: center;
background: var(--color-track);
color:var(--color-custom);
border: 1rem solid var(--color-bg);
@extend .glass-effect;
i {
opacity: var(--opacity);
}
span {
font-family: monospace;
letter-spacing: 0;
margin-left: 4rem;
}
}
}
.panel:not(.tab) > .keys-container {
--color-border:transparent;
padding: 0rem;
.keys {
margin: 1rem;
box-shadow: 0 0 0 1rem var(--color-fg);
outline:1rem solid var(--color-light);
border: 1rem solid var(--color-bg);
}
}