-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectUser.pde
409 lines (332 loc) · 14.9 KB
/
KinectUser.pde
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* Copyright (c) 2010 The Jackson Laboratory
*
* This software was developed by Matt Hibbs' Lab at The Jackson
* Laboratory (see http://cbfg.jax.org/).
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This class stores information about a user interacting through the Kinect.
*/
class KinectUser {
public static final float DEPTH_LOWER = 200.0;
public static final float DEPTH_UPPER = 800.0;
int ID;
PVector lefthand = null; // hand coords
PVector righthand = null;
PVector plefthand; // previous hand coords
PVector prighthand;
PVector dragstart = null;
PVector CoM; // center of mass coords, used for hand depth
PVector[] arrow = new PVector[] {
new PVector(-16, 0),
new PVector(-16, -8),
new PVector(-8, -8),
new PVector(-8, -32),
new PVector(-32, -32),
new PVector(0, -sin(PI / 3.0) * 64.0),
new PVector(32, -32),
new PVector(8, -32),
new PVector(8, -8),
new PVector(16, -8),
new PVector(16, 0)
};
long righthandDown = -1; // time hold start
long lefthandDown = -1; // same, but for angle
long sameDistTime = -1; // time that the hand distance has been the same
float rightvelocity; // pixels/second
float leftangle; // hand angle in degrees
float handDiff; // hand horizontal distance
float phandDiff = -1.0;
float firsthandDiff = -1.0;
boolean ready = true;
boolean leftReady = true;
boolean dragZoom = false;
boolean zoomReady = true;
boolean dragReady = true;
boolean pressed = false;
PVector cursorPos;
PFont big = createFont("Arial", 64.0, true);
KinectUser() {
cursorPos = new PVector(drawWidth / 2.0, drawHeight / 2.0);
}
KinectUser(float x, float y) {
cursorPos = new PVector(x, y);
}
boolean update(PVector newleft, PVector newright, PVector newCoM) {
textFont(big);
if (pressed) {
pressed = false;
}
float seconds = (System.currentTimeMillis() - lastTime) / 1000.0;
boolean retVal = false;
lastTime = System.currentTimeMillis();
plefthand = lefthand;
lefthand = newleft;
if (righthand != null) {
rightvelocity = (float)dist(newright.x, newright.y, righthand.x, righthand.y) / seconds;
if (prighthand != null && newCoM.z - righthand.z > DEPTH_LOWER) {
float coef = map(newCoM.z - righthand.z , 0, DEPTH_UPPER - DEPTH_LOWER, 0.1, 2);
cursorPos.x += coef * (righthand.x - prighthand.x);
cursorPos.y += coef * (righthand.y - prighthand.y);
}
if (cursorPos.x > drawWidth) {
cursorPos.x = drawWidth;
} else if (cursorPos.x < 0) {
cursorPos.x = 0;
}
if (cursorPos.y > drawHeight) {
cursorPos.y = drawHeight;
} else if (cursorPos.y < 0) {
cursorPos.y = 0;
}
}
prighthand = righthand;
righthand = newright;
phandDiff = handDiff;
handDiff = abs(newright.x - newleft.x);
CoM = newCoM;
leftangle = getAngle(newCoM, lefthand);
if (Float.toString(leftangle).equals("NaN")) {
leftangle = Float.NaN;
}
// right hand down
if (rightvelocity < 150.0 && newCoM.z - righthand.z > DEPTH_LOWER && !dragZoom && (lefthandDown == -1 || (System.currentTimeMillis() - lefthandDown) > 3000)) {
if (righthandDown == -1) {
righthandDown = System.currentTimeMillis();
}
} else {
righthandDown = -1;
ready = true;
}
if (dragstart != null && !(CoM.z - lefthand.z > DEPTH_LOWER)) { // dragstart is about to be set to null, tell components to stop panning
switch (tabs.currentpage) {
case 0:
if (lefthand.x > filebrowser.x && lefthand.x < filebrowser.x + filebrowser.cWidth && lefthand.y > filebrowser.y && lefthand.y < filebrowser.y + filebrowser.cHeight) {
filebrowser.panEnd(ID);
}
((UIKTree)fileTree).panEnd(ID);
break;
case 1:
loddisplay.panEnd(ID);
break;
}
}
// left and right hands down
if (CoM.z - lefthand.z > DEPTH_LOWER && CoM.z - righthand.z > DEPTH_LOWER && zoomReady && tabs.currentpage == 1) {
double old = 1.0;
if (!dragZoom) {
firsthandDiff = abs(lefthand.x - righthand.x);
loddisplay.oldzoomFactor = old = loddisplay.zoomFactor;
loddisplay.zoomStart(ID);
}
if (tabs.currentpage == 1) {
old = loddisplay.zoomFactor;
loddisplay.zoomFactor = loddisplay.oldzoomFactor * (100.0 / map(handDiff, 0, firsthandDiff, 0, 100));
if (loddisplay.zoomFactor < 0.01 || loddisplay.zoomFactor > 1.0) {
loddisplay.zoomFactor = old;
} else {
if (loddisplay.current_chr == -1) {
loddisplay.offset -= ((old * chrTotal) - (loddisplay.zoomFactor * chrTotal)) / 2.0;
} else {
loddisplay.offset -= ((old * loddisplay.maxOffset) - (loddisplay.zoomFactor * loddisplay.maxOffset)) / 2.0;
}
}
}
dragZoom = true;
righthandDown = -1;
lefthandDown = -1;
leftReady = false; // don't register angles while zooming
ready = false;
dragstart = null;
} else if (leftangle > 100.0 && leftangle < 120.0 && dragstart == null) { // left hand in angle region
// NOTE: using center of mass (CoM) only provides one other point to use in calculating the angle
// for this reason, angles are larger than they otherwise would be
if (lefthandDown == -1) {
lefthandDown = System.currentTimeMillis();
}
if (System.currentTimeMillis() - lefthandDown > 3000) {
ready = true;
} else {
ready = false;
}
if (dragZoom) {
loddisplay.zoomEnd(ID);
}
dragZoom = false;
zoomReady = false;
} else if (CoM.z - lefthand.z > DEPTH_LOWER) {
if (dragstart == null) {
dragstart = lefthand;
switch (tabs.currentpage) {
case 0:
if (lefthand.x > filebrowser.x && lefthand.x < filebrowser.x + filebrowser.cWidth && lefthand.y > filebrowser.y && lefthand.y < filebrowser.y + filebrowser.cHeight) {
filebrowser.panStart(ID);
}
((UIKTree)fileTree).panStart(ID);
break;
case 1:
loddisplay.panStart(ID);
break;
}
}
lefthandDown = -1;
} else {
lefthandDown = -1;
if (dragZoom) {
loddisplay.zoomEnd(ID);
}
dragZoom = false;
dragstart = null;
leftReady = true;
if (!(CoM.z - lefthand.z > DEPTH_LOWER && CoM.z - righthand.z > DEPTH_LOWER)) {
zoomReady = true;
}
}
// right hand has been held down
if (!dragZoom && righthandDown != -1 && System.currentTimeMillis() - righthandDown >= 2000 && System.currentTimeMillis() - righthandDown < 2500 && ready) {
mousePressed = retVal = pressed = true;
mouseButton = LEFT;
mouseX = round(cursorPos.x);
mouseY = round(cursorPos.y);
mouseId = ID;
mousePressed();
ready = false;
leftReady = true;
}
// left hand has been held at an angle
if (lefthandDown != -1 && System.currentTimeMillis() - lefthandDown >= 2500 && System.currentTimeMillis() - lefthandDown < 3000 && leftReady) {
mousePressed = retVal = pressed = true;
mouseButton = RIGHT;
mouseX = round(cursorPos.x);
mouseY = round(cursorPos.y);
mouseId = ID;
mousePressed();
leftReady = false;
ready = true;
}
// start timer if hand distance changes less than 5.0 pixels
if (dragZoom && phandDiff != -1.0 && CoM.z - lefthand.z > DEPTH_LOWER && abs(phandDiff - handDiff) < 10.0) {
if (sameDistTime == -1) {
sameDistTime = System.currentTimeMillis();
}
} else {
sameDistTime = -1;
}
// both hands have been held in place, zooming stops
if (sameDistTime != -1 && System.currentTimeMillis() - sameDistTime > 2000) {
dragZoom = false;
leftReady = true;
ready = false;
sameDistTime = -1;
zoomReady = false;
if (tabs.currentpage == 1) {
((LODDisplay)tabs.get(1).get(0)).oldzoomFactor = ((LODDisplay)tabs.get(1).get(0)).zoomFactor;
}
loddisplay.zoomEnd(ID);
}
stroke(0x00);
strokeWeight(1);
ellipseMode(CENTER);
if (dragstart != null) {
pushStyle();
noStroke();
fill(0x00);
float hyp = dist(lefthand.x, lefthand.y, dragstart.x, dragstart.y);
float angle = acos((lefthand.x - dragstart.x) / hyp);
if (lefthand.y - dragstart.y < 0.0) {
angle = TWO_PI - angle;
}
angle += HALF_PI;
while (angle > TWO_PI) {
angle = TWO_PI - angle;
}
while (angle < 0.0) {
angle += TWO_PI;
}
if (!exiting && !kinect_showmenu) {
switch (tabs.currentpage) {
case 0: // file management
if (lefthand.x > filebrowser.x && lefthand.x < filebrowser.x + filebrowser.cWidth && lefthand.y > filebrowser.y && lefthand.y < filebrowser.y + filebrowser.cHeight) {
filebrowser.pan(new PVector(lefthand.x - dragstart.x, lefthand.y - dragstart.y));
((UIKTree)fileTree).pan(new PVector(lefthand.x - dragstart.x, 0));
} else {
((UIKTree)fileTree).pan(new PVector(lefthand.x - dragstart.x, lefthand.y - dragstart.y));
}
break;
case 1:
loddisplay.pan(new PVector(1.5 * (lefthand.x - dragstart.x), lefthand.y - dragstart.y));
break;
}
}
dragstart = lefthand;
if (hyp < 10.0) {
ellipse(lefthand.x, lefthand.y, 25, 25);
} else {
beginShape();
for (PVector _p : addAngleBatch(arrow, angle)) {
vertex(lefthand.x + _p.x, lefthand.y + _p.y);
}
endShape(CLOSE);
popStyle();
}
}
if (lefthandDown != -1 && System.currentTimeMillis() - lefthandDown > 1000 && System.currentTimeMillis() - lefthandDown < 3000) {
float radius = 50.0;
if (righthandDown != -1 && System.currentTimeMillis() - righthandDown > 1000 && System.currentTimeMillis() - righthandDown < 2500) {
radius = 70.0;
}
if (System.currentTimeMillis() - lefthandDown > 2500) {
fill(0x00, 0xFF, 0x00, map(System.currentTimeMillis() - lefthandDown, 2500, 3000, 0x00, 0xFF));
} else {
fill(0x00, 0xFF, 0x00);
}
arc(cursorPos.x, cursorPos.y, radius, radius, TWO_PI - map(System.currentTimeMillis() - lefthandDown, 1000, 2500, 0.0, TWO_PI), TWO_PI);
}
if (righthandDown != -1 && System.currentTimeMillis() - righthandDown > 1000 && System.currentTimeMillis() - righthandDown < 2500) {
if (System.currentTimeMillis() - righthandDown > 2000) {
fill(0x00, 0x00, 0xFF, map(System.currentTimeMillis() - righthandDown, 2000, 2500, 0x00, 0xFF));
} else {
fill(0x00, 0x00, 0xFF);
}
arc(cursorPos.x, cursorPos.y, 50.0, 50.0, 0.0, map(System.currentTimeMillis() - righthandDown, 1000, 2000, 0.0, TWO_PI));
}
if (!dragZoom) {
fill(0xFF, 0x00, 0x00, (newCoM.z - righthand.z > DEPTH_LOWER) ? 0xFF : 0x7F);
ellipse(cursorPos.x, cursorPos.y, 30.0, 30.0);
} else {
if (sameDistTime != -1) {
stroke(0x00, map(System.currentTimeMillis() - sameDistTime, 0, 2000, 0xFF, 0x00));
} else {
stroke(0x00);
}
strokeWeight(3);
line(lefthand.x, (drawHeight / 2.0) - 32, lefthand.x, (drawHeight / 2.0) + 32);
line(righthand.x, (drawHeight / 2.0) - 32, righthand.x, (drawHeight / 2.0) + 32);
line(lefthand.x, drawHeight / 2.0, righthand.x, drawHeight / 2.0);
strokeWeight(1);
}
return retVal;
}
float getAngle(PVector center, PVector pt) {
if (pt.y < center.y) {
return 180 - ((180.0 / PI) * atan(abs(center.x - pt.x) / abs(center.y - pt.y)));
} else if (pt.y > center.y) {
return (180.0 / PI) * atan(abs(center.x - pt.x) / abs(center.y - pt.y));
} else {
return 90.0;
}
}
}