-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridController.pde
134 lines (100 loc) · 2.9 KB
/
GridController.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
public class Marker {
int x, y;
String text;
boolean isSet = false;
public Marker(int x, int y, String text) {
this.x = x;
this.y = y;
this.text = text;
isSet = true;
}
public Marker() {}
}
public class GridController {
int nCellsX;
int nCellsY;
float xspacing;
float yspacing;
color gridColour;
PGraphics gridBuffer;
boolean isEnabled = true;
Marker[] markers = new Marker[4];
public GridController (PGraphics gridBuffer, int nCellsX, int nCellsY, color gridColour) {
this.gridBuffer = gridBuffer;
this.nCellsX = nCellsX;
this.nCellsY = nCellsY;
this.gridColour = gridColour;
int gWidth = gridBuffer.width;
int gHeight = gridBuffer.height;
xspacing = Math.max(10, (float)gWidth/nCellsX);
yspacing = Math.max(10, (float)gHeight/nCellsY);
for (int i=0; i<markers.length; i++)
markers[i] = new Marker();
draw();
}
public void enabled(boolean isItEnabled) {
this.isEnabled = isItEnabled;
}
public void toggle() {
this.isEnabled = !this.isEnabled;
draw();
}
public GridController draw() {
if (!isEnabled) {
gridBuffer.beginDraw();
gridBuffer.background(255,0);
gridBuffer.endDraw();
return this;
}
int gWidth = gridBuffer.width;
int gHeight = gridBuffer.height;
gridBuffer.beginDraw();
gridBuffer.background(255,0);
gridBuffer.stroke(gridColour);
for (int i = 0, j=0; i < gWidth; i+=xspacing, j++) {
gridBuffer.line (i, 0, i, gHeight);
if (j%4 == 0) gridBuffer.text(j, i, 0+10); //FIXME: this %4 is very arbitrary
}
for (int i = 0, j=0; i < gHeight; i+=yspacing, j++) {
gridBuffer.line (0, i, gWidth, i);
gridBuffer.text(nCellsY+1-j, 0, i); //BE stupid hack +1!
}
gridBuffer.endDraw();
for (Marker marker : markers) {
if (marker.isSet)
mark(marker, gridBuffer);
}
return this;
}
public GridController mark(int index, int x, int y) {
if (index >= markers.length)
return this;
markers[index] = new Marker(x,y,""+index);
//mark(markers[index], gridBuffer);
draw();
return this;
}
public GridController mark(Marker marker, PGraphics buffer) {
buffer.beginDraw();
buffer.noFill();
buffer.stroke(255);
buffer.ellipse(marker.x, marker.y, 15, 15);
buffer.text(marker.text,marker.x-4,marker.y+5);
buffer.endDraw();
return this;
}
public GridController markCell(int x, int y, PGraphics buffer) {
int bHeight = buffer.height;
int ymax = (int)((float)bHeight/yspacing);
int yy = ymax - y + 1;
int xx = x-1;
buffer.beginDraw();
buffer.noFill();
buffer.stroke(255);
buffer.ellipse(xx*xspacing, yy*yspacing, 10, 10);
println(xx*xspacing, yy*yspacing, 10, 10);
//buffer.line(xx*xspacing, yy*yspacing, xx*xspacing+5, yy*yspacing);
buffer.endDraw();
return this;
}
}