InputManager.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Input;
  3. public static class InputManager
  4. {
  5. private static KeyboardState currentKeyState;
  6. private static KeyboardState previousKeyState;
  7. private static GamePadState currentGamePadState;
  8. private static GamePadState previousGamePadState;
  9. private static MouseState mouseState;
  10. public static void Update()
  11. {
  12. previousKeyState = currentKeyState;
  13. currentKeyState = Keyboard.GetState();
  14. previousGamePadState = currentGamePadState;
  15. currentGamePadState = GamePad.GetState(0);
  16. mouseState = Mouse.GetState();
  17. }
  18. public static bool IsKeyDown(Keys key)
  19. {
  20. return currentKeyState.IsKeyDown(key);
  21. }
  22. public static bool IsKeyPressed(Keys key)
  23. {
  24. return currentKeyState.IsKeyDown(key) && !previousKeyState.IsKeyDown(key);
  25. }
  26. public static bool IsKeyReleased(Keys key)
  27. {
  28. return !currentKeyState.IsKeyDown(key) && previousKeyState.IsKeyDown(key);
  29. }
  30. public static float MouseX => mouseState.X;
  31. public static float MouseY => mouseState.Y;
  32. public static Vector2 MousePosition => new Vector2(mouseState.X, mouseState.Y);
  33. public static void SetMousePosition(int x, int y)
  34. {
  35. Mouse.SetPosition(x, y);
  36. }
  37. public static bool IsButtonDown(Buttons button)
  38. {
  39. return currentGamePadState.IsButtonDown(button);
  40. }
  41. public static bool IsButtonPressed(Buttons button)
  42. {
  43. return currentGamePadState.IsButtonDown(button) && !previousGamePadState.IsButtonDown(button);
  44. }
  45. public static bool IsButtonReleased(Buttons button)
  46. {
  47. return !currentGamePadState.IsButtonDown(button) && previousGamePadState.IsButtonDown(button);
  48. }
  49. public static float RightTriggerValue => currentGamePadState.Triggers.Right;
  50. public static Vector2 LeftThumbStick => currentGamePadState.ThumbSticks.Left;
  51. public static float LeftTriggerValue => currentGamePadState.Triggers.Left;
  52. public static Vector2 RightThumbStick => currentGamePadState.ThumbSticks.Right;
  53. }