InputState.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //-----------------------------------------------------------------------------
  2. // InputState.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Input;
  9. using Microsoft.Xna.Framework.Input.Touch;
  10. namespace AlienGameSample
  11. {
  12. /// <summary>
  13. /// Helper for reading input from keyboard and gamepad. This class tracks both
  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. public class InputState
  19. {
  20. public GamePadState CurrentGamePadStates;
  21. public GamePadState LastGamePadStates;
  22. public TouchCollection TouchStates;
  23. /// <summary>
  24. /// Constructs a new input state.
  25. /// </summary>
  26. public InputState()
  27. {
  28. CurrentGamePadStates = GamePad.GetState(PlayerIndex.One);
  29. TouchStates = TouchPanel.GetState();
  30. }
  31. /// <summary>
  32. /// Checks for a "menu up" input action, from any player,
  33. /// on either keyboard or gamepad.
  34. /// </summary>
  35. public bool MenuUp
  36. {
  37. get
  38. {
  39. return IsNewButtonPress(Buttons.DPadUp);
  40. }
  41. }
  42. /// <summary>
  43. /// Checks for a "menu down" input action, from any player,
  44. /// on either keyboard or gamepad.
  45. /// </summary>
  46. public bool MenuDown
  47. {
  48. get
  49. {
  50. return IsNewButtonPress(Buttons.DPadDown);
  51. }
  52. }
  53. /// <summary>
  54. /// Checks for a "menu select" input action, from any player,
  55. /// on either keyboard or gamepad.
  56. /// </summary>
  57. public bool MenuSelect
  58. {
  59. get
  60. {
  61. return IsNewButtonPress(Buttons.A);
  62. }
  63. }
  64. /// <summary>
  65. /// Checks for a "menu cancel" input action, from any player,
  66. /// on either keyboard or gamepad.
  67. /// </summary>
  68. public bool MenuCancel
  69. {
  70. get
  71. {
  72. return IsNewButtonPress(Buttons.Back);
  73. }
  74. }
  75. /// <summary>
  76. /// Checks for a "pause the game" input action, from any player,
  77. /// on either keyboard or gamepad.
  78. /// </summary>
  79. public bool PauseGame
  80. {
  81. get
  82. {
  83. return IsNewButtonPress(Buttons.Back) ||
  84. IsNewButtonPress(Buttons.Start);
  85. }
  86. }
  87. /// <summary>
  88. /// Reads the latest state of the keyboard and gamepad.
  89. /// </summary>
  90. public void Update()
  91. {
  92. LastGamePadStates = CurrentGamePadStates;
  93. CurrentGamePadStates = GamePad.GetState(PlayerIndex.One);
  94. TouchStates = TouchPanel.GetState();
  95. }
  96. /// <summary>
  97. /// Helper for checking if a button was newly pressed during this update,
  98. /// by any player.
  99. /// </summary>
  100. public bool IsNewButtonPress(Buttons button)
  101. {
  102. return IsNewButtonPress(button, PlayerIndex.One);
  103. }
  104. /// <summary>
  105. /// Helper for checking if a button was newly pressed during this update,
  106. /// by the specified player.
  107. /// </summary>
  108. public bool IsNewButtonPress(Buttons button, PlayerIndex playerIndex)
  109. {
  110. return (CurrentGamePadStates.IsButtonDown(button) &&
  111. LastGamePadStates.IsButtonUp(button));
  112. }
  113. /// <summary>
  114. /// Checks for a "menu select" input action from the specified player.
  115. /// </summary>
  116. public bool IsMenuSelect(PlayerIndex playerIndex)
  117. {
  118. return IsNewButtonPress(Buttons.A, playerIndex) ||
  119. IsNewButtonPress(Buttons.Start, playerIndex);
  120. }
  121. /// <summary>
  122. /// Checks for a "menu cancel" input action from the specified player.
  123. /// </summary>
  124. public bool IsMenuCancel(PlayerIndex playerIndex)
  125. {
  126. return IsNewButtonPress(Buttons.B, playerIndex) ||
  127. IsNewButtonPress(Buttons.Back, playerIndex);
  128. }
  129. }
  130. }