Advertisement
DaveVoyles

InputState method parameters

May 13th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.75 KB | None | 0 0
  1. /* Looking over some of the inputs for the GSM kit for XNA, I've noticed that some of the
  2. booleans have two parameters, while others only have one. Why?
  3.  
  4.         /// <summary>
  5.         /// Checks for a "pause the game" input action.
  6.         /// The controllingPlayer parameter specifies which player to read
  7.         /// input for. If this is null, it will accept input from any player.
  8.         /// </summary>
  9.         public bool IsPauseGame(PlayerIndex? controllingPlayer)
  10.         {
  11.             PlayerIndex playerIndex;
  12.  
  13.             return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
  14.                    IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
  15.                    IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
  16.         }
  17.  
  18.  
  19.         /// <summary>
  20.         /// Checks for a "menu cancel" input action.
  21.         /// The controllingPlayer parameter specifies which player to read input for.
  22.         /// If this is null, it will accept input from any player. When the action
  23.         /// is detected, the output playerIndex reports which player pressed it.
  24.         /// </summary>
  25.         public bool IsMenuCancel(PlayerIndex? controllingPlayer,
  26.                                  out PlayerIndex playerIndex)
  27.         {
  28.             return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
  29.                    IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
  30.                    IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
  31.         }
  32.  
  33. */
  34.  
  35. #region File Description
  36. //-----------------------------------------------------------------------------
  37. // InputState.cs
  38. //
  39. // Microsoft XNA Community Game Platform
  40. // Copyright (C) Microsoft Corporation. All rights reserved.
  41. //-----------------------------------------------------------------------------
  42. #endregion
  43.  
  44. #region Using Statements
  45. using Microsoft.Xna.Framework;
  46. using Microsoft.Xna.Framework.Input;
  47. using Microsoft.Xna.Framework.Input.Touch;
  48. using System.Collections.Generic;
  49. #endregion
  50.  
  51. namespace GameStateManagement
  52. {
  53.     /// <summary>
  54.     /// Helper for reading input from keyboard, gamepad, and touch input. This class
  55.     /// tracks both the current and previous state of the input devices, and implements
  56.     /// query methods for high level input actions such as "move up through the menu"
  57.     /// or "pause the game".
  58.     /// </summary>
  59.     public class InputState
  60.     {
  61.         #region Fields
  62.  
  63.         public const int MaxInputs = 4;
  64.  
  65.         public readonly KeyboardState[] CurrentKeyboardStates;
  66.         public readonly GamePadState[] CurrentGamePadStates;
  67.  
  68.         public readonly KeyboardState[] LastKeyboardStates;
  69.         public readonly GamePadState[] LastGamePadStates;
  70.  
  71.         public readonly bool[] GamePadWasConnected;
  72.  
  73.         public TouchCollection TouchState;
  74.  
  75.         public readonly List<GestureSample> Gestures = new List<GestureSample>();
  76.  
  77.         #endregion
  78.  
  79.         #region Initialization
  80.  
  81.  
  82.         /// <summary>
  83.         /// Constructs a new input state.
  84.         /// </summary>
  85.         public InputState()
  86.         {
  87.             CurrentKeyboardStates = new KeyboardState[MaxInputs];
  88.             CurrentGamePadStates = new GamePadState[MaxInputs];
  89.  
  90.             LastKeyboardStates = new KeyboardState[MaxInputs];
  91.             LastGamePadStates = new GamePadState[MaxInputs];
  92.  
  93.             GamePadWasConnected = new bool[MaxInputs];
  94.         }
  95.  
  96.  
  97.         #endregion
  98.  
  99.         #region Public Methods
  100.  
  101.  
  102.         /// <summary>
  103.         /// Reads the latest state of the keyboard and gamepad.
  104.         /// </summary>
  105.         public void Update()
  106.         {
  107.             for (int i = 0; i < MaxInputs; i++)
  108.             {
  109.                 LastKeyboardStates[i] = CurrentKeyboardStates[i];
  110.                 LastGamePadStates[i] = CurrentGamePadStates[i];
  111.  
  112.                 CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
  113.                 CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
  114.  
  115.                 // Keep track of whether a gamepad has ever been
  116.                 // connected, so we can detect if it is unplugged.
  117.                 if (CurrentGamePadStates[i].IsConnected)
  118.                 {
  119.                     GamePadWasConnected[i] = true;
  120.                 }
  121.             }
  122.  
  123.             TouchState = TouchPanel.GetState();
  124.  
  125.             Gestures.Clear();
  126.             while (TouchPanel.IsGestureAvailable)
  127.             {
  128.                 Gestures.Add(TouchPanel.ReadGesture());
  129.             }
  130.         }
  131.  
  132.  
  133.         /// <summary>
  134.         /// Helper for checking if a key was newly pressed during this update. The
  135.         /// controllingPlayer parameter specifies which player to read input for.
  136.         /// If this is null, it will accept input from any player. When a keypress
  137.         /// is detected, the output playerIndex reports which player pressed it.
  138.         /// </summary>
  139.         public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer,
  140.                                             out PlayerIndex playerIndex)
  141.         {
  142.             if (controllingPlayer.HasValue)
  143.             {
  144.                 // Read input from the specified player.
  145.                 playerIndex = controllingPlayer.Value;
  146.  
  147.                 int i = (int)playerIndex;
  148.  
  149.                 return (CurrentKeyboardStates[i].IsKeyDown(key) &&
  150.                         LastKeyboardStates[i].IsKeyUp(key));
  151.             }
  152.             else
  153.             {
  154.                 // Accept input from any player.
  155.                 return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
  156.                         IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
  157.                         IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
  158.                         IsNewKeyPress(key, PlayerIndex.Four, out playerIndex));
  159.             }
  160.         }
  161.  
  162.  
  163.         /// <summary>
  164.         /// Helper for checking if a button was newly pressed during this update.
  165.         /// The controllingPlayer parameter specifies which player to read input for.
  166.         /// If this is null, it will accept input from any player. When a button press
  167.         /// is detected, the output playerIndex reports which player pressed it.
  168.         /// </summary>
  169.         public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer,
  170.                                                      out PlayerIndex playerIndex)
  171.         {
  172.             if (controllingPlayer.HasValue)
  173.             {
  174.                 // Read input from the specified player.
  175.                 playerIndex = controllingPlayer.Value;
  176.  
  177.                 int i = (int)playerIndex;
  178.  
  179.                 return (CurrentGamePadStates[i].IsButtonDown(button) &&
  180.                         LastGamePadStates[i].IsButtonUp(button));
  181.             }
  182.             else
  183.             {
  184.                 // Accept input from any player.
  185.                 return (IsNewButtonPress(button, PlayerIndex.One, out playerIndex) ||
  186.                         IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) ||
  187.                         IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) ||
  188.                         IsNewButtonPress(button, PlayerIndex.Four, out playerIndex));
  189.             }
  190.         }
  191.  
  192.  
  193.         /// <summary>
  194.         /// Checks for a "menu select" input action.
  195.         /// The controllingPlayer parameter specifies which player to read input for.
  196.         /// If this is null, it will accept input from any player. When the action
  197.         /// is detected, the output playerIndex reports which player pressed it.
  198.         /// </summary>
  199.         public bool IsMenuSelect(PlayerIndex? controllingPlayer,
  200.                                  out PlayerIndex playerIndex)
  201.         {
  202.             return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
  203.                    IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) ||
  204.                    IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) ||
  205.                    IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
  206.         }
  207.  
  208.  
  209.         /// <summary>
  210.         /// Checks for a "menu cancel" input action.
  211.         /// The controllingPlayer parameter specifies which player to read input for.
  212.         /// If this is null, it will accept input from any player. When the action
  213.         /// is detected, the output playerIndex reports which player pressed it.
  214.         /// </summary>
  215.         public bool IsMenuCancel(PlayerIndex? controllingPlayer,
  216.                                  out PlayerIndex playerIndex)
  217.         {
  218.             return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
  219.                    IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
  220.                    IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
  221.         }
  222.  
  223.  
  224.         /// <summary>
  225.         /// Checks for a "menu up" input action.
  226.         /// The controllingPlayer parameter specifies which player to read
  227.         /// input for. If this is null, it will accept input from any player.
  228.         /// </summary>
  229.         public bool IsMenuUp(PlayerIndex? controllingPlayer)
  230.         {
  231.             PlayerIndex playerIndex;
  232.  
  233.             return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex) ||
  234.                    IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex) ||
  235.                    IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
  236.         }
  237.  
  238.  
  239.         /// <summary>
  240.         /// Checks for a "menu down" input action.
  241.         /// The controllingPlayer parameter specifies which player to read
  242.         /// input for. If this is null, it will accept input from any player.
  243.         /// </summary>
  244.         public bool IsMenuDown(PlayerIndex? controllingPlayer)
  245.         {
  246.             PlayerIndex playerIndex;
  247.  
  248.             return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex) ||
  249.                    IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) ||
  250.                    IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
  251.         }
  252.  
  253.  
  254.         /// <summary>
  255.         /// Checks for a "pause the game" input action.
  256.         /// The controllingPlayer parameter specifies which player to read
  257.         /// input for. If this is null, it will accept input from any player.
  258.         /// </summary>
  259.         public bool IsPauseGame(PlayerIndex? controllingPlayer)
  260.         {
  261.             PlayerIndex playerIndex;
  262.  
  263.             return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
  264.                    IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
  265.                    IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
  266.         }
  267.  
  268.  
  269.         #endregion
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement