Dim theme song volume when window is not focused.
Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
parent
b525ab4533
commit
1e59819f3a
|
@ -257,6 +257,14 @@ public class MusicController {
|
|||
return (trackExists() && player.setPosition(position / 1000f));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the music volume.
|
||||
* @param volume [0, 1]
|
||||
*/
|
||||
public static void setVolume(float volume) {
|
||||
SoundStore.get().setMusicVolume(volume);
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays the theme song.
|
||||
*/
|
||||
|
|
|
@ -102,6 +102,11 @@ public class MainMenu extends BasicGameState {
|
|||
*/
|
||||
private float bgAlpha = 0f;
|
||||
|
||||
/**
|
||||
* Whether the theme song volume has been dimmed.
|
||||
*/
|
||||
private boolean volumeDimmed = false;
|
||||
|
||||
// game-related variables
|
||||
private GameContainer container;
|
||||
private StateBasedGame game;
|
||||
|
@ -249,6 +254,18 @@ public class MainMenu extends BasicGameState {
|
|||
musicNext.hoverUpdate(delta, mouseX, mouseY);
|
||||
musicPrevious.hoverUpdate(delta, mouseX, mouseY);
|
||||
|
||||
// window focus change: increase/decrease theme song volume
|
||||
if (MusicController.isThemePlaying()) {
|
||||
boolean hasFocus = container.hasFocus();
|
||||
if (volumeDimmed == hasFocus) {
|
||||
if (hasFocus) // restored focus
|
||||
MusicController.setVolume(Options.getMusicVolume());
|
||||
else // lost focus
|
||||
MusicController.setVolume(Options.getMusicVolume() / 3f);
|
||||
volumeDimmed = !hasFocus;
|
||||
}
|
||||
}
|
||||
|
||||
// fade in background
|
||||
if (bgAlpha < 0.9f) {
|
||||
bgAlpha += delta / 1000f;
|
||||
|
|
Loading…
Reference in New Issue
Block a user