/
BaseDirect.py
1378 lines (1205 loc) · 40.1 KB
/
BaseDirect.py
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
# won't be going vizard imports
import viz
import vizjoy
import vizshape
# python imports
import math
import random
import time
import pdb
import vizspace
import vizcam
MATCH_DISTANCE = .5
class Move:
NONE=0
RIGHT=1
LEFT=2
FORWARD=3
BACK=4
UP=5
DOWN=6
class Rotate:
NONE=0
CW=1
CCW=2
class Navigation:
walk = Move.NONE
strafe = Move.NONE
fly = Move.NONE
yaw = Rotate.NONE
pitch = Rotate.NONE
roll = Rotate.NONE
TRANSLATION_SPEED = 10
ROTATION_SPEED = 30
rotation_speed = ROTATION_SPEED
translation_speed = TRANSLATION_SPEED
walk_speed = TRANSLATION_SPEED
strafe_speed = TRANSLATION_SPEED
half_speed = False
class Colors:
BLACK = [0.0,0.0,0.0]
WHITE = [1.0,1.0,1.0]
RED = [1.0,0.0,0.0]
GREEN = [0.0,1.0,0.0]
BLUE = [0.0,0.0,1.0]
YELLOW = [1.0,1.0,0.0]
ORANGE = [1.0,0.5,0.0]
GREY = [0.5,0.5,0.5]
LIGHT_GREY = [0.75,0.75,0.75]
DARK_GREY = [0.25,0.25,0.25]
LIGHT_BLUE = [0.2,0.5,0.9]
highlight = YELLOW
puzzleEmpty = LIGHT_GREY
puzzleFilled = GREEN
class Attr:
puzzleIndices = [-1 for i in range(5)] #used to identify spaces to be marked filled/unfilled
placed = False #used to determine win
#-------------------------------------------------------------------------------
# createQuad(left_bot,left_top,right_bot,right_top)
#-------------------------------------------------------------------------------
def createQuad(left_bot,left_top,right_bot,right_top):
viz.startlayer(viz.QUADS)
viz.vertexcolor(0,0,0)
viz.vertex(left_bot)
viz.vertex(left_top)
viz.vertex(right_top)
viz.vertex(right_bot)
return viz.endlayer()
#-------------------------------------------------------------------------------
# createSolidBox(min,max)
#-------------------------------------------------------------------------------
def createSolidBox(min,max):
viz.startlayer(viz.QUADS, 'top')
viz.vertexcolor(1.0,0.5,0.0)
#top
#viz.vertexcolor(0.0,1.0,0.0)
viz.normal(0,1,0)
viz.vertex( max[0], max[1], min[2])
viz.vertex( min[0], max[1], min[2])
viz.vertex( min[0], max[1], max[2])
viz.vertex( max[0], max[1], max[2])
viz.startlayer(viz.QUADS,'bottom')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#bottom
#viz.vertexcolor(1.0,0.5,0.0)
viz.normal(0,-1,0)
viz.vertex( max[0], min[1], max[2])
viz.vertex( min[0], min[1], max[2])
viz.vertex( min[0], min[1], min[2])
viz.vertex( max[0], min[1], min[2])
viz.startlayer(viz.QUADS,'front')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#front
#viz.vertexcolor(1.0,0.0,0.0)
viz.normal(0,0,1)
viz.vertex( max[0], max[1], max[2])
viz.vertex( min[0], max[1], max[2])
viz.vertex( min[0], min[1], max[2])
viz.vertex( max[0], min[1], max[2])
viz.startlayer(viz.QUADS,'back')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#back
#viz.vertexcolor(1.0,1.0,0.0)
viz.normal(0,0,-1)
viz.vertex( max[0], min[1], min[2])
viz.vertex( min[0], min[1], min[2])
viz.vertex( min[0], max[1], min[2])
viz.vertex( max[0], max[1], min[2])
viz.startlayer(viz.QUADS,'left')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#left
#viz.vertexcolor(0.0,0.0,1.0)
viz.normal(-1,0,0)
viz.vertex( min[0], max[1], max[2])
viz.vertex( min[0], max[1], min[2])
viz.vertex( min[0], min[1], min[2])
viz.vertex( min[0], min[1], max[2])
viz.startlayer(viz.QUADS,'right')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#right
#viz.vertexcolor(1.0,0.0,1.0)
viz.normal(1,0,0)
viz.vertex( max[0], max[1], min[2])
viz.vertex( max[0], max[1], max[2])
viz.vertex( max[0], min[1], max[2])
viz.vertex( max[0], min[1], min[2])
return viz.endlayer()
#-------------------------------------------------------------------------------
# createWireBox(min,max)
#-------------------------------------------------------------------------------
def createWireBox(min,max):
viz.startlayer(viz.LINE_LOOP, 'top')
viz.vertexcolor(0.0,0.0,0.0)
viz.linewidth(2.0)
#top
#viz.vertexcolor(0.0,1.0,0.0)
#viz.normal(0,1,0)
viz.vertex( max[0], max[1], min[2])
viz.vertex( min[0], max[1], min[2])
viz.vertex( min[0], max[1], max[2])
viz.vertex( max[0], max[1], max[2])
viz.startlayer(viz.LINE_LOOP,'bottom')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#bottom
#viz.vertexcolor(1.0,0.5,0.0)
#viz.normal(0,-1,0)
viz.vertex( max[0], min[1], max[2])
viz.vertex( min[0], min[1], max[2])
viz.vertex( min[0], min[1], min[2])
viz.vertex( max[0], min[1], min[2])
viz.startlayer(viz.LINE_LOOP,'front')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#front
#viz.vertexcolor(1.0,0.0,0.0)
#viz.normal(0,0,1)
viz.vertex( max[0], max[1], max[2])
viz.vertex( min[0], max[1], max[2])
viz.vertex( min[0], min[1], max[2])
viz.vertex( max[0], min[1], max[2])
viz.startlayer(viz.LINE_LOOP,'back')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#back
#viz.vertexcolor(1.0,1.0,0.0)
#viz.normal(0,0,-1)
viz.vertex( max[0], min[1], min[2])
viz.vertex( min[0], min[1], min[2])
viz.vertex( min[0], max[1], min[2])
viz.vertex( max[0], max[1], min[2])
viz.startlayer(viz.LINE_LOOP,'left')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#left
#viz.vertexcolor(0.0,0.0,1.0)
#viz.normal(-1,0,0)
viz.vertex( min[0], max[1], max[2])
viz.vertex( min[0], max[1], min[2])
viz.vertex( min[0], min[1], min[2])
viz.vertex( min[0], min[1], max[2])
viz.startlayer(viz.LINE_LOOP,'right')
viz.vertexcolor(.5,.5,0.0)
viz.linewidth(2.0)
#right
#viz.vertexcolor(1.0,0.0,1.0)
#viz.normal(1,0,0)
viz.vertex( max[0], max[1], min[2])
viz.vertex( max[0], max[1], max[2])
viz.vertex( max[0], min[1], max[2])
viz.vertex( max[0], min[1], min[2])
return viz.endlayer()
#-------------------------------------------------------------------------------
# createPuzzleCube()
#-------------------------------------------------------------------------------
def createPuzzleCube(boxR=1,boxG=1,boxB=1,outR=0,outG=0,outB=0):
# create the solid box
box = createSolidBox([-0.5,-0.5,-0.5],[0.5,0.5,0.5])
box.color(boxR,boxG,boxB)
box.alpha(0.8)
# create the box outline
wire = createWireBox([-0.5,-0.5,-0.5],[0.5,0.5,0.5])
wire.parent(box)
wire.color(outR,outG,outB)
wire.alpha(1.0)
return box
#-------------------------------------------------------------------------------
# buildPuzzle(x,y,z)
#-------------------------------------------------------------------------------
def buildPuzzle(x,y,z):
global shape
global blockState
global pieces
global pieceAttr
global placeable
placeable = False
blockState = []
pieceAttr = []
#Build puzzle space
for i in range(x):
for j in range(y):
for k in range(z):
box = createSolidBox([-0.5,-0.5,-0.5],[0.5,0.5,0.5])
box.color(Colors.LIGHT_GREY)
box.alpha(0.0)
box.parent(shape)
box.setPosition([0.5+i,0.5+j,0.5+k])
blockState.append(False)
wire = createWireBox([-1.5,-1.5,-1.5],[1.5,1.5,1.5])
wire.color(0,0,0)
wire.alpha(1.0)
wire.setPosition([1.5, 1.5, 1.5])
# piece 1 - L shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+2.0,0.5])
piece1.setPosition([-3.0,0.0,0.0])
pieces.append(piece1)
# piece 2 - T shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5-1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
piece1.setPosition([-6.0,0.0,0.0])
pieces.append(piece1)
# piece 3 - small L shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
piece1.setPosition([-10.0,0.0,0.0])
pieces.append(piece1)
# piece 4 - Z shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5-1.0,0.5+1.0,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
piece1.setPosition([-13.0,0.0,0.0])
pieces.append(piece1)
# piece 5 - curved Z shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5-1.0])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
piece1.setPosition([-17.0,0.0,0.0])
pieces.append(piece1)
# piece 6 - curved Z shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5+1.0])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5+1.0])
piece1.setPosition([7.0,0.0,0.0])
pieces.append(piece1)
# piece 7 - curved T shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5+1.0])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
piece1.setPosition([4.0,0.0,0.0])
pieces.append(piece1)
#build the parallel pieceAttrs (piece attributes) array
for p in pieces:
tmp = Attr()
tmp.puzzleIndices = [-1 for i in range( len(p.getChildren()) )]
pieceAttr.append(tmp)
#-------------------------------------------------------------------------------
# buildSideBar()
#-------------------------------------------------------------------------------
def buildSideBar():
global pieces
global sidebar
global sidebarBG
# piece 1 - L shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+2.0,0.5])
sidebar.append(piece1)
# piece 2 - T shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5-1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
sidebar.append(piece1)
# piece 3 - small L shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
sidebar.append(piece1)
# piece 4 - Z shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5-1.0,0.5+1.0,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
sidebar.append(piece1)
# piece 5 - curved Z shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5-1.0])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
sidebar.append(piece1)
# piece 6 - curved Z shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5+1.0])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5+1.0])
sidebar.append(piece1)
# piece 7 - curved T shaped
piece1 = viz.add(viz.GROUP,viz.WORLD)
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5+1.0,0.5,0.5])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5,0.5+1.0])
box = createPuzzleCube(0.2,0.5,0.8)
box.parent(piece1)
box.setPosition([0.5,0.5+1.0,0.5])
sidebar.append(piece1)
#-------------------------------------------------------------------------------
# keyDown(whichKey)
#-------------------------------------------------------------------------------
def keyDown(whichKey):
global debug
global cursor
global cursorPos
global highlightedObj
global highlightedObjType
global selectedObj
global selectedObjType
global selectedIndex
global blockState
global absCursorPos
absCursorPos = cursor.getPosition(viz.ABS_GLOBAL)
#print 'The following key was pressed: ', whichKey
if whichKey == 'x':
debug = not debug
# navigation
if whichKey == viz.KEY_UP:
viz.MainView.move(0,0,Navigation.translation_speed*viz.elapsed(),viz.BODY_ORI)
elif whichKey == viz.KEY_DOWN:
viz.MainView.move(0,0,-Navigation.translation_speed*viz.elapsed(),viz.BODY_ORI)
if whichKey == viz.KEY_LEFT:
Navigation.yaw = Rotate.CCW
elif whichKey == viz.KEY_RIGHT:
Navigation.yaw = Rotate.CW
if whichKey == ']':
Navigation.pitch = Rotate.CCW
elif whichKey == '[':
Navigation.pitch = Rotate.CW
if whichKey == '}':
Navigation.roll = Rotate.CCW
elif whichKey == '{':
Navigation.roll = Rotate.CW
if whichKey == 'i':
Navigation.fly = Move.UP
elif whichKey == 'k':
Navigation.fly = Move.DOWN
# cursor
if whichKey == 'w':
cursorPos[1] += 0.05
elif whichKey == 's':
cursorPos[1] -= 0.05
if whichKey == 'a':
cursorPos[0] -= 0.05
elif whichKey == 'd':
cursorPos[0] += 0.05
if whichKey == 'e':
cursorPos[2] += 0.05
elif whichKey == 'q':
cursorPos[2] -= 0.05
# selection
if whichKey == viz.KEY_RETURN:
selection();
rem_piece();
# #picking a piece up
# if selectedObj == None and highlightedObj != None:
# if highlightedObjType == 'piece':
# for i in pieceAttr[selectedIndex].puzzleIndices:
# blockState[i] = False
# selectedObj = highlightedObj
# selectedObjType = highlightedObjType
# selectedIndex = pieces.index(selectedObj)
# pieceAttr[selectedIndex].placed = False
# rem_piece()
#
# # piece placement (try to drop a piece)
# elif selectedObj != None:
# if place_piece():
# check_for_win()
# selectedObj = None
# selectedObjType = None
# selectedIndex = -1
# selection #2 (up/down piece selection)
if whichKey == 'm':
cycle_select(forward = True)
elif whichKey == 'n':
cycle_select(forward = False)
# translation of the selected object is done by moving the cursor
# orientation
if selectedObj != None:
if whichKey == 't':
selectedObj.setEuler(0,90,0,viz.REL_LOCAL)#yaw,pitch,roll
elif whichKey == 'g':
selectedObj.setEuler(0,-90,0,viz.REL_LOCAL)#yaw,pitch,roll
if whichKey == 'f':
selectedObj.setEuler(90,0,0,viz.REL_LOCAL)#yaw,pitch,roll
elif whichKey == 'h':
selectedObj.setEuler(-90,0,0,viz.REL_LOCAL)#yaw,pitch,roll
if whichKey == 'y':
selectedObj.setEuler(0,0,90,viz.REL_LOCAL)#yaw,pitch,roll
elif whichKey == 'r':
selectedObj.setEuler(0,0,-90,viz.REL_LOCAL)#yaw,pitch,roll
#-------------------------------------------------------------------------------
# cycle_select
#-------------------------------------------------------------------------------
def cycle_select(forward = True):
global selectedObj
global selectedIndex
global selectedObjType
global pieces
global pieceAttr
if forward:
while True:
selectedIndex += 1
if selectedIndex >= len(pieces):
selectedIndex = -1
break
if not pieceAttr[selectedIndex].placed:
break
else:
while True:
selectedIndex -= 1
if selectedIndex < -1:
selectedIndex = len(pieces)
continue
if selectedIndex == -1:
break
if not pieceAttr[selectedIndex].placed:
break
if selectedIndex == -1:
selectedObj.setPosition([25, 25, 25], viz.ABS_GLOBAL)
selectedObj.visible(False)
selectedObj = None
selectedObjType = None
else:
if selectedObj != None:
selectedObj.setPosition([25, 25, 25], viz.ABS_GLOBAL)
selectedObj.visible(False)
selectedObj = pieces[selectedIndex]
selectedObj.visible(True)
selectedObjType = 'piece'
#-------------------------------------------------------------------------------
# keyUp(whichKey)
#-------------------------------------------------------------------------------
def keyUp(whichKey):
#print 'The following key was released: ', whichKey
if whichKey == viz.KEY_UP or whichKey == viz.KEY_DOWN:
Navigation.walk = Move.NONE
if whichKey == viz.KEY_LEFT or whichKey == viz.KEY_RIGHT:
Navigation.yaw = Rotate.NONE
if whichKey == ']' or whichKey == '[':
Navigation.pitch = Rotate.NONE
if whichKey == '}' or whichKey == '{':
Navigation.roll = Rotate.NONE
if whichKey == 'i' or whichKey == 'k':
Navigation.fly = Move.NONE
#-------------------------------------------------------------------------------
# place_piece
#-------------------------------------------------------------------------------
def place_piece():
global selectedObj
global blockState
global deltaVector
global stateIndices
global placeable
print "place_piecefxn"
if selectedObj != None:
if placeable:
pieceAttr[selectedIndex].placed = True
for i in pieceAttr[selectedIndex].puzzleIndices:
blockState[i] = True
for block in selectedObj.getChildren():
block.color(Colors.GREEN)
print "placed & colored green"
print block
pos = selectedObj.getPosition(viz.ABS_GLOBAL)
selectedObj.setPosition([pos[0]+deltaVector[0], pos[1]+deltaVector[1], pos[2]+deltaVector[2]], viz.ABS_GLOBAL)
return True
return False
#-------------------------------------------------------------------------------
# remove_piece. opposite of place piece, since a user could re-position a piece
#-------------------------------------------------------------------------------
def rem_piece():
global selectedObj
global blockState
global deltaVector
global stateIndices
if selectedObj != None:
pieceAttr[selectedIndex].placed = False
for i in pieceAttr[selectedIndex].puzzleIndices:
blockState[i] = False
for block in selectedObj.getChildren():
block.color(Colors.GREEN)
# pos = selectedObj.getPosition(viz.ABS_GLOBAL)
# selectedObj.setPosition([pos[0]+deltaVector[0], pos[1]+deltaVector[1], pos[2]+deltaVector[2]], viz.ABS_GLOBAL)
# return True
# return False
#-------------------------------------------------------------------------------
# check_for_win
#-------------------------------------------------------------------------------
def check_for_win():
global blockState
global won
count = 0
for p in pieceAttr:
if p.placed is not True:
print count
return False
count += 1
print "You win"
won = True
#-------------------------------------------------------------------------------
# updateView()
#-------------------------------------------------------------------------------
def updateView():
if Navigation.walk == Move.FORWARD:
viz.MainView.move(0,0,Navigation.translation_speed*viz.elapsed(),viz.BODY_ORI)
elif Navigation.walk == Move.BACK:
viz.MainView.move(0,0,-Navigation.translation_speed*viz.elapsed(),viz.BODY_ORI)
if Navigation.yaw == Rotate.CW: #right
viz.MainView.setEuler([Navigation.rotation_speed*viz.elapsed()*2.0,0,0],viz.BODY_ORI,viz.REL_PARENT)
elif Navigation.yaw == Rotate.CCW: #left
viz.MainView.setEuler([-Navigation.rotation_speed*viz.elapsed()*2.0,0,0],viz.BODY_ORI,viz.REL_PARENT)
if Navigation.pitch == Rotate.CW: #down
viz.MainView.setEuler([0,Navigation.rotation_speed*viz.elapsed(),0],viz.HEAD_ORI,viz.REL_LOCAL)
elif Navigation.pitch == Rotate.CCW: #up
viz.MainView.setEuler([0,-Navigation.rotation_speed*viz.elapsed(),0],viz.HEAD_ORI,viz.REL_LOCAL)
if Navigation.roll == Rotate.CW: #right
viz.MainView.setEuler([0,0,Navigation.rotation_speed*viz.elapsed()],viz.HEAD_ORI,viz.REL_LOCAL)
elif Navigation.roll == Rotate.CCW: #left
viz.MainView.setEuler([0,0,-Navigation.rotation_speed*viz.elapsed()],viz.HEAD_ORI,viz.REL_LOCAL)
if Navigation.fly == Move.UP:
viz.MainView.move(0,Navigation.TRANSLATION_SPEED/2*viz.elapsed(),0,viz.BODY_ORI)
elif Navigation.fly == Move.DOWN:
viz.MainView.move(0,-Navigation.TRANSLATION_SPEED/2*viz.elapsed(),0,viz.BODY_ORI)
#checks if this is a throw away or not
def isSelPieceInPuzzle():
global shape
global selectedObj
deltaVector = [10,10,10]
#check each block of the piece against each box in the puzzle space
for i, pieceBox in enumerate(selectedObj.getChildren()):
pbc = pieceBox.getBoundingBox(viz.ABS_GLOBAL).center
pieceAttr[selectedIndex].puzzleIndices[i] = -1
sbc = [0, 0, 0]#$ shapeBox.getBoundingBox(viz.ABS_GLOBAL).center
vec = [sbc[0]-pbc[0], sbc[1]-pbc[1], sbc[2]-pbc[2]]
mag = magnitude(vec)
if mag <= 5:
return True
#for j, shapeBox in enumerate(shape.getChildren()):
# print shape.getChildren()
#if mag < magnitude(deltaVector):
# deltaVector = vec #everything is orthogonal, so all vectors will be the same size
return False
#-------------------------------------------------------------------------------
# updateCursor()
#-------------------------------------------------------------------------------
def updateCursor():
global cursor
global cursorPos
global shape
global highlightedObj
global highlightedObjType
global selectedObj
global selectedObjType
global selectedIndex
global deltaVector
global blockState
global pieceAttr
global placeable
global is900Sensor1
global iSenseMode
# update cursor position. Not relevant for iSense, so commenting it out
# cursor.setMatrix(viz.MainView.getMatrix(), viz.ABS_GLOBAL)
# cursor.setPosition(cursorPos, viz.REL_LOCAL)
# absPos = cursor.getPosition(viz.ABS_GLOBAL)
# if we're debugging on keyboard, use keyboard. Otherwise, use wand/spacemouse combo
if iSenseMode == True: # iSense-specific code
cursor.setMatrix(viz.MainView.getMatrix(), viz.ABS_GLOBAL)
sensorPos = is900Sensor1.getPosition()
sensorPos = [(-sensorPos[0]+1.6)*10.0,(sensorPos[1]-1.3)*10.0,(-sensorPos[2]+.53)*10.0]
cursor.setPosition(sensorPos, viz.REL_LOCAL)
absPos = cursor.getPosition(viz.ABS_GLOBAL)
else:
#print "using keyboard now"
cursor.setMatrix(viz.MainView.getMatrix(), viz.ABS_GLOBAL)
cursor.setPosition(cursorPos, viz.REL_LOCAL)
absPos = cursor.getPosition(viz.ABS_GLOBAL)
# print absPos[0], absPos[1], absPos[2]
# Using the Cursor
if selectedObj == None:
# print isCursorInSidebar(absPos)
if isCursorInSidebar(absPos):
checkCursorToPieceInSidebar()
else:
checkCursorToPieceInWorld()
#Dragging a piece
else:
placeable = True
deltaVector = [10,10,10]
#print blockState
#check each block of the piece against each box in the puzzle space
for i, pieceBox in enumerate(selectedObj.getChildren()):
pbc = pieceBox.getBoundingBox(viz.ABS_GLOBAL).center
pieceAttr[selectedIndex].puzzleIndices[i] = -1
for j, shapeBox in enumerate(shape.getChildren()):
# print shape.getChildren()
sbc = shapeBox.getBoundingBox(viz.ABS_GLOBAL).center
vec = [sbc[0]-pbc[0], sbc[1]-pbc[1], sbc[2]-pbc[2]]
mag = magnitude(vec)
if mag <= MATCH_DISTANCE and blockState[j] != True:
pieceAttr[selectedIndex].puzzleIndices[i] = j
if mag < magnitude(deltaVector):
deltaVector = vec #everything is orthogonal, so all vectors will be the same size
break
if pieceAttr[selectedIndex].puzzleIndices[i] == -1:
#box is outsize of puzzle or in an already filled space
placeable = False
pieceBox.color(Colors.RED)
if placeable:
for box in selectedObj.getChildren():
box.color(Colors.highlight)
def magnitude(vec):
return math.sqrt( (vec[0]*vec[0]) + (vec[1]*vec[1]) + (vec[2]*vec[2]))
#space mouse rotation
def spaceRot(e):
global identity
global viewpoint
global zoom
viewpoint.setAxisAngle(1,0,0,-e.ori[0]*e.elapsed/2.0,viz.REL_LOCAL)
viewpoint.setAxisAngle(0,1,0,-e.ori[1]*e.elapsed/2.0,viz.REL_LOCAL)
viewpoint.setAxisAngle(0,0,1,-e.ori[2]*e.elapsed/2.0,viz.REL_LOCAL)
#checks whether teh cursor can select in teh sidebar or not
def isCursorInSidebar(pos):
sPos = viz.MainWindow.worldToScreen(pos)
#print sPos
if sPos[0] > 0.85:
return True
return False
def checkCursorToPieceInSidebar():
global cursor
global cursorPos
global shape
global highlightedObj
global highlightedObjType
global selectedObj
global selectedObjType
global selectedIndex
global deltaVector
global blockState
global pieceAttr
global placeable
absPos = cursor.getPosition(viz.ABS_GLOBAL)
# check if cursor is inside any of the pieces
highlightedObj = None
highlightedObjType = None
isInside = False
#print len(sidebar)
for i, piece in enumerate(sidebar):
children = piece.getChildren()
# have to check each one of the individual boxes of the piece
for box in children:
pos = box.getPosition(viz.ABS_GLOBAL)
#print viz.MainWindow.worldToScreen(absPos), viz.MainWindow.worldToScreen(pos)
curPosFlat = viz.MainWindow.worldToScreen(absPos)
curPosFlat[2] = 0
blockPostFlat = viz.MainWindow.worldToScreen(pos)
blockPostFlat[2] = 0
#print curPosFlat, blockPostFlat
if pointsCloseEnough(curPosFlat, blockPostFlat, 0.025):#absPos[0] >= pos[0]-0.5 and absPos[0] <= pos[0]+0.5 and absPos[1] >= pos[1]-0.5 and absPos[1] <= pos[1]+0.5 and absPos[2] >= pos[2]-0.5 and absPos[2] <= pos[2]+0.5:
isInside = True
#print "inside sidebar box"
highlightedObj = piece
highlightedObjType = 'piece'
selectedIndex = i
break
# if the cursor is inside any of the boxes, highlight them all
if isInside == True or piece == selectedObj:
#print "coloring!"
for box in children:
box.color(Colors.highlight)
break
else:
for box in children:
box.color(Colors.LIGHT_BLUE)
print isInside
if isInside == False:
selectedIndex = -1
#checks the intersection of a cursor and a piece in the world view (as opposed to a piece in the side view)
def checkCursorToPieceInWorld():
global cursor
global cursorPos
global shape
global highlightedObj
global highlightedObjType
global selectedObj
global selectedObjType
global selectedIndex
global deltaVector
global blockState
global pieceAttr
global placeable
absPos = cursor.getPosition(viz.ABS_GLOBAL)
# check if cursor is inside any of the pieces
highlightedObj = None
highlightedObjType = None
isInside = False
for i, piece in enumerate(pieces):
children = piece.getChildren()
# have to check each one of the individual boxes of the piece
for box in children:
pos = box.getPosition(viz.ABS_GLOBAL)
if pointsCloseEnoughWorld(absPos, pos):#absPos[0] >= pos[0]-0.5 and absPos[0] <= pos[0]+0.5 and absPos[1] >= pos[1]-0.5 and absPos[1] <= pos[1]+0.5 and absPos[2] >= pos[2]-0.5 and absPos[2] <= pos[2]+0.5:
isInside = True
highlightedObj = piece
highlightedObjType = 'piece'
#selectedIndex = i
break
# if the cursor is inside any of the boxes, highlight them all
if isInside == True or piece == selectedObj:
for box in children:
box.color(Colors.highlight)
break
else:
for box in children:
box.color(Colors.LIGHT_BLUE)
#checks if two positions are close enough to one another. Used to count if a cursor is inside a box
def pointsCloseEnoughWorld(abs, pos):
return pointsCloseEnough(abs, pos, 0.5)
def pointsCloseEnough(absPos, pos, disp):
if absPos[0] >= pos[0]-disp and absPos[0] <= pos[0]+disp and absPos[1] >= pos[1]-disp and absPos[1] <= pos[1]+disp and absPos[2] >= pos[2]-disp and absPos[2] <= pos[2]+disp:
return True
return False