using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
namespace Nuclex
{
/// Service allowing object to monitor user input for a form or control
public interface IInputPublisherService
{
/// Fired when the mouse has been clicked
event MouseEventHandler MouseClick;
/// Fired when a mouse button is pressed down
event MouseEventHandler MouseDown;
/// Fired when a mouse button is released again
event MouseEventHandler MouseUp;
/// Fired when the mouse has been moved
event MouseEventHandler MouseMove;
/// Fired when a character has been entered
event KeyPressEventHandler KeyPress;
/// Fired when a key is pressed down
event KeyEventHandler KeyDown;
/// Fired when a key is released again
event KeyEventHandler KeyUp;
}
/// Service allowing object to monitor user input for a form or control
internal class ControlInputPublisher : IInputPublisherService, IDisposable
{
/// Fired when the mouse has been clicked
public event MouseEventHandler MouseClick;
/// Fired when a mouse button is pressed down
public event MouseEventHandler MouseDown;
/// Fired when a mouse button is released again
public event MouseEventHandler MouseUp;
/// Fired when the mouse has been moved
public event MouseEventHandler MouseMove;
/// Fired when a character has been entered
public event KeyPressEventHandler KeyPress;
/// Fired when a key is pressed down
public event KeyEventHandler KeyDown;
/// Fired when a key is released again
public event KeyEventHandler KeyUp;
/// Initializes a new user control input event publisher
/// User control whose input events to publish
public ControlInputPublisher(UserControl control)
{
this.control = control;
control.MouseClick += new MouseEventHandler(mouseClick);
control.MouseDown += new MouseEventHandler(mouseDown);
control.MouseUp += new MouseEventHandler(mouseUp);
control.MouseMove += new MouseEventHandler(mouseMove);
control.KeyDown += new KeyEventHandler(keyDown);
control.KeyPress += new KeyPressEventHandler(keyPress);
control.KeyUp += new KeyEventHandler(keyUp);
}
/// Immediately releases all resources owned by the object
public void Dispose()
{
if (this.control != null)
{
this.control.MouseClick -= new MouseEventHandler(mouseClick);
this.control.MouseDown -= new MouseEventHandler(mouseDown);
this.control.MouseUp -= new MouseEventHandler(mouseUp);
this.control.MouseMove -= new MouseEventHandler(mouseMove);
this.control.KeyDown -= new KeyEventHandler(keyDown);
this.control.KeyPress -= new KeyPressEventHandler(keyPress);
this.control.KeyUp -= new KeyEventHandler(keyUp);
this.control = null;
GC.SuppressFinalize(this);
}
}
/// Called when a mouse button has been clicked
/// Window on which the mouse has been clicked
///
/// Informations about the mouse state at the time of clicking
///
private void mouseClick(object sender, MouseEventArgs arguments)
{
if (MouseClick != null)
MouseClick(sender, arguments);
}
/// Called when a mouse button has been pressed
/// Window on which the button has been pressed
///
/// Informations about the mouse state at the time of press
///
private void mouseDown(object sender, MouseEventArgs arguments)
{
if (MouseDown != null)
MouseDown(sender, arguments);
}
/// Called when a mouse button has been released
/// Window on which the button has been released
///
/// Informations about the mouse state at the time of release
///
private void mouseUp(object sender, MouseEventArgs arguments)
{
if (MouseUp != null)
MouseUp(sender, arguments);
}
/// Called when the mouse has been moved
/// Window over which the mouse has been moved
///
/// Informations about the mouse state at the time of movement
///
private void mouseMove(object sender, MouseEventArgs arguments)
{
if (MouseMove != null)
MouseMove(sender, arguments);
}
/// Called when a character has been entered
/// Window having the input focus
///
/// Informations about the keyboard state at the time of entering
///
private void keyPress(object sender, KeyPressEventArgs arguments)
{
if (KeyPress != null)
KeyPress(sender, arguments);
}
/// Called when a key has been pressed
/// Window having the input focus
///
/// Informations about the keyboard state at the time of press
///
private void keyDown(object sender, KeyEventArgs arguments)
{
if (KeyDown != null)
KeyDown(sender, arguments);
}
/// Called when a key has been released
/// Window having the input focus
///
/// Informations about the keyboard state at the time of release
///
private void keyUp(object sender, KeyEventArgs arguments)
{
if (KeyUp != null)
KeyUp(sender, arguments);
}
/// User control whose input events this publisher makes public
private UserControl control;
}
} // namespace Nuclex