forked from y0ast/KinectGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zig.js
1840 lines (1606 loc) · 62.1 KB
/
zig.js
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
// script: zig.js
// ZDK for Javascript
if ('undefined' == typeof zig) {
// Minified version of sylvester, used internally
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('9 17={3i:\'0.1.3\',16:1e-6};l v(){}v.23={e:l(i){8(i<1||i>7.4.q)?w:7.4[i-1]},2R:l(){8 7.4.q},1u:l(){8 F.1x(7.2u(7))},24:l(a){9 n=7.4.q;9 V=a.4||a;o(n!=V.q){8 1L}J{o(F.13(7.4[n-1]-V[n-1])>17.16){8 1L}}H(--n);8 2x},1q:l(){8 v.u(7.4)},1b:l(a){9 b=[];7.28(l(x,i){b.19(a(x,i))});8 v.u(b)},28:l(a){9 n=7.4.q,k=n,i;J{i=k-n;a(7.4[i],i+1)}H(--n)},2q:l(){9 r=7.1u();o(r===0){8 7.1q()}8 7.1b(l(x){8 x/r})},1C:l(a){9 V=a.4||a;9 n=7.4.q,k=n,i;o(n!=V.q){8 w}9 b=0,1D=0,1F=0;7.28(l(x,i){b+=x*V[i-1];1D+=x*x;1F+=V[i-1]*V[i-1]});1D=F.1x(1D);1F=F.1x(1F);o(1D*1F===0){8 w}9 c=b/(1D*1F);o(c<-1){c=-1}o(c>1){c=1}8 F.37(c)},1m:l(a){9 b=7.1C(a);8(b===w)?w:(b<=17.16)},34:l(a){9 b=7.1C(a);8(b===w)?w:(F.13(b-F.1A)<=17.16)},2k:l(a){9 b=7.2u(a);8(b===w)?w:(F.13(b)<=17.16)},2j:l(a){9 V=a.4||a;o(7.4.q!=V.q){8 w}8 7.1b(l(x,i){8 x+V[i-1]})},2C:l(a){9 V=a.4||a;o(7.4.q!=V.q){8 w}8 7.1b(l(x,i){8 x-V[i-1]})},22:l(k){8 7.1b(l(x){8 x*k})},x:l(k){8 7.22(k)},2u:l(a){9 V=a.4||a;9 i,2g=0,n=7.4.q;o(n!=V.q){8 w}J{2g+=7.4[n-1]*V[n-1]}H(--n);8 2g},2f:l(a){9 B=a.4||a;o(7.4.q!=3||B.q!=3){8 w}9 A=7.4;8 v.u([(A[1]*B[2])-(A[2]*B[1]),(A[2]*B[0])-(A[0]*B[2]),(A[0]*B[1])-(A[1]*B[0])])},2A:l(){9 m=0,n=7.4.q,k=n,i;J{i=k-n;o(F.13(7.4[i])>F.13(m)){m=7.4[i]}}H(--n);8 m},2Z:l(x){9 a=w,n=7.4.q,k=n,i;J{i=k-n;o(a===w&&7.4[i]==x){a=i+1}}H(--n);8 a},3g:l(){8 S.2X(7.4)},2d:l(){8 7.1b(l(x){8 F.2d(x)})},2V:l(x){8 7.1b(l(y){8(F.13(y-x)<=17.16)?x:y})},1o:l(a){o(a.K){8 a.1o(7)}9 V=a.4||a;o(V.q!=7.4.q){8 w}9 b=0,2b;7.28(l(x,i){2b=x-V[i-1];b+=2b*2b});8 F.1x(b)},3a:l(a){8 a.1h(7)},2T:l(a){8 a.1h(7)},1V:l(t,a){9 V,R,x,y,z;2S(7.4.q){27 2:V=a.4||a;o(V.q!=2){8 w}R=S.1R(t).4;x=7.4[0]-V[0];y=7.4[1]-V[1];8 v.u([V[0]+R[0][0]*x+R[0][1]*y,V[1]+R[1][0]*x+R[1][1]*y]);1I;27 3:o(!a.U){8 w}9 C=a.1r(7).4;R=S.1R(t,a.U).4;x=7.4[0]-C[0];y=7.4[1]-C[1];z=7.4[2]-C[2];8 v.u([C[0]+R[0][0]*x+R[0][1]*y+R[0][2]*z,C[1]+R[1][0]*x+R[1][1]*y+R[1][2]*z,C[2]+R[2][0]*x+R[2][1]*y+R[2][2]*z]);1I;2P:8 w}},1t:l(a){o(a.K){9 P=7.4.2O();9 C=a.1r(P).4;8 v.u([C[0]+(C[0]-P[0]),C[1]+(C[1]-P[1]),C[2]+(C[2]-(P[2]||0))])}1d{9 Q=a.4||a;o(7.4.q!=Q.q){8 w}8 7.1b(l(x,i){8 Q[i-1]+(Q[i-1]-x)})}},1N:l(){9 V=7.1q();2S(V.4.q){27 3:1I;27 2:V.4.19(0);1I;2P:8 w}8 V},2n:l(){8\'[\'+7.4.2K(\', \')+\']\'},26:l(a){7.4=(a.4||a).2O();8 7}};v.u=l(a){9 V=25 v();8 V.26(a)};v.i=v.u([1,0,0]);v.j=v.u([0,1,0]);v.k=v.u([0,0,1]);v.2J=l(n){9 a=[];J{a.19(F.2F())}H(--n);8 v.u(a)};v.1j=l(n){9 a=[];J{a.19(0)}H(--n);8 v.u(a)};l S(){}S.23={e:l(i,j){o(i<1||i>7.4.q||j<1||j>7.4[0].q){8 w}8 7.4[i-1][j-1]},33:l(i){o(i>7.4.q){8 w}8 v.u(7.4[i-1])},2E:l(j){o(j>7.4[0].q){8 w}9 a=[],n=7.4.q,k=n,i;J{i=k-n;a.19(7.4[i][j-1])}H(--n);8 v.u(a)},2R:l(){8{2D:7.4.q,1p:7.4[0].q}},2D:l(){8 7.4.q},1p:l(){8 7.4[0].q},24:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(7.4.q!=M.q||7.4[0].q!=M[0].q){8 1L}9 b=7.4.q,15=b,i,G,10=7.4[0].q,j;J{i=15-b;G=10;J{j=10-G;o(F.13(7.4[i][j]-M[i][j])>17.16){8 1L}}H(--G)}H(--b);8 2x},1q:l(){8 S.u(7.4)},1b:l(a){9 b=[],12=7.4.q,15=12,i,G,10=7.4[0].q,j;J{i=15-12;G=10;b[i]=[];J{j=10-G;b[i][j]=a(7.4[i][j],i+1,j+1)}H(--G)}H(--12);8 S.u(b)},2i:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}8(7.4.q==M.q&&7.4[0].q==M[0].q)},2j:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(!7.2i(M)){8 w}8 7.1b(l(x,i,j){8 x+M[i-1][j-1]})},2C:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(!7.2i(M)){8 w}8 7.1b(l(x,i,j){8 x-M[i-1][j-1]})},2B:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}8(7.4[0].q==M.q)},22:l(a){o(!a.4){8 7.1b(l(x){8 x*a})}9 b=a.1u?2x:1L;9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}o(!7.2B(M)){8 w}9 d=7.4.q,15=d,i,G,10=M[0].q,j;9 e=7.4[0].q,4=[],21,20,c;J{i=15-d;4[i]=[];G=10;J{j=10-G;21=0;20=e;J{c=e-20;21+=7.4[i][c]*M[c][j]}H(--20);4[i][j]=21}H(--G)}H(--d);9 M=S.u(4);8 b?M.2E(1):M},x:l(a){8 7.22(a)},32:l(a,b,c,d){9 e=[],12=c,i,G,j;9 f=7.4.q,1p=7.4[0].q;J{i=c-12;e[i]=[];G=d;J{j=d-G;e[i][j]=7.4[(a+i-1)%f][(b+j-1)%1p]}H(--G)}H(--12);8 S.u(e)},31:l(){9 a=7.4.q,1p=7.4[0].q;9 b=[],12=1p,i,G,j;J{i=1p-12;b[i]=[];G=a;J{j=a-G;b[i][j]=7.4[j][i]}H(--G)}H(--12);8 S.u(b)},1y:l(){8(7.4.q==7.4[0].q)},2A:l(){9 m=0,12=7.4.q,15=12,i,G,10=7.4[0].q,j;J{i=15-12;G=10;J{j=10-G;o(F.13(7.4[i][j])>F.13(m)){m=7.4[i][j]}}H(--G)}H(--12);8 m},2Z:l(x){9 a=w,12=7.4.q,15=12,i,G,10=7.4[0].q,j;J{i=15-12;G=10;J{j=10-G;o(7.4[i][j]==x){8{i:i+1,j:j+1}}}H(--G)}H(--12);8 w},30:l(){o(!7.1y){8 w}9 a=[],n=7.4.q,k=n,i;J{i=k-n;a.19(7.4[i][i])}H(--n);8 v.u(a)},1K:l(){9 M=7.1q(),1c;9 n=7.4.q,k=n,i,1s,1n=7.4[0].q,p;J{i=k-n;o(M.4[i][i]==0){2e(j=i+1;j<k;j++){o(M.4[j][i]!=0){1c=[];1s=1n;J{p=1n-1s;1c.19(M.4[i][p]+M.4[j][p])}H(--1s);M.4[i]=1c;1I}}}o(M.4[i][i]!=0){2e(j=i+1;j<k;j++){9 a=M.4[j][i]/M.4[i][i];1c=[];1s=1n;J{p=1n-1s;1c.19(p<=i?0:M.4[j][p]-M.4[i][p]*a)}H(--1s);M.4[j]=1c}}}H(--n);8 M},3h:l(){8 7.1K()},2z:l(){o(!7.1y()){8 w}9 M=7.1K();9 a=M.4[0][0],n=M.4.q-1,k=n,i;J{i=k-n+1;a=a*M.4[i][i]}H(--n);8 a},3f:l(){8 7.2z()},2y:l(){8(7.1y()&&7.2z()===0)},2Y:l(){o(!7.1y()){8 w}9 a=7.4[0][0],n=7.4.q-1,k=n,i;J{i=k-n+1;a+=7.4[i][i]}H(--n);8 a},3e:l(){8 7.2Y()},1Y:l(){9 M=7.1K(),1Y=0;9 a=7.4.q,15=a,i,G,10=7.4[0].q,j;J{i=15-a;G=10;J{j=10-G;o(F.13(M.4[i][j])>17.16){1Y++;1I}}H(--G)}H(--a);8 1Y},3d:l(){8 7.1Y()},2W:l(a){9 M=a.4||a;o(1g(M[0][0])==\'1f\'){M=S.u(M).4}9 T=7.1q(),1p=T.4[0].q;9 b=T.4.q,15=b,i,G,10=M[0].q,j;o(b!=M.q){8 w}J{i=15-b;G=10;J{j=10-G;T.4[i][1p+j]=M[i][j]}H(--G)}H(--b);8 T},2w:l(){o(!7.1y()||7.2y()){8 w}9 a=7.4.q,15=a,i,j;9 M=7.2W(S.I(a)).1K();9 b,1n=M.4[0].q,p,1c,2v;9 c=[],2c;J{i=a-1;1c=[];b=1n;c[i]=[];2v=M.4[i][i];J{p=1n-b;2c=M.4[i][p]/2v;1c.19(2c);o(p>=15){c[i].19(2c)}}H(--b);M.4[i]=1c;2e(j=0;j<i;j++){1c=[];b=1n;J{p=1n-b;1c.19(M.4[j][p]-M.4[i][p]*M.4[j][i])}H(--b);M.4[j]=1c}}H(--a);8 S.u(c)},3c:l(){8 7.2w()},2d:l(){8 7.1b(l(x){8 F.2d(x)})},2V:l(x){8 7.1b(l(p){8(F.13(p-x)<=17.16)?x:p})},2n:l(){9 a=[];9 n=7.4.q,k=n,i;J{i=k-n;a.19(v.u(7.4[i]).2n())}H(--n);8 a.2K(\'\\n\')},26:l(a){9 i,4=a.4||a;o(1g(4[0][0])!=\'1f\'){9 b=4.q,15=b,G,10,j;7.4=[];J{i=15-b;G=4[i].q;10=G;7.4[i]=[];J{j=10-G;7.4[i][j]=4[i][j]}H(--G)}H(--b);8 7}9 n=4.q,k=n;7.4=[];J{i=k-n;7.4.19([4[i]])}H(--n);8 7}};S.u=l(a){9 M=25 S();8 M.26(a)};S.I=l(n){9 a=[],k=n,i,G,j;J{i=k-n;a[i]=[];G=k;J{j=k-G;a[i][j]=(i==j)?1:0}H(--G)}H(--n);8 S.u(a)};S.2X=l(a){9 n=a.q,k=n,i;9 M=S.I(n);J{i=k-n;M.4[i][i]=a[i]}H(--n);8 M};S.1R=l(b,a){o(!a){8 S.u([[F.1H(b),-F.1G(b)],[F.1G(b),F.1H(b)]])}9 d=a.1q();o(d.4.q!=3){8 w}9 e=d.1u();9 x=d.4[0]/e,y=d.4[1]/e,z=d.4[2]/e;9 s=F.1G(b),c=F.1H(b),t=1-c;8 S.u([[t*x*x+c,t*x*y-s*z,t*x*z+s*y],[t*x*y+s*z,t*y*y+c,t*y*z-s*x],[t*x*z-s*y,t*y*z+s*x,t*z*z+c]])};S.3b=l(t){9 c=F.1H(t),s=F.1G(t);8 S.u([[1,0,0],[0,c,-s],[0,s,c]])};S.39=l(t){9 c=F.1H(t),s=F.1G(t);8 S.u([[c,0,s],[0,1,0],[-s,0,c]])};S.38=l(t){9 c=F.1H(t),s=F.1G(t);8 S.u([[c,-s,0],[s,c,0],[0,0,1]])};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,2o]).4;9 e=[Q[0]+(Q[0]-d)-c[0],Q[1]+(Q[1]-2h)-c[1],Q[2]+(Q[2]-2o)-c[2]];8 14.u(c,e)}1d o(a.U){8 7.1V(F.1A,a)}1d{9 P=a.4||a;8 14.u(7.K.1t([P[0],P[1],(P[2]||0)]),7.U)}},1Z:l(a,b){a=v.u(a);b=v.u(b);o(a.4.q==2){a.4.19(0)}o(b.4.q==2){b.4.19(0)}o(a.4.q>3||b.4.q>3){8 w}9 c=b.1u();o(c===0){8 w}7.K=a;7.U=v.u([b.4[0]/c,b.4[1]/c,b.4[2]/c]);8 7}};14.u=l(a,b){9 L=25 14();8 L.1Z(a,b)};14.X=14.u(v.1j(3),v.i);14.Y=14.u(v.1j(3),v.j);14.Z=14.u(v.1j(3),v.k);l 11(){}11.23={24:l(a){8(7.1h(a.K)&&7.1m(a))},1q:l(){8 11.u(7.K,7.W)},2U:l(a){9 V=a.4||a;8 11.u([7.K.4[0]+V[0],7.K.4[1]+V[1],7.K.4[2]+(V[2]||0)],7.W)},1m:l(a){9 b;o(a.W){b=7.W.1C(a.W);8(F.13(b)<=17.16||F.13(F.1A-b)<=17.16)}1d o(a.U){8 7.W.2k(a.U)}8 w},2k:l(a){9 b=7.W.1C(a.W);8(F.13(F.1A/2-b)<=17.16)},1o:l(a){o(7.1v(a)||7.1h(a)){8 0}o(a.K){9 A=7.K.4,B=a.K.4,N=7.W.4;8 F.13((A[0]-B[0])*N[0]+(A[1]-B[1])*N[1]+(A[2]-B[2])*N[2])}1d{9 P=a.4||a;9 A=7.K.4,N=7.W.4;8 F.13((A[0]-P[0])*N[0]+(A[1]-P[1])*N[1]+(A[2]-(P[2]||0))*N[2])}},1h:l(a){o(a.W){8 w}o(a.U){8(7.1h(a.K)&&7.1h(a.K.2j(a.U)))}1d{9 P=a.4||a;9 A=7.K.4,N=7.W.4;9 b=F.13(N[0]*(A[0]-P[0])+N[1]*(A[1]-P[1])+N[2]*(A[2]-(P[2]||0)));8(b<=17.16)}},1v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(a){o(a.W){9 A=7.K.4,N=7.W.4;9 b=A[0],18=A[1],1a=A[2],2M=N[0],2L=N[1],2Q=N[2];9 c=7.K.1t(a).4;9 d=b+2M,2p=18+2L,2m=1a+2Q;9 Q=a.1r([d,2p,2m]).4;9 e=[Q[0]+(Q[0]-d)-c[0],Q[1]+(Q[1]-2p)-c[1],Q[2]+(Q[2]-2m)-c[2]];8 11.u(c,e)}1d o(a.U){8 7.1V(F.1A,a)}1d{9 P=a.4||a;8 11.u(7.K.1t([P[0],P[1],(P[2]||0)]),7.W)}},1Z:l(a,b,c){a=v.u(a);a=a.1N();o(a===w){8 w}b=v.u(b);b=b.1N();o(b===w){8 w}o(1g(c)==\'1f\'){c=w}1d{c=v.u(c);c=c.1N();o(c===w){8 w}}9 d=a.4[0],18=a.4[1],1a=a.4[2];9 e=b.4[0],1W=b.4[1],1X=b.4[2];9 f,1i;o(c!==w){9 g=c.4[0],2l=c.4[1],2t=c.4[2];f=v.u([(1W-18)*(2t-1a)-(1X-1a)*(2l-18),(1X-1a)*(g-d)-(e-d)*(2t-1a),(e-d)*(2l-18)-(1W-18)*(g-d)]);1i=f.1u();o(1i===0){8 w}f=v.u([f.4[0]/1i,f.4[1]/1i,f.4[2]/1i])}1d{1i=F.1x(e*e+1W*1W+1X*1X);o(1i===0){8 w}f=v.u([b.4[0]/1i,b.4[1]/1i,b.4[2]/1i])}7.K=a;7.W=f;8 7}};11.u=l(a,b,c){9 P=25 11();8 P.1Z(a,b,c)};11.2I=11.u(v.1j(3),v.k);11.2H=11.u(v.1j(3),v.i);11.2G=11.u(v.1j(3),v.j);11.36=11.2I;11.35=11.2H;11.3j=11.2G;9 $V=v.u;9 $M=S.u;9 $L=14.u;9 $P=11.u;',62,206,'||||elements|||this|return|var||||||||||||function|||if||length||||create|Vector|null|||||||||Math|nj|while||do|anchor||||||||Matrix||direction||normal||||kj|Plane|ni|abs|Line|ki|precision|Sylvester|A2|push|A3|map|els|else||undefined|typeof|contains|mod|Zero|D3|D2|isParallelTo|kp|distanceFrom|cols|dup|pointClosestTo|np|reflectionIn|modulus|intersects|A1|sqrt|isSquare|X2|PI|X3|angleFrom|mod1|C2|mod2|sin|cos|break|C3|toRightTriangular|false|Y3|to3D|E2|E1|E3|Rotation|Y2|Y1|intersectionWith|rotate|v12|v13|rank|setVectors|nc|sum|multiply|prototype|eql|new|setElements|case|each|PA3|PA2|part|new_element|round|for|cross|product|AD2|isSameSizeAs|add|isPerpendicularTo|v22|AN3|inspect|AD3|AN2|toUnitVector|PsubQ3|PsubQ2|v23|dot|divisor|inverse|true|isSingular|determinant|max|canMultiplyFromLeft|subtract|rows|col|random|ZX|YZ|XY|Random|join|N2|N1|D1|slice|default|N3|dimensions|switch|liesIn|translate|snapTo|augment|Diagonal|trace|indexOf|diagonal|transpose|minor|row|isAntiparallelTo|ZY|YX|acos|RotationZ|RotationY|liesOn|RotationX|inv|rk|tr|det|toDiagonalMatrix|toUpperTriangular|version|XZ'.split('|'),0,{}));
// zig.js main closure
(function(){
//-----------------------------------------------------------------------------
// Helper objects
//-----------------------------------------------------------------------------
function Events() {
var events = {};
var listeners = [];
function addEventListener(eventName, callback) {
eventName = "on" + eventName;
if (!events.hasOwnProperty(eventName)) {
events[eventName] = [];
}
events[eventName].push(callback);
return callback;
}
function removeEventListener(eventName, callback) {
eventName = "on" + eventName;
if (!events.hasOwnProperty(eventName)) return;
var i = events[eventName].indexOf(callback);
if (1 >= 0) events[eventName].splice(i,1);
}
function addListener(listener) {
listeners.push(listener);
return listener;
}
function removeListener(listener) {
if (undefined === listener) {
listeners = [];
return;
}
var i = listeners.indexOf(listener);
if (i>=0) listeners.splice(i,1);
}
function _sendEvent(target, eventName, arg) {
if (target.hasOwnProperty(eventName)) {
try {
target[eventName].call(target, arg);
} catch (e) {
console.error("Error calling callback for " + eventName + ":\n" + e.stack);
}
}
}
function fireEvent(eventName, arg, specificListener) {
eventName = "on" + eventName;
// first listeners
listeners.forEach(function(listener) {
if (undefined !== specificListener && specificListener != listener) return;
_sendEvent(listener, eventName, arg);
});
// and then events
if (events.hasOwnProperty(eventName)) {
events[eventName].forEach(function(cb) {
if (undefined !== specificListener && cb != specificListener) return;
try {
cb.call(null, arg);
} catch (e) {
console.error("Error calling callback for " + eventName + ":\n" + e.stack);
}
});
}
}
function eventify(obj) {
obj.addEventListener = addEventListener;
obj.removeEventListener = removeEventListener;
obj.addListener = function(listener) {
addListener(listener);
_sendEvent(listener, 'onattach', obj);
_sendEvent(obj, 'onlistenerattach', listener);
return listener;
}
obj.removeListener = function(listener) {
fireEvent('detach', obj, listener);
if (undefined === listener) {
listeners.forEach(function (l) {
_sendEvent(obj, 'onlistenerdetach', l);
});
} else {
_sendEvent(obj, 'onlistenerdetach', listener);
}
removeListener(listener);
}
return obj;
}
return {
addEventListener : addEventListener,
removeEventListener : removeEventListener,
addListener : addListener,
removeListener : removeListener,
fireEvent : fireEvent,
eventify : eventify,
}
}
function BoundingBox(size, center) {
if (undefined === center) {
center = [0,0,0];
}
if (undefined === size) {
size = [0,0,0];
}
var center = $V(center);
var size = $V(size);
var extents = size.multiply(0.5);
var min = center.subtract(extents);
var max = center.add(extents);
function contains(point) {
point = $V(point);
return (point.e(1) >= min.e(1) && point.e(1) <= max.e(1) &&
point.e(2) >= min.e(2) && point.e(2) <= max.e(2) &&
point.e(3) >= min.e(3) && point.e(3) <= max.e(3));
}
function recenter(newCenter) {
center = $V(newCenter);
min = center.subtract(extents);
max = center.add(extents);
}
function resize(newSize) {
size = $V(newSize);
extents = size.multiply(0.5);
min = center.subtract(extents);
max = center.add(extents);
}
function inspect() {
console.log({center: center, size: size, min : min, max : max});
}
return {
contains : contains,
resize : resize,
recenter : recenter,
inspect : inspect,
getsize : function() { return size; },
getcenter : function() { return center; },
getmin : function() { return min; },
getmax : function() { return max; },
}
}
function FpsCounter() {
var lastFrame;
var pub = {
markframe : markframe,
lastDelta : 0,
fps : 0,
}
function markframe(timestamp) {
timestamp = timestamp || (new Date()).getTime();
lastFrame = lastFrame || timestamp;
pub.lastDelta = ((timestamp - lastFrame) / 1000);
pub.fps = 1 / pub.lastDelta;
lastFrame = timestamp;
}
return pub;
}
function clamp(x, min, max) {
if (x < min) return min;
if (x > max) return max;
return x;
}
function vclamp(v, vmin, vmax) {
return [clamp(v[0], vmin[0], vmax[0]), clamp(v[1], vmin[1], vmax[1]), clamp(v[2], vmin[2], vmax[2])];
}
function lerp(from, to, amount) {
return from + ((to - from) * amount);
}
function vlerp(p1,p2,r) {
out = [];
for (i=0;i<p1.length;i++) {
out.push(p2[i]*r+p1[i]*(1-r));
}
return out;
}
function vscale(v1, v2) {
return [v1[0]*v2[0], v1[1]*v2[1], v1[2]*v2[2]];
}
// enum: zig.Joint
// List of Joint ID's
var Joint = {
Invalid : 0,
Head : 1,
Neck : 2,
Torso : 3,
Waist : 4,
LeftCollar : 5,
LeftShoulder : 6,
LeftElbow : 7,
LeftWrist : 8,
LeftHand : 9,
LeftFingertip : 10,
RightCollar : 11,
RightShoulder : 12,
RightElbow : 13,
RightWrist : 14,
RightHand : 15,
RightFingertip : 16,
LeftHip : 17,
LeftKnee : 18,
LeftAnkle : 19,
LeftFoot : 20,
RightHip : 21,
RightKnee : 22,
RightAnkle : 23,
RightFoot : 24,
ExternalHandpoint : 100,
};
// enum: zig.Orientation
// Possible orientations for oriented controls (<Fader>, for instance)
var Orientation = {
X : 0,
Y : 1,
Z : 2,
}
//-----------------------------------------------------------------------------
// class: SteadyDetector
// Detects steady
//
// event: steady
// Triggered when hand is steady. Triggered only once per steady
//
// event: unsteady
// Triggered when hand is unsteady. Triggered only once per unsteady.
//-----------------------------------------------------------------------------
function SteadyDetector(maxVariance) {
if (undefined === maxVariance) {
maxVariance = 50;
}
var frameCount = 15;
var pointBuffer = [];
var maxVariance = maxVariance;
var events = Events();
function sumMatrix(mat) {
var sum = 0;
elements = mat.elements
for(var i=0; i<elements.length; i++) {
for(var j=0; j<elements[i].length; j++) {
sum += elements[i][j];
}
}
return sum;
}
// Reference : Oliver K. Smith: Eigenvalues of a symmetric 3 × 3 matrix. Commun. ACM 4(4): 168 (1961)
// find the eigenvalues of a 3x3 symmetric matrix
function getEigenvalues(mat) {
var m = mat.trace() / 3;
var K = mat.subtract( Matrix.I(3).x(m)); // K = mat - I*tr(mat)
var q = K.determinant() / 2;
var tempForm = K.x(K);
var p = sumMatrix(tempForm) / 6;
// NB in Smith's paper he uses phi = (1/3)*arctan(sqrt(p*p*p - q*q)/q), which is equivalent to below:
var phi = (1/3)*Math.acos(q/Math.sqrt(p*p*p));
if (Math.abs(q) >= Math.abs(Math.sqrt(p*p*p))) {
phi = 0;
}
if (phi < 0) {
phi = phi + Math.PI/3;
}
var eig1 = m + 2*Math.sqrt(p)*Math.cos(phi);
var eig2 = m - Math.sqrt(p)*(Math.cos(phi) + Math.sqrt(3)*Math.sin(phi));
var eig3 = m - Math.sqrt(p)*(Math.cos(phi) - Math.sqrt(3)*Math.sin(phi));
return [eig1, eig2, eig3];
}
function getCofactorMatrix(mat) {
var dims = mat.dimensions();
var xSize = dims.cols;
var ySize = dims.rows;
var output = mat.map(function(x, i, j) { return mat.minor(i+1,j+1,xSize-1, ySize-1).determinant(); } );
return output;
}
function getStddevs(vectors) {
if (vectors.length == 0) { return []; }
var sum = Vector.Zero(vectors[0].dimensions());
for (var k=0; k<vectors.length; k++) {
sum = sum.add(vectors[k]);
}
var avg = sum.multiply(1/(vectors.length));
var covarianceMatrix = Matrix.Zero(avg.dimensions(), avg.dimensions());
for (var k=0; k<vectors.length; k++) {
var temp = vectors[k].subtract(avg);
covarianceMatrix = covarianceMatrix.map(function(x, i, j) { return x + temp.elements[i-1]*temp.elements[j-1]; } );
}
var values = getEigenvalues(covarianceMatrix);
for (var k=0; k<values.length; k++) {
values[k] = Math.sqrt(Math.abs(values[k]));
}
return values;
}
function clear() {
pointBuffer = [];
publicApi.isSteady = false;
}
// method: addPosition
// Add an external position [x,y,z] to the steady detector
//
// Arguments:
// position - the external position [x,y,z] to add
function addPosition(position) {
pointBuffer.push($V(position));
while (pointBuffer.length > frameCount) {
pointBuffer.shift();
}
pb = pointBuffer;
var steadyThisFrame = true;
var stdDevs = getStddevs(pointBuffer);
for (var k=0; k<stdDevs.length; k++) {
steadyThisFrame &= stdDevs[k] < publicApi.maxVariance;
}
if (steadyThisFrame && (!publicApi.isSteady)) {
publicApi.isSteady = true;
events.fireEvent('steady', publicApi);
events.fireEvent('steadychanged', publicApi);
} else if (!steadyThisFrame && publicApi.isSteady) {
publicApi.isSteady = false;
events.fireEvent('unsteady', publicApi);
events.fireEvent('steadychanged', publicApi);
}
}
function onsessionstart(focusPosition) {
clear();
}
function onsessionupdate(position) {
addPosition(position);
}
var publicApi = {
addPosition : addPosition,
// property: maxVariance
// Steady detector sensitivity
maxVariance : maxVariance,
onsessionstart : onsessionstart,
onsessionupdate : onsessionupdate,
// property: isSteady
// Is hand steady right now? *Read only*.
isSteady : false,
}
events.eventify(publicApi);
return publicApi;
}
//-----------------------------------------------------------------------------
// class: Fader
// Axis aligned fader. Can be oriented on x, y, or z. The fader can also be split up into logical items using <itemsCount>.
//
// event: valuechange
// Triggered whenever the fader value changes
//
// event: edge
// Triggered when the fader reaches an edge (value of 0 or 1). Will only trigger once per edge
//
// event: hoverstart
// Triggered when the hand is hovering over <Fader.hoverItem>
//
// event: hoverstop
// Triggered when the hand is not hovering over <Fader.hoverItem> any more
//-----------------------------------------------------------------------------
function Fader(orientation, size) {
// defaults
size = size || 250;
// return object
var api = {
// property: itemsCount
// How many logical items on our Fader
itemsCount : 1,
// property: hysteresis
// Used by <Fader.hoverstart> and <Fader.hoverstop> events.
hysteresis : 0.1,
// property: initialValue
// Value of fader when hand is in the initial focus position of the UI session
initialValue : 0.5,
// property: flip
// Use this to flip the value of the fader
flip : false,
// property: value
// Current fader value. Useful for visualizing the fader
value : 0,
// property: hoverItem
// Logical hovered item. -1 if not in session
hoverItem : -1,
// property: driftAmount
// If larger than 0, the fader will drift towards current hand point until fader <value> is <initialValue>
driftAmount : 0,
// property: autoMoveToContain
// If true, the fader will always move to contain current hand point
autoMoveToContain : false,
// property: size
// Physical size of fader, in millimeters
size : size,
// property: orientation
// Which <zig.Orientation> is this fader aligned to?
orientation : orientation,
updatePosition : updatePosition,
updateValue : updateValue,
moveTo : moveTo,
moveToContain : moveToContain,
onsessionstart : onsessionstart,
onsessionupdate : onsessionupdate,
onsessionend : onsessionend,
}
var events = Events();
events.eventify(api);
var isEdge = false;
var center = [0,0,0];
var fps = FpsCounter();
// hand point control callbacks
function onsessionstart(focusPosition) {
moveTo(focusPosition, api.initialValue);
api.value = api.initialValue;
api.hoverItem = Math.floor(api.itemsCount * api.value);
events.fireEvent('hoverstart', api);
}
function onsessionupdate(position) {
updatePosition(position);
}
function onsessionend() {
events.fireEvent('hoverstop', api);
api.hoverItem = -1;
}
// method: updatePosition
// Manually update a fader with external positions
//
// Arguments:
// position - external position [x,y,z]
function updatePosition(position) {
fps.markframe();
if (api.autoMoveToContain) {
moveToContain(position);
}
var distanceFromCenter = position[api.orientation] - center[api.orientation];
var ret = (distanceFromCenter / api.size) + 0.5;
ret = clamp(ret, 0, 1);
if (api.flip) ret = 1 - ret;
updateValue(ret);
if (api.driftAmount != 0) {
var delta = api.initialValue - api.value;
moveTo(position, api.value + (delta * 0.05));//api.driftAmount * fps.lastDelta));
}
}
// method: updateValue
// Manually update the fader with external values
//
// Arguments:
// value - normalized (0-1) external value
function updateValue(value) {
var newSelected = api.hoverItem;
var minValue = (api.hoverItem * (1 / api.itemsCount)) - api.hysteresis;
var maxValue = (api.hoverItem + 1) * (1 / api.itemsCount) + api.hysteresis;
api.value = value;
events.fireEvent('valuechange', api);
var isThisFrameEdge = (value == 0) || (value == 1);
if (!isEdge && isThisFrameEdge) {
events.fireEvent('edge', api);
}
isEdge = isThisFrameEdge;
if (api.value > maxValue) {
newSelected++;
}
if (api.value < minValue) {
newSelected--;
}
if (newSelected != api.hoverItem) {
events.fireEvent('hoverstop', api);
api.hoverItem = newSelected;
events.fireEvent('hoverstart', api);
}
}
// method: moveTo
// Move the fader so that when the hand is at given position, the <Fader.value> will be given value
//
// Arguments:
// position - position [x,y,z]
// value - target value for given position
function moveTo(position, value) {
if (api.flip) value = 1 - value;
center[api.orientation] = position[api.orientation] + ((0.5 - value) * api.size);
}
// method: moveToContain
// Move the fader to ensure that given position is within the fader bounds
//
// Arguments:
// position - position [x,y,z]
function moveToContain(position) {
var distanceFromCenter = position[api.orientation] - center[api.orientation];
if (distanceFromCenter > api.size / 2) {
center[api.orientation] += distanceFromCenter - (api.size / 2);
} else if (distanceFromCenter < api.size / -2) {
center[api.orientation] += distanceFromCenter + (api.size / 2);
}
}
return api;
}
// 3D fader
function Fader3D(size) {
var events = Events();
var api = {
size : size,
value : [0,0,0],
initialValue : [0.5, 0.5, 0.5],
onsessionstart : onsessionstart,
onsessionupdate : onsessionupdate,
onsessionend : function() {},
}
events.eventify(api);
var center = [0,0,0];
function onsessionstart(focusPosition) {
moveTo(focusPosition, api.initialValue);
}
function onsessionupdate(position) {
var d = $V(position).subtract($V(center)).elements;
var val = [clamp((d[0]/api.size[0]) + 0.5, 0, 1),
clamp((d[1]/api.size[1]) + 0.5, 0, 1),
clamp((d[2]/api.size[2]) + 0.5, 0, 1)];
updateValue(val);
}
function updateValue(value) {
api.value = value;
events.fireEvent('valuechange', api);
}
function moveTo(position, value) {
//var delta = vscale($V([0.5,0.5,0.5]).subtract($V(value)).elements, api.size);
//center = position.add($V(delta)).elements;
center[0] = position[0] + ((0.5 - value[0]) * api.size[0]);
center[1] = position[1] + ((0.5 - value[1]) * api.size[1]);
center[2] = position[2] + ((0.5 - value[2]) * api.size[2]);
}
return api;
}
// 2D fader
function Fader2D(width, height) {
width = width || 300;
height = height || 250;
var events = Events();
var api = {
width : width,
height : height,
value : [0,0],
onsessionstart : onsessionstart,
onsessionupdate : onsessionupdate,
onsessionend : onsessionend,
}
events.eventify(api);
var fader3d = Fader3D([width, height, 1]);
fader3d.addEventListener('valuechange', function(f) {
api.value[0] = f.value[0];
api.value[1] = f.value[1];
events.fireEvent('valuechange', api);
});
function onsessionstart(focusPosition) {
fader3d.onsessionstart(focusPosition);
}
function onsessionupdate(position) {
fader3d.size[0] = api.width;
fader3d.size[1] = api.height;
fader3d.onsessionupdate(position);
}
function onsessionend() {
fader3d.onsessionend();
}
return api;
}
//-----------------------------------------------------------------------------
// class: PushDetector
// Detects push gestures
//
// event: push
// Push
//
// event: release
// Release
//
// event: click
// Click
//-----------------------------------------------------------------------------
function PushDetector(size) {
size = size || 160;
var api = {
// property: isPushed
// push state, true when pushed. *read only*
isPushed : false,
// property: pushProgress
// Normalized push progress, useful for push visualization on a cursor
pushProgress : 0,
// property: pushTime
// Timestamp of last push
pushTime : 0,
// property: pushPosition
// position of last push event
pushPosition : [0,0,0],
// property: driftAmount
// How fast should the push detector drift after the hand point
driftAmount : 15,
// method: release
// force a release. should be called after the push event, and before the
// release event.
release : release,
fader : undefined,
onsessionstart: onsessionstart,
onsessionupdate: onsessionupdate,
onsessionend : onsessionend,
}
var events = Events();
events.eventify(api);
var fader = Fader(Orientation.Z, size);
fader.flip = true; // positive Z is backwards by default, so flip it
fader.initialValue = 0.2;
fader.autoMoveToContain = true;
api.fader = fader;
fader.driftAmount = api.driftSpeed; // mm/s
function onsessionstart(focusPosition) {
fader.onsessionstart(focusPosition);
}
function onsessionupdate(position) {
fader.moveToContain(position);
fader.onsessionupdate(position);
api.pushProgress = fader.value;
if (!api.isPushed) {
if (1.0 == api.pushProgress) {
api.isPushed = true;
api.pushTime = (new Date()).getTime();
api.pushPosition = position;
fader.driftAmount = 0; // stop drifting when pushed
events.fireEvent('push', api);
}
} else {
if (api.pushProgress < 0.5) {
release();
}
}
}
function release() {
if (!api.isPushed) return;
api.isPushed = false;
fader.driftAmount = api.driftAmount;
events.fireEvent('release', api);
if (isClick()) {
events.fireEvent('click', api);
}
}
function onsessionend() {
fader.onsessionend();
if (api.isPushed) {
api.isPushed = false;
events.fireEvent('release', api);
}
}
function isClick() {
var delta = (new Date()).getTime() - api.pushTime;
return (delta < 1000);
}
return api;
}
//PullDetector
function PullDetector(size) {
size = size || 160;
var api = {
// property: isPulled
// pull state, true when pulled. *read only*
isPulled : false,
// property: pullProgress
// Normalized pull progress, useful for pull visualization on a cursor
pullProgress : 0,
// property: pullTime
// Timestamp of last pull
pullTime : 0,
// property: pullPosition
// position of last pull event
pullPosition : [0,0,0],
// property: driftAmount
// How fast should the pull detector drift after the hand point
driftAmount : 15,
// method: release
// force a release. should be called after the pull event, and before the
// release event.
release : release,
fader : undefined,
onsessionstart: onsessionstart,
onsessionupdate: onsessionupdate,
onsessionend : onsessionend,
}
var events = Events();
events.eventify(api);
var fader = Fader(Orientation.Z, size);
// fader.flip = true; // Don't flip for pulling
fader.initialValue = 0.2;
fader.autoMoveToContain = true;
api.fader = fader;
fader.driftAmount = api.driftSpeed; // mm/s
function onsessionstart(focusPosition) {
fader.onsessionstart(focusPosition);
}
function onsessionupdate(position) {
fader.moveToContain(position);
fader.onsessionupdate(position);
api.pullProgress = fader.value;
if (!api.isPulled) {
if (1.0 == api.pullProgress) {
api.isPulled = true;
api.pullTime = (new Date()).getTime();
api.pullPosition = position;
fader.driftAmount = 0; // stop drifting when pulled
events.fireEvent('pull', api);
}
} else {
if (api.pullProgress < 0.5) {
release();
}
}
}
function release() {
if (!api.isPulled) return;
api.isPulled = false;
fader.driftAmount = api.driftAmount;
events.fireEvent('release', api);
if (isClick()) {
events.fireEvent('click', api);
}
}
function onsessionend() {
fader.onsessionend();
if (api.isPulled) {
api.isPulled = false;
events.fireEvent('release', api);
}
}
function isClick() {
var delta = (new Date()).getTime() - api.pullTime;
return (delta < 1000);
}
return api;
}
//-----------------------------------------------------------------------------
// class: SwipeDetector
// Detects swipes
//
// event: swipeup
// Swipe up
//
// event: swipedown
// Swipe down
//
// event: swipeleft
// Swipe left
//
// event: swiperight
// Swipe right
//
// event: swipe
// Triggered every swipe with direction argument. Direction will be one of 'up', 'down', 'left', or 'right'.
//
// event: swiperelease
// Triggered after a swipe is 'released', when the hand moves back towards the initial position
//-----------------------------------------------------------------------------
function SwipeDetector() {
var events = Events();
var horizontalFader = Fader(Orientation.X);
var verticalFader = Fader(Orientation.Y);
var api = {
// property: driftAmount
// How fast should the swipe detector follow the hand point
driftAmount : 20,
// property: horizontalFader
// Internal <Fader> used to detect horizontal swipes. *Read only*.
horizontalFader : horizontalFader,
// property: verticalFader
// Internal <Fader> used to detect vertical swipes. *Read only*.
verticalFader : verticalFader,
// property: isSwiped
// True when the swipe detector is 'swiped', between one of the swipe* events and the <swiperelease> event
isSwiped : false,
onattach : onattach,
ondetach : ondetach,
onedge : onedge,
onvaluechange : onvaluechange,
}
events.eventify(api);
horizontalFader.autoMoveToContain = true;
verticalFader.autoMoveToContain = true;
horizontalFader.driftAmount = api.driftAmount;
verticalFader.driftAmount = api.driftAmount;
horizontalFader.swipeDirections = ['left','right'];
verticalFader.swipeDirections = ['down','up'];
horizontalFader.addListener(api);
verticalFader.addListener(api);
function onedge(fader) {
var dir = fader.swipeDirections[fader.value];
events.fireEvent('swipe' + dir, api);
events.fireEvent('swipe', dir);
fader.driftAmount = 0;
fader.swipeValue = fader.value;
}
function onvaluechange(fader) {
if (undefined !== fader.swipeValue) {
if (Math.abs(fader.swipeValue - fader.value) >= 0.5) {
delete fader.swipeValue;
fader.driftAmount = api.driftAmount;
events.fireEvent('swiperelease', api);
}
}
}
function onattach(target) {
target.addListener(horizontalFader);
target.addListener(verticalFader);
}
function ondetach(target) {
target.removeListener(horizontalFader);
target.removeListener(verticalFader);
}
return api;
}
//-----------------------------------------------------------------------------
// class: Cursor
// Basic cursor, implemented using <Fader2D> and <PushDetector>
//
// event: push
// Push
//
// event: release
// Release
//
// event: click
// Click
//-----------------------------------------------------------------------------
function Cursor() {
var fader2d = Fader2D();
var pushDetector = PushDetector();
var events = Events();
var api = {
onattach : onattach,
ondetach : ondetach,
pushDetector : pushDetector,
fader2d : fader2d,
value : [0,0],
x : 0,
y : 0,
}
events.eventify(api);
fader2d.addEventListener('valuechange', function(f) {
api.value[0] = api.x = f.value[0];
api.value[1] = api.y = 1 - f.value[1];
events.fireEvent('move', api);
});
pushDetector.addEventListener('push', function(pd) {
events.fireEvent('push', api);
});
pushDetector.addEventListener('release', function(pd) {
events.fireEvent('release', api);
});
pushDetector.addEventListener('click', function(pd) {
events.fireEvent('click', api);
});
function onattach(target) {
target.addListener(fader2d);
target.addListener(pushDetector);
}
function ondetach(target) {
target.removeListener(fader2d);
target.removeListener(pushDetector);
}
return api;
}
//-----------------------------------------------------------------------------
// class: WaveDetector
// Detects wave gestures
//
// event: wave
// Triggered when a wave gesture is detected
//-----------------------------------------------------------------------------
function WaveDetector() {
var fader = Fader(Orientation.X, 100);
fader.autoMoveToContain = true;
fader.driftAmount = 15;
var api = {
onattach : onattach,
ondetach : ondetach,
// property: fader
// The <Fader> used internally by the wave detector
fader : fader,