add slider + update menu

This commit is contained in:
Luca Puderbach 2024-10-13 17:42:31 +02:00
parent b33ddcb2c2
commit 68e252e174
2 changed files with 51 additions and 10 deletions

View File

@ -7,21 +7,25 @@
package pp.battleship.client;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Checkbox;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.style.ElementId;
import pp.dialog.Dialog;
import pp.dialog.StateCheckboxModel;
import pp.dialog.TextInputDialog;
import java.io.File;
import java.io.IOException;
import java.util.prefs.Preferences;
import com.simsilica.lemur.Button;
import com.simsilica.lemur.Checkbox;
import com.simsilica.lemur.Label;
import com.simsilica.lemur.style.ElementId;
import static pp.battleship.Resources.lookup;
import pp.battleship.client.gui.BgMusic;
import pp.battleship.client.gui.VolumeSlider;
import pp.dialog.Dialog;
import pp.dialog.StateCheckboxModel;
import pp.dialog.TextInputDialog;
import static pp.util.PreferencesUtils.getPreferences;
/**
* The Menu class represents the main menu in the Battleship game application.
* It extends the Dialog class and provides functionalities for loading, saving,
@ -33,6 +37,7 @@ class Menu extends Dialog {
private final BattleshipApp app;
private final Button loadButton = new Button(lookup("menu.map.load"));
private final Button saveButton = new Button(lookup("menu.map.save"));
private final VolumeSlider slider;
/**
* Constructs the Menu dialog for the Battleship application.
@ -42,9 +47,16 @@ class Menu extends Dialog {
public Menu(BattleshipApp app) {
super(app.getDialogManager());
this.app = app;
slider = new VolumeSlider(app.getStateManager().getState(BgMusic.class));
addChild(new Label(lookup("battleship.name"), new ElementId("header"))); //NON-NLS
addChild(new Checkbox(lookup("menu.sound-enabled"),
new StateCheckboxModel(app, GameSound.class)));
// addChild(new Checkbox(lookup("menu.sound-enabled"),
// new StateCheckboxModel(app, GameSound.class)));
addChild(new Checkbox(lookup("menu.sound-enabled"), new StateCheckboxModel(app, GameSound.class)));
addChild(new Checkbox(lookup("menu.background-sound-enabled"), new StateCheckboxModel(app, BgMusic.class)));
addChild(slider);
addChild(loadButton)
.addClickCommands(s -> ifTopDialog(this::loadDialog));
addChild(saveButton)
@ -64,6 +76,10 @@ class Menu extends Dialog {
loadButton.setEnabled(app.getGameLogic().mayLoadMap());
saveButton.setEnabled(app.getGameLogic().maySaveMap());
}
@Override
public void update(float delta) {
slider.update();
}
/**
* As an escape action, this method closes the menu if it is the top dialog.

View File

@ -0,0 +1,25 @@
package pp.battleship.client.gui;
import com.simsilica.lemur.Slider;
public class VolumeSlider extends Slider {
private final BgMusic music;
private double vol;
public VolumeSlider(BgMusic music) {
super();
this.music = music;
vol = BgMusic.volumeInPreferences();
getModel().setPercent(vol);
}
public void update() {
if (vol != getModel().getPercent()) {
vol = getModel().getPercent();
music.setVolume( (float) vol);
}
}
}