-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCrossDevice.cs
325 lines (289 loc) · 10.4 KB
/
CrossDevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
using UnityEngine;
using Valve.VR;
public static class CrossDevice
{
// For handling cross-device needs, like different input subtleties
// between Rift Touch and Vive controllers.
public static DeviceType type { get; private set; }
public static bool desktopMode = false;
public static bool rigPositionIsAuthority = true;
public static Side desktopDialogSide = Side.Left;
public static bool oculusTouchLegacyMode = false;
const string oculusTouchLegacyMode_key = "oculusTouchLegacyMode";
public static bool hasStick = false;
public static bool hasSeparateTriggerAndGrab = false;
public static ulong button_grab { get; private set; }
public static ulong button_grabTip { get; private set; }
public static ulong button_teleport { get; private set; }
public static ulong button_context { get; private set; }
public static ulong button_delete { get; private set; }
public static ulong button_legPuppeteering { get; private set; }
public static bool joystickWasFound { get; private set; }
/*
// Strings may slightly differ, e.g. "Vive. MV" or "Vive MV.", and not just per OS
const string model_vive = "Vive MV";
const string model_index = "Valve Index"; // Speculative, we don't know this one yet
const string model_rift = "Oculus Rift CV1";
const string model_lenovoExplorer = "Lenovo Explorer";
const string model_hpWindowsMixedReality = "HP Windows Mixed Reality Headset";
const string model_samsungOdyssey = "Samsung Windows Mixed Reality 800ZAA";
const string model_acer = "Acer AH100";
*/
public static void Init()
{
oculusTouchLegacyMode = PlayerPrefs.GetInt(oculusTouchLegacyMode_key, 0) == 1;
type = GetTypeByModelName(GetVrModelString());
SetButtonMappingForType();
}
public static string GetVrModelString()
{
return UnityEngine.XR.XRDevice.model != null ?
UnityEngine.XR.XRDevice.model : "";
}
static string GetModelOverride()
{
string modelOverride = null;
string[] names = Input.GetJoystickNames();
if (names != null)
{
foreach (string name in names)
{
if ( !string.IsNullOrEmpty(name) &&
( name.Contains("Knuckles") || name.Contains("Index") )
)
{
joystickWasFound = true;
modelOverride = "Valve Index";
// Debug.Log("Found Knuckles due to: " + name);
break;
}
}
}
return modelOverride;
}
static DeviceType GetTypeByModelName(string model)
{
DeviceType thisType = DeviceType.Other;
string modelOverride = GetModelOverride();
if ( !string.IsNullOrEmpty(modelOverride) )
{
// Debug.Log("Model name was " + model + ", but changing to " + modelOverride);
model = modelOverride;
}
model = model.ToLower();
if ( model.Contains("rift") || model.Contains("quest") )
{
thisType = DeviceType.OculusTouch;
}
else if ( model.Contains("index") )
{
thisType = DeviceType.Index;
}
else if ( model.Contains("vive") )
{
thisType = DeviceType.Vive;
}
else if (
model.Contains("mixed") ||
model.Contains("lenovo explorer") ||
model.Contains("acer")
)
{
thisType = DeviceType.WindowsMixedReality;
}
return thisType;
}
static void SetButtonMappingForType()
{
ulong buttonXA = ( 1ul << (int)EVRButtonId.k_EButton_A );
switch (type)
{
case DeviceType.OculusTouch:
button_grab = SteamVR_Controller.ButtonMask.Grip;
button_grabTip = SteamVR_Controller.ButtonMask.Trigger;
if (oculusTouchLegacyMode)
{
button_teleport = buttonXA;
button_context = SteamVR_Controller.ButtonMask.ApplicationMenu;
button_delete = SteamVR_Controller.ButtonMask.Touchpad;
}
else
{
button_teleport = SteamVR_Controller.ButtonMask.Touchpad;
button_context = SteamVR_Controller.ButtonMask.ApplicationMenu;
button_delete = buttonXA;
}
hasStick = true;
hasSeparateTriggerAndGrab = true;
break;
case DeviceType.Index:
button_grab = SteamVR_Controller.ButtonMask.Grip;
button_grabTip = SteamVR_Controller.ButtonMask.Trigger;
button_teleport = SteamVR_Controller.ButtonMask.Touchpad;
button_context = SteamVR_Controller.ButtonMask.ApplicationMenu;
button_delete = buttonXA;
hasStick = true;
hasSeparateTriggerAndGrab = true;
break;
case DeviceType.Vive:
case DeviceType.WindowsMixedReality:
default:
button_grab = SteamVR_Controller.ButtonMask.Trigger;
button_grabTip = SteamVR_Controller.ButtonMask.Trigger;
button_teleport = SteamVR_Controller.ButtonMask.Touchpad;
button_context = SteamVR_Controller.ButtonMask.ApplicationMenu;
button_delete = SteamVR_Controller.ButtonMask.Grip;
hasStick = false;
hasSeparateTriggerAndGrab = false;
break;
}
button_legPuppeteering = button_teleport;
}
public static void AdjustControllerTransformIfNeeded(Transform transform)
{
if ( transform.CompareTag("HandCore") )
{
switch (type)
{
case DeviceType.OculusTouch:
{
transform.Rotate( new Vector3(23.5f, 0f, 0f) );
float ourScale = Managers.personManager != null ? Managers.personManager.GetOurScale() : 1f;
transform.Translate( Vector3.forward * (-0.02f * ourScale) );
}
break;
case DeviceType.Index:
{
transform.Rotate( new Vector3(23.5f, 0f, 0f) );
float ourScale = Managers.personManager != null ? Managers.personManager.GetOurScale() : 1f;
transform.Translate( Vector3.forward * (-0.02f * ourScale) );
}
break;
case DeviceType.WindowsMixedReality:
{
transform.Rotate( new Vector3(30f, 0f, 0f) );
}
break;
}
}
}
public static bool GetPress(SteamVR_Controller.Device controller, ulong buttonType, Side handSide)
{
bool isIt = false;
if (desktopMode)
{
isIt = GetMouseButton(buttonType, handSide);
}
else if (controller != null)
{
isIt = controller.GetPress(buttonType);
}
return isIt;
}
public static bool GetPressUp(SteamVR_Controller.Device controller, ulong buttonType, Side handSide)
{
bool isIt = false;
if (desktopMode)
{
isIt = GetMouseButtonUp(buttonType, handSide);
}
else if (controller != null)
{
isIt = controller.GetPressUp(buttonType);
}
return isIt;
}
public static bool GetPressDown(SteamVR_Controller.Device controller, ulong buttonType, Side handSide)
{
bool isIt = false;
if (desktopMode)
{
isIt = GetMouseButtonDown(buttonType, handSide);
}
else if (controller != null)
{
isIt = controller.GetPressDown(buttonType);
}
return isIt;
}
static bool GetMouseButton(ulong buttonType, Side handSide)
{
bool isIt = false;
int mouseButton = GetMouseButtonInt(buttonType, handSide);
if (mouseButton != -1)
{
isIt = Input.GetMouseButton(mouseButton);
}
return isIt;
}
static bool GetMouseButtonUp(ulong buttonType, Side handSide)
{
bool isIt = false;
int mouseButton = GetMouseButtonInt(buttonType, handSide);
if (mouseButton != -1)
{
isIt = Input.GetMouseButtonUp(mouseButton);
}
return isIt;
}
static bool GetMouseButtonDown(ulong buttonType, Side handSide)
{
bool isIt = false;
int mouseButton = GetMouseButtonInt(buttonType, handSide);
if (mouseButton != -1)
{
isIt = Input.GetMouseButtonDown(mouseButton);
}
return isIt;
}
public static int GetMouseButtonInt(ulong buttonType, Side handSide)
{
int button = -1;
if (buttonType == button_grab || buttonType == button_grabTip)
{
if ( Our.GetCurrentNonStartDialog() == null )
{
button = 0;
}
}
else if (buttonType == button_teleport)
{
}
else if (buttonType == button_delete)
{
button = 2;
}
else if (buttonType == button_context)
{
if (handSide == desktopDialogSide)
{
button = 1;
}
}
return button;
}
public static void TriggerHapticPulse(Hand hand, ushort pulseAmount)
{
if (Managers.areaManager != null && Managers.areaManager.startedLaunchFadeIn &&
hand != null && Universe.features.hapticPulse)
{
if (desktopMode)
{
Managers.soundManager.Play("bump", hand.transform, 0.05f);
}
else
{
if (hand.controller != null && hand.controller.connected)
{
hand.controller.TriggerHapticPulse(pulseAmount);
}
}
}
}
public static void SetOculusTouchLegacyMode(bool state)
{
oculusTouchLegacyMode = state;
PlayerPrefs.SetInt(oculusTouchLegacyMode_key, state ? 1 : 0);
SetButtonMappingForType();
}
}