Skip to content

Commit

Permalink
Finished settings input form
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiPl01 committed Dec 28, 2021
1 parent 46a88ca commit ebf4cec
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 82 deletions.
6 changes: 0 additions & 6 deletions src/main/java/my/project/gui/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.FileInputStream;
Expand All @@ -17,11 +16,6 @@ public class App extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
// FXMLLoader loader = new FXMLLoader();
// loader.setLocation(getClass().getResource("/fxml/MainBox.fxml"));
// BorderPane mainContainer = loader.load();
// Scene scene = new Scene(mainContainer);

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("/fxml/InputBox.fxml"));
AnchorPane inputContainer = loader.load();
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/my/project/gui/controllers/InputBoxController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package my.project.gui.controllers;

import javafx.fxml.FXML;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import my.project.gui.config.Config;
import my.project.gui.config.ConfigLoader;
import my.project.gui.config.MapSettings;
Expand All @@ -12,10 +17,11 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import java.util.Objects;

public class InputBoxController {
private static final String CONFIG_JSON_PATH = "./src/main/resources/config.json";
private static final String MAIN_SCENE_PATH = "/fxml/MainBox.fxml";

private static final int FOLDING_MAP_TAB_INDEX = 0;
private static final int FENCED_MAP_TAB_INDEX = 1;
Expand Down Expand Up @@ -48,7 +54,7 @@ private void onStart() {
if (!fencedMapCheckbox.isSelected()) {
settings.put(MapType.FENCED, fencedInputFormController.generateMapSettings());
}
System.out.println(settings);
openMainScene(settings);
}

@FXML
Expand Down Expand Up @@ -106,4 +112,35 @@ private Config loadConfig() {
return null;
}
}

private void openMainScene(Map<MapType, MapSettings> settings) {
// Load main container
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(MAIN_SCENE_PATH));
BorderPane borderPane = null;
try {
borderPane = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
MainController mainController = loader.getController();

// Load maps containers
try {
mainController.init(settings);
} catch (IOException e) {
System.out.println("Error while loading map containers");
e.printStackTrace();
return;
}

// Set main container in the scene
Stage stage = (Stage)formTabPane.getScene().getWindow();
assert borderPane != null;
Scene scene = new Scene(borderPane);
String css = Objects.requireNonNull(getClass().getResource("/css/style.css")).toExternalForm();
scene.getStylesheets().add(css);
stage.setScene(scene);
stage.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
public class InputFormController {
private static final String NORMAL_STRATEGY_NAME = "normal";
private static final String MAGIC_STRATEGY_NAME = "magic";
private static final String DEFAULT_STRATEGY_NAME = NORMAL_STRATEGY_NAME;

private String currentStrategy = NORMAL_STRATEGY_NAME;

private InputBoxController parentController;
private MapConfig mapConfig;
private MapType mapType;
private List<TextField> textFields = new ArrayList<>();
private final List<TextField> textFields = new ArrayList<>();

@FXML
private TextField widthInput;
Expand Down Expand Up @@ -110,14 +111,14 @@ public void fillWithDefaults() {
bushEnergyInput.setText(String.valueOf(mapConfig.bushEnergy.def));
grassEnergyInput.setText(String.valueOf(mapConfig.grassEnergy.def));
initialAnimalsInput.setText(String.valueOf(mapConfig.animalsCount.def));
setupCombobox(NORMAL_STRATEGY_NAME, MAGIC_STRATEGY_NAME);
strategyCombobox.setValue(DEFAULT_STRATEGY_NAME);
if (Objects.equals(currentStrategy, NORMAL_STRATEGY_NAME)) disableMagicInput();
else enableMagicInput();
}

public void clearInput() {
textFields.forEach(field -> field.setText(""));
setupCombobox(NORMAL_STRATEGY_NAME, MAGIC_STRATEGY_NAME);
strategyCombobox.setValue(DEFAULT_STRATEGY_NAME);
}

private void disableInputFields() {
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/my/project/gui/controllers/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,40 @@
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.SplitPane;
import my.project.simulation.enums.MapStrategy;
import my.project.gui.config.MapSettings;
import my.project.gui.enums.MapType;
import my.project.simulation.maps.FencedMap;
import my.project.simulation.maps.FoldingMap;
import my.project.simulation.maps.IMap;

import java.io.IOException;
import java.util.Map;

public class MainController {
private double mainPaneWidth = 0;
private double mainPaneHeight = 0;
private static final String LEFT_CONTAINER_PATH = "/fxml/ContainerLeft.fxml";
private static final String RIGHT_CONTAINER_PATH = "/fxml/ContainerRight.fxml";

@FXML
private SplitPane mainSplitPane;

@FXML
private void initialize() throws IOException {
IMap foldingMap = new FoldingMap(20, 20, .3, 200, 3, 60,
40, 40);
foldingMap.setStrategy(MapStrategy.MAGIC);

IMap fencedMap = new FencedMap(10, 10, .5, 100,
5, 50, 50, 50);
public void init(Map<MapType, MapSettings> settings) throws IllegalArgumentException, IOException {
if (settings.containsKey(MapType.FOLDING) && settings.containsKey(MapType.FENCED)) {
loadContainer(createMap(MapType.FOLDING, settings.get(MapType.FOLDING)), LEFT_CONTAINER_PATH);
loadContainer(createMap(MapType.FENCED, settings.get(MapType.FENCED)), RIGHT_CONTAINER_PATH);
} else if (settings.containsKey(MapType.FOLDING)) {
loadContainer(createMap(MapType.FOLDING, settings.get(MapType.FOLDING)), LEFT_CONTAINER_PATH);
} else if (settings.containsKey(MapType.FENCED)) {
loadContainer(createMap(MapType.FENCED, settings.get(MapType.FENCED)), LEFT_CONTAINER_PATH);
} else throw new IllegalArgumentException("Invalid settings. Cannot create a map.");
}

loadContainer(foldingMap, "/fxml/ContainerLeft.fxml");
loadContainer(fencedMap, "/fxml/ContainerRight.fxml");
private IMap createMap(MapType mapType, MapSettings mapSettings) {
return switch (mapType) {
case FOLDING -> new FoldingMap(mapSettings);
case FENCED -> new FencedMap(mapSettings);
};
}

private void loadContainer(IMap map, String path) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ public void run() {
}
}
}

// TODO - move statistics saving to another place (only if user decides to generate a file with statistics)
// for (IMap map: visualizers.values()) map.getStatsMeter().generateCSVFile();
}

private boolean initialize() {
Expand Down
41 changes: 19 additions & 22 deletions src/main/java/my/project/simulation/maps/AbstractMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package my.project.simulation.maps;

import my.project.gui.config.MapSettings;
import my.project.gui.simulation.grid.IBuilder;
import my.project.simulation.enums.MapStrategy;
import my.project.simulation.stats.StatsMeter;
Expand All @@ -13,8 +14,8 @@

public abstract class AbstractMap implements IMap, IObserver {
private static final double MIN_BREED_ENERGY_RATIO = .5;
private static final int MAGIC_STRATEGY_RESPAWN_THRESHOLD = 5;
private static final int MAX_MAGIC_RESPAWNS_COUNT = 3;
private final int magicStrategyRespawnThreshold;
private final int maxMagicRespawnsCount;

private final int width;
private final int height;
Expand All @@ -35,7 +36,7 @@ public abstract class AbstractMap implements IMap, IObserver {

protected final int fieldsCount;
private final int initialAnimalsCount;
private MapStrategy strategy = MapStrategy.NORMAL; // The default strategy is normal
private MapStrategy strategy;
private int magicRespawnsCount = 0;

private long dayNum = 0;
Expand All @@ -48,23 +49,24 @@ public abstract class AbstractMap implements IMap, IObserver {
protected StatsMeter statsMeter;
private AnimalTracker animalTracker;

// TODO - add input type checking (on a frontEnd side)
AbstractMap(int width, int height, double jungleRatio,
int startEnergy, int moveEnergy, int bushEnergy, int grassEnergy,
int animalsCount) {
AbstractMap(MapSettings mapSettings) {
// Store initial values
this.width = width;
this.height = height;
this.initialAnimalsCount = animalsCount;
this.startEnergy = startEnergy;
this.moveEnergy = moveEnergy;
this.bushEnergy = bushEnergy;
this.grassEnergy = grassEnergy;
this.width = mapSettings.width();
this.height = mapSettings.height();
this.initialAnimalsCount = mapSettings.animalsCount();
this.startEnergy = mapSettings.startEnergy();
this.moveEnergy = mapSettings.moveEnergy();
this.bushEnergy = mapSettings.bushEnergy();
this.grassEnergy = mapSettings.grassEnergy();
this.magicStrategyRespawnThreshold = mapSettings.magicRespawnAnimals();
this.maxMagicRespawnsCount = mapSettings.magicRespawnsCount();
this.strategy = mapSettings.mapStrategy();

// Calculate map upper-right bound vector
this.mapUpperRight = new Vector2D(width - 1, height - 1);

// Calculate jungle bounding vectors and a number of fields
double jungleRatio = mapSettings.jungleRatio();
int jungleWidth = (int)Math.round(width * jungleRatio);
if (jungleWidth % 2 != width % 2) jungleWidth += 1;
int jungleHeight = (int)Math.round(height * jungleRatio);
Expand Down Expand Up @@ -99,8 +101,8 @@ public void removeSprite(ISprite sprite) throws NoSuchElementException {
genomesTree.remove(animal.getGenome(), animal);
// If a strategy is set to magic
if (strategy == MapStrategy.MAGIC &&
animalsCount - diedAnimalsCount == MAGIC_STRATEGY_RESPAWN_THRESHOLD &&
magicRespawnsCount++ < MAX_MAGIC_RESPAWNS_COUNT) {
animalsCount - diedAnimalsCount == magicStrategyRespawnThreshold &&
magicRespawnsCount++ < maxMagicRespawnsCount) {
handleMagicRespawn();
}
}
Expand Down Expand Up @@ -169,11 +171,6 @@ public long getNewAnimalID() {
return animalsCount + 1;
}

@Override
public void setStrategy(MapStrategy strategy) {
this.strategy = strategy;
}

@Override
public void update() {
if (areAnimalsAlive()) {
Expand All @@ -192,7 +189,7 @@ public boolean areAnimalsAlive() {
}

@Override
public void setAnimalTracker(AnimalTracker tracker) { // TODO - add a possibility tu setup trackers
public void setAnimalTracker(AnimalTracker tracker) {
if (animalTracker != null) animalTracker.remove();
animalTracker = tracker;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/my/project/simulation/maps/FencedMap.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package my.project.simulation.maps;

import my.project.gui.config.MapSettings;
import my.project.simulation.enums.MapStrategy;
import my.project.simulation.stats.StatsMeter;
import my.project.simulation.utils.Vector2D;

public class FencedMap extends AbstractMap {
private static final String STATISTICS_FILE_NAME = "stats-fencedMap.csv";

public FencedMap(int width, int height, double jungleRatio,
int startEnergy, int moveEnergy, int bushEnergy, int grassEnergy,
int animalsCount) {
super(width, height, jungleRatio, startEnergy, moveEnergy, bushEnergy, grassEnergy, animalsCount);
public FencedMap(MapSettings mapSettings) {
super(mapSettings);
statsMeter = new StatsMeter(this, STATISTICS_FILE_NAME);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/my/project/simulation/maps/FoldingMap.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package my.project.simulation.maps;

import my.project.gui.config.MapSettings;
import my.project.simulation.enums.MapStrategy;
import my.project.simulation.stats.StatsMeter;
import my.project.simulation.utils.Vector2D;

public class FoldingMap extends AbstractMap {
private static final String STATISTICS_FILE_NAME = "stats-foldingMap.csv";

public FoldingMap(int width, int height, double jungleRatio,
int startEnergy, int moveEnergy, int bushEnergy, int grassEnergy,
int animalsCount) {
super(width, height, jungleRatio, startEnergy, moveEnergy, bushEnergy, grassEnergy, animalsCount);
public FoldingMap(MapSettings mapSettings) {
super(mapSettings);
statsMeter = new StatsMeter(this, STATISTICS_FILE_NAME);
}

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/my/project/simulation/maps/IMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public interface IMap {

long getCurrentDayNum();

void setStrategy(MapStrategy strategy);

Set<List<Integer>> getDominantGenomes();

Set<Animal> getAllAnimals();
Expand Down
35 changes: 10 additions & 25 deletions src/main/resources/fxml/MainBox.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="my.project.gui.controllers.MainController">
<center>
<SplitPane fx:id="mainSplitPane" dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
<SplitPane fx:id="mainSplitPane" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<!-- <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />-->
<!-- <fx:include fx:id="containerLeft" source="ContainerLeft.fxml" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />-->
<!-- </AnchorPane>-->
<!-- <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />-->
<!-- <fx:include fx:id="containerRight" source="ContainerRight.fxml" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />-->
<!-- </AnchorPane>-->
</items>
</SplitPane>
</center>
<bottom>
<HBox BorderPane.alignment="CENTER">
Expand All @@ -26,25 +32,4 @@
</padding>
</HBox>
</bottom>
<top>
<MenuBar BorderPane.alignment="CENTER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</top>
</BorderPane>

0 comments on commit ebf4cec

Please sign in to comment.