InputState.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 Flocking
  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 CurrentKeyState;
  29. public GamePadState CurrentPadState;
  30. public KeyboardState LastKeyState;
  31. public GamePadState LastPadState;
  32. #endregion
  33. #region Properties
  34. /// <summary>
  35. /// Checks for Y move amount on either keyboard or gamepad.
  36. /// </summary>
  37. public float MoveCatY
  38. {
  39. get
  40. {
  41. if (CurrentKeyState.IsKeyDown(Keys.W))
  42. {
  43. return -1.0f;
  44. }
  45. else if (CurrentKeyState.IsKeyDown(Keys.S))
  46. {
  47. return 1.0f;
  48. }
  49. else
  50. {
  51. //negative = move up
  52. return -(CurrentPadState.ThumbSticks.Left.Y);
  53. }
  54. }
  55. }
  56. /// <summary>
  57. /// Checks for X move amount on either keyboard or gamepad.
  58. /// </summary>
  59. public float MoveCatX
  60. {
  61. get
  62. {
  63. if (CurrentKeyState.IsKeyDown(Keys.A))
  64. {
  65. return -1.0f;
  66. }
  67. else if (CurrentKeyState.IsKeyDown(Keys.D))
  68. {
  69. return 1.0f;
  70. }
  71. else
  72. {
  73. return CurrentPadState.ThumbSticks.Left.X;
  74. }
  75. }
  76. }
  77. /// <summary>
  78. /// Checks for slider move amount on either keyboard or gamepad.
  79. /// </summary>
  80. public float SliderMove
  81. {
  82. get
  83. {
  84. if (CurrentKeyState.IsKeyDown(Keys.Left)||
  85. CurrentPadState.IsButtonDown(Buttons.DPadLeft))
  86. {
  87. return -1.0f;
  88. }
  89. else if (CurrentKeyState.IsKeyDown(Keys.Right) ||
  90. CurrentPadState.IsButtonDown(Buttons.DPadRight))
  91. {
  92. return 1.0f;
  93. }
  94. return -CurrentPadState.Triggers.Left + CurrentPadState.Triggers.Right;
  95. }
  96. }
  97. /// <summary>
  98. /// Checks for a "menu cancel" input action (on either keyboard or gamepad).
  99. /// </summary>
  100. public bool Exit
  101. {
  102. get
  103. {
  104. return IsNewKeyPress(Keys.Escape) ||
  105. (CurrentPadState.Buttons.Back == ButtonState.Pressed &&
  106. LastPadState.Buttons.Back == ButtonState.Released);
  107. }
  108. }
  109. /// <summary>
  110. /// Checks for a "reset distances" input action (on either keyboard or gamepad).
  111. /// </summary>
  112. public bool ResetDistances
  113. {
  114. get
  115. {
  116. return IsNewKeyPress(Keys.B) ||
  117. (CurrentPadState.Buttons.B == ButtonState.Pressed &&
  118. LastPadState.Buttons.B == ButtonState.Released);
  119. }
  120. }
  121. /// <summary>
  122. /// Checks for a "reset flock" input action (on either keyboard or gamepad).
  123. /// </summary>
  124. public bool ResetFlock
  125. {
  126. get
  127. {
  128. return IsNewKeyPress(Keys.X) ||
  129. (CurrentPadState.Buttons.X == ButtonState.Pressed &&
  130. LastPadState.Buttons.X == ButtonState.Released);
  131. }
  132. }
  133. /// <summary>
  134. /// Checks for an "up" input action (on either keyboard or gamepad).
  135. /// </summary>
  136. public bool Up
  137. {
  138. get
  139. {
  140. return IsNewKeyPress(Keys.Up) ||
  141. (CurrentPadState.DPad.Up == ButtonState.Pressed &&
  142. LastPadState.DPad.Up == ButtonState.Released);
  143. }
  144. }
  145. /// <summary>
  146. /// Checks for an "down" input action (on either keyboard or gamepad).
  147. /// </summary>
  148. public bool Down
  149. {
  150. get
  151. {
  152. return IsNewKeyPress(Keys.Down) ||
  153. (CurrentPadState.DPad.Down == ButtonState.Pressed &&
  154. LastPadState.DPad.Down == ButtonState.Released);
  155. }
  156. }
  157. /// <summary>
  158. /// Add or remove the cat
  159. /// </summary>
  160. public bool ToggleCatButton
  161. {
  162. get
  163. {
  164. return IsNewKeyPress(Keys.Y) ||
  165. (CurrentPadState.Buttons.Y == ButtonState.Pressed &&
  166. LastPadState.Buttons.Y == ButtonState.Released);
  167. }
  168. }
  169. #endregion
  170. #region Methods
  171. /// <summary>
  172. /// Reads the latest state of the keyboard and gamepad.
  173. /// </summary>
  174. public void Update()
  175. {
  176. LastKeyState = CurrentKeyState;
  177. LastPadState = CurrentPadState;
  178. CurrentKeyState = Keyboard.GetState();
  179. CurrentPadState = GamePad.GetState(PlayerIndex.One);
  180. }
  181. /// <summary>
  182. /// Helper for checking if a key was newly pressed during this update.
  183. /// </summary>
  184. bool IsNewKeyPress(Keys key)
  185. {
  186. return (CurrentKeyState.IsKeyDown(key) &&
  187. LastKeyState.IsKeyUp(key));
  188. }
  189. #endregion
  190. }
  191. }