aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/esieequest/view/app/UserInterface.java91
-rw-r--r--src/esieequest/view/web/WebInterface.java32
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;
8import java.awt.Image; 8import java.awt.Image;
9import java.awt.event.ActionEvent; 9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener; 10import java.awt.event.ActionListener;
11import java.awt.event.FocusEvent;
12import java.awt.event.FocusListener;
13import java.awt.event.KeyEvent;
11import java.awt.image.BufferedImage; 14import java.awt.image.BufferedImage;
12import java.net.URL; 15import java.net.URL;
16import java.util.HashMap;
13import java.util.HashSet; 17import java.util.HashSet;
18import java.util.Map.Entry;
14import java.util.Set; 19import java.util.Set;
15 20
21import javax.swing.AbstractAction;
16import javax.swing.JButton; 22import javax.swing.JButton;
23import javax.swing.JComponent;
17import javax.swing.JLabel; 24import javax.swing.JLabel;
18import javax.swing.JPanel; 25import javax.swing.JPanel;
19import javax.swing.JTextField; 26import javax.swing.JTextField;
20import javax.swing.JTextPane; 27import javax.swing.JTextPane;
28import javax.swing.KeyStroke;
21import javax.swing.border.EmptyBorder; 29import javax.swing.border.EmptyBorder;
22 30
31import lombok.Getter;
32
23import com.wordpress.tipsforjava.swing.StretchIcon; 33import com.wordpress.tipsforjava.swing.StretchIcon;
24 34
25import esieequest.controller.GameEngine; 35import esieequest.controller.GameEngine;
@@ -46,6 +56,7 @@ abstract class UserInterface implements Viewable, ActionListener {
46 56
47 private GameEngine gameEngine; 57 private GameEngine gameEngine;
48 58
59 @Getter
49 private JPanel layout; 60 private JPanel layout;
50 61
51 private JTextPane questTextPane; 62 private JTextPane questTextPane;
@@ -96,6 +107,8 @@ abstract class UserInterface implements Viewable, ActionListener {
96 107
97 this.buildUI(); 108 this.buildUI();
98 this.setActionListener(this); 109 this.setActionListener(this);
110 this.bindKeys();
111 this.bindFocus();
99 this.setControlsState(false); 112 this.setControlsState(false);
100 } 113 }
101 114
@@ -253,15 +266,6 @@ abstract class UserInterface implements Viewable, ActionListener {
253 } 266 }
254 267
255 /** 268 /**
256 * Returns the layout.
257 *
258 * @return the layout
259 */
260 public JPanel getLayout() {
261 return this.layout;
262 }
263
264 /**
265 * Sets the action listener for the given JButtons. 269 * Sets the action listener for the given JButtons.
266 * 270 *
267 * @param buttons 271 * @param buttons
@@ -281,13 +285,79 @@ abstract class UserInterface implements Viewable, ActionListener {
281 * @param actionListener 285 * @param actionListener
282 * the action listener 286 * the action listener
283 */ 287 */
284 public void setActionListener(final ActionListener actionListener) { 288 private void setActionListener(final ActionListener actionListener) {
285 this.inputField.addActionListener(actionListener); 289 this.inputField.addActionListener(actionListener);
286 this.setActionListener(this.gameButtons, actionListener); 290 this.setActionListener(this.gameButtons, actionListener);
287 this.setActionListener(this.controlButtons, actionListener); 291 this.setActionListener(this.controlButtons, actionListener);
288 } 292 }
289 293
290 /** 294 /**
295 * Binds keys to corresponding buttons.
296 */
297 private void bindKeys() {
298
299 final HashMap<Integer, JButton> keys = new HashMap<>();
300 keys.put(KeyEvent.VK_LEFT, this.leftButton);
301 keys.put(KeyEvent.VK_RIGHT, this.rightButton);
302 keys.put(KeyEvent.VK_UP, this.forwardButton);
303 keys.put(KeyEvent.VK_DOWN, this.backButton);
304 keys.put(KeyEvent.VK_HOME, this.inventoryButton);
305 keys.put(KeyEvent.VK_PAGE_UP, this.actionButton);
306
307 for (final Entry<Integer, JButton> entry : keys.entrySet()) {
308 final int key = entry.getKey();
309 final JButton button = entry.getValue();
310
311 this.layout.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
312 KeyStroke.getKeyStroke(key, 0), key);
313
314 this.layout.getActionMap().put(key, new AbstractAction() {
315
316 private static final long serialVersionUID = 1L;
317
318 @Override
319 public void actionPerformed(final ActionEvent actionEvent) {
320 button.doClick();
321 }
322
323 });
324 }
325
326 }
327
328 /**
329 * Binds focus catching components to delegate key strokes to the main
330 * window.
331 */
332 public void bindFocus() {
333 final FocusListener focusLossDelegator = new FocusListener() {
334 @Override
335 public void focusGained(final FocusEvent focusEvent) {
336 }
337
338 @Override
339 public void focusLost(final FocusEvent focusEvent) {
340 UserInterface.this.layout.requestFocus();
341 }
342 };
343
344 final FocusListener focusGainDelegator = new FocusListener() {
345 @Override
346 public void focusGained(final FocusEvent focusEvent) {
347 UserInterface.this.layout.requestFocus();
348 }
349
350 @Override
351 public void focusLost(final FocusEvent focusEvent) {
352 }
353 };
354
355 this.inputField.addFocusListener(focusLossDelegator);
356 this.infoTextPane.addFocusListener(focusGainDelegator);
357 this.questTextPane.addFocusListener(focusGainDelegator);
358 }
359
360 /**
291 * Clears the textual input field. 361 * Clears the textual input field.
292 */ 362 */
293 private void clearInputField() { 363 private void clearInputField() {
@@ -385,6 +455,7 @@ abstract class UserInterface implements Viewable, ActionListener {
385 */ 455 */
386 @Override 456 @Override
387 public void actionPerformed(final ActionEvent actionEvent) { 457 public void actionPerformed(final ActionEvent actionEvent) {
458 this.echo(actionEvent.toString());
388 if (actionEvent.getActionCommand() == Text.INVENTORY_BUTTON.toString()) { 459 if (actionEvent.getActionCommand() == Text.INVENTORY_BUTTON.toString()) {
389 this.toggleInventory(); 460 this.toggleInventory();
390 } else { 461 } 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 {
155 Event.addNativePreviewHandler(new NativePreviewHandler() { 155 Event.addNativePreviewHandler(new NativePreviewHandler() {
156 @Override 156 @Override
157 public void onPreviewNativeEvent(final NativePreviewEvent event) { 157 public void onPreviewNativeEvent(final NativePreviewEvent event) {
158 // final int key = event.getNativeEvent().getKeyCode(); 158 if (WebInterface.this.inputField.getText().length() > 0) {
159 return;
160 }
161
162 if (event.getTypeInt() != Event.ONKEYDOWN) {
163 return;
164 }
165
166 switch (event.getNativeEvent().getKeyCode()) {
167 case KeyCodes.KEY_LEFT:
168 WebInterface.this.leftButton.click();
169 break;
170 case KeyCodes.KEY_RIGHT:
171 WebInterface.this.rightButton.click();
172 break;
173 case KeyCodes.KEY_UP:
174 WebInterface.this.forwardButton.click();
175 break;
176 case KeyCodes.KEY_DOWN:
177 WebInterface.this.backButton.click();
178 break;
179 case KeyCodes.KEY_HOME:
180 WebInterface.this.inventoryButton.click();
181 break;
182 case KeyCodes.KEY_PAGEUP:
183 WebInterface.this.actionButton.click();
184 break;
185 default:
186 WebInterface.this.inputField.setFocus(true);
187 break;
188 }
159 } 189 }
160 }); 190 });
161 } 191 }