diff --git a/src/org/gestouch/gestures/Gesture.as b/src/org/gestouch/gestures/Gesture.as index 4caa21a..56c2c6d 100644 --- a/src/org/gestouch/gestures/Gesture.as +++ b/src/org/gestouch/gestures/Gesture.as @@ -156,17 +156,6 @@ package org.gestouch.gestures // //-------------------------------------------------------------------------- - override public function dispatchEvent(event:Event):Boolean - { - if (hasEventListener(event.type)) - { - return super.dispatchEvent(event); - } - - return true; - } - - [Abstract] /** * Reflects gesture class (for better perfomance). @@ -327,15 +316,11 @@ package org.gestouch.gestures } - protected function setState(newState:uint, event:GestureEvent = null):void + protected function setState(newState:uint):Boolean { - if (_state == newState) + if (_state == newState && _state == GestureState.CHANGED) { - if (_state == GestureState.CHANGED && event) - { - dispatchEvent(event); - } - return; + return true; } //TODO: is state sequence validation needed? e.g.: @@ -349,7 +334,7 @@ package org.gestouch.gestures if (delegate && !delegate.gestureShouldBegin(this)) { setState(GestureState.FAILED); - return; + return false; } } @@ -363,16 +348,17 @@ package org.gestouch.gestures //TODO: what if RTE happens in event handlers? - dispatchEvent(new GestureStateEvent(GestureStateEvent.STATE_CHANGE, _state, oldState)); - if (event) + if (hasEventListener(GestureStateEvent.STATE_CHANGE)) { - dispatchEvent(event); + dispatchEvent(new GestureStateEvent(GestureStateEvent.STATE_CHANGE, _state, oldState)); } if (_state == GestureState.BEGAN || _state == GestureState.RECOGNIZED) { _gesturesManager.onGestureRecognized(this); } + + return true; } diff --git a/src/org/gestouch/gestures/LongPressGesture.as b/src/org/gestouch/gestures/LongPressGesture.as index 4504d8f..9307ef9 100644 --- a/src/org/gestouch/gestures/LongPressGesture.as +++ b/src/org/gestouch/gestures/LongPressGesture.as @@ -133,7 +133,10 @@ package org.gestouch.gestures else if (state == GestureState.BEGAN || state == GestureState.CHANGED) { updateLocation(); - setState(GestureState.CHANGED, new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y)); + if (setState(GestureState.CHANGED) && hasEventListener(LongPressGestureEvent.GESTURE_LONG_PRESS)) + { + dispatchEvent(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y)); + } } } @@ -146,7 +149,10 @@ package org.gestouch.gestures if (((GestureState.BEGAN | GestureState.CHANGED) & state) > 0) { updateLocation(); - setState(GestureState.ENDED, new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GesturePhase.END, _localLocation.x, _localLocation.y)); + if (setState(GestureState.ENDED) && hasEventListener(LongPressGestureEvent.GESTURE_LONG_PRESS)) + { + dispatchEvent(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GesturePhase.END, _localLocation.x, _localLocation.y)); + } } else { @@ -173,7 +179,10 @@ package org.gestouch.gestures if (state == GestureState.POSSIBLE) { updateLocation(); - setState(GestureState.BEGAN, new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y)); + if (setState(GestureState.BEGAN) && hasEventListener(LongPressGestureEvent.GESTURE_LONG_PRESS)) + { + dispatchEvent(new LongPressGestureEvent(LongPressGestureEvent.GESTURE_LONG_PRESS, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y)); + } } } } diff --git a/src/org/gestouch/gestures/PanGesture.as b/src/org/gestouch/gestures/PanGesture.as index fcb3c49..1830e03 100644 --- a/src/org/gestouch/gestures/PanGesture.as +++ b/src/org/gestouch/gestures/PanGesture.as @@ -158,7 +158,10 @@ package org.gestouch.gestures offset = offset.subtract(slopVector); } - setState(GestureState.BEGAN, new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y, offset.x, offset.y)); + if (setState(GestureState.BEGAN) && hasEventListener(PanGestureEvent.GESTURE_PAN)) + { + dispatchEvent(new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y, offset.x, offset.y)); + } } } else if (state == GestureState.BEGAN || state == GestureState.CHANGED) @@ -169,7 +172,10 @@ package org.gestouch.gestures offsetX = _location.x - prevLocationX; offsetY = _location.y - prevLocationY; - setState(GestureState.CHANGED, new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, offsetX, offsetY)); + if (setState(GestureState.CHANGED) && hasEventListener(PanGestureEvent.GESTURE_PAN)) + { + dispatchEvent(new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, offsetX, offsetY)); + } } } @@ -184,7 +190,10 @@ package org.gestouch.gestures } else { - setState(GestureState.ENDED, new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GesturePhase.END, _localLocation.x, _localLocation.y, 0, 0)); + if (setState(GestureState.ENDED) && hasEventListener(PanGestureEvent.GESTURE_PAN)) + { + dispatchEvent(new PanGestureEvent(PanGestureEvent.GESTURE_PAN, false, false, GesturePhase.END, _localLocation.x, _localLocation.y, 0, 0)); + } } } else diff --git a/src/org/gestouch/gestures/RotateGesture.as b/src/org/gestouch/gestures/RotateGesture.as index 5c67b29..c3c3960 100644 --- a/src/org/gestouch/gestures/RotateGesture.as +++ b/src/org/gestouch/gestures/RotateGesture.as @@ -138,11 +138,17 @@ package org.gestouch.gestures if (state == GestureState.POSSIBLE) { - setState(GestureState.BEGAN, new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y, rotation)); + if (setState(GestureState.BEGAN) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y, rotation)); + } } else { - setState(GestureState.CHANGED, new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, rotation)); + if (setState(GestureState.CHANGED) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, rotation)); + } } } } @@ -155,7 +161,10 @@ package org.gestouch.gestures { if (state == GestureState.BEGAN || state == GestureState.CHANGED) { - setState(GestureState.ENDED, new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.END, _localLocation.x, _localLocation.y, 0)); + if (setState(GestureState.ENDED) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.END, _localLocation.x, _localLocation.y, 0)); + } } else if (state == GestureState.POSSIBLE) { @@ -171,7 +180,10 @@ package org.gestouch.gestures if (state == GestureState.BEGAN || state == GestureState.CHANGED) { updateLocation(); - setState(GestureState.CHANGED, new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, 0)); + if (setState(GestureState.CHANGED) && hasEventListener(RotateGestureEvent.GESTURE_ROTATE)) + { + dispatchEvent(new RotateGestureEvent(RotateGestureEvent.GESTURE_ROTATE, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, 0)); + } } } } diff --git a/src/org/gestouch/gestures/SwipeGesture.as b/src/org/gestouch/gestures/SwipeGesture.as index 2ddc24f..06ec8f1 100644 --- a/src/org/gestouch/gestures/SwipeGesture.as +++ b/src/org/gestouch/gestures/SwipeGesture.as @@ -117,7 +117,10 @@ package org.gestouch.gestures { if (absVel >= minVelocity || (minDistance != minDistance || offsetLength >= minDistance)) { - setState(GestureState.RECOGNIZED, new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y, _offset.x, _offset.y)); + if (setState(GestureState.RECOGNIZED) && hasEventListener(SwipeGestureEvent.GESTURE_SWIPE)) + { + dispatchEvent(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y, _offset.x, _offset.y)); + } } } else @@ -145,7 +148,10 @@ package org.gestouch.gestures } else if (absVelX >= minVelocity || (minDistance != minDistance || absOffsetX >= minDistance)) { - setState(GestureState.RECOGNIZED, new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y, _offset.x, 0)); + if (setState(GestureState.RECOGNIZED) && hasEventListener(SwipeGestureEvent.GESTURE_SWIPE)) + { + dispatchEvent(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y, _offset.x, 0)); + } } } else if (absVelY > absVelX) @@ -165,7 +171,10 @@ package org.gestouch.gestures } else if (absVelY >= minVelocity || (minDistance != minDistance || absOffsetY >= minDistance)) { - setState(GestureState.RECOGNIZED, new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y, 0, _offset.y)); + if (setState(GestureState.RECOGNIZED) && hasEventListener(SwipeGestureEvent.GESTURE_SWIPE)) + { + dispatchEvent(new SwipeGestureEvent(SwipeGestureEvent.GESTURE_SWIPE, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y, 0, _offset.y)); + } } } else diff --git a/src/org/gestouch/gestures/TapGesture.as b/src/org/gestouch/gestures/TapGesture.as index e6254d4..afbf9a6 100644 --- a/src/org/gestouch/gestures/TapGesture.as +++ b/src/org/gestouch/gestures/TapGesture.as @@ -151,7 +151,10 @@ package org.gestouch.gestures if (_tapCounter == numTapsRequired) { updateLocation(); - setState(GestureState.RECOGNIZED, new TapGestureEvent(TapGestureEvent.GESTURE_TAP, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y)); + if (setState(GestureState.RECOGNIZED) && hasEventListener(TapGestureEvent.GESTURE_TAP)) + { + dispatchEvent(new TapGestureEvent(TapGestureEvent.GESTURE_TAP, false, false, GesturePhase.ALL, _localLocation.x, _localLocation.y)); + } } else { diff --git a/src/org/gestouch/gestures/ZoomGesture.as b/src/org/gestouch/gestures/ZoomGesture.as index 5a47d48..5ac4edd 100644 --- a/src/org/gestouch/gestures/ZoomGesture.as +++ b/src/org/gestouch/gestures/ZoomGesture.as @@ -148,11 +148,17 @@ package org.gestouch.gestures if (state == GestureState.POSSIBLE) { - setState(GestureState.BEGAN, new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y, scaleX, scaleY)); + if (setState(GestureState.BEGAN) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM)) + { + dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.BEGIN, _localLocation.x, _localLocation.y, scaleX, scaleY)); + } } else { - setState(GestureState.CHANGED, new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, scaleX, scaleY)); + if (setState(GestureState.CHANGED) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM)) + { + dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, scaleX, scaleY)); + } } } } @@ -165,7 +171,10 @@ package org.gestouch.gestures { if (state == GestureState.BEGAN || state == GestureState.CHANGED) { - setState(GestureState.ENDED, new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.END, _localLocation.x, _localLocation.y, 1, 1)); + if (setState(GestureState.ENDED) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM)) + { + dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.END, _localLocation.x, _localLocation.y, 1, 1)); + } } else if (state == GestureState.POSSIBLE) { @@ -181,7 +190,10 @@ package org.gestouch.gestures if (state == GestureState.BEGAN || state == GestureState.CHANGED) { updateLocation(); - setState(GestureState.CHANGED, new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, 1, 1)); + if (setState(GestureState.CHANGED) && hasEventListener(ZoomGestureEvent.GESTURE_ZOOM)) + { + dispatchEvent(new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM, false, false, GesturePhase.UPDATE, _localLocation.x, _localLocation.y, 1, 1)); + } } } }