Dim theme song volume when window is not focused.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-10 20:14:39 -05:00
parent b525ab4533
commit 1e59819f3a
2 changed files with 25 additions and 0 deletions

View File

@ -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.
*/

View File

@ -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;