-
Notifications
You must be signed in to change notification settings - Fork 12
/
Frame.pde
215 lines (188 loc) · 5.67 KB
/
Frame.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
class Frame {
//static
PGraphics pg = null;
int letter_scale = 3;
PixelColor[] pixs = null;
PGraphics frame = null;
PGraphics thumb = null;
public int rows = 0;
public int cols = 0;
int last_y = 0;
int last_x = 0;
Frame(int cols, int rows) {
this.cols = cols;
this.rows = rows;
this.pixs = new PixelColor[rows*cols];
this.clear();
size(780, 720, P2D);
this.pg = createGraphics(8*letter_scale, 10*letter_scale, P2D);
}
public PGraphics draw_full(int draw_rad, int draw_border) {
if(this.frame == null) this.frame = draw_canvas(draw_rad, draw_border);
return this.frame;
}
public PGraphics draw_thumb(int draw_rad, int draw_border) {
if(this.thumb == null) this.thumb = draw_canvas(draw_rad, draw_border);
return this.thumb;
}
public PGraphics draw_canvas(int draw_rad, int draw_border) {
PGraphics canvas = createGraphics(this.cols * draw_rad, this.rows * draw_rad, P2D);
canvas.beginDraw();
canvas.background(55);
canvas.smooth();
canvas.noStroke();
canvas.rectMode(CENTER);
for(int y = 0; y < this.rows; y++) {
for(int x = 0; x < this.cols; x++) {
canvas.fill(this.get_pixel(x,y).get_color());
if(device.draw_as_circle()) {
canvas.ellipse( draw_rad * (x + 0.5), draw_rad * (y + 0.5), draw_rad-border, draw_rad-border);
}
else {
canvas.rect( draw_rad * (x + 0.5), draw_rad * (y + 0.5), draw_rad-border, draw_rad-border);
}
}
}
// println( "drawn" );
canvas.endDraw();
return canvas;
}
public void clear() {
this.set_pixels(new PixelColor());
}
public void fill(PixelColor pc) {
this.set_pixels(pc);
}
public void set_pixels(PixelColor pc) {
for( int y = 0; y < this.rows; y++ ) {
for( int x = 0; x < this.cols; x++ ) {
set_pixel(x, y, pc);
}
}
}
public void set_pixels(PixelColor[] pix) {
for( int y = 0; y < this.rows; y++ ) {
for( int x = 0; x < this.cols; x++ ) {
set_pixel(x, y, pix[pos(x,y)]);
}
}
}
public PixelColor get_pixel(int x, int y) {
if(x < 0 || y < 0 || x > width || y > height ) return null;
return pixs[pos(x,y)];
}
public PixelColor set_row(int y, PixelColor pc) {
for( int x = 0; x < this.cols; x++ ) {
pc = set_colored_pixel(x, y, pc);
}
return pc;
}
public PixelColor set_col(int x, PixelColor pc) {
for( int y = 0; y < this.rows; y++ ) {
pc = set_colored_pixel(x, y, pc);
}
return pc;
}
public PixelColor set_colored_pixel(int x, int y, PixelColor pc) {
if( x >= cols ) return set_row( y, pc);
if( y >= rows) return set_col( x, pc);
if( this.get_pixel(x,y).equal(pc) ) {
this.frame = null;
this.thumb = null;
return this.get_pixel(x,y).next_color();
}
return set_pixel(x, y, pc);
}
public PixelColor set_pixel(int x, int y, PixelColor pc) {
if( pc == null ) pc = new PixelColor();
if(this.get_pixel(x,y) != null ) {
this.get_pixel(x,y).set_color(pc);
}
else {
pixs[pos(x,y)] = pc.clone();
}
this.frame = null;
this.thumb = null;
return this.get_pixel(x,y).clone();
}
public PixelColor update(int x, int y, PixelColor pc, boolean ignore_last) {
if(!ignore_last && x == last_x && y == last_y) return null;
last_x = x;
last_y = y;
if(color_mode) return set_pixel(x, y, pc);
return set_colored_pixel(x, y, pc);
}
public Frame clone() {
Frame f = new Frame(this.cols, this.rows);
f.set_pixels(pixs);
return f;
}
public void shift_left() {
for( int y = 0; y < this.rows; y++ ) {
for( int x = 0; x < this.cols; x++ ) {
set_pixel(x, y, ( x < this.cols - 1) ? get_pixel(x+1,y) : null );
}
}
}
public void shift_right() {
for( int y = 0; y < this.rows; y++ ) {
for( int x = this.cols - 1; x >= 0; x-- ) {
set_pixel(x, y, ( x > 0) ? get_pixel(x-1,y) : null );
}
}
}
public void shift_up() {
for( int y = 0; y < this.rows; y++ ) {
for( int x = 0; x < this.cols; x++ ) {
set_pixel(x, y, ( y < this.rows - 1) ? get_pixel(x,y+1) : null );
}
}
}
public void shift_down() {
for( int y = this.rows - 1; y >= 0; y-- ) {
for( int x = 0; x < this.cols; x++ ) {
set_pixel(x, y, ( y > 0) ? get_pixel(x,y-1) : null );
}
}
}
private int pos(int x, int y ) {
return (y * this.cols) + x;
}
private PixelColor set_letter(char letter, PFont font, PixelColor pc) {
return set_letter(letter, font, pc, 50);
}
private PixelColor set_letter(char letter, PFont font, PixelColor pc, int trashhold) {
PixelColor new_pc = null;
int offset = 0;
this.pg.beginDraw();
this.pg.fill(#FF0000); //red
this.pg.background(0);
this.pg.textFont(font,10*letter_scale);
this.pg.text(letter,0,8*letter_scale);
this.pg.endDraw();
this.pg.loadPixels();
for(int row = 0; row < 10; row++) {
int sum = 0;
for(int col = 0; col < 8; col++) {
if( this.get_pixs(row, col) > trashhold) {
new_pc = this.set_colored_pixel(col, row-offset, pc);
sum++;
}
}
if(sum == 0 && offset < 2) offset++;
if(row-offset == 7) return new_pc; //exit earlier in case we dont have lower parts
}
return new_pc;
}
private int get_pixs(int x, int y) {
int trashhold = 0;
x *= this.letter_scale;
y *= this.letter_scale;
for(int k = 0; k < this.letter_scale; k++) {
for(int k2 = 0; k2 < this.letter_scale; k2++) {
trashhold += red(this.pg.pixels[(x+k)*8*this.letter_scale+y+k2]);
}
}
return trashhold / (this.letter_scale * this.letter_scale);
}
}