Beispiel #1
0
        public State GetState()
        {
            unsafe
            {
                if (mouseDevice == null)
                {
                    return(new State(Vector3I.Zero));
                }

                DIMOUSESTATE diState = new DIMOUSESTATE();

                int hr;

                hr = IDirectInputDevice8.GetDeviceState(mouseDevice,
                                                        (uint)sizeof(DIMOUSESTATE), &diState);

                if (Wrapper.FAILED(hr))
                {
                    hr = IDirectInputDevice8.Acquire(mouseDevice);
                    while (hr == DInput.GetHRESULT_DIERR_INPUTLOST())
                    {
                        hr = IDirectInputDevice8.Acquire(mouseDevice);
                    }

                    return(new State(Vector3I.Zero));
                }

                return(new State(new Vector3I(diState.lX, diState.lY, diState.lZ)));
            }
        }
        unsafe protected override void OnUpdateState()
        {
            int hr;

            // gain access to device
            hr = IDirectInputDevice8.Poll(directInputDevice);
            if (Wrapper.FAILED(hr))
            {
                if (hr == DInput.GetHRESULT_DIERR_INPUTLOST())
                {
                    if (!IsDeviceLost())
                    {
                        DeviceLost();
                        return;
                    }
                }

                hr = IDirectInputDevice8.Acquire(directInputDevice);
                while (hr == DInput.GetHRESULT_DIERR_INPUTLOST())
                {
                    hr = IDirectInputDevice8.Acquire(directInputDevice);
                }
                IDirectInputDevice8.Poll(directInputDevice);
            }
            else
            {
                if (IsDeviceLost())
                {
                    DeviceRestore();
                }
            }

            // get data

            uint entries = BufferSize;
            DIDEVICEOBJECTDATA *entryPtr = (DIDEVICEOBJECTDATA *)deviceDataBuffer;

            hr = IDirectInputDevice8.GetDeviceData(directInputDevice,
                                                   (uint)sizeof(DIDEVICEOBJECTDATA), entryPtr, ref entries, 0);
            if (Wrapper.FAILED(hr))
            {
                //Log.Info( "Cannot get device data for '" + Name + "' error = " +
                //   DInput.GetOutString( DInput.DXGetErrorStringW( hr ) ) );
                return;
            }

            // process data

            for (int k = 0; k < entries; k++)
            {
                switch (entryPtr->dwOfs)
                {
                case DInput.DIJOFS_X:
                case DInput.DIJOFS_Y:
                case DInput.DIJOFS_Z:
                case DInput.DIJOFS_RX:
                case DInput.DIJOFS_RY:
                case DInput.DIJOFS_RZ:
                {
                    JoystickAxes axisName = JoystickAxes.X;
                    switch (entryPtr->dwOfs)
                    {
                    case DInput.DIJOFS_X: axisName = JoystickAxes.X; break;

                    case DInput.DIJOFS_Y: axisName = JoystickAxes.Y; break;

                    case DInput.DIJOFS_Z: axisName = JoystickAxes.Z; break;

                    case DInput.DIJOFS_RX: axisName = JoystickAxes.Rx; break;

                    case DInput.DIJOFS_RY: axisName = JoystickAxes.Ry; break;

                    case DInput.DIJOFS_RZ: axisName = JoystickAxes.Rz; break;
                    }

                    Axis axis = GetAxisByName(axisName);

                    float value = (float)((int)entryPtr->dwData) / MaxRange;

                    //invert value for specific axes
                    if (axis.Name == JoystickAxes.Y || axis.Name == JoystickAxes.Ry ||
                        axis.Name == JoystickAxes.Rz)
                    {
                        value = -value;
                    }

                    axis.Value = value;

                    InputDeviceManager.Instance.SendEvent(new JoystickAxisChangedEvent(this, axis));
                }
                break;

                case DInput.DIJOFS_SLIDER00:
                {
                    Vector2F value = Sliders[0].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[0].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[0], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER01:
                {
                    Vector2F value = Sliders[0].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[0].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[0], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_SLIDER10:
                {
                    Vector2F value = Sliders[1].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[1].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[1], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER11:
                {
                    Vector2F value = Sliders[1].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[1].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[1], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_SLIDER20:
                {
                    Vector2F value = Sliders[2].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[2].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[2], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER21:
                {
                    Vector2F value = Sliders[2].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[2].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[2], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_SLIDER30:
                {
                    Vector2F value = Sliders[3].Value;
                    value.X          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[3].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[3], JoystickSliderAxes.X));
                }
                break;

                case DInput.DIJOFS_SLIDER31:
                {
                    Vector2F value = Sliders[3].Value;
                    value.Y          = -(float)((int)entryPtr->dwData) / MaxRange;
                    Sliders[3].Value = value;
                    InputDeviceManager.Instance.SendEvent(
                        new JoystickSliderChangedEvent(this, Sliders[3], JoystickSliderAxes.Y));
                }
                break;

                case DInput.DIJOFS_POV0:
                    UpdatePOV(0, entryPtr->dwData);
                    break;

                case DInput.DIJOFS_POV1:
                    UpdatePOV(1, entryPtr->dwData);
                    break;

                case DInput.DIJOFS_POV2:
                    UpdatePOV(2, entryPtr->dwData);
                    break;

                case DInput.DIJOFS_POV3:
                    UpdatePOV(3, entryPtr->dwData);
                    break;

                default:
                    if (entryPtr->dwOfs >= DInput.DIJOFS_BUTTON0 && entryPtr->dwOfs < DInput.DIJOFS_BUTTON128)
                    {
                        int  buttonIndex = (int)(entryPtr->dwOfs - DInput.DIJOFS_BUTTON0);
                        bool pressed     = ((entryPtr->dwData & 0x80) != 0);

                        Button button = Buttons[buttonIndex];
                        if (button.Pressed != pressed)
                        {
                            button.Pressed = pressed;

                            if (pressed)
                            {
                                InputDeviceManager.Instance.SendEvent(
                                    new JoystickButtonDownEvent(this, button));
                            }
                            else
                            {
                                InputDeviceManager.Instance.SendEvent(
                                    new JoystickButtonUpEvent(this, button));
                            }
                        }
                    }
                    break;
                }

                entryPtr++;
            }

            //update states for effects. effects can be destroyed inside OnUpdateState().
            if (ForceFeedbackController != null)
            {
                ForceFeedbackController.OnUpdateState();
            }
        }