Skip to content

Commit 0f167e4

Browse files
RefactorInputStateMonitor's so it's no longer a partial class with the InputManager. Still need another review to look into improvement.
1 parent 4c06cc6 commit 0f167e4

3 files changed

Lines changed: 259 additions & 297 deletions

File tree

Assets/Tests/InputSystem/CoreTests_Editor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,16 +2964,16 @@ public void Editor_LeavingPlayMode_DestroysAllActionStates()
29642964
action.Enable();
29652965

29662966
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.EqualTo(1));
2967-
Assert.That(InputSystem.manager.m_StateChangeMonitors.Length, Is.GreaterThan(0));
2968-
Assert.That(InputSystem.manager.m_StateChangeMonitors[0].count, Is.EqualTo(1));
2967+
Assert.That(InputSystem.manager.m_StateMonitors.m_MonitorsPerDevice.Length, Is.GreaterThan(0));
2968+
Assert.That(InputSystem.manager.m_StateMonitors.m_MonitorsPerDevice[0].count, Is.EqualTo(1));
29692969

29702970
// Exit play mode.
29712971
InputSystem.OnPlayModeChange(PlayModeStateChange.ExitingPlayMode);
29722972
InputSystem.OnPlayModeChange(PlayModeStateChange.EnteredEditMode);
29732973

29742974
Assert.That(InputActionState.s_GlobalState.globalList.length, Is.Zero);
29752975
// Won't get removed, just cleared.
2976-
Assert.That(InputSystem.manager.m_StateChangeMonitors[0].listeners[0].control, Is.Null);
2976+
Assert.That(InputSystem.manager.m_StateMonitors.m_MonitorsPerDevice[0].listeners[0].control, Is.Null);
29772977
}
29782978

29792979
[Test]

Packages/com.unity.inputsystem/InputSystem/InputManager.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace UnityEngine.InputSystem
4646
///
4747
/// Manages devices, layouts, and event processing.
4848
/// </remarks>
49-
internal partial class InputManager : IDisposable
49+
internal class InputManager : IDisposable
5050
{
5151
private InputManager() {}
5252

@@ -405,6 +405,24 @@ public event Action onActionsChange
405405

406406
public bool isProcessingEvents => m_InputEventStream.isOpen;
407407

408+
public void AddStateChangeMonitor(InputControl control, IInputStateChangeMonitor monitor, long monitorIndex, uint groupIndex)
409+
=> m_StateMonitors.AddStateChangeMonitor(control, monitor, monitorIndex, groupIndex);
410+
411+
public void RemoveStateChangeMonitor(InputControl control, IInputStateChangeMonitor monitor, long monitorIndex)
412+
=> m_StateMonitors.RemoveStateChangeMonitor(control, monitor, monitorIndex);
413+
414+
public void AddStateChangeMonitorTimeout(InputControl control, IInputStateChangeMonitor monitor, double time, long monitorIndex, int timerIndex)
415+
=> m_StateMonitors.AddStateChangeMonitorTimeout(control, monitor, time, monitorIndex, timerIndex);
416+
417+
public void RemoveStateChangeMonitorTimeout(IInputStateChangeMonitor monitor, long monitorIndex, int timerIndex)
418+
=> m_StateMonitors.RemoveStateChangeMonitorTimeout(monitor, monitorIndex, timerIndex);
419+
420+
public void SignalStateChangeMonitor(InputControl control, IInputStateChangeMonitor monitor)
421+
=> m_StateMonitors.SignalStateChangeMonitor(control, monitor);
422+
423+
public unsafe void FireStateChangeNotifications()
424+
=> m_StateMonitors.FireStateChangeNotifications();
425+
408426
#if UNITY_EDITOR
409427
/// <summary>
410428
/// Callback that can be used to display a warning and draw additional custom Editor UI for bindings.
@@ -1487,17 +1505,11 @@ public void RemoveDevice(InputDevice device, bool keepOnListOfAvailableDevices =
14871505
return;
14881506

14891507
// Remove state monitors while device index is still valid.
1490-
RemoveStateChangeMonitors(device);
1508+
m_StateMonitors.OnDeviceRemoved(device);
14911509

14921510
// Remove from device array.
14931511
var deviceIndex = device.m_DeviceIndex;
14941512
var deviceId = device.deviceId;
1495-
if (deviceIndex < m_StateChangeMonitors.LengthSafe())
1496-
{
1497-
// m_StateChangeMonitors mirrors layout of m_Devices *but* may be shorter.
1498-
var count = m_StateChangeMonitors.Length;
1499-
ArrayHelpers.EraseAtWithCapacity(m_StateChangeMonitors, ref count, deviceIndex);
1500-
}
15011513
ArrayHelpers.EraseAtWithCapacity(m_Devices, ref m_DevicesCount, deviceIndex);
15021514

15031515
m_DevicesById.Remove(deviceId);
@@ -2207,6 +2219,8 @@ internal void InstallRuntime(IInputRuntime runtime)
22072219
InputAnalytics.Initialize(this);
22082220
m_Runtime.onShutdown = () => InputAnalytics.OnShutdown(this);
22092221
#endif
2222+
2223+
m_StateMonitors = new InputManagerStateMonitors(() => m_DevicesCount, () => isProcessingEvents, m_Runtime);
22102224
}
22112225

22122226
internal void InstallGlobals()
@@ -2374,6 +2388,7 @@ internal struct AvailableDevice
23742388
#endif
23752389

23762390
private IInputRuntime m_Runtime;
2391+
internal InputManagerStateMonitors m_StateMonitors;
23772392
private InputMetrics m_Metrics;
23782393
private InputSettings m_Settings;
23792394

@@ -3331,7 +3346,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
33313346
// Normally, we process action timeouts after first processing all events. If we have no
33323347
// events, we still need to check timeouts.
33333348
if (shouldProcessActionTimeouts)
3334-
ProcessStateChangeMonitorTimeouts();
3349+
m_StateMonitors.ProcessTimeouts();
33353350

33363351
k_InputUpdateProfilerMarker.End();
33373352
InvokeAfterUpdateCallback(updateType);
@@ -3764,7 +3779,7 @@ private unsafe void OnUpdate(InputUpdateType updateType, ref InputEventBuffer ev
37643779
m_DiscardOutOfFocusEvents = false;
37653780

37663781
if (shouldProcessActionTimeouts)
3767-
ProcessStateChangeMonitorTimeouts();
3782+
m_StateMonitors.ProcessTimeouts();
37683783

37693784
k_InputUpdateProfilerMarker.End();
37703785
////FIXME: need to ensure that if someone calls QueueEvent() from an onAfterUpdate callback, we don't end up with a
@@ -4092,7 +4107,7 @@ internal unsafe bool UpdateState(InputDevice device, InputUpdateType updateType,
40924107

40934108
// If state monitors need to be re-sorted, do it now.
40944109
// NOTE: This must happen with the monitors in non-signalled state!
4095-
SortStateChangeMonitorsIfNecessary(deviceIndex);
4110+
m_StateMonitors.SortMonitorsForDeviceIfNeeded(deviceIndex);
40964111

40974112
// Before we update state, let change monitors compare the old and the new state.
40984113
// We do this instead of first updating the front buffer and then comparing to the
@@ -4101,7 +4116,7 @@ internal unsafe bool UpdateState(InputDevice device, InputUpdateType updateType,
41014116
// state, we can have multiple state events in the same frame yet still get reliable
41024117
// change notifications.
41034118
var haveSignalledMonitors =
4104-
ProcessStateChangeMonitors(deviceIndex, statePtr,
4119+
m_StateMonitors.ProcessStateChange(deviceIndex, statePtr,
41054120
deviceBuffer + stateBlockOfDevice.byteOffset,
41064121
stateSize, stateOffsetInDevice);
41074122

@@ -4185,7 +4200,7 @@ internal unsafe bool UpdateState(InputDevice device, InputUpdateType updateType,
41854200
// Now that we've committed the new state to memory, if any of the change
41864201
// monitors fired, let the associated actions know.
41874202
if (haveSignalledMonitors)
4188-
FireStateChangeNotifications(deviceIndex, internalTime, eventPtr);
4203+
m_StateMonitors.FireStateChangeNotifications(deviceIndex, internalTime, eventPtr);
41894204

41904205
return makeDeviceCurrent;
41914206
}

0 commit comments

Comments
 (0)