Overview

A device in the context of UxbTransform is an external unit that provides input that can be read by the system. Simple examples of devices are a mouse, keyboard, or gamepad. Most devices are composed of many Device Components-- for example a mouse is composed of two or more buttons, a scroll wheel, and a position tracking unit. Devices are responsible for updating the state of their components and alerting the system that changes have occured.

Included Devices

  • Mouse
  • WiiMote
  • Gamepad

Extending Devices

To add a new device a class must be created that inherits from the abstract device class and is decorated with the DeviceAttribute. Additionally the PartCreationPolicyAttribute should be applied to alert MEF not to use a shared instance of the device. The device class must implement methods to connect and disconnect the device to and from UxbTransform. A get property should be exposed for each component of the device. When device changed the OnDeviceStateChanged method should be called to alert the system.

Sample

[PartCreationPolicy(CreationPolicy.NonShared)]
[Device("Mouse", "MouseDevice")]
public class MouseDevice : Device
{
    MouseHook _mouse;

    ButtonDeviceComponent _left;
    DeltaDeviceComponent _wheel;
    PositionalDeviceComponent _cursor;
            
    public ButtonDeviceComponent Left { get { return _left; } }
    public DeltaDeviceComponent Wheel { get { return _wheel; } }
    public PositionalDeviceComponent Cursor { get { return _cursor; } }

    public MouseDevice()
    {
        _mouse = new MouseHook();

        _left = new ButtonDeviceComponent(this) { Name = "Left", State = ButtonState.Up };
        _wheel = new DeltaDeviceComponent(this) { Name = "Wheel" };
        _cursor = new PositionalDeviceComponent(this) { Name = "Cursor" };
    }

    protected override Boolean ConnectImplementation()
    {
        _mouse.Start();

        _mouse.MouseDown += new System.Windows.Forms.MouseEventHandler(_mouse_MouseDown);
        _mouse.MouseUp += new System.Windows.Forms.MouseEventHandler(_mouse_MouseUp);
        _mouse.MouseMove += new System.Windows.Forms.MouseEventHandler(_mouse_MouseMove);
        _mouse.MouseWheel += new System.Windows.Forms.MouseEventHandler(_mouse_MouseWheel);

        return true;
    }

    protected override void DisconnectImplementation()
    {
        _mouse.MouseDown -= _mouse_MouseDown;
        _mouse.MouseUp -= _mouse_MouseUp;
        _mouse.MouseMove -= _mouse_MouseMove;
        _mouse.MouseWheel -= _mouse_MouseWheel;

        _mouse.Stop();
    }

    void _mouse_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        Rectangle screenDimensions = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
        _cursor.X = (Single)((Single)e.X / (Single)screenDimensions.Width);
        _cursor.Y = (Single)((Single)e.Y / (Single)screenDimensions.Height);

        OnDeviceStateChanged();
    }

    void _mouse_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        _wheel.Delta = e.Delta;

        OnDeviceStateChanged();

        _wheel.Delta = 0;
    }

    void _mouse_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        _left.State = ButtonState.Up;
        OnDeviceStateChanged();
    }

    void _mouse_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        _left.State = ButtonState.
        OnDeviceStateChanged();
    }

    public override string ToString()
    {
        return String.Format("Mouse {0}", Index);
    }
}

Last edited Dec 2, 2009 at 1:39 AM by PhilipB, version 3

Comments

No comments yet.