MultiScreenController.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 UnityEngine;
  12. using UnityEngine.EventSystems;
  13. /// <summary> A controller for handling multi screens. </summary>
  14. public class MultiScreenController : MonoBehaviour, ISystemButtonStateProvider
  15. {
  16. private SystemButtonState m_SystemButtonState = new SystemButtonState();
  17. private ISystemButtonStateReceiver m_Receiver;
  18. /// <summary> System defined button. </summary>
  19. [SerializeField]
  20. private NRButton Trigger;
  21. /// <summary> The application. </summary>
  22. [SerializeField]
  23. private NRButton App;
  24. /// <summary> The home. </summary>
  25. [SerializeField]
  26. private NRButton Home;
  27. public void BindReceiver(ISystemButtonStateReceiver receiver)
  28. {
  29. this.m_Receiver = receiver;
  30. InitSystemButtonEvent();
  31. }
  32. /// <summary> Initializes the system button event. </summary>
  33. private void InitSystemButtonEvent()
  34. {
  35. Trigger.TriggerEvent += OnBtnTrigger;
  36. App.TriggerEvent += OnBtnTrigger;
  37. Home.TriggerEvent += OnBtnTrigger;
  38. }
  39. /// <summary> Executes the 'button trigger' action. </summary>
  40. /// <param name="key"> The key.</param>
  41. /// <param name="go"> The go.</param>
  42. /// <param name="racastInfo"> Information describing the racast.</param>
  43. private void OnBtnTrigger(string key, GameObject go, RaycastResult racastInfo)
  44. {
  45. if (key.Equals(NRButton.Enter))
  46. {
  47. if (go == App.gameObject)
  48. {
  49. m_SystemButtonState.buttons[0] = true;
  50. }
  51. if (go == Trigger.gameObject)
  52. {
  53. m_SystemButtonState.buttons[1] = true;
  54. }
  55. if (go == Home.gameObject)
  56. {
  57. m_SystemButtonState.buttons[2] = true;
  58. }
  59. }
  60. else if (key.Equals(NRButton.Exit))
  61. {
  62. if (go == App.gameObject)
  63. {
  64. m_SystemButtonState.buttons[0] = false;
  65. }
  66. if (go == Trigger.gameObject)
  67. {
  68. m_SystemButtonState.buttons[1] = false;
  69. }
  70. if (go == Home.gameObject)
  71. {
  72. m_SystemButtonState.buttons[2] = false;
  73. }
  74. }
  75. if (go == Trigger.gameObject
  76. && (key.Equals(NRButton.Hover) || key.Equals(NRButton.Enter)))
  77. {
  78. CalculateTouchPos(go, racastInfo);
  79. }
  80. else
  81. {
  82. m_SystemButtonState.touch_x = 0f;
  83. m_SystemButtonState.touch_y = 0f;
  84. }
  85. this.m_Receiver?.OnDataReceived(m_SystemButtonState);
  86. }
  87. /// <summary> Calculates the touch position. </summary>
  88. /// <param name="go"> The go.</param>
  89. /// <param name="racastInfo"> Information describing the racast.</param>
  90. private void CalculateTouchPos(GameObject go, RaycastResult racastInfo)
  91. {
  92. RectTransform rect = go.GetComponent<RectTransform>();
  93. Vector3[] v = new Vector3[4];
  94. rect.GetWorldCorners(v);
  95. var touchToCenter = racastInfo.worldPosition - go.transform.position;
  96. var rightToCenter = (v[3] - v[0]) * 0.5f;
  97. var topToCenter = (v[1] - v[0]) * 0.5f;
  98. var halfWidth = (v[3] - v[0]).magnitude * 0.5f;
  99. var halfHeight = (v[1] - v[0]).magnitude * 0.5f;
  100. var alpha = Vector3.Angle(rightToCenter, touchToCenter);
  101. var touchToX = (touchToCenter * Mathf.Cos(alpha * Mathf.PI / 180)).magnitude;
  102. var touchToY = (touchToCenter * Mathf.Sin(alpha * Mathf.PI / 180)).magnitude;
  103. bool x_forward = Vector3.Dot(touchToCenter, rightToCenter) > 0;
  104. bool y_forward = Vector3.Dot(touchToCenter, topToCenter) > 0;
  105. var touchx = touchToX > halfWidth ? (x_forward ? 1f : -1f) : (x_forward ? touchToX / halfWidth : -touchToX / halfWidth);
  106. var touchy = touchToY > halfHeight ? (y_forward ? 1f : -1f) : (y_forward ? touchToY / halfHeight : -touchToY / halfHeight);
  107. m_SystemButtonState.touch_x = touchx;
  108. m_SystemButtonState.touch_y = touchy;
  109. }
  110. #if UNITY_EDITOR
  111. /// <summary> Executes the 'disable' action. </summary>
  112. private void OnDisable()
  113. {
  114. if (!NRInput.EmulateVirtualDisplayInEditor)
  115. ClearSystemButtonState();
  116. }
  117. /// <summary> Clears the system button state. </summary>
  118. private void ClearSystemButtonState()
  119. {
  120. }
  121. #endif
  122. }
  123. }