using System.Linq; using Chernobyl.Collections.Generic.Event; using Chernobyl.Input.Controls; using Chernobyl.Input.Controls.Axis; using Chernobyl.Mathematics.Mechanics; using Microsoft.Xna.Framework.Graphics; namespace Chernobyl.Input.Xna.Controls.Mouse { /// /// An XNA implementation of the Chernobyl.Input Mouse. /// public class XnaMouse : Axis3DControl, IMouse { /// /// Initializes a new instance of the class. /// /// The /// instance to inject and take services from this control and it's /// children. public XnaMouse(IEventCollection services) : base(services) { _services = services; LeftButton = new LeftMouseButton(_services, this); MiddleButton = new MiddleMouseButton(_services, this); RightButton = new RightMouseButton(_services, this); ExtraButton1 = new ExtraMouseButton1(_services, this); ExtraButton2 = new ExtraMouseButton2(_services, this); } /// /// The X axis of the 2 dimensions of this control. /// public override IAxisControl X { get { if (_x == null) { _x = new MouseXAxis(_services); Control.MakeParentChild(this, _x); } return _x; } } /// /// The Y axis of the 2 dimensions of this control. /// public override IAxisControl Y { get { if (_y == null) { _y = new MouseYAxis(_services); Control.MakeParentChild(this, _y); } return _y; } } /// /// The Z axis of the 3 dimensions of this mechanism. /// public override IAxisControl Z { get { return ScrollWheel; } } /// /// The left mouse button. /// public virtual IButtonControl LeftButton { get; private set; } /// /// The middle mouse button. /// public virtual IButtonControl MiddleButton { get; private set; } /// /// The right mouse button /// public virtual IButtonControl RightButton { get; private set; } /// /// An extra button available on some mouse devices. /// public virtual IButtonControl ExtraButton1 { get; private set; } /// /// An extra button available on some mouse devices. /// public virtual IButtonControl ExtraButton2 { get; private set; } /// /// Gets the position of the scroll wheel. This is a cumulative /// value since the application was started. Forward on the scroll /// wheel is a positive direction, backward is a negative direction. /// public virtual IAxisControl ScrollWheel { get { // delay the creation of this IAxisControl so that it does not // update when it is not needed. return _scrollWheel ?? (_scrollWheel = new MouseScrollWheel(_services, this)); } } /// /// Represents the mouse's X axis. /// public class MouseXAxis : AxisControl { /// /// Constructor. /// /// The services that are to be injected into /// this control and its children. public MouseXAxis(IEventCollection services) : base(services) { Name = "Mouse X Axis"; } /// /// The amount of change, from the axis' /// to its . Implementors should return /// the raw change amount and not try to apply /// to the returned value. The /// class will apply the /// value for you. /// protected override float OverridableChangeAmount { get { return Microsoft.Xna.Framework.Input.Mouse.GetState().X - PreviousValue; } } } /// /// Represents the mouse's Y axis. /// public class MouseYAxis : AxisControl { /// /// Constructor. /// /// The services that are to be injected into /// this control and its children. public MouseYAxis(IEventCollection services) : base(services) { Name = "Mouse Y Axis"; MaxValue = services.OfType().First().Viewport.Height; } /// /// The amount of change, from the axis' /// to its . Implementors should return /// the raw change amount and not try to apply /// to the returned value. The /// class will apply the /// value for you. /// protected override float OverridableChangeAmount { get { // we subtract from the maximum value to orient the Y axis to the bottom left corner of // the screen because XNA orients to the top left. return (MaxValue - Microsoft.Xna.Framework.Input.Mouse.GetState().Y) - PreviousValue; } } /// /// We use the maximum value to orient the mouse axis to the bottom /// left point of the screen because XNA orients to the top left. /// int MaxValue { get; set; } } /// /// The instance to inject and take /// services from this control and it's children. /// readonly IEventCollection _services; /// /// The backing field to . /// IAxisControl _scrollWheel; /// /// The backing field to . /// IAxisControl _x; /// /// The backing field to . /// IAxisControl _y; } }