Skip to content
Navigation Menu
{{ message }}
forked from Unity-Technologies/EditorXR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceInputModule.cs
More file actions
349 lines (291 loc) · 12.8 KB
/
Copy pathDeviceInputModule.cs
File metadata and controls
349 lines (291 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#if UNITY_EDITOR
#if !UNITY_2017_2_OR_NEWER
#pragma warning disable 649 // "never assigned to" warning
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.EditorVR.Utilities;
using UnityEngine;
using UnityEngine.InputNew;
namespace UnityEditor.Experimental.EditorVR.Modules
{
sealed class DeviceInputModule : MonoBehaviour
{
class InputProcessor
{
public IProcessInput processor;
public ActionMapInput input;
public int order;
}
[SerializeField]
ActionMap m_TrackedObjectActionMap;
[SerializeField]
ActionMap m_StandardToolActionMap;
PlayerHandle m_PlayerHandle;
readonly HashSet<InputControl> m_LockedControls = new HashSet<InputControl>();
readonly Dictionary<ActionMapInput, ICustomActionMap> m_IgnoreLocking = new Dictionary<ActionMapInput, ICustomActionMap>();
readonly Dictionary<string, Node> m_TagToNode = new Dictionary<string, Node>
{
{ "Left", Node.LeftHand },
{ "Right", Node.RightHand }
};
readonly List<InputProcessor> m_InputProcessors = new List<InputProcessor>();
public TrackedObject trackedObjectInput { get; private set; }
public Action<HashSet<IProcessInput>, ConsumeControlDelegate> processInput;
public Action<List<ActionMapInput>> updatePlayerHandleMaps;
public Func<Transform, InputDevice> inputDeviceForRayOrigin;
// Local method use only -- created here to reduce garbage collection
readonly HashSet<IProcessInput> m_ProcessedInputs = new HashSet<IProcessInput>();
readonly List<InputDevice> m_SystemDevices = new List<InputDevice>();
readonly Dictionary<Type, string[]> m_DeviceTypeTags = new Dictionary<Type, string[]>();
readonly List<InputProcessor> m_InputProcessorsCopy = new List<InputProcessor>();
readonly List<InputProcessor> m_RemoveInputProcessorsCopy = new List<InputProcessor>();
static readonly List<InputControl> k_RemoveList = new List<InputControl>();
ConsumeControlDelegate m_ConsumeControl;
void Awake()
{
m_ConsumeControl = ConsumeControl;
}
public List<InputDevice> GetSystemDevices()
{
// For now let's filter out any other devices other than VR controller devices; Eventually, we may support mouse / keyboard etc.
m_SystemDevices.Clear();
var devices = InputSystem.devices;
for (int i = 0; i < devices.Count; i++)
{
var device = devices[i];
if (device is VRInputDevice && device.tagIndex != -1)
m_SystemDevices.Add(device);
}
return m_SystemDevices;
}
public void InitializePlayerHandle()
{
m_PlayerHandle = PlayerHandleManager.GetNewPlayerHandle();
m_PlayerHandle.global = true;
m_PlayerHandle.processAll = true;
}
void OnDestroy()
{
PlayerHandleManager.RemovePlayerHandle(m_PlayerHandle);
}
/// <summary>
/// Called in the EditorVR Update() function
/// </summary>
public void ProcessInput()
{
k_RemoveList.Clear();
// Maintain a consumed control, so that other AMIs don't pick up the input, until it's no longer used
foreach (var lockedControl in m_LockedControls)
{
if (!lockedControl.provider.active || Mathf.Approximately(lockedControl.rawValue,
lockedControl.provider.GetControlData(lockedControl.index).defaultValue))
k_RemoveList.Add(lockedControl);
else
ConsumeControl(lockedControl);
}
// Remove separately, since we cannot remove while iterating
foreach (var inputControl in k_RemoveList)
{
if (!inputControl.provider.active)
ResetControl(inputControl);
m_LockedControls.Remove(inputControl);
}
k_RemoveList.Clear();
m_ProcessedInputs.Clear();
m_InputProcessorsCopy.Clear();
m_InputProcessorsCopy.AddRange(m_InputProcessors);
foreach (var processor in m_InputProcessorsCopy)
{
processor.processor.ProcessInput(processor.input, m_ConsumeControl);
}
if (processInput != null)
processInput(m_ProcessedInputs, m_ConsumeControl);
}
public void CreateDefaultActionMapInputs()
{
trackedObjectInput = (TrackedObject)CreateActionMapInput(m_TrackedObjectActionMap, null);
}
public ActionMapInput CreateActionMapInput(ActionMap map, InputDevice device)
{
// Check for improper use of action maps first
if (device != null && !IsValidActionMapForDevice(map, device))
return null;
var devices = device == null ? GetSystemDevices() : new List<InputDevice> { device };
var actionMapInput = ActionMapInput.Create(map);
// It's possible that there are no suitable control schemes for the device that is being initialized,
// so ActionMapInput can't be marked active
var successfulInitialization = false;
if (actionMapInput.TryInitializeWithDevices(devices))
{
successfulInitialization = true;
}
else
{
// For two-handed tools, the single device won't work, so collect the devices from the action map
devices = InputUtils.CollectInputDevicesFromActionMaps(new List<ActionMap>() { map });
if (actionMapInput.TryInitializeWithDevices(devices))
successfulInitialization = true;
}
if (successfulInitialization)
{
actionMapInput.autoReinitialize = false;
// Resetting AMIs cause all AMIs (active or not) that use the same sources to be reset, which causes
// problems (e.g. dropping objects because wasJustPressed becomes true when reset)
actionMapInput.resetOnActiveChanged = false;
actionMapInput.active = true;
}
return actionMapInput;
}
internal ActionMapInput CreateActionMapInputForObject(object obj, InputDevice device)
{
var customMap = obj as ICustomActionMap;
if (customMap != null)
{
if (customMap is IStandardActionMap)
Debug.LogWarning("Cannot use IStandardActionMap and ICustomActionMap together in " + obj.GetType());
var input = CreateActionMapInput(customMap.actionMap, device);
if (customMap.ignoreActionMapInputLocking)
m_IgnoreLocking[input] = customMap;
return input;
}
var standardMap = obj as IStandardActionMap;
if (standardMap != null)
{
standardMap.standardActionMap = m_StandardToolActionMap;
return CreateActionMapInput(m_StandardToolActionMap, device);
}
return null;
}
// TODO: Order doesn't matter any more ostensibly, so let's simply add when AMIs are created
public void UpdatePlayerHandleMaps()
{
var maps = m_PlayerHandle.maps;
maps.Clear();
foreach (var processor in m_InputProcessors)
{
var input = processor.input;
if (input != null)
maps.Add(input);
}
maps.Add(trackedObjectInput);
if (updatePlayerHandleMaps != null)
updatePlayerHandleMaps(maps);
}
static bool IsValidActionMapForDevice(ActionMap actionMap, InputDevice device)
{
var untaggedDevicesFound = 0;
var taggedDevicesFound = 0;
var nonMatchingTagIndices = 0;
var matchingTagIndices = 0;
if (actionMap == null)
return false;
foreach (var scheme in actionMap.controlSchemes)
{
foreach (var serializableDeviceType in scheme.deviceSlots)
{
if (serializableDeviceType.tagIndex != -1)
{
taggedDevicesFound++;
if (serializableDeviceType.tagIndex != device.tagIndex)
nonMatchingTagIndices++;
else
matchingTagIndices++;
}
else
{
untaggedDevicesFound++;
}
}
}
if (nonMatchingTagIndices > 0 && matchingTagIndices == 0)
{
LogError(string.Format("The action map {0} contains a specific device tag, but is being spawned on the wrong device tag", actionMap));
return false;
}
if (taggedDevicesFound > 0 && untaggedDevicesFound != 0)
{
LogError(string.Format("The action map {0} contains both a specific device tag and an unspecified tag, which is not supported", actionMap.name));
return false;
}
return true;
}
static void LogError(string error)
{
Debug.LogError(string.Format("DeviceInputModule: {0}", error));
}
void ConsumeControl(InputControl control)
{
// Consuming a control inherently locks it (for now), since consuming a control for one frame only might leave
// another AMI to pick up a wasPressed the next frame, since it's own input would have been cleared. The
// control is released when it returns to it's default value
m_LockedControls.Add(control);
ResetControl(control);
}
void ResetControl(InputControl control)
{
var ami = control.provider as ActionMapInput;
var playerHandleMaps = m_PlayerHandle.maps;
for (int i = 0; i < playerHandleMaps.Count; i++)
{
var input = playerHandleMaps[i];
if (m_IgnoreLocking.ContainsKey(input))
continue;
if (input != ami)
input.ResetControl(control);
}
}
public Node GetDeviceNode(InputDevice device)
{
string[] tags;
var deviceType = device.GetType();
if (!m_DeviceTypeTags.TryGetValue(deviceType, out tags))
{
tags = InputDeviceUtility.GetDeviceTags(deviceType);
m_DeviceTypeTags[deviceType] = tags;
}
if (tags != null && device.tagIndex != -1)
{
var tag = tags[device.tagIndex];
Node node;
if (m_TagToNode.TryGetValue(tag, out node))
return node;
}
return Node.None;
}
public void AddInputProcessor(IProcessInput processInput, object userData)
{
var rayOrigin = userData as Transform;
var inputDevice = inputDeviceForRayOrigin(rayOrigin);
var input = CreateActionMapInputForObject(processInput, inputDevice);
var order = 0;
var processInputAttribute = (ProcessInputAttribute)processInput.GetType().GetCustomAttributes(typeof(ProcessInputAttribute), true).FirstOrDefault();
if (processInputAttribute != null)
order = processInputAttribute.order;
m_InputProcessors.Add(new InputProcessor { processor = processInput, input = input, order = order });
m_InputProcessors.Sort((a, b) => b.order.CompareTo(a.order));
}
public void RemoveInputProcessor(IProcessInput processInput)
{
m_RemoveInputProcessorsCopy.Clear();
m_RemoveInputProcessorsCopy.AddRange(m_InputProcessors);
foreach (var processor in m_RemoveInputProcessorsCopy)
{
if (processor.processor == processInput)
{
m_InputProcessors.Remove(processor);
var input = processor.input;
for (var i = 0; i < input.controlCount; i++)
{
m_LockedControls.Remove(input[i]);
}
var customActionMap = processInput as ICustomActionMap;
if (customActionMap != null)
m_IgnoreLocking.Remove(processor.input);
}
}
}
}
}
#endif
You can’t perform that action at this time.
