public void Actions_HoldInteraction_CanBePerformedWhenInvolvingMoreThanOneControl()
    {
        var keyboard = InputSystem.AddDevice <Keyboard>();

        InputSystem.AddDevice <Mouse>();

        // Add several bindings just to ensure that if conflict resolution is in the mix,
        // things don't go sideways.

        var action = new InputAction(interactions: "hold(duration=2)");

        action.AddCompositeBinding("ButtonWithOneModifier")
        .With("Modifier", "<Keyboard>/a")
        .With("Button", "<Keyboard>/s");
        action.AddCompositeBinding("ButtonWithOneModifier")
        .With("Modifier", "<Mouse>/leftButton")
        .With("Button", "<Mouse>/rightButton");
        action.AddCompositeBinding("ButtonWithOneModifier")
        .With("Modifier", "<Keyboard>/shift")
        .With("Button", "<Mouse>/rightButton");

        action.Enable();

        var startedCount   = 0;
        var performedCount = 0;
        var canceledCount  = 0;

        action.started   += _ => ++ startedCount;
        action.performed += _ => ++ performedCount;
        action.canceled  += _ => ++ canceledCount;

        InputSystem.QueueStateEvent(keyboard, new KeyboardState(Key.A));
        InputSystem.Update();
        InputSystem.QueueStateEvent(keyboard, new KeyboardState(Key.A, Key.S));
        InputSystem.Update();

        Assert.That(startedCount, Is.EqualTo(1));
        Assert.That(performedCount, Is.Zero);
        Assert.That(canceledCount, Is.Zero);

        // Release before hold time.
        InputSystem.QueueStateEvent(keyboard, default(KeyboardState));
        InputSystem.Update();

        Assert.That(startedCount, Is.EqualTo(1));
        Assert.That(performedCount, Is.Zero);
        Assert.That(canceledCount, Is.EqualTo(1));

        currentTime += 3;

        InputSystem.Update();

        Assert.That(startedCount, Is.EqualTo(1));
        Assert.That(performedCount, Is.Zero);
        Assert.That(canceledCount, Is.EqualTo(1));
    }
Ejemplo n.º 2
0
 private void setupDefaultBindings()
 {
     moveAction = inputs.AddAction("Move");
     moveAction.AddBinding("<Gamepad>/dpad");
     moveAction.AddBinding("<Gamepad>/leftStick");
     moveAction.AddCompositeBinding("2DVector")
     .With("Up", "<Keyboard>/upArrow")
     .With("Up", "<Keyboard>/w")
     .With("Down", "<Keyboard>/downArrow")
     .With("Down", "<Keyboard>/s")
     .With("Left", "<Keyboard>/leftArrow")
     .With("Left", "<Keyboard>/a")
     .With("Right", "<Keyboard>/rightArrow")
     .With("Right", "<Keyboard>/d");
     primaryAction = inputs.AddAction("Primary");
     primaryAction.AddBinding("<Gamepad>/leftShoulder");
     primaryAction.AddBinding("<Gamepad>/buttonWest");
     primaryAction.AddBinding("<Gamepad>/buttonEast");
     primaryAction.AddBinding("<Keyboard>/z");
     primaryAction.AddBinding("<Keyboard>/j");
     secondaryAction = inputs.AddAction("Secondary");
     secondaryAction.AddBinding("<Gamepad>/buttonSouth");
     secondaryAction.AddBinding("<Keyboard>/x");
     secondaryAction.AddBinding("<Keyboard>/k");
     modifierAction = inputs.AddAction("Modifier");
     modifierAction.AddBinding("<Gamepad>/rightShoulder");
     modifierAction.AddBinding("<Keyboard>/leftShift");
     modifierAction.AddBinding("<Keyboard>/l");
 }
Ejemplo n.º 3
0
        public virtual void Init()
        {
            var path = string.IsNullOrEmpty(dataPath) ? "Input/InputData" : dataPath;

            _inputData = Resources.Load <InputData>(path);

            if (_inputData == null)
            {
                Debug.LogError("No Input Data file, please create on 'Resources/" + path + ".asset'\nusing Create 'InputSystem/Combo'");
                throw new NullReferenceException();
            }

#if INPUT_SYSTEM
            _lookAction = new InputAction("look", binding: "");
            _moveAction = new InputAction("move", binding: "");

            _lookAction.AddBinding("<Mouse>/delta");
            _moveAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/w")
            .With("Down", "<Keyboard>/s")
            .With("Left", "<Keyboard>/a")
            .With("Right", "<Keyboard>/d");

            _lookAction.continuous = true;
            _moveAction.continuous = true;
            _moveAction.performed += Left_AxisRead;
            _lookAction.performed += Right_AxisRead;

            _lookAction.Enable();
            _moveAction.Enable();
#endif

            UnblockInputs();
        }
    public void Actions_InteractiveRebinding_CanRestrictToSpecificBinding()
    {
        var action = new InputAction();

        action.AddCompositeBinding("dpad")
        .With("Up", "<Keyboard>/w")
        .With("Down", "<Keyboard>/s")
        .With("Left", "<Keyboard>/a")
        .With("Right", "<Keyboard>/d");
        var keyboard = InputSystem.AddDevice <Keyboard>();

        using (var rebind =
                   action.PerformInteractiveRebinding()
                   .WithTargetBinding(3)     // Left
                   .Start())
        {
            InputSystem.QueueStateEvent(keyboard, new KeyboardState(Key.U));
            InputSystem.Update();

            Assert.That(rebind.completed, Is.True);
            Assert.That(action.bindings[0].path, Is.EqualTo("dpad"));
            Assert.That(action.bindings[1].path, Is.EqualTo("<Keyboard>/w"));
            Assert.That(action.bindings[2].path, Is.EqualTo("<Keyboard>/s"));
            Assert.That(action.bindings[3].path, Is.EqualTo("<Keyboard>/a"));
            Assert.That(action.bindings[4].path, Is.EqualTo("<Keyboard>/d"));
            Assert.That(action.bindings[1].overridePath, Is.Null);
            Assert.That(action.bindings[2].overridePath, Is.Null);
            Assert.That(action.bindings[3].overridePath, Is.EqualTo("<Keyboard>/u"));
            Assert.That(action.bindings[4].overridePath, Is.Null);
        }
    }
Ejemplo n.º 5
0
        private void SetupControls()
        {
            var leftRight = new InputAction("LeftRight");

            leftRight.AddCompositeBinding("Axis")
            .With("Positive", "<Keyboard>/rightArrow")
            .With("Negative", "<Keyboard>/leftArrow");
            leftRight.Enable();

            var topBottom = new InputAction("LeftRight");

            topBottom.AddCompositeBinding("Axis")
            .With("Positive", "<Keyboard>/upArrow")
            .With("Negative", "<Keyboard>/downArrow");
            topBottom.Enable();

            var fireButton = new InputAction("Fire");

            fireButton.AddBinding("<Keyboard>/space");
            fireButton.Enable();

            leftRight.started    += ctx => rotation = -ctx.ReadValue <float>();
            leftRight.canceled   += ctx => rotation = 0f;
            topBottom.started    += ctx => thrust = ctx.ReadValue <float>();
            topBottom.canceled   += ctx => thrust = 0f;
            fireButton.performed += ctx => InvokeServerRpc(CmdFire, cannonTransform.position, transform.rotation, rb.velocity);
        }
        void Start()
        {
            var map = new InputActionMap("Simple Camera Controller");

            lookAction             = map.AddAction("look", binding: "<Mouse>/delta");
            movementAction         = map.AddAction("move", binding: "<Gamepad>/leftStick");
            verticalMovementAction = map.AddAction("Vertical Movement");
            boostFactorAction      = map.AddAction("Boost Factor", binding: "<Mouse>/scroll");

            lookAction.AddBinding("<Gamepad>/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
            movementAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/w")
            .With("Up", "<Keyboard>/upArrow")
            .With("Down", "<Keyboard>/s")
            .With("Down", "<Keyboard>/downArrow")
            .With("Left", "<Keyboard>/a")
            .With("Left", "<Keyboard>/leftArrow")
            .With("Right", "<Keyboard>/d")
            .With("Right", "<Keyboard>/rightArrow");
            verticalMovementAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/pageUp")
            .With("Down", "<Keyboard>/pageDown")
            .With("Up", "<Keyboard>/e")
            .With("Down", "<Keyboard>/q")
            .With("Up", "<Gamepad>/rightshoulder")
            .With("Down", "<Gamepad>/leftshoulder");
            boostFactorAction.AddBinding("<Gamepad>/Dpad").WithProcessor("scaleVector2(x=1, y=4)");

            movementAction.Enable();
            lookAction.Enable();
            verticalMovementAction.Enable();
            boostFactorAction.Enable();
        }
Ejemplo n.º 7
0
        protected override void OnStartRunning()
        {
            _customInputs       = new NativeArray <float>(Constants.INPUT_BUFFER_CAPACITY, Allocator.Persistent);
            _customSticksInputs = new NativeArray <float2>(Constants.INPUT_BUFFER_CAPACITY, Allocator.Persistent);

            _moveAction = new InputAction("move", binding: "<Gamepad>/leftStick");
            _moveAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/w")
            .With("Down", "<Keyboard>/s")
            .With("Left", "<Keyboard>/a")
            .With("Right", "<Keyboard>/d");

            _moveAction.performed += context => { _moveInput = context.ReadValue <Vector2>(); };
            _moveAction.started   += context => { _moveInput = context.ReadValue <Vector2>(); };
            _moveAction.canceled  += context => { _moveInput = context.ReadValue <Vector2>(); };
            _moveAction.Enable();

            _mouseAction            = new InputAction("mouse", binding: "<Mouse>/position");
            _mouseAction.performed += context => { _mouseInput = context.ReadValue <Vector2>(); };
            _mouseAction.canceled  += context => { _mouseInput = context.ReadValue <Vector2>(); };
            _mouseAction.Enable();

            _lookAction = new InputAction("look", binding: "<Gamepad>/rightStick");
            _lookAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/upArrow")
            .With("Down", "<Keyboard>/downArrow")
            .With("Left", "<Keyboard>/leftArrow")
            .With("Right", "<Keyboard>/rightArrow");

            //_lookAction.AddBinding(new InputBinding("<Pointer>/delta"));

            _lookAction.performed += context => { _lookInput = context.ReadValue <Vector2>(); };
            _lookAction.canceled  += context => { _lookInput = context.ReadValue <Vector2>(); };
            _lookAction.Enable();

            //Here goes custom Actions for virtual keys 0..9
            for (var i = 0; i <= 9; i++)
            {
                var j = i;
                _customActions.Add(new InputAction($"CustomAction{j}", binding: $"<Keyboard>/{j}"));
                switch (j)
                {
                case 0:
                    _customActions.Last().AddBinding(new InputBinding("<Mouse>/leftButton"));
                    break;

                case 1:
                    _customActions.Last().AddBinding(new InputBinding("<Mouse>/rightButton"));
                    break;
                }

                _customActions.Last().performed += context => { _customInputs[j] = context.ReadValue <float>(); };
                _customActions.Last().canceled  += context => { _customInputs[j] = context.ReadValue <float>(); };
                _customActions.Last().Enable();
            }

            RegisterCustomSticks();
        }
    void Awake()
    {
        pCon          = GetComponentInChildren <PlayerController>();
        rb            = GetComponent <Rigidbody>();
        groundChecker = transform.GetChild(0);
        playerStats   = GetComponent <PlayerStatsScript>();
        myControls    = new GameInputControls();
        var moveUpAction = new InputAction("MoveUp");

        moveUpAction.AddCompositeBinding("Axis").With("Positive", "<Keyboard>/w").With("Negative", "<Keyboard>/s");
    }
Ejemplo n.º 9
0
    public void Start()
    {
        InputAction moveInput = new InputAction("move");

        moveInput.AddCompositeBinding("2DVector")
        .With("Right", "<keyboard>/leftArrow")
        .With("Left", "<keyboard>/rightArrow")
        .With("Down", "<keyboard>/downArrow")
        .With("up", "<keyboard>/upArrow");
        moveInput.performed += (e) => Move(e);
    }
Ejemplo n.º 10
0
    //------------------------------------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------------------------------------
    private void Awake()
    {
        action = new InputAction(type: InputActionType.Value, binding: "Movement");
        action.AddCompositeBinding("2DVector")
        .With("Up", "<Keyboard>/w")
        .With("Down", "<Keyboard>/s")
        .With("Left", "<Keyboard>/a")
        .With("Right", "<Keyboard>/d");

        action.AddCompositeBinding("2DVector(mode=2)")
        .With("Up", "<Gamepad>/leftStick/up")
        .With("Down", "<Gamepad>/leftStick/down")
        .With("Left", "<Gamepad>/leftStick/left")
        .With("Right", "<Gamepad>/leftStick/right");

        jumpAction = new InputAction(type: InputActionType.Button, binding: "<Gamepad>/buttonSouth", interactions: "Press");
        jumpAction.AddBinding("<Keyboard>/space", interactions: "Press");

        runAction = new InputAction(type: InputActionType.Button, binding: "<Gamepad>/leftStickPress");
        runAction.AddBinding("<Keyboard>/leftShift");
    }
Ejemplo n.º 11
0
    // Start is called before the first frame update
    void Start()
    {
        jump     = new InputAction("Jump", binding: "<keyboard>/space");
        movement = new InputAction("Movement", binding: "<Gamepad>/leftStick");
        movement.AddCompositeBinding("Dpad")
        .With("Up", "<keyboard>/w")
        .With("Down", "<keyboard>/s")
        .With("Left", "<keyboard>/a")
        .With("Right", "<keyboard>/d");

        movement.Enable();
        jump.Enable();
    }
Ejemplo n.º 12
0
    void Start()
    {
        var input = new InputAction("WASD");

        ign.AddCompositeBinding("2DVector").With("Up", "<Keyboard>/" + GameManager.GM.forward).With("Down", "<Keyboard>/" + GameManager.GM.Backward).With("Left", "<Keyboard>/" + GameManager.GM.Left).With("Right", "<Keyboard>/" + GameManager.GM.Right);

        Debug.Log(input);
        ign.Enable();
        ign.performed += OnMove;
        ign.canceled  += OnMove;

        //control.Movement.Move.AddCompositeBinding("Dpad").With("Up", "<Keyboard>/W");
    }
        void InitializeValuesNIS()
        {
            move = new InputAction("Left Stick", InputActionType.Value, "<Gamepad>/leftStick");
            move.AddCompositeBinding("2DVector")
            .With("Up", "<Keyboard>/w")
            .With("Down", "<Keyboard>/s")
            .With("Left", "<Keyboard>/a")
            .With("Right", "<Keyboard>/d");

            look = new InputAction("Right Stick", InputActionType.Value, "<Gamepad>/rightStick");
            look.AddBinding("<Pointer>/Delta")
            .WithProcessor("ScaleVector2(x=0.1,y=0.1)");
            jump = new InputAction("Jump", InputActionType.Button, "<Gamepad>/buttonSouth");
            jump.AddBinding("<Keyboard>/space");
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(this);
#endif
        }
Ejemplo n.º 14
0
//    InputAction jump;

    void Start()
    {
        movement = new InputAction("PlayerMovement", binding: "<Gamepad>/leftStick");
        movement.AddCompositeBinding("Dpad")
        .With("Up", "<Keyboard>/w")
        .With("Up", "<Keyboard>/upArrow")
        .With("Down", "<Keyboard>/s")
        .With("Down", "<Keyboard>/downArrow")
        .With("Left", "<Keyboard>/a")
        .With("Left", "<Keyboard>/leftArrow")
        .With("Right", "<Keyboard>/d")
        .With("Right", "<Keyboard>/rightArrow");

//        jump = new InputAction("PlayerJump", binding: "<Gamepad>/a");
//        jump.AddBinding("<Keyboard>/space");

        movement.Enable();
//        jump.Enable();
    }
Ejemplo n.º 15
0
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent <Animator>();

        //Move 액션 생성 및 타입 설정
        moveAction = new InputAction("Move", InputActionType.Value);

        //Move 액션의 복합 바인딩 정보 정의
        moveAction.AddCompositeBinding("2DVector")
        .With("Up", "<Keyboard>/w")
        .With("Down", "<Keyboard>/s")
        .With("Left", "<Keyboard>/a")
        .With("Right", "<Keyboard>/d");

        //Move 액션의 performed, canceled 이벤트 연결
        moveAction.performed += ctx => {
            Vector2 dir = ctx.ReadValue <Vector2>();
            moveDir = new Vector3(dir.x, 0, dir.y);
            //Warrior_Run 애니메이션 실행
            anim.SetFloat("Movement", dir.magnitude);
        };

        moveAction.canceled += ctx => {
            moveDir = Vector3.zero;
            anim.SetFloat("Movement", 0.0f);
        };

        //Move 액션의 활성화
        moveAction.Enable();

        //Attack 액션 생성
        attackAction = new InputAction("Attack",
                                       InputActionType.Button,
                                       "<Keyboard>/space");

        //Attack 액션의 performed 이벤트 연결
        attackAction.performed += ctx => {
            anim.SetTrigger("Attack");
        };
        //Attack 액션의 활성화
        attackAction.Enable();
    }
Ejemplo n.º 16
0
    protected override void OnStartRunning()
    {
        moveAction = new InputAction("move", binding: "<Gamepad>/rightStick");
        moveAction.AddCompositeBinding("Dpad")
        .With("Up", "<Keyboard>/w")
        .With("Down", "<Keyboard>/s")
        .With("Left", "<Keyboard>/a")
        .With("Right", "<Keyboard>/d");

        moveAction.performed += context =>
        {
            !-URDATA - ! = context.ReadValue <Vector2>();
        };
        moveAction.canceled += context =>
        {
            !-URDATA - ! = context.ReadValue <Vector2>();
        };
        moveAction.Enable();

        lookAction            = new InputAction("look", binding: "<Mouse>/position");
        lookAction.performed += context =>
        {
            !-URDATA - ! = context.ReadValue <Vector2>();
        };
        lookAction.canceled += context =>
        {
            !-URDATA - ! = context.ReadValue <Vector2>();
        };
        lookAction.Enable();

        shootAction            = new InputAction("shoot", binding: "<Mouse>/leftButton");
        shootAction.performed += context =>
        {
            !-URDATA - ! = context.ReadValue <float>();
        };
        shootAction.canceled += context =>
        {
            !-URDATA - != context.ReadValue <float>();
        };
        shootAction.Enable();
    }
Ejemplo n.º 17
0
    public void UI_CanDriveVirtualMouseCursorFromGamepad()
    {
        const float kCursorSpeed = 100;
        const float kScrollSpeed = 25;

        var eventSystemGO = new GameObject();

        eventSystemGO.SetActive(false);
        eventSystemGO.AddComponent <EventSystem>();
        eventSystemGO.AddComponent <InputSystemUIInputModule>();

        var canvasGO = new GameObject();

        canvasGO.SetActive(false);
        canvasGO.AddComponent <Canvas>();

        var cursorGO = new GameObject();

        cursorGO.SetActive(false);
        var cursorTransform = cursorGO.AddComponent <RectTransform>();
        var cursorInput     = cursorGO.AddComponent <VirtualMouseInput>();

        cursorInput.cursorSpeed     = kCursorSpeed;
        cursorInput.scrollSpeed     = kScrollSpeed;
        cursorInput.cursorTransform = cursorTransform;
        cursorTransform.SetParent(canvasGO.transform, worldPositionStays: false);
        cursorTransform.pivot            = new Vector2(0.5f, 0.5f);
        cursorTransform.anchorMin        = Vector2.zero;
        cursorTransform.anchorMax        = Vector2.zero;
        cursorTransform.anchoredPosition = new Vector2(123, 234);

        var positionAction      = new InputAction(type: InputActionType.Value, binding: "<Gamepad>/*stick");
        var leftButtonAction    = new InputAction(binding: "<Gamepad>/buttonSouth");
        var rightButtonAction   = new InputAction(binding: "<Gamepad>/rightShoulder");
        var middleButtonAction  = new InputAction(binding: "<Gamepad>/leftShoulder");
        var forwardButtonAction = new InputAction(binding: "<Gamepad>/buttonWest");
        var backButtonAction    = new InputAction(binding: "<Gamepad>/buttonEast");
        var scrollWheelAction   = new InputAction();

        scrollWheelAction.AddCompositeBinding("2DVector(mode=2)")
        .With("Up", "<Gamepad>/leftTrigger")
        .With("Down", "<Gamepad>/rightTrigger")
        .With("Left", "<Gamepad>/dpad/left")
        .With("Right", "<Gamepad>/dpad/right");

        cursorInput.stickAction         = new InputActionProperty(positionAction);
        cursorInput.leftButtonAction    = new InputActionProperty(leftButtonAction);
        cursorInput.rightButtonAction   = new InputActionProperty(rightButtonAction);
        cursorInput.middleButtonAction  = new InputActionProperty(middleButtonAction);
        cursorInput.scrollWheelAction   = new InputActionProperty(scrollWheelAction);
        cursorInput.forwardButtonAction = new InputActionProperty(forwardButtonAction);
        cursorInput.backButtonAction    = new InputActionProperty(backButtonAction);

        var gamepad = InputSystem.AddDevice <Gamepad>();

        // Get rid of deadzones to simplify computations.
        InputSystem.settings.defaultDeadzoneMin = 0;
        InputSystem.settings.defaultDeadzoneMax = 1;

        eventSystemGO.SetActive(true);
        canvasGO.SetActive(true);
        cursorGO.SetActive(true);

        // Make sure the component added a virtual mouse.
        var virtualMouse = Mouse.current;

        Assert.That(virtualMouse, Is.Not.Null);
        Assert.That(virtualMouse.layout, Is.EqualTo("VirtualMouse"));
        Assert.That(cursorInput.virtualMouse, Is.SameAs(virtualMouse));

        // Make sure we can disable and re-enable the component.
        cursorGO.SetActive(false);

        Assert.That(Mouse.current, Is.Null);

        cursorGO.SetActive(true);

        Assert.That(Mouse.current, Is.Not.Null);
        Assert.That(Mouse.current, Is.SameAs(virtualMouse));

        // Ensure everything is at default values.
        // Starting position should be that of the cursor's initial transform.
        Assert.That(virtualMouse.position.ReadValue(), Is.EqualTo(new Vector2(123, 234)).Using(Vector2EqualityComparer.Instance));
        Assert.That(virtualMouse.delta.ReadValue(), Is.EqualTo(Vector2.zero));
        Assert.That(virtualMouse.scroll.ReadValue(), Is.EqualTo(Vector2.zero));
        Assert.That(virtualMouse.leftButton.isPressed, Is.False);
        Assert.That(virtualMouse.rightButton.isPressed, Is.False);
        Assert.That(virtualMouse.middleButton.isPressed, Is.False);
        Assert.That(cursorTransform.anchoredPosition, Is.EqualTo(new Vector2(123, 234)));

        // Now move the mouse cursor with the left stick and ensure we get a response.
        currentTime = 1;
        Set(gamepad.leftStick, new Vector2(0.25f, 0.75f));

        // No time has passed yet so first frame shouldn't move at all.
        Assert.That(virtualMouse.position.ReadValue(), Is.EqualTo(new Vector2(123, 234)).Using(Vector2EqualityComparer.Instance));
        Assert.That(virtualMouse.delta.ReadValue(), Is.EqualTo(Vector2.zero));
        Assert.That(cursorTransform.anchoredPosition, Is.EqualTo(new Vector2(123, 234)));

        currentTime = 1.4;
        InputSystem.Update();

        const float kFirstDeltaX = kCursorSpeed * 0.25f * 0.4f;
        const float kFirstDeltaY = kCursorSpeed * 0.75f * 0.4f;

        Assert.That(virtualMouse.position.ReadValue(), Is.EqualTo(new Vector2(123 + kFirstDeltaX, 234 + kFirstDeltaY)).Using(Vector2EqualityComparer.Instance));
        Assert.That(virtualMouse.delta.ReadValue(), Is.EqualTo(new Vector2(kFirstDeltaX, kFirstDeltaY)).Using(Vector2EqualityComparer.Instance));
        Assert.That(cursorTransform.anchoredPosition, Is.EqualTo(new Vector2(123 + kFirstDeltaX, 234 + kFirstDeltaY)).Using(Vector2EqualityComparer.Instance));

        // Each update should move the cursor along while the stick is actuated.
        currentTime = 2;
        InputSystem.Update();

        const float kSecondDeltaX = kCursorSpeed * 0.25f * 0.6f;
        const float kSecondDeltaY = kCursorSpeed * 0.75f * 0.6f;

        Assert.That(virtualMouse.position.ReadValue(), Is.EqualTo(new Vector2(123 + kFirstDeltaX + kSecondDeltaX, 234 + kFirstDeltaY + kSecondDeltaY)).Using(Vector2EqualityComparer.Instance));
        Assert.That(virtualMouse.delta.ReadValue(), Is.EqualTo(new Vector2(kSecondDeltaX, kSecondDeltaY)).Using(Vector2EqualityComparer.Instance));
        Assert.That(cursorTransform.anchoredPosition, Is.EqualTo(new Vector2(123 + kFirstDeltaX + kSecondDeltaX, 234 + kFirstDeltaY + kSecondDeltaY)).Using(Vector2EqualityComparer.Instance));

        // Only the final state of the stick in an update should matter.
        currentTime = 3;
        InputSystem.QueueStateEvent(gamepad, new GamepadState {
            leftStick = new Vector2(0.34f, 0.45f)
        });
        InputSystem.QueueStateEvent(gamepad, new GamepadState {
            leftStick = new Vector2(0.45f, 0.56f)
        });
        InputSystem.Update();

        const float kThirdDeltaX = kCursorSpeed * 0.45f;
        const float kThirdDeltaY = kCursorSpeed * 0.56f;

        Assert.That(virtualMouse.position.ReadValue(), Is.EqualTo(new Vector2(123 + kFirstDeltaX + kSecondDeltaX + kThirdDeltaX, 234 + kFirstDeltaY + kSecondDeltaY + kThirdDeltaY)).Using(Vector2EqualityComparer.Instance));
        Assert.That(virtualMouse.delta.ReadValue(), Is.EqualTo(new Vector2(kThirdDeltaX, kThirdDeltaY)).Using(Vector2EqualityComparer.Instance));
        Assert.That(cursorTransform.anchoredPosition, Is.EqualTo(new Vector2(123 + kFirstDeltaX + kSecondDeltaX + kThirdDeltaX, 234 + kFirstDeltaY + kSecondDeltaY + kThirdDeltaY)).Using(Vector2EqualityComparer.Instance));

        var leftClickAction    = new InputAction(binding: "<Mouse>/leftButton");
        var middleClickAction  = new InputAction(binding: "<Mouse>/middleButton");
        var rightClickAction   = new InputAction(binding: "<Mouse>/rightButton");
        var forwardClickAction = new InputAction(binding: "<Mouse>/forwardButton");
        var backClickAction    = new InputAction(binding: "<Mouse>/backButton");
        var scrollAction       = new InputAction(binding: "<Mouse>/scroll");

        leftClickAction.Enable();
        middleClickAction.Enable();
        rightClickAction.Enable();
        forwardClickAction.Enable();
        backClickAction.Enable();
        scrollAction.Enable();

        // Press buttons.
        PressAndRelease(gamepad.buttonSouth);
        Assert.That(leftClickAction.triggered);
        PressAndRelease(gamepad.rightShoulder);
        Assert.That(rightClickAction.triggered);
        PressAndRelease(gamepad.leftShoulder);
        Assert.That(middleClickAction.triggered);
        PressAndRelease(gamepad.buttonWest);
        Assert.That(forwardClickAction.triggered);
        PressAndRelease(gamepad.buttonEast);
        Assert.That(backClickAction.triggered);

        // Scroll wheel.
        Set(gamepad.leftTrigger, 0.5f);
        Assert.That(scrollAction.ReadValue <Vector2>(), Is.EqualTo(new Vector2(0, kScrollSpeed * 0.5f)).Using(Vector2EqualityComparer.Instance));
        Set(gamepad.rightTrigger, 0.3f);
        Assert.That(scrollAction.ReadValue <Vector2>(), Is.EqualTo(new Vector2(0, kScrollSpeed * (0.5f - 0.3f))).Using(Vector2EqualityComparer.Instance));
        Set(gamepad.leftTrigger, 0);
        Assert.That(scrollAction.ReadValue <Vector2>(), Is.EqualTo(new Vector2(0, -kScrollSpeed * 0.3f)).Using(Vector2EqualityComparer.Instance));
        Press(gamepad.dpad.left);
        Assert.That(scrollAction.ReadValue <Vector2>(), Is.EqualTo(new Vector2(-kScrollSpeed, -kScrollSpeed * 0.3f)).Using(Vector2EqualityComparer.Instance));
        Press(gamepad.dpad.right);
        Assert.That(scrollAction.ReadValue <Vector2>(), Is.EqualTo(new Vector2(0, -kScrollSpeed * 0.3f)).Using(Vector2EqualityComparer.Instance));
        Release(gamepad.dpad.left);
        Assert.That(scrollAction.ReadValue <Vector2>(), Is.EqualTo(new Vector2(kScrollSpeed, -kScrollSpeed * 0.3f)).Using(Vector2EqualityComparer.Instance));
    }
Ejemplo n.º 18
0
        void RegisterInputs()
        {
#if USE_INPUT_SYSTEM
            var map = new InputActionMap("Free Camera");

            lookAction  = map.AddAction("look", binding: "<Mouse>/delta");
            moveAction  = map.AddAction("move", binding: "<Gamepad>/leftStick");
            speedAction = map.AddAction("speed", binding: "<Gamepad>/dpad");
            yMoveAction = map.AddAction("yMove");

            lookAction.AddBinding("<Gamepad>/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
            moveAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/w")
            .With("Up", "<Keyboard>/upArrow")
            .With("Down", "<Keyboard>/s")
            .With("Down", "<Keyboard>/downArrow")
            .With("Left", "<Keyboard>/a")
            .With("Left", "<Keyboard>/leftArrow")
            .With("Right", "<Keyboard>/d")
            .With("Right", "<Keyboard>/rightArrow");
            speedAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/home")
            .With("Down", "<Keyboard>/end");
            yMoveAction.AddCompositeBinding("Dpad")
            .With("Up", "<Keyboard>/pageUp")
            .With("Down", "<Keyboard>/pageDown")
            .With("Up", "<Keyboard>/e")
            .With("Down", "<Keyboard>/q")
            .With("Up", "<Gamepad>/rightshoulder")
            .With("Down", "<Gamepad>/leftshoulder");

            moveAction.Enable();
            lookAction.Enable();
            speedAction.Enable();
            fireAction.Enable();
            yMoveAction.Enable();
#endif

#if UNITY_EDITOR && !USE_INPUT_SYSTEM
            List <InputManagerEntry> inputEntries = new List <InputManagerEntry>();

            // Add new bindings
            inputEntries.Add(new InputManagerEntry {
                name = kRightStickX, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fourth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f
            });
            inputEntries.Add(new InputManagerEntry {
                name = kRightStickY, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fifth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f, invert = true
            });

            inputEntries.Add(new InputManagerEntry {
                name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page up", altBtnPositive = "joystick button 5", btnNegative = "page down", altBtnNegative = "joystick button 4", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f
            });
            inputEntries.Add(new InputManagerEntry {
                name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "q", btnNegative = "e", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f
            });

            inputEntries.Add(new InputManagerEntry {
                name = kSpeedAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "home", btnNegative = "end", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f
            });
            inputEntries.Add(new InputManagerEntry {
                name = kSpeedAxis, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f
            });

            InputRegistering.RegisterInputs(inputEntries);
#endif
        }
Ejemplo n.º 19
0
    /// <summary>
    /// Adds a new <see cref="KeybindOverride"/> to the list of overrides. This will remove any already existing overrides.
    /// </summary>
    /// <param name="keybindOverride"></param>
    public static void AddKeybindOverride(KeybindOverride keybindOverride)
    {
        // Do not override keybinds if paths are not in acceptable bounds
        if (keybindOverride.OverrideKeybindPaths.Count <= 0 || keybindOverride.OverrideKeybindPaths.Count > 4)
        {
            return;
        }
        // Remove anyexisting override to prevent duplicates
        AllOverrides.RemoveAll(x => x.InputActionName == keybindOverride.InputActionName && x.CompositeKeybindName == keybindOverride.CompositeKeybindName);

        // Grab our CMInput object and the map our action map is in.
        CMInput        input = CMInputCallbackInstaller.InputInstance;
        InputActionMap map   = input.asset.actionMaps.Where(x => x.actions.Any(y => y.name == keybindOverride.InputActionName)).FirstOrDefault();

        if (map is null)
        {
            return;
        }

        InputAction action = map.FindAction(keybindOverride.InputActionName);

        // Determine what existing bindings we need to erase
        List <InputBinding> toErase = new List <InputBinding>();
        // Grab our composite keybind
        InputBinding bindingToOverride = action.bindings.Where(x => x.name == keybindOverride.CompositeKeybindName).FirstOrDefault();

        if (bindingToOverride == null) // This is not a composite keybind, just grab the first one
        {
            bindingToOverride = action.bindings.First();
        }
        toErase.Add(bindingToOverride);
        // Grab all composite pieces
        for (int i = action.GetBindingIndex(bindingToOverride) + 1; i < action.bindings.Count; i++)
        {
            if (action.bindings[i].isPartOfComposite)
            {
                toErase.Add(action.bindings[i]);
            }
            else
            {
                break;
            }
        }
        // Reverse them so that the Composite keybind is erased last, and prevents errors.
        toErase.Reverse();
        // Erase the bindings
        foreach (InputBinding binding in toErase)
        {
            Debug.Log($"Deleting {binding.name} from {action.name}");
            action.ChangeBinding(action.GetBindingIndex(binding)).Erase();
        }

        // Add a new binding depending on some conditions
        switch (keybindOverride.OverrideKeybindPaths.Count)
        {
        case 1:     // With one override path, make a regular binding
            action.AddBinding(keybindOverride.OverrideKeybindPaths[0]);
            break;

        case 2 when keybindOverride.IsAxisComposite:     // Create a 1D Axis if we need to.
            action.AddCompositeBinding("1DAxis")
            .With("positive", keybindOverride.OverrideKeybindPaths[0])
            .With("negative", keybindOverride.OverrideKeybindPaths[1]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        case 2 when !keybindOverride.IsAxisComposite:     // Else, create a composite.
            action.AddCompositeBinding("ButtonWithOneModifier")
            .With("modifier", keybindOverride.OverrideKeybindPaths[0])
            .With("button", keybindOverride.OverrideKeybindPaths[1]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        case 3:     // No 1.5D Axis, so just a composite with two modifiers.
            action.AddCompositeBinding("ButtonWithTwoModifiers")
            .With("modifier2", keybindOverride.OverrideKeybindPaths[0])
            .With("modifier1", keybindOverride.OverrideKeybindPaths[1])
            .With("button", keybindOverride.OverrideKeybindPaths[2]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        case 4 when keybindOverride.IsAxisComposite:     // 4 paths means a 2D Axis composite
            action.AddCompositeBinding("2DVector(mode=2)")
            .With("up", keybindOverride.OverrideKeybindPaths[0])
            .With("left", keybindOverride.OverrideKeybindPaths[1])
            .With("down", keybindOverride.OverrideKeybindPaths[2])
            .With("right", keybindOverride.OverrideKeybindPaths[3]);
            RenameCompositeBinding(action, keybindOverride);
            break;

        default: break;
        }

        Debug.Log($"Added keybind override for {keybindOverride.InputActionName}.");
        AllOverrides.Add(keybindOverride);
    }