Prechádzať zdrojové kódy

Added MouseState and Methods to Input

Michael Cummings 12 rokov pred
rodič
commit
6136e6bbe8
1 zmenil súbory, kde vykonal 97 pridanie a 1 odobranie
  1. 97 1
      HoneycombRush/ScreenManager/InputState.cs

+ 97 - 1
HoneycombRush/ScreenManager/InputState.cs

@@ -24,14 +24,22 @@ namespace HoneycombRush
     /// </summary>
     public class InputState
     {
+		public enum MouseButton
+		{
+			Left,
+			Middle,
+			Right
+		}
         #region Fields
 
         public const int MaxInputs = 4;
 
         public readonly KeyboardState[] CurrentKeyboardStates;
+		public readonly MouseState[] CurrentMouseStates;
         public readonly GamePadState[] CurrentGamePadStates;
 
         public readonly KeyboardState[] LastKeyboardStates;
+		public readonly MouseState[] LastMouseStates;
         public readonly GamePadState[] LastGamePadStates;
 
         public readonly bool[] GamePadWasConnected;
@@ -51,9 +59,11 @@ namespace HoneycombRush
         public InputState()
         {
             CurrentKeyboardStates = new KeyboardState[MaxInputs];
+			CurrentMouseStates = new MouseState[MaxInputs];
             CurrentGamePadStates = new GamePadState[MaxInputs];
 
             LastKeyboardStates = new KeyboardState[MaxInputs];
+			LastMouseStates = new MouseState[MaxInputs];
             LastGamePadStates = new GamePadState[MaxInputs];
 
             GamePadWasConnected = new bool[MaxInputs];
@@ -73,9 +83,11 @@ namespace HoneycombRush
             for (int i = 0; i < MaxInputs; i++)
             {
                 LastKeyboardStates[i] = CurrentKeyboardStates[i];
+				LastMouseStates[i] = CurrentMouseStates[i];
                 LastGamePadStates[i] = CurrentGamePadStates[i];
 
                 CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
+				CurrentMouseStates[i] = Mouse.GetState();
                 CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
 
                 // Keep track of whether a gamepad has ever been
@@ -153,7 +165,90 @@ namespace HoneycombRush
             }
         }
 
-        /// <summary>
+		/// <summary>
+		/// 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.
+		/// </summary>
+		public bool IsNewMouseClick(MouseButton mouseButton , PlayerIndex? controllingPlayer,
+		                          out PlayerIndex playerIndex)
+		{
+			if (controllingPlayer.HasValue)
+			{
+				// Read input from the specified player.
+				playerIndex = controllingPlayer.Value;
+
+				int i = (int)playerIndex;
+				switch( mouseButton){
+					case MouseButton.Left:
+						return (CurrentMouseStates[i].LeftButton == ButtonState.Pressed && 
+					            LastMouseStates[i].LeftButton == ButtonState.Released);
+					case MouseButton.Right:
+						return (CurrentMouseStates[i].RightButton == ButtonState.Pressed && 
+						        LastMouseStates[i].RightButton == ButtonState.Released);
+					case MouseButton.Middle:
+						return (CurrentMouseStates[i].MiddleButton == ButtonState.Pressed && 
+						        LastMouseStates[i].MiddleButton == ButtonState.Released);
+					default:
+						return IsNewMouseClick(MouseButton.Left, controllingPlayer, out playerIndex) ||
+							   IsNewMouseClick(MouseButton.Middle, controllingPlayer, out playerIndex) ||
+							   IsNewMouseClick(MouseButton.Right, controllingPlayer, out playerIndex);
+				}
+
+			}
+			else
+			{
+				// Accept input from any player.
+				return (IsNewMouseClick(mouseButton, PlayerIndex.One, out playerIndex) ||
+				        IsNewMouseClick(mouseButton, PlayerIndex.Two, out playerIndex) ||
+				        IsNewMouseClick(mouseButton, PlayerIndex.Three, out playerIndex) ||
+				        IsNewMouseClick(mouseButton, PlayerIndex.Four, out playerIndex));
+			}
+
+		}
+
+		/// <summary>
+		/// 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.
+		/// </summary>
+		public bool IsMouseDown(MouseButton mouseButton , PlayerIndex? controllingPlayer,
+		                            out PlayerIndex playerIndex)
+		{
+			if (controllingPlayer.HasValue)
+			{
+				// Read input from the specified player.
+				playerIndex = controllingPlayer.Value;
+
+				int i = (int)playerIndex;
+				switch( mouseButton  ) {
+					case MouseButton.Left:
+						return CurrentMouseStates[i].LeftButton == ButtonState.Pressed;
+					case MouseButton.Right:
+						return CurrentMouseStates[i].RightButton == ButtonState.Pressed;
+					case MouseButton.Middle:
+						return CurrentMouseStates[i].MiddleButton == ButtonState.Pressed;
+					default:
+						return IsMouseDown(MouseButton.Left, controllingPlayer, out playerIndex) ||
+							   IsMouseDown(MouseButton.Middle, controllingPlayer, out playerIndex) ||
+							   IsMouseDown(MouseButton.Right, controllingPlayer, out playerIndex);
+				}
+
+			}
+			else
+			{
+				// Accept input from any player.
+				return (IsMouseDown(mouseButton, PlayerIndex.One, out playerIndex) ||
+				        IsMouseDown(mouseButton, PlayerIndex.Two, out playerIndex) ||
+				        IsMouseDown(mouseButton, PlayerIndex.Three, out playerIndex) ||
+				        IsMouseDown(mouseButton, PlayerIndex.Four, out playerIndex));
+			}
+
+		}
+
+		/// <summary>
         /// 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
@@ -194,6 +289,7 @@ namespace HoneycombRush
         {
             return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
                    IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) ||
+			   	   IsNewMouseClick(MouseButton.Left, controllingPlayer, out playerIndex) ||
                    IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) ||
                    IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
         }