This repository has been archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathInputManager.cs
481 lines (424 loc) · 16.7 KB
/
InputManager.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
MIT License
Copyright (c) 2021 jbienz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace LookingGlass
{
/// <summary>
/// Represents the hardware buttons present on all generations of Looking Glass displays.
/// </summary>
public enum HardwareButton
{
/// <summary>
/// Square button on Looking Glass classic devices.
/// </summary>
Square = 0,
/// <summary>
/// Left button on Looking Glass classic devices.
/// </summary>
Left = 1,
/// <summary>
/// Right button on Looking Glass classic devices.
/// </summary>
Right = 2,
/// <summary>
/// Circle button on Looking Glass classic devices.
/// </summary>
Circle = 3,
/// <summary>
/// Forward button on Portrait and Gen2 devices.
/// </summary>
Forward = 4,
/// <summary>
/// Back button on Portrait and Gen2 devices.
/// </summary>
Back = 5,
/// <summary>
/// Play / Pause / Loop button on Portrait and Gen2 devices.
/// </summary>
PlayPause = 6
}
/// <summary>
/// Indicates when hardware input will be emulated.
/// </summary>
public enum InputEmulationMode
{
/// <summary>
/// Hardware input will never be emulated.
/// </summary>
Never,
/// <summary>
/// Hardware input will only be emulated while in the Unity editor.
/// </summary>
EditorOnly,
/// <summary>
/// Hardware input will always be emulated.
/// </summary>
Always
}
/// <summary>
/// Manages hardware buttons on types of Looking Glass displays including Gen2 and classic.
/// </summary>
/// <remarks>
/// This is an updated version of the
/// <see href="https://docs.lookingglassfactory.com/Unity/Scripts/ButtonManager">ButtonManager</see>
/// designed to work with both Gen1 and Gen2 hardware.
/// </remarks>
public class InputManager
{
#region Nested Types
/// <summary>
/// Different button states we can check for.
/// </summary>
private enum ButtonState
{
/// <summary>
/// The button was pressed on this frame.
/// </summary>
Down,
/// <summary>
/// The button was released on this frame.
/// </summary>
Up,
/// <summary>
/// The button is held.
/// </summary>
Held
}
/// <summary>
/// Used in a lookup table to map a <see cref="HardwareButton"/> to one or more <see cref="KeyCode"/>s or
/// <see cref="ExtendedKeyCode"/>s.
/// </summary>
private class KeyMap
{
#region Member Variables
private List<KeyCode> keys = new List<KeyCode>();
private List<ExtendedKeyCode> extendedKeys = new List<ExtendedKeyCode>();
#endregion // Member Variables
#region Public Properties
/// <summary>
/// Gets the keys in the map.
/// </summary>
public List<KeyCode> Keys => keys;
/// <summary>
/// Gets the extended keys in the map.
/// </summary>
public List<ExtendedKeyCode> ExtendedKeys => extendedKeys;
#endregion // Public Properties
}
#endregion // Nested Types
#region Constants
private const string CLASSIC_JOY_KEY = "holoplay";
private const float JOY_CHECK_INTERVAL = 3.0f;
#endregion // Constants
#region Member Variables
static private Dictionary<HardwareButton, KeyMap> buttonKeyMap;
static private int classicJoyNumber = -2;
static private InputEmulationMode emulationMode = InputEmulationMode.Always;
static private bool searchForClassic = true;
static private float timeSinceClassicCheck = -3f;
#endregion // Member Variables
#region Constructors
/// <summary>
/// Initializes the <see cref="InputManager"/> singleton.
/// </summary>
static InputManager()
{
AddDefaultBindings();
UpdateEmulationBindings();
}
#endregion // Constructors
#region Internal Methods
/// <summary>
/// Adds the default key bindings.
/// </summary>
static private void AddDefaultBindings()
{
// Add KeyMap entries that map the media keys to Portrait buttons
GetKeyMap(HardwareButton.Forward).ExtendedKeys.Add(ExtendedKeyCode.MediaNext);
GetKeyMap(HardwareButton.Back).ExtendedKeys.Add(ExtendedKeyCode.MediaPrevious);
GetKeyMap(HardwareButton.PlayPause).ExtendedKeys.Add(ExtendedKeyCode.MediaPlayPause);
}
/// <summary>
/// Check to see if the specified button matches the specified state.
/// </summary>
/// <param name="button">
/// The <see cref="HardwareButton"/> to check.
/// </param>
/// <param name="state">
/// The <see cref="ButtonState"/> to check for.
/// </param>
/// <returns>
/// <c>true</c> if the button matches the specified state; otherwise <c>false</c>.
/// </returns>
static private bool CheckButtonState(HardwareButton button, ButtonState state)
{
// If we haven't found a classic joystick yet and we're searching for one, try to search again now
if ((searchForClassic) && (classicJoyNumber < 1))
{
DoClassicSearch();
}
// Which functions are we using to test keys and extended keys?
Func<KeyCode, bool> keyFunc;
Func<ExtendedKeyCode, bool> extendedKeyFunc;
switch (state)
{
case ButtonState.Down:
keyFunc = Input.GetKeyDown;
extendedKeyFunc = ExtendedInput.GetKeyDown;
break;
case ButtonState.Up:
keyFunc = Input.GetKeyUp;
extendedKeyFunc = ExtendedInput.GetKeyUp;
break;
case ButtonState.Held:
keyFunc = Input.GetKey;
extendedKeyFunc = ExtendedInput.GetKey;
break;
default:
throw new InvalidOperationException($"Unknown state '{state}'.");
}
// Get the KeyMap for the specified hardware button
KeyMap keyMap = GetKeyMap(button);
// Check standard keys first
foreach (var key in keyMap.Keys)
{
if (keyFunc(key)) { return true; }
}
// Check extended keys
foreach (var eKey in keyMap.ExtendedKeys)
{
if (extendedKeyFunc(eKey)) { return true; }
}
// No key or extended key matched the target state
return false;
}
/// <summary>
/// Searches for a joystick representing a classic hardware display.
/// </summary>
static private void DoClassicSearch()
{
// If already found, ignore
if (classicJoyNumber > 0) { return; }
// If too little time has passed since last check, ignore
if ((Time.unscaledTime - timeSinceClassicCheck) < JOY_CHECK_INTERVAL) { return; }
// Checking now
timeSinceClassicCheck = Time.unscaledTime;
// Get all joystick names
string[] joyNames = Input.GetJoystickNames();
// Look at each name
for (int i = 0; i < joyNames.Length; i++)
{
if (joyNames[i].ToLower().Contains(CLASSIC_JOY_KEY))
{
classicJoyNumber = i + 1; // Unity joystick IDs are 1 bound not 0 bound
break;
}
}
// Warn if not found, but only once
if (classicJoyNumber == -2)
{
Debug.LogWarning($"{nameof(InputManager)} - No HoloPlay joystick found but will continue to search.");
classicJoyNumber = -1;
}
// If the joystick has been found, add KeyMap entries that map the joystick buttons to classic hardware buttons
if (classicJoyNumber > 0)
{
GetKeyMap(HardwareButton.Square).Keys.Add(JoyButtonToCode(classicJoyNumber, 1));
GetKeyMap(HardwareButton.Left).Keys.Add(JoyButtonToCode(classicJoyNumber, 2));
GetKeyMap(HardwareButton.Right).Keys.Add(JoyButtonToCode(classicJoyNumber, 3));
GetKeyMap(HardwareButton.Circle).Keys.Add(JoyButtonToCode(classicJoyNumber, 4));
}
}
/// <summary>
/// Gets the <see cref="KeyMap"/> for the specified button, creating it if necessary.
/// </summary>
/// <param name="button">
/// The <see cref="HardwareButton"/> to get the <see cref="KeyMap"/> for.
/// </param>
/// <returns>
/// The <see cref="KeyMap"/> for the button.
/// </returns>
static private KeyMap GetKeyMap(HardwareButton button)
{
// Make sure the overall lookup table is crated
if (buttonKeyMap == null)
{
buttonKeyMap = new Dictionary<HardwareButton, KeyMap>();
}
// Try to get an existing KeyMap. If not found, create it.
KeyMap keyMap;
if (!buttonKeyMap.TryGetValue(button, out keyMap))
{
// Create it
keyMap = new KeyMap();
// Store it
buttonKeyMap[button] = keyMap;
}
// Return the map
return keyMap;
}
/// <summary>
/// Gets the <see cref="KeyCode"/> that represents the specified joystick and button.
/// </summary>
/// <param name="joystick">
/// The number of the joystick.
/// </param>
/// <param name="button">
/// The number of the button.
/// </param>
/// <returns>
/// The <see cref="KeyCode"/> that represents the joystick and button.
/// </returns>
static private KeyCode JoyButtonToCode(int joystick, int button)
{
// Validate
if ((joystick < 1) || (joystick > 8)) { throw new ArgumentOutOfRangeException(nameof(joystick)); }
if ((button < 0) || (button > 19)) { throw new ArgumentOutOfRangeException(nameof(button)); }
// Convert
return (KeyCode)Enum.Parse(typeof(KeyCode), "Joystick" + joystick + "Button" + button);
}
/// <summary>
/// Updates emulation based on the state of <see cref="EmulationMode"/> and the current app.
/// </summary>
static private void UpdateEmulationBindings()
{
// If emulation is enabled, add emulation key map entries as well
if ((emulationMode == InputEmulationMode.Always) || (emulationMode == InputEmulationMode.EditorOnly && Application.isEditor))
{
GetKeyMap(HardwareButton.Square).Keys.Add(KeyCode.Alpha1);
GetKeyMap(HardwareButton.Left).Keys.Add(KeyCode.Alpha2);
GetKeyMap(HardwareButton.Right).Keys.Add(KeyCode.Alpha3);
GetKeyMap(HardwareButton.Circle).Keys.Add(KeyCode.Alpha4);
}
else
{
// Remove won't throw an exception if not found
GetKeyMap(HardwareButton.Square).Keys.Remove(KeyCode.Alpha1);
GetKeyMap(HardwareButton.Left).Keys.Remove(KeyCode.Alpha2);
GetKeyMap(HardwareButton.Right).Keys.Remove(KeyCode.Alpha3);
GetKeyMap(HardwareButton.Circle).Keys.Remove(KeyCode.Alpha4);
}
}
#endregion // Internal Methods
#region Public Methods
/// <summary>
/// Returns <c>true</c> if any button is held.
/// </summary>
/// <returns>
/// <c>true</c> if any button is held; otherwise <c>false</c>.
/// </returns>
static public bool GetAnyButton()
{
// Get all buttons
var allButtons = Enum.GetValues(typeof(HardwareButton));
// Check for any button
foreach (var button in allButtons)
{
if (GetButton((HardwareButton)button)) { return true; }
}
// None found
return false;
}
/// <summary>
/// Returns <c>true</c> on the first frame that any button is pressed.
/// </summary>
/// <returns>
/// <c>true</c> if any button was pressed on this frame; otherwise <c>false</c>.
/// </returns>
static public bool GetAnyButtonDown()
{
// Get all buttons
var allButtons = Enum.GetValues(typeof(HardwareButton));
// Check for any button
foreach (var button in allButtons)
{
if (GetButtonDown((HardwareButton)button)) { return true; }
}
// None found
return false;
}
/// <summary>
/// Indicates if the specified button is held down.
/// </summary>
/// <param name="button">
/// The <see cref="HardwareButton"/> to test.
/// </param>
/// <returns>
/// <c>true</c> if the specified button is held down; otherwise <c>false</c>.
/// </returns>
static public bool GetButton(HardwareButton button)
{
return CheckButtonState(button, ButtonState.Held);
}
/// <summary>
/// Returns <c>true</c> on the first frame when the specified button is pressed.
/// </summary>
/// The <see cref="HardwareButton"/> to test.
/// <returns>
/// <c>true</c> if the specified button was pressed on this frame; otherwise <c>false</c>.
/// </returns>
static public bool GetButtonDown(HardwareButton button)
{
return CheckButtonState(button, ButtonState.Down);
}
/// <summary>
/// Returns <c>true</c> on the first frame when the specified button is released.
/// </summary>
/// The <see cref="HardwareButton"/> to test.
/// <returns>
/// <c>true</c> if the specified button was released on this frame; otherwise <c>false</c>.
/// </returns>
static public bool GetButtonUp(HardwareButton button)
{
return CheckButtonState(button, ButtonState.Up);
}
#endregion // Public Methods
#region Public Properties
/// <summary>
/// Gets or sets a value that indicates when to emulate hardware buttons.
/// </summary>
static public InputEmulationMode EmulationMode
{
get
{
return emulationMode;
}
set
{
// Make sure changing
if (value == emulationMode) { return; }
// Update emulation
UpdateEmulationBindings();
}
}
/// <summary>
/// Gets or sets a value that indicates whether to search for classic hardware.
/// </summary>
/// <remarks>
/// Classic hardware appears as a Joystick device. If this property is <c>true</c> (default) the manager will
/// search for the proper joystick after every 3 seconds until one is found.
/// </remarks>
static public bool SearchForClassic { get => searchForClassic; set => searchForClassic = value; }
#endregion // Public Properties
}
}