InputSystem.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. using System;
  2. using System.Collections;
  3. using EZXR.Glass.Core;
  4. using EZXR.Glass.SixDof;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. namespace EZXR.Glass.Inputs
  8. {
  9. [ScriptExecutionOrder(-47)]
  10. public class InputSystem : MonoBehaviour
  11. {
  12. #region singleton
  13. private static InputSystem instance;
  14. public static InputSystem Instance
  15. {
  16. get
  17. {
  18. return instance;
  19. }
  20. }
  21. #endregion
  22. [SerializeField]
  23. private ControllerType m_ControllerType;
  24. [Obsolete("Please use 'CurrentActiveControllerType' instead!")]
  25. public ControllerType controllerType
  26. {
  27. get
  28. {
  29. return m_ControllerType;
  30. }
  31. }
  32. /// <summary>
  33. /// 当前激活的控制器类型
  34. /// </summary>
  35. public static ControllerType CurrentActiveControllerType
  36. {
  37. get
  38. {
  39. return Instance.m_ControllerType;
  40. }
  41. }
  42. /// <summary>
  43. /// 开启动态模式时,支持热切换
  44. /// </summary>
  45. [SerializeField]
  46. private bool dynamicMode = true;
  47. /// <summary>
  48. /// InputSystem当前是否被启用
  49. /// </summary>
  50. private bool isActive = true;
  51. /// <summary>
  52. /// 记录了左手柄的所有信息
  53. /// </summary>
  54. public static InputInfoBase leftHand;
  55. /// <summary>
  56. /// 记录了右手柄的所有信息
  57. /// </summary>
  58. public static InputInfoBase rightHand;
  59. /// <summary>
  60. /// 按键按住或握持住时,手柄保持激活
  61. /// </summary>
  62. private bool keepHandlesActive = false;
  63. private bool isInited = false;
  64. private bool isPaused = false;
  65. private bool isGlassStandby = false;
  66. private bool isHandlesStandby = false;
  67. public void SetActive(bool value)
  68. {
  69. isActive = value;
  70. switch (CurrentActiveControllerType)
  71. {
  72. case ControllerType.HandTracking:
  73. ARHandManager.Instance.gameObject.SetActive(value);
  74. break;
  75. case ControllerType.Controllers:
  76. HandleControllerManager.Instance.gameObject.SetActive(value);
  77. break;
  78. }
  79. }
  80. private void Awake()
  81. {
  82. instance = this;
  83. //TODO 初始化交互:头控<手势<手柄
  84. if (dynamicMode) m_ControllerType = ControllerType.Helmet;
  85. Debug.Log($"ControllerManager, init dynamicMode: {dynamicMode}, controllerType: {CurrentActiveControllerType}");
  86. if (dynamicMode)
  87. {
  88. m_ControllerType = ControllerType.HandTracking;
  89. leftHand = ARHandManager.leftHand;
  90. rightHand = ARHandManager.rightHand;
  91. }
  92. }
  93. private IEnumerator Start()
  94. {
  95. //Debug.Log("InputSystem--> KeyBoard");
  96. //if (KeyBoard.Instance == null)
  97. //{
  98. // Instantiate(ResourcesManager.Load<GameObject>("KeyBoard"));
  99. //}
  100. HandleControllerManager.Instance.InitRegistration(null, OnHandleConnectChanged, null, OnHandleButtonChanged, OnHandleHoldChanged, OnHandleSilenceChanged);
  101. HandleControllerSession.Instance.InitServerReconnectRegistration(OnServerReconnected);
  102. #if SYSTEMUI
  103. SDKEventSystem.OnSystemEnterStandbyMode += OnServerStandby;
  104. #else
  105. OSEventSystem.OnSystemEnterStandbyMode += OnServerStandby;
  106. #endif
  107. if (dynamicMode)
  108. {
  109. //StartCoroutine(ChangeToGestureController());
  110. //StartCoroutine(CheckStatus());
  111. m_ControllerType = ControllerType.HandTracking;
  112. leftHand = ARHandManager.leftHand;
  113. rightHand = ARHandManager.rightHand;
  114. HandleControllerManager.Instance.SetActive(false);
  115. ARHandManager.Instance.SetActive(true & isActive);
  116. Debug.Log($"ControllerManager, init Change to GestureController ({Time.frameCount})");
  117. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
  118. }
  119. else
  120. {
  121. if (CurrentActiveControllerType == ControllerType.HandTracking)
  122. {
  123. leftHand = ARHandManager.leftHand;
  124. rightHand = ARHandManager.rightHand;
  125. HandleControllerManager.Instance.SetActive(false);
  126. if (!isPaused)
  127. {
  128. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
  129. Debug.Log($"ControllerManager, init SendIntent ControllerType HandTracking ({Time.frameCount})");
  130. }
  131. }
  132. else
  133. {
  134. leftHand = HandleControllerManager.leftHand;
  135. rightHand = HandleControllerManager.rightHand;
  136. ARHandManager.Instance.SetActive(false);
  137. if (!isPaused)
  138. {
  139. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
  140. Debug.Log($"ControllerManager, init SendIntent ControllerType Controllers ({Time.frameCount})");
  141. }
  142. }
  143. }
  144. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  145. HandleControllerSession.Instance.ChangeControllerType((int)CurrentActiveControllerType);
  146. isInited = true;
  147. }
  148. private void Update()
  149. {
  150. // 不受HandleControllerSession未激活影响
  151. HandleControllerSession.Instance?.UpdateHandleCallback();
  152. if (isGlassStandby)
  153. {
  154. isGlassStandby = false;
  155. if (dynamicMode || (!dynamicMode && CurrentActiveControllerType == ControllerType.HandTracking))
  156. {
  157. StartCoroutine(ChangeToGestureController());
  158. }
  159. Debug.Log($"ControllerManager, Awake after standby, isGlassStandby = {isGlassStandby} ({Time.frameCount})");
  160. }
  161. if (isHandlesStandby)
  162. {
  163. isHandlesStandby = false;
  164. if (dynamicMode) StartCoroutine(ChangeToGestureController());
  165. Debug.Log($"ControllerManager, Awake after Handles Standby, controllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
  166. }
  167. //if (!isPaused && Input.GetKeyDown(KeyCode.I))
  168. // InitDynamicMode(ControllerType.Controllers);
  169. //if (!isPaused && Input.GetKeyDown(KeyCode.E))
  170. // EnableDynamicMode();
  171. //if (!isPaused && Input.GetKeyDown(KeyCode.H))
  172. // DisableDynamicMode(ControllerType.HandTracking);
  173. //if (!isPaused && Input.GetKeyDown(KeyCode.C))
  174. // DisableDynamicMode(ControllerType.Controllers);
  175. }
  176. private void OnApplicationPause(bool pause)
  177. {
  178. if (!pause)
  179. {
  180. if (isInited)
  181. {
  182. if (dynamicMode)
  183. {
  184. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
  185. //手柄未连接状态下(回调失效),Resume时强制切到手势模式
  186. if (!HandleControllerManager.Instance.GetConnectState(HandType.Left) &&
  187. !HandleControllerManager.Instance.GetConnectState(HandType.Right))
  188. {
  189. isHandlesStandby = true;
  190. Debug.Log($"ControllerManager, OnApplicationResume(Handles Standby) ChangeControllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
  191. }
  192. }
  193. else
  194. {
  195. switch (m_ControllerType)
  196. {
  197. case ControllerType.Helmet:
  198. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Head");
  199. break;
  200. case ControllerType.HandTracking:
  201. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
  202. break;
  203. case ControllerType.Controllers:
  204. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
  205. break;
  206. }
  207. }
  208. HandleControllerSession.Instance.ChangeControllerType((int)CurrentActiveControllerType);
  209. Debug.Log($"ControllerManager, OnApplicationResume(Inited) ChangeControllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
  210. }
  211. else
  212. {
  213. //在上一应用时进入了待机模式(手柄算法会重置),
  214. //再Resume该应用时应通知Server同步到当前ControllerType
  215. StartCoroutine(CheckStatusWhenResume());
  216. }
  217. }
  218. isPaused = pause;
  219. Debug.Log($"ControllerManager, OnApplicationPause isPaused: {isPaused}, dynamicMode: {dynamicMode} ({Time.frameCount})");
  220. }
  221. // for fix
  222. private IEnumerator CheckStatus()
  223. {
  224. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  225. Debug.Log($"ControllerManager, rightHand check ConnectState: {HandleControllerManager.Instance.GetConnectState(HandType.Right)}");
  226. if (HandleControllerManager.Instance.GetConnectState(HandType.Left) ||
  227. HandleControllerManager.Instance.GetConnectState(HandType.Right))
  228. {
  229. //StartCoroutineChangeToHandleController());
  230. }
  231. }
  232. private IEnumerator CheckStatusWhenResume()
  233. {
  234. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  235. HandleControllerSession.Instance.ChangeControllerType((int)CurrentActiveControllerType);
  236. Debug.Log($"ControllerManager, OnApplicationResume(UnInit) ChangeControllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
  237. }
  238. /// <summary>
  239. /// 动态模式初始化时,指定控制器类型
  240. /// </summary>
  241. public void InitDynamicMode(ControllerType type)
  242. {
  243. StartCoroutine(InitDynamicMode_Coroutine(type));
  244. }
  245. /// <summary>
  246. /// 开启动态模式,自动热切换
  247. /// </summary>
  248. public void EnableDynamicMode()
  249. {
  250. StartCoroutine(EnableDynamicMode_Coroutine());
  251. }
  252. /// <summary>
  253. /// 禁用动态模式时,指定控制器类型
  254. /// </summary>
  255. /// <param name="type"></param>
  256. public void DisableDynamicMode(ControllerType type)
  257. {
  258. StartCoroutine(DisableDynamicMode_Coroutine(type));
  259. }
  260. private IEnumerator InitDynamicMode_Coroutine(ControllerType type)
  261. {
  262. yield return new WaitUntil(() => isInited);
  263. if (!dynamicMode) yield break;
  264. if (type == ControllerType.HandTracking)
  265. {
  266. if (isPaused) yield break;
  267. if (m_ControllerType == ControllerType.HandTracking) yield break;
  268. m_ControllerType = ControllerType.HandTracking;
  269. leftHand = ARHandManager.leftHand;
  270. rightHand = ARHandManager.rightHand;
  271. HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  272. HandleControllerManager.Instance.SetActive(false);
  273. ARHandManager.Instance.SetActive(true & isActive);
  274. Debug.Log($"ControllerManager, InitDynamicMode Change to GestureController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
  275. }
  276. else if (type == ControllerType.Controllers)
  277. {
  278. //手柄未连接时从纯手柄应用切回launcher,强制置为手势
  279. if (!HandleControllerManager.Instance.GetConnectState(HandType.Left) && !HandleControllerManager.Instance.GetConnectState(HandType.Right))
  280. {
  281. if (m_ControllerType == ControllerType.HandTracking) yield break;
  282. m_ControllerType = ControllerType.HandTracking;
  283. leftHand = ARHandManager.leftHand;
  284. rightHand = ARHandManager.rightHand;
  285. HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  286. HandleControllerManager.Instance.SetActive(false);
  287. ARHandManager.Instance.SetActive(true & isActive);
  288. Debug.Log($"ControllerManager, OnApplicationResume Change to GestureController ({Time.frameCount})");
  289. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
  290. }
  291. else
  292. {
  293. if (isPaused) yield break;
  294. if (m_ControllerType == ControllerType.Controllers) yield break;
  295. m_ControllerType = ControllerType.Controllers;
  296. leftHand = HandleControllerManager.leftHand;
  297. rightHand = HandleControllerManager.rightHand;
  298. HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  299. ARHandManager.Instance.SetActive(false);
  300. HandleControllerManager.Instance.SetActive(true & isActive);
  301. Debug.Log($"ControllerManager, InitDynamicMode Change to HandleController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
  302. }
  303. }
  304. }
  305. private IEnumerator EnableDynamicMode_Coroutine()
  306. {
  307. yield return new WaitUntil(() => isInited);
  308. dynamicMode = true;
  309. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
  310. }
  311. private IEnumerator DisableDynamicMode_Coroutine(ControllerType type)
  312. {
  313. yield return new WaitUntil(() => isInited);
  314. dynamicMode = false;
  315. switch (type)
  316. {
  317. case ControllerType.Helmet:
  318. StartCoroutine(ChangeToHelmetController());
  319. break;
  320. case ControllerType.HandTracking:
  321. StartCoroutine(ChangeToGestureController());
  322. break;
  323. case ControllerType.Controllers:
  324. StartCoroutine(ChangeToHandleController());
  325. break;
  326. }
  327. }
  328. /// <summary>
  329. /// 手柄连接状态改变
  330. /// </summary>
  331. /// <param name="handType"></param>
  332. /// <param name="connected"></param>
  333. private void OnHandleConnectChanged(HandType handType, bool connected)
  334. {
  335. if (!dynamicMode) return;
  336. if (isPaused) return;
  337. Debug.Log($"ControllerManager, {handType} OnHandleConnectChanged, connected = {connected} ({Time.frameCount})");
  338. if (connected)
  339. {
  340. StartCoroutine(ChangeToHandleController());
  341. }
  342. else
  343. {
  344. if (!HandleControllerManager.Instance.GetConnectState(HandType.Left) &&
  345. !HandleControllerManager.Instance.GetConnectState(HandType.Right))
  346. {
  347. keepHandlesActive = false;
  348. StartCoroutine(ChangeToGestureController());
  349. }
  350. }
  351. }
  352. /// <summary>
  353. /// 手柄按键状态改变
  354. /// </summary>
  355. /// <param name="handType"></param>
  356. /// <param name="pressed"></param>
  357. private void OnHandleButtonChanged(HandType handType, bool pressed)
  358. {
  359. if (!dynamicMode) return;
  360. if (isPaused) return;
  361. //Debug.Log($"ControllerManager, {handType} OnHandleButtonChanged, pressed = {pressed} ({Time.frameCount})");
  362. StartCoroutine(ChangeToHandleController());
  363. //按键按住过程中,保持手柄功能激活
  364. keepHandlesActive = pressed;
  365. }
  366. /// <summary>
  367. /// 手柄握持状态改变
  368. /// </summary>
  369. /// <param name="handType"></param>
  370. /// <param name="isHeld"></param>
  371. private void OnHandleHoldChanged(HandType handType, bool isHeld)
  372. {
  373. if (!dynamicMode) return;
  374. if (isPaused) return;
  375. //Debug.Log($"ControllerManager, {handType} OnHandleHoldChanged, isHeld = {isHeld} ({Time.frameCount})");
  376. StartCoroutine(ChangeToHandleController());
  377. //手柄握持过程中,保持手柄功能激活
  378. keepHandlesActive = isHeld;
  379. }
  380. /// <summary>
  381. /// 手柄静置状态改变
  382. /// </summary>
  383. /// <param name="handType"></param>
  384. /// <param name="silent"></param>
  385. private void OnHandleSilenceChanged(HandType handType, bool silent)
  386. {
  387. if (!dynamicMode) return;
  388. if (isPaused) return;
  389. //Debug.Log($"ControllerManager, {handType} OnHandleSilenceChanged, silent = {silent} ({Time.frameCount})");
  390. if (silent && !keepHandlesActive)
  391. {
  392. StartCoroutine(ChangeToGestureController());
  393. }
  394. }
  395. /// <summary>
  396. /// (手柄算法)Server重连
  397. /// </summary>
  398. /// <param name="connected"></param>
  399. private void OnServerReconnected(bool connected)
  400. {
  401. Debug.Log($"ControllerManager, OnServerReconnected, connected = {connected} ({Time.frameCount})");
  402. if (connected && isInited)
  403. {
  404. //在Server重启后应通知Server同步到当前ControllerType
  405. StartCoroutine(CheckStatusWhenResume());
  406. }
  407. }
  408. /// <summary>
  409. /// (眼镜系统)进入待机状态
  410. /// </summary>
  411. private void OnServerStandby()
  412. {
  413. isGlassStandby = true;
  414. Debug.Log($"ControllerManager, OnServerSleep, isGlassStandby = {isGlassStandby} ({Time.frameCount})");
  415. }
  416. private IEnumerator ChangeToHelmetController()
  417. {
  418. if (isPaused) yield break;
  419. if (m_ControllerType == ControllerType.Helmet) yield break;
  420. //TODO
  421. if (dynamicMode)
  422. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
  423. else
  424. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Head");
  425. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  426. HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  427. }
  428. private IEnumerator ChangeToGestureController()
  429. {
  430. if (isPaused) yield break;
  431. if (m_ControllerType == ControllerType.HandTracking) yield break;
  432. m_ControllerType = ControllerType.HandTracking;
  433. leftHand = ARHandManager.leftHand;
  434. rightHand = ARHandManager.rightHand;
  435. //HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  436. HandleControllerManager.Instance.SetActive(false);
  437. ARHandManager.Instance.SetActive(true & isActive);
  438. Debug.Log($"ControllerManager, Change to GestureController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
  439. //Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
  440. if (dynamicMode)
  441. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
  442. else
  443. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
  444. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  445. HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  446. }
  447. private IEnumerator ChangeToHandleController()
  448. {
  449. if (isPaused) yield break;
  450. if (m_ControllerType == ControllerType.Controllers) yield break;
  451. m_ControllerType = ControllerType.Controllers;
  452. leftHand = HandleControllerManager.leftHand;
  453. rightHand = HandleControllerManager.rightHand;
  454. //HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  455. ARHandManager.Instance.SetActive(false);
  456. HandleControllerManager.Instance.SetActive(true & isActive);
  457. Debug.Log($"ControllerManager, Change to HandleController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
  458. //Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
  459. if (dynamicMode)
  460. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
  461. else
  462. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
  463. yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
  464. HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
  465. }
  466. }
  467. }