diff --git a/README.textile b/README.textile index 4f6d6b4..9076e0a 100644 --- a/README.textile +++ b/README.textile @@ -1,56 +1,109 @@ -h2. Gestouch: NUI gestures detection framework for mouse, touch and multitouch AS3 development. +h1. Gestouch: NUI gestures detection framework for mouse, touch and multitouch AS3 development. -Gestouch is a very basic framework that helps you to detect gestures when you develop NUI (Natural User Interface). -Last versions of Flash Player and AIR have built-in touch and multitouch support, but the gestures support is quite poor: only small set of gestures are supported, they depend on OS, they are not customizable, only one can be processed at the same time and, finally, you are forced to use either raw TouchEvents, or gestures (@see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/Multitouch.html#inputMode). +Gestouch is a ActionScript library/framework that helps you to deal with single- and multitouch gestures for building better NUI (Natural User Interface). -This framework is aimed to simplify the process of detecting raw TouchEvents and processing them into a specific gesture(s). There are several built-in common gestures, but you are welcome write your own. + +h3. Why? There's already gesture support in Flash/AIR! + +Yes, last versions of Flash Player and AIR runtimes have built-in touch and multitouch support, but the gestures support is very poor: only small set of gestures are supported, they depend on OS, they are not customizable in any way, only one can be processed at the same time and, finally, you are forced to use either raw TouchEvents, or gestures (@see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/Multitouch.html#inputMode). +_Upd:_ With "native way" you also won't get anything out of Stage3D and of custom input like TUIO protocol. + + + +h3. What Gestouch does in short? + +Well basically there's 3 distinctive tasks to solve. +# To provide various input. It can be standard MouseEvents, TouchEvents or more complex things like custom input via TUIO protocol for your hand-made installation. So what we get here is Touches (touch points). +# To recognize gesture out of touch points. Each type of Gesture has it's own inner algorithms that ... +# To manage gestures relations. Because they may "overlap" and once some has been recognized probably we don't want other to do so. + +Gestouch solves these 3 tasks. +I was hardly inspired by Apple team, how they solved this (quite recently to my big surprise! I thought they had it right from the beginning) in they Cocoa-touch UIKit framework. Gestouch is very similar in many ways. But I wouldn't call it "direct port" because 1) the whole architecture was implemented based just on conference videos and user documentation 2) flash platform is a different platform with own specialization, needs, etc. +So I want Gestouch to go far beyond that. Features: +* Pretty neat architecture! Very similar to Apple's UIGestureRecognizers (Cocoa-Touch UIKit) +* Doesn't require any additional software (may use runtime's build-in touch support) +* Works across all platforms (where Flash Player or AIR run of course) in exactly same way +* Doesn't break your DisplayList architecture (could be easily used for Flex development) +* Extendable. You can write your own application-specific gestures +* Open-source and free +* *_+Planning to make it work with Stage3D. Hello Starling!+_* + + + +h3. Current state of the project + +v0.3 introduces "new architecture". I'm planning to develop everything in develop branch and merge to master only release versions. Release versions suppose to be pretty stable. As much as I test them on the examples project. +Current plan is to fix possible bugs in v0.3.#, and I really want to introduce Stage3D support in v0.4. So watch both branches. +And I hope people to become giving some real feedback at least. -* Doesn't require any additional software (uses runtimes build-in touch support); -* Doesn't break your DisplayList architecture (doesn't require any wrappers, so could be easily used for Flex development); -* Basically allows you to write multi-user interfaces; -* Extendable. You can write your own application-specific gestures; -* Open-source and easy to use. h3. Getting Started -* "Introduction video":http://www.youtube.com/watch?v=NjkmB8rfQjY -* "Disclaimer and Architecture Overview":http://github.com/fljot/Gestouch/wiki/Overview -* "Usage":http://github.com/fljot/Gestouch/wiki/Usage +Like so: +
var doubleTap:TapGesture = new TapGesture(myButton);
+doubleTap.numTapsRequired = 2;
+doubleTap.addEventListener(TapGestureEvent.GESTURE_TAP, onDoubleTap);
+...
+private function onDoubleTap(event:TapGestureEvent):void
+{
+ // handle double tap!
+}
+
+or
+var freeTransform:TransformGesture = new TransformGesture(myImage);
+freeTransform.addEventListener(TransformGestureEvent.GESTURE_TRANSFORM, onFreeTransform);
+...
+private function onFreeTransform(event:TransformGestureEvent):void
+{
+ // move, rotate, scale — all at once for better performance!
+}
+
+* Check the "Gestouch Examples":http://github.com/fljot/GestouchExamples project for a quick jump-in
+* *+Highly recommended+* to watch videos from Apple WWDC conferences as they explain all the concepts and show more or less real-life examples. @see links below
+* "Introduction video":http://www.youtube.com/watch?v=NjkmB8rfQjY - my first video, currently outdated
+* TODO: wiki
-h3. Code
-
-* "Gestouch Framework":http://github.com/fljot/Gestouch
-* "Gestouch Examples":http://github.com/fljot/GestouchExamples
h3. Roadmap, TODOs
-* Simulator (for testing multitouch gestures without special devices)
-* Chained gestures concept / behaviors.
+* *Stage3D support.* Hello Starling! Must move away from target as InteractiveObject to some abstract adapters.
+* "Massive gestures" & Clusters. For bigger form-factor multitouch usage, when gestures must be a bit less about separate fingers but rather touch clusters (massive multitouch)
+* -Simulator (for testing multitouch gestures without special devices)- With new architecture it must be relatively easy to create SimulatorInputAdapter
+* Chained gestures concept? To transfer touches from one gesture to another. Example: press/hold for circular menu, then drag it around.
* 3-fingers (3D) gestures (two fingers still, one moving)
+
+h3. News
+
+* "Follow me on Twitter":http://twitter.com/fljot for latest updates
+* Don't forget about "issues":https://github.com/fljot/Gestouch/issues section as good platform for discussions.
+
+
+
h3. Contribution, Donations
Contribute, share. Found it useful, nothing to add? Hire me for some project.
-h3. News:
-* "Follow me on Twitter":http://twitter.com/fljot for latest updates
+h3. Links
+* "Gestouch Examples":http://github.com/fljot/GestouchExamples
-h3. Other Resources
+* "Apple WWDC 2011: Making the Most of Multi-Touch on iOS":https://developer.apple.com/videos/wwdc/2011/?id=118
+* "Apple WWDC 2010: Simplifying Touch Event Handling with Gesture Recognizers":https://developer.apple.com/videos/wwdc/2010/?id=120
+* "Apple WWDC 2010: Advanced Gesture Recognition":https://developer.apple.com/videos/wwdc/2010/?id=121
+* "Event Handling Guide for iOS":https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/
+* "UIGestureRecognizer Class Reference":https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIGestureRecognizer_Class/
-* "GestureWorks":http://www.gestureworks.com
* "TUIO":http://www.tuio.org
-* "Google: Flash + Multitouch":http://www.google.com/search?q=flash+multitouch
-
h2. License
diff --git a/src/org/gestouch/Direction.as b/src/org/gestouch/Direction.as
deleted file mode 100644
index 56e8852..0000000
--- a/src/org/gestouch/Direction.as
+++ /dev/null
@@ -1,17 +0,0 @@
-package org.gestouch
-{
- public class Direction
- {
- public static const NONE:String = "none";
- public static const LEFT:String = "left";
- public static const RIGHT:String = "right";
- public static const UP:String = "up";
- public static const DOWN:String = "down";
- public static const HORIZONTAL:String = "horizontal";
- public static const VERTICAL:String = "vertical";
- public static const STRAIGHT_AXES:String = "straightsAxes";
- public static const DIAGONAL_AXES:String = "diagonalAxes";
- public static const OCTO:String = "octo";
- public static const ALL:String = "all";
- }
-}
\ No newline at end of file
diff --git a/src/org/gestouch/core/GestureState.as b/src/org/gestouch/core/GestureState.as
new file mode 100644
index 0000000..07c9e75
--- /dev/null
+++ b/src/org/gestouch/core/GestureState.as
@@ -0,0 +1,17 @@
+package org.gestouch.core
+{
+ /**
+ * @author Pavel fljot
+ */
+ public class GestureState
+ {
+ public static const IDLE:uint = 1 << 0;//1
+ public static const POSSIBLE:uint = 1 << 1;//2
+ public static const RECOGNIZED:uint = 1 << 2;//4
+ public static const BEGAN:uint = 1 << 3;//8
+ public static const CHANGED:uint = 1 << 4;//16
+ public static const ENDED:uint = 1 << 5;//32
+ public static const CANCELLED:uint = 1 << 6;//64
+ public static const FAILED:uint = 1 << 7;//128
+ }
+}
\ No newline at end of file
diff --git a/src/org/gestouch/core/GesturesManager.as b/src/org/gestouch/core/GesturesManager.as
index f196ae0..9dc5a02 100644
--- a/src/org/gestouch/core/GesturesManager.as
+++ b/src/org/gestouch/core/GesturesManager.as
@@ -1,371 +1,402 @@
package org.gestouch.core
{
- import org.gestouch.events.MouseTouchEvent;
- import org.gestouch.utils.ObjectPool;
+ import org.gestouch.gestures.Gesture;
+ import org.gestouch.input.MouseInputAdapter;
+ import org.gestouch.input.TouchInputAdapter;
+ import flash.display.DisplayObject;
+ import flash.display.DisplayObjectContainer;
import flash.display.InteractiveObject;
+ import flash.display.Shape;
import flash.display.Stage;
- import flash.errors.IllegalOperationError;
import flash.events.Event;
- import flash.events.MouseEvent;
- import flash.events.TouchEvent;
import flash.ui.Multitouch;
- import flash.ui.MultitouchInputMode;
import flash.utils.Dictionary;
- import flash.utils.getTimer;
-
/**
* @author Pavel fljot
*/
public class GesturesManager implements IGesturesManager
{
- public static var implementation:IGesturesManager;
-
- protected static var _impl:IGesturesManager;
- protected static var _initialized:Boolean = false;
+ public static var initDefaultInputAdapter:Boolean = true;
+ private static var _instance:IGesturesManager;
+ private static var _allowInstantiation:Boolean;
+ protected const _touchesManager:ITouchesManager = TouchesManager.getInstance();
+ protected const _frameTickerShape:Shape = new Shape();
+ protected var _inputAdapters:Vector.Gesture-specific configuratin properties:
- * timeThreshold — time between first touchBegin and second touchEnd events,
- * moveThreshold — maximum allowed distance between two taps.
For multitouch usage this is a bit more complex then "first touchBeing and second touchEnd":
- * Taps are counted once minTouchPointsCount
of touch points are down and then fully released.
- * So it's time in milliseconds between full press and full release events for gesture to be detected.
DragGestureEvent has 3 possible phases: GesturePhase.BEGIN, GesturePhase.UPDATE, GesturePhase.END
- * - * @see org.gestouch.events.DragGestureEvent - * @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/GestureEvent.html#phase - * @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/GesturePhase.html - * - * @author Pavel fljot - */ - public class DragGesture extends MovingGestureBase - { - public function DragGesture(target:InteractiveObject = null, settings:Object = null) - { - super(target, settings); - } - - - - - //-------------------------------------------------------------------------- - // - // Static methods - // - //-------------------------------------------------------------------------- - - public static function add(target:InteractiveObject, settings:Object = null):DragGesture - { - return new DragGesture(target, settings); - } - - - public static function remove(target:InteractiveObject):DragGesture - { - return GesturesManager.gestouch_internal::removeGestureByTarget(DragGesture, target) as DragGesture; - } - - - - - //-------------------------------------------------------------------------- - // - // Public methods - // - //-------------------------------------------------------------------------- - - override public function reflect():Class - { - return DragGesture; - } - - - override public function onTouchBegin(touchPoint:TouchPoint):void - { - // No need to track more points than we need - if (_trackingPointsCount == maxTouchPointsCount) - { - return; - } - - _trackPoint(touchPoint); - - if (_trackingPointsCount > 1) - { - _updateCentralPoint(); - _centralPoint.lastMove.x = _centralPoint.lastMove.y = 0; - } - } - - - override public function onTouchMove(touchPoint:TouchPoint):void - { - // do calculations only when we track enough points - if (_trackingPointsCount < minTouchPointsCount) - { - return; - } - - _updateCentralPoint(); - - if (!_slopPassed) - { - _slopPassed = _checkSlop(_centralPoint.moveOffset); - - if (_slopPassed) - { - var slopVector:Point = slop > 0 ? null : new Point(); - if (!slopVector) - { - if (_canMoveHorizontally && _canMoveVertically) - { - slopVector = _centralPoint.moveOffset.clone(); - slopVector.normalize(slop); - slopVector.x = Math.round(slopVector.x); - slopVector.y = Math.round(slopVector.y); - } - else if (_canMoveVertically) - { - slopVector = new Point(0, _centralPoint.moveOffset.y >= slop ? slop : -slop); - } - else if (_canMoveHorizontally) - { - slopVector = new Point(_centralPoint.moveOffset.x >= slop ? slop : -slop, 0); - } - } - _centralPoint.lastMove = _centralPoint.moveOffset.subtract(slopVector); - _dispatch(new DragGestureEvent(DragGestureEvent.GESTURE_DRAG, true, false, GesturePhase.BEGIN, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y, 1, 1, 0, _centralPoint.lastMove.x, _centralPoint.lastMove.y)); - } - } - else - { - _dispatch(new DragGestureEvent(DragGestureEvent.GESTURE_DRAG, true, false, GesturePhase.UPDATE, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y, 1, 1, 0, _centralPoint.lastMove.x, _centralPoint.lastMove.y)); - } - } - - - override public function onTouchEnd(touchPoint:TouchPoint):void - { - var ending:Boolean = (_slopPassed && _trackingPointsCount == minTouchPointsCount); - _forgetPoint(touchPoint); - - _updateCentralPoint(); - - if (ending) - { - _reset(); - _dispatch(new DragGestureEvent(DragGestureEvent.GESTURE_DRAG, true, false, GesturePhase.END, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y, 1, 1, 0, 0, 0)); - } - } - } -} \ No newline at end of file diff --git a/src/org/gestouch/gestures/Gesture.as b/src/org/gestouch/gestures/Gesture.as index 469bb8e..c3b9b7f 100644 --- a/src/org/gestouch/gestures/Gesture.as +++ b/src/org/gestouch/gestures/Gesture.as @@ -1,30 +1,33 @@ package org.gestouch.gestures { + import org.gestouch.core.GestureState; import org.gestouch.core.GesturesManager; - import org.gestouch.core.IGesture; - import org.gestouch.core.TouchPoint; + import org.gestouch.core.IGestureDelegate; + import org.gestouch.core.IGesturesManager; + import org.gestouch.core.ITouchesManager; + import org.gestouch.core.Touch; + import org.gestouch.core.TouchesManager; import org.gestouch.core.gestouch_internal; - import org.gestouch.events.GestureTrackingEvent; + import org.gestouch.events.GestureStateEvent; - import flash.display.DisplayObjectContainer; import flash.display.InteractiveObject; - import flash.errors.IllegalOperationError; import flash.events.EventDispatcher; - import flash.events.GestureEvent; - import flash.events.TouchEvent; import flash.geom.Point; import flash.system.Capabilities; + import flash.utils.Dictionary; - [Event(name="gestureTrackingBegin", type="org.gestouch.events.GestureTrackingEvent")] - [Event(name="gestureTrackingEnd", type="org.gestouch.events.GestureTrackingEvent")] + [Event(name="stateChange", type="org.gestouch.events.GestureStateEvent")] /** * Base class for all gestures. Gesture is essentially a detector that tracks touch points * in order detect specific gesture motion and form gesture event on target. * + * TODO: + * - + * * @author Pavel fljot */ - public class Gesture extends EventDispatcher implements IGesture + public class Gesture extends EventDispatcher { /** * Threshold for screen distance they must move to count as valid input @@ -33,81 +36,32 @@ package org.gestouch.gestures */ public static const DEFAULT_SLOP:uint = Math.round(20 / 252 * flash.system.Capabilities.screenDPI); - /** - * Array of configuration properties (Strings). - */ - protected var _propertyNames:Array = ["minTouchPointsCount", "maxTouchPointsCount"]; + + public var delegate:IGestureDelegate; + + protected const _touchesManager:ITouchesManager = TouchesManager.getInstance(); + protected const _gesturesManager:IGesturesManager = GesturesManager.getInstance(); /** * Map (generic object) of tracking touch points, where keys are touch points IDs. */ - protected var _trackingPointsMap:Object = {}; - protected var _trackingPointsCount:int = 0; - protected var _firstTouchPoint:TouchPoint; - protected var _lastLocalCentralPoint:Point; + protected var _touchesMap:Object = {}; + protected var _centralPoint:Point = new Point(); + protected var _localLocation:Point; + /** + * List of gesture we require to fail. + * @see requireGestureToFail() + */ + protected var _gesturesToFail:Dictionary = new Dictionary(true); + protected var _pendingRecognizedState:uint; - public function Gesture(target:InteractiveObject = null, settings:Object = null) + public function Gesture(target:InteractiveObject = null) { - // Check if gesture reflects it's class properly - reflect(); - - _preinit(); + super(); - GesturesManager.gestouch_internal::addGesture(this); + preinit(); this.target = target; - - if (settings != null) - { - _parseSettings(settings); - } - } - - - /** @private */ - private var _minTouchPointsCount:uint = 1; - /** - * Minimum amount of touch points required for gesture. - * - * @default 1 - */ - public function get minTouchPointsCount():uint - { - return _minTouchPointsCount; - } - public function set minTouchPointsCount(value:uint):void - { - if (_minTouchPointsCount == value) return; - - _minTouchPointsCount = value; - if (maxTouchPointsCount < minTouchPointsCount) - { - maxTouchPointsCount = minTouchPointsCount; - } - } - - - /** @private */ - private var _maxTouchPointsCount:uint = 1; - - /** - * Maximum amount of touch points required for gesture. - * - * @default 1 - */ - public function get maxTouchPointsCount():uint - { - return _maxTouchPointsCount; - } - public function set maxTouchPointsCount(value:uint):void - { - if (value < minTouchPointsCount) - { - throw new IllegalOperationError("maxTouchPointsCount can not be less then minTouchPointsCount"); - } - if (_maxTouchPointsCount == value) return; - - _maxTouchPointsCount = value; } @@ -131,72 +85,78 @@ package org.gestouch.gestures } public function set target(value:InteractiveObject):void { - if (target == value) return; + if (_target == value) + return; - GesturesManager.gestouch_internal::updateGestureTarget(this, target, value); - - // if GesturesManager hasn't thrown any error we can safely continue - - _uninstallTarget(target); + uninstallTarget(target); _target = value; - _installTarget(target); + installTarget(target); } - /** - * Storage for the trackingPoints property. + /** @private */ + protected var _enabled:Boolean = true; + + /** + * @default true */ - protected var _trackingPoints:Vector.For the most gestures these points are which on top of the target.
- * - * @see #isTracking() - * @see #shouldTrackPoint() - */ - public function get trackingPoints():Vector.Designed for multitouch gestures, where center could be used for - * approximation or anchor. Use _adjustCentralPoint() method for updating centralPoint.
- * - * @see #_adjustCentralPoint() */ - public function get centralPoint():TouchPoint + public function get location():Point { - return _centralPoint; + //TODO: to clone or not clone? performance & convention or ... + return _location.clone(); } + //-------------------------------------------------------------------------- // // Public methods // //-------------------------------------------------------------------------- - + [Abstract] /** * Reflects gesture class (for better perfomance). @@ -211,44 +171,9 @@ package org.gestouch.gestures } - /** - * Used by GesturesManager to check wether this gesture is interested in - * tracking this touch point upon this event (of type TouchEvent.TOUCH_BEGIN). - * - *Most of the gestures check, if event.target is target or target contains event.target.
- * - *No need to use it directly.
- * - * @see org.gestouch.core.GesturesManager - * @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/TouchEvent.html - */ - public function shouldTrackPoint(event:TouchEvent, tp:TouchPoint):Boolean + public function isTrackingTouch(touchID:uint):Boolean { - // No need to track more points than we need - if (_trackingPointsCount == maxTouchPointsCount) - { - return false; - } - //By default gesture is interested only in those touchpoints on top of target - var touchTarget:InteractiveObject = event.target as InteractiveObject; - if (touchTarget != target && !(target is DisplayObjectContainer && (target as DisplayObjectContainer).contains(touchTarget))) - { - return false; - } - - return true; - } - - - /** - * Used by GesturesManager to check wether this gesture is tracking this touch point. - * (Not to invoke onTouchBegin, onTouchMove and onTouchEnd methods with no need) - * - * @see org.gestouch.core.GesturesManager - */ - public function isTracking(touchPointID:uint):Boolean - { - return (_trackingPointsMap[touchPointID] === true); + return (_touchesMap[touchID] != undefined); } @@ -257,23 +182,22 @@ package org.gestouch.gestures * *Could be useful to "stop" gesture for the current interaction cycle.
*/ - public function cancel():void + public function reset():void { - GesturesManager.gestouch_internal::cancelGesture(this); - } - - - /** - * TODO: write description, decide wethere this API is good. - */ - public function pickAndContinue(gesture:IGesture):void - { - GesturesManager.gestouch_internal::addCurrentGesture(this); + //FIXME: proper state change? + _location.x = 0; + _location.y = 0; + _touchesMap = {}; + _touchesCount = 0; - for each (var tp:TouchPoint in gesture.trackingPoints) + for (var key:* in _gesturesToFail) { - onTouchBegin(tp); + var gestureToFail:Gesture = key as Gesture; + gestureToFail.removeEventListener(GestureStateEvent.STATE_CHANGE, gestureToFail_stateChangeHandler); } + _pendingRecognizedState = 0; + + setState(GestureState.IDLE); } @@ -284,69 +208,35 @@ package org.gestouch.gestures */ public function dispose():void { - _reset(); + //TODO + reset(); target = null; - try - { - GesturesManager.gestouch_internal::removeGesture(this); - } - catch (err:Error) - { - // do nothing - // GesturesManager may throw Error if this gesture is already removed: - // in case dispose() is called by GesturesManager upon GestureClass.remove(target) - - // this part smells a bit, eh? - } + _gesturesToFail = null; } - [Abstract] - /** - * Internal method, used by GesturesManager. - * - *NB! This is abstract method and must be overridden.
- */ - public function onTouchBegin(touchPoint:TouchPoint):void + public function canBePreventedByGesture(preventingGesture:Gesture):Boolean { - + return true; } - [Abstract] - /** - * Internal method, used by GesturesManager. - * - *NB! This is abstract method and must be overridden.
- */ - public function onTouchMove(touchPoint:TouchPoint):void + public function canPreventGesture(preventedGesture:Gesture):Boolean { - } - - - [Abstract] - /** - * Internal method, used by GesturesManager. - * - *NB! This is abstract method and must be overridden.
- */ - public function onTouchEnd(touchPoint:TouchPoint):void - { - + return true; } /** - * Internal method, used by GesturesManager. Called upon gesture is cancelled. - * - * @see #cancel() + * NB! Current implementation is highly experimental! See examples for more info. */ - public function onCancel():void + public function requireGestureToFail(gesture:Gesture):void { - _reset(); + //TODO + _gesturesToFail[gesture] = true; } - + // -------------------------------------------------------------------------- @@ -357,14 +247,8 @@ package org.gestouch.gestures /** * First method, called in constructor. - * - *Good place to put gesture configuration related code. For example (abstract):
- *You should remove all listeners from target here.
+ * * @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html */ - protected function _uninstallTarget(target:InteractiveObject):void + protected function uninstallTarget(target:InteractiveObject):void { - + if (target) + { + _gesturesManager.gestouch_internal::removeGesture(this); + } } /** - * Dispatches gesture event on gesture and on target. - * - *Why dispatching event on gesture? Because it make sense to dispatch event from - * detector object (gesture) and we can add [Event] metatag for better autocompletion.
- * - *Why dispatching event on target? Becase it supposed to be like this in - * comparsion to native way, and it also make sense as similar to mouse and touch events.
- * - * @param event GestureEvent to be dispatched - * - * @see http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/GestureEvent.html + * TODO: clarify usage. For now it's supported to call this method in onTouchBegin with return. */ - protected function _dispatch(event:GestureEvent):void + protected function ignoreTouch(touch:Touch):void { - if (hasEventListener(event.type)) + if (_touchesMap.hasOwnProperty(touch.id)) { - dispatchEvent(event); + delete _touchesMap[touch.id]; + _touchesCount--; + } + } + + + [Abstract] + /** + *NB! This is abstract method and must be overridden.
+ */ + protected function onTouchBegin(touch:Touch):void + { + } + + + [Abstract] + /** + *NB! This is abstract method and must be overridden.
+ */ + protected function onTouchMove(touch:Touch):void + { + } + + + [Abstract] + /** + *NB! This is abstract method and must be overridden.
+ */ + protected function onTouchEnd(touch:Touch):void + { + } + + + protected function setState(newState:uint):Boolean + { + if (_state == newState && _state == GestureState.CHANGED) + { + return true; } - // event is almost always bubbles, so no point for optimization - target.dispatchEvent(event); - } - - - /** - * Parses settings and configures the gesture. - * - * @param settings Generic object with configuration properties - */ - protected function _parseSettings(settings:Object):void - { - for each (var propertyName:String in _propertyNames) + //TODO: is state sequence validation needed? e.g.: + //POSSIBLE should be followed by BEGAN or RECOGNIZED or FAILED + //BEGAN should be follwed by CHANGED or ENDED or CANCELLED + //CHANGED should be followed by CHANGED or ENDED or CANCELLED + //... + + if (newState == GestureState.BEGAN || newState == GestureState.RECOGNIZED) { - if (settings.hasOwnProperty(propertyName)) + var gestureToFail:Gesture; + // first we check if other required-to-fail gestures recognized + // TODO: is this really necessary? using "requireGestureToFail" API assume that + // required-to-fail gesture always recognizes AFTER this one. + for (var key:* in _gesturesToFail) { - this[propertyName] = settings[propertyName]; + gestureToFail = key as Gesture; + if (gestureToFail.state != GestureState.IDLE && gestureToFail.state != GestureState.POSSIBLE + && gestureToFail.state != GestureState.FAILED) + { + // Looks like other gesture won't fail, + // which means the required condition will not happen, so we must fail + setState(GestureState.FAILED); + return false; + } + } + // then we check of other required-to-fail gestures are actually tracked (not IDLE) + // and not still not recognized (e.g. POSSIBLE state) + for (key in _gesturesToFail) + { + gestureToFail = key as Gesture; + if (gestureToFail.state == GestureState.POSSIBLE) + { + // Other gesture might fail soon, so we postpone state change + _pendingRecognizedState = newState; + return false; + } + // else if gesture is in IDLE state it means it doesn't track anything, + // so we simply ignore it as it doesn't seem like conflict from this perspective + // (perspective of using "requireGestureToFail" API) + } + + + if (delegate && !delegate.gestureShouldBegin(this)) + { + setState(GestureState.FAILED); + return false; } } + + var oldState:uint = _state; + _state = newState; + + if (((GestureState.CANCELLED | GestureState.RECOGNIZED | GestureState.ENDED | GestureState.FAILED) & _state) > 0) + { + _gesturesManager.gestouch_internal::scheduleGestureStateReset(this); + } + + //TODO: what if RTE happens in event handlers? + + if (hasEventListener(GestureStateEvent.STATE_CHANGE)) + { + dispatchEvent(new GestureStateEvent(GestureStateEvent.STATE_CHANGE, _state, oldState)); + } + + if (_state == GestureState.BEGAN || _state == GestureState.RECOGNIZED) + { + _gesturesManager.gestouch_internal::onGestureRecognized(this); + } + + return true; } - /** - * Saves touchPoint for tracking for the current gesture cycle. - * - *If this is the first touch point, it updates _firstTouchPoint and _centralPoint.
- * - * @see #_firstTouchPoint - * @see #centralPoint - * @see #trackingPointsCount - */ - protected function _trackPoint(touchPoint:TouchPoint):void + gestouch_internal function setState_internal(state:uint):void { - _trackingPointsMap[touchPoint.id] = true; - var index:uint = _trackingPoints.push(touchPoint); - _trackingPointsCount++; - if (index == 1) - { - _firstTouchPoint = touchPoint; - _centralPoint = touchPoint.clone() as TouchPoint; - } - else if (_trackingPointsCount == minTouchPointsCount) - { - _updateCentralPoint(); - _centralPoint.touchBeginPos.x = _centralPoint.x; - _centralPoint.touchBeginPos.y = _centralPoint.y; - _centralPoint.moveOffset.x = 0; - _centralPoint.moveOffset.y = 0; - _centralPoint.lastMove.x = 0; - _centralPoint.lastMove.y = 0; - } - else if (_trackingPointsCount > minTouchPointsCount) - { - _adjustCentralPoint(); - } - - if (_trackingPointsCount == minTouchPointsCount) - { - if (hasEventListener(GestureTrackingEvent.GESTURE_TRACKING_BEGIN)) - { - dispatchEvent(new GestureTrackingEvent(GestureTrackingEvent.GESTURE_TRACKING_BEGIN)); - } - } + setState(state); } - /** - * Removes touchPoint from the list of tracking points. - * - *If this is the first touch point, it updates _firstTouchPoint and _centralPoint.
- * - * @see #trackingPoints - * @see #_trackingPointsMap - * @see #trackingPointsCount - */ - protected function _forgetPoint(touchPoint:TouchPoint):void - { - delete _trackingPointsMap[touchPoint.id]; - _trackingPoints.splice(_trackingPoints.indexOf(touchPoint), 1); - _trackingPointsCount--; - - _adjustCentralPoint(); - - if (_trackingPointsCount == minTouchPointsCount + 1) - { - if (hasEventListener(GestureTrackingEvent.GESTURE_TRACKING_END)) - { - dispatchEvent(new GestureTrackingEvent(GestureTrackingEvent.GESTURE_TRACKING_END)); - } - } - } - - - /** - * Updates _centralPoint and all it's properties - * (such as positions, offsets, velocity, etc...). - * Also updates _lastLocalCentralPoint (used for dispatching events). - * - * @see #centralPoint - * @see #_lastLocalCentralPoint - * @see #trackingPoints - */ - protected function _updateCentralPoint():void + protected function updateCentralPoint():void { + var touchLocation:Point; var x:Number = 0; var y:Number = 0; - var velX:Number = 0; - var velY:Number = 0; - for each (var tp:TouchPoint in _trackingPoints) + for (var touchID:String in _touchesMap) { - x += tp.x; - y += tp.y; - velX += tp.velocity.x; - velY += tp.velocity.y; + touchLocation = (_touchesMap[int(touchID)] as Touch).location; + x += touchLocation.x; + y += touchLocation.y; } - x /= _trackingPointsCount; - y /= _trackingPointsCount; - var lastMoveX:Number = x - _centralPoint.x; - var lastMoveY:Number = y - _centralPoint.y; - velX /= _trackingPointsCount; - velY /= _trackingPointsCount; - - _centralPoint.x = x; - _centralPoint.y = y; - _centralPoint.lastMove.x = lastMoveX; - _centralPoint.lastMove.y = lastMoveY; - _centralPoint.velocity.x = velX; - _centralPoint.velocity.y = velY; - // tp.moveOffset = tp.subtract(tp.touchBeginPos); - _centralPoint.moveOffset.x = x - _centralPoint.touchBeginPos.x; - _centralPoint.moveOffset.y = y - _centralPoint.touchBeginPos.y; - - _lastLocalCentralPoint = target.globalToLocal(_centralPoint); + _centralPoint.x = x / _touchesCount; + _centralPoint.y = y / _touchesCount; } - - - protected function _adjustCentralPoint():void + + + protected function updateLocation():void { - var oldCentralPoint:TouchPoint = _centralPoint.clone() as TouchPoint; - _updateCentralPoint(); - var centralPointChange:Point = _centralPoint.subtract(oldCentralPoint); - _centralPoint.touchBeginPos = _centralPoint.touchBeginPos.add(centralPointChange); - // fix moveOffset according to fixed touchBeginPos - _centralPoint.moveOffset.x = _centralPoint.x - _centralPoint.touchBeginPos.x; - _centralPoint.moveOffset.y = _centralPoint.y - _centralPoint.touchBeginPos.y; - // restore original lastMove - _centralPoint.lastMove.x = oldCentralPoint.lastMove.x; - _centralPoint.lastMove.y = oldCentralPoint.lastMove.y; + updateCentralPoint(); + _location.x = _centralPoint.x; + _location.y = _centralPoint.y; + _localLocation = target.globalToLocal(_location); } /** - * Reset data for the current tracking (interaction) cycle. - * - *Clears up _trackingPointsMap, _trackingPoints, _trackingPointsCount - * and other custom gestures-specific things.
- * - *Generally invoked in onCancel method and when certain conditions of gesture - * have been failed and gesture doesn't need to continue processsing - * (e.g. timer has completed in DoubleTapGesture)
- * - * @see #trackingPoints - * @see #trackingPointsCount - * @see #onCancel() + * Executed once requiredToFail gestures have been failed and + * pending (delayed) recognized state has been entered. + * You must dispatch gesture event here. */ - protected function _reset():void + protected function onDelayedRecognize():void { - // forget all touch points - _trackingPointsMap = {}; - _trackingPoints.length = 0; - _trackingPointsCount = 0; + + } + + + + + //-------------------------------------------------------------------------- + // + // Event handlers + // + //-------------------------------------------------------------------------- + + gestouch_internal function touchBeginHandler(touch:Touch):void + { + _touchesMap[touch.id] = touch; + _touchesCount++; + + onTouchBegin(touch); + + if (_touchesCount == 1 && state == GestureState.IDLE) + { + for (var key:* in _gesturesToFail) + { + var gestureToFail:Gesture = key as Gesture; + gestureToFail.addEventListener(GestureStateEvent.STATE_CHANGE, gestureToFail_stateChangeHandler, false, 0, true); + } + + setState(GestureState.POSSIBLE); + } + } + + + gestouch_internal function touchMoveHandler(touch:Touch):void + { + _touchesMap[touch.id] = touch; + onTouchMove(touch); + } + + + gestouch_internal function touchEndHandler(touch:Touch):void + { + delete _touchesMap[touch.id]; + _touchesCount--; + + onTouchEnd(touch); + } + + + protected function gestureToFail_stateChangeHandler(event:GestureStateEvent):void + { + if (state != GestureState.POSSIBLE) + return;//just in case..FIXME? + + if (!_pendingRecognizedState) + return; + + if (event.newState == GestureState.FAILED) + { + for (var key:* in _gesturesToFail) + { + var gestureToFail:Gesture = key as Gesture; + if (gestureToFail.state == GestureState.POSSIBLE) + { + // we're still waiting for some gesture to fail + return; + } + } + + if (setState(_pendingRecognizedState)) + { + onDelayedRecognize(); + } + } + else if (event.newState != GestureState.POSSIBLE) + { + setState(GestureState.FAILED); + } } } } \ No newline at end of file diff --git a/src/org/gestouch/gestures/LongPressGesture.as b/src/org/gestouch/gestures/LongPressGesture.as index 070069b..932aa17 100644 --- a/src/org/gestouch/gestures/LongPressGesture.as +++ b/src/org/gestouch/gestures/LongPressGesture.as @@ -1,149 +1,164 @@ package org.gestouch.gestures { - import org.gestouch.core.GesturesManager; - import org.gestouch.core.TouchPoint; - import org.gestouch.core.gestouch_internal; + import org.gestouch.core.GestureState; + import org.gestouch.core.Touch; import org.gestouch.events.LongPressGestureEvent; import flash.display.InteractiveObject; - import flash.events.GesturePhase; import flash.events.TimerEvent; import flash.utils.Timer; - [Event(name="gestureLongPress", type="org.gestouch.events.LongPressGestureEvent")] /** - * + * TODO: -location + * - check on iOS (Obj-C) what happens when numTouchesRequired=2, two finger down, then quickly release one. * * @author Pavel fljot */ public class LongPressGesture extends Gesture { + public var numTouchesRequired:uint = 1; /** - * Default value 1000ms - */ - public var timeThreshold:uint = 500; - /** - * Deafult value is Gesture.DEFAULT_SLOP - * @see org.gestouchers.core.Gesture#DEFAULT_SLOP - */ + * The minimum time interval in millisecond fingers must press on the target for the gesture to be recognized. + * + * @default 500 + */ + public var minPressDuration:uint = 500; public var slop:Number = Gesture.DEFAULT_SLOP; - protected var _thresholdTimer:Timer; + protected var _timer:Timer; + protected var _numTouchesRequiredReached:Boolean; - public function LongPressGesture(target:InteractiveObject = null, settings:Object = null) + public function LongPressGesture(target:InteractiveObject = null) { - super(target, settings); + super(target); } - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // - // Static methods + // Public methods // - //-------------------------------------------------------------------------- - - public static function add(target:InteractiveObject, settings:Object = null):LongPressGesture - { - return new LongPressGesture(target, settings); - } - - - public static function remove(target:InteractiveObject):LongPressGesture - { - return GesturesManager.gestouch_internal::removeGestureByTarget(LongPressGesture, target) as LongPressGesture; - } - - - - - //-------------------------------------------------------------------------- - // - // Public methods - // - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- override public function reflect():Class { - return LongPressGesture; + return TapGesture; + } + + + override public function reset():void + { + super.reset(); + + _numTouchesRequiredReached = false; + _timer.reset(); } - override public function onTouchBegin(touchPoint:TouchPoint):void + + + // -------------------------------------------------------------------------- + // + // Protected methods + // + // -------------------------------------------------------------------------- + + override protected function preinit():void { - // No need to track more points than we need - if (_trackingPointsCount == maxTouchPointsCount) + super.preinit(); + + _timer = new Timer(minPressDuration, 1); + _timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_timerCompleteHandler); + } + + + override protected function onTouchBegin(touch:Touch):void + { + if (touchesCount > numTouchesRequired) { + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + ignoreTouch(touch); + } + else + { + setState(GestureState.FAILED); + } return; } - _trackPoint(touchPoint); - - if (_trackingPointsCount == minTouchPointsCount) + if (touchesCount == numTouchesRequired) { - _thresholdTimer.reset(); - _thresholdTimer.delay = timeThreshold; - _thresholdTimer.start(); - } - } - - - override public function onTouchMove(touchPoint:TouchPoint):void - { - // faster isNaN - if (_thresholdTimer.currentCount == 0 && slop == slop) - { - if (touchPoint.moveOffset.length > slop) + _numTouchesRequiredReached = true; + _timer.reset(); + _timer.delay = minPressDuration; + if (minPressDuration > 0) { - cancel(); + _timer.start(); + } + else + { + timer_timerCompleteHandler(); } } } - override public function onTouchEnd(touchPoint:TouchPoint):void - { - _forgetPoint(touchPoint); - - var held:Boolean = (_thresholdTimer.currentCount > 0); - _thresholdTimer.reset(); - - if (held) + override protected function onTouchMove(touch:Touch):void + { + if (state == GestureState.POSSIBLE && slop > 0 && touch.locationOffset.length > slop) { - _updateCentralPoint(); - _reset(); - _dispatch(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, true, false, GesturePhase.END, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y)); + setState(GestureState.FAILED); + } + else if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + updateLocation(); + if (setState(GestureState.CHANGED) && hasEventListener(LongPressGestureEvent.GESTURE_LONG_PRESS)) + { + dispatchEvent(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GestureState.CHANGED, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } } } - - - //-------------------------------------------------------------------------- - // - // Protected methods - // - //-------------------------------------------------------------------------- - - override protected function _preinit():void + override protected function onTouchEnd(touch:Touch):void { - super._preinit(); - - _thresholdTimer = new Timer(timeThreshold, 1); - _thresholdTimer.addEventListener(TimerEvent.TIMER_COMPLETE, _onThresholdTimerComplete); - - _propertyNames.push("timeThreshold", "slop"); + //TODO: check proper condition (behavior) on iOS native + if (_numTouchesRequiredReached) + { + if (((GestureState.BEGAN | GestureState.CHANGED) & state) > 0) + { + updateLocation(); + if (setState(GestureState.ENDED) && hasEventListener(LongPressGestureEvent.GESTURE_LONG_PRESS)) + { + dispatchEvent(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GestureState.ENDED, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } + } + else + { + setState(GestureState.FAILED); + } + } + else + { + setState(GestureState.FAILED); + } } - override protected function _reset():void + override protected function onDelayedRecognize():void { - super._reset(); - - _thresholdTimer.reset(); + if (hasEventListener(LongPressGestureEvent.GESTURE_LONG_PRESS)) + { + dispatchEvent(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GestureState.BEGAN, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } } @@ -154,11 +169,18 @@ package org.gestouch.gestures // Event handlers // //-------------------------------------------------------------------------- - - protected function _onThresholdTimerComplete(event:TimerEvent):void + + protected function timer_timerCompleteHandler(event:TimerEvent = null):void { - _updateCentralPoint(); - _dispatch(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, true, false, GesturePhase.BEGIN, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y)); + if (state == GestureState.POSSIBLE) + { + updateLocation(); + if (setState(GestureState.BEGAN) && hasEventListener(LongPressGestureEvent.GESTURE_LONG_PRESS)) + { + dispatchEvent(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GestureState.BEGAN, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } + } } } } \ No newline at end of file diff --git a/src/org/gestouch/gestures/MovingGestureBase.as b/src/org/gestouch/gestures/MovingGestureBase.as deleted file mode 100644 index ae55d1d..0000000 --- a/src/org/gestouch/gestures/MovingGestureBase.as +++ /dev/null @@ -1,178 +0,0 @@ -package org.gestouch.gestures -{ - import flash.display.InteractiveObject; - import flash.geom.Point; - import org.gestouch.Direction; - - - - /** - * Base class for those gestures where you have to move finger/mouse, - * i.e. DragGesture, SwipeGesture - * - * @author Pavel fljot - */ - public class MovingGestureBase extends Gesture - { - /** - * Threshold for screen distance they must move to count as valid input - * (not an accidental offset on touch). Once this distance is passed, - * gesture starts more intensive and specific processing in onTouchMove() method. - * - * @default Gesture.DEFAULT_SLOP - * - * @see org.gestouch.gestures.Gesture#DEFAULT_SLOP - */ - public var slop:Number = Gesture.DEFAULT_SLOP; - - protected var _slopPassed:Boolean = false; - protected var _canMoveHorizontally:Boolean = true; - protected var _canMoveVertically:Boolean = true; - - - public function MovingGestureBase(target:InteractiveObject = null, settings:Object = null) - { - super(target, settings); - - if (reflect() == MovingGestureBase) - { - dispose(); - throw new Error("This is abstract class and cannot be instantiated."); - } - } - - - /** - * @private - * Storage for direction property. - */ - protected var _direction:String = Direction.ALL; - - - /** - * Allowed direction for this gesture. Used to determine slop overcome - * and could be used for specific calculations (as in SwipeGesture for example). - * - * @default Direction.ALL - * - * @see org.gestouch.Direction - * @see org.gestouch.gestures.SwipeGesture - */ - public function get direction():String - { - return _direction; - } - public function set direction(value:String):void - { - if (_direction == value) return; - - _validateDirection(value); - - _direction = value; - - _canMoveHorizontally = (_direction != Direction.VERTICAL); - _canMoveVertically = (_direction != Direction.HORIZONTAL); - } - - - - - //-------------------------------------------------------------------------- - // - // Protected methods - // - //-------------------------------------------------------------------------- - - override protected function _preinit():void - { - super._preinit(); - - _propertyNames.push("slop", "direction"); - } - - - override protected function _reset():void - { - super._reset(); - - _slopPassed = false; - } - - - /** - * Validates direction property (in setter) to help - * developer prevent accidental mistake (Strings suck). - * - * @see org.gestouch.Direction - */ - protected function _validateDirection(value:String):void - { - if (value != Direction.HORIZONTAL && - value != Direction.VERTICAL && - value != Direction.STRAIGHT_AXES && - value != Direction.DIAGONAL_AXES && - value != Direction.OCTO && - value != Direction.ALL) - { - throw new ArgumentError("Invalid direction value \"" + value + "\"."); - } - } - - - /** - * Checks wether slop has been overcome. - * Typically used in onTouchMove() method. - * - * @param moveDelta offset of touch point / central point - * starting from beginning of interaction cycle. - * - * @see #onTouchMove() - */ - protected function _checkSlop(moveDelta:Point):Boolean - { - var slopPassed:Boolean = false; - if (!(slop > 0)) - { - // return true immideately if slop is 0 or NaN - return true; - } - - if (_canMoveHorizontally && _canMoveVertically) - { - slopPassed = moveDelta.length > slop; - } - else if (_canMoveHorizontally) - { - slopPassed = Math.abs(moveDelta.x) > slop; - } - else if (_canMoveVertically) - { - slopPassed = Math.abs(moveDelta.y) > slop; - } - - if (slopPassed) - { - var slopVector:Point; - if (_canMoveHorizontally && _canMoveVertically) - { - slopVector = moveDelta.clone(); - slopVector.normalize(slop); - slopVector.x = Math.round(slopVector.x); - slopVector.y = Math.round(slopVector.y); - } - else if (_canMoveHorizontally) - { - slopVector = new Point(moveDelta.x >= slop ? slop : -slop, 0); - } - else if (_canMoveVertically) - { - slopVector = new Point(0, moveDelta.y >= slop ? slop : -slop); - } -// _gestureAnchorPoint = _touchPoint.add(slopVector); -// startGestureTrack(); - } - - return slopPassed; - } - } -} \ No newline at end of file diff --git a/src/org/gestouch/gestures/PanGesture.as b/src/org/gestouch/gestures/PanGesture.as new file mode 100644 index 0000000..ddf74bf --- /dev/null +++ b/src/org/gestouch/gestures/PanGesture.as @@ -0,0 +1,218 @@ +package org.gestouch.gestures +{ + import org.gestouch.core.GestureState; + import org.gestouch.core.Touch; + import org.gestouch.events.PanGestureEvent; + + import flash.display.InteractiveObject; + import flash.geom.Point; + + [Event(name="gesturePan", type="org.gestouch.events.PanGestureEvent")] + /** + * TODO: + * -location + * -check native behavior on iDevice + * + * @author Pavel fljot + */ + public class PanGesture extends Gesture + { + public var slop:Number = Gesture.DEFAULT_SLOP; + /** + * Used for initial slop overcome calculations only. + */ + public var direction:uint = PanGestureDirection.NO_DIRECTION; + + protected var _gestureBeginOffsetX:Number; + protected var _gestureBeginOffsetY:Number; + + + public function PanGesture(target:InteractiveObject = null) + { + super(target); + } + + + /** @private */ + private var _maxNumTouchesRequired:uint = 1; + + /** + * + */ + public function get maxNumTouchesRequired():uint + { + return _maxNumTouchesRequired; + } + public function set maxNumTouchesRequired(value:uint):void + { + if (_maxNumTouchesRequired == value) + return; + + if (value < minNumTouchesRequired) + throw ArgumentError("maxNumTouchesRequired must be not less then minNumTouchesRequired"); + + _maxNumTouchesRequired = value; + } + + + /** @private */ + private var _minNumTouchesRequired:uint = 1; + + /** + * + */ + public function get minNumTouchesRequired():uint + { + return _minNumTouchesRequired; + } + public function set minNumTouchesRequired(value:uint):void + { + if (_minNumTouchesRequired == value) + return; + + if (value > maxNumTouchesRequired) + throw ArgumentError("minNumTouchesRequired must be not greater then maxNumTouchesRequired"); + + _minNumTouchesRequired = value; + } + + + + + // -------------------------------------------------------------------------- + // + // Public methods + // + // -------------------------------------------------------------------------- + + override public function reflect():Class + { + return PanGesture; + } + + + override public function reset():void + { + _gestureBeginOffsetX = NaN; + _gestureBeginOffsetY = NaN; + + super.reset(); + } + + + + + // -------------------------------------------------------------------------- + // + // Protected methods + // + // -------------------------------------------------------------------------- + + override protected function onTouchBegin(touch:Touch):void + { + if (touchesCount > maxNumTouchesRequired) + { + //TODO + ignoreTouch(touch); + return; + } + + if (touchesCount >= minNumTouchesRequired) + { + updateLocation(); + } + } + + + override protected function onTouchMove(touch:Touch):void + { + if (touchesCount < minNumTouchesRequired) + return; + + var prevLocationX:Number; + var prevLocationY:Number; + var offsetX:Number; + var offsetY:Number; + + if (state == GestureState.POSSIBLE) + { + // Check if finger moved enough for gesture to be recognized + 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; + updateLocation(); + offsetX = _location.x - prevLocationX; + offsetY = _location.y - prevLocationY; + // acummulate begin offsets for the case when this gesture recognition is delayed by requireGestureToFail + _gestureBeginOffsetX = (_gestureBeginOffsetX != _gestureBeginOffsetX) ? offsetX : _gestureBeginOffsetX + offsetX; + _gestureBeginOffsetY = (_gestureBeginOffsetY != _gestureBeginOffsetY) ? offsetY : _gestureBeginOffsetY + offsetY; + + if (setState(GestureState.BEGAN) && hasEventListener(PanGestureEvent.GESTURE_PAN)) + { + dispatchEvent(new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GestureState.BEGAN, + _location.x, _location.y, _localLocation.x, _localLocation.y, offsetX, offsetY)); + } + } + } + else if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + prevLocationX = _location.x; + prevLocationY = _location.y; + updateLocation(); + offsetX = _location.x - prevLocationX; + offsetY = _location.y - prevLocationY; + + if (setState(GestureState.CHANGED) && hasEventListener(PanGestureEvent.GESTURE_PAN)) + { + dispatchEvent(new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GestureState.CHANGED, + _location.x, _location.y, _localLocation.x, _localLocation.y, offsetX, offsetY)); + } + } + } + + + override protected function onTouchEnd(touch:Touch):void + { + if (touchesCount < minNumTouchesRequired) + { + if (state == GestureState.POSSIBLE) + { + setState(GestureState.FAILED); + } + else + { + if (setState(GestureState.ENDED) && hasEventListener(PanGestureEvent.GESTURE_PAN)) + { + dispatchEvent(new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GestureState.ENDED, + _location.x, _location.y, _localLocation.x, _localLocation.y, 0, 0)); + } + } + } + else + { + updateLocation(); + } + } + + + override protected function onDelayedRecognize():void + { + if (hasEventListener(PanGestureEvent.GESTURE_PAN)) + { + dispatchEvent(new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GestureState.BEGAN, + _location.x, _location.y, _localLocation.x, _localLocation.y, _gestureBeginOffsetX, _gestureBeginOffsetY)); + } + } + } +} \ No newline at end of file diff --git a/src/org/gestouch/gestures/PanGestureDirection.as b/src/org/gestouch/gestures/PanGestureDirection.as new file mode 100644 index 0000000..8925601 --- /dev/null +++ b/src/org/gestouch/gestures/PanGestureDirection.as @@ -0,0 +1,12 @@ +package org.gestouch.gestures +{ + /** + * @author Pavel fljot + */ + public class PanGestureDirection + { + public static const NO_DIRECTION:uint = 0; + public static const VERTICAL:uint = 1 << 0; + public static const HORIZONTAL:uint = 1 << 1; + } +} \ No newline at end of file diff --git a/src/org/gestouch/gestures/RotateGesture.as b/src/org/gestouch/gestures/RotateGesture.as index bf8d552..da1665b 100644 --- a/src/org/gestouch/gestures/RotateGesture.as +++ b/src/org/gestouch/gestures/RotateGesture.as @@ -1,68 +1,42 @@ package org.gestouch.gestures { - import org.gestouch.GestureUtils; - import org.gestouch.core.GesturesManager; - import org.gestouch.core.TouchPoint; - import org.gestouch.core.gestouch_internal; + import org.gestouch.core.GestureState; + import org.gestouch.core.Touch; import org.gestouch.events.RotateGestureEvent; + import org.gestouch.utils.GestureUtils; import flash.display.InteractiveObject; - import flash.events.GesturePhase; import flash.geom.Point; - [Event(name="gestureRotate", type="org.gestouch.events.RotateGestureEvent")] /** + * TODO: + * -check native behavior on iDevice + * * @author Pavel fljot */ public class RotateGesture extends Gesture - { + { + public var slop:Number = Gesture.DEFAULT_SLOP >> 1; - protected var _currVector:Point = new Point(); - protected var _lastVector:Point = new Point(); + protected var _touch1:Touch; + protected var _touch2:Touch; + protected var _transformVector:Point; - public function RotateGesture(target:InteractiveObject, settings:Object = null) + public function RotateGesture(target:InteractiveObject = null) { - super(target, settings); + super(target); } - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // - // Static methods + // Public methods // - //-------------------------------------------------------------------------- - - public static function add(target:InteractiveObject = null, settings:Object = null):RotateGesture - { - return new RotateGesture(target, settings); - } - - - public static function remove(target:InteractiveObject):RotateGesture - { - return GesturesManager.gestouch_internal::removeGestureByTarget(RotateGesture, target) as RotateGesture; - } - - - - - //-------------------------------------------------------------------------- - // - // Public methods - // - //-------------------------------------------------------------------------- - - override public function onCancel():void - { - super.onCancel(); - - - } - + // -------------------------------------------------------------------------- override public function reflect():Class { @@ -70,70 +44,113 @@ package org.gestouch.gestures } - override public function onTouchBegin(touchPoint:TouchPoint):void + + + // -------------------------------------------------------------------------- + // + // Protected methods + // + // -------------------------------------------------------------------------- + + override protected function onTouchBegin(touch:Touch):void { - // No need to track more points than we need - if (_trackingPointsCount == maxTouchPointsCount) + if (touchesCount > 2) { + //TODO + ignoreTouch(touch); return; } - _trackPoint(touchPoint); - - if (_trackingPointsCount == minTouchPointsCount) + if (touchesCount == 1) { - _lastVector.x = _trackingPoints[1].x - _trackingPoints[0].x; - _lastVector.y = _trackingPoints[1].y - _trackingPoints[0].y; + _touch1 = touch; + } + else + { + _touch2 = touch; - _updateCentralPoint(); - - _dispatch(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, true, false, GesturePhase.BEGIN, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y)); + _transformVector = _touch2.location.subtract(_touch1.location); } } - override public function onTouchMove(touchPoint:TouchPoint):void + override protected function onTouchMove(touch:Touch):void { - // do calculations only when we track enough points - if (_trackingPointsCount < minTouchPointsCount) - { + if (touchesCount < 2) return; - } - _updateCentralPoint(); + var recognized:Boolean = true; - _currVector.x = _trackingPoints[1].x - _trackingPoints[0].x; - _currVector.y = _trackingPoints[1].y - _trackingPoints[0].y; - - var a1:Number = Math.atan2(_lastVector.y, _lastVector.x); - var a2:Number = Math.atan2(_currVector.y, _currVector.x); - var angle:Number = a2 - a1; - angle *= GestureUtils.RADIANS_TO_DEGREES; - - _lastVector.x = _currVector.x; - _lastVector.y = _currVector.y; - - _dispatch(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, true, false, GesturePhase.UPDATE, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y, 1, 1, angle)); - } - - - override public function onTouchEnd(touchPoint:TouchPoint):void - { - var ending:Boolean = (_trackingPointsCount == minTouchPointsCount); - _forgetPoint(touchPoint); - - if (ending) + if (state == GestureState.POSSIBLE && slop > 0 && touch.locationOffset.length < slop) { - _dispatch(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, true, false, GesturePhase.END, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y)); + recognized = false; + } + + if (recognized) + { + var currTransformVector:Point = _touch2.location.subtract(_touch1.location); + var rotation:Number = Math.atan2(currTransformVector.y, currTransformVector.x) - Math.atan2(_transformVector.y, _transformVector.x); + rotation *= GestureUtils.RADIANS_TO_DEGREES; + _transformVector.x = currTransformVector.x; + _transformVector.y = currTransformVector.y; + + updateLocation(); + + if (state == GestureState.POSSIBLE) + { + if (setState(GestureState.BEGAN) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GestureState.BEGAN, + _location.x, _location.y, _localLocation.x, _localLocation.y, rotation)); + } + } + else + { + if (setState(GestureState.CHANGED) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GestureState.CHANGED, + _location.x, _location.y, _localLocation.x, _localLocation.y, rotation)); + } + } } } - override protected function _preinit():void + override protected function onTouchEnd(touch:Touch):void { - super._preinit(); - - minTouchPointsCount = 2; + if (touchesCount == 0) + { + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + if (setState(GestureState.ENDED) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GestureState.ENDED, + _location.x, _location.y, _localLocation.x, _localLocation.y, 0)); + } + } + else if (state == GestureState.POSSIBLE) + { + setState(GestureState.FAILED); + } + } + else// == 1 + { + if (touch == _touch1) + { + _touch1 = _touch2; + } + _touch2 = null; + + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + updateLocation(); + if (setState(GestureState.CHANGED) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GestureState.CHANGED, + _location.x, _location.y, _localLocation.x, _localLocation.y, 0)); + } + } + } } } } \ No newline at end of file diff --git a/src/org/gestouch/gestures/SwipeGesture.as b/src/org/gestouch/gestures/SwipeGesture.as index d09d842..9665493 100644 --- a/src/org/gestouch/gestures/SwipeGesture.as +++ b/src/org/gestouch/gestures/SwipeGesture.as @@ -1,180 +1,243 @@ package org.gestouch.gestures { - import org.gestouch.Direction; - import org.gestouch.GestureUtils; - import org.gestouch.core.GesturesManager; - import org.gestouch.core.TouchPoint; - import org.gestouch.core.gestouch_internal; + import org.gestouch.core.GestureState; + import org.gestouch.core.Touch; import org.gestouch.events.SwipeGestureEvent; import flash.display.InteractiveObject; - import flash.events.GesturePhase; import flash.geom.Point; - import flash.utils.getTimer; - [Event(name="gestureSwipe", type="org.gestouch.events.SwipeGestureEvent")] /** - * SwipeGesture detects swipe motion (also known as flick or flig). - * - *I couldn't find any certain definition of Swipe except for it's defined as quick. - * So I've implemented detection via two threshold velocities — one is in the direction of the movement, - * and second is the "side"-one (orthogonal). They form a velocity rectangle, where you have to move - * with a velocity greater then velocityThreshold value and less then sideVelocityThreshold.
+ * TODO: + * -check native behavior on iDevice * * @author Pavel fljot */ - public class SwipeGesture extends MovingGestureBase + public class SwipeGesture extends Gesture { - public var moveThreshold:Number = Gesture.DEFAULT_SLOP; - public var minTimeThreshold:uint = 50; - public var velocityThreshold:Number = 7 * GestureUtils.IPS_TO_PPMS; - public var sideVelocityThreshold:Number = 2 * GestureUtils.IPS_TO_PPMS; + public var slop:Number = Gesture.DEFAULT_SLOP; + public var numTouchesRequired:uint = 1; + public var minVelocity:Number = 0.8; + public var minOffset:Number = Gesture.DEFAULT_SLOP; + public var direction:uint = SwipeGestureDirection.ORTHOGONAL; + public var maxDirectionalOffset:Number = Gesture.DEFAULT_SLOP << 1; - protected var _startTime:uint; + protected var _offset:Point = new Point(); + protected var _startTime:int; + protected var _noDirection:Boolean; + protected var _avrgVel:Point = new Point(); + protected var _prevAvrgVel:Point = new Point(); + protected var _decelerationCounter:uint = 0; - public function SwipeGesture(target:InteractiveObject = null, settings:Object = null) + public function SwipeGesture(target:InteractiveObject = null) { - super(target, settings); + super(target); } - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // - // Static methods + // Public methods // - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- - public static function add(target:InteractiveObject, settings:Object = null):SwipeGesture - { - return new SwipeGesture(target, settings); - } - - - public static function remove(target:InteractiveObject):SwipeGesture - { - return GesturesManager.gestouch_internal::removeGestureByTarget(SwipeGesture, target) as SwipeGesture; - } - - - - - //-------------------------------------------------------------------------- - // - // Public methods - // - //-------------------------------------------------------------------------- - override public function reflect():Class { return SwipeGesture; } - - override public function onTouchBegin(touchPoint:TouchPoint):void - { - // No need to track more points than we need - if (_trackingPointsCount == maxTouchPointsCount) - { - return; - } - _trackPoint(touchPoint); + override public function reset():void + { + _startTime = 0; + _offset.x = 0; + _offset.y = 0; + _decelerationCounter = 0; + + super.reset(); } - override public function onTouchMove(touchPoint:TouchPoint):void + + + // -------------------------------------------------------------------------- + // + // Protected methods + // + // -------------------------------------------------------------------------- + + override protected function onTouchBegin(touch:Touch):void { - // do calculations only when we track enought points - if (_trackingPointsCount < minTouchPointsCount) + if (touchesCount > numTouchesRequired) { + //TODO: or ignore? + setState(GestureState.FAILED); return; } - _updateCentralPoint(); - - if (!_slopPassed) + if (touchesCount == 1) { - _slopPassed = _checkSlop(_centralPoint.moveOffset); + // Because we want to fail as quick as possible + _startTime = touch.time; + } + if (touchesCount == numTouchesRequired) + { + updateLocation(); + _avrgVel.x = _avrgVel.y = 0; + + // cache direction condition for performance + _noDirection = (SwipeGestureDirection.ORTHOGONAL & direction) == 0; + } + } + + + override protected function onTouchMove(touch:Touch):void + { + if (touchesCount < numTouchesRequired) + return; + + var prevCentralPointX:Number = _centralPoint.x; + var prevCentralPointY:Number = _centralPoint.y; + updateCentralPoint(); + + _offset.x = _centralPoint.x - _location.x; + _offset.y = _centralPoint.y - _location.y; + var offsetLength:Number = _offset.length; + + if (offsetLength < slop) + { + // no need in processing - we're in the very beginning of movement + return; } - if (_slopPassed) + // average velocity (total offset to total duration) + _prevAvrgVel.x = _avrgVel.x; + _prevAvrgVel.y = _avrgVel.y; + var absPrevAvrgVel:Number = _prevAvrgVel.length; + var totalTime:int = touch.time - _startTime; + _avrgVel.x = _offset.x / totalTime; + _avrgVel.y = _offset.y / totalTime; + var avrgVel:Number = _avrgVel.length; + + + if (avrgVel * 0.95 < absPrevAvrgVel) { - var velocity:Point = _centralPoint.velocity; + _decelerationCounter++; + } + if (_decelerationCounter > 5 || avrgVel < 0.1) + { + setState(GestureState.FAILED); + return; + } + + if (_noDirection) + { + // We should quickly fail if we have noticable deceleration + // or first movement happend way later after touch - var foo:Number = _centralPoint.moveOffset.length;//FIXME! - var swipeDetected:Boolean = false; - - if (getTimer() - _startTime > minTimeThreshold && foo > 10) + if (avrgVel >= minVelocity && (minOffset != minOffset || offsetLength >= minOffset)) { - var lastMoveX:Number = 0; - var lastMoveY:Number = 0; - - if (_canMoveHorizontally && _canMoveVertically) + if (setState(GestureState.RECOGNIZED) && hasEventListener(SwipeGestureEvent.GESTURE_SWIPE)) { - lastMoveX = _centralPoint.lastMove.x; - lastMoveY = _centralPoint.lastMove.y; - - if (direction == Direction.STRAIGHT_AXES) - { - // go to logic below: if (!swipeDetected && _canMove*).. - } - else if (direction == Direction.OCTO) - { - swipeDetected = velocity.length >= velocityThreshold; - - if (Math.abs(velocity.y) < sideVelocityThreshold) - { - // horizontal swipe - lastMoveY = 0; - } - else if (Math.abs(velocity.x) < sideVelocityThreshold) - { - // vertical swipe - lastMoveX = 0; - } - } - else - { - // free direction swipe - swipeDetected = velocity.length >= velocityThreshold; - } - } - - if (!swipeDetected && _canMoveHorizontally) - { - swipeDetected = Math.abs(velocity.x) >= velocityThreshold && - Math.abs(velocity.y) < sideVelocityThreshold; - - lastMoveX = _centralPoint.lastMove.x; - lastMoveY = 0; - } - if (!swipeDetected && _canMoveVertically) - { - swipeDetected = Math.abs(velocity.y) >= velocityThreshold && - Math.abs(velocity.x) < sideVelocityThreshold; - - lastMoveX = 0; - lastMoveY = _centralPoint.lastMove.y; - } - - if (swipeDetected) - { - _reset(); -// trace("swipe detected:", lastMoveX, lastMoveY); - _dispatch(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, true, false, GesturePhase.ALL, target.mouseX, target.mouseY, 1, 1, 0, lastMoveX, lastMoveY)); + _localLocation = target.globalToLocal(_location);//refresh local location in case target moved + dispatchEvent(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GestureState.RECOGNIZED, + _location.x, _location.y, _localLocation.x, _localLocation.y, _offset.x, _offset.y)); } } } + else + { + var recentOffsetX:Number = _centralPoint.x - prevCentralPointX; + var recentOffsetY:Number = _centralPoint.y - prevCentralPointY; + //faster Math.abs() + var absVelX:Number = _avrgVel.x > 0 ? _avrgVel.x : -_avrgVel.x; + var absVelY:Number = _avrgVel.y > 0 ? _avrgVel.y : -_avrgVel.y; + + if (absVelX > absVelY) + { + var absOffsetX:Number = _offset.x > 0 ? _offset.x : -_offset.x; + + if ((SwipeGestureDirection.HORIZONTAL & direction) == 0) + { + // horizontal velocity is greater then vertical, but we're not interested in any horizontal direction + setState(GestureState.FAILED); + } + else if (recentOffsetX < 0 && (direction & SwipeGestureDirection.LEFT) == 0 || + recentOffsetX > 0 && (direction & SwipeGestureDirection.RIGHT) == 0 || + Math.abs(_offset.y) > maxDirectionalOffset) + { + // movement in opposite direction + // or too much diagonally + setState(GestureState.FAILED); + } + else if (absVelX >= minVelocity && (minOffset != minOffset || absOffsetX >= minOffset)) + { + _offset.y = 0; + if (setState(GestureState.RECOGNIZED) && hasEventListener(SwipeGestureEvent.GESTURE_SWIPE)) + { + _localLocation = target.globalToLocal(_location);//refresh local location in case target moved + dispatchEvent(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GestureState.RECOGNIZED, + _location.x, _location.y, _localLocation.x, _localLocation.y, _offset.x, _offset.y)); + } + } + } + else if (absVelY > absVelX) + { + var absOffsetY:Number = _offset.y > 0 ? _offset.y : -_offset.y; + + if ((SwipeGestureDirection.VERTICAL & direction) == 0) + { + // horizontal velocity is greater then vertical, but we're not interested in any horizontal direction + setState(GestureState.FAILED); + } + else if (recentOffsetY < 0 && (direction & SwipeGestureDirection.UP) == 0 || + recentOffsetY > 0 && (direction & SwipeGestureDirection.DOWN) == 0 || + Math.abs(_offset.x) > maxDirectionalOffset) + { + // movement in opposite direction + // or too much diagonally + setState(GestureState.FAILED); + } + else if (absVelY >= minVelocity && (minOffset != minOffset || absOffsetY >= minOffset)) + { + _offset.x = 0; + if (setState(GestureState.RECOGNIZED) && hasEventListener(SwipeGestureEvent.GESTURE_SWIPE)) + { + _localLocation = target.globalToLocal(_location);//refresh local location in case target moved + dispatchEvent(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GestureState.RECOGNIZED, + _location.x, _location.y, _localLocation.x, _localLocation.y, _offset.x, _offset.y)); + } + } + } + else + { + setState(GestureState.FAILED); + } + } + } + + + override protected function onTouchEnd(touch:Touch):void + { + if (touchesCount < numTouchesRequired) + { + setState(GestureState.FAILED); + } } - override public function onTouchEnd(touchPoint:TouchPoint):void + override protected function onDelayedRecognize():void { - _forgetPoint(touchPoint); + if (hasEventListener(SwipeGestureEvent.GESTURE_SWIPE)) + { + _localLocation = target.globalToLocal(_location);//refresh local location in case target moved + dispatchEvent(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GestureState.RECOGNIZED, + _location.x, _location.y, _localLocation.x, _localLocation.y, _offset.x, _offset.y)); + } } } } \ No newline at end of file diff --git a/src/org/gestouch/gestures/SwipeGestureDirection.as b/src/org/gestouch/gestures/SwipeGestureDirection.as new file mode 100644 index 0000000..20d2095 --- /dev/null +++ b/src/org/gestouch/gestures/SwipeGestureDirection.as @@ -0,0 +1,18 @@ +package org.gestouch.gestures +{ + /** + * @author Pavel fljot + */ + public class SwipeGestureDirection + { + public static const RIGHT:uint = 1 << 0; + public static const LEFT:uint = 1 << 1; + public static const UP:uint = 1 << 2; + public static const DOWN:uint = 1 << 3; + + public static const NO_DIRECTION:uint = 0; + public static const HORIZONTAL:uint = RIGHT | LEFT; + public static const VERTICAL:uint = UP | DOWN; + public static const ORTHOGONAL:uint = RIGHT | LEFT | UP | DOWN; + } +} \ No newline at end of file diff --git a/src/org/gestouch/gestures/TapGesture.as b/src/org/gestouch/gestures/TapGesture.as new file mode 100644 index 0000000..fc5881b --- /dev/null +++ b/src/org/gestouch/gestures/TapGesture.as @@ -0,0 +1,181 @@ +package org.gestouch.gestures +{ + import org.gestouch.core.GestureState; + import org.gestouch.core.Touch; + import org.gestouch.events.TapGestureEvent; + + import flash.display.InteractiveObject; + import flash.events.TimerEvent; + import flash.utils.Timer; + + + [Event(name="gestureTap", type="org.gestouch.events.TapGestureEvent")] + /** + * TODO: check failing conditions (iDevice) + * + * @author Pavel fljot + */ + public class TapGesture extends Gesture + { + public var numTouchesRequired:uint = 1; + public var numTapsRequired:uint = 1; + public var slop:Number = Gesture.DEFAULT_SLOP; + public var maxTapDelay:uint = 400; + public var maxTapDuration:uint = 1500; + + protected var _timer:Timer; + protected var _numTouchesRequiredReached:Boolean; + protected var _tapCounter:uint = 0; + + + public function TapGesture(target:InteractiveObject = null) + { + super(target); + } + + + + + // -------------------------------------------------------------------------- + // + // Public methods + // + // -------------------------------------------------------------------------- + + override public function reflect():Class + { + return TapGesture; + } + + + override public function reset():void + { + _numTouchesRequiredReached = false; + _tapCounter = 0; + _timer.reset(); + + super.reset(); + } + + + override public function canPreventGesture(preventedGesture:Gesture):Boolean + { + if (preventedGesture is TapGesture && + (preventedGesture as TapGesture).numTapsRequired > this.numTapsRequired) + { + return false; + } + return true; + } + + + + + // -------------------------------------------------------------------------- + // + // Protected methods + // + // -------------------------------------------------------------------------- + + override protected function preinit():void + { + super.preinit(); + + _timer = new Timer(maxTapDelay, 1); + _timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_timerCompleteHandler); + } + + + override protected function onTouchBegin(touch:Touch):void + { + if (touchesCount > numTouchesRequired) + { + // We put more fingers then required at the same time, + // so treat that as failed + setState(GestureState.FAILED); + return; + } + + if (touchesCount == 1) + { + _timer.reset(); + _timer.delay = maxTapDuration; + _timer.start(); + } + + if (touchesCount == numTouchesRequired) + { + _numTouchesRequiredReached = true; + } + } + + + override protected function onTouchMove(touch:Touch):void + { + if (slop >= 0 && touch.locationOffset.length > slop) + { + setState(GestureState.FAILED); + } + } + + + override protected function onTouchEnd(touch:Touch):void + { + if (!_numTouchesRequiredReached) + { + //TODO: check this condition on iDevice + setState(GestureState.FAILED); + } + else if (touchesCount == 0) + { + // reset flag for the next "full press" cycle + _numTouchesRequiredReached = false; + + _tapCounter++; + _timer.reset(); + + if (_tapCounter == numTapsRequired) + { + updateLocation(); + if (setState(GestureState.RECOGNIZED) && hasEventListener(TapGestureEvent.GESTURE_TAP)) + { + dispatchEvent(new TapGestureEvent(TapGestureEvent.GESTURE_TAP, false, false, GestureState.RECOGNIZED, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } + } + else + { + _timer.delay = maxTapDelay; + _timer.start(); + } + } + } + + + override protected function onDelayedRecognize():void + { + if (hasEventListener(TapGestureEvent.GESTURE_TAP)) + { + dispatchEvent(new TapGestureEvent(TapGestureEvent.GESTURE_TAP, false, false, GestureState.RECOGNIZED, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } + } + + + + + //-------------------------------------------------------------------------- + // + // Event handlers + // + //-------------------------------------------------------------------------- + + protected function timer_timerCompleteHandler(event:TimerEvent):void + { + if (state == GestureState.POSSIBLE) + { + setState(GestureState.FAILED); + } + } + } +} \ No newline at end of file diff --git a/src/org/gestouch/gestures/TransformGesture.as b/src/org/gestouch/gestures/TransformGesture.as new file mode 100644 index 0000000..b3a2daa --- /dev/null +++ b/src/org/gestouch/gestures/TransformGesture.as @@ -0,0 +1,187 @@ +package org.gestouch.gestures +{ + import org.gestouch.core.GestureState; + import org.gestouch.core.Touch; + import org.gestouch.events.TransformGestureEvent; + import org.gestouch.utils.GestureUtils; + + import flash.display.InteractiveObject; + import flash.geom.Point; + + + [Event(name="gestureTransform", type="org.gestouch.events.TransformGestureEvent")] + /** + * @author Pavel fljot + */ + public class TransformGesture extends Gesture + { + public var slop:Number = Gesture.DEFAULT_SLOP; + + protected var _touch1:Touch; + protected var _touch2:Touch; + protected var _transformVector:Point; + + + public function TransformGesture(target:InteractiveObject = null) + { + super(target); + } + + + + + // -------------------------------------------------------------------------- + // + // Public methods + // + // -------------------------------------------------------------------------- + + override public function reflect():Class + { + return TransformGesture; + } + + + override public function reset():void + { + _touch1 = null; + _touch2 = null; + + super.reset(); + } + + + + + // -------------------------------------------------------------------------- + // + // Protected methods + // + // -------------------------------------------------------------------------- + + override protected function onTouchBegin(touch:Touch):void + { + if (touchesCount > 2) + { + //TODO: to ignore or to keep this touch somewhere? + ignoreTouch(touch); + return; + } + + if (touchesCount == 1) + { + _touch1 = touch; + } + else + { + _touch2 = touch; + + _transformVector = _touch2.location.subtract(_touch1.location); + } + + updateLocation(); + + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + if (setState(GestureState.CHANGED) && hasEventListener(TransformGestureEvent.GESTURE_TRANSFORM)) + { + dispatchEvent(new TransformGestureEvent(TransformGestureEvent.GESTURE_TRANSFORM, false, false, GestureState.CHANGED, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } + } + } + + + override protected function onTouchMove(touch:Touch):void + { + var prevLocation:Point = _location.clone(); + updateLocation(); + + var recognized:Boolean = true; + + if (state == GestureState.POSSIBLE && slop > 0 && touch.locationOffset.length < slop) + { + recognized = false; + } + + if (recognized) + { + var prevLocalLocation:Point; + var offsetX:Number = _location.x - prevLocation.x; + var offsetY:Number = _location.y - prevLocation.y; + var scale:Number = 1; + var rotation:Number = 0; + if (_touch2) + { + var currTransformVector:Point = _touch2.location.subtract(_touch1.location); + rotation = Math.atan2(currTransformVector.y, currTransformVector.x) - Math.atan2(_transformVector.y, _transformVector.x); + rotation *= GestureUtils.RADIANS_TO_DEGREES; + scale = currTransformVector.length / _transformVector.length; + _transformVector = _touch2.location.subtract(_touch1.location); + } + + + if (state == GestureState.POSSIBLE) + { + if (setState(GestureState.BEGAN) && hasEventListener(TransformGestureEvent.GESTURE_TRANSFORM)) + { + // Note that we dispatch previous location point which gives a way to perform + // accurate UI redraw. See examples project for more info. + prevLocalLocation = target.globalToLocal(prevLocation); + dispatchEvent(new TransformGestureEvent(TransformGestureEvent.GESTURE_TRANSFORM, false, false, GestureState.BEGAN, + prevLocation.x, prevLocation.y, prevLocalLocation.x, prevLocalLocation.y, scale, scale, rotation, offsetX, offsetY)); + } + } + else + { + if (setState(GestureState.CHANGED) && hasEventListener(TransformGestureEvent.GESTURE_TRANSFORM)) + { + // Note that we dispatch previous location point which gives a way to perform + // accurate UI redraw. See examples project for more info. + prevLocalLocation = target.globalToLocal(prevLocation); + dispatchEvent(new TransformGestureEvent(TransformGestureEvent.GESTURE_TRANSFORM, false, false, GestureState.CHANGED, + prevLocation.x, prevLocation.y, prevLocalLocation.x, prevLocalLocation.y, scale, scale, rotation, offsetX, offsetY)); + } + } + } + } + + + override protected function onTouchEnd(touch:Touch):void + { + if (touchesCount == 0) + { + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + if (setState(GestureState.ENDED) && hasEventListener(TransformGestureEvent.GESTURE_TRANSFORM)) + { + dispatchEvent(new TransformGestureEvent(TransformGestureEvent.GESTURE_TRANSFORM, false, false, GestureState.ENDED, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } + } + else if (state == GestureState.POSSIBLE) + { + setState(GestureState.FAILED); + } + } + else// == 1 + { + if (touch == _touch1) + { + _touch1 = _touch2; + } + _touch2 = null; + + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + updateLocation(); + if (setState(GestureState.CHANGED) && hasEventListener(TransformGestureEvent.GESTURE_TRANSFORM)) + { + dispatchEvent(new TransformGestureEvent(TransformGestureEvent.GESTURE_TRANSFORM, false, false, GestureState.CHANGED, + _location.x, _location.y, _localLocation.x, _localLocation.y)); + } + } + } + } + } +} \ No newline at end of file diff --git a/src/org/gestouch/gestures/ZoomGesture.as b/src/org/gestouch/gestures/ZoomGesture.as index 2508432..09608b3 100644 --- a/src/org/gestouch/gestures/ZoomGesture.as +++ b/src/org/gestouch/gestures/ZoomGesture.as @@ -1,60 +1,42 @@ package org.gestouch.gestures { - import org.gestouch.core.GesturesManager; - import org.gestouch.core.TouchPoint; - import org.gestouch.core.gestouch_internal; + import org.gestouch.core.GestureState; + import org.gestouch.core.Touch; import org.gestouch.events.ZoomGestureEvent; import flash.display.InteractiveObject; - import flash.events.GesturePhase; import flash.geom.Point; - [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 lockAspectRatio:Boolean = true; - protected var _currVector:Point = new Point(); - protected var _lastVector:Point = new Point(); + protected var _touch1:Touch; + protected var _touch2:Touch; + protected var _transformVector:Point; - public function ZoomGesture(target:InteractiveObject, settings:Object = null) + public function ZoomGesture(target:InteractiveObject = null) { - super(target, settings); + super(target); } - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- // - // Static methods + // Public methods // - //-------------------------------------------------------------------------- - - public static function add(target:InteractiveObject = null, settings:Object = null):ZoomGesture - { - return new ZoomGesture(target, settings); - } - - - public static function remove(target:InteractiveObject):ZoomGesture - { - return GesturesManager.gestouch_internal::removeGestureByTarget(ZoomGesture, target) as ZoomGesture; - } - - - - - //-------------------------------------------------------------------------- - // - // Public methods - // - //-------------------------------------------------------------------------- + // -------------------------------------------------------------------------- override public function reflect():Class { @@ -62,79 +44,123 @@ package org.gestouch.gestures } - override public function onTouchBegin(touchPoint:TouchPoint):void + + + // -------------------------------------------------------------------------- + // + // Protected methods + // + // -------------------------------------------------------------------------- + + override protected function onTouchBegin(touch:Touch):void { - // No need to track more points than we need - if (_trackingPointsCount == maxTouchPointsCount) + if (touchesCount > 2) { + //TODO + ignoreTouch(touch); return; } - _trackPoint(touchPoint); - - if (_trackingPointsCount == minTouchPointsCount) + if (touchesCount == 1) { - _lastVector.x = _trackingPoints[1].x - _trackingPoints[0].x; - _lastVector.y = _trackingPoints[1].y - _trackingPoints[0].y; + _touch1 = touch; + } + else// == 2 + { + _touch2 = touch; - _updateCentralPoint(); - - _dispatch(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, true, false, GesturePhase.BEGIN, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y)); + _transformVector = _touch2.location.subtract(_touch1.location); } } - override public function onTouchMove(touchPoint:TouchPoint):void + override protected function onTouchMove(touch:Touch):void { - // do calculations only when we track enought points - if (_trackingPointsCount < minTouchPointsCount) - { + if (touchesCount < 2) return; + + var recognized:Boolean = true; + + if (state == GestureState.POSSIBLE && slop > 0 && touch.locationOffset.length < slop) + { + recognized = false; } - _updateCentralPoint(); - - _currVector.x = _trackingPoints[1].x - _trackingPoints[0].x; - _currVector.y = _trackingPoints[1].y - _trackingPoints[0].y; - - var scaleX:Number = _currVector.x / _lastVector.x; - var scaleY:Number = _currVector.y / _lastVector.y; - if (lockAspectRatio) + if (recognized) { - scaleX = scaleY = _currVector.length / _lastVector.length; - } - else - { - scaleX = _currVector.x / _lastVector.x; - scaleY = _currVector.y / _lastVector.y; - } - - _lastVector.x = _currVector.x; - _lastVector.y = _currVector.y; - - _dispatch(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, true, false, GesturePhase.UPDATE, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y, scaleX, scaleY)); - } - - - override public function onTouchEnd(touchPoint:TouchPoint):void - { - var ending:Boolean = (_trackingPointsCount == minTouchPointsCount); - _forgetPoint(touchPoint); - - if (ending) - { - _dispatch(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, true, false, GesturePhase.END, _lastLocalCentralPoint.x, _lastLocalCentralPoint.y)); + 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)) + { + 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)); + } + } } } - override protected function _preinit():void + override protected function onTouchEnd(touch:Touch):void { - super._preinit(); - - minTouchPointsCount = 2; - - _propertyNames.push("lockAspectRatio"); + if (touchesCount == 0) + { + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + if (setState(GestureState.ENDED) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM)) + { + dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GestureState.ENDED, + _location.x, _location.y, _localLocation.x, _localLocation.y, 1, 1)); + } + } + else if (state == GestureState.POSSIBLE) + { + setState(GestureState.FAILED); + } + } + else//== 1 + { + if (touch == _touch1) + { + _touch1 = _touch2; + } + _touch2 = null; + + if (state == GestureState.BEGAN || state == GestureState.CHANGED) + { + updateLocation(); + 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, 1, 1)); + } + } + } } } } \ No newline at end of file diff --git a/src/org/gestouch/input/AbstractInputAdapter.as b/src/org/gestouch/input/AbstractInputAdapter.as new file mode 100644 index 0000000..8543af5 --- /dev/null +++ b/src/org/gestouch/input/AbstractInputAdapter.as @@ -0,0 +1,51 @@ +package org.gestouch.input +{ + import org.gestouch.core.IGesturesManager; + import org.gestouch.core.IInputAdapter; + import org.gestouch.core.ITouchesManager; + + + /** + * @author Pavel fljot + */ + public class AbstractInputAdapter implements IInputAdapter + { + protected var _touchesManager:ITouchesManager; + protected var _gesturesManager:IGesturesManager; + + + public function AbstractInputAdapter() + { + if (Object(this).constructor == AbstractInputAdapter) + { + throw new Error("This is abstract class and should not be directly instantiated."); + } + } + + + public function set touchesManager(value:ITouchesManager):void + { + _touchesManager = value; + } + + + public function set gesturesManager(value:IGesturesManager):void + { + _gesturesManager = value; + } + + + [Abstract] + public function init():void + { + throw new Error("This is abstract method."); + } + + + [Abstract] + public function dispose():void + { + throw new Error("This is abstract method."); + } + } +} \ No newline at end of file diff --git a/src/org/gestouch/input/MouseInputAdapter.as b/src/org/gestouch/input/MouseInputAdapter.as new file mode 100644 index 0000000..38094fe --- /dev/null +++ b/src/org/gestouch/input/MouseInputAdapter.as @@ -0,0 +1,133 @@ +package org.gestouch.input +{ + import org.gestouch.core.Touch; + import org.gestouch.core.gestouch_internal; + + import flash.display.InteractiveObject; + import flash.display.Stage; + import flash.events.EventPhase; + import flash.events.MouseEvent; + import flash.geom.Point; + import flash.utils.getTimer; + + + /** + * @author Pavel fljot + */ + public class MouseInputAdapter extends AbstractInputAdapter + { + private static const PRIMARY_TOUCH_POINT_ID:uint = 0; + + protected var _stage:Stage; + + + public function MouseInputAdapter(stage:Stage) + { + super(); + + if (!stage) + { + throw new Error("Stage must be not null."); + } + + _stage = stage; + + stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); + } + + + override public function init():void + { + _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); + } + + + override public function dispose():void + { + _stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); + uninstallStageListeners(); + } + + + protected function installStageListeners():void + { + // Maximum priority to prevent event hijacking + _stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true, int.MAX_VALUE); + _stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true, int.MAX_VALUE); + // To catch event out of stage + _stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, int.MAX_VALUE); + } + + + protected function uninstallStageListeners():void + { + _stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true); + _stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true); + _stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); + } + + + protected function mouseDownHandler(event:MouseEvent):void + { + // Way to prevent MouseEvent/TouchEvent collisions. + // Also helps to ignore possible fake events. + if (_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID)) + return; + + installStageListeners(); + + var touch:Touch = _touchesManager.createTouch(); + touch.id = 0; + touch.target = event.target as InteractiveObject; + touch.gestouch_internal::setLocation(new Point(event.stageX, event.stageY)); + touch.gestouch_internal::setTime(getTimer()); + touch.gestouch_internal::setBeginTime(getTimer()); + + _touchesManager.addTouch(touch); + + _gesturesManager.gestouch_internal::onTouchBegin(touch); + } + + + protected function mouseMoveHandler(event:MouseEvent):void + { + // Way to prevent MouseEvent/TouchEvent collisions. + // Also helps to ignore possible fake events. + if (!_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID)) + return; + + var touch:Touch = _touchesManager.getTouch(PRIMARY_TOUCH_POINT_ID); + touch.gestouch_internal::updateLocation(event.stageX, event.stageY); + touch.gestouch_internal::setTime(getTimer()); + + _gesturesManager.gestouch_internal::onTouchMove(touch); + } + + + protected function mouseUpHandler(event:MouseEvent):void + { + // If event happens outside of stage it will be with AT_TARGET phase + if (event.eventPhase == EventPhase.BUBBLING_PHASE) + return; + + + // Way to prevent MouseEvent/TouchEvent collisions. + // Also helps to ignore possible fake events. + if (!_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID)) + return; + + var touch:Touch = _touchesManager.getTouch(PRIMARY_TOUCH_POINT_ID); + touch.gestouch_internal::updateLocation(event.stageX, event.stageY); + touch.gestouch_internal::setTime(getTimer()); + + _gesturesManager.gestouch_internal::onTouchEnd(touch); + + _touchesManager.removeTouch(touch); + + if (_touchesManager.activeTouchesCount == 0) + { + uninstallStageListeners(); + } + } + } +} \ No newline at end of file diff --git a/src/org/gestouch/input/TUIOInputAdapter.as b/src/org/gestouch/input/TUIOInputAdapter.as new file mode 100644 index 0000000..5dae3e9 --- /dev/null +++ b/src/org/gestouch/input/TUIOInputAdapter.as @@ -0,0 +1,17 @@ +package org.gestouch.input +{ + import org.gestouch.input.AbstractInputAdapter; + + + /** + * @author Pavel fljot + */ + public class TUIOInputAdapter extends AbstractInputAdapter + { + public function TUIOInputAdapter() + { + super(); + //TODO + } + } +} \ No newline at end of file diff --git a/src/org/gestouch/input/TouchInputAdapter.as b/src/org/gestouch/input/TouchInputAdapter.as new file mode 100644 index 0000000..b86afab --- /dev/null +++ b/src/org/gestouch/input/TouchInputAdapter.as @@ -0,0 +1,174 @@ +package org.gestouch.input +{ + import org.gestouch.core.Touch; + import org.gestouch.core.gestouch_internal; + + import flash.display.InteractiveObject; + import flash.display.Stage; + import flash.events.EventPhase; + import flash.events.TouchEvent; + import flash.geom.Point; + import flash.utils.getTimer; + + + /** + * @author Pavel fljot + */ + public class TouchInputAdapter extends AbstractInputAdapter + { + protected var _stage:Stage; + /** + * The hash map of touches instantiated via TouchEvent. + * Used to avoid collisions (double processing) with MouseInputAdapter. + * + * TODO: any better way? + */ + protected var _touchesMap:Object = {}; + + + public function TouchInputAdapter(stage:Stage) + { + super(); + + if (!stage) + { + throw new Error("Stage must be not null."); + } + + _stage = stage; + } + + + override public function init():void + { + _stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true); + } + + + override public function dispose():void + { + _stage.removeEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true); + uninstallStageListeners(); + } + + + protected function installStageListeners():void + { + // Maximum priority to prevent event hijacking + _stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler, true, int.MAX_VALUE); + _stage.addEventListener(TouchEvent.TOUCH_END, touchEndHandler, true, int.MAX_VALUE); + // To catch event out of stage + _stage.addEventListener(TouchEvent.TOUCH_END, touchEndHandler, false, int.MAX_VALUE); + } + + + protected function uninstallStageListeners():void + { + _stage.removeEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler, true); + _stage.removeEventListener(TouchEvent.TOUCH_END, touchEndHandler, true); + _stage.removeEventListener(TouchEvent.TOUCH_END, touchEndHandler); + } + + + protected function touchBeginHandler(event:TouchEvent):void + { + // Way to prevent MouseEvent/TouchEvent collisions. + // Also helps to ignore possible fake events. + if (_touchesManager.hasTouch(event.touchPointID)) + return; + + installStageListeners(); + + var touch:Touch = _touchesManager.createTouch(); + touch.id = event.touchPointID; + touch.target = event.target as InteractiveObject; + 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.gestouch_internal::setTime(event["timestamp"]); + touch.gestouch_internal::setBeginTime(event["timestamp"]); + } + else + { + touch.gestouch_internal::setTime(getTimer()); + touch.gestouch_internal::setBeginTime(getTimer()); + } + + _touchesManager.addTouch(touch); + _touchesMap[touch.id] = true; + + _gesturesManager.gestouch_internal::onTouchBegin(touch); + } + + + protected function touchMoveHandler(event:TouchEvent):void + { + // Way to prevent MouseEvent/TouchEvent collisions. + // Also helps to ignore possible fake events. + if (!_touchesManager.hasTouch(event.touchPointID) || !_touchesMap.hasOwnProperty(event.touchPointID)) + return; + + var touch:Touch = _touchesManager.getTouch(event.touchPointID); + 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.gestouch_internal::setTime(event["timestamp"]); + } + else + { + touch.gestouch_internal::setTime(getTimer()); + } + + _gesturesManager.gestouch_internal::onTouchMove(touch); + } + + + protected function touchEndHandler(event:TouchEvent):void + { + // If event happens outside of stage it will be with AT_TARGET phase + if (event.eventPhase == EventPhase.BUBBLING_PHASE) + return; + + // Way to prevent MouseEvent/TouchEvent collisions. + // Also helps to ignore possible fake events. + if (!_touchesManager.hasTouch(event.touchPointID)) + return; + + var touch:Touch = _touchesManager.getTouch(event.touchPointID); + 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.gestouch_internal::setTime(event["timestamp"]); + } + else + { + touch.gestouch_internal::setTime(getTimer()); + } + + _gesturesManager.gestouch_internal::onTouchEnd(touch); + + _touchesManager.removeTouch(touch); + delete _touchesMap[touch.id]; + + if (_touchesManager.activeTouchesCount == 0) + { + uninstallStageListeners(); + } + + // TODO: handle cancelled touch: + // if (event.hasOwnProperty("isTouchPointCanceled") && event["isTouchPointCanceled"] && ... + } + } +} \ No newline at end of file diff --git a/src/org/gestouch/GestureUtils.as b/src/org/gestouch/utils/GestureUtils.as similarity index 96% rename from src/org/gestouch/GestureUtils.as rename to src/org/gestouch/utils/GestureUtils.as index abbc265..4d57d69 100644 --- a/src/org/gestouch/GestureUtils.as +++ b/src/org/gestouch/utils/GestureUtils.as @@ -1,4 +1,4 @@ -package org.gestouch +package org.gestouch.utils { import flash.system.Capabilities; /** diff --git a/src/org/gestouch/utils/ObjectPool.as b/src/org/gestouch/utils/ObjectPool.as deleted file mode 100644 index 8c920c0..0000000 --- a/src/org/gestouch/utils/ObjectPool.as +++ /dev/null @@ -1,131 +0,0 @@ -package org.gestouch.utils -{ - import flash.utils.getQualifiedClassName; - - /** - * @author Pavel fljot - * - * "inspired" by Jonnie Hallman - * @link http://destroytoday.com - * @link https://github.com/destroytoday - * - * Added some optimization and changes. - */ - public class ObjectPool - { - // -------------------------------------------------------------------------- - // - // Properties - // - // -------------------------------------------------------------------------- - - protected var _type:Class; - protected var objectList:Array = []; - - - // -------------------------------------------------------------------------- - // - // Constructor - // - // -------------------------------------------------------------------------- - - public function ObjectPool(type:Class, size:uint = 0) - { - _type = type; - - if (size > 0) - { - allocate(size); - } - } - - - - - // -------------------------------------------------------------------------- - // - // Getters / Setters - // - // -------------------------------------------------------------------------- - - public function get type():Class - { - return _type; - } - - - public function get numObjects():uint - { - return objectList.length; - } - - - - // -------------------------------------------------------------------------- - // - // Public Methods - // - // -------------------------------------------------------------------------- - - public function hasObject(object:Object):Boolean - { - return objectList.indexOf(object) > -1; - } - - - public function getObject():* - { - return numObjects > 0 ? objectList.pop() : createObject(); - } - - - public function disposeObject(object:Object):void - { - if (!(object is type)) - { - throw new TypeError("Disposed object type mismatch. Expected " + type + ", got " + getQualifiedClassName(object)); - } - - addObject(object); - } - - - public function empty():void - { - objectList.length = 0; - } - - - - //-------------------------------------------------------------------------- - // - // Protected methods - // - //-------------------------------------------------------------------------- - - protected function addObject(object:Object):* - { - if (!hasObject(object)) - objectList[objectList.length] = object; - - return object; - } - - - protected function createObject():* - { - return new type(); - } - - - protected function allocate(value:uint):void - { - var n:int = value - numObjects; - - while (n-- > 0) - { - addObject(createObject()); - } - } - } -} \ No newline at end of file diff --git a/version.properties b/version.properties index 7791a91..b8fb300 100644 --- a/version.properties +++ b/version.properties @@ -1 +1 @@ -project.version = 0.2 \ No newline at end of file +project.version = 0.3 \ No newline at end of file