hanze/game-client

Squashed commit of the following: (5e3986e678c7e2d2f4d0b11057a7bf3ecbaecd46)
Repositories

commit 5e3986e678c7e2d2f4d0b11057a7bf3ecbaecd46
parent 09424e8c51e8babc32d9112c6b9a36c2a1027428
Author: A Koens <arjan@koens.biz>
Date:   Sun, 23 Oct 2022 16:00:19 +0200

Squashed commit of the following:

commit ca58fe04c9b92c1ce755b5097e167fe35dc830e2
Author: A Koens <43842651+Akoens@users.noreply.github.com>
Date:   Sun Oct 23 15:59:33 2022 +0200

    Delete settings.json

commit de55c3ac0079212341da705643bf9458f6904ea1
Author: A Koens <arjan@koens.biz>
Date:   Sun Oct 23 15:58:17 2022 +0200

    Update .gitignore

commit 56eec25f6d63ab2c648740371414c827cca5addc
Author: A Koens <arjan@koens.biz>
Date:   Sun Oct 23 15:57:26 2022 +0200

    Changes default Settings

    Changes the way default settings are handled.

commit 8c1ccc951cd6f403d9346217bfaa3d7459dfb3d2
Author: A Koens <arjan@koens.biz>
Date:   Sun Oct 23 15:49:28 2022 +0200

    New Settings System

    Added new Settings handling using Gson.

Diffstat:
M.gitignore3+++
Mpom.xml11+++++------
Dsettings.json0
Dsettings.properties6------
Msrc/main/java/module-info.java6+++++-
Msrc/main/java/nl/isygameclient/Application.java32++++++++++++++++++++++++++++----
Dsrc/main/java/nl/isygameclient/models/SimpleCalculator.java8--------
Dsrc/main/java/nl/isygameclient/util/Save.java12------------
Dsrc/main/java/nl/isygameclient/util/SaveHandler.java14--------------
Msrc/main/java/nl/isygameclient/util/Settings.java16++++++++--------
Msrc/main/java/nl/isygameclient/util/SettingsHandler.java109++++++++++++++++++++++++++++---------------------------------------------------
Dsrc/test/java/nl/isygameclient/models/SimpleCalculatorTest.java20--------------------
Dsrc/test/java/nl/isygameclient/util/SettingsTest.java14--------------
13 files changed, 88 insertions(+), 163 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -36,6 +36,9 @@ target/* *.tar.gz *.rar +# Ignore Settings +settings.json + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* diff --git a/pom.xml b/pom.xml @@ -28,7 +28,7 @@ <dependency> <groupId>org.controlsfx</groupId> <artifactId>controlsfx</artifactId> - <version>11.1.1</version> + <version>11.1.2</version> </dependency> <!-- JavaFX Styling Dependencies --> @@ -59,12 +59,11 @@ <version>4.7.0-9.1.2</version> </dependency> - <!-- JSON Dependencies --> - <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple --> + <!-- GSON Dependencies --> <dependency> - <groupId>com.googlecode.json-simple</groupId> - <artifactId>json-simple</artifactId> - <version>1.1.1</version> + <groupId>com.google.code.gson</groupId> + <artifactId>gson</artifactId> + <version>2.9.0</version> </dependency> diff --git a/settings.json b/settings.json diff --git a/settings.properties b/settings.properties @@ -1,6 +0,0 @@ -#Application Settings -#Mon Oct 17 20:12:28 CEST 2022 -screenWidth=680 -fullscreen=false -screenHeight=480 -title=ISY Game Client diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java @@ -7,11 +7,15 @@ module nl.isygameclient { requires de.jensd.fx.glyphs.commons; requires com.jfoenix; + + requires static lombok; + requires com.google.gson; - exports nl.isygameclient; + exports nl.isygameclient; exports nl.isygameclient.controllers; exports nl.isygameclient.models; opens nl.isygameclient to javafx.fxml; + opens nl.isygameclient.util to com.google.gson; opens nl.isygameclient.controllers to javafx.fxml; } \ No newline at end of file diff --git a/src/main/java/nl/isygameclient/Application.java b/src/main/java/nl/isygameclient/Application.java @@ -3,23 +3,47 @@ package nl.isygameclient; import java.io.IOException; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; -import javafx.scene.paint.Color; import javafx.stage.Stage; +import nl.isygameclient.util.Settings; +import nl.isygameclient.util.SettingsHandler; public class Application extends javafx.application.Application { + private static final String SETTINGS_FILENAME = "settings.json"; + private Stage primaryStage; + @Override public void start(Stage stage) throws IOException { primaryStage = stage; + + // Load Settings + SettingsHandler settingsHandler = new SettingsHandler(); + Settings settings = settingsHandler.load(SETTINGS_FILENAME); + + // Load Scene FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("views/TicTacToeGame.fxml")); - Scene scene = new Scene(fxmlLoader.load(), Color.TRANSPARENT); + Scene scene = new Scene(fxmlLoader.load()); primaryStage.setScene(scene); - primaryStage.show(); + + // Set Initial Values + primaryStage.setTitle(settings.title); + primaryStage.setFullScreen(settings.isFullScreen); + primaryStage.setMaximized(settings.isMaximized); + primaryStage.setWidth(settings.screenWidth); + primaryStage.setHeight(settings.screenHeight); + + // Save Values on Close primaryStage.setOnCloseRequest((windowEvent) -> { -// SettingsHandler.saveProperties(); + settings.screenWidth = primaryStage.getWidth(); + settings.screenHeight = primaryStage.getHeight(); + settings.isFullScreen = primaryStage.isFullScreen(); + settings.isMaximized = primaryStage.isMaximized(); + settingsHandler.save(SETTINGS_FILENAME, settings); }); + + primaryStage.show(); } public Stage getPrimaryStage() { diff --git a/src/main/java/nl/isygameclient/models/SimpleCalculator.java b/src/main/java/nl/isygameclient/models/SimpleCalculator.java @@ -1,8 +0,0 @@ -package nl.isygameclient.models; - -public class SimpleCalculator { - - public int add(int numberA, int numberB) { - return numberA + numberB; - } -} diff --git a/src/main/java/nl/isygameclient/util/Save.java b/src/main/java/nl/isygameclient/util/Save.java @@ -1,12 +0,0 @@ -package nl.isygameclient.util; - -import java.util.List; - -public class Save { - - private String title; - private boolean fullscreen; - private int screenHeight; - private int screenWidth; - private List<String> games; -} diff --git a/src/main/java/nl/isygameclient/util/SaveHandler.java b/src/main/java/nl/isygameclient/util/SaveHandler.java @@ -1,14 +0,0 @@ -package nl.isygameclient.util; - -public abstract class SaveHandler { - - private Save save; - - public void save(){ - - } - - public void load() { - - } -} diff --git a/src/main/java/nl/isygameclient/util/Settings.java b/src/main/java/nl/isygameclient/util/Settings.java @@ -1,14 +1,14 @@ package nl.isygameclient.util; -import lombok.Data; +import java.util.ArrayList; +import java.util.Arrays; -@Data public class Settings { - - private String title = "ISY Game Client"; - private boolean fullscreen = true; - private int screenHeight = 1080; - private int screenWidth = 1920; - + public String title = "ISY Game Client"; + public boolean isFullScreen = true; + public boolean isMaximized = false; + public double screenHeight = 680.0; + public double screenWidth = 480.0; + public ArrayList<String> games = new ArrayList<>(Arrays.asList("TicTacToe", "Othello", "Starvation")); } diff --git a/src/main/java/nl/isygameclient/util/SettingsHandler.java b/src/main/java/nl/isygameclient/util/SettingsHandler.java @@ -1,75 +1,44 @@ package nl.isygameclient.util; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; -import java.io.FileInputStream; -import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.lang.reflect.Field; -import java.util.Properties; - -public abstract class SettingsHandler { - - private static Settings settings; - - public static Settings getSettings() { - if (settings == null) { - settings = new Settings(); - SettingsHandler.loadPropertiesIntoSettings(); - } - return settings; - } - - private static Properties loadPropertiesFromFile() throws IOException { - Properties properties = new Properties(); - InputStream is = new FileInputStream("settings.properties"); - properties.load(is); - return properties; - } - - public static void loadPropertiesIntoSettings() { - try { - Properties properties = loadPropertiesFromFile(); - Field[] fields = settings.getClass().getDeclaredFields(); - for (Field field : fields) { - field.setAccessible(true); - Object value; - if (field.getType().equals(String.class)) { - value = String.valueOf(properties.getProperty(field.getName())); - } else if (field.getType().equals(int.class)) { - value = Integer.parseInt(properties.getProperty(field.getName())); - } else if (field.getType().equals(float.class)) { - value = Float.parseFloat(properties.getProperty(field.getName())); - } else if (field.getType().equals(boolean.class)) { - value = Boolean.parseBoolean(properties.getProperty(field.getName())); - } else { - return; - } - field.set(settings, value); - field.setAccessible(false); - } - } catch (IllegalAccessException | NumberFormatException | IOException e) { - System.out.println("ERROR: Unable to load properties from file"); - e.printStackTrace(); - } - } - - public static void saveProperties() { - try { - FileOutputStream fos = new FileOutputStream("settings.properties"); - Properties properties = new Properties(); - Field[] fields = settings.getClass().getDeclaredFields(); - for (Field field : fields) { - field.setAccessible(true); - properties.put(field.getName(), String.valueOf(field.get(settings))); - } - properties.store(fos, "Application Settings"); - fos.flush(); - fos.close(); - - } catch (IOException | IllegalAccessException e) { - System.out.println("ERROR: Unable to save to properties file"); - e.printStackTrace(); - } - } +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; + +public class SettingsHandler { + + public void save(String filename, Settings settings) { + try { + Gson gson = new GsonBuilder() + .setPrettyPrinting() + .create(); + + FileWriter writer = new FileWriter(filename); + writer.write(gson.toJson(settings)); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public Settings load(String filename) { + try { + Gson gson = new GsonBuilder() + .setPrettyPrinting() + .create(); + + String inFile = new String(Files.readAllBytes(Paths.get(filename))); + return gson.fromJson(inFile, Settings.class); + } catch (IOException e) { + e.printStackTrace(); + System.err.println("Could Not Load Settings File, Using Defaults Instead"); + return new Settings(); + } + } } + diff --git a/src/test/java/nl/isygameclient/models/SimpleCalculatorTest.java b/src/test/java/nl/isygameclient/models/SimpleCalculatorTest.java @@ -1,19 +0,0 @@ -package nl.isygameclient.models; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class SimpleCalculatorTest { - @Test - void twoPlusTwoShouldEqualFour() { - var calculator = new SimpleCalculator(); - assertEquals(4, calculator.add(2, 2)); - } - - @Test - void ThreePlusSevenShouldEqualTen() { - var calculator = new SimpleCalculator(); - assertEquals(10, calculator.add(3, 7)); - } -} -\ No newline at end of file diff --git a/src/test/java/nl/isygameclient/util/SettingsTest.java b/src/test/java/nl/isygameclient/util/SettingsTest.java @@ -1,13 +0,0 @@ -package nl.isygameclient.util; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.Test; - -class SettingsTest { - - @Test - void ShouldGiveTitle() { - assertEquals(String.class, SettingsHandler.getSettings().getTitle().getClass()); - } -} -\ No newline at end of file