From 900404c33330552f81617e04eeb825ff20fc2d17 Mon Sep 17 00:00:00 2001 From: "SND\\simsmaster_cp" Date: Mon, 2 Jan 2012 16:45:51 +0000 Subject: [PATCH] Completed work for RecordingKeyboard, updated sample Game. --- .../RecordableDevice.cs | 8 ++- .../RecordingKeyboard.cs | 8 +-- RecordingSample/RecordingSample/Game1.cs | 58 +++++++++++++------ 3 files changed, 49 insertions(+), 25 deletions(-) diff --git a/InputSystems/ANX.InputSystem.Recording/RecordableDevice.cs b/InputSystems/ANX.InputSystem.Recording/RecordableDevice.cs index 6e90e1ca..69336fa9 100644 --- a/InputSystems/ANX.InputSystem.Recording/RecordableDevice.cs +++ b/InputSystems/ANX.InputSystem.Recording/RecordableDevice.cs @@ -113,6 +113,7 @@ namespace ANX.InputSystem.Recording if (RecordingState != RecordingState.Recording) throw new InvalidOperationException("Recording wasn't started for this device!"); + nullStateCounter = 0; RecordingState = RecordingState.None; } @@ -155,6 +156,7 @@ namespace ANX.InputSystem.Recording { recordStream.WriteByte((byte)PacketType.NullFrameCounter); recordStream.Write(BitConverter.GetBytes(nullStateCounter), 0, 4); + nullStateCounter = 0; } recordStream.WriteByte((byte)PacketType.InputData); @@ -198,11 +200,13 @@ namespace ANX.InputSystem.Recording } /// - /// Fires the EndOfPlaybackReaced event. Overwrite this method to change - /// this behavoir. + /// Fires the EndOfPlaybackReaced event and Calls StopPlayback(). + /// Overwrite this method to change this behavoir. /// protected virtual void OnEndOfPlaybackReached() { + StopPlayback(); + if (EndOfPlaybackReached != null) EndOfPlaybackReached(this, EventArgs.Empty); } diff --git a/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs b/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs index a7a6c2c0..b00b62ff 100644 --- a/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs +++ b/InputSystems/ANX.InputSystem.Recording/RecordingKeyboard.cs @@ -140,7 +140,7 @@ namespace ANX.InputSystem.Recording { for (int j = 0; j < 8; j++) { - if (i == PacketLenght - 1 && j == (i + 2) % 8) + if (i == PacketLenght - 1 && j == recordedKeys.Length % 8) break; if (state.IsKeyDown(recordedKeys[i * 8 + j])) @@ -156,18 +156,18 @@ namespace ANX.InputSystem.Recording byte[] buffer = ReadState(); if (buffer == null) - return new KeyboardState(); + return new KeyboardState(new Keys[0]); 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(); + KeyboardState state = new KeyboardState(new Keys[0]); for (int i = 0; i < PacketLenght; i++) { for (int j = 0; j < 8; j++) { - if (i == PacketLenght - 1 && j == (i + 2) % 8) + if (i == PacketLenght - 1 && j == recordedKeys.Length % 8) break; if ((buffer[i] & keyBitmasks[i * 8 + j]) != 0) diff --git a/RecordingSample/RecordingSample/Game1.cs b/RecordingSample/RecordingSample/Game1.cs index 0c411b5e..3fb52d06 100644 --- a/RecordingSample/RecordingSample/Game1.cs +++ b/RecordingSample/RecordingSample/Game1.cs @@ -70,6 +70,9 @@ namespace RecordingSample Texture2D logo; KeyboardState oldState; + RecordingMouse recMouse; + RecordingKeyboard recKeyboard; + public Game1() { graphics = new GraphicsDeviceManager(this); @@ -80,8 +83,11 @@ namespace RecordingSample { Window.Title = "Use Mouse to move arround, press r to record, p for playback and n for none"; - //We know the Mouse is a RecordingMouse - this is quite ugly... could this be improved? - ((RecordingMouse)AddInSystemFactory.Instance.GetDefaultCreator().Mouse).Initialize(MouseRecordInfo.Position); + //this is quite ugly... could this be improved? + recMouse = ((RecordingMouse)AddInSystemFactory.Instance.GetDefaultCreator().Mouse); + recMouse.Initialize(MouseRecordInfo.Position); + recKeyboard = ((RecordingKeyboard)AddInSystemFactory.Instance.GetDefaultCreator().Keyboard); + recKeyboard.Initialize(Keys.Enter); base.Initialize(); } @@ -91,32 +97,42 @@ namespace RecordingSample spriteBatch = new SpriteBatch(GraphicsDevice); logo = Content.Load(@"Textures/ANX.Framework.Logo_459x121"); - //oldState = Keyboard.GetState(); + oldState = Keyboard.GetState(); } protected override void Update(GameTime gameTime) { - //KeyboardState newState = Keyboard.GetState(); + KeyboardState newState = Keyboard.GetState(); - //if (oldState.IsKeyUp(Keys.R) && newState.IsKeyDown(Keys.R)) - // mouse.StartRecording(); + if (oldState.IsKeyUp(Keys.R) && newState.IsKeyDown(Keys.R)) + { + recMouse.StartRecording(); + recKeyboard.StartRecording(); + } - //if (oldState.IsKeyUp(Keys.P) && newState.IsKeyDown(Keys.P)) - //{ - // if (mouse.RecordingState == RecordingState.Recording) - // mouse.StopRecording(); - // mouse.StartPlayback(); - //} + if (oldState.IsKeyUp(Keys.P) && newState.IsKeyDown(Keys.P)) + { + if (recMouse.RecordingState == RecordingState.Recording) + recMouse.StopRecording(); + recMouse.StartPlayback(); - //if (oldState.IsKeyUp(Keys.N) && newState.IsKeyDown(Keys.N)) - //{ - // if (mouse.RecordingState == RecordingState.Recording) - // mouse.StartRecording(); + if (recKeyboard.RecordingState == RecordingState.Recording) + recKeyboard.StopRecording(); + recKeyboard.StartPlayback(); + } - // mouse.StopPlayback(); - //} + if (oldState.IsKeyUp(Keys.N) && newState.IsKeyDown(Keys.N)) + { + if (recMouse.RecordingState == RecordingState.Recording) + recMouse.StopRecording(); + recMouse.StopPlayback(); - //oldState = newState; + if (recKeyboard.RecordingState == RecordingState.Recording) + recKeyboard.StopRecording(); + recKeyboard.StopPlayback(); + } + + oldState = newState; base.Update(gameTime); } @@ -125,6 +141,10 @@ namespace RecordingSample { GraphicsDevice.Clear(Color.CornflowerBlue); + spriteBatch.Begin(); + if(Keyboard.GetState().IsKeyDown(Keys.Enter)) + spriteBatch.Draw(logo, Vector2.Zero, Color.White); + spriteBatch.End(); spriteBatch.Begin(); spriteBatch.Draw(logo, new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 115, 30), Color.White); spriteBatch.End();