//---------------------------------------------------------------------------------------------------------------------------------------------------
//
// Copyright (C)2007 DarkWynter Studios. All rights reserved.
//
//---------------------------------------------------------------------------------------------------------------------------------------------------
// {Contact : darkwynter.com for licensing information
//---------------------------------------------------------------------------------------------------------------------------------------------------
namespace DarkWynterEngine.Controllers
{
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Diagnostics;
///
/// InputEvent class can be used for Key input or controller input
/// Every instance of InputEvent is tied to a particular key or button
/// The Held() or NewPress() commands are used to check that button
/// The current KeyboardState or GamePadState (for the appropriate player)
/// must be passed into the functions
///
public class InputEvent
{
//Button we are checking for
Buttons button;
//Keyboard key we are checking for?
Keys key;
//timer to make sure the key isn't taken more than once at a time
Stopwatch keyboardTimer;
const int KEY_TIMER_MAX_MILLI = 100;
//If the button was just pressed
bool buttonPressed = false;
///
/// Initialize event with a button from the controller
///
/// Button to check, use Buttons enum
public InputEvent(Buttons buttonID)
{
button = buttonID;
}
///
/// Initialize event with key from the keyboard
///
/// Key to check, use Keys enum
public InputEvent(Keys keyID)
{
key = keyID;
keyboardTimer = Stopwatch.StartNew();
}
///
/// Check if a key was just pressed and not held down from the last check
/// Use this if you don't want input from a key to carry over to the next frame
///
/// Current KeyboardState
/// Whether key was just pressed
public bool NewPress(KeyboardState keyState)
{
if (keyState.IsKeyDown(key))
{
if (keyboardTimer.ElapsedMilliseconds > KEY_TIMER_MAX_MILLI)
{
keyboardTimer.Reset();
keyboardTimer.Start();
return true;
}
else
{
return false;
}
}
return false;
}
///
/// Check if a key is being held down
///
/// Current KeyboardState
/// Whether key is being held down
public bool Held(KeyboardState keyState)
{
if (keyState.IsKeyDown(key))
{
return true;
}
else
{
return false;
}
}
///
/// Check if a button was just pressed and not held down from the last check
/// Use this if you don't want input from a button to carry over to the next frame
///
/// Current GamePadState from appropriate player
/// Whether button was just pressed
public bool NewPress(GamePadState gamePad)
{
if (gamePad.IsButtonDown(button))
{
//check if it was already pressed before
if (buttonPressed)
{
return false;
}
else
{
buttonPressed = true;
return true;
}
}
else
{
buttonPressed = false;
}
return false;
}
///
/// Check if a button is being held down
///
/// Current GamePadState from appropriate player
/// Whether button is being held down
public bool Held(GamePadState gamePad)
{
if (gamePad.IsButtonDown(button))
{
return true;
}
else
{
buttonPressed = false;
return false;
}
}
}
}