InputState.cs 6.7 KB

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