Merge remote-tracking branch 'origin/Settings' into UI

# Conflicts:
#	app/src/main/java/org/toop/app/App.java
#	app/src/main/java/org/toop/app/layer/layers/OptionsLayer.java
#	framework/src/main/java/org/toop/framework/audio/SoundManager.java
This commit is contained in:
ramollia
2025-10-05 17:03:09 +02:00
11 changed files with 227 additions and 14 deletions

2
.idea/misc.xml generated
View File

@@ -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>

View File

@@ -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>

View File

@@ -13,16 +13,21 @@ 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.framework.audio.events.AudioEvents;
import org.toop.framework.eventbus.EventFlow;
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 width;
private static SettingsAsset settingsAsset;
private static boolean isQuitting;
@@ -32,7 +37,14 @@ public final class App extends Application {
@Override
public void start(Stage stage) throws Exception {
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());
@@ -63,7 +75,6 @@ public final class App extends Application {
App.isQuitting = false;
new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent();
activate(new MainLayer());
}

View File

@@ -4,18 +4,28 @@ import org.toop.app.App;
import org.toop.app.layer.Container;
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;
public final class OptionsLayer extends Layer {
private static int currentVolume = 0;
private static boolean isWindowed = true;
private Locale currentLocale = AppContext.getLocale();
private LocalizationAsset locale = ResourceManager.get("localization");
AppSettings appSettings = new AppSettings();
SettingsAsset settings = appSettings.getPath();
private int currentVolume = settings.getVolume();
private boolean isWindowed = !(settings.getFullscreen());
OptionsLayer() {
super("options.css");
@@ -59,6 +69,10 @@ public final class OptionsLayer extends Layer {
}
AppContext.setLocale(locale);
this.currentLocale = AppContext.getLocale();
this.locale = ResourceManager.get("localization");
App.reloadAll();
});
@@ -79,11 +93,13 @@ public final class OptionsLayer extends Layer {
});
languageBox.setValue(AppContext.getLocale());
languageBox.setValue(currentLocale);
}
private void addVolumeSlider(Container container) {
container.addSlider(100, currentVolume, (volume) -> {
currentVolume = volume;
settings.setVolume(volume);
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(volume.doubleValue())).asyncPostEvent();
});
}
@@ -92,6 +108,7 @@ public final class OptionsLayer extends Layer {
container.addToggle(AppContext.getString("windowed"), AppContext.getString("fullscreen"), !isWindowed, (fullscreen) -> {
isWindowed = !fullscreen;
App.setFullscreen(fullscreen);
settings.setFullscreen(fullscreen);
});
}
}

View File

@@ -0,0 +1,50 @@
package org.toop.local;
import org.toop.app.App;
import org.toop.framework.asset.resources.SettingsAsset;
import org.toop.framework.audio.events.AudioEvents;
import org.toop.framework.eventbus.EventFlow;
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);
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(settingsData.volume)).asyncPostEvent();
}
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;
}
}

Binary file not shown.

View File

@@ -116,6 +116,12 @@
<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>

View File

@@ -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;
}
}

View File

@@ -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();
}
}

View File

@@ -69,7 +69,7 @@ public class SoundManager {
}
private void handleVolumeChange(AudioEvents.ChangeVolume event) {
double newVolume = event.newVolume() / 100.0;
double newVolume = event.newVolume() / 100;
if (newVolume > 1.0) this.volume = 1.0;
else this.volume = Math.max(newVolume, 0.0);
for (MediaPlayer mediaPlayer : this.activeMusic) {
@@ -78,7 +78,7 @@ public class SoundManager {
}
private void handleGetCurrentVolume(AudioEvents.GetCurrentVolume event) {
new EventFlow().addPostEvent(new AudioEvents.GetCurrentVolumeReponse(volume * 100.0, event.snowflakeId()))
new EventFlow().addPostEvent(new AudioEvents.GetCurrentVolumeReponse(volume * 100, event.snowflakeId()))
.asyncPostEvent();
}

View File

@@ -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;
}