2
0

InputState.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // InputState.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Input.Touch;
  13. using System.Collections.Generic;
  14. #endregion
  15. namespace NetworkStateManagement
  16. {
  17. /// <summary>
  18. /// Helper for reading input from keyboard, gamepad, and touch input. This class
  19. /// tracks both the current and previous state of the input devices, and implements
  20. /// query methods for high level input actions such as "move up through the menu"
  21. /// or "pause the game".
  22. /// </summary>
  23. public class InputState
  24. {
  25. #region Fields
  26. public const int MaxInputs = 4;
  27. public readonly KeyboardState[] CurrentKeyboardStates;
  28. public readonly GamePadState[] CurrentGamePadStates;
  29. public readonly KeyboardState[] LastKeyboardStates;
  30. public readonly GamePadState[] LastGamePadStates;
  31. public readonly bool[] GamePadWasConnected;
  32. public TouchCollection TouchState;
  33. public readonly List<GestureSample> Gestures = new List<GestureSample>();
  34. #endregion
  35. #region Initialization
  36. /// <summary>
  37. /// Constructs a new input state.
  38. /// </summary>
  39. public InputState()
  40. {
  41. CurrentKeyboardStates = new KeyboardState[MaxInputs];
  42. CurrentGamePadStates = new GamePadState[MaxInputs];
  43. LastKeyboardStates = new KeyboardState[MaxInputs];
  44. LastGamePadStates = new GamePadState[MaxInputs];
  45. GamePadWasConnected = new bool[MaxInputs];
  46. }
  47. #endregion
  48. #region Public Methods
  49. /// <summary>
  50. /// Reads the latest state of the keyboard and gamepad.
  51. /// </summary>
  52. public void Update()
  53. {
  54. for (int i = 0; i < MaxInputs; i++)
  55. {
  56. LastKeyboardStates[i] = CurrentKeyboardStates[i];
  57. LastGamePadStates[i] = CurrentGamePadStates[i];
  58. CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
  59. CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
  60. // Keep track of whether a gamepad has ever been
  61. // connected, so we can detect if it is unplugged.
  62. if (CurrentGamePadStates[i].IsConnected)
  63. {
  64. GamePadWasConnected[i] = true;
  65. }
  66. }
  67. TouchState = TouchPanel.GetState();
  68. Gestures.Clear();
  69. while (TouchPanel.IsGestureAvailable)
  70. {
  71. Gestures.Add(TouchPanel.ReadGesture());
  72. }
  73. }
  74. /// <summary>
  75. /// Helper for checking if a key was newly pressed during this update. The
  76. /// controllingPlayer parameter specifies which player to read input for.
  77. /// If this is null, it will accept input from any player. When a keypress
  78. /// is detected, the output playerIndex reports which player pressed it.
  79. /// </summary>
  80. public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
  81. out PlayerIndex playerIndex)
  82. {
  83. if (controllingPlayer.HasValue)
  84. {
  85. // Read input from the specified player.
  86. playerIndex = controllingPlayer.Value;
  87. int i = (int)playerIndex;
  88. return (CurrentKeyboardStates[i].IsKeyDown(key) &&
  89. LastKeyboardStates[i].IsKeyUp(key));
  90. }
  91. else
  92. {
  93. // Accept input from any player.
  94. return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
  95. IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
  96. IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
  97. IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
  98. }
  99. }
  100. /// <summary>
  101. /// Helper for checking if a button was newly pressed during this update.
  102. /// The controllingPlayer parameter specifies which player to read input for.
  103. /// If this is null, it will accept input from any player. When a button press
  104. /// is detected, the output playerIndex reports which player pressed it.
  105. /// </summary>
  106. public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer,
  107. out PlayerIndex playerIndex)
  108. {
  109. if (controllingPlayer.HasValue)
  110. {
  111. // Read input from the specified player.
  112. playerIndex = controllingPlayer.Value;
  113. int i = (int)playerIndex;
  114. return (CurrentGamePadStates[i].IsButtonDown(button) &&
  115. LastGamePadStates[i].IsButtonUp(button));
  116. }
  117. else
  118. {
  119. // Accept input from any player.
  120. return (IsNewButtonPress(button, PlayerIndex.One, out playerIndex) ||
  121. IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) ||
  122. IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) ||
  123. IsNewButtonPress(button, PlayerIndex.Four, out playerIndex));
  124. }
  125. }
  126. /// <summary>
  127. /// Checks for a "menu select" input action.
  128. /// The controllingPlayer parameter specifies which player to read input for.
  129. /// If this is null, it will accept input from any player. When the action
  130. /// is detected, the output playerIndex reports which player pressed it.
  131. /// </summary>
  132. public bool IsMenuSelect(PlayerIndex? controllingPlayer,
  133. out PlayerIndex playerIndex)
  134. {
  135. return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
  136. IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) ||
  137. IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) ||
  138. IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
  139. }
  140. /// <summary>
  141. /// Checks for a "menu cancel" input action.
  142. /// The controllingPlayer parameter specifies which player to read input for.
  143. /// If this is null, it will accept input from any player. When the action
  144. /// is detected, the output playerIndex reports which player pressed it.
  145. /// </summary>
  146. public bool IsMenuCancel(PlayerIndex? controllingPlayer,
  147. out PlayerIndex playerIndex)
  148. {
  149. return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
  150. IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
  151. IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
  152. }
  153. /// <summary>
  154. /// Checks for a "menu up" input action.
  155. /// The controllingPlayer parameter specifies which player to read
  156. /// input for. If this is null, it will accept input from any player.
  157. /// </summary>
  158. public bool IsMenuUp(PlayerIndex? controllingPlayer)
  159. {
  160. PlayerIndex playerIndex;
  161. return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex) ||
  162. IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex) ||
  163. IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
  164. }
  165. /// <summary>
  166. /// Checks for a "menu down" input action.
  167. /// The controllingPlayer parameter specifies which player to read
  168. /// input for. If this is null, it will accept input from any player.
  169. /// </summary>
  170. public bool IsMenuDown(PlayerIndex? controllingPlayer)
  171. {
  172. PlayerIndex playerIndex;
  173. return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex) ||
  174. IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) ||
  175. IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
  176. }
  177. /// <summary>
  178. /// Checks for a "pause the game" input action.
  179. /// The controllingPlayer parameter specifies which player to read
  180. /// input for. If this is null, it will accept input from any player.
  181. /// </summary>
  182. public bool IsPauseGame(PlayerIndex? controllingPlayer)
  183. {
  184. PlayerIndex playerIndex;
  185. return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
  186. IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
  187. IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
  188. }
  189. #endregion
  190. }
  191. }