Changeset - fbe417cb765c
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 7 years ago 2018-05-10 07:12:39
drewp@bigasterisk.com
fix or workaround js errors on timeline. view zooming broken. adjusters not drawing
Ignore-this: f3d990bc9e312efb3625ac439d89c561
2 files changed with 51 insertions and 36 deletions:
0 comments (0 inline, 0 general)
light9/web/timeline/adjusters.coffee
Show inline comments
 
@@ -31,70 +31,73 @@ class AdjustersCanvas extends Polymer.El
 
  onMove: (ev) ->
 
    pos = $V([ev.x, ev.y])
 
    if @currentDrag
 
      @currentDrag.cur = pos
 
      @currentDrag.adj.continueDrag(@currentDrag.cur.subtract(@currentDrag.start))
 

	
 
  onUp: (ev) ->
 
    return unless @currentDrag
 
    @currentDrag.adj.endDrag()
 
    @currentDrag = null
 
    
 
  setAdjuster: (adjId, makeAdjustable) ->
 
    # callers register/unregister the Adjustables they want us to make
 
    # adjuster elements for. Caller invents adjId.  makeAdjustable is
 
    # a function returning the Adjustable or it is null to clear any
 
    # adjusters with this id.
 
    if not @adjs[adjId] or not makeAdjustable?
 
      if not makeAdjustable?
 
        delete @adjs[adjId]
 
      else
 
        adj = makeAdjustable()
 
        @adjs[adjId] = adj
 
        adj.id = adjId
 

	
 
    @debounce('adj redraw', @redraw.bind(@))
 
    #@debounce('adj redraw', @redraw.bind(@))
 
    setTimeout((() => @redraw()), 2)
 

	
 
    window.debug_adjsCount = Object.keys(@adjs).length
 

	
 
  updateAllCoords: ->
 
    @redraw()
 

	
 
  _adjAtPoint: (pt) ->
 
    nearest = @qt.find(pt.e(1), pt.e(2))
 
    if not nearest? or nearest.distanceFrom(pt) > 50
 
      return null
 
    return nearest?.adj
 

	
 
  resizeUpdate: (ev) ->
 
    @$.canvas.width = ev.target.offsetWidth
 
    @$.canvas.height = ev.target.offsetHeight
 
    @redraw()
 

	
 
  redraw: (adjs) ->
 
    @debounce('redraw', @_throttledRedraw.bind(@))
 
    @_throttledRedraw(adjs)
 
    #@debounce('redraw', @_throttledRedraw.bind(@))
 

	
 
  _throttledRedraw: () ->
 
    return unless @ctx?
 
    console.time('adjs redraw')
 
    @_layoutCenters()
 
    
 
    @ctx.clearRect(0, 0, @$.canvas.width, @$.canvas.height)
 

	
 
    for adjId, adj of @adjs
 
      ctr = adj.getCenter()
 
      target = adj.getTarget()
 
      @_drawConnector(ctr, target)
 
      
 
      @_drawAdjuster(adj.getDisplayValue(),
 
                     Math.floor(ctr.e(1)) - 20, Math.floor(ctr.e(2)) - 10,
 
                     Math.floor(ctr.e(1)) + 20, Math.floor(ctr.e(2)) + 10)
 
    console.timeEnd('adjs redraw')
 

	
 
  _layoutCenters: ->
 
    # push Adjustable centers around to avoid overlaps
 
    # Todo: also don't overlap inlineattr boxes
 
    # Todo: don't let their connector lines cross each other
 
    @qt = d3.quadtree([], ((d)->d.e(1)), ((d)->d.e(2)))
 
    @qt.extent([[0,0], [8000,8000]])
 
    for _, adj of @adjs
 
      desired = adj.getSuggestedCenter()
 
      output = desired
 
@@ -134,24 +137,26 @@ class AdjustersCanvas extends Polymer.El
 
    @ctx.fillStyle = 'rgba(255, 255, 0, 0.5)'
 
    @ctx.beginPath()
 
    Drawing.roundRect(@ctx, x1, y1, x2, y2, radius)
 
    @ctx.fill()
 

	
 
    @ctx.shadowColor = 'rgba(0,0,0,0)'
 
        
 
    @ctx.strokeStyle = 'yellow'
 
    @ctx.lineWidth = 2
 
    @ctx.setLineDash([3, 3])
 
    @ctx.beginPath()
 
    Drawing.roundRect(@ctx, x1, y1, x2, y2, radius)
 
    @ctx.stroke()
 
    @ctx.setLineDash([])
 

	
 
    @ctx.font = "12px sans"
 
    @ctx.fillStyle = '#000'
 
    @ctx.fillText(label, x1 + 5, y2 - 5, x2 - x1 - 10)
 

	
 
    # coords from a center that's passed in
 
    # # special layout for the thaeter ones with middinh 
 
    # l/r arrows
 
    # mouse arrow cursor upon hover, and accent the hovered adjuster
 
    # connector
 

	
 
customElements.define(AdjustersCanvas.is, AdjustersCanvas)
 
\ No newline at end of file
light9/web/timeline/timeline.coffee
Show inline comments
 
@@ -97,187 +97,199 @@ class TimelineEditor extends Polymer.Ele
 
  @behaviors: [ Polymer.IronResizableBehavior ]
 
  @properties:
 
    viewState: { type: Object }
 
    debug: {type: String}
 
    graph: {type: Object, notify: true}
 
    project: {type: Object}
 
    setAdjuster: {type: Function, notify: true}
 
    playerSong: {type: String, notify: true}
 
    followPlayerSong: {type: Boolean, notify: true, value: true}
 
    song: {type: String, notify: true}
 
    show: {value: 'http://light9.bigasterisk.com/show/dance2017'}
 
    songTime: {type: Number, notify: true, observer: '_onSongTime'}
 
    songDuration: {type: Number, notify: true, observer: '_onSongDuration'}
 
    songPlaying: {type: Boolean, notify: true}
 
    fullZoomX: {type: Object, notify: true}
 
    zoomInX: {type: Object, notify: true}
 
    selection: {type: Object, notify: true}
 
  width: ko.observable(1)
 
  @listeners:
 
    'iron-resize': '_onIronResize'
 
  @observers: [
 
    'setSong(playerSong, followPlayerSong)',
 
    'onGraph(graph)',
 
    ]
 
  _onIronResize: ->
 
    @width(@offsetWidth)
 
  _onSongTime: (t) ->
 
    #@viewState.cursor.t(t)
 
  _onSongDuration: (d) ->
 
    d = 700 if d < 1 # bug is that asco isn't giving duration, but 0 makes the scale corrupt
 
    #@viewState.zoomSpec.duration(d)
 
  setSong: (s) ->
 
    @song = @playerSong if @followPlayerSong
 
  onGraph: (graph) ->
 
    @project = new Project(graph)
 
    
 
  connectedCallback: ->
 
    super.connectedCallback()
 
    ko.options.deferUpdates = true;
 

	
 
    @dia = @$.dia
 

	
 
    @selection = {hover: ko.observable(null), selected: ko.observable([])}
 

	
 
    window.debug_zoomOrLayoutChangedCount = 0
 
    window.debug_adjUpdateDisplay = 0
 
    
 
    @viewState =
 
      zoomSpec:
 
        duration: ko.observable(100) # current song duration
 
        t1: ko.observable(0) # need validation to stay in bounds and not go too close
 
        t2: ko.observable(100)
 
      cursor:
 
        t: ko.observable(20)
 
      mouse:
 
        pos: ko.observable($V([0,0]))
 
    window.viewState = @viewState
 
    @fullZoomX = d3.scaleLinear()
 
    @zoomInX = d3.scaleLinear()
 
    @setAdjuster = (adjId, makeAdjustable) =>
 
      @$.adjustersCanvas.setAdjuster(adjId, makeAdjustable)
 
      ac = @$.adjustersCanvas
 
      setTimeout((()=>ac.setAdjuster(adjId, makeAdjustable)),10)
 

	
 
    setTimeout =>
 
      ko.computed(@zoomOrLayoutChanged.bind(@))
 
      ko.computed(@songTimeChanged.bind(@))
 

	
 
    setInterval(@updateDebugSummary.bind(@), 100)
 
      @trackMouse()
 
      @bindKeys()
 
      @bindWheelZoom(@dia)
 
      setTimeout => # depends on child node being ready
 
          @forwardMouseEventsToAdjustersCanvas()
 
        , 400
 

	
 
      @makeZoomAdjs()
 

	
 
      zoomed = @$.zoomed
 
      setupDrop(@$.dia.shadowRoot.querySelector('svg'),
 
                zoomed.$.rows, @, zoomed.onDrop.bind(zoomed))
 

	
 
      setInterval(@updateDebugSummary.bind(@), 100)
 
    , 500
 

	
 
    #if anchor == loadtest
 
    #  add note and delete it repeatedly
 
    #  disconnect the graph, make many notes, drag a point over many steps, measure lag somewhere
 

	
 
  _onIronResize: ->
 
    @width(@offsetWidth)
 
  _onSongTime: (t) ->
 
    #@viewState.cursor.t(t)
 
  _onSongDuration: (d) ->
 
    d = 700 if d < 1 # bug is that asco isn't giving duration, but 0 makes the scale corrupt
 
    #@viewState.zoomSpec.duration(d)
 
    
 
  setSong: (s) ->
 
    @song = @playerSong if @followPlayerSong
 
  onGraph: (graph) ->
 
    @project = new Project(graph)
 

	
 
  updateDebugSummary: ->
 
    elemCount = (tag) -> document.getElementsByTagName(tag).length
 
    @debug = "#{window.debug_zoomOrLayoutChangedCount} layout change,
 
     #{elemCount('light9-timeline-note')} notes,
 
     #{@selection.selected().length} selected
 
     #{elemCount('light9-timeline-graph-row')} rows,
 
     #{window.debug_adjsCount} adjuster items registered,
 
     #{window.debug_adjUpdateDisplay} adjuster updateDisplay calls,
 
    "
 
    
 
  attached: ->
 
    super()
 
    @dia = @$.dia
 
    ko.computed(@zoomOrLayoutChanged.bind(@))
 
    ko.computed(@songTimeChanged.bind(@))
 

	
 
    @trackMouse()
 
    @bindKeys()
 
    @bindWheelZoom(@dia)
 
    @forwardMouseEventsToAdjustersCanvas()
 

	
 
    @makeZoomAdjs()
 

	
 
    zoomed = @$.zoomed
 
    setupDrop(@$.dia.querySelector('svg'), zoomed.$.rows, @, zoomed.onDrop.bind(zoomed))
 

	
 

	
 
  zoomOrLayoutChanged: ->
 
    log('zoomOrLayoutChanged')
 
    # not for cursor updates
 

	
 
    vs = @viewState
 
    
 
    if vs.zoomSpec.t1() < 0
 
      vs.zoomSpec.t1(0)
 
    if vs.zoomSpec.duration() and vs.zoomSpec.t2() > vs.zoomSpec.duration()
 
      vs.zoomSpec.t2(vs.zoomSpec.duration())
 

	
 
    window.debug_zoomOrLayoutChangedCount++
 
    @fullZoomX.domain([0, vs.zoomSpec.duration()])
 
    @fullZoomX.range([0, @width()])
 

	
 
    # had trouble making notes update when this changes
 
    zoomInX = d3.scaleLinear()
 
    zoomInX.domain([vs.zoomSpec.t1(), vs.zoomSpec.t2()])
 
    zoomInX.range([0, @width()])
 
    @zoomInX = zoomInX
 

	
 
    # todo: these run a lot of work purely for a time change
 
    @dia.setTimeAxis(@width(), @$.zoomed.$.audio.offsetTop, @zoomInX)
 
    @$.adjustersCanvas.updateAllCoords()
 
    if @$.zoomed?.$?.audio?
 
      @dia.setTimeAxis(@width(), @$.zoomed.$.audio.offsetTop, @zoomInX)
 
      @$.adjustersCanvas.updateAllCoords()
 

	
 
    # cursor needs update when layout changes, but I don't want
 
    # zoom/layout to depend on the playback time
 
    setTimeout(@songTimeChanged.bind(@), 1)
 

	
 
  songTimeChanged: ->
 
    return unless @$.zoomed?.$?.time?
 
    @$.cursorCanvas.setCursor(@$.audio.offsetTop, @$.audio.offsetHeight,
 
                              @$.zoomed.$.time.offsetTop,
 
                              @$.zoomed.$.time.offsetHeight,
 
                              @fullZoomX, @zoomInX, @viewState.cursor)
 
    
 
  trackMouse: ->
 
    # not just for show- we use the mouse pos sometimes
 
    for evName in ['mousemove', 'touchmove']
 
      @addEventListener evName, (ev) =>
 
        ev.preventDefault()
 

	
 
        # todo: consolidate with _editorCoordinates version
 
        if ev.touches?.length
 
          ev = ev.touches[0]
 

	
 
        @root = @getBoundingClientRect()
 
        @viewState.mouse.pos($V([ev.pageX - @root.left, ev.pageY - @root.top]))
 

	
 
        @$.cursorCanvas.setMouse(@viewState.mouse.pos())
 
        # should be controlled by a checkbox next to follow-player-song-choice
 
        @sendMouseToVidref() unless window.location.hash.match(/novidref/)
 

	
 
  sendMouseToVidref: ->
 
    now = Date.now()
 
    if (!@$.vidrefLastSent? || @$.vidrefLastSent < now - 200) && !@songPlaying
 
      @$.vidrefTime.body = {t: @latestMouseTime(), source: 'timeline'}
 
      @$.vidrefTime.generateRequest()
 
      @$.vidrefLastSent = now
 

	
 
  latestMouseTime: ->
 
    @zoomInX.invert(@viewState.mouse.pos().e(1))
 

	
 
  bindWheelZoom: (elem) ->
 
    elem.addEventListener 'mousewheel', (ev) =>
 
      zs = @viewState.zoomSpec
 

	
 
      center = @latestMouseTime()
 
      left = center - zs.t1()
 
      right = zs.t2() - center
 
      scale = Math.pow(1.005, ev.deltaY)
 

	
 
      zs.t1(center - left * scale)
 
      zs.t2(center + right * scale)
 
      log('view to', ko.toJSON(@viewState))
 

	
 
  forwardMouseEventsToAdjustersCanvas: ->
 
    ac = @$.adjustersCanvas
 
    @addEventListener('mousedown', ac.onDown.bind(ac))
 
    @addEventListener('mousemove', ac.onMove.bind(ac))
 
    @addEventListener('mouseup', ac.onUp.bind(ac))
 

	
 
  animatedZoom: (newT1, newT2, secs) ->
 
    fps = 30
 
    oldT1 = @viewState.zoomSpec.t1()
 
    oldT2 = @viewState.zoomSpec.t2()
 
    lastTime = 0
 
    for step in [0..secs * fps]
 
      frac = step / (secs * fps)
 
      do (frac) =>
 
        gotoStep = =>
 
          @viewState.zoomSpec.t1((1 - frac) * oldT1 + frac * newT1)
 
          @viewState.zoomSpec.t2((1 - frac) * oldT2 + frac * newT2)
 
        delay = frac * secs * 1000
 
        setTimeout(gotoStep, delay)
 
        lastTime = delay
 
    setTimeout(=>
 
        @viewState.zoomSpec.t1(newT1)
 
        @viewState.zoomSpec.t2(newT2)
 
@@ -389,60 +401,58 @@ class TimeZoomed extends Polymer.Element
 
         backgroundColor: 0xff6060,
 
  
 
    })
 
     
 
  connectedCallback: ->
 
    super.connectedCallback()
 
     
 
    @$.rows.appendChild(@renderer.view);
 
  
 
    # iron-resize should be doing this but it never fires
 
    setInterval(@update.bind(@), 1000)
 
    
 
  onGraph: ->
 
    @graph.runHandler(@gatherNotes.bind(@), 'zoom notes')
 
  gatherNotes: ->
 
    U = (x) => @graph.Uri(x)
 

	
 
    log('assign rows',@song, 'graph has', @graph.quads().length)
 
    graphics = new PIXI.Graphics({nativeLines: true})
 

	
 
    for uri in _.sortBy(@graph.objects(@song, U(':note')), 'uri')
 
      #should only make new ones
 
      # 
 
      child = new Note(@graph, @selection, @dia, uri, @setAdjuster, @song, @zoomInX)
 
      log('note ',uri)
 
      originTime = @graph.floatValue(uri, U(':originTime'))
 
      effect = @graph.uriValue(uri, U(':effectClass'))
 
      for curve in @graph.objects(uri, U(':curve'))
 
        if @graph.uriValue(curve, U(':attr')).equals(U(':strength'))
 

	
 
          [@pointUris, @worldPts] = @project.getCurvePoints(curve, originTime)
 
          curveWidthCalc = () => @_curveWidth(@worldPts)
 

	
 
          h = 150 #@offsetHeight
 
          screenPts = ($V([@zoomInX(pt.e(1)), @offsetTop + (1 - pt.e(2)) * h]) for pt in @worldPts)
 

	
 
          graphics.beginFill(0xFF3300);
 
          graphics.lineStyle(4, 0xffd900, 1)
 

	
 
          graphics.moveTo(screenPts[0].e(1), screenPts[0].e(2))
 
          for p in screenPts.slice(1)
 
            graphics.lineTo(p.e(1), p.e(2))
 
         graphics.endFill()
 
    
 
     @rows = []#(new NoteRow(@graph, @dia, @song, @zoomInX, @noteUris, i, @selection) for i in [0...ROW_COUNT])
 

	
 

	
 

	
 
     @stage.children.splice(0)
 
     @stage.addChild(graphics)
 
     @renderer.render(@stage)
 
    
 

	
 
  onDrop: (effect, pos) ->
 
    U = (x) => @graph.Uri(x)
 

	
 
    return unless effect and effect.match(/^http/)
 

	
 
    # we could probably accept some initial overrides right on the
 
    # effect uri, maybe as query params
0 comments (0 inline, 0 general)