using System;
using Chernobyl.Input.Controls;
using Chernobyl.Utility;
namespace Chernobyl.Interface.Input
{
///
/// An that provides interface control using an
/// .
///
public class MousePointerControl : IPointerControl
{
///
/// Initializes a new instance of the class.
///
/// The instance that provides the interface control.
public MousePointerControl(IMouse mouse)
{
Mouse = mouse;
}
///
/// An event that is raised each time the control mechanism is pressed
/// down. This mechanism can only be once per
/// mechanism's pointer. The mechanism must raise
/// for a pointer prior to that pointer raising
/// again. For example, a mouse can only raise
/// once and then it must raise
/// to allow to be raised. A touch screen on
/// the other hand can invoke for each finger
/// that touches the screen but must invoke for each
/// finger.
///
public event EventHandler PressedDown;
///
/// An event that is raised each time the control mechanism is let up.
/// See for more information on this topic.
///
public event EventHandler LetUp;
///
/// The instance that provides the interface control.
///
public IMouse Mouse
{
get { return _mouse; }
private set
{
value.ThrowIfNull("value");
IMouse previousValue = _mouse;
_mouse = value;
if(previousValue != null)
{
previousValue.LeftButton.OnDown -= MouseLeftButtonOnDown;
previousValue.LeftButton.OnUp -= MouseLeftButtonOnUp;
}
if(_mouse != null)
{
_mouse.LeftButton.OnDown += MouseLeftButtonOnDown;
_mouse.LeftButton.OnUp += MouseLeftButtonOnUp;
}
}
}
///
/// An event handler that is invoked when the
/// of has been let
/// up. This method raises the event on this instance.
///
/// The instance that generated the event.
/// The instance containing the
/// event data.
void MouseLeftButtonOnUp(object sender, EventArgs e)
{
if(LetUp != null)
LetUp(this, new PointerEventArgs(_mouse));
}
///
/// An event handler that is invoked when the
/// of has been
/// pressed down. This method raises the event
/// on this instance.
///
/// The instance that generated the event.
/// The instance containing the
/// event data.
void MouseLeftButtonOnDown(object sender, EventArgs e)
{
if(PressedDown != null)
PressedDown(this, new PointerEventArgs(_mouse));
}
///
/// The backing field to .
///
IMouse _mouse;
}
}