Advertisement
lunkums

Simple MonoGame Input

Oct 16th, 2022
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | Gaming | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Input;
  3.  
  4. namespace LD51
  5. {
  6.     public class Input
  7.     {
  8.         private static KeyboardState previousKeyboardState = Keyboard.GetState();
  9.         private static KeyboardState currentKeyboardState = Keyboard.GetState();
  10.  
  11.         private static MouseState previousMouseState = Mouse.GetState();
  12.         private static MouseState currentMouseState = Mouse.GetState();
  13.  
  14.         private static bool gameInFocus = false;
  15.  
  16.         private Input() { }
  17.  
  18.         public static Vector2 MouseWorldPosition => new Vector2(currentMouseState.X, -currentMouseState.Y);
  19.  
  20.         public static void Update(bool gameInFocus)
  21.         {
  22.             Input.gameInFocus = gameInFocus;
  23.  
  24.             previousKeyboardState = currentKeyboardState;
  25.             currentKeyboardState = Keyboard.GetState();
  26.  
  27.             previousMouseState = currentMouseState;
  28.             currentMouseState = Mouse.GetState();
  29.         }
  30.  
  31.         public static bool IsKeyPressed(Keys key)
  32.         {
  33.             return currentKeyboardState.IsKeyDown(key) && previousKeyboardState.IsKeyUp(key);
  34.         }
  35.  
  36.         public static bool IsKeyDown(Keys key)
  37.         {
  38.             return currentKeyboardState.IsKeyDown(key);
  39.         }
  40.  
  41.         public static bool LeftMousePressed()
  42.         {
  43.             return gameInFocus && currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released;
  44.         }
  45.  
  46.         public static bool RightMousePressed()
  47.         {
  48.             return gameInFocus && currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Released;
  49.         }
  50.     }
  51. }
  52.  
Tags: C# monogame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement