using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace TrueCraft.Client.Input
{
///
/// Encapsulates mouse input in an event-driven manner.
///
public sealed class MouseHandler : GameComponent
{
///
/// Raised when this mouse component is moved.
///
public event EventHandler Move;
///
/// Raised when a button for this mouse component is pressed.
///
public event EventHandler ButtonDown;
///
/// Raised when a button for this mouse component is released.
///
public event EventHandler ButtonUp;
///
/// Raised when the scroll wheel for this mouse component is moved.
///
public event EventHandler Scroll;
///
/// Gets the state for this mouse component.
///
public MouseState State { get; private set; }
///
/// Creates a new mouse component.
///
/// The parent game for the component.
public MouseHandler(Game game)
: base(game)
{
}
///
/// Initializes this mouse component.
///
public override void Initialize()
{
State = Mouse.GetState();
base.Initialize();
}
///
/// Updates this mouse component.
///
/// The game time for the update.
public override void Update(GameTime gameTime)
{
var newState = Mouse.GetState();
Process(newState, State);
State = newState;
base.Update(gameTime);
}
///
/// Processes a change between two states.
///
/// The new state.
/// The old state.
private void Process(MouseState newState, MouseState oldState)
{
// Movement.
if ((newState.X != oldState.X) || (newState.Y != oldState.Y))
{
var args = new MouseMoveEventArgs(newState.X, newState.Y, (newState.X - oldState.X), (newState.Y - oldState.Y));
if (Move != null)
Move(this, args);
}
// Scrolling.
if (newState.ScrollWheelValue != oldState.ScrollWheelValue)
{
var args = new MouseScrollEventArgs(newState.X, newState.Y, newState.ScrollWheelValue, (newState.ScrollWheelValue - oldState.ScrollWheelValue));
if (Scroll != null)
Scroll(this, args);
}
// A bit of code duplication here, shame XNA doesn't expose button state through an enumeration...
// Left button.
if (newState.LeftButton != oldState.LeftButton)
{
var args = new MouseButtonEventArgs(newState.X, newState.Y, MouseButton.Left, (newState.LeftButton == ButtonState.Pressed));
if (args.IsPressed)
{
if (ButtonDown != null)
ButtonDown(this, args);
}
else
{
if (ButtonUp != null)
ButtonUp(this, args);
}
}
// Right button.
if (newState.RightButton != oldState.RightButton)
{
var args = new MouseButtonEventArgs(newState.X, newState.Y, MouseButton.Right, (newState.RightButton == ButtonState.Pressed));
if (args.IsPressed)
{
if (ButtonDown != null)
ButtonDown(this, args);
}
else
{
if (ButtonUp != null)
ButtonUp(this, args);
}
}
// Middle button.
if (newState.MiddleButton != oldState.MiddleButton)
{
var args = new MouseButtonEventArgs(newState.X, newState.Y, MouseButton.Middle, (newState.MiddleButton == ButtonState.Pressed));
if (args.IsPressed)
{
if (ButtonDown != null)
ButtonDown(this, args);
}
else
{
if (ButtonUp != null)
ButtonUp(this, args);
}
}
}
///
/// Called when this mouse component is being disposed of.
///
/// Whether Dispose() called this method.
protected override void Dispose(bool disposing)
{
if (disposing)
{
Move = null;
ButtonDown = null;
ButtonUp = null;
Scroll = null;
}
base.Dispose(disposing);
}
}
}