InputState.cs 9.4 KB

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