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>
|
||||
<groupId>de.extio</groupId>
|
||||
<artifactId>game-engine</artifactId>
|
||||
<version>1.0.26</version>
|
||||
<version>1.0.27</version>
|
||||
|
||||
<properties>
|
||||
<java.version>25</java.version>
|
||||
|
||||
@@ -9,6 +9,8 @@ import de.extio.game_engine.storage.StorageService;
|
||||
|
||||
public final class AudioController implements AudioControl {
|
||||
|
||||
private static final long AUDIO_OPTIONS_STORE_DELAY_MS = 333;
|
||||
|
||||
private final StorageService storageService;
|
||||
|
||||
private final AudioOptions audioOptions; // Passed as reference to strategies
|
||||
@@ -29,6 +31,8 @@ public final class AudioController implements AudioControl {
|
||||
|
||||
private boolean useSoftwareMixing;
|
||||
|
||||
private long delayedAudioOptionsStoreAt = -1;
|
||||
|
||||
public AudioController(final StaticResourceService resourceService, final StorageService storageService) {
|
||||
this.storageService = storageService;
|
||||
this.audioOptions = new AudioOptions();
|
||||
@@ -75,9 +79,15 @@ public final class AudioController implements AudioControl {
|
||||
if (this.audioStrategy != null) {
|
||||
this.audioStrategy.run();
|
||||
}
|
||||
if (this.consumeDelayedAudioOptionsStore()) {
|
||||
this.storeAudioOptions();
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
if (this.consumePendingAudioOptionsStore()) {
|
||||
this.storeAudioOptions();
|
||||
}
|
||||
this.audioControllerThread.interrupt();
|
||||
this.stopStrategy();
|
||||
}
|
||||
@@ -169,9 +179,25 @@ public final class AudioController implements AudioControl {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyAudioOptions(final AudioOptions audioOptions) {
|
||||
public synchronized void applyAudioOptions(final AudioOptions 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) {
|
||||
|
||||
@@ -63,7 +63,6 @@ final class AudioStrategyJavaMixer implements AudioStrategy {
|
||||
.name("Audio Music")
|
||||
.daemon(true)
|
||||
.start(musicAudioPlayer);
|
||||
musicThread.start();
|
||||
this.musicAudioPlayer = new AudioThread(musicThread, musicAudioPlayer);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,32 +171,48 @@ final class OptionsAudioTab extends AbstractOptionsTab implements OptionsTab {
|
||||
return false;
|
||||
}
|
||||
|
||||
final var options = this.audioControl().getAudioOptions();
|
||||
|
||||
switch (controlId) {
|
||||
case "OptionsModule_Audio_SFX_Enable" -> {
|
||||
final var currentValue = this.audioControl().getAudioOptions().getSfxOptions().isEnable();
|
||||
this.audioControl().getAudioOptions().getSfxOptions().setEnable(!currentValue);
|
||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
||||
final var currentValue = options.getSfxOptions().isEnable();
|
||||
options.getSfxOptions().setEnable(!currentValue);
|
||||
this.audioControl().applyAudioOptions(options);
|
||||
this.refreshContent(context);
|
||||
return true;
|
||||
}
|
||||
case "OptionsModule_Audio_Music_Enable" -> {
|
||||
final var currentValue = this.audioControl().getAudioOptions().getMusicOptions().isEnable();
|
||||
this.audioControl().getAudioOptions().getMusicOptions().setEnable(!currentValue);
|
||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
||||
final var currentValue = options.getMusicOptions().isEnable();
|
||||
options.getMusicOptions().setEnable(!currentValue);
|
||||
this.audioControl().applyAudioOptions(options);
|
||||
this.refreshContent(context);
|
||||
return true;
|
||||
}
|
||||
case "OptionsModule_Audio_SFX_SoftwareMixing" -> {
|
||||
final var currentValue = this.audioControl().getAudioOptions().getSfxOptions().isSoftwareMixing();
|
||||
this.audioControl().getAudioOptions().getSfxOptions().setSoftwareMixing(!currentValue);
|
||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
||||
final var currentValue = options.getSfxOptions().isSoftwareMixing();
|
||||
options.getSfxOptions().setSoftwareMixing(!currentValue);
|
||||
this.audioControl().applyAudioOptions(options);
|
||||
this.refreshContent(context);
|
||||
return true;
|
||||
}
|
||||
case "OptionsModule_Audio_Music_SoftwareMixing" -> {
|
||||
final var currentValue = this.audioControl().getAudioOptions().getMusicOptions().isSoftwareMixing();
|
||||
this.audioControl().getAudioOptions().getMusicOptions().setSoftwareMixing(!currentValue);
|
||||
this.audioControl().applyAudioOptions(this.audioControl().getAudioOptions());
|
||||
final var currentValue = options.getMusicOptions().isSoftwareMixing();
|
||||
options.getMusicOptions().setSoftwareMixing(!currentValue);
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user