InputState.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Input;
  13. #endregion
  14. namespace NetRumble
  15. {
  16. /// <summary>
  17. /// Helper for reading input from keyboard and gamepad. This public class tracks
  18. /// the current and previous state of both input devices, and implements query
  19. /// properties for high level input actions such as "move up through the menu"
  20. /// or "pause the game".
  21. /// </summary>
  22. /// <remarks>
  23. /// This public class is similar to one in the GameStateManagement sample.
  24. /// </remarks>
  25. public class InputState
  26. {
  27. #region Fields
  28. public KeyboardState CurrentKeyboardState;
  29. public GamePadState CurrentGamePadState;
  30. public KeyboardState LastKeyboardState;
  31. public GamePadState LastGamePadState;
  32. #endregion
  33. #region Properties
  34. /// <summary>
  35. /// Checks for a "menu up" input action (on either keyboard or gamepad).
  36. /// </summary>
  37. public bool MenuUp
  38. {
  39. get
  40. {
  41. return IsNewKeyPress(Keys.Up) ||
  42. (CurrentGamePadState.DPad.Up == ButtonState.Pressed &&
  43. LastGamePadState.DPad.Up == ButtonState.Released) ||
  44. (CurrentGamePadState.ThumbSticks.Left.Y > 0 &&
  45. LastGamePadState.ThumbSticks.Left.Y <= 0);
  46. }
  47. }
  48. /// <summary>
  49. /// Checks for a "menu down" input action (on either keyboard or gamepad).
  50. /// </summary>
  51. public bool MenuDown
  52. {
  53. get
  54. {
  55. return IsNewKeyPress(Keys.Down) ||
  56. (CurrentGamePadState.DPad.Down == ButtonState.Pressed &&
  57. LastGamePadState.DPad.Down == ButtonState.Released) ||
  58. (CurrentGamePadState.ThumbSticks.Left.Y < 0 &&
  59. LastGamePadState.ThumbSticks.Left.Y >= 0);
  60. }
  61. }
  62. /// <summary>
  63. /// Checks for a "menu select" input action (on either keyboard or gamepad).
  64. /// </summary>
  65. public bool MenuSelect
  66. {
  67. get
  68. {
  69. return IsNewKeyPress(Keys.Space) ||
  70. IsNewKeyPress(Keys.Enter) ||
  71. (CurrentGamePadState.Buttons.A == ButtonState.Pressed &&
  72. LastGamePadState.Buttons.A == ButtonState.Released) ||
  73. (CurrentGamePadState.Buttons.Start == ButtonState.Pressed &&
  74. LastGamePadState.Buttons.Start == ButtonState.Released);
  75. }
  76. }
  77. /// <summary>
  78. /// Checks for a "menu cancel" input action (on either keyboard or gamepad).
  79. /// </summary>
  80. public bool MenuCancel
  81. {
  82. get
  83. {
  84. return IsNewKeyPress(Keys.Escape) ||
  85. (CurrentGamePadState.Buttons.B == ButtonState.Pressed &&
  86. LastGamePadState.Buttons.B == ButtonState.Released) ||
  87. (CurrentGamePadState.Buttons.Back == ButtonState.Pressed &&
  88. LastGamePadState.Buttons.Back == ButtonState.Released);
  89. }
  90. }
  91. /// <summary>
  92. /// Checks for a "pause the game" input action (on either keyboard or gamepad).
  93. /// </summary>
  94. public bool PauseGame
  95. {
  96. get
  97. {
  98. return IsNewKeyPress(Keys.Escape) ||
  99. (CurrentGamePadState.Buttons.Back == ButtonState.Pressed &&
  100. LastGamePadState.Buttons.Back == ButtonState.Released) ||
  101. (CurrentGamePadState.Buttons.Start == ButtonState.Pressed &&
  102. LastGamePadState.Buttons.Start == ButtonState.Released);
  103. }
  104. }
  105. /// <summary>
  106. /// Checks for a positive "ship color change" input action
  107. /// </summary>
  108. public bool ShipColorChangeUp
  109. {
  110. get
  111. {
  112. return IsNewKeyPress(Keys.Up) ||
  113. (CurrentGamePadState.Buttons.RightShoulder == ButtonState.Pressed &&
  114. LastGamePadState.Buttons.RightShoulder == ButtonState.Released);
  115. }
  116. }
  117. /// <summary>
  118. /// Checks for a negative "ship color change" input action.
  119. /// </summary>
  120. public bool ShipColorChangeDown
  121. {
  122. get
  123. {
  124. return IsNewKeyPress(Keys.Down) ||
  125. (CurrentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed &&
  126. LastGamePadState.Buttons.LeftShoulder == ButtonState.Released);
  127. }
  128. }
  129. /// <summary>
  130. /// Checks for a positive "ship model change" input action.
  131. /// </summary>
  132. public bool ShipModelChangeUp
  133. {
  134. get
  135. {
  136. return IsNewKeyPress(Keys.Right) ||
  137. (CurrentGamePadState.Triggers.Right >= 1f &&
  138. LastGamePadState.Triggers.Right < 1f);
  139. }
  140. }
  141. /// <summary>
  142. /// Checks for a negative "ship model change" input action.
  143. /// </summary>
  144. public bool ShipModelChangeDown
  145. {
  146. get
  147. {
  148. return IsNewKeyPress(Keys.Left) ||
  149. (CurrentGamePadState.Triggers.Left >= 1f &&
  150. LastGamePadState.Triggers.Left < 1f);
  151. }
  152. }
  153. /// <summary>
  154. /// Checks for a "mark ready" input action (on either keyboard or gamepad).
  155. /// </summary>
  156. public bool MarkReady
  157. {
  158. get
  159. {
  160. return IsNewKeyPress(Keys.X) ||
  161. (CurrentGamePadState.Buttons.X == ButtonState.Pressed &&
  162. LastGamePadState.Buttons.X == ButtonState.Released);
  163. }
  164. }
  165. #endregion
  166. #region Methods
  167. /// <summary>
  168. /// Reads the latest state of the keyboard and gamepad.
  169. /// </summary>
  170. public void Update()
  171. {
  172. LastKeyboardState = CurrentKeyboardState;
  173. LastGamePadState = CurrentGamePadState;
  174. CurrentKeyboardState = Keyboard.GetState();
  175. CurrentGamePadState = GamePad.GetState(PlayerIndex.One);
  176. }
  177. /// <summary>
  178. /// Helper for checking if a key was newly pressed during this update.
  179. /// </summary>
  180. public bool IsNewKeyPress(Keys key)
  181. {
  182. return (CurrentKeyboardState.IsKeyDown(key) &&
  183. LastKeyboardState.IsKeyUp(key));
  184. }
  185. #endregion
  186. }
  187. }