/truck/TankA/ScreenManager/InputState.cs
C# | 223 lines | 118 code | 42 blank | 63 comment | 4 complexity | 75adf7ce2dd872da54dadd56dae7b49a MD5 | raw file
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
10#region Using Statements
11using Microsoft.Xna.Framework;
12using Microsoft.Xna.Framework.Input;
13#endregion
14
15namespace TankA
16{
17 /// <summary>
18 /// Helper for reading input from keyboard and gamepad. This class tracks both
19 /// the current and previous state of both input devices, and implements query
20 /// methods for high level input actions such as "move up through the menu"
21 /// or "pause the game".
22 /// </summary>
23 public class InputState
24 {
25 #region Fields
26
27 public const int MaxInputs = 4;
28
29 public readonly KeyboardState[] CurrentKeyboardStates;
30 public readonly GamePadState[] CurrentGamePadStates;
31
32 public readonly KeyboardState[] LastKeyboardStates;
33 public readonly GamePadState[] LastGamePadStates;
34
35 public readonly bool[] GamePadWasConnected;
36
37 #endregion
38
39 #region Initialization
40
41
42 /// <summary>
43 /// Constructs a new input state.
44 /// </summary>
45 public InputState()
46 {
47 CurrentKeyboardStates = new KeyboardState[MaxInputs];
48 CurrentGamePadStates = new GamePadState[MaxInputs];
49
50 LastKeyboardStates = new KeyboardState[MaxInputs];
51 LastGamePadStates = new GamePadState[MaxInputs];
52
53 GamePadWasConnected = new bool[MaxInputs];
54 }
55
56
57 #endregion
58
59 #region Public Methods
60
61
62 /// <summary>
63 /// Reads the latest state of the keyboard and gamepad.
64 /// </summary>
65 public void Update()
66 {
67 for (int i = 0; i < MaxInputs; i++)
68 {
69 LastKeyboardStates[i] = CurrentKeyboardStates[i];
70 LastGamePadStates[i] = CurrentGamePadStates[i];
71
72 CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
73 CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
74
75 // Keep track of whether a gamepad has ever been
76 // connected, so we can detect if it is unplugged.
77 if (CurrentGamePadStates[i].IsConnected)
78 {
79 GamePadWasConnected[i] = true;
80 }
81 }
82 }
83
84
85 /// <summary>
86 /// Helper for checking if a key was newly pressed during this update. The
87 /// controllingPlayer parameter specifies which player to read input for.
88 /// If this is null, it will accept input from any player. When a keypress
89 /// is detected, the output playerIndex reports which player pressed it.
90 /// </summary>
91 public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
92 out PlayerIndex playerIndex)
93 {
94 if (controllingPlayer.HasValue)
95 {
96 // Read input from the specified player.
97 playerIndex = controllingPlayer.Value;
98
99 int i = (int)playerIndex;
100
101 return (CurrentKeyboardStates[i].IsKeyDown(key) &&
102 LastKeyboardStates[i].IsKeyUp(key));
103 }
104 else
105 {
106 // Accept input from any player.
107 return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
108 IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
109 IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
110 IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
111 }
112 }
113
114
115 /// <summary>
116 /// Helper for checking if a button was newly pressed during this update.
117 /// The controllingPlayer parameter specifies which player to read input for.
118 /// If this is null, it will accept input from any player. When a button press
119 /// is detected, the output playerIndex reports which player pressed it.
120 /// </summary>
121 public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer,
122 out PlayerIndex playerIndex)
123 {
124 if (controllingPlayer.HasValue)
125 {
126 // Read input from the specified player.
127 playerIndex = controllingPlayer.Value;
128
129 int i = (int)playerIndex;
130
131 return (CurrentGamePadStates[i].IsButtonDown(button) &&
132 LastGamePadStates[i].IsButtonUp(button));
133 }
134 else
135 {
136 // Accept input from any player.
137 return (IsNewButtonPress(button, PlayerIndex.One, out playerIndex) ||
138 IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) ||
139 IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) ||
140 IsNewButtonPress(button, PlayerIndex.Four, out playerIndex));
141 }
142 }
143
144
145 /// <summary>
146 /// Checks for a "menu select" input action.
147 /// The controllingPlayer parameter specifies which player to read input for.
148 /// If this is null, it will accept input from any player. When the action
149 /// is detected, the output playerIndex reports which player pressed it.
150 /// </summary>
151 public bool IsMenuSelect(PlayerIndex? controllingPlayer,
152 out PlayerIndex playerIndex)
153 {
154 return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
155 IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) ||
156 IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) ||
157 IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
158 }
159
160
161 /// <summary>
162 /// Checks for a "menu cancel" input action.
163 /// The controllingPlayer parameter specifies which player to read input for.
164 /// If this is null, it will accept input from any player. When the action
165 /// is detected, the output playerIndex reports which player pressed it.
166 /// </summary>
167 public bool IsMenuCancel(PlayerIndex? controllingPlayer,
168 out PlayerIndex playerIndex)
169 {
170 return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
171 IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
172 IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
173 }
174
175
176 /// <summary>
177 /// Checks for a "menu up" input action.
178 /// The controllingPlayer parameter specifies which player to read
179 /// input for. If this is null, it will accept input from any player.
180 /// </summary>
181 public bool IsMenuUp(PlayerIndex? controllingPlayer)
182 {
183 PlayerIndex playerIndex;
184
185 return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex) ||
186 IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex) ||
187 IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
188 }
189
190
191 /// <summary>
192 /// Checks for a "menu down" input action.
193 /// The controllingPlayer parameter specifies which player to read
194 /// input for. If this is null, it will accept input from any player.
195 /// </summary>
196 public bool IsMenuDown(PlayerIndex? controllingPlayer)
197 {
198 PlayerIndex playerIndex;
199
200 return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex) ||
201 IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) ||
202 IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
203 }
204
205
206 /// <summary>
207 /// Checks for a "pause the game" input action.
208 /// The controllingPlayer parameter specifies which player to read
209 /// input for. If this is null, it will accept input from any player.
210 /// </summary>
211 public bool IsPauseGame(PlayerIndex? controllingPlayer)
212 {
213 PlayerIndex playerIndex;
214
215 return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
216 IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
217 IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
218 }
219
220
221 #endregion
222 }
223}