fix(AudioController, OptionsAudioTab): optimize audio options handling and improve state management
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>de.extio</groupId>
|
<groupId>de.extio</groupId>
|
||||||
<artifactId>game-engine</artifactId>
|
<artifactId>game-engine</artifactId>
|
||||||
<version>1.0.26</version>
|
<version>1.0.27</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java.version>25</java.version>
|
<java.version>25</java.version>
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import de.extio.game_engine.storage.StorageService;
|
|||||||
|
|
||||||
public final class AudioController implements AudioControl {
|
public final class AudioController implements AudioControl {
|
||||||
|
|
||||||
|
private static final long AUDIO_OPTIONS_STORE_DELAY_MS = 333;
|
||||||
|
|
||||||
private final StorageService storageService;
|
private final StorageService storageService;
|
||||||
|
|
||||||
private final AudioOptions audioOptions; // Passed as reference to strategies
|
private final AudioOptions audioOptions; // Passed as reference to strategies
|
||||||
@@ -29,6 +31,8 @@ public final class AudioController implements AudioControl {
|
|||||||
|
|
||||||
private boolean useSoftwareMixing;
|
private boolean useSoftwareMixing;
|
||||||
|
|
||||||
|
private long delayedAudioOptionsStoreAt = -1;
|
||||||
|
|
||||||
public AudioController(final StaticResourceService resourceService, final StorageService storageService) {
|
public AudioController(final StaticResourceService resourceService, final StorageService storageService) {
|
||||||
this.storageService = storageService;
|
this.storageService = storageService;
|
||||||
this.audioOptions = new AudioOptions();
|
this.audioOptions = new AudioOptions();
|
||||||
@@ -75,9 +79,15 @@ public final class AudioController implements AudioControl {
|
|||||||
if (this.audioStrategy != null) {
|
if (this.audioStrategy != null) {
|
||||||
this.audioStrategy.run();
|
this.audioStrategy.run();
|
||||||
}
|
}
|
||||||
|
if (this.consumeDelayedAudioOptionsStore()) {
|
||||||
|
this.storeAudioOptions();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
|
if (this.consumePendingAudioOptionsStore()) {
|
||||||
|
this.storeAudioOptions();
|
||||||
|
}
|
||||||
this.audioControllerThread.interrupt();
|
this.audioControllerThread.interrupt();
|
||||||
this.stopStrategy();
|
this.stopStrategy();
|
||||||
}
|
}
|
||||||
@@ -169,9 +179,25 @@ public final class AudioController implements AudioControl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void applyAudioOptions(final AudioOptions audioOptions) {
|
public synchronized void applyAudioOptions(final AudioOptions audioOptions) {
|
||||||
this.audioOptions.apply(audioOptions);
|
this.audioOptions.apply(audioOptions);
|
||||||
this.storeAudioOptions();
|
this.delayedAudioOptionsStoreAt = System.currentTimeMillis() + AUDIO_OPTIONS_STORE_DELAY_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized boolean consumeDelayedAudioOptionsStore() {
|
||||||
|
if (this.delayedAudioOptionsStoreAt < 0 || System.currentTimeMillis() < this.delayedAudioOptionsStoreAt) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.delayedAudioOptionsStoreAt = -1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized boolean consumePendingAudioOptionsStore() {
|
||||||
|
if (this.delayedAudioOptionsStoreAt < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this.delayedAudioOptionsStoreAt = -1;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<StaticResource> wrapOpener(final StaticResource opener, final List<StaticResource> audioFileNames) {
|
private List<StaticResource> wrapOpener(final StaticResource opener, final List<StaticResource> audioFileNames) {
|
||||||
|
|||||||
@@ -63,7 +63,6 @@ final class AudioStrategyJavaMixer implements AudioStrategy {
|
|||||||
.name("Audio Music")
|
.name("Audio Music")
|
||||||
.daemon(true)
|
.daemon(true)
|
||||||
.start(musicAudioPlayer);
|
.start(musicAudioPlayer);
|
||||||
musicThread.start();
|
|
||||||
this.musicAudioPlayer = new AudioThread(musicThread, musicAudioPlayer);
|
this.musicAudioPlayer = new AudioThread(musicThread, musicAudioPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -171,32 +171,48 @@ final class OptionsAudioTab extends AbstractOptionsTab implements OptionsTab {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final var options = this.audioControl().getAudioOptions();
|
||||||
|
|
||||||
switch (controlId) {
|
switch (controlId) {
|
||||||
case "OptionsModule_Audio_SFX_Enable" -> {
|
case "OptionsModule_Audio_SFX_Enable" -> {
|
||||||
final var currentValue = this.audioControl().getAudioOptions().getSfxOptions().isEnable();
|
final var currentValue = options.getSfxOptions().isEnable();
|
||||||
this.audioControl().getAudioOptions().getSfxOptions().setEnable(!currentValue);
|
options.getSfxOptions().setEnable(!currentValue);
|
||||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
this.audioControl().applyAudioOptions(options);
|
||||||
this.refreshContent(context);
|
this.refreshContent(context);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case "OptionsModule_Audio_Music_Enable" -> {
|
case "OptionsModule_Audio_Music_Enable" -> {
|
||||||
final var currentValue = this.audioControl().getAudioOptions().getMusicOptions().isEnable();
|
final var currentValue = options.getMusicOptions().isEnable();
|
||||||
this.audioControl().getAudioOptions().getMusicOptions().setEnable(!currentValue);
|
options.getMusicOptions().setEnable(!currentValue);
|
||||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
this.audioControl().applyAudioOptions(options);
|
||||||
this.refreshContent(context);
|
this.refreshContent(context);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case "OptionsModule_Audio_SFX_SoftwareMixing" -> {
|
case "OptionsModule_Audio_SFX_SoftwareMixing" -> {
|
||||||
final var currentValue = this.audioControl().getAudioOptions().getSfxOptions().isSoftwareMixing();
|
final var currentValue = options.getSfxOptions().isSoftwareMixing();
|
||||||
this.audioControl().getAudioOptions().getSfxOptions().setSoftwareMixing(!currentValue);
|
options.getSfxOptions().setSoftwareMixing(!currentValue);
|
||||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
this.audioControl().applyAudioOptions(options);
|
||||||
this.refreshContent(context);
|
this.refreshContent(context);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
case "OptionsModule_Audio_Music_SoftwareMixing" -> {
|
case "OptionsModule_Audio_Music_SoftwareMixing" -> {
|
||||||
final var currentValue = this.audioControl().getAudioOptions().getMusicOptions().isSoftwareMixing();
|
final var currentValue = options.getMusicOptions().isSoftwareMixing();
|
||||||
this.audioControl().getAudioOptions().getMusicOptions().setSoftwareMixing(!currentValue);
|
options.getMusicOptions().setSoftwareMixing(!currentValue);
|
||||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
this.audioControl().applyAudioOptions(options);
|
||||||
|
this.refreshContent(context);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case "OptionsModule_Audio_SFX_Volume_Slider" -> {
|
||||||
|
final var newVolume = (double) event.getPayload();
|
||||||
|
options.getSfxOptions().setVolume(newVolume);
|
||||||
|
this.audioControl().applyAudioOptions(options);
|
||||||
|
this.refreshContent(context);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
case "OptionsModule_Audio_Music_Volume_Slider" -> {
|
||||||
|
final var newVolume = (double) event.getPayload();
|
||||||
|
options.getMusicOptions().setVolume(newVolume);
|
||||||
|
this.audioControl().applyAudioOptions(options);
|
||||||
this.refreshContent(context);
|
this.refreshContent(context);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user