123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #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 Flocking
- {
- /// <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 CurrentKeyState;
- public GamePadState CurrentPadState;
- public KeyboardState LastKeyState;
- public GamePadState LastPadState;
- #endregion
- #region Properties
- /// <summary>
- /// Checks for Y move amount on either keyboard or gamepad.
- /// </summary>
- public float MoveCatY
- {
- get
- {
- if (CurrentKeyState.IsKeyDown(Keys.W))
- {
- return -1.0f;
- }
- else if (CurrentKeyState.IsKeyDown(Keys.S))
- {
- return 1.0f;
- }
- else
- {
- //negative = move up
- return -(CurrentPadState.ThumbSticks.Left.Y);
- }
- }
- }
- /// <summary>
- /// Checks for X move amount on either keyboard or gamepad.
- /// </summary>
- public float MoveCatX
- {
- get
- {
- if (CurrentKeyState.IsKeyDown(Keys.A))
- {
- return -1.0f;
- }
- else if (CurrentKeyState.IsKeyDown(Keys.D))
- {
- return 1.0f;
- }
- else
- {
- return CurrentPadState.ThumbSticks.Left.X;
- }
- }
- }
- /// <summary>
- /// Checks for slider move amount on either keyboard or gamepad.
- /// </summary>
- public float SliderMove
- {
- get
- {
- if (CurrentKeyState.IsKeyDown(Keys.Left)||
- CurrentPadState.IsButtonDown(Buttons.DPadLeft))
- {
- return -1.0f;
- }
- else if (CurrentKeyState.IsKeyDown(Keys.Right) ||
- CurrentPadState.IsButtonDown(Buttons.DPadRight))
- {
- return 1.0f;
- }
- return -CurrentPadState.Triggers.Left + CurrentPadState.Triggers.Right;
- }
- }
- /// <summary>
- /// Checks for a "menu cancel" input action (on either keyboard or gamepad).
- /// </summary>
- public bool Exit
- {
- get
- {
- return IsNewKeyPress(Keys.Escape) ||
- (CurrentPadState.Buttons.Back == ButtonState.Pressed &&
- LastPadState.Buttons.Back == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for a "reset distances" input action (on either keyboard or gamepad).
- /// </summary>
- public bool ResetDistances
- {
- get
- {
- return IsNewKeyPress(Keys.B) ||
- (CurrentPadState.Buttons.B == ButtonState.Pressed &&
- LastPadState.Buttons.B == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for a "reset flock" input action (on either keyboard or gamepad).
- /// </summary>
- public bool ResetFlock
- {
- get
- {
- return IsNewKeyPress(Keys.X) ||
- (CurrentPadState.Buttons.X == ButtonState.Pressed &&
- LastPadState.Buttons.X == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for an "up" input action (on either keyboard or gamepad).
- /// </summary>
- public bool Up
- {
- get
- {
- return IsNewKeyPress(Keys.Up) ||
- (CurrentPadState.DPad.Up == ButtonState.Pressed &&
- LastPadState.DPad.Up == ButtonState.Released);
- }
- }
- /// <summary>
- /// Checks for an "down" input action (on either keyboard or gamepad).
- /// </summary>
- public bool Down
- {
- get
- {
- return IsNewKeyPress(Keys.Down) ||
- (CurrentPadState.DPad.Down == ButtonState.Pressed &&
- LastPadState.DPad.Down == ButtonState.Released);
- }
- }
- /// <summary>
- /// Add or remove the cat
- /// </summary>
- public bool ToggleCatButton
- {
- get
- {
- return IsNewKeyPress(Keys.Y) ||
- (CurrentPadState.Buttons.Y == ButtonState.Pressed &&
- LastPadState.Buttons.Y == ButtonState.Released);
- }
- }
- #endregion
- #region Methods
- /// <summary>
- /// Reads the latest state of the keyboard and gamepad.
- /// </summary>
- public void Update()
- {
- LastKeyState = CurrentKeyState;
- LastPadState = CurrentPadState;
- CurrentKeyState = Keyboard.GetState();
- CurrentPadState = GamePad.GetState(PlayerIndex.One);
- }
- /// <summary>
- /// Helper for checking if a key was newly pressed during this update.
- /// </summary>
- bool IsNewKeyPress(Keys key)
- {
- return (CurrentKeyState.IsKeyDown(key) &&
- LastKeyState.IsKeyUp(key));
- }
- #endregion
- }
- }
|