-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathLeapListener.cs
More file actions
162 lines (160 loc) · 6.74 KB
/
LeapListener.cs
File metadata and controls
162 lines (160 loc) · 6.74 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using Leap;
using System.Windows;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
namespace LeapMotionDriverWindow
{
class LeapListener: Leap.Listener
{
public delegate void GripMoveHandler(System.Windows.Vector position);
public delegate void FingerPointerHandler(Point pointer);
public delegate void SwipeDirectionHandler(System.Windows.Vector direction);
public event FingerPointerHandler OnFingerPointer;
public event GripMoveHandler OnGripMove;
public event EventHandler OnGriped;
public event EventHandler OnGripReleased;
public event EventHandler OnPalmOpened;
public event SwipeDirectionHandler OnSwiped;
private readonly Point EMPTY_POINT = new Point(-999, -999);
private Point lastHandGrabPosition;
public LeapListener()
{
lastHandGrabPosition = EMPTY_POINT;
}
public override void OnInit(Controller cntrlr)
{
Console.WriteLine("Initialized");//123
Console.WriteLine("Initialized");//123
}
public override void OnConnect(Controller cntrlr)
{
Console.WriteLine("Connected");
}
public override void OnDisconnect(Controller cntrlr)
{
Console.WriteLine("Disconnected");
}
public override void OnExit(Controller cntrlr)
{
Console.WriteLine("Exit");
Console.WriteLine("Exited");
}
public override void OnFrame(Controller controller)
{
Frame currentFrame = controller.Frame();
GripTranslation(controller, currentFrame);
FingerPointer(controller, currentFrame);
GestureRecognize(currentFrame);
CheckPalmOpen(currentFrame);
}
private void GestureRecognize(Frame currentFrame)
{
for (int g = 0; g < currentFrame.Gestures().Count; g++)
{
switch (currentFrame.Gestures()[g].Type)
{
case Gesture.GestureType.TYPE_CIRCLE:
//Handle circle gestures
break;
case Gesture.GestureType.TYPE_KEY_TAP:
//Handle key tap gestures
break;
case Gesture.GestureType.TYPE_SCREEN_TAP:
//Handle screen tap gestures
break;
case Gesture.GestureType.TYPE_SWIPE:
HandleSwipe(new SwipeGesture(currentFrame.Gestures()[g]));
break;
default:
//Handle unrecognized gestures
break;
}
}
}
private void HandleSwipe(SwipeGesture swipe)
{
switch (swipe.State)
{
case Gesture.GestureState.STATE_START:
break;
case Gesture.GestureState.STATE_UPDATE:
break;
case Gesture.GestureState.STATE_STOP:
if (this.OnSwiped != null)
this.OnSwiped(new System.Windows.Vector(swipe.Direction.x, swipe.Direction.y));
break;
default:
break;
}
}
private void FingerPointer(Controller controller, Frame currentFrame)
{
if (currentFrame.Hands.IsEmpty) return;
if (currentFrame.Fingers.Count < 1) return;
var finger = currentFrame.Fingers[0];
var screen = controller.LocatedScreens.ClosestScreenHit(finger);
if (screen != null && screen.IsValid)
{
var tipVelocity = (int)finger.TipVelocity.Magnitude;
if (tipVelocity > 25)
{
var xScreenIntersect = screen.Intersect(finger, true).x;
var yScreenIntersect = screen.Intersect(finger, true).y;
if (xScreenIntersect.ToString() != "NaN")
{
var x = (int)(xScreenIntersect * screen.WidthPixels);
var y = (int)(screen.HeightPixels - (yScreenIntersect * screen.HeightPixels));
if (this.OnFingerPointer != null)
this.OnFingerPointer(new Point(x, y));
}
}
}
}
private void CheckPalmOpen(Frame currentFrame)
{
if (currentFrame.Hands.Count > 0 && currentFrame.Hands[0].Direction.y > 0)
{
if (this.OnPalmOpened != null) this.OnPalmOpened(this, EventArgs.Empty);
}
}
private void GripTranslation(Controller controller, Frame currentFrame)
{
if (currentFrame.Hands.Count > 0 && currentFrame.Hands[0].GrabStrength > 0.9)
{
var grabHand = currentFrame.Hands[0];
if (lastHandGrabPosition == EMPTY_POINT)
{
lastHandGrabPosition = new Point(grabHand.StabilizedPalmPosition.x, grabHand.StabilizedPalmPosition.y);
if (this.OnGriped != null)
this.OnGriped(this, EventArgs.Empty);
}
else
{
var screen = controller.LocatedScreens.ClosestScreenHit(grabHand.StabilizedPalmPosition, grabHand.Direction);
if (screen != null && screen.IsValid)
{
var xScreenIntersect = screen.Intersect(grabHand.StabilizedPalmPosition, grabHand.Direction, true).x;
var yScreenIntersect = screen.Intersect(grabHand.StabilizedPalmPosition, grabHand.Direction, true).y;
if (xScreenIntersect.ToString() != "NaN")
{
var x = (int)(xScreenIntersect * screen.WidthPixels);
var y = (int)(screen.HeightPixels - (yScreenIntersect * screen.HeightPixels));
if (this.OnGripMove != null)
this.OnGripMove(new System.Windows.Vector(x, y));
}
lastHandGrabPosition = new Point(grabHand.StabilizedPalmPosition.x, grabHand.StabilizedPalmPosition.y);
}
}
}
else
{
if (lastHandGrabPosition == EMPTY_POINT) return;
lastHandGrabPosition = EMPTY_POINT;
if (this.OnGripReleased != null)
this.OnGripReleased(this, EventArgs.Empty);
}
}
}
}