ControllerInputFromAction.cs 20 KB

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