mirror of
https://github.com/scratchfoundation/Gestouch.git
synced 2025-03-22 10:15:17 -04:00
Improve internal algorithm for ZoomGesture
This commit is contained in:
parent
fd55468579
commit
f8149935db
1 changed files with 49 additions and 38 deletions
|
@ -13,19 +13,18 @@ package org.gestouch.gestures
|
|||
*/
|
||||
[Event(name="gestureZoom", type="org.gestouch.events.ZoomGestureEvent")]
|
||||
/**
|
||||
* TODO:
|
||||
* -check native behavior on iDevice
|
||||
*
|
||||
* @author Pavel fljot
|
||||
*/
|
||||
public class ZoomGesture extends Gesture
|
||||
{
|
||||
public var slop:Number = Gesture.DEFAULT_SLOP >> 1;
|
||||
public var slop:Number = Gesture.DEFAULT_SLOP;
|
||||
public var lockAspectRatio:Boolean = true;
|
||||
|
||||
protected var _touch1:Touch;
|
||||
protected var _touch2:Touch;
|
||||
protected var _transformVector:Point;
|
||||
protected var _initialDistance:Number;
|
||||
|
||||
|
||||
public function ZoomGesture(target:Object = null)
|
||||
|
@ -73,6 +72,7 @@ package org.gestouch.gestures
|
|||
_touch2 = touch;
|
||||
|
||||
_transformVector = _touch2.location.subtract(_touch1.location);
|
||||
_initialDistance = _transformVector.length;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,48 +82,59 @@ package org.gestouch.gestures
|
|||
if (touchesCount < 2)
|
||||
return;
|
||||
|
||||
var recognized:Boolean = true;
|
||||
var currTransformVector:Point = _touch2.location.subtract(_touch1.location);
|
||||
var scaleX:Number;
|
||||
var scaleY:Number;
|
||||
|
||||
if (state == GestureState.POSSIBLE && slop > 0 && touch.locationOffset.length < slop)
|
||||
if (state == GestureState.POSSIBLE)
|
||||
{
|
||||
recognized = false;
|
||||
const d:Number = currTransformVector.length - _initialDistance;
|
||||
const absD:Number = d >= 0 ? d : -d;
|
||||
if (absD < slop)
|
||||
{
|
||||
// Not recognized yet
|
||||
return;
|
||||
}
|
||||
|
||||
if (slop > 0)
|
||||
{
|
||||
// adjust _transformVector to avoid initial "jump"
|
||||
const slopVector:Point = currTransformVector.clone();
|
||||
slopVector.normalize(_initialDistance + (d >= 0 ? slop : -slop));
|
||||
_transformVector = slopVector;
|
||||
}
|
||||
}
|
||||
|
||||
if (recognized)
|
||||
|
||||
if (lockAspectRatio)
|
||||
{
|
||||
var currTransformVector:Point = _touch2.location.subtract(_touch1.location);
|
||||
var scaleX:Number;
|
||||
var scaleY:Number;
|
||||
if (lockAspectRatio)
|
||||
scaleX = scaleY = currTransformVector.length / _transformVector.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
scaleX = currTransformVector.x / _transformVector.x;
|
||||
scaleY = currTransformVector.y / _transformVector.y;
|
||||
}
|
||||
|
||||
_transformVector.x = currTransformVector.x;
|
||||
_transformVector.y = currTransformVector.y;
|
||||
|
||||
updateLocation();
|
||||
|
||||
if (state == GestureState.POSSIBLE)
|
||||
{
|
||||
if (setState(GestureState.BEGAN) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM))
|
||||
{
|
||||
scaleX = scaleY = currTransformVector.length / _transformVector.length;
|
||||
dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GestureState.BEGAN,
|
||||
_location.x, _location.y, _localLocation.x, _localLocation.y, scaleX, scaleY));
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
if (setState(GestureState.CHANGED) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM))
|
||||
{
|
||||
scaleX = currTransformVector.x / _transformVector.x;
|
||||
scaleY = currTransformVector.y / _transformVector.y;
|
||||
}
|
||||
|
||||
_transformVector.x = currTransformVector.x;
|
||||
_transformVector.y = currTransformVector.y;
|
||||
|
||||
updateLocation();
|
||||
|
||||
if (state == GestureState.POSSIBLE)
|
||||
{
|
||||
if (setState(GestureState.BEGAN) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM))
|
||||
{
|
||||
dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GestureState.BEGAN,
|
||||
_location.x, _location.y, _localLocation.x, _localLocation.y, scaleX, scaleY));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (setState(GestureState.CHANGED) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM))
|
||||
{
|
||||
dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GestureState.CHANGED,
|
||||
_location.x, _location.y, _localLocation.x, _localLocation.y, scaleX, scaleY));
|
||||
}
|
||||
dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GestureState.CHANGED,
|
||||
_location.x, _location.y, _localLocation.x, _localLocation.y, scaleX, scaleY));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -144,7 +155,7 @@ package org.gestouch.gestures
|
|||
else if (state == GestureState.POSSIBLE)
|
||||
{
|
||||
setState(GestureState.FAILED);
|
||||
}
|
||||
}
|
||||
}
|
||||
else//== 1
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue