コード例 #1
0
        public JoystickState GetState(int player_index)
        {
            lock (sync)
            {
                JoystickState state = new JoystickState();

                if (IsValid(player_index))
                {
                    int device_index = player_to_index[player_index];
                    int index        = index_to_stick[device_index];
                    JoystickDevice <WinMMJoyDetails> stick =
                        sticks[index] as JoystickDevice <WinMMJoyDetails>;

                    // For joysticks with fewer than three axes or four buttons, we must
                    // use joyGetPos; otherwise, joyGetPosEx. This is not just a cosmetic
                    // difference, simple devices will return incorrect results if we use
                    // joyGetPosEx on them.
                    if (stick.Details.Capabilities.AxisCount <= 3 || stick.Details.Capabilities.ButtonCount <= 4)
                    {
                        // Use joyGetPos
                        JoyInfo info = new JoyInfo();

                        JoystickError result = UnsafeNativeMethods.joyGetPos(device_index, ref info);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info.Buttons & 1 << i) != 0);
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                    else
                    {
                        // Use joyGetPosEx
                        JoyInfoEx info = new JoyInfoEx();
                        info.Size  = JoyInfoEx.SizeInBytes;
                        info.Flags = JoystickFlags.All;

                        JoystickError result = UnsafeNativeMethods.joyGetPosEx(device_index, ref info);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info.Buttons & 1 << i) != 0);
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                }

                return(state);
            }
        }
コード例 #2
0
        public JoystickState GetState(int player_index)
        {
            lock (sync)
            {
                JoystickState state = new JoystickState();

                if (IsValid(player_index))
                {
                    int device_index = player_to_index[player_index];
                    int index = index_to_stick[device_index];
                    JoystickDevice<WinMMJoyDetails> stick =
                        sticks[index] as JoystickDevice<WinMMJoyDetails>;

                    // For joysticks with fewer than three axes or four buttons, we must
                    // use joyGetPos; otherwise, joyGetPosEx. This is not just a cosmetic
                    // difference, simple devices will return incorrect results if we use
                    // joyGetPosEx on them.
                    if (stick.Details.Capabilities.AxisCount <= 3 || stick.Details.Capabilities.ButtonCount <= 4)
                    {
                        // Use joyGetPos
                        JoyInfo info = new JoyInfo();

                        JoystickError result = UnsafeNativeMethods.joyGetPos(device_index, ref info);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info.Buttons & 1 << i) != 0);
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                    else
                    {
                        // Use joyGetPosEx
                        JoyInfoEx info_ex = new JoyInfoEx();
                        info_ex.Size = JoyInfoEx.SizeInBytes;
                        info_ex.Flags = JoystickFlags.All;

                        JoystickError result = UnsafeNativeMethods.joyGetPosEx(device_index, ref info_ex);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info_ex.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info_ex.Buttons & 1 << i) != 0);
                            }

                            for (int i = 0; i < stick.Details.Capabilities.HatCount; i++)
                            {
                                // A discrete POV returns specific values for left, right, etc.
                                // A continuous POV returns an integer indicating an angle in degrees * 100, e.g. 18000 == 180.00 degrees.
                                // The vast majority of joysticks have discrete POVs, so we'll treat all of them as discrete for simplicity.
                                if ((JoystickPovPosition)info_ex.Pov != JoystickPovPosition.Centered)
                                {
                                    HatPosition hatpos = HatPosition.Centered;
                                    if (info_ex.Pov < 4500 || info_ex.Pov >= 31500)
                                        hatpos |= HatPosition.Up;
                                    if (info_ex.Pov >= 4500 && info_ex.Pov < 13500)
                                        hatpos |= HatPosition.Right;
                                    if (info_ex.Pov >= 13500 && info_ex.Pov < 22500)
                                        hatpos |= HatPosition.Down;
                                    if (info_ex.Pov >= 22500 && info_ex.Pov < 31500)
                                        hatpos |= HatPosition.Left;

                                    state.SetHat(JoystickHat.Hat0 + i, new JoystickHatState(hatpos));
                                }
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                }

                return state;
            }
        }
コード例 #3
0
ファイル: WinMMJoystick.cs プロジェクト: noggs/opentk
        public JoystickState GetState(int player_index)
        {
            lock (sync)
            {
                JoystickState state = new JoystickState();

                if (IsValid(player_index))
                {
                    int device_index = player_to_index[player_index];
                    int index = index_to_stick[device_index];
                    JoystickDevice<WinMMJoyDetails> stick =
                        sticks[index] as JoystickDevice<WinMMJoyDetails>;

                    // For joysticks with fewer than three axes or four buttons, we must
                    // use joyGetPos; otherwise, joyGetPosEx. This is not just a cosmetic
                    // difference, simple devices will return incorrect results if we use
                    // joyGetPosEx on them.
                    if (stick.Details.Capabilities.AxisCount <= 3 || stick.Details.Capabilities.ButtonCount <= 4)
                    {
                        // Use joyGetPos
                        JoyInfo info = new JoyInfo();

                        JoystickError result = UnsafeNativeMethods.joyGetPos(device_index, ref info);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info.Buttons & 1 << i) != 0);
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                    else
                    {
                        // Use joyGetPosEx
                        JoyInfoEx info = new JoyInfoEx();
                        info.Size = JoyInfoEx.SizeInBytes;
                        info.Flags = JoystickFlags.All;

                        JoystickError result = UnsafeNativeMethods.joyGetPosEx(device_index, ref info);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info.Buttons & 1 << i) != 0);
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                }

                return state;
            }
        }
コード例 #4
0
ファイル: WinMMJoystick.cs プロジェクト: batbuild/opentk
        public JoystickState GetState(int player_index)
        {
            lock (sync)
            {
                JoystickState state = new JoystickState();

                if (IsValid(player_index))
                {
                    int device_index = player_to_index[player_index];
                    int index        = index_to_stick[device_index];
                    JoystickDevice <WinMMJoyDetails> stick =
                        sticks[index] as JoystickDevice <WinMMJoyDetails>;

                    // For joysticks with fewer than three axes or four buttons, we must
                    // use joyGetPos; otherwise, joyGetPosEx. This is not just a cosmetic
                    // difference, simple devices will return incorrect results if we use
                    // joyGetPosEx on them.
                    if (stick.Details.Capabilities.AxisCount <= 3 || stick.Details.Capabilities.ButtonCount <= 4)
                    {
                        // Use joyGetPos
                        JoyInfo info = new JoyInfo();

                        JoystickError result = UnsafeNativeMethods.joyGetPos(device_index, ref info);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info.Buttons & 1 << i) != 0);
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                    else
                    {
                        // Use joyGetPosEx
                        JoyInfoEx info_ex = new JoyInfoEx();
                        info_ex.Size  = JoyInfoEx.SizeInBytes;
                        info_ex.Flags = JoystickFlags.All;

                        JoystickError result = UnsafeNativeMethods.joyGetPosEx(device_index, ref info_ex);
                        if (result == JoystickError.NoError)
                        {
                            for (int i = 0; i < stick.Details.Capabilities.AxisCount; i++)
                            {
                                state.SetAxis(JoystickAxis.Axis0 + i, CalculateOffset(info_ex.GetAxis(i), stick.Details.Min[i], stick.Details.Max[i]));
                            }

                            for (int i = 0; i < stick.Details.Capabilities.ButtonCount; i++)
                            {
                                state.SetButton(JoystickButton.Button0 + i, (info_ex.Buttons & 1 << i) != 0);
                            }

                            for (int i = 0; i < stick.Details.Capabilities.HatCount; i++)
                            {
                                // A discrete POV returns specific values for left, right, etc.
                                // A continuous POV returns an integer indicating an angle in degrees * 100, e.g. 18000 == 180.00 degrees.
                                // The vast majority of joysticks have discrete POVs, so we'll treat all of them as discrete for simplicity.
                                if ((JoystickPovPosition)info_ex.Pov != JoystickPovPosition.Centered)
                                {
                                    HatPosition hatpos = HatPosition.Centered;
                                    if (info_ex.Pov < 4500 || info_ex.Pov >= 31500)
                                    {
                                        hatpos |= HatPosition.Up;
                                    }
                                    if (info_ex.Pov >= 4500 && info_ex.Pov < 13500)
                                    {
                                        hatpos |= HatPosition.Right;
                                    }
                                    if (info_ex.Pov >= 13500 && info_ex.Pov < 22500)
                                    {
                                        hatpos |= HatPosition.Down;
                                    }
                                    if (info_ex.Pov >= 22500 && info_ex.Pov < 31500)
                                    {
                                        hatpos |= HatPosition.Left;
                                    }

                                    state.SetHat(JoystickHat.Hat0 + i, new JoystickHatState(hatpos));
                                }
                            }

                            state.SetIsConnected(true);
                        }
                        else if (result == JoystickError.Unplugged)
                        {
                            UnplugJoystick(player_index);
                        }
                    }
                }

                return(state);
            }
        }