Files
@ 7fe81130b735
Branch filter:
Location: light9/light9/web/timeline/adjusters.coffee
7fe81130b735
5.7 KiB
text/coffeescript
move web logging to https://github.com/visionmedia/debug/ so it can have channels that can be turned off
Ignore-this: bc5147fdfe1e4712d671cfc36698a6f
Ignore-this: bc5147fdfe1e4712d671cfc36698a6f
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | log = debug('adjusters')
Drawing = window.Drawing
maxDist = 60
coffeeElementSetup(class AdjustersCanvas extends Polymer.mixinBehaviors([Polymer.IronResizableBehavior], Polymer.Element)
@is: 'light9-adjusters-canvas'
@getter_properties:
setAdjuster: {type: Function, notify: true }
@getter_observers: [
'updateAllCoords(adjs)'
]
constructor: ->
super()
@redraw = _.throttle(@_throttledRedraw.bind(@), 30, {leading: false})
@adjs = {}
@hoveringNear = null
ready: ->
super.ready()
@addEventListener('iron-resize', @resizeUpdate.bind(@))
@ctx = @$.canvas.getContext('2d')
@redraw()
@setAdjuster = @_setAdjuster.bind(@)
# These don't fire; TimelineEditor calls the handlers for us.
@addEventListener('mousedown', @onDown.bind(@))
@addEventListener('mousemove', @onMove.bind(@))
@addEventListener('mouseup', @onUp.bind(@))
_mousePos: (ev) ->
$V([ev.clientX, ev.clientY - @offsetParent.offsetTop])
onDown: (ev) ->
if ev.buttons == 1
start = @_mousePos(ev)
adj = @_adjAtPoint(start)
if adj
ev.stopPropagation()
@currentDrag = {start: start, adj: adj}
adj.startDrag()
onMove: (ev) ->
pos = @_mousePos(ev)
if @currentDrag
@hoveringNear = null
@currentDrag.cur = pos
@currentDrag.adj.continueDrag(
@currentDrag.cur.subtract(@currentDrag.start))
@redraw()
else
near = @_adjAtPoint(pos)
if @hoveringNear != near
@hoveringNear = near
@redraw()
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 makeAdjustable?
if @adjs[adjId]
delete @adjs[adjId]
else
# this might be able to reuse an existing one a bit
adj = makeAdjustable()
@adjs[adjId] = adj
adj.id = adjId
@redraw()
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) > maxDist
return null
return nearest?.adj
resizeUpdate: (ev) ->
@$.canvas.width = ev.target.offsetWidth
@$.canvas.height = ev.target.offsetHeight
@canvasCenter = $V([@$.canvas.width / 2, @$.canvas.height / 2])
@redraw()
_throttledRedraw: () ->
return unless @ctx?
console.time('adjs redraw')
@_layoutCenters()
@ctx.clearRect(0, 0, @$.canvas.width, @$.canvas.height)
for adjId, adj of @adjs
ctr = adj.getHandle()
target = adj.getTarget()
if @_isOffScreen(target)
continue
@_drawConnector(ctr, target)
@_drawAdjuster(adj.getDisplayValue(),
ctr.e(1) - 20, ctr.e(2) - 10,
ctr.e(1) + 20, ctr.e(2) + 10,
adj == @hoveringNear)
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
adj.handle = @_clampOnScreen(adj.getSuggestedHandle())
numTries = 8
for tries in [0...numTries]
for _, adj of @adjs
current = adj.handle
@qt.remove(current)
nearest = @qt.find(current.e(1), current.e(2), maxDist)
if nearest
dist = current.distanceFrom(nearest)
if dist < maxDist
current = @_stepAway(current, nearest, 1 / numTries)
adj.handle = current
current.adj = adj
@qt.add(current)
#if -50 < output.e(1) < 20 # mostly for zoom-left
# output.setElements([
# Math.max(20, output.e(1)),
# output.e(2)])
_stepAway: (current, nearest, dx) ->
away = current.subtract(nearest).toUnitVector()
toScreenCenter = @canvasCenter.subtract(current).toUnitVector()
goalSpacingPx = 20
@_clampOnScreen(current.add(away.x(goalSpacingPx * dx)))
_isOffScreen: (pos) ->
pos.e(1) < 0 or pos.e(1) > @$.canvas.width or pos.e(2) < 0 or pos.e(2) > @$.canvas.height
_clampOnScreen: (pos) ->
marg = 30
$V([Math.max(marg, Math.min(@$.canvas.width - marg, pos.e(1))),
Math.max(marg, Math.min(@$.canvas.height - marg, pos.e(2)))])
_drawConnector: (ctr, target) ->
@ctx.strokeStyle = '#aaa'
@ctx.lineWidth = 2
@ctx.beginPath()
Drawing.line(@ctx, ctr, target)
@ctx.stroke()
_drawAdjuster: (label, x1, y1, x2, y2, hover) ->
radius = 8
@ctx.shadowColor = 'black'
@ctx.shadowBlur = 15
@ctx.shadowOffsetX = 5
@ctx.shadowOffsetY = 9
@ctx.fillStyle = if hover then '#ffff88' else '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
)
|