Exemple #1
0
        public GlfwGamepad(int i)
        {
            var hasState = GlfwProvider.GLFW.Value.GetGamepadState(i, out var state);

            Index       = i;
            Buttons     = new Button[GamepadButtonCount];
            Thumbsticks = new Thumbstick[GamepadThumbstickCount];
            Triggers    = new Trigger[GamepadTriggerCount];

            _connected = hasState;

            for (int j = 0; j < GamepadButtonCount; j++)
            {
                ((Button[])Buttons)[j] = new Button
                                             ((ButtonName)j, j, hasState && state.Buttons[j] == (int)InputAction.Press);
            }

            for (int j = 0; j < GamepadThumbstickCount; j++)
            {
                ((Thumbstick[])Thumbsticks)[j] = new Thumbstick(j, 0, 0);
            }

            for (int j = 0; j < GamepadTriggerCount; j++)
            {
                ((Trigger[])Triggers)[j] = new Trigger(j, 0);
            }
        }
 /// <summary>
 ///     Add a ThumbstickDirection binding that can be checked for state (Pressed, Released, Active)
 /// </summary>
 public static bool AddBinding(this InputManager inputManager, string bindingName,
                               ThumbstickDirection thumbstickDirection,
                               Thumbstick thumbstick, params InputBinding[] modifiers)
 {
     DefaultBinding inputBinding = new ThumbstickDirectionBinding(thumbstickDirection, thumbstick, modifiers);
     return inputManager.AddBinding(bindingName, inputBinding);
 }
Exemple #3
0
 public void SetBinding(Thumbstick thumbstick, params Modifier[] modifiers)
 {
     ClearBindings();
     this.Thumbstick = thumbstick;
     this.type       = BindingType.Thumbstick;
     SetModifiers(modifiers);
 }
Exemple #4
0
        private bool IsThumbstickDirectionActive(ThumbstickDirection direction, Thumbstick thumbstick, FrameState state)
        {
            GamePadState gamepadState      = state == FrameState.Current ? CurrentGamePadState : LastGamePadState;
            Vector2      gamepadThumbstick = thumbstick == Thumbstick.Left ? gamepadState.ThumbSticks.Left : gamepadState.ThumbSticks.Right;
            bool         isActive          = false;

            switch (direction)
            {
            case ThumbstickDirection.Up:
                isActive = (gamepadThumbstick.Y >= thumbstickThreshold);
                break;

            case ThumbstickDirection.Down:
                isActive = (gamepadThumbstick.Y <= -thumbstickThreshold);
                break;

            case ThumbstickDirection.Left:
                isActive = (gamepadThumbstick.X <= -thumbstickThreshold);
                break;

            case ThumbstickDirection.Right:
                isActive = (gamepadThumbstick.X >= thumbstickThreshold);
                break;

            default:
                break;
            }

            return(isActive);
        }
Exemple #5
0
        public void AddKeyBinding(string bindingName, Thumbstick thumbstick, params Modifier[] modifiers)
        {
            InputBinding inputBinding = new InputBinding();

            inputBinding.SetBinding(thumbstick, modifiers);
            AddKeyBinding(bindingName, inputBinding);
        }
Exemple #6
0
        private bool IsThumbstickActive(Thumbstick thumbstick, FrameState state)
        {
            GamePadState gamepadState      = state == FrameState.Current ? CurrentGamePadState : LastGamePadState;
            Vector2      gamepadThumbstick = thumbstick == Thumbstick.Left ? gamepadState.ThumbSticks.Left : gamepadState.ThumbSticks.Right;

            return(gamepadThumbstick.Length() >= thumbstickThreshold);
        }
Exemple #7
0
        public virtual bool Left(Thumbstick theThumbstick)
        {
            bool aIsPressed = false;

            switch (theThumbstick)
            {
            case Thumbstick.Left:
            {
                if (mCurrentGamePadState.DPad.Left == ButtonState.Pressed || mCurrentKeyboardState.IsKeyDown(mKeys.Left))
                {
                    aIsPressed = true;
                }
                else if (mCurrentGamePadState.ThumbSticks.Left.X < 0)
                {
                    aIsPressed = true;
                }
                break;
            }

            case Thumbstick.Right:
            {
                if (mCurrentGamePadState.ThumbSticks.Right.X < 0)
                {
                    aIsPressed = true;
                }
                break;
            }
            }

            return(aIsPressed);
        }
Exemple #8
0
        public GlfwGamepad(int i)
        {
            var hasState = GlfwProvider.GLFW.Value.GetGamepadState(i, out var state);

            Index        = i;
            _buttons     = (Button *)Marshal.AllocHGlobal(GamepadButtonCount * sizeof(Button));
            _thumbsticks = (Thumbstick *)Marshal.AllocHGlobal(GamepadThumbstickCount * sizeof(Thumbstick));
            _triggers    = (Trigger *)Marshal.AllocHGlobal(GamepadTriggerCount * sizeof(Trigger));
            Buttons      = new GlfwReadOnlyList <Button>(_buttons, GamepadButtonCount);
            Thumbsticks  = new GlfwReadOnlyList <Thumbstick>(_thumbsticks, GamepadThumbstickCount);
            Triggers     = new GlfwReadOnlyList <Trigger>(_triggers, GamepadTriggerCount);

            _connected = hasState;

            for (int j = 0; j < GamepadButtonCount; j++)
            {
                _buttons[j] = new Button((ButtonName)j, j, hasState && state.Buttons[j] == (int)InputAction.Press);
            }

            for (int j = 0; j < GamepadThumbstickCount; j++)
            {
                _thumbsticks[j] = new Thumbstick(j, 0, 0);
            }

            for (int j = 0; j < GamepadTriggerCount; j++)
            {
                _triggers[j] = new Trigger(j, 0);
            }
        }
Exemple #9
0
        public InputBinding(BindingType type, params Modifier[] modifiers)
        {
            this.type           = type;
            ThumbstickDirection = ThumbstickDirection.None;
            MouseButton         = MouseButton.None;
            Thumbstick          = Thumbstick.None;
            Trigger             = Trigger.None;
            Key    = Keys.None;
            Button = Buttons.BigButton;

            SetModifiers(modifiers);
        }
Exemple #10
0
 /// <summary>
 /// Gets the rotation of the specified thumbstick
 /// </summary>
 /// <param name="thumbstick">The thumbstick to use</param>
 /// <param name="playerIndex">The player to check</param>
 /// <returns></returns>
 public static Vector2 GetThumbstickRotation(Thumbstick thumbstick, PlayerIndex playerIndex)
 {
     //Gets the thumbstick rotation for the specified thumbstick
     if (thumbstick == Thumbstick.Left)
     {
         return(GamePad.GetState(playerIndex).ThumbSticks.Left);
     }
     else
     {
         return(GamePad.GetState(playerIndex).ThumbSticks.Right);
     }
 }
Exemple #11
0
        internal void SetThumbstick(Thumbstick stick)
        {
            switch (stick.Index)
            {
            case 0:
                LeftThumbStick = new Vector(stick.X, -stick.Y);     // Y-akseli on väärinpäin?
                break;

            case 1:
                RightThumbStick = new Vector(stick.X, -stick.Y);
                break;
            }
        }
    public void Update()
    {
        Thumbstick axes = binding.Axes();

        boundAction(axes.X, axes.Y);
    }
Exemple #13
0
        /// <summary>
        /// Updates the values in this <see cref="InputManager3D"/> by processing input states.
        /// </summary>
        /// <param name="dtSeconds">The amount of time since the last update, measured in seconds.</param>
        public void Update(float dtSeconds)
        {
            if (CurrentMouse != null)
            {
                if (CurrentMouse.IsButtonPressed(MouseButton.Left))
                {
                    CameraRotationY += (CurrentMouse.Position.X - lastMousePos.X) * MouseSensitivity;
                    CameraRotationX  = Math.Clamp(CameraRotationX + (CurrentMouse.Position.Y - lastMousePos.Y) * -MouseSensitivity, -1.57f, 1.57f);
                    if (LockMouseWhileRotating)
                    {
                        CurrentMouse.Position = lastMousePos;
                    }
                }

                lastMousePos = CurrentMouse.Position;
            }

            if (CurrentKeyboard != null)
            {
                float sinY = MathF.Sin(CameraRotationY);
                float cosY = MathF.Cos(CameraRotationY);

                if (CurrentKeyboard.IsKeyPressed(Key.W))
                {
                    CameraPosition += new Vector3(cosY, 0, sinY) * CameraMoveSpeed * dtSeconds;
                }
                if (CurrentKeyboard.IsKeyPressed(Key.S))
                {
                    CameraPosition -= new Vector3(cosY, 0, sinY) * CameraMoveSpeed * dtSeconds;
                }

                if (CurrentKeyboard.IsKeyPressed(Key.A))
                {
                    CameraPosition += new Vector3(sinY, 0, -cosY) * CameraMoveSpeed * dtSeconds;
                }
                if (CurrentKeyboard.IsKeyPressed(Key.D))
                {
                    CameraPosition -= new Vector3(sinY, 0, -cosY) * CameraMoveSpeed * dtSeconds;
                }

                if (CurrentKeyboard.IsKeyPressed(Key.E))
                {
                    CameraPosition.Y += CameraMoveSpeed * dtSeconds;
                }
                if (CurrentKeyboard.IsKeyPressed(Key.Q))
                {
                    CameraPosition.Y -= CameraMoveSpeed * dtSeconds;
                }
            }

            if (CurrentGamepad != null && CurrentGamepad.Thumbsticks.Count != 0)
            {
                Thumbstick thumbstick = CurrentGamepad.Thumbsticks[0];
                if (Math.Abs(thumbstick.Position) > 0.2f)
                {
                    CameraRotationY += thumbstick.X * dtSeconds * CameraThumbstickSensitivity;
                    CameraRotationX  = Math.Clamp(CameraRotationX - thumbstick.Y * dtSeconds * CameraThumbstickSensitivity, -1.57f, 1.57f);
                }

                if (CurrentGamepad.Thumbsticks.Count >= 2)
                {
                    thumbstick = CurrentGamepad.Thumbsticks[1];
                    if (Math.Abs(thumbstick.Position) > 0.2f)
                    {
                        float rot = thumbstick.Direction + CameraRotationY + MathF.PI / 2f;
                        float spd = thumbstick.Position * dtSeconds * CameraMoveSpeed;
                        CameraPosition.X += MathF.Cos(rot) * spd;
                        CameraPosition.Z += MathF.Sin(rot) * spd;
                    }
                }

                if (CurrentGamepad.Triggers.Count >= 2)
                {
                    if (CurrentGamepad.Triggers[0].Position > 0.2f)
                    {
                        CameraPosition.Y -= CurrentGamepad.Triggers[0].Position * dtSeconds * CameraMoveSpeed;
                    }
                    if (CurrentGamepad.Triggers[1].Position > 0.2f)
                    {
                        CameraPosition.Y += CurrentGamepad.Triggers[1].Position * dtSeconds * CameraMoveSpeed;
                    }
                }
            }
        }
Exemple #14
0
 private bool IsThumbstickActive(Thumbstick thumbstick, FrameState state)
 {
     GamePadState gamepadState = state == FrameState.Current ? CurrentGamePadState : LastGamePadState;
     Vector2 gamepadThumbstick = thumbstick == Thumbstick.Left ? gamepadState.ThumbSticks.Left : gamepadState.ThumbSticks.Right;
     return gamepadThumbstick.Length() >= thumbstickThreshold;
 }
Exemple #15
0
 public void AddKeyBinding(string bindingName, Thumbstick thumbstick, params Modifier[] modifiers)
 {
     InputBinding inputBinding = new InputBinding();
     inputBinding.SetBinding(thumbstick, modifiers);
     AddKeyBinding(bindingName, inputBinding);
 }
Exemple #16
0
        public InputBinding(BindingType type, params Modifier[] modifiers)
        {
            this.type = type;
            ThumbstickDirection = ThumbstickDirection.None;
            MouseButton = MouseButton.None;
            Thumbstick = Thumbstick.None;
            Trigger = Trigger.None;
            Key = Keys.None;
            Button = Buttons.BigButton;

            SetModifiers(modifiers);
        }
Exemple #17
0
        private bool IsThumbstickDirectionActive(ThumbstickDirection direction, Thumbstick thumbstick, FrameState state)
        {
            GamePadState gamepadState = state == FrameState.Current ? CurrentGamePadState : LastGamePadState;
            Vector2 gamepadThumbstick = thumbstick == Thumbstick.Left ? gamepadState.ThumbSticks.Left : gamepadState.ThumbSticks.Right;
            bool isActive = false;
            switch (direction)
            {
                case ThumbstickDirection.Up:
                    isActive = (gamepadThumbstick.Y >= thumbstickThreshold);
                    break;
                case ThumbstickDirection.Down:
                    isActive = (gamepadThumbstick.Y <= -thumbstickThreshold);
                    break;
                case ThumbstickDirection.Left:
                    isActive = (gamepadThumbstick.X <= -thumbstickThreshold);
                    break;
                case ThumbstickDirection.Right:
                    isActive = (gamepadThumbstick.X >= thumbstickThreshold);
                    break;
                default:
                    break;
            }

            return isActive;
        }
 public ThumbstickDirectionBinding(ThumbstickDirection thumbstickDirection, Thumbstick thumbstick,
                                   params InputBinding[] modifiers)
     : base(modifiers)
 {
     Thumbstick = thumbstick;
     Direction = thumbstickDirection;
 }
Exemple #19
0
 public static Thumbstick ApplyDeadzone(Thumbstick thumbstick, Deadzone deadzone)
 {
     return(new Thumbstick(thumbstick.Index, deadzone.Apply(thumbstick.X), deadzone.Apply(thumbstick.Y)));
 }
Exemple #20
0
 public void SetBinding(Thumbstick thumbstick, params Modifier[] modifiers)
 {
     ClearBindings();
     this.Thumbstick = thumbstick;
     this.type = BindingType.Thumbstick;
     SetModifiers(modifiers);
 }
Exemple #21
0
 /// <summary>
 /// Gets the rotation of the specified thumbstick
 /// </summary>
 /// <param name="thumbstick">The thumbstick to use</param>
 /// <param name="playerIndex">The player to check</param>
 /// <returns></returns>
 public static Vector2 GetThumbstickRotation(Thumbstick thumbstick, PlayerIndex playerIndex)
 {
     //Gets the thumbstick rotation for the specified thumbstick
     if (thumbstick == Thumbstick.Left)
     {
         return GamePad.GetState(playerIndex).ThumbSticks.Left;
     }
     else
     {
         return GamePad.GetState(playerIndex).ThumbSticks.Right;
     }
 }
Exemple #22
0
 public void OnGamepadThumbstickMoved(object sender, Thumbstick stick)
 {
     throw new NotImplementedException();
 }
Exemple #23
0
 public Vector2 GetThumbStickState(Thumbstick thumbStick)
 {
     switch (thumbStick)
     {
         case Thumbstick.Left:
             return CurrentState.ThumbSticks.Left * new Vector2(1, -1);
         case Thumbstick.Right:
             return CurrentState.ThumbSticks.Right * new Vector2(1, -1);
     }
     return Vector2.Zero;
 }
Exemple #24
0
 private void GamepadThumbstickMoved(IGamepad arg1, Thumbstick arg2)
 {
     internalState.SetThumbstick(arg2);
 }
Exemple #25
0
        public virtual bool Left(Thumbstick theThumbstick)
        {
            bool aIsPressed = false;

            switch (theThumbstick)
            {
                case Thumbstick.Left:
                    {
                        if (mCurrentGamePadState.DPad.Left == ButtonState.Pressed || mCurrentKeyboardState.IsKeyDown(mKeys.Left))
                        {
                            aIsPressed = true;
                        }
                        else if (mCurrentGamePadState.ThumbSticks.Left.X < 0)
                        {
                            aIsPressed = true;
                        }
                        break;
                    }
                case Thumbstick.Right:
                    {
                        if (mCurrentGamePadState.ThumbSticks.Right.X < 0)
                        {
                            aIsPressed = true;
                        }
                        break;
                    }
            }

            return aIsPressed;
        }
Exemple #26
0
        public void DoEvent(Event @event)
        {
            switch ((EventType)@event.Type)
            {
            case EventType.Controlleraxismotion:
            {
                switch ((GameControllerAxis)@event.Caxis.Axis)
                {
                case GameControllerAxis.ControllerAxisLeftx:
                {
                    _thumbsticksChanged[0] = true;
                    _thumbsticks[0]        = new Thumbstick
                                                 (0, (float)@event.Caxis.Value / short.MaxValue, _thumbsticks[0].Y);
                    break;
                }

                case GameControllerAxis.ControllerAxisLefty:
                {
                    _thumbsticksChanged[0] = true;
                    _thumbsticks[0]        = new Thumbstick
                                                 (0, _thumbsticks[0].X, (float)@event.Caxis.Value / short.MaxValue);
                    break;
                }

                case GameControllerAxis.ControllerAxisRightx:
                {
                    _thumbsticksChanged[1] = true;
                    _thumbsticks[1]        = new Thumbstick
                                                 (1, (float)@event.Caxis.Value / short.MaxValue, _thumbsticks[1].Y);
                    break;
                }

                case GameControllerAxis.ControllerAxisRighty:
                {
                    _thumbsticksChanged[1] = true;
                    _thumbsticks[1]        = new Thumbstick
                                                 (1, _thumbsticks[1].X, (float)@event.Caxis.Value / short.MaxValue);
                    break;
                }

                case GameControllerAxis.ControllerAxisTriggerleft:
                {
                    TriggerMoved?.Invoke
                        (this, _triggers[0] = new Trigger(0, (float)@event.Caxis.Value / short.MaxValue));
                    break;
                }

                case GameControllerAxis.ControllerAxisTriggerright:
                {
                    TriggerMoved?.Invoke
                        (this, _triggers[1] = new Trigger(1, (float)@event.Caxis.Value / short.MaxValue));
                    break;
                }
                }

                break;
            }

            case EventType.Controllerbuttondown:
            {
                var ogBtn = _buttons[@event.Cbutton.Button];
                ButtonDown?.Invoke
                    (this, _buttons[@event.Cbutton.Button] = new Button(ogBtn.Name, ogBtn.Index, true));
                break;
            }

            case EventType.Controllerbuttonup:
            {
                var ogBtn = _buttons[@event.Cbutton.Button];
                ButtonUp?.Invoke
                    (this, _buttons[@event.Cbutton.Button] = new Button(ogBtn.Name, ogBtn.Index, false));
                break;
            }

            case EventType.Controllerdeviceadded:
            {
                _ctx.ChangeConnection(this, true);
                break;
            }

            case EventType.Controllerdeviceremoved:
            {
                _ctx.ChangeConnection(this, false);
                break;
            }
            }

            for (var i = 0; i < _thumbsticksChanged.Length; i++)
            {
                if (_thumbsticksChanged[i])
                {
                    ThumbstickMoved?.Invoke(this, _thumbsticks[i]);
                    _thumbsticksChanged[i] = false;
                }
            }
        }
Exemple #27
0
        public void Update()
        {
            if (!GlfwProvider.GLFW.Value.GetGamepadState(Index, out var state))
            {
                // Detect when this gamepad disconnects
                if (_connected)
                {
                    OnConnectionChanged?.Invoke(this, false);
                    _connected = false;
                }

                return;
            }

            // Detect when this gamepad connects
            if (!_connected)
            {
                OnConnectionChanged?.Invoke(this, true);
                _connected = true;
            }

            // Buttons
            for (var i = 0; i < GamepadButtonCount; i++)
            {
                if ((_buttons[i].Pressed ? 1 : 0) != state.Buttons[i])
                {
                    (_buttons[i].Pressed ? ButtonUp : ButtonDown)?.Invoke
                        (this, _buttons[i] = new Button((ButtonName)i, i, state.Buttons[i] == 1));
                }

                _buttons[i] = new Button((ButtonName)i, i, state.Buttons[i] == 1);
            }

            // Left Thumbstick
            var thumbstick0 = new Thumbstick(0, Deadzone.Apply(state.Axes[0]), Deadzone.Apply(state.Axes[1]));

            if (_thumbsticks[0].X != thumbstick0.X || _thumbsticks[0].Y != thumbstick0.Y)
            {
                ThumbstickMoved?.Invoke
                (
                    this,
                    thumbstick0
                );
            }

            _thumbsticks[0] = thumbstick0;

            // Right Thumbstick
            var thumbstick1 = new Thumbstick(1, Deadzone.Apply(state.Axes[2]), Deadzone.Apply(state.Axes[3]));

            if (_thumbsticks[1].X != thumbstick1.X || _thumbsticks[1].Y != thumbstick1.Y)
            {
                ThumbstickMoved?.Invoke
                (
                    this,
                    thumbstick1
                );
            }

            _thumbsticks[1] = thumbstick1;

            // Left Trigger
            var trigger0 = new Trigger(0, Deadzone.Apply(state.Axes[4]));

            if (_triggers[0].Position != trigger0.Position)
            {
                TriggerMoved?.Invoke(this, trigger0);
            }

            _triggers[0] = trigger0;

            // Right Trigger
            var trigger1 = new Trigger(1, Deadzone.Apply(state.Axes[5]));

            if (_triggers[1].Position != trigger1.Position)
            {
                TriggerMoved?.Invoke(this, trigger1);
            }

            _triggers[1] = trigger1;
        }
Exemple #28
0
 private static void GamepadOnThumbstickMoved(IGamepad g, Thumbstick t)
 {
     Console.WriteLine("G" + g.Index + "> " + t.Index + " thumbstick moved: (" + t.X + ", " + t.Y + ")");
 }
Exemple #29
0
 private static void GamepadOnThumbstickMoved(IGamepad g, Thumbstick t)
 {
     Console.WriteLine($"G{g.Index}> {t.Index} thumbstick moved: ({t.X}, {t.Y})");
 }
 public ThumbstickBinding(Thumbstick thumbstick, params InputBinding[] modifiers)
     : base(modifiers)
 {
     Thumbstick = thumbstick;
 }