Ejemplo n.º 1
0
        private void AddJoystick(JoyInputs joysticks, int i)
        {
            IntPtr joy   = SDL.SDL_JoystickOpen(i);
            var    guid  = SDL.SDL_JoystickGetGUID(joy);
            var    guid2 = SDL.SDL_JoystickGetDeviceGUID(i);
            var    name  = SDL.SDL_JoystickName(joy);

            var conf = _inputList.FirstOrDefault(cfg => cfg.ProductGuid == guid);

            if (conf == null)
            {
                conf = _inputList.FirstOrDefault(cfg => cfg.DeviceName == name);
            }

            if (conf != null)
            {
                joysticks.Add(new JoyInput(SDL.SDL_JoystickInstanceID(joy), joy, conf));
            }
        }
Ejemplo n.º 2
0
    private void LoadGamePad()
    {
        switch (SelectedGamePad)
        {
        case GamePad.Gamepad1:
            joyInputs = padManager.Gamepad1;
            break;

        case GamePad.Gamepad2:
            joyInputs = padManager.Gamepad2;
            break;

        case GamePad.Gamepad3:
            joyInputs = padManager.Gamepad3;
            break;

        case GamePad.Gamepad4:
            joyInputs = padManager.Gamepad4;
            break;
        }
    }
Ejemplo n.º 3
0
    private void Start()
    {
        padManager = FindObjectOfType <GamePadManager>();
        switch (SelectedGamePad)
        {
        case GamePad.Gamepad1:
            joy = padManager.Gamepad1;
            break;

        case GamePad.Gamepad2:
            joy = padManager.Gamepad2;
            break;

        case GamePad.Gamepad3:
            joy = padManager.Gamepad3;
            break;

        case GamePad.Gamepad4:
            joy = padManager.Gamepad4;
            break;
        }
    }
Ejemplo n.º 4
0
    private void LoadJoyData()
    {
        switch (SelectedGamePad)
        {
        case GamePad.Gamepad1:
            joy             = padManager.Gamepad1;
            joyDefault      = padManager.Gamepad1Default;
            selectedGamepad = 1;
            break;

        case GamePad.Gamepad2:
            joy             = padManager.Gamepad2;
            joyDefault      = padManager.Gamepad2Default;
            selectedGamepad = 2;
            break;

        case GamePad.Gamepad3:
            joy             = padManager.Gamepad3;
            joyDefault      = padManager.Gamepad3Default;
            selectedGamepad = 3;
            break;

        case GamePad.Gamepad4:
            joy             = padManager.Gamepad4;
            joyDefault      = padManager.Gamepad4Default;
            selectedGamepad = 4;
            break;
        }

        XAxis.isOn  = joy.xAxisInvert;
        YAxis.isOn  = joy.yAxisInvert;
        XAxis2.isOn = joy._3thAxisInvert;
        YAxis2.isOn = joy._5thAxisInvert;


        FindObjectOfType <GamePadInputTest>().joy = joy;
        loadComplete = true;
    }
Ejemplo n.º 5
0
    private void ResetButtons()
    {
        Gamepad1Default = new JoyInputs
        {
            button0Key  = "joystick 1 button 0",
            button1Key  = "joystick 1 button 1",
            button2Key  = "joystick 1 button 2",
            button3Key  = "joystick 1 button 3",
            button4Key  = "joystick 1 button 5",
            button5Key  = "joystick 1 button 7",
            button6Key  = "joystick 1 button 4",
            button7Key  = "joystick 1 button 6",
            xAxisKey    = "XAxis1",
            yAxisKey    = "YAxis1",
            _3thAxisKey = "3rdAxis1",
            _5thAxisKey = "4rdAxis1"
        };

        Gamepad2Default = new JoyInputs
        {
            button0Key  = "joystick 2 button 0",
            button1Key  = "joystick 2 button 1",
            button2Key  = "joystick 2 button 2",
            button3Key  = "joystick 2 button 3",
            button4Key  = "joystick 2 button 5",
            button5Key  = "joystick 2 button 7",
            button6Key  = "joystick 2 button 4",
            button7Key  = "joystick 2 button 6",
            xAxisKey    = "XAxis2",
            yAxisKey    = "YAxis2",
            _3thAxisKey = "3rdAxis2",
            _5thAxisKey = "4rdAxis2"
        };

        Gamepad3Default = new JoyInputs
        {
            button0Key  = "joystick 3 button 0",
            button1Key  = "joystick 3 button 1",
            button2Key  = "joystick 3 button 2",
            button3Key  = "joystick 3 button 3",
            button4Key  = "joystick 3 button 5",
            button5Key  = "joystick 3 button 7",
            button6Key  = "joystick 3 button 4",
            button7Key  = "joystick 3 button 6",
            xAxisKey    = "XAxis3",
            yAxisKey    = "YAxis3",
            _3thAxisKey = "3rdAxis3",
            _5thAxisKey = "4rdAxis3"
        };

        Gamepad4Default = new JoyInputs
        {
            button0Key  = "joystick 4 button 0",
            button1Key  = "joystick 4 button 1",
            button2Key  = "joystick 4 button 2",
            button3Key  = "joystick 4 button 3",
            button4Key  = "joystick 4 button 5",
            button5Key  = "joystick 4 button 7",
            button6Key  = "joystick 4 button 4",
            button7Key  = "joystick 4 button 6",
            xAxisKey    = "XAxis4",
            yAxisKey    = "YAxis4",
            _3thAxisKey = "3rdAxis4",
            _5thAxisKey = "4rdAxis4"
        };

        // joy1
        Gamepad1 = SaveSystem.Load <JoyInputs>("Joy1Data");
        if (Gamepad1 is null)
        {
            Debug.Log("First Run");
            Gamepad1 = new JoyInputs();
            //firstRun = true;
            // setting up first value

            Gamepad1 = Mu.Tools.Utilities.Utilities.DeepClone(Gamepad1Default);
            SaveSystem.Save(Gamepad1, "Joy1Data");
            Debug.Log("save gamepad 1 data");
        }

        // joy2
        Gamepad2 = SaveSystem.Load <JoyInputs>("Joy2Data");
        if (Gamepad2 is null)
        {
            Gamepad2 = new JoyInputs();
            //firstRun = true;
            // setting up first value
            Gamepad2 = Mu.Tools.Utilities.Utilities.DeepClone(Gamepad2Default);
            SaveSystem.Save(Gamepad2, "Joy2Data");
            Debug.Log("save gamepad 2 data");
        }

        // joy3
        Gamepad3 = SaveSystem.Load <JoyInputs>("Joy3Data");
        if (Gamepad3 is null)
        {
            Debug.Log("First Run");
            Gamepad3 = new JoyInputs();
            //firstRun = true;
            // setting up first value
            Gamepad3 = Mu.Tools.Utilities.Utilities.DeepClone(Gamepad3Default);
            SaveSystem.Save(Gamepad3, "Joy3Data");
        }

        //joy4
        Gamepad4 = SaveSystem.Load <JoyInputs>("Joy4Data");
        if (Gamepad4 is null)
        {
            Gamepad4 = new JoyInputs();
            //firstRun = true;
            // setting up first value
            Gamepad4 = Mu.Tools.Utilities.Utilities.DeepClone(Gamepad4Default);
            SaveSystem.Save(Gamepad4, "Joy4Data");
        }
    }
Ejemplo n.º 6
0
        public void DoWork()
        {
            JoyInputs joysticks = new JoyInputs();

            SDL.SDL_SetHint(SDL.SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
            SDL.SDL_Init(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_InitSubSystem(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_JoystickEventState(1);

            int numJoysticks = SDL.SDL_NumJoysticks();

            for (int i = 0; i < numJoysticks; i++)
            {
                AddJoystick(joysticks, i);
            }

            InputKeyInfo oldIState = new InputKeyInfo();
            InputKeyInfo istate    = new InputKeyInfo();

            while (true)
            {
                if (_waitHandle.WaitOne(1))
                {
                    break;
                }

                try
                {
                    SDL.SDL_Event evt;
                    if (SDL.SDL_PollEvent(out evt) != 0)
                    {
                        if (evt.type == SDL.SDL_EventType.SDL_QUIT)
                        {
                            break;
                        }

                        switch (evt.type)
                        {
                        case SDL.SDL_EventType.SDL_JOYAXISMOTION:
                        {
                            const int DEADZONE     = 500;
                            int       initialValue = 0;

                            int normValue = 0;

                            if (Math.Abs(evt.jaxis.axisValue - initialValue) > DEADZONE)
                            {
                                if (evt.jaxis.axisValue - initialValue > 0)
                                {
                                    normValue = 1;
                                }
                                else
                                {
                                    normValue = -1;
                                }
                            }

                            var axis = joysticks.FindInputMapping(evt.jaxis.which, "axis", evt.jaxis.axis);
                            if (axis != null)
                            {
                                var axisName     = axis.Name;
                                var revertedAxis = RevertedAxis(axisName);
                                int value        = evt.jaxis.axisValue;

                                if (value != 0 && (Math.Abs(value) / value) == -axis.Value)
                                {
                                    if (revertedAxis != axisName)
                                    {
                                        axisName     = revertedAxis;
                                        revertedAxis = axis.Name;
                                        value        = -value;
                                    }
                                    else
                                    {
                                        normValue = 0;
                                        value     = 0;
                                    }
                                }

                                if (normValue != 0)
                                {
                                    istate.Remove(revertedAxis);
                                    istate.Add(axisName, value, true);
                                }
                                else
                                {
                                    istate.Remove(revertedAxis);
                                    istate.Remove(axisName);
                                }
                            }
                        }
                        break;

                        case SDL.SDL_EventType.SDL_JOYBUTTONDOWN:
                        case SDL.SDL_EventType.SDL_JOYBUTTONUP:
                        {
                            var js = joysticks.FirstOrDefault(j => j.Id == evt.jbutton.which);
                            if (js != null)
                            {
                                foreach (var conf in js.Config.Input.Where(i => i.Type == "button" && i.Id == evt.jbutton.button))
                                {
                                    if (evt.jbutton.state == SDL.SDL_PRESSED)
                                    {
                                        istate.Add(conf.Name);
                                    }
                                    else
                                    {
                                        istate.Remove(conf.Name);
                                    }
                                }
                            }
                        }
                        break;

                        case SDL.SDL_EventType.SDL_JOYHATMOTION:
                        {
                            var js = joysticks.FirstOrDefault(j => j.Id == evt.jhat.which);
                            if (js != null)
                            {
                                var up = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_UP);
                                if (up != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_UP) == SDL.SDL_HAT_UP)
                                    {
                                        istate.Add(up.Name);
                                    }
                                    else
                                    {
                                        istate.Remove(up.Name);
                                    }
                                }

                                var right = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_RIGHT);
                                if (right != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_RIGHT) == SDL.SDL_HAT_RIGHT)
                                    {
                                        istate.Add(right.Name);
                                    }
                                    else
                                    {
                                        istate.Remove(right.Name);
                                    }
                                }

                                var down = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_DOWN);
                                if (down != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_DOWN) == SDL.SDL_HAT_DOWN)
                                    {
                                        istate.Add(down.Name);
                                    }
                                    else
                                    {
                                        istate.Remove(down.Name);
                                    }
                                }

                                var left = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_LEFT);
                                if (left != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_LEFT) == SDL.SDL_HAT_LEFT)
                                    {
                                        istate.Add(left.Name);
                                    }
                                    else
                                    {
                                        istate.Remove(left.Name);
                                    }
                                }
                            }
                        }
                        break;

                        case SDL.SDL_EventType.SDL_JOYDEVICEREMOVED:
                        {
                            var js = joysticks.FirstOrDefault(j => j.Id == evt.jdevice.which);
                            if (js != null)
                            {
                                SDL.SDL_JoystickClose(js.SdlJoystick);
                                joysticks.Remove(js);
                            }
                        }

                        break;

                        case SDL.SDL_EventType.SDL_JOYDEVICEADDED:
                            if (!joysticks.Any(j => j.Id == evt.jdevice.which))
                            {
                                AddJoystick(joysticks, evt.jdevice.which);
                            }
                            break;
                        }

                        if (istate != oldIState)
                        {
                            Debug.WriteLine("State : " + istate.ToString() + " - OldState : " + oldIState.ToString());

                            //istate.HasNewInput(InputKey.leftanalogleft, oldIState, true);

                            ProcessJoystickState(istate, oldIState);

                            oldIState = istate.Clone();
                        }

                        Thread.Sleep(1);
                    }
                }
                catch { }
            }

            foreach (var joy in joysticks)
            {
                SDL.SDL_JoystickClose(joy.SdlJoystick);
            }

            SDL.SDL_QuitSubSystem(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_Quit();
        }
Ejemplo n.º 7
0
        public void DoWork()
        {
            JoyInputs joysticks = new JoyInputs();

            SDL.SDL_SetHint(SDL.SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
            SDL.SDL_Init(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_InitSubSystem(SDL.SDL_INIT_JOYSTICK);
            SDL.SDL_JoystickEventState(1);

            int numJoysticks = SDL.SDL_NumJoysticks();

            for (int i = 0; i < numJoysticks; i++)
            {
                AddJoystick(joysticks, i);
            }

            InputKey oldState = (InputKey)0;
            InputKey state    = (InputKey)0;

            while (true)
            {
                if (_waitHandle.WaitOne(1))
                {
                    break;
                }

                try
                {
                    SDL.SDL_Event evt;
                    if (SDL.SDL_PollEvent(out evt) != 0)
                    {
                        if (evt.type == SDL.SDL_EventType.SDL_QUIT)
                        {
                            break;
                        }

                        switch (evt.type)
                        {
                        case SDL.SDL_EventType.SDL_JOYAXISMOTION:
                        {
                            /*
                             * const int DEADZONE = 23000;
                             * int initialValue = 0;
                             *
                             * int normValue = 0;
                             * if (Math.Abs(evt.jaxis.axisValue - initialValue) > DEADZONE) // batocera
                             * {
                             *   if (evt.jaxis.axisValue - initialValue > 0) // batocera
                             *       normValue = 1;
                             *   else
                             *       normValue = -1;
                             * }
                             *
                             * var axis = joysticks.FindInputMapping(evt.jaxis.which, "axis", evt.jaxis.axis);
                             * if (axis != null)
                             * {
                             *   if (normValue == 1)
                             *       state |= axis.Name;
                             *   else
                             *       state &= ~axis.Name;
                             * }
                             */
                        }
                        break;

                        case SDL.SDL_EventType.SDL_JOYBUTTONDOWN:
                        case SDL.SDL_EventType.SDL_JOYBUTTONUP:
                        {
                            var js = joysticks.FirstOrDefault(j => j.Id == evt.jbutton.which);
                            if (js != null)
                            {
                                foreach (var conf in js.Config.Input.Where(i => i.Type == "button" && i.Id == evt.jbutton.button))
                                {
                                    if (evt.jbutton.state == SDL.SDL_PRESSED)
                                    {
                                        state |= conf.Name;
                                    }
                                    else
                                    {
                                        state &= ~conf.Name;
                                    }
                                }
                            }
                        }
                        break;

                        case SDL.SDL_EventType.SDL_JOYHATMOTION:
                        {
                            var js = joysticks.FirstOrDefault(j => j.Id == evt.jhat.which);
                            if (js != null)
                            {
                                var up = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_UP);
                                if (up != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_UP) == SDL.SDL_HAT_UP)
                                    {
                                        state |= up.Name;
                                    }
                                    else
                                    {
                                        state &= ~up.Name;
                                    }
                                }

                                var right = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_RIGHT);
                                if (right != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_RIGHT) == SDL.SDL_HAT_RIGHT)
                                    {
                                        state |= right.Name;
                                    }
                                    else
                                    {
                                        state &= ~right.Name;
                                    }
                                }

                                var down = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_DOWN);
                                if (down != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_DOWN) == SDL.SDL_HAT_DOWN)
                                    {
                                        state |= down.Name;
                                    }
                                    else
                                    {
                                        state &= ~down.Name;
                                    }
                                }

                                var left = js.Config.Input.FirstOrDefault(i => i.Type == "hat" && i.Id == evt.jhat.hat && i.Value == SDL.SDL_HAT_LEFT);
                                if (left != null)
                                {
                                    if ((evt.jhat.hatValue & SDL.SDL_HAT_LEFT) == SDL.SDL_HAT_LEFT)
                                    {
                                        state |= left.Name;
                                    }
                                    else
                                    {
                                        state &= ~left.Name;
                                    }
                                }
                            }
                        }
                        break;

                        case SDL.SDL_EventType.SDL_JOYDEVICEREMOVED:
                        {
                            var js = joysticks.FirstOrDefault(j => j.Id == evt.jdevice.which);
                            if (js != null)
                            {
                                SDL.SDL_JoystickClose(js.SdlJoystick);
                                joysticks.Remove(js);
                            }
                        }

                        break;

                        case SDL.SDL_EventType.SDL_JOYDEVICEADDED:
                            if (!joysticks.Any(j => j.Id == evt.jdevice.which))
                            {
                                AddJoystick(joysticks, evt.jdevice.which);
                            }
                            break;
                        }

                        if (state != oldState)
                        {
                            ProcessJoystickState(state, oldState);
                            oldState = state;
                        }

                        Thread.Sleep(1);
                    }
                }
                catch { }
            }

            foreach (var joy in joysticks)
            {
                SDL.SDL_JoystickClose(joy.SdlJoystick);
            }

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