示例#1
0
        internal InputState(InputConfig config,
                            GamePadState padState, Maybe <KeyboardState> keyState,
                            InputState previousState)
        {
            int inputCount = Enum_Values.GetEnumValues(typeof(Inputs)).Length;

            InputsHeldTime   = new int[inputCount];
            InputsRepeatTime = new int[inputCount];
            InputsReleased   = new bool[inputCount];

            GamePad = padState;
            if (keyState.IsSomething)
            {
                KeyState = keyState;
            }

            UpdateState(config, previousState);
        }
示例#2
0
        private void UpdateState(InputConfig config, InputState previousState)
        {
            float leftStickAngle = (float)Math.Atan2(GamePad.ThumbSticks.Left.Y, GamePad.ThumbSticks.Left.X);

            if (leftStickAngle < 0)
            {
                leftStickAngle += MathHelper.TwoPi;
            }
            leftStickAngle *= 360 / MathHelper.TwoPi;

            // Loop through inputs
            foreach (Inputs input in Enum_Values.GetEnumValues(typeof(Inputs)))
            {
                int key = (int)input;

                bool keyPressed = (KeyState.IsKeyDown(config.KeyRedirect[input]) ||
                                   GamePad.IsButtonDown(config.PadRedirect[input]));
                if (Tactile.Input.INPUT_OVERRIDES.ContainsKey(input))
                {
                    keyPressed |= KeyState.IsKeyDown(Tactile.Input.INPUT_OVERRIDES[input]);
                }

                // Left stick
                if (LeftStickActive(GamePad))
                {
                    switch (key)
                    {
                    case ((int)Inputs.Right):
                        if (leftStickAngle < 67.5f || leftStickAngle > 292.5f)
                        {
                            keyPressed = true;
                        }
                        break;

                    case ((int)Inputs.Up):
                        if (leftStickAngle > 22.5f && leftStickAngle < 157.5f)
                        {
                            keyPressed = true;
                        }
                        break;

                    case ((int)Inputs.Left):
                        if (leftStickAngle > 112.5f && leftStickAngle < 247.5f)
                        {
                            keyPressed = true;
                        }
                        break;

                    case ((int)Inputs.Down):
                        if (leftStickAngle > 202.5f && leftStickAngle < 337.5f)
                        {
                            keyPressed = true;
                        }
                        break;
                    }
                }
                if (!Tactile.Input.INVERSE_DIRECTIONS_CANCEL)
                {
                    // If pressing up and down
                    if (key == (int)Inputs.Down && Pressed(Inputs.Up))
                    {
                        keyPressed = false;
                    }
                    // If pressing left and right
                    if (key == (int)Inputs.Right && Pressed(Inputs.Left))
                    {
                        keyPressed = false;
                    }
                }
                // If not pressed, remove lock
                if (!keyPressed)
                {
                    LockedRepeats.Remove(key);
                }

                // Set data to state
                if (keyPressed)
                {
                    // If pressed, set input held time to previous frame's hold time plus 1
                    InputsHeldTime[key]   = previousState.InputsHeldTime[key] + 1;
                    InputsRepeatTime[key] = previousState.InputsRepeatTime[key] + 1;
                }
                else if (previousState.Pressed(input))
                {
                    // If not pressed and was pressed last frame, set released value
                    InputsReleased[key] = true;
                }
            }

            if (Tactile.Input.INVERSE_DIRECTIONS_CANCEL)
            {
                if (Pressed(Inputs.Down) && Pressed(Inputs.Up))
                {
                    InputsHeldTime[(int)Inputs.Down] = 0;
                    InputsHeldTime[(int)Inputs.Up]   = 0;

                    InputsRepeatTime[(int)Inputs.Down] = 0;
                    InputsRepeatTime[(int)Inputs.Up]   = 0;
                }
                if (Pressed(Inputs.Left) && Pressed(Inputs.Right))
                {
                    InputsHeldTime[(int)Inputs.Left]  = 0;
                    InputsHeldTime[(int)Inputs.Right] = 0;

                    InputsRepeatTime[(int)Inputs.Left]  = 0;
                    InputsRepeatTime[(int)Inputs.Right] = 0;
                }
            }
            foreach (Inputs key in Tactile.Input.DIRECTIONS)
            {
                // If just triggered a direction and it's not locked, unlock repeats
                if (Triggered(key) && !LockedRepeats.Contains((int)key))
                {
                    ClearLockedRepeats();
                    break;
                }
            }
            // If any direction was just pressed or released,
            // reset the repeat value for all pressed directions to 1
            if (Tactile.Input.DIRECTIONS.Any(x => Triggered(x) || Released(x)))
            {
                foreach (Inputs key in Tactile.Input.DIRECTIONS)
                {
                    if (InputsRepeatTime[(int)key] > 0)
                    {
                        InputsRepeatTime[(int)key] = 1;
                    }
                }
            }
        }