InputState.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //-----------------------------------------------------------------------------
  2. // InputState.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Input;
  11. using Microsoft.Xna.Framework.Input.Touch;
  12. namespace GameStateManagement
  13. {
  14. /// <summary>
  15. /// Helper for reading input from keyboard, gamepad, and touch input. This class
  16. /// tracks both the current and previous state of the input devices, and implements
  17. /// query methods for high level input actions such as "move up through the menu"
  18. /// or "pause the game".
  19. /// </summary>
  20. public class InputState
  21. {
  22. public const int MaxInputs = 4;
  23. public readonly KeyboardState[] CurrentKeyboardStates;
  24. public readonly GamePadState[] CurrentGamePadStates;
  25. public readonly KeyboardState[] LastKeyboardStates;
  26. public readonly GamePadState[] LastGamePadStates;
  27. public readonly bool[] GamePadWasConnected;
  28. public TouchCollection TouchState;
  29. public MouseState CurrentMouseState;
  30. public MouseState LastMouseState;
  31. public readonly List<GestureSample> Gestures = new List<GestureSample> ();
  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. /// <summary>
  44. /// Reads the latest state of the keyboard and gamepad.
  45. /// </summary>
  46. public void Update ()
  47. {
  48. for (int i = 0; i < MaxInputs; i++) {
  49. LastKeyboardStates [i] = CurrentKeyboardStates [i];
  50. LastGamePadStates [i] = CurrentGamePadStates [i];
  51. CurrentKeyboardStates [i] = Keyboard.GetState ((PlayerIndex)i);
  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. GamePadWasConnected [i] = true;
  57. }
  58. }
  59. TouchState = TouchPanel.GetState ();
  60. LastMouseState = CurrentMouseState;
  61. CurrentMouseState = Mouse.GetState ();
  62. UpdateMouseStates();
  63. Gestures.Clear ();
  64. while (TouchPanel.IsGestureAvailable) {
  65. Gestures.Add (TouchPanel.ReadGesture ());
  66. }
  67. }
  68. bool dragging = false;
  69. bool dragComplete = false;
  70. bool leftMouseDown = false;
  71. int dragThreshold = 3;
  72. MouseGestureType mouseGestureType;
  73. Vector2 currentMousePosition = Vector2.Zero;
  74. Vector2 prevMousePosition = Vector2.Zero;
  75. Vector2 dragMouseStart = Vector2.Zero;
  76. Vector2 dragMouseEnd = Vector2.Zero;
  77. public MouseGestureType MouseGesture
  78. {
  79. get {
  80. return mouseGestureType;
  81. }
  82. }
  83. public Vector2 CurrentMousePosition
  84. {
  85. get {
  86. return currentMousePosition;
  87. }
  88. }
  89. public Vector2 PrevMousePosition
  90. {
  91. get {
  92. return prevMousePosition;
  93. }
  94. }
  95. public Vector2 MouseDelta
  96. {
  97. get {
  98. return prevMousePosition - currentMousePosition;
  99. }
  100. }
  101. public Vector2 MouseDragDelta
  102. {
  103. get {
  104. return dragMouseStart - dragMouseEnd;
  105. }
  106. }
  107. public Vector2 MouseDragStartPosition
  108. {
  109. get {
  110. return dragMouseStart;
  111. }
  112. }
  113. public Vector2 MouseDragEndPosition
  114. {
  115. get {
  116. return dragMouseEnd;
  117. }
  118. }
  119. void UpdateMouseStates ()
  120. {
  121. currentMousePosition.X = CurrentMouseState.X;
  122. currentMousePosition.Y = CurrentMouseState.Y;
  123. prevMousePosition.X = LastMouseState.X;
  124. prevMousePosition.Y = LastMouseState.Y;
  125. if (mouseGestureType.HasFlag(MouseGestureType.LeftClick))
  126. mouseGestureType = mouseGestureType ^ MouseGestureType.LeftClick;
  127. if (mouseGestureType.HasFlag(MouseGestureType.Move))
  128. mouseGestureType = mouseGestureType ^ MouseGestureType.Move;
  129. if (MouseDelta.Length() != 0)
  130. mouseGestureType = mouseGestureType | MouseGestureType.Move;
  131. // If we were dragging and the left mouse button was released
  132. // then we are no longer dragging and need to throw the banana.
  133. if (CurrentMouseState.LeftButton == ButtonState.Released &&
  134. dragging) {
  135. leftMouseDown = false;
  136. dragging = false;
  137. dragComplete = true;
  138. dragMouseEnd = currentMousePosition;
  139. mouseGestureType |= MouseGestureType.DragComplete;
  140. mouseGestureType = mouseGestureType ^ MouseGestureType.FreeDrag;
  141. //Console.WriteLine ("Dragging: " + mouseGestureType);
  142. }
  143. // Let's set the left mouse down and the mouse origin
  144. if (!leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Pressed &&
  145. !CurrentMouseState.Equals (LastMouseState)) {
  146. //Console.WriteLine ("left down");
  147. leftMouseDown = true;
  148. dragComplete = false;
  149. dragMouseStart = currentMousePosition;
  150. }
  151. if (leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Released &&
  152. !CurrentMouseState.Equals (LastMouseState)) {
  153. leftMouseDown = false;
  154. mouseGestureType |= MouseGestureType.LeftClick;
  155. }
  156. // Here we test the distance and if over the threshold then we set the dragging to true
  157. // Current threshold is 5 pixels.
  158. if (leftMouseDown && !dragging) {
  159. Vector2 delta = dragMouseStart - currentMousePosition;
  160. if (delta.Length() > dragThreshold) {
  161. dragging = true;
  162. dragMouseStart = currentMousePosition;
  163. mouseGestureType = mouseGestureType | MouseGestureType.FreeDrag;
  164. //Console.WriteLine ("Dragging: " + mouseGestureType);
  165. }
  166. }
  167. //Console.WriteLine(mouseGestureType);
  168. }
  169. /// <summary>
  170. /// Helper for checking if a key was newly pressed during this update. The
  171. /// controllingPlayer parameter specifies which player to read input for.
  172. /// If this is null, it will accept input from any player. When a keypress
  173. /// is detected, the output playerIndex reports which player pressed it.
  174. /// </summary>
  175. public bool IsNewKeyPress (Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  176. {
  177. if (controllingPlayer.HasValue) {
  178. // Read input from the specified player.
  179. playerIndex = controllingPlayer.Value;
  180. int i = (int)playerIndex;
  181. return (CurrentKeyboardStates [i].IsKeyDown (key) && LastKeyboardStates [i].IsKeyUp (key));
  182. } else {
  183. // Accept input from any player.
  184. return (IsNewKeyPress (key, PlayerIndex.One, out playerIndex) ||
  185. IsNewKeyPress (key, PlayerIndex.Two, out playerIndex) ||
  186. IsNewKeyPress (key, PlayerIndex.Three, out playerIndex) ||
  187. IsNewKeyPress (key, PlayerIndex.Four, out playerIndex));
  188. }
  189. }
  190. /// <summary>
  191. /// Helper for checking if a button was newly pressed during this update.
  192. /// The controllingPlayer parameter specifies which player to read input for.
  193. /// If this is null, it will accept input from any player. When a button press
  194. /// is detected, the output playerIndex reports which player pressed it.
  195. /// </summary>
  196. public bool IsNewButtonPress (Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  197. {
  198. if (controllingPlayer.HasValue) {
  199. // Read input from the specified player.
  200. playerIndex = controllingPlayer.Value;
  201. int i = (int)playerIndex;
  202. return (CurrentGamePadStates [i].IsButtonDown (button) && LastGamePadStates [i].IsButtonUp (button));
  203. } else {
  204. // Accept input from any player.
  205. return (IsNewButtonPress (button, PlayerIndex.One, out playerIndex) ||
  206. IsNewButtonPress (button, PlayerIndex.Two, out playerIndex) ||
  207. IsNewButtonPress (button, PlayerIndex.Three, out playerIndex) ||
  208. IsNewButtonPress (button, PlayerIndex.Four, out playerIndex));
  209. }
  210. }
  211. /// <summary>
  212. /// Checks for a "menu select" input action.
  213. /// The controllingPlayer parameter specifies which player to read input for.
  214. /// If this is null, it will accept input from any player. When the action
  215. /// is detected, the output playerIndex reports which player pressed it.
  216. /// </summary>
  217. public bool IsMenuSelect (PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  218. {
  219. return IsNewKeyPress (Keys.Space, controllingPlayer, out playerIndex) ||
  220. IsNewKeyPress (Keys.Enter, controllingPlayer, out playerIndex) ||
  221. IsNewButtonPress (Buttons.A, controllingPlayer, out playerIndex) ||
  222. IsNewButtonPress (Buttons.Start, controllingPlayer, out playerIndex);
  223. }
  224. /// <summary>
  225. /// Checks for a "menu cancel" input action.
  226. /// The controllingPlayer parameter specifies which player to read input for.
  227. /// If this is null, it will accept input from any player. When the action
  228. /// is detected, the output playerIndex reports which player pressed it.
  229. /// </summary>
  230. public bool IsMenuCancel (PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
  231. {
  232. return IsNewKeyPress (Keys.Escape, controllingPlayer, out playerIndex) ||
  233. IsNewButtonPress (Buttons.B, controllingPlayer, out playerIndex) ||
  234. IsNewButtonPress (Buttons.Back, controllingPlayer, out playerIndex);
  235. }
  236. /// <summary>
  237. /// Checks for a "menu up" input action.
  238. /// The controllingPlayer parameter specifies which player to read
  239. /// input for. If this is null, it will accept input from any player.
  240. /// </summary>
  241. public bool IsMenuUp (PlayerIndex? controllingPlayer)
  242. {
  243. PlayerIndex playerIndex;
  244. return IsNewKeyPress (Keys.Up, controllingPlayer, out playerIndex) ||
  245. IsNewButtonPress (Buttons.DPadUp, controllingPlayer, out playerIndex) ||
  246. IsNewButtonPress (Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
  247. }
  248. /// <summary>
  249. /// Checks for a "menu down" input action.
  250. /// The controllingPlayer parameter specifies which player to read
  251. /// input for. If this is null, it will accept input from any player.
  252. /// </summary>
  253. public bool IsMenuDown (PlayerIndex? controllingPlayer)
  254. {
  255. PlayerIndex playerIndex;
  256. return IsNewKeyPress (Keys.Down, controllingPlayer, out playerIndex) ||
  257. IsNewButtonPress (Buttons.DPadDown, controllingPlayer, out playerIndex) ||
  258. IsNewButtonPress (Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
  259. }
  260. /// <summary>
  261. /// Checks for a "pause the game" input action.
  262. /// The controllingPlayer parameter specifies which player to read
  263. /// input for. If this is null, it will accept input from any player.
  264. /// </summary>
  265. public bool IsPauseGame (PlayerIndex? controllingPlayer)
  266. {
  267. PlayerIndex playerIndex;
  268. return IsNewKeyPress (Keys.Escape, controllingPlayer, out playerIndex) ||
  269. IsNewButtonPress (Buttons.Back, controllingPlayer, out playerIndex) ||
  270. IsNewButtonPress (Buttons.Start, controllingPlayer, out playerIndex);
  271. }
  272. }
  273. }