From 848f591930ea2b1247b2d5558d1abb74a657ad21 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 12 Mar 2025 15:44:03 +0100 Subject: [PATCH] pads: per-point customization on the way --- CHANGELOG.md | 4 +- src/client/widgets/common/queue.js | 2 +- src/client/widgets/pads/multixy.js | 126 ++++++++++++++++++++--------- src/client/widgets/pads/pad.js | 58 ++++++++++++- src/client/widgets/pads/xy.js | 84 ++++++------------- src/scss/widgets/pads.scss | 4 +- 6 files changed, 173 insertions(+), 105 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eee71dd8..c3c87e97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,9 @@ - xy/multixy: add `auto` choice to `axisLock` (locks on the first moved axis) - file: `extension` can now be an array of strings - plot/eq: smoother curves - - sliders/pads: `doubleTap` can now be `"script"`, in which case `onTouch` will be called with `event.type` set to `doubleTap` + - sliders/pads: `doubleTap` can now be `"script"`, in which case `onTouch` will be called with `event.type` set to `doubleTap` + - xy: add `label` property + - multixy: add `padsAttr` property for per-pad customization ## 1.27.5 diff --git a/src/client/widgets/common/queue.js b/src/client/widgets/common/queue.js index 8cb754c9..6bd18305 100644 --- a/src/client/widgets/common/queue.js +++ b/src/client/widgets/common/queue.js @@ -34,7 +34,7 @@ class CanvasQueue extends EventEmitter{ flush() { for (var h in this.queue) { - if (this.queue[h].width > 0 && this.queue[h].height > 0) { + if (this.queue[h].width > 0 && this.queue[h].height > 0 && this.queue[h].visible) { this.queue[h].draw() } } diff --git a/src/client/widgets/pads/multixy.js b/src/client/widgets/pads/multixy.js index 3bb56312..7baed047 100644 --- a/src/client/widgets/pads/multixy.js +++ b/src/client/widgets/pads/multixy.js @@ -8,7 +8,7 @@ var Pad = require('./pad'), var xyDefaults = Xy.defaults()._props() -module.exports = class MultiXy extends Pad { +class MultiXy extends Pad { static description() { @@ -31,6 +31,17 @@ module.exports = class MultiXy extends Pad { 'Defines the number of points on the pad', 'Can be an array of strings that will be used as labels for the points (ex: `[\'A\', \'B\']`)' ]}, + pointsAttr: {type: 'object', value: [], help: [ + 'Defines per-point properties that are otherwise inherited from the multixy. Must be an array of objects (one per point) that may contain the following keys:', + '- visible (visibility and interactability)', + '- colorFill (background color)', + '- colorStroke (outline color)', + '- colorText (label color)', + '- color (shorthand for colorFill and colorStroke)', + '- alphaFillOn (background opacity)', + '- pointSize', + '- label' + ]}, snap: {type: 'boolean', value: false, help: [ 'By default, the points are dragged from their initial position.', 'If set to `true`, touching anywhere on the widget\'s surface will make them snap to the touching coordinates', @@ -64,7 +75,6 @@ module.exports = class MultiXy extends Pad { super(options) this.npoints = Array.isArray(this.getProp('points')) ? this.getProp('points').length : this.getProp('points') || 0 - this.labels = Array.isArray(this.getProp('points')) this.preventChildrenTouchState = true this.pads = [] @@ -83,7 +93,7 @@ module.exports = class MultiXy extends Pad { logScaleY:this.getProp('logScaleY'), axisLock:this.getProp('axisLock'), pointSize: 'auto', - pips: this.getProp('pips') && i == this.npoints-1, + pips: false, sensitivity: this.getProp('sensitivity'), input:false }, parent: this}) @@ -92,6 +102,12 @@ module.exports = class MultiXy extends Pad { } + // tiny hack for pips until there's a proper range refactoring + this.faders = this.pads[0].faders + + this.pointsAttr = [] + this.updatePadsAttr() + this.padsCoords = [] this.touchMap = {} @@ -108,6 +124,7 @@ module.exports = class MultiXy extends Pad { for (var i in this.pads) { if (Object.values(this.touchMap).indexOf(i) != -1) continue + if (!this.pads[i].getProp('visible')) continue ndiff = Math.abs(e.offsetX - this.padsCoords[i][0]) + Math.abs(e.offsetY - (this.padsCoords[i][1] + this.height)) @@ -116,6 +133,7 @@ module.exports = class MultiXy extends Pad { diff = ndiff } + } this.touchMap[e.pointerId] = id @@ -132,8 +150,6 @@ module.exports = class MultiXy extends Pad { this.pads[id].trigger('draginit', e) this.pads[id].touched = 1 - if (this.getProp('ephemeral')) this.batchDraw() - }, {element: this.widget, multitouch: true}) this.on('drag',(e)=>{ @@ -163,8 +179,6 @@ module.exports = class MultiXy extends Pad { delete this.touchMap[e.pointerId] - if (this.getProp('ephemeral')) this.batchDraw() - }, {element: this.widget, multitouch: true}) this.on('value-changed',(e)=>{ @@ -241,8 +255,8 @@ module.exports = class MultiXy extends Pad { super.cacheCanvasStyle(style) for (var k in this.pads) { this.pads[k].cacheCanvasStyle() - this.pads[k].batchDraw() } + this.batchDraw() } @@ -252,6 +266,7 @@ module.exports = class MultiXy extends Pad { for (var k in this.pads) { this.pads[k].setVisibility() } + this.batchDraw() } @@ -270,25 +285,7 @@ module.exports = class MultiXy extends Pad { this.clear() - this.clearRect = [] - - for (var i=0;ix.js==n) + if (cssVar) { + this.pads[i].canvas.style.setProperty(cssVar.css, this.pointsAttr[i][n] == 'auto' ? '' : this.pointsAttr[i][n]) + } + + if (n == 'visible') { + this.pads[i].canvas.style.setProperty('opacity', this.pointsAttr[i][n] ? 1 : 0) + } + + } + } + } + + + } + onPropChanged(propName, options, oldPropValue) { if (super.onPropChanged(...arguments)) return true switch (propName) { + case 'padding': + for (let w of this.pads) { + w.onPropChanged(propName) + } + return + case 'colorText': case 'colorWidget': case 'colorFill': @@ -343,19 +392,11 @@ module.exports = class MultiXy extends Pad { case 'alphaStroke': case 'alphaFillOff': case 'alphaFillOn': - case 'padding': - case 'lineWidth': - for (let w of this.pads) { - w.onPropChanged(propName) - } - return + case 'pointsAttr': case 'spring': + case 'pointSize': case 'axisLock': - for (let w of this.pads) { - w.cachedProps[propName] = this.getProp(propName) - w.onPropChanged(propName, options) - } - this.batchDraw() + this.updatePadsAttr() return } @@ -369,3 +410,10 @@ module.exports = class MultiXy extends Pad { } } + + +MultiXy.dynamicProps = MultiXy.prototype.constructor.dynamicProps.filter(n => n !== 'decimals').concat([ + 'pointsAttr', +]) + +module.exports = MultiXy diff --git a/src/client/widgets/pads/pad.js b/src/client/widgets/pads/pad.js index ca925418..93a2f924 100644 --- a/src/client/widgets/pads/pad.js +++ b/src/client/widgets/pads/pad.js @@ -23,6 +23,61 @@ class Pad extends Canvas { } + drawPips() { + + var margin = this.padPadding, + pipTexts = margin >= this.fontSize * 1.5 + + this.ctx.lineWidth = PXSCALE + this.ctx.fillStyle = this.cssVars.colorText + this.ctx.strokeStyle = this.cssVars.colorStroke + this.ctx.globalAlpha = this.cssVars.alphaPips + this.ctx.beginPath() + + for (let i in this.faders.x.rangeKeys) { + + let pip = this.faders.x.rangeKeys[i], + px = Math.round(0.5 * this.faders.x.percentToCoord(pip)) * 2 + + if (parseInt(px) === px) px -= 0.5 + + if (pipTexts) { + this.ctx.textAlign = 'center' + let label = this.faders.x.rangeLabels[i] + this.ctx.fillText(label, px, this.height - margin / 2) + } + + if (pip == 0 || pip == 100) continue + this.ctx.moveTo(px, margin - PXSCALE) + this.ctx.lineTo(px, this.height - margin + PXSCALE) + + + } + for (let i in this.faders.y.rangeKeys) { + + let pip = this.faders.y.rangeKeys[i], + py = Math.round(0.5 * this.faders.y.percentToCoord(pip)) * 2 + + if (parseInt(py) === py) py -= 0.5 + + if (pipTexts) { + this.ctx.textAlign = 'right' + let label = this.faders.y.rangeLabels[i] + this.ctx.fillText(label, margin / 2 + this.fontSize / 2, py) + } + + if (pip == 0 || pip == 100) continue + this.ctx.moveTo(margin - PXSCALE, py) + this.ctx.lineTo(this.width - margin + PXSCALE, py) + + + } + + this.ctx.globalAlpha = this.cssVars.alphaFillOn + this.ctx.stroke() + + } + onPropChanged(propName, options, oldPropValue) { var ret = super.onPropChanged(...arguments) @@ -47,7 +102,8 @@ Pad.cssVariables = Pad.prototype.constructor.cssVariables.concat( Pad.dynamicProps = Pad.prototype.constructor.dynamicProps.filter(n => n !== 'decimals').concat([ 'spring', 'axisLock', - 'pointSize' + 'pointSize', + 'label' ]) module.exports = Pad diff --git a/src/client/widgets/pads/xy.js b/src/client/widgets/pads/xy.js index 2aab17b7..5c2f36e0 100644 --- a/src/client/widgets/pads/xy.js +++ b/src/client/widgets/pads/xy.js @@ -21,6 +21,7 @@ module.exports = class Xy extends Pad { pointSize: {type: 'integer', value: 20, help: 'Defines the points\' size'}, ephemeral: {type: 'boolean', value: false, help: 'When set to `true`, the point will be drawn only while touched.'}, pips: {type: 'boolean', value: true, help: 'Set to `false` to hide the scale'}, + label: {type: 'string', value: '', help: 'Text displayed in the handle'} }, class_specific: { snap: {type: 'boolean', value: false, help: [ @@ -219,6 +220,8 @@ module.exports = class Xy extends Pad { draw() { + if (!this.visible) return + var pointSize = this.cssVars.pointSize, margin = this.padPadding, x = this.faders.x.percentToCoord(this.faders.x.percent), @@ -238,69 +241,11 @@ module.exports = class Xy extends Pad { - - var pipsDrawn = false - if (this.getProp('pips')) { - - var pipTexts = margin >= this.fontSize * 1.5 - - this.ctx.lineWidth = PXSCALE - this.ctx.fillStyle = this.cssVars.colorText - this.ctx.strokeStyle = this.cssVars.colorStroke - this.ctx.globalAlpha = this.cssVars.alphaPips - this.ctx.beginPath() - - for (let i in this.faders.x.rangeKeys) { - - let pip = this.faders.x.rangeKeys[i], - px = Math.round(0.5 * this.faders.x.percentToCoord(pip)) * 2 - - if (parseInt(px) === px) px -= 0.5 - - if (pipTexts) { - this.ctx.textAlign = 'center' - let label = this.faders.x.rangeLabels[i] - this.ctx.fillText(label, px, this.height - margin / 2) - pipsDrawn = true - } - - if (pip == 0 || pip == 100) continue - this.ctx.moveTo(px, margin - PXSCALE) - this.ctx.lineTo(px, this.height - margin + PXSCALE) - pipsDrawn = true - - - } - for (let i in this.faders.y.rangeKeys) { - - let pip = this.faders.y.rangeKeys[i], - py = Math.round(0.5 * this.faders.y.percentToCoord(pip)) * 2 - - if (parseInt(py) === py) py -= 0.5 - - if (pipTexts) { - this.ctx.textAlign = 'right' - let label = this.faders.y.rangeLabels[i] - this.ctx.fillText(label, margin / 2 + this.fontSize / 2, py) - pipsDrawn = true - } - - if (pip == 0 || pip == 100) continue - this.ctx.moveTo(margin - PXSCALE, py) - this.ctx.lineTo(this.width - margin + PXSCALE, py) - pipsDrawn = true - - - } - - this.ctx.globalAlpha = this.cssVars.alphaFillOn - this.ctx.stroke() - + if (this.getProp('pips')) this.drawPips() + else { + // this.clearRect = [x - pointSize - 2 * PXSCALE, y - pointSize - 2 * PXSCALE, (pointSize + PXSCALE) * 4, (pointSize + PXSCALE) * 4] } - if (!pipsDrawn) this.clearRect = [x - pointSize - 2 * PXSCALE, y - pointSize - 2 * PXSCALE, (pointSize + PXSCALE) * 4, (pointSize + PXSCALE) * 4] - - if (!ephemeral || this.active) { this.ctx.strokeStyle = this.cssVars.colorStroke this.ctx.globalAlpha = this.active ? 1 : 0.75 @@ -308,6 +253,20 @@ module.exports = class Xy extends Pad { this.ctx.beginPath() this.ctx.arc(x, y, pointSize, Math.PI * 2, false) this.ctx.stroke() + + var t = this.getProp('label') + if (t) { + this.ctx.fillStyle = this.cssVars.colorText + this.ctx.globalAlpha = 1 + this.ctx.textAlign = 'center' + this.ctx.fillText(t, x + 0.5 * PXSCALE, y + PXSCALE) + + if (!this.getProp('pips')) { + var length = this.fontSize * t.length + // this.clearRect.push([x - length / 2, y - this.fontSize / 2, length, this.fontSize + 2 * PXSCALE]) + } + + } } @@ -320,6 +279,9 @@ module.exports = class Xy extends Pad { switch (propName) { + case 'label': + this.batchDraw() + return case 'spring': if (this.getProp('spring') && !this.touched) this.setValue([this.faders.x.getSpringValue(),this.faders.y.getSpringValue()], {...options}) return diff --git a/src/scss/widgets/pads.scss b/src/scss/widgets/pads.scss index ba13d73d..edc88ea2 100644 --- a/src/scss/widgets/pads.scss +++ b/src/scss/widgets/pads.scss @@ -32,7 +32,7 @@ --point-size: 20rem; - canvas:not(:first-child) { + canvas { position: absolute; top: 0; left: 0; @@ -139,7 +139,7 @@ .html { pointer-events: auto; } - + inner { position: relative; flex: 1;