示例#1
0
        /// <summary>
        /// Reads the input from the controller.
        /// </summary>
        /// <param name="joystick">The state of the joystick to read input from.</param>
        internal static void ReadInput(JoystickState joystick)
        {
            PowerNotches powerNotch = PowerNotches.None;
            BrakeNotches brakeNotch = BrakeNotches.None;

            powerNotch = joystick.IsButtonDown(ButtonIndex.Power1) ? powerNotch | PowerNotches.Power1 : powerNotch & ~PowerNotches.Power1;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake1) ? brakeNotch | BrakeNotches.Brake1 : brakeNotch & ~BrakeNotches.Brake1;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake2) ? brakeNotch | BrakeNotches.Brake2 : brakeNotch & ~BrakeNotches.Brake2;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake3) ? brakeNotch | BrakeNotches.Brake3 : brakeNotch & ~BrakeNotches.Brake3;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake4) ? brakeNotch | BrakeNotches.Brake4 : brakeNotch & ~BrakeNotches.Brake4;

            if (usesHat)
            {
                // The adapter uses the hat to map the direction buttons.
                // This is the case of some PlayStation adapters.
                powerNotch = joystick.GetHat((JoystickHat)hatIndex).IsLeft ? powerNotch | PowerNotches.Power2 : powerNotch & ~PowerNotches.Power2;
                powerNotch = joystick.GetHat((JoystickHat)hatIndex).IsRight ? powerNotch | PowerNotches.Power3 : powerNotch & ~PowerNotches.Power3;
            }
            else
            {
                // The adapter maps the direction buttons to independent buttons.
                powerNotch = joystick.IsButtonDown(ButtonIndex.Power2) ? powerNotch | PowerNotches.Power2 : powerNotch & ~PowerNotches.Power2;
                powerNotch = joystick.IsButtonDown(ButtonIndex.Power3) ? powerNotch | PowerNotches.Power3 : powerNotch & ~PowerNotches.Power3;
            }

            if (usesHat && powerNotch == PowerNotches.P4)
            {
                if (InputTranslator.PreviousPowerNotch < InputTranslator.PowerNotches.P3)
                {
                    // Hack for adapters which map the direction buttons to a hat and confuse N with P4
                    InputTranslator.PowerNotch = InputTranslator.PowerNotches.N;
                }
                else
                {
                    InputTranslator.PowerNotch = InputTranslator.PowerNotches.P4;
                }
            }
            else if (powerNotch != PowerNotches.Transition)
            {
                // Set notch only if it is not a transition
                InputTranslator.PowerNotch = PowerNotchMap[powerNotch];
            }
            if (brakeNotch != BrakeNotches.Transition && (brakeNotch == BrakeNotches.Emergency || brakeNotch >= BrakeNotches.B8))
            {
                // Set notch only if it is not a transition nor an unmarked notch
                InputTranslator.BrakeNotch = BrakeNotchMap[brakeNotch];
            }
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(ButtonIndex.Select);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start]  = joystick.GetButton(ButtonIndex.Start);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A]      = joystick.GetButton(ButtonIndex.A);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B]      = joystick.GetButton(ButtonIndex.B);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C]      = joystick.GetButton(ButtonIndex.C);
        }
        static public JoystickHatState GetHat(this JoystickState item, int hat_index)
        {
            switch (hat_index)
            {
            case 0: return(item.GetHat(JoystickHat.Hat0));

            case 1: return(item.GetHat(JoystickHat.Hat1));

            case 2: return(item.GetHat(JoystickHat.Hat2));

            case 3: return(item.GetHat(JoystickHat.Hat3));
            }

            throw new UnaccountedBranchException("hat_index", hat_index);
        }
示例#3
0
        void InitializeCallbacks()
        {
            const int dzp = 400;
            const int dzn = -400;

            names.Clear();
            actions.Clear();
            NumButtons = 0;

            AddItem("X+", () => state.GetAxis(0) >= dzp);
            AddItem("X-", () => state.GetAxis(0) <= dzn);
            AddItem("Y+", () => state.GetAxis(1) >= dzp);
            AddItem("Y-", () => state.GetAxis(1) <= dzn);
            AddItem("Z+", () => state.GetAxis(2) >= dzp);
            AddItem("Z-", () => state.GetAxis(2) <= dzn);

            // Enjoy our delicious sliders. They're smaller than regular burgers but cost more.

            int jb = 1;

            for (int i = 0; i < 64; i++)
            {
                AddItem($"B{jb}", () => state.GetButton(i) == ButtonState.Pressed);
                jb++;
            }

            jb = 1;
            foreach (JoystickHat enval in Enum.GetValues(typeof(JoystickHat)))
            {
                AddItem($"POV{jb}U", () => state.GetHat(enval).IsUp);
                AddItem($"POV{jb}D", () => state.GetHat(enval).IsDown);
                AddItem($"POV{jb}L", () => state.GetHat(enval).IsLeft);
                AddItem($"POV{jb}R", () => state.GetHat(enval).IsRight);
                jb++;
            }
        }
示例#4
0
        private bool IsActivated(JoystickState joystickState, ControllerInputId controllerInputId)
        {
            if (controllerInputId <= ControllerInputId.Button20)
            {
                return(joystickState.IsButtonDown((int)controllerInputId));
            }
            else if (controllerInputId <= ControllerInputId.Axis5)
            {
                int axis = controllerInputId - ControllerInputId.Axis0;

                return(joystickState.GetAxis(axis) > TriggerThreshold);
            }
            else if (controllerInputId <= ControllerInputId.Hat2Right)
            {
                int hat = (controllerInputId - ControllerInputId.Hat0Up) / 4;

                int baseHatId = (int)ControllerInputId.Hat0Up + (hat * 4);

                JoystickHatState hatState = joystickState.GetHat((JoystickHat)hat);

                if (hatState.IsUp && ((int)controllerInputId % baseHatId == 0))
                {
                    return(true);
                }
                if (hatState.IsDown && ((int)controllerInputId % baseHatId == 1))
                {
                    return(true);
                }
                if (hatState.IsLeft && ((int)controllerInputId % baseHatId == 2))
                {
                    return(true);
                }
                if (hatState.IsRight && ((int)controllerInputId % baseHatId == 3))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#5
0
        /// <summary>
        /// Reads the input from the controller.
        /// </summary>
        /// <param name="joystick">The state of the joystick to read input from.</param>
        internal static void ReadInput(JoystickState joystick)
        {
            double brakeAxis = joystick.GetAxis(0);
            double powerAxis = joystick.GetAxis(1);

            foreach (var notch in BrakeNotchMap)
            {
                if (brakeAxis >= GetRangeMin((byte)notch.Key) && brakeAxis <= GetRangeMax((byte)notch.Key))
                {
                    InputTranslator.BrakeNotch = notch.Value;
                }
            }
            foreach (var notch in PowerNotchMap)
            {
                if (powerAxis >= GetRangeMin((byte)notch.Key) && powerAxis <= GetRangeMax((byte)notch.Key))
                {
                    InputTranslator.PowerNotch = notch.Value;
                }
            }

            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(ButtonIndex.Select);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start]  = joystick.GetButton(ButtonIndex.Start);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A]      = joystick.GetButton(ButtonIndex.A);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B]      = joystick.GetButton(ButtonIndex.B);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C]      = joystick.GetButton(ButtonIndex.C);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D]      = joystick.GetButton(ButtonIndex.D);

            if (hasDirectionButtons)
            {
                InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up]    = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsUp ? 1 : 0);
                InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down]  = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsDown ? 1 : 0);
                InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left]  = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsLeft ? 1 : 0);
                InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsRight ? 1 : 0);
            }
        }
示例#6
0
        public void Poll()
        {
            for (int i = 0; i < 4; i++)
            {
                JoystickCapabilities caps = Joystick.GetCapabilities(i);
                if (caps.IsConnected && joysticks[i].Description == DisconnectedName)
                {
                    // New joystick connected
                    joysticks[i] = new LegacyJoystickDevice(
                        i,
                        caps.AxisCount + 2 * caps.HatCount,
                        caps.ButtonCount);
                    //device.Description = Joystick.GetName(i);
                    joysticks[i].Description = ConnectedName;
                }
                else if (!caps.IsConnected && joysticks[i].Description != DisconnectedName)
                {
                    // Joystick disconnected
                    joysticks[i]             = new LegacyJoystickDevice(i, 0, 0);
                    joysticks[i].Description = DisconnectedName;
                }

                JoystickState state = Joystick.GetState(i);
                for (int axis_index = 0; axis_index < caps.AxisCount; axis_index++)
                {
                    JoystickAxis axis = JoystickAxis.Axis0 + axis_index;
                    joysticks[i].SetAxis(axis, state.GetAxis(axis));
                }
                for (int button_index = 0; button_index < caps.ButtonCount; button_index++)
                {
                    JoystickButton button = JoystickButton.Button0 + button_index;
                    joysticks[i].SetButton(button, state.GetButton(button) == ButtonState.Pressed);
                }
                for (int hat_index = 0; hat_index < caps.HatCount; hat_index++)
                {
                    // LegacyJoystickDriver report hats as pairs of axes
                    // Make sure we have enough axes left for this mapping
                    int axis_index = caps.AxisCount + 2 * hat_index;
                    if (axis_index < JoystickState.MaxAxes)
                    {
                        JoystickHat      hat       = JoystickHat.Hat0 + hat_index;
                        JoystickHatState hat_state = state.GetHat(hat);
                        JoystickAxis     axis      = JoystickAxis.Axis0 + axis_index;
                        float            x         = 0;
                        float            y         = 0;
                        if (hat_state.IsDown)
                        {
                            y--;
                        }
                        if (hat_state.IsUp)
                        {
                            y++;
                        }
                        if (hat_state.IsLeft)
                        {
                            x--;
                        }
                        if (hat_state.IsRight)
                        {
                            x++;
                        }

                        joysticks[i].SetAxis(axis, x);
                        joysticks[i].SetAxis(axis + 1, y);
                    }
                }
            }
        }
 internal override JoystickHatState GetHat(int Hat)
 {
     return(state.GetHat((JoystickHat)Hat));
 }
示例#8
0
        /// <summary>
        /// Reads the input from the controller.
        /// </summary>
        internal override void ReadInput()
        {
            JoystickState    joystick   = Joystick.GetState(joystickIndex);
            PowerNotchesEnum powerNotch = PowerNotchesEnum.None;
            BrakeNotchesEnum brakeNotch = BrakeNotchesEnum.None;

            powerNotch = joystick.IsButtonDown(ButtonIndex.Power1) ? powerNotch | PowerNotchesEnum.Power1 : powerNotch & ~PowerNotchesEnum.Power1;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake1) ? brakeNotch | BrakeNotchesEnum.Brake1 : brakeNotch & ~BrakeNotchesEnum.Brake1;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake2) ? brakeNotch | BrakeNotchesEnum.Brake2 : brakeNotch & ~BrakeNotchesEnum.Brake2;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake3) ? brakeNotch | BrakeNotchesEnum.Brake3 : brakeNotch & ~BrakeNotchesEnum.Brake3;
            brakeNotch = joystick.IsButtonDown(ButtonIndex.Brake4) ? brakeNotch | BrakeNotchesEnum.Brake4 : brakeNotch & ~BrakeNotchesEnum.Brake4;

            if (UsesHat)
            {
                // The adapter uses the hat to map the direction buttons.
                // This is the case of some PlayStation adapters.
                powerNotch = joystick.GetHat((JoystickHat)HatIndex).IsLeft ? powerNotch | PowerNotchesEnum.Power2 : powerNotch & ~PowerNotchesEnum.Power2;
                powerNotch = joystick.GetHat((JoystickHat)HatIndex).IsRight ? powerNotch | PowerNotchesEnum.Power3 : powerNotch & ~PowerNotchesEnum.Power3;
            }
            else
            {
                // The adapter maps the direction buttons to independent buttons.
                powerNotch = joystick.IsButtonDown(ButtonIndex.Power2) ? powerNotch | PowerNotchesEnum.Power2 : powerNotch & ~PowerNotchesEnum.Power2;
                powerNotch = joystick.IsButtonDown(ButtonIndex.Power3) ? powerNotch | PowerNotchesEnum.Power3 : powerNotch & ~PowerNotchesEnum.Power3;
            }

            if (UsesHat && powerNotch == PowerNotchesEnum.P4)
            {
                // Hack for adapters using a hat where pressing left and right simultaneously reports only left being pressed
                if (InputTranslator.PreviousPowerNotch < InputTranslator.PowerNotches.P3)
                {
                    InputTranslator.PowerNotch = InputTranslator.PowerNotches.N;
                }
                else
                {
                    InputTranslator.PowerNotch = InputTranslator.PowerNotches.P4;
                }
            }
            else if (UsesHat && powerNotch == PowerNotchesEnum.Transition)
            {
                // Hack for adapters using a hat where pressing left and right simultaneously reports nothing being pressed, the same as the transition state
                // Has the side effect of the power notch jumping P1>N>P2, but it is barely noticeable unless moving the handle very slowly
                if (InputTranslator.PreviousPowerNotch < InputTranslator.PowerNotches.P2)
                {
                    InputTranslator.PowerNotch = InputTranslator.PowerNotches.N;
                }
            }
            else if (powerNotch != PowerNotchesEnum.Transition)
            {
                // Set notch only if it is not a transition
                InputTranslator.PowerNotch = PowerNotchMap[powerNotch];
            }
            if (brakeNotch != BrakeNotchesEnum.Transition && (brakeNotch == BrakeNotchesEnum.Emergency || brakeNotch >= BrakeNotchesEnum.B8))
            {
                // Set notch only if it is not a transition nor an unmarked notch
                InputTranslator.BrakeNotch = BrakeNotchMap[brakeNotch];
            }
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(ButtonIndex.Select);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start]  = joystick.GetButton(ButtonIndex.Start);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A]      = joystick.GetButton(ButtonIndex.A);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B]      = joystick.GetButton(ButtonIndex.B);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C]      = joystick.GetButton(ButtonIndex.C);
        }
        public GamePadState GetState(int index)
        {
            JoystickState joy = Joystick.GetState(index);
            GamePadState  pad = new GamePadState();

            if (joy.IsConnected)
            {
                pad.SetConnected(true);
                pad.SetPacketNumber(joy.PacketNumber);

                GamePadConfiguration configuration = GetConfiguration(Joystick.GetGuid(index));

                foreach (GamePadConfigurationItem map in configuration)
                {
                    switch (map.Source.Type)
                    {
                    case ConfigurationType.Axis:
                    {
                        // JoystickAxis -> Buttons/GamePadAxes mapping
                        JoystickAxis source_axis = map.Source.Axis;
                        short        value       = joy.GetAxisRaw(source_axis);

                        switch (map.Target.Type)
                        {
                        case ConfigurationType.Axis:
                            pad.SetAxis(map.Target.Axis, value);
                            break;

                        case ConfigurationType.Button:
                            // Todo: if SDL2 GameController config is ever updated to
                            // distinguish between negative/positive axes, then remove
                            // Math.Abs below.
                            // Button is considered press when the axis is >= 0.5 from center
                            pad.SetButton(map.Target.Button, Math.Abs(value) >= short.MaxValue >> 1);
                            break;
                        }
                    }
                    break;

                    case ConfigurationType.Button:
                    {
                        // JoystickButton -> Buttons/GamePadAxes mapping
                        JoystickButton source_button = map.Source.Button;
                        bool           pressed       = joy.GetButton(source_button) == ButtonState.Pressed;

                        switch (map.Target.Type)
                        {
                        case ConfigurationType.Axis:
                            // Todo: if SDL2 GameController config is ever updated to
                            // distinguish between negative/positive axes, then update
                            // the following line to support both.
                            short value = pressed ?
                                          short.MaxValue :
                                          (map.Target.Axis & (GamePadAxes.LeftTrigger | GamePadAxes.RightTrigger)) != 0 ?
                                          short.MinValue :
                                          (short)0;
                            pad.SetAxis(map.Target.Axis, value);
                            break;

                        case ConfigurationType.Button:
                            pad.SetButton(map.Target.Button, pressed);
                            break;
                        }
                    }
                    break;

                    case ConfigurationType.Hat:
                    {
                        // JoystickHat -> Buttons/GamePadAxes mapping
                        JoystickHat      source_hat = map.Source.Hat;
                        JoystickHatState state      = joy.GetHat(source_hat);

                        bool pressed = false;
                        switch (map.Source.HatPosition)
                        {
                        case HatPosition.Down:
                            pressed = state.IsDown;
                            break;

                        case HatPosition.Up:
                            pressed = state.IsUp;
                            break;

                        case HatPosition.Left:
                            pressed = state.IsLeft;
                            break;

                        case HatPosition.Right:
                            pressed = state.IsRight;
                            break;
                        }

                        switch (map.Target.Type)
                        {
                        case ConfigurationType.Axis:
                            // Todo: if SDL2 GameController config is ever updated to
                            // distinguish between negative/positive axes, then update
                            // the following line to support both.
                            short value = pressed ?
                                          short.MaxValue :
                                          (map.Target.Axis & (GamePadAxes.LeftTrigger | GamePadAxes.RightTrigger)) != 0 ?
                                          short.MinValue :
                                          (short)0;
                            pad.SetAxis(map.Target.Axis, value);
                            break;

                        case ConfigurationType.Button:
                            pad.SetButton(map.Target.Button, pressed);
                            break;
                        }
                    }
                    break;
                    }
                }
            }

            return(pad);
        }
示例#10
0
        void InitializeJoystickControls()
        {
            // OpenTK GamePad axis return float values (as opposed to the shorts of SlimDX)
            const float ConversionFactor = 1.0f / short.MaxValue;
            const float dzp = (short)4000 * ConversionFactor;
            const float dzn = (short)-4000 * ConversionFactor;

            //const float dzt = 0.6f;

            // axis
            AddItem("X+", () => jState.GetAxis(0) >= dzp);
            AddItem("X-", () => jState.GetAxis(0) <= dzn);
            AddItem("Y+", () => jState.GetAxis(1) >= dzp);
            AddItem("Y-", () => jState.GetAxis(1) <= dzn);
            AddItem("Z+", () => jState.GetAxis(2) >= dzp);
            AddItem("Z-", () => jState.GetAxis(2) <= dzn);
            AddItem("W+", () => jState.GetAxis(3) >= dzp);
            AddItem("W-", () => jState.GetAxis(3) <= dzn);
            AddItem("V+", () => jState.GetAxis(4) >= dzp);
            AddItem("V-", () => jState.GetAxis(4) <= dzn);
            AddItem("S+", () => jState.GetAxis(5) >= dzp);
            AddItem("S-", () => jState.GetAxis(5) <= dzn);
            AddItem("Q+", () => jState.GetAxis(6) >= dzp);
            AddItem("Q-", () => jState.GetAxis(6) <= dzn);
            AddItem("P+", () => jState.GetAxis(7) >= dzp);
            AddItem("P-", () => jState.GetAxis(7) <= dzn);
            AddItem("N+", () => jState.GetAxis(8) >= dzp);
            AddItem("N-", () => jState.GetAxis(8) <= dzn);
            // should be enough axis, but just in case:
            for (int i = 9; i < 64; i++)
            {
                int j = i;
                AddItem(string.Format("Axis{0}+", j.ToString()), () => jState.GetAxis(j) >= dzp);
                AddItem(string.Format("Axis{0}-", j.ToString()), () => jState.GetAxis(j) <= dzn);
            }

            // buttons
            for (int i = 0; i < _joystickCapabilities.Value.ButtonCount; i++)
            {
                int j = i;
                AddItem(string.Format("B{0}", i + 1), () => jState.GetButton(j) == ButtonState.Pressed);
            }

            // hats
            AddItem("POV1U", () => jState.GetHat(JoystickHat.Hat0).IsUp);
            AddItem("POV1D", () => jState.GetHat(JoystickHat.Hat0).IsDown);
            AddItem("POV1L", () => jState.GetHat(JoystickHat.Hat0).IsLeft);
            AddItem("POV1R", () => jState.GetHat(JoystickHat.Hat0).IsRight);
            AddItem("POV2U", () => jState.GetHat(JoystickHat.Hat1).IsUp);
            AddItem("POV2D", () => jState.GetHat(JoystickHat.Hat1).IsDown);
            AddItem("POV2L", () => jState.GetHat(JoystickHat.Hat1).IsLeft);
            AddItem("POV2R", () => jState.GetHat(JoystickHat.Hat1).IsRight);
            AddItem("POV3U", () => jState.GetHat(JoystickHat.Hat2).IsUp);
            AddItem("POV3D", () => jState.GetHat(JoystickHat.Hat2).IsDown);
            AddItem("POV3L", () => jState.GetHat(JoystickHat.Hat2).IsLeft);
            AddItem("POV3R", () => jState.GetHat(JoystickHat.Hat2).IsRight);
            AddItem("POV4U", () => jState.GetHat(JoystickHat.Hat3).IsUp);
            AddItem("POV4D", () => jState.GetHat(JoystickHat.Hat3).IsDown);
            AddItem("POV4L", () => jState.GetHat(JoystickHat.Hat3).IsLeft);
            AddItem("POV4R", () => jState.GetHat(JoystickHat.Hat3).IsRight);
        }
示例#11
0
        static unsafe void Main(string[] args)
        {
            bool list     = false;
            bool dump     = false; //show state and controller name
            int  index    = 0;     //current device
            bool checkold = true;  //compare last state
            bool axis     = false; //due to bad calibration axis can report data all the time


            for (int i = 0; i < args.Length; ++i)
            {
                string arg = args[i];

                if (arg == "-l" || arg == "--list")
                {
                    list = true;
                }
                else if (arg == "--no-check-state")
                {
                    checkold = false;
                }
                else if (arg == "-a" || arg == "--axis")
                {
                    axis = true;
                }
                else if (arg == "-i" || arg == "--index")
                {
                    if (i + 1 >= args.Length)
                    {
                        Logger.Error?.Print(LogClass.Application, $"Invalid option '{arg}'");

                        continue;
                    }

                    string i = args[++i];
                    index = Int32.Parse(i);
                }
                else if (arg == "-d" || arg == "--debug")
                {
                    dump = true;
                }
                else
                {
                    index = Int32.Parse(arg);
                }
            }

            NativeWindowSettings nativeSettings = NativeWindowSettings.Default;

            nativeSettings.Size            = new Vector2i(1280, 720);
            nativeSettings.Title           = "Game";
            nativeSettings.WindowState     = WindowState.Normal;
            nativeSettings.WindowBorder    = WindowBorder.Resizable;
            nativeSettings.NumberOfSamples = 4;

            GameWindow gameWindow = new GameWindow(GameWindowSettings.Default, nativeSettings);

            if (list)
            {
                for (int i = 0; i < gameWindow.JoystickStates.Count; i++)
                {
                    JoystickState state = gameWindow.JoystickStates[i];
                    if (dump)
                    {
                        Console.WriteLine($"testing {index} {state}");
                    }

                    if (state != null)
                    {
                        var name = GLFW.GetJoystickName(i);

                        Console.WriteLine($"Controller/{i} ({name})");
                    }
                }
                gameWindow.ProcessEvents();
                return;
            }

            while (true)
            {
                JoystickState state = gameWindow.JoystickStates[index];
                if (dump)
                {
                    Console.WriteLine($"Controller/{index} ({state})");
                }

                if (state != null)
                {
                    GLFW.GetJoystickHatsRaw(index, out var hatCount);
                    GLFW.GetJoystickAxesRaw(index, out var axisCount);
                    GLFW.GetJoystickButtonsRaw(index, out var buttonCount);
                    var name = GLFW.GetJoystickName(index);

                    Console.WriteLine($"Controller/{index} ({name})");
                    if (list)
                    {
                        continue;
                    }
                    Console.WriteLine($"{hatCount} {axisCount} {buttonCount}");
                    for (int j = 0; j < hatCount; j++)
                    {
                        Console.WriteLine($"hat{j}: {state.GetHat(j)}");
                    }

                    for (int j = 0; j < axisCount; j++)
                    {
                        Console.WriteLine($"axis{j}: {state.GetAxis(j)}");
                    }

                    for (int j = 0; j < buttonCount; j++)
                    {
                        if (state.IsButtonDown(j))
                        {
                            Console.WriteLine($"button {j}: down");
                        }
                    }
                }
                //Thread.Sleep(2000);
                gameWindow.ProcessEvents();
                if (list)
                {
                    break;
                }
            }
        }
示例#12
0
        /// <summary>
        /// Reads the input from the controller.
        /// </summary>
        internal override void ReadInput()
        {
            JoystickState joystick  = Joystick.GetState(joystickIndex);
            double        brakeAxis = Math.Round(joystick.GetAxis(0), 4);
            double        powerAxis = Math.Round(joystick.GetAxis(1), 4);

            for (int i = 0; i < brakeBytes.Length; i += 2)
            {
                // Each notch uses two bytes, minimum value and maximum value
                if (brakeAxis >= GetAxisValue(brakeBytes[i]) && brakeAxis <= GetAxisValue(brakeBytes[i + 1]))
                {
                    if (brakeBytes.Length == i + 2)
                    {
                        // Last notch should be Emergency
                        InputTranslator.BrakeNotch = InputTranslator.BrakeNotches.Emergency;
                    }
                    else
                    {
                        // Regular brake notch
                        InputTranslator.BrakeNotch = (InputTranslator.BrakeNotches)(i / 2);
                    }
                    break;
                }
            }
            for (int i = 0; i < powerBytes.Length; i += 2)
            {
                // Each notch uses two bytes, minimum value and maximum value
                if (powerAxis >= GetAxisValue(powerBytes[i]) && powerAxis <= GetAxisValue(powerBytes[i + 1]))
                {
                    InputTranslator.PowerNotch = (InputTranslator.PowerNotches)(i / 2);
                    break;
                }
            }

            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start]  = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Start]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.A]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.B]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.C]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.D]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.LDoor]  = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.LDoor]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.RDoor]  = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.RDoor]);

            if (Buttons.HasFlag(ControllerButtons.DPad))
            {
                if (comboDpad)
                {
                    // On some controllers, check for Select+A/B/C/D combo
                    bool dPadUp    = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.D]));
                    bool dPadDown  = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.B]));
                    bool dPadLeft  = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.A]));
                    bool dPadRight = Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select])) && Convert.ToBoolean(joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.C]));
                    bool dPadAny   = dPadUp || dPadDown || dPadLeft || dPadRight;
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up]    = (ButtonState)(dPadUp ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down]  = (ButtonState)(dPadDown ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left]  = (ButtonState)(dPadLeft ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(dPadRight ? 1 : 0);
                    // Disable original buttons if necessary
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] ^ (ButtonState)(dPadAny ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A]      = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A] ^ (ButtonState)(dPadLeft ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B]      = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B] ^ (ButtonState)(dPadDown ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C]      = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C] ^ (ButtonState)(dPadRight ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D]      = InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D] ^ (ButtonState)(dPadUp ? 1 : 0);
                }
                else
                {
                    // On other controllers, read the first hat
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up]    = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsUp ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down]  = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsDown ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left]  = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsLeft ? 1 : 0);
                    InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsRight ? 1 : 0);
                }
            }
        }
示例#13
0
        private void updateDevice(ref JoystickCapabilities caps, ref JoystickState state, ref JoystickState defaultState)
        {
            for (int i = 0; i < JoyKey.Count; i++)
            {
                buttons[i] = false;
            }
            List <Keys> Current = new List <Keys>(JoyKey.Count);

            for (int i = 0; i < caps.ButtonCount; i++)
            {
                if (state.IsButtonDown((JoystickButton)((int)JoystickButton.Button0 + i)))
                {
                    buttons[i] = true;
                    Current.Add((Keys)((int)JoyKey.Button0 + i));
                }
            }

            for (int i = 0; i < caps.AxisCount; i++)
            {
                JoystickAxis axis             = (JoystickAxis)(i + (int)JoystickAxis.Axis0);
                float        axisState        = state.GetAxis(axis);
                float        defaultAxisState = defaultState.GetAxis(axis);

                // find the dead zone base value by rounding the default state to the nearest deadZoneRound
                float deadZoneBase = (float)Math.Round(defaultAxisState / deadZoneRound) * deadZoneRound;

                // the min/max input thresholds are a fractional value between the deadZoneBase value and the min/max value
                float inputThresholdMin = deadZoneBase + (-1 - deadZoneBase) * inputThresholdFraction;
                float inputThresholdMax = deadZoneBase + (1 - deadZoneBase) * inputThresholdFraction;

                if (Math.Abs(axisState - deadZoneBase) < deadZone)
                {
                }
                else if (axisState < inputThresholdMin)
                {
                    int key = (int)JoyKey.AxisXMin + i * 2;
                    buttons[key - (int)JoyKey.Button0] = true;
                    Current.Add((Keys)key);
                }
                else if (axisState > inputThresholdMax)
                {
                    int key = (int)JoyKey.AxisXMin + i * 2 + 1;
                    buttons[key - (int)JoyKey.Button0] = true;
                    Current.Add((Keys)key);
                }
            }

            // WORKAROUND: HatCount is incorrectly reported as 0 for XInput controllers, so always try at least 1 hat
            for (int i = 0; i < Math.Max(1, caps.HatCount); i++)
            {
                JoystickHatState hatState = state.GetHat((JoystickHat)(i + (int)JoystickHat.Hat0));
                if (hatState.IsUp)
                {
                    int key = (int)JoyKey.Hat0U + i * 4;
                    buttons[key - (int)JoyKey.Button0] = true;
                    Current.Add((Keys)key);
                }
                if (hatState.IsRight)
                {
                    int key = (int)JoyKey.Hat0U + i * 4 + 1;
                    buttons[key - (int)JoyKey.Button0] = true;
                    Current.Add((Keys)key);
                }
                if (hatState.IsDown)
                {
                    int key = (int)JoyKey.Hat0U + i * 4 + 2;
                    buttons[key - (int)JoyKey.Button0] = true;
                    Current.Add((Keys)key);
                }
                if (hatState.IsLeft)
                {
                    int key = (int)JoyKey.Hat0U + i * 4 + 3;
                    buttons[key - (int)JoyKey.Button0] = true;
                    Current.Add((Keys)key);
                }
            }

            if (OnButtonDown != null)
            {
                if (Current.Count > 0)
                {
                    OnButtonDown(null, Current);
                }
            }
            if (OnButtonPressed != null)
            {
                List <Keys> down = new List <Keys>(JoyKey.Count);
                for (int i = 0; i < JoyKey.Count; i++)
                {
                    // release
                    if (lastButtons[i] && !buttons[i])
                    {
                        down.Add((Keys)((int)JoyKey.Button0 + i));
                    }
                }
                if (down.Count > 0)
                {
                    OnButtonPressed(null, down);
                }
            }
            LastKeyDown.Clear();
            LastKeyDown.AddRange(KeyDown);
            KeyDown.Clear();
            KeyDown.AddRange(Current);
            buttons.CopyTo(lastButtons, 0);
        }
        public ControllerHandler(Dictionary <string, ButtonState> oldPsButtons, int freq, string mode, byte controllmode = 0)
        {
            _freq = freq;

            JoystickState joyStick = Joystick.GetState(0);

            if (joyStick.IsConnected)
            {
                _xl        = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis0) * 100));      // Left  stick sideways
                _yl        = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis1) * 100 * -1)); // Left  stick updown
                _xr        = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis3) * 100 * -1)); // Right stick sideways
                _yr        = Convert.ToInt16(Math.Floor(joyStick.GetAxis(JoystickAxis.Axis4) * 100));      // Right stick updown
                _cross     = joyStick.GetButton(JoystickButton.Button0);                                   // Cross
                _circle    = joyStick.GetButton(JoystickButton.Button1);                                   // Circle
                _triangle  = joyStick.GetButton(JoystickButton.Button3);                                   // Triangle
                _square    = joyStick.GetButton(JoystickButton.Button2);                                   // Square
                _start     = joyStick.GetButton(JoystickButton.Button7);                                   // Start
                _selectB   = joyStick.GetButton(JoystickButton.Button6);                                   // Select
                _l1        = joyStick.GetButton(JoystickButton.Button4);                                   // L1
                _r1        = joyStick.GetButton(JoystickButton.Button5);                                   // R1
                _l3        = joyStick.GetButton(JoystickButton.Button8);                                   // L3
                _hatSwitch = joyStick.GetHat(JoystickHat.Hat0);                                            // hatSwitch

//                Console.WriteLine("" +
//                                  "x left: {0}\n" +
//                                  "y left: {1}\n" +
//                                  "x right: {2}\n" +
//                                  "y right: {3}\n" +
//                                  "Speed: {4}\n" +
//                                  "Wheels: {5}\n" +
//                                  "ControllMode: {6}\n"
//                    , _xl, _yl, _xr, _yr, _speed * 2.55, _wheelPos, _controllMode);
                _psButtons = new Dictionary <string, ButtonState>
                {
                    { "cross", _cross },
                    { "circle", _circle },
                    { "triangle", _triangle },
                    { "square", _square },
                    { "start", _start },
                    { "select", _selectB },
                    { "l1", _l1 },
                    { "r1", _r1 },
                    { "l3", _l3 }
                };
//                foreach (var button in _psButtons)
//                {
//                    Console.WriteLine(button.Key + ": " + button.Value);
//                }
//                Console.WriteLine();

                if (oldPsButtons != null)
                {
                    #region Speed controll code

                    #region Left stick code

                    if (_controllMode == 1)
                    {
                        // Backwards
                        if (_yl > 5)
                        {
                            _speed = _yl;
                        }
                        else if (_yl < 5 && _speed < 1)
                        {
                            _speed += 5;
                        }
                        // Forwards
                        if (_yl < -5)
                        {
                            _speed = _yl;
                        }
                        else if (_yl > -5 && _speed > 1)
                        {
                            _speed -= 5;
                        }
                        if (_speed >= -5 && _speed <= 5)
                        {
                            _speed = 0;                              // Fix for absolute stop of engines
                        }
                    }

                    #endregion

                    #region cross+circle

                    if (_controllMode == 2)
                    {
                        if (_cross == ButtonState.Pressed && _hatSwitch.Position == HatPosition.Centered &&
                            !(_speed == 70 || _speed > 70))
                        {
                            _speed += 10;
                        }
                        else if (_cross == ButtonState.Pressed && _hatSwitch.IsUp && _speed != 100)
                        {
                            _speed += 10;
                        }
                        else if (_cross == ButtonState.Pressed && _hatSwitch.Position == HatPosition.Centered && _speed > 70)
                        {
                            _speed -= 10;
                        }
                        else if (_cross == ButtonState.Released && _speed > 0)
                        {
                            _speed = 0;
                        }
                        else if (_circle == ButtonState.Pressed && !(_speed == -70 || _speed < -70))
                        {
                            _speed -= 10;
                        }
                        else if (_circle == ButtonState.Pressed && _hatSwitch.IsDown && _speed != -100)
                        {
                            _speed -= 10;
                        }

                        else if (_circle == ButtonState.Pressed && _hatSwitch.Position == HatPosition.Centered &&
                                 _speed < -70)
                        {
                            _speed += 10;
                        }
                        else if (_circle == ButtonState.Released && _speed < 0)
                        {
                            _speed = 0;
                        }
                    }

                    #endregion

                    #endregion
                    #region steering controll code
                    #region leftstick

                    if (_controllMode == 1)
                    {
                        // Backwards
                        if (_xl > 5)
                        {
                            _wheelPos1 = _xl;
                        }
                        else if (_xl < 5 && _wheelPos1 > 1)
                        {
                            _wheelPos1 = 0;
                        }
                        // Forwards
                        if (_xl < -5)
                        {
                            _wheelPos2 = _xl;
                        }
                        else if (_xl > -5 && _wheelPos2 < -1)
                        {
                            _wheelPos2 = 0;
                        }
                        if (_xl == 0)
                        {
                            _wheelPos1 = 0;
                            _wheelPos2 = 0;
                        }
                    }

                    #endregion

                    #region cross+circle

                    if (_controllMode == 2)
                    {
                        if (_hatSwitch.IsRight && _wheelPos1 < 50)
                        {
                            _wheelPos1 += 10;
                        }
                        else if (_hatSwitch.Position == HatPosition.Centered && _wheelPos1 > 0)
                        {
                            _wheelPos1 = 0;
                        }
                        if (_hatSwitch.IsLeft && _wheelPos2 < 50)
                        {
                            _wheelPos2 += 10;
                        }
                        else if (_hatSwitch.Position == HatPosition.Centered && _wheelPos2 > 0)
                        {
                            _wheelPos2 = 0;
                        }
                    }

                    #endregion

                    #endregion

                    #region pan_tilt
                    #region servoPosY
                    if (_yr > 5)
                    {
                        servoPosY = _yr;
                    }

                    else if (_yr < 5 && servoPosY > 1)
                    {
                        servoPosY = 0;
                    }
                    if (_yr < -5)
                    {
                        servoPosY = _yr;
                    }
                    else if (_yr > -5 && servoPosY < -1)
                    {
                        servoPosY = 0;
                    }
                    if (_yr == 0)
                    {
                        servoPosY = 0;
                    }
                    #endregion
                    #region servoPosX
                    if (_xr > 5)
                    {
                        servoPosX = _xr;
                    }
                    else if (_xr < 5 && servoPosX > 1)
                    {
                        servoPosX = 0;
                    }

                    if (_xr < -5)
                    {
                        servoPosX = _xr;
                    }
                    else if (_xr > -5 && servoPosX < -1)
                    {
                        servoPosX = 0;
                    }
                    if (_xr == 0)
                    {
                        servoPosX = 0;
                    }
                    #endregion
                    #endregion
                    #region Button checks

                    // Gets all the buttons out of the oldpsbuttons to see if it was pressed
                    ButtonState selectOld, startOld, triangleOld, squareOld, l1Old, r1Old, l3Old;
                    oldPsButtons.TryGetValue("select", out selectOld);
                    oldPsButtons.TryGetValue("start", out startOld);
                    oldPsButtons.TryGetValue("triangle", out triangleOld);
                    oldPsButtons.TryGetValue("square", out squareOld);
                    oldPsButtons.TryGetValue("l1", out l1Old);
                    oldPsButtons.TryGetValue("r1", out r1Old);
                    oldPsButtons.TryGetValue("l1", out l3Old);



                    if (_start == ButtonState.Released && startOld == ButtonState.Pressed)
                    {
                        _resetmh   = true;
                        _speed     = 0;
                        _wheelPos1 = 0;
                        _wheelPos2 = 0;
                    }

                    #endregion
                }
            }
        }
示例#15
0
        public void tポーリング(bool bWindowがアクティブ中, bool bバッファ入力有効)
        {
            #region [ bButtonフラグ初期化 ]
            for (int i = 0; i < 256; i++)
            {
                this.bButtonPushDown[i] = false;
                this.bButtonPullUp[i]   = false;
            }
            #endregion

            if (bWindowがアクティブ中)
            {
                this.list入力イベント.Clear();                                        // #xxxxx 2012.6.11 yyagi; To optimize, I removed new();


                #region [ 入力 ]
                //-----------------------------
                JoystickState ButtonState = Joystick.GetState(ID);
                if (ButtonState.IsConnected)
                {
                    #region [ X軸- ]
                    //-----------------------------
                    if (ButtonState.GetAxis(0) < -0.5)
                    {
                        if (this.bButtonState[0] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 0,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[0]    = true;
                            this.bButtonPushDown[0] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[0] == true)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 0,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[0]  = false;
                            this.bButtonPullUp[0] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ X軸+ ]
                    //-----------------------------
                    if (ButtonState.GetAxis(0) > 0.5)
                    {
                        if (this.bButtonState[1] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 1,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[1]    = true;
                            this.bButtonPushDown[1] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[1] == true)
                        {
                            STInputEvent event7 = new STInputEvent()
                            {
                                nKey       = 1,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(event7);

                            this.bButtonState[1]  = false;
                            this.bButtonPullUp[1] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ Y軸- ]
                    //-----------------------------
                    if (ButtonState.GetAxis(1) < -0.5)
                    {
                        if (this.bButtonState[2] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 2,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[2]    = true;
                            this.bButtonPushDown[2] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[2] == true)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 2,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[2]  = false;
                            this.bButtonPullUp[2] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ Y軸+ ]
                    //-----------------------------
                    if (ButtonState.GetAxis(1) > 0.5)
                    {
                        if (this.bButtonState[3] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 3,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[3]    = true;
                            this.bButtonPushDown[3] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[3] == true)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 3,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[3]  = false;
                            this.bButtonPullUp[3] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ Z軸- ]
                    //-----------------------------
                    if (ButtonState.GetAxis(2) < -0.5)
                    {
                        if (this.bButtonState[4] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 4,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[4]    = true;
                            this.bButtonPushDown[4] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[4] == true)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 4,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[4]  = false;
                            this.bButtonPullUp[4] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ Z軸+ ]
                    //-----------------------------
                    if (ButtonState.GetAxis(2) > 0.5)
                    {
                        if (this.bButtonState[5] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 5,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[5]    = true;
                            this.bButtonPushDown[5] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[5] == true)
                        {
                            STInputEvent event15 = new STInputEvent()
                            {
                                nKey       = 5,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(event15);

                            this.bButtonState[5]  = false;
                            this.bButtonPullUp[5] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ Z軸回転- ]
                    //-----------------------------
                    if (ButtonState.GetAxis(3) < -0.5)
                    {
                        if (this.bButtonState[6] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 6,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[6]    = true;
                            this.bButtonPushDown[6] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[4] == true)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 6,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[6]  = false;
                            this.bButtonPullUp[6] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ Z軸回転+ ]
                    //-----------------------------
                    if (ButtonState.GetAxis(3) > 0.5)
                    {
                        if (this.bButtonState[7] == false)
                        {
                            STInputEvent ev = new STInputEvent()
                            {
                                nKey       = 7,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(ev);

                            this.bButtonState[7]    = true;
                            this.bButtonPushDown[7] = true;
                        }
                    }
                    else
                    {
                        if (this.bButtonState[7] == true)
                        {
                            STInputEvent event15 = new STInputEvent()
                            {
                                nKey       = 7,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(event15);

                            this.bButtonState[7]  = false;
                            this.bButtonPullUp[7] = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    #region [ Button ]
                    //-----------------------------
                    bool bIsButtonPressedReleased = false;
                    for (int j = 0; j < 128; j++)
                    {
                        if (this.bButtonState[8 + j] == false && ButtonState.IsButtonDown(j))
                        {
                            STInputEvent item = new STInputEvent()
                            {
                                nKey       = 8 + j,
                                b押された      = true,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(item);

                            this.bButtonState[8 + j]    = true;
                            this.bButtonPushDown[8 + j] = true;
                            bIsButtonPressedReleased    = true;
                        }
                        else if (this.bButtonState[8 + j] == true && !ButtonState.IsButtonDown(j))
                        {
                            STInputEvent item = new STInputEvent()
                            {
                                nKey       = 8 + j,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(item);

                            this.bButtonState[8 + j]  = false;
                            this.bButtonPullUp[8 + j] = true;
                            bIsButtonPressedReleased  = true;
                        }
                    }
                    //-----------------------------
                    #endregion
                    // #24341 2011.3.12 yyagi: POV support
                    #region [ POV HAT 4/8way (only single POV switch is supported)]
                    JoystickHatState hatState = ButtonState.GetHat(JoystickHat.Hat0);

                    for (int nWay = 0; nWay < 8; nWay++)
                    {
                        if (hatState.Position == (OpenTK.Input.HatPosition)nWay + 1)
                        {
                            if (this.bButtonState[8 + 128 + nWay] == false)
                            {
                                STInputEvent stevent = new STInputEvent()
                                {
                                    nKey       = 8 + 128 + nWay,
                                    b押された      = true,
                                    nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                     // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                                };
                                this.list入力イベント.Add(stevent);

                                this.bButtonState[stevent.nKey]    = true;
                                this.bButtonPushDown[stevent.nKey] = true;
                            }
                            bIsButtonPressedReleased = true;
                        }
                    }
                    if (bIsButtonPressedReleased == false)                     // #xxxxx 2011.12.3 yyagi 他のボタンが何も押され/離されてない=POVが離された
                    {
                        int nWay = 0;
                        for (int i = 8 + 0x80; i < 8 + 0x80 + 8; i++)
                        {                                                                   // 離されたボタンを調べるために、元々押されていたボタンを探す。
                            if (this.bButtonState[i] == true)                               // DirectInputを直接いじるならこんなことしなくて良いのに、あぁ面倒。
                            {                                                               // この処理が必要なために、POVを1個しかサポートできない。無念。
                                nWay = i;
                                break;
                            }
                        }
                        if (nWay != 0)
                        {
                            STInputEvent stevent = new STInputEvent()
                            {
                                nKey       = nWay,
                                b押された      = false,
                                nTimeStamp = CSound管理.rc演奏用タイマ.nシステム時刻ms,                                 // 演奏用タイマと同じタイマを使うことで、BGMと譜面、入力ずれを防ぐ。
                            };
                            this.list入力イベント.Add(stevent);

                            this.bButtonState[nWay]  = false;
                            this.bButtonPullUp[nWay] = true;
                        }
                    }
                    #endregion
                    //-----------------------------
                    #endregion
                }
            }
        }
示例#16
0
        private static bool IsAnyButtonPressed(out ControllerInputId pressedButton, int index, double triggerThreshold)
        {
            JoystickState        joystickState        = Joystick.GetState(index);
            JoystickCapabilities joystickCapabilities = Joystick.GetCapabilities(index);

            //Buttons
            for (int i = 0; i != joystickCapabilities.ButtonCount; i++)
            {
                if (joystickState.IsButtonDown(i))
                {
                    Enum.TryParse($"Button{i}", out pressedButton);

                    return(true);
                }
            }

            //Axis
            for (int i = 0; i != joystickCapabilities.AxisCount; i++)
            {
                if (joystickState.GetAxis(i) > 0.5f && joystickState.GetAxis(i) > triggerThreshold)
                {
                    Enum.TryParse($"Axis{i}", out pressedButton);

                    return(true);
                }
            }

            //Hats
            for (int i = 0; i != joystickCapabilities.HatCount; i++)
            {
                JoystickHatState hatState = joystickState.GetHat((JoystickHat)i);
                string           pos      = null;

                if (hatState.IsUp)
                {
                    pos = "Up";
                }
                if (hatState.IsDown)
                {
                    pos = "Down";
                }
                if (hatState.IsLeft)
                {
                    pos = "Left";
                }
                if (hatState.IsRight)
                {
                    pos = "Right";
                }
                if (pos == null)
                {
                    continue;
                }

                Enum.TryParse($"Hat{i}{pos}", out pressedButton);

                return(true);
            }

            pressedButton = ControllerInputId.Unbound;

            return(false);
        }
示例#17
0
        /// <summary>
        /// Reads the input from the controller.
        /// </summary>
        internal override void ReadInput()
        {
            JoystickState joystick   = Joystick.GetState(joystickIndex);
            double        handleAxis = Math.Round(joystick.GetAxis(1), 4);

            for (int i = 0; i < brakeBytes.Length; i += 2)
            {
                // Each notch uses two bytes, minimum value and maximum value
                if (handleAxis >= GetAxisValue(brakeBytes[i]) && handleAxis <= GetAxisValue(brakeBytes[i + 1]))
                {
                    if (brakeBytes.Length == i + 2)
                    {
                        // Last notch should be Emergency
                        InputTranslator.BrakeNotch = InputTranslator.BrakeNotches.Emergency;
                    }
                    else
                    {
                        // Regular brake notch
                        InputTranslator.BrakeNotch = (InputTranslator.BrakeNotches)(i / 2);
                    }
                    break;
                }
                InputTranslator.BrakeNotch = InputTranslator.BrakeNotches.Released;
            }
            for (int i = 0; i < powerBytes.Length; i += 2)
            {
                // Each notch uses two bytes, minimum value and maximum value
                if (handleAxis >= GetAxisValue(powerBytes[i]) && handleAxis <= GetAxisValue(powerBytes[i + 1]))
                {
                    InputTranslator.PowerNotch = (InputTranslator.PowerNotches)(i / 2);
                    break;
                }
                InputTranslator.PowerNotch = InputTranslator.PowerNotches.N;
            }

            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Select] = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Select]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Start]  = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.Start]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.A]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.A]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.B]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.B]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.C]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.C]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.D]      = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.D]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.LDoor]  = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.LDoor]);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.RDoor]  = joystick.GetButton(buttonIndex[(int)InputTranslator.ControllerButton.RDoor]);

            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Up]    = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsUp ? 1 : 0);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Down]  = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsDown ? 1 : 0);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Left]  = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsLeft ? 1 : 0);
            InputTranslator.ControllerButtons[(int)InputTranslator.ControllerButton.Right] = (ButtonState)(joystick.GetHat(JoystickHat.Hat0).IsRight ? 1 : 0);
        }