Input adapters fix to catch events on empty stage

This commit is contained in:
Pavel fljot 2012-03-08 13:40:04 +02:00
parent 97486ba2fe
commit e14bbd11bb
3 changed files with 25 additions and 16 deletions

View file

@ -1,7 +1,5 @@
package org.gestouch.core package org.gestouch.core
{ {
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
/** /**
* @author Pavel fljot * @author Pavel fljot
*/ */
@ -12,10 +10,6 @@ package org.gestouch.core
protected var _touchesMap:Object = {}; protected var _touchesMap:Object = {};
{
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
}
public function TouchesManager() public function TouchesManager()
{ {

View file

@ -31,20 +31,20 @@ package org.gestouch.input
} }
_stage = stage; _stage = stage;
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true);
} }
override public function init():void override public function init():void
{ {
_stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true);
_stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);// to catch with EventPhase.AT_TARGET
} }
override public function dispose():void override public function dispose():void
{ {
_stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); _stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true);
_stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
uninstallStageListeners(); uninstallStageListeners();
} }
@ -53,8 +53,8 @@ package org.gestouch.input
{ {
// Maximum priority to prevent event hijacking // Maximum priority to prevent event hijacking
_stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true, int.MAX_VALUE); _stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true, int.MAX_VALUE);
_stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, false, int.MAX_VALUE);
_stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, 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); _stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, int.MAX_VALUE);
} }
@ -62,6 +62,7 @@ package org.gestouch.input
protected function uninstallStageListeners():void protected function uninstallStageListeners():void
{ {
_stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true); _stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler, true);
_stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
_stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true); _stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, true);
_stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); _stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
} }
@ -69,6 +70,8 @@ package org.gestouch.input
protected function mouseDownHandler(event:MouseEvent):void protected function mouseDownHandler(event:MouseEvent):void
{ {
if (event.eventPhase == EventPhase.BUBBLING_PHASE)
return;//we listen in capture or at_target (to catch on empty stage)
// Way to prevent MouseEvent/TouchEvent collisions. // Way to prevent MouseEvent/TouchEvent collisions.
// Also helps to ignore possible fake events. // Also helps to ignore possible fake events.
if (_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID)) if (_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID))
@ -77,8 +80,8 @@ package org.gestouch.input
installStageListeners(); installStageListeners();
var touch:Touch = _touchesManager.createTouch(); var touch:Touch = _touchesManager.createTouch();
touch.id = 0;
touch.target = event.target as InteractiveObject; touch.target = event.target as InteractiveObject;
touch.id = PRIMARY_TOUCH_POINT_ID;
touch.gestouch_internal::setLocation(new Point(event.stageX, event.stageY)); touch.gestouch_internal::setLocation(new Point(event.stageX, event.stageY));
touch.gestouch_internal::setTime(getTimer()); touch.gestouch_internal::setTime(getTimer());
touch.gestouch_internal::setBeginTime(getTimer()); touch.gestouch_internal::setBeginTime(getTimer());
@ -91,6 +94,8 @@ package org.gestouch.input
protected function mouseMoveHandler(event:MouseEvent):void protected function mouseMoveHandler(event:MouseEvent):void
{ {
if (event.eventPhase == EventPhase.BUBBLING_PHASE)
return;//we listen in capture or at_target (to catch on empty stage)
// Way to prevent MouseEvent/TouchEvent collisions. // Way to prevent MouseEvent/TouchEvent collisions.
// Also helps to ignore possible fake events. // Also helps to ignore possible fake events.
if (!_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID)) if (!_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID))
@ -106,10 +111,8 @@ package org.gestouch.input
protected function mouseUpHandler(event:MouseEvent):void 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) if (event.eventPhase == EventPhase.BUBBLING_PHASE)
return; return;//we listen in capture or at_target (to catch on empty stage)
// Way to prevent MouseEvent/TouchEvent collisions. // Way to prevent MouseEvent/TouchEvent collisions.
// Also helps to ignore possible fake events. // Also helps to ignore possible fake events.

View file

@ -8,6 +8,8 @@ package org.gestouch.input
import flash.events.EventPhase; import flash.events.EventPhase;
import flash.events.TouchEvent; import flash.events.TouchEvent;
import flash.geom.Point; import flash.geom.Point;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.utils.getTimer; import flash.utils.getTimer;
@ -25,6 +27,10 @@ package org.gestouch.input
*/ */
protected var _touchesMap:Object = {}; protected var _touchesMap:Object = {};
{
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
}
public function TouchInputAdapter(stage:Stage) public function TouchInputAdapter(stage:Stage)
{ {
@ -42,12 +48,14 @@ package org.gestouch.input
override public function init():void override public function init():void
{ {
_stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true); _stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true);
_stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler);// to catch with EventPhase.AT_TARGET
} }
override public function dispose():void override public function dispose():void
{ {
_stage.removeEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true); _stage.removeEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true);
_stage.removeEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler);
uninstallStageListeners(); uninstallStageListeners();
} }
@ -56,8 +64,8 @@ package org.gestouch.input
{ {
// Maximum priority to prevent event hijacking // Maximum priority to prevent event hijacking
_stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler, true, int.MAX_VALUE); _stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler, true, int.MAX_VALUE);
_stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler, false, int.MAX_VALUE);
_stage.addEventListener(TouchEvent.TOUCH_END, touchEndHandler, 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); _stage.addEventListener(TouchEvent.TOUCH_END, touchEndHandler, false, int.MAX_VALUE);
} }
@ -65,6 +73,7 @@ package org.gestouch.input
protected function uninstallStageListeners():void protected function uninstallStageListeners():void
{ {
_stage.removeEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler, true); _stage.removeEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler, true);
_stage.removeEventListener(TouchEvent.TOUCH_MOVE, touchMoveHandler);
_stage.removeEventListener(TouchEvent.TOUCH_END, touchEndHandler, true); _stage.removeEventListener(TouchEvent.TOUCH_END, touchEndHandler, true);
_stage.removeEventListener(TouchEvent.TOUCH_END, touchEndHandler); _stage.removeEventListener(TouchEvent.TOUCH_END, touchEndHandler);
} }
@ -72,6 +81,8 @@ package org.gestouch.input
protected function touchBeginHandler(event:TouchEvent):void protected function touchBeginHandler(event:TouchEvent):void
{ {
if (event.eventPhase == EventPhase.BUBBLING_PHASE)
return;//we listen in capture or at_target (to catch on empty stage)
// Way to prevent MouseEvent/TouchEvent collisions. // Way to prevent MouseEvent/TouchEvent collisions.
// Also helps to ignore possible fake events. // Also helps to ignore possible fake events.
if (_touchesManager.hasTouch(event.touchPointID)) if (_touchesManager.hasTouch(event.touchPointID))
@ -107,6 +118,8 @@ package org.gestouch.input
protected function touchMoveHandler(event:TouchEvent):void protected function touchMoveHandler(event:TouchEvent):void
{ {
if (event.eventPhase == EventPhase.BUBBLING_PHASE)
return;//we listen in capture or at_target (to catch on empty stage)
// Way to prevent MouseEvent/TouchEvent collisions. // Way to prevent MouseEvent/TouchEvent collisions.
// Also helps to ignore possible fake events. // Also helps to ignore possible fake events.
if (!_touchesManager.hasTouch(event.touchPointID) || !_touchesMap.hasOwnProperty(event.touchPointID)) if (!_touchesManager.hasTouch(event.touchPointID) || !_touchesMap.hasOwnProperty(event.touchPointID))
@ -133,9 +146,8 @@ package org.gestouch.input
protected function touchEndHandler(event:TouchEvent):void 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) if (event.eventPhase == EventPhase.BUBBLING_PHASE)
return; return;//we listen in capture or at_target (to catch on empty stage)
// Way to prevent MouseEvent/TouchEvent collisions. // Way to prevent MouseEvent/TouchEvent collisions.
// Also helps to ignore possible fake events. // Also helps to ignore possible fake events.