- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/NewGamePhysics/StateManager/InputState.cs

    00001 //-----------------------------------------------------------------------------
    00002 // InputState.cs
    00003 // (Based on XNA Community Game Platform Demo, Microsoft Corp., 2008)
    00004 //-----------------------------------------------------------------------------
    00005
    00006 namespace NewGamePhysics.StateManager
    00007 {
    00008     using Microsoft.Xna.Framework;
    00009     using Microsoft.Xna.Framework.Input;
    00010
    00011     using NewGamePhysics.Utilities;
    00012
    00019     public class InputState
    00020     {
    00021         #region Fields
    00022
    00023         public const int MaxInputs = 4;
    00024
    00025         public readonly KeyboardState[] CurrentKeyboardStates;
    00026         public readonly GamePadState[] CurrentGamePadStates;
    00027
    00028         public readonly KeyboardState[] LastKeyboardStates;
    00029         public readonly GamePadState[] LastGamePadStates;
    00030
    00031         public readonly bool[] GamePadWasConnected;
    00032
    00036         private Gamepads gamepads;
    00037
    00038         #endregion
    00039
    00040         #region Initialization
    00041
    00045         public InputState()
    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         }
    00058
    00059
    00060         #endregion
    00061
    00062         #region Public Methods
    00063
    00067         public void Update()
    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         }
    00090
    00097         public bool IsKeyDown(Keys key, PlayerIndex? controllingPlayer,
    00098                                             out PlayerIndex playerIndex)
    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         }
    00118
    00125         public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
    00126                                             out PlayerIndex playerIndex)
    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         }
    00153         public bool IsButtonDown(Buttons button, PlayerIndex? controllingPlayer,
    00154                                                      out PlayerIndex playerIndex)
    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         }
    00174
    00181         public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer,
    00182                                                      out PlayerIndex playerIndex)
    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         }
    00203
    00211         public bool IsMenuSelect(PlayerIndex? controllingPlayer,
    00212                                  out PlayerIndex playerIndex)
    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         }
    00219
    00220
    00228         public bool IsMenuCancel(PlayerIndex? controllingPlayer,
    00229                                  out PlayerIndex playerIndex)
    00230         {
    00231             return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
    00232                    IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
    00233                    IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
    00234         }
    00235
    00242         public bool IsInputUp(PlayerIndex? controllingPlayer)
    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         }
    00250
    00257         public bool IsNewInputUp(PlayerIndex? controllingPlayer)
    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         }
    00265
    00272         public bool IsInputDown(PlayerIndex? controllingPlayer)
    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         }
    00280
    00287         public bool IsNewInputDown(PlayerIndex? controllingPlayer)
    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         }
    00295
    00302         public bool IsInputLeft(PlayerIndex? controllingPlayer)
    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         }
    00310
    00317         public bool IsNewInputLeft(PlayerIndex? controllingPlayer)
    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         }
    00325
    00332         public bool IsInputRight(PlayerIndex? controllingPlayer)
    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         }
    00340
    00347         public bool IsNewInputRight(PlayerIndex? controllingPlayer)
    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         }
    00355
    00362         public bool IsInputCancel(PlayerIndex? controllingPlayer)
    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         }
    00370
    00377         public bool IsInputSelect(PlayerIndex? controllingPlayer)
    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         }
    00386
    00392         public bool IsDisconnected(PlayerIndex? controllingPlayer)
    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         }
    00410         #endregion
    00411     }
    00412 }