ControllerInfoDisplayUI.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. using System;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. namespace NRKernal.NRExamples
  13. {
  14. /// <summary> A controller information display user interface. </summary>
  15. public class ControllerInfoDisplayUI : MonoBehaviour
  16. {
  17. /// <summary> The main information text. </summary>
  18. public Text mainInfoText;
  19. /// <summary> The extra information text. </summary>
  20. public Text extraInfoText;
  21. /// <summary> The extra information string. </summary>
  22. private string m_ExtraInfoStr;
  23. /// <summary> The maximum line. </summary>
  24. private int m_MaxLine = 20;
  25. /// <summary> The current debug hand. </summary>
  26. private ControllerHandEnum m_CurrentDebugHand = ControllerHandEnum.Right;
  27. private Transform m_CenterCamera;
  28. private Transform CenterCamera
  29. {
  30. get
  31. {
  32. if (m_CenterCamera == null)
  33. {
  34. if (NRSessionManager.Instance.CenterCameraAnchor != null)
  35. {
  36. m_CenterCamera = NRSessionManager.Instance.CenterCameraAnchor;
  37. }
  38. else if (Camera.main != null)
  39. {
  40. m_CenterCamera = Camera.main.transform;
  41. }
  42. }
  43. return m_CenterCamera;
  44. }
  45. }
  46. /// <summary> Updates this object. </summary>
  47. private void Update()
  48. {
  49. if (NRInput.GetAvailableControllersCount() < 2)
  50. {
  51. m_CurrentDebugHand = NRInput.DomainHand;
  52. }
  53. else
  54. {
  55. if (NRInput.GetButtonDown(ControllerHandEnum.Right, ControllerButton.TRIGGER))
  56. {
  57. m_CurrentDebugHand = ControllerHandEnum.Right;
  58. }
  59. else if (NRInput.GetButtonDown(ControllerHandEnum.Left, ControllerButton.TRIGGER))
  60. {
  61. m_CurrentDebugHand = ControllerHandEnum.Left;
  62. }
  63. }
  64. if (NRInput.GetButtonDown(m_CurrentDebugHand, ControllerButton.TRIGGER))
  65. AddExtraInfo("trigger_btn_down");
  66. if (NRInput.GetButtonDown(m_CurrentDebugHand, ControllerButton.HOME))
  67. AddExtraInfo("home_btn_down");
  68. if (NRInput.GetButtonDown(m_CurrentDebugHand, ControllerButton.APP))
  69. AddExtraInfo("app_btn_down");
  70. if (NRInput.GetButtonUp(m_CurrentDebugHand, ControllerButton.TRIGGER))
  71. AddExtraInfo("trigger_btn_up");
  72. if (NRInput.GetButtonUp(m_CurrentDebugHand, ControllerButton.HOME))
  73. AddExtraInfo("home_btn_up");
  74. if (NRInput.GetButtonUp(m_CurrentDebugHand, ControllerButton.APP))
  75. AddExtraInfo("app_btn_up");
  76. FollowMainCam();
  77. RefreshInfoTexts();
  78. }
  79. /// <summary> Follow main camera. </summary>
  80. private void FollowMainCam()
  81. {
  82. transform.position = CenterCamera.position;
  83. transform.rotation = CenterCamera.rotation;
  84. }
  85. /// <summary> Refresh information texts. </summary>
  86. private void RefreshInfoTexts()
  87. {
  88. mainInfoText.text =
  89. "controller count: " + NRInput.GetAvailableControllersCount().ToString() + "\n"
  90. + "type: " + NRInput.GetControllerType().ToString() + "\n"
  91. + "current debug hand: " + m_CurrentDebugHand.ToString() + "\n"
  92. + "position available: " + NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_POSITION).ToString() + "\n"
  93. + "rotation available: " + NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_ROTATION).ToString() + "\n"
  94. + "gyro available: " + NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_GYRO).ToString() + "\n"
  95. + "accel available: " + NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_ACCEL).ToString() + "\n"
  96. + "mag available: " + NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_MAG).ToString() + "\n"
  97. + "battery available: " + NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_BATTERY).ToString() + "\n"
  98. + "vibration available: " + NRInput.GetControllerAvailableFeature(ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_HAPTIC_VIBRATE).ToString() + "\n"
  99. + "rotation: " + NRInput.GetRotation(m_CurrentDebugHand).ToString("F3") + "\n"
  100. + "position: " + NRInput.GetPosition(m_CurrentDebugHand).ToString("F3") + "\n"
  101. + "touch: " + NRInput.GetTouch(m_CurrentDebugHand).ToString("F3") + "\n"
  102. + "trigger button: " + NRInput.GetButton(m_CurrentDebugHand, ControllerButton.TRIGGER).ToString() + "\n"
  103. + "home button: " + NRInput.GetButton(m_CurrentDebugHand, ControllerButton.HOME).ToString() + "\n"
  104. + "app button: " + NRInput.GetButton(m_CurrentDebugHand, ControllerButton.APP).ToString() + "\n"
  105. + "grip button: " + NRInput.GetButton(m_CurrentDebugHand, ControllerButton.GRIP).ToString() + "\n"
  106. + "touchpad button: " + NRInput.GetButton(m_CurrentDebugHand, ControllerButton.TOUCHPAD_BUTTON).ToString() + "\n"
  107. + "gyro: " + NRInput.GetGyro(m_CurrentDebugHand).ToString("F3") + "\n"
  108. + "accel: " + NRInput.GetAccel(m_CurrentDebugHand).ToString("F3") + "\n"
  109. + "mag: " + NRInput.GetMag(m_CurrentDebugHand).ToString("F3") + "\n"
  110. + "battery: " + NRInput.GetControllerBattery(m_CurrentDebugHand);
  111. extraInfoText.text = m_ExtraInfoStr;
  112. //Debug.Log("istouching:" + NRInput.IsTouching() + " value:" + NRInput.GetTouch(m_CurrentDebugHand).ToString("F3"));
  113. PrintInputState();
  114. }
  115. private void PrintInputState()
  116. {
  117. Debug.LogFormat("istouching:{0} getbutton app:{1} trigger:{2} home:{3} \n" +
  118. "getbuttondown app:{4} trigger:{5} home:{6} \n" +
  119. "getbuttonup app:{7} trigger:{8} home:{9} \n" +
  120. "origin touch:[{10}] gettouch:{11}", NRInput.IsTouching(), NRInput.GetButton(ControllerButton.APP), NRInput.GetButton(ControllerButton.TRIGGER), NRInput.GetButton(ControllerButton.HOME)
  121. , NRInput.GetButtonDown(ControllerButton.APP), NRInput.GetButtonDown(ControllerButton.TRIGGER), NRInput.GetButtonDown(ControllerButton.HOME)
  122. , NRInput.GetButtonUp(ControllerButton.APP), NRInput.GetButtonUp(ControllerButton.TRIGGER), NRInput.GetButtonUp(ControllerButton.HOME)
  123. , NRVirtualDisplayer.SystemButtonState.ToString(), NRInput.GetTouch(m_CurrentDebugHand).ToString("F3"));
  124. }
  125. /// <summary> Adds an extra information. </summary>
  126. /// <param name="infoStr"> The information string.</param>
  127. private void AddExtraInfo(string infoStr)
  128. {
  129. if (string.IsNullOrEmpty(infoStr))
  130. return;
  131. if (string.IsNullOrEmpty(m_ExtraInfoStr))
  132. m_ExtraInfoStr = infoStr;
  133. else
  134. m_ExtraInfoStr = m_ExtraInfoStr + Environment.NewLine + infoStr;
  135. int count = m_ExtraInfoStr.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Length;
  136. if (count > m_MaxLine)
  137. m_ExtraInfoStr = m_ExtraInfoStr.Substring(m_ExtraInfoStr.IndexOf(Environment.NewLine) + Environment.NewLine.Length);
  138. }
  139. }
  140. }