I started porting my Touch manager from haxe starling. I'm not finished but it looks a bit like this:
TouchManager Class
package starling.extensions.touchmanager{
import starling.core.Starling;
import starling.events.TouchEvent;
import starling.events.Touch;
import starling.events.TouchPhase;
import flash.events.MouseEvent;
import starling.display.Sprite;
import flash.geom.Point;
public class TouchManager {
private var __touchMap:Object;
private var __touchRegistry:Array;
private var __starling:Starling;
public static var current:TouchManager;
public function TouchManager(starling:Starling) {
(__starling = starling).stage.addEventListener(TouchEvent.TOUCH, __onTouchReceived);
__touchRegistry = [];
__touchMap = new Object();
__starling.nativeStage.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN, __onRightMouseDown);
}
public static function init(starling:Starling):TouchManager{
return current = new TouchManager(starling);
}
public static function addTouch(touchObject:TouchObject):String{
if(current.__touchMap[touchObject.id] == undefined){
current.__touchMap[touchObject.id] = touchObject;
current.__touchRegistry.push(touchObject);
} else {
throw("Touch Map can only contain a single reference per id");
}
return touchObject.id;
}
public static function removeTouch(id:String):void{
current.__touchRegistry.removeAt(current.__touchRegistry.indexOf(current.__touchMap[id]));
current.__touchMap[id] = null;
delete current.__touchMap[id];
}
private function __onTouchReceived(e:TouchEvent):void{
for(var i:int = 0; i<__touchRegistry.length;i++){
var touchObject:TouchObject = __touchRegistry[i];
var touch:Touch = e.getTouch(touchObject.displayObject, TouchPhase.BEGAN);
if(touch&&touchObject.onDownCallback!=null) touchObject.onDownCallback(touch, touchObject.displayObject);
else if((touch=e.getTouch(touchObject.displayObject, TouchPhase.MOVED))&&touchObject.onMovedCallback!=null) touchObject.onMovedCallback(touch, touchObject.displayObject);
else if((touch=e.getTouch(touchObject.displayObject, TouchPhase.ENDED))&&touchObject.onUpCallback!=null) touchObject.onUpCallback(touch, touchObject.displayObject);
}
}
private function __onRightMouseDown(e:MouseEvent):void{
var globalPoint:Point = new Point(e.stageX,e.stageY)
for(var i:int = 0; i<__touchRegistry.length;i++){
var touchObject:TouchObject = __touchRegistry[i];
if(touchObject.displayObject is Sprite){
//if(!Sprite(touchObject.displayObject).touchGroup){
var localPoint:Point = touchObject.displayObject.globalToLocal(globalPoint);
if(touchObject.displayObject.bounds.containsPoint(localPoint)){
touchObject.onRightClickCallback(localPoint, globalPoint, touchObject.displayObject);
}
//}
}
}
}
}
}
TouchObject Class
package starling.extensions.touchmanager{
import starling.display.DisplayObject;
public class TouchObject {
public var id: String;
public var displayObject: DisplayObject;
public var onDownCallback: Function;
public var onUpCallback: Function;
public var onOverCallback: Function;
public var onOutCallback: Function;
public var onMovedCallback: Function;
public var onRightClickCallback: Function;
public function TouchObject(id: String, displayObject: DisplayObject = null, onDownCallback: Function = null, onUpCallback: Function = null, onOverCallback: Function = null, onOutCallback: Function = null, onMovedCallback: Function = null, onRightClickCallback:Function = null) {
this.id = id;
this.displayObject = displayObject;
this.onDownCallback = onDownCallback;
this.onUpCallback = onUpCallback;
this.onOverCallback = onOverCallback;
this.onOutCallback = onOutCallback;
this.onMovedCallback = onMovedCallback;
this.onRightClickCallback = onRightClickCallback;
}
}
}
I need to wrap it up and do some optimizations I didnt really need in haxe.
In purpose, it allows you to handle mouse or touch events globally in a more intuitive "a la carte" way more akin to the regular flash display list. To use it, you call the static method Init and then use the static addTouch method to pass a TouchObject containing an arbitrary id for the touch event, and optional callback functions for each phase including right click.
When you need to dispose of the TouchObject (you dont need it anymore) simply call the static method removeTouch() and pass the id of the touch object you want to dispose of.
Usage:
TouchManager.Init();
var quad:Quad = new Quad(32,32,0xFFFFFF);
someSprite.addChild(quad);
function onDown(touch:Touch, displayObject:DisplayObject):void{
//do something on mouse down
trace("MOUSE_DOWN");
}
TouchManager.addTouch("randomQuad", quad, onDown);
TouchManager.removeTouch("randomQuad");