Exemple #1
0
        private void ProcessEvent(ref SDL_Event evt)
        {
            switch (evt.type)
            {
            case SDL_EventType.ControllerAxisMotion:
                SDL_ControllerAxisEvent axisEvent = Unsafe
                                                    .As <SDL_Event, SDL_ControllerAxisEvent>(ref evt);
                if (axisEvent.which == _instanceId)
                {
                    _axisValues[(int)axisEvent.axis] = axisEvent.value < 0
                            ? -((float)axisEvent.value / short.MinValue)
                            : (float)axisEvent.value / short.MaxValue;
                }
                break;

            case SDL_EventType.ControllerButtonDown:
            case SDL_EventType.ControllerButtonUp:
                SDL_ControllerButtonEvent buttonEvent = Unsafe
                                                        .As <SDL_Event, SDL_ControllerButtonEvent>(ref evt);
                if (buttonEvent.which == _instanceId)
                {
                    int  index = (int)buttonEvent.button;
                    bool down  = buttonEvent.state == 1;
                    _newButtons[index]  = !_buttonState[index] && down;
                    _buttonState[index] = down;
                }
                break;
            }
        }
        private void ProcessAnEvent(ref SDL_Event ev)
        {
            int id;

            switch (ev.type)
            {
            case SDL_EventType.ControllerDeviceAdded:
                UpdateGamepadRegister();
                _applicationMessenger.QueueMessage(FrameworkMessage.GamepadAdded);
                break;

            case SDL_EventType.ControllerDeviceRemoved:
                UpdateGamepadRegister();
                _applicationMessenger.QueueMessage(FrameworkMessage.GamepadRemoved);
                break;

            case SDL_EventType.ControllerDeviceRemapped:
                //Unsure if am required to process this event. For future investigation
                break;

            case SDL_EventType.ControllerButtonUp:
            case SDL_EventType.ControllerButtonDown:
                SDL_ControllerButtonEvent buttonEvent = Unsafe.As <SDL_Event, SDL_ControllerButtonEvent>(ref ev);

                id = buttonEvent.which;

                if (_controllers.ContainsKey(id))
                {
                    var controller = _controllers[id];

                    var button = ToGamepadButton(buttonEvent.button);

                    var pressed = buttonEvent.state == 1;     //SDL_PRESSED and SDL_RELEASED don't appear to exist

                    controller.ProcessButtonEvent(button, pressed);
                }
                break;

            case SDL_EventType.ControllerAxisMotion:
                SDL_ControllerAxisEvent axisEvent = Unsafe.As <SDL_Event, SDL_ControllerAxisEvent>(ref ev);

                id = axisEvent.which;

                if (_controllers.ContainsKey(id))
                {
                    var controller = _controllers[id];

                    var axis = ToGamepadAxis(axisEvent.axis);

                    var value = NormalizeAxis(axisEvent.value);

                    controller.ProcessAxisEvent(axis, value);
                }
                break;
            }
        }
        /// <summary>
        /// Handles an SDL2 axis motion event.
        /// </summary>
        /// <param name="evt">The SDL2 event data.</param>
        private void OnAxisMotion(SDL_ControllerAxisEvent evt)
        {
            var value = NormalizeAxisValue(evt.value);

            switch ((SDL_GameControllerAxis)evt.axis)
            {
            case SDL_GameControllerAxis.LEFTX:
                prevLeftJoystickX = leftJoystickX;
                leftJoystickX     = value;
                OnAxisChanged(GamePadAxis.LeftJoystickX, value);
                CheckForAxisPresses(GamePadAxis.LeftJoystickX, prevLeftJoystickX, value);
                break;

            case SDL_GameControllerAxis.LEFTY:
                prevLeftJoystickY = leftJoystickY;
                leftJoystickY     = value;
                OnAxisChanged(GamePadAxis.LeftJoystickY, value);
                CheckForAxisPresses(GamePadAxis.LeftJoystickY, prevLeftJoystickY, value);
                break;

            case SDL_GameControllerAxis.RIGHTX:
                prevRightJoystickX = rightJoystickX;
                rightJoystickX     = value;
                OnAxisChanged(GamePadAxis.RightJoystickX, value);
                CheckForAxisPresses(GamePadAxis.RightJoystickX, prevRightJoystickX, value);
                break;

            case SDL_GameControllerAxis.RIGHTY:
                prevRightJoystickY = rightJoystickX;
                rightJoystickY     = value;
                OnAxisChanged(GamePadAxis.RightJoystickY, value);
                CheckForAxisPresses(GamePadAxis.RightJoystickY, prevRightJoystickY, value);
                break;

            case SDL_GameControllerAxis.TRIGGERLEFT:
                prevLeftTrigger = leftTrigger;
                leftTrigger     = value;
                OnAxisChanged(GamePadAxis.LeftTrigger, value);
                CheckForAxisPresses(GamePadAxis.LeftTrigger, prevLeftTrigger, value);
                break;

            case SDL_GameControllerAxis.TRIGGERRIGHT:
                prevRightTrigger = rightTrigger;
                rightTrigger     = value;
                OnAxisChanged(GamePadAxis.RightTrigger, value);
                CheckForAxisPresses(GamePadAxis.RightTrigger, prevRightTrigger, value);
                break;
            }
        }
Exemple #4
0
        private void ProcessEvent(ref SDL_Event ev)
        {
            switch (ev.type)
            {
            case SDL_EventType.ControllerAxisMotion:
                SDL_ControllerAxisEvent axisEvent = Unsafe.As <SDL_Event, SDL_ControllerAxisEvent>(ref ev);
                if (axisEvent.which == _controllerIndex)
                {
                    _axisValues[axisEvent.axis] = Normalize(axisEvent.value);
                }
                break;

            case SDL_EventType.ControllerButtonDown:
            case SDL_EventType.ControllerButtonUp:
                SDL_ControllerButtonEvent buttonEvent = Unsafe.As <SDL_Event, SDL_ControllerButtonEvent>(ref ev);
                if (buttonEvent.which == _controllerIndex)
                {
                    _buttons[buttonEvent.button] = buttonEvent.state == 1;
                }
                break;
            }
        }