public Button AddButtonCommand(string name, Device device = null, Device.Control control = null)
        {
            Command cmd = Commands.Find(x => x.FunctionName == name);

            if (cmd == null)
            {
                cmd = new Button();
                cmd.FunctionName = name;

                Commands.Add(cmd);
            }

            if (cmd as Button != null)
            {
                Button.ButtonInput button = new Button.ButtonInput();
                BindInput(cmd, button, device, control);
            }

            return(cmd as Button);
        }
        public Axis AddAxisCommand(string name, Device device, Device.Control posControl, Device.Control negControl)
        {
            Command cmd = Commands.Find(x => x.FunctionName == name);

            if (cmd == null)
            {
                cmd = new Axis();
                cmd.FunctionName = name;

                Commands.Add(cmd);
            }

            if (cmd as Axis != null)
            {
                Axis.AxisInput axis = new Axis.AxisInput();
                axis.NegativeButton = negControl;
                BindInput(cmd, axis, device, posControl);
            }

            return(cmd as Axis);
        }
        public Axis AddAxisCommand(string name, Device device = null, Device.Control control = null, AxisSignTypes sign = AxisSignTypes.Normal, InverseTypes invert = InverseTypes.Normal, NegationTypes negate = NegationTypes.Normal)
        {
            Command cmd = Commands.Find(x => x.FunctionName == name);

            if (cmd == null)
            {
                cmd = new Axis();
                cmd.FunctionName = name;

                Commands.Add(cmd);
            }

            if (cmd as Axis != null)
            {
                Axis.AxisInput axis = new Axis.AxisInput();
                BindInput(cmd, axis, device, control);

                axis.Sign      = sign;
                axis.Negation  = negate;
                axis.Inversion = (sign == AxisSignTypes.Unsigned) ? invert : InverseTypes.Normal;
            }

            return(cmd as Axis);
        }
        private static void BindInput(Command cmd, Command.Input input, Device device = null, Device.Control control = null)
        {
            cmd.Inputs.Add(input);

            if (device != null && control != null)
            {
                input.DeviceGUID = device.GUID;
                input.DeviceName = device.DeviceName;

                input.BoundInputName = control.Name;

                if (control as Joystick.Axis != null)
                {
                    input.BoundInput = Command.Input.BoundInputTypes.Axis;
                }
                else if (control as Joystick.Button != null)
                {
                    input.BoundInput = Command.Input.BoundInputTypes.Button;
                }
                else if (control as Joystick.Hat != null)
                {
                    input.BoundInput = Command.Input.BoundInputTypes.Hat;
                }
                else if (device as Keyboard != null)
                {
                    input.BoundInput = Command.Input.BoundInputTypes.Key;
                }
            }

            input.Command = cmd;
        }
                internal override void Setup()
                {
                    if (BoundDevice == null)
                    {
                        return;
                    }

                    if (BoundInput == BoundInputTypes.Button)
                    {
                        Joystick stick = BoundDevice as Joystick;
                        if (stick == null)
                        {
                            return;
                        }

                        foreach (var button in stick.Buttons)
                        {
                            if (button.Name.ToLowerInvariant() == BoundInputName.ToLowerInvariant())
                            {
                                ActualControl = button;
                                break;
                            }
                        }
                    }
                    else if (BoundInput == BoundInputTypes.Hat)
                    {
                        Joystick stick = BoundDevice as Joystick;
                        if (stick == null)
                        {
                            return;
                        }

                        string[] parts = BoundInputName.ToLowerInvariant().Split(":".ToCharArray());
                        string   name  = parts[0];
                        TriggerValue = 0.5;
                        if (parts.Length > 1)
                        {
                            double.TryParse(parts[1], out TriggerValue);
                        }

                        foreach (var hat in stick.Hats)
                        {
                            if (hat.Name.ToLowerInvariant() == name)
                            {
                                ActualControl = hat;
                                break;
                            }
                        }
                    }
                    else if (BoundInput == BoundInputTypes.Axis)
                    {
                        Joystick stick = BoundDevice as Joystick;
                        if (stick == null)
                        {
                            return;
                        }

                        string[] parts = BoundInputName.ToLowerInvariant().Split(":".ToCharArray());
                        string   name  = parts[0];
                        TriggerValue = 1;
                        if (parts.Length > 1)
                        {
                            double.TryParse(parts[1], out TriggerValue);
                        }

                        foreach (var axis in stick.Axes)
                        {
                            if (axis.Name.ToLowerInvariant() == name)
                            {
                                ActualControl = axis;
                                break;
                            }
                        }
                    }
                    else if (BoundInput == BoundInputTypes.Key)
                    {
                        Keyboard board = BoundDevice as Keyboard;
                        if (board == null)
                        {
                            return;
                        }

                        Keyboard.KeyCodes code;
                        if (Enum.TryParse(BoundInputName, out code))
                        {
                            BoundIndex = (int)code;
                        }
                    }
                }
                internal override void Setup()
                {
                    if (BoundDevice == null)
                    {
                        return;
                    }

                    if (BoundInput == BoundInputTypes.Axis)
                    {
                        Joystick stick = BoundDevice as Joystick;
                        if (stick == null)
                        {
                            return;
                        }

                        foreach (var axis in stick.Axes)
                        {
                            if (axis.Name.ToLowerInvariant() == BoundInputName.ToLowerInvariant())
                            {
                                BoundAxis = axis;
                                break;
                            }
                        }
                    }
                    else if (BoundInput == BoundInputTypes.Button || BoundInput == BoundInputTypes.Key)
                    {
                        string[] parts = BoundInputName.Split(":".ToCharArray());
                        if (BoundInput == BoundInputTypes.Key)
                        {
                            Keyboard board = BoundDevice as Keyboard;
                            if (board == null)
                            {
                                return;
                            }

                            PositiveButton = board.Keys;
                            PositiveIndex  = 0;
                            if (parts[0] != string.Empty)
                            {
                                Keyboard.KeyCodes code;
                                if (Enum.TryParse(parts[0], out code))
                                {
                                    PositiveIndex = (int)code;
                                }
                            }

                            if (parts.Length > 1 || parts[1] != string.Empty)
                            {
                                NegativeButton = board.Keys;
                                Keyboard.KeyCodes code;
                                if (Enum.TryParse(parts[0], out code))
                                {
                                    NegativeIndex = (int)code;
                                }
                            }
                        }
                        else
                        {
                            Joystick stick = BoundDevice as Joystick;
                            if (stick == null)
                            {
                                return;
                            }

                            foreach (var button in stick.Buttons)
                            {
                                if (button.Name.ToLowerInvariant() == parts[0].ToLowerInvariant())
                                {
                                    PositiveButton = button;
                                }
                                else if (parts.Length > 1 && button.Name.ToLowerInvariant() == parts[1].ToLowerInvariant())
                                {
                                    NegativeButton = button;
                                }
                            }
                        }
                    }
                    else if (BoundInput == BoundInputTypes.Hat)
                    {
                        Joystick stick = BoundDevice as Joystick;
                        if (stick == null)
                        {
                            return;
                        }

                        string[] parts = BoundInputName.Split(":".ToCharArray());
                        HatVertAxis = parts.Length > 1 && parts[1].ToUpper() == "V";
                        string name = parts[0].ToLowerInvariant();

                        foreach (var hat in stick.Hats)
                        {
                            if (hat.Name.ToLowerInvariant() == name)
                            {
                                PositiveButton = hat;
                                break;
                            }
                        }
                    }
                }
        private static void ControlMapSetter_ControllChanged(object sender, Device.Control e)
        {
            if (Done || CommandToTest == null)
            {
                return;
            }

            Joystick stick = sender as Joystick;

            if (stick == null)
            {
                return;
            }

            ControlMapping.Command.Input iInput = null;

            if (CommandToTest as ControlMapping.Axis != null)
            {
                ControlMapping.Axis.AxisInput aInput = new ControlMapping.Axis.AxisInput();

                if (InputToUpdate != null && InputToUpdate as ControlMapping.Axis.AxisInput != null)
                {
                    aInput = InputToUpdate as ControlMapping.Axis.AxisInput;
                }

                if (e as Joystick.Axis != null)
                {
                    aInput.DeviceName     = stick.DeviceName;
                    aInput.DeviceGUID     = stick.GUID;
                    aInput.BoundInputName = e.Name;
                }
                else if (e as Joystick.Button != null)
                {
                    aInput.DeviceName = stick.DeviceName;
                    aInput.DeviceGUID = stick.GUID;

                    if (SetPositiveKeyAxis)
                    {
                        if (aInput.BoundInputName.Contains(":"))
                        {
                            aInput.BoundInputName = e.Name + ":" + aInput.BoundInputName.Substring(aInput.BoundInputName.IndexOf(':') + 1);
                        }
                        else
                        {
                            aInput.BoundInputName = e.Name;
                        }
                    }
                    else
                    {
                        if (aInput.BoundInputName.Contains(":"))
                        {
                            aInput.BoundInputName = aInput.BoundInputName.Substring(0, aInput.BoundInputName.IndexOf(':') - 1);
                        }
                        aInput.BoundInputName = aInput.BoundInputName + ":" + e.Name;
                    }
                }
                else if (e as Joystick.Hat != null)
                {
                    aInput.DeviceName     = stick.DeviceName;
                    aInput.DeviceGUID     = stick.GUID;
                    aInput.BoundInputName = e.Name;
                    Joystick.Hat hat = e as Joystick.Hat;

                    if (hat.OrdinalIsPressed(Joystick.Hat.Ordinals.North) || hat.OrdinalIsPressed(Joystick.Hat.Ordinals.South))
                    {
                        aInput.BoundInputName += ":V";
                    }
                    else
                    {
                        aInput.BoundInputName += ":H";
                    }
                }

                iInput = aInput;
            }
            else if (CommandToTest as ControlMapping.Button != null)
            {
                ControlMapping.Button.ButtonInput bInput = new ControlMapping.Button.ButtonInput();

                if (InputToUpdate != null && InputToUpdate as ControlMapping.Button.ButtonInput != null)
                {
                    bInput = InputToUpdate as ControlMapping.Button.ButtonInput;
                }

                bInput.DeviceName     = stick.DeviceName;
                bInput.DeviceGUID     = stick.GUID;
                bInput.BoundInputName = e.Name;
                bInput.TriggerValue   = e.GetValue(0) > 0 ? 1 : -1;

                if (e as Joystick.Hat != null)
                {
                    bInput.TriggerValue = e.GetValue(0);
                }

                iInput = bInput;
            }

            if (AddInput && iInput != null)
            {
                CommandToTest.Inputs.Add(iInput);
            }

            Done = true;
        }