diff options
author | Timothée Floure | 2016-05-09 16:37:10 +0200 |
---|---|---|
committer | Timothée Floure | 2016-05-09 16:37:10 +0200 |
commit | aa7cd18ee5b13ee4373db58dc8ab9157a348c72e (patch) | |
tree | a1ea64130b8cf4a571c8af0fece86285db6a78e4 /src/ch | |
parent | c12373535ca703ac2fbbd1d1775547b142d3970c (diff) | |
download | xblast-aa7cd18ee5b13ee4373db58dc8ab9157a348c72e.tar.gz |
Implementation of the KeyboardEventHandler Class
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/client/KeyboardEventHandler.java | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/ch/epfl/xblast/client/KeyboardEventHandler.java b/src/ch/epfl/xblast/client/KeyboardEventHandler.java new file mode 100644 index 0000000..bebc4a5 --- /dev/null +++ b/src/ch/epfl/xblast/client/KeyboardEventHandler.java | |||
@@ -0,0 +1,38 @@ | |||
1 | package ch.epfl.xblast.client; | ||
2 | |||
3 | import ch.epfl.xblast.PlayerAction; | ||
4 | |||
5 | import java.awt.event.KeyAdapter; | ||
6 | import java.awt.event.KeyEvent; | ||
7 | import java.util.Map; | ||
8 | import java.util.function.Consumer; | ||
9 | |||
10 | /** | ||
11 | * @author Timothée FLOURE (257420) | ||
12 | */ | ||
13 | public class KeyboardEventHandler extends KeyAdapter{ | ||
14 | private final Map<Integer,PlayerAction> playerActionMap; | ||
15 | private final Consumer consumer; | ||
16 | |||
17 | /** | ||
18 | * Instantiates a new KeyboardEventHandler. | ||
19 | * | ||
20 | * @param playerActionMap the map of key codes to PlayerAction. | ||
21 | * @param consumer consumer related to the EventHandler | ||
22 | */ | ||
23 | public KeyboardEventHandler(Map<Integer,PlayerAction> playerActionMap, Consumer<PlayerAction> consumer) { | ||
24 | this.playerActionMap = playerActionMap; | ||
25 | this.consumer = consumer; | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * Executes the command related to the given keycode. | ||
30 | * | ||
31 | * @param e event (pressed key) | ||
32 | */ | ||
33 | @Override | ||
34 | public void keyTyped(KeyEvent e) { | ||
35 | if (playerActionMap.containsKey(e.getKeyCode())) | ||
36 | consumer.accept(playerActionMap.get(e.getKeyCode())); | ||
37 | } | ||
38 | } | ||