From 03be2894b30b262eafc4005dc8a060ee16a76b87 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 18 May 2014 20:12:56 +0200 Subject: Add key binding --- src/esieequest/view/app/UserInterface.java | 91 ++++++++++++++++++++++++++---- src/esieequest/view/web/WebInterface.java | 32 ++++++++++- 2 files changed, 112 insertions(+), 11 deletions(-) diff --git a/src/esieequest/view/app/UserInterface.java b/src/esieequest/view/app/UserInterface.java index 90a99c2..d544c9a 100644 --- a/src/esieequest/view/app/UserInterface.java +++ b/src/esieequest/view/app/UserInterface.java @@ -8,18 +8,28 @@ import java.awt.GridLayout; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.net.URL; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map.Entry; import java.util.Set; +import javax.swing.AbstractAction; import javax.swing.JButton; +import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JTextPane; +import javax.swing.KeyStroke; import javax.swing.border.EmptyBorder; +import lombok.Getter; + import com.wordpress.tipsforjava.swing.StretchIcon; import esieequest.controller.GameEngine; @@ -46,6 +56,7 @@ abstract class UserInterface implements Viewable, ActionListener { private GameEngine gameEngine; + @Getter private JPanel layout; private JTextPane questTextPane; @@ -96,6 +107,8 @@ abstract class UserInterface implements Viewable, ActionListener { this.buildUI(); this.setActionListener(this); + this.bindKeys(); + this.bindFocus(); this.setControlsState(false); } @@ -252,15 +265,6 @@ abstract class UserInterface implements Viewable, ActionListener { } - /** - * Returns the layout. - * - * @return the layout - */ - public JPanel getLayout() { - return this.layout; - } - /** * Sets the action listener for the given JButtons. * @@ -281,12 +285,78 @@ abstract class UserInterface implements Viewable, ActionListener { * @param actionListener * the action listener */ - public void setActionListener(final ActionListener actionListener) { + private void setActionListener(final ActionListener actionListener) { this.inputField.addActionListener(actionListener); this.setActionListener(this.gameButtons, actionListener); this.setActionListener(this.controlButtons, actionListener); } + /** + * Binds keys to corresponding buttons. + */ + private void bindKeys() { + + final HashMap keys = new HashMap<>(); + keys.put(KeyEvent.VK_LEFT, this.leftButton); + keys.put(KeyEvent.VK_RIGHT, this.rightButton); + keys.put(KeyEvent.VK_UP, this.forwardButton); + keys.put(KeyEvent.VK_DOWN, this.backButton); + keys.put(KeyEvent.VK_HOME, this.inventoryButton); + keys.put(KeyEvent.VK_PAGE_UP, this.actionButton); + + for (final Entry entry : keys.entrySet()) { + final int key = entry.getKey(); + final JButton button = entry.getValue(); + + this.layout.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( + KeyStroke.getKeyStroke(key, 0), key); + + this.layout.getActionMap().put(key, new AbstractAction() { + + private static final long serialVersionUID = 1L; + + @Override + public void actionPerformed(final ActionEvent actionEvent) { + button.doClick(); + } + + }); + } + + } + + /** + * Binds focus catching components to delegate key strokes to the main + * window. + */ + public void bindFocus() { + final FocusListener focusLossDelegator = new FocusListener() { + @Override + public void focusGained(final FocusEvent focusEvent) { + } + + @Override + public void focusLost(final FocusEvent focusEvent) { + UserInterface.this.layout.requestFocus(); + } + }; + + final FocusListener focusGainDelegator = new FocusListener() { + @Override + public void focusGained(final FocusEvent focusEvent) { + UserInterface.this.layout.requestFocus(); + } + + @Override + public void focusLost(final FocusEvent focusEvent) { + } + }; + + this.inputField.addFocusListener(focusLossDelegator); + this.infoTextPane.addFocusListener(focusGainDelegator); + this.questTextPane.addFocusListener(focusGainDelegator); + } + /** * Clears the textual input field. */ @@ -385,6 +455,7 @@ abstract class UserInterface implements Viewable, ActionListener { */ @Override public void actionPerformed(final ActionEvent actionEvent) { + this.echo(actionEvent.toString()); if (actionEvent.getActionCommand() == Text.INVENTORY_BUTTON.toString()) { this.toggleInventory(); } else { diff --git a/src/esieequest/view/web/WebInterface.java b/src/esieequest/view/web/WebInterface.java index 8f6c1ce..0d5f201 100644 --- a/src/esieequest/view/web/WebInterface.java +++ b/src/esieequest/view/web/WebInterface.java @@ -155,7 +155,37 @@ class WebInterface extends Composite implements Viewable { Event.addNativePreviewHandler(new NativePreviewHandler() { @Override public void onPreviewNativeEvent(final NativePreviewEvent event) { - // final int key = event.getNativeEvent().getKeyCode(); + if (WebInterface.this.inputField.getText().length() > 0) { + return; + } + + if (event.getTypeInt() != Event.ONKEYDOWN) { + return; + } + + switch (event.getNativeEvent().getKeyCode()) { + case KeyCodes.KEY_LEFT: + WebInterface.this.leftButton.click(); + break; + case KeyCodes.KEY_RIGHT: + WebInterface.this.rightButton.click(); + break; + case KeyCodes.KEY_UP: + WebInterface.this.forwardButton.click(); + break; + case KeyCodes.KEY_DOWN: + WebInterface.this.backButton.click(); + break; + case KeyCodes.KEY_HOME: + WebInterface.this.inventoryButton.click(); + break; + case KeyCodes.KEY_PAGEUP: + WebInterface.this.actionButton.click(); + break; + default: + WebInterface.this.inputField.setFocus(true); + break; + } } }); } -- cgit v1.2.3