123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- using System;
- using System.Collections;
- using EZXR.Glass.Core;
- using EZXR.Glass.SixDof;
- using UnityEngine;
- using UnityEngine.EventSystems;
- namespace EZXR.Glass.Inputs
- {
- [ScriptExecutionOrder(-47)]
- public class InputSystem : MonoBehaviour
- {
- #region singleton
- private static InputSystem instance;
- public static InputSystem Instance
- {
- get
- {
- return instance;
- }
- }
- #endregion
- [SerializeField]
- private ControllerType m_ControllerType;
- [Obsolete("Please use 'CurrentActiveControllerType' instead!")]
- public ControllerType controllerType
- {
- get
- {
- return m_ControllerType;
- }
- }
- /// <summary>
- /// 当前激活的控制器类型
- /// </summary>
- public static ControllerType CurrentActiveControllerType
- {
- get
- {
- return Instance.m_ControllerType;
- }
- }
- /// <summary>
- /// 开启动态模式时,支持热切换
- /// </summary>
- [SerializeField]
- private bool dynamicMode = true;
- /// <summary>
- /// InputSystem当前是否被启用
- /// </summary>
- private bool isActive = true;
- /// <summary>
- /// 记录了左手柄的所有信息
- /// </summary>
- public static InputInfoBase leftHand;
- /// <summary>
- /// 记录了右手柄的所有信息
- /// </summary>
- public static InputInfoBase rightHand;
- /// <summary>
- /// 按键按住或握持住时,手柄保持激活
- /// </summary>
- private bool keepHandlesActive = false;
- private bool isInited = false;
- private bool isPaused = false;
- private bool isGlassStandby = false;
- private bool isHandlesStandby = false;
- public void SetActive(bool value)
- {
- isActive = value;
- switch (CurrentActiveControllerType)
- {
- case ControllerType.HandTracking:
- ARHandManager.Instance.gameObject.SetActive(value);
- break;
- case ControllerType.Controllers:
- HandleControllerManager.Instance.gameObject.SetActive(value);
- break;
- }
- }
- private void Awake()
- {
- instance = this;
- //TODO 初始化交互:头控<手势<手柄
- if (dynamicMode) m_ControllerType = ControllerType.Helmet;
- Debug.Log($"ControllerManager, init dynamicMode: {dynamicMode}, controllerType: {CurrentActiveControllerType}");
- if (dynamicMode)
- {
- m_ControllerType = ControllerType.HandTracking;
- leftHand = ARHandManager.leftHand;
- rightHand = ARHandManager.rightHand;
- }
- }
- private IEnumerator Start()
- {
- //Debug.Log("InputSystem--> KeyBoard");
- //if (KeyBoard.Instance == null)
- //{
- // Instantiate(ResourcesManager.Load<GameObject>("KeyBoard"));
- //}
- HandleControllerManager.Instance.InitRegistration(null, OnHandleConnectChanged, null, OnHandleButtonChanged, OnHandleHoldChanged, OnHandleSilenceChanged);
- HandleControllerSession.Instance.InitServerReconnectRegistration(OnServerReconnected);
- #if SYSTEMUI
- SDKEventSystem.OnSystemEnterStandbyMode += OnServerStandby;
- #else
- OSEventSystem.OnSystemEnterStandbyMode += OnServerStandby;
- #endif
- if (dynamicMode)
- {
- //StartCoroutine(ChangeToGestureController());
- //StartCoroutine(CheckStatus());
- m_ControllerType = ControllerType.HandTracking;
- leftHand = ARHandManager.leftHand;
- rightHand = ARHandManager.rightHand;
- HandleControllerManager.Instance.SetActive(false);
- ARHandManager.Instance.SetActive(true & isActive);
- Debug.Log($"ControllerManager, init Change to GestureController ({Time.frameCount})");
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
- }
- else
- {
- if (CurrentActiveControllerType == ControllerType.HandTracking)
- {
- leftHand = ARHandManager.leftHand;
- rightHand = ARHandManager.rightHand;
- HandleControllerManager.Instance.SetActive(false);
- if (!isPaused)
- {
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
- Debug.Log($"ControllerManager, init SendIntent ControllerType HandTracking ({Time.frameCount})");
- }
- }
- else
- {
- leftHand = HandleControllerManager.leftHand;
- rightHand = HandleControllerManager.rightHand;
- ARHandManager.Instance.SetActive(false);
- if (!isPaused)
- {
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
- Debug.Log($"ControllerManager, init SendIntent ControllerType Controllers ({Time.frameCount})");
- }
- }
- }
- yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
- HandleControllerSession.Instance.ChangeControllerType((int)CurrentActiveControllerType);
- isInited = true;
- }
- private void Update()
- {
- // 不受HandleControllerSession未激活影响
- HandleControllerSession.Instance?.UpdateHandleCallback();
- if (isGlassStandby)
- {
- isGlassStandby = false;
- if (dynamicMode || (!dynamicMode && CurrentActiveControllerType == ControllerType.HandTracking))
- {
- StartCoroutine(ChangeToGestureController());
- }
- Debug.Log($"ControllerManager, Awake after standby, isGlassStandby = {isGlassStandby} ({Time.frameCount})");
- }
- if (isHandlesStandby)
- {
- isHandlesStandby = false;
- if (dynamicMode) StartCoroutine(ChangeToGestureController());
- Debug.Log($"ControllerManager, Awake after Handles Standby, controllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
- }
- //if (!isPaused && Input.GetKeyDown(KeyCode.I))
- // InitDynamicMode(ControllerType.Controllers);
- //if (!isPaused && Input.GetKeyDown(KeyCode.E))
- // EnableDynamicMode();
- //if (!isPaused && Input.GetKeyDown(KeyCode.H))
- // DisableDynamicMode(ControllerType.HandTracking);
- //if (!isPaused && Input.GetKeyDown(KeyCode.C))
- // DisableDynamicMode(ControllerType.Controllers);
- }
- private void OnApplicationPause(bool pause)
- {
- if (!pause)
- {
- if (isInited)
- {
- if (dynamicMode)
- {
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
- //手柄未连接状态下(回调失效),Resume时强制切到手势模式
- if (!HandleControllerManager.Instance.GetConnectState(HandType.Left) &&
- !HandleControllerManager.Instance.GetConnectState(HandType.Right))
- {
- isHandlesStandby = true;
- Debug.Log($"ControllerManager, OnApplicationResume(Handles Standby) ChangeControllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
- }
- }
- else
- {
- switch (m_ControllerType)
- {
- case ControllerType.Helmet:
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Head");
- break;
- case ControllerType.HandTracking:
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
- break;
- case ControllerType.Controllers:
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
- break;
- }
- }
- HandleControllerSession.Instance.ChangeControllerType((int)CurrentActiveControllerType);
- Debug.Log($"ControllerManager, OnApplicationResume(Inited) ChangeControllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
- }
- else
- {
- //在上一应用时进入了待机模式(手柄算法会重置),
- //再Resume该应用时应通知Server同步到当前ControllerType
- StartCoroutine(CheckStatusWhenResume());
- }
- }
- isPaused = pause;
- Debug.Log($"ControllerManager, OnApplicationPause isPaused: {isPaused}, dynamicMode: {dynamicMode} ({Time.frameCount})");
- }
- // for fix
- private IEnumerator CheckStatus()
- {
- yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
- Debug.Log($"ControllerManager, rightHand check ConnectState: {HandleControllerManager.Instance.GetConnectState(HandType.Right)}");
- if (HandleControllerManager.Instance.GetConnectState(HandType.Left) ||
- HandleControllerManager.Instance.GetConnectState(HandType.Right))
- {
- //StartCoroutineChangeToHandleController());
- }
- }
- private IEnumerator CheckStatusWhenResume()
- {
- yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
- HandleControllerSession.Instance.ChangeControllerType((int)CurrentActiveControllerType);
- Debug.Log($"ControllerManager, OnApplicationResume(UnInit) ChangeControllerType: {CurrentActiveControllerType}, dynamicMode: {dynamicMode} ({Time.frameCount})");
- }
- /// <summary>
- /// 动态模式初始化时,指定控制器类型
- /// </summary>
- public void InitDynamicMode(ControllerType type)
- {
- StartCoroutine(InitDynamicMode_Coroutine(type));
- }
- /// <summary>
- /// 开启动态模式,自动热切换
- /// </summary>
- public void EnableDynamicMode()
- {
- StartCoroutine(EnableDynamicMode_Coroutine());
- }
- /// <summary>
- /// 禁用动态模式时,指定控制器类型
- /// </summary>
- /// <param name="type"></param>
- public void DisableDynamicMode(ControllerType type)
- {
- StartCoroutine(DisableDynamicMode_Coroutine(type));
- }
- private IEnumerator InitDynamicMode_Coroutine(ControllerType type)
- {
- yield return new WaitUntil(() => isInited);
- if (!dynamicMode) yield break;
- if (type == ControllerType.HandTracking)
- {
- if (isPaused) yield break;
- if (m_ControllerType == ControllerType.HandTracking) yield break;
- m_ControllerType = ControllerType.HandTracking;
- leftHand = ARHandManager.leftHand;
- rightHand = ARHandManager.rightHand;
- HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- HandleControllerManager.Instance.SetActive(false);
- ARHandManager.Instance.SetActive(true & isActive);
- Debug.Log($"ControllerManager, InitDynamicMode Change to GestureController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
- }
- else if (type == ControllerType.Controllers)
- {
- //手柄未连接时从纯手柄应用切回launcher,强制置为手势
- if (!HandleControllerManager.Instance.GetConnectState(HandType.Left) && !HandleControllerManager.Instance.GetConnectState(HandType.Right))
- {
- if (m_ControllerType == ControllerType.HandTracking) yield break;
- m_ControllerType = ControllerType.HandTracking;
- leftHand = ARHandManager.leftHand;
- rightHand = ARHandManager.rightHand;
- HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- HandleControllerManager.Instance.SetActive(false);
- ARHandManager.Instance.SetActive(true & isActive);
- Debug.Log($"ControllerManager, OnApplicationResume Change to GestureController ({Time.frameCount})");
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
- }
- else
- {
- if (isPaused) yield break;
- if (m_ControllerType == ControllerType.Controllers) yield break;
- m_ControllerType = ControllerType.Controllers;
- leftHand = HandleControllerManager.leftHand;
- rightHand = HandleControllerManager.rightHand;
- HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- ARHandManager.Instance.SetActive(false);
- HandleControllerManager.Instance.SetActive(true & isActive);
- Debug.Log($"ControllerManager, InitDynamicMode Change to HandleController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
- }
- }
- }
- private IEnumerator EnableDynamicMode_Coroutine()
- {
- yield return new WaitUntil(() => isInited);
- dynamicMode = true;
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
- }
- private IEnumerator DisableDynamicMode_Coroutine(ControllerType type)
- {
- yield return new WaitUntil(() => isInited);
- dynamicMode = false;
- switch (type)
- {
- case ControllerType.Helmet:
- StartCoroutine(ChangeToHelmetController());
- break;
- case ControllerType.HandTracking:
- StartCoroutine(ChangeToGestureController());
- break;
- case ControllerType.Controllers:
- StartCoroutine(ChangeToHandleController());
- break;
- }
- }
- /// <summary>
- /// 手柄连接状态改变
- /// </summary>
- /// <param name="handType"></param>
- /// <param name="connected"></param>
- private void OnHandleConnectChanged(HandType handType, bool connected)
- {
- if (!dynamicMode) return;
- if (isPaused) return;
- Debug.Log($"ControllerManager, {handType} OnHandleConnectChanged, connected = {connected} ({Time.frameCount})");
- if (connected)
- {
- StartCoroutine(ChangeToHandleController());
- }
- else
- {
- if (!HandleControllerManager.Instance.GetConnectState(HandType.Left) &&
- !HandleControllerManager.Instance.GetConnectState(HandType.Right))
- {
- keepHandlesActive = false;
- StartCoroutine(ChangeToGestureController());
- }
- }
- }
- /// <summary>
- /// 手柄按键状态改变
- /// </summary>
- /// <param name="handType"></param>
- /// <param name="pressed"></param>
- private void OnHandleButtonChanged(HandType handType, bool pressed)
- {
- if (!dynamicMode) return;
- if (isPaused) return;
- //Debug.Log($"ControllerManager, {handType} OnHandleButtonChanged, pressed = {pressed} ({Time.frameCount})");
- StartCoroutine(ChangeToHandleController());
- //按键按住过程中,保持手柄功能激活
- keepHandlesActive = pressed;
- }
- /// <summary>
- /// 手柄握持状态改变
- /// </summary>
- /// <param name="handType"></param>
- /// <param name="isHeld"></param>
- private void OnHandleHoldChanged(HandType handType, bool isHeld)
- {
- if (!dynamicMode) return;
- if (isPaused) return;
- //Debug.Log($"ControllerManager, {handType} OnHandleHoldChanged, isHeld = {isHeld} ({Time.frameCount})");
- StartCoroutine(ChangeToHandleController());
- //手柄握持过程中,保持手柄功能激活
- keepHandlesActive = isHeld;
- }
- /// <summary>
- /// 手柄静置状态改变
- /// </summary>
- /// <param name="handType"></param>
- /// <param name="silent"></param>
- private void OnHandleSilenceChanged(HandType handType, bool silent)
- {
- if (!dynamicMode) return;
- if (isPaused) return;
- //Debug.Log($"ControllerManager, {handType} OnHandleSilenceChanged, silent = {silent} ({Time.frameCount})");
- if (silent && !keepHandlesActive)
- {
- StartCoroutine(ChangeToGestureController());
- }
- }
- /// <summary>
- /// (手柄算法)Server重连
- /// </summary>
- /// <param name="connected"></param>
- private void OnServerReconnected(bool connected)
- {
- Debug.Log($"ControllerManager, OnServerReconnected, connected = {connected} ({Time.frameCount})");
- if (connected && isInited)
- {
- //在Server重启后应通知Server同步到当前ControllerType
- StartCoroutine(CheckStatusWhenResume());
- }
- }
- /// <summary>
- /// (眼镜系统)进入待机状态
- /// </summary>
- private void OnServerStandby()
- {
- isGlassStandby = true;
- Debug.Log($"ControllerManager, OnServerSleep, isGlassStandby = {isGlassStandby} ({Time.frameCount})");
- }
- private IEnumerator ChangeToHelmetController()
- {
- if (isPaused) yield break;
- if (m_ControllerType == ControllerType.Helmet) yield break;
- //TODO
- if (dynamicMode)
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
- else
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Head");
- yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
- HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- }
- private IEnumerator ChangeToGestureController()
- {
- if (isPaused) yield break;
- if (m_ControllerType == ControllerType.HandTracking) yield break;
- m_ControllerType = ControllerType.HandTracking;
- leftHand = ARHandManager.leftHand;
- rightHand = ARHandManager.rightHand;
- //HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- HandleControllerManager.Instance.SetActive(false);
- ARHandManager.Instance.SetActive(true & isActive);
- Debug.Log($"ControllerManager, Change to GestureController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
- //Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
- if (dynamicMode)
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
- else
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Hand");
- yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
- HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- }
- private IEnumerator ChangeToHandleController()
- {
- if (isPaused) yield break;
- if (m_ControllerType == ControllerType.Controllers) yield break;
- m_ControllerType = ControllerType.Controllers;
- leftHand = HandleControllerManager.leftHand;
- rightHand = HandleControllerManager.rightHand;
- //HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- ARHandManager.Instance.SetActive(false);
- HandleControllerManager.Instance.SetActive(true & isActive);
- Debug.Log($"ControllerManager, Change to HandleController, leftHand = {leftHand.GetType().Name}, rightHand = {rightHand.GetType().Name} ({Time.frameCount})");
- //Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
- if (dynamicMode)
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Dynamic");
- else
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "ControllerType", Application.identifier + "," + "Controllers");
- yield return new WaitUntil(() => SessionManager.Instance != null && SessionManager.Instance.IsInited);
- HandleControllerSession.Instance.ChangeControllerType((int)m_ControllerType);
- }
- }
- }
|