custom color picker

This commit is contained in:
bordun 2020-02-27 14:35:08 +01:00
parent c67c2b4c50
commit 7b8af3eeea
53 changed files with 988 additions and 519 deletions

View file

@ -107,7 +107,6 @@ function updateWidget(widget, options={}) {
newWidget.container.parentNode.replaceChild(newWidget.container, widget.container)
if (newWidget.getProp('type') === 'tab') newWidget.parent.trigger('tab-created', {widget: widget})
if (newWidget.getProp('id') === 'root') DOM.get('.editor-root')[0].setAttribute('data-widget', newWidget.hash)
if (reuseChildren && !(newWidget instanceof Panel)) {

View file

@ -399,12 +399,8 @@ var Editor = class Editor {
this.unselect()
this.selectedWidgets = []
DOM.get('.disable-editor')[0].classList.add('on')
DOM.get('.enable-editor')[0].classList.remove('on')
document.body.classList.remove('editor-enabled')
var gridForm = DOM.get('#grid-width-form')[0]
if (gridForm) gridForm.parentNode.removeChild(gridForm)
document.body.classList.remove('editor-enabled')
keyboardJS.setContext('global')

View file

@ -216,7 +216,7 @@ module.exports = {
this._customBindings['drag'] !== 0 ||
this._customBindings['draginit'] !== 0 ||
this._customBindings['dragend'] !== 0 ||
!options
!options || options.ignoreCustomBindings
) {
return
}
@ -243,7 +243,7 @@ module.exports = {
this._customBindings['drag'] !== 0 ||
this._customBindings['draginit'] !== 0 ||
this._customBindings['dragend'] !== 0 ||
!options
!options || options.ignoreCustomBindings
) {
return
}

View file

@ -0,0 +1,52 @@
/*
Adapted from https://gist.github.com/Yaffle/1145197#file-convertpointfrompagetonode-js
Disabled for now
*/
var I = new WebKitCSSMatrix()
function Point(x, y, z) {
this.x = x
this.y = y
this.z = z
}
Point.prototype.transformBy = function(matrix) {
var tmp = matrix.multiply(I.translate(this.x, this.y, this.z))
return new Point(tmp.m41, tmp.m42, tmp.m43)
}
function getTransformationMatrix(w) {
var transformationMatrix = I,
node = w
while (node) {
if (node._ignore_css_transforms) break
if (node._drag_widget) {
var transform = node._drag_widget.cssTransform || 'none',
c = transform === 'none' ? I : new WebKitCSSMatrix(transform)
transformationMatrix = c.multiply(transformationMatrix)
}
node = node.parentNode
}
transformationMatrix = I.multiply(transformationMatrix)
return transformationMatrix
}
module.export = function cssTransformCoords(node, x, y) {
return new Point(pageX, pageY, 0).transformBy(getTransformationMatrix(element).inverse())
}

View file

@ -1,4 +1,6 @@
const Touch = window.Touch || class Touch {}
// const cssTransformCoords = require('./transform-coords')
module.exports = {
@ -76,6 +78,12 @@ module.exports = {
}
// if (!event.traversing) {
// // css transfom fix (doesn't work well with traversing gestures)
// var transformedCoords = cssTransformCoords(event.firstTarget, event.movementX, event.movementY)
// event.movementX = transformedCoords.x
// event.movementY = transformedCoords.y
// }
return event
@ -88,6 +96,13 @@ module.exports = {
event.offsetX = event.pageX - off.left
event.offsetY = event.pageY - off.top
// if (!event.traversing) {
// // css transfom fix (doesn't work well with traversing gestures)
// var transformedCoords = cssTransformCoords(event.firstTarget, event.offsetX, event.offsetY)
// event.offsetX = transformedCoords.x
// event.offsetY = transformedCoords.y
// }
},
TRAVERSING_SAMEWIDGET: 1

View file

@ -6,7 +6,6 @@ var utils = require('../ui/utils'),
editor = require('../editor/'),
locales = require('../locales'),
{deepCopy} = require('../utils'),
sidepanel = require('../ui/sidepanel'),
UiModal = require('../ui/ui-modal'),
notifications = require('../ui/notifications'),
{TRAVERSING_SAMEWIDGET} = require('../events/utils'),
@ -168,8 +167,6 @@ module.exports = {
editor.historyState = data.historyState
if (data.editorEnabled) editor.enable()
if (data.sidepanelOpened) sidepanel.open()
if (data.traversing === TRAVERSING_SAMEWIDGET) {
DOM.get('.traversingSmart')[0].click()
} else if (data.traversing) {

View file

@ -33,6 +33,11 @@ module.exports = {
fs_unnavailable: 'Fullscreen not available',
fs_addtohome: 'You must add this page to your home screen to launch it in fullscreen',
// inspector
inspector_title: 'Inspector',
inspector_color_confirm: 'Confirm',
inspector_color_apply: 'Apply',
// remote save
remotesave_open: 'Open (on the server)',
remotesave_save: 'Save (on the server)',

View file

@ -32,6 +32,12 @@ module.exports = {
fs_unnavailable: 'Plein écran indisponible',
fs_addtohome: 'Vous devez ajouter cette page à votre écran d\'accueil pour pouvoir l\'ouvrir en plein écran.',
// inspector
inspector_title: 'Inspecteur',
inspector_color_confirm: 'Confirmer',
inspector_color_apply: 'Appliquer',
// remote save
remotesave_open: 'Ouvrir (sur le serveur)',
remotesave_save: 'Enregistrer (sur le serveur)',

View file

@ -32,6 +32,11 @@ module.exports = { // @https://github.com/suhr
fs_unnavailable: 'Полный экран не доступен',
fs_addtohome: 'Необходимо добавить эту страницу на главный экран чтобы запустить её в полноэкранном режиме',
// inspector
inspector_title: 'Inspector',
inspector_color_confirm: 'Confirm',
inspector_color_apply: 'Apply',
// remote save
remotesave_open: 'Open (on the server)',
remotesave_save: 'Save (on the server)',

View file

@ -240,7 +240,6 @@ var SessionManager = class SessionManager {
create() {
this.load({type: 'root'},function(){
require('../ui/sidepanel').open()
editor.enable()
editor.select(widgetManager.getWidgetById('root'))
})

View file

@ -0,0 +1,114 @@
var UiWidget = require('./ui-widget'),
Rgb = require('../widgets/pads/rgb'),
UiModal = require('./ui-modal'),
html = require('nanohtml'),
raw = require('nanohtml/raw'),
chroma = require('chroma-js'),
resize = require('../events/resize'),
locales = require('../locales')
class ColorPicker extends UiWidget {
constructor() {
super({})
this.name = ''
this.value = ''
this.rgb = new Rgb({props: {
...Rgb.defaults()._props(),
type: 'rgb',
label: false,
width: 250,
height: 200,
alphaStroke: 0,
snap: true
// height: '100%',
}, parent: this})
this.rgb.container.classList.add('not-editable')
this.modal = new UiModal({
closable: true,
title: '',
hide: true,
content: html`
<div class="color-picker-modal">
${this.rgb.container}
<div class="actions">
<div class="btn color-apply">${locales('inspector_color_apply')}</div>
<div class="btn color-confirm">${locales('inspector_color_confirm')}</div>
</div>
</div>
`,
enterKey: this.confirm.bind(this)
})
DOM.get(this.modal.container, '.color-confirm')[0].addEventListener('click', this.confirm.bind(this))
DOM.get(this.modal.container, '.color-apply')[0].addEventListener('click', this.apply.bind(this))
this.rgb.on('change', (e)=>{
e.stopPropagation = true
})
// this.rgb.on('dragend', (e)=>{
// this.value = chroma(this.rgb.value).hex()
// this.trigger('change')
// })
this.opened = 0
}
open() {
this.modal.open()
resize.check(this.rgb.container)
this.opened = 1
}
close() {
this.modal.close()
this.setName()
this.opened = 0
}
apply() {
this.value = chroma(this.rgb.value).hex()
this.trigger('change')
}
confirm() {
this.value = chroma(this.rgb.value).hex()
this.trigger('change')
this.close()
}
isVisible() {
return true
}
setName(n = '') {
this.name = n
}
setValue(v) {
this.rgb.setValue(chroma(v).rgb())
}
}
module.exports = ColorPicker

View file

@ -14,7 +14,6 @@ class UiDragResize extends UiWidget {
this.container = html`
<div id="drag-resize">
<div class="position-handle"></div>
<div class="helper"></div>
<div class="no-widget-select handle nw" data-action="left,top"></div>
<div class="no-widget-select handle e" data-action="width"></div>
@ -42,12 +41,13 @@ class UiDragResize extends UiWidget {
this.on('draginit', (event)=>{
var node = event.firstTarget
if (!this.handles.includes(node)) return
if (event.shiftKey || !this.handles.includes(node)) return
this.initLeft = this.left
this.initTop = this.top
this.initWidth = this.width
this.initHeight = this.height
this.matrix = ''
this.container.classList.add('dragging')
this.dragging = node.getAttribute('data-action').split(',')
@ -94,15 +94,7 @@ class UiDragResize extends UiWidget {
}, {element: this.container})
// handles appear on top of modal widgets
// very special case fix here...
this.watchedModalWidget = null
this.checkModalState = (e)=>{
if (e.widget !== this.watchedModalWidget) return
console.log(e.widget.value)
this.container.style.display = e.widget.value ? 'none' : ''
}
// this.handles[0]._ignore_css_transforms = true
}
@ -110,13 +102,6 @@ class UiDragResize extends UiWidget {
if (!this.mounted) return
if (this.watchedModalWidget) {
// reset modal widget fix
this.watchedModalWidget.off('change', this.checkModalState)
this.watchedModalWidget = null
this.container.style.display = ''
}
this.container.parentNode.removeChild(this.container)
this.mounted = false
@ -129,13 +114,6 @@ class UiDragResize extends UiWidget {
var widget = widgets[0]
if (widget instanceof Modal) {
// modal widget fix
this.watchedModalWidget = widget
widget.on('change', this.checkModalState)
if (widget.getValue()) this.container.style.display = 'none'
}
if (
widget instanceof Tab ||
widget instanceof Root ||
@ -153,8 +131,6 @@ class UiDragResize extends UiWidget {
this.handles[i].style.display = handlesVisibility[i] ? '' : 'none'
}
var parent = widget.parentNode,
lefts = widgets.map(w => w.container.offsetLeft),
tops = widgets.map(w => w.container.offsetTop),
@ -166,6 +142,9 @@ class UiDragResize extends UiWidget {
this.width = (Math.max(...rights) - Math.min(...lefts)) / PXSCALE
this.height = (Math.max(...bottoms) - Math.min(...tops)) / PXSCALE
// this.cssTransform = widget.cssTransform || 'none'
// this.cssTransformOrigin = widget.cssTransformOrigin
this.updateCss()
parent.appendChild(this.container)
@ -184,6 +163,9 @@ class UiDragResize extends UiWidget {
this.container.style.setProperty('--' + k, val + 'rem')
// this.container.style.transform = this.cssTransform
// this.container.style.transformOrigin = this.cssTransformOrigin
}
}

View file

@ -4,13 +4,15 @@ var locales = require('../locales'),
ipc = require('../ipc'),
doubleClick = require('../events/double-click'),
{icon} = require('./utils'),
UiModal = require('./ui-modal')
UiModal = require('./ui-modal'),
keyboardJS = require('keyboardjs')
module.exports = function UiFilebrowser(options, callback) {
var save = options.save,
saveInputFocus = undefined
saveInputFocus = undefined,
previousKbContext = keyboardJS.getContext()
var popup = new UiModal({
closable: true,
@ -192,6 +194,7 @@ module.exports = function UiFilebrowser(options, callback) {
ipc.off('listDir', null, popup)
document.removeEventListener('keydown', keyDownHandler)
document.removeEventListener('keyup', keyUpHandler)
keyboardJS.setContext(previousKbContext)
})
browser.appendChild(ariane)
@ -202,5 +205,6 @@ module.exports = function UiFilebrowser(options, callback) {
ipc.send('listDir', {extension: options.extension, path: options.directory ? [options.directory] : null})
popup.open()
keyboardJS.setContext('filebrowser')
}

View file

@ -101,7 +101,7 @@ class UiInspectorField extends UiWidget {
val = chroma(style.getPropertyValue('--' + this.name.replace(/([A-Z])/g, (a,s)=>'-' + s.toLowerCase())).trim()).hex('rgb')
} catch(e) {console.log(e)}
let picker = html`<input class="osc-inspector-color" type="color" name="${this.name}" value="${val}"/>`
let picker = html`<osc-inspector-color type="text" name="${this.name}" value="${val}" style="--color-picker-value: ${val}"></osc-inspector-color>`
this.container.appendChild(picker)

View file

@ -1,10 +1,14 @@
var UiWidget = require('./ui-widget'),
UiInspectorField = require('./ui-inspector-field'),
UiColorPicker = require('./ui-colorpicker'),
morph = require('nanomorph'),
{widgets, defaults} = require('../widgets/'),
{deepCopy} = require('../utils'),
html = require('nanohtml')
class UiInspector extends UiWidget {
constructor(options) {
@ -31,6 +35,15 @@ class UiInspector extends UiWidget {
})
this.colorPicker = new UiColorPicker()
this.colorPicker.on('change', ()=>{
var textarea = DOM.get(this.container, `textarea[name="${this.colorPicker.name}"]`)
if (textarea) {
textarea[0].value = this.colorPicker.value
DOM.dispatchEvent(textarea[0], 'change')
}
})
this.container.addEventListener('click', (e)=>{
var node = e.target
@ -45,6 +58,12 @@ class UiInspector extends UiWidget {
DOM.dispatchEvent(textarea[0], 'change')
}
} else if (node.tagName === 'OSC-INSPECTOR-COLOR') {
this.colorPicker.setName(node.getAttribute('name'))
this.colorPicker.setValue(node.getAttribute('value'))
this.colorPicker.open()
}
})
@ -67,6 +86,7 @@ class UiInspector extends UiWidget {
this.container.innerHTML = ''
this.mounted = false
this.clearTimeout = null
this.colorPicker.close()
})
}
@ -124,11 +144,6 @@ class UiInspector extends UiWidget {
} else {
// if (widget.parent !== widgetManager && widget.parent.getProp('type') === 'strip') {
// // special case for widgets in strips
// if (propName === 'top' || propName === 'left' || propName === (widget.parent.getProp('horizontal') ? 'height' : 'width')) continue
// }
field = new UiInspectorField({
parent: this,
widget: widget,
@ -137,6 +152,7 @@ class UiInspector extends UiWidget {
default: defaults[w.props.type][propName],
tabIndex: tabIndex++
}).container
if (!field) continue
}
@ -146,6 +162,7 @@ class UiInspector extends UiWidget {
content.appendChild(field)
}
}
if (category) content.appendChild(category)
@ -159,6 +176,17 @@ class UiInspector extends UiWidget {
this.mounted = true
if (this.colorPicker.opened) {
var picker = DOM.get(this.container, `osc-inspector-color[name="${this.colorPicker.name}"]`)
if (picker) {
this.colorPicker.setValue(picker[0].getAttribute('value'))
} else {
this.colorPicker.close()
}
}
}
onChange(event) {

View file

@ -2,8 +2,9 @@ var UiWidget = require('./ui-widget'),
{icon} = require('./utils'),
html = require('nanohtml'),
raw = require('nanohtml/raw'),
SINGLETON = null,
CONTAINER = null
MODAL_SINGLETON = null,
MODAL_CONTAINER = null,
OPENED_MODALS = 0
class UiModal extends UiWidget {
@ -11,12 +12,12 @@ class UiModal extends UiWidget {
super(options)
if (!CONTAINER) CONTAINER = DOM.get('osc-modal-container')[0]
if (!MODAL_CONTAINER) MODAL_CONTAINER = DOM.get('osc-modal-container')[0]
if (options.closable) {
if (SINGLETON) SINGLETON.close()
SINGLETON = this
if (MODAL_SINGLETON) MODAL_SINGLETON.close()
MODAL_SINGLETON = this
}
@ -77,7 +78,11 @@ class UiModal extends UiWidget {
if (this.escKey) document.removeEventListener('keydown', this.escKeyHandler)
if (this.enterKey) document.removeEventListener('keydown', this.enterKeyHandler)
CONTAINER.removeChild(this.container)
if (--OPENED_MODALS === 0) MODAL_CONTAINER.classList.remove('backdrop')
MODAL_CONTAINER.removeChild(this.container)
this.trigger('close')
@ -90,7 +95,10 @@ class UiModal extends UiWidget {
if (this.escKey) document.addEventListener('keydown', this.escKeyHandler)
if (this.enterKey) document.addEventListener('keydown', this.enterKeyHandler)
CONTAINER.appendChild(this.container)
if (++OPENED_MODALS === 1) MODAL_CONTAINER.classList.add('backdrop')
MODAL_CONTAINER.appendChild(this.container)
this.trigger('open')
}

View file

@ -62,6 +62,10 @@ class UiTree extends UiWidget {
})
}
widgetManager.on('prop-changed', (e)=>{
console.log(e)
})
}
clear() {

View file

@ -1,64 +1,6 @@
var Widget = require('./widget'),
resize = require('../../events/resize')
class CanvasQueue {
constructor() {
this.queue = {}
this.flushed = 0
this.running = false
this.frameLength = 1000 / CANVAS_FRAMERATE
this.lastFrame = 0
this.bindedLoop = this.loop.bind(this)
}
push(widget) {
this.queue[widget.hash] = widget
this.flushed = 0
if (!this.running) {
this.running = true
requestAnimationFrame(this.bindedLoop)
}
}
flush() {
for (var h in this.queue) {
this.queue[h].draw()
}
this.queue = {}
this.flushed++
}
loop(timestamp) {
if (this.flushed >= 10) {
this.running = false
return
}
requestAnimationFrame(this.bindedLoop)
var delta = timestamp - this.lastFrame
if (delta > this.frameLength) {
this.lastFrame = timestamp - (delta % this.frameLength)
this.flush()
}
}
}
var canvasQueue = new CanvasQueue()
resize = require('../../events/resize'),
canvasQueue = require('./queue')
class Canvas extends Widget {
@ -82,7 +24,7 @@ class Canvas extends Widget {
this.clearRect = []
this.visible = false
this.hasSize = false
this.colors = {}
this.cssVars = {}
@ -123,7 +65,10 @@ class Canvas extends Widget {
this.cacheCanvasStyle(style)
if (!this.visible) this.visible = true
if (!this.hasSize) {
this.hasSize = true
this.setVisibility()
}
this.batchDraw()
@ -172,11 +117,15 @@ class Canvas extends Widget {
}
isVisible() {
return this.hasSize && super.isVisible()
}
batchDraw() {
if (this.visible) {
canvasQueue.push(this)
}
if (this.visible) canvasQueue.push(this)
}

View file

@ -0,0 +1,62 @@
var EventEmitter = require('../../events/event-emitter')
class CanvasQueue extends EventEmitter{
constructor() {
super()
this.queue = {}
this.flushed = 0
this.running = false
this.frameLength = 1000 / CANVAS_FRAMERATE
this.lastFrame = 0
this.bindedLoop = this.loop.bind(this)
}
push(widget) {
this.queue[widget.hash] = widget
this.flushed = 0
if (!this.running) {
this.running = true
requestAnimationFrame(this.bindedLoop)
}
}
flush() {
for (var h in this.queue) {
this.queue[h].draw()
}
this.queue = {}
this.flushed++
}
loop(timestamp) {
if (this.flushed >= 10) {
this.running = false
return
}
requestAnimationFrame(this.bindedLoop)
var delta = timestamp - this.lastFrame
if (delta > this.frameLength) {
this.lastFrame = timestamp - (delta % this.frameLength)
this.trigger('frame', {timestamp})
this.flush()
}
}
}
module.exports = new CanvasQueue()

View file

@ -168,6 +168,7 @@ class Widget extends EventEmitter {
this.reCreateOptions = options.reCreateOptions
this.removed = false
this.mounted = false
this.visible = true
this.createPropsCache()
@ -197,6 +198,7 @@ class Widget extends EventEmitter {
this.setContainerStyles()
this.setCssVariables()
this.setVisibility()
}
@ -758,7 +760,7 @@ class Widget extends EventEmitter {
return
case 'visible':
this.setContainerStyles(['visibility'])
this.setVisibility()
resize.check(this.container)
return
@ -775,7 +777,6 @@ class Widget extends EventEmitter {
case 'css':
this.setContainerStyles(['css'])
this.onPropChanged('color')
var re = /width|height|display/
if (re.test(oldPropValue) || re.test(this.getProp('css'))) {
resize.check(this.container)
@ -821,10 +822,6 @@ class Widget extends EventEmitter {
setContainerStyles(styles = ['geometry', 'label', 'css', 'visibility']) {
if (styles.includes('visibility')) {
this.container.style.display = this.getProp('visible') ? '' : 'none'
}
if (styles.includes('geometry')) {
// geometry
@ -941,6 +938,19 @@ class Widget extends EventEmitter {
this.extraCssClasses = extraCssClasses
}
// store transform matrix if any
// if (css.includes('transform')) {
// setTimeout(()=>{
// var style = window.getComputedStyle(this.container)
// this.cssTransform = style.transform || 'none'
// this.cssTransformOrigin = style.transformOrigin
// })
// } else {
// this.cssTransform = 'none'
// }
}
return style
@ -960,6 +970,28 @@ class Widget extends EventEmitter {
}
isVisible() {
return this.getProp('visible') && this.parent.isVisible()
}
setVisibility() {
var visible = this.isVisible()
if (visible !== this.visible) {
this.visible = visible
this.container.style.display = this.getProp('visible') ? '' : 'none'
for (var c of this.children) {
c.setVisibility()
}
}
}
reCreateWidget(options={}){
if (!this.removed) return updateWidget(this, options)

View file

@ -28,6 +28,7 @@ class Matrix extends StaticProperties(Panel, {scroll: false}) {
quantity: {type: 'number', value: 4, help: 'Defines the number of widgets in the matrix'},
start: {type: 'integer', value: 0, help: 'First widget\'s index'},
traversing: {type: 'boolean', value: true, help: 'Set to `false` to disable traversing gestures'},
restrainModals: {type: 'boolean', value: true, help: 'Set to `false` to let modal widgets ignore this container\'s boundaries.'},
props: {type: 'object', value: {}, help: [
'Defines a set of property to override the widgets\' defaults.',
'Formulas in this field are resolved with an extra variable representing each widget\'s index: `$`',
@ -40,7 +41,7 @@ class Matrix extends StaticProperties(Panel, {scroll: false}) {
constructor(options) {
super({...options, html: html`<div class="panel"></div>`})
super(options)
this.value = []

View file

@ -28,6 +28,7 @@ class Modal extends Panel {
popupTop: {type: 'number|percentage', value: 'auto', help: 'Modal popup\'s position'},
layout: {type: 'string', value: 'default', choices: ['default', 'vertical', 'horizontal', 'grid'], help:''},
gridTemplate: {type: 'string|number', value: '', help:'If `layout` is `grid`, can be either a number of columns of a value css grid-template definition.'},
traversing: {type: 'boolean', value: false, help: 'Set to `true` to enable traversing gestures in this widget. Set to `smart` or `auto` to limit affected widgets by the type of the first touched widget'},
scroll: {type: 'boolean', value: true, help: 'Set to `false` to disable scrollbars'},
variables: {type: '*', value: '@{parent.variables}', help: 'Defines one or more arbitrary variables that can be inherited by children widgets'},
@ -47,7 +48,7 @@ class Modal extends Panel {
super(options)
this.popup = html`
<div class="popup">
<div class="popup backdrop">
<div class="popup-wrapper">
<div class="popup-title closable"><span class="popup-label"></span><span class="closer">${raw(icon('times'))}</span></div>
<div class="popup-content widget panel-container not-editable contains-widgets"></div>
@ -77,14 +78,14 @@ class Modal extends Panel {
this.popup.classList.add('y-positionned')
}
this.light = this.container.appendChild(html`<div class="toggle"></div>`)
this.toggle = this.container.appendChild(html`<div class="toggle"></div>`)
if (this.getProp('doubleTap')) {
doubletab(this.light, ()=>{
doubletab(this.toggle, ()=>{
this.setValue(1, {sync:true, send:true})
})
} else {
this.light.addEventListener('fast-click',(e)=>{
this.toggle.addEventListener('fast-click',(e)=>{
if (e.capturedByEditor === true) return
e.detail.preventOriginalEvent = true
this.setValue(1, {sync:true, send:true})
@ -113,6 +114,13 @@ class Modal extends Panel {
}
isVisible() {
return this.value && super.isVisible()
}
setValue(v, options={}) {
@ -132,7 +140,7 @@ class Modal extends Panel {
this.popup.classList.toggle('show', this.value)
this.container.classList.toggle('on', this.value)
// this.setVisibility()
this.bindEscKey(this.value)

View file

@ -22,8 +22,8 @@ class Panel extends Container() {
layout: {type: 'string', value: 'default', choices: ['default', 'vertical', 'horizontal', 'grid'], help:''},
gridTemplate: {type: 'string|number', value: '', help:'If `layout` is `grid`, can be either a number of columns of a value css grid-template definition.'},
scroll: {type: 'boolean', value: true, help: 'Set to `false` to disable scrollbars'},
traversing: {type: 'boolean', value: false, help: 'Set to `true` to enable traversing gestures in this widget. Set to `smart` or `auto` to limit affected widgets by the type of the first touched widget'},
scroll: {type: 'boolean', value: true, help: 'Set to `false` to disable scrollbars'},
variables: {type: '*', value: '@{parent.variables}', help: 'Defines one or more arbitrary variables that can be inherited by children widgets'},
}, [], {
@ -44,9 +44,9 @@ class Panel extends Container() {
constructor(options) {
super({...options, html: html`<div class="panel"></div>`})
super({...options, html: html`<inner></inner>`})
this.container.classList.toggle('noscroll', this.getProp('scroll') === false)
this.container.classList.toggle('no-scroll', this.getProp('scroll') === false)
this.container.classList.add('layout-' + this.getProp('layout'))
if (this.getProp('layout') === 'grid') {
@ -157,7 +157,7 @@ class Panel extends Container() {
this.tabs.push(widget)
this.navigation.appendChild(html`
<div class="tablink" data-widget="${widget.hash}" style="${style}">
${widget.label.innerHTML}
${raw(widget.label.innerHTML)}
</div>
`)

View file

@ -66,6 +66,12 @@ class Root extends StaticProperties(Panel, {scroll: false, label: false, id: 'ro
}
isVisible() {
return true
}
}

View file

@ -19,8 +19,8 @@ module.exports = class Tab extends Panel {
// detached: {type: 'boolean', value: true, help: 'Set to `false` if the tab contains `frame` widgets that should not be reloaded when the tab opens'},
layout: {type: 'string', value: 'default', choices: ['default', 'vertical', 'horizontal', 'grid'], help:''},
gridTemplate: {type: 'string|number', value: '', help:'If `layout` is `grid`, can be either a number of columns of a value css grid-template definition.'},
scroll: {type: 'boolean', value: true, help: 'Set to `false` to disable scrollbars'},
traversing: {type: 'boolean', value: false, help: 'Set to `true` to enable traversing gestures in this widget. Set to `smart` or `auto` to limit affected widgets by the type of the first touched widget'},
scroll: {type: 'boolean', value: true, help: 'Set to `false` to disable scrollbars'},
variables: {type: '*', value: '@{parent.variables}', help: 'Defines one or more arbitrary variables that can be inherited by children widgets'},
}, [
@ -55,16 +55,24 @@ module.exports = class Tab extends Panel {
if (this.getProp('detached')) this.container.removeChild(this.widget)
this.container.classList.remove('show')
this.detached = true
this.setVisibility()
}
show() {
if (!this.detached) return
if (this.getProp('detached')) this.container.appendChild(this.widget)
this.container.classList.add('show')
this.detached = false
this.setVisibility()
resize.check(this.widget, true)
}
isVisible() {
return !this.detached && super.isVisible()
}
onPropChanged(propName, options, oldPropValue) {
if (super.onPropChanged(...arguments)) return true

View file

@ -4,7 +4,7 @@ var {clip} = require('../utils'),
StaticProperties = require('../mixins/static_properties')
module.exports = class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22050}}) {
module.exports = class Eq extends StaticProperties(Plot, {rangeX: {min: 20, max: 22000}}) {
static description() {

View file

@ -56,8 +56,8 @@ module.exports = class Plot extends StaticProperties(Canvas, {bypass: true, inte
this.logScaleY = this.getProp('logScaleY')
this.pips = {
x : {
min: Math.abs(this.rangeX.min)>=1000?this.rangeY.min/1000+'k':this.rangeX.min,
max: Math.abs(this.rangeX.max)>=1000?this.rangeY.max/1000+'k':this.rangeX.max
min: Math.abs(this.rangeX.min)>=1000?this.rangeX.min/1000+'k':this.rangeX.min,
max: Math.abs(this.rangeX.max)>=1000?this.rangeX.max/1000+'k':this.rangeX.max
},
y : {
min: Math.abs(this.rangeY.min)>=1000?this.rangeY.min/1000+'k':this.rangeY.min,

View file

@ -1,7 +1,8 @@
var {clip} = require('../utils'),
Widget = require('../common/widget'),
Plot = require('./plot'),
StaticProperties = require('../mixins/static_properties')
StaticProperties = require('../mixins/static_properties'),
canvasQueue = require('../common/queue')
module.exports = class Visualizer extends StaticProperties(Plot, {rangeX: {min: '', max: ''}, dots: false, smooth: false, interaction: false}) {
@ -42,6 +43,8 @@ module.exports = class Visualizer extends StaticProperties(Plot, {rangeX: {min:
this.watchDuration = 1000 * this.getProp('duration')
this.ticks = 0
this.bindedLoop = this.loop.bind(this)
}
startLoop() {
@ -49,9 +52,9 @@ module.exports = class Visualizer extends StaticProperties(Plot, {rangeX: {min:
this.clock = Date.now()
if (!this.looping) {
this.lastUpdate = Date.now()
canvasQueue.on('frame', this.bindedLoop)
this.looping = true
this.ticks = 0
this.loop()
}
}
@ -59,11 +62,12 @@ module.exports = class Visualizer extends StaticProperties(Plot, {rangeX: {min:
var t = Date.now()
if (t -this.clock >= this.watchDuration) {
if (t - this.clock >= this.watchDuration) {
canvasQueue.off('frame', this.bindedLoop)
this.looping = false
}
this.ticks += (t - this.lastUpdate) / (1000/this.fps)
this.ticks += (t - this.lastUpdate) / (1000 / this.fps)
if (Math.floor(this.ticks) > 0) {
this.shiftData(Math.floor(this.ticks))
@ -73,17 +77,11 @@ module.exports = class Visualizer extends StaticProperties(Plot, {rangeX: {min:
this.lastUpdate = t
if (!this.looping) return
setTimeout(()=>{
this.loop()
}, (1000/this.fps))
}
shiftData(n) {
for (var i=0; i<n; i++) {
for (var i = 0; i < n; i++) {
this.value.push(this.value[this.length - 1])
this.value.splice(0,1)
}
@ -98,17 +96,18 @@ module.exports = class Visualizer extends StaticProperties(Plot, {rangeX: {min:
setValue(v, options={}) {
if (Array.isArray(v) && v.length == this.length) {
this.value = v
if (typeof(v) === 'number') {
this.value[this.length - 1] = v
this.startLoop()
if (options.sync) this.changed(options)
} else if (typeof(v) == 'number'){
} else if (Array.isArray(v) && v.length === this.length) {
this.value[this.length - 1] = v
this.value = v
this.startLoop()
if (options.sync) this.changed(options)
@ -118,5 +117,14 @@ module.exports = class Visualizer extends StaticProperties(Plot, {rangeX: {min:
}
onRemove() {
if (this.looping) {
canvasQueue.off('frame', this.bindedLoop)
}
super.onRemove()
}
}

View file

@ -91,7 +91,7 @@ module.exports = class Keyboard extends Panel {
data.address = '@{parent.address}'
data.preArgs = `JS{{
var a = @{parent.preArgs};
var b = typeof a === 'string' and a === '' ? [] : Array.isArray(a) ? a : [a];
var b = typeof a === 'string' && a === '' ? [] : Array.isArray(a) ? a : [a];
return b.concat([${i}])
}}`

View file

@ -248,12 +248,14 @@ module.exports = class MultiXy extends Pad {
switch (propName) {
case 'colorText':
case 'colorWidget':
case 'colorFill':
case 'colorStroke':
case 'alphaStroke':
case 'alphaFillOff':
case 'alphaFillOn':
case 'padding':
for (var w of this.pads) {
w.onPropChanged(propName)
}

View file

@ -1,14 +1,14 @@
var Pad = require('./pad'),
var Widget = require('../common/widget'),
Xy = require('./xy'),
Fader = require('./_fake_fader'),
Fader = require('../sliders/fader'),
{clip, mapToScale, hsbToRgb, rgbToHsb} = require('../utils'),
Input = require('../inputs/input'),
html = require('nanohtml')
var faderDefaults = Fader.defaults()._props(),
xyDefaults = Xy.defaults()._props()
module.exports = class Rgb extends Pad {
module.exports = class Rgb extends Widget {
static description() {
@ -28,8 +28,8 @@ module.exports = class Rgb extends Pad {
]},
spring: {type: 'boolean', value: false, help: 'When set to `true`, the widget will go back to its default value when released'},
range: {type: 'object', value: {min: 0, max: 255}, help: 'Defines the widget\'s output scale.'},
input: {type: 'boolean', value: true, help: 'Set to `false` to hide the built-in input widget'},
alpha:{type: 'boolean', value: false, help: 'Set to `true` to enable alpha channel'}
alpha:{type: 'boolean', value: false, help: 'Set to `true` to enable alpha channel'},
rangeAlpha: {type: 'object', value: {min: 0, max: 1}, help: 'Defines the widget\'s output scale for the alpha channel.'},
}, ['color'], {
@ -39,82 +39,81 @@ module.exports = class Rgb extends Pad {
constructor(options) {
options.props.pointSize = 10
super({...options, html: html`<inner></inner>`})
super(options)
this.hueWrapper = this.widget.appendChild(html`<div class="hue-wrapper"></div>`)
this.hue = new Fader({props:{
...faderDefaults,
id:'h',
compact:true,
pips:false,
horizontal:true,
snap:this.getProp('snap'),
range:{min:0,max:360},
input:false,
precision:2
}, cancelDraw: false, parent: this})
this.hue.sendValue = ()=>{}
this.hueWrapper.appendChild(this.hue.widget)
this.hue.on('change',(e)=>{
e.stopPropagation = true
this.dragHandle(true)
})
this.container.classList.toggle('contains-alpha', this.getProp('alpha'))
this.pad = new Xy({props:{
...xyDefaults,
type: 'xy',
label: false,
snap:this.getProp('snap'),
spring:this.getProp('spring'),
rangeX:{min:0,max:100},
rangeY:{min:0,max:100},
precision:2,
pointSize: this.getProp('pointSize'),
pointSize: 10,
pips: false,
input:false
}, parent: this})
this.pad.sendValue = ()=>{}
this.wrapper.appendChild(this.pad.widget)
this.pad.container.classList.add('not-editable', 'pad')
this.widget.appendChild(this.pad.container)
this.pad.on('change',(e)=>{
e.stopPropagation = true
this.dragHandle()
})
this.hue = new HueFader({props:{
...faderDefaults,
type: 'fader',
mode: 'line',
label: false,
pips:false,
horizontal:false,
snap:this.getProp('snap'),
range:{min:0,max:360},
precision:2
}, parent: this})
this.hue.container.classList.add('not-editable', 'hue')
this.widget.appendChild(this.hue.container)
this.hue.on('change',(e)=>{
e.stopPropagation = true
this.dragHandle(true)
})
if (this.getProp('alpha')) {
this.alphaWrapper = this.widget.appendChild(html`<div class="alpha-wrapper"></div>`)
this.alpha = new Fader({props:{
...faderDefaults,
id:'a',
compact:true,
type: 'fader',
label:false,
pips:false,
horizontal:true,
snap:this.getProp('snap'),
range:{min:0,max:255},
range: this.getProp('rangeAlpha'),
default: 255,
input:false,
precision:2
}, cancelDraw: false, parent: this})
this.alpha.sendValue = ()=>{}
this.alphaWrapper.appendChild(this.alpha.widget)
}, parent: this})
this.alpha.container.classList.add('not-editable', 'alpha')
this.widget.appendChild(this.alpha.container)
this.alpha.on('change',(e)=>{
e.stopPropagation = true
this.dragAlphaHandle()
})
}
if (this.getProp('input')) {
var initValue = Array.isArray(this.getProp('default')) && this.getProp('default').length >= 3 ? this.getProp('default') : Array(3).fill(this.getProp('range').min)
if (this.getProp('alpha') && initValue.length < 4) {
initValue[3] = this.getProp('rangeAlpha').min
}
if (this.getProp('spring')) {
this.on('dragend', (e)=>{
this.input = new Input({
props:{...Input.defaults()._props(), precision:this.getProp('precision'), unit:this.getProp('unit')},
parent:this, parentNode:this.widget
})
this.widget.appendChild(this.input.widget)
this.input.on('change', (e)=>{
e.stopPropagation = true
this.setValue(this.input.getValue(), {sync:true, send:true})
this.showValue()
})
this.setValue(initValue, {sync:true,send:true})
}, {element: this.widget, ignoreCustomBindings: true})
}
@ -123,7 +122,7 @@ module.exports = class Rgb extends Pad {
this.hsb = {h:0,s:0,b:0}
this.setValue([this.getProp('range').min, this.getProp('range').min, this.getProp('range').min])
this.setValue(initValue)
}
@ -178,11 +177,14 @@ module.exports = class Rgb extends Pad {
if (this.touched && !options.dragged) return this.setValueTouchedQueue = [v, options]
var value = Array(this.getProp('alpha') ? 4 : 3)
for (var i = 0; i < value.length; i++) {
for (var i = 0; i < 3; i++) {
value[i] = clip(v[i], [this.getProp('range').min, this.getProp('range').max])
}
if (value.length === 4 && value[3] === undefined) value[3] = this.alpha.value
if (value.length === 4) {
value[3] = v[3] === undefined ? this.alpha.value : clip(v[3], [this.getProp('rangeAlpha').min, this.getProp('rangeAlpha').max])
}
var hsb = rgbToHsb({
r: value[0],
@ -211,32 +213,12 @@ module.exports = class Rgb extends Pad {
if (!options.nohue && !options.dragged) {
var hue = hsbToRgb({h:this.hsb.h,s:100,b:100}),
hueStr = `rgb(${Math.round(hue.r)},${Math.round(hue.g)},${Math.round(hue.b)})`
this.canvas.setAttribute('style',`background-color:${hueStr}`)
this.pad.container.setAttribute('style',`background-color:${hueStr}`)
this.hue.setValue(this.hsb.h, {sync: false, send:false, dragged:false})
}
this.showValue()
}
draw() {}
showValue() {
if (this.getProp('input')) this.input.setValue(this.value)
}
getSplit() {
return this.getProp('split')?
typeof this.getProp('split') == 'object' && this.getProp('split').length == 3 ?
this.getProp('split')
: [this.getProp('address') + '/r', this.getProp('address') + '/g', this.getProp('address') + '/b']
: false
}
onPropChanged(propName, options, oldPropValue) {
@ -244,24 +226,83 @@ module.exports = class Rgb extends Pad {
switch (propName) {
case 'color':
case 'colorText':
case 'colorWidget':
case 'colorFill':
case 'colorStroke':
case 'alphaStroke':
case 'alphaFillOff':
case 'alphaFillOn':
for (var w of [this.hue, this.pad]) {
w.onPropChanged('color')
w.onPropChanged(propName)
}
if (this.input) this.input.onPropChanged('color')
if (this.alpha) this.alpha.onPropChanged('color')
if (this.alpha) this.alpha.onPropChanged(propName)
return
}
}
setVisibility() {
super.setVisibility()
for (var w of [this.hue, this.pad]) {
if (w) w.setVisibility()
}
if (this.alpha) this.alpha.setVisibility()
}
onRemove() {
this.hue.onRemove()
this.pad.onRemove()
if (this.input) this.input.onRemove()
if (this.alpha) this.alpha.onRemove()
super.onRemove()
}
}
class HueFader extends Fader {
draw() {
var width = this.getProp('horizontal') ? this.height : this.width,
height = !this.getProp('horizontal') ? this.height : this.width
var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent,
d = Math.round(this.percentToCoord(percent)),
o = Math.round(this.percentToCoord(this.valueToPercent(this.originValue))),
m = this.getProp('horizontal') ? this.height / 2 : this.width / 2,
dashed = this.getProp('dashed'),
flat = this.getProp('mode') === 'flat'
// sharp border trick
if (width % 2 && parseInt(m) === m) m -= 0.5
this.clear()
this.ctx.strokeStyle = this.gaugeGradient || this.cssVars.colorFill
if (flat) {
this.ctx.lineWidth = Math.round(width - this.gaugePadding * 2)
} else {
this.ctx.lineWidth = 2 * PXSCALE
}
// flat knob
this.ctx.globalAlpha = 1
this.ctx.fillStyle = this.cssVars.colorFill
this.ctx.beginPath()
this.ctx.rect(this.gaugePadding, Math.min(d, height - 2 * PXSCALE), width - this.gaugePadding * 2, 2 * PXSCALE)
this.ctx.fill()
this.clearRect = [0, 0, width, height]
}
}

View file

@ -1,17 +1,10 @@
var Pad = require('./pad'),
Fader = require('./_fake_fader'),
Fader = require('../sliders/fader'),
doubletab = require('../mixins/double_tap'),
touchstate = require('../mixins/touch_state')
var faderDefaults = Fader.defaults()._props()
class XyFader extends Fader {
draw(){}
batchDraw(){}
}
module.exports = class Xy extends Pad {
static description() {
@ -56,9 +49,10 @@ module.exports = class Xy extends Pad {
super(options)
this.faders = {
x: new XyFader({props:{
x: new Fader({props:{
...faderDefaults,
id:0,
visible:false,
horizontal:true,
default:this.getProp('default').length === 2 ? this.getProp('default')[0] : '',
snap:this.getProp('snap'),
@ -67,9 +61,10 @@ module.exports = class Xy extends Pad {
logScale:this.getProp('logScaleX'),
sensitivity: this.getProp('sensitivity'),
}, parent: this}),
y: new XyFader({props:{
y: new Fader({props:{
...faderDefaults,
id:1,
visible:false,
horizontal:false,
default:this.getProp('default').length === 2 ? this.getProp('default')[1] : '',
snap:this.getProp('snap'),
@ -89,10 +84,9 @@ module.exports = class Xy extends Pad {
this.active = false
this.on('draginit',(e)=>{
e.stopPropagation = true
this.active = true
this.faders.x.trigger('draginit', e)
this.faders.y.trigger('draginit', e)
this.faders.x.trigger('draginit', {...e, stopPropagation: true})
this.faders.y.trigger('draginit', {...e, stopPropagation: true})
this.dragHandle()
}, {element: this.canvas})
@ -103,10 +97,9 @@ module.exports = class Xy extends Pad {
}, {element: this.canvas})
this.on('dragend', (e)=>{
e.stopPropagation = true
this.active = false
this.faders.x.trigger('dragend', e)
this.faders.y.trigger('dragend', e)
this.faders.x.trigger('dragend', {...e, stopPropagation: true})
this.faders.y.trigger('dragend', {...e, stopPropagation: true})
if (this.getProp('spring')) {
this.setValue([this.faders.x.getSpringValue(),this.faders.y.getSpringValue()],{sync:true,send:true})
} else {
@ -261,8 +254,9 @@ module.exports = class Xy extends Pad {
}
this.ctx.globalAlpha = this.cssVars.alphaStroke
this.ctx.globalAlpha = this.cssVars.alphaFillOn
this.ctx.stroke()
}
if (!pipsDrawn) this.clearRect = [x - this.pointSize - 2 * PXSCALE, y - this.pointSize - 2 * PXSCALE, (this.pointSize + PXSCALE) * 4, (this.pointSize + PXSCALE) * 4]

View file

@ -17,6 +17,7 @@ module.exports = class Fader extends Slider {
_fader:'fader',
mode: {type: 'string', value: 'flat', choices: ['flat', 'line'], help: 'Design style'},
horizontal: {type: 'boolean', value: false, help: 'Set to `true` to display the fader horizontally'},
alignRight: {type: 'boolean', value: false, help: 'Set to `true` to invert the pip\'s and fader\'s position'},
pips: {type: 'boolean', value: true, help: 'Set to `false` to hide the scale'},
@ -62,17 +63,9 @@ module.exports = class Fader extends Slider {
super(options)
this.widget.classList.add('fader')
this.gaugeGradient = null
if (this.getProp('horizontal')) {
this.widget.classList.add('horizontal')
this.container.classList.add('horizontal')
}
if (this.getProp('alignRight') && !this.getProp('horizontal')) {
this.widget.classList.add('align-right')
}
this.container.classList.toggle('alt', this.getProp('mode') !== 'flat')
if (false && this.getProp('pips')) {
@ -205,28 +198,41 @@ module.exports = class Fader extends Slider {
var percent = this.getProp('steps') ? this.valueToPercent(this.value) : this.percent,
d = Math.round(this.percentToCoord(percent)),
o = Math.round(this.percentToCoord(this.valueToPercent(this.originValue))),
m = this.getProp('horizontal') ? this.height / 2 : this.width / 2
m = this.getProp('horizontal') ? this.height / 2 : this.width / 2,
dashed = this.getProp('dashed'),
flat = this.getProp('mode') === 'flat'
// sharp border trick
if (width % 2 && parseInt(m) === m) m -= 0.5
var dashed = this.getProp('dashed')
this.clear()
this.ctx.strokeStyle = this.gaugeGradient || this.cssVars.colorFill
this.ctx.lineWidth = Math.round(width - this.gaugePadding * 2)
if (flat) {
this.ctx.lineWidth = Math.round(width - this.gaugePadding * 2)
} else {
this.ctx.lineWidth = 2 * PXSCALE
}
if (dashed) this.ctx.setLineDash([PXSCALE, PXSCALE])
if (this.cssVars.alphaFillOff) {
// gaugo off
this.ctx.globalAlpha = this.cssVars.alphaFillOff
this.ctx.beginPath()
this.ctx.moveTo(m, height - this.gaugePadding)
this.ctx.lineTo(m, this.gaugePadding)
this.ctx.stroke()
}
if (this.cssVars.alphaFillOn) {
// gaugo on
this.ctx.globalAlpha = this.cssVars.alphaFillOn
this.ctx.beginPath()
this.ctx.moveTo(m, o)
@ -236,27 +242,57 @@ module.exports = class Fader extends Slider {
if (dashed) this.ctx.setLineDash([])
this.ctx.globalAlpha = this.cssVars.alphaStroke
this.ctx.strokeStyle = this.cssVars.colorStroke
if (flat) {
this.ctx.beginPath()
this.ctx.moveTo(0, 0)
this.ctx.lineTo(width, 0)
this.ctx.lineTo(width, height)
this.ctx.lineTo(0, height)
this.ctx.closePath()
this.ctx.lineWidth = 2 * PXSCALE
this.ctx.stroke()
// stroke
this.ctx.globalAlpha = this.cssVars.alphaStroke
this.ctx.strokeStyle = this.cssVars.colorStroke
this.ctx.beginPath()
this.ctx.moveTo(0, 0)
this.ctx.lineTo(width, 0)
this.ctx.lineTo(width, height)
this.ctx.lineTo(0, height)
this.ctx.closePath()
this.ctx.lineWidth = 2 * PXSCALE
this.ctx.stroke()
this.ctx.globalAlpha = 1
this.ctx.fillStyle = this.cssVars.colorFill
// flat knob
this.ctx.globalAlpha = 1
this.ctx.fillStyle = this.cssVars.colorFill
this.ctx.beginPath()
this.ctx.rect(this.gaugePadding, Math.min(d, height - 3 * PXSCALE), width - this.gaugePadding * 2, PXSCALE)
this.ctx.fill()
this.clearRect = [0, 0, width, height]
} else {
this.ctx.globalAlpha = 1
this.ctx.fillStyle = this.cssVars.colorFill
this.ctx.beginPath()
this.ctx.arc(m, d, 3 * PXSCALE, 0, 2 * Math.PI)
this.ctx.fill()
this.ctx.globalAlpha = 0.25
this.ctx.fillStyle = this.cssVars.colorFill
this.ctx.beginPath()
this.ctx.arc(m, d, 10 * PXSCALE, 0, 2 * Math.PI)
this.ctx.fill()
this.clearRect = [m - 10 * PXSCALE, this.gaugePadding - 10 * PXSCALE, 20 * PXSCALE, height - 2 * this.gaugePadding + 20 * PXSCALE]
}
this.ctx.beginPath()
this.ctx.rect(this.gaugePadding, Math.min(d, height - 3 * PXSCALE), width - this.gaugePadding * 2, PXSCALE)
this.ctx.fill()
this.clearRect = [0, 0, width, height]
}

View file

@ -56,8 +56,6 @@ module.exports = class Knob extends Slider {
super(options)
this.widget.classList.add('knob')
this.lastOffsetX = 0
this.lastOffsetY = 0
this.minDimension = 0