Advertisement
Guest User

InputHelper

a guest
Apr 9th, 2010
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.85 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Input;
  3.  
  4. namespace YourNamespaceHere
  5. {
  6.     /// <summary>
  7.     /// an enum of all available mouse buttons.
  8.     /// </summary>
  9.     public enum MouseButtons
  10.     {
  11.         LeftButton,
  12.         MiddleButton,
  13.         RightButton,
  14.         ExtraButton1,
  15.         ExtraButton2
  16.     }
  17.  
  18.     public class InputHelper
  19.     {
  20.         private GamePadState _lastGamepadState;
  21.         private GamePadState _currentGamepadState;
  22. #if (!XBOX)
  23.         private KeyboardState _lastKeyboardState;
  24.         private KeyboardState _currentKeyboardState;
  25.         private MouseState _lastMouseState;
  26.         private MouseState _currentMouseState;
  27. #endif
  28.         private PlayerIndex _index = PlayerIndex.One;
  29.         private bool refreshData = false;
  30.  
  31.         /// <summary>
  32.         /// Fetches the latest input states.
  33.         /// </summary>
  34.         public void Update()
  35.         {
  36.             if (!refreshData)
  37.                 refreshData = true;
  38.             if (_lastGamepadState == null && _currentGamepadState == null)
  39.             {
  40.                 _lastGamepadState = _currentGamepadState = GamePad.GetState(_index);
  41.             }
  42.             else
  43.             {
  44.                 _lastGamepadState = _currentGamepadState;
  45.                 _currentGamepadState = GamePad.GetState(_index);
  46.             }
  47. #if (!XBOX)
  48.             if (_lastKeyboardState == null && _currentKeyboardState == null)
  49.             {
  50.                 _lastKeyboardState = _currentKeyboardState = Keyboard.GetState();
  51.             }
  52.             else
  53.             {
  54.                 _lastKeyboardState = _currentKeyboardState;
  55.                 _currentKeyboardState = Keyboard.GetState();
  56.             }
  57.             if (_lastMouseState == null && _currentMouseState == null)
  58.             {
  59.                 _lastMouseState = _currentMouseState = Mouse.GetState();
  60.             }
  61.             else
  62.             {
  63.                 _lastMouseState = _currentMouseState;
  64.                 _currentMouseState = Mouse.GetState();
  65.             }
  66. #endif
  67.         }
  68.  
  69.         /// <summary>
  70.         /// The previous state of the gamepad.
  71.         /// Exposed only for convenience.
  72.         /// </summary>
  73.         public GamePadState LastGamepadState
  74.         {
  75.             get { return _lastGamepadState; }
  76.         }
  77.         /// <summary>
  78.         /// the current state of the gamepad.
  79.         /// Exposed only for convenience.
  80.         /// </summary>
  81.         public GamePadState CurrentGamepadState
  82.         {
  83.             get { return _currentGamepadState; }
  84.         }
  85.         /// <summary>
  86.         /// the index that is used to poll the gamepad.
  87.         /// </summary>
  88.         public PlayerIndex Index
  89.         {
  90.             get { return _index; }
  91.             set {
  92.                 _index = value;
  93.                 if (refreshData)
  94.                 {
  95.                     Update();
  96.                     Update();
  97.                 }
  98.             }
  99.         }
  100. #if (!XBOX)
  101.         /// <summary>
  102.         /// The previous keyboard state.
  103.         /// Exposed only for convenience.
  104.         /// </summary>
  105.         public KeyboardState LastKeyboardState
  106.         {
  107.             get { return _lastKeyboardState; }
  108.         }
  109.         /// <summary>
  110.         /// The current state of the keyboard.
  111.         /// Exposed only for convenience.
  112.         /// </summary>
  113.         public KeyboardState CurrentKeyboardState
  114.         {
  115.             get { return _currentKeyboardState; }
  116.         }
  117.         /// <summary>
  118.         /// The previous mouse state.
  119.         /// Exposed only for convenience.
  120.         /// </summary>
  121.         public MouseState LastMouseState
  122.         {
  123.             get { return _lastMouseState; }
  124.         }
  125.         /// <summary>
  126.         /// The current state of the mouse.
  127.         /// Exposed only for convenience.
  128.         /// </summary>
  129.         public MouseState CurrentMouseState
  130.         {
  131.             get { return _currentMouseState; }
  132.         }
  133. #endif
  134.         /// <summary>
  135.         /// The current position of the left stick.
  136.         /// Y is automatically reversed for you.
  137.         /// </summary>
  138.         public Vector2 LeftStickPosition
  139.         {
  140.             get
  141.             {
  142.                 return new Vector2(
  143.                     _currentGamepadState.ThumbSticks.Left.X,
  144.                     -CurrentGamepadState.ThumbSticks.Left.Y);
  145.             }
  146.         }
  147.         /// <summary>
  148.         /// The current position of the right stick.
  149.         /// Y is automatically reversed for you.
  150.         /// </summary>
  151.         public Vector2 RightStickPosition
  152.         {
  153.             get
  154.             {
  155.                 return new Vector2(
  156.                     _currentGamepadState.ThumbSticks.Right.X,
  157.                     -_currentGamepadState.ThumbSticks.Right.Y);
  158.             }
  159.         }
  160.         /// <summary>
  161.         /// The current velocity of the left stick.
  162.         /// Y is automatically reversed for you.
  163.         /// expressed as:
  164.         /// current stick position - last stick position.
  165.         /// </summary>
  166.         public Vector2 LeftStickVelocity
  167.         {
  168.             get
  169.             {
  170.                 Vector2 temp =
  171.                     _currentGamepadState.ThumbSticks.Left -
  172.                     _lastGamepadState.ThumbSticks.Left;
  173.                 return new Vector2(temp.X, -temp.Y);
  174.             }
  175.         }
  176.         /// <summary>
  177.         /// The current velocity of the right stick.
  178.         /// Y is automatically reversed for you.
  179.         /// expressed as:
  180.         /// current stick position - last stick position.
  181.         /// </summary>
  182.         public Vector2 RightStickVelocity
  183.         {
  184.             get
  185.             {
  186.                 Vector2 temp =
  187.                     _currentGamepadState.ThumbSticks.Right -
  188.                     _lastGamepadState.ThumbSticks.Right;
  189.                 return new Vector2(temp.X, -temp.Y);
  190.             }
  191.         }
  192.         /// <summary>
  193.         /// the current position of the left trigger.
  194.         /// </summary>
  195.         public float LeftTriggerPosition
  196.         {
  197.             get { return _currentGamepadState.Triggers.Left; }
  198.         }
  199.         /// <summary>
  200.         /// the current position of the right trigger.
  201.         /// </summary>
  202.         public float RightTriggerPosition
  203.         {
  204.             get { return _currentGamepadState.Triggers.Right; }
  205.         }
  206.         /// <summary>
  207.         /// the velocity of the left trigger.
  208.         /// expressed as:
  209.         /// current trigger position - last trigger position.
  210.         /// </summary>
  211.         public float LeftTriggerVelocity
  212.         {
  213.             get
  214.             {
  215.                 return
  216.                     _currentGamepadState.Triggers.Left -
  217.                     _lastGamepadState.Triggers.Left;
  218.             }
  219.         }
  220.         /// <summary>
  221.         /// the velocity of the right trigger.
  222.         /// expressed as:
  223.         /// current trigger position - last trigger position.
  224.         /// </summary>
  225.         public float RightTriggerVelocity
  226.         {
  227.             get
  228.             {
  229.                 return _currentGamepadState.Triggers.Right -
  230.                     _lastGamepadState.Triggers.Right;
  231.             }
  232.         }
  233. #if (!XBOX)
  234.         /// <summary>
  235.         /// the current mouse position.
  236.         /// </summary>
  237.         public Vector2 MousePosition
  238.         {
  239.             get { return new Vector2(_currentMouseState.X, _currentMouseState.Y); }
  240.         }
  241.         /// <summary>
  242.         /// the current mouse velocity.
  243.         /// Expressed as:
  244.         /// current mouse position - last mouse position.
  245.         /// </summary>
  246.         public Vector2 MouseVelocity
  247.         {
  248.             get
  249.             {
  250.                 return (
  251.                     new Vector2(_currentMouseState.X, _currentMouseState.Y) -
  252.                     new Vector2(_lastMouseState.X, _lastMouseState.Y)
  253.                     );
  254.             }
  255.         }
  256.         /// <summary>
  257.         /// the current mouse scroll wheel position.
  258.         /// See the Mouse's ScrollWheel property for details.
  259.         /// </summary>
  260.         public float MouseScrollWheelPosition
  261.         {
  262.             get
  263.             {
  264.                 return _currentMouseState.ScrollWheelValue;
  265.             }
  266.         }
  267.         /// <summary>
  268.         /// the mouse scroll wheel velocity.
  269.         /// Expressed as:
  270.         /// current scroll wheel position -
  271.         /// the last scroll wheel position.
  272.         /// </summary>
  273.         public float MouseScrollWheelVelocity
  274.         {
  275.             get
  276.             {
  277.                 return (_currentMouseState.ScrollWheelValue - _lastMouseState.ScrollWheelValue);
  278.             }
  279.         }
  280. #endif
  281.         /// <summary>
  282.         /// Used for debug purposes.
  283.         /// Indicates if the user wants to exit immediately.
  284.         /// </summary>
  285.         public bool ExitRequested
  286.         {
  287. #if (!XBOX)
  288.             get
  289.             {
  290.                 return (
  291.                     (IsCurPress(Buttons.Start) &&
  292.                     IsCurPress(Buttons.Back)) ||
  293.                     IsCurPress(Keys.Escape));
  294.             }
  295. #else
  296.             get { return (IsCurPress(Buttons.Start) && IsCurPress(Buttons.Back)); }
  297. #endif
  298.         }
  299.         /// <summary>
  300.         /// Checks if the requested button is a new press.
  301.         /// </summary>
  302.         /// <param name="button">
  303.         /// The button to check.
  304.         /// </param>
  305.         /// <returns>
  306.         /// a bool indicating whether the selected button is being
  307.         /// pressed in the current state but not the last state.
  308.         /// </returns>
  309.         public bool IsNewPress(Buttons button)
  310.         {
  311.             return (
  312.                 _lastGamepadState.IsButtonUp(button) &&
  313.                 _currentGamepadState.IsButtonDown(button));
  314.         }
  315.         /// <summary>
  316.         /// Checks if the requested button is a current press.
  317.         /// </summary>
  318.         /// <param name="button">
  319.         /// the button to check.
  320.         /// </param>
  321.         /// <returns>
  322.         /// a bool indicating whether the selected button is being
  323.         /// pressed in the current state and in the last state.
  324.         /// </returns>
  325.         public bool IsCurPress(Buttons button)
  326.         {
  327.             return (
  328.                 _lastGamepadState.IsButtonDown(button) &&
  329.                 _currentGamepadState.IsButtonDown(button));
  330.         }
  331.         /// <summary>
  332.         /// Checks if the requested button is an old press.
  333.         /// </summary>
  334.         /// <param name="button">
  335.         /// the button to check.
  336.         /// </param>
  337.         /// <returns>
  338.         /// a bool indicating whether the selected button is not being
  339.         /// pressed in the current state and is being pressed in the last state.
  340.         /// </returns>
  341.         public bool IsOldPress(Buttons button)
  342.         {
  343.             return (
  344.                 _lastGamepadState.IsButtonDown(button) &&
  345.                 _currentGamepadState.IsButtonUp(button));
  346.         }
  347. #if (!XBOX)
  348.         /// <summary>
  349.         /// Checks if the requested key is a new press.
  350.         /// </summary>
  351.         /// <param name="key">
  352.         /// the key to check.
  353.         /// </param>
  354.         /// <returns>
  355.         /// a bool that indicates whether the selected key is being
  356.         /// pressed in the current state and not in the last state.
  357.         /// </returns>
  358.         public bool IsNewPress(Keys key)
  359.         {
  360.             return (
  361.                 _lastKeyboardState.IsKeyUp(key) &&
  362.                 _currentKeyboardState.IsKeyDown(key));
  363.         }
  364.         /// <summary>
  365.         /// Checks if the requested key is a current press.
  366.         /// </summary>
  367.         /// <param name="key">
  368.         /// the key to check.
  369.         /// </param>
  370.         /// <returns>
  371.         /// a bool that indicates whether the selected key is being
  372.         /// pressed in the current state and in the last state.
  373.         /// </returns>
  374.         public bool IsCurPress(Keys key)
  375.         {
  376.             return (
  377.                 _lastKeyboardState.IsKeyDown(key) &&
  378.                 _currentKeyboardState.IsKeyDown(key));
  379.         }
  380.         /// <summary>
  381.         /// Checks if the requested button is an old press.
  382.         /// </summary>
  383.         /// <param name="key">
  384.         /// the key to check.
  385.         /// </param>
  386.         /// <returns>
  387.         /// a bool indicating whether the selectde button is not being
  388.         /// pressed in the current state and being pressed in the last state.
  389.         /// </returns>
  390.         public bool IsOldPress(Keys key)
  391.         {
  392.             return (
  393.                 _lastKeyboardState.IsKeyDown(key) &&
  394.                 _currentKeyboardState.IsKeyUp(key));
  395.         }
  396.         /// <summary>
  397.         /// Checks if the requested mosue button is a new press.
  398.         /// </summary>
  399.         /// <param name="button">
  400.         /// teh mouse button to check.
  401.         /// </param>
  402.         /// <returns>
  403.         /// a bool indicating whether the selected mouse button is being
  404.         /// pressed in the current state but not in the last state.
  405.         /// </returns>
  406.         public bool IsNewPress(MouseButtons button)
  407.         {
  408.             switch (button)
  409.             {
  410.                 case MouseButtons.LeftButton:
  411.                     return (
  412.                         _lastMouseState.LeftButton == ButtonState.Released &&
  413.                         _currentMouseState.LeftButton == ButtonState.Pressed);
  414.                 case MouseButtons.MiddleButton:
  415.                     return (
  416.                         _lastMouseState.MiddleButton == ButtonState.Released &&
  417.                         _currentMouseState.MiddleButton == ButtonState.Pressed);
  418.                 case MouseButtons.RightButton:
  419.                     return (
  420.                         _lastMouseState.RightButton == ButtonState.Released &&
  421.                         _currentMouseState.RightButton == ButtonState.Pressed);
  422.                 case MouseButtons.ExtraButton1:
  423.                     return (
  424.                         _lastMouseState.XButton1 == ButtonState.Released &&
  425.                         _currentMouseState.XButton1 == ButtonState.Pressed);
  426.                 case MouseButtons.ExtraButton2:
  427.                     return (
  428.                         _lastMouseState.XButton2 == ButtonState.Released &&
  429.                         _currentMouseState.XButton2 == ButtonState.Pressed);
  430.                 default:
  431.                     return false;
  432.             }
  433.         }
  434.         /// <summary>
  435.         /// Checks if the requested mosue button is a current press.
  436.         /// </summary>
  437.         /// <param name="button">
  438.         /// the mouse button to be checked.
  439.         /// </param>
  440.         /// <returns>
  441.         /// a bool indicating whether the selected mouse button is being
  442.         /// pressed in the current state and in the last state.
  443.         /// </returns>
  444.         public bool IsCurPress(MouseButtons button)
  445.         {
  446.             switch (button)
  447.             {
  448.                 case MouseButtons.LeftButton:
  449.                     return (
  450.                         _lastMouseState.LeftButton == ButtonState.Pressed &&
  451.                         _currentMouseState.LeftButton == ButtonState.Pressed);
  452.                 case MouseButtons.MiddleButton:
  453.                     return (
  454.                         _lastMouseState.MiddleButton == ButtonState.Pressed &&
  455.                         _currentMouseState.MiddleButton == ButtonState.Pressed);
  456.                 case MouseButtons.RightButton:
  457.                     return (
  458.                         _lastMouseState.RightButton == ButtonState.Pressed &&
  459.                         _currentMouseState.RightButton == ButtonState.Pressed);
  460.                 case MouseButtons.ExtraButton1:
  461.                     return (
  462.                         _lastMouseState.XButton1 == ButtonState.Pressed &&
  463.                         _currentMouseState.XButton1 == ButtonState.Pressed);
  464.                 case MouseButtons.ExtraButton2:
  465.                     return (
  466.                         _lastMouseState.XButton2 == ButtonState.Pressed &&
  467.                         _currentMouseState.XButton2 == ButtonState.Pressed);
  468.                 default:
  469.                     return false;
  470.             }
  471.         }
  472.         /// <summary>
  473.         /// Checks if the requested mosue button is an old press.
  474.         /// </summary>
  475.         /// <param name="button">
  476.         /// the mouse button to check.
  477.         /// </param>
  478.         /// <returns>
  479.         /// a bool indicating whether the selected mouse button is not being
  480.         /// pressed in the current state and is being pressed in the old state.
  481.         /// </returns>
  482.         public bool IsOldPress(MouseButtons button)
  483.         {
  484.             switch (button)
  485.             {
  486.                 case MouseButtons.LeftButton:
  487.                     return (
  488.                         _lastMouseState.LeftButton == ButtonState.Pressed &&
  489.                         _currentMouseState.LeftButton == ButtonState.Released);
  490.                 case MouseButtons.MiddleButton:
  491.                     return (
  492.                         _lastMouseState.MiddleButton == ButtonState.Pressed &&
  493.                         _currentMouseState.MiddleButton == ButtonState.Released);
  494.                 case MouseButtons.RightButton:
  495.                     return (
  496.                         _lastMouseState.RightButton == ButtonState.Pressed &&
  497.                         _currentMouseState.RightButton == ButtonState.Released);
  498.                 case MouseButtons.ExtraButton1:
  499.                     return (
  500.                         _lastMouseState.XButton1 == ButtonState.Pressed &&
  501.                         _currentMouseState.XButton1 == ButtonState.Released);
  502.                 case MouseButtons.ExtraButton2:
  503.                     return (
  504.                         _lastMouseState.XButton2 == ButtonState.Pressed &&
  505.                         _currentMouseState.XButton2 == ButtonState.Released);
  506.                 default:
  507.                     return false;
  508.             }
  509.         }
  510. #endif
  511.     }
  512. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement