Optimized event dispatching

This commit is contained in:
Pavel fljot 2011-12-30 18:26:16 +02:00
parent 3e1b5948b2
commit 47f2f848e4
7 changed files with 80 additions and 40 deletions

View file

@ -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;
}

View file

@ -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));
}
}
}
}

View file

@ -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

View file

@ -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));
}
}
}
}

View file

@ -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

View file

@ -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
{

View file

@ -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));
}
}
}
}