示例#1
0
    public void Users_CanAssignControlScheme_AndMaskOutBindingsFromOtherControlSchemes()
    {
        var gamepad       = InputSystem.AddDevice <Gamepad>();
        var gamepadScheme = new InputControlScheme("Gamepad")
                            .WithRequiredDevice("<Gamepad>");

        var map    = new InputActionMap("map");
        var action = map.AddAction("action");

        action.AddBinding("<Gamepad>/buttonSouth", groups: "Gamepad");
        action.AddBinding("<Mouse>/leftButton", groups: "KeyboardMouse");

        var user = new TestUser();

        InputUser.Add(user);

        // Trying to do it before we've assigned actions should throw.
        Assert.That(() => user.AssignControlScheme(gamepadScheme)
                    .AndMaskBindingsFromOtherControlSchemes(), Throws.InvalidOperationException);

        user.AssignInputActions(map);
        user.AssignInputDevice(gamepad);
        user.AssignControlScheme(gamepadScheme)
        .AndMaskBindingsFromOtherControlSchemes();

        Assert.That(action.controls, Is.EquivalentTo(new[] { gamepad.buttonSouth }));
        Assert.That(map.bindingMask, Is.EqualTo(new InputBinding {
            groups = "Gamepad"
        }));
    }
示例#2
0
    public void Users_CanAssignActionAssetToUser()
    {
        var asset = ScriptableObject.CreateInstance <InputActionAsset>();

        var user = new TestUser();

        InputUser.Add(user);

        Assert.That(user.GetInputActions(), Is.Null);

        user.AssignInputActions(asset);

        Assert.That(user.GetInputActions(), Is.SameAs(asset));
    }
示例#3
0
    public void Users_CanRestrictBindingToAssignedInputDevices()
    {
        var gamepad1 = InputSystem.AddDevice <Gamepad>();
        var gamepad2 = InputSystem.AddDevice <Gamepad>();
        var gamepad3 = InputSystem.AddDevice <Gamepad>();

        var map1 = new InputActionMap("map");

        map1.AddAction("action").AddBinding("<Gamepad>/buttonSouth", groups: "Gamepad");
        var map2 = map1.Clone();

        var user1 = new TestUser();
        var user2 = new TestUser();

        InputUser.Add(user1);
        InputUser.Add(user2);

        user1.AssignInputDevice(gamepad1);
        user2.AssignInputDevice(gamepad2);

        user1.AssignInputActions(map1);
        user2.AssignInputActions(map2);

        // Have bound to everything available globally.
        Assert.That(map1["action"].controls, Is.EquivalentTo(new[] { gamepad1.buttonSouth, gamepad2.buttonSouth, gamepad3.buttonSouth }));
        Assert.That(map2["action"].controls, Is.EquivalentTo(new[] { gamepad1.buttonSouth, gamepad2.buttonSouth, gamepad3.buttonSouth }));

        user2.BindOnlyToAssignedInputDevices();

        // Have bound only to currently assigned devices.
        Assert.That(map1["action"].controls, Is.EquivalentTo(new[] { gamepad1.buttonSouth, gamepad2.buttonSouth, gamepad3.buttonSouth }));
        Assert.That(map2["action"].controls, Is.EquivalentTo(new[] { gamepad2.buttonSouth }));

        user2.ClearAssignedInputDevices();
        user2.AssignInputDevice(gamepad3);

        // Have updated bindings to reflect change in assigned devices.
        Assert.That(map1["action"].controls, Is.EquivalentTo(new[] { gamepad1.buttonSouth, gamepad2.buttonSouth, gamepad3.buttonSouth }));
        Assert.That(map2["action"].controls, Is.EquivalentTo(new[] { gamepad3.buttonSouth }));

        user2.BindOnlyToAssignedInputDevices(false);

        // Have gone back to binding to everything.
        Assert.That(map1["action"].controls, Is.EquivalentTo(new[] { gamepad1.buttonSouth, gamepad2.buttonSouth, gamepad3.buttonSouth }));
        Assert.That(map2["action"].controls, Is.EquivalentTo(new[] { gamepad1.buttonSouth, gamepad2.buttonSouth, gamepad3.buttonSouth }));
    }
示例#4
0
    public void Users_CanDetectWhenUnassignedDeviceIsUsed()
    {
        var gamepad1 = InputSystem.AddDevice <Gamepad>();
        var gamepad2 = InputSystem.AddDevice <Gamepad>();
        var gamepad3 = InputSystem.AddDevice <Gamepad>();

        var asset = ScriptableObject.CreateInstance <InputActionAsset>();
        var map   = new InputActionMap("map");

        asset.AddActionMap(map);

        var actionAssignedToUser = map.AddAction("action", binding: "<Gamepad>/buttonSouth");

        actionAssignedToUser.Enable();

        var actionNotAssignedToUser = new InputAction(binding: "<Gamepad>/buttonNorth");

        actionNotAssignedToUser.Enable();

        var user = new TestUser();

        InputUser.Add(user);

        // Noise.
        InputUser.Add(new TestUser());
        InputUser.all[1].AssignInputDevice(gamepad3);

        IInputUser   receivedUser    = null;
        InputAction  receivedAction  = null;
        InputControl receivedControl = null;

        InputUser.onUnassignedDeviceUsed +=
            (u, a, c) =>
        {
            Assert.That(receivedUser, Is.Null);
            receivedUser    = u;
            receivedAction  = a;
            receivedControl = c;
        };

        user.AssignInputActions(asset);
        user.AssignInputDevice(gamepad1);

        // No callback if using gamepad1.
        InputSystem.QueueStateEvent(gamepad1, new GamepadState().WithButton(GamepadButton.South));
        InputSystem.Update();

        Assert.That(receivedUser, Is.Null);
        Assert.That(receivedAction, Is.Null);
        Assert.That(receivedControl, Is.Null);

        // Callback when using gamepad2.
        InputSystem.QueueStateEvent(gamepad2, new GamepadState().WithButton(GamepadButton.South));
        InputSystem.Update();

        Assert.That(receivedUser, Is.SameAs(user));
        Assert.That(receivedAction, Is.SameAs(actionAssignedToUser));
        Assert.That(receivedControl, Is.SameAs(gamepad2.buttonSouth));

        receivedUser    = null;
        receivedControl = null;
        receivedAction  = null;

        // No callback when triggering action not assigned to user.
        InputSystem.QueueStateEvent(gamepad1, new GamepadState().WithButton(GamepadButton.North));
        InputSystem.Update();

        Assert.That(receivedUser, Is.Null);
        Assert.That(receivedAction, Is.Null);
        Assert.That(receivedControl, Is.Null);
    }