public void Actions_InteractiveRebinding_CanBeRestrictedToSpecificBindingGroups()
    {
        var action = new InputAction();

        action.AddBinding("<Keyboard>/space", groups: "Keyboard");
        action.AddBinding("<Gamepad>/buttonSouth", groups: "Gamepad");

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

        using (var rebind =
                   action.PerformInteractiveRebinding()
                   .WithBindingGroup("Gamepad")
                   .Start())
        {
            Assert.That(rebind.bindingMask, Is.EqualTo(new InputBinding {
                groups = "Gamepad"
            }));

            InputSystem.QueueStateEvent(gamepad, new GamepadState().WithButton(GamepadButton.North));
            InputSystem.Update();

            Assert.That(rebind.completed, Is.True);
            Assert.That(action.bindings[0].path, Is.EqualTo("<Keyboard>/space"));
            Assert.That(action.bindings[0].overridePath, Is.Null);
            Assert.That(action.bindings[1].path, Is.EqualTo("<Gamepad>/buttonSouth"));
            Assert.That(action.bindings[1].overridePath, Is.EqualTo("<Gamepad>/buttonNorth"));
        }
    }
Example #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");
 }
Example #3
0
    //------------------------------------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------------------------------------
    protected override void Awake()
    {
        base.Awake();

        action = new InputAction(type: InputActionType.Value);
        action.AddBinding("<Gamepad>/rightStick", processors: "ScaleVector2(x=1, y=1)");
        action.AddBinding("<Mouse>/delta", processors: "ScaleVector2(x=0.5, y=0.5),ScaleVector2(x=0.1, y=0.1)");
    }
Example #4
0
    // Start is called before the first frame update
    void Start()
    {
        inputAction = new InputAction(binding: "<Gamepad>/buttonSouth");
        inputAction.AddBinding("<Mouse>/leftButton");
        Debug.Log(inputAction);
        Debug.Log(inputActionReference);

        inputAction.AddBinding(inputActionReference.action.bindings[0].ToString());
        Debug.Log(inputAction);
    }
Example #5
0
    private void RegisterDevices()
    {
        // For now, we keep this simple:
        // We look at all connected devices and add as "the" inputDevice if the type matches the `device` field selected.
        // We don't do anything with ambidextrous controllers or if, say, the user has two HMDs connected.
        // Nor do we do anything if a controller is disconnected.
        List <UnityEngine.XR.InputDevice> allInputDevices = new List <UnityEngine.XR.InputDevice>();

        UnityEngine.XR.InputDevices.GetDevices(allInputDevices);
        List <XRNodeState> allNodeStates = new List <XRNodeState>();

        UnityEngine.XR.InputTracking.GetNodeStates(allNodeStates);

        for (int i = 0; i < allInputDevices.Count; i++)
        {
            if ((allInputDevices[i].characteristics & UnityEngine.XR.InputDeviceCharacteristics.HeadMounted) == UnityEngine.XR.InputDeviceCharacteristics.HeadMounted)
            {
                if (deviceType == DeviceType.HMD)
                {
                    inputDevice = allInputDevices[i];

                    positionAction = new InputAction();
                    positionAction.AddBinding(new InputBinding("<XRHMD>/centerEyePosition"));
                    positionAction.performed += OnPositionAction;
                    positionAction.Enable();
                    rotationAction = new InputAction();
                    rotationAction.AddBinding(new InputBinding("<XRHMD>/centerEyeRotation"));
                    rotationAction.performed += OnRotationAction;
                    rotationAction.Enable();
                }
            }
            else if ((allInputDevices[i].characteristics & UnityEngine.XR.InputDeviceCharacteristics.HeldInHand) == UnityEngine.XR.InputDeviceCharacteristics.HeldInHand)
            {
                if ((allInputDevices[i].characteristics & UnityEngine.XR.InputDeviceCharacteristics.Left) == UnityEngine.XR.InputDeviceCharacteristics.Left)
                {
                    if (deviceType == DeviceType.LeftHand)
                    {
                        inputDevice = allInputDevices[i];

                        poseAction = new InputAction();
                        poseAction.AddBinding("<XRController>{LeftHand}/devicePose");
                        poseAction.performed += OnPoseAction;
                        poseAction.Enable();
                    }
                }
                else if ((allInputDevices[i].characteristics & UnityEngine.XR.InputDeviceCharacteristics.Right) == UnityEngine.XR.InputDeviceCharacteristics.Right)
                {
                    if (deviceType == DeviceType.RightHand)
                    {
                        inputDevice = allInputDevices[i];

                        poseAction = new InputAction();
                        poseAction.AddBinding("<XRController>{RightHand}/devicePose");
                        poseAction.performed += OnPoseAction;
                        poseAction.Enable();
                    }
                }
            }
        }
    }
Example #6
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();
        }
Example #8
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();
        }
Example #9
0
    // Use this for initialization
    void Start()
    {
        pen_holder = transform.Find("Pen");
        if (pen_holder == null)
        {
            throw new Exception("Gameobject \"Pen\" is not found!");
        }

        pen_rotation = pen_holder.Find("RotationHolder");

        m_originalPos  = pen_holder.position;
        m_rotateAdjust = pen_rotation.GetChild(0).localEulerAngles;

        m_penButtonAction            = new InputAction(name: "PenButtonAction", InputActionType.PassThrough, binding: "<pen>/<button>");
        m_penButtonAction.performed += callbackContext => OnButtonPress(callbackContext.control as ButtonControl);
        m_penButtonAction.Enable();

        m_penVector2Action            = new InputAction(name: "PenVectorAction", InputActionType.PassThrough, binding: "<pen>/<vector2>");
        m_penVector2Action.performed += callbackContext => OnVector2Change(callbackContext.control as Vector2Control);
        m_penVector2Action.Enable();

        m_penAxisAction = new InputAction(name: "PenAxisAction", InputActionType.PassThrough, binding: "<pen>/twist");
        m_penAxisAction.AddBinding("<pen>/pressure");
        m_penAxisAction.performed += callbackContext => OnAxisChange(callbackContext.control as AxisControl);
        m_penAxisAction.Enable();
    }
    void Awake()
    {
        action = new InputAction("Test");

        action.AddBinding("<Keyboard>/e")
        .WithInteraction("Press");

        action.performed += ctx =>
        {
            Debug.Log("!");
        };
    }
Example #11
0
    private void Update()
    {
        if (canvasGroup.interactable && DataUtility.gameData.player1Device != null && DataUtility.gameData.player2Device != null)
        {
            startGame.SetActive(true);
        }
        else
        {
            startGame.SetActive(false);
        }

        if (canvasGroup.interactable)
        {
            if (!init)
            {
                var devices = InputSystem.devices.Where(x => x is Gamepad || x is Keyboard);

                foreach (var d in devices)
                {
                    GameObject go = Instantiate(prefab, content);
                    UIDevices.Add(d, go);
                    go.GetComponent <ControllerSide>().Initialize(d, this);
                }

                InputSystem.onDeviceChange += OnChange;

                startAction = new InputAction();
                startAction.AddBinding("<Gamepad>/Start");
                startAction.AddBinding("<Keyboard>/Enter");
                startAction.performed += OnStart;
                startAction.Enable();

                init = true;
            }
        }
    }
        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
        }
    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();
    }
Example #14
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");
    }
    public void Actions_ReleasedHoldInteractionIsCancelled_WithMultipleBindings()
    {
        var keyboard = InputSystem.AddDevice <Keyboard>();

        var action = new InputAction(binding: "<Keyboard>/space", interactions: "hold(duration=0.4)");

        action.AddBinding("<Keyboard>/s");
        action.Enable();

        using (var trace = new InputActionTrace(action))
        {
            // Press and hold.
            Press(keyboard.spaceKey, time: 10);

            Assert.That(trace, Started <HoldInteraction>(action, keyboard.spaceKey, time: 10, value: 1.0));
            Assert.That(action.ReadValue <float>(), Is.EqualTo(1));
            Assert.That(action.phase, Is.EqualTo(InputActionPhase.Started));

            trace.Clear();

            // Exceed hold time. Make sure action performs and *stays* performed.
            currentTime = 10.5;
            InputSystem.Update();

            Assert.That(trace,
                        Performed <HoldInteraction>(action, keyboard.spaceKey, time: 10.5, duration: 0.5, value: 1.0));
            Assert.That(action.phase, Is.EqualTo(InputActionPhase.Performed));
            Assert.That(action.ReadValue <float>(), Is.EqualTo(1));

            trace.Clear();

            // Release.
            Release(keyboard.spaceKey, time: 10.6);

            Assert.That(trace, Canceled <HoldInteraction>(action, keyboard.spaceKey, duration: 0.6, time: 10.6, value: 0.0));
            Assert.That(action.phase, Is.EqualTo(InputActionPhase.Waiting));
            Assert.That(action.ReadValue <float>(), Is.Zero);
        }
    }
Example #16
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
        }
        public void TPD_UpdateOptions_AffectProcessedData()
        {
            var tpd    = CreateGameObjectWithTPD();
            var device = InputSystem.AddDevice <TestHMD>();

            InputEventPtr stateEvent;

            using (StateEvent.From(device, out stateEvent))
            {
                var positionAction = new InputAction();
                positionAction.AddBinding("<TestHMD>/vector3");

                var rotationAction = new InputAction();
                rotationAction.AddBinding("<TestHMD>/quaternion");

                tpd.positionAction = positionAction;
                tpd.rotationAction = rotationAction;

                // before render only
                Reset(tpd.gameObject);
                tpd.updateType   = TrackedPoseDriver.UpdateType.BeforeRender;
                tpd.trackingType = TrackedPoseDriver.TrackingType.RotationAndPosition;

                device.quaternion.WriteValueInto(stateEvent, testrot);
                device.vector3.WriteValueInto(stateEvent, testpos);

                InputSystem.QueueEvent(stateEvent);
                InputSystem.Update(InputUpdateType.Dynamic);
                Assert.That(tpd.gameObject.transform.position, Is.Not.EqualTo(testpos));
                Assert.That(!tpd.gameObject.transform.rotation.Equals(testrot));

                Reset(tpd.gameObject);
                InputSystem.QueueEvent(stateEvent);
                InputSystem.Update(InputUpdateType.BeforeRender);
                Assert.That(tpd.gameObject.transform.position, Is.EqualTo(testpos));
                Assert.That(tpd.gameObject.transform.rotation.Equals(testrot));

                // update only
                Reset(tpd.gameObject);
                tpd.updateType   = TrackedPoseDriver.UpdateType.Update;
                tpd.trackingType = TrackedPoseDriver.TrackingType.RotationAndPosition;

                InputSystem.QueueEvent(stateEvent);
                InputSystem.Update(InputUpdateType.Dynamic);
                Assert.That(tpd.gameObject.transform.position, Is.EqualTo(testpos));
                Assert.That(tpd.gameObject.transform.rotation.Equals(testrot));

                Reset(tpd.gameObject);
                InputSystem.QueueEvent(stateEvent);
                InputSystem.Update(InputUpdateType.BeforeRender);
                Assert.That(tpd.gameObject.transform.position, Is.Not.EqualTo(testpos));
                Assert.That(!tpd.gameObject.transform.rotation.Equals(testrot));


                // check the rot/pos case also Update AND Render.
                tpd.updateType   = TrackedPoseDriver.UpdateType.UpdateAndBeforeRender;
                tpd.trackingType = TrackedPoseDriver.TrackingType.PositionOnly;
                Reset(tpd.gameObject);

                InputSystem.QueueEvent(stateEvent);
                InputSystem.Update(InputUpdateType.Dynamic);
                Assert.That(tpd.gameObject.transform.position, Is.EqualTo(testpos));
                Assert.That(!tpd.gameObject.transform.rotation.Equals(testrot));

                tpd.trackingType = TrackedPoseDriver.TrackingType.RotationOnly;
                Reset(tpd.gameObject);
                InputSystem.QueueEvent(stateEvent);
                InputSystem.Update(InputUpdateType.BeforeRender);
                Assert.That(tpd.gameObject.transform.position, Is.Not.EqualTo(testpos));
                Assert.That(tpd.gameObject.transform.rotation.Equals(testrot));
            }
        }
    public void Actions_CanPerformPressInteraction()
    {
        var gamepad = InputSystem.AddDevice <Gamepad>();

        // We add a second input device (and bind to it), to test that the binding
        // conflict resolution will not interfere with the interaction handling.
        InputSystem.AddDevice <Keyboard>();

        // Test all three press behaviors concurrently.
        var pressOnlyAction = new InputAction("PressOnly", binding: "<Gamepad>/buttonSouth", interactions: "press(behavior=0)");

        pressOnlyAction.AddBinding("<Keyboard>/a");
        var releaseOnlyAction = new InputAction("ReleaseOnly", binding: "<Gamepad>/buttonSouth", interactions: "press(behavior=1)");

        releaseOnlyAction.AddBinding("<Keyboard>/s");
        var pressAndReleaseAction = new InputAction("PressAndRelease", binding: "<Gamepad>/buttonSouth", interactions: "press(behavior=2)");

        pressAndReleaseAction.AddBinding("<Keyboard>/d");

        pressOnlyAction.Enable();
        releaseOnlyAction.Enable();
        pressAndReleaseAction.Enable();

        using (var pressOnly = new InputActionTrace(pressOnlyAction))
            using (var releaseOnly = new InputActionTrace(releaseOnlyAction))
                using (var pressAndRelease = new InputActionTrace(pressAndReleaseAction))
                {
                    runtime.currentTime = 1;
                    Press(gamepad.buttonSouth);

                    Assert.That(pressOnly,
                                Started <PressInteraction>(pressOnlyAction, gamepad.buttonSouth, value: 1.0, time: 1)
                                .AndThen(Performed <PressInteraction>(pressOnlyAction, gamepad.buttonSouth, time: 1, duration: 0, value: 1.0)));
                    Assert.That(releaseOnly, Started <PressInteraction>(releaseOnlyAction, gamepad.buttonSouth, time: 1, value: 1.0));
                    Assert.That(pressAndRelease,
                                Started <PressInteraction>(pressAndReleaseAction, gamepad.buttonSouth, time: 1, value: 1.0)
                                .AndThen(Performed <PressInteraction>(pressAndReleaseAction, gamepad.buttonSouth, time: 1, duration: 0, value: 1.0)));

                    pressOnly.Clear();
                    releaseOnly.Clear();
                    pressAndRelease.Clear();

                    runtime.currentTime = 2;
                    Release(gamepad.buttonSouth);

                    Assert.That(pressOnly, Canceled <PressInteraction>(pressOnlyAction, gamepad.buttonSouth, value: 0.0, time: 2, duration: 1));
                    Assert.That(releaseOnly,
                                Performed <PressInteraction>(releaseOnlyAction, gamepad.buttonSouth, value: 0.0, time: 2, duration: 1)
                                .AndThen(Canceled <PressInteraction>(releaseOnlyAction, gamepad.buttonSouth, value: 0.0, time: 2, duration: 1)));
                    Assert.That(pressAndRelease,
                                Performed <PressInteraction>(pressAndReleaseAction, gamepad.buttonSouth, value: 0.0, time: 2, duration: 1)
                                .AndThen(Canceled <PressInteraction>(pressAndReleaseAction, gamepad.buttonSouth, value: 0.0, time: 2, duration: 1)));

                    pressOnly.Clear();
                    releaseOnly.Clear();
                    pressAndRelease.Clear();

                    runtime.currentTime = 5;
                    Press(gamepad.buttonSouth);

                    Assert.That(pressOnly,
                                Started <PressInteraction>(pressOnlyAction, gamepad.buttonSouth, value: 1.0, time: 5)
                                .AndThen(Performed <PressInteraction>(pressOnlyAction, gamepad.buttonSouth, time: 5, duration: 0, value: 1.0)));
                    Assert.That(releaseOnly, Started <PressInteraction>(releaseOnlyAction, gamepad.buttonSouth, time: 5, value: 1.0));
                    Assert.That(pressAndRelease,
                                Started <PressInteraction>(pressAndReleaseAction, gamepad.buttonSouth, time: 5, value: 1.0)
                                .AndThen(Performed <PressInteraction>(pressAndReleaseAction, gamepad.buttonSouth, time: 5, duration: 0, value: 1.0)));
                }
    }
Example #19
0
    public void Components_CanUpdateGameObjectTransformThroughTrackedPoseDriver()
    {
        var position = new Vector3(1.0f, 2.0f, 3.0f);
        var rotation = new Quaternion(0.09853293f, 0.09853293f, 0.09853293f, 0.9853293f);

        var go     = new GameObject();
        var tpd    = go.AddComponent <TrackedPoseDriver>();
        var device = InputSystem.AddDevice <TestHMD>();

        using (StateEvent.From(device, out var stateEvent))
        {
            var positionAction = new InputAction();
            positionAction.AddBinding("<TestHMD>/vector3");

            var rotationAction = new InputAction();
            rotationAction.AddBinding("<TestHMD>/quaternion");

            tpd.positionAction = positionAction;
            tpd.rotationAction = rotationAction;

            // before render only
            var go1 = tpd.gameObject;
            go1.transform.position = Vector3.zero;
            go1.transform.rotation = new Quaternion(0, 0, 0, 0);
            tpd.updateType         = TrackedPoseDriver.UpdateType.BeforeRender;
            tpd.trackingType       = TrackedPoseDriver.TrackingType.RotationAndPosition;

            device.quaternion.WriteValueIntoEvent(rotation, stateEvent);
            device.vector3.WriteValueIntoEvent(position, stateEvent);

            InputSystem.QueueEvent(stateEvent);
            InputSystem.Update(InputUpdateType.Dynamic);
            Assert.That(tpd.gameObject.transform.position, Is.Not.EqualTo(position));
            Assert.That(!tpd.gameObject.transform.rotation.Equals(rotation));

            var go2 = tpd.gameObject;
            go2.transform.position = Vector3.zero;
            go2.transform.rotation = new Quaternion(0, 0, 0, 0);
            InputSystem.QueueEvent(stateEvent);
            InputSystem.Update(InputUpdateType.BeforeRender);
            Assert.That(tpd.gameObject.transform.position, Is.EqualTo(position));
            Assert.That(tpd.gameObject.transform.rotation.Equals(rotation));

            // update only
            var go3 = tpd.gameObject;
            go3.transform.position = Vector3.zero;
            go3.transform.rotation = new Quaternion(0, 0, 0, 0);
            tpd.updateType         = TrackedPoseDriver.UpdateType.Update;
            tpd.trackingType       = TrackedPoseDriver.TrackingType.RotationAndPosition;

            InputSystem.QueueEvent(stateEvent);
            InputSystem.Update(InputUpdateType.Dynamic);
            Assert.That(tpd.gameObject.transform.position, Is.EqualTo(position));
            Assert.That(tpd.gameObject.transform.rotation.Equals(rotation));

            var go4 = tpd.gameObject;
            go4.transform.position = Vector3.zero;
            go4.transform.rotation = new Quaternion(0, 0, 0, 0);
            InputSystem.QueueEvent(stateEvent);
            InputSystem.Update(InputUpdateType.BeforeRender);
            Assert.That(tpd.gameObject.transform.position, Is.Not.EqualTo(position));
            Assert.That(!tpd.gameObject.transform.rotation.Equals(rotation));

            // check the rot/pos case also Update AND Render.
            tpd.updateType   = TrackedPoseDriver.UpdateType.UpdateAndBeforeRender;
            tpd.trackingType = TrackedPoseDriver.TrackingType.PositionOnly;
            var go5 = tpd.gameObject;
            go5.transform.position = Vector3.zero;
            go5.transform.rotation = new Quaternion(0, 0, 0, 0);

            InputSystem.QueueEvent(stateEvent);
            InputSystem.Update(InputUpdateType.Dynamic);
            Assert.That(tpd.gameObject.transform.position, Is.EqualTo(position));
            Assert.That(!tpd.gameObject.transform.rotation.Equals(rotation));

            tpd.trackingType = TrackedPoseDriver.TrackingType.RotationOnly;
            var go6 = tpd.gameObject;
            go6.transform.position = Vector3.zero;
            go6.transform.rotation = new Quaternion(0, 0, 0, 0);
            InputSystem.QueueEvent(stateEvent);
            InputSystem.Update(InputUpdateType.BeforeRender);
            Assert.That(tpd.gameObject.transform.position, Is.Not.EqualTo(position));
            Assert.That(tpd.gameObject.transform.rotation.Equals(rotation));
        }
    }
Example #20
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);
    }
Example #21
0
    public void Actions_CanPerformPressInteraction()
    {
        var gamepad = InputSystem.AddDevice <Gamepad>();

        // We add a second input device (and bind to it), to test that the binding
        // conflict resolution will not interfere with the interaction handling.
        InputSystem.AddDevice <Keyboard>();

        // Test all three press behaviors concurrently.
        var pressOnlyAction = new InputAction("PressOnly", binding: "<Gamepad>/buttonSouth", interactions: "press");

        pressOnlyAction.AddBinding("<Keyboard>/a");
        var releaseOnlyAction = new InputAction("ReleaseOnly", binding: "<Gamepad>/buttonSouth", interactions: "press(behavior=1)");

        releaseOnlyAction.AddBinding("<Keyboard>/s");
        var pressAndReleaseAction = new InputAction("PressAndRelease", binding: "<Gamepad>/buttonSouth", interactions: "press(behavior=2)");

        pressAndReleaseAction.AddBinding("<Keyboard>/d");

        pressOnlyAction.Enable();
        releaseOnlyAction.Enable();
        pressAndReleaseAction.Enable();

        using (var trace = new InputActionTrace())
        {
            trace.SubscribeToAll();

            runtime.currentTime = 1;
            Press(gamepad.buttonSouth);

            var actions = trace.ToArray();
            Assert.That(actions, Has.Length.EqualTo(5));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressOnlyAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Started).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressOnlyAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Performed).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressAndReleaseAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Started).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressAndReleaseAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Performed).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(releaseOnlyAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Started).And.With.Property("duration")
                        .EqualTo(0));

            trace.Clear();

            runtime.currentTime = 2;
            Release(gamepad.buttonSouth);

            actions = trace.ToArray();
            Assert.That(actions, Has.Length.EqualTo(3));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(releaseOnlyAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Performed).And.With.Property("duration")
                        .EqualTo(1));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressAndReleaseAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Started).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressAndReleaseAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Performed).And.With.Property("duration")
                        .EqualTo(0));

            trace.Clear();

            runtime.currentTime = 5;
            Press(gamepad.buttonSouth);

            actions = trace.ToArray();
            Assert.That(actions, Has.Length.EqualTo(5));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressOnlyAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Started).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressOnlyAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Performed).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressAndReleaseAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Started).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(pressAndReleaseAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Performed).And.With.Property("duration")
                        .EqualTo(0));
            Assert.That(actions,
                        Has.Exactly(1).With.Property("action").SameAs(releaseOnlyAction).And.With.Property("phase")
                        .EqualTo(InputActionPhase.Started).And.With.Property("duration")
                        .EqualTo(0));
        }
    }