Helper for reading input from keyboard and gamepad. This class tracks both the current and previous state of both input devices, and implements query methods for high level input actions such as "move up through the menu" or "pause the game". More...
Public Member Functions | |
| InputState () | |
| Constructs a new input state. | |
| void | Update () |
| Reads the latest state of the keyboard and gamepad. | |
| bool | IsKeyDown (Keys key, PlayerIndex?controllingPlayer, out PlayerIndex playerIndex) |
| Helper for checking if a key is currently pressed. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a keydown is detected, the output playerIndex reports which player pressed it. | |
| bool | IsNewKeyPress (Keys key, PlayerIndex?controllingPlayer, out PlayerIndex playerIndex) |
| Helper for checking if a key was newly pressed during this update. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a keypress is detected, the output playerIndex reports which player pressed it. | |
| bool | IsButtonDown (Buttons button, PlayerIndex?controllingPlayer, out PlayerIndex playerIndex) |
| Helper for checking if a button is currently pressed. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a button down is detected, the output playerIndex reports which player pressed it. | |
| bool | IsNewButtonPress (Buttons button, PlayerIndex?controllingPlayer, out PlayerIndex playerIndex) |
| Helper for checking if a button was newly pressed during this update. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a button press is detected, the output playerIndex reports which player pressed it. | |
| bool | IsMenuSelect (PlayerIndex?controllingPlayer, out PlayerIndex playerIndex) |
| Checks for a "menu select" input action. Enables on Space, Enter, A and Start. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When the action is detected, the output playerIndex reports which player pressed it. | |
| bool | IsMenuCancel (PlayerIndex?controllingPlayer, out PlayerIndex playerIndex) |
| Checks for a "menu cancel" input action. Enables on Escape, B and Back. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When the action is detected, the output playerIndex reports which player pressed it. | |
| bool | IsInputUp (PlayerIndex?controllingPlayer) |
| Checks for an "up" input state. Enables on Up (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsNewInputUp (PlayerIndex?controllingPlayer) |
| Checks for a "up" input action (key up toggle). Enables on Up (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsInputDown (PlayerIndex?controllingPlayer) |
| Checks if an "down" input state. Enables on Down (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsNewInputDown (PlayerIndex?controllingPlayer) |
| Checks for a "down" input action (key down toggle). Enables on Down (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsInputLeft (PlayerIndex?controllingPlayer) |
| Checks for a "left" input state. Enables on Left (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsNewInputLeft (PlayerIndex?controllingPlayer) |
| Checks for a "left" input action (key left toggle). Enables on Left (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsInputRight (PlayerIndex?controllingPlayer) |
| Checks for a "right" input state. Enables on Right (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsNewInputRight (PlayerIndex?controllingPlayer) |
| Checks for a "right" input action (key right toggle). Enables on Right (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsInputCancel (PlayerIndex?controllingPlayer) |
| Checks for a "cancel" input action (key toggle). Enables on Escape, Back and Start. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsInputSelect (PlayerIndex?controllingPlayer) |
| Checks for a "select" input action. Enables on Space, Enter, A and RightTrigger. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
| bool | IsDisconnected (PlayerIndex?controllingPlayer) |
| Checks for a disconnection occurence (i.e. was connected, but is not anymore). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. | |
Public Attributes | |
| const int | MaxInputs = 4 |
| readonly KeyboardState[] | CurrentKeyboardStates |
| readonly GamePadState[] | CurrentGamePadStates |
| readonly KeyboardState[] | LastKeyboardStates |
| readonly GamePadState[] | LastGamePadStates |
| readonly bool[] | GamePadWasConnected |
Helper for reading input from keyboard and gamepad. This class tracks both the current and previous state of both input devices, and implements query methods for high level input actions such as "move up through the menu" or "pause the game".
Definition at line 19 of file InputState.cs.
| NewGamePhysics.StateManager.InputState.InputState | ( | ) |
Constructs a new input state.
Definition at line 45 of file InputState.cs.
00046 { 00047 CurrentKeyboardStates = new KeyboardState[MaxInputs]; 00048 CurrentGamePadStates = new GamePadState[MaxInputs]; 00049 00050 LastKeyboardStates = new KeyboardState[MaxInputs]; 00051 LastGamePadStates = new GamePadState[MaxInputs]; 00052 00053 GamePadWasConnected = new bool[MaxInputs]; 00054 00055 // New DirectX input object 00056 this.gamepads = new Gamepads(); 00057 }
| bool NewGamePhysics.StateManager.InputState.IsButtonDown | ( | Buttons | button, | |
| PlayerIndex? | controllingPlayer, | |||
| out PlayerIndex | playerIndex | |||
| ) |
Helper for checking if a button is currently pressed. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a button down is detected, the output playerIndex reports which player pressed it.
Definition at line 153 of file InputState.cs.
00155 { 00156 if (controllingPlayer.HasValue) 00157 { 00158 // Read input from the specified player. 00159 playerIndex = controllingPlayer.Value; 00160 00161 int i = (int)playerIndex; 00162 00163 return (CurrentGamePadStates[i].IsButtonDown(button)); 00164 } 00165 else 00166 { 00167 // Accept input from any player. 00168 return (IsButtonDown(button, PlayerIndex.One, out playerIndex) || 00169 IsButtonDown(button, PlayerIndex.Two, out playerIndex) || 00170 IsButtonDown(button, PlayerIndex.Three, out playerIndex) || 00171 IsButtonDown(button, PlayerIndex.Four, out playerIndex)); 00172 } 00173 }
| bool NewGamePhysics.StateManager.InputState.IsDisconnected | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a disconnection occurence (i.e. was connected, but is not anymore). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 392 of file InputState.cs.
00393 { 00394 if (controllingPlayer.HasValue) 00395 { 00396 // Check state from the specified player. 00397 PlayerIndex playerIndex = controllingPlayer.Value; 00398 int i = (int)playerIndex; 00399 return (!this.CurrentGamePadStates[i].IsConnected && this.GamePadWasConnected[i]); 00400 } 00401 else 00402 { 00403 // Check state from any player. 00404 return (!this.CurrentGamePadStates[(int)PlayerIndex.One].IsConnected && this.GamePadWasConnected[(int)PlayerIndex.One]) || 00405 (!this.CurrentGamePadStates[(int)PlayerIndex.Two].IsConnected && this.GamePadWasConnected[(int)PlayerIndex.Two]) || 00406 (!this.CurrentGamePadStates[(int)PlayerIndex.Three].IsConnected && this.GamePadWasConnected[(int)PlayerIndex.Three]) || 00407 (!this.CurrentGamePadStates[(int)PlayerIndex.Four].IsConnected && this.GamePadWasConnected[(int)PlayerIndex.Four]); 00408 } 00409 }
| bool NewGamePhysics.StateManager.InputState.IsInputCancel | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "cancel" input action (key toggle). Enables on Escape, Back and Start. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 362 of file InputState.cs.
00363 { 00364 PlayerIndex playerIndex; 00365 00366 return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) || 00367 IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) || 00368 IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex); 00369 }
| bool NewGamePhysics.StateManager.InputState.IsInputDown | ( | PlayerIndex? | controllingPlayer | ) |
Checks if an "down" input state. Enables on Down (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 272 of file InputState.cs.
00273 { 00274 PlayerIndex playerIndex; 00275 00276 return IsKeyDown(Keys.Down, controllingPlayer, out playerIndex) || 00277 IsButtonDown(Buttons.DPadDown, controllingPlayer, out playerIndex) || 00278 IsButtonDown(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex); 00279 }
| bool NewGamePhysics.StateManager.InputState.IsInputLeft | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "left" input state. Enables on Left (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 302 of file InputState.cs.
00303 { 00304 PlayerIndex playerIndex; 00305 00306 return IsKeyDown(Keys.Left, controllingPlayer, out playerIndex) || 00307 IsButtonDown(Buttons.DPadLeft, controllingPlayer, out playerIndex) || 00308 IsButtonDown(Buttons.LeftThumbstickLeft, controllingPlayer, out playerIndex); 00309 }
| bool NewGamePhysics.StateManager.InputState.IsInputRight | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "right" input state. Enables on Right (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 332 of file InputState.cs.
00333 { 00334 PlayerIndex playerIndex; 00335 00336 return IsKeyDown(Keys.Right, controllingPlayer, out playerIndex) || 00337 IsButtonDown(Buttons.DPadRight, controllingPlayer, out playerIndex) || 00338 IsButtonDown(Buttons.LeftThumbstickRight, controllingPlayer, out playerIndex); 00339 }
| bool NewGamePhysics.StateManager.InputState.IsInputSelect | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "select" input action. Enables on Space, Enter, A and RightTrigger. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 377 of file InputState.cs.
00378 { 00379 PlayerIndex playerIndex; 00380 00381 return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) || 00382 IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) || 00383 IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) || 00384 IsNewButtonPress(Buttons.RightTrigger, controllingPlayer, out playerIndex); 00385 }
| bool NewGamePhysics.StateManager.InputState.IsInputUp | ( | PlayerIndex? | controllingPlayer | ) |
Checks for an "up" input state. Enables on Up (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 242 of file InputState.cs.
00243 { 00244 PlayerIndex playerIndex; 00245 00246 return IsKeyDown(Keys.Up, controllingPlayer, out playerIndex) || 00247 IsButtonDown(Buttons.DPadUp, controllingPlayer, out playerIndex) || 00248 IsButtonDown(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex); 00249 }
| bool NewGamePhysics.StateManager.InputState.IsKeyDown | ( | Keys | key, | |
| PlayerIndex? | controllingPlayer, | |||
| out PlayerIndex | playerIndex | |||
| ) |
Helper for checking if a key is currently pressed. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a keydown is detected, the output playerIndex reports which player pressed it.
Definition at line 97 of file InputState.cs.
00099 { 00100 if (controllingPlayer.HasValue) 00101 { 00102 // Read input from the specified player. 00103 playerIndex = controllingPlayer.Value; 00104 00105 int i = (int)playerIndex; 00106 00107 return (CurrentKeyboardStates[i].IsKeyDown(key)); 00108 } 00109 else 00110 { 00111 // Accept input from any player. 00112 return (IsKeyDown(key, PlayerIndex.One, out playerIndex) || 00113 IsKeyDown(key, PlayerIndex.Two, out playerIndex) || 00114 IsKeyDown(key, PlayerIndex.Three, out playerIndex) || 00115 IsKeyDown(key, PlayerIndex.Four, out playerIndex)); 00116 } 00117 }
| bool NewGamePhysics.StateManager.InputState.IsMenuCancel | ( | PlayerIndex? | controllingPlayer, | |
| out PlayerIndex | playerIndex | |||
| ) |
Checks for a "menu cancel" input action. Enables on Escape, B and Back. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When the action is detected, the output playerIndex reports which player pressed it.
Definition at line 228 of file InputState.cs.
00230 { 00231 return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) || 00232 IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) || 00233 IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex); 00234 }
| bool NewGamePhysics.StateManager.InputState.IsMenuSelect | ( | PlayerIndex? | controllingPlayer, | |
| out PlayerIndex | playerIndex | |||
| ) |
Checks for a "menu select" input action. Enables on Space, Enter, A and Start. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When the action is detected, the output playerIndex reports which player pressed it.
Definition at line 211 of file InputState.cs.
00213 { 00214 return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) || 00215 IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) || 00216 IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) || 00217 IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex); 00218 }
| bool NewGamePhysics.StateManager.InputState.IsNewButtonPress | ( | Buttons | button, | |
| PlayerIndex? | controllingPlayer, | |||
| out PlayerIndex | playerIndex | |||
| ) |
Helper for checking if a button was newly pressed during this update. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a button press is detected, the output playerIndex reports which player pressed it.
Definition at line 181 of file InputState.cs.
00183 { 00184 if (controllingPlayer.HasValue) 00185 { 00186 // Read input from the specified player. 00187 playerIndex = controllingPlayer.Value; 00188 00189 int i = (int)playerIndex; 00190 00191 return (CurrentGamePadStates[i].IsButtonDown(button) && 00192 LastGamePadStates[i].IsButtonUp(button)); 00193 } 00194 else 00195 { 00196 // Accept input from any player. 00197 return (IsNewButtonPress(button, PlayerIndex.One, out playerIndex) || 00198 IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) || 00199 IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) || 00200 IsNewButtonPress(button, PlayerIndex.Four, out playerIndex)); 00201 } 00202 }
| bool NewGamePhysics.StateManager.InputState.IsNewInputDown | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "down" input action (key down toggle). Enables on Down (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 287 of file InputState.cs.
00288 { 00289 PlayerIndex playerIndex; 00290 00291 return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex) || 00292 IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) || 00293 IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex); 00294 }
| bool NewGamePhysics.StateManager.InputState.IsNewInputLeft | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "left" input action (key left toggle). Enables on Left (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 317 of file InputState.cs.
00318 { 00319 PlayerIndex playerIndex; 00320 00321 return IsNewKeyPress(Keys.Left, controllingPlayer, out playerIndex) || 00322 IsNewButtonPress(Buttons.DPadLeft, controllingPlayer, out playerIndex) || 00323 IsNewButtonPress(Buttons.LeftThumbstickLeft, controllingPlayer, out playerIndex); 00324 }
| bool NewGamePhysics.StateManager.InputState.IsNewInputRight | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "right" input action (key right toggle). Enables on Right (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 347 of file InputState.cs.
00348 { 00349 PlayerIndex playerIndex; 00350 00351 return IsNewKeyPress(Keys.Right, controllingPlayer, out playerIndex) || 00352 IsNewButtonPress(Buttons.DPadRight, controllingPlayer, out playerIndex) || 00353 IsNewButtonPress(Buttons.LeftThumbstickRight, controllingPlayer, out playerIndex); 00354 }
| bool NewGamePhysics.StateManager.InputState.IsNewInputUp | ( | PlayerIndex? | controllingPlayer | ) |
Checks for a "up" input action (key up toggle). Enables on Up (cursor, dpad and thumbstick). The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player.
Definition at line 257 of file InputState.cs.
00258 { 00259 PlayerIndex playerIndex; 00260 00261 return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex) || 00262 IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex) || 00263 IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex); 00264 }
| bool NewGamePhysics.StateManager.InputState.IsNewKeyPress | ( | Keys | key, | |
| PlayerIndex? | controllingPlayer, | |||
| out PlayerIndex | playerIndex | |||
| ) |
Helper for checking if a key was newly pressed during this update. The controllingPlayer parameter specifies which player to read input for. If this is null, it will accept input from any player. When a keypress is detected, the output playerIndex reports which player pressed it.
Definition at line 125 of file InputState.cs.
00127 { 00128 if (controllingPlayer.HasValue) 00129 { 00130 // Read input from the specified player. 00131 playerIndex = controllingPlayer.Value; 00132 00133 int i = (int)playerIndex; 00134 00135 return (CurrentKeyboardStates[i].IsKeyDown(key) && 00136 LastKeyboardStates[i].IsKeyUp(key)); 00137 } 00138 else 00139 { 00140 // Accept input from any player. 00141 return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) || 00142 IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) || 00143 IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) || 00144 IsNewKeyPress(key, PlayerIndex.Four, out playerIndex)); 00145 } 00146 }
| void NewGamePhysics.StateManager.InputState.Update | ( | ) |
Reads the latest state of the keyboard and gamepad.
Definition at line 67 of file InputState.cs.
00068 { 00069 for (int i = 0; i < MaxInputs; i++) 00070 { 00071 LastKeyboardStates[i] = CurrentKeyboardStates[i]; 00072 LastGamePadStates[i] = CurrentGamePadStates[i]; 00073 00074 CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i); 00075 CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i); 00076 00077 // Keep track of whether a gamepad has ever been 00078 // connected, so we can detect if it is unplugged. 00079 if (CurrentGamePadStates[i].IsConnected) 00080 { 00081 GamePadWasConnected[i] = true; 00082 } 00083 } 00084 00085 // Check gamepad 00086 if ((this.gamepads != null) && (this.gamepads.Items.Count > 0)) 00087 { 00088 } 00089 }
1.6.2