InputState.cs 5.9 KB

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