1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

added a simple input handler as well as some changes to the startup package

This commit is contained in:
Ellpeck 2019-08-07 22:25:33 +02:00
parent 38a9fbaac0
commit 369b4bf672
9 changed files with 222 additions and 21 deletions

View file

@ -14,15 +14,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MonoGame.Extended" Version="3.6.0-beta0001"> <PackageReference Include="MonoGame.Extended" Version="3.6.0-beta0001"/>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625"> <PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MLEM\MLEM.csproj" /> <ProjectReference Include="..\MLEM\MLEM.csproj"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -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();
}
}

View file

@ -11,20 +11,19 @@
<PackageProjectUrl>https://github.com/Ellpeck/MLEM</PackageProjectUrl> <PackageProjectUrl>https://github.com/Ellpeck/MLEM</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl> <RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl>
<PackageLicenseUrl>https://github.com/Ellpeck/MLEM/blob/master/LICENSE</PackageLicenseUrl> <PackageLicenseUrl>https://github.com/Ellpeck/MLEM/blob/master/LICENSE</PackageLicenseUrl>
<Version>1.0.0</Version> <Version>1.0.1</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Coroutine" Version="1.0.1"/> <PackageReference Include="Coroutine" Version="1.0.1" />
<PackageReference Include="MonoGame.Extended" Version="3.6.0-beta0001"/> <PackageReference Include="MonoGame.Extended" Version="3.6.0-beta0001" />
<PackageReference Include="MonoGame.Extended.Input" Version="3.6.0-beta0001"/>
<PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625"> <PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj"/> <ProjectReference Include="..\MLEM.Extended\MLEM.Extended.csproj" />
<ProjectReference Include="..\MLEM\MLEM.csproj"/> <ProjectReference Include="..\MLEM\MLEM.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -2,21 +2,18 @@ using Coroutine;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using MLEM.Input;
using MonoGame.Extended; using MonoGame.Extended;
using MonoGame.Extended.Input;
namespace MLEM.Startup { namespace MLEM.Startup {
public class MlemGame : Game { public class MlemGame : Game {
private static MlemGame instance; private static MlemGame instance;
public static KeyboardStateExtended Keyboard => instance.keyboardState; public static InputHandler Input => instance.InputHandler;
public static MouseStateExtended Mouse => instance.mouseState;
public readonly GraphicsDeviceManager GraphicsDeviceManager; public readonly GraphicsDeviceManager GraphicsDeviceManager;
public SpriteBatch SpriteBatch { get; private set; } public SpriteBatch SpriteBatch { get; protected set; }
public InputHandler InputHandler { get; protected set; }
private KeyboardStateExtended keyboardState;
private MouseStateExtended mouseState;
public MlemGame(int windowWidth = 1280, int windowHeight = 720, bool vsync = false, bool allowResizing = true, string contentDir = "Content") { public MlemGame(int windowWidth = 1280, int windowHeight = 720, bool vsync = false, bool allowResizing = true, string contentDir = "Content") {
instance = this; instance = this;
@ -40,6 +37,7 @@ namespace MLEM.Startup {
protected override void LoadContent() { protected override void LoadContent() {
this.SpriteBatch = new SpriteBatch(this.GraphicsDevice); this.SpriteBatch = new SpriteBatch(this.GraphicsDevice);
this.InputHandler = new InputHandler();
} }
protected override void Initialize() { protected override void Initialize() {
@ -50,10 +48,16 @@ namespace MLEM.Startup {
protected override void Update(GameTime gameTime) { protected override void Update(GameTime gameTime) {
base.Update(gameTime); base.Update(gameTime);
CoroutineHandler.Tick(gameTime.GetElapsedSeconds()); if (this.InputHandler != null)
this.InputHandler.Update();
this.keyboardState = KeyboardExtended.GetState(); CoroutineHandler.Tick(gameTime.GetElapsedSeconds());
this.mouseState = MouseExtended.GetState(); CoroutineHandler.RaiseEvent(CoroutineEvents.Update);
}
protected override void Draw(GameTime gameTime) {
base.Draw(gameTime);
CoroutineHandler.RaiseEvent(CoroutineEvents.Draw);
} }
public static T LoadContent<T>(string name) { public static T LoadContent<T>(string name) {

26
MLEM.Ui/MLEM.Ui.csproj Normal file
View file

@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<Authors>Ellpeck</Authors>
<Description>A Ui system that is part of (M)LEM (L)ibrary by (E)llpeck for (M)onoGame</Description>
<PackageTags>monogame ellpeck mlem utility extensions</PackageTags>
<PackageProjectUrl>https://github.com/Ellpeck/MLEM</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl>
<PackageLicenseUrl>https://github.com/Ellpeck/MLEM/blob/master/LICENSE</PackageLicenseUrl>
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Extended" Version="3.6.0-beta0001" />
<PackageReference Include="MonoGame.Framework.Portable" Version="3.6.0.1625">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MLEM\MLEM.csproj" />
</ItemGroup>
</Project>

View file

@ -8,6 +8,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Startup", "MLEM.Startu
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{1BC4682B-AA14-4937-B5C7-707E20FE88FF}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{1BC4682B-AA14-4937-B5C7-707E20FE88FF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MLEM.Ui", "MLEM.Ui\MLEM.Ui.csproj", "{6F00629A-8B87-4264-8896-19983285E32F}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{1BC4682B-AA14-4937-B5C7-707E20FE88FF}.Release|Any CPU.Build.0 = 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 EndGlobalSection
EndGlobal EndGlobal

143
MLEM/Input/InputHandler.cs Normal file
View file

@ -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
}
}

View file

@ -10,7 +10,7 @@
<PackageProjectUrl>https://github.com/Ellpeck/MLEM</PackageProjectUrl> <PackageProjectUrl>https://github.com/Ellpeck/MLEM</PackageProjectUrl>
<RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl> <RepositoryUrl>https://github.com/Ellpeck/MLEM</RepositoryUrl>
<PackageLicenseUrl>https://github.com/Ellpeck/MLEM/blob/master/LICENSE</PackageLicenseUrl> <PackageLicenseUrl>https://github.com/Ellpeck/MLEM/blob/master/LICENSE</PackageLicenseUrl>
<Version>1.0.10</Version> <Version>1.0.11</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View file

@ -1,8 +1,23 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using MLEM.Input;
using MLEM.Startup; using MLEM.Startup;
namespace Tests { namespace Tests {
public class GameImpl : MlemGame { 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");
}
} }
} }