73 lines
2.1 KiB
Haxe
73 lines
2.1 KiB
Haxe
package ru.m.event;
|
|
|
|
import flash.geom.Point;
|
|
import flash.display.DisplayObject;
|
|
import flash.events.TouchEvent;
|
|
|
|
typedef Touch = {
|
|
var id:Int;
|
|
var point:Point;
|
|
}
|
|
|
|
class GestureManager {
|
|
private var target:DisplayObject;
|
|
private var touchesMap:Map<Int, Touch>;
|
|
private var touches:Array<Touch>;
|
|
private var distance:Float;
|
|
|
|
public function new(target:DisplayObject) {
|
|
this.target = target;
|
|
touchesMap = new Map();
|
|
touches = new Array();
|
|
target.addEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
|
|
target.addEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
|
|
target.addEventListener(TouchEvent.TOUCH_END, onTouchEnd);
|
|
}
|
|
|
|
private function addTouch(id:Int, point:Point):Void {
|
|
var touch:Touch = {id: id, point: point};
|
|
touchesMap.set(touch.id, touch);
|
|
touches.push(touch);
|
|
if (touches.length == 2) {
|
|
distance = Point.distance(touches[0].point, touches[1].point);
|
|
}
|
|
}
|
|
|
|
private function updateTouch(id:Int, point:Point):Void {
|
|
touchesMap.get(id).point = point;
|
|
if (touches.length == 2) {
|
|
var newDistance = Point.distance(touches[0].point, touches[1].point);
|
|
var event = new ZoomGestureEvent(ZoomGestureEvent.GESTURE_ZOOM);
|
|
event.zoom = (newDistance - distance) * 0.001;
|
|
distance = newDistance;
|
|
target.dispatchEvent(event);
|
|
}
|
|
}
|
|
|
|
private function removeTouch(id:Int):Void {
|
|
touches.remove(touchesMap.get(id));
|
|
touchesMap.remove(id);
|
|
}
|
|
|
|
private function onTouchBegin(event:TouchEvent):Void {
|
|
addTouch(event.touchPointID, new Point(event.stageX, event.stageY));
|
|
}
|
|
|
|
private function onTouchMove(event:TouchEvent):Void {
|
|
updateTouch(event.touchPointID, new Point(event.stageX, event.stageY));
|
|
}
|
|
|
|
private function onTouchEnd(event:TouchEvent):Void {
|
|
removeTouch(event.touchPointID);
|
|
}
|
|
|
|
public function dispose():Void {
|
|
if (target != null) {
|
|
target.removeEventListener(TouchEvent.TOUCH_BEGIN, onTouchBegin);
|
|
target.removeEventListener(TouchEvent.TOUCH_MOVE, onTouchMove);
|
|
target.removeEventListener(TouchEvent.TOUCH_END, onTouchEnd);
|
|
target = null;
|
|
}
|
|
}
|
|
}
|