Skip to content

Commit 2235fd7

Browse files
committed
🔧 Improved transition from stats to settings
1 parent 57255e3 commit 2235fd7

File tree

8 files changed

+116
-48
lines changed

8 files changed

+116
-48
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>po</groupId>
55
<artifactId>Symulacja</artifactId>
6-
<version>0.5.0-alpha</version>
6+
<version>0.6.0-alpha</version>
77
<dependencies>
88
<dependency>
99
<groupId>org.openjfx</groupId>

src/main/java/agents/Agent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919

2020
public abstract class Agent extends Circle implements Agentable {
2121

22-
public static int RADIUS = 4;
22+
public static int RADIUS = 4; // first assign default value, then possibly change with a slider
2323

2424
protected static int ID = 0; // custom noise seed for every agent
25-
private static final int MARGIN = 10; // spawning margin
25+
private static final int MARGIN = 10; // spawning margin
2626
protected static final Random rnd = new Random();
2727

2828
private final OpenSimplexNoise osn; // noise object

src/main/java/app/Window.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void handle(long now) {
8282
agentsToAdd.remove(0);
8383
agents.addAll(agentsToAdd);
8484
root.getChildren().addAll(agentsToAdd);
85+
agentsToAdd.clear();
8586
}
8687

8788
if(infectious) {

src/main/java/utils/Scenes.java

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,51 @@
1313
import agents.AgentYoung;
1414
import app.Window;
1515
import javafx.geometry.Pos;
16-
import javafx.scene.Node;
1716
import javafx.scene.control.Slider;
1817
import javafx.scene.layout.Pane;
1918
import javafx.scene.layout.StackPane;
2019
import javafx.scene.paint.Color;
2120
import javafx.scene.shape.Circle;
2221
import javafx.scene.shape.Rectangle;
2322
import javafx.scene.text.Text;
23+
import utils.obj.NewTextField;
24+
import utils.obj.StatsText;
2425

2526
public 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) {

src/main/java/utils/Utils.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,20 @@ public static ArrayList<Agent> createAgents(int y, int e, int d, int i) {
3636
return agents;
3737
}
3838

39-
public static void listAgents(ArrayList<Agent> agents) {
39+
public static void countAgents(ArrayList<Agent> agents) {
4040
int y = 0, e = 0, d = 0, i = 0;
4141
for(Agent a : agents) {
4242
if(a instanceof AgentYoung) y++;
4343
if(a instanceof AgentElderly) e++;
4444
if(a instanceof AgentDoctor) d++;
4545
if(a.isInfected()) i++;
4646
}
47+
48+
Window.YOUNG = y;
49+
Window.ELDERLY = e;
50+
Window.DOCTORS = d;
51+
Window.INFECTED = (y-i);
52+
4753
Log.success("Creating " + (y-i) + " young agents");
4854
Log.success("Creating " + e + " elderly agents");
4955
Log.success("Creating " + d + " doctor agents");

src/main/java/utils/NewTextField.java renamed to src/main/java/utils/obj/NewTextField.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils;
1+
package utils.obj;
22

33
import java.util.ArrayList;
44

@@ -23,8 +23,13 @@ public NewTextField(String defaultVal, Color agentColor) {
2323
getStyleClass().add("agent-value");
2424

2525
textProperty().addListener((obs, oldVal, newVal) -> {
26-
if(newVal.length() > 3)
26+
try {
27+
if(newVal.length() > 3)
28+
setText(oldVal);
29+
Integer.parseInt(newVal);
30+
} catch (Exception e) {
2731
setText(oldVal);
32+
}
2833
return;
2934
});
3035
}

src/main/java/utils/NewText.java renamed to src/main/java/utils/obj/StatsText.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package utils;
1+
package utils.obj;
22

33
import app.Window;
44
import javafx.geometry.Pos;
55
import javafx.scene.layout.StackPane;
66
import javafx.scene.text.Text;
77

8-
public class NewText extends StackPane {
8+
public class StatsText extends StackPane {
99

1010
private final int max;
1111

1212
private final Text name;
1313
private final Text stats;
1414

15-
public NewText(String defaultVal, int x, int y, int max) {
15+
public StatsText(String defaultVal, int x, int y, int max) {
1616
this.name = new Text(defaultVal + ": ");
1717
this.stats = new Text();
1818
this.max = max;

src/resources/styles/mainStyle.css

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@
4343
-fx-control-inner-background: #FCFCFC;
4444
}
4545

46-
#speed-text {
47-
-fx-fill: #FCFCFC;
48-
-fx-font-family: 'Raleway';
49-
-fx-font-size: 15;
50-
}
46+
5147

5248
#start-stack {
5349
-fx-cursor: hand;
@@ -59,6 +55,26 @@
5955
-fx-fill: #FCFCFC;
6056
}
6157

58+
#speed-text {
59+
-fx-fill: #FCFCFC;
60+
-fx-font-family: 'Raleway';
61+
-fx-font-size: 15;
62+
}
63+
64+
#radius-text {
65+
-fx-fill: #FCFCFC;
66+
-fx-font-family: 'Raleway';
67+
-fx-font-size: 15;
68+
}
69+
70+
#radius-slider .track {
71+
-fx-background-color: #515658;
72+
}
73+
74+
#radius-slider .thumb {
75+
-fx-background-color: #6E7577;
76+
}
77+
6278
#speed-slider .track {
6379
-fx-background-color: #515658;
6480
}
@@ -75,4 +91,14 @@
7591
-fx-minor-tick-count: 0;
7692
-fx-block-increment: 1;
7793
-fx-control-inner-background: #515658;
94+
}
95+
96+
#radius-slider {
97+
-fx-tick-label-fill: #6E7577;
98+
-fx-show-tick-labels: true;
99+
-fx-snap-to-ticks: true;
100+
-fx-major-tick-unit: 1;
101+
-fx-minor-tick-count: 0;
102+
-fx-block-increment: 1;
103+
-fx-control-inner-background: #515658;
78104
}

0 commit comments

Comments
 (0)