@@ -39,12 +39,10 @@ public virtual bool CheckCapability(MixedRealityCapability capability)
3939 return ( capability == MixedRealityCapability . MotionController ) ;
4040 }
4141
42- protected static readonly Dictionary < string , GenericXRSDKController > ActiveControllers = new Dictionary < string , GenericXRSDKController > ( ) ;
42+ protected static readonly Dictionary < InputDevice , GenericXRSDKController > ActiveControllers = new Dictionary < InputDevice , GenericXRSDKController > ( ) ;
4343
44- private InputDevice leftInputDevice ;
45- private InputDevice rightInputDevice ;
46- private bool wasLeftInputDeviceValid = false ;
47- private bool wasRightInputDeviceValid = false ;
44+ private readonly List < InputDevice > inputDevices = new List < InputDevice > ( ) ;
45+ private readonly List < InputDevice > lastInputDevices = new List < InputDevice > ( ) ;
4846
4947 /// <inheritdoc/>
5048 public override void Update ( )
@@ -56,71 +54,45 @@ public override void Update()
5654 return ;
5755 }
5856
59- if ( ! leftInputDevice . isValid )
57+ InputDevices . GetDevicesWithCharacteristics ( InputDeviceCharacteristics . Controller , inputDevices ) ;
58+ foreach ( InputDevice device in inputDevices )
6059 {
61- if ( wasLeftInputDeviceValid )
60+ if ( device . isValid )
6261 {
63- GenericXRSDKController controller = GetOrAddController ( leftInputDevice ) ;
62+ GenericXRSDKController controller = GetOrAddController ( device ) ;
6463
65- if ( controller ! = null )
64+ if ( controller = = null )
6665 {
67- CoreServices . InputSystem ? . RaiseSourceLost ( controller . InputSource , controller ) ;
66+ continue ;
6867 }
6968
70- RemoveController ( leftInputDevice ) ;
71- wasLeftInputDeviceValid = false ;
72- }
73-
74- leftInputDevice = InputDevices . GetDeviceAtXRNode ( XRNode . LeftHand ) ;
75-
76- if ( leftInputDevice . isValid )
77- {
78- wasLeftInputDeviceValid = true ;
79- GenericXRSDKController controller = GetOrAddController ( leftInputDevice ) ;
80-
81- if ( controller != null )
69+ if ( ! lastInputDevices . Contains ( device ) )
8270 {
8371 CoreServices . InputSystem ? . RaiseSourceDetected ( controller . InputSource , controller ) ;
8472 }
85- }
86- }
87- else
88- {
89- GetOrAddController ( leftInputDevice ) ? . UpdateController ( leftInputDevice ) ;
90- }
91-
92- if ( ! rightInputDevice . isValid )
93- {
94- if ( wasRightInputDeviceValid )
95- {
96- GenericXRSDKController controller = GetOrAddController ( rightInputDevice ) ;
97-
98- if ( controller != null )
73+ else
9974 {
100- CoreServices . InputSystem ? . RaiseSourceLost ( controller . InputSource , controller ) ;
75+ // Remove devices from our previously tracked list as we update them.
76+ // This will allow us to remove all stale devices that were tracked
77+ // last frame but not this one.
78+ lastInputDevices . Remove ( device ) ;
79+ controller . UpdateController ( device ) ;
10180 }
102-
103- RemoveController ( rightInputDevice ) ;
104- wasRightInputDeviceValid = false ;
10581 }
82+ }
10683
107- rightInputDevice = InputDevices . GetDeviceAtXRNode ( XRNode . RightHand ) ;
108-
109- if ( rightInputDevice . isValid )
84+ foreach ( InputDevice device in lastInputDevices )
85+ {
86+ GenericXRSDKController controller = GetOrAddController ( device ) ;
87+ if ( controller != null )
11088 {
111- wasRightInputDeviceValid = true ;
112- GenericXRSDKController controller = GetOrAddController ( rightInputDevice ) ;
113-
114- if ( controller != null )
115- {
116- CoreServices . InputSystem ? . RaiseSourceDetected ( controller . InputSource , controller ) ;
117- }
89+ CoreServices . InputSystem ? . RaiseSourceLost ( controller . InputSource , controller ) ;
90+ RemoveController ( device ) ;
11891 }
11992 }
120- else
121- {
122- GetOrAddController ( rightInputDevice ) ? . UpdateController ( rightInputDevice ) ;
123- }
93+
94+ lastInputDevices . Clear ( ) ;
95+ lastInputDevices . AddRange ( inputDevices ) ;
12496 }
12597
12698 #region Controller Utilities
@@ -133,9 +105,9 @@ public override void Update()
133105 protected virtual GenericXRSDKController GetOrAddController ( InputDevice inputDevice )
134106 {
135107 // If a device is already registered with the ID provided, just return it.
136- if ( ActiveControllers . ContainsKey ( inputDevice . name ) )
108+ if ( ActiveControllers . ContainsKey ( inputDevice ) )
137109 {
138- var controller = ActiveControllers [ inputDevice . name ] ;
110+ var controller = ActiveControllers [ inputDevice ] ;
139111 Debug . Assert ( controller != null ) ;
140112 return controller ;
141113 }
@@ -182,7 +154,7 @@ protected virtual GenericXRSDKController GetOrAddController(InputDevice inputDev
182154 detectedController . InputSource . Pointers [ i ] . Controller = detectedController ;
183155 }
184156
185- ActiveControllers . Add ( inputDevice . name , detectedController ) ;
157+ ActiveControllers . Add ( inputDevice , detectedController ) ;
186158 return detectedController ;
187159 }
188160
@@ -204,7 +176,7 @@ protected virtual void RemoveController(InputDevice inputDevice)
204176 controller . Visualizer . GameObjectProxy . SetActive ( false ) ;
205177 }
206178
207- ActiveControllers . Remove ( inputDevice . name ) ;
179+ ActiveControllers . Remove ( inputDevice ) ;
208180 }
209181 }
210182
0 commit comments