commit 2311f65da3662e6865b5933be9b3a6960d477000
parent 52bcd6764df8b7ed35ccafd9b7277b7ce4699975
Author: A Koens <[email protected]>
Date: Tue, 27 Sep 2022 18:27:04 +0200
Update Settings
Settings refactoring, optimized it to allow for easier adding of settings.
Diffstat:
6 files changed, 93 insertions(+), 78 deletions(-)
diff --git a/settings.properties b/settings.properties
@@ -1,5 +1,6 @@
-#Window Settings
-#Tue Sep 13 10:44:53 CEST 2022
-screenWidth=1920
-screenHeight=1080
+#Application Settings
+#Tue Sep 27 17:59:22 CEST 2022
+screenWidth=680
+fullscreen=false
+screenHeight=480
title=ISY Game Client
diff --git a/src/main/java/com/example/isygameclient/Application.java b/src/main/java/com/example/isygameclient/Application.java
@@ -1,6 +1,7 @@
package com.example.isygameclient;
-import com.example.isygameclient.util.Settings;
+import com.example.isygameclient.util.SettingsHandler;
+
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
@@ -11,14 +12,14 @@ public class Application extends javafx.application.Application {
@Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("views/hello-view.fxml"));
- Scene scene = new Scene(fxmlLoader.load(), Settings.getInstance().getScreenWidth(),
- Settings.getInstance().getScreenHeight());
- primaryStage.setTitle(Settings.getInstance().getTitle());
+ Scene scene = new Scene(fxmlLoader.load(), SettingsHandler.getSettings().getScreenWidth(),
+ SettingsHandler.getSettings().getScreenHeight());
+ primaryStage.setTitle(SettingsHandler.getSettings().getTitle());
primaryStage.setScene(scene);
- primaryStage.setFullScreen(true);
+ primaryStage.setFullScreen(SettingsHandler.getSettings().isFullscreen());
primaryStage.show();
primaryStage.setOnCloseRequest((windowEvent) -> {
- Settings.getInstance().saveProperties();
+ SettingsHandler.saveProperties();
});
}
diff --git a/src/main/java/com/example/isygameclient/controllers/HelloController.java b/src/main/java/com/example/isygameclient/controllers/HelloController.java
@@ -1,6 +1,6 @@
package com.example.isygameclient.controllers;
-import com.example.isygameclient.util.Settings;
+import com.example.isygameclient.util.SettingsHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
@@ -15,7 +15,7 @@ public class HelloController {
@FXML
protected void onHelloButtonClick() {
- welcomeText.setText(Settings.getInstance().getTitle());
+ welcomeText.setText(SettingsHandler.getSettings().getTitle());
((Stage)welcomeButton.getScene().getWindow()).setFullScreen(true);
}
}
\ No newline at end of file
diff --git a/src/main/java/com/example/isygameclient/util/Settings.java b/src/main/java/com/example/isygameclient/util/Settings.java
@@ -1,77 +1,13 @@
package com.example.isygameclient.util;
-
import lombok.Data;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Properties;
@Data
public class Settings {
- private static Settings instance;
-
private String title = "ISY Game Client";
+ private boolean fullscreen = true;
private int screenHeight = 1080;
private int screenWidth = 1920;
-
-
- private Settings() {
- loadSettingsFromProperties();
- }
-
- public static Settings getInstance() {
- if (Settings.instance == null) {
- Settings.instance = new Settings();
- }
- return Settings.instance;
- }
-
- private void loadSettingsFromProperties() {
- Properties properties = loadProperties();
- if (properties != null) {
- try {
- this.title = String.valueOf(properties.getProperty("title"));
- this.screenHeight = Integer.parseInt(properties.getProperty("screenHeight"));
- this.screenWidth = Integer.parseInt(properties.getProperty("screenWidth"));
- } catch (NumberFormatException e) {
- System.out.println("ERROR: Unable to load properties");
- e.printStackTrace();
- }
-
- }
- }
-
- private Properties loadProperties() {
- try {
- Properties properties = new Properties();
- InputStream is = new FileInputStream("settings.properties");
- properties.load(is);
- return properties;
- } catch (IOException e) {
- System.out.println("ERROR: Unable to load properties file");
- e.printStackTrace();
- }
- return null;
- }
-
- public void saveProperties() {
- try {
- FileOutputStream fos = new FileOutputStream("settings.properties");
- Properties properties = new Properties();
- properties.put("title", String.valueOf(Settings.getInstance().getTitle()));
- properties.put("screenHeight", String.valueOf(Settings.getInstance().getScreenHeight()));
- properties.put("screenWidth", String.valueOf(Settings.getInstance().getScreenWidth()));
- properties.store(fos, "Window Settings");
- fos.flush();
- fos.close();
-
- } catch (IOException e) {
- System.out.println("ERROR: Unable to save to properties file");
- e.printStackTrace();
- }
- }
}
diff --git a/src/main/java/com/example/isygameclient/util/SettingsHandler.java b/src/main/java/com/example/isygameclient/util/SettingsHandler.java
@@ -0,0 +1,77 @@
+package com.example.isygameclient.util;
+
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+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();
+ }
+ }
+
+}
+
diff --git a/src/test/java/com/example/isygameclient/util/SettingsTest.java b/src/test/java/com/example/isygameclient/util/SettingsTest.java
@@ -8,6 +8,6 @@ class SettingsTest {
@Test
void ShouldGiveTitle() {
- assertEquals(String.class, Settings.getInstance().getTitle().getClass());
+ assertEquals(String.class, SettingsHandler.getSettings().getTitle().getClass());
}
}
\ No newline at end of file