Exemplo n.º 1
0
    public void Events_EventBuffer_CanBeReset()
    {
        var gamepad = InputSystem.AddDevice <Gamepad>();

        unsafe
        {
            using (var buffer = new InputEventBuffer())
            {
                buffer.AppendEvent(DeviceConfigurationEvent.Create(gamepad.id, 123).ToEventPtr());
                buffer.AppendEvent(DeviceConfigurationEvent.Create(gamepad.id, 234).ToEventPtr());

                var events = buffer.ToArray();
                Assert.That(events, Has.Length.EqualTo(2));
                Assert.That(events[0].type, Is.EqualTo(new FourCC(DeviceConfigurationEvent.Type)));
                Assert.That(events[1].type, Is.EqualTo(new FourCC(DeviceConfigurationEvent.Type)));

                buffer.Reset();

                Assert.That(buffer.eventCount, Is.Zero);

                buffer.AppendEvent(DeviceRemoveEvent.Create(gamepad.id, 432).ToEventPtr());

                events = buffer.ToArray();

                Assert.That(events.Length, Is.EqualTo(1));
                Assert.That(events[0].type, Is.EqualTo(new FourCC(DeviceRemoveEvent.Type)));
            }
        }
    }
Exemplo n.º 2
0
    public void Events_EventBuffer_CanIterateEvents()
    {
        var gamepad = InputSystem.AddDevice <Gamepad>();

        unsafe
        {
            using (StateEvent.From(gamepad, out var eventPtr))
                using (var buffer = new InputEventBuffer(eventPtr, 1))
                {
                    Assert.That(buffer.eventCount, Is.EqualTo(1));
                    Assert.That(buffer.sizeInBytes, Is.EqualTo(InputEventBuffer.BufferSizeUnknown));
                    Assert.That(buffer.capacityInBytes, Is.Zero);
                    Assert.That(buffer.bufferPtr, Is.EqualTo(eventPtr));

                    var events = buffer.ToArray();
                    Assert.That(events, Has.Length.EqualTo(1));
                    Assert.That(events[0], Is.EqualTo(eventPtr));
                }
        }
    }
    public void Events_EventBuffer_CanAddEvents()
    {
        var gamepad = InputSystem.AddDevice <Gamepad>();

        unsafe
        {
            InputEventPtr eventPtr;
            using (StateEvent.From(gamepad, out eventPtr))
                using (var buffer = new InputEventBuffer())
                {
                    // Write two events into buffer.
                    gamepad.leftStick.WriteValueInto(eventPtr, Vector2.one);
                    eventPtr.id      = 111;
                    eventPtr.time    = 123;
                    eventPtr.handled = false;
                    buffer.AppendEvent(eventPtr);
                    gamepad.leftStick.WriteValueInto(eventPtr, Vector2.zero);
                    eventPtr.id      = 222;
                    eventPtr.time    = 234;
                    eventPtr.handled = true;
                    buffer.AppendEvent(eventPtr);

                    Assert.That(buffer.eventCount, Is.EqualTo(2));
                    var events = buffer.ToArray();

                    Assert.That(events, Has.Length.EqualTo(2));
                    Assert.That(events[0].type, Is.EqualTo(new FourCC(StateEvent.Type)));
                    Assert.That(events[1].type, Is.EqualTo(new FourCC(StateEvent.Type)));
                    Assert.That(events[0].time, Is.EqualTo(123).Within(0.00001));
                    Assert.That(events[1].time, Is.EqualTo(234).Within(0.00001));
                    Assert.That(events[0].id, Is.EqualTo(111));
                    Assert.That(events[1].id, Is.EqualTo(222));
                    Assert.That(events[0].handled, Is.False);
                    Assert.That(events[1].handled, Is.True);
                    Assert.That(events[0].deviceId, Is.EqualTo(gamepad.id));
                    Assert.That(events[1].deviceId, Is.EqualTo(gamepad.id));
                    Assert.That(gamepad.leftStick.ReadUnprocessedValueFrom(events[0]), Is.EqualTo(Vector2.one));
                    Assert.That(gamepad.leftStick.ReadUnprocessedValueFrom(events[1]), Is.EqualTo(Vector2.zero));
                }
        }
    }