BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Util / OVRGearVrControllerTest.cs

OVRGearVrControllerTest.cs

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