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