From ba0d0039b3463cba497abd25f4fbf62cf74398e3 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Thu, 22 May 2014 14:55:25 +0200 Subject: Implement music on Swing GUI --- src/esieequest/view/app/UserInterface.java | 97 +++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index facc706..ed8e78f 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -12,9 +12,12 @@ import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; +import java.io.IOException; import java.net.URL; import java.util.HashMap; import java.util.Map.Entry; +import java.util.Timer; +import java.util.TimerTask; import javax.swing.AbstractAction; import javax.swing.JButton; @@ -28,6 +31,8 @@ import javax.swing.border.EmptyBorder; import lombok.Getter; +import org.newdawn.easyogg.OggClip; + import com.wordpress.tipsforjava.swing.StretchIcon; import esieequest.controller.GameEngine; @@ -54,7 +59,7 @@ abstract class UserInterface implements Viewable, ActionListener { private static final String ILLUSTRATION_DIR = "res/frame/"; private static final String ILLUSTRATION_EXT = ".html"; - private static final String SOUND_DIR = "res/audio/"; + private static final String SOUND_DIR = "res/snd/"; private static final String SOUND_EXT = ".ogg"; private GameEngine gameEngine; @@ -98,6 +103,12 @@ abstract class UserInterface implements Viewable, ActionListener { private JButton leftButton; private JButton rightButton; + private final Timer timer; + private TimerTask timerTask; + + private OggClip audio; + private boolean muted; + /** * The default constructor. */ @@ -107,6 +118,8 @@ abstract class UserInterface implements Viewable, ActionListener { this.bindKeys(); this.bindFocus(); this.setControlsState(false); + this.timer = new Timer(); + this.muted = false; } /** @@ -310,6 +323,20 @@ abstract class UserInterface implements Viewable, ActionListener { }); } + this.layout.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KeyEvent.VK_ESCAPE); + + this.layout.getActionMap().put(KeyEvent.VK_ESCAPE, new AbstractAction() { + + private static final long serialVersionUID = 1L; + + @Override + public void actionPerformed(final ActionEvent actionEvent) { + UserInterface.this.skipScene(); + } + + }); + } /** @@ -388,7 +415,7 @@ abstract class UserInterface implements Viewable, ActionListener { */ private void setIllustration(final String imageName) { final URL imageURL = this.getClass().getClassLoader().getResource( - imageName + UserInterface.ILLUSTRATION_EXT); + UserInterface.ILLUSTRATION_DIR + imageName + UserInterface.ILLUSTRATION_EXT); final StretchIcon imageIcon; if (imageURL == null) { imageIcon = new StretchIcon(this.generatePlaceholderImage(imageName), true); @@ -414,6 +441,14 @@ abstract class UserInterface implements Viewable, ActionListener { return bufferedImage; } + /** + * Skips the currently player Scene. + */ + private void skipScene() { + this.timerTask.run(); + this.timerTask.cancel(); + } + /** * Opens the inventory (switches to the inventory tab). */ @@ -448,16 +483,46 @@ abstract class UserInterface implements Viewable, ActionListener { * the URL of the audio file */ private void playAudio(final String fileName) { - // TODO Auto-generated method stub - // this.echo(Text.NOT_IMPLEMENTED.toString()); + if (this.audio != null) { + this.audio.stop(); + this.audio.close(); + } + + try { + this.audio = new OggClip(UserInterface.SOUND_DIR + fileName + UserInterface.SOUND_EXT); + this.setAudioGain(); + this.audio.play(); + } catch (final IOException e) { + e.printStackTrace(); + } } /** * Toggles the sound (music). */ private void toggleAudio() { - // TODO Auto-generated method stub - this.echo(Text.NOT_IMPLEMENTED.toString()); + this.muted = !this.muted; + this.setAudioGain(); + } + + /** + * Sets the gain. + * + * FIXME: PulseAudio does not comply with the JSAPI + */ + private void setAudioGain() { + if (this.audio == null) { + return; + } + if (this.audio.stopped()) { + return; + } + + if (this.muted) { + this.audio.setGain(-1.0f); + } else { + this.audio.setGain(1.0f); + } } /** @@ -496,7 +561,6 @@ abstract class UserInterface implements Viewable, ActionListener { public void echo(final String message) { this.closeInventory(); this.infoTextPane.setText(message); - this.clearInputField(); } @Override @@ -540,13 +604,28 @@ abstract class UserInterface implements Viewable, ActionListener { @Override public void playScene(final Scene scene) { - scene.getCallback().call(); + this.disableInput(); + + this.setQuestLabel(scene.getTitle()); + this.echo(scene.getText()); + this.setIllustration(scene.name()); this.playAudio(scene.name()); + + this.timerTask = new TimerTask() { + @Override + public void run() { + scene.getCallback().call(); + } + }; + this.timer.schedule(this.timerTask, scene.getDuration()); } @Override public void stopMusic() { - return; + if (this.audio != null) { + this.audio.stop(); + this.audio.close(); + } } } -- cgit v1.2.3