123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #region File Description
- //-----------------------------------------------------------------------------
- // InputState.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Input;
- #if ANDROID
- using Microsoft.Xna.Framework.Input.Touch;
- using System.Collections.Generic;
- #endif
- #endregion
- namespace VectorRumble
- {
- /// <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>
- /// <remarks>Based on a class in the Game State Management sample.</remarks>
- public class InputState
- {
- #region Fields
- public const int MaxInputs = 4;
- public readonly KeyboardState[] CurrentKeyboardStates;
- public readonly GamePadState[] CurrentGamePadStates;
- public readonly KeyboardState[] LastKeyboardStates;
- public readonly GamePadState[] LastGamePadStates;
-
- #if ANDROID
- public TouchCollection CurrentTouchState;
- public List<GestureSample> Gestures = new List<GestureSample>();
- #endif
- #endregion
- #region Initialization
- /// <summary>
- /// Constructs a new input state.
- /// </summary>
- public InputState()
- {
- CurrentKeyboardStates = new KeyboardState[MaxInputs];
- CurrentGamePadStates = new GamePadState[MaxInputs];
- LastKeyboardStates = new KeyboardState[MaxInputs];
- LastGamePadStates = new GamePadState[MaxInputs];
- #if ANDROID
-
- #endif
- }
- #endregion
- #region Properties
- /// <summary>
- /// Checks for a "menu up" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuUp
- {
- get
- {
- return IsNewKeyPress(Keys.Up) ||
- IsNewButtonPress(Buttons.DPadUp) ||
- IsNewButtonPress(Buttons.LeftThumbstickUp);
- }
- }
- /// <summary>
- /// Checks for a "menu down" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuDown
- {
- get
- {
- return IsNewKeyPress(Keys.Down) ||
- IsNewButtonPress(Buttons.DPadDown) ||
- IsNewButtonPress(Buttons.LeftThumbstickDown);
- }
- }
- /// <summary>
- /// Checks for a "menu select" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuSelect
- {
- get
- {
- return IsNewKeyPress(Keys.Space) ||
- IsNewKeyPress(Keys.Enter) ||
- IsNewButtonPress(Buttons.A) ||
- IsNewButtonPress(Buttons.Start);
- }
- }
- /// <summary>
- /// Checks for a "menu cancel" input action, from any player,
- /// on either keyboard or gamepad.
- /// </summary>
- public bool MenuCancel
- {
- get
- {
- return IsNewKeyPress(Keys.Escape) ||
- IsNewButtonPress(Buttons.B) ||
- 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 IsNewKeyPress(Keys.Escape) ||
- IsNewButtonPress(Buttons.Back) ||
- IsNewButtonPress(Buttons.Start);
- }
- }
- #endregion
- #region Methods
- /// <summary>
- /// Reads the latest state of the keyboard and gamepad.
- /// </summary>
- public void Update()
- {
- for (int i = 0; i < MaxInputs; i++)
- {
- LastKeyboardStates[i] = CurrentKeyboardStates[i];
- LastGamePadStates[i] = CurrentGamePadStates[i];
- CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
- CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
- }
- #if ANDROID
- CurrentTouchState = TouchPanel.GetState();
- Gestures.Clear();
- while(TouchPanel.IsGestureAvailable)
- {
- Gestures.Add(TouchPanel.ReadGesture());
- }
- #endif
- }
- /// <summary>
- /// Helper for checking if a key was newly pressed during this update,
- /// by any player.
- /// </summary>
- public bool IsNewKeyPress(Keys key)
- {
- for (int i = 0; i < MaxInputs; i++)
- {
- if (IsNewKeyPress(key, (PlayerIndex)i))
- return true;
- }
- return false;
- }
- /// <summary>
- /// Helper for checking if a key was newly pressed during this update,
- /// by the specified player.
- /// </summary>
- public bool IsNewKeyPress(Keys key, PlayerIndex playerIndex)
- {
- return (CurrentKeyboardStates[(int)playerIndex].IsKeyDown(key) &&
- LastKeyboardStates[(int)playerIndex].IsKeyUp(key));
- }
- /// <summary>
- /// Helper for checking if a button was newly pressed during this update,
- /// by any player.
- /// </summary>
- public bool IsNewButtonPress(Buttons button)
- {
- for (int i = 0; i < MaxInputs; i++)
- {
- if (IsNewButtonPress(button, (PlayerIndex)i))
- return true;
- }
- return false;
- }
- /// <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[(int)playerIndex].IsButtonDown(button) &&
- LastGamePadStates[(int)playerIndex].IsButtonUp(button));
- }
- /// <summary>
- /// Checks for a "menu select" input action from the specified player.
- /// </summary>
- public bool IsMenuSelect(PlayerIndex playerIndex)
- {
- return IsNewKeyPress(Keys.Space, playerIndex) ||
- IsNewKeyPress(Keys.Enter, playerIndex) ||
- 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 IsNewKeyPress(Keys.Escape, playerIndex) ||
- IsNewButtonPress(Buttons.B, playerIndex) ||
- IsNewButtonPress(Buttons.Back, playerIndex);
- }
- #endregion
- }
- }
|