Add a music volume slider (#135)

This commit is contained in:
Stef
2025-10-07 11:47:57 +02:00
committed by GitHub
parent 58269e917a
commit 3af182b986
7 changed files with 80 additions and 6 deletions

View File

@@ -20,6 +20,10 @@ public class SettingsAsset extends JsonAsset<Settings> {
return getContent().fxVolume;
}
public int getMusicVolume() {
return getContent().musicVolume;
}
public Locale getLocale() {
return Locale.forLanguageTag(getContent().locale);
}
@@ -46,6 +50,11 @@ public class SettingsAsset extends JsonAsset<Settings> {
save();
}
public void setMusicVolume(int musicVolume) {
getContent().musicVolume = musicVolume;
save();
}
public void setLocale(String locale) {
getContent().locale = locale;
save();

View File

@@ -1,5 +1,6 @@
package org.toop.framework.audio;
import com.sun.scenario.Settings;
import javafx.scene.media.MediaPlayer;
import org.toop.framework.audio.events.AudioEvents;
import org.toop.framework.eventbus.EventFlow;
@@ -12,6 +13,7 @@ public class AudioVolumeManager {
private double volume = 1.0;
private double fxVolume = 1.0;
private double musicVolume = 1.0;
public AudioVolumeManager(SoundManager soundManager){
this.sM = soundManager;
@@ -19,12 +21,15 @@ public class AudioVolumeManager {
new EventFlow()
.listen(this::handleVolumeChange)
.listen(this::handleFxVolumeChange)
.listen(this::handleMusicVolumeChange)
.listen(this::handleGetCurrentVolume)
.listen(this::handleGetCurrentFxVolume);
.listen(this::handleGetCurrentFxVolume)
.listen(this::handleGetCurrentMusicVolume);
}
public void updateMusicVolume(MediaPlayer mediaPlayer){
mediaPlayer.setVolume(this.volume);
mediaPlayer.setVolume(this.musicVolume * this.volume);
}
public void updateSoundEffectVolume(Clip clip){
@@ -32,7 +37,7 @@ public class AudioVolumeManager {
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float min = volumeControl.getMinimum();
float max = volumeControl.getMaximum();
float dB = (float) (Math.log10(Math.max(fxVolume, 0.0001)) * 20.0); // convert linear to dB
float dB = (float) (Math.log10(Math.max(this.fxVolume * this.volume, 0.0001)) * 20.0); // convert linear to dB
dB = Math.max(min, Math.min(max, dB));
volumeControl.setValue(dB);
}
@@ -55,6 +60,18 @@ public class AudioVolumeManager {
for (MediaPlayer mediaPlayer : sM.getActiveMusic()) {
this.updateMusicVolume(mediaPlayer);
}
for (Clip clip : sM.getActiveSoundEffects().values()){
updateSoundEffectVolume(clip);
}
}
private void handleMusicVolumeChange(AudioEvents.ChangeMusicVolume event){
this.musicVolume = limitVolume(event.newVolume() / 100);
System.out.println(this.musicVolume);
System.out.println(this.volume);
for (MediaPlayer mediaPlayer : sM.getActiveMusic()){
this.updateMusicVolume(mediaPlayer);
}
}
private void handleGetCurrentVolume(AudioEvents.GetCurrentVolume event) {
@@ -66,4 +83,9 @@ public class AudioVolumeManager {
new EventFlow().addPostEvent(new AudioEvents.GetCurrentFxVolumeResponse(fxVolume * 100, event.snowflakeId()))
.asyncPostEvent();
}
private void handleGetCurrentMusicVolume(AudioEvents.GetCurrentMusicVolume event){
new EventFlow().addPostEvent(new AudioEvents.GetCurrentMusicVolumeResponse(musicVolume * 100, event.snowflakeId()))
.asyncPostEvent();
}
}

View File

@@ -16,6 +16,8 @@ public class AudioEvents extends EventsBase {
public record StartBackgroundMusic() implements EventWithoutSnowflake {}
public record ChangeVolume(double newVolume) implements EventWithoutSnowflake {}
public record ChangeFxVolume(double newVolume) implements EventWithoutSnowflake {}
public record ChangeMusicVolume(double newVolume) implements EventWithoutSnowflake {}
public record GetCurrentVolume(long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
@@ -51,6 +53,18 @@ public class AudioEvents extends EventsBase {
}
}
public record GetCurrentMusicVolume(long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
return Map.of();
}
@Override
public long eventSnowflake() {
return this.snowflakeId;
}
}
public record GetCurrentFxVolumeResponse(double currentVolume, long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
@@ -63,5 +77,18 @@ public class AudioEvents extends EventsBase {
}
}
public record GetCurrentMusicVolumeResponse(double currentVolume, long snowflakeId) implements EventWithSnowflake {
@Override
public Map<String, Object> result() {
return Map.of();
}
@Override
public long eventSnowflake() {
return this.snowflakeId;
}
}
public record ClickButton() implements EventWithoutSnowflake {}
}
}

View File

@@ -5,6 +5,7 @@ public class Settings {
public String locale = "en";
public String theme = "dark";
public String layoutSize = "medium";
public int volume = 15;
public int volume = 100;
public int fxVolume = 20;
public int musicVolume = 15;
}