-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDjay.java
329 lines (308 loc) · 9.29 KB
/
Djay.java
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
323
324
325
326
327
328
329
import java.awt.*;
import java.awt.event.*;
/* DJ with a wand in Djay using Leap Motion */
class DjayListener extends Listener {
final static int BOTTOM = 1, TOPLEFT = 2, TOPRIGHT = 3, NOTHING = 0;
final static int SWIPELEFT = 1, SWIPERIGHT = 2, SCROLL = 3, SWIPEUP = 4, SCRATCH = 5;
final static int LEFT=0, RIGHT=1, UP=2;
final static int DOWN = 1, FLATISH = 3;
final int FRAMETHRESHOLD=20;
int area = NOTHING; // area detects whether a hand is in BOTTOM, TOPLEFT, or TOPRIGHT;
int pointDirection = NOTHING;
int event = NOTHING;
int amount = 0;
int delayAmount = 100; //shortest period between two of the same gesture
int scrollDelayAmount = 2;
int scratchDelayAmount = 2;
int actionCounter = 0;
int scrollCounter = 0;
int scratchCounter = 0;
boolean present = true;
public void onInit(Controller controller) {
System.out.println("Initialized");
}
public void onConnect(Controller controller) {
System.out.println("Connected");
}
public void onDisconnect(Controller controller) {
System.out.println("Disconnected");
}
int getArea(Vector pos) {
if (pos!=null){
if (pos.getX()<0) {
return LEFT;
} else {
return RIGHT;
}
}
return NOTHING;
}
int getPointDirection(Finger finger) {
int yDirection = (int)(Math.atan2(finger.tip().getDirection().getZ(), finger.tip().getDirection().getY()) * 180/Math.PI + 180);
if (yDirection > 180) yDirection -= 360;
if (yDirection > -20) {
return UP;
}
else if (yDirection < -120) {
return DOWN;
}
else {
return FLATISH;
}
}
int[] getEvent(FingerArray fingers, Controller controller) {
int[] eventAr={NOTHING,NOTHING};
if (pointDirection == UP) {
eventAr[0]=SCROLL;
eventAr[1]=(int)(fingers.get(0).velocity().getX())/-100;
} else if (testSwipeLRU(controller,LEFT)){
eventAr[0]=SWIPELEFT;
eventAr[1]=0;
} else if (testSwipeLRU(controller,RIGHT)) {
eventAr[0]=SWIPERIGHT;
eventAr[1]=0;
} else if (testSwipeLRU(controller,UP)) {
eventAr[0]=SWIPEUP;
eventAr[1]=0;
} else if (pointDirection == DOWN){
eventAr[0]=SCRATCH;
eventAr[1]=(int)(fingers.get(0).velocity().getZ()/50);
}
return eventAr;
}
public Boolean testSwipeLRU(Controller controller,int space){
Frame tempframe;
HandArray temphands;
long tempnumHands;
Frame mainframe = controller.frame();
HandArray hands = mainframe.hands();
long numHands = hands.size();
for (int i=0;i<numHands;i++){
Boolean swipe[] = new Boolean[FRAMETHRESHOLD];
for (int k=0;k<FRAMETHRESHOLD;k++){
tempframe = controller.frame(k);
temphands = tempframe.hands();
tempnumHands = temphands.size();
if (tempnumHands>=numHands || tempnumHands>i){
Hand hand=hands.get(i);
FingerArray fingers = hand.fingers();
long numFingers= fingers.size();
Boolean validfinger=false;
if (numFingers>=1){
for (int j=0;j < numFingers;j++){
Finger finger = fingers.get(j);
if (space==0){
if (finger.velocity().getX()>700 && Math.abs(finger.velocity().getY())<250 && Math.abs(finger.velocity().getZ())<250){
validfinger=true;
}
} else if (space==1){
if (finger.velocity().getX()<-700 && Math.abs(finger.velocity().getY())<250 && Math.abs(finger.velocity().getZ())<250){
validfinger=true;
}
} else {
if (finger.velocity().getY()>1000){
validfinger=true;
}
}
}
}
swipe[k]=validfinger;
} else {
swipe[k]=false;
}
}
Double total=0.0;
for (int k=0;k<FRAMETHRESHOLD;k++){
if (swipe[k]==true){
total++;
}
}
if ((total/FRAMETHRESHOLD)>.85){
return true;
}
}
return false;
}
Vector getPos(FingerArray fingers, long numFingers){
Vector pos = new Vector(0, 0, 0);
for (int i = 0; i < numFingers; ++i) {
Finger finger = fingers.get(i);
Ray tip = finger.tip();
pos.setX(pos.getX() + tip.getPosition().getX());
pos.setY(pos.getY() + tip.getPosition().getY());
pos.setZ(pos.getZ() + tip.getPosition().getZ());
}
pos = new Vector(pos.getX()/numFingers, pos.getY()/numFingers, pos.getZ()/numFingers);
return pos;
}
public void onFrame(Controller controller) {
// update counters
if (scrollCounter > 0) {
scrollCounter--;
}
if (scratchCounter > 0) {
scratchCounter--;
}
if (actionCounter > 0) {
actionCounter--;
}
Frame frame = controller.frame();
HandArray hands = frame.hands();
long numHands = hands.size();
event=NOTHING;
area = NOTHING;
pointDirection = NOTHING;
present = false;
if (numHands >= 1) {
present = true;
// Get the first hand
Hand hand = hands.get(0);
// Check if the hand has any fingers
FingerArray fingers = hand.fingers();
long numFingers = fingers.size();
Vector pos=null;
if (numFingers >= 1) {
pos=getPos(fingers,numFingers);
area = getArea(pos);
pointDirection = getPointDirection(fingers.get(0));
} else {
// Check if the hand has a palm
Ray palmRay = hand.palm();
if (palmRay != null) {
Vector palm = palmRay.getPosition();
//fingers not detected, get area of your hand
Vector palmPos = new Vector(palm.getX(), palm.getY(), palm.getZ());
area = getArea(palmPos);
}
}
int[] eventAr=getEvent(fingers, controller);
event=eventAr[0];
amount=eventAr[1];
}
// key presses
if (present) {
try {
Robot robot = new Robot();
if (pointDirection == DjayListener.UP) {
if (event == DjayListener.SCROLL) {
if (scrollCounter == 0) {
robot.mouseMove(500,700);
robot.mouseWheel(amount);
scrollCounter = scrollDelayAmount;
}
}
}
else if (area == DjayListener.LEFT) {
switch (event) {
case DjayListener.SWIPEUP: {
if (actionCounter == 0) {
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
actionCounter = delayAmount;
}
break;
}
case DjayListener.SWIPERIGHT: {
if (actionCounter == 0) {
robot.keyPress(KeyEvent.VK_META); //load
robot.keyPress(KeyEvent.VK_LEFT);
robot.keyRelease(KeyEvent.VK_LEFT);
robot.keyRelease(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_2); //sync
robot.keyRelease(KeyEvent.VK_2);
robot.keyPress(KeyEvent.VK_DOWN); //next song
robot.keyRelease(KeyEvent.VK_DOWN);
actionCounter = delayAmount;
}
break;
}
case DjayListener.SWIPELEFT: {
if (actionCounter == 0) {
robot.keyPress(KeyEvent.VK_META); //load
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_9); //sync
robot.keyRelease(KeyEvent.VK_9);
robot.keyPress(KeyEvent.VK_DOWN); //next song
robot.keyRelease(KeyEvent.VK_DOWN);
actionCounter = delayAmount;
}
break;
}
case DjayListener.SCRATCH: {
if (scratchCounter == 0) {
robot.mouseMove(350,365);
robot.mouseWheel(amount);
scratchCounter = scratchDelayAmount;
}
break;
}
}
}
else if (area == DjayListener.RIGHT) {
switch (event) {
case DjayListener.SWIPEUP: {
if (actionCounter == 0) {
robot.keyPress(KeyEvent.VK_0);
robot.keyRelease(KeyEvent.VK_0);
actionCounter = delayAmount;
}
break;
}
case DjayListener.SWIPERIGHT: {
if (actionCounter == 0) {
robot.keyPress(KeyEvent.VK_META); //load
robot.keyPress(KeyEvent.VK_LEFT);
robot.keyRelease(KeyEvent.VK_LEFT);
robot.keyRelease(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_2); //sync
robot.keyRelease(KeyEvent.VK_2);
robot.keyPress(KeyEvent.VK_DOWN); //next song
robot.keyRelease(KeyEvent.VK_DOWN);
actionCounter = delayAmount;
}
break;
}
case DjayListener.SWIPELEFT: {
if (actionCounter == 0) {
robot.keyPress(KeyEvent.VK_META); //load
robot.keyPress(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_RIGHT);
robot.keyRelease(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_9); //sync
robot.keyRelease(KeyEvent.VK_9);
robot.keyPress(KeyEvent.VK_DOWN); //next song
robot.keyRelease(KeyEvent.VK_DOWN);
actionCounter = delayAmount;
}
break;
}
case DjayListener.SCRATCH: {
if (scratchCounter == 0) {
robot.mouseMove(600, 365);
robot.mouseWheel(amount);
scratchCounter = scratchDelayAmount;
}
break;
}
}
}
} catch (AWTException e) {
e.printStackTrace();
}
}
}
}
class Djay {
public static void main(String[] args) {
// Create a sample listener and assign it to a controller to receive events
DjayListener listener = new DjayListener();
Controller controller = new Controller(listener);
// Keep this process running until Enter is pressed
System.out.println("Press Enter to quit...");
System.console().readLine();
// The controller must be disposed of before the listener
controller = null;
}
}