mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
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:
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@@ -13,7 +13,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
||||||
@@ -19,7 +19,11 @@
|
|||||||
<artifactId>spotless-maven-plugin</artifactId>
|
<artifactId>spotless-maven-plugin</artifactId>
|
||||||
<version>2.46.1</version>
|
<version>2.46.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.10.1</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.toop</groupId>
|
<groupId>org.toop</groupId>
|
||||||
<artifactId>pism_framework</artifactId>
|
<artifactId>pism_framework</artifactId>
|
||||||
|
|||||||
@@ -13,16 +13,21 @@ import javafx.application.Application;
|
|||||||
import javafx.scene.Scene;
|
import javafx.scene.Scene;
|
||||||
import javafx.scene.layout.StackPane;
|
import javafx.scene.layout.StackPane;
|
||||||
import javafx.stage.Stage;
|
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;
|
import java.util.Stack;
|
||||||
|
|
||||||
public final class App extends Application {
|
public final class App extends Application {
|
||||||
private static Stage stage;
|
private static Stage stage;
|
||||||
private static StackPane root;
|
private static StackPane root;
|
||||||
private static Stack<Layer> stack;
|
private static Stack<Layer> stack;
|
||||||
|
private static int height;
|
||||||
private static int width;
|
private static int width;
|
||||||
private static int height;
|
private static SettingsAsset settingsAsset;
|
||||||
|
|
||||||
private static boolean isQuitting;
|
private static boolean isQuitting;
|
||||||
|
|
||||||
@@ -32,7 +37,14 @@ public final class App extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws Exception {
|
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);
|
final Scene scene = new Scene(root);
|
||||||
scene.getStylesheets().add(ResourceManager.get(CssAsset.class, "app.css").getUrl());
|
scene.getStylesheets().add(ResourceManager.get(CssAsset.class, "app.css").getUrl());
|
||||||
@@ -63,7 +75,6 @@ public final class App extends Application {
|
|||||||
|
|
||||||
App.isQuitting = false;
|
App.isQuitting = false;
|
||||||
|
|
||||||
new EventFlow().addPostEvent(new AudioEvents.StartBackgroundMusic()).asyncPostEvent();
|
|
||||||
activate(new MainLayer());
|
activate(new MainLayer());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,18 +4,28 @@ import org.toop.app.App;
|
|||||||
import org.toop.app.layer.Container;
|
import org.toop.app.layer.Container;
|
||||||
import org.toop.app.layer.Layer;
|
import org.toop.app.layer.Layer;
|
||||||
import org.toop.app.layer.containers.VerticalContainer;
|
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.audio.events.AudioEvents;
|
||||||
import org.toop.framework.eventbus.EventFlow;
|
import org.toop.framework.eventbus.EventFlow;
|
||||||
import org.toop.local.AppContext;
|
import org.toop.local.AppContext;
|
||||||
|
|
||||||
import javafx.geometry.Pos;
|
import javafx.geometry.Pos;
|
||||||
import javafx.scene.control.ChoiceBox;
|
import javafx.scene.control.ChoiceBox;
|
||||||
|
import org.toop.local.AppSettings;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public final class OptionsLayer extends Layer {
|
public final class OptionsLayer extends Layer {
|
||||||
private static int currentVolume = 0;
|
private Locale currentLocale = AppContext.getLocale();
|
||||||
private static boolean isWindowed = true;
|
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() {
|
OptionsLayer() {
|
||||||
super("options.css");
|
super("options.css");
|
||||||
@@ -59,6 +69,10 @@ public final class OptionsLayer extends Layer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AppContext.setLocale(locale);
|
AppContext.setLocale(locale);
|
||||||
|
|
||||||
|
this.currentLocale = AppContext.getLocale();
|
||||||
|
this.locale = ResourceManager.get("localization");
|
||||||
|
|
||||||
App.reloadAll();
|
App.reloadAll();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,12 +93,14 @@ public final class OptionsLayer extends Layer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
languageBox.setValue(AppContext.getLocale());
|
languageBox.setValue(AppContext.getLocale());
|
||||||
|
languageBox.setValue(currentLocale);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addVolumeSlider(Container container) {
|
private void addVolumeSlider(Container container) {
|
||||||
container.addSlider(100, currentVolume, (volume) -> {
|
container.addSlider(100, currentVolume, (volume) -> {
|
||||||
currentVolume = volume;
|
currentVolume = volume;
|
||||||
new EventFlow().addPostEvent(new AudioEvents.ChangeVolume(volume.doubleValue())).asyncPostEvent();
|
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) -> {
|
container.addToggle(AppContext.getString("windowed"), AppContext.getString("fullscreen"), !isWindowed, (fullscreen) -> {
|
||||||
isWindowed = !fullscreen;
|
isWindowed = !fullscreen;
|
||||||
App.setFullscreen(fullscreen);
|
App.setFullscreen(fullscreen);
|
||||||
});
|
settings.setFullscreen(fullscreen);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
50
app/src/main/java/org/toop/local/AppSettings.java
Normal file
50
app/src/main/java/org/toop/local/AppSettings.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
app/src/main/resources/assets/audio/music/gladius.mp3
Normal file
BIN
app/src/main/resources/assets/audio/music/gladius.mp3
Normal file
Binary file not shown.
@@ -116,8 +116,14 @@
|
|||||||
<artifactId>reflections</artifactId>
|
<artifactId>reflections</artifactId>
|
||||||
<version>0.10.2</version>
|
<version>0.10.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.10.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -69,7 +69,7 @@ public class SoundManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleVolumeChange(AudioEvents.ChangeVolume event) {
|
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;
|
if (newVolume > 1.0) this.volume = 1.0;
|
||||||
else this.volume = Math.max(newVolume, 0.0);
|
else this.volume = Math.max(newVolume, 0.0);
|
||||||
for (MediaPlayer mediaPlayer : this.activeMusic) {
|
for (MediaPlayer mediaPlayer : this.activeMusic) {
|
||||||
@@ -78,7 +78,7 @@ public class SoundManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void handleGetCurrentVolume(AudioEvents.GetCurrentVolume event) {
|
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();
|
.asyncPostEvent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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