mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +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:
@@ -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) {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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