using System.Collections.Generic; namespace RecrownedAthenaeum.Input { /// /// Utilities to better manage input for the game. /// public static class InputUtilities { /// /// Listeners for changes in input. /// public static List InputListeners; static KeyboardState keyboardState; static MouseState mouseState; static InputUtilities() { InputListeners = new List(); } /// /// Updates inputs. /// Should be called once every game update to be up to date with new states of inputs. /// public static void Update() { KeyboardState updatedKeyboardState = Keyboard.GetState(); MouseState updatedMouseState = Mouse.GetState(); bool disableKeyboard = false; bool disableMouse = false; foreach (IInputListener inputListener in InputListeners) { if (!disableKeyboard && keyboardState != updatedKeyboardState) { disableKeyboard = inputListener.KeyboardStateChanged(updatedKeyboardState); } if (!disableMouse && mouseState != updatedMouseState) { disableMouse = inputListener.MouseStateChanged(updatedMouseState); } if (disableKeyboard && disableMouse) { break; } } UpdateStates(); } /// /// Poll used to check if mouse was clicked. /// /// True if clicked. public static bool MouseClicked() { if (mouseState.LeftButton == ButtonState.Pressed) { return Mouse.GetState().LeftButton != ButtonState.Pressed; } return false; } private static void UpdateStates() { keyboardState = Keyboard.GetState(); mouseState = Mouse.GetState(); } /// /// Checks whether mouse is within boundaries. /// /// Boundaries to check. /// public static bool MouseWithinBoundries(Rectangle bounds) { MouseState mouseState = Mouse.GetState(); if (mouseState.X >= bounds.X && mouseState.X <= (bounds.X + bounds.Width) && mouseState.Y >= bounds.Y && mouseState.Y <= (bounds.Y + bounds.Height)) { return true; } return false; } } }