[add] add GestureManager
This commit is contained in:
7
src/haxe/ru/m/event/GestureEvent.hx
Normal file
7
src/haxe/ru/m/event/GestureEvent.hx
Normal file
@@ -0,0 +1,7 @@
|
||||
package ru.m.event;
|
||||
|
||||
import flash.events.Event;
|
||||
|
||||
class GestureEvent extends Event {
|
||||
|
||||
}
|
||||
73
src/haxe/ru/m/event/GestureManager.hx
Normal file
73
src/haxe/ru/m/event/GestureManager.hx
Normal file
@@ -0,0 +1,73 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
7
src/haxe/ru/m/event/ZoomGestureEvent.hx
Normal file
7
src/haxe/ru/m/event/ZoomGestureEvent.hx
Normal file
@@ -0,0 +1,7 @@
|
||||
package ru.m.event;
|
||||
|
||||
class ZoomGestureEvent extends GestureEvent {
|
||||
public static var GESTURE_ZOOM(default, never):String = "gesture_zoom";
|
||||
|
||||
public var zoom(default, default):Float;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.puzzlez.render;
|
||||
|
||||
import ru.m.event.ZoomGestureEvent;
|
||||
import ru.m.event.GestureManager;
|
||||
import flash.display.DisplayObject;
|
||||
import flash.events.MouseEvent;
|
||||
import flash.geom.Point;
|
||||
@@ -11,11 +13,21 @@ class RenderManager {
|
||||
private var content:DisplayObject;
|
||||
private var container:DisplayObject;
|
||||
private var movePoint:Point;
|
||||
private var gesture:GestureManager;
|
||||
|
||||
public function new(content:DisplayObject, container:DisplayObject) {
|
||||
this.content = content;
|
||||
this.container = container;
|
||||
content.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
|
||||
content.addEventListener(ZoomGestureEvent.GESTURE_ZOOM, onGestureZoom);
|
||||
gesture = new GestureManager(content);
|
||||
}
|
||||
|
||||
private function onGestureZoom(event:ZoomGestureEvent):Void {
|
||||
if (locked) {
|
||||
return;
|
||||
}
|
||||
container.scaleX = container.scaleY += event.zoom;
|
||||
}
|
||||
|
||||
private function onMouseDown(event:MouseEvent):Void {
|
||||
@@ -47,6 +59,10 @@ class RenderManager {
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
if (gesture != null) {
|
||||
gesture.dispose();
|
||||
gesture = null;
|
||||
}
|
||||
content.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user