Exemple #1
0
        private void OnJoystickCallback(int jid, GLFW_Enum eventType)
        {
            if (eventType == GLFW_Enum.CONNECTED)
            {
                var name      = GLFW.GetJoystickName(jid);
                var isGamepad = GLFW.JoystickIsGamepad(jid) != 0;

                GLFW.GetJoystickButtons(jid, out int buttonCount);
                GLFW.GetJoystickAxes(jid, out int axisCount);

                OnJoystickConnect((uint)jid, name, (uint)buttonCount, (uint)axisCount, isGamepad);
            }
            else if (eventType == GLFW_Enum.DISCONNECTED)
            {
                OnJoystickDisconnect((uint)jid);
            }
        }
Exemple #2
0
        internal void AfterUpdate()
        {
            const float AXIS_EPSILON = 0.000001f;

            for (int jid = 0; jid <= (int)GLFW_Enum.JOYSTICK_LAST; jid++)
            {
                uint index = (uint)jid;

                if (GLFW.JoystickPresent(jid) != 0)
                {
                    if (GLFW.JoystickIsGamepad(jid) != 0 && GLFW.GetGamepadState(jid, ref gamepadState) != 0)
                    {
                        for (int i = 0; i < gamepadState.Buttons.Length; i++)
                        {
                            var button = GamepadButtonToEnum((GLFW_Enum)i);
                            var down   = IsGamepadButtonDown(index, button);
                            var state  = gamepadState.Buttons[i];

                            if (!down && state == 1)
                            {
                                OnGamepadButtonDown(index, button);
                            }
                            else if (down && state == 0)
                            {
                                OnGamepadButtonUp(index, button);
                            }
                        }

                        for (int i = 0; i < gamepadState.Axes.Length; i++)
                        {
                            var axis    = GamepadAxisToEnum((GLFW_Enum)i);
                            var current = GetGamepadAxis(index, axis);
                            var next    = gamepadState.Axes[i];

                            if (Math.Abs(current - next) > AXIS_EPSILON)
                            {
                                OnGamepadAxis(index, axis, next);
                            }
                        }
                    }
                    else
                    {
                        unsafe
                        {
                            char *buttons = (char *)GLFW.GetJoystickButtons(jid, out int buttonCount).ToPointer();
                            for (int i = 0; i < buttonCount; i++)
                            {
                                var button = (uint)i;
                                var down   = IsJoystickButtonDown(index, button);
                                var state  = buttons[i];

                                if (!down && state == 1)
                                {
                                    OnJoystickButtonDown(index, button);
                                }
                                else if (down && state == 0)
                                {
                                    OnJoystickButtonUp(index, button);
                                }
                            }

                            float *axes = (float *)GLFW.GetJoystickAxes(jid, out int axesCount).ToPointer();
                            for (int i = 0; i < axesCount; i++)
                            {
                                var axis    = (uint)i;
                                var current = GetJoystickAxis(index, axis);
                                var next    = axes[i];

                                if (Math.Abs(current - next) > AXIS_EPSILON)
                                {
                                    OnJoystickAxis(index, axis, next);
                                }
                            }
                        }
                    }
                }
            }
        }