ControllerVisualManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. /// <summary> Manager for controller visuals. </summary>
  15. [DisallowMultipleComponent]
  16. public class ControllerVisualManager : MonoBehaviour
  17. {
  18. /// <summary> The states. </summary>
  19. private ControllerState[] m_States;
  20. /// <summary> The controller visuals. </summary>
  21. private IControllerVisual[] m_ControllerVisuals;
  22. /// <summary> Executes the 'enable' action. </summary>
  23. private void OnEnable()
  24. {
  25. NRInput.OnControllerStatesUpdated += OnControllerStatesUpdated;
  26. }
  27. /// <summary> Executes the 'disable' action. </summary>
  28. private void OnDisable()
  29. {
  30. NRInput.OnControllerStatesUpdated -= OnControllerStatesUpdated;
  31. }
  32. /// <summary> Initializes this object. </summary>
  33. /// <param name="states"> The states.</param>
  34. public void Init(ControllerState[] states)
  35. {
  36. this.m_States = states;
  37. m_ControllerVisuals = new IControllerVisual[states.Length];
  38. }
  39. /// <summary> Change controller visual. </summary>
  40. /// <param name="index"> Zero-based index of the.</param>
  41. /// <param name="visualType"> Type of the visual.</param>
  42. public void ChangeControllerVisual(int index, ControllerVisualType visualType)
  43. {
  44. if (m_ControllerVisuals[index] != null)
  45. {
  46. DestroyVisual(index);
  47. }
  48. CreateControllerVisual(index, visualType);
  49. }
  50. /// <summary> Executes the 'controller states updated' action. </summary>
  51. private void OnControllerStatesUpdated()
  52. {
  53. UpdateAllVisuals();
  54. }
  55. /// <summary> Updates all visuals. </summary>
  56. private void UpdateAllVisuals()
  57. {
  58. int availableCount = NRInput.GetAvailableControllersCount();
  59. if (availableCount > 1)
  60. {
  61. UpdateVisual(0, m_States[0]);
  62. UpdateVisual(1, m_States[1]);
  63. }
  64. else
  65. {
  66. int activeVisual = NRInput.DomainHand == ControllerHandEnum.Right ? 0 : 1;
  67. int deactiveVisual = NRInput.DomainHand == ControllerHandEnum.Right ? 1 : 0;
  68. UpdateVisual(activeVisual, m_States[0]);
  69. DestroyVisual(deactiveVisual);
  70. }
  71. }
  72. /// <summary> Updates the visual. </summary>
  73. /// <param name="index"> Zero-based index of the.</param>
  74. /// <param name="state"> The state.</param>
  75. private void UpdateVisual(int index, ControllerState state)
  76. {
  77. ControllerVisualType visualType = ControllerVisualFactory.GetDefaultVisualType(state.controllerType);
  78. if (m_ControllerVisuals[index] != null && visualType == ControllerVisualType.None)
  79. {
  80. DestroyVisual(index);
  81. }
  82. else if (m_ControllerVisuals[index] == null && visualType != ControllerVisualType.None)
  83. {
  84. CreateControllerVisual(index, visualType);
  85. }
  86. if (m_ControllerVisuals[index] != null)
  87. {
  88. m_ControllerVisuals[index].SetActive(NRInput.ControllerVisualActive);
  89. m_ControllerVisuals[index].UpdateVisual(state);
  90. }
  91. }
  92. /// <summary> Creates controller visual. </summary>
  93. /// <param name="index"> Zero-based index of the.</param>
  94. /// <param name="visualType"> Type of the visual.</param>
  95. private void CreateControllerVisual(int index, ControllerVisualType visualType)
  96. {
  97. GameObject visualGo = ControllerVisualFactory.CreateControllerVisualObject(visualType);
  98. if (visualGo == null)
  99. return;
  100. m_ControllerVisuals[index] = visualGo.GetComponent<IControllerVisual>();
  101. if (m_ControllerVisuals[index] != null)
  102. {
  103. ControllerAnchorEnum ancherEnum = (index == 0 ? ControllerAnchorEnum.RightModelAnchor : ControllerAnchorEnum.LeftModelAnchor);
  104. visualGo.transform.parent = NRInput.AnchorsHelper.GetAnchor(ancherEnum);
  105. visualGo.transform.localPosition = Vector3.zero;
  106. visualGo.transform.localRotation = Quaternion.identity;
  107. visualGo.transform.localScale = Vector3.one;
  108. }
  109. else
  110. {
  111. NRDebugger.Error("The ControllerVisual prefab:" + visualGo.name + " does not contain IControllerVisual interface");
  112. Destroy(visualGo);
  113. }
  114. }
  115. /// <summary> Destroys the visual described by index. </summary>
  116. /// <param name="index"> Zero-based index of the.</param>
  117. private void DestroyVisual(int index)
  118. {
  119. if (m_ControllerVisuals[index] != null)
  120. {
  121. m_ControllerVisuals[index].DestroySelf();
  122. m_ControllerVisuals[index] = null;
  123. }
  124. }
  125. }
  126. }