XRInputManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using NibiruAxis;
  2. using NibiruTask;
  3. using Nxr.Internal;
  4. using SC.XR.Unity.Module_InputSystem;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. using UnityEngine.InputSystem;
  10. using UnityEngine.XR;
  11. using UnityEngine.XR.OpenXR.Input;
  12. using static Nxr.Internal.NxrButtonEvent;
  13. public class XRInputManager : MonoBehaviour
  14. {
  15. public Transform left = null;
  16. public Transform right = null;
  17. public InputActionProperty inputEvent;
  18. public List<InputActionReference> listinput;
  19. int xy = 8;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. for (int i = 0; i < 15; i++)
  24. {
  25. listinput[i].action.Enable();
  26. listinput[i].action.performed += OnEventL;
  27. listinput[i].action.canceled += OnCanceledEventL;
  28. }
  29. for (int i = 15; i < listinput.Count; i++)
  30. {
  31. listinput[i].action.Enable();
  32. listinput[i].action.performed += OnEventR;
  33. listinput[i].action.canceled += OnCanceledEventR;
  34. }
  35. InputDevices.deviceConnected += onDeviceConnected;
  36. InputDevices.deviceDisconnected += onDeviceDisConnected;
  37. Invoke("startXR", 1f);
  38. }
  39. public NxrTrackedDevice rightNxr;
  40. void startXR()
  41. {
  42. left.gameObject.SetActive(false);
  43. right.gameObject.SetActive(true);
  44. #if UNITY_EDITOR
  45. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, false);
  46. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, true);
  47. #else
  48. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.KS, true);
  49. Module_InputSystem.instance.SetActiveInputDevice(InputDeviceType.Head, false);
  50. #endif
  51. return;
  52. left.gameObject.SetActive(false);
  53. right.gameObject.SetActive(false);
  54. List<UnityEngine.XR.InputDevice> inputDevices = new List<UnityEngine.XR.InputDevice>();
  55. InputDevices.GetDevices(inputDevices);
  56. for (int i = 0; i < inputDevices.Count; i++)
  57. {
  58. onDeviceConnected(inputDevices[i]);
  59. }
  60. }
  61. private void Update()
  62. {
  63. if (NxrInput.GetControllerKeyDown(CKeyEvent.KEYCODE_3DOF_CONTROLLER_TRIGGER))
  64. {
  65. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  66. }
  67. if (NxrInput.GetControllerKeyUp(CKeyEvent.KEYCODE_3DOF_CONTROLLER_TRIGGER))
  68. {
  69. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  70. }
  71. if (Input.GetKeyDown(KeyCode.X))
  72. {
  73. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN);
  74. }
  75. if (Input.GetKeyUp(KeyCode.X))
  76. {
  77. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP);
  78. }
  79. right.position = NxrPlayerCtrl.Instance.mTransform.position;
  80. right.eulerAngles = NxrPlayerCtrl.Instance.mTransform.eulerAngles;
  81. }
  82. private void onDeviceDisConnected(UnityEngine.XR.InputDevice obj)
  83. {
  84. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  85. {
  86. Debug.Log("Left===>" + obj.isValid);
  87. left.gameObject.SetActive(obj.isValid);
  88. }
  89. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  90. {
  91. Debug.Log("Right===>" + obj.isValid);
  92. right.gameObject.SetActive(obj.isValid);
  93. }
  94. }
  95. private void OnApplicationPause(bool pause)
  96. {
  97. if (!pause)
  98. {
  99. List<UnityEngine.XR.InputDevice> inputDevices = new List<UnityEngine.XR.InputDevice>();
  100. InputDevices.GetDevices(inputDevices);
  101. for (int i = 0; i < inputDevices.Count; i++)
  102. {
  103. onDeviceConnected(inputDevices[i]);
  104. }
  105. }
  106. }
  107. private void onDeviceConnected(UnityEngine.XR.InputDevice obj)
  108. {
  109. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  110. {
  111. Debug.Log("Left===>" + obj.isValid);
  112. left.gameObject.SetActive(obj.isValid);
  113. }
  114. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  115. {
  116. Debug.Log("Right===>" + obj.isValid);
  117. right.gameObject.SetActive(obj.isValid);
  118. }
  119. }
  120. private void OnCanceledEventL(InputAction.CallbackContext context)
  121. {
  122. Debug.Log("OnCanceledEventL" + context.control.name);
  123. switch (context.control.name)
  124. {
  125. case "secondarytouched":
  126. break;
  127. case "secondarybutton":
  128. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.UP);
  129. break;
  130. case "primarytouched":
  131. break;
  132. case "primarybutton":
  133. Debug.Log("InputKeyCode.A===¡·UP");
  134. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.UP);
  135. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  136. break;
  137. case "touchpadtouched":
  138. break;
  139. case "thumbstickclicked":
  140. break;
  141. case "thumbstick":
  142. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  143. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  144. break;
  145. case "grip":
  146. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  147. break;
  148. case "grippressed":
  149. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.UP);
  150. break;
  151. case "triggerButton":
  152. // API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  153. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  154. break;
  155. case "menu":
  156. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.UP);
  157. break;
  158. }
  159. }
  160. void OnEventL(InputAction.CallbackContext context)
  161. {
  162. Debug.Log("OnEventL" + context.control.name);
  163. switch (context.control.name)
  164. {
  165. case "secondarytouched":
  166. break;
  167. case "secondarybutton":
  168. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Y, InputKeyState.DOWN);
  169. break;
  170. case "primarytouched":
  171. break;
  172. case "primarybutton":
  173. Debug.Log("InputKeyCode.A===¡·DOWN");
  174. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.X, InputKeyState.DOWN);
  175. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  176. break;
  177. case "touchpadtouched":
  178. break;
  179. case "thumbstickclicked":
  180. break;
  181. case "thumbstick":
  182. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  183. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  184. break;
  185. case "grip":
  186. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  187. break;
  188. case "grippressed":
  189. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LHallInside, InputKeyState.DOWN);
  190. break;
  191. case "triggerButton":
  192. // API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  193. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  194. break;
  195. case "menu":
  196. API_GSXR_Module_InputSystem_KS.GSXR_KSLeft.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.LFunction, InputKeyState.DOWN);
  197. break;
  198. }
  199. }
  200. private void OnCanceledEventR(InputAction.CallbackContext context)
  201. {
  202. Debug.Log("OnCanceledEventR" + context.control.name);
  203. switch (context.control.name)
  204. {
  205. case "secondarytouched":
  206. break;
  207. case "secondarybutton":
  208. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.UP);
  209. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  210. break;
  211. case "primarytouched":
  212. break;
  213. case "primarybutton":
  214. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.UP);
  215. break;
  216. case "touchpadtouched":
  217. break;
  218. case "thumbstickclicked":
  219. break;
  220. case "thumbstick":
  221. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  222. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  223. break;
  224. case "grip":
  225. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  226. break;
  227. case "grippressed":
  228. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.UP);
  229. break;
  230. case "triggerButton":
  231. // API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  232. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  233. break;
  234. case "menu":
  235. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.UP);
  236. break;
  237. }
  238. }
  239. void OnEventR(InputAction.CallbackContext context)
  240. {
  241. Debug.Log("OnEventR" + context.control.name);
  242. switch (context.control.name)
  243. {
  244. case "secondarytouched":
  245. break;
  246. case "secondarybutton":
  247. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.B, InputKeyState.DOWN);
  248. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  249. break;
  250. case "primarytouched":
  251. break;
  252. case "primarybutton":
  253. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.A, InputKeyState.DOWN);
  254. break;
  255. case "touchpadtouched":
  256. break;
  257. case "thumbstickclicked":
  258. break;
  259. case "thumbstick":
  260. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickX = xy + (int)(context.ReadValue<Vector2>().x * 10);
  261. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.JoystickY = xy + (int)(context.ReadValue<Vector2>().y * 10);
  262. break;
  263. case "grip":
  264. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallInside = 10 - (int)((context.ReadValue<float>()) * 10f);
  265. break;
  266. case "grippressed":
  267. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RHallInside, InputKeyState.DOWN);
  268. break;
  269. case "triggerButton":
  270. // API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.HallFoward = 10 - (int)((context.ReadValue<float>()) * 10f);
  271. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  272. break;
  273. case "menu":
  274. API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDataKS.inputKeys.InputDataAddKey(InputKeyCode.RFunction, InputKeyState.DOWN);
  275. break;
  276. }
  277. }
  278. // Debug.Log("OnEvent=name==>" + context.control.name);
  279. /*
  280. Debug.Log("OnEvent=layout==>" + context.control.layout);
  281. Debug.Log("OnEvent=noisy==>" + context.control.noisy);
  282. Debug.Log("OnEvent=path==>" + context.control.path);
  283. Debug.Log("OnEvent=shortDisplayName==>" + context.control.shortDisplayName);
  284. Debug.Log("OnEvent=synthetic==>" + context.control.synthetic);
  285. Debug.Log("OnEvent=valueSizeInBytes==>" + context.control.valueSizeInBytes);
  286. Debug.Log("OnEvent=valueType==>" + context.control.valueType.Name);
  287. Debug.Log("OnEvent=variants==>" + context.control.variants);
  288. Debug.Log("OnEvent=variants==>" + context.control.EvaluateMagnitude());*/
  289. }