//----------------------------------------------------------------------------- // InputState.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input.Touch; namespace AlienGameSample { /// /// Helper for reading input from keyboard and gamepad. This class tracks both /// the current and previous state of both input devices, and implements query /// properties for high level input actions such as "move up through the menu" /// or "pause the game". /// public class InputState { public GamePadState CurrentGamePadStates; public GamePadState LastGamePadStates; public TouchCollection TouchStates; /// /// Constructs a new input state. /// public InputState() { CurrentGamePadStates = GamePad.GetState(PlayerIndex.One); TouchStates = TouchPanel.GetState(); } /// /// Checks for a "menu up" input action, from any player, /// on either keyboard or gamepad. /// public bool MenuUp { get { return IsNewButtonPress(Buttons.DPadUp); } } /// /// Checks for a "menu down" input action, from any player, /// on either keyboard or gamepad. /// public bool MenuDown { get { return IsNewButtonPress(Buttons.DPadDown); } } /// /// Checks for a "menu select" input action, from any player, /// on either keyboard or gamepad. /// public bool MenuSelect { get { return IsNewButtonPress(Buttons.A); } } /// /// Checks for a "menu cancel" input action, from any player, /// on either keyboard or gamepad. /// public bool MenuCancel { get { return IsNewButtonPress(Buttons.Back); } } /// /// Checks for a "pause the game" input action, from any player, /// on either keyboard or gamepad. /// public bool PauseGame { get { return IsNewButtonPress(Buttons.Back) || IsNewButtonPress(Buttons.Start); } } /// /// Reads the latest state of the keyboard and gamepad. /// public void Update() { LastGamePadStates = CurrentGamePadStates; CurrentGamePadStates = GamePad.GetState(PlayerIndex.One); TouchStates = TouchPanel.GetState(); } /// /// Helper for checking if a button was newly pressed during this update, /// by any player. /// public bool IsNewButtonPress(Buttons button) { return IsNewButtonPress(button, PlayerIndex.One); } /// /// Helper for checking if a button was newly pressed during this update, /// by the specified player. /// public bool IsNewButtonPress(Buttons button, PlayerIndex playerIndex) { return (CurrentGamePadStates.IsButtonDown(button) && LastGamePadStates.IsButtonUp(button)); } /// /// Checks for a "menu select" input action from the specified player. /// public bool IsMenuSelect(PlayerIndex playerIndex) { return IsNewButtonPress(Buttons.A, playerIndex) || IsNewButtonPress(Buttons.Start, playerIndex); } /// /// Checks for a "menu cancel" input action from the specified player. /// public bool IsMenuCancel(PlayerIndex playerIndex) { return IsNewButtonPress(Buttons.B, playerIndex) || IsNewButtonPress(Buttons.Back, playerIndex); } } }