ControllerInputFromAction.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using GxrSdk;
  2. using SC.InputSystem;
  3. using SC.InputSystem.InputDeviceHandShank;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using UnityEngine;
  8. using UnityEngine.InputSystem;
  9. using UnityEngine.XR;
  10. using WCQ.CommonV1_20200325;
  11. public class ControllerInputFromAction : MonoBehaviour, IGxrControllerPoseHandler, IGxrButtonHandler
  12. {
  13. public ControllerIndex index = ControllerIndex.Left;
  14. public List<InputActionReference> listinput;
  15. public List<InputActionReference> listtrack;
  16. public Transform Controller
  17. {
  18. get {
  19. return GetController(index)?.inputDeviceHandShankUI.transform;
  20. }
  21. }
  22. private void Start() {
  23. SC.InputSystem.InputSystem.ISInitializeCallBack += InputSystemVaildCallBack;
  24. }
  25. private void OnDestroy() {
  26. SC.InputSystem.InputSystem.ISInitializeCallBack -= InputSystemVaildCallBack;
  27. }
  28. private void Update()
  29. {
  30. //GetController(index)?.gameObject.SetActive(true);
  31. //if (index == ControllerIndex.Left)
  32. // InputDataHandShank.HSData.HS0Connect = true;
  33. //else
  34. // InputDataHandShank.HSData.HS1Connect = true;
  35. //if (GetController(index))
  36. //{
  37. // GetController(index).inputDataHandShank.blueToothName = "K07";
  38. // GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  39. // GetController(index).inputDataHandShank.isVaild = true;
  40. //}
  41. //Debug.Log("LateUpdate " + GetController(index).inputDataHandShank.rotation);
  42. }
  43. void InputSystemVaildCallBack()
  44. {
  45. Invoke("DelayInvoke", 1);
  46. }
  47. void OnEnable()
  48. {
  49. // 注册控制器姿态监听器
  50. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrControllerPoseHandler>(this);
  51. // 注册按键事件监听器
  52. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrButtonHandler>(this);
  53. // 注册触摸事件监听器
  54. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrTouchHandler>(this);
  55. }
  56. void OnDisable()
  57. {
  58. // 注销控制器姿态监听器
  59. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrControllerPoseHandler>(this);
  60. // 注销按键事件监听器
  61. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrButtonHandler>(this);
  62. // 注销触摸事件监听器
  63. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrTouchHandler>(this);
  64. }
  65. void DelayInvoke() {
  66. Debug.Log("InputSystem Initialize ===" + index + " ");
  67. var HandedControllers = new List<UnityEngine.XR.InputDevice>();
  68. var desiredCharacteristics = (index == ControllerIndex.Left ? UnityEngine.XR.InputDeviceCharacteristics.Left : UnityEngine.XR.InputDeviceCharacteristics.Right) | UnityEngine.XR.InputDeviceCharacteristics.Controller;
  69. UnityEngine.XR.InputDevices.GetDevicesWithCharacteristics(desiredCharacteristics, HandedControllers);
  70. Debug.Log(HandedControllers.Count + " ===========");
  71. foreach (var device in HandedControllers)
  72. {
  73. Debug.Log(string.Format("Device name '{0}' has characteristics '{1}'", device.name, device.characteristics.ToString()));
  74. }
  75. if(index== ControllerIndex.Right)
  76. {
  77. Debug.Log("Left onDeviceConnected===>" + index);
  78. InputDataHandShank.HSData.HS1Connect = true;
  79. GetController(ControllerIndex.Left)?.gameObject.SetActive(true);
  80. GetController(index).inputDataHandShank.blueToothName = "K07";
  81. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  82. GetController(index).inputDataHandShank.isVaild = true;
  83. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
  84. }else
  85. {
  86. Debug.Log("Left onDeviceConnected===>" + index);
  87. InputDataHandShank.HSData.HS0Connect = false;
  88. GetController(ControllerIndex.Left)?.gameObject.SetActive(false);
  89. GetController(index).inputDataHandShank.blueToothName = "K07";
  90. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  91. GetController(index).inputDataHandShank.isVaild = false;
  92. }
  93. /*
  94. if (HandedControllers.Count == 1)
  95. {
  96. onDeviceConnected(HandedControllers[0]);
  97. }
  98. if (Application.platform != RuntimePlatform.Android)
  99. {
  100. onDeviceConnected(new UnityEngine.XR.InputDevice());
  101. }
  102. */
  103. // InputDevices.deviceConnected += onDeviceConnected;
  104. // InputDevices.deviceDisconnected += onDeviceDisConnected;
  105. }
  106. private void onDeviceDisConnected(UnityEngine.XR.InputDevice obj) {
  107. if (Application.platform == RuntimePlatform.Android)
  108. {
  109. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  110. {
  111. Debug.Log("Left onDeviceDisConnected===>" + index);
  112. InputDataHandShank.HSData.HS0Connect = false;
  113. GetController(ControllerIndex.Left)?.gameObject.SetActive(false);
  114. GetController(index).inputDataHandShank.blueToothName = "K07";
  115. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  116. GetController(index).inputDataHandShank.isVaild = false;
  117. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
  118. }
  119. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  120. {
  121. Debug.Log("Right onDeviceDisConnected===>" + index);
  122. InputDataHandShank.HSData.HS1Connect = false;
  123. GetController(ControllerIndex.Right)?.gameObject.SetActive(false);
  124. GetController(index).inputDataHandShank.blueToothName = "K07";
  125. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  126. GetController(index).inputDataHandShank.isVaild = false;
  127. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
  128. }
  129. }
  130. else {
  131. GetController(index)?.gameObject.SetActive(false);
  132. if (index == ControllerIndex.Left)
  133. InputDataHandShank.HSData.HS0Connect = false;
  134. else
  135. InputDataHandShank.HSData.HS1Connect = false;
  136. GetController(index).inputDataHandShank.blueToothName = "K07";
  137. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  138. GetController(index).inputDataHandShank.isVaild = false;
  139. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack -= updateRotationCallBack;
  140. }
  141. foreach ( var inputactionreference in listinput ) {
  142. inputactionreference.action.Disable();
  143. inputactionreference.action.performed -= OnInputPerformed;
  144. inputactionreference.action.canceled -= OnInputCanceled;
  145. }
  146. foreach ( var inputactionreference in listtrack ) {
  147. inputactionreference.action.Disable();
  148. inputactionreference.action.performed -= OnTrackPerformed;
  149. //inputactionreference.action.canceled -= OnTrackCanceled;
  150. }
  151. }
  152. private void onDeviceConnected(UnityEngine.XR.InputDevice obj)
  153. {
  154. if (Application.platform == RuntimePlatform.Android)
  155. {
  156. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Left.ToString()))
  157. {
  158. Debug.Log("Left onDeviceConnected===>" + index);
  159. InputDataHandShank.HSData.HS0Connect = true;
  160. GetController(ControllerIndex.Left)?.gameObject.SetActive(true);
  161. GetController(index).inputDataHandShank.blueToothName = "K07";
  162. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  163. GetController(index).inputDataHandShank.isVaild = true;
  164. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
  165. }
  166. if (obj.characteristics.ToString().Contains(InputDeviceCharacteristics.Right.ToString()))
  167. {
  168. Debug.Log("Right onDeviceConnected===>" + index);
  169. InputDataHandShank.HSData.HS1Connect = true;
  170. GetController(ControllerIndex.Right)?.gameObject.SetActive(true);
  171. GetController(index).inputDataHandShank.blueToothName = "K07";
  172. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  173. GetController(index).inputDataHandShank.isVaild = true;
  174. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
  175. }
  176. }
  177. else
  178. {
  179. GetController(index)?.gameObject.SetActive(true);
  180. if (index == ControllerIndex.Left)
  181. InputDataHandShank.HSData.HS0Connect = true;
  182. else
  183. InputDataHandShank.HSData.HS1Connect = true;
  184. GetController(index).inputDataHandShank.blueToothName = "K07";
  185. GetController(index).inputDataHandShank.handShankType = HandShankType.K07;
  186. GetController(index).inputDataHandShank.isVaild = true;
  187. GetController(index).inputDataGetHandShank.inputDataGetHandShank3DOF.updateRotationCallBack += updateRotationCallBack;
  188. }
  189. foreach ( var inputactionreference in listinput ) {
  190. inputactionreference.action.Enable();
  191. inputactionreference.action.performed += OnInputPerformed;
  192. inputactionreference.action.canceled += OnInputCanceled;
  193. }
  194. foreach ( var inputactionreference in listtrack ) {
  195. inputactionreference.action.Enable();
  196. inputactionreference.action.performed += OnTrackPerformed;
  197. // inputactionreference.action.canceled += OnTrackCanceled;
  198. }
  199. }
  200. public Quaternion controllerRotation { get; private set; }
  201. public Vector3 controllerPosition { get; private set; }
  202. void OnTrackPerformed(InputAction.CallbackContext context) {
  203. //Debug.Log(Time.frameCount+" OnTrackPerformed" + index + " " + context.control.name);
  204. switch ( context.control.name ) {
  205. case "deviceposition":
  206. case "devicePosition":
  207. controllerPosition = context.ReadValue<Vector3>();
  208. break;
  209. case "devicerotation":
  210. case "deviceRotation":
  211. //Debug.Log(Time.frameCount + " OnTrackPerformed" + index + " " + context.control.name+" "+context.ReadValue<Quaternion>());
  212. // GetController(index).inputDataHandShank.rotation = context.ReadValue<Quaternion>();
  213. controllerRotation = context.ReadValue<Quaternion>();
  214. break;
  215. }
  216. }
  217. //private void OnTrackCanceled(InputAction.CallbackContext context) {
  218. // Debug.Log("OnTrackCanceled" + index + " " + context.control.name);
  219. // switch ( context.control.name ) {
  220. // case "devicePosition":
  221. // break;
  222. // case "deviceRotation":
  223. // GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  224. // break;
  225. // }
  226. //}
  227. public GameObject head;
  228. void updateRotationCallBack() {
  229. Debug.Log(index+" updateRotationCallBack===>");
  230. GetController(index).inputDataHandShank.rotation = controllerRotation;//Quaternion.AngleAxis(20, transform.forward)*
  231. GetController(index).inputDataHandShank.position = new Vector3(head.transform.position.x, head.transform.position.y-0.5f, head.transform.position.z);
  232. }
  233. private void LateUpdate()
  234. {
  235. //Debug.Log("LateUpdate " + GetController(index).inputDataHandShank.rotation);
  236. if (GetController(index))
  237. {
  238. // GetController(index).inputDeviceHandShankUI.transform.rotation = controllerRotation;
  239. //GetController(index).inputDeviceHandShankUI.model.lineIndicate.transform.rotation = controllerRotation;
  240. //if (GetController(index).inputDeviceUIBase)
  241. //{
  242. // GetController(index).inputDeviceUIBase.BeforTargetDetecterUpdateUI();
  243. //}
  244. //if (GetController(index).inputDataProcessBase != null)
  245. //{
  246. // GetController(index).inputDataProcessBase.OnUpdateAfterUI();
  247. //}
  248. ///没有UI,没必要执行TargetDecter的必要
  249. //if (GetController(index).inputDeviceUIBase != null && GetController(index).inputDeviceTargetDetecterBase != null)
  250. //{
  251. // GetController(index).inputDeviceTargetDetecterBase.OnUpdate();
  252. //}
  253. //if (GetController(index).inputDeviceUIBase)
  254. //{
  255. // GetController(index).inputDeviceUIBase.AfterTargetDetecterUpdateUI();
  256. //}
  257. //GetController(index).inputDeviceHandShankUI.modelK07.transform.rotation = controllerRotation;
  258. //if (GetController(index).inputDataBase != null)
  259. //{
  260. // GetController(index).inputDataBase.OnUpdate();
  261. //}
  262. //if (GetController(index).inputDataGetBase != null)
  263. //{
  264. // GetController(index).inputDataGetBase.OnUpdate();
  265. //}
  266. //GetController(index).inputDataHandShank.rotation = controllerRotation;
  267. //GetController(index).inputDataHandShank.position = controllerPosition;
  268. //if (GetController(index).inputDevicePartStatusBase != null)
  269. //{
  270. // GetController(index).inputDevicePartStatusBase.OnUpdate();
  271. //}
  272. /////如果数据无效或不可获取,则关闭UI并退出
  273. //if (GetController(index).inputDataBase.isVaild == false)
  274. //{
  275. // GetController(index).inputDeviceUIBase.UIDisable();
  276. // DebugMy.Log("inputDataBase.isVaild == false ,Turn Off UI and Return", this);
  277. // return;
  278. //}
  279. //else
  280. //{
  281. // DebugMy.Log("inputDataBase.isVaild == true", this);
  282. // GetController(index).inputDeviceUIBase.UIEnable();
  283. //}
  284. /////一些UI坐标值刷新必须在UI更新前执行完
  285. //if (GetController(index).inputDataProcessBase != null)
  286. //{
  287. // GetController(index).inputDataProcessBase.OnUpdateBeforUI();
  288. //}
  289. //if (GetController(index).inputDeviceUIBase)
  290. //{
  291. // GetController(index).inputDeviceUIBase.BeforTargetDetecterUpdateUI();
  292. //}
  293. //if (GetController(index).inputDataProcessBase != null)
  294. //{
  295. // GetController(index).inputDataProcessBase.OnUpdateAfterUI();
  296. //}
  297. /////没有UI,没必要执行TargetDecter的必要
  298. //if (GetController(index).inputDeviceUIBase != null && GetController(index).inputDeviceTargetDetecterBase != null)
  299. //{
  300. // GetController(index).inputDeviceTargetDetecterBase.OnUpdate();
  301. //}
  302. //if (GetController(index).inputDeviceUIBase)
  303. //{
  304. // GetController(index).inputDeviceUIBase.AfterTargetDetecterUpdateUI();
  305. //} ///一些UI坐标值刷新必须在UI更新前执行完
  306. //if (GetController(index) != null)
  307. //{
  308. // GetController(index).inputDeviceUIBase.model.lineIndicate.DrawLineIndicate() ;
  309. //}
  310. }
  311. }
  312. int xy = 8;
  313. void OnInputPerformed(InputAction.CallbackContext context) {
  314. Debug.Log("OnInput================>" + index + " " + context.control.name);
  315. switch ( context.control.name ) {
  316. case "secondarytouched":
  317. break;
  318. case "secondarybutton":
  319. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  320. break;
  321. case "primarytouched":
  322. break;
  323. case "primarybutton":
  324. break;
  325. case "touchpadtouched":
  326. break;
  327. case "thumbstickclicked":
  328. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Tp, InputKeyState.DOWN);
  329. break;
  330. case "thumbstick":
  331. GetController(index)?.inputDataHandShank.InputDataAddTouch(true,
  332. new Vector2(xy + (int)(context.ReadValue<Vector2>().x * 10), xy + (int)(context.ReadValue<Vector2>().y * 10)));
  333. break;
  334. case "grip":
  335. break;
  336. case "grippressed":
  337. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  338. break;
  339. case "triggerpressed":
  340. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  341. break;
  342. case "menu":
  343. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Function, InputKeyState.DOWN);
  344. break;
  345. case "triggerButton":
  346. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  347. break;
  348. case "gripButton":
  349. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  350. break;
  351. }
  352. }
  353. private void OnInputCanceled(InputAction.CallbackContext context) {
  354. Debug.Log("OnInput================>" + index + " " + context.control.name);
  355. switch ( context.control.name ) {
  356. case "secondarytouched":
  357. break;
  358. case "secondarybutton":
  359. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  360. break;
  361. case "primarytouched":
  362. break;
  363. case "primarybutton":
  364. break;
  365. case "touchpadtouched":
  366. break;
  367. case "thumbstickclicked":
  368. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Tp, InputKeyState.UP);
  369. break;
  370. case "thumbstick":
  371. GetController(index)?.inputDataHandShank.InputDataAddTouch(true,
  372. new Vector2(xy + (int)(context.ReadValue<Vector2>().x * 10), xy + (int)(context.ReadValue<Vector2>().y * 10)));
  373. break;
  374. case "grip":
  375. break;
  376. case "grippressed":
  377. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  378. break;
  379. case "triggerpressed":
  380. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  381. break;
  382. case "menu":
  383. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Function, InputKeyState.UP);
  384. break;
  385. case "triggerButton":
  386. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  387. break;
  388. case "gripButton":
  389. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  390. break;
  391. }
  392. }
  393. public enum ControllerIndex {
  394. Left = 0,
  395. Right = 1
  396. }
  397. InputDeviceHandShankPart GetController(ControllerIndex controllerIndex) {
  398. if ( SC.InputSystem.InputSystem.Instant && SC.InputSystem.InputSystem.Instant.HandShank ) {
  399. if ( ControllerIndex.Left == controllerIndex ) {
  400. return SC.InputSystem.InputSystem.Instant.HandShank.inputDeviceHandShankPartList.Where(e => e.PartType == SC.InputSystem.InputDevicePartType.HandShankOne).FirstOrDefault();
  401. } else if ( ControllerIndex.Right == controllerIndex ) {
  402. return SC.InputSystem.InputSystem.Instant.HandShank.inputDeviceHandShankPartList.Where(e => e.PartType == SC.InputSystem.InputDevicePartType.HandShankTwo).FirstOrDefault();
  403. }
  404. }
  405. return null;
  406. }
  407. public void OnControllerPoseUpdated(GxrControllerPoseEventData eventData)
  408. {
  409. controllerRotation = eventData.CurrentRotation;
  410. }
  411. public void OnButtonDown(GxrButtonEventData eventData)
  412. {
  413. if(index==ControllerIndex.Right)
  414. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN);
  415. }
  416. public void OnButtonUp(GxrButtonEventData eventData)
  417. {
  418. if (index == ControllerIndex.Right)
  419. GetController(index)?.inputDataBase.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP);
  420. }
  421. public void OnButtonClick(GxrButtonEventData eventData)
  422. {
  423. }
  424. public void OnButtonLongClick(GxrButtonEventData eventData)
  425. {
  426. }
  427. public void OnButtonDoubleClick(GxrButtonEventData eventData)
  428. {
  429. }
  430. public void OnTouchBegin(GxrTouchEventData eventData)
  431. {
  432. }
  433. public void OnTouchEnd(GxrTouchEventData eventData)
  434. {
  435. }
  436. public void OnTouchSwipe(GxrTouchEventData eventData)
  437. {
  438. }
  439. public void OnTouchPositionUpdated(GxrTouchEventData eventData)
  440. {
  441. }
  442. }