SvrInput.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //using SnapdragonVR;
  5. public class SvrInput : MonoBehaviour
  6. {
  7. private SvrManager svrManager = null;
  8. public SvrController PrimaryController = null;
  9. public SvrController SecondaryController = null;
  10. public static SvrInput Instance { get; private set; }
  11. public static SvrController Controller { get { return Instance.PrimaryController; } }
  12. public delegate void OnRecenterCallback();
  13. public OnRecenterCallback OnRecenterListener;
  14. public delegate IEnumerator OnBackCallback();
  15. public OnBackCallback OnBackListener;
  16. public Coroutine HandleBack { get; set; }
  17. public enum eInputStyle { Press, Tap, Hold, None }
  18. [Tooltip ("Android Back, Unity Escape, Controller Back Button")]
  19. public eInputStyle backInputType = eInputStyle.Press;
  20. private float backHoldTimer = 0;
  21. [Tooltip ("Android Touch, Unity Mouse, Controller Thumb Button")]
  22. public eInputStyle recenterInputType = eInputStyle.Hold;
  23. private float recenterHoldTimer = 0;
  24. [Tooltip("Button press duration in seconds to produce a Hold event")]
  25. public float ButtonHoldDuration = 1; // Seconds
  26. void Awake()
  27. {
  28. Instance = this;
  29. svrManager = SvrManager.Instance;
  30. Input.backButtonLeavesApp = false;
  31. OnRecenterListener = HandleRecenter;
  32. OnBackListener = HandleQuit;
  33. HandleBack = null;
  34. }
  35. public IEnumerator HandleQuit()
  36. {
  37. svrManager.SetOverlayFade(SvrManager.eFadeState.FadeOut);
  38. yield return new WaitUntil(() => svrManager.IsOverlayFading() == false);
  39. Application.Quit();
  40. HandleBack = null;
  41. }
  42. public void HandleRecenter()
  43. {
  44. SvrPlugin.Instance.RecenterTracking();
  45. Controller.Recenter();
  46. }
  47. void Update()
  48. {
  49. if (!SvrPlugin.Instance.IsInitialized())
  50. return;
  51. if (!SvrPlugin.Instance.IsRunning())
  52. return;
  53. if (!Input.backButtonLeavesApp &&
  54. CheckButton(Input.GetKey(KeyCode.Escape) || (PrimaryController && PrimaryController.GetButton(SvrController.svrControllerButton.Back)),
  55. backInputType, ref backHoldTimer))
  56. {
  57. Debug.Log("SvrInput - Quit");
  58. if (OnBackListener != null && HandleBack == null) HandleBack = StartCoroutine(OnBackListener());
  59. }
  60. if (CheckButton(Input.GetMouseButton(0) || Input.GetKey((KeyCode)10) || (PrimaryController && PrimaryController.GetButton(SvrController.svrControllerButton.PrimaryThumbstick)),
  61. recenterInputType, ref recenterHoldTimer))
  62. {
  63. Debug.Log("SvrInput - Recenter");
  64. if (OnRecenterListener != null) OnRecenterListener();
  65. }
  66. }
  67. public bool CheckButton(bool buttonValue, eInputStyle buttonInputType, ref float buttonHoldTimer)
  68. {
  69. if (buttonInputType == eInputStyle.None)
  70. {
  71. //Debug.Log("SvrInput - Ignored");
  72. return false;
  73. }
  74. if (buttonValue)
  75. {
  76. if (buttonInputType == eInputStyle.Press && buttonHoldTimer <= 0)
  77. {
  78. Debug.Log("SvrInput - Press");
  79. return true;
  80. }
  81. if (buttonHoldTimer < ButtonHoldDuration)
  82. {
  83. buttonHoldTimer += Time.deltaTime; // Increment timer
  84. if (buttonInputType == eInputStyle.Hold && buttonHoldTimer > ButtonHoldDuration)
  85. {
  86. Debug.Log("SvrInput - Hold");
  87. return true;
  88. }
  89. }
  90. }
  91. else if (buttonHoldTimer > 0)
  92. {
  93. if (buttonInputType == eInputStyle.Tap && buttonHoldTimer < ButtonHoldDuration)
  94. {
  95. buttonHoldTimer = 0; // Terminate timer
  96. Debug.Log("SvrInput - Tap");
  97. return true;
  98. }
  99. buttonHoldTimer = 0; // Terminate timer
  100. }
  101. return false;
  102. }
  103. //void OnGUI()
  104. //{
  105. // Event e = Event.current;
  106. // if (e.isKey)
  107. // Debug.Log("Detected key code: " + e.keyCode);
  108. //}
  109. }