OVRGearVrControllerTest.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. public class OVRGearVrControllerTest : MonoBehaviour
  19. {
  20. public class BoolMonitor
  21. {
  22. public delegate bool BoolGenerator();
  23. private string m_name = "";
  24. private BoolGenerator m_generator;
  25. private bool m_prevValue = false;
  26. private bool m_currentValue = false;
  27. private bool m_currentValueRecentlyChanged = false;
  28. private float m_displayTimeout = 0.0f;
  29. private float m_displayTimer = 0.0f;
  30. public BoolMonitor(string name, BoolGenerator generator, float displayTimeout = 0.5f)
  31. {
  32. m_name = name;
  33. m_generator = generator;
  34. m_displayTimeout = displayTimeout;
  35. }
  36. public void Update()
  37. {
  38. m_prevValue = m_currentValue;
  39. m_currentValue = m_generator();
  40. if (m_currentValue != m_prevValue)
  41. {
  42. m_currentValueRecentlyChanged = true;
  43. m_displayTimer = m_displayTimeout;
  44. }
  45. if (m_displayTimer > 0.0f)
  46. {
  47. m_displayTimer -= Time.deltaTime;
  48. if (m_displayTimer <= 0.0f)
  49. {
  50. m_currentValueRecentlyChanged = false;
  51. m_displayTimer = 0.0f;
  52. }
  53. }
  54. }
  55. public void AppendToStringBuilder(ref StringBuilder sb)
  56. {
  57. sb.Append(m_name);
  58. if (m_currentValue && m_currentValueRecentlyChanged)
  59. sb.Append(": *True*\n");
  60. else if (m_currentValue)
  61. sb.Append(": True \n");
  62. else if (!m_currentValue && m_currentValueRecentlyChanged)
  63. sb.Append(": *False*\n");
  64. else if (!m_currentValue)
  65. sb.Append(": False \n");
  66. }
  67. }
  68. public Text uiText;
  69. private List<BoolMonitor> monitors;
  70. private StringBuilder data;
  71. void Start()
  72. {
  73. if (uiText != null)
  74. {
  75. uiText.supportRichText = false;
  76. }
  77. data = new StringBuilder(2048);
  78. monitors = new List<BoolMonitor>()
  79. {
  80. // virtual
  81. new BoolMonitor("WasRecentered", () => OVRInput.GetControllerWasRecentered()),
  82. new BoolMonitor("One", () => OVRInput.Get(OVRInput.Button.One)),
  83. new BoolMonitor("OneDown", () => OVRInput.GetDown(OVRInput.Button.One)),
  84. new BoolMonitor("OneUp", () => OVRInput.GetUp(OVRInput.Button.One)),
  85. new BoolMonitor("One (Touch)", () => OVRInput.Get(OVRInput.Touch.One)),
  86. new BoolMonitor("OneDown (Touch)", () => OVRInput.GetDown(OVRInput.Touch.One)),
  87. new BoolMonitor("OneUp (Touch)", () => OVRInput.GetUp(OVRInput.Touch.One)),
  88. new BoolMonitor("Two", () => OVRInput.Get(OVRInput.Button.Two)),
  89. new BoolMonitor("TwoDown", () => OVRInput.GetDown(OVRInput.Button.Two)),
  90. new BoolMonitor("TwoUp", () => OVRInput.GetUp(OVRInput.Button.Two)),
  91. new BoolMonitor("PrimaryIndexTrigger", () => OVRInput.Get(OVRInput.Button.PrimaryIndexTrigger)),
  92. new BoolMonitor("PrimaryIndexTriggerDown", () => OVRInput.GetDown(OVRInput.Button.PrimaryIndexTrigger)),
  93. new BoolMonitor("PrimaryIndexTriggerUp", () => OVRInput.GetUp(OVRInput.Button.PrimaryIndexTrigger)),
  94. new BoolMonitor("PrimaryIndexTrigger (Touch)", () => OVRInput.Get(OVRInput.Touch.PrimaryIndexTrigger)),
  95. new BoolMonitor("PrimaryIndexTriggerDown (Touch)", () => OVRInput.GetDown(OVRInput.Touch.PrimaryIndexTrigger)),
  96. new BoolMonitor("PrimaryIndexTriggerUp (Touch)", () => OVRInput.GetUp(OVRInput.Touch.PrimaryIndexTrigger)),
  97. new BoolMonitor("PrimaryHandTrigger", () => OVRInput.Get(OVRInput.Button.PrimaryHandTrigger)),
  98. new BoolMonitor("PrimaryHandTriggerDown", () => OVRInput.GetDown(OVRInput.Button.PrimaryHandTrigger)),
  99. new BoolMonitor("PrimaryHandTriggerUp", () => OVRInput.GetUp(OVRInput.Button.PrimaryHandTrigger)),
  100. new BoolMonitor("Up", () => OVRInput.Get(OVRInput.Button.Up)),
  101. new BoolMonitor("Down", () => OVRInput.Get(OVRInput.Button.Down)),
  102. new BoolMonitor("Left", () => OVRInput.Get(OVRInput.Button.Left)),
  103. new BoolMonitor("Right", () => OVRInput.Get(OVRInput.Button.Right)),
  104. new BoolMonitor("Touchpad (Click)", () => OVRInput.Get(OVRInput.Button.PrimaryTouchpad)),
  105. new BoolMonitor("TouchpadDown (Click)", () => OVRInput.GetDown(OVRInput.Button.PrimaryTouchpad)),
  106. new BoolMonitor("TouchpadUp (Click)", () => OVRInput.GetUp(OVRInput.Button.PrimaryTouchpad)),
  107. new BoolMonitor("Touchpad (Touch)", () => OVRInput.Get(OVRInput.Touch.PrimaryTouchpad)),
  108. new BoolMonitor("TouchpadDown (Touch)", () => OVRInput.GetDown(OVRInput.Touch.PrimaryTouchpad)),
  109. new BoolMonitor("TouchpadUp (Touch)", () => OVRInput.GetUp(OVRInput.Touch.PrimaryTouchpad)),
  110. // raw
  111. new BoolMonitor("Start", () => OVRInput.Get(OVRInput.RawButton.Start)),
  112. new BoolMonitor("StartDown", () => OVRInput.GetDown(OVRInput.RawButton.Start)),
  113. new BoolMonitor("StartUp", () => OVRInput.GetUp(OVRInput.RawButton.Start)),
  114. new BoolMonitor("Back", () => OVRInput.Get(OVRInput.RawButton.Back)),
  115. new BoolMonitor("BackDown", () => OVRInput.GetDown(OVRInput.RawButton.Back)),
  116. new BoolMonitor("BackUp", () => OVRInput.GetUp(OVRInput.RawButton.Back)),
  117. new BoolMonitor("A", () => OVRInput.Get(OVRInput.RawButton.A)),
  118. new BoolMonitor("ADown", () => OVRInput.GetDown(OVRInput.RawButton.A)),
  119. new BoolMonitor("AUp", () => OVRInput.GetUp(OVRInput.RawButton.A)),
  120. };
  121. }
  122. static string prevConnected = "";
  123. static BoolMonitor controllers = new BoolMonitor("Controllers Changed", () => { return OVRInput.GetConnectedControllers().ToString() != prevConnected; });
  124. void Update()
  125. {
  126. OVRInput.Controller activeController = OVRInput.GetActiveController();
  127. data.Length = 0;
  128. byte recenterCount = OVRInput.GetControllerRecenterCount();
  129. data.AppendFormat("RecenterCount: {0}\n", recenterCount);
  130. byte battery = OVRInput.GetControllerBatteryPercentRemaining();
  131. data.AppendFormat("Battery: {0}\n", battery);
  132. float framerate = OVRPlugin.GetAppFramerate();
  133. data.AppendFormat("Framerate: {0:F2}\n", framerate);
  134. string activeControllerName = activeController.ToString();
  135. data.AppendFormat("Active: {0}\n", activeControllerName);
  136. string connectedControllerNames = OVRInput.GetConnectedControllers().ToString();
  137. data.AppendFormat("Connected: {0}\n", connectedControllerNames);
  138. data.AppendFormat("PrevConnected: {0}\n", prevConnected);
  139. controllers.Update();
  140. controllers.AppendToStringBuilder(ref data);
  141. prevConnected = connectedControllerNames;
  142. Quaternion rot = OVRInput.GetLocalControllerRotation(activeController);
  143. data.AppendFormat("Orientation: ({0:F2}, {1:F2}, {2:F2}, {3:F2})\n", rot.x, rot.y, rot.z, rot.w);
  144. Vector3 angVel = OVRInput.GetLocalControllerAngularVelocity(activeController);
  145. data.AppendFormat("AngVel: ({0:F2}, {1:F2}, {2:F2})\n", angVel.x, angVel.y, angVel.z);
  146. Vector3 angAcc = OVRInput.GetLocalControllerAngularAcceleration(activeController);
  147. data.AppendFormat("AngAcc: ({0:F2}, {1:F2}, {2:F2})\n", angAcc.x, angAcc.y, angAcc.z);
  148. Vector3 pos = OVRInput.GetLocalControllerPosition(activeController);
  149. data.AppendFormat("Position: ({0:F2}, {1:F2}, {2:F2})\n", pos.x, pos.y, pos.z);
  150. Vector3 vel = OVRInput.GetLocalControllerVelocity(activeController);
  151. data.AppendFormat("Vel: ({0:F2}, {1:F2}, {2:F2})\n", vel.x, vel.y, vel.z);
  152. Vector3 acc = OVRInput.GetLocalControllerAcceleration(activeController);
  153. data.AppendFormat("Acc: ({0:F2}, {1:F2}, {2:F2})\n", acc.x, acc.y, acc.z);
  154. Vector2 primaryTouchpad = OVRInput.Get(OVRInput.Axis2D.PrimaryTouchpad);
  155. data.AppendFormat("PrimaryTouchpad: ({0:F2}, {1:F2})\n", primaryTouchpad.x, primaryTouchpad.y);
  156. Vector2 secondaryTouchpad = OVRInput.Get(OVRInput.Axis2D.SecondaryTouchpad);
  157. data.AppendFormat("SecondaryTouchpad: ({0:F2}, {1:F2})\n", secondaryTouchpad.x, secondaryTouchpad.y);
  158. float indexTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger);
  159. data.AppendFormat("PrimaryIndexTriggerAxis1D: ({0:F2})\n", indexTrigger);
  160. float handTrigger = OVRInput.Get(OVRInput.Axis1D.PrimaryHandTrigger);
  161. data.AppendFormat("PrimaryHandTriggerAxis1D: ({0:F2})\n", handTrigger);
  162. for (int i = 0; i < monitors.Count; i++)
  163. {
  164. monitors[i].Update();
  165. monitors[i].AppendToStringBuilder(ref data);
  166. }
  167. if (uiText != null)
  168. {
  169. uiText.text = data.ToString();
  170. }
  171. }
  172. }