示例#1
0
    ////REVIEW: this logic seems to low-level to be here; can we move this into the input system somehow?
    /// <summary>
    /// In multi-player, we want players to be able to join on new devices simply by pressing
    /// a button. This callback is invoked on every input event and we determine whether we have
    /// a new join.
    /// </summary>
    /// <param name="eventPtr">An input event.</param>
    /// <remarks>
    /// </remarks>
    private void OnInputEventInMultiPlayer(InputEventPtr eventPtr)
    {
        // Ignore if not a state event.
        if (!eventPtr.IsA <StateEvent>() && !eventPtr.IsA <DeltaStateEvent>())
        {
            return;
        }

        // Ignore if device is already assigned to a player.
        var device = InputSystem.GetDeviceById(eventPtr.deviceId);

        if (device == null)
        {
            return;
        }
        if (InputUser.FindUserForDevice(device) != null)
        {
            return;
        }

        ////REVIEW: what about devices that we can't actually bind to from any of our existing bindings?
        ////        seems like we should detect that here and not initiate a join
        ////        we could alternatively create an InputAction and put all the bindings we have on there and then do joins from that

        // See if a button was pressed on the device.
        var controls = device.allControls;

        for (var i = 0; i < controls.Count; ++i)
        {
            var control = controls[i];

            // Skip if not a button.
            var button = control as ButtonControl;
            if (button == null)
            {
                continue;
            }

            // If it changed from pressed to not pressed, we have a winner.
            float valueInEvent;
            if (button.ReadValue() < InputConfiguration.ButtonPressPoint &&
                button.ReadValueFrom(eventPtr, out valueInEvent) &&
                valueInEvent >= InputConfiguration.ButtonPressPoint)
            {
                OnJoin(device);
                break;
            }
        }
    }
示例#2
0
    public void Users_CanFindUserThatIsAssignedToSpecificDevice()
    {
        var gamepad1 = InputSystem.AddDevice <Gamepad>();
        var gamepad2 = InputSystem.AddDevice <Gamepad>();
        var gamepad3 = InputSystem.AddDevice <Gamepad>();

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

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

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

        Assert.That(InputUser.FindUserForDevice(gamepad1), Is.SameAs(user1));
        Assert.That(InputUser.FindUserForDevice(gamepad2), Is.SameAs(user2));
        Assert.That(InputUser.FindUserForDevice(gamepad3), Is.Null);
    }