From 4d5bef0252291f6c77a2f2726636547bac7fda6d Mon Sep 17 00:00:00 2001 From: Pavel fljot Date: Fri, 2 Mar 2012 20:33:16 +0200 Subject: [PATCH] Touch properties updates (and corresponding gestures fixes) --- src/org/gestouch/core/Touch.as | 76 ++++++++++++++++--- src/org/gestouch/gestures/Gesture.as | 8 +- src/org/gestouch/gestures/LongPressGesture.as | 12 +-- src/org/gestouch/gestures/PanGesture.as | 29 +++---- src/org/gestouch/gestures/RotateGesture.as | 31 ++------ src/org/gestouch/gestures/TapGesture.as | 19 +---- src/org/gestouch/gestures/ZoomGesture.as | 31 ++------ src/org/gestouch/input/MouseInputAdapter.as | 17 ++--- src/org/gestouch/input/TouchInputAdapter.as | 24 +++--- 9 files changed, 114 insertions(+), 133 deletions(-) diff --git a/src/org/gestouch/core/Touch.as b/src/org/gestouch/core/Touch.as index 6ed565c..dddcb81 100644 --- a/src/org/gestouch/core/Touch.as +++ b/src/org/gestouch/core/Touch.as @@ -1,6 +1,7 @@ package org.gestouch.core { import flash.display.InteractiveObject; + import flash.geom.Point; /** @@ -20,18 +21,11 @@ package org.gestouch.core */ public var target:InteractiveObject; - public var x:Number; - public var y:Number; public var sizeX:Number; public var sizeY:Number; public var pressure:Number; - public var time:uint; -// public var touchBeginPos:Point; -// public var touchBeginTime:uint; -// public var moveOffset:Point; // public var lastMove:Point; -// public var velocity:Point; public function Touch(id:uint = 0) @@ -40,16 +34,76 @@ package org.gestouch.core } + protected var _location:Point; + public function get location():Point + { + return _location.clone(); + } + gestouch_internal function setLocation(value:Point):void + { + _location = value; + _beginLocation = _location.clone(); + } + gestouch_internal function updateLocation(x:Number, y:Number):void + { + if (_location) + { + _location.x = x; + _location.y = y; + } + else + { + gestouch_internal::setLocation(new Point(x, y)); + } + } + + + protected var _beginLocation:Point; + public function get beginLocation():Point + { + return _beginLocation.clone(); + } + + + public function get locationOffset():Point + { + return _location.subtract(_beginLocation); + } + + + protected var _time:uint; + public function get time():uint + { + return _time; + } + gestouch_internal function setTime(value:uint):void + { + _time = value; + } + + + protected var _beginTime:uint; + public function get beginTime():uint + { + return _beginTime; + } + gestouch_internal function setBeginTime(value:uint):void + { + _beginTime = value; + } + + public function clone():Touch { var touch:Touch = new Touch(id); - touch.x = x; - touch.y = y; + touch._location = _location; + touch._beginLocation = _beginLocation; touch.target = target; touch.sizeX = sizeX; touch.sizeY = sizeY; touch.pressure = pressure; - touch.time = time; + touch._time = _time; + touch._beginTime = _beginTime; return touch; } @@ -57,7 +111,7 @@ package org.gestouch.core public function toString():String { - return "Touch [id: " + id + ", x: " + x + ", y: " + y + ", ...]"; + return "Touch [id: " + id + ", location: " + location + ", ...]"; } } } \ No newline at end of file diff --git a/src/org/gestouch/gestures/Gesture.as b/src/org/gestouch/gestures/Gesture.as index f2a459c..6c859c2 100644 --- a/src/org/gestouch/gestures/Gesture.as +++ b/src/org/gestouch/gestures/Gesture.as @@ -359,14 +359,14 @@ package org.gestouch.gestures protected function updateCentralPoint():void { - var touch:Touch; + var touchLocation:Point; var x:Number = 0; var y:Number = 0; for (var touchID:String in _touchesMap) { - touch = _touchesMap[int(touchID)] as Touch; - x += touch.x; - y += touch.y; + touchLocation = (_touchesMap[int(touchID)] as Touch).location; + x += touchLocation.x; + y += touchLocation.y; } _centralPoint.x = x / _touchesCount; _centralPoint.y = y / _touchesCount; diff --git a/src/org/gestouch/gestures/LongPressGesture.as b/src/org/gestouch/gestures/LongPressGesture.as index 550d154..cc6679f 100644 --- a/src/org/gestouch/gestures/LongPressGesture.as +++ b/src/org/gestouch/gestures/LongPressGesture.as @@ -28,8 +28,6 @@ package org.gestouch.gestures public var slop:Number = Gesture.DEFAULT_SLOP; protected var _timer:Timer; - protected var _touchBeginX:Array = []; - protected var _touchBeginY:Array = []; protected var _numTouchesRequiredReached:Boolean; @@ -57,8 +55,6 @@ package org.gestouch.gestures { super.reset(); - _touchBeginX.length = 0; - _touchBeginY.length = 0; _numTouchesRequiredReached = false; _timer.reset(); } @@ -96,9 +92,6 @@ package org.gestouch.gestures return; } - _touchBeginX[touch.id] = touch.x; - _touchBeginY[touch.id] = touch.y; - if (touchesCount == numTouchesRequired) { _numTouchesRequiredReached = true; @@ -120,10 +113,7 @@ package org.gestouch.gestures { if (state == GestureState.POSSIBLE && slop > 0) { - // Fail if touch overcome slop distance - var dx:Number = Number(_touchBeginX[touch.id]) - touch.x; - var dy:Number = Number(_touchBeginY[touch.id]) - touch.y; - if (Math.sqrt(dx*dx + dy*dy) > slop) + if (touch.locationOffset.length > slop) { setState(GestureState.FAILED); return; diff --git a/src/org/gestouch/gestures/PanGesture.as b/src/org/gestouch/gestures/PanGesture.as index c89eac3..d449856 100644 --- a/src/org/gestouch/gestures/PanGesture.as +++ b/src/org/gestouch/gestures/PanGesture.as @@ -24,9 +24,6 @@ package org.gestouch.gestures */ public var direction:uint = PanGestureDirection.NO_DIRECTION; - protected var _touchBeginX:Array = []; - protected var _touchBeginY:Array = []; - public function PanGesture(target:InteractiveObject = null) { @@ -91,15 +88,6 @@ package org.gestouch.gestures return PanGesture; } - - override public function reset():void - { - _touchBeginX.length = 0; - _touchBeginY.length = 0; - - super.reset(); - } - @@ -118,9 +106,6 @@ package org.gestouch.gestures return; } - _touchBeginX[touch.id] = touch.x; - _touchBeginY[touch.id] = touch.y; - if (touchesCount >= minNumTouchesRequired) { updateLocation(); @@ -141,9 +126,17 @@ package org.gestouch.gestures if (state == GestureState.POSSIBLE) { // Check if finger moved enough for gesture to be recognized - var dx:Number = (direction == PanGestureDirection.VERTICAL) ? 0 : Number(_touchBeginX[touch.id]) - touch.x; - var dy:Number = (direction == PanGestureDirection.HORIZONTAL) ? 0 : Number(_touchBeginY[touch.id]) - touch.y; - if (Math.sqrt(dx*dx + dy*dy) > slop || slop != slop)//faster isNaN(slop) + var locationOffset:Point = touch.locationOffset; + if (direction == PanGestureDirection.VERTICAL) + { + locationOffset.x = 0; + } + else if (direction == PanGestureDirection.HORIZONTAL) + { + locationOffset.y = 0; + } + + if (locationOffset.length > slop || slop != slop)//faster isNaN(slop) { prevLocationX = _location.x; prevLocationY = _location.y; diff --git a/src/org/gestouch/gestures/RotateGesture.as b/src/org/gestouch/gestures/RotateGesture.as index 1e52c4d..add1e79 100644 --- a/src/org/gestouch/gestures/RotateGesture.as +++ b/src/org/gestouch/gestures/RotateGesture.as @@ -21,8 +21,6 @@ package org.gestouch.gestures { public var slop:Number = Gesture.DEFAULT_SLOP >> 1; - protected var _touchBeginX:Array = []; - protected var _touchBeginY:Array = []; protected var _rotationVector:Point = new Point(); protected var _firstTouch:Touch; protected var _secondTouch:Touch; @@ -47,15 +45,6 @@ package org.gestouch.gestures return RotateGesture; } - - override public function reset():void - { - _touchBeginX.length = 0; - _touchBeginY.length = 0; - - super.reset(); - } - @@ -82,13 +71,7 @@ package org.gestouch.gestures { _secondTouch = touch; - _touchBeginX[_firstTouch.id] = _firstTouch.x; - _touchBeginY[_firstTouch.id] = _firstTouch.y; - _touchBeginX[_secondTouch.id] = _secondTouch.x; - _touchBeginY[_secondTouch.id] = _secondTouch.y; - - _rotationVector.x = _secondTouch.x - _firstTouch.x; - _rotationVector.y = _secondTouch.y - _firstTouch.y; + _rotationVector = _secondTouch.location.subtract(_firstTouch.location); } } @@ -106,19 +89,14 @@ package org.gestouch.gestures if (touchesCount == 2) { - var currRotationVector:Point = new Point(_secondTouch.x - _firstTouch.x, _secondTouch.y - _firstTouch.y); var recognized:Boolean; if (state == GestureState.POSSIBLE) { // we start once any finger moved enough - var dx:Number = Number(_touchBeginX[touch.id]) - touch.x; - var dy:Number = Number(_touchBeginY[touch.id]) - touch.y; - if (Math.sqrt(dx*dx + dy*dy) > slop || slop != slop)//faster isNaN(slop) + if (touch.locationOffset.length > slop || slop != slop)//faster isNaN(slop) { recognized = true; - _rotationVector.x = _secondTouch.x - _firstTouch.x; - _rotationVector.y = _secondTouch.y - _firstTouch.y; } } else @@ -128,13 +106,14 @@ package org.gestouch.gestures if (recognized) { - updateLocation(); - + var currRotationVector:Point = _secondTouch.location.subtract(_firstTouch.location); var rotation:Number = Math.atan2(currRotationVector.y, currRotationVector.x) - Math.atan2(_rotationVector.y, _rotationVector.x); rotation *= GestureUtils.RADIANS_TO_DEGREES; _rotationVector.x = currRotationVector.x; _rotationVector.y = currRotationVector.y; + updateLocation(); + if (state == GestureState.POSSIBLE) { if (setState(GestureState.BEGAN) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) diff --git a/src/org/gestouch/gestures/TapGesture.as b/src/org/gestouch/gestures/TapGesture.as index b73a3c0..52f31c7 100644 --- a/src/org/gestouch/gestures/TapGesture.as +++ b/src/org/gestouch/gestures/TapGesture.as @@ -24,8 +24,6 @@ package org.gestouch.gestures public var maxTapDuration:uint = 1500; protected var _timer:Timer; - protected var _touchBeginX:Array = []; - protected var _touchBeginY:Array = []; protected var _numTouchesRequiredReached:Boolean; protected var _tapCounter:uint = 0; @@ -51,9 +49,7 @@ package org.gestouch.gestures override public function reset():void - { - _touchBeginX.length = 0; - _touchBeginY.length = 0; + { _numTouchesRequiredReached = false; _tapCounter = 0; _timer.reset(); @@ -100,9 +96,6 @@ package org.gestouch.gestures return; } - _touchBeginX[touch.id] = touch.x; - _touchBeginY[touch.id] = touch.y; - if (touchesCount == 1) { _timer.reset(); @@ -119,15 +112,9 @@ package org.gestouch.gestures override protected function onTouchMove(touch:Touch):void { - if (slop >= 0) + if (slop >= 0 && touch.locationOffset.length > slop) { - // Fail if touch overcome slop distance - var dx:Number = Number(_touchBeginX[touch.id]) - touch.x; - var dy:Number = Number(_touchBeginY[touch.id]) - touch.y; - if (Math.sqrt(dx*dx + dy*dy) > slop) - { - setState(GestureState.FAILED); - } + setState(GestureState.FAILED); } } diff --git a/src/org/gestouch/gestures/ZoomGesture.as b/src/org/gestouch/gestures/ZoomGesture.as index 4d0cf12..c3e1cd4 100644 --- a/src/org/gestouch/gestures/ZoomGesture.as +++ b/src/org/gestouch/gestures/ZoomGesture.as @@ -21,8 +21,6 @@ package org.gestouch.gestures public var slop:Number = Gesture.DEFAULT_SLOP >> 1; public var lockAspectRatio:Boolean = true; - protected var _touchBeginX:Array = []; - protected var _touchBeginY:Array = []; protected var _scaleVector:Point = new Point(); protected var _firstTouch:Touch; protected var _secondTouch:Touch; @@ -47,15 +45,6 @@ package org.gestouch.gestures return ZoomGesture; } - - override public function reset():void - { - _touchBeginX.length = 0; - _touchBeginY.length = 0; - - super.reset(); - } - @@ -82,13 +71,7 @@ package org.gestouch.gestures { _secondTouch = touch; - _touchBeginX[_firstTouch.id] = _firstTouch.x; - _touchBeginY[_firstTouch.id] = _firstTouch.y; - _touchBeginX[_secondTouch.id] = _secondTouch.x; - _touchBeginY[_secondTouch.id] = _secondTouch.y; - - _scaleVector.x = _secondTouch.x - _firstTouch.x; - _scaleVector.y = _secondTouch.y - _firstTouch.y; + _scaleVector = _secondTouch.location.subtract(_firstTouch.location); } } @@ -106,19 +89,14 @@ package org.gestouch.gestures if (touchesCount == 2) { - var currScaleVector:Point = new Point(_secondTouch.x - _firstTouch.x, _secondTouch.y - _firstTouch.y); var recognized:Boolean; if (state == GestureState.POSSIBLE) { // Check if finger moved enough for gesture to be recognized - var dx:Number = Number(_touchBeginX[touch.id]) - touch.x; - var dy:Number = Number(_touchBeginY[touch.id]) - touch.y; - if (Math.sqrt(dx*dx + dy*dy) > slop || slop != slop)//faster isNaN(slop) + if (touch.locationOffset.length > slop || slop != slop)//faster isNaN(slop) { recognized = true; - _scaleVector.x = _secondTouch.x - _firstTouch.x; - _scaleVector.y = _secondTouch.y - _firstTouch.y; } } else @@ -128,8 +106,7 @@ package org.gestouch.gestures if (recognized) { - updateLocation(); - + var currScaleVector:Point = _secondTouch.location.subtract(_firstTouch.location); var scaleX:Number; var scaleY:Number; if (lockAspectRatio) @@ -145,6 +122,8 @@ package org.gestouch.gestures _scaleVector.x = currScaleVector.x; _scaleVector.y = currScaleVector.y; + updateLocation(); + if (state == GestureState.POSSIBLE) { if (setState(GestureState.BEGAN) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM)) diff --git a/src/org/gestouch/input/MouseInputAdapter.as b/src/org/gestouch/input/MouseInputAdapter.as index 51ce265..38094fe 100644 --- a/src/org/gestouch/input/MouseInputAdapter.as +++ b/src/org/gestouch/input/MouseInputAdapter.as @@ -7,6 +7,7 @@ package org.gestouch.input import flash.display.Stage; import flash.events.EventPhase; import flash.events.MouseEvent; + import flash.geom.Point; import flash.utils.getTimer; @@ -78,9 +79,9 @@ package org.gestouch.input var touch:Touch = _touchesManager.createTouch(); touch.id = 0; touch.target = event.target as InteractiveObject; - touch.x = event.stageX; - touch.y = event.stageY; - touch.time = getTimer(); + touch.gestouch_internal::setLocation(new Point(event.stageX, event.stageY)); + touch.gestouch_internal::setTime(getTimer()); + touch.gestouch_internal::setBeginTime(getTimer()); _touchesManager.addTouch(touch); @@ -96,9 +97,8 @@ package org.gestouch.input return; var touch:Touch = _touchesManager.getTouch(PRIMARY_TOUCH_POINT_ID); - touch.x = event.stageX; - touch.y = event.stageY; - touch.time = getTimer(); + touch.gestouch_internal::updateLocation(event.stageX, event.stageY); + touch.gestouch_internal::setTime(getTimer()); _gesturesManager.gestouch_internal::onTouchMove(touch); } @@ -117,9 +117,8 @@ package org.gestouch.input return; var touch:Touch = _touchesManager.getTouch(PRIMARY_TOUCH_POINT_ID); - touch.x = event.stageX; - touch.y = event.stageY; - touch.time = getTimer(); + touch.gestouch_internal::updateLocation(event.stageX, event.stageY); + touch.gestouch_internal::setTime(getTimer()); _gesturesManager.gestouch_internal::onTouchEnd(touch); diff --git a/src/org/gestouch/input/TouchInputAdapter.as b/src/org/gestouch/input/TouchInputAdapter.as index f4d87a7..b86afab 100644 --- a/src/org/gestouch/input/TouchInputAdapter.as +++ b/src/org/gestouch/input/TouchInputAdapter.as @@ -7,6 +7,7 @@ package org.gestouch.input import flash.display.Stage; import flash.events.EventPhase; import flash.events.TouchEvent; + import flash.geom.Point; import flash.utils.getTimer; @@ -81,19 +82,20 @@ package org.gestouch.input var touch:Touch = _touchesManager.createTouch(); touch.id = event.touchPointID; touch.target = event.target as InteractiveObject; - touch.x = event.stageX; - touch.y = event.stageY; + touch.gestouch_internal::setLocation(new Point(event.stageX, event.stageY)); touch.sizeX = event.sizeX; touch.sizeY = event.sizeY; touch.pressure = event.pressure; //TODO: conditional compilation? if (event.hasOwnProperty("timestamp")) { - touch.time = event["timestamp"]; + touch.gestouch_internal::setTime(event["timestamp"]); + touch.gestouch_internal::setBeginTime(event["timestamp"]); } else { - touch.time = getTimer(); + touch.gestouch_internal::setTime(getTimer()); + touch.gestouch_internal::setBeginTime(getTimer()); } _touchesManager.addTouch(touch); @@ -111,19 +113,18 @@ package org.gestouch.input return; var touch:Touch = _touchesManager.getTouch(event.touchPointID); - touch.x = event.stageX; - touch.y = event.stageY; + touch.gestouch_internal::updateLocation(event.stageX, event.stageY); touch.sizeX = event.sizeX; touch.sizeY = event.sizeY; touch.pressure = event.pressure; //TODO: conditional compilation? if (event.hasOwnProperty("timestamp")) { - touch.time = event["timestamp"]; + touch.gestouch_internal::setTime(event["timestamp"]); } else { - touch.time = getTimer(); + touch.gestouch_internal::setTime(getTimer()); } _gesturesManager.gestouch_internal::onTouchMove(touch); @@ -142,19 +143,18 @@ package org.gestouch.input return; var touch:Touch = _touchesManager.getTouch(event.touchPointID); - touch.x = event.stageX; - touch.y = event.stageY; + touch.gestouch_internal::updateLocation(event.stageX, event.stageY); touch.sizeX = event.sizeX; touch.sizeY = event.sizeY; touch.pressure = event.pressure; //TODO: conditional compilation? if (event.hasOwnProperty("timestamp")) { - touch.time = event["timestamp"]; + touch.gestouch_internal::setTime(event["timestamp"]); } else { - touch.time = getTimer(); + touch.gestouch_internal::setTime(getTimer()); } _gesturesManager.gestouch_internal::onTouchEnd(touch);