Esempio n. 1
0
        private GamePadAxisEvent CreateAxisEvent(GamePadAxis axis, double newValue)
        {
            GamePadAxisEvent axisEvent = InputEventPool <GamePadAxisEvent> .GetOrCreate(this);

            axisEvent.Value = (float)newValue;
            axisEvent.Axis  = axis;
            state.Update(axisEvent);
            return(axisEvent);
        }
Esempio n. 2
0
        private bool UpdateFloat(ref float a, GamePadAxisEvent evt)
        {
            // ReSharper disable once CompareOfFloatsByEqualityOperator
            if (a == evt.Value)
            {
                return(false);
            }

            a = evt.Value;
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Updates the state from any gamepad events received that have mapped buttons
        /// </summary>
        /// <param name="axisEvent">The gamepad event to process</param>
        /// <returns><c>true</c> if the event made any changes</returns>
        public bool Update(GamePadAxisEvent axisEvent)
        {
            switch (axisEvent.Axis)
            {
            case GamePadAxis.LeftThumbX:
                return(UpdateFloat(ref LeftThumb.X, axisEvent));

            case GamePadAxis.LeftThumbY:
                return(UpdateFloat(ref LeftThumb.Y, axisEvent));

            case GamePadAxis.RightThumbX:
                return(UpdateFloat(ref RightThumb.X, axisEvent));

            case GamePadAxis.RightThumbY:
                return(UpdateFloat(ref RightThumb.Y, axisEvent));

            case GamePadAxis.LeftTrigger:
                return(UpdateFloat(ref LeftTrigger, axisEvent));

            case GamePadAxis.RightTrigger:
                return(UpdateFloat(ref RightTrigger, axisEvent));
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// Maps game controller events to gamepad events
        /// </summary>
        /// <returns>The equivalent gamepad event</returns>
        /// <param name="targetDevice">The gamepad that events are mapped to</param>
        /// <param name="sourceDevice">The game controller that is mapped to a gamepad</param>
        /// <param name="controllerEvent">The controller input event as a source</param>
        /// <param name="target">Target list</param>
        public virtual void MapInputEvent(IGamePadDevice targetDevice, IGameControllerDevice sourceDevice, InputEvent controllerEvent, List <InputEvent> target)
        {
            var buttonEvent = controllerEvent as GameControllerButtonEvent;

            if (buttonEvent != null)
            {
                if (buttonEvent.Index < buttonMap.Count &&
                    buttonMap[buttonEvent.Index] != GamePadButton.None)
                {
                    GamePadButtonEvent buttonEvent1 = InputEventPool <GamePadButtonEvent> .GetOrCreate(targetDevice);

                    buttonEvent1.Button = buttonMap[buttonEvent.Index];
                    buttonEvent1.IsDown = buttonEvent.IsDown;
                    target.Add(buttonEvent1);
                }

                if (buttonEvent.Index < buttonsToTriggerMap.Count &&
                    buttonsToTriggerMap[buttonEvent.Index].Axis != GamePadAxis.None)
                {
                    var mappedAxis = buttonsToTriggerMap[buttonEvent.Index];

                    GamePadAxisEvent axisEvent1 = InputEventPool <GamePadAxisEvent> .GetOrCreate(targetDevice);

                    axisEvent1.Axis  = mappedAxis.Axis;
                    axisEvent1.Value = buttonEvent.IsDown ? 1.0f : 0.0f;
                    if (mappedAxis.Invert)
                    {
                        axisEvent1.Value = -axisEvent1.Value;
                    }
                    target.Add(axisEvent1);
                }
            }
            else
            {
                var axisEvent = controllerEvent as GameControllerAxisEvent;
                if (axisEvent != null)
                {
                    if (axisEvent.Index < axisMap.Count &&
                        axisMap[axisEvent.Index].Axis != GamePadAxis.None)
                    {
                        var mappedAxis = axisMap[axisEvent.Index];

                        GamePadAxisEvent axisEvent1 = InputEventPool <GamePadAxisEvent> .GetOrCreate(targetDevice);

                        axisEvent1.Axis = mappedAxis.Axis;
                        if (mappedAxis.Invert)
                        {
                            axisEvent1.Value = -axisEvent.Value;
                        }
                        else
                        {
                            axisEvent1.Value = axisEvent.Value;
                        }
                        if (mappedAxis.Remap)
                        {
                            axisEvent1.Value = (axisEvent1.Value + 1.0f) * 0.5f;
                            if (axisEvent1.Value < 0.0001f)
                            {
                                axisEvent1.Value = 0.0f;
                            }
                        }

                        target.Add(axisEvent1);
                    }
                    if (axisEvent.Index < axisToButtonMap.Count &&
                        axisToButtonMap[axisEvent.Index] != GamePadButton.None)
                    {
                        GamePadButtonEvent buttonEvent1 = InputEventPool <GamePadButtonEvent> .GetOrCreate(targetDevice);

                        buttonEvent1.Button = axisToButtonMap[axisEvent.Index];
                        buttonEvent1.IsDown = axisEvent.Value > 0.5f;
                        target.Add(buttonEvent1);
                    }
                }
                else if (mapFirstPovToPad)
                {
                    var directionEvent = controllerEvent as GameControllerDirectionEvent;
                    if (directionEvent?.Index == 0)
                    {
                        GamePadButton targetButtons = GameControllerUtils.DirectionToButtons(directionEvent.Direction);

                        // Pad buttons down
                        for (int i = 0; i < 4; i++)
                        {
                            int mask = (1 << i);
                            if (((int)targetDevice.State.Buttons & mask) != ((int)targetButtons & mask))
                            {
                                GamePadButtonEvent buttonEvent1 = InputEventPool <GamePadButtonEvent> .GetOrCreate(targetDevice);

                                buttonEvent1.Button = (GamePadButton)mask;
                                buttonEvent1.IsDown = ((int)targetButtons & mask) != 0;
                                target.Add(buttonEvent1);
                            }
                        }
                    }
                }
            }
        }