A game about forced loneliness, made by TACStudios
1#if UNITY_EDITOR && UNITY_2021_1_OR_NEWER
2
3using System;
4using System.Collections.Generic;
5using UnityEditor.DeviceSimulation;
6using UnityEngine.InputSystem.LowLevel;
7
8namespace UnityEngine.InputSystem.Editor
9{
10 internal class InputSystemPlugin : DeviceSimulatorPlugin
11 {
12 internal Touchscreen SimulatorTouchscreen;
13
14 private bool m_InputSystemEnabled;
15 private bool m_Quitting;
16 private List<InputDevice> m_DisabledDevices;
17
18 public override string title => "Input System";
19
20 public override void OnCreate()
21 {
22 m_InputSystemEnabled = EditorPlayerSettingHelpers.newSystemBackendsEnabled;
23 if (m_InputSystemEnabled)
24 {
25 // Monitor whether the editor is quitting to avoid risking unsafe EnableDevice while quitting
26 UnityEditor.EditorApplication.quitting += OnQuitting;
27
28 m_DisabledDevices = new List<InputDevice>();
29
30 // deviceSimulator is never null when the plugin is instantiated by a simulator window, but it can be null during unit tests
31 if (deviceSimulator != null)
32 deviceSimulator.touchScreenInput += OnTouchEvent;
33 InputSystem.onDeviceChange += OnDeviceChange;
34
35 // UGUI elements like a button don't get pressed when multiple pointers for example mouse and touchscreen are sending data at the same time
36 foreach (var device in InputSystem.devices)
37 {
38 DisableConflictingDevice(device);
39 }
40
41 SimulatorTouchscreen = InputSystem.AddDevice<Touchscreen>("Device Simulator Touchscreen");
42 }
43 }
44
45 internal void OnTouchEvent(TouchEvent touchEvent)
46 {
47 // Input System does not accept 0 as id
48 var id = touchEvent.touchId + 1;
49
50 InputSystem.QueueStateEvent(SimulatorTouchscreen,
51 new TouchState
52 {
53 touchId = id,
54 phase = ToInputSystem(touchEvent.phase),
55 position = touchEvent.position
56 });
57 }
58
59 private void DisableConflictingDevice(InputDevice device)
60 {
61 if (device.native && (device is Mouse || device is Pen) && device.enabled)
62 {
63 InputSystem.DisableDevice(device);
64 m_DisabledDevices.Add(device);
65 }
66 }
67
68 private void OnDeviceChange(InputDevice device, InputDeviceChange change)
69 {
70 if (change == InputDeviceChange.Added || change == InputDeviceChange.Reconnected)
71 DisableConflictingDevice(device);
72 }
73
74 private static UnityEngine.InputSystem.TouchPhase ToInputSystem(UnityEditor.DeviceSimulation.TouchPhase original)
75 {
76 switch (original)
77 {
78 case UnityEditor.DeviceSimulation.TouchPhase.Began:
79 return UnityEngine.InputSystem.TouchPhase.Began;
80 case UnityEditor.DeviceSimulation.TouchPhase.Moved:
81 return UnityEngine.InputSystem.TouchPhase.Moved;
82 case UnityEditor.DeviceSimulation.TouchPhase.Ended:
83 return UnityEngine.InputSystem.TouchPhase.Ended;
84 case UnityEditor.DeviceSimulation.TouchPhase.Canceled:
85 return UnityEngine.InputSystem.TouchPhase.Canceled;
86 case UnityEditor.DeviceSimulation.TouchPhase.Stationary:
87 return UnityEngine.InputSystem.TouchPhase.Stationary;
88 default:
89 throw new ArgumentOutOfRangeException(nameof(original), original, "Unexpected value");
90 }
91 }
92
93 public override void OnDestroy()
94 {
95 if (m_InputSystemEnabled)
96 {
97 // deviceSimulator is never null when the plugin is instantiated by a simulator window, but it can be null during unit tests
98 if (deviceSimulator != null)
99 deviceSimulator.touchScreenInput -= OnTouchEvent;
100 InputSystem.onDeviceChange -= OnDeviceChange;
101
102 UnityEditor.EditorApplication.quitting -= OnQuitting;
103
104 if (SimulatorTouchscreen != null)
105 InputSystem.RemoveDevice(SimulatorTouchscreen);
106 foreach (var device in m_DisabledDevices)
107 {
108 // Note that m_Quitting is used here to mitigate the problem reported in issue tracker:
109 // https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-10774.
110 // Enabling a device will call into IOCTL of backend which will (may) be destroyed prior
111 // to this callback on Unity version <= 2022.2. This is not a fix for the actual problem
112 // of shutdown order but a package fix to mitigate this problem.
113 if (device.added && !m_Quitting)
114 InputSystem.EnableDevice(device);
115 }
116 }
117 }
118
119 private void OnQuitting()
120 {
121 m_Quitting = true;
122 }
123 }
124}
125
126#endif