-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEtcch_A_Sketch.pde
367 lines (329 loc) · 9.5 KB
/
Etcch_A_Sketch.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
int frameWidth = 110;
float plotterX = 350;
float plotterY = 350;
int windowX = 0, windowY = 0;
int windowXPrevFrame = 0, windowYPrevFrame = 0;
int movingWindowFrameCount = 0;
int staticWindowFrameCount = 0;
int staticWindowFrameCountOld = 0;
int numOfWindowShakes = 0;
int tempX = 0, tempY = 0;
float plotterSpeed = 0;
float knobSpeed = 0.0;
float leftKnobRotation = 0.1;
float rightKnobRotation = 0.1;
float randomX, randomY;
boolean isUpPressed = false;
boolean isDownPressed = false;
boolean isRightPressed = false;
boolean isLeftPressed = false;
boolean isShiftPressed = false;
boolean isCtrlPressed = false;
boolean isCPressed = false;
boolean isRPressed = false;
float plotterSpeedIncrement = 3;
float knobSpeedIncrement = 0.03;
float plotterSpeedDecrement = -0.7;
float knobSpeedDecrement = -0.007;
PFont font;
void setup() {
// The red background/frame
pushMatrix();
size(850, 700);
noStroke();
fill(207, 0, 0);
rect(0, 0, 850, 700);
popMatrix();
// Rectangle 1 for white border
pushMatrix();
translate(0, 0);
stroke(240);
strokeWeight(25);
fill(255);
rect(110, 110, 630, 480, 25);
popMatrix();
// Rectangle 2 for black border
pushMatrix();
translate(0, 0);
stroke(10);
strokeWeight(17);
fill(255);
rect(110, 110, 630, 480, 25);
popMatrix();
// The main canvas with gray border
pushMatrix();
translate(0, 0);
stroke(150, 150, 150);
strokeWeight(7);
fill(204, 204, 204);
rect(110, 110, 630, 480, 25);
popMatrix();
// Left knob shadow
pushMatrix();
stroke(120);
strokeWeight(10);
fill(240);
ellipse(70, 640, 80, 80);
popMatrix();
// Right knob shadow
pushMatrix();
stroke(120);
strokeWeight(10);
fill(240);
ellipse(780, 640, 80, 80);
popMatrix();
// Text with shadow
font = loadFont("MaturaMTScriptCapitals-48.vlw");
textFont(font);
textSize(48);
fill(70);
text("Etcch A Sketch", 264, 664);
fill(222, 184, 132);
text("Etcch A Sketch", 260, 660);
}
void draw() {
// Get coordinates of the main window
windowX = getWindowCoordinatesX();
windowY = getWindowCoordinatesY();
//print("x: " + windowX + " y: " + windowY + "\n");
//print(frameCount);
//println(movingWindowFrameCount + " " + staticWindowFrameCount + " " + numOfWindowShakes);
// Check if the window is being shaken
// Window is shaken if previous coordinates are different than current coordinates
if (windowX != windowXPrevFrame || windowY != windowYPrevFrame) {
movingWindowFrameCount++;
staticWindowFrameCount = 0;
// If window is moved for some frames, then increase number of shakes
if (movingWindowFrameCount > 25) {
numOfWindowShakes++;
// Clear the screen if the window is shaken properly
// This also makes sure the screen isn't erased
// when the window is slightly moved
if (numOfWindowShakes > 10) {
stroke(150, 150, 150);
strokeWeight(7);
fill(204, 204, 204);
rect(110, 110, 630, 480, 25);
}
}
}
// If previous and current coordinates are same, window is not being shaken
else {
movingWindowFrameCount = 0;
staticWindowFrameCount++;
// If window remains static for a while, then number of shakes is zero
if (staticWindowFrameCount > 50) {
numOfWindowShakes = 0;
}
}
windowXPrevFrame = windowX;
windowYPrevFrame = windowY;
// Left knob
pushMatrix();
stroke(24);
strokeWeight(3);
fill(240);
ellipse(70, 640, 80, 80);
popMatrix();
// Right knob
pushMatrix();
stroke(24);
strokeWeight(3);
fill(240);
ellipse(780, 640, 80, 80);
popMatrix();
// Line on left knob
pushMatrix();
stroke(24);
strokeWeight(3);
fill(2);
translate(70, 640);
rotate(leftKnobRotation);
line(0, -40, 0, 0);
line(0, 0, 0, 40);
popMatrix();
// Line on right knob
pushMatrix();
stroke(24);
strokeWeight(3);
fill(2);
translate(780, 640);
rotate(rightKnobRotation);
line(0, -40, 0, 0);
line(0, 0, 0, 40);
popMatrix();
// The plotter/brush/cursor
if (isCPressed) {
// Clear the screen
stroke(150, 150, 150);
strokeWeight(7);
fill(204, 204, 204);
rect(110, 110, 630, 480, 25);
} else {
translate(plotterX, plotterY);
stroke(30, 30, 30);
strokeWeight(2);
point(0, 0);
}
// Handle movement of the plotter/brush
// and rotation of knobs
if (isUpPressed && plotterY > frameWidth+10) {
plotterY = plotterY - (1 + plotterSpeed);
rightKnobRotation = rightKnobRotation + (0.01 + knobSpeed);
}
if (isDownPressed && plotterY < height-frameWidth-10) {
plotterY = plotterY + (1 + plotterSpeed);
rightKnobRotation = rightKnobRotation - (0.01 + knobSpeed);
}
if (isLeftPressed && plotterX > frameWidth+10) {
plotterX = plotterX - (1 + plotterSpeed);
leftKnobRotation = leftKnobRotation - (0.01 + knobSpeed);
}
if (isRightPressed && plotterX < width-frameWidth-10) {
plotterX = plotterX + (1 + plotterSpeed);
leftKnobRotation = leftKnobRotation + (0.01 + knobSpeed);
}
// experimental:
if (isRPressed) {
if (plotterY > frameWidth+10 && plotterY < height-frameWidth-10) {
randomY = random(-10, 10);
} else if (plotterY <= frameWidth+10) {
randomY = random(0, 10);
} else if (plotterY >= height-frameWidth-10) {
randomY = random(-10, 0);
}
if (plotterX > frameWidth+10 && plotterX < width-frameWidth-10) {
randomX = random(-10, 10);
} else if (plotterX <= frameWidth+10) {
randomX = random(0, 10);
} else if (plotterX >= width-frameWidth-10) {
randomX = random(-10, 0);
}
plotterX = plotterX + (randomX + plotterSpeed);
plotterY = plotterY + (randomY + plotterSpeed);
leftKnobRotation = leftKnobRotation + (0.01 + knobSpeed);
rightKnobRotation = rightKnobRotation - (0.01 + knobSpeed);
}
println(plotterX + " " + plotterY);
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP && isUpPressed == false) {
isUpPressed = true;
}
if (keyCode == DOWN && isDownPressed == false) {
isDownPressed = true;
}
if (keyCode == LEFT && isLeftPressed == false) {
isLeftPressed = true;
}
if (keyCode == RIGHT && isRightPressed == false) {
isRightPressed = true;
}
if (keyCode == SHIFT && isShiftPressed == false) {
isShiftPressed = true;
plotterSpeed = plotterSpeedIncrement;
knobSpeed = knobSpeedIncrement;
}
if (keyCode == CONTROL && isCtrlPressed == false) {
isCtrlPressed = true;
plotterSpeed = plotterSpeedDecrement;
knobSpeed = knobSpeedDecrement;
}
} else {
if (key == 'w' && isUpPressed == false) {
isUpPressed = true;
}
if (key == 's' && isDownPressed == false) {
isDownPressed = true;
}
if (key == 'a' && isLeftPressed == false) {
isLeftPressed = true;
}
if (key == 'd' && isRightPressed == false) {
isRightPressed = true;
}
if (key == 'r' && isRPressed == false) {
isRPressed = true;
}
else if (key == 'c' && isCPressed == false) {
isCPressed = true;
}
}
}
void keyReleased() {
if (key == CODED) {
if (keyCode == UP) {
isUpPressed = false;
}
if (keyCode == DOWN) {
isDownPressed = false;
}
if (keyCode == LEFT) {
isLeftPressed = false;
}
if (keyCode == RIGHT) {
isRightPressed = false;
}
if (keyCode == SHIFT) {
isShiftPressed = false;
plotterSpeed = 0;
knobSpeed = 0.0;
}
if (keyCode == CONTROL) {
isCtrlPressed = false;
plotterSpeed = 0;
knobSpeed = 0.0;
}
} else {
if (key == 'w') {
isUpPressed = false;
}
if (key == 's') {
isDownPressed = false;
}
if (key == 'a') {
isLeftPressed = false;
}
if (key == 'd') {
isRightPressed = false;
}
if (key == 'r') {
isRPressed = false;
}
// Save screenshot
else if (key == 't') {
saveFrame("./screenshots/screenshot-####.jpg");
javax.swing.JOptionPane.showMessageDialog(null, "Screenshot saved");
}
else if (key == 'c') {
isCPressed = false;
}
else if (key == 'i') {
javax.swing.JOptionPane.showMessageDialog(null, "Use arrow keys to move\nHold down shift/ctrl to increase/decrease speed\nT - screenshot\nC (or shake the window) - Clear screen");
}
}
}
// Find x-coordinate of the main window
int getWindowCoordinatesX(){
int locX = 0;
// Check if renderer is JAVA2D
if(surface instanceof processing.awt.PSurfaceAWT){
java.awt.Frame frame = ( (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative()).getFrame();
java.awt.Point point = frame.getLocationOnScreen();
locX = (int)point.getX();
//print("java2d");
}
return locX;
}
// FindplotterY-coordinate of the main window
int getWindowCoordinatesY(){
int locY = 0;
// Check if renderer is JAVA2D
if(surface instanceof processing.awt.PSurfaceAWT){
java.awt.Frame frame = ( (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative()).getFrame();
java.awt.Point point = frame.getLocationOnScreen();
locY = (int)point.getY();
}
return locY;
}