# HG changeset patch # User Drew Perttula # Date 2016-06-04 06:30:33 # Node ID d6396679c12107f0aff3c43dbee14a63fbee26a3 # Parent e4c65310c0502deb6e71bf05152bbedf2202b76d move to adjustable.coffee and also simplify the getTarget code Ignore-this: e7addcd75d8fcac10df92c64f8a77ecd diff --git a/light9/web/adjustable.coffee b/light9/web/adjustable.coffee new file mode 100644 --- /dev/null +++ b/light9/web/adjustable.coffee @@ -0,0 +1,105 @@ +log = console.log + +class Adjustable + # Some value you can edit in the UI, probably by dragging stuff. May + # have a associated. This object does the + # layout and positioning. + # + # The way dragging should work is that you start in the yellow *adj + # widget*, wherever it is, but your drag is moving the *target*. The + # adj will travel around too, but it may do extra moves to not bump + # into stuff or to get out from under your finger. + + constructor: (@config) -> + # config has: + # getTarget -> vec2 of current target position + # getSuggestedTargetOffset -> vec2 pixel offset from target + # emptyBox -> true if you want no value display + + getDisplayValue: () -> + return '' if @config.emptyBox + d3.format(".4g")(@_getValue()) + + getCenter: () -> # vec2 of pixels + @getTarget().add(@config.getSuggestedTargetOffset()) + + getTarget: () -> # vec2 of pixels + @config.getTarget() + + subscribe: (onChange) -> + # change could be displayValue or center or target. This likely + # calls onChange right away if there's any data yet. + setInterval((() => onChange()), 100) + + startDrag: () -> + # todo + @dragStartValue = @_getValue() + + continueDrag: (pos) -> + # pos is vec2 of pixels relative to the drag start + + # todo + newValue = @dragStartValue + pos.e(0) * .1 + graph.patchObject(@_subj, @_pred, graph.Literal(newValue), @_ctx) + + endDrag: () -> + 0 + +class window.AdjustableFloatObservable extends Adjustable + constructor: (@config) -> + # config also has: + # observable -> ko.observable we will read and write + # getValueForPos(pos) -> what should we set to if the user + # moves target to this coord? + + _getValue: () -> + @config.observable() + + _editorCoordinates: () -> # vec2 of mouse relative to + ev = d3.event.sourceEvent + + if ev.target.tagName == "LIGHT9-TIMELINE-EDITOR" + rootElem = ev.target + else + rootElem = ev.target.closest('light9-timeline-editor') + + @root = rootElem.getBoundingClientRect() if rootElem + offsetParentPos = $V([ev.pageX - @root.left, ev.pageY - @root.top]) + + setMouse(offsetParentPos) + return offsetParentPos + + continueDrag: (pos) -> + # pos is vec2 of pixels relative to the drag start. + + epos = @_editorCoordinates() + log('offsetParentPos', epos.elements) + + newValue = @config.getValueForPos(epos) + @config.observable(newValue) + + subscribe: (onChange) -> + ko.computed => + @config.observable() + onChange() + +class window.AdjustableFloatObject extends Adjustable + constructor: (@config) -> + # config has graph, subj, pred, ctx, getSuggestedTargetOffset + super(@config) + + _getValue: () -> # for drag math + @config.graph.floatValue(@config.subj, @config.pred) + + getCenter: () -> + $V([100 + 200 * @_getValue(), 200]) + + subscribe: (onChange) -> + @config.graph.subscribe @config.subj, @config.pred, null, (patch) => + onChange() + + continueDrag: (pos) -> + # pos is vec2 of pixels relative to the drag start + + newValue = @dragStartValue + pos.e(1) / 200 + @config.graph.patchObject(@config.subj, @config.pred, @config.graph.Literal(newValue), @_ctx) diff --git a/light9/web/timeline-elements.html b/light9/web/timeline-elements.html --- a/light9/web/timeline-elements.html +++ b/light9/web/timeline-elements.html @@ -338,4 +338,5 @@ + diff --git a/light9/web/timeline.coffee b/light9/web/timeline.coffee --- a/light9/web/timeline.coffee +++ b/light9/web/timeline.coffee @@ -14,108 +14,6 @@ window.graph.loadTrig(" ") -class Adjustable - # Some value you can edit in the UI, probably by dragging stuff. May - # have a associated. This object does the - # layout and positioning. - constructor: (@config) -> - # config has getTarget, getSuggestedTargetOffset, getValue, emptyBox - - getDisplayValue: () -> - return '' if @config.emptyBox - d3.format(".4g")(@_getValue()) - - getCenter: () -> # vec2 of pixels - @getTarget().add(@config.getSuggestedTargetOffset()) - - getTarget: () -> # vec2 of pixels - @config.getTarget() - - subscribe: (onChange) -> - # change could be displayValue or center or target. This likely - # calls onChange right away if there's any data yet. - setInterval((() => onChange()), 100) - - startDrag: () -> - # todo - @dragStartValue = @_getValue() - - continueDrag: (pos) -> - # pos is vec2 of pixels relative to the drag start - - # todo - newValue = @dragStartValue + pos.e(0) * .1 - graph.patchObject(@_subj, @_pred, graph.Literal(newValue), @_ctx) - - endDrag: () -> - 0 - -class AdjustableFloatObservable extends Adjustable - constructor: (@config) -> - # config has observable, getValueForPos, valueLow, targetLow, valueHigh, targetHigh, getSuggestedTargetOffset - ko.computed => - @_normalizedValue = d3.scaleLinear().domain([ - ko.unwrap(@config.valueLow), - ko.unwrap(@config.valueHigh)]).range([0, 1]) - - _getValue: () -> - @config.observable() - - getTarget: () -> - f = @_normalizedValue(@_getValue()) - [lo, hi] = [ko.unwrap(@config.targetLow), - ko.unwrap(@config.targetHigh)] - return lo.add(hi.subtract(lo).multiply(f)) - - _editorCoordinates: () -> # vec2 of mouse relative to - ev = d3.event.sourceEvent - - if ev.target.tagName == "LIGHT9-TIMELINE-EDITOR" - rootElem = ev.target - else - rootElem = ev.target.closest('light9-timeline-editor') - - @root = rootElem.getBoundingClientRect() if rootElem - offsetParentPos = $V([ev.pageX - @root.left, ev.pageY - @root.top]) - - setMouse(offsetParentPos) - return offsetParentPos - - continueDrag: (pos) -> - # pos is vec2 of pixels relative to the drag start. - - epos = @_editorCoordinates() - log('offsetParentPos', epos.elements) - - newValue = @config.getValueForPos(epos) - @config.observable(newValue) - - subscribe: (onChange) -> - ko.computed => - @config.observable() - onChange() - -class AdjustableFloatObject extends Adjustable - constructor: (@config) -> - # config has graph, subj, pred, ctx, getSuggestedTargetOffset - super(@config) - - _getValue: () -> # for drag math - @config.graph.floatValue(@config.subj, @config.pred) - - getCenter: () -> - $V([100 + 200 * @_getValue(), 200]) - - subscribe: (onChange) -> - @config.graph.subscribe @config.subj, @config.pred, null, (patch) => - onChange() - - continueDrag: (pos) -> - # pos is vec2 of pixels relative to the drag start - - newValue = @dragStartValue + pos.e(1) / 200 - @config.graph.patchObject(@config.subj, @config.pred, @config.graph.Literal(newValue), @_ctx) - Polymer is: 'light9-timeline-editor' @@ -180,25 +78,22 @@ Polymer makeZoomAdjs: -> yMid = @$.audio.offsetTop + @$.audio.offsetHeight / 2 dur = @viewState.zoomSpec.duration + valForPos = (pos) => x = pos.e(1) t = @fullZoomX.invert(x) left = new AdjustableFloatObservable({ observable: @viewState.zoomSpec.t1, - valueLow: 0 - valueHigh: dur - targetLow: $V([0, yMid]) - targetHigh: $V([@offsetWidth, yMid]) + getTarget: () => + $V([@fullZoomX(@viewState.zoomSpec.t1()), yMid]) getSuggestedTargetOffset: () => $V([-50, 0]) getValueForPos: valForPos }) right = new AdjustableFloatObservable({ observable: @viewState.zoomSpec.t2, - valueLow: 0 - valueHigh: dur - targetLow: $V([0, yMid]) - targetHigh: $V([@offsetWidth, yMid]) + getTarget: () => + $V([@fullZoomX(@viewState.zoomSpec.t2()), yMid]) getSuggestedTargetOffset: () => $V([50, 0]) getValueForPos: valForPos }) @@ -216,11 +111,9 @@ Polymer pan = new AdjustableFloatObservable({ observable: panObs emptyBox: true - valueLow: 0 - valueHigh: dur - # not right- the sides shouldn't be able to go offscreen - targetLow: $V([0, 30]) # y = @$.audio.offsetTop + @$.audio.offsetHeight / 2] - targetHigh: $V([@offsetWidth, 30]) + # fullzoom is not right- the sides shouldn't be able to go + # offscreen + getTarget: () => $V([@fullZoomX(panObs()), yMid]) getSuggestedTargetOffset: () => $V([0, 0]) getValueForPos: valForPos }) diff --git a/makefile b/makefile --- a/makefile +++ b/makefile @@ -85,3 +85,6 @@ arduino_upload: /usr/share/arduino/Ardui make upload effect_node_setup: create_virtualenv packages binexec install_python_deps + +coffee: + coffee -cw light9/web/*.coffee