123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #region File Description
- //-----------------------------------------------------------------------------
- // InputState.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Input;
- #endregion
- namespace NetRumble
- {
- /// <summary>
- /// Helper for reading input from keyboard and gamepad. This public class tracks
- /// 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>
- /// This public class is similar to one in the GameStateManagement sample.
- /// </remarks>
- public class InputState
- {
- #region Fields
- public KeyboardState CurrentKeyboardState;
- public GamePadState CurrentGamePadState;
- public KeyboardState LastKeyboardState;
- public GamePadState LastGamePadState;
- #endregion
- #region Properties
- /// <summary>
- /// Checks for a "menu up" input action (on either keyboard or gamepad).
- /// </summary>
- public bool MenuUp
- {
- get
- {
- return IsNewKeyPress(Keys.Up) ||
- (CurrentGamePadState.DPad.Up == ButtonState.Pressed &&
- LastGamePadState.DPad.Up == ButtonState.Released) ||
- (CurrentGamePadState.ThumbSticks.Left.Y > 0 &&
- LastGamePadState.ThumbSticks.Left.Y <= 0);
- }
- }
- /// <summary>
- /// Checks for a "menu down" input action (on either keyboard or gamepad).
- /// </summary>
- public bool MenuDown
- {
- get
- {
- return IsNewKeyPress(Keys.Down) ||
- (CurrentGamePadState.DPad.Down == ButtonState.Pressed &&
- LastGamePadState.DPad.Down == ButtonState.Released) ||
- (CurrentGamePadState.ThumbSticks.Left.Y < 0 &&
- LastGamePadState.ThumbSticks.Left.Y >= 0);
- }
- }
- /// <summary>
- /// Checks for a "menu select" input action (on either keyboard or gamepad).
- /// </summary>
- public bool MenuSelect
- {
- get
- {
- return IsNewKeyPress(Keys.Space) ||
- IsNewKeyPress(Keys.Enter) ||
- (CurrentGamePadState.Buttons.A == ButtonState.Pressed &&
- LastGamePadState.Buttons.A == ButtonState.Released) ||
- (CurrentGamePadState.Buttons.Start == ButtonState.Pressed &&
- LastGamePadState.Buttons.Start == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for a "menu cancel" input action (on either keyboard or gamepad).
- /// </summary>
- public bool MenuCancel
- {
- get
- {
- return IsNewKeyPress(Keys.Escape) ||
- (CurrentGamePadState.Buttons.B == ButtonState.Pressed &&
- LastGamePadState.Buttons.B == ButtonState.Released) ||
- (CurrentGamePadState.Buttons.Back == ButtonState.Pressed &&
- LastGamePadState.Buttons.Back == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for a "pause the game" input action (on either keyboard or gamepad).
- /// </summary>
- public bool PauseGame
- {
- get
- {
- return IsNewKeyPress(Keys.Escape) ||
- (CurrentGamePadState.Buttons.Back == ButtonState.Pressed &&
- LastGamePadState.Buttons.Back == ButtonState.Released) ||
- (CurrentGamePadState.Buttons.Start == ButtonState.Pressed &&
- LastGamePadState.Buttons.Start == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for a positive "ship color change" input action
- /// </summary>
- public bool ShipColorChangeUp
- {
- get
- {
- return IsNewKeyPress(Keys.Up) ||
- (CurrentGamePadState.Buttons.RightShoulder == ButtonState.Pressed &&
- LastGamePadState.Buttons.RightShoulder == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for a negative "ship color change" input action.
- /// </summary>
- public bool ShipColorChangeDown
- {
- get
- {
- return IsNewKeyPress(Keys.Down) ||
- (CurrentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed &&
- LastGamePadState.Buttons.LeftShoulder == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for a positive "ship model change" input action.
- /// </summary>
- public bool ShipModelChangeUp
- {
- get
- {
- return IsNewKeyPress(Keys.Right) ||
- (CurrentGamePadState.Triggers.Right >= 1f &&
- LastGamePadState.Triggers.Right < 1f);
- }
- }
- /// <summary>
- /// Checks for a negative "ship model change" input action.
- /// </summary>
- public bool ShipModelChangeDown
- {
- get
- {
- return IsNewKeyPress(Keys.Left) ||
- (CurrentGamePadState.Triggers.Left >= 1f &&
- LastGamePadState.Triggers.Left < 1f);
- }
- }
- /// <summary>
- /// Checks for a "mark ready" input action (on either keyboard or gamepad).
- /// </summary>
- public bool MarkReady
- {
- get
- {
- return IsNewKeyPress(Keys.X) ||
- (CurrentGamePadState.Buttons.X == ButtonState.Pressed &&
- LastGamePadState.Buttons.X == ButtonState.Released);
- }
- }
- #endregion
- #region Methods
- /// <summary>
- /// Reads the latest state of the keyboard and gamepad.
- /// </summary>
- public void Update()
- {
- LastKeyboardState = CurrentKeyboardState;
- LastGamePadState = CurrentGamePadState;
- CurrentKeyboardState = Keyboard.GetState();
- CurrentGamePadState = GamePad.GetState(PlayerIndex.One);
- }
- /// <summary>
- /// Helper for checking if a key was newly pressed during this update.
- /// </summary>
- public bool IsNewKeyPress(Keys key)
- {
- return (CurrentKeyboardState.IsKeyDown(key) &&
- LastKeyboardState.IsKeyUp(key));
- }
- #endregion
- }
- }
|