From e14bbd11bbe3bae3653914aedb4ef0b43f063962 Mon Sep 17 00:00:00 2001 From: Pavel fljot Date: Thu, 8 Mar 2012 13:40:04 +0200 Subject: [PATCH] Input adapters fix to catch events on empty stage --- src/org/gestouch/core/TouchesManager.as | 6 ------ src/org/gestouch/input/MouseInputAdapter.as | 17 ++++++++++------- src/org/gestouch/input/TouchInputAdapter.as | 18 +++++++++++++++--- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/org/gestouch/core/TouchesManager.as b/src/org/gestouch/core/TouchesManager.as index 1e96a8f..aa42691 100644 --- a/src/org/gestouch/core/TouchesManager.as +++ b/src/org/gestouch/core/TouchesManager.as @@ -1,7 +1,5 @@ package org.gestouch.core { - import flash.ui.Multitouch; - import flash.ui.MultitouchInputMode; /** * @author Pavel fljot */ @@ -12,10 +10,6 @@ package org.gestouch.core protected var _touchesMap:Object = {}; - { - Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; - } - public function TouchesManager() { diff --git a/src/org/gestouch/input/MouseInputAdapter.as b/src/org/gestouch/input/MouseInputAdapter.as index 38094fe..4a3e8ea 100644 --- a/src/org/gestouch/input/MouseInputAdapter.as +++ b/src/org/gestouch/input/MouseInputAdapter.as @@ -31,20 +31,20 @@ package org.gestouch.input } _stage = stage; - - stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); } override public function init():void { _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); + _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);// to catch with EventPhase.AT_TARGET } override public function dispose():void { _stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler, true); + _stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); uninstallStageListeners(); } @@ -53,8 +53,8 @@ package org.gestouch.input { // Maximum priority to prevent event hijacking _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); - // To catch event out of stage _stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler, false, int.MAX_VALUE); } @@ -62,6 +62,7 @@ package org.gestouch.input protected function uninstallStageListeners():void { _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); } @@ -69,6 +70,8 @@ package org.gestouch.input 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. // Also helps to ignore possible fake events. if (_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID)) @@ -77,8 +80,8 @@ package org.gestouch.input installStageListeners(); var touch:Touch = _touchesManager.createTouch(); - touch.id = 0; 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::setTime(getTimer()); touch.gestouch_internal::setBeginTime(getTimer()); @@ -91,6 +94,8 @@ package org.gestouch.input 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. // Also helps to ignore possible fake events. if (!_touchesManager.hasTouch(PRIMARY_TOUCH_POINT_ID)) @@ -106,10 +111,8 @@ package org.gestouch.input 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; - + return;//we listen in capture or at_target (to catch on empty stage) // Way to prevent MouseEvent/TouchEvent collisions. // Also helps to ignore possible fake events. diff --git a/src/org/gestouch/input/TouchInputAdapter.as b/src/org/gestouch/input/TouchInputAdapter.as index b86afab..99f82f4 100644 --- a/src/org/gestouch/input/TouchInputAdapter.as +++ b/src/org/gestouch/input/TouchInputAdapter.as @@ -8,6 +8,8 @@ package org.gestouch.input import flash.events.EventPhase; import flash.events.TouchEvent; import flash.geom.Point; + import flash.ui.Multitouch; + import flash.ui.MultitouchInputMode; import flash.utils.getTimer; @@ -25,6 +27,10 @@ package org.gestouch.input */ protected var _touchesMap:Object = {}; + { + Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; + } + public function TouchInputAdapter(stage:Stage) { @@ -42,12 +48,14 @@ package org.gestouch.input override public function init():void { _stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true); + _stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler);// to catch with EventPhase.AT_TARGET } override public function dispose():void { _stage.removeEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler, true); + _stage.removeEventListener(TouchEvent.TOUCH_BEGIN, touchBeginHandler); uninstallStageListeners(); } @@ -56,8 +64,8 @@ package org.gestouch.input { // Maximum priority to prevent event hijacking _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); - // To catch event out of stage _stage.addEventListener(TouchEvent.TOUCH_END, touchEndHandler, false, int.MAX_VALUE); } @@ -65,6 +73,7 @@ package org.gestouch.input protected function uninstallStageListeners():void { _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); } @@ -72,6 +81,8 @@ package org.gestouch.input 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. // Also helps to ignore possible fake events. if (_touchesManager.hasTouch(event.touchPointID)) @@ -107,6 +118,8 @@ package org.gestouch.input 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. // Also helps to ignore possible fake events. if (!_touchesManager.hasTouch(event.touchPointID) || !_touchesMap.hasOwnProperty(event.touchPointID)) @@ -133,9 +146,8 @@ package org.gestouch.input 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; + return;//we listen in capture or at_target (to catch on empty stage) // Way to prevent MouseEvent/TouchEvent collisions. // Also helps to ignore possible fake events.