-
Notifications
You must be signed in to change notification settings - Fork 0
/
GestureManager.hx
63 lines (51 loc) · 1.78 KB
/
GestureManager.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package deengames.io;
import flixel.util.FlxPoint;
import flixel.FlxG;
class GestureManager
{
private var gestureStart:FlxPoint;
// TODO: support more than one callback per gesture
// TODO: take a sprite or pressed-on object instead of the direction only
private var callbacks:Map<Gesture, SwipeDirection->Void> = new Map<Gesture, SwipeDirection->Void>();
public function new() { }
public function onGesture(gesture:Gesture, callback:SwipeDirection->Void)
{
callbacks.set(gesture, callback);
}
// Call this every tick
public function update() : Void {
if (FlxG.mouse.pressed && gestureStart == null) {
// TODO: use world position
this.gestureStart = FlxG.mouse.getScreenPosition();
} else if (!FlxG.mouse.pressed && gestureStart != null) {
var gesture:Gesture = Gesture.Swipe; // TODO: detect, implement more
var gestureStop:FlxPoint = FlxG.mouse.getScreenPosition();
var vector:FlxPoint = new FlxPoint(gestureStop.x - gestureStart.x, gestureStop.y - gestureStart.y);
if ((vector.x * vector.x) + (vector.y + vector.y) <= 25) {
// Too small to tell what the user wants; movement of 4x4 pixels or so
return;
} else {
var swipeDirection:SwipeDirection;
if (Math.abs(vector.x) >= Math.abs(vector.y)) {
swipeDirection = vector.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
} else {
swipeDirection = vector.y > 0 ? SwipeDirection.Down : SwipeDirection.Up;
}
if (this.callbacks.exists(Gesture.Swipe)) {
var callback:SwipeDirection->Void = this.callbacks.get(Gesture.Swipe);
callback(swipeDirection);
}
gestureStart = null;
}
}
}
}
enum Gesture {
Swipe;
}
enum SwipeDirection {
Left;
Right;
Up;
Down;
}