123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- //-----------------------------------------------------------------------------
- // 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
- {
- /// <summary>
- /// 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".
- /// </summary>
- public class InputState
- {
- public GamePadState CurrentGamePadStates;
- public GamePadState LastGamePadStates;
- public TouchCollection TouchStates;
-
- /// <summary>
- /// Constructs a new input state.
- /// </summary>
- public InputState()
- {
- CurrentGamePadStates = GamePad.GetState(PlayerIndex.One);
- TouchStates = TouchPanel.GetState();
- }
- /// <summary>
- /// Checks for a "menu up" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuUp
- {
- get
- {
- return IsNewButtonPress(Buttons.DPadUp);
- }
- }
- /// <summary>
- /// Checks for a "menu down" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuDown
- {
- get
- {
- return IsNewButtonPress(Buttons.DPadDown);
- }
- }
- /// <summary>
- /// Checks for a "menu select" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuSelect
- {
- get
- {
- return IsNewButtonPress(Buttons.A);
- }
- }
- /// <summary>
- /// Checks for a "menu cancel" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuCancel
- {
- get
- {
- return IsNewButtonPress(Buttons.Back);
- }
- }
- /// <summary>
- /// Checks for a "pause the game" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool PauseGame
- {
- get
- {
- return IsNewButtonPress(Buttons.Back) ||
- IsNewButtonPress(Buttons.Start);
- }
- }
- /// <summary>
- /// Reads the latest state of the keyboard and gamepad.
- /// </summary>
- public void Update()
- {
- LastGamePadStates = CurrentGamePadStates;
- CurrentGamePadStates = GamePad.GetState(PlayerIndex.One);
- TouchStates = TouchPanel.GetState();
- }
- /// <summary>
- /// Helper for checking if a button was newly pressed during this update,
- /// by any player.
- /// </summary>
- public bool IsNewButtonPress(Buttons button)
- {
- return IsNewButtonPress(button, PlayerIndex.One);
- }
- /// <summary>
- /// Helper for checking if a button was newly pressed during this update,
- /// by the specified player.
- /// </summary>
- public bool IsNewButtonPress(Buttons button, PlayerIndex playerIndex)
- {
- return (CurrentGamePadStates.IsButtonDown(button) &&
- LastGamePadStates.IsButtonUp(button));
- }
- /// <summary>
- /// Checks for a "menu select" input action from the specified player.
- /// </summary>
- public bool IsMenuSelect(PlayerIndex playerIndex)
- {
- return IsNewButtonPress(Buttons.A, playerIndex) ||
- IsNewButtonPress(Buttons.Start, playerIndex);
- }
- /// <summary>
- /// Checks for a "menu cancel" input action from the specified player.
- /// </summary>
- public bool IsMenuCancel(PlayerIndex playerIndex)
- {
- return IsNewButtonPress(Buttons.B, playerIndex) ||
- IsNewButtonPress(Buttons.Back, playerIndex);
- }
- }
- }
|