GameScreenInput.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameScreenInput.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 Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Input;
  12. using RobotGameData.Input;
  13. #endregion
  14. namespace RobotGameData.Screen
  15. {
  16. /// <summary>
  17. /// Helper for reading input from keyboard and ControlPad. This class tracks both
  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. public class GameScreenInput
  23. {
  24. #region Fields
  25. InputComponent input = null;
  26. bool enable = true;
  27. #endregion
  28. #region Properties
  29. public bool Enable
  30. {
  31. get { return enable; }
  32. set { enable = value; }
  33. }
  34. /// <summary>
  35. /// Checks for a "menu up" input action (on either keyboard or GamePad).
  36. /// </summary>
  37. public bool MenuUp
  38. {
  39. get
  40. {
  41. if (!Enable) return false;
  42. return (
  43. (input.IsStrokeKey(Keys.W) &&
  44. (input.PlayerIndex == PlayerIndex.One)) ||
  45. (input.IsStrokeKey(Keys.Up) &&
  46. (input.PlayerIndex == PlayerIndex.Two)) ||
  47. input.IsStrokeControlPad(ControlPad.UpPad) ||
  48. input.IsStrokeControlPad(ControlPad.LeftThumbStickUp));
  49. }
  50. }
  51. /// <summary>
  52. /// Checks for a "menu down" input action (on either keyboard or GamePad).
  53. /// </summary>
  54. public bool MenuDown
  55. {
  56. get
  57. {
  58. if (!Enable) return false;
  59. return (
  60. (input.IsStrokeKey(Keys.S) &&
  61. (input.PlayerIndex == PlayerIndex.One)) ||
  62. (input.IsStrokeKey(Keys.Down) &&
  63. (input.PlayerIndex == PlayerIndex.Two)) ||
  64. input.IsStrokeControlPad(ControlPad.DownPad) ||
  65. input.IsStrokeControlPad(ControlPad.LeftThumbStickDown));
  66. }
  67. }
  68. /// <summary>
  69. /// Checks for a "menu left" input action (on either keyboard or GamePad).
  70. /// </summary>
  71. public bool MenuLeft
  72. {
  73. get
  74. {
  75. if (!Enable) return false;
  76. return (
  77. (input.IsStrokeKey(Keys.A) &&
  78. (input.PlayerIndex == PlayerIndex.One)) ||
  79. (input.IsStrokeKey(Keys.Left) &&
  80. (input.PlayerIndex == PlayerIndex.Two)) ||
  81. input.IsStrokeControlPad(ControlPad.LeftPad) ||
  82. input.IsStrokeControlPad(ControlPad.LeftThumbStickLeft));
  83. }
  84. }
  85. /// <summary>
  86. /// Checks for a "menu right" input action (on either keyboard or GamePad).
  87. /// </summary>
  88. public bool MenuRight
  89. {
  90. get
  91. {
  92. if (!Enable) return false;
  93. return (
  94. (input.IsStrokeKey(Keys.D) &&
  95. (input.PlayerIndex == PlayerIndex.One)) ||
  96. (input.IsStrokeKey(Keys.Right) &&
  97. (input.PlayerIndex == PlayerIndex.Two)) ||
  98. input.IsStrokeControlPad(ControlPad.RightPad) ||
  99. input.IsStrokeControlPad(ControlPad.LeftThumbStickRight));
  100. }
  101. }
  102. /// <summary>
  103. /// Checks for a "menu select" input action (on either keyboard or GamePad).
  104. /// </summary>
  105. public bool MenuSelect
  106. {
  107. get
  108. {
  109. if (!Enable) return false;
  110. return
  111. (!input.IsPressKey(Keys.LeftAlt) &&
  112. !input.IsPressKey(Keys.RightAlt) &&
  113. (input.IsStrokeKey(Keys.Space) || input.IsStrokeKey(Keys.Enter)) ||
  114. input.IsStrokeControlPad(ControlPad.A));
  115. }
  116. }
  117. /// <summary>
  118. /// Checks for a "menu cancel" input action (on either keyboard or GamePad).
  119. /// </summary>
  120. public bool MenuExit
  121. {
  122. get
  123. {
  124. if (!Enable) return false;
  125. return (input.IsStrokeKey(Keys.Escape) ||
  126. input.IsStrokeControlPad(ControlPad.Back));
  127. }
  128. }
  129. /// <summary>
  130. /// Checks for a "menu cancel" input action (on either keyboard or GamePad).
  131. /// </summary>
  132. public bool MenuCancel
  133. {
  134. get
  135. {
  136. if (!Enable) return false;
  137. return (input.IsStrokeKey(Keys.LeftControl) ||
  138. input.IsStrokeControlPad(ControlPad.B) ||
  139. input.IsStrokeControlPad(ControlPad.Y));
  140. }
  141. }
  142. /// <summary>
  143. /// Checks for a "pause the game" input action (on either keyboard or GamePad).
  144. /// </summary>
  145. public bool PauseGame
  146. {
  147. get
  148. {
  149. if (!Enable) return false;
  150. return (input.IsStrokeKey(Keys.Escape) ||
  151. input.IsStrokeControlPad(ControlPad.Start) ||
  152. input.IsStrokeControlPad(ControlPad.Back));
  153. }
  154. }
  155. #endregion
  156. /// <summary>
  157. /// Constructor.
  158. /// </summary>
  159. /// <param name="index">player index</param>
  160. public GameScreenInput(PlayerIndex index)
  161. {
  162. input = FrameworkCore.InputManager.GetInputComponent(index);
  163. }
  164. /// <summary>
  165. /// resets input state.
  166. /// </summary>
  167. public void Reset()
  168. {
  169. input.Reset();
  170. enable = true;
  171. }
  172. }
  173. }