#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
namespace DoraMataEngine.Input
{
///
/// Input manager class.
///
public class InputManagerDM
{
#region Members
///
/// Keeps track of the current game pads states.
///
private GamePadState[] currentGamePadsState;
///
/// Keeps track of the last game pads states.
///
private GamePadState[] lastGamePadsState;
///
/// Keeps track of the current keyboard state.
///
private KeyboardState currentKeyboardState;
///
/// Keeps track of the last keyboard state.
///
private KeyboardState lastKeyboardState;
///
/// Keeps track of the current mouse state.
///
private MouseState currentMouseState;
///
/// Keeps track of the last mouse state.
///
private MouseState lastMouseState;
#endregion
#region Properties
///
/// Current keyboard state.
///
public KeyboardState CurrentKeyboardState
{
get
{
return currentKeyboardState;
}
}
///
/// Last keyboard state.
///
public KeyboardState LastKeyboardState
{
get
{
return lastKeyboardState;
}
}
///
/// Current mouse state.
///
public MouseState CurrentMouseState
{
get
{
return currentMouseState;
}
}
///
/// Last mouse state.
///
public MouseState LastMouseState
{
get
{
return lastMouseState;
}
}
///
/// Current game pads state.
///
public GamePadState[] CurrentGamePadsState
{
get
{
return currentGamePadsState;
}
}
///
/// Last game pads state.
///
public GamePadState[] LastGamePadsState
{
get
{
return lastGamePadsState;
}
}
///
/// Checks for a "menu up" input action.
///
public bool MenuUpInput
{
get
{
for (int i = 0; i < 4; ++i)
{
if (((currentGamePadsState[i].DPad.Up == ButtonState.Pressed) &&
(lastGamePadsState[i].DPad.Up == ButtonState.Released)) ||
((currentGamePadsState[i].ThumbSticks.Left.Y > 0) &&
(lastGamePadsState[i].ThumbSticks.Left.Y <= 0)))
{
return true;
}
}
if (HasKeyJustBeenPressed(Keys.Up))
{
return true;
}
return false;
}
}
///
/// Checks for a "menu down" input action.
///
public bool MenuDownInput
{
get
{
for (int i = 0; i < 4; ++i)
{
if (((currentGamePadsState[i].DPad.Down == ButtonState.Pressed) &&
(lastGamePadsState[i].DPad.Down == ButtonState.Released)) ||
((currentGamePadsState[i].ThumbSticks.Left.Y < 0) &&
(lastGamePadsState[i].ThumbSticks.Left.Y >= 0)))
{
return true;
}
}
if (HasKeyJustBeenPressed(Keys.Down))
{
return true;
}
return false;
}
}
///
/// Checks for a "menu select" input action.
///
public bool MenuSelectInput
{
get
{
for (int i = 0; i < 4; ++i)
{
if (((currentGamePadsState[i].Buttons.A == ButtonState.Pressed) &&
(lastGamePadsState[i].Buttons.A == ButtonState.Released)) ||
((currentGamePadsState[i].Buttons.Start == ButtonState.Pressed) &&
(lastGamePadsState[i].Buttons.Start == ButtonState.Released)))
{
return true;
}
}
if (HasKeyJustBeenPressed(Keys.Enter))
{
return true;
}
if (HasKeyJustBeenPressed(Keys.Space))
{
return true;
}
return false;
}
}
///
/// Checks for a "menu cancel" input action.
///
public bool MenuCancelInput
{
get
{
for (int i = 0; i < 4; ++i)
{
if (((currentGamePadsState[i].Buttons.B == ButtonState.Pressed) &&
(lastGamePadsState[i].Buttons.B == ButtonState.Released)) ||
((currentGamePadsState[i].Buttons.Back == ButtonState.Pressed) &&
(lastGamePadsState[i].Buttons.Back == ButtonState.Released)))
{
return true;
}
}
if (HasKeyJustBeenPressed(Keys.Escape))
{
return true;
}
return false;
}
}
///
/// Checks for a "pause the game" input action.
///
public bool PauseGameInput
{
get
{
for (int i = 0; i < 4; ++i)
{
if (((currentGamePadsState[i].Buttons.Start == ButtonState.Pressed) &&
(lastGamePadsState[i].Buttons.Start == ButtonState.Released)) ||
((currentGamePadsState[i].Buttons.Back == ButtonState.Pressed) &&
(lastGamePadsState[i].Buttons.Back == ButtonState.Released)))
{
return true;
}
}
if (HasKeyJustBeenPressed(Keys.Escape))
{
return true;
}
return false;
}
}
///
/// The default X axis value.
///
public float XAxisInput
{
get
{
float result = 0.0f;
return result;
}
}
#endregion
#region Initialization / Cleanup
///
/// Default constructor.
///
public InputManagerDM()
{
currentGamePadsState = new GamePadState[4];
lastGamePadsState = new GamePadState[4];
for (int i = 0; i < 4; ++i)
{
currentGamePadsState[i] = GamePad.GetState((PlayerIndex)i);
lastGamePadsState[i] = currentGamePadsState[i];
}
currentKeyboardState = Keyboard.GetState();
lastKeyboardState = currentKeyboardState;
currentMouseState = Mouse.GetState();
lastMouseState = currentMouseState;
}
#endregion
#region Update
///
/// Update the input devices.
///
public void Update()
{
for (int i = 0; i < 4; ++i)
{
lastGamePadsState[i] = currentGamePadsState[i];
currentGamePadsState[i] = GamePad.GetState((PlayerIndex)i);
}
lastKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
lastMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
}
#endregion
#region Methods
///
/// Helper for checking if a key has just been pressed.
///
///
///
public bool HasKeyJustBeenPressed(Keys key)
{
return (currentKeyboardState.IsKeyDown(key) && lastKeyboardState.IsKeyUp(key));
}
///
/// Helper that uses a pair of keys to simulate a positive or negative axis input.
///
///
///
///
///
public static float ReadKeyboardAxis(KeyboardState keyboardState, Keys downKey, Keys upKey)
{
float result = 0.0f;
if (keyboardState.IsKeyDown(downKey))
{
result -= 1.0f;
}
if (keyboardState.IsKeyDown(upKey))
{
result += 1.0f;
}
return result;
}
///
/// Power curve function. This curve makes small input values even smaller while preserving
/// the full range when the stick is moved in an extreme position.
///
///
///
///
public static float PowerCurve(float value, float power)
{
return (float)Math.Pow(Math.Abs(value), power) * Math.Sign(value);
}
#endregion
}
}