added MainVolume slider in the menu

added the logic so that the volume of music or sound is multiplied by the main volume
created the class MainVolume to handle the logic of the main volume
adjusted methods in GameSounds and BackgroundMusic to integrate main volume
This commit is contained in:
Hanno Fleischer hanno.fleischer@unibw.de
2024-10-05 17:32:14 +02:00
parent 586251b2ad
commit 52673dfbce
8 changed files with 167 additions and 21 deletions

View File

@@ -33,15 +33,14 @@ public class BackgroundMusic implements GameEventListener {
private boolean musicEnabled;
private float volume;
private Application app;
private final BattleshipApp app;
/**
* Initializes and controls the BackgroundMusic
*
* @param app The main Application
* @param musicFilePath The Path for the specific background music
*/
public BackgroundMusic(Application app, String musicFilePath) {
public BackgroundMusic(BattleshipApp app) {
this.volume = prefs.getFloat(VOLUME_PREF, 1.0f);
this.musicEnabled = prefs.getBoolean(MUSIC_ENABLED_PREF, true);
this.app = app;
@@ -69,7 +68,7 @@ public BackgroundMusic(Application app, String musicFilePath) {
*/
private AudioNode createAudioNode(String musicFilePath) {
AudioNode audioNode = new AudioNode(app.getAssetManager(), musicFilePath, DataType.Stream);
audioNode.setVolume(volume);
audioNode.setVolume(volume * app.getMainVolume().getMainVolume());
audioNode.setPositional(false);
audioNode.setLooping(true);
audioNode.setName(musicFilePath);
@@ -135,6 +134,16 @@ public void toogleMusic() {
prefs.putBoolean(MUSIC_ENABLED_PREF, musicEnabled);
}
/**
* this method is used when the main volume changes
*/
public void setVolume(){
float mainVolume = app.getMainVolume().getMainVolume();
menuMusic.setVolume(volume * mainVolume);
battleMusic.setVolume(volume * mainVolume);
gameOverMusicL.setVolume(volume * mainVolume);
}
/**
* Method to set the volume for the music
*
@@ -142,9 +151,10 @@ public void toogleMusic() {
*/
public void setVolume(float volume) {
this.volume = volume;
menuMusic.setVolume(volume);
battleMusic.setVolume(volume);
gameOverMusicL.setVolume(volume);
float mainVolume = app.getMainVolume().getMainVolume();
menuMusic.setVolume(volume * mainVolume);
battleMusic.setVolume(volume * mainVolume);
gameOverMusicL.setVolume(volume * mainVolume);
prefs.putFloat(VOLUME_PREF, volume);
}

View File

@@ -127,6 +127,11 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
*/
private BackgroundMusic backgroundMusic;
/**
* The object that handles the main volume
*/
private MainVolume mainVolume;
static {
// Configure logging
LogManager manager = LogManager.getLogManager();
@@ -231,7 +236,8 @@ public void simpleInitApp() {
setupGui();
serverConnection.connect();
backgroundMusic = new BackgroundMusic(this, "Music/MainMenu/Dark_Intro.ogg");
mainVolume = new MainVolume(this);
backgroundMusic = new BackgroundMusic(this);
}
/**
@@ -443,4 +449,13 @@ public BackgroundMusic getBackgroundMusic(){
logic.addListener(backgroundMusic);
return backgroundMusic;
}
/**
* this method returns the object which handles the main volume
*
* @return an object of MainVolume
*/
public MainVolume getMainVolume(){
return mainVolume;
}
}

View File

@@ -30,11 +30,16 @@ public class GameSound extends AbstractAppState implements GameEventListener {
static final Logger LOGGER = System.getLogger(GameSound.class.getName());
private static final Preferences PREFERENCES = getPreferences(GameSound.class);
private static final String ENABLED_PREF = "enabled"; //NON-NLS
private static final String SOUND_VOLUME_PREF = "volume";
private float volume;
private AudioNode splashSound;
private AudioNode shipDestroyedSound;
private AudioNode explosionSound;
private BattleshipApp app;
/**
* Checks if sound is enabled in the preferences.
*
@@ -75,9 +80,11 @@ public void setEnabled(boolean enabled) {
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (BattleshipApp) app;
shipDestroyedSound = loadSound(app, "Sound/Effects/sunken.wav"); //NON-NLS
splashSound = loadSound(app, "Sound/Effects/splash.wav"); //NON-NLS
explosionSound = loadSound(app, "Sound/Effects/explosion.wav"); //NON-NLS
volume = PREFERENCES.getFloat(SOUND_VOLUME_PREF, 1.0f);
}
/**
@@ -124,6 +131,40 @@ public void shipDestroyed() {
shipDestroyedSound.playInstance();
}
/**
* this method sets the sound volume of the sounds
*
* @param volume the volume to be set to
*/
public void setSoundVolume(float volume) {
float mainVolume = app.getMainVolume().getMainVolume();
shipDestroyedSound.setVolume(volume);
splashSound.setVolume(volume);
explosionSound.setVolume(volume);
this.volume = volume;
PREFERENCES.putFloat(SOUND_VOLUME_PREF, volume);
}
/**
* this method will be used if the main volume changes
*/
public void setSoundVolume() {
float mainVolume = app.getMainVolume().getMainVolume();
shipDestroyedSound.setVolume(volume * mainVolume);
splashSound.setVolume(volume * mainVolume);
explosionSound.setVolume(volume * mainVolume);
PREFERENCES.putFloat(SOUND_VOLUME_PREF, volume);
}
/**
* this method returns the sound
*
* @return
*/
public float getVolume(){
return volume;
}
@Override
public void receivedEvent(SoundEvent event) {
switch (event.sound()) {

View File

@@ -0,0 +1,34 @@
package pp.battleship.client;
import pp.battleship.model.Battleship;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.prefs.Preferences;
public class MainVolume {
private Preferences prefs = Preferences.userNodeForPackage(MainVolume.class);
private static final String MAIN_VOLUME_PREFS = "MainVolume";
static final Logger LOGGER = System.getLogger(MainVolume.class.getName());
private float mainVolume;
private BattleshipApp app;
public MainVolume(BattleshipApp app) {
this.mainVolume = prefs.getFloat(MAIN_VOLUME_PREFS, 1.0f);
this.app = app;
}
public void setMainVolume(float mainVolume) {
LOGGER.log(Level.DEBUG, "setMainVolume: mainVolume = {0}", mainVolume);
app.getBackgroundMusic().setVolume();
app.getStateManager().getState(GameSound.class).setSoundVolume();
this.mainVolume = mainVolume;
prefs.putFloat(MAIN_VOLUME_PREFS, mainVolume);
}
public float getMainVolume() {
return mainVolume;
}
}

View File

@@ -37,7 +37,13 @@ class Menu extends Dialog {
private final Button loadButton = new Button(lookup("menu.map.load"));
private final Button saveButton = new Button(lookup("menu.map.save"));
private static final double SLIDER_DELTA = 0.1;
private static final double SLIDER_MIN_VALUE = 0.0;
private static final double SLIDER_MAX_VALUE = 2.0;
private final VersionedReference<Double> volumeRef;
private final VersionedReference<Double> soundVolumeRef;
private final VersionedReference<Double> mainVolumeRef;
/**
* Constructs the Menu dialog for the Battleship application.
@@ -48,22 +54,32 @@ public Menu(BattleshipApp app) {
super(app.getDialogManager());
this.app = app;
addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS
addChild(new Label(lookup("menu.main.volume"), new ElementId("label")));
Slider mainVolumeSlider = new Slider();
mainVolumeSlider.setModel(new DefaultRangedValueModel(SLIDER_MIN_VALUE, SLIDER_MAX_VALUE, app.getMainVolume().getMainVolume()));
mainVolumeSlider.setDelta(SLIDER_DELTA);
addChild(mainVolumeSlider);
mainVolumeRef = mainVolumeSlider.getModel().createReference();
addChild(new Label(lookup("menu.sound.volume"), new ElementId("label")));
addChild(new Checkbox(lookup("menu.sound-enabled"),
new StateCheckboxModel(app, GameSound.class)));
Slider soundSlider = new Slider();
soundSlider.setModel(new DefaultRangedValueModel(SLIDER_MIN_VALUE, SLIDER_MAX_VALUE, app.getStateManager().getState(GameSound.class).getVolume()));
soundSlider.setDelta(SLIDER_DELTA);
addChild(soundSlider);
soundVolumeRef = soundSlider.getModel().createReference();
addChild(new Label(lookup("menu.volume"), new ElementId("label")));
Checkbox musicToggle = new Checkbox(lookup("menu.music.toggle"));
musicToggle.setChecked(app.getBackgroundMusic().isMusicEnabled());
musicToggle.addClickCommands(s -> toggleMusic());
addChild(musicToggle);
addChild(new Label(lookup("menu.volume")));
Slider volumeSlider = new Slider();
volumeSlider.setModel(new DefaultRangedValueModel(0.0 , 2.0, app.getBackgroundMusic().getVolume()));
volumeSlider.setDelta(0.1);
volumeSlider.setModel(new DefaultRangedValueModel(SLIDER_MIN_VALUE, SLIDER_MAX_VALUE, app.getBackgroundMusic().getVolume()));
volumeSlider.setDelta(SLIDER_DELTA);
addChild(volumeSlider);
volumeRef = volumeSlider.getModel().createReference();
addChild(loadButton)
@@ -85,8 +101,24 @@ public Menu(BattleshipApp app) {
public void update(float tpf){
if(volumeRef.update()){
double newVolume = volumeRef.get();
adjustVolume(newVolume);
adjustMusicVolume(newVolume);
}
else if (soundVolumeRef.update()) {
double newSoundVolume = soundVolumeRef.get();
adjustSoundVolume(newSoundVolume);
} else if (mainVolumeRef.update()) {
double newMainVolume = mainVolumeRef.get();
adjustMainVolume(newMainVolume);
}
}
/**
* this method adjusts the main volume
*
* @param newVolume the volume to be set as main volume
*/
private void adjustMainVolume(double newVolume) {
app.getMainVolume().setMainVolume((float) newVolume);
}
/**
@@ -94,10 +126,19 @@ public void update(float tpf){
*
* @param volume is the double value of the volume
*/
private void adjustVolume(double volume) {
private void adjustMusicVolume(double volume) {
app.getBackgroundMusic().setVolume((float) volume);
}
/**
* this method adjusts the volume for the sound
*
* @param volume is a double value of the sound volume
*/
private void adjustSoundVolume(double volume) {
app.getStateManager().getState(GameSound.class).setSoundVolume((float) volume);
}
/**
* this method toggles the background music on and off
*/

View File

@@ -29,12 +29,14 @@ port.number=Port
wait.its.not.your.turn=Wait, it's not your turn!!
menu.quit=Quit game
menu.return-to-game=Return to game
menu.sound-enabled=Sound switched on
menu.sound-enabled=Toggle the sound
menu.main.volume= Main volume
menu.map.load=Load map from file...
menu.map.save=Save map in file...
menu.music.toggle=Toggle the music
invalid.map=Your submitted map was invalid
menu.volume=Volume
menu.volume=Music volume
menu.sound.volume=Sound volume
label.file=File:
label.connecting=Connecting...
dialog.error=Error

View File

@@ -29,12 +29,14 @@ port.number=Port
wait.its.not.your.turn=Warte, Du bist nicht dran!!
menu.quit=Spiel beenden
menu.return-to-game=Zurück zum Spiel
menu.sound-enabled=Sound eingeschaltet
menu.sound-enabled=An/Ausschalten des Sounds
menu.main.volume=Gesammt Lautst<73>rke
menu.map.load=Karte von Datei laden...
menu.map.save=Karte in Datei speichern...
menu.music.toggle=An/Ausschalten der Musik
invalid.map=Die angegebene Karte war ung<6E>ltig
menu.volume=Lautst<EFBFBD>rke
menu.volume=Lautst<EFBFBD>rke der Musik
menu.sound.volume=Lautst<EFBFBD>rke des Sounds
label.file=Datei:
label.connecting=Verbindung wird aufgebaut...
dialog.error=Fehler

View File

@@ -36,6 +36,7 @@
selector("label", "pp") {
insets = new Insets3f(2, 2, 2, 2)
color = buttonEnabledColor
textHAlignment = HAlignment.Center
}
selector("header", "pp") {