123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using EZXR.Glass.Core;
- using EZXR.Glass.Inputs;
- using UnityEngine;
- namespace EZXR.Glass.Inputs
- {
- public enum HandleKeyEvent
- {
- Idle,
- Down,
- Up,
-
-
-
-
- PrePressed,
- Pressing
- }
- [ScriptExecutionOrder(-49)]
- public class HandleControllerManager : MonoBehaviour
- {
- #region singleton
- private static HandleControllerManager instance;
- public static HandleControllerManager Instance
- {
- get
- {
- return instance;
- }
- }
- #endregion
-
-
-
- public static ControllerInfo leftHand;
-
-
-
- public static ControllerInfo rightHand;
-
- public Transform leftController;
- public Transform rightController;
- private List<Action<HandType, bool>> bindingActions = new List<Action<HandType, bool>>();
- private List<Action<HandType, bool>> connectedActions = new List<Action<HandType, bool>>();
- private List<Action<HandType, float>> powerChangedActions = new List<Action<HandType, float>>();
- private List<Action<HandType, bool>> holdChangedActions = new List<Action<HandType, bool>>();
- private List<Action<HandType, bool>> silenceChangedActions = new List<Action<HandType, bool>>();
- private List<Action<HandType, bool>> buttonChangedActions = new List<Action<HandType, bool>>();
- private List<Action<HandType, int>> trackingChangedActions = new List<Action<HandType, int>>();
-
-
-
-
-
-
- public delegate bool Delegate_SetRayDataByExternal(ControllerInfo controllerInfo, ref Vector3 value);
- public static Delegate_SetRayDataByExternal SetRayDirByExternal;
- public static Delegate_SetRayDataByExternal SetRayStartPointByExternal;
- private void Awake()
- {
- instance = this;
- leftHand = leftController.GetComponent<ControllerInfo>();
- rightHand = rightController.GetComponent<ControllerInfo>();
- }
- private void Start()
- {
- HandleControllerSession.Instance.InitRegistration(OnBindingEventCallback, OnConnectEventCallback,
- OnButtonEventCallback, OnAxis2DEventCallback,
- OnHoldEventCallback, OnSilenceEventCallback,
- OnTrackingStateChangedCallback);
- if (leftHand != null)
- {
-
- leftHand.Init(HandType.Left);
- }
- if (rightHand != null)
- {
-
- rightHand.Init(HandType.Right);
- }
- StartCoroutine(WaitForTimeout());
- }
- private void Update()
- {
-
-
- if (leftHand.Exist || rightHand.Exist)
- HandleControllerSession.Instance.UpdateHandlePose();
- if (leftController != null && leftHand.Exist)
- leftController.SetLocalPositionAndRotation(HandleControllerSession.controllerPose_left.position, HandleControllerSession.controllerPose_left.rotation);
- if (rightController != null && rightHand.Exist)
- rightController.SetLocalPositionAndRotation(HandleControllerSession.controllerPose_right.position, HandleControllerSession.controllerPose_right.rotation);
- }
-
-
-
-
-
-
- public void InitRegistration(Action<HandType, bool> bindingEventCallback, Action<HandType, bool> connectedEventCallback, Action<HandType, float> powerChangedCallback)
- {
- InitRegistration(bindingEventCallback, connectedEventCallback, powerChangedCallback, null, null, null);
- }
-
-
-
-
-
-
-
- public void InitRegistration(Action<HandType, bool> bindingEventCallback, Action<HandType, bool> connectedEventCallback, Action<HandType, float> powerChangedCallback, Action<HandType, int> trackingStateChangedCallback)
- {
- InitRegistration(bindingEventCallback, connectedEventCallback, powerChangedCallback, null, null, null);
- trackingChangedActions.Add(trackingStateChangedCallback);
- }
-
-
-
-
-
-
-
-
-
- public void InitRegistration(Action<HandType, bool> bindingEventCallback,
- Action<HandType, bool> connectedEventCallback,
- Action<HandType, float> powerChangedCallback,
- Action<HandType, bool> buttonChangedCallback,
- Action<HandType, bool> holdChangedCallback,
- Action<HandType, bool> silenceChangedCallback)
- {
- bindingActions.Add(bindingEventCallback);
- connectedActions.Add(connectedEventCallback);
- powerChangedActions.Add(powerChangedCallback);
- silenceChangedActions.Add(silenceChangedCallback);
- holdChangedActions.Add(holdChangedCallback);
- buttonChangedActions.Add(buttonChangedCallback);
- }
-
-
-
- public bool BindHandle(HandType handType)
- {
- return HandleControllerSession.Instance.BindHandle((int)handType);
- }
-
-
-
- public bool UnbindHandle(HandType handType)
- {
- return HandleControllerSession.Instance.UnbindHandle((int)handType);
- }
-
-
-
- public bool VibrateHandle(HandType handType, int level, int time)
- {
- return HandleControllerSession.Instance.VibrateHandle((int)handType, level, time);
- }
-
-
-
-
-
- public bool GetBindState(HandType handType)
- {
- return HandleControllerSession.Instance.GetBindState(handType);
- }
-
-
-
- public bool GetConnectState(HandType handType)
- {
- return HandleControllerSession.Instance.GetConnectState((int)handType);
- }
-
-
-
- public float GetPowerStats(HandType handType)
- {
- return HandleControllerSession.Instance.GetPowerStats((int)handType);
- }
-
-
-
- public bool GetButtonDown(HandType handType, HandleKeyCode keyCode)
- {
- if (handType == HandType.Left)
- return leftHand.GetButtonDown(keyCode);
- else
- return rightHand.GetButtonDown(keyCode);
- }
-
-
-
- public bool GetButtonUp(HandType handType, HandleKeyCode keyCode)
- {
- if (handType == HandType.Left)
- return leftHand.GetButtonUp(keyCode);
- else
- return rightHand.GetButtonUp(keyCode);
- }
-
-
-
- public bool GetButton(HandType handType, HandleKeyCode keyCode)
- {
- if (handType == HandType.Left)
- return leftHand.GetButton(keyCode);
- else
- return rightHand.GetButton(keyCode);
- }
-
-
-
-
-
- public Vector2 GetAxis2D(HandType handType)
- {
- if (handType == HandType.Left)
- return leftHand.GetAxis2D();
- else
- return rightHand.GetAxis2D();
- }
-
- public void SetActive(bool value)
- {
-
-
-
-
-
-
- gameObject.SetActive(value);
- if (value)
- {
-
-
-
-
- StartCoroutine(leftHand.CheckStatus());
- StartCoroutine(rightHand.CheckStatus());
- }
- }
- private IEnumerator SetActive_WaitForSeconds(HandType handType, bool value)
- {
- yield return new WaitForSeconds(1);
- (handType == HandType.Left ? leftController : rightController).gameObject.SetActive(value);
- }
- private void OnBindingEventCallback(HandType handType, bool status)
- {
- if (handType == HandType.Left)
- {
- leftHand.UpdateBindingState(status, _status =>
- {
- bindingActions.ForEach(action => { if (action != null) action(HandType.Left, _status); });
- });
- }
- else
- {
- rightHand.UpdateBindingState(status, _status =>
- {
- bindingActions.ForEach(action => { if (action != null) action(HandType.Right, _status); });
- });
- }
- }
- private void OnConnectEventCallback(HandType handType, bool connected)
- {
- if (handType == HandType.Left)
- {
- leftHand.UpdateConnectedState(connected, _connected =>
- {
- connectedActions.ForEach(action => { if (action != null) action(HandType.Left, _connected); });
- });
- }
- else
- {
- rightHand.UpdateConnectedState(connected, _connected =>
- {
- connectedActions.ForEach(action => { if (action != null) action(HandType.Right, _connected); });
- });
- }
- }
- private void OnPowerStatsChangedCallback(HandType handType, float power)
- {
- if (handType == HandType.Left)
- {
- leftHand.UpdatePowerStats(power, _power =>
- {
- powerChangedActions.ForEach(action => { if (action != null) action(HandType.Left, _power); });
- });
- }
- else
- {
- rightHand.UpdatePowerStats(power, _power =>
- {
- powerChangedActions.ForEach(action => { if (action != null) action(HandType.Right, _power); });
- });
- }
- }
- private void OnButtonEventCallback(HandType handType, HandleKeyCode keycode, bool pressed)
- {
- if (handType == HandType.Left)
- {
- leftHand.UpdateButtonState(keycode, pressed);
- buttonChangedActions.ForEach(action => { if (action != null) action(HandType.Left, pressed); });
- }
- else
- {
- rightHand.UpdateButtonState(keycode, pressed);
- buttonChangedActions.ForEach(action => { if (action != null) action(HandType.Right, pressed); });
- }
- }
- private void OnAxis2DEventCallback(HandType handType, Vector2 coord)
- {
- if (handType == HandType.Left)
- leftHand.UpdateAxis2D(coord);
- else
- rightHand.UpdateAxis2D(coord);
- }
- private void OnHoldEventCallback(HandType handType, HandleKeyCode keycode, bool isHeld)
- {
- if (handType == HandType.Left)
- {
- leftHand.UpdateHoldState(isHeld, _isHeld =>
- {
- holdChangedActions.ForEach(action => { if (action != null) action(HandType.Left, _isHeld); });
- });
- }
- else
- {
- rightHand.UpdateHoldState(isHeld, _isHeld =>
- {
- holdChangedActions.ForEach(action => { if (action != null) action(HandType.Right, _isHeld); });
- });
- }
- }
- private void OnSilenceEventCallback(HandType handType, bool silent)
- {
- if (handType == HandType.Left)
- {
- leftHand.UpdateSilenceState(silent, _silent =>
- {
- silenceChangedActions.ForEach(action => { if (action != null) action(HandType.Left, _silent); });
- });
- }
- else
- {
- rightHand.UpdateSilenceState(silent, _silent =>
- {
- silenceChangedActions.ForEach(action => { if (action != null) action(HandType.Right, _silent); });
- });
- }
- }
- private void OnTrackingStateChangedCallback(HandType handType, int state)
- {
- if (handType == HandType.Left)
- {
- trackingChangedActions.ForEach(action => { if (action != null) action(HandType.Left, state); });
- }
- else
- {
- trackingChangedActions.ForEach(action => { if (action != null) action(HandType.Right, state); });
- }
- }
-
-
-
-
- private IEnumerator WaitForTimeout()
- {
- yield return new WaitForSeconds(3);
- if (leftHand.Exist) OnPowerStatsChangedCallback(HandType.Left, GetPowerStats(HandType.Left));
- if (rightHand.Exist) OnPowerStatsChangedCallback(HandType.Right, GetPowerStats(HandType.Right));
- while (true)
- {
- yield return new WaitForSeconds(300);
- if (leftHand.Exist) OnPowerStatsChangedCallback(HandType.Left, GetPowerStats(HandType.Left));
- if (rightHand.Exist) OnPowerStatsChangedCallback(HandType.Right, GetPowerStats(HandType.Right));
- }
- }
- }
- }
|