-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPinceles.pde
266 lines (234 loc) · 7.01 KB
/
Pinceles.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
// Nuevos pinceles se pueden probar en el editor online de p5.js usando este bosquejo de ejemplo:
// https://editor.p5js.org/codeanticode/sketches/EEURt9RtI
void cargarPinceles() {
pinceles = new ArrayList<Pincel>();
pinceles.add(new PincelAndiamo(0, new char[]{'Q', 'q'}));
pinceles.add(new PincelLinea(1, new char[]{'W', 'w'}));
pinceles.add(new PincelCinta(2, new char[]{'E', 'e'}));
pinceles.add(new PincelChispa(3, new char[]{'R', 'r'}));
pinceles.add(new PincelCirculo(4, new char[]{'T', 't'}));
pinceles.add(new PincelYellowTail(5, new char[]{'Y', 'y'}));
pinceles.add(new PincelCuadrados(6, new char[]{'U', 'u'}));
pinceles.add(new PincelAbanico(7, new char[]{'I', 'i'}));
pinceles.add(new PincelRectangulos(8, new char[]{'O', 'o'}));
}
void agregarPincelesAlInterface(float left) {
for (int i = 1; i <= 8; i++) {
Widget w = new SelectButton(intf, left + (i-1) * 25, 5, 20, 20, "pincel" + i, "seleccionarPincel", "P" + i);
intf.addWidget(w, intf.getWidget("container"));
((SelectButton)w).selected = i == 1;
}
}
void actualizarInterfacePinceles(int sel) {
for (int i = 1; i <= 8; i++) {
SelectButton button = (SelectButton)intf.getWidget("pincel" + i);
button.selected = sel == i - 1;
}
}
void seleccionarPincel(String nombre) {
for (int i = 1; i <= 8; i++) {
if (nombre.equals("pincel" + i)) {
int sel = i - 1;
estado.seleccionarPincel(sel);
for (int j = 1; j <= 8; j++) {
SelectButton w = (SelectButton)intf.getWidget("pincel" + j);
if (j - 1 != sel) {
w.selected = false;
}
}
}
}
}
boolean distintos(Toque ptoque, Toque toque) {
if (ptoque == null) return false;
return ptoque.x != toque.x || ptoque.y != toque.y;
}
abstract class Pincel {
String nombre;
char[] teclas;
int indice;
boolean animarOpacidad;
Pincel(int indice, char[] teclas) {
this.indice = indice;
this.teclas = teclas;
this.animarOpacidad = true;
}
abstract Pincel nuevoPincel();
abstract void pintar(Toque[] toques, color tinta, float escala);
}
class PincelLinea extends Pincel {
PincelLinea(int indice, char[] teclas) {
super(indice, teclas);
}
Pincel nuevoPincel() {
return new PincelLinea(indice, teclas);
}
void pintar(Toque[] toques, color tinta, float escala) {
stroke(tinta);
strokeWeight(escala);
Toque ptoque = null;
for (Toque toque: toques) {
if (distintos(ptoque, toque) && !toque.primero) {
line(ptoque.x, ptoque.y, toque.x, toque.y);
}
ptoque = toque;
}
}
}
class PincelCinta extends Pincel {
PincelCinta(int indice, char[] teclas) {
super(indice, teclas);
}
Pincel nuevoPincel() {
return new PincelCinta(indice, teclas);
}
void pintar(Toque[] toques, color tinta, float escala) {
noStroke();
fill(tinta);
float w = 0;
Toque ptoque = null;
for (Toque toque: toques) {
if (toque.primero) {
if (ptoque != null) endShape();
beginShape(QUAD_STRIP);
w = 0;
} else if (distintos(ptoque, toque)) {
float dx = toque.x - ptoque.x;
float dy = toque.y - ptoque.y;
float d2 = sqrt(sq(dx) + sq(dy));
float nx = 0;
float ny = 0;
if (!toque.ultimo) {
nx = dy / d2;
ny = -dx / d2;
}
w = 0.9 * w + 0.1 * toque.p;
vertex(toque.x + nx * w * escala, toque.y + ny * w * escala);
vertex(toque.x - nx * w * escala, toque.y - ny * w * escala);
}
ptoque = toque;
}
endShape();
}
}
class PincelChispa extends Pincel {
PincelChispa(int indice, char[] teclas) {
super(indice, teclas);
}
Pincel nuevoPincel() {
return new PincelChispa(indice, teclas);
}
void pintar(Toque[] toques, color tinta, float escala) {
if (1 < toques.length) {
strokeCap(ROUND);
Toque toque = toques[toques.length - 1];
Toque ptoque = toques[toques.length - 2];
float r = 2 * toque.p * escala;
stroke(tinta);
noFill();
strokeWeight(r);
line(ptoque.x, ptoque.y, toque.x, toque.y);
} else if (toques.length == 1) {
noStroke();
fill(tinta);
Toque toque = toques[toques.length - 1];
float r = 5 * escala;
ellipse(toque.x, toque.y, r, r);
}
}
}
class PincelCirculo extends Pincel {
float offset;
PincelCirculo(int indice, char[] teclas) {
super(indice, teclas);
offset = random(10);
}
Pincel nuevoPincel() {
return new PincelCirculo(indice, teclas);
}
void pintar(Toque[] toques, color tinta, float escala) {
if (0 < toques.length) {
noStroke();
fill(tinta);
Toque toque = toques[toques.length - 1];
float r = 20 * escala * noise(offset + millis() / 2500.0);
ellipse(toque.x, toque.y, r, r);
}
}
}
class PincelCuadrados extends Pincel {
float offset;
PincelCuadrados(int indice, char[] teclas) {
super(indice, teclas);
offset = random(10);
}
Pincel nuevoPincel() {
return new PincelCuadrados(indice, teclas);
}
void pintar(Toque[] toques, color tinta, float escala) {
if (0 < toques.length) {
rectMode(CENTER);
noStroke();
fill(tinta);
Toque toque = toques[toques.length - 1];
float r = 20 * escala * noise(offset + millis() / 2500.0);
rect(toque.x, toque.y, r, r);
}
}
}
class PincelAbanico extends Pincel {
PincelAbanico(int indice, char[] teclas) {
super(indice, teclas);
}
Pincel nuevoPincel() {
return new PincelAbanico(indice, teclas);
}
void pintar(Toque[] toques, color tinta, float escala) {
stroke(tinta);
strokeWeight(escala);
Toque ptoque = null;
float xc = 0, yc = 0;
for (Toque toque: toques) {
if (toque.primero) {
xc = toque.x;
yc = toque.y;
}
if (distintos(ptoque, toque) && !toque.primero) {
line(xc, yc, toque.x, toque.y);
}
ptoque = toque;
}
}
}
class PincelRectangulos extends Pincel {
PincelRectangulos(int indice, char[] teclas) {
super(indice, teclas);
}
Pincel nuevoPincel() {
return new PincelRectangulos(indice, teclas);
}
void pintar(Toque[] toques, color tinta, float escala) {
fill(tinta);
noStroke();
Toque toque0 = null;
for (int i = 0; i < toques.length; i++) {
Toque toque = toques[i];
if (toque.primero) {
toque0 = toque;
}
if (toque.ultimo) {
float dx = toque.x - toque0.x;
float dy = toque.y - toque0.y;
float d2 = sqrt(sq(dx) + sq(dy));
float nx = dy / d2;
float ny = -dx / d2;
beginShape(QUAD);
vertex(toque0.x + nx * escala, toque0.y + ny * escala);
vertex(toque.x + nx * escala, toque.y + ny * escala);
vertex(toque.x - nx * escala, toque.y - ny * escala);
vertex(toque0.x - nx * escala, toque0.y - ny * escala);
endShape();
}
}
}
}