diff --git a/src/client/widgets/containers/panel.mjs b/src/client/widgets/containers/panel.mjs index 57061a6a..9d5b099b 100644 --- a/src/client/widgets/containers/panel.mjs +++ b/src/client/widgets/containers/panel.mjs @@ -112,6 +112,7 @@ class Panel extends Container() { } this.scroll = [0, 0] + this.scrollThumb = [1, 1] this.scrollWidth = 1 this.scrollHeight = 1 this.settingScroll = false @@ -119,11 +120,13 @@ class Panel extends Container() { fastdom.measure(()=>{ this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight + this.scrollThumb = [this.widget.clientWidth / this.widget.scrollWidth || 1, this.widget.clientHeight / this.widget.scrollHeight || 1] }) this.on('scroll', ()=>{ this.scrollWidth = this.widget.scrollWidth - this.widget.clientWidth this.scrollHeight = this.widget.scrollHeight - this.widget.clientHeight - this.scroll = [this.widget.scrollLeft, this.widget.scrollTop] + this.scroll = [this.widget.scrollLeft / this.scrollWidth || 0, this.widget.scrollTop / this.scrollHeight || 0] + this.scrollThumb = [this.widget.clientWidth / this.widget.scrollWidth || 1, this.widget.clientHeight / this.widget.scrollHeight || 1] if (this.settingScroll) { clearTimeout(this.settingScrollEnd) this.settingScrollEnd = setTimeout(()=>{ @@ -133,8 +136,8 @@ class Panel extends Container() { this.scripts.onScroll.run({ value: this.value, event: { - x: this.widget.scrollLeft / this.scrollWidth || 0, - y: this.widget.scrollTop / this.scrollHeight || 0 + x: this.scroll[0], + y: this.scroll[1] } }, {sync: true, send: true}) } diff --git a/src/client/widgets/index.mjs b/src/client/widgets/index.mjs index 64394bb9..6ea652cf 100644 --- a/src/client/widgets/index.mjs +++ b/src/client/widgets/index.mjs @@ -45,6 +45,7 @@ import fader from './sliders/fader.mjs' import knob from './sliders/knob.mjs' import encoder from './sliders/encoder.mjs' import range from './sliders/range.mjs' +import scrollbar from './sliders/scrollbar.mjs' // scripts import script from './scripts/script-widget.mjs' @@ -100,6 +101,7 @@ export const widgets = { knob, encoder, range, + scrollbar, // scripts script, @@ -114,7 +116,7 @@ export const categories = { 'Graphs':['plot','eq','visualizer'], 'Indicators':['led', 'text'], 'Pads':['xy','rgb','multixy', 'canvas'], - 'Sliders':['fader','knob', 'encoder', 'range'], + 'Sliders':['fader','knob', 'encoder', 'range', 'scrollbar'], 'Scripts':['script', 'variable'], } diff --git a/src/client/widgets/sliders/fader.mjs b/src/client/widgets/sliders/fader.mjs index 58c37d97..8b829585 100644 --- a/src/client/widgets/sliders/fader.mjs +++ b/src/client/widgets/sliders/fader.mjs @@ -119,9 +119,9 @@ class Fader extends Slider { } - shouldDrag(e) { + shouldDrag(e, testZone) { - var zone = this.getProp('touchZone'), + var zone = testZone || this.getProp('touchZone'), design = this.getProp('design'), horizontal = this.getProp('horizontal'), snap = this.getProp('snap') @@ -309,7 +309,7 @@ class Fader extends Slider { // flat knob - this.ctx.globalAlpha = 1 + this.ctx.globalAlpha = this.cssVars.alphaKnob || 1 this.ctx.fillStyle = this.cssVars.colorKnob this.ctx.beginPath() diff --git a/src/client/widgets/sliders/scrollbar.mjs b/src/client/widgets/sliders/scrollbar.mjs new file mode 100644 index 00000000..4b1b9e4f --- /dev/null +++ b/src/client/widgets/sliders/scrollbar.mjs @@ -0,0 +1,184 @@ +import Fader from './fader.mjs' +import StaticProperties from '../mixins/static_properties.mjs' +import {clip} from '../utils.mjs' +import widgetManager from '../../managers/widgets.mjs' + +export default class Scrollbar extends StaticProperties(Fader, { + range: {min: 0, max: 1}, + touchZone: 'all', + alphaFillOn: 0, + snap: false, + design: 'compact' +}) { + + static defaults() { + + return super.defaults().extend({ + style: { + _separator_fader_style: 'Scrollbar style', + design: null, + knobSize: null, + // colorKnob: null, + pips: null, + dashed: null, + gradient: null, + }, + class_specific: { + snap: null, + touchZone: null, + spring: null, + doubleTap: null, + range: null, + logScale: null, + steps: null, + origin: null, + widgetId: {type: 'string', value: '', help: '`id` of the container widget to control'}, + } + }) + + } + + constructor(options) { + + super(options) + + this.scrollTarget = null + this.thumbSize = 1 + + if (!this.getProp('horizontal')) { + this.rangeVals[0] = 1 + this.rangeVals[1] = 0 + } + + widgetManager.on('widget-created', (e)=>{ + + if (this.scrollTarget) return + + var {id, widget} = e + + if (id === this.getProp('widgetId') && widget.getProp('scroll')) { + this.unbindTarget() + this.bindTarget(widget) + } + + }, {context: this}) + } + + + draginitHandle(e) { + + // super.draginitHandle(...arguments) + + if (!e.traversing && !this.shouldDrag(e)) { + e.cancelDragEvent = true + return + } + + this.percent = clip(this.percent,[0,100]) + + // snap when not touching the knob zone + if (this.shouldDrag(e, 'knob')) return + + this.percent = this.getProp('horizontal')? + (e.offsetX - this.gaugePadding) / (this.width - this.gaugePadding * 2) * 100: + (this.height - e.offsetY - this.gaugePadding) / (this.height - this.gaugePadding * 2) * 100 + + this.setValue(this.percentToValue(this.percent), {send: true, sync: true, dragged: true}) + + } + + + mousewheelHandle(e) { + + e.preventDefault() + e.stopPropagation() + + var increment + if (this.getProp('horizontal')) { + increment = e.deltaY / this.scrollTarget.scrollWidth * 100 + } else { + increment = -e.deltaY / this.scrollTarget.scrollHeight * 100 + } + + this.percent = clip(this.percent + increment, [0,100]) + this.setValue(this.percentToValue(this.percent), {sync: true, send: true, dragged: true}) + + } + + cacheCanvasStyle(style){ + + super.cacheCanvasStyle(style) + + var size = this.getProp('horizontal') ? this.width : this.height + this.cssVars.knobSize = this.thumbSize * size + this.cssVars.alphaKnob = 0.25 + + } + + + unbindTarget() { + + if (!this.scrollTarget) return + + this.scrollTarget.off(undefined, undefined, this) + + this.scrollTarget = null + + } + + bindTarget(target) { + + if (!target) return + + this.scrollTarget = target + + // listen for cloneTarget's deletion + // if it is just edited, its recreation will be caught by the global 'widget-created' event handler + this.scrollTarget.on('widget-removed', (e)=>{ + if (this.scrollTarget === e.widget) { + this.unbindTarget() + } + }, {context: this}) + + var onScroll = ()=>{ + if (!this.scrollTarget) return + + var index = this.getProp('horizontal') ? 0 : 1, + size = this.getProp('horizontal') ? this.width : this.height + + this.thumbSize = this.scrollTarget.scrollThumb[index] + this.cssVars.knobSize = this.thumbSize * size + this.setValue(this.scrollTarget.scroll[index], {fromPanel:true, script:true}) + } + + this.scrollTarget.on('scroll', onScroll) + setTimeout(()=>{ + onScroll() + }) + + } + + + + setValue(v, options={}) { + + super.setValue(v, options) + + if (!options.fromPanel && this.scrollTarget) { + if (this.getProp('horizontal')) { + this.scrollTarget.setScroll(this.value, undefined, true) + } else { + this.scrollTarget.setScroll(undefined, this.value, true) + } + + } + } + + onRemove(){ + + this.unbindTarget() + super.onRemove() + + } + +} diff --git a/src/scss/widgets/sliders.scss b/src/scss/widgets/sliders.scss index 416e4c6f..ae8a61bf 100644 --- a/src/scss/widgets/sliders.scss +++ b/src/scss/widgets/sliders.scss @@ -1,4 +1,5 @@ .fader-container, +.scrollbar-container, .knob-container, .encoder-container, .range-container { @@ -13,6 +14,7 @@ } +.scrollbar-container, .fader-container, .range-container { @@ -22,7 +24,7 @@ --widget-padding: 1rem; --knob-size: 1rem; --line-width: 1rem; - + canvas { text-align: left; }