Created
March 8, 2019 16:10
-
-
Save MaximovInk/98f5cdf321c5807d4b927e2aa58e45f0 to your computer and use it in GitHub Desktop.
Monogame Input like Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Input; | |
public static class Input | |
{ | |
#region Keyboard | |
private static KeyboardState kstate; | |
private static KeyboardState last_kstate; | |
/// <summary> | |
/// Is key pressed | |
/// </summary> | |
public static bool GetKey(Keys key) => kstate.IsKeyDown(key); | |
/// <summary> | |
/// Is key pressed once | |
/// </summary> | |
public static bool GetKeyDown(Keys key) => kstate.IsKeyDown(key) && !last_kstate.IsKeyDown(key); | |
/// <summary> | |
/// Is key released | |
/// </summary> | |
public static bool GetKeyUp(Keys key) => kstate.IsKeyUp(key); | |
/// <summary> | |
/// Caps lock is active | |
/// </summary> | |
public static bool Caps => kstate.CapsLock; | |
#endregion | |
#region Mouse | |
private static MouseState mstate; | |
private static MouseState last_mstate; | |
/// <summary> | |
/// Delta of mouse scroll wheel | |
/// </summary> | |
public static int MouseScrollWheelDelta => mstate.ScrollWheelValue - last_mstate.ScrollWheelValue; | |
/// <summary> | |
/// Mouse position | |
/// </summary> | |
public static Vector2 MousePosition => new Vector2(mstate.X, mstate.Y); | |
/// <summary> | |
/// Delta of mouse position | |
/// </summary> | |
public static Vector2 MousePositionDelta => new Vector2(mstate.X - last_mstate.X , mstate.Y - last_mstate.Y); | |
/// <summary> | |
/// Current mouse scroll wheel value | |
/// </summary> | |
public static int MouseScrollWheelValue => mstate.ScrollWheelValue; | |
/// <summary> | |
/// Mouse button is pressed | |
/// </summary> | |
/// <param name="index">0-left , 1 - right , 2 - middle , 3 - any</param> | |
public static bool GetMouseButton(int index) | |
{ | |
bool | |
l = mstate.LeftButton == ButtonState.Pressed , | |
r = mstate.RightButton == ButtonState.Pressed , | |
m = mstate.MiddleButton == ButtonState.Pressed; | |
switch (index) | |
{ | |
case 0: | |
return l; | |
case 1: | |
return r; | |
case 2: | |
return m; | |
case 3: | |
return l || r || m; | |
} | |
return false; | |
} | |
/// <summary> | |
/// Mouse button is pressed once | |
/// </summary> | |
/// <param name="index">0-left , 1 - right , 2 - middle , 3 - any</param> | |
public static bool GetMouseButtonDown(int index) | |
{ | |
bool | |
l = mstate.LeftButton == ButtonState.Pressed && last_mstate.LeftButton != ButtonState.Pressed, | |
r = mstate.RightButton == ButtonState.Pressed && last_mstate.RightButton != ButtonState.Pressed, | |
m = mstate.MiddleButton == ButtonState.Pressed && last_mstate.MiddleButton != ButtonState.Pressed; | |
switch (index) | |
{ | |
case 0: | |
return l; | |
case 1: | |
return r; | |
case 2: | |
return m; | |
case 3: | |
return l || r || m; | |
} | |
return false; | |
} | |
/// <summary> | |
/// Mouse button is released | |
/// </summary> | |
/// <param name="index">0-left , 1 - right , 2 - middle</param> | |
public static bool GetMouseButtonUp(int index) | |
{ | |
return !GetMouseButton(index); | |
} | |
#endregion | |
/// <summary> | |
/// Update inputs | In begin Update() | |
/// </summary> | |
public static void Update() | |
{ | |
last_kstate = kstate; | |
last_mstate = mstate; | |
kstate = Keyboard.GetState(); | |
mstate = Mouse.GetState(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment