This repository was archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegoStacks.pde
More file actions
312 lines (250 loc) · 7.09 KB
/
LegoStacks.pde
File metadata and controls
312 lines (250 loc) · 7.09 KB
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
import fullscreen.*;
import hypermedia.video.*;
import java.awt.*;
import java.util.List;
import controlP5.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
FullScreen fs;
OpenCV opencv;
ControlP5 controlP5;
// Set capture resolution.
int w = 320;
int h = 240;
int maxTime = 60000;
int cameraIndex = 8;
boolean debugMode = false;
int displayMode = 0;
final int DISPLAY_MENU = 0;
final int DISPLAY_REGISTER = 1;
final int DISPLAY_PLAY = 2;
final int DISPLAY_CALIBRATE = 3;
final int DISPLAY_RESULT = 4;
Participant participant;
PFont font;
LegoCollection collection;
controlP5.Textlabel titleLabel, timerLabel, resultTitleLabel, resultTimeLabel;
controlP5.Button playButton, calibrateButton;
controlP5.Button calibrateSaveButton, calibrateResetButton, calibrateCancelButton;
controlP5.Textfield nameField;
controlP5.Button startPlayButton, cancelPlayButton, newPlayButton, menuButton;
controlP5.ControlTimer timer;
PImage backgroundImage;
boolean ignoreClickHack = false;
void setup() {
size(w, h);
backgroundImage = loadImage("background.jpg");
// Create the fullscreen object
fs = new FullScreen(this);
fs.setResolution(w*2, h*2);
// enter fullscreen mode
fs.enter();
font = loadFont("AndaleMono-48.vlw");
textFont(font, 32);
// Create the ControlP5 object for the user interface.
controlP5 = new ControlP5(this);
// controlP5.setColorForeground(0xffaa0000);
// controlP5.setColorBackground(0xff660000);
// controlP5.setColorLabel(0xffdddddd);
// controlP5.setColorValue(0xffff88ff);
// controlP5.setColorActive(0xffff0000);
titleLabel = controlP5.addTextlabel("titleLabel", "Lego Bootstrap", 10, 10);
playButton = controlP5.addButton("playButton", 0.0, 135, 80, 50, 20);
playButton.setCaptionLabel("Play");
calibrateButton = controlP5.addButton("calibrateButton", 0.0, 135, 110, 50, 20);
calibrateButton.setCaptionLabel("Calibrate");
hideMenu();
calibrateSaveButton = controlP5.addButton("calibrateSaveButton");
calibrateSaveButton.setCaptionLabel("Save");
calibrateResetButton = controlP5.addButton("calibrateResetButton");
calibrateResetButton.setCaptionLabel("Reset");
calibrateCancelButton = controlP5.addButton("calibrateCancelButton");
calibrateCancelButton.setCaptionLabel("Cancel");
hideCalibrate();
nameField = controlP5.addTextfield("nameField", 60, 60, 200, 20);
nameField.setCaptionLabel("Your drupal.org username");
nameField.setAutoClear(false);
startPlayButton = controlP5.addButton("startPlayButton", 0.0, 60, 150, 50, 20);
startPlayButton.setCaptionLabel("Play Now");
cancelPlayButton = controlP5.addButton("cancelPlayButton", 0.0, 120, 150, 50, 20);
cancelPlayButton.setCaptionLabel("Cancel");
hideRegister();
timerLabel = controlP5.addTextlabel("timerLabel", "", 10, 30);
hidePlay();
resultTitleLabel = controlP5.addTextlabel("resultTitleLabel", "Congratulations", 110, 80);
resultTimeLabel = controlP5.addTextlabel("resultTimeLabel", "Your time", 135, 100);
newPlayButton = controlP5.addButton("newPlayButton", 0.0, 135, 130, 50, 20);
newPlayButton.setCaptionLabel("New Game");
menuButton = controlP5.addButton("menuButton", 0.0, 135, 160, 50, 20);
menuButton.setCaptionLabel("Main Menu");
hideResult();
timer = new controlP5.ControlTimer();
opencv = new OpenCV(this);
opencv.capture(w, h, cameraIndex);
participant = new Participant();
collection = new LegoCollection();
collection.load();
showMenu();
}
void draw() {
background(0);
controlP5.draw();
opencv.read();
PImage original = opencv.image();
switch(displayMode) {
case DISPLAY_MENU:
case DISPLAY_REGISTER:
case DISPLAY_RESULT:
image(backgroundImage, 0, 0, width, height);
break;
case DISPLAY_CALIBRATE:
image(original, 0, 0, width, height);
collection.draw();
break;
case DISPLAY_PLAY:
image(original, 0, 0, width, height);
timerLabel.setValue(timer.toString());
collection.draw();
if (timer.time() >= maxTime) {
gameOver();
}
break;
}
}
void gameOver() {
participant.time = timer.time();
participant.saveScore();
displayMode = DISPLAY_RESULT;
hidePlay();
showResult();
if (participant.time < maxTime) {
resultTitleLabel.setValue("Congratulations " + participant.name + "!\nSee your ranking at lego.wunderkraut.com");
resultTimeLabel.setValue(timer.toString());
}
else {
resultTitleLabel.setValue("Sorry " + participant.name + "...");
resultTimeLabel.setValue("Your time is over.");
}
}
void mouseClicked() {
if (displayMode == DISPLAY_CALIBRATE) {
if (ignoreClickHack) {
ignoreClickHack = false;
}
else {
collection.pick(mouseX, mouseY);
}
}
}
void keyPressed() {
// Switch debug mode on/off.
if (key == 'd') {
debugMode = !debugMode;
}
}
/**************************
* Widget event handlers. *
**************************/
void calibrateButton(float theValue) {
displayMode = DISPLAY_CALIBRATE;
hideMenu();
showCalibrate();
ignoreClickHack = true;
}
void calibrateSaveButton(float theValue) {
collection.save();
displayMode = DISPLAY_MENU;
hideCalibrate();
showMenu();
}
void calibrateResetButton(float theValue) {
ignoreClickHack = true;
collection.clear();
}
void calibrateCancelButton(float theValue) {
displayMode = DISPLAY_MENU;
hideCalibrate();
showMenu();
}
void playButton(float theValue) {
hideMenu();
showRegister();
nameField.setFocus(true);
}
void startPlayButton(float theValue) {
participant.name = nameField.getText();
displayMode = DISPLAY_PLAY;
hideRegister();
showPlay();
timer.reset();
}
void cancelPlayButton(float theValue) {
displayMode = DISPLAY_MENU;
hideRegister();
showMenu();
}
void newPlayButton(float theValue) {
displayMode = DISPLAY_REGISTER;
hideResult();
showRegister();
}
void menuButton(float theValue) {
displayMode = DISPLAY_MENU;
hideResult();
showMenu();
}
/************************************************************
* Helper functions to hide/show different sets of widgets. *
************************************************************/
void hideMenu() {
// titleLabel.hide();
playButton.hide();
calibrateButton.hide();
}
void showMenu() {
// titleLabel.show();
if (collection.size() > 0) {
playButton.show();
}
calibrateButton.show();
}
void hideCalibrate() {
calibrateSaveButton.hide();
calibrateResetButton.hide();
calibrateCancelButton.hide();
}
void showCalibrate() {
calibrateSaveButton.show();
calibrateResetButton.show();
calibrateCancelButton.show();
}
void hideRegister() {
nameField.hide();
startPlayButton.hide();
cancelPlayButton.hide();
}
void showRegister() {
nameField.show();
startPlayButton.show();
cancelPlayButton.show();
}
void hidePlay() {
timerLabel.hide();
}
void showPlay() {
timerLabel.show();
}
void hideResult() {
menuButton.hide();
newPlayButton.hide();
resultTitleLabel.hide();
resultTimeLabel.hide();
}
void showResult() {
menuButton.show();
newPlayButton.show();
resultTitleLabel.show();
resultTimeLabel.show();
}