-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainController.pde
More file actions
259 lines (234 loc) · 6.74 KB
/
MainController.pde
File metadata and controls
259 lines (234 loc) · 6.74 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
class MainController {
private MainView mainView = new MainView();
private ButtonCollection buttonCollection;
int lockedIndex = -1;
boolean firstPress = true;
TextBox textBoxSelected = null;
boolean bcBool = false;
double goal = 400, pastM;
public MainController(ButtonCollection bc) {
buttonCollection = bc;
}
void drawButtons() {
mainView.drawButtons(getButtonPos(), getButtonLabel(), getIsSmart());
}
void drawCanvas() {
mainView.drawCanvas();
}
public ArrayList<Boolean> getIsSmart() {
ArrayList<Button> buttonArray = buttonCollection.getCollection();
ArrayList<Boolean> isSmart = new ArrayList<Boolean>();
for (int i = 0; i<buttonArray.size(); i++) {
if (buttonArray.get(i).isSmart) {
isSmart.add(true);
Button btemp = buttonArray.get(i);
TextBox Temp = btemp.tb;
Temp.drawSmartTextBox();
} else {
isSmart.add(false);
Button btemp = buttonArray.get(i);
TextBox Temp = btemp.tb;
Temp.drawTextBox();
}
}
return isSmart;
}
public ArrayList<Integer> getButtonPos() {
ArrayList<Button> buttonArray = buttonCollection.getCollection();
ArrayList<Integer> values = new ArrayList<Integer>();
for (int i = 0; i<buttonArray.size(); i++) {
values.add(buttonArray.get(i).posX);
values.add(buttonArray.get(i).posY);
}
return values;
}
public ArrayList<String> getButtonLabel() {
ArrayList<Button> buttonArray = buttonCollection.getCollection();
ArrayList<String> values = new ArrayList<String>();
for (int i = 0; i<buttonArray.size(); i++) {
values.add(buttonArray.get(i).val);
}
return values;
}
void mouseReleasedController() {
firstPress = true;
if (lockedIndex >= 0)
{
Button b = buttonCollection.getButton(lockedIndex);
if (canvas.overCanvas())
{
canvas.snapToCanvas(buttonCollection, b);
} else
{
buttonCollection.removeButton(b);
}
b.tb.update(b.posX, b.posY);
}
lockedIndex = -1;
}
void mouseDraggedController() {
if (firstPress)
{
Button b = buttonCollection.overButton();
if (b != null)
{
lockedIndex = buttonCollection.getIndex(b);
b.posX = mouseX-b.buttonWidth/2;
b.posY = mouseY-b.buttonHeight/2;
b.tb.update(b.posX, b.posY);
if (canvas.overCanvas())
{
canvas.removeFromCanvas(buttonCollection, b);
}
else
{
if(b.isSmart)
{
buttonCollection.addButton(new Button(b.origX, b.origY, b.val, 1, b.outputString));
}
else
{
buttonCollection.addButton(new Button(b.origX, b.origY, b.val, b.outputString));
}
}
}
}else{
if (lockedIndex >= 0)
{
Button b = buttonCollection.getButton(lockedIndex);
b.posX = mouseX-b.buttonWidth/2;
b.posY = mouseY-b.buttonHeight/2;
b.tb.update(b.posX, b.posY);
}
}
firstPress = false;
}
void mousePressedController() {
Button selectedButton = buttonCollection.overTextBox();
if (selectedButton!=null) {
if (textBoxSelected!=null) {
textBoxSelected.setUserInput('b');
}
textBoxSelected = selectedButton.tb;
} else {
textBoxSelected = null;
buttonCollection.stopBlinking();
}
if (exitButton.overBlock()) {
exit();
} else if (resetButton.overBlock()) {
incrementer = 'a';
for (Button b : buttonCollection.getCollection()) {
b.posX = b.origX;
b.posY = b.origY;
for (int i = b.tb.userInput.size()-1; i>=0; i--) {
b.tb.userInput.remove(i);
}
for (int i = b.tb.userInput.size()-1; i>=0; i--) {
b.tb.relativeValue.remove(i);
}
b.tb.displayInput = "";
b.tb.relativeInput = "";
b.tb.condStm = "=";
b.tb.update(b.posX, b.posY);
}
canvas.resetCanvas();
mainView.resetBuildTerminalText();
} else if (buildButton.overBlock()) {
mainView.resetBuildTerminalText();
mainView.appendBuildTerminalText(" Build started");
output = createWriter("./output/output.ino");
printSetupFunction();
write("void loop() {\n");
ArrayList<Button> baseButtons = canvas.getBaseButtons();
parse(baseButtons, 1);
write("}\n");
output.close();
mainView.appendBuildTerminalText("\nBuild complete, output file saved to output/output.ino");
}
}
void keyPressedHandler() {
if (key>='0'&& key <= '9'||key == BACKSPACE || key==DELETE||(key == '<' || key == '>' || key == '=')||key=='i') {
if (textBoxSelected!=null) {
switch (key) {
case BACKSPACE:
case DELETE:
textBoxSelected.setUserInput('\0');
break;
case '<':
case '>':
case '=':
textBoxSelected.setCondStmt(key);
break;
case 'i':
textBoxSelected.setUserInput(key);
break;
default:
textBoxSelected.setUserInput(key);
break;
}
bcBool = false;
}
}
}
void blink() {
if (!bcBool && millis()-pastM>goal) {
pastM = millis();
if (textBoxSelected!=null) {
textBoxSelected.displayInput = textBoxSelected.displayInput + "|";
textBoxSelected.relativeInput = textBoxSelected.relativeInput + "|";
bcBool = true;
}
} else if (millis()-pastM>goal) {
pastM = millis();
if (textBoxSelected!=null) {
bcBool = false;
textBoxSelected.setUserInput('b');
}
}
}
void printSetupFunction()
{
write("#include \"EngduinoLight.h\" \n");
write("#include \"EngduinoThermistor.h\" \n");
write("#include \"EngduinoButton.h\" \n");
write("#include \"EngduinoLEDs.h\" \n");
write("#include \"EngduinoAccelerometer.h\" \n");
write("#include \"Wire.h\" \n");
write("\n");
write("void setup() {\n");
write("\tEngduinoLight.begin();\n");
write("\tEngduinoThermistor.begin();\n");
write("\tEngduinoButton.begin();\n");
write("\tEngduinoLEDs.begin();\n");
write("\tEngduinoAccelerometer.begin();\n");
write("}\n\n");
}
void parse(ArrayList<Button> buttons, int indent)
{
for(Button button : buttons)
{
for(int i = 0; i < indent; i++)
{
write("\t");
}
write(button.getOutputString());
if(button.isSmart)
{
write(" {\n");
parse(button.getNested(), indent+1);
for(int i = 0; i < indent; i++)
{
write("\t");
}
write("}");
}
write("\n");
}
}
void write(String s)
{
output.print(s);
output.flush();
}
}