From a32da95cdeb9fbfe4fd56795c9de78acd4542ca3 Mon Sep 17 00:00:00 2001 From: "SND\\simsmaster_cp" Date: Thu, 29 Dec 2011 17:11:12 +0000 Subject: [PATCH] Added Recording and Playback logic for the Keyboard - untested! --- ANX.Framework/Properties/AssemblyInfo.cs | 1 + .../RecordingKeyboard.cs | 103 +++++++++++++++++- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/ANX.Framework/Properties/AssemblyInfo.cs b/ANX.Framework/Properties/AssemblyInfo.cs index 9f6f8c67..518db61f 100644 --- a/ANX.Framework/Properties/AssemblyInfo.cs +++ b/ANX.Framework/Properties/AssemblyInfo.cs @@ -40,3 +40,4 @@ using System.Runtime.InteropServices; [assembly:InternalsVisibleTo("ANX.Framework.Windows.Kinect")] [assembly:InternalsVisibleTo("ANX.Framework.Windows.XInput")] [assembly:InternalsVisibleTo("ANX.Framework.Windows.XAudio")] +[assembly:InternalsVisibleTo("ANX.InputSystem.Recording")] \ No newline at end of file diff --git a/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs b/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs index ecd21737..a7a6c2c0 100644 --- a/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs +++ b/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs @@ -66,6 +66,7 @@ namespace ANX.InputSystem.Recording { private IKeyboard realKeyboard; private Keys[] recordedKeys; + private byte[] keyBitmasks; private IntPtr tmpWindowHandle = IntPtr.Zero; @@ -87,14 +88,94 @@ namespace ANX.InputSystem.Recording } } - public Framework.Input.KeyboardState GetState() + public KeyboardState GetState() { - throw new NotImplementedException(); + switch (RecordingState) + { + case RecordingState.None: + return realKeyboard.GetState(); + case RecordingState.Playback: + return ReadKeybaordState(PlayerIndex.One); + case RecordingState.Recording: + KeyboardState realState = realKeyboard.GetState(); + WriteKeyboardState(realState, PlayerIndex.One); + return realState; + default: + throw new InvalidOperationException("The recordingState is invalid!"); + } } - public Framework.Input.KeyboardState GetState(PlayerIndex playerIndex) + public KeyboardState GetState(PlayerIndex playerIndex) { - throw new NotImplementedException(); + switch (RecordingState) + { + case RecordingState.None: + return realKeyboard.GetState(playerIndex); + case RecordingState.Playback: + return ReadKeybaordState(playerIndex); + case RecordingState.Recording: + KeyboardState realState = realKeyboard.GetState(playerIndex); + WriteKeyboardState(realState, playerIndex); + return realState; + default: + throw new InvalidOperationException("The recordingState is invalid!"); + } + } + + private void WriteKeyboardState(KeyboardState state, PlayerIndex index) + { + Keys[] pressedKeys = state.GetPressedKeys(); + + if (pressedKeys.Length == 0 || !pressedKeys.Any((key) => recordedKeys.Contains(key))) //No Key or none of the recorded keys is down + { + WriteState(null); + return; + } + + byte[] buffer = new byte[PacketLenght]; + + buffer[0] = (byte)((int)index & 3); //Just the first two bits + + for (int i = 0; i < PacketLenght; i++) + { + for (int j = 0; j < 8; j++) + { + if (i == PacketLenght - 1 && j == (i + 2) % 8) + break; + + if (state.IsKeyDown(recordedKeys[i * 8 + j])) + buffer[i] |= keyBitmasks[i * 8 + j]; + } + } + + WriteState(buffer); + } + + private KeyboardState ReadKeybaordState(PlayerIndex expectedIndex) + { + byte[] buffer = ReadState(); + + if (buffer == null) + return new KeyboardState(); + + if ((PlayerIndex)(buffer[0] & 3) != expectedIndex) + throw new InvalidOperationException("The requested playerIndex does no match the next recorded state. Refer to documetation."); + + KeyboardState state = new KeyboardState(); + + for (int i = 0; i < PacketLenght; i++) + { + for (int j = 0; j < 8; j++) + { + if (i == PacketLenght - 1 && j == (i + 2) % 8) + break; + + if ((buffer[i] & keyBitmasks[i * 8 + j]) != 0) + state.AddPressedKey(recordedKeys[i * 8 + j]); + } + } + + return state; } public void Dispose() @@ -142,7 +223,9 @@ namespace ANX.InputSystem.Recording if (tmpWindowHandle != IntPtr.Zero) WindowHandle = tmpWindowHandle; - PacketLenght = keys.Length / 8; //8bit per byte + PacketLenght = GetPaketSize(); //8bit per byte + + UpdateBitmasks(); base.Initialize(bufferStream); } @@ -154,5 +237,15 @@ namespace ANX.InputSystem.Recording else return (int)Math.Ceiling((double)recordedKeys.Length / 8.0) + 1; //we need a additional byte to store the player index } + + private void UpdateBitmasks() + { + keyBitmasks = new byte[recordedKeys.Length]; + + for (int i = 0; i < recordedKeys.Length; i++) + { + keyBitmasks[i] = (byte)Math.Pow(2.0, (i + 2) % 8); //The first two bits are reserved for the player index + } + } } }