1313import agents .AgentYoung ;
1414import app .Window ;
1515import javafx .geometry .Pos ;
16- import javafx .scene .Node ;
1716import javafx .scene .control .Slider ;
1817import javafx .scene .layout .Pane ;
1918import javafx .scene .layout .StackPane ;
2019import javafx .scene .paint .Color ;
2120import javafx .scene .shape .Circle ;
2221import javafx .scene .shape .Rectangle ;
2322import javafx .scene .text .Text ;
23+ import utils .obj .NewTextField ;
24+ import utils .obj .StatsText ;
2425
2526public class Scenes {
2627
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 ;
3034
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 ;
3638
37-
39+ /* returns Pane containing main elements */
3840 public static Pane getMainScene (ArrayList <Agent > agents ) {
3941 final Pane root = new Pane ();
4042 root .setStyle ("-fx-background-color: #2F2F2F" );
4143 root .setPrefSize (WIDTH +PANEL_WIDTH , HEIGHT );
4244
45+ /* main stackpane */
4346 panelStack = new StackPane ();
4447 panelStack .setPrefSize (PANEL_WIDTH , HEIGHT );
4548 panelStack .setTranslateX (WIDTH );
4649 panelStack .setTranslateY (0 );
4750 panelStack .setAlignment (Pos .TOP_LEFT );
4851 panelStack .setId ("panel-stack" );
49-
52+
53+ /* main stackpane background */
5054 final Rectangle panelBg = new Rectangle (PANEL_WIDTH , HEIGHT );
5155 panelBg .setId ("panel-bg" );
5256 panelBg .setTranslateX (0 );
5357 panelBg .setTranslateY (0 );
5458
55- settingsStack = getSettingsPanel (agents );
59+ /* set panel to settings view */
60+ settingsStack = getSettingsPanel (agents );
5661 panelStack .getChildren ().addAll (panelBg , settingsStack );
5762
5863 root .getChildren ().add (panelStack );
@@ -67,23 +72,16 @@ public static void showStats() {
6772 public static void showSettings () {
6873 panelStack .getChildren ().remove (statsStack );
6974 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- }
7775
7876 for (NewTextField tf : settingInputs ) {
7977 tf .requestFocus ();
8078 }
81- s . setValue ( 1 );
82- s .requestFocus ();
79+
80+ settingInputs . get ( 0 ) .requestFocus ();
8381 }
84-
82+
8583 @ SuppressWarnings ("static-access" )
86- public static StackPane getStatsPanel (ArrayList <Agent > agents ) {
84+ public static StackPane getStatsPanel (ArrayList <Agent > agents ) {
8785 final StackPane statsStack = new StackPane ();
8886 statsStack .setPrefSize (PANEL_WIDTH , HEIGHT );
8987 statsStack .setAlignment (Pos .TOP_LEFT );
@@ -98,9 +96,9 @@ public static StackPane getStatsPanel(ArrayList<Agent> agents) {
9896 final int agentTextSpacing = 30 ;
9997 int incr = 0 ;
10098
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 ());
104102
105103 int infected = 0 ;
106104 for (Agent a : agents )
@@ -160,6 +158,8 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
160158 eStack .getChildren ().addAll (eAgent , eValue , eText );
161159 agentStacks .add (eStack );
162160
161+ /* AGENT DOCTOR STACK */
162+
163163 final Circle dAgent = new Circle (agentRadius , AgentColor .DOCTOR );
164164 dAgent .setTranslateX (agentX );
165165 final NewTextField dValue = new NewTextField (AgentColor .DOCTOR , Window .DOCTORS );
@@ -171,6 +171,8 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
171171 dStack .getChildren ().addAll (dAgent , dValue , dText );
172172 agentStacks .add (dStack );
173173
174+ /* AGENT INFECTED STACK */
175+
174176 final Circle iAgent = new Circle (agentRadius , AgentColor .INFECTED );
175177 iAgent .setTranslateX (agentX );
176178 final NewTextField iValue = new NewTextField (AgentColor .INFECTED , Window .INFECTED );
@@ -182,10 +184,12 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
182184 iStack .getChildren ().addAll (iAgent , iValue , iText );
183185 agentStacks .add (iStack );
184186
187+ /* event for unfocusing TextFields */
185188 settingsStack .setOnMousePressed (e -> {
186189 settingsText .requestFocus ();
187190 });
188191
192+ /* start button stack & components */
189193 final StackPane startStack = new StackPane ();
190194 startStack .setMaxSize (115 , 40 );
191195 startStack .setTranslateY (-50 );
@@ -209,6 +213,7 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
209213 Utils .fadeColors (startText , 200 , WHITE , DARK_GREY );
210214 });
211215
216+ /* position and style agent stacks */
212217 incr = 0 ;
213218 for (StackPane sp : agentStacks ) {
214219 sp .setTranslateX (0 );
@@ -217,9 +222,12 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
217222 sp .setAlignment (Pos .CENTER_LEFT );
218223 }
219224
225+ /* position and bind event to TextFields */
220226 for (NewTextField tf : inputs ) {
221227 tf .setMaxSize (40 , 25 );
222228 tf .setTranslateX (agentX + agentRadius *2 + inputSpacing );
229+
230+ /* on focus loss live change agents count */
223231 tf .focusedProperty ().addListener ((obs , focusOut , focus ) -> {
224232 if (focusOut ) {
225233 if (tf .getText ().length () == 0 ) tf .setText (tf .getDefaultVal ());
@@ -230,6 +238,7 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
230238 } else {
231239 addAgents (tf .getAgentColor (), diff );
232240 }
241+
233242 if (tf .getAgentColor () == AgentColor .INFECTED ) {
234243 startStack .setDisable (tf .getNumText () == 0 );
235244 startStack .setOpacity (tf .getNumText () == 0 ? 0.5 : 1 );
@@ -244,18 +253,37 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
244253 speedText .setTranslateX (127 );
245254 speedText .setId ("speed-text" );
246255
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 );
250259 speedSlider .setTranslateY (380 );
251260 speedSlider .setId ("speed-slider" );
252261
253262 speedSlider .valueProperty ().addListener ((obs , oldVal , newVal ) -> {
254263 Window .DELTA_SPEED = newVal .intValue ();
255264 });
256265
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+
257285 startStack .setOnMouseClicked (e -> {
258- Utils .listAgents (agents );
286+ Utils .countAgents (agents );
259287 statsStack = getStatsPanel (agents );
260288 showStats ();
261289
@@ -265,15 +293,14 @@ public static StackPane getSettingsPanel(ArrayList<Agent> agents) {
265293
266294 startStack .getChildren ().addAll (startBg , startText );
267295
268- settingsStack .getChildren ().addAll (settingsText , startStack , speedSlider , speedText );
296+ settingsStack .getChildren ().addAll (settingsText , startStack , speedSlider , speedText , radiusSlider , radiusText );
269297 settingsStack .getChildren ().addAll (agentStacks );
270298
271299 settingInputs = inputs ;
272300 return settingsStack ;
273301 }
274302
275- public static void addAgents (Color color , int amount ) {
276- Window .agentsToAdd .clear ();
303+ public static void addAgents (Color color , int amount ) {
277304 if (color == AgentColor .YOUNG ) {
278305 for (int i =0 ; i <amount ; i ++)
279306 Window .agentsToAdd .add (new AgentYoung ());
@@ -295,7 +322,10 @@ public static void addAgents(Color color, int amount) {
295322 Window .agentsToAdd .add (a );
296323 }
297324 }
298- Window .agentsToAdd .add (0 , null );
325+
326+ if (Window .agentsToAdd .get (0 ) != null ) {
327+ Window .agentsToAdd .add (0 , null );
328+ }
299329 }
300330
301331 public static void removeAgents (ArrayList <Agent > agents , Color color , int amount ) {
0 commit comments