/
ledcubetiming.pde
471 lines (429 loc) · 13.1 KB
/
ledcubetiming.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
final static byte GRID = 4; //# of squares in grid.
int DIM = 40; //pixel dimension of each grid.
int count = 0; //what frame you're on
Rect[] rect; //declare the array of Rects.
PFont f;
int frameIncrease = 0; //used for small boxes
int framex = DIM * 3 * (GRID + 1);
int framey = DIM*5;
int framexdim = DIM*4;
int frameydim = DIM;
float filly = DIM * 6.5;
int specy = DIM * 8;
float arrowy = DIM * 9.5;
float savey = DIM * 11;
float exporty = DIM * 12.5;
boolean player = false;
int maxCount;
PrintWriter output;
int clearRow=5;
int timing[] = new int[1000];
void setup(){
size(DIM * 4 * (GRID + 1) - DIM,DIM*17,OPENGL);
f=createFont("Serif",20);
textFont(f);
rect = new Rect[4];
for(int i = 0; i < 4; i++)
rect[i] = new Rect(i);
}
void draw(){
//println(maxCount);
background(206);
//println(maxCount + ","+count);
for(int i = 0; i < 4; i++)
rect[i].drawRect(i);
drawButtons();
//rect(DIM*2,DIM*13.5,10,10);
noFill();
//for(int i=0;i<6;i++)
//rect(DIM*0.5 + DIM*3.05*i,DIM*14,DIM*2.8,DIM*2.8);
//rectangle that highlights current small box
if(count < 4)
rect(DIM*0.5 + DIM*3.05*count,DIM*14,DIM*2.8,DIM*2.8);
else
rect(DIM*0.5 + DIM*3.05*3,DIM*14,DIM*2.8,DIM*2.8);
drawBoxes();
drawSmallBoxes();
fill(0);
text(timing[count],width-DIM/2,DIM*4.6);
}
void keyPressed(){
println(keyCode);
if(key == ENTER || keyCode == 32){
count++;
maxCount++;
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[i].leds[count][row][col] = rect[i].leds[count-1][row][col]; //prevent leds from resetting after frame increases
}
if(count > 3) frameIncrease++;
}
if(keyCode==RIGHT){
if(count < maxCount){
count++;
if(count >3)
frameIncrease++;
}
}
if(keyCode==LEFT){
if(count > 0){
count--;
if(count >2)
frameIncrease--;
}
}
if(keyCode==UP){
if(timing[count] < 99)
timing[count]++;
}
if(keyCode==DOWN){
if(timing[count] > 0)
timing[count]--;
}
for(int i=0;i<4;i++)
if(keyCode==49+i)
clearRow=i;
}
void mousePressed(){
for(int n=0; n < 4; n++) //change the status of an LED
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col) {
if(mouseX>(row*DIM + DIM * n * (GRID + 1)) && mouseX<(row*DIM + DIM * n * (GRID + 1) +DIM) && mouseY>(col*DIM) && mouseY<(col*DIM +DIM))
rect[n].leds[count][row][col]= !rect[n].leds[count][row][col];
}
//if frame button is pressed
if(mouseX>(framex) && mouseX<(framex + framexdim/3) && mouseY>(framey) && mouseY<(framey + frameydim)){
if(count++ ==maxCount){
maxCount++;
}
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[i].leds[count][row][col] = rect[i].leds[count-1][row][col]; //prevent leds from resetting after frame increases
}
if(count > 3) frameIncrease++;
}
//if insert button is pressed
if(mouseX>(framex+framexdim/3) && mouseX<(framex + 2* framexdim/3) && mouseY>(framey) && mouseY<(framey + frameydim)){
count++;
maxCount++;
for(int q=maxCount;q>count;q--)
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[i].leds[q][row][col] = rect[i].leds[q-1][row][col]; //prevent leds from resetting after frame increases
}
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[i].leds[count][row][col] = rect[i].leds[count-1][row][col]; //prevent leds from resetting after frame increases
}
if(count > 3) frameIncrease++;
}
//if delete frame button is pressed
if(mouseX>(framex + 2*framexdim/3) && mouseX<(framex + framexdim) && mouseY>(framey) && mouseY<(framey + frameydim)){
//maxCount--;
for(int j=count;j<maxCount;j++)
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[i].leds[j][row][col] = rect[i].leds[j+1][row][col];
}
maxCount--;
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col)
rect[i].leds[maxCount+1][row][col] = false;
}
//if fill button is pressed
if(mouseX>(framex) && mouseX<(framex + framexdim/2) && mouseY>(filly) && mouseY<(filly + frameydim)){
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col)
rect[i].leds[count][row][col] = true; //fill
}
//if clear button is pressed
if(mouseX>(framex+framexdim/2) && mouseX<(framex + framexdim) && mouseY>(filly) && mouseY<(filly + frameydim)){
//for(int z=0; z< 4; z++)
if(clearRow != 5){
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[clearRow].leds[count][row][col] = false;
}
clearRow=5;
}
else
for(int i=0; i< 4; i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[i].leds[count][row][col] = false; //fill
}
}
//if left arrow is pressed
if(mouseX>(framex) && mouseX<(framex + framexdim/2) && mouseY>(arrowy) && mouseY<(arrowy + frameydim)){
if(count > 0){
count--;
if(count >2)
frameIncrease--;
}
}
//if right arrow is pressed
if(mouseX>(framex+framexdim/2) && mouseX<(framex+framexdim/2 + framexdim/2) && mouseY>(arrowy) && mouseY<(arrowy + frameydim)){
if(count < maxCount){
count++;
if(count >3)
frameIncrease++;
}
}
//if save button is pressed
if(mouseX>(framex) && mouseX<(framex+framexdim/2) && mouseY>(savey) && mouseY<(savey + frameydim)){
//saver();
selectOutput("Select a file to save to","saver");
}
//if load button is pressed
if(mouseX>(framex+framexdim/2) && mouseX<(framex+framexdim) && mouseY>(savey) && mouseY<(savey + frameydim)){
selectInput("Select a file to load from","load");
}
//if export button is pressed
if(mouseX>(framex) && mouseX<(framex+framexdim/2) && mouseY>(exporty) && mouseY<(exporty + frameydim)){
export();
}
//if spec1 is pressed
if(mouseX>(framex + 0*framexdim/3) && mouseX<(framex + framexdim/3) && mouseY>(specy) && mouseY<(specy + frameydim)){
if(clearRow != 5)
for(int i=0;i<4;i++)
for (int row = 0; row != GRID; ++row)
for (int col = 0; col != GRID; ++col){
rect[i].leds[count][row][col] = rect[clearRow].leds[count][row][col];
}
}
}
int rotateAmount = 0;
void drawBoxes(){
pushMatrix();
translate(DIM*7, DIM*9, 0);
rotateY(rotateAmount);
if(mousePressed && mouseY > DIM*4 && mouseX < (width - DIM*4) && mouseY < DIM*13.5)
rotateAmount = pmouseX / 100;
//rotateZ(pmouseY / 100);
fill(0);
text("Front",-DIM*0.3,DIM*3.2,DIM*4);
noFill();
box(DIM*6);
fill(0);
text("Front",DIM*4,DIM*13.5);
translate(DIM*3 - DIM * 8,DIM*3,DIM*3 - DIM * 6);
for(int k=0;k<4;k++){
for(int j=0;j<4;j++){
for(int i=0; i < 4; i++){
translate(DIM*2,0,0);
if(!rect[k].leds[count][i][j] == true)
fill(206);
else
fill(0,0,255);
box(DIM/2);
}
translate(0,0,DIM * 2);
translate(-DIM*8,0,0);
}
translate(0,-DIM*2,-DIM*8);
}
popMatrix();
}
void drawSmallBoxes(){
pushMatrix();
// for(int i=0;i<6;i++)
//rect(DIM/1.4 + DIM *3 * i,DIM*14,DIM *2.5,DIM * 2.5);
DIM = DIM / 4;
translate(0, DIM*61, 0);
noFill();
int q,w;
if(count < 4){
q = 0;
w=6;
}
else{
q = count-3;
w=count+3;
}
//for(int q = count; q < count + 6;q++){
for(q = q; q <w;q++){
pushMatrix();
//rotateX(PI/12 * q);
translate(DIM* 8 + DIM * 12 * q,0,0);
translate(-DIM*12*frameIncrease,0,0);
noFill();
box(DIM*6);
translate(DIM*3 - DIM * 8,DIM*3,DIM*3 - DIM * 6);
for(int k=0;k<4;k++){
for(int j=0;j<4;j++){
for(int i=0; i < 4; i++){
translate(DIM*2,0,0);
if(!rect[k].leds[q][i][j] == true)
fill(206);
else
fill(0,0,255);
box(DIM/2);
}
translate(0,0,DIM * 2);
translate(-DIM*8,0,0);
}
translate(0,-DIM*2,-DIM*8);
}
popMatrix();
}
popMatrix();
DIM = DIM * 4;
}
void drawButtons(){
fill(206);
rect(framex,framey,framexdim/3,frameydim);
rect(framex+framexdim/3,framey,framexdim/3,frameydim);
rect(framex+2*framexdim/3,framey,framexdim/3,frameydim);
fill(0);
text(count,width - DIM, height - DIM / 6);
fill(0,0,255);
rect(framex,filly,framexdim/2,frameydim);
fill(255,255,255);
rect(framex+framexdim/2,filly,framexdim/2,frameydim);
fill(160);
rect(framex,arrowy,framexdim/2,frameydim);
rect(framex+framexdim/2,arrowy,framexdim/2,frameydim);
fill(100);
rect(framex,savey,framexdim/2,frameydim);
rect(framex+framexdim/2,savey,framexdim/2,frameydim);
fill(60);
rect(framex,exporty,framexdim,frameydim);
fill(200);
rect(framex,specy,framexdim/3,frameydim);
rect(framex+framexdim/3,specy,framexdim/3,frameydim);
rect(framex+2*framexdim/3,specy,framexdim/3,frameydim);
}
void export(){
output = createWriter("cube.txt");
output.print("{");
for(int i=0;i<=maxCount;i++){
for(int k=0;k<4;k++){
int anum=0;
byte q=0;
for (int col = GRID -1 ; col != -1; --col){
// for (int col = 0 ; col != GRID; ++col){
// byte q = 0;
//anum="";//binary(0);
for (int row = 0; row != GRID; ++row)
{
//output.print(anum);
if(rect[k].leds[i][row][col] != false)
// output.print("1");
anum+=(pow(2,q));
q++;
}
}
output.print(anum);
output.print(",");
}
output.print(timing[i]+1);
if(i == maxCount){
}
else
output.print(",");
}
output.print("};");
output.flush();
}
void saver(File selection){
output = createWriter(selection);
for(int i=0;i<=maxCount;i++){
for(int k=0;k<4;k++){
// for (int row = 0; row != GRID; ++row){
for (int col = 0 ; col != GRID; ++col){
// for (int col = 0 ; col != GRID; ++col){
for (int row = 0; row != GRID; ++row){
if(rect[k].leds[i][row][col] != false)
output.print("1");
else
output.print("0");
}
}
}
if(timing[i] < 10)
output.print("0");
output.print(timing[i]);
}
output.flush();
}
void load(File selection){
byte b[] = loadBytes(selection);
int row=0;
int col=0;
boolean firstTime = true;
maxCount = int(b.length/64) - 1;
//if(maxCount > 5)
//count = maxCount - 5;
for(int i=0 ;i<=maxCount;i++){
// firstTime=true;
row=0;
col=0;
for(int z=0 +66*i;z < 64 + 66 *i;z++){
if ((z-i*2) % 16 ==0) firstTime = true;
if(b[z] - 48 == 1){
rect[(z-i*2) % 64 /16].leds[i][row][col] = true;
println("row:"+row+" col: "+col);
//rect[z % 64 /16].leds[i][3-col][row] = true;
}
else{
rect[(z-i*2) % 64 /16].leds[i][row][col] = false;
}
//rect[z % 64 /16].leds[i][3-col][row] = false;
//text(b[z],DIM,height/2);
//row = (row + 1) % 4;
//if(z % 4 == 0 && firstTime == false)
if(row % 4 ==3)
col = (col + 1) % 4;
//firstTime=false;
row = (row + 1) % 4;
//row = (row + 1) % 4;
//firstTime = false;
}
println("TEST" + i);
timing[i] = (b[64 + 66 * i] - 48) * 10 + (b[65+66*i]-48);
}
}
/*void load(File selection){
byte b[] = loadBytes(selection);
int row=0;
int col=0;
boolean firstTime = true;
maxCount = int(b.length/64) - 1;
//if(maxCount > 5)
//count = maxCount - 5;
for(int i=0 ;i<=maxCount;i++){
// firstTime=true;
row=0;
col=0;
for(int z=0 +64*i;z < 64 + 64 *i;z++){
if (z % 16 ==0) firstTime = true;
if(b[z] - 48 == 1){
rect[z % 64 /16].leds[i][row][col] = true;
println("row:"+row+" col: "+col);
//rect[z % 64 /16].leds[i][3-col][row] = true;
}
else{
rect[z % 64 /16].leds[i][row][col] = false;
}
//rect[z % 64 /16].leds[i][3-col][row] = false;
//text(b[z],DIM,height/2);
//row = (row + 1) % 4;
//if(z % 4 == 0 && firstTime == false)
if(row % 4 ==3)
col = (col + 1) % 4;
//firstTime=false;
row = (row + 1) % 4;
//row = (row + 1) % 4;
//firstTime = false;
}
println("TEST" + i);
timing[0]=0;
}
}*/