-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGestures_Control.pde
1308 lines (1144 loc) · 35.9 KB
/
Gestures_Control.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Gestures_Control.pde;
*
* Copyright (c) 2011 Jeremy Archer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import SimpleOpenNI.SimpleOpenNI;
int screenWidth = 1280, screenHeight = 800;
SimpleOpenNI openni;
ArrayList<Person> people;
ArrayList<Target> targets, targetsToAddNextCycle;
PFont monaco, helvetica;
Target fullScreenTarget;
/*
* Represents a single cursor and hand belonging to a
* figure on the screen.
*/
class Hand {
int x, y, z, deltaX, deltaY, deltaZ;
int grabX, grabY;
boolean grabbing, visible, isLeftHand;
Person person;
Target grabTarget;
PVector start, direction;
/*
* Initialize this hand given its sidedness and
* parent.
*/
Hand(Person p, boolean isLeft) {
person = p;
grabbing = visible = false;
isLeftHand = isLeft;
x = y = z = grabX = grabY = -1;
deltaX = deltaY = deltaZ = 0;
start = direction = null;
}
/*
* Moves this hand to a new position.
*
* Internally, this method handles smoothing,
* meaning that
*/
void moveHand(int newX, int newY, int newZ) {
x += deltaX = (newX - x + 1) / 4;
y += deltaY = (newY - y + 1) / 4;
z += deltaZ = (newZ - z + 1);
/*
* Check for NaN: for complicated reasons,
* (1/0) != (1/0).
*/
if (x != x) x = 0;
if (y != y) y = 0;
if (z != z) z = 0;
/*
* Hide the hand if it is off-screen.
*/
visible = (x >= 0 && y >= 0 && x < width && y < height);
}
/*
* Renders this hand on the current Processing canvas.
*
* It will look like a circle with a letter (L or R)
* attached.
*/
void draw() {
if (!visible) return;
noStroke();
if (grabTarget != null)
fill(200, 0, 0);
else
fill(200, 200, 0);
ellipse(x - 10, y - 10, 20, 20);
fill(255);
textFont(helvetica, 60f);
text(isLeftHand ? "L" : "R", x - 10, y - 10);
}
/*
* Pulls the latest data from the SimpleOpenNI instance.
*/
void updateFromNI() {
PVector projYourHand = new PVector(), yourHand = new PVector();
PVector projYourTorso = new PVector(), yourTorso = new PVector();
/*
* Get the position of the hand, as a PVector.
*
* (c1 represents the certainty of that position.)
*/
float c1 = openni.getJointPositionSkeleton(
/* user ID */ person.getID(),
/* joint */ isLeftHand ? SimpleOpenNI.SKEL_LEFT_HAND : SimpleOpenNI.SKEL_RIGHT_HAND,
/* destination */ yourHand
);
/*
* Get the position of the torso, as a PVector.
*
* (c2, as above, represents the certainty.)
*/
float c2 = openni.getJointPositionSkeleton(
/* user ID */ person.getID(),
/* joint */ SimpleOpenNI.SKEL_TORSO,
/* destination */ yourTorso
);
/*
* KLUGE: If we're fairly uncertain about either vector,
* hide this hand.
*
* Since the NI documentation is fairly sparse in this
* manner, this factor is pretty arbitrary.
*/
if (c1 <= 0.001f || c2 <= 0.001f) {
visible = false;
return;
}
/*
* Convert the perspective vectors (angle and distance) to
* real-world (x, y, z) vectors.
*/
// openni.convertProjectiveToRealWorld(projYourTorso, yourTorso);
// openni.convertProjectiveToRealWorld(projYourHand, yourHand);
/*
* Scale the vectors arbitrarily to the screen size.
* They are currently in milimeters, so we need to
* convert them to screen (pixel) co-ordinates.
*
* Here, we're using a grab box of two meters wide,
* one meter tall, and about half a meter deep.
*/
int newX = (int)(((yourHand.x - yourTorso.x) / 1500.0 + 0.5f) * width);
int newY = (int)(((yourTorso.y - yourHand.y) / 800.0 + 0.5f) * height);
int newZ = (int)(((yourTorso.z - yourHand.z) / 450.0) * 100.0);
/*
* Move the hand to this new position.
*/
moveHand(newX, newY, newZ);
}
/*
* Detect if this hand is intersecting something on the hit image
* in the blue-channel.
*
* The "hit image," in this case, is an image with all the objects
* rendered in a flat, per-object color, so that we can simply see
* the color at a certain (x, y)-location to see what object lies
* there.
*/
public int intersect(PImage hitTestImage) {
/*
* Disable if we're invisible; i.e. off-screen.
*
* That's why we don't need a conditional here.
*/
if (!visible) return -1;
/*
* A few things are going on here:
*
* First, the image is scaled down to 10%, meaning that we need
* to scale both x and y by 10. We also need to linearize them,
* (in row-major order). Then the result, which is a color, may
* have some alpha components, so we get rid of them and extract
* the blue component.
*/
int value = hitTestImage.pixels[(x / 10) + (y / 10) * (width / 10)] & 0xff;
/*
* The blue-component value of zero is black (#000), so we need
* to shift this guy down by one so that #000001 maps to 0 and
* #000000 maps to -1 (no intersection).
*/
return value - 1;
}
/*
* Detect if this hand is intersecting something on the hit image
* in the green-channel.
*
* The "hit image," in this case, is an image with all the objects
* rendered in a flat, per-object color, so that we can simply see
* the color at a certain (x, y)-location to see what object lies
* there.
*/
public int intersectGreen(PImage hitTestImage) {
/*
* Disable if we're invisible; i.e. off-screen.
*
* That's why we don't need a conditional here.
*/
if (!visible) return -1;
/*
* A few things are going on here:
*
* First, the image is scaled down to 10%, meaning that we need
* to scale both x and y by 10. We also need to linearize them,
* (in row-major order). Then the result, which is a color, may
* have some alpha components, so we get rid of them and extract
* the green component.
*/
int value = (hitTestImage.pixels[(x / 10) + (y / 10) * (width / 10)] >> 8) & 0xff;
/*
* The blue-component value of zero is black (#000), so we need
* to shift this guy down by one so that #000001 maps to 0 and
* #000000 maps to -1 (no intersection).
*/
return value - 1;
}
/*
* Mark this hand as having hovered over another object, given
* the target's ID and secondary (green-component) color index.
*
* If t is null, mark this object as not grabbing anything.
*/
public void grab(Target t, int secondaryIndex) {
/*
* Record this object
*/
grabTarget = t;
/*
* If given a new target, tell that object to begin
* showing hover feedback.
*/
if (t != null)
t.takeHoverDataFrom(this, secondaryIndex);
}
/*
* Sets the target given the rendered hit image and numbered targets.
*
* The index of each object will be encoded in the blue-channel of the
* PImage, so that, for example: object 0 (the first object in the list),
* will be encoded as #000001; object 1 as #000002; et. al.
*/
public void setTargetFrom(PImage hitTestImage, ArrayList<Target> targets) {
/*
* If we're already hovering over something, unregister us from the
* object. If it turns out that we are still hovering over it with
* both hands, the Person class will register us as having grabbed
* it.
*/
if (grabTarget != null)
grabTarget.setGrabbedBy(null);
/*
* Extract the blue and green indices from the hit image.
*/
int index = intersect(hitTestImage);
int secondaryIndex = intersectGreen(hitTestImage);
/*
* If we're hovering over something, mark it as having been hovered
* over.
*/
if (index == -1) grab(null, 0);
else grab(targets.get(index), secondaryIndex);
}
/*
* Gets the target we're currently hovering over.
*/
public Target getGrabTarget() {
return grabTarget;
}
/*
* Returns whether this hand is on-screen.
*/
public boolean getIsVisible() {
return visible;
}
/*
* Returns the X-coordinate (in screen pixels) of this hand.
*/
public int getX() {
return x;
}
/*
* Returns the Y-coordinate (in screen pixels) of this hand.
*/
public int getY() {
return y;
}
/*
* Returns the Z-coordinate of this hand.
*
* In practice, this will usually be in the range [-150.0,
* +150.0], but there isn't a defined rectangle like for x
* and y.
*/
public int getZ() {
return z;
}
}
/*
* Controlling class for a person with two hands.
*/
class Person {
Hand left, right;
int userID;
Target grabTarget;
/*
* Initialize this person given their ID in OpenNI.
*/
public Person(int id) {
left = new Hand(this, true);
right = new Hand(this, false);
userID = id;
}
/*
* Return the internal OpenNI identifier.
*/
public int getID() {
return userID;
}
/*
* Returns the left hand.
*/
public Hand getLeftHand() { return left; }
/*
* Returns the left hand.
*/
public Hand getRightHand() { return right; }
/*
* Update the current grabbing / hovering status, given the hit image
* and list of targets.
*
* The hit image is a rendered image with the various targets rendered
* as a silhouette in a flat color. The first object in the array is
* in color #000001, the second in #000002, and so on.
*/
public void updateGrabbing(PImage hitTestImage, ArrayList<Target> targets) {
/*
* Update the hovering status for both hands.
*/
left.setTargetFrom(hitTestImage, targets);
right.setTargetFrom(hitTestImage, targets);
/*
* If we're hovering over the same object with both hands,
* then mark the target as having been grabbed by us.
*/
if (left.getGrabTarget() == right.getGrabTarget() && (left.getGrabTarget() != null))
left.getGrabTarget().setGrabbedBy(this);
}
/*
* Gets the average X position of the two hands.
*/
public int getAverageX() {
return (getLeftHand().getX() + getRightHand().getX()) / 2;
}
/*
* Gets the average Y position of the two hands.
*/
public int getAverageY() {
return (getLeftHand().getY() + getRightHand().getY()) / 2;
}
/*
* Gets the angle [-pi, +pi] between the two hands.
*/
public float getHandAngle() {
return atan2(getRightHand().getY() - getLeftHand().getY(),
getRightHand().getX() - getLeftHand().getX());
}
/*
* Gets the distance between both hands.
*/
public float getGrabRadius() {
float dx = getRightHand().getY() - getLeftHand().getY(),
dy = getRightHand().getX() - getLeftHand().getX();
return (float)Math.sqrt(dx*dx + dy*dy);
}
}
/*
* Represents a grabbable object on the screen.
*/
interface Target {
/*
* Renders this object as a silhouette onto the PGraphics object
* given the value the blue component should have.
*/
void drawHitRegion(PGraphics g, int blueComponent);
/*
* Renders this object in full color.
*/
void draw();
/*
* Sets this object as having been grabbed by the specified
* person. This method will be called a lot (every frame)
* so it should do little more than set a variable.
*/
void setGrabbedBy(Person p);
/*
* Moves this object to the new location.
*/
void moveTo(int x, int y);
/*
* Rotates this object to the specified angle.
*/
void rotateTo(float angleInRadians);
/*
* Updates the position and rotation of the object given
* the person grabbing it.
*/
void updateGrabbing();
/*
* Update the UI to show feedback to being hovered over by
* the specified hand. This method is also passed the
* green component of the color the hand is hovering over.
*/
void takeHoverDataFrom(Hand h, int secondaryIndex);
}
/*
* Represents a target that is nested inside another.
*/
interface SubTarget {
/*
* Renders this object as a silhouette. The PGraphics will
* already be initialized with the proper color.
*/
void drawHitRegion(PGraphics g);
/*
* Renders this object in full color.
*/
void draw();
/*
* Marks this object as currently being hovered over.
*/
void hoverOver();
/*
* Updates this object given the current visibility of this
* SubTarget.
*/
void updateHovering(boolean enabled);
}
/*
* Represents an abstract rectangular target.
*
* By default, this class only draws a grey rectangle on the
* screen, but supports SubTargets, rotation and movement.
*
* Most other Targets are subclasses of this one.
*/
class Rectangle implements Target {
int x, y, w, h;
float rotationAngle;
Person grabbedBy;
ArrayList<SubTarget> subTargets;
/*
* Initializes this rectangle with the default size, 500x300,
* and places it in the center of the screen.
*/
public Rectangle() {
w = 500;
h = 300;
x = (width - w) / 2;
y = (height - h) / 2;
rotationAngle = 0.0f;
grabbedBy = null;
subTargets = new ArrayList<SubTarget>();
}
/*
* Returns true if this object can be bumped into full-screen
*/
boolean canBeMadeFullScreen() { // meant to be overriden.
return false;
}
/*
* Renders this object in color. The coordinate system will
* already have been shifted and rotated so that the object
* should be placed to the right and below (0, 0).
*
* Subclasses note: this method renders SubTargets. If you
* intend to replace or override this method, you must manually
* render all SubTarget instances in this class, like so:
*
* ...
*
* void drawTarget() {
*
* ...
*
* for (SubTarget target : subTargets) {
* target.draw();
* }
* }
*
* ...
*/
void drawTarget() { // meant to be overriden.
if (grabbedBy != null)
stroke(200, 0, 0);
else
stroke(200, 200, 0);
strokeWeight(5);
fill(50);
rect(0, 0, w, h);
for (SubTarget target : subTargets) {
target.draw();
}
}
/*
* Renders this target as a silhouette onto the image buffer
* instance. The coordinate system will already have been
* shifted and rotated so that the object should be placed
* to the right and below (0, 0).
*
* Subclasses note: this method renders SubTargets. If you
* intend to replace or override this method, you must manually
* render all SubTarget instances in this class, like the below.
*
* Likewise, before rendering, you must ensure that all objects
* are drawn in the proper color:
*
* ...
* void drawTargetHitRegions(PGraphics g, int blueComponent) {
* g.fill(0, 0, blueComponent);
*
* ...
*
* for (int i = 0; i < subTargets.size(); i++) {
* g.fill(0, (i + 1), blueComponent);
* subTargets.get(i).drawHitRegion(g);
* }
* }
* ...
*/
void drawTargetHitRegions(PGraphics g, int blueComponent) { // meant to be overriden.
g.fill(0, 0, blueComponent);
g.rect(0, 0, w, h);
for (int i = 0; i < subTargets.size(); i++) {
g.fill(0, (i + 1), blueComponent);
subTargets.get(i).drawHitRegion(g);
}
}
/*
* Adds a SubTarget to this list.
*/
void addSubTarget(SubTarget tar) {
subTargets.add(tar);
}
/*
* Draws this object in silhouette form in a color with the
* specified blue component, as specified in the interface.
*/
void drawHitRegion(PGraphics g, int blueComponent) {
/*
* Determine if the entire screen has a single component.
*/
if (fullScreenTarget != null) {
/*
* If we're full-screen, render the entire screen as
* the same color. It's wasteful, but cycles are
* plentiful nowadays.
*/
if (fullScreenTarget == this) {
/*
* Transform our co-ordiante system and render.
*/
g.pushMatrix();
g.fill(0, 0, blueComponent);
g.rect(0, 0, width, height);
g.translate(width / 2, height / 2);
g.scale(Math.min(((float)width) / w, ((float)height) / h));
g.translate(-w / 2, -h / 2);
drawTargetHitRegions(g, blueComponent);
g.popMatrix();
}
} else {
/*
* Transform our co-ordinate system and render.
*/
g.pushMatrix();
g.translate(x, y);
g.translate(w / 2, h / 2);
g.rotate(rotationAngle);
/*
* If we're grabbing, increase the size of the object
* to make it easier to grip.
*/
if (grabbedBy != null) g.scale(1.5);
g.translate(-w / 2, -h / 2);
drawTargetHitRegions(g, blueComponent);
g.popMatrix();
}
}
/*
* Renders this object in full-color.
*/
void draw() {
/*
* Determine whether one component is blocking the
* entire screen.
*/
if (fullScreenTarget == this) {
/*
* If we're in full-screen, simply transform
* and render.
*/
pushMatrix();
translate(width / 2, height / 2);
scale(Math.min(((float)width) / w, ((float)height) / h));
translate(-w / 2, -h / 2);
drawTarget();
popMatrix();
/*
* Retransform and put an axis through both hands to indicate
* that distance is how a full-screen view is maintained.
*/
pushMatrix();
strokeWeight(1);
stroke(255, 255, 255, 50);
noFill();
translate(grabbedBy.getAverageX(), grabbedBy.getAverageY());
rotate(grabbedBy.getHandAngle());
ellipse(0, 0, 0.6 * width, 0.6 * width);
line(-width * 10, 0, width * 10, 0);
line(0, -height * 10, 0, height * 10);
popMatrix();
} else if (fullScreenTarget == null) {
/*
* If no target is in full-screen, then transform and render
* in the normal place.
*/
pushMatrix();
translate(x + w / 2, y + h / 2);
rotate(rotationAngle);
translate(-w / 2, -h / 2);
drawTarget();
/*
* If we've been grabbed, render an overlay of crosshairs and
* an outer radius.
*/
if (grabbedBy != null) {
translate(w / 2, h / 2);
strokeWeight(1);
stroke(255, 255, 255, 50);
noFill();
line(-width * 10, 0, width * 20, 0);
line(0, -height * 10, 0, height * 20);
ellipse(0, 0, 100, 100);
ellipse(0, 0, 40, 40);
}
popMatrix();
}
/*
* Update the status of all subtargets.
*
* Pass true if this subtarget is visible.
*/
for (SubTarget tar : subTargets) {
tar.updateHovering((grabbedBy == null && fullScreenTarget == null) ||
fullScreenTarget == this);
}
}
/*
* Mark this target as having been grabbed.
*/
void setGrabbedBy(Person p) {
grabbedBy = p;
}
/*
* Update hovering data from the given hand and highlight
* the the target.
*/
void takeHoverDataFrom(Hand p, int greenComponent) {
if (greenComponent != -1) {
SubTarget tar = subTargets.get(greenComponent);
tar.hoverOver();
}
}
/*
* Moves the rectangle to the specified x and y.
*/
void moveTo(int newX, int newY) {
x = newX - w / 2;
y = newY - h / 2;
}
/*
* Rotates this rectangle to the specified angle from
* the horizontal.
*/
void rotateTo(float angleInRadians) {
rotationAngle = angleInRadians;
}
/*
* Updates the position and rotation of this object given
* the position of the grabbing hand.
*/
void updateGrabbing() {
if (grabbedBy != null && fullScreenTarget == null) {
/*
* If no one is full screen, then move and rotate
* this block.
*/
moveTo(grabbedBy.getAverageX(), grabbedBy.getAverageY());
rotateTo(grabbedBy.getHandAngle());
/*
* If the hands are close enough together, pop into
* full screen mode.
*/
if (grabbedBy.getGrabRadius() < 100) {
fullScreenTarget = this;
}
} else if (fullScreenTarget == this) {
/*
* If we're out of full screen and the hands are too
* far apart, pop out of full screen.
*/
if (grabbedBy == null || grabbedBy.getGrabRadius() > 0.6 * width) {
fullScreenTarget = null;
}
}
}
}
/*
* A movable view of NI's depth image, colored for calibration.
*/
class SceneViewTarget extends Rectangle {
/*
* By default, this view is half the pixel width of NI's
* scene image.
*/
SceneViewTarget() {
w = openni.sceneWidth() / 2;
h = openni.sceneHeight() / 2;
x = width - w;
y = height - h;
}
/*
* This can be made full-screen. When in full-screen
* mode, it displays a four-up view of IR, the scene,
* depth and RGB information.
*/
boolean canBeMadeFullScreen() {
return true;
}
/*
* Renders the NI scene image, with overlays for the
* arms and torso.
*/
void drawSceneView() {
rect(0, 0, w, h);
image(openni.sceneImage(), 0, 0, w, h);
}
/*
* Renders this control.
*/
void drawTarget() {
/*
* Draw the scene view.
*/
if (fullScreenTarget == this) {
/*
* Draw the scene view in the top left.
*/
pushMatrix();
scale(0.5, 0.5);
drawSceneView();
popMatrix();
/*
* Render the IR, depth, and RGB images. I don't
* believe that IR and RGB can be seen at the same
* time for technical reasons.
*/
if (openni.irImage() != null)
image(openni.irImage(), w / 2, 0, w / 2, h / 2);
image(openni.depthImage(), 0, h / 2, w / w, h / 2);
if (openni.rgbImage() != null)
image(openni.rgbImage(), w / 2, h / 2, w / 2, h / 2);
} else {
/*
* In regular mode, just render the scene.
*/
pushMatrix();
drawSceneView();
popMatrix();
}
}
}
/*
* Represents a textual label that can be placed
* and moved.
*/
class RefrigeratorMagnet extends Rectangle {
String label;
/*
* Initialize this widget given the string
* to display. It will be placed randomly on
* the screen (with caveats -- can you see
* why?) and with enough width to display
* the message.
*/
RefrigeratorMagnet(String s) {
textFont(helvetica,90);
w = (int)textWidth(s) + 20;
h = 100;
x = (int)random(width - w);
y = (int)random(height - h);
label = s;
}
/*
* Renders the magnet. It will be drawn in
* Helvetica with a colored border, depending
* on its status.
*/
void drawTarget() {
if (grabbedBy != null)
stroke(200, 0, 0);
else
stroke(200, 200, 0);
strokeWeight(5);
fill(10);
rect(0, 0, w, h);
fill(255);
textFont(helvetica, 90);
text(label, 10, 80);
for (SubTarget target : subTargets) {
target.draw();
}
}
}
/*
* A collection of key widgets that allow the
* user to type letters and make textual
* "refrigerator magnets," as above.
*/
class KeyboardTarget extends Rectangle {
/*
* The keys were chosen this way based on
* relative word frequency in English: the
* most common words are in the center, which
* (theoretically) makes it more accessible.
*
* Some special characters: the at-sign ("@"),
* represents "enter," or "newline;" the back-
* chevron ("<") represents "backspace" or
* delete, and the space represents "no
* button."
*/
String topRow = "JFDWK@",
secondRow = "GNIRM<",
thirdRow = "LTEOU ",
forthRow = "YSAHV ",
fifthRow = "XPCBQZ";
String letters;
/*
* Initialize this keyboard at a size of 600x600,
* so that each key gets 100x100.
*/
KeyboardTarget() {
x = y = 0;
w = width / 3;
h = width / 3;
letters = "";
String[] rows = { topRow, secondRow, thirdRow, forthRow, fifthRow };
int sz = w / 6;
/*
* Loop through and create buttons for each key.
*/
for (int i = 0; i < 6; i += 1) {
for (int j = 0; j < 5; j += 1) {
if (rows[j].charAt(i) != ' ') {
addSubTarget(new ExplodingButton (
/* location and size */ i * sz, j * sz + sz, sz * 9 / 10, sz * 9 / 10,
/* label */ rows[j].substring(i, i + 1),
/* parent */ this
));
}
}
}
}
/*
* Draws the containing rectangle and renders
* the keys for this keyboard.
*/
void drawTarget() {
/*
* Render a border around the keys that is colored
* based on text-entry status.
*/