/truck/TankA/ScreenManager/InputState.cs

https://bitbucket.org/kenzoix/tanka · C# · 223 lines · 118 code · 42 blank · 63 comment · 4 complexity · 75adf7ce2dd872da54dadd56dae7b49a MD5 · raw file

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