refactor and documentation

This commit is contained in:
Johannes Schmelz 2024-10-08 19:57:37 +02:00
parent 133921cfbb
commit 5cca8f5c05
3 changed files with 27 additions and 9 deletions

View File

@ -283,7 +283,7 @@ public class BattleshipApp extends SimpleApplication implements BattleshipClient
} }
/** /**
* Attaches the game sound state and sets its initial enabled state. * Attaches the background music state and sets its initial enabled state.
*/ */
private void attachGameMusic() { private void attachGameMusic() {
final GameMusic gameSound = new GameMusic(); final GameMusic gameSound = new GameMusic();

View File

@ -34,7 +34,7 @@ public class GameMusic extends AbstractAppState{
/** /**
* Checks if sound is enabled in the preferences. * Checks if sound is enabled in the preferences.
* *
* @return //TODO * @return float to which the volume is set
*/ */
public static float volumeInPreferences() { public static float volumeInPreferences() {
return PREFERENCES.getFloat(VOLUME_PREF, 0.5f); return PREFERENCES.getFloat(VOLUME_PREF, 0.5f);
@ -78,7 +78,7 @@ public class GameMusic extends AbstractAppState{
return null; return null;
} }
/** TODO /**
* Sets the enabled state of this AppState. * Sets the enabled state of this AppState.
* Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)} * Overrides {@link com.jme3.app.state.AbstractAppState#setEnabled(boolean)}
* *
@ -87,11 +87,15 @@ public class GameMusic extends AbstractAppState{
@Override @Override
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
if (isEnabled() == enabled) return; if (isEnabled() == enabled) return;
else if(!isEnabled() && enabled) {
if (music != null) music.play(); if (music != null) {
} else if (isEnabled() && !enabled) { if (enabled) {
if (music != null) music.stop(); music.play();
} else {
music.stop();
} }
}
super.setEnabled(enabled); super.setEnabled(enabled);
LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS LOGGER.log(Level.INFO, "Sound enabled: {0}", enabled); //NON-NLS
PREFERENCES.putBoolean(ENABLED_PREF, enabled); PREFERENCES.putBoolean(ENABLED_PREF, enabled);
@ -104,6 +108,10 @@ public class GameMusic extends AbstractAppState{
setEnabled(!isEnabled()); setEnabled(!isEnabled());
} }
/**
* Sets the volume of music
* @param vol the volume to which the music should be set
*/
public void setVolume(float vol){ public void setVolume(float vol){
music.setVolume(vol); music.setVolume(vol);
PREFERENCES.putFloat(VOLUME_PREF, vol); PREFERENCES.putFloat(VOLUME_PREF, vol);

View File

@ -1,13 +1,20 @@
package pp.battleship.client.gui; package pp.battleship.client.gui;
import com.simsilica.lemur.Slider; import com.simsilica.lemur.Slider;
/**
* The VolumeSlider class represents the Volume Slider in the Menu.
* It extends the Slider class and provides functionalities for setting the music volume,
* with the help of the Slider in the GUI
*/
public class VolumeSlider extends Slider { public class VolumeSlider extends Slider {
private final GameMusic music; private final GameMusic music;
private double vol; private double vol;
/**
* Constructs the Volume Slider for the Menu dialog
* @param music the music instance
*/
public VolumeSlider(GameMusic music) { public VolumeSlider(GameMusic music) {
super(); super();
this.music = music; this.music = music;
@ -15,6 +22,9 @@ public class VolumeSlider extends Slider {
getModel().setPercent(vol); getModel().setPercent(vol);
} }
/**
* when triggered it updates the volume to the value set with the slider
*/
public void update() { public void update() {
if (vol != getModel().getPercent()) { if (vol != getModel().getPercent()) {
vol = getModel().getPercent(); vol = getModel().getPercent();