aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2014-05-22 14:55:25 +0200
committerPacien TRAN-GIRARD2014-05-22 20:21:16 +0200
commitba0d0039b3463cba497abd25f4fbf62cf74398e3 (patch)
treedd631355159b3f2e09d87169358707a258a903a3 /src
parentc863481f22231dab3ba1ca2f47db39d402caca33 (diff)
downloadesieequest-ba0d0039b3463cba497abd25f4fbf62cf74398e3.tar.gz
Implement music on Swing GUI
Diffstat (limited to 'src')
-rw-r--r--src/esieequest/view/app/UserInterface.java97
1 files changed, 88 insertions, 9 deletions
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;
12import java.awt.event.FocusListener; 12import java.awt.event.FocusListener;
13import java.awt.event.KeyEvent; 13import java.awt.event.KeyEvent;
14import java.awt.image.BufferedImage; 14import java.awt.image.BufferedImage;
15import java.io.IOException;
15import java.net.URL; 16import java.net.URL;
16import java.util.HashMap; 17import java.util.HashMap;
17import java.util.Map.Entry; 18import java.util.Map.Entry;
19import java.util.Timer;
20import java.util.TimerTask;
18 21
19import javax.swing.AbstractAction; 22import javax.swing.AbstractAction;
20import javax.swing.JButton; 23import javax.swing.JButton;
@@ -28,6 +31,8 @@ import javax.swing.border.EmptyBorder;
28 31
29import lombok.Getter; 32import lombok.Getter;
30 33
34import org.newdawn.easyogg.OggClip;
35
31import com.wordpress.tipsforjava.swing.StretchIcon; 36import com.wordpress.tipsforjava.swing.StretchIcon;
32 37
33import esieequest.controller.GameEngine; 38import esieequest.controller.GameEngine;
@@ -54,7 +59,7 @@ abstract class UserInterface implements Viewable, ActionListener {
54 private static final String ILLUSTRATION_DIR = "res/frame/"; 59 private static final String ILLUSTRATION_DIR = "res/frame/";
55 private static final String ILLUSTRATION_EXT = ".html"; 60 private static final String ILLUSTRATION_EXT = ".html";
56 61
57 private static final String SOUND_DIR = "res/audio/"; 62 private static final String SOUND_DIR = "res/snd/";
58 private static final String SOUND_EXT = ".ogg"; 63 private static final String SOUND_EXT = ".ogg";
59 64
60 private GameEngine gameEngine; 65 private GameEngine gameEngine;
@@ -98,6 +103,12 @@ abstract class UserInterface implements Viewable, ActionListener {
98 private JButton leftButton; 103 private JButton leftButton;
99 private JButton rightButton; 104 private JButton rightButton;
100 105
106 private final Timer timer;
107 private TimerTask timerTask;
108
109 private OggClip audio;
110 private boolean muted;
111
101 /** 112 /**
102 * The default constructor. 113 * The default constructor.
103 */ 114 */
@@ -107,6 +118,8 @@ abstract class UserInterface implements Viewable, ActionListener {
107 this.bindKeys(); 118 this.bindKeys();
108 this.bindFocus(); 119 this.bindFocus();
109 this.setControlsState(false); 120 this.setControlsState(false);
121 this.timer = new Timer();
122 this.muted = false;
110 } 123 }
111 124
112 /** 125 /**
@@ -310,6 +323,20 @@ abstract class UserInterface implements Viewable, ActionListener {
310 }); 323 });
311 } 324 }
312 325
326 this.layout.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
327 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), KeyEvent.VK_ESCAPE);
328
329 this.layout.getActionMap().put(KeyEvent.VK_ESCAPE, new AbstractAction() {
330
331 private static final long serialVersionUID = 1L;
332
333 @Override
334 public void actionPerformed(final ActionEvent actionEvent) {
335 UserInterface.this.skipScene();
336 }
337
338 });
339
313 } 340 }
314 341
315 /** 342 /**
@@ -388,7 +415,7 @@ abstract class UserInterface implements Viewable, ActionListener {
388 */ 415 */
389 private void setIllustration(final String imageName) { 416 private void setIllustration(final String imageName) {
390 final URL imageURL = this.getClass().getClassLoader().getResource( 417 final URL imageURL = this.getClass().getClassLoader().getResource(
391 imageName + UserInterface.ILLUSTRATION_EXT); 418 UserInterface.ILLUSTRATION_DIR + imageName + UserInterface.ILLUSTRATION_EXT);
392 final StretchIcon imageIcon; 419 final StretchIcon imageIcon;
393 if (imageURL == null) { 420 if (imageURL == null) {
394 imageIcon = new StretchIcon(this.generatePlaceholderImage(imageName), true); 421 imageIcon = new StretchIcon(this.generatePlaceholderImage(imageName), true);
@@ -415,6 +442,14 @@ abstract class UserInterface implements Viewable, ActionListener {
415 } 442 }
416 443
417 /** 444 /**
445 * Skips the currently player Scene.
446 */
447 private void skipScene() {
448 this.timerTask.run();
449 this.timerTask.cancel();
450 }
451
452 /**
418 * Opens the inventory (switches to the inventory tab). 453 * Opens the inventory (switches to the inventory tab).
419 */ 454 */
420 private void openInventory() { 455 private void openInventory() {
@@ -448,16 +483,46 @@ abstract class UserInterface implements Viewable, ActionListener {
448 * the URL of the audio file 483 * the URL of the audio file
449 */ 484 */
450 private void playAudio(final String fileName) { 485 private void playAudio(final String fileName) {
451 // TODO Auto-generated method stub 486 if (this.audio != null) {
452 // this.echo(Text.NOT_IMPLEMENTED.toString()); 487 this.audio.stop();
488 this.audio.close();
489 }
490
491 try {
492 this.audio = new OggClip(UserInterface.SOUND_DIR + fileName + UserInterface.SOUND_EXT);
493 this.setAudioGain();
494 this.audio.play();
495 } catch (final IOException e) {
496 e.printStackTrace();
497 }
453 } 498 }
454 499
455 /** 500 /**
456 * Toggles the sound (music). 501 * Toggles the sound (music).
457 */ 502 */
458 private void toggleAudio() { 503 private void toggleAudio() {
459 // TODO Auto-generated method stub 504 this.muted = !this.muted;
460 this.echo(Text.NOT_IMPLEMENTED.toString()); 505 this.setAudioGain();
506 }
507
508 /**
509 * Sets the gain.
510 *
511 * FIXME: PulseAudio does not comply with the JSAPI
512 */
513 private void setAudioGain() {
514 if (this.audio == null) {
515 return;
516 }
517 if (this.audio.stopped()) {
518 return;
519 }
520
521 if (this.muted) {
522 this.audio.setGain(-1.0f);
523 } else {
524 this.audio.setGain(1.0f);
525 }
461 } 526 }
462 527
463 /** 528 /**
@@ -496,7 +561,6 @@ abstract class UserInterface implements Viewable, ActionListener {
496 public void echo(final String message) { 561 public void echo(final String message) {
497 this.closeInventory(); 562 this.closeInventory();
498 this.infoTextPane.setText(message); 563 this.infoTextPane.setText(message);
499 this.clearInputField();
500 } 564 }
501 565
502 @Override 566 @Override
@@ -540,13 +604,28 @@ abstract class UserInterface implements Viewable, ActionListener {
540 604
541 @Override 605 @Override
542 public void playScene(final Scene scene) { 606 public void playScene(final Scene scene) {
543 scene.getCallback().call(); 607 this.disableInput();
608
609 this.setQuestLabel(scene.getTitle());
610 this.echo(scene.getText());
611 this.setIllustration(scene.name());
544 this.playAudio(scene.name()); 612 this.playAudio(scene.name());
613
614 this.timerTask = new TimerTask() {
615 @Override
616 public void run() {
617 scene.getCallback().call();
618 }
619 };
620 this.timer.schedule(this.timerTask, scene.getDuration());
545 } 621 }
546 622
547 @Override 623 @Override
548 public void stopMusic() { 624 public void stopMusic() {
549 return; 625 if (this.audio != null) {
626 this.audio.stop();
627 this.audio.close();
628 }
550 } 629 }
551 630
552} 631}