Skip to content

Commit

Permalink
Merge pull request CartBlanche#41 from borrillis/feature/MouseSupport…
Browse files Browse the repository at this point in the history
…4HoneycombRush

Feature/mouse support4 honeycomb rush
  • Loading branch information
CartBlanche committed Jun 25, 2013
2 parents 83b56cf + 645ac02 commit 2a25285
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 9 deletions.
98 changes: 97 additions & 1 deletion HoneycombRush/ScreenManager/InputState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,9 +59,11 @@ public class InputState
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];
Expand All @@ -73,9 +83,11 @@ public void Update()
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
Expand Down Expand Up @@ -153,7 +165,90 @@ public bool IsKeyDown(Keys key, PlayerIndex? controllingPlayer,
}
}

/// <summary>
/// <summary>
/// Helper for checking if a mouse 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 click
/// 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 mouse button is currently pressed during the update. The
/// controllingPlayer parameter specifies which player to read input for.
/// If this is null, it will accept input from any player. When a pressed button
/// 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
Expand Down Expand Up @@ -194,6 +289,7 @@ public bool IsMenuSelect(PlayerIndex? controllingPlayer,
{
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);
}
Expand Down
3 changes: 2 additions & 1 deletion HoneycombRush/ScreenManager/MenuScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public override void HandleInput(GameTime gameTime, InputState input)
selectedEntry = 0;
}
else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player) )
{
OnSelectEntry(selectedEntry, player);
}
Expand Down
15 changes: 11 additions & 4 deletions HoneycombRush/Screens/GameplayScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,10 @@ public override void HandleInput(GameTime gameTime, InputState input)
{
showDebugInfo = !showDebugInfo;
}
if (input.IsKeyDown(Keys.Space, ControllingPlayer, out player) && !beeKeeper.IsCollectingHoney &&
!beeKeeper.IsStung)
if ( (input.IsKeyDown(Keys.Space, ControllingPlayer, out player) ||
input.IsNewMouseClick(InputState.MouseButton.Right, ControllingPlayer, out player))
&& !beeKeeper.IsCollectingHoney
&& !beeKeeper.IsStung)
{
isSmokebuttonClicked = true;
}
Expand Down Expand Up @@ -836,8 +838,13 @@ private Vector2 SetMotion(InputState input)
{
vecY++;
}

leftThumbstick = new Vector2(vecX, vecY);
if (input.IsMouseDown( InputState.MouseButton.Left, ControllingPlayer, out playerIndex ) )
{
vecX = input.CurrentMouseStates[(int)playerIndex].X - beeKeeper.Bounds.X ;
vecY = input.CurrentMouseStates[(int)playerIndex].Y - beeKeeper.Bounds.Y;
}
leftThumbstick = new Vector2 (vecX, vecY);
leftThumbstick.Normalize();
}

Vector2 movementVector = leftThumbstick * 12f * ScreenManager.SpriteBatch.ScaleVector;
Expand Down
3 changes: 2 additions & 1 deletion HoneycombRush/Screens/HighScoreScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public override void HandleInput(GameTime gameTime, InputState input)
PlayerIndex player;
// Handle keyboard input
if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
{
Exit();
}
Expand Down
3 changes: 2 additions & 1 deletion HoneycombRush/Screens/LevelOverScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ public override void HandleInput(GameTime gameTime, InputState input)
}
// Handle keyboard
else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
{
StartNewLevelOrExit(input);
}
Expand Down
3 changes: 2 additions & 1 deletion HoneycombRush/Screens/LoadingAndInstructionScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public override void HandleInput(GameTime gameTime, InputState input)
ScreenManager.AddScreen(new MainMenuScreen(), PlayerIndex.One);
}
else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
{
LoadResources();
}
Expand Down

0 comments on commit 2a25285

Please sign in to comment.