From 0f2cb1b0af029883de22e7c6d89a3eb711dcc1c9 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 14 Feb 2024 19:35:17 +0100 Subject: [PATCH] xy: add `axisLock` property; allow locking one the axis by holding `Shift` while dragging --- CHANGELOG.md | 1 + src/client/widgets/pads/pad.js | 3 ++- src/client/widgets/pads/xy.js | 32 ++++++++++++++++++++++++++------ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02b72774..2a3c4494 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - widgets - dropdown: allow resubmitting the same value - plot: missing `logScaleX` and `logScaleY` properties + - xy: add `axisLock` property; allow locking one the axis by holding `Shift` while dragging ## 1.25.7 diff --git a/src/client/widgets/pads/pad.js b/src/client/widgets/pads/pad.js index 3b278654..df82e556 100644 --- a/src/client/widgets/pads/pad.js +++ b/src/client/widgets/pads/pad.js @@ -29,7 +29,8 @@ class Pad extends Canvas { } Pad.dynamicProps = Pad.prototype.constructor.dynamicProps.filter(n => n !== 'decimals').concat([ - 'spring' + 'spring', + 'axisLock' ]) module.exports = Pad diff --git a/src/client/widgets/pads/xy.js b/src/client/widgets/pads/xy.js index 92a973b4..a68dce22 100644 --- a/src/client/widgets/pads/xy.js +++ b/src/client/widgets/pads/xy.js @@ -32,6 +32,10 @@ module.exports = class Xy extends Pad { rangeY: {type: 'object', value: {min:0,max:1}, help: 'Defines the min and max values for the y axis'}, logScaleX: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the x axis. Set to `-1` for exponential scale.'}, logScaleY: {type: 'boolean|number', value: false, help: 'Set to `true` to use logarithmic scale for the y axis. Set to `-1` for exponential scale.'}, + axisLock: {type: 'string', value: '', choices: ['', 'x', 'y'], help: [ + 'Restrict movements to one of the axes only.', + 'When left to the default value, holding `Shift` while dragging will lock the pad according the first movement.' + ]}, doubleTap: {type: 'boolean|string', value: false, help: [ 'Set to `true` to make the fader reset to its default value when receiving a double tap.', 'Can also be an osc address, which case the widget will just send an osc message: `/ `' @@ -88,23 +92,39 @@ module.exports = class Xy extends Pad { this.active = false + this.autoAxisLock = '' + this.on('draginit',(e)=>{ this.active = true - this.faders.x.trigger('draginit', {...e, stopPropagation: true}) - this.faders.y.trigger('draginit', {...e, stopPropagation: true}) + this.autoAxisLock = '' + var axis = this.getProp('axisLock') + if (axis !== 'y') this.faders.x.trigger('draginit', {...e, stopPropagation: true}) + if (axis !== 'x') this.faders.y.trigger('draginit', {...e, stopPropagation: true}) this.dragHandle() }, {element: this.canvas}) this.on('drag',(e)=>{ - this.faders.x.trigger('drag', e) - this.faders.y.trigger('drag', e) + var axis = this.getProp('axisLock') + if (e.shiftKey && axis === '') { + if (this.autoAxisLock === '') { + if (Math.abs(e.movementX) > Math.abs(e.movementY)) { + this.autoAxisLock = 'x' + } else if (Math.abs(e.movementY) > Math.abs(e.movementX)) { + this.autoAxisLock = 'y' + } + } + axis = this.autoAxisLock + } + if (axis !== 'y') this.faders.x.trigger('drag', e) + if (axis !== 'x') this.faders.y.trigger('drag', e) this.dragHandle() }, {element: this.canvas}) this.on('dragend', (e)=>{ this.active = false - this.faders.x.trigger('dragend', {...e, stopPropagation: true}) - this.faders.y.trigger('dragend', {...e, stopPropagation: true}) + var axis = this.getProp('axisLock') + if (axis !== 'y') this.faders.x.trigger('dragend', {...e, stopPropagation: true}) + if (axis !== 'x') 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, spring:true}) } else {