InputState.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /// Helper for checking if a key was newly pressed during this update.
  29. /// </summary>
  30. public bool IsNewKeyPress(Keys key)
  31. {
  32. return (CurrentKeyboardState.IsKeyDown(key) &&
  33. LastKeyboardState.IsKeyUp(key));
  34. }
  35. /// <summary>
  36. /// Checks for a "menu up" input action (on either keyboard or gamepad).
  37. /// </summary>
  38. public bool MenuUp
  39. {
  40. get
  41. {
  42. return IsNewKeyPress(Keys.Up) ||
  43. (CurrentGamePadState.DPad.Up == ButtonState.Pressed &&
  44. LastGamePadState.DPad.Up == ButtonState.Released) ||
  45. (CurrentGamePadState.ThumbSticks.Left.Y > 0 &&
  46. LastGamePadState.ThumbSticks.Left.Y <= 0);
  47. }
  48. }
  49. /// <summary>
  50. /// Checks for a "menu down" input action (on either keyboard or gamepad).
  51. /// </summary>
  52. public bool MenuDown
  53. {
  54. get
  55. {
  56. return IsNewKeyPress(Keys.Down) ||
  57. (CurrentGamePadState.DPad.Down == ButtonState.Pressed &&
  58. LastGamePadState.DPad.Down == ButtonState.Released) ||
  59. (CurrentGamePadState.ThumbSticks.Left.Y < 0 &&
  60. LastGamePadState.ThumbSticks.Left.Y >= 0);
  61. }
  62. }
  63. /// <summary>
  64. /// Checks for a "menu select" input action (on either keyboard or gamepad).
  65. /// </summary>
  66. public bool MenuSelect
  67. {
  68. get
  69. {
  70. return IsNewKeyPress(Keys.Space) ||
  71. IsNewKeyPress(Keys.Enter) ||
  72. (CurrentGamePadState.Buttons.A == ButtonState.Pressed &&
  73. LastGamePadState.Buttons.A == ButtonState.Released) ||
  74. (CurrentGamePadState.Buttons.Start == ButtonState.Pressed &&
  75. LastGamePadState.Buttons.Start == ButtonState.Released);
  76. }
  77. }
  78. /// <summary>
  79. /// Checks for a "menu cancel" input action (on either keyboard or gamepad).
  80. /// </summary>
  81. public bool MenuCancel
  82. {
  83. get
  84. {
  85. return IsNewKeyPress(Keys.Escape) ||
  86. (CurrentGamePadState.Buttons.B == ButtonState.Pressed &&
  87. LastGamePadState.Buttons.B == ButtonState.Released) ||
  88. (CurrentGamePadState.Buttons.Back == ButtonState.Pressed &&
  89. LastGamePadState.Buttons.Back == ButtonState.Released);
  90. }
  91. }
  92. /// <summary>
  93. /// Checks for a "pause the game" input action (on either keyboard or gamepad).
  94. /// </summary>
  95. public bool PauseGame
  96. {
  97. get
  98. {
  99. return IsNewKeyPress(Keys.Escape) ||
  100. (CurrentGamePadState.Buttons.Back == ButtonState.Pressed &&
  101. LastGamePadState.Buttons.Back == ButtonState.Released) ||
  102. (CurrentGamePadState.Buttons.Start == ButtonState.Pressed &&
  103. LastGamePadState.Buttons.Start == ButtonState.Released);
  104. }
  105. }
  106. /// <summary>
  107. /// Checks for a positive "ship color change" input action
  108. /// </summary>
  109. public bool ShipColorChangeUp
  110. {
  111. get
  112. {
  113. return IsNewKeyPress(Keys.Up) ||
  114. (CurrentGamePadState.Buttons.RightShoulder == ButtonState.Pressed &&
  115. LastGamePadState.Buttons.RightShoulder == ButtonState.Released);
  116. }
  117. }
  118. /// <summary>
  119. /// Checks for a negative "ship color change" input action.
  120. /// </summary>
  121. public bool ShipColorChangeDown
  122. {
  123. get
  124. {
  125. return IsNewKeyPress(Keys.Down) ||
  126. (CurrentGamePadState.Buttons.LeftShoulder == ButtonState.Pressed &&
  127. LastGamePadState.Buttons.LeftShoulder == ButtonState.Released);
  128. }
  129. }
  130. /// <summary>
  131. /// Checks for a positive "ship model change" input action.
  132. /// </summary>
  133. public bool ShipModelChangeUp
  134. {
  135. get
  136. {
  137. return IsNewKeyPress(Keys.Right) ||
  138. (CurrentGamePadState.Triggers.Right >= 1f &&
  139. LastGamePadState.Triggers.Right < 1f);
  140. }
  141. }
  142. /// <summary>
  143. /// Checks for a negative "ship model change" input action.
  144. /// </summary>
  145. public bool ShipModelChangeDown
  146. {
  147. get
  148. {
  149. return IsNewKeyPress(Keys.Left) ||
  150. (CurrentGamePadState.Triggers.Left >= 1f &&
  151. LastGamePadState.Triggers.Left < 1f);
  152. }
  153. }
  154. /// <summary>
  155. /// Checks for a "mark ready" input action (on either keyboard or gamepad).
  156. /// </summary>
  157. public bool MarkReady
  158. {
  159. get
  160. {
  161. return IsNewKeyPress(Keys.X) ||
  162. (CurrentGamePadState.Buttons.X == ButtonState.Pressed &&
  163. LastGamePadState.Buttons.X == ButtonState.Released);
  164. }
  165. }
  166. Matrix inputTransformation;
  167. readonly float baseBufferWidth;
  168. readonly float baseBufferHeight;
  169. /// <summary>
  170. /// Constructs a new input state.
  171. /// </summary>
  172. public InputState(float baseBufferWidth, float baseBufferHeight)
  173. {
  174. this.baseBufferWidth = baseBufferWidth;
  175. this.baseBufferHeight = baseBufferHeight;
  176. // Initialize the input state with the base buffer dimensions
  177. }
  178. /// <summary>
  179. /// Reads the latest state of the keyboard and gamepad.
  180. /// </summary>
  181. public void Update()
  182. {
  183. LastKeyboardState = CurrentKeyboardState;
  184. LastGamePadState = CurrentGamePadState;
  185. CurrentKeyboardState = Keyboard.GetState();
  186. CurrentGamePadState = GamePad.GetState(PlayerIndex.One);
  187. }
  188. /// <summary>
  189. /// Updates the matrix used to transform input coordinates.
  190. /// </summary>
  191. /// <param name="inputTransformation">The transformation matrix to apply.</param>
  192. public void UpdateInputTransformation(Matrix inputTransformation)
  193. {
  194. this.inputTransformation = inputTransformation;
  195. }
  196. }
  197. }