-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathInputSystemPlugin.cs
More file actions
181 lines (154 loc) · 7.04 KB
/
Copy pathInputSystemPlugin.cs
File metadata and controls
181 lines (154 loc) · 7.04 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
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.DeviceSimulation;
using UnityEditor.UIElements;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.UIElements;
namespace UnityEngine.InputSystem.Editor
{
internal class InputSystemPlugin : DeviceSimulatorPlugin
{
internal Touchscreen SimulatorTouchscreen;
private bool m_InputSystemEnabled;
private bool m_Quitting;
private bool m_ConflictingDevicesDisabled;
private VisualElement m_RootElement;
private EditorWindow m_LastFocusedWindow;
private List<InputDevice> m_DisabledDevices;
public override string title => "Input System";
public override void OnCreate()
{
m_InputSystemEnabled = EditorPlayerSettingHelpers.newSystemBackendsEnabled;
if (m_InputSystemEnabled)
{
// Monitor whether the editor is quitting to avoid risking unsafe EnableDevice while quitting
UnityEditor.EditorApplication.quitting += OnQuitting;
// Poll the active window so conflicting devices are only disabled while the simulator is focused.
UnityEditor.EditorApplication.update += OnUpdate;
m_DisabledDevices = new List<InputDevice>();
// deviceSimulator is never null when the plugin is instantiated by a simulator window, but it can be null during unit tests
if (deviceSimulator != null)
deviceSimulator.touchScreenInput += OnTouchEvent;
InputSystem.onDeviceChange += OnDeviceChange;
SimulatorTouchscreen = InputSystem.AddDevice<Touchscreen>("Device Simulator Touchscreen");
}
}
internal void OnTouchEvent(TouchEvent touchEvent)
{
// Input System does not accept 0 as id
var id = touchEvent.touchId + 1;
InputSystem.QueueStateEvent(SimulatorTouchscreen,
new TouchState
{
touchId = id,
phase = ToInputSystem(touchEvent.phase),
position = touchEvent.position
});
}
public override VisualElement OnCreateUI()
{
m_RootElement = new VisualElement();
m_RootElement.Add(new HelpBox(
L10n.Tr("Manages Input System devices while the Simulator is focused."),
HelpBoxMessageType.Info));
return m_RootElement;
}
private void OnUpdate()
{
if (!EditorApplication.isPlaying)
return;
var focusedWindow = EditorWindow.focusedWindow;
if (focusedWindow == m_LastFocusedWindow)
return;
m_LastFocusedWindow = focusedWindow;
var simulatorFocused =
m_RootElement != null
&& focusedWindow != null
&& focusedWindow.rootVisualElement.panel == m_RootElement.panel;
if (simulatorFocused && !m_ConflictingDevicesDisabled)
{
// UGUI elements like a button don't get pressed when multiple pointers for example mouse and touchscreen are sending data at the same time
foreach (var device in InputSystem.devices)
DisableConflictingDevice(device);
m_ConflictingDevicesDisabled = true;
}
else if (!simulatorFocused && m_ConflictingDevicesDisabled)
{
ReenableConflictingDevices();
m_ConflictingDevicesDisabled = false;
}
}
private void DisableConflictingDevice(InputDevice device)
{
if (device.native && (device is Mouse || device is Pen) && device.enabled)
{
InputSystem.DisableDevice(device);
m_DisabledDevices.Add(device);
}
}
private void ReenableConflictingDevices()
{
foreach (var device in m_DisabledDevices)
{
// Note that m_Quitting is used here to mitigate the problem reported in issue tracker:
// https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-10774.
// Enabling a device will call into IOCTL of backend which may be destroyed prior
// to this callback on Unity version. This is not a fix for the actual problem
// of shutdown order but a package fix to mitigate this problem.
// The core problem with the destruction order was still there in Unity 6.5.
if (device.added && !m_Quitting)
InputSystem.EnableDevice(device);
}
m_DisabledDevices.Clear();
}
private void OnDeviceChange(InputDevice device, InputDeviceChange change)
{
// Only disable newly added/reconnected devices while the simulator is the active window.
if (!m_ConflictingDevicesDisabled)
return;
if (change == InputDeviceChange.Added || change == InputDeviceChange.Reconnected)
DisableConflictingDevice(device);
}
private static UnityEngine.InputSystem.TouchPhase ToInputSystem(UnityEditor.DeviceSimulation.TouchPhase original)
{
switch (original)
{
case UnityEditor.DeviceSimulation.TouchPhase.Began:
return UnityEngine.InputSystem.TouchPhase.Began;
case UnityEditor.DeviceSimulation.TouchPhase.Moved:
return UnityEngine.InputSystem.TouchPhase.Moved;
case UnityEditor.DeviceSimulation.TouchPhase.Ended:
return UnityEngine.InputSystem.TouchPhase.Ended;
case UnityEditor.DeviceSimulation.TouchPhase.Canceled:
return UnityEngine.InputSystem.TouchPhase.Canceled;
case UnityEditor.DeviceSimulation.TouchPhase.Stationary:
return UnityEngine.InputSystem.TouchPhase.Stationary;
default:
throw new ArgumentOutOfRangeException(nameof(original), original, "Unexpected value");
}
}
public override void OnDestroy()
{
if (m_InputSystemEnabled)
{
// deviceSimulator is never null when the plugin is instantiated by a simulator window, but it can be null during unit tests
if (deviceSimulator != null)
deviceSimulator.touchScreenInput -= OnTouchEvent;
InputSystem.onDeviceChange -= OnDeviceChange;
UnityEditor.EditorApplication.quitting -= OnQuitting;
UnityEditor.EditorApplication.update -= OnUpdate;
if (SimulatorTouchscreen != null)
InputSystem.RemoveDevice(SimulatorTouchscreen);
ReenableConflictingDevices();
m_RootElement = null;
}
}
private void OnQuitting()
{
m_Quitting = true;
}
}
}
#endif