13
13
import agents .AgentYoung ;
14
14
import app .Window ;
15
15
import javafx .geometry .Pos ;
16
- import javafx .scene .Node ;
17
16
import javafx .scene .control .Slider ;
18
17
import javafx .scene .layout .Pane ;
19
18
import javafx .scene .layout .StackPane ;
20
19
import javafx .scene .paint .Color ;
21
20
import javafx .scene .shape .Circle ;
22
21
import javafx .scene .shape .Rectangle ;
23
22
import javafx .scene .text .Text ;
23
+ import utils .obj .NewTextField ;
24
+ import utils .obj .StatsText ;
24
25
25
26
public class Scenes {
26
27
27
- public static StackPane panelStack ;
28
- public static StackPane settingsStack ;
29
- public static StackPane statsStack ;
28
+ private static StackPane panelStack ;
29
+ private static StackPane settingsStack ;
30
+ private static StackPane statsStack ;
31
+ private static Slider speedSlider ;
32
+ private static Slider radiusSlider ;
33
+ private static ArrayList <NewTextField > settingInputs ;
30
34
31
- public static NewText iText ;
32
- public static NewText dText ;
33
- public static NewText rText ;
34
-
35
- public static ArrayList <NewTextField > settingInputs ;
35
+ public static StatsText iText ;
36
+ public static StatsText dText ;
37
+ public static StatsText rText ;
36
38
37
-
39
+ /* returns Pane containing main elements */
38
40
public static Pane getMainScene (ArrayList <Agent > agents ) {
39
41
final Pane root = new Pane ();
40
42
root .setStyle ("-fx-background-color: #2F2F2F" );
41
43
root .setPrefSize (WIDTH +PANEL_WIDTH , HEIGHT );
42
44
45
+ /* main stackpane */
43
46
panelStack = new StackPane ();
44
47
panelStack .setPrefSize (PANEL_WIDTH , HEIGHT );
45
48
panelStack .setTranslateX (WIDTH );
46
49
panelStack .setTranslateY (0 );
47
50
panelStack .setAlignment (Pos .TOP_LEFT );
48
51
panelStack .setId ("panel-stack" );
49
-
52
+
53
+ /* main stackpane background */
50
54
final Rectangle panelBg = new Rectangle (PANEL_WIDTH , HEIGHT );
51
55
panelBg .setId ("panel-bg" );
52
56
panelBg .setTranslateX (0 );
53
57
panelBg .setTranslateY (0 );
54
58
55
- settingsStack = getSettingsPanel (agents );
59
+ /* set panel to settings view */
60
+ settingsStack = getSettingsPanel (agents );
56
61
panelStack .getChildren ().addAll (panelBg , settingsStack );
57
62
58
63
root .getChildren ().add (panelStack );
@@ -67,23 +72,16 @@ public static void showStats() {
67
72
public static void showSettings () {
68
73
panelStack .getChildren ().remove (statsStack );
69
74
panelStack .getChildren ().add (settingsStack );
70
-
71
- Slider s = null ;
72
- for (Node n : settingsStack .getChildren ()) {
73
- if (n .getId () != null && n .getId ().equals ("speed-slider" )) {
74
- s = (Slider ) n ;
75
- }
76
- }
77
75
78
76
for (NewTextField tf : settingInputs ) {
79
77
tf .requestFocus ();
80
78
}
81
- s . setValue ( 1 );
82
- s .requestFocus ();
79
+
80
+ settingInputs . get ( 0 ) .requestFocus ();
83
81
}
84
-
82
+
85
83
@ SuppressWarnings ("static-access" )
86
- public static StackPane getStatsPanel (ArrayList <Agent > agents ) {
84
+ public static StackPane getStatsPanel (ArrayList <Agent > agents ) {
87
85
final StackPane statsStack = new StackPane ();
88
86
statsStack .setPrefSize (PANEL_WIDTH , HEIGHT );
89
87
statsStack .setAlignment (Pos .TOP_LEFT );
@@ -98,9 +96,9 @@ public static StackPane getStatsPanel(ArrayList<Agent> agents) {
98
96
final int agentTextSpacing = 30 ;
99
97
int incr = 0 ;
100
98
101
- iText = new NewText ("INFECTED" , agentTextX , agentTextY + agentTextSpacing *incr ++, agents .size ());
102
- dText = new NewText ("DEAD" , agentTextX , agentTextY + agentTextSpacing *incr ++, agents .size ());
103
- rText = new NewText ("RECOVERED" , agentTextX , agentTextY + agentTextSpacing *incr ++, agents .size ());
99
+ iText = new StatsText ("INFECTED" , agentTextX , agentTextY + agentTextSpacing *incr ++, agents .size ());
100
+ dText = new StatsText ("DEAD" , agentTextX , agentTextY + agentTextSpacing *incr ++, agents .size ());
101
+ rText = new StatsText ("RECOVERED" , agentTextX , agentTextY + agentTextSpacing *incr ++, agents .size ());
104
102
105
103
int infected = 0 ;
106
104
for (Agent a : agents )
@@ -160,6 +158,8 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
160
158
eStack .getChildren ().addAll (eAgent , eValue , eText );
161
159
agentStacks .add (eStack );
162
160
161
+ /* AGENT DOCTOR STACK */
162
+
163
163
final Circle dAgent = new Circle (agentRadius , AgentColor .DOCTOR );
164
164
dAgent .setTranslateX (agentX );
165
165
final NewTextField dValue = new NewTextField (AgentColor .DOCTOR , Window .DOCTORS );
@@ -171,6 +171,8 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
171
171
dStack .getChildren ().addAll (dAgent , dValue , dText );
172
172
agentStacks .add (dStack );
173
173
174
+ /* AGENT INFECTED STACK */
175
+
174
176
final Circle iAgent = new Circle (agentRadius , AgentColor .INFECTED );
175
177
iAgent .setTranslateX (agentX );
176
178
final NewTextField iValue = new NewTextField (AgentColor .INFECTED , Window .INFECTED );
@@ -182,10 +184,12 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
182
184
iStack .getChildren ().addAll (iAgent , iValue , iText );
183
185
agentStacks .add (iStack );
184
186
187
+ /* event for unfocusing TextFields */
185
188
settingsStack .setOnMousePressed (e -> {
186
189
settingsText .requestFocus ();
187
190
});
188
191
192
+ /* start button stack & components */
189
193
final StackPane startStack = new StackPane ();
190
194
startStack .setMaxSize (115 , 40 );
191
195
startStack .setTranslateY (-50 );
@@ -209,6 +213,7 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
209
213
Utils .fadeColors (startText , 200 , WHITE , DARK_GREY );
210
214
});
211
215
216
+ /* position and style agent stacks */
212
217
incr = 0 ;
213
218
for (StackPane sp : agentStacks ) {
214
219
sp .setTranslateX (0 );
@@ -217,9 +222,12 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
217
222
sp .setAlignment (Pos .CENTER_LEFT );
218
223
}
219
224
225
+ /* position and bind event to TextFields */
220
226
for (NewTextField tf : inputs ) {
221
227
tf .setMaxSize (40 , 25 );
222
228
tf .setTranslateX (agentX + agentRadius *2 + inputSpacing );
229
+
230
+ /* on focus loss live change agents count */
223
231
tf .focusedProperty ().addListener ((obs , focusOut , focus ) -> {
224
232
if (focusOut ) {
225
233
if (tf .getText ().length () == 0 ) tf .setText (tf .getDefaultVal ());
@@ -230,6 +238,7 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
230
238
} else {
231
239
addAgents (tf .getAgentColor (), diff );
232
240
}
241
+
233
242
if (tf .getAgentColor () == AgentColor .INFECTED ) {
234
243
startStack .setDisable (tf .getNumText () == 0 );
235
244
startStack .setOpacity (tf .getNumText () == 0 ? 0.5 : 1 );
@@ -244,18 +253,37 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
244
253
speedText .setTranslateX (127 );
245
254
speedText .setId ("speed-text" );
246
255
247
- Slider speedSlider = new Slider (1 , 5 , 1 );
248
- speedSlider .setMaxWidth (PANEL_WIDTH - 100 );
249
- speedSlider .setTranslateX (50 );
256
+ speedSlider = new Slider (1 , 5 , 1 );
257
+ speedSlider .setMaxWidth (PANEL_WIDTH - 50 );
258
+ speedSlider .setTranslateX (25 );
250
259
speedSlider .setTranslateY (380 );
251
260
speedSlider .setId ("speed-slider" );
252
261
253
262
speedSlider .valueProperty ().addListener ((obs , oldVal , newVal ) -> {
254
263
Window .DELTA_SPEED = newVal .intValue ();
255
264
});
256
265
266
+ Text radiusText = new Text ("RADIUS" );
267
+ radiusText .setTranslateY (430 );
268
+ radiusText .setTranslateX (127 );
269
+ radiusText .setId ("speed-text" );
270
+
271
+ radiusSlider = new Slider (1 , 15 , 1 );
272
+ radiusSlider .setMaxWidth (PANEL_WIDTH - 50 );
273
+ radiusSlider .setTranslateX (25 );
274
+ radiusSlider .setTranslateY (450 );
275
+ radiusSlider .setId ("radius-slider" );
276
+ radiusSlider .setValue (Agent .RADIUS );
277
+
278
+ radiusSlider .valueProperty ().addListener ((obs , oldVal , newVal ) -> {
279
+ for (Agent a : agents ) {
280
+ Agent .RADIUS = newVal .intValue ();
281
+ a .setRadius (Agent .RADIUS );
282
+ }
283
+ });
284
+
257
285
startStack .setOnMouseClicked (e -> {
258
- Utils .listAgents (agents );
286
+ Utils .countAgents (agents );
259
287
statsStack = getStatsPanel (agents );
260
288
showStats ();
261
289
@@ -265,15 +293,14 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
265
293
266
294
startStack .getChildren ().addAll (startBg , startText );
267
295
268
- settingsStack .getChildren ().addAll (settingsText , startStack , speedSlider , speedText );
296
+ settingsStack .getChildren ().addAll (settingsText , startStack , speedSlider , speedText , radiusSlider , radiusText );
269
297
settingsStack .getChildren ().addAll (agentStacks );
270
298
271
299
settingInputs = inputs ;
272
300
return settingsStack ;
273
301
}
274
302
275
- public static void addAgents (Color color , int amount ) {
276
- Window .agentsToAdd .clear ();
303
+ public static void addAgents (Color color , int amount ) {
277
304
if (color == AgentColor .YOUNG ) {
278
305
for (int i =0 ; i <amount ; i ++)
279
306
Window .agentsToAdd .add (new AgentYoung ());
@@ -295,7 +322,10 @@ public static void addAgents(Color color, int amount) {
295
322
Window .agentsToAdd .add (a );
296
323
}
297
324
}
298
- Window .agentsToAdd .add (0 , null );
325
+
326
+ if (Window .agentsToAdd .get (0 ) != null ) {
327
+ Window .agentsToAdd .add (0 , null );
328
+ }
299
329
}
300
330
301
331
public static void removeAgents (ArrayList <Agent > agents , Color color , int amount ) {
0 commit comments