From 369b4bf67282f62e556ac8a9471acd501ab50fd0 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 7 Aug 2019 22:25:33 +0200 Subject: [PATCH] added a simple input handler as well as some changes to the startup package --- MLEM.Extended/MLEM.Extended.csproj | 6 +- MLEM.Startup/CoroutineEvents.cs | 10 ++ MLEM.Startup/MLEM.Startup.csproj | 11 +-- MLEM.Startup/MlemGame.cs | 24 +++-- MLEM.Ui/MLEM.Ui.csproj | 26 ++++++ MLEM.sln | 6 ++ MLEM/Input/InputHandler.cs | 143 +++++++++++++++++++++++++++++ MLEM/MLEM.csproj | 2 +- Tests/GameImpl.cs | 15 +++ 9 files changed, 222 insertions(+), 21 deletions(-) create mode 100644 MLEM.Startup/CoroutineEvents.cs create mode 100644 MLEM.Ui/MLEM.Ui.csproj create mode 100644 MLEM/Input/InputHandler.cs diff --git a/MLEM.Extended/MLEM.Extended.csproj b/MLEM.Extended/MLEM.Extended.csproj index b6327c9..78937f3 100644 --- a/MLEM.Extended/MLEM.Extended.csproj +++ b/MLEM.Extended/MLEM.Extended.csproj @@ -14,15 +14,13 @@ - - all - + all - + \ No newline at end of file diff --git a/MLEM.Startup/CoroutineEvents.cs b/MLEM.Startup/CoroutineEvents.cs new file mode 100644 index 0000000..f303483 --- /dev/null +++ b/MLEM.Startup/CoroutineEvents.cs @@ -0,0 +1,10 @@ +using Coroutine; + +namespace MLEM.Startup { + public static class CoroutineEvents { + + public static readonly Event Update = new Event(); + public static readonly Event Draw = new Event(); + + } +} \ No newline at end of file diff --git a/MLEM.Startup/MLEM.Startup.csproj b/MLEM.Startup/MLEM.Startup.csproj index 31220e8..f9b8de4 100644 --- a/MLEM.Startup/MLEM.Startup.csproj +++ b/MLEM.Startup/MLEM.Startup.csproj @@ -11,20 +11,19 @@ https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM/blob/master/LICENSE - 1.0.0 + 1.0.1 - - - + + all - - + + diff --git a/MLEM.Startup/MlemGame.cs b/MLEM.Startup/MlemGame.cs index cba95a9..7f150d6 100644 --- a/MLEM.Startup/MlemGame.cs +++ b/MLEM.Startup/MlemGame.cs @@ -2,21 +2,18 @@ using Coroutine; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using MLEM.Input; using MonoGame.Extended; -using MonoGame.Extended.Input; namespace MLEM.Startup { public class MlemGame : Game { private static MlemGame instance; - public static KeyboardStateExtended Keyboard => instance.keyboardState; - public static MouseStateExtended Mouse => instance.mouseState; + public static InputHandler Input => instance.InputHandler; public readonly GraphicsDeviceManager GraphicsDeviceManager; - public SpriteBatch SpriteBatch { get; private set; } - - private KeyboardStateExtended keyboardState; - private MouseStateExtended mouseState; + public SpriteBatch SpriteBatch { get; protected set; } + public InputHandler InputHandler { get; protected set; } public MlemGame(int windowWidth = 1280, int windowHeight = 720, bool vsync = false, bool allowResizing = true, string contentDir = "Content") { instance = this; @@ -40,6 +37,7 @@ namespace MLEM.Startup { protected override void LoadContent() { this.SpriteBatch = new SpriteBatch(this.GraphicsDevice); + this.InputHandler = new InputHandler(); } protected override void Initialize() { @@ -50,10 +48,16 @@ namespace MLEM.Startup { protected override void Update(GameTime gameTime) { base.Update(gameTime); - CoroutineHandler.Tick(gameTime.GetElapsedSeconds()); + if (this.InputHandler != null) + this.InputHandler.Update(); - this.keyboardState = KeyboardExtended.GetState(); - this.mouseState = MouseExtended.GetState(); + CoroutineHandler.Tick(gameTime.GetElapsedSeconds()); + CoroutineHandler.RaiseEvent(CoroutineEvents.Update); + } + + protected override void Draw(GameTime gameTime) { + base.Draw(gameTime); + CoroutineHandler.RaiseEvent(CoroutineEvents.Draw); } public static T LoadContent(string name) { diff --git a/MLEM.Ui/MLEM.Ui.csproj b/MLEM.Ui/MLEM.Ui.csproj new file mode 100644 index 0000000..c4b8c3a --- /dev/null +++ b/MLEM.Ui/MLEM.Ui.csproj @@ -0,0 +1,26 @@ + + + netstandard2.0 + + + + Ellpeck + A Ui system that is part of (M)LEM (L)ibrary by (E)llpeck for (M)onoGame + monogame ellpeck mlem utility extensions + https://github.com/Ellpeck/MLEM + https://github.com/Ellpeck/MLEM + https://github.com/Ellpeck/MLEM/blob/master/LICENSE + 1.0.0 + + + + + + all + + + + + + + \ No newline at end of file diff --git a/MLEM.sln b/MLEM.sln index 4012969..fd155a4 100644 --- a/MLEM.sln +++ b/MLEM.sln @@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Startup", "MLEM.Startu EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{1BC4682B-AA14-4937-B5C7-707E20FE88FF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Ui", "MLEM.Ui\MLEM.Ui.csproj", "{6F00629A-8B87-4264-8896-19983285E32F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -30,5 +32,9 @@ Global {1BC4682B-AA14-4937-B5C7-707E20FE88FF}.Debug|Any CPU.Build.0 = Debug|Any CPU {1BC4682B-AA14-4937-B5C7-707E20FE88FF}.Release|Any CPU.ActiveCfg = Release|Any CPU {1BC4682B-AA14-4937-B5C7-707E20FE88FF}.Release|Any CPU.Build.0 = Release|Any CPU + {6F00629A-8B87-4264-8896-19983285E32F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6F00629A-8B87-4264-8896-19983285E32F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6F00629A-8B87-4264-8896-19983285E32F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6F00629A-8B87-4264-8896-19983285E32F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/MLEM/Input/InputHandler.cs b/MLEM/Input/InputHandler.cs new file mode 100644 index 0000000..a996fae --- /dev/null +++ b/MLEM/Input/InputHandler.cs @@ -0,0 +1,143 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; + +namespace MLEM.Input { + public class InputHandler { + + public KeyboardState LastKeyboardState { get; private set; } + public KeyboardState KeyboardState { get; private set; } + private readonly bool handleKeyboard; + + public MouseState LastMouseState { get; private set; } + public MouseState MouseState { get; private set; } + public Point MousePosition => this.MouseState.Position; + public Point LastMousePosition => this.LastMouseState.Position; + public int ScrollWheel => this.MouseState.ScrollWheelValue; + public int LastScrollWheel => this.LastMouseState.ScrollWheelValue; + private readonly bool handleMouse; + + private readonly GamePadState[] lastGamepads = new GamePadState[GamePad.MaximumGamePadCount]; + private readonly GamePadState[] gamepads = new GamePadState[GamePad.MaximumGamePadCount]; + private readonly bool handleGamepads; + + public InputHandler(bool handleKeyboard = true, bool handleMouse = true, bool handleGamepads = true) { + this.handleKeyboard = handleKeyboard; + this.handleMouse = handleMouse; + this.handleGamepads = handleGamepads; + } + + public void Update() { + if (this.handleKeyboard) { + this.LastKeyboardState = this.KeyboardState; + this.KeyboardState = Keyboard.GetState(); + } + if (this.handleMouse) { + this.LastMouseState = this.MouseState; + this.MouseState = Mouse.GetState(); + } + if (this.handleGamepads) { + for (var i = 0; i < GamePad.MaximumGamePadCount; i++) { + this.lastGamepads[i] = this.gamepads[i]; + this.gamepads[i] = GamePad.GetState(i); + } + } + } + + public GamePadState GetLastGamepadState(int index) { + return this.lastGamepads[index]; + } + + public GamePadState GetGamepadState(int index) { + return this.gamepads[index]; + } + + public bool IsKeyDown(Keys key) { + return this.KeyboardState.IsKeyDown(key); + } + + public bool IsKeyUp(Keys key) { + return this.KeyboardState.IsKeyUp(key); + } + + public bool WasKeyDown(Keys key) { + return this.LastKeyboardState.IsKeyDown(key); + } + + public bool WasKeyUp(Keys key) { + return this.LastKeyboardState.IsKeyUp(key); + } + + public bool IsKeyPressed(Keys key) { + return this.WasKeyUp(key) && this.IsKeyDown(key); + } + + public bool IsMouseButtonDown(MouseButton button) { + return GetState(this.MouseState, button) == ButtonState.Pressed; + } + + public bool IsMouseButtonUp(MouseButton button) { + return GetState(this.MouseState, button) == ButtonState.Released; + } + + public bool WasMouseButtonDown(MouseButton button) { + return GetState(this.LastMouseState, button) == ButtonState.Pressed; + } + + public bool WasMouseButtonUp(MouseButton button) { + return GetState(this.LastMouseState, button) == ButtonState.Released; + } + + public bool IsMouseButtonPressed(MouseButton button) { + return this.WasMouseButtonUp(button) && this.IsMouseButtonDown(button); + } + + public bool IsGamepadButtonDown(int index, Buttons button) { + return this.GetGamepadState(index).IsButtonDown(button); + } + + public bool IsGamepadButtonUp(int index, Buttons button) { + return this.GetGamepadState(index).IsButtonUp(button); + } + + public bool WasGamepadButtonDown(int index, Buttons button) { + return this.GetLastGamepadState(index).IsButtonDown(button); + } + + public bool WasGamepadButtonUp(int index, Buttons button) { + return this.GetLastGamepadState(index).IsButtonUp(button); + } + + public bool IsGamepadButtonPressed(int index, Buttons button) { + return this.WasGamepadButtonUp(index, button) && this.IsGamepadButtonDown(index, button); + } + + private static ButtonState GetState(MouseState state, MouseButton button) { + switch (button) { + case MouseButton.Left: + return state.LeftButton; + case MouseButton.Middle: + return state.MiddleButton; + case MouseButton.Right: + return state.RightButton; + case MouseButton.Extra1: + return state.XButton1; + case MouseButton.Extra2: + return state.XButton2; + default: + throw new ArgumentException(nameof(button)); + } + } + + } + + public enum MouseButton { + + Left, + Middle, + Right, + Extra1, + Extra2 + + } +} \ No newline at end of file diff --git a/MLEM/MLEM.csproj b/MLEM/MLEM.csproj index 74bc156..95e7c84 100644 --- a/MLEM/MLEM.csproj +++ b/MLEM/MLEM.csproj @@ -10,7 +10,7 @@ https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM https://github.com/Ellpeck/MLEM/blob/master/LICENSE - 1.0.10 + 1.0.11 diff --git a/Tests/GameImpl.cs b/Tests/GameImpl.cs index 7e988ef..ba63c6a 100644 --- a/Tests/GameImpl.cs +++ b/Tests/GameImpl.cs @@ -1,8 +1,23 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; +using MLEM.Input; using MLEM.Startup; namespace Tests { public class GameImpl : MlemGame { + protected override void Update(GameTime gameTime) { + base.Update(gameTime); + + // Input tests + if (Input.IsKeyPressed(Keys.A)) + Console.WriteLine("A was pressed"); + if (Input.IsMouseButtonPressed(MouseButton.Left)) + Console.WriteLine("Left was pressed"); + if (Input.IsGamepadButtonPressed(0, Buttons.A)) + Console.WriteLine("Gamepad A was pressed"); + } } } \ No newline at end of file