2
0

InputState.cs 9.2 KB

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