From 83c7a8c1b46ca173dfcb3252d9136dfe15d3376d Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 9 May 2016 19:50:58 +0200 Subject: Clean keyboard handler --- .../epfl/xblast/client/KeyboardEventHandler.java | 27 ++++++++++++++-------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/ch/epfl/xblast/client/KeyboardEventHandler.java b/src/ch/epfl/xblast/client/KeyboardEventHandler.java index bebc4a5..a0d3848 100644 --- a/src/ch/epfl/xblast/client/KeyboardEventHandler.java +++ b/src/ch/epfl/xblast/client/KeyboardEventHandler.java @@ -1,38 +1,47 @@ package ch.epfl.xblast.client; +import ch.epfl.xblast.Lists; import ch.epfl.xblast.PlayerAction; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Map; +import java.util.Objects; import java.util.function.Consumer; /** + * The game action keyboard event handler. + * * @author Timothée FLOURE (257420) + * @author Pacien TRAN-GIRARD (261948) */ -public class KeyboardEventHandler extends KeyAdapter{ - private final Map playerActionMap; - private final Consumer consumer; +public class KeyboardEventHandler extends KeyAdapter { + + private final Map playerActionMap; + private final Consumer consumer; /** * Instantiates a new KeyboardEventHandler. * * @param playerActionMap the map of key codes to PlayerAction. - * @param consumer consumer related to the EventHandler + * @param consumer consumer related to the EventHandler */ - public KeyboardEventHandler(Map playerActionMap, Consumer consumer) { - this.playerActionMap = playerActionMap; + public KeyboardEventHandler(Map playerActionMap, Consumer consumer) { + this.playerActionMap = Lists.immutableMap(playerActionMap); this.consumer = consumer; } /** - * Executes the command related to the given keycode. + * Executes the command related to the given key code. * * @param e event (pressed key) */ @Override public void keyTyped(KeyEvent e) { - if (playerActionMap.containsKey(e.getKeyCode())) - consumer.accept(playerActionMap.get(e.getKeyCode())); + PlayerAction act = this.playerActionMap.get(e.getKeyCode()); + + if (Objects.nonNull(act)) + this.consumer.accept(act); } + } -- cgit v1.2.3