-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGui.pde
199 lines (175 loc) · 6.98 KB
/
Gui.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
import controlP5.*;
class Gui {
private GuiCallback delegate;
private ControlP5 cp5;
private Button startButton;
private Button resetButton;
private Textlabel moveInfectedSliderLabel;
private Slider moveInfectedSlider;
private Textlabel ballCountTextFieldLabel;
private Textfield ballCountTextField;
private Textfield secondsInputTextField;
private Textlabel generalMovementSliderLabel;
private Slider generalMovementSlider;
private Textlabel superSpreaderSliderLabel;
private Slider superSpreaderSlider;
private PFont SFFont_25;
private PFont SFFont_14;
Gui(PApplet applet, GuiCallback delegate) {
this.cp5 = new ControlP5(applet);
this.delegate = delegate;
this.SFFont_25 = loadFont("SFProDisplay-Light-25.vlw");
this.SFFont_14 = loadFont("SFProDisplay-Light-14.vlw");
}
// GUI setup
void setupGui() {
setupStartButton();
setupResetButton();
setupMoveOnInfectedSlider();
setupBallCountInput();
setupTimeInput();
setupGeneralMovementSlider();
setupSuperSpreaderSlider();
}
void setupStartButton() {
startButton = cp5.addButton("START")
.setFont(SFFont_14)
.setPosition((Constants.View.SCREEN_WIDTH / 2) - (Constants.Gui.START_BUTTON_WIDTH / 2), Constants.View.SCREEN_HEIGHT / 2)
.setSize(Constants.Gui.START_BUTTON_WIDTH, Constants.Gui.START_BUTTON_HEIGHT)
.onPress(new CallbackListener() {
public void controlEvent(CallbackEvent event) {
delegate.startButtonTapped();
}
});
}
void setupResetButton() {
resetButton = cp5.addButton("RESET")
.setFont(SFFont_14)
.setPosition((Constants.View.SCREEN_WIDTH - Constants.Gui.RESET_BUTTON_WIDTH - Constants.Gui.STANDARD_PADDING), (Constants.View.BOTTOM_LINE_POS + Constants.Gui.STANDARD_PADDING))
.setSize(Constants.Gui.RESET_BUTTON_WIDTH, (Constants.View.SCREEN_HEIGHT - Constants.View.BOTTOM_LINE_POS - (2 * Constants.Gui.STANDARD_PADDING)))
.onPress(new CallbackListener () {
public void controlEvent(CallbackEvent event) {
delegate.resetButtonTapped();
}
});
}
void setupMoveOnInfectedSlider() {
moveInfectedSliderLabel = cp5.addTextlabel("stopMovingInfectedLabel")
.setText("Slow infected movement by %:")
.setPosition(Constants.Gui.STANDARD_PADDING, Constants.View.BOTTOM_LINE_POS + Constants.Gui.TOP_LABEL_PADDING)
.setColorValue(Constants.Color.BLACK)
.setFont(SFFont_14);
moveInfectedSlider = cp5.addSlider("infectedSlider")
.setLabel("")
.setRange(0, 100)
.setValue(25)
.setPosition(Constants.Gui.COL_ONE_CONTROL_X, Constants.View.BOTTOM_LINE_POS + (Constants.Gui.STANDARD_PADDING * 2))
.setSize(Constants.Gui.INPUT_WIDTH, Constants.Gui.INPUT_HEIGHT);
}
void setupGeneralMovementSlider() {
generalMovementSliderLabel = cp5.addTextlabel("generalMovementSliderLabel")
.setText("% of people allowed to move:")
.setPosition(Constants.Gui.STANDARD_PADDING, moveInfectedSliderLabel.getPosition()[1] + Constants.Gui.LABEL_SPACING)
.setColorValue(Constants.Color.BLACK)
.setFont(SFFont_14);
generalMovementSlider = cp5.addSlider("generalMovementSlider")
.setLabel("")
.setRange(0, 100)
.setValue(100)
.setPosition(Constants.Gui.COL_ONE_CONTROL_X, moveInfectedSlider.getPosition()[1] + Constants.Gui.CONTROL_SPACING)
.setSize(Constants.Gui.INPUT_WIDTH, Constants.Gui.INPUT_HEIGHT);
}
void setupSuperSpreaderSlider() {
superSpreaderSliderLabel = cp5.addTextlabel("superSPreaderSliderLabel")
.setText("% of super spreaders (x2 speed):")
.setPosition(Constants.Gui.STANDARD_PADDING, generalMovementSliderLabel.getPosition()[1] + Constants.Gui.LABEL_SPACING)
.setColorValue(Constants.Color.BLACK)
.setFont(SFFont_14);
superSpreaderSlider = cp5.addSlider("superSpreaderSlider")
.setLabel("")
.setRange(0, 100)
.setValue(0)
.setPosition(Constants.Gui.COL_ONE_CONTROL_X, generalMovementSlider.getPosition()[1] + Constants.Gui.CONTROL_SPACING)
.setSize(Constants.Gui.INPUT_WIDTH, Constants.Gui.INPUT_HEIGHT);
}
void setupBallCountInput() {
ballCountTextFieldLabel = cp5.addTextlabel("ballCountLabel")
.setText("Number of people:")
.setPosition(moveInfectedSlider.getPosition()[0] + Constants.Gui.INPUT_WIDTH + (Constants.Gui.STANDARD_PADDING * 15), Constants.View.BOTTOM_LINE_POS + Constants.Gui.TOP_LABEL_PADDING)
.setColorValue(Constants.Color.BLACK)
.setFont(SFFont_14);
ballCountTextField = cp5.addTextfield("ballCountInput")
.setLabel("")
.setPosition(Constants.Gui.COL_TWO_CONTROL_X, Constants.View.BOTTOM_LINE_POS + Constants.Gui.TOP_LABEL_PADDING)
.setSize(Constants.Gui.INPUT_WIDTH, Constants.Gui.INPUT_HEIGHT)
.setFont(SFFont_14)
.setColorBackground(color(Constants.Color.WHITE))
.setColor(Constants.Color.BLACK);
setDefaultBallCount();
}
void setupTimeInput() {
cp5.addTextlabel("secondsInputLabel")
.setText("Simulation duration (seconds):")
.setPosition(moveInfectedSlider.getPosition()[0] + Constants.Gui.INPUT_WIDTH + (Constants.Gui.STANDARD_PADDING * 15), ballCountTextField.getPosition()[1] + Constants.Gui.CONTROL_SPACING)
.setColorValue(Constants.Color.BLACK)
.setFont(SFFont_14);
secondsInputTextField = cp5.addTextfield("secondsInput")
.setLabel("")
.setPosition(Constants.Gui.COL_TWO_CONTROL_X, ballCountTextField.getPosition()[1] + Constants.Gui.CONTROL_SPACING)
.setSize(Constants.Gui.INPUT_WIDTH, Constants.Gui.INPUT_HEIGHT)
.setFont(SFFont_14)
.setColorBackground(color(Constants.Color.WHITE))
.setColor(Constants.Color.BLACK);
setDefaultDuration();
}
// GUI getters & setters
void showResetButton(boolean show) {
if (show) {
startButton.show();
} else {
startButton.hide();
}
}
float getInfectionMovementReductionPercentage() {
return moveInfectedSlider.getValue() / 100;
}
int getBallCountFromTextField() {
String ballCountText = ballCountTextField.getText();
if (Utility.isInteger(ballCountText)) {
int ballCount = Integer.parseInt(ballCountText);
int area = (Constants.View.BOTTOM_LINE_POS - Constants.View.TOP_LINE_POS) * Constants.View.SCREEN_WIDTH;
int totalCircleArea = ((Constants.Simulator.RADIUS * 2) * (Constants.Simulator.RADIUS * 2)) * ballCount;
if ((float)totalCircleArea/(float)area > 0.5) {
setDefaultBallCount();
return Constants.Simulator.DEFAULT_BALL_COUNT;
} else {
return ballCount;
}
} else {
setDefaultBallCount();
return Constants.Simulator.DEFAULT_BALL_COUNT;
}
}
int getDurationFromTextField() {
String secondsText = secondsInputTextField.getText();
if (Utility.isInteger(secondsText)) {
return Integer.parseInt(secondsText);
} else {
setDefaultDuration();
return Constants.Simulator.DEFAULT_TOTAL_SECONDS;
}
}
float getMovementPercentage() {
return generalMovementSlider.getValue() / 100;
}
float getSuperSpreaderPercentage() {
return superSpreaderSlider.getValue() / 100;
}
// Set default values
void setDefaultBallCount() {
ballCountTextField.setText(Integer.toString(Constants.Simulator.DEFAULT_BALL_COUNT));
}
void setDefaultDuration() {
secondsInputTextField.setText(Integer.toString(Constants.Simulator.DEFAULT_TOTAL_SECONDS));
}
}