Skip to content
Permalink
Browse files

Fixed the last panning bug when you changed the layer position while …

…dragging
  • Loading branch information...
koenbok committed Jan 28, 2016
1 parent 4581bf2 commit 33153770324e4e2b445d0ea1edf5ca82fd0199eb
Showing with 32 additions and 26 deletions.
  1. +4 −5 framer/GestureInputRecognizer.coffee
  2. +22 −5 framer/LayerDraggable.coffee
  3. +6 −16 framer/LayerPinchable.coffee
@@ -334,9 +334,6 @@ class exports.GestureInputRecognizer

fingers: event.touches?.length or 0 # Number of fingers used √
touchCenter: @_getEventPoint(event) # Center between two fingers √
touchCenterStart: @_getEventPoint(event) #
touchCenterDelta: null
touchCenterOffset: @_getEventPoint(event) #
touchOffset: {x:0, y:0} # Offset between two fingers √
touchDistance: 0 # Distance between two fingers √
scale: 1 # Scale value from two fingers √
@@ -365,7 +362,7 @@ class exports.GestureInputRecognizer
touchPointA = @_getTouchPoint(event, 0)
touchPointB = @_getTouchPoint(event, 1)
event.touchCenter = Utils.pointCenter(touchPointB, touchPointA)
event.touchCenterOffset = Utils.pointSubtract(event.touchCenter, event.touchCenterStart)
event.touchOffset = Utils.pointSubtract(touchPointB, touchPointA)
event.touchDistance = Utils.pointDistance(touchPointA, touchPointB)
event.rotation = Utils.pointAngle(touchPointA, touchPointB)

@@ -387,9 +384,11 @@ class exports.GestureInputRecognizer
event.scaleDirection = @session.lastEvent.scaleDirection

# For delta we switch to center-compare if there are two fingers
if @session?.lastEvent
if @session?.lastEvent
# If we just switched fingers, we skip the delta event entirely
if event.fingers != @session.lastEvent.fingers == 2
event.delta = {x:0, y:0}
# If we are having two finger events, we use the touchCenter as base for delta
if event.fingers == 2 and @session.lastEvent.fingers == 2
event.delta = Utils.pointSubtract(event.touchCenter, @session.lastEvent.touchCenter)

@@ -94,14 +94,16 @@ class exports.LayerDraggable extends BaseClass

@_eventBuffer = new EventBuffer
@_constraints = null
# @_propagateEvents = false
@_ignoreUpdateLayerPosition = true

@attach()

attach: ->
@layer.on(Gestures.TapStart, @touchStart)
@layer.on(Gestures.Pan, @_touchMove)
@layer.on(Gestures.TapEnd, @_touchEnd)
@layer.on("change:x", @_updateLayerPosition)
@layer.on("change:y", @_updateLayerPosition)

remove: ->
@layer.off(Gestures.PanStart, @touchStart)
@@ -117,6 +119,12 @@ class exports.LayerDraggable extends BaseClass
# this is for example needed with the slider.
@_touchStart(event)

_updateLayerPosition: =>
# This updates the layer position if it's extrenally changed while
# a drag is going on at the same time.
return if @_ignoreUpdateLayerPosition is true
@_point = @layer.point

_touchStart: (event) =>

# Only reset isMoving if this was not animating when we were clicking
@@ -133,6 +141,7 @@ class exports.LayerDraggable extends BaseClass
# Extract the event (mobile may have multiple)
touchEvent = Events.touchEvent(event)

# TODO: we should use the event velocity
@_eventBuffer.push
x: touchEvent.clientX
y: touchEvent.clientY
@@ -159,6 +168,7 @@ class exports.LayerDraggable extends BaseClass
y: touchEvent.clientY - @_correctedLayerStartPoint.y

@_point = @layer.point
@_ignoreUpdateLayerPosition = false

@emit(Events.DragStart, event)

@@ -186,6 +196,7 @@ class exports.LayerDraggable extends BaseClass
point.x = @_point.x + (event.delta.x * scaleX) if @horizontal
point.y = @_point.y + (event.delta.y * scaleY) if @vertical

# Save the point for the next update so we have the unrounded, unconstrained value
@_point = _.clone(point)

# Constraints and overdrag
@@ -214,7 +225,11 @@ class exports.LayerDraggable extends BaseClass
point.x = parseInt(point.x) if @horizontal
point.y = parseInt(point.y) if @vertical

# While we update the layer position ourselves, we don't want
# to trigger the updater for external changes.
@_ignoreUpdateLayerPosition = true
@layer.point = @updatePosition(point)
@_ignoreUpdateLayerPosition = false

if @isDragging
@emit(Events.Move, @layer.point)
@@ -231,12 +246,14 @@ class exports.LayerDraggable extends BaseClass

@emit(Events.DragEnd, event)

# # Set _isDragging after DragEnd is fired, so that calls to calculateVelocity()
# # still returns dragging velocity - both in case the user calls calculateVelocity(),
# # (which would return a stale value before the simulation had finished one tick)
# # and because @_start currently calls calculateVelocity().
# Set _isDragging after DragEnd is fired, so that calls to calculateVelocity()
# still returns dragging velocity - both in case the user calls calculateVelocity(),
# (which would return a stale value before the simulation had finished one tick)
# and because @_start currently calls calculateVelocity().
@_isDragging = false

@_ignoreUpdateLayerPosition = true


##############################################################
# Constraints
@@ -59,22 +59,12 @@ class exports.LayerPinchable extends BaseClass
@layer.originY = pinchLocation.y / @layer.height

topInSuperAfter = Utils.convertPoint({}, @layer, @layer.superLayer)
xDiff = topInSuperAfter.x - topInSuperBefore.x
yDiff = topInSuperAfter.y - topInSuperBefore.y
@layer.x -= xDiff
@layer.y -= yDiff

# # This is not a great fix, we should add this in the draggable. Basically
# # we need to account in the draggable for a change of origin relative to
# # the dragging start offset.

# if @layer._draggable
# @layer.draggable._originDiff ?= {x:0, y:0}
# @layer.draggable._originDiff.x += xDiff
# @layer.draggable._originDiff.y += yDiff


# print "_centerOrigin"
originDelta =
x: topInSuperAfter.x - topInSuperBefore.x
y: topInSuperAfter.y - topInSuperBefore.y

@layer.x -= originDelta.x
@layer.y -= originDelta.y

_pinchStart: (event) =>
@_reset()

0 comments on commit 3315377

Please sign in to comment.
You can’t perform that action at this time.