-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
322 lines (275 loc) · 9.14 KB
/
Main.java
File metadata and controls
322 lines (275 loc) · 9.14 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import java.io.IOException;
import java.lang.Math;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.leapmotion.leap.*;
import com.leapmotion.leap.Controller.PolicyFlag;
import com.leapmotion.leap.Gesture.State;
import com.leapmotion.leap.Gesture.Type;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
class GestureInfo {
Pointable p;
Vector direction;
Vector position;
public GestureInfo(Pointable p, Vector direction, Vector position) {
super();
this.p = p;
this.direction = direction;
this.position = position;
}
}
public class Main extends Listener {
Robot _robot;
Map<Integer, GestureInfo> boundFingers = new HashMap();
List<Integer> circles = new ArrayList();
List<Integer> lrSwipes = new ArrayList();
Mappings _mappings;
public Main() {
try {
_robot = new Robot();
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_robot.delay(2000);
_mappings = new Mappings(_robot);
// example: mappings.doAction(Actions.SWIPE_LEFT);
}
// public static long lastKeypress = 0;
// public static String lastActiontype = null;
private void keyPress(String action) {
// long now = System.currentTimeMillis();
// if(now - lastKeypress > 500 || lastActiontype.equals(action)) {
// lastKeypress = now;
// lastActiontype = action;
_mappings.doAction(action);
// }
}
public void onInit(Controller controller) {
}
public void onConnect(Controller controller) {
controller.enableGesture(Gesture.Type.TYPE_SWIPE);
controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);
controller.enableGesture(Gesture.Type.TYPE_CIRCLE);
controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
Config config = controller.config();
config.setFloat("Gesture.Swipe.MinLength", 150);
config.setFloat("Gesture.Swipe.MinVelocity", 400);
config.setFloat("Gesture.Circle.MinArc", (float) Math.PI * 1.8f);
config.setFloat("Gesture.Circle.MinRadius", 25);
config.save();
}
public void onDisconnect(Controller controller) {
// Note: not dispatched when running in a debugger.
System.out.println("Disconnected");
}
public void onExit(Controller controller) {
System.out.println("Exited");
}
public void goDown(Pointable p, Vector position) {
if (!boundFingers.containsKey(p.id())) {
GestureInfo gi = new GestureInfo(p, Vector.down(), position);
boundFingers.put(p.id(), gi);
}
if (boundFingers.get(p.id()).direction.equals(Vector.down())) {
boundFingers.get(p.id()).position = position;
System.out.println("going down " + p.id());
keyPress(Actions.SWIPE_DOWN);
_robot.delay(50);
}
}
public void goUp(Pointable p, Vector position) {
if (!boundFingers.containsKey(p.id())) {
GestureInfo gi = new GestureInfo(p, Vector.up(), position);
boundFingers.put(p.id(), gi);
}
if (boundFingers.get(p.id()).direction.equals(Vector.up())) {
boundFingers.get(p.id()).position = position;
System.out.println("going up " + p.id());
keyPress(Actions.SWIPE_UP);
_robot.delay(50);
}
}
public void goDirection(Pointable f) {
GestureInfo gi = boundFingers.get(f.id());
Vector v = gi.direction;
if (v.equals(Vector.up())) {
goUp(f, gi.position);
}
if (v.equals(Vector.down())) {
goDown(f, gi.position);
}
}
public void onFrame(Controller controller) {
Frame frame = controller.frame();
// Sy2221stem.out.println(frame.hands().count());
// Hand hand = frame.hands().frontmost();
// if(hand != null) {
// Frame lastFrame = controller.frame(5);
// Hand lastHand = lastFrame.hand(hand.id());
// System.out.println("hands: "+hand.pointables().count()+", "+hand.pointables().count());
// if(hand.pointables().count() >= 2 && lastHand.pointables().count() >=
// 2 ) {
// float lastRadius = lastHand.sphereRadius();
// float currRadius = hand.sphereRadius();
//
// float radiusDifference = currRadius - lastRadius;
// float threshold = 1.5f;
//
// System.out.println("radiusDifference: " + radiusDifference);
// if( radiusDifference > threshold ) {
// keyPress(Actions.ZOOM_OUT);
// } else if (radiusDifference < -threshold) {
// keyPress(Actions.ZOOM_IN);
// }
//
// }
// return;
// }
PointableList fingers = frame.pointables();
// System.out.println(fingers.count());
if(fingers.count() == 10) {
_mappings.switchMapping();
System.out.println("SWITCHED MAPPING MOVE FINGERS AWAY");
_robot.delay(1000);
System.out.println("LOLOLOL?");
}
// if (fingers.count() != 1)
// return;
for (Pointable finger : fingers) {
if (boundFingers.containsKey(finger.id())) {
GestureInfo gi = boundFingers.get(finger.id());
goDirection(finger);
if (finger.tipPosition().distanceTo(gi.position) > 70) {
boundFingers.remove(finger.id());
}
}
}
GestureList gestures = frame.gestures();
int numSwipes = 0;
for(Gesture g : gestures) {
if(g.type() == Type.TYPE_SWIPE)
numSwipes++;
}
for (int i = 0; i < gestures.count(); i++) {
Gesture gesture = gestures.get(i);
switch (gesture.type()) {
// Scroll up/down
case TYPE_SWIPE:
float CONE_ANGLE = (float) Math.PI / 3;
SwipeGesture swipe = new SwipeGesture(gesture);
System.out.println("Swiping with "+numSwipes+" fingers");
if (!lrSwipes.contains(swipe.id())
&& swipe.state().equals(Gesture.State.STATE_STOP)) {
if (swipe.direction().angleTo(Vector.right()) < CONE_ANGLE
&& swipe.position().getX() > 0) {
lrSwipes.add(swipe.id());
System.out.println("go right");
keyPress(Actions.SWIPE_RIGHT);
}
if (swipe.direction().angleTo(Vector.left()) < CONE_ANGLE
&& swipe.position().getX() < 0) {
lrSwipes.add(swipe.id());
System.out.println("go right");
keyPress(Actions.SWIPE_LEFT);
}
}
if(numSwipes < 2)
break;
if (swipe.direction().angleTo(Vector.down()) < CONE_ANGLE) {
goDown(swipe.pointable(), swipe.position());
}
if (swipe.direction().angleTo(Vector.up()) < CONE_ANGLE) {
goUp(swipe.pointable(), swipe.position());
}
System.out.println("Swipe id: " + swipe.id() + ", "
+ swipe.state() + ", position: " + swipe.position()
+ ", direction: " + swipe.direction() + ", speed: "
+ swipe.speed());
break;
// Type "f"
case TYPE_SCREEN_TAP:
ScreenTapGesture screenTap = new ScreenTapGesture(gesture);
System.out.println("Screen Tap id: " + screenTap.id() + ", "
+ screenTap.state() + ", position: "
+ screenTap.position() + ", direction: "
+ screenTap.direction());
keyPress(Actions.SCREEN_TAP);
break;
// Type 1-2-3-42
case TYPE_KEY_TAP:
KeyTapGesture keyTap = new KeyTapGesture(gesture);
System.out.println("Key Tap id: " + keyTap.id() + ", "
+ keyTap.state() + ", position: " + keyTap.position()
+ ", direction: " + keyTap.direction());
String numFromLeft = null;
int xPos = keyTap.position().getX() < 0 ? 0 : 1;
int yPos = keyTap.position().getY() < 150 ? 1 : 0;
String[] actions = new String[] {Actions.KEY_TAP_ONE, Actions.KEY_TAP_TWO, Actions.KEY_TAP_THREE, Actions.KEY_TAP_FOUR};
numFromLeft = actions[xPos + 2 * yPos];
System.out.println("Clicked key " + (xPos + 2 * yPos));
if(numFromLeft != null)
keyPress(numFromLeft);
break;
// Go forward/back
case TYPE_CIRCLE:
CircleGesture circle = new CircleGesture(gesture);
if (circle.progress() == 0 || circles.contains(circle.id()))
break;
if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI / 4) {
// clockwise
keyPress(Actions.CLOCKWISE);
} else {
// counterclockwise
keyPress(Actions.COUNTER_CLOCKWISE);
}
circles.add(circle.id());
// Calculate clock direction using the angle between circle
// normal and pointable
String clockwiseness;
if (circle.pointable().direction().angleTo(circle.normal()) <= Math.PI / 4) {
// Clockwise if angle is less than 90 degrees
clockwiseness = "clockwise";
} else {
clockwiseness = "counterclockwise";
}
// Calculate angle swept since last frame
double sweptAngle = 0;
if (circle.state() != State.STATE_START) {
CircleGesture previousUpdate = new CircleGesture(controller
.frame(1).gesture(circle.id()));
sweptAngle = (circle.progress() - previousUpdate.progress())
* 2 * Math.PI;
}
System.out.println("Circle id: " + circle.id() + ", "
+ circle.state() + ", progress: " + circle.progress()
+ ", radius: " + circle.radius() + ", angle: "
+ Math.toDegrees(sweptAngle) + ", " + clockwiseness);
break;
default:
System.out.println("Unknown gesture type.");
break;
}
}
}
public static void main(String[] args) {
Controller controller = new Controller();
controller.setPolicyFlags(PolicyFlag.POLICY_BACKGROUND_FRAMES);
Main main = new Main();
controller.addListener(main);
// Keep this process running until Enter is pressed
System.out.println("Press Enter to quit...");
try {
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
// Remove the sample listener when done
controller.removeListener(main);
}
}