|
@@ -21,7 +21,6 @@ namespace CatapultGame
|
|
|
/// </summary>
|
|
|
public class InputState
|
|
|
{
|
|
|
-
|
|
|
public const int MaxInputs = 4;
|
|
|
public readonly KeyboardState[] CurrentKeyboardStates;
|
|
|
public readonly GamePadState[] CurrentGamePadStates;
|
|
@@ -31,16 +30,20 @@ namespace CatapultGame
|
|
|
public TouchCollection TouchState;
|
|
|
public MouseState CurrentMouseState;
|
|
|
public MouseState LastMouseState;
|
|
|
- public readonly List<GestureSample> Gestures = new List<GestureSample> ();
|
|
|
-
|
|
|
-
|
|
|
+ public readonly List<GestureSample> Gestures = new List<GestureSample>();
|
|
|
|
|
|
+ Matrix inputTransformation;
|
|
|
+ readonly float baseBufferWidth;
|
|
|
+ readonly float baseBufferHeight;
|
|
|
|
|
|
/// <summary>
|
|
|
/// Constructs a new input state.
|
|
|
/// </summary>
|
|
|
- public InputState ()
|
|
|
+ public InputState(float baseBufferWidth, float baseBufferHeight)
|
|
|
{
|
|
|
+ this.baseBufferWidth = baseBufferWidth;
|
|
|
+ this.baseBufferHeight = baseBufferHeight;
|
|
|
+
|
|
|
CurrentKeyboardStates = new KeyboardState[MaxInputs];
|
|
|
CurrentGamePadStates = new GamePadState[MaxInputs];
|
|
|
|
|
@@ -50,39 +53,38 @@ namespace CatapultGame
|
|
|
GamePadWasConnected = new bool[MaxInputs];
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Reads the latest state of the keyboard and gamepad.
|
|
|
/// </summary>
|
|
|
- public void Update ()
|
|
|
+ public void Update()
|
|
|
{
|
|
|
- for (int i = 0; i < MaxInputs; i++) {
|
|
|
- LastKeyboardStates [i] = CurrentKeyboardStates [i];
|
|
|
- LastGamePadStates [i] = CurrentGamePadStates [i];
|
|
|
+ for (int i = 0; i < MaxInputs; i++)
|
|
|
+ {
|
|
|
+ LastKeyboardStates[i] = CurrentKeyboardStates[i];
|
|
|
+ LastGamePadStates[i] = CurrentGamePadStates[i];
|
|
|
|
|
|
- CurrentKeyboardStates [i] = Keyboard.GetState ();
|
|
|
- CurrentGamePadStates [i] = GamePad.GetState ((PlayerIndex)i);
|
|
|
+ CurrentKeyboardStates[i] = Keyboard.GetState();
|
|
|
+ CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
|
|
|
|
|
|
// Keep track of whether a gamepad has ever been
|
|
|
// connected, so we can detect if it is unplugged.
|
|
|
- if (CurrentGamePadStates [i].IsConnected) {
|
|
|
- GamePadWasConnected [i] = true;
|
|
|
+ if (CurrentGamePadStates[i].IsConnected)
|
|
|
+ {
|
|
|
+ GamePadWasConnected[i] = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- TouchState = TouchPanel.GetState ();
|
|
|
+ TouchState = TouchPanel.GetState();
|
|
|
|
|
|
LastMouseState = CurrentMouseState;
|
|
|
- CurrentMouseState = Mouse.GetState ();
|
|
|
+ CurrentMouseState = Mouse.GetState();
|
|
|
|
|
|
UpdateMouseStates();
|
|
|
|
|
|
- Gestures.Clear ();
|
|
|
- while (TouchPanel.IsGestureAvailable) {
|
|
|
- Gestures.Add (TouchPanel.ReadGesture ());
|
|
|
+ Gestures.Clear();
|
|
|
+ while (TouchPanel.IsGestureAvailable)
|
|
|
+ {
|
|
|
+ Gestures.Add(TouchPanel.ReadGesture());
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -98,54 +100,61 @@ namespace CatapultGame
|
|
|
|
|
|
public MouseGestureType MouseGesture
|
|
|
{
|
|
|
- get {
|
|
|
+ get
|
|
|
+ {
|
|
|
return mouseGestureType;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Vector2 CurrentMousePosition
|
|
|
{
|
|
|
- get {
|
|
|
+ get
|
|
|
+ {
|
|
|
return currentMousePosition;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Vector2 PrevMousePosition
|
|
|
{
|
|
|
- get {
|
|
|
+ get
|
|
|
+ {
|
|
|
return prevMousePosition;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Vector2 MouseDelta
|
|
|
{
|
|
|
- get {
|
|
|
+ get
|
|
|
+ {
|
|
|
return prevMousePosition - currentMousePosition;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Vector2 MouseDragDelta
|
|
|
{
|
|
|
- get {
|
|
|
+ get
|
|
|
+ {
|
|
|
return dragMouseStart - dragMouseEnd;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Vector2 MouseDragStartPosition
|
|
|
{
|
|
|
- get {
|
|
|
+ get
|
|
|
+ {
|
|
|
return dragMouseStart;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Vector2 MouseDragEndPosition
|
|
|
{
|
|
|
- get {
|
|
|
+ get
|
|
|
+ {
|
|
|
return dragMouseEnd;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- void UpdateMouseStates ()
|
|
|
+ void UpdateMouseStates()
|
|
|
{
|
|
|
currentMousePosition.X = CurrentMouseState.X;
|
|
|
currentMousePosition.Y = CurrentMouseState.Y;
|
|
@@ -165,7 +174,8 @@ namespace CatapultGame
|
|
|
// If we were dragging and the left mouse button was released
|
|
|
// then we are no longer dragging and need to throw the banana.
|
|
|
if (CurrentMouseState.LeftButton == ButtonState.Released &&
|
|
|
- dragging) {
|
|
|
+ dragging)
|
|
|
+ {
|
|
|
|
|
|
leftMouseDown = false;
|
|
|
dragging = false;
|
|
@@ -179,7 +189,8 @@ namespace CatapultGame
|
|
|
|
|
|
// Let's set the left mouse down and the mouse origin
|
|
|
if (!leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Pressed &&
|
|
|
- !CurrentMouseState.Equals (LastMouseState)) {
|
|
|
+ !CurrentMouseState.Equals(LastMouseState))
|
|
|
+ {
|
|
|
//Console.WriteLine ("left down");
|
|
|
leftMouseDown = true;
|
|
|
dragComplete = false;
|
|
@@ -187,18 +198,21 @@ namespace CatapultGame
|
|
|
}
|
|
|
|
|
|
if (leftMouseDown && CurrentMouseState.LeftButton == ButtonState.Released &&
|
|
|
- !CurrentMouseState.Equals (LastMouseState)) {
|
|
|
+ !CurrentMouseState.Equals(LastMouseState))
|
|
|
+ {
|
|
|
leftMouseDown = false;
|
|
|
mouseGestureType |= MouseGestureType.LeftClick;
|
|
|
}
|
|
|
|
|
|
// Here we test the distance and if over the threshold then we set the dragging to true
|
|
|
// Current threshold is 5 pixels.
|
|
|
- if (leftMouseDown && !dragging) {
|
|
|
+ if (leftMouseDown && !dragging)
|
|
|
+ {
|
|
|
|
|
|
Vector2 delta = dragMouseStart - currentMousePosition;
|
|
|
|
|
|
- if (delta.Length() > dragThreshold) {
|
|
|
+ if (delta.Length() > dragThreshold)
|
|
|
+ {
|
|
|
dragging = true;
|
|
|
dragMouseStart = currentMousePosition;
|
|
|
mouseGestureType = mouseGestureType | MouseGestureType.FreeDrag;
|
|
@@ -215,123 +229,130 @@ namespace CatapultGame
|
|
|
/// 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 IsNewKeyPress (Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
+ public bool IsNewKeyPress(Keys key, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
{
|
|
|
- if (controllingPlayer.HasValue) {
|
|
|
+ if (controllingPlayer.HasValue)
|
|
|
+ {
|
|
|
// Read input from the specified player.
|
|
|
playerIndex = controllingPlayer.Value;
|
|
|
|
|
|
int i = (int)playerIndex;
|
|
|
|
|
|
- return (CurrentKeyboardStates [i].IsKeyDown (key) && LastKeyboardStates [i].IsKeyUp (key));
|
|
|
- } else {
|
|
|
+ return (CurrentKeyboardStates[i].IsKeyDown(key) && LastKeyboardStates[i].IsKeyUp(key));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
// Accept input from any player.
|
|
|
- return (IsNewKeyPress (key, PlayerIndex.One, out playerIndex) ||
|
|
|
- IsNewKeyPress (key, PlayerIndex.Two, out playerIndex) ||
|
|
|
- IsNewKeyPress (key, PlayerIndex.Three, out playerIndex) ||
|
|
|
- IsNewKeyPress (key, PlayerIndex.Four, out playerIndex));
|
|
|
+ return (IsNewKeyPress(key, PlayerIndex.One, out playerIndex) ||
|
|
|
+ IsNewKeyPress(key, PlayerIndex.Two, out playerIndex) ||
|
|
|
+ IsNewKeyPress(key, PlayerIndex.Three, out playerIndex) ||
|
|
|
+ IsNewKeyPress(key, 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
|
|
|
/// is detected, the output playerIndex reports which player pressed it.
|
|
|
/// </summary>
|
|
|
- public bool IsNewButtonPress (Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
+ public bool IsNewButtonPress(Buttons button, PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
{
|
|
|
- if (controllingPlayer.HasValue) {
|
|
|
+ if (controllingPlayer.HasValue)
|
|
|
+ {
|
|
|
// Read input from the specified player.
|
|
|
playerIndex = controllingPlayer.Value;
|
|
|
|
|
|
int i = (int)playerIndex;
|
|
|
|
|
|
- return (CurrentGamePadStates [i].IsButtonDown (button) && LastGamePadStates [i].IsButtonUp (button));
|
|
|
- } else {
|
|
|
+ return (CurrentGamePadStates[i].IsButtonDown(button) && LastGamePadStates[i].IsButtonUp(button));
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
// Accept input from any player.
|
|
|
- return (IsNewButtonPress (button, PlayerIndex.One, out playerIndex) ||
|
|
|
- IsNewButtonPress (button, PlayerIndex.Two, out playerIndex) ||
|
|
|
- IsNewButtonPress (button, PlayerIndex.Three, out playerIndex) ||
|
|
|
- IsNewButtonPress (button, PlayerIndex.Four, out playerIndex));
|
|
|
+ return (IsNewButtonPress(button, PlayerIndex.One, out playerIndex) ||
|
|
|
+ IsNewButtonPress(button, PlayerIndex.Two, out playerIndex) ||
|
|
|
+ IsNewButtonPress(button, PlayerIndex.Three, out playerIndex) ||
|
|
|
+ IsNewButtonPress(button, PlayerIndex.Four, out playerIndex));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Checks for a "menu select" input action.
|
|
|
/// The controllingPlayer parameter specifies which player to read input for.
|
|
|
/// If this is null, it will accept input from any player. When the action
|
|
|
/// is detected, the output playerIndex reports which player pressed it.
|
|
|
/// </summary>
|
|
|
- public bool IsMenuSelect (PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
+ public bool IsMenuSelect(PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
{
|
|
|
- return IsNewKeyPress (Keys.Space, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewKeyPress (Keys.Enter, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.A, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.Start, controllingPlayer, out playerIndex);
|
|
|
+ return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Checks for a "menu cancel" input action.
|
|
|
/// The controllingPlayer parameter specifies which player to read input for.
|
|
|
/// If this is null, it will accept input from any player. When the action
|
|
|
/// is detected, the output playerIndex reports which player pressed it.
|
|
|
/// </summary>
|
|
|
- public bool IsMenuCancel (PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
+ public bool IsMenuCancel(PlayerIndex? controllingPlayer, out PlayerIndex playerIndex)
|
|
|
{
|
|
|
- return IsNewKeyPress (Keys.Escape, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.B, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.Back, controllingPlayer, out playerIndex);
|
|
|
+ return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.B, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Checks for a "menu up" input action.
|
|
|
/// The controllingPlayer parameter specifies which player to read
|
|
|
/// input for. If this is null, it will accept input from any player.
|
|
|
/// </summary>
|
|
|
- public bool IsMenuUp (PlayerIndex? controllingPlayer)
|
|
|
+ public bool IsMenuUp(PlayerIndex? controllingPlayer)
|
|
|
{
|
|
|
PlayerIndex playerIndex;
|
|
|
|
|
|
- return IsNewKeyPress (Keys.Up, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.DPadUp, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
|
|
|
+ return IsNewKeyPress(Keys.Up, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.DPadUp, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.LeftThumbstickUp, controllingPlayer, out playerIndex);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Checks for a "menu down" input action.
|
|
|
/// The controllingPlayer parameter specifies which player to read
|
|
|
/// input for. If this is null, it will accept input from any player.
|
|
|
/// </summary>
|
|
|
- public bool IsMenuDown (PlayerIndex? controllingPlayer)
|
|
|
+ public bool IsMenuDown(PlayerIndex? controllingPlayer)
|
|
|
{
|
|
|
PlayerIndex playerIndex;
|
|
|
|
|
|
- return IsNewKeyPress (Keys.Down, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.DPadDown, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
|
|
|
+ return IsNewKeyPress(Keys.Down, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.DPadDown, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.LeftThumbstickDown, controllingPlayer, out playerIndex);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// Checks for a "pause the game" input action.
|
|
|
/// The controllingPlayer parameter specifies which player to read
|
|
|
/// input for. If this is null, it will accept input from any player.
|
|
|
/// </summary>
|
|
|
- public bool IsPauseGame (PlayerIndex? controllingPlayer)
|
|
|
+ public bool IsPauseGame(PlayerIndex? controllingPlayer)
|
|
|
{
|
|
|
PlayerIndex playerIndex;
|
|
|
|
|
|
- return IsNewKeyPress (Keys.Escape, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.Back, controllingPlayer, out playerIndex) ||
|
|
|
- IsNewButtonPress (Buttons.Start, controllingPlayer, out playerIndex);
|
|
|
+ return IsNewKeyPress(Keys.Escape, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.Back, controllingPlayer, out playerIndex) ||
|
|
|
+ IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+ /// <summary>
|
|
|
+ /// Updates the matrix used to transform input coordinates.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="inputTransformation">The transformation matrix to apply.</param>
|
|
|
+ public void UpdateInputTransformation(Matrix inputTransformation)
|
|
|
+ {
|
|
|
+ this.inputTransformation = inputTransformation;
|
|
|
+ }
|
|
|
}
|
|
|
-}
|
|
|
+}
|