public KeyboardInstant() { kbdInstant = new EnumMap<KeysConfiguration, Boolean>(KeysConfiguration.class); // Default : all keys are unpressed for (KeysConfiguration key : KeysConfiguration.values()) { kbdInstant.put(key, false); } }
/** * Serialize this object into a ByteBuffer * * @return EasyBuffering */ @Override public void serialize(EasyBuffering p_buffer) { p_buffer.clear(); int index = 0; for (KeysConfiguration key : KeysConfiguration.values()) { bools[index++] = kbdInstant.get(key); } p_buffer.putBooleans(bools); }
/** Update keyboard state */ public void update() { if (kbHandler == null) { kbHandler = Zildo.pdPlugin.kbHandler; } for (KeysConfiguration key : KeysConfiguration.values()) { kbdInstant.put(key, kbHandler.isKeyDown(key.code)); } currentDirection = kbHandler.getDirection(); }
/** * Deserialize a ByteBuffer into a KeyboardInstant object. * * @param p_buffer * @return */ public static KeyboardInstant deserialize(EasyBuffering p_buffer) { EnumMap<KeysConfiguration, Boolean> instant = new EnumMap<KeysConfiguration, Boolean>(KeysConfiguration.class); boolean[] bools = p_buffer.readBooleans(KEYS_LENGTH); int index = 0; for (KeysConfiguration key : KeysConfiguration.values()) { instant.put(key, bools[index++]); } return new KeyboardInstant(instant); }
public class KeyboardInstant implements EasySerializable { KeyboardHandler kbHandler = Zildo.pdPlugin.kbHandler; EnumMap<KeysConfiguration, Boolean> kbdInstant; Vector2f currentDirection; boolean[] bools = new boolean[KEYS_LENGTH]; static int KEYS_LENGTH = KeysConfiguration.values().length; static EasyBuffering buf = new EasyBuffering(KEYS_LENGTH * 4); public KeyboardInstant() { kbdInstant = new EnumMap<KeysConfiguration, Boolean>(KeysConfiguration.class); // Default : all keys are unpressed for (KeysConfiguration key : KeysConfiguration.values()) { kbdInstant.put(key, false); } } public KeyboardInstant(EnumMap<KeysConfiguration, Boolean> p_keys) { kbdInstant = p_keys; } public boolean isKeyDown(KeysConfiguration key) { return kbdInstant.get(key); } public void setKey(KeysConfiguration key, boolean value) { kbdInstant.put(key, value); } public Vector2f getDirection() { return currentDirection; } /** Update keyboard state */ public void update() { if (kbHandler == null) { kbHandler = Zildo.pdPlugin.kbHandler; } for (KeysConfiguration key : KeysConfiguration.values()) { kbdInstant.put(key, kbHandler.isKeyDown(key.code)); } currentDirection = kbHandler.getDirection(); } public EasyBuffering serialize() { serialize(buf); return buf; } /** * Serialize this object into a ByteBuffer * * @return EasyBuffering */ @Override public void serialize(EasyBuffering p_buffer) { p_buffer.clear(); int index = 0; for (KeysConfiguration key : KeysConfiguration.values()) { bools[index++] = kbdInstant.get(key); } p_buffer.putBooleans(bools); } /** * Deserialize a ByteBuffer into a KeyboardInstant object. * * @param p_buffer * @return */ public static KeyboardInstant deserialize(EasyBuffering p_buffer) { EnumMap<KeysConfiguration, Boolean> instant = new EnumMap<KeysConfiguration, Boolean>(KeysConfiguration.class); boolean[] bools = p_buffer.readBooleans(KEYS_LENGTH); int index = 0; for (KeysConfiguration key : KeysConfiguration.values()) { instant.put(key, bools[index++]); } return new KeyboardInstant(instant); } }