mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
settings
This commit is contained in:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -13,7 +13,7 @@
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_24" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="openjdk-25" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -19,7 +19,11 @@
|
||||
<artifactId>spotless-maven-plugin</artifactId>
|
||||
<version>2.46.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.10.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.toop</groupId>
|
||||
<artifactId>pism_framework</artifactId>
|
||||
|
||||
@@ -10,16 +10,19 @@ import javafx.application.Application;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.layout.StackPane;
|
||||
import javafx.stage.Stage;
|
||||
import org.toop.framework.asset.resources.SettingsAsset;
|
||||
import org.toop.local.AppSettings;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Stack;
|
||||
|
||||
public final class App extends Application {
|
||||
private static Stage stage;
|
||||
private static StackPane root;
|
||||
private static Stack<Layer> stack;
|
||||
|
||||
private static int width;
|
||||
private static int height;
|
||||
private static int height;
|
||||
private static int width;
|
||||
private static SettingsAsset settingsAsset;
|
||||
|
||||
private static boolean isQuitting;
|
||||
|
||||
@@ -29,7 +32,14 @@ public final class App extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws Exception {
|
||||
final StackPane root = new StackPane();
|
||||
|
||||
App.stage = stage;
|
||||
final StackPane root = new StackPane();
|
||||
App.root = root;
|
||||
App.stack = new Stack<>();
|
||||
|
||||
AppSettings settings = new AppSettings();
|
||||
settings.applySettings();
|
||||
|
||||
final Scene scene = new Scene(root);
|
||||
scene.getStylesheets().add(ResourceManager.get(CssAsset.class, "app.css").getUrl());
|
||||
@@ -48,13 +58,8 @@ public final class App extends Application {
|
||||
|
||||
stage.setScene(scene);
|
||||
stage.setResizable(false);
|
||||
|
||||
stage.show();
|
||||
|
||||
App.stage = stage;
|
||||
App.root = root;
|
||||
App.stack = new Stack<>();
|
||||
|
||||
App.width = (int) stage.getWidth();
|
||||
App.height = (int) stage.getHeight();
|
||||
|
||||
|
||||
@@ -6,12 +6,14 @@ import org.toop.app.layer.Layer;
|
||||
import org.toop.app.layer.containers.VerticalContainer;
|
||||
import org.toop.framework.asset.ResourceManager;
|
||||
import org.toop.framework.asset.resources.LocalizationAsset;
|
||||
import org.toop.framework.asset.resources.SettingsAsset;
|
||||
import org.toop.framework.audio.events.AudioEvents;
|
||||
import org.toop.framework.eventbus.EventFlow;
|
||||
import org.toop.local.AppContext;
|
||||
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.control.ChoiceBox;
|
||||
import org.toop.local.AppSettings;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -19,8 +21,11 @@ public final class OptionsLayer extends Layer {
|
||||
private Locale currentLocale = AppContext.getLocale();
|
||||
private LocalizationAsset locale = ResourceManager.get("localization");
|
||||
|
||||
private static int currentVolume = 25;
|
||||
private static boolean isWindowed = true;
|
||||
AppSettings appSettings = new AppSettings();
|
||||
SettingsAsset settings = appSettings.getPath();
|
||||
|
||||
private int currentVolume = settings.getVolume();
|
||||
private boolean isWindowed = !(settings.getFullscreen());
|
||||
|
||||
OptionsLayer() {
|
||||
super("options.css");
|
||||
@@ -61,6 +66,8 @@ public final class OptionsLayer extends Layer {
|
||||
|
||||
AppContext.setLocale(locale);
|
||||
|
||||
settings.setLocale(locale.toLanguageTag());
|
||||
|
||||
this.currentLocale = AppContext.getLocale();
|
||||
this.locale = ResourceManager.get("localization");
|
||||
|
||||
@@ -70,14 +77,14 @@ public final class OptionsLayer extends Layer {
|
||||
for (final Locale localeFile : locale.getAvailableLocales()) {
|
||||
languageBox.getItems().add(localeFile);
|
||||
}
|
||||
|
||||
languageBox.setValue(currentLocale);
|
||||
}
|
||||
|
||||
private void addVolumeSlider(Container container) {
|
||||
container.addSlider(100, currentVolume, (volume) -> {
|
||||
currentVolume = volume;
|
||||
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(volume.doubleValue() / 100.0)).asyncPostEvent();
|
||||
settings.setVolume(volume);
|
||||
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(volume.doubleValue() / 100.0)).asyncPostEvent();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -85,6 +92,7 @@ public final class OptionsLayer extends Layer {
|
||||
container.addToggle("Windowed", "Fullscreen", !isWindowed, (fullscreen) -> {
|
||||
isWindowed = !fullscreen;
|
||||
App.setFullscreen(fullscreen);
|
||||
});
|
||||
settings.setFullscreen(fullscreen);
|
||||
});
|
||||
}
|
||||
}
|
||||
46
app/src/main/java/org/toop/local/AppSettings.java
Normal file
46
app/src/main/java/org/toop/local/AppSettings.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package org.toop.local;
|
||||
|
||||
import org.toop.app.App;
|
||||
import org.toop.framework.asset.resources.SettingsAsset;
|
||||
import org.toop.framework.settings.Settings;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Locale;
|
||||
|
||||
public class AppSettings {
|
||||
|
||||
private SettingsAsset settingsAsset;
|
||||
|
||||
public void applySettings() {
|
||||
SettingsAsset settings = getPath();
|
||||
if (!settings.isLoaded()) {
|
||||
settings.load();
|
||||
}
|
||||
Settings settingsData = settings.getContent();
|
||||
|
||||
AppContext.setLocale(Locale.of(settingsData.locale));
|
||||
App.setFullscreen(settingsData.fullScreen);
|
||||
}
|
||||
|
||||
public SettingsAsset getPath() {
|
||||
if (this.settingsAsset == null) {
|
||||
String os = System.getProperty("os.name").toLowerCase();
|
||||
String basePath;
|
||||
|
||||
if (os.contains("win")) {
|
||||
basePath = System.getenv("APPDATA");
|
||||
if (basePath == null) {
|
||||
basePath = System.getProperty("user.home");
|
||||
}
|
||||
} else if (os.contains("mac")) {
|
||||
basePath = System.getProperty("user.home") + "/Library/Application Support";
|
||||
} else {
|
||||
basePath = System.getProperty("user.home") + "/.config";
|
||||
}
|
||||
|
||||
File settingsFile = new File(basePath + File.separator + "ISY1" + File.separator + "settings.json");
|
||||
this.settingsAsset = new SettingsAsset(settingsFile);
|
||||
}
|
||||
return this.settingsAsset;
|
||||
}
|
||||
}
|
||||
@@ -116,8 +116,14 @@
|
||||
<artifactId>reflections</artifactId>
|
||||
<version>0.10.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.10.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package org.toop.framework.asset.resources;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.toop.framework.asset.types.FileExtension;
|
||||
import org.toop.framework.asset.types.LoadableResource;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
@FileExtension({"json"})
|
||||
public class JsonAsset<T> extends BaseResource implements LoadableResource {
|
||||
|
||||
private T content;
|
||||
private Class<T> type;
|
||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
public JsonAsset(File file, Class<T> type) {
|
||||
super(file);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
File file = getFile();
|
||||
if (!file.exists()) {
|
||||
try {
|
||||
// make a new file with the declared constructor (example: settings) if it doesn't exist
|
||||
content = type.getDeclaredConstructor().newInstance();
|
||||
save();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Could not make default JSON settings for" + file, e);
|
||||
}
|
||||
} else {
|
||||
// else open the file, try reading it using gson, and set it to loaded
|
||||
try (FileReader reader = new FileReader(file)) {
|
||||
content = gson.fromJson(reader, type);
|
||||
this.isLoaded = true;
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException("Failed to load JSON asset" + getFile(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
this.content = null;
|
||||
this.isLoaded = false;
|
||||
}
|
||||
|
||||
public T getContent() {
|
||||
if (!isLoaded()) {
|
||||
load();
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
File file = getFile();
|
||||
File parent = file.getParentFile();
|
||||
if (parent != null && !parent.exists()) {
|
||||
parent.mkdirs();
|
||||
}
|
||||
try (FileWriter writer = new FileWriter(file)) {
|
||||
gson.toJson(content, writer);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to save JSON asset" + getFile(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoaded() {
|
||||
return this.isLoaded;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.toop.framework.asset.resources;
|
||||
|
||||
|
||||
import org.toop.framework.settings.Settings;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Locale;
|
||||
|
||||
public class SettingsAsset extends JsonAsset<Settings> {
|
||||
|
||||
public SettingsAsset(File file) {
|
||||
super(file, Settings.class);
|
||||
}
|
||||
|
||||
public int getVolume() {
|
||||
return getContent().volume;
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return Locale.forLanguageTag(getContent().locale);
|
||||
}
|
||||
|
||||
public boolean getFullscreen() {
|
||||
return getContent().fullScreen;
|
||||
}
|
||||
|
||||
public void setVolume(int volume) {
|
||||
getContent().volume = volume;
|
||||
save();
|
||||
}
|
||||
|
||||
public void setLocale(String locale) {
|
||||
getContent().locale = locale;
|
||||
save();
|
||||
}
|
||||
|
||||
public void setFullscreen(boolean fullscreen) {
|
||||
getContent().fullScreen = fullscreen;
|
||||
save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.toop.framework.settings;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class Settings {
|
||||
public boolean fullScreen = false;
|
||||
public String locale = Locale.getDefault().toLanguageTag();
|
||||
public int volume = 100;
|
||||
}
|
||||
Reference in New Issue
Block a user