public void State_CanSetUpMonitorsForStateChanges_OnMultiBitFields() { var device = InputSystem.AddDevice <TestDeviceWithMultiBitControl>(); var monitorFired = false; InputControl receivedControl = null; Action <InputControl, double, InputEventPtr, long> action = (control, time, eventPtr, monitorIndex) => { Assert.That(!monitorFired); monitorFired = true; receivedControl = control; }; InputSystem.AddStateChangeMonitor(device["dpad"], action); InputSystem.AddStateChangeMonitor(device["data"], action); InputSystem.QueueStateEvent(device, new StateWithMultiBitControl().WithDpad(3)); InputSystem.Update(); Assert.That(monitorFired); Assert.That(receivedControl, Is.SameAs(device["dpad"])); monitorFired = false; receivedControl = null; InputSystem.QueueStateEvent(device, new StateWithMultiBitControl().WithDpad(3).WithData(1234)); InputSystem.Update(); Assert.That(monitorFired); Assert.That(receivedControl, Is.SameAs(device["data"])); }
public void State_StateChangeMonitor_CanBeAddedFromMonitorCallback() { var gamepad = InputSystem.AddDevice <Gamepad>(); InputSystem.AddStateChangeMonitor(gamepad.buttonWest, (c, d, e, m) => { Debug.Log("ButtonWest"); }); InputSystem.AddStateChangeMonitor(gamepad.buttonEast, (control, time, eventPtr, monitorIndex) => { Debug.Log("ButtonEast"); InputSystem.AddStateChangeMonitor(gamepad.buttonSouth, (c, t, e, m) => { Debug.Log("ButtonSouth"); }); }); LogAssert.Expect(LogType.Log, "ButtonEast"); Press(gamepad.buttonEast); LogAssert.NoUnexpectedReceived(); LogAssert.Expect(LogType.Log, "ButtonSouth"); Press(gamepad.buttonSouth); LogAssert.NoUnexpectedReceived(); LogAssert.Expect(LogType.Log, "ButtonWest"); Press(gamepad.buttonWest); LogAssert.NoUnexpectedReceived(); }
public void State_CanRemoveStateChangeMonitorWithSpecificMonitorIndex() { var gamepad = InputSystem.AddDevice <Gamepad>(); const int kLeftStick = 12345678; const int kRightStick = 87654321; var monitorFired = false; long?receivedMonitorIndex = null; var monitor = InputSystem.AddStateChangeMonitor(gamepad.leftStick, (control, time, eventPtr, monitorIndex) => { Assert.That(!monitorFired); monitorFired = true; receivedMonitorIndex = monitorIndex; }, kLeftStick); InputSystem.AddStateChangeMonitor(gamepad.rightStick, monitor, kRightStick); InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = Vector2.one }); InputSystem.Update(); Assert.That(monitorFired); Assert.That(receivedMonitorIndex.Value, Is.EqualTo(kLeftStick)); monitorFired = false; receivedMonitorIndex = null; InputSystem.QueueStateEvent(gamepad, new GamepadState { rightStick = Vector2.one, leftStick = Vector2.one }); InputSystem.Update(); Assert.That(monitorFired); Assert.That(receivedMonitorIndex.Value, Is.EqualTo(kRightStick)); InputSystem.RemoveStateChangeMonitor(gamepad.leftStick, monitor, kLeftStick); monitorFired = false; receivedMonitorIndex = null; InputSystem.QueueStateEvent(gamepad, new GamepadState { rightStick = Vector2.one }); InputSystem.Update(); Assert.That(!monitorFired); InputSystem.QueueStateEvent(gamepad, new GamepadState()); InputSystem.Update(); Assert.That(monitorFired); Assert.That(receivedMonitorIndex.Value, Is.EqualTo(kRightStick)); }
public void State_CanThrowExceptionFromStateChangeMonitorCallback() { var gamepad = InputSystem.AddDevice <Gamepad>(); InputSystem.AddStateChangeMonitor(gamepad.buttonWest, (c, d, e, m) => throw new InvalidOperationException("TESTEXCEPTION")); LogAssert.Expect(LogType.Error, new Regex("Exception.*thrown from state change monitor.*Gamepad.*buttonWest.*")); LogAssert.Expect(LogType.Exception, new Regex(".*InvalidOperationException.*TESTEXCEPTION")); Press(gamepad.buttonWest); LogAssert.NoUnexpectedReceived(); }
public void State_StateChangeMonitorTimeout_CanBeAddedFromMonitorCallback() { var gamepad = InputSystem.AddDevice <Gamepad>(); var monitorFired = false; var timeoutFired = false; IInputStateChangeMonitor monitor = null; monitor = InputSystem.AddStateChangeMonitor(gamepad.leftStick, (control, time, eventPtr, monitorIndex) => { Assert.That(!monitorFired); monitorFired = true; InputSystem.AddStateChangeMonitorTimeout(gamepad.leftStick, monitor, runtime.currentTime + 1); }, timerExpiredCallback: (control, time, monitorIndex, timerIndex) => { Assert.That(!timeoutFired); timeoutFired = true; }); // Trigger monitor callback. InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = Vector2.one }); InputSystem.Update(); Assert.That(monitorFired); // Expire timer. runtime.currentTime += 2; InputSystem.Update(); Assert.That(timeoutFired); }
public void State_CanWaitForStateChangeWithinGivenAmountOfTime() { var gamepad = InputSystem.AddDevice <Gamepad>(); var monitorFired = false; var timeoutFired = false; double? receivedTime = null; int? receivedTimerIndex = null; InputControl receivedControl = null; var monitor = InputSystem.AddStateChangeMonitor(gamepad.leftStick, (control, time, eventPtr, monitorIndex) => { Assert.That(!monitorFired); monitorFired = true; }, timerExpiredCallback: (control, time, monitorIndex, timerIndex) => { Assert.That(!timeoutFired); timeoutFired = true; receivedTime = time; receivedTimerIndex = timerIndex; receivedControl = control; }); // Add and immediately expire timeout. InputSystem.AddStateChangeMonitorTimeout(gamepad.leftStick, monitor, runtime.currentTime + 1, timerIndex: 1234); runtime.currentTime += 2; InputSystem.Update(); Assert.That(timeoutFired); Assert.That(!monitorFired); Assert.That(receivedTimerIndex.Value, Is.EqualTo(1234)); Assert.That(receivedTime.Value, Is.EqualTo(runtime.currentTime).Within(0.00001)); Assert.That(receivedControl, Is.SameAs(gamepad.leftStick)); timeoutFired = false; receivedTimerIndex = null; receivedTime = null; // Add timeout and obsolete it by state change. Then advance past timeout time // and make sure we *don't* get a notification. InputSystem.AddStateChangeMonitorTimeout(gamepad.leftStick, monitor, runtime.currentTime + 1, timerIndex: 4321); InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = Vector2.one }); InputSystem.Update(); Assert.That(monitorFired); Assert.That(!timeoutFired); runtime.currentTime += 2; InputSystem.Update(); Assert.That(!timeoutFired); // Add and remove timeout. Then advance past timeout time and make sure we *don't* // get a notification. InputSystem.AddStateChangeMonitorTimeout(gamepad.leftStick, monitor, runtime.currentTime + 1, timerIndex: 1423); InputSystem.RemoveStateChangeMonitorTimeout(monitor, timerIndex: 1423); InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = Vector2.one }); InputSystem.Update(); Assert.That(!timeoutFired); }
public void State_CanSetUpMonitorsForStateChanges() { var gamepad = InputSystem.AddDevice <Gamepad>(); var monitorFired = false; InputControl receivedControl = null; double? receivedTime = null; InputEventPtr?receivedEventPtr = null; var monitor = InputSystem.AddStateChangeMonitor(gamepad.leftStick, (control, time, eventPtr, monitorIndex) => { Assert.That(!monitorFired); monitorFired = true; receivedControl = control; receivedTime = time; receivedEventPtr = eventPtr; }); // Left stick only. InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = new Vector2(0.5f, 0.5f) }, 0.5); InputSystem.Update(); Assert.That(monitorFired, Is.True); Assert.That(receivedControl, Is.SameAs(gamepad.leftStick)); Assert.That(receivedTime.Value, Is.EqualTo(0.5).Within(0.000001)); Assert.That(receivedEventPtr.Value.deviceId, Is.EqualTo(gamepad.id)); monitorFired = false; receivedControl = null; receivedTime = 0; receivedEventPtr = null; // Left stick again but with no value change. InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = new Vector2(0.5f, 0.5f) }, 0.6); InputSystem.Update(); Assert.That(monitorFired, Is.False); // Left and right stick. InputSystem.QueueStateEvent(gamepad, new GamepadState { rightStick = new Vector2(0.75f, 0.75f), leftStick = new Vector2(0.75f, 0.75f) }, 0.7); InputSystem.Update(); Assert.That(monitorFired, Is.True); Assert.That(receivedControl, Is.SameAs(gamepad.leftStick)); Assert.That(receivedTime.Value, Is.EqualTo(0.7).Within(0.000001)); Assert.That(receivedEventPtr.Value.deviceId, Is.EqualTo(gamepad.id)); monitorFired = false; receivedControl = null; receivedTime = 0; receivedEventPtr = null; // Right stick only. InputSystem.QueueStateEvent(gamepad, new GamepadState { rightStick = new Vector2(0.5f, 0.5f), leftStick = new Vector2(0.75f, 0.75f) }, 0.8); InputSystem.Update(); Assert.That(monitorFired, Is.False); // Component control of left stick. InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = new Vector2(0.75f, 0.5f) }, 0.9); InputSystem.Update(); Assert.That(monitorFired, Is.True); ////REVIEW: do we want to be able to detect the child control that actually changed? could be multiple, though Assert.That(receivedControl, Is.SameAs(gamepad.leftStick)); Assert.That(receivedTime.Value, Is.EqualTo(0.9).Within(0.000001)); Assert.That(receivedEventPtr.Value.deviceId, Is.EqualTo(gamepad.id)); // Remove state monitor and change leftStick again. InputSystem.RemoveStateChangeMonitor(gamepad.leftStick, monitor); monitorFired = false; receivedControl = null; receivedTime = 0; receivedEventPtr = null; InputSystem.QueueStateEvent(gamepad, new GamepadState { leftStick = new Vector2(0.0f, 0.0f) }, 1.0); InputSystem.Update(); Assert.That(monitorFired, Is.False); }