Пример #1
0
        public Input ToSdlCode(InputKey key)
        {
            Input input = this[key];

            if (input == null)
            {
                return(null);
            }

            if (input.Type == "key")
            {
                return(input);
            }

            var mapping = SdlGameControllers.GetGameControllerMapping(ProductGuid);

            if (mapping == null)
            {
                return(input);
            }

            var sdlret = mapping.FirstOrDefault(m => m.Input.Type == input.Type && m.Input.Value == input.Value && m.Input.Id == input.Id);

            if (sdlret == null)
            {
                return(input);
            }

            Input ret = new Input()
            {
                Name = input.Name
            };

            if (sdlret.Button != SDL_CONTROLLER_BUTTON.INVALID)
            {
                ret.Type  = "button";
                ret.Value = 1;
                ret.Id    = (int)sdlret.Button;
                return(ret);
            }

            if (sdlret.Axis != SDL_CONTROLLER_AXIS.INVALID)
            {
                ret.Type  = "axis";
                ret.Id    = (int)sdlret.Axis;
                ret.Value = 1;
                return(ret);
            }

            return(ToXInputCodes(key));
        }
Пример #2
0
        public Input ToSdlCode(InputKey key)
        {
            Input input = this[key];

            if (input == null)
            {
                return(null);
            }

            if (input.Type == "key")
            {
                return(input);
            }

            var ctrl = SdlGameControllers.GetGameController(ProductGuid);

            if (ctrl == null)
            {
                return(input);
            }

            var mapping = ctrl.Mapping;

            var sdlret = mapping.FirstOrDefault(m => m.Input.Type == input.Type && m.Input.Value == input.Value && m.Input.Id == input.Id);

            if (sdlret == null)
            {
                if (mapping.All(m => m.Axis == SDL_CONTROLLER_AXIS.INVALID))
                {
                    switch (key)
                    {
                    case InputKey.left:
                        sdlret = mapping.FirstOrDefault(m => m.Input.Type == input.Type && m.Button == SDL_CONTROLLER_BUTTON.DPAD_LEFT);
                        break;

                    case InputKey.right:
                        sdlret = mapping.FirstOrDefault(m => m.Input.Type == input.Type && m.Button == SDL_CONTROLLER_BUTTON.DPAD_RIGHT);
                        break;

                    case InputKey.up:
                        sdlret = mapping.FirstOrDefault(m => m.Input.Type == input.Type && m.Button == SDL_CONTROLLER_BUTTON.DPAD_UP);
                        break;

                    case InputKey.down:
                        sdlret = mapping.FirstOrDefault(m => m.Input.Type == input.Type && m.Button == SDL_CONTROLLER_BUTTON.DPAD_DOWN);
                        break;
                    }
                }

                if (sdlret == null)
                {
                    SimpleLogger.Instance.Warning("ToSdlCode error can't find <input name=\"" + key.ToString() + "\" type=\"" + input.Type + "\" id=\"" + input.Id + "\" value=\"" + input.Value + "\" /> in SDL2 mapping :\r\n" + ctrl.SdlBinding);
                    return(input);
                }
            }

            Input ret = new Input()
            {
                Name = input.Name
            };

            if (sdlret.Button != SDL_CONTROLLER_BUTTON.INVALID)
            {
                ret.Type  = "button";
                ret.Value = 1;
                ret.Id    = (int)sdlret.Button;
                return(ret);
            }

            if (sdlret.Axis != SDL_CONTROLLER_AXIS.INVALID)
            {
                ret.Type  = "axis";
                ret.Id    = (int)sdlret.Axis;
                ret.Value = 1;
                return(ret);
            }

            return(ToXInputCodes(key));
        }
Пример #3
0
        static SdlGameControllers()
        {
            _controllers = new List <SdlGameControllers>();

            SDL.SDL_Init(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_InitSubSystem(SDL.SDL_INIT_JOYSTICK);

            int numJoysticks = SDL.SDL_NumJoysticks();

            for (int i = 0; i < numJoysticks; i++)
            {
                if (SDL.SDL_IsGameController(i) == SDL.SDL_bool.SDL_TRUE)
                {
                    var mappingString = SDL.SDL_GameControllerMappingForDeviceIndex(i);
                    if (mappingString == null)
                    {
                        continue;
                    }

                    string[] mapArray = mappingString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (mapArray.Length == 0)
                    {
                        continue;
                    }

                    List <SdlControllerMapping> sdlMapping = ExtractMapping(mapArray.Skip(2).ToArray());

                    SdlGameControllers ctl = new SdlGameControllers();

                    ctl.Guid       = SDL.SDL_JoystickGetDeviceGUID(i);
                    ctl.VendorId   = int.Parse((mapArray[0].Substring(10, 2) + mapArray[0].Substring(8, 2)).ToUpper(), System.Globalization.NumberStyles.HexNumber);
                    ctl.ProductId  = int.Parse((mapArray[0].Substring(18, 2) + mapArray[0].Substring(16, 2)).ToUpper(), System.Globalization.NumberStyles.HexNumber);
                    ctl.Name       = SDL.SDL_GameControllerNameForIndex(i);
                    ctl.Mapping    = sdlMapping;
                    ctl.SdlBinding = mappingString;

                    if (!_controllers.Any(c => c.Guid == ctl.Guid))
                    {
                        _controllers.Add(ctl);
                    }
                }
            }

            // Add all other mappings ( Debug without physical controller )
            for (int i = 0; i < SDL.SDL_GameControllerNumMappings(); i++)
            {
                var mappingString = SDL.SDL_GameControllerMappingForIndex(i);

                string[] mapArray = mappingString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                if (mapArray.Length == 0)
                {
                    continue;
                }

                SdlGameControllers ctl = new SdlGameControllers();
                ctl.Guid       = InputConfig.FromEmulationStationGuidString(mapArray[0]);
                ctl.VendorId   = int.Parse((mapArray[0].Substring(10, 2) + mapArray[0].Substring(8, 2)).ToUpper(), System.Globalization.NumberStyles.HexNumber);
                ctl.ProductId  = int.Parse((mapArray[0].Substring(18, 2) + mapArray[0].Substring(16, 2)).ToUpper(), System.Globalization.NumberStyles.HexNumber);
                ctl.Name       = mapArray[1];
                ctl.Mapping    = ExtractMapping(mapArray.Skip(2).ToArray());
                ctl.SdlBinding = mappingString;
                _controllers.Add(ctl);
            }


            SDL.SDL_QuitSubSystem(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_Quit();
        }
        static SdlGameControllers()
        {
            _controllers = new List <SdlGameControllers>();

            SDL.SDL_Init(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_InitSubSystem(SDL.SDL_INIT_JOYSTICK);

            int numJoysticks = SDL.SDL_NumJoysticks();

            for (int i = 0; i < numJoysticks; i++)
            {
                if (SDL.SDL_IsGameController(i) == SDL.SDL_bool.SDL_TRUE)
                {
                    var mappingString = SDL.SDL_GameControllerMappingForDeviceIndex(i);
                    if (mappingString == null)
                    {
                        continue;
                    }

                    string[] mapArray = mappingString.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Skip(2).ToArray();
                    if (mapArray.Length == 0)
                    {
                        continue;
                    }

                    List <SdlControllerMapping> sdlMapping = new List <SdlControllerMapping>();

                    foreach (var tt in mapArray)
                    {
                        var map = tt.Split(new char[] { ':' });
                        if (map.Length == 2)
                        {
                            SdlControllerMapping sm = new SdlControllerMapping();

                            char half_axis_output = ' ';

                            string name = map[0];
                            if (name.Length > 0 && (name[0] == '+' || name[0] == '-'))
                            {
                                half_axis_output = name[0];
                                name             = name.Substring(1);
                            }

                            if (half_axis_output == '+')
                            {
                                sm.AxisMin = 0;
                                sm.AxisMax = 32767;
                            }
                            else if (half_axis_output == '-')
                            {
                                sm.AxisMin = 0;
                                sm.AxisMax = -32768;
                            }
                            else
                            {
                                sm.AxisMin = -32768;
                                sm.AxisMax = 32767;
                            }

                            sm.Button = SDL_CONTROLLER_BUTTON.INVALID;
                            sm.Axis   = SDL_CONTROLLER_AXIS.INVALID;

                            switch (map[0])
                            {
                            // Buttons
                            case "a": sm.Button = SDL_CONTROLLER_BUTTON.A; break;

                            case "b": sm.Button = SDL_CONTROLLER_BUTTON.B; break;

                            case "back": sm.Button = SDL_CONTROLLER_BUTTON.BACK; break;

                            case "dpdown": sm.Button = SDL_CONTROLLER_BUTTON.DPAD_DOWN; break;

                            case "dpleft": sm.Button = SDL_CONTROLLER_BUTTON.DPAD_LEFT; break;

                            case "dpright": sm.Button = SDL_CONTROLLER_BUTTON.DPAD_RIGHT; break;

                            case "dpup": sm.Button = SDL_CONTROLLER_BUTTON.DPAD_UP; break;

                            case "leftshoulder": sm.Button = SDL_CONTROLLER_BUTTON.LEFTSHOULDER; break;

                            case "rightstick": sm.Button = SDL_CONTROLLER_BUTTON.RIGHTSTICK; break;

                            case "leftstick": sm.Button = SDL_CONTROLLER_BUTTON.LEFTSTICK; break;

                            case "rightshoulder": sm.Button = SDL_CONTROLLER_BUTTON.RIGHTSHOULDER; break;

                            case "start": sm.Button = SDL_CONTROLLER_BUTTON.START; break;

                            case "x": sm.Button = SDL_CONTROLLER_BUTTON.X; break;

                            case "y": sm.Button = SDL_CONTROLLER_BUTTON.Y; break;

                            case "guide": sm.Button = SDL_CONTROLLER_BUTTON.GUIDE; break;

                            // Axis
                            case "lefttrigger": sm.Axis = SDL_CONTROLLER_AXIS.TRIGGERLEFT; break;

                            case "righttrigger": sm.Axis = SDL_CONTROLLER_AXIS.TRIGGERRIGHT; break;

                            case "leftx": sm.Axis = SDL_CONTROLLER_AXIS.LEFTX; break;

                            case "lefty": sm.Axis = SDL_CONTROLLER_AXIS.LEFTY; break;

                            case "rightx": sm.Axis = SDL_CONTROLLER_AXIS.RIGHTX; break;

                            case "righty": sm.Axis = SDL_CONTROLLER_AXIS.RIGHTY; break;

                            default:
                                continue;
                            }

                            // 030000007d0400000840000000000000,Destroyer Tiltpad,+leftx:h0.2,+lefty:h0.4,-leftx:h0.8,-lefty:h0.1,a:b1,b:b2,dpdown:+a1,dpleft:-a0,dpright:+a0,dpup:-a1,leftshoulder:b4,rightshoulder:b5,x:b0,y:b3,platform:Windows,

                            Input cfg = new Input();

                            if (map[1].StartsWith("b"))
                            {
                                cfg.Type  = "button";
                                cfg.Id    = map[1].Substring(1).ToInteger();
                                cfg.Value = 1;
                            }
                            else if (map[1].StartsWith("a"))
                            {
                                cfg.Type  = "axis";
                                cfg.Id    = map[1].Substring(1).ToInteger();
                                cfg.Value = 1;
                            }
                            else if (map[1].StartsWith("+a"))
                            {
                                cfg.Type  = "axis";
                                cfg.Id    = map[1].Substring(2).ToInteger();
                                cfg.Value = 1;
                            }
                            else if (map[1].StartsWith("-a"))
                            {
                                cfg.Type  = "axis";
                                cfg.Id    = map[1].Substring(2).ToInteger();
                                cfg.Value = -1;
                            }
                            else if (map[1].StartsWith("h")) // h0.4
                            {
                                var hatIds = map[1].Substring(1).Split(new char[] { '.' }).Select(v => v.ToInteger()).ToArray();
                                if (hatIds.Length > 1)
                                {
                                    cfg.Type  = "hat";
                                    cfg.Id    = hatIds[0];
                                    cfg.Value = hatIds[1];
                                }
                            }
                            else
                            {
                                continue;
                            }

                            sm.Input = cfg;
                            sdlMapping.Add(sm);
                        }
                    }

                    SdlGameControllers ctl = new SdlGameControllers();

                    ctl.Guid    = SDL.SDL_JoystickGetDeviceGUID(i);
                    ctl.Name    = SDL.SDL_GameControllerNameForIndex(i);
                    ctl.Mapping = sdlMapping;

                    _controllers.Add(ctl);
                }
            }

            SDL.SDL_QuitSubSystem(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_Quit();
        }