Ejemplo n.º 1
0
 /// <summary>
 /// Finds a layout matching the given gamepad
 /// </summary>
 /// <param name="source">The source that the <paramref name="device"/> came from</param>
 /// <param name="device">The device to find a layout for</param>
 /// <returns>The gamepad layout that was found, or null if none was found</returns>
 public static GamePadLayout FindLayout(IInputSource source, IGameControllerDevice device)
 {
     lock (layouts)
     {
         foreach (var layout in layouts)
         {
             if (layout.MatchDevice(source, device))
             {
                 return(layout);
             }
         }
         return(null);
     }
 }
Ejemplo n.º 2
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);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GamePadFromLayout"/> class.
 /// </summary>
 protected GamePadFromLayout(InputManager inputManager, IGameControllerDevice controller, GamePadLayout layout)
 {
     InputManager         = inputManager;
     Layout               = layout;
     GameControllerDevice = controller;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Checks if a device matches this gamepad layout, and thus should use this when mapping it to a <see cref="GamePadState"/>
 /// </summary>
 /// <param name="source">Source that this device comes from</param>
 /// <param name="device">The device to match</param>
 public abstract bool MatchDevice(IInputSource source, IGameControllerDevice device);
Ejemplo n.º 5
0
 /// <summary>
 /// Allows the user to perform some additional setup operations when using this layout on a device
 /// </summary>
 /// <param name="targetDevice">The gamepad that events are mapped to</param>
 /// <param name="sourceDevice">The game controller that is mapped to a gamepad</param>
 public virtual void InitializeDevice(IGamePadDevice targetDevice, IGameControllerDevice sourceDevice)
 {
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns the value of a direction controller converted to a <see cref="GamePadButton"/> which has the matching Pad flags set
        /// </summary>
        /// <param name="device">The gamepad</param>
        /// <param name="index">The index of the direction controller</param>
        /// <returns></returns>
        public static GamePadButton GetDPad(this IGameControllerDevice device, int index)
        {
            var dir = device.GetDirection(index);

            return(dir.IsNeutral ? GamePadButton.None : GameControllerUtils.DirectionToButtons(dir));
        }
Ejemplo n.º 7
0
 public override bool MatchDevice(IInputSource source, IGameControllerDevice device)
 {
     return(CompareProductId(device.ProductId, commonProductId, 4));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Gets the number of direction inputs on this gamepad
 /// </summary>
 /// <param name="device">The gamepad</param>
 /// <returns>The number of direction controllers</returns>
 public static int GetDirectionCount(this IGameControllerDevice device)
 {
     return(device.DirectionInfos.Count);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Gets the number of axes on this gamepad
 /// </summary>
 /// <param name="device">The gamepad</param>
 /// <returns>The number of axes</returns>
 public static int GetAxisCount(this IGameControllerDevice device)
 {
     return(device.AxisInfos.Count);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets the number of buttons on this gamepad
 /// </summary>
 /// <param name="device">The gamepad</param>
 /// <returns>The number of buttons</returns>
 public static int GetButtonCount(this IGameControllerDevice device)
 {
     return(device.ButtonInfos.Count);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Determines whether the specified button is being pressed down
 /// </summary>
 /// /// <param name="controller">The controller</param>
 /// <param name="button">The button</param>
 /// <returns><c>true</c> if the specified button is being pressed down; otherwise, <c>false</c>.</returns>
 public static bool IsButtonDown(this IGameControllerDevice controller, int button)
 {
     return(controller.DownButtons.Contains(button));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Determines whether the specified button is released since the previous update.
 /// </summary>
 /// /// <param name="controller">The controller</param>
 /// <param name="button">The button</param>
 /// <returns><c>true</c> if the specified button is released; otherwise, <c>false</c>.</returns>
 public static bool IsButtonReleased(this IGameControllerDevice controller, int button)
 {
     return(controller.ReleasedButtons.Contains(button));
 }