NxrHeadControl.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. namespace Nxr.Internal
  7. {
  8. public class NxrHeadControl : MonoBehaviour
  9. {
  10. [NonSerialized]
  11. public static GameObject eventGameObject;
  12. [NonSerialized]
  13. public static BaseEventData baseEventData;
  14. private float duration = 2f;
  15. [SerializeField] private Image selection;
  16. private Coroutine fillcoroutine;
  17. private bool isselect;
  18. private bool isfilled;
  19. public float Duration { get { return duration; } }
  20. public GameObject pointImage;
  21. private void Start()
  22. {
  23. selection.fillAmount = 0f;
  24. duration = NxrViewer.Instance.Duration;
  25. pointImage = GetComponentInChildren<Image>().gameObject.transform.GetChild(0).gameObject;
  26. }
  27. public void Show()
  28. {
  29. pointImage.SetActive(false);
  30. selection.gameObject.SetActive(true);
  31. isselect = true;
  32. }
  33. public void Hide()
  34. {
  35. pointImage.SetActive(true);
  36. selection.gameObject.SetActive(false);
  37. isselect = false;
  38. // This effectively resets the radial for when it's shown again.
  39. selection.fillAmount = 0f;
  40. }
  41. private IEnumerator FillSelectionRadial()
  42. {
  43. isfilled = false;
  44. float timer = 0f;
  45. selection.fillAmount = 0f;
  46. while (timer < duration)
  47. {
  48. selection.fillAmount = timer / duration;
  49. timer += Time.deltaTime;
  50. yield return null;
  51. }
  52. selection.fillAmount = 1f;
  53. isselect = false;
  54. isfilled = true;
  55. pointImage.SetActive(true);
  56. if (eventGameObject != null)
  57. {
  58. ExecuteEvents.ExecuteHierarchy(eventGameObject, baseEventData, ExecuteEvents.pointerClickHandler);
  59. }
  60. }
  61. public IEnumerator WaitForSelectionRadialToFill()
  62. {
  63. isfilled = false;
  64. Show();
  65. while (!isfilled)
  66. {
  67. yield return null;
  68. }
  69. Hide();
  70. }
  71. public void HandleGazeStay()
  72. {
  73. }
  74. public void HandleDown()
  75. {
  76. if (isselect)
  77. {
  78. fillcoroutine = StartCoroutine(FillSelectionRadial());
  79. }
  80. }
  81. public void HandleUp()
  82. {
  83. if (fillcoroutine != null)
  84. StopCoroutine(fillcoroutine);
  85. selection.fillAmount = 0f;
  86. pointImage.SetActive(true);
  87. }
  88. }
  89. }