ControllerInfoBase.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. using AOT;
  2. using SC.XR.Unity;
  3. using SC.XR.Unity.Module_InputSystem;
  4. using SC.XR.Unity.Module_InputSystem.InputDeviceGC.KS;
  5. using System;
  6. using System.Collections.Generic;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. using static API_GSXR_Module_InputSystem_KS;
  11. using static API_GSXR_Slam;
  12. public class ControllerInfoBase : MonoBehaviour
  13. {
  14. #region CanvasText
  15. [SerializeField]
  16. protected Text title;
  17. [SerializeField]
  18. protected TextMeshProUGUI Version;
  19. [SerializeField]
  20. protected TextMeshProUGUI button_XA;
  21. [SerializeField]
  22. protected TextMeshProUGUI button_XA2;
  23. [SerializeField]
  24. protected TextMeshProUGUI button_YB;
  25. [SerializeField]
  26. protected TextMeshProUGUI button_YB2;
  27. [SerializeField]
  28. protected TextMeshProUGUI button_Function;
  29. [SerializeField]
  30. protected TextMeshProUGUI button_Function2;
  31. [SerializeField]
  32. protected TextMeshProUGUI button_Side;
  33. [SerializeField]
  34. protected TextMeshProUGUI button_Side2;
  35. [SerializeField]
  36. protected TextMeshProUGUI button_Trigger;
  37. [SerializeField]
  38. protected TextMeshProUGUI button_Trigger2;
  39. [SerializeField]
  40. protected TextMeshProUGUI button_JoystickKey;
  41. [SerializeField]
  42. protected TextMeshProUGUI button_JoystickKey2;
  43. [SerializeField]
  44. protected TextMeshProUGUI joyStick;
  45. [SerializeField]
  46. protected TextMeshProUGUI Hall_Forward;
  47. [SerializeField]
  48. protected TextMeshProUGUI Hall_Inside;
  49. [SerializeField]
  50. protected TextMeshProUGUI rotation;
  51. [SerializeField]
  52. protected TextMeshProUGUI location;
  53. [SerializeField]
  54. protected TextMeshProUGUI BatteryLevel;
  55. [SerializeField]
  56. protected TextMeshProUGUI Text_button_XA;
  57. [SerializeField]
  58. protected TextMeshProUGUI Text_button_YB;
  59. [SerializeField]
  60. protected TextMeshProUGUI Text_button_Function;
  61. [SerializeField]
  62. protected TextMeshProUGUI Text_button_Side;
  63. [SerializeField]
  64. protected TextMeshProUGUI Text_button_Trigger;
  65. [SerializeField]
  66. protected TextMeshProUGUI Text_button_joystickKey;
  67. [SerializeField]
  68. protected TextMeshProUGUI Text_rotation;
  69. [SerializeField]
  70. protected TextMeshProUGUI Text_location;
  71. [SerializeField]
  72. protected Button vibration;
  73. #endregion
  74. public bool is_Left = false;
  75. bool isOne=true;
  76. protected void Start()
  77. {
  78. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateRegister(ChangeColor);
  79. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateRegister(KeyDown);
  80. API_GSXR_Module_InputSystem.GSXR_KeyUpDelegateRegister(KeyUp);
  81. API_GSXR_Module_InputSystem.GSXR_KeyLongDelegateRegister(KeyLong);
  82. API_GSXR_Module_InputSystem.GSXR_KeyTouchEnterDelegateRegister(KeyTouchEnter);
  83. API_GSXR_Module_InputSystem.GSXR_KeyTouchExitDelegateRegister(KeyTouchExit);
  84. API_GSXR_Slam.plugin.GSXR_Set_ControllerHallRangeCallback(OnHallRangeEvent);
  85. }
  86. protected void Update()
  87. {
  88. //if (API_GSXR_Slam.SlamManager == null && !API_GSXR_Slam.SlamManager.IsRunning)
  89. //{
  90. // return;
  91. //}
  92. if (isOne)
  93. {
  94. isOne = false;
  95. SetControllerType();
  96. }
  97. ShowControllerData();
  98. }
  99. protected void LateUpdate()
  100. {
  101. //if (API_GSXR_Slam.SlamManager == null && !API_GSXR_Slam.SlamManager.IsRunning)
  102. //{
  103. // return;
  104. //}
  105. UpdateHallRange();
  106. ShowDeviceBattery();
  107. }
  108. protected void SetControllerType()
  109. {
  110. if (is_Left)
  111. {
  112. title.text = "Left Controller";
  113. Version.text = "Version:" + API_GSXR_Slam.plugin.GSXR_Get_ControllerHSVersion((int)GCType.Left).ToString();
  114. }
  115. else
  116. {
  117. title.text = "Right Controller";
  118. Text_button_XA.text = "A:";
  119. Text_button_YB.text = "B:";
  120. Version.text = "Version:"+ API_GSXR_Slam.plugin.GSXR_Get_ControllerHSVersion((int)GCType.Right).ToString();
  121. }
  122. }
  123. protected void ShowControllerData()
  124. {
  125. if (is_Left)
  126. {
  127. joyStick.text = GSXR_Get_JoystickValue(GCType.Left).ToString();
  128. if (GSXR_Get_JoystickValue(GCType.Left).x != 8 || GSXR_Get_JoystickValue(GCType.Left).y != 8)
  129. {
  130. ChangeColor_Green(joyStick);
  131. }
  132. rotation.text = GSXR_KSRotation(GCType.Left).ToString("f3");
  133. if (GSXR_KSRotation(GCType.Left) != Quaternion.identity)
  134. {
  135. ChangeColor_Green(rotation);
  136. ChangeColor_Green(Text_rotation);
  137. }
  138. location.text = GSXR_KSPosition(GCType.Left).ToString("f3");
  139. if (GSXR_KSPosition(GCType.Left) != Vector3.zero)
  140. {
  141. ChangeColor_Green(location);
  142. ChangeColor_Green(Text_location);
  143. }
  144. }
  145. else
  146. {
  147. joyStick.text = GSXR_Get_JoystickValue(GCType.Right).ToString();
  148. if (GSXR_Get_JoystickValue(GCType.Right).x != 8 || GSXR_Get_JoystickValue(GCType.Right).y != 8)
  149. {
  150. ChangeColor_Green(joyStick);
  151. }
  152. rotation.text = GSXR_KSRotation(GCType.Right).ToString("f3");
  153. if (GSXR_KSRotation(GCType.Right) != Quaternion.identity)
  154. {
  155. ChangeColor_Green(rotation);
  156. ChangeColor_Green(Text_rotation);
  157. }
  158. location.text = GSXR_KSPosition(GCType.Right).ToString("f3");
  159. if (GSXR_KSPosition(GCType.Right) != Vector3.zero)
  160. {
  161. ChangeColor_Green(location);
  162. ChangeColor_Green(Text_location);
  163. }
  164. }
  165. }
  166. #region Controller Key State
  167. protected void KeyDown(InputKeyCode keyCode, InputDevicePartBase part)
  168. {
  169. if (part.PartType==InputDevicePartType.KSLeft || part.PartType == InputDevicePartType.KSRight)
  170. {
  171. ShowKeyState(keyCode, "Down");
  172. }
  173. }
  174. protected void KeyUp(InputKeyCode keyCode, InputDevicePartBase part)
  175. {
  176. if (part.PartType == InputDevicePartType.KSLeft || part.PartType == InputDevicePartType.KSRight)
  177. {
  178. ShowKeyState(keyCode, "Up");
  179. }
  180. }
  181. protected void KeyLong(InputKeyCode keyCode, InputDevicePartBase part)
  182. {
  183. if (part.PartType == InputDevicePartType.KSLeft || part.PartType == InputDevicePartType.KSRight)
  184. {
  185. ShowKeyState(keyCode, "Long");
  186. }
  187. }
  188. protected void KeyTouchEnter(InputKeyCode keyCode, InputDevicePartBase part)
  189. {
  190. if (part.PartType == InputDevicePartType.KSLeft || part.PartType == InputDevicePartType.KSRight)
  191. {
  192. ShowTouchKeyState(keyCode, "TouchEnter");
  193. }
  194. }
  195. protected void KeyTouchExit(InputKeyCode keyCode, InputDevicePartBase part)
  196. {
  197. if (part.PartType == InputDevicePartType.KSLeft || part.PartType == InputDevicePartType.KSRight)
  198. {
  199. ShowTouchKeyState(keyCode, "TouchExit");
  200. }
  201. }
  202. protected void ShowKeyState(InputKeyCode keyCode, string str)
  203. {
  204. if (is_Left)
  205. {
  206. switch (keyCode)
  207. {
  208. case InputKeyCode.X:
  209. ControllerKeyState(button_XA2, str);
  210. break;
  211. case InputKeyCode.Y:
  212. ControllerKeyState(button_YB2, str);
  213. break;
  214. case InputKeyCode.LHallForward:
  215. ControllerKeyState(button_Trigger2, str);
  216. break;
  217. case InputKeyCode.LFunction:
  218. ControllerKeyState(button_Function2, str);
  219. break;
  220. case InputKeyCode.LHallInside:
  221. ControllerKeyState(button_Side2, str);
  222. break;
  223. case InputKeyCode.LjoystickKey:
  224. ControllerKeyState(button_JoystickKey2, str);
  225. break;
  226. default:
  227. break;
  228. }
  229. }
  230. else
  231. {
  232. switch (keyCode)
  233. {
  234. case InputKeyCode.A:
  235. ControllerKeyState(button_XA2, str);
  236. break;
  237. case InputKeyCode.B:
  238. ControllerKeyState(button_YB2, str);
  239. break;
  240. case InputKeyCode.RHallForward:
  241. ControllerKeyState(button_Trigger2, str);
  242. break;
  243. case InputKeyCode.RFunction:
  244. ControllerKeyState(button_Function2, str);
  245. break;
  246. case InputKeyCode.RHallInside:
  247. ControllerKeyState(button_Side2, str);
  248. break;
  249. case InputKeyCode.RjoystickKey:
  250. ControllerKeyState(button_JoystickKey2, str);
  251. break;
  252. default:
  253. break;
  254. }
  255. }
  256. }
  257. protected void ShowTouchKeyState(InputKeyCode keyCode, string str)
  258. {
  259. if (is_Left)
  260. {
  261. switch (keyCode)
  262. {
  263. case InputKeyCode.X:
  264. ControllerKeyState(button_XA, str);
  265. break;
  266. case InputKeyCode.Y:
  267. ControllerKeyState(button_YB, str);
  268. break;
  269. case InputKeyCode.LHallForward:
  270. ControllerKeyState(button_Trigger, str);
  271. break;
  272. case InputKeyCode.LFunction:
  273. ControllerKeyState(button_Function, str);
  274. break;
  275. case InputKeyCode.LHallInside:
  276. ControllerKeyState(button_Side, str);
  277. break;
  278. case InputKeyCode.LjoystickKey:
  279. ControllerKeyState(button_JoystickKey, str);
  280. break;
  281. default:
  282. break;
  283. }
  284. }
  285. else
  286. {
  287. switch (keyCode)
  288. {
  289. case InputKeyCode.A:
  290. ControllerKeyState(button_XA, str);
  291. break;
  292. case InputKeyCode.B:
  293. ControllerKeyState(button_YB, str);
  294. break;
  295. case InputKeyCode.RHallForward:
  296. ControllerKeyState(button_Trigger, str);
  297. break;
  298. case InputKeyCode.RFunction:
  299. ControllerKeyState(button_Function, str);
  300. break;
  301. case InputKeyCode.RHallInside:
  302. ControllerKeyState(button_Side, str);
  303. break;
  304. case InputKeyCode.RjoystickKey:
  305. ControllerKeyState(button_JoystickKey, str);
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. }
  312. protected void ControllerKeyState(TextMeshProUGUI _text, string str)
  313. {
  314. _text.text = str;
  315. }
  316. #endregion
  317. #region Show Hall Range Value
  318. public class HallRangeData
  319. {
  320. public int deviceID;
  321. public int HallFoward;
  322. public int HallInside;
  323. }
  324. public static List<HallRangeData> TempHallRangeDataList = new List<HallRangeData>();
  325. [MonoPInvokeCallback(typeof(Action<int, int, int>))]
  326. static void OnHallRangeEvent(int hall_x, int hall_y, int lr)
  327. {
  328. TempHallRangeDataList.Add(new HallRangeData() { HallInside = hall_x, HallFoward = hall_y, deviceID = lr });
  329. }
  330. protected void UpdateHallRange()
  331. {
  332. if (TempHallRangeDataList.Count > 0)
  333. {
  334. if ((is_Left && TempHallRangeDataList[0].deviceID == 0)
  335. ||
  336. (!is_Left && TempHallRangeDataList[0].deviceID == 1))
  337. {
  338. Hall_Forward.text = TempHallRangeDataList[0].HallFoward.ToString();
  339. if (TempHallRangeDataList[0].HallFoward != 10)
  340. {
  341. ChangeColor_Green(Hall_Forward);
  342. }
  343. Hall_Inside.text = TempHallRangeDataList[0].HallInside.ToString();
  344. if (TempHallRangeDataList[0].HallInside != 10)
  345. {
  346. ChangeColor_Green(Hall_Inside);
  347. }
  348. TempHallRangeDataList.RemoveAt(0);
  349. }
  350. }
  351. }
  352. #endregion
  353. protected void ChangeColor(InputKeyCode keyCode, InputDevicePartBase part)
  354. {
  355. if (is_Left)
  356. {
  357. if (keyCode == InputKeyCode.LHallForward)
  358. {
  359. ChangeColor_Green(button_Trigger);
  360. ChangeColor_Green(button_Trigger2);
  361. ChangeColor_Green(Text_button_Trigger);
  362. }
  363. if (keyCode == InputKeyCode.LHallInside)
  364. {
  365. ChangeColor_Green(button_Side);
  366. ChangeColor_Green(button_Side2);
  367. ChangeColor_Green(Text_button_Side);
  368. }
  369. if (keyCode == InputKeyCode.X)
  370. {
  371. ChangeColor_Green(button_XA);
  372. ChangeColor_Green(button_XA2);
  373. ChangeColor_Green(Text_button_XA);
  374. }
  375. if (keyCode == InputKeyCode.Y)
  376. {
  377. ChangeColor_Green(button_YB);
  378. ChangeColor_Green(button_YB2);
  379. ChangeColor_Green(Text_button_YB);
  380. }
  381. if (keyCode == InputKeyCode.LFunction)
  382. {
  383. ChangeColor_Green(button_Function);
  384. ChangeColor_Green(button_Function2);
  385. ChangeColor_Green(Text_button_Function);
  386. }
  387. if (keyCode == InputKeyCode.LjoystickKey)
  388. {
  389. ChangeColor_Green(button_JoystickKey);
  390. ChangeColor_Green(button_JoystickKey2);
  391. ChangeColor_Green(Text_button_joystickKey);
  392. }
  393. }
  394. else
  395. {
  396. if (keyCode == InputKeyCode.RHallForward)
  397. {
  398. ChangeColor_Green(button_Trigger);
  399. ChangeColor_Green(button_Trigger2);
  400. ChangeColor_Green(Text_button_Trigger);
  401. }
  402. if (keyCode == InputKeyCode.RHallInside)
  403. {
  404. ChangeColor_Green(button_Side);
  405. ChangeColor_Green(button_Side2);
  406. ChangeColor_Green(Text_button_Side);
  407. }
  408. if (keyCode == InputKeyCode.A)
  409. {
  410. ChangeColor_Green(button_XA);
  411. ChangeColor_Green(button_XA2);
  412. ChangeColor_Green(Text_button_XA);
  413. }
  414. if (keyCode == InputKeyCode.B)
  415. {
  416. ChangeColor_Green(button_YB);
  417. ChangeColor_Green(button_YB2);
  418. ChangeColor_Green(Text_button_YB);
  419. }
  420. if (keyCode == InputKeyCode.RFunction)
  421. {
  422. ChangeColor_Green(button_Function);
  423. ChangeColor_Green(button_Function2);
  424. ChangeColor_Green(Text_button_Function);
  425. }
  426. if (keyCode == InputKeyCode.RjoystickKey)
  427. {
  428. ChangeColor_Green(button_JoystickKey);
  429. ChangeColor_Green(button_JoystickKey2);
  430. ChangeColor_Green(Text_button_joystickKey);
  431. }
  432. }
  433. }
  434. protected void ShowDeviceBattery()
  435. {
  436. if (is_Left)
  437. {
  438. BatteryLevel.text = "BatteryLevel: " + API_GSXR_Slam.plugin.GSXR_Get_ControllerBattery((int)GCType.Left);
  439. }
  440. else
  441. {
  442. BatteryLevel.text = "BatteryLevel: " + API_GSXR_Slam.plugin.GSXR_Get_ControllerBattery((int)GCType.Right);
  443. }
  444. }
  445. protected void ChangeColor_Green(TextMeshProUGUI text)
  446. {
  447. if (text.color!=Color.green)
  448. {
  449. text.color = Color.green;
  450. }
  451. }
  452. private void OnDestroy()
  453. {
  454. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateUnRegister(ChangeColor);
  455. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateUnRegister(KeyDown);
  456. API_GSXR_Module_InputSystem.GSXR_KeyUpDelegateUnRegister(KeyUp);
  457. API_GSXR_Module_InputSystem.GSXR_KeyLongDelegateUnRegister(KeyLong);
  458. API_GSXR_Module_InputSystem.GSXR_KeyTouchEnterDelegateUnRegister(KeyTouchEnter);
  459. API_GSXR_Module_InputSystem.GSXR_KeyTouchExitDelegateUnRegister(KeyTouchExit);
  460. API_GSXR_Slam.plugin.GSXR_Set_ControllerHallRangeCallback(null);
  461. isOne = true;
  462. }
  463. }