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 --- .classpath | 4 ++ build.xml | 4 ++ lib/easyogg.jar | Bin 0 -> 9520 bytes lib/jogg-0.0.7.jar | Bin 0 -> 6839 bytes lib/jorbis-0.0.15.jar | Bin 0 -> 58917 bytes lib/startup2.jar | Bin 0 -> 104522 bytes res/res/snd/END.ogg | Bin 0 -> 1155224 bytes res/res/snd/FEED_ATHANASE.ogg | Bin 0 -> 513150 bytes res/res/snd/FIND_CONSOLE.ogg | Bin 0 -> 1627908 bytes res/res/snd/FIND_DISK.ogg | Bin 0 -> 969007 bytes res/res/snd/INSTALL_CONSOLE.ogg | Bin 0 -> 1719262 bytes res/res/snd/INTRO.ogg | Bin 0 -> 923189 bytes res/res/snd/RUN_CONSOLE.ogg | Bin 0 -> 121718 bytes res/res/snd/WHAT_HAPPENED.ogg | Bin 0 -> 2019139 bytes src/esieequest/view/app/UserInterface.java | 97 ++++++++++++++++++++++++++--- 15 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 lib/easyogg.jar create mode 100644 lib/jogg-0.0.7.jar create mode 100644 lib/jorbis-0.0.15.jar create mode 100644 lib/startup2.jar create mode 100644 res/res/snd/END.ogg create mode 100644 res/res/snd/FEED_ATHANASE.ogg create mode 100644 res/res/snd/FIND_CONSOLE.ogg create mode 100644 res/res/snd/FIND_DISK.ogg create mode 100644 res/res/snd/INSTALL_CONSOLE.ogg create mode 100644 res/res/snd/INTRO.ogg create mode 100644 res/res/snd/RUN_CONSOLE.ogg create mode 100644 res/res/snd/WHAT_HAPPENED.ogg diff --git a/.classpath b/.classpath index e0c30ac..90df3ec 100644 --- a/.classpath +++ b/.classpath @@ -7,5 +7,9 @@ + + + + diff --git a/build.xml b/build.xml index fe7a851..1f76860 100644 --- a/build.xml +++ b/build.xml @@ -22,6 +22,10 @@ + + + + diff --git a/lib/easyogg.jar b/lib/easyogg.jar new file mode 100644 index 0000000..bf70633 Binary files /dev/null and b/lib/easyogg.jar differ diff --git a/lib/jogg-0.0.7.jar b/lib/jogg-0.0.7.jar new file mode 100644 index 0000000..1cbd1ad Binary files /dev/null and b/lib/jogg-0.0.7.jar differ diff --git a/lib/jorbis-0.0.15.jar b/lib/jorbis-0.0.15.jar new file mode 100644 index 0000000..4cf51f9 Binary files /dev/null and b/lib/jorbis-0.0.15.jar differ diff --git a/lib/startup2.jar b/lib/startup2.jar new file mode 100644 index 0000000..697ed76 Binary files /dev/null and b/lib/startup2.jar differ diff --git a/res/res/snd/END.ogg b/res/res/snd/END.ogg new file mode 100644 index 0000000..db2b370 Binary files /dev/null and b/res/res/snd/END.ogg differ diff --git a/res/res/snd/FEED_ATHANASE.ogg b/res/res/snd/FEED_ATHANASE.ogg new file mode 100644 index 0000000..94e8668 Binary files /dev/null and b/res/res/snd/FEED_ATHANASE.ogg differ diff --git a/res/res/snd/FIND_CONSOLE.ogg b/res/res/snd/FIND_CONSOLE.ogg new file mode 100644 index 0000000..39aee7e Binary files /dev/null and b/res/res/snd/FIND_CONSOLE.ogg differ diff --git a/res/res/snd/FIND_DISK.ogg b/res/res/snd/FIND_DISK.ogg new file mode 100644 index 0000000..b852de2 Binary files /dev/null and b/res/res/snd/FIND_DISK.ogg differ diff --git a/res/res/snd/INSTALL_CONSOLE.ogg b/res/res/snd/INSTALL_CONSOLE.ogg new file mode 100644 index 0000000..fe7a296 Binary files /dev/null and b/res/res/snd/INSTALL_CONSOLE.ogg differ diff --git a/res/res/snd/INTRO.ogg b/res/res/snd/INTRO.ogg new file mode 100644 index 0000000..da90187 Binary files /dev/null and b/res/res/snd/INTRO.ogg differ diff --git a/res/res/snd/RUN_CONSOLE.ogg b/res/res/snd/RUN_CONSOLE.ogg new file mode 100644 index 0000000..8c43dd1 Binary files /dev/null and b/res/res/snd/RUN_CONSOLE.ogg differ diff --git a/res/res/snd/WHAT_HAPPENED.ogg b/res/res/snd/WHAT_HAPPENED.ogg new file mode 100644 index 0000000..0aac689 Binary files /dev/null and b/res/res/snd/WHAT_HAPPENED.ogg differ 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