InputState.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #if ANDROID
  13. using Microsoft.Xna.Framework.Input.Touch;
  14. using System.Collections.Generic;
  15. #endif
  16. #endregion
  17. namespace VectorRumble
  18. {
  19. /// <summary>
  20. /// Helper for reading input from keyboard and gamepad. This class tracks both
  21. /// the current and previous state of both input devices, and implements query
  22. /// properties for high level input actions such as "move up through the menu"
  23. /// or "pause the game".
  24. /// </summary>
  25. /// <remarks>Based on a class in the Game State Management sample.</remarks>
  26. public class InputState
  27. {
  28. #region Fields
  29. public const int MaxInputs = 4;
  30. public readonly KeyboardState[] CurrentKeyboardStates;
  31. public readonly GamePadState[] CurrentGamePadStates;
  32. public readonly KeyboardState[] LastKeyboardStates;
  33. public readonly GamePadState[] LastGamePadStates;
  34. #if ANDROID
  35. public TouchCollection CurrentTouchState;
  36. public List<GestureSample> Gestures = new List<GestureSample>();
  37. #endif
  38. #endregion
  39. #region Initialization
  40. /// <summary>
  41. /// Constructs a new input state.
  42. /// </summary>
  43. public InputState()
  44. {
  45. CurrentKeyboardStates = new KeyboardState[MaxInputs];
  46. CurrentGamePadStates = new GamePadState[MaxInputs];
  47. LastKeyboardStates = new KeyboardState[MaxInputs];
  48. LastGamePadStates = new GamePadState[MaxInputs];
  49. #if ANDROID
  50. #endif
  51. }
  52. #endregion
  53. #region Properties
  54. /// <summary>
  55. /// Checks for a "menu up" input action, from any player,
  56. /// on either keyboard or gamepad.
  57. /// </summary>
  58. public bool MenuUp
  59. {
  60. get
  61. {
  62. return IsNewKeyPress(Keys.Up) ||
  63. IsNewButtonPress(Buttons.DPadUp) ||
  64. IsNewButtonPress(Buttons.LeftThumbstickUp);
  65. }
  66. }
  67. /// <summary>
  68. /// Checks for a "menu down" input action, from any player,
  69. /// on either keyboard or gamepad.
  70. /// </summary>
  71. public bool MenuDown
  72. {
  73. get
  74. {
  75. return IsNewKeyPress(Keys.Down) ||
  76. IsNewButtonPress(Buttons.DPadDown) ||
  77. IsNewButtonPress(Buttons.LeftThumbstickDown);
  78. }
  79. }
  80. /// <summary>
  81. /// Checks for a "menu select" input action, from any player,
  82. /// on either keyboard or gamepad.
  83. /// </summary>
  84. public bool MenuSelect
  85. {
  86. get
  87. {
  88. return IsNewKeyPress(Keys.Space) ||
  89. IsNewKeyPress(Keys.Enter) ||
  90. IsNewButtonPress(Buttons.A) ||
  91. IsNewButtonPress(Buttons.Start);
  92. }
  93. }
  94. /// <summary>
  95. /// Checks for a "menu cancel" input action, from any player,
  96. /// on either keyboard or gamepad.
  97. /// </summary>
  98. public bool MenuCancel
  99. {
  100. get
  101. {
  102. return IsNewKeyPress(Keys.Escape) ||
  103. IsNewButtonPress(Buttons.B) ||
  104. IsNewButtonPress(Buttons.Back);
  105. }
  106. }
  107. /// <summary>
  108. /// Checks for a "pause the game" input action, from any player,
  109. /// on either keyboard or gamepad.
  110. /// </summary>
  111. public bool PauseGame
  112. {
  113. get
  114. {
  115. return IsNewKeyPress(Keys.Escape) ||
  116. IsNewButtonPress(Buttons.Back) ||
  117. IsNewButtonPress(Buttons.Start);
  118. }
  119. }
  120. #endregion
  121. #region Methods
  122. /// <summary>
  123. /// Reads the latest state of the keyboard and gamepad.
  124. /// </summary>
  125. public void Update()
  126. {
  127. for (int i = 0; i < MaxInputs; i++)
  128. {
  129. LastKeyboardStates[i] = CurrentKeyboardStates[i];
  130. LastGamePadStates[i] = CurrentGamePadStates[i];
  131. CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
  132. CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
  133. }
  134. #if ANDROID
  135. CurrentTouchState = TouchPanel.GetState();
  136. Gestures.Clear();
  137. while(TouchPanel.IsGestureAvailable)
  138. {
  139. Gestures.Add(TouchPanel.ReadGesture());
  140. }
  141. #endif
  142. }
  143. /// <summary>
  144. /// Helper for checking if a key was newly pressed during this update,
  145. /// by any player.
  146. /// </summary>
  147. public bool IsNewKeyPress(Keys key)
  148. {
  149. for (int i = 0; i < MaxInputs; i++)
  150. {
  151. if (IsNewKeyPress(key, (PlayerIndex)i))
  152. return true;
  153. }
  154. return false;
  155. }
  156. /// <summary>
  157. /// Helper for checking if a key was newly pressed during this update,
  158. /// by the specified player.
  159. /// </summary>
  160. public bool IsNewKeyPress(Keys key, PlayerIndex playerIndex)
  161. {
  162. return (CurrentKeyboardStates[(int)playerIndex].IsKeyDown(key) &&
  163. LastKeyboardStates[(int)playerIndex].IsKeyUp(key));
  164. }
  165. /// <summary>
  166. /// Helper for checking if a button was newly pressed during this update,
  167. /// by any player.
  168. /// </summary>
  169. public bool IsNewButtonPress(Buttons button)
  170. {
  171. for (int i = 0; i < MaxInputs; i++)
  172. {
  173. if (IsNewButtonPress(button, (PlayerIndex)i))
  174. return true;
  175. }
  176. return false;
  177. }
  178. /// <summary>
  179. /// Helper for checking if a button was newly pressed during this update,
  180. /// by the specified player.
  181. /// </summary>
  182. public bool IsNewButtonPress(Buttons button, PlayerIndex playerIndex)
  183. {
  184. return (CurrentGamePadStates[(int)playerIndex].IsButtonDown(button) &&
  185. LastGamePadStates[(int)playerIndex].IsButtonUp(button));
  186. }
  187. /// <summary>
  188. /// Checks for a "menu select" input action from the specified player.
  189. /// </summary>
  190. public bool IsMenuSelect(PlayerIndex playerIndex)
  191. {
  192. return IsNewKeyPress(Keys.Space, playerIndex) ||
  193. IsNewKeyPress(Keys.Enter, playerIndex) ||
  194. IsNewButtonPress(Buttons.A, playerIndex) ||
  195. IsNewButtonPress(Buttons.Start, playerIndex);
  196. }
  197. /// <summary>
  198. /// Checks for a "menu cancel" input action from the specified player.
  199. /// </summary>
  200. public bool IsMenuCancel(PlayerIndex playerIndex)
  201. {
  202. return IsNewKeyPress(Keys.Escape, playerIndex) ||
  203. IsNewButtonPress(Buttons.B, playerIndex) ||
  204. IsNewButtonPress(Buttons.Back, playerIndex);
  205. }
  206. #endregion
  207. }
  208. }