InputState.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. using System.Collections.Generic;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Input;
  12. using Microsoft.Xna.Framework.Input.Touch;
  13. namespace GameStateManagement
  14. {
  15. /// <summary>
  16. /// Helper for reading input from keyboard, gamepad, and touch input. This class
  17. /// tracks both the current and previous state of the input devices, and implements
  18. /// query 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. public const int MaxInputs = 4;
  24. public readonly KeyboardState[] CurrentKeyboardStates;
  25. public readonly GamePadState[] CurrentGamePadStates;
  26. public readonly KeyboardState[] LastKeyboardStates;
  27. public readonly GamePadState[] LastGamePadStates;
  28. public readonly bool[] GamePadWasConnected;
  29. public TouchCollection TouchState;
  30. public readonly List<GestureSample> Gestures = new List<GestureSample>();
  31. /// <summary>
  32. /// Constructs a new input state.
  33. /// </summary>
  34. public InputState()
  35. {
  36. CurrentKeyboardStates = new KeyboardState[MaxInputs];
  37. CurrentGamePadStates = new GamePadState[MaxInputs];
  38. LastKeyboardStates = new KeyboardState[MaxInputs];
  39. LastGamePadStates = new GamePadState[MaxInputs];
  40. GamePadWasConnected = new bool[MaxInputs];
  41. }
  42. /// <summary>
  43. /// Reads the latest state user input.
  44. /// </summary>
  45. public void Update()
  46. {
  47. for (int i = 0; i < MaxInputs; i++)
  48. {
  49. LastKeyboardStates[i] = CurrentKeyboardStates[i];
  50. LastGamePadStates[i] = CurrentGamePadStates[i];
  51. CurrentKeyboardStates[i] = Keyboard.GetState();
  52. CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
  53. // Keep track of whether a gamepad has ever been
  54. // connected, so we can detect if it is unplugged.
  55. if (CurrentGamePadStates[i].IsConnected)
  56. {
  57. GamePadWasConnected[i] = true;
  58. }
  59. }
  60. // Get the raw touch state from the TouchPanel
  61. TouchState = TouchPanel.GetState();
  62. // Read in any detected gestures into our list for the screens to later process
  63. Gestures.Clear();
  64. while (TouchPanel.IsGestureAvailable)
  65. {
  66. Gestures.Add(TouchPanel.ReadGesture());
  67. }
  68. }
  69. /// <summary>
  70. /// Helper for checking if a key was pressed during this update. The
  71. /// controllingPlayer parameter specifies which player to read input for.
  72. /// If this is null, it will accept input from any player. When a keypress
  73. /// is detected, the output playerIndex reports which player pressed it.
  74. /// </summary>
  75. public bool IsKeyPressed(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  76. {
  77. if (controllingPlayer.HasValue)
  78. {
  79. // Read input from the specified player.
  80. playerIndex = controllingPlayer.Value;
  81. int i = (int)playerIndex;
  82. return CurrentKeyboardStates[i].IsKeyDown(key);
  83. }
  84. else
  85. {
  86. // Accept input from any player.
  87. return (IsKeyPressed(key, PlayerIndex.One, out playerIndex) ||
  88. IsKeyPressed(key, PlayerIndex.Two, out playerIndex) ||
  89. IsKeyPressed(key, PlayerIndex.Three, out playerIndex) ||
  90. IsKeyPressed(key, PlayerIndex.Four, out playerIndex));
  91. }
  92. }
  93. /// <summary>
  94. /// Helper for checking if a button was pressed during this update.
  95. /// The controllingPlayer parameter specifies which player to read input for.
  96. /// If this is null, it will accept input from any player. When a button press
  97. /// is detected, the output playerIndex reports which player pressed it.
  98. /// </summary>
  99. public bool IsButtonPressed(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  100. {
  101. if (controllingPlayer.HasValue)
  102. {
  103. // Read input from the specified player.
  104. playerIndex = controllingPlayer.Value;
  105. int i = (int)playerIndex;
  106. return CurrentGamePadStates[i].IsButtonDown(button);
  107. }
  108. else
  109. {
  110. // Accept input from any player.
  111. return (IsButtonPressed(button, PlayerIndex.One, out playerIndex) ||
  112. IsButtonPressed(button, PlayerIndex.Two, out playerIndex) ||
  113. IsButtonPressed(button, PlayerIndex.Three, out playerIndex) ||
  114. IsButtonPressed(button, PlayerIndex.Four, out playerIndex));
  115. }
  116. }
  117. /// <summary>
  118. /// Helper for checking if a key was newly pressed during this update. The
  119. /// controllingPlayer parameter specifies which player to read input for.
  120. /// If this is null, it will accept input from any player. When a keypress
  121. /// is detected, the output playerIndex reports which player pressed it.
  122. /// </summary>
  123. public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  124. {
  125. if (controllingPlayer.HasValue)
  126. {
  127. // Read input from the specified player.
  128. playerIndex = controllingPlayer.Value;
  129. int i = (int)playerIndex;
  130. return (CurrentKeyboardStates[i].IsKeyDown(key) &&
  131. LastKeyboardStates[i].IsKeyUp(key));
  132. }
  133. else
  134. {
  135. // Accept input from any player.
  136. return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
  137. IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
  138. IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
  139. IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
  140. }
  141. }
  142. /// <summary>
  143. /// Helper for checking if a button was newly pressed during this update.
  144. /// The controllingPlayer parameter specifies which player to read input for.
  145. /// If this is null, it will accept input from any player. When a button press
  146. /// is detected, the output playerIndex reports which player pressed it.
  147. /// </summary>
  148. public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  149. {
  150. if (controllingPlayer.HasValue)
  151. {
  152. // Read input from the specified player.
  153. playerIndex = controllingPlayer.Value;
  154. int i = (int)playerIndex;
  155. return (CurrentGamePadStates[i].IsButtonDown(button) &&
  156. LastGamePadStates[i].IsButtonUp(button));
  157. }
  158. else
  159. {
  160. // Accept input from any player.
  161. return (IsNewButtonPress(button, PlayerIndex.One, out playerIndex) ||
  162. IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) ||
  163. IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) ||
  164. IsNewButtonPress(button, PlayerIndex.Four, out playerIndex));
  165. }
  166. }
  167. }
  168. }