Exemplo n.º 1
0
        private void OpenDevice(int deviceIndex)
        {
            var joystickId   = SDL.SDL_JoystickGetDeviceGUID(deviceIndex);
            var joystickName = SDL.SDL_JoystickNameForIndex(deviceIndex);

            if (joystickInstanceIdToDeviceId.ContainsKey(GetJoystickInstanceId(deviceIndex)))
            {
                throw new InvalidOperationException($"SDL GameController already opened {deviceIndex}/{joystickId}/{joystickName}");
            }

            var controller = new GameControllerSDL(this, deviceIndex);

            IInputDevice resultingDevice = controller;

            // Find gamepad layout
            var layout = GamePadLayouts.FindLayout(this, controller);

            if (layout != null)
            {
                // Create a gamepad wrapping around the controller
                var gamePad = new GamePadSDL(this, inputManager, controller, layout);
                resultingDevice = gamePad; // Register gamepad instead
            }

            controller.Disconnected += (sender, args) =>
            {
                // Queue device for removal
                devicesToRemove.Add(resultingDevice.Id);
                joystickInstanceIdToDeviceId.Remove(controller.InstanceId);
            };

            RegisterDevice(resultingDevice);
            joystickInstanceIdToDeviceId.Add(controller.InstanceId, resultingDevice.Id);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens a new game controller or gamepad
        /// </summary>
        /// <param name="deviceInstance">The device instance</param>
        public void OpenDevice(DeviceInstance deviceInstance)
        {
            // Ignore XInput devices since they are handled by XInput
            if (XInputChecker.IsXInputDevice(ref deviceInstance.ProductGuid))
            {
                return;
            }

            if (Devices.ContainsKey(deviceInstance.InstanceGuid))
            {
                throw new InvalidOperationException($"DirectInput GameController already opened {deviceInstance.InstanceGuid}/{deviceInstance.InstanceName}");
            }


            GameControllerDirectInput controller;

            try
            {
                controller = new GameControllerDirectInput(this, directInput, deviceInstance);
            }
            catch (SharpDXException)
            {
                // Some failure occured during device creation
                return;
            }

            // Find gamepad layout
            var layout = GamePadLayouts.FindLayout(this, controller);

            if (layout != null)
            {
                // Creata a gamepad wrapping around the controller
                var gamePad = new GamePadDirectInput(this, inputManager, controller, layout);
                controller.Disconnected += (sender, args) =>
                {
                    // Queue device for removal
                    devicesToRemove.Add(gamePad.Id);
                };
                RegisterDevice(gamePad); // Register gamepad instead
            }
            else
            {
                controller.Disconnected += (sender, args) =>
                {
                    // Queue device for removal
                    devicesToRemove.Add(controller.Id);
                };
                RegisterDevice(controller);
            }
        }