RemoteInput.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. using System;
  2. using UnityEngine.InputSystem;
  3. using UnityEngine.InputSystem.Users;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using UnityEngine.InputSystem.EnhancedTouch;
  6. using UE = UnityEngine;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. namespace Unity.RenderStreaming.Samples
  10. {
  11. using UnityInputSystem = UnityEngine.InputSystem.InputSystem;
  12. enum KeyboardEventType
  13. {
  14. KeyUp = 0,
  15. KeyDown = 1,
  16. }
  17. enum EventType
  18. {
  19. Keyboard = 0,
  20. Mouse = 1,
  21. MouseWheel = 2,
  22. Touch = 3,
  23. ButtonClick = 4,
  24. Gamepad = 5,
  25. }
  26. enum GamepadEventType {
  27. ButtonUp = 0,
  28. ButtonDown = 1,
  29. ButtonPressed = 2,
  30. Axis = 3
  31. }
  32. enum GamepadKeyCode {
  33. Button0 = 0,
  34. Button1,
  35. Button2,
  36. Button3,
  37. Button4,
  38. Button5,
  39. Button6,
  40. Button7,
  41. Button8,
  42. Button9,
  43. Axis0Button,
  44. Axis1Button,
  45. DpadUp,
  46. DpadDown,
  47. DpadLeft,
  48. DpadRight,
  49. Axis0 = 100,
  50. Axis1
  51. }
  52. internal static class RemoteInputReceiver
  53. {
  54. private static readonly Dictionary<RemoteInput, uint> s_mapRemoteInputAndInputUserId;
  55. private static readonly List<RemoteInput> s_listRemoteInput;
  56. static RemoteInputReceiver()
  57. {
  58. s_mapRemoteInputAndInputUserId = new Dictionary<RemoteInput, uint>();
  59. s_listRemoteInput = new List<RemoteInput>();
  60. }
  61. internal static void Dispose()
  62. {
  63. s_mapRemoteInputAndInputUserId.Clear();
  64. s_listRemoteInput.Clear();
  65. }
  66. public static IReadOnlyList<RemoteInput> All()
  67. {
  68. return s_listRemoteInput;
  69. }
  70. public static RemoteInput Create()
  71. {
  72. InputUser user = InputUser.CreateUserWithoutPairedDevices();
  73. user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice<Mouse>(), user);
  74. user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice<Keyboard>(), user);
  75. user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice<Gamepad>(), user);
  76. user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice<Touchscreen>(), user);
  77. RemoteInput remoteInput = new RemoteInput(ref user);
  78. s_mapRemoteInputAndInputUserId.Add(remoteInput, user.id);
  79. s_listRemoteInput.Add(remoteInput);
  80. return remoteInput;
  81. }
  82. internal static void Delete(RemoteInput remoteInput)
  83. {
  84. if (remoteInput == null)
  85. {
  86. throw new ArgumentException("The instance of argument is null");
  87. }
  88. bool found = s_mapRemoteInputAndInputUserId.TryGetValue(remoteInput, out uint userId);
  89. if (!found)
  90. {
  91. throw new ArgumentException("The instance of argument is not found");
  92. }
  93. InputUser user = InputUser.all.First(_user => _user.id == userId);
  94. var arrayDeviceId = user.pairedDevices.Select(device => device.deviceId).ToArray();
  95. user.UnpairDevicesAndRemoveUser();
  96. foreach (var deviceId in arrayDeviceId)
  97. {
  98. UnityInputSystem.RemoveDevice(UnityInputSystem.GetDeviceById(deviceId));
  99. }
  100. s_mapRemoteInputAndInputUserId.Remove(remoteInput);
  101. s_listRemoteInput.Remove(remoteInput);
  102. }
  103. }
  104. public interface IInput
  105. {
  106. Mouse RemoteMouse { get; }
  107. Keyboard RemoteKeyboard { get; }
  108. Touchscreen RemoteTouchscreen { get; }
  109. Gamepad RemoteGamepad { get; }
  110. }
  111. internal class RemoteInput : IInput, IDisposable
  112. {
  113. private GamepadState m_gamepadState;
  114. public Mouse RemoteMouse { get; }
  115. public Keyboard RemoteKeyboard { get; }
  116. public Touchscreen RemoteTouchscreen { get; }
  117. public Gamepad RemoteGamepad { get; }
  118. public Action<int> ActionButtonClick;
  119. private UnityEngine.Vector2Int m_prevMousePos;
  120. private KeyboardState m_keyboardState = new KeyboardState();
  121. private bool disposed;
  122. internal RemoteInput(ref InputUser user)
  123. {
  124. RemoteMouse = user.pairedDevices.FirstOrDefault(device => device is Mouse) as Mouse;
  125. RemoteKeyboard = user.pairedDevices.FirstOrDefault(device => device is Keyboard) as Keyboard;
  126. RemoteTouchscreen = user.pairedDevices.FirstOrDefault(device => device is Touchscreen) as Touchscreen;
  127. RemoteGamepad = user.pairedDevices.FirstOrDefault(device => device is Gamepad) as Gamepad;
  128. }
  129. ~RemoteInput()
  130. {
  131. Dispose();
  132. }
  133. public void Dispose()
  134. {
  135. if (this.disposed)
  136. {
  137. return;
  138. }
  139. RemoteInputReceiver.Delete(this);
  140. this.disposed = true;
  141. GC.SuppressFinalize(this);
  142. }
  143. //---------------------------------------------------------------------------------------------------------------------
  144. public void ProcessInput(byte[] bytes)
  145. {
  146. if (bytes == null)
  147. throw new ArgumentNullException();
  148. if(bytes.Length == 0)
  149. throw new ArgumentException("byte length is zero");
  150. switch ((EventType)bytes[0])
  151. {
  152. case EventType.Keyboard:
  153. var type = (KeyboardEventType)bytes[1];
  154. var repeat = bytes[2] == 1;
  155. var key = bytes[3];
  156. var character = (char)bytes[4];
  157. ProcessKeyEvent(type, repeat, key, character);
  158. break;
  159. case EventType.Mouse:
  160. var deltaX = BitConverter.ToInt16(bytes, 1);
  161. var deltaY = BitConverter.ToInt16(bytes, 3);
  162. var button = bytes[5];
  163. ProcessMouseMoveEvent(deltaX, deltaY, button);
  164. break;
  165. case EventType.MouseWheel:
  166. var scrollX = BitConverter.ToSingle(bytes, 1);
  167. var scrollY = BitConverter.ToSingle(bytes, 5);
  168. ProcessMouseWheelEvent(scrollX, scrollY);
  169. break;
  170. case EventType.Touch:
  171. {
  172. var length = bytes[1];
  173. var index = 2;
  174. var touches = new TouchState[length];
  175. for (int i = 0; i < length; i++)
  176. {
  177. const int INPUTSYSTEM_ZERO_ID_GUARD = 128; //ID 0 is reserved by inputsystem
  178. int identifier = BitConverter.ToInt32(bytes, index) + INPUTSYSTEM_ZERO_ID_GUARD;
  179. index += 4;
  180. var phase = (UnityEngine.InputSystem.TouchPhase)bytes[index];
  181. index += 1;
  182. var pageX = BitConverter.ToInt16(bytes, index);
  183. index += 2;
  184. var pageY = BitConverter.ToInt16(bytes, index);
  185. index += 2;
  186. var force = BitConverter.ToSingle(bytes, index);
  187. index += 4;
  188. touches[i] = new TouchState
  189. {
  190. touchId = identifier,
  191. phase = phase,
  192. position = new UnityEngine.Vector2Int(pageX, pageY),
  193. pressure = force
  194. };
  195. }
  196. ProcessTouchMoveEvent(touches);
  197. if (Touch.activeTouches.Count > length)
  198. {
  199. ChangeEndStateUnusedTouches(touches);
  200. }
  201. }
  202. break;
  203. case EventType.ButtonClick:
  204. var elementId = BitConverter.ToInt16(bytes, 1);
  205. ProcessButtonClickEvent(elementId);
  206. break;
  207. case EventType.Gamepad:
  208. {
  209. GamepadEventType gamepadEventType = (GamepadEventType)bytes[1];
  210. switch (gamepadEventType)
  211. {
  212. case GamepadEventType.ButtonDown:
  213. case GamepadEventType.ButtonUp:
  214. case GamepadEventType.ButtonPressed:
  215. {
  216. var buttonIndex = bytes[2];
  217. var value = BitConverter.ToDouble(bytes, 3);
  218. ProcessGamepadButtonEvent(gamepadEventType, (GamepadKeyCode) buttonIndex, value);
  219. }
  220. break;
  221. case GamepadEventType.Axis:
  222. {
  223. var buttonIndex = bytes[2];
  224. var x = BitConverter.ToDouble(bytes, 3);
  225. var y = BitConverter.ToDouble(bytes, 11);
  226. ProcessGamepadAxisEvent(x, y, (GamepadKeyCode) buttonIndex);
  227. }
  228. break;
  229. }
  230. UnityInputSystem.QueueStateEvent(RemoteGamepad, m_gamepadState);
  231. }
  232. break;
  233. }
  234. }
  235. #region Gamepads Events
  236. void ProcessGamepadButtonEvent(GamepadEventType state, GamepadKeyCode buttonIndex, double value)
  237. {
  238. GamepadButton buttonToUpdate = GamepadButton.DpadUp;
  239. GamepadState gamepadState = m_gamepadState;
  240. switch(buttonIndex)
  241. {
  242. case GamepadKeyCode.DpadUp:
  243. buttonToUpdate = GamepadButton.DpadUp;
  244. break;
  245. case GamepadKeyCode.DpadDown:
  246. buttonToUpdate = GamepadButton.DpadDown;
  247. break;
  248. case GamepadKeyCode.DpadLeft:
  249. buttonToUpdate = GamepadButton.DpadLeft;
  250. break;
  251. case GamepadKeyCode.DpadRight:
  252. buttonToUpdate = GamepadButton.DpadRight;
  253. break;
  254. case GamepadKeyCode.Button0:
  255. buttonToUpdate = GamepadButton.B;
  256. break;
  257. case GamepadKeyCode.Button1:
  258. buttonToUpdate = GamepadButton.A;
  259. break;
  260. case GamepadKeyCode.Button2:
  261. buttonToUpdate = GamepadButton.Y;
  262. break;
  263. case GamepadKeyCode.Button3:
  264. buttonToUpdate = GamepadButton.X;
  265. break;
  266. case GamepadKeyCode.Button4:
  267. buttonToUpdate = GamepadButton.LeftShoulder;
  268. break;
  269. case GamepadKeyCode.Button5:
  270. buttonToUpdate = GamepadButton.RightShoulder;
  271. break;
  272. case GamepadKeyCode.Button6:
  273. buttonToUpdate = GamepadButton.LeftTrigger;
  274. gamepadState.leftTrigger = (float)value;
  275. break;
  276. case GamepadKeyCode.Button7:
  277. buttonToUpdate = GamepadButton.RightTrigger;
  278. gamepadState.rightTrigger = (float) value;
  279. break;
  280. case GamepadKeyCode.Axis0Button:
  281. buttonToUpdate = GamepadButton.LeftStick;
  282. break;
  283. case GamepadKeyCode.Axis1Button:
  284. buttonToUpdate = GamepadButton.RightStick;
  285. break;
  286. default:
  287. UE.Debug.Log("Unmapped button code: " + buttonIndex);
  288. break;
  289. }
  290. m_gamepadState = gamepadState.WithButton(buttonToUpdate, GamepadEventType.ButtonDown == state || GamepadEventType.ButtonPressed == state);
  291. }
  292. void ProcessGamepadAxisEvent(double x, double y, GamepadKeyCode axisKeyCode)
  293. {
  294. GamepadState gamepadState = m_gamepadState;
  295. if(axisKeyCode == GamepadKeyCode.Axis0)
  296. gamepadState.leftStick = new UE.Vector2((float)x, (float)y);
  297. if(axisKeyCode == GamepadKeyCode.Axis1)
  298. gamepadState.rightStick = new UE.Vector2((float)x, (float)y);
  299. m_gamepadState = gamepadState;
  300. }
  301. #endregion
  302. void ProcessKeyEvent(KeyboardEventType state, bool repeat, byte keyCode, char character)
  303. {
  304. switch(state)
  305. {
  306. case KeyboardEventType.KeyDown:
  307. if (!repeat)
  308. {
  309. m_keyboardState.Set((Key)keyCode, true);
  310. UnityInputSystem.QueueStateEvent(RemoteKeyboard, m_keyboardState);
  311. }
  312. if(character != 0)
  313. {
  314. UnityInputSystem.QueueTextEvent(RemoteKeyboard, character);
  315. }
  316. break;
  317. case KeyboardEventType.KeyUp:
  318. m_keyboardState.Set((Key)keyCode, false);
  319. UnityInputSystem.QueueStateEvent(RemoteKeyboard, m_keyboardState);
  320. break;
  321. }
  322. }
  323. void ProcessMouseMoveEvent(short x, short y, byte button)
  324. {
  325. UnityEngine.Vector2Int pos = new UnityEngine.Vector2Int(x, y);
  326. UnityEngine.Vector2Int delta = pos - m_prevMousePos;
  327. UnityInputSystem.QueueStateEvent(RemoteMouse, new MouseState {
  328. position = pos,
  329. delta = delta,
  330. buttons = button
  331. });
  332. m_prevMousePos = pos;
  333. }
  334. void ProcessMouseWheelEvent(float scrollX, float scrollY)
  335. {
  336. UnityInputSystem.QueueStateEvent(RemoteMouse, new MouseState { scroll = new UnityEngine.Vector2(scrollX, scrollY) });
  337. }
  338. void ProcessTouchMoveEvent(TouchState[] touches)
  339. {
  340. for (var i = 0; i < touches.Length; i++)
  341. {
  342. UnityInputSystem.QueueStateEvent(RemoteTouchscreen, touches[i]);
  343. }
  344. }
  345. void ChangeEndStateUnusedTouches(TouchState[] touches)
  346. {
  347. int touchCount = Touch.activeTouches.Count;
  348. for (var i = 0; i < touchCount; i++)
  349. {
  350. int touchId = Touch.activeTouches[i].touchId;
  351. if (!Array.Exists(touches, v => v.touchId == touchId))
  352. {
  353. if (Touch.activeTouches[i].phase == TouchPhase.Ended)
  354. {
  355. continue;
  356. }
  357. UnityInputSystem.QueueStateEvent(RemoteTouchscreen, new TouchState
  358. {
  359. touchId = touchId,
  360. phase = TouchPhase.Ended,
  361. position = Touch.activeTouches[i].screenPosition
  362. });
  363. }
  364. }
  365. }
  366. void ProcessButtonClickEvent(int elementId)
  367. {
  368. ActionButtonClick?.Invoke(elementId);
  369. }
  370. }
  371. }