-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHOSASManager.cs
More file actions
168 lines (144 loc) · 6.51 KB
/
HOSASManager.cs
File metadata and controls
168 lines (144 loc) · 6.51 KB
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
/*
* Written by Jonas H.
*
* Manages flight sticks
* handling state change, such as reconnection
*
* Just place anywhere in your project
*
* You can add support for other flight sticks by
* 1. Creating an Input Layout Override
* 2. Adding a binding using the SidedStickInitialize Attribute
* 3. Creating a registerStick method
* 4. Adding the method using the SidedStickRegistrate Attribute
*
* Take a look at SidedTM16KM.cs to see, how it's done
* You do NOT need to change anything in this file
*/
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.InputSystem;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
public static class HOSASManager
{
// ---------------------------------------------------------------------------------------------------------------------------
// This stores the info & actions of supported flight sticks
// You can add more using the SidedStickInitialize & SidedStickRegistrate Attributes
// ---------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Add support for your own stick here, by crating an implementation of registerStick and adding it here
/// </summary>
public static Dictionary<string, System.Action<InputDevice>> supportedSticks = new Dictionary<string, System.Action<InputDevice>>();
/// <summary>
/// Change the settings of sticks, if their switch is flipped
/// Listen to Input Events
/// <summary>
public static InputAction changeHandAction = new InputAction(type: InputActionType.PassThrough);
// ---------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// Inital Setup of the Sticks. Makes sure to listen to any changes
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
#if UNITY_EDITOR
[InitializeOnLoadMethod]
#endif
static void OnEnable()
{
// Add generic leftHand / rightHand layout overrides for flightSticks
SidedStick.InitializeGenericStickSides();
// Load all the supported stick info using reflection
// First find all SidedStickInitializeAttributed methods
foreach (MethodInfo initializeMethod in System.AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.SelectMany(type => type.GetMethods())
.Where(method => method.GetCustomAttributes(typeof(SidedStickInitializeAttribute), false).Length > 0))
{
// Call initilisation
initializeMethod.Invoke(null, null);
// Add input binding to changeHandAction
string binding = initializeMethod.GetCustomAttribute<SidedStickInitializeAttribute>().binding;
if(binding != null) changeHandAction.AddBinding(binding);
}
// Now do the same for the registration methods
foreach (MethodInfo initializeMethod in System.AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.SelectMany(type => type.GetMethods())
.Where(method => method.GetCustomAttributes(typeof(SidedStickRegistrateAttribute), false).Length > 0))
{
// Get Info from Attribute
System.Action<InputDevice> registrateStickMethod = (System.Action<InputDevice>) initializeMethod.CreateDelegate(typeof(System.Action<InputDevice>));
string productName = initializeMethod.GetCustomAttribute<SidedStickRegistrateAttribute>().productName;
// Add registerStickMethod to supportedSticks
bool success = supportedSticks.TryAdd(productName, registrateStickMethod);
if(!success) Debug.LogWarning("HOSAS - could not add " + productName + " to manager. Possibly, it already is supported. This is to be expected, while in the editor");
}
// Now register all the sticks
registerAllSticks();
// Make sure to check any Sticks, which get plugged in sometime later
InputSystem.onDeviceChange += (device, change) =>
{
switch (change)
{
case InputDeviceChange.Enabled:
case InputDeviceChange.Reconnected:
case InputDeviceChange.HardReset:
Joystick stick = device as Joystick;
if (stick != null) registerStick(device);
break;
default:
break;
}
};
// Alsoupdated Sticks if there are changes
changeHandAction.performed += registerStickFromAction;
changeHandAction.canceled += registerStickFromAction;
changeHandAction.Enable();
}
/// <summary>
/// Looks through all devices and then decides on the side each joystick should use.
/// </summary>
static void registerAllSticks()
{
// Look at all devices
foreach (Joystick stick in Joystick.all)
{
registerStick(stick);
}
}
/// <summary>
/// Automatically detect the device that triggered the action and then re-register it
/// </summary>
/// <param name="context"></param>
public static void registerStickFromAction(InputAction.CallbackContext context)
{
// NOTE: the value could be read from the context, but other factors might be at play, so I don't
InputDevice device = context.control.device;
registerStick(device);
}
/// <summary>
/// Attempts to read the side side the stick is supposed to be on & then assigns it accordingly
/// </summary>
/// <param name="device">The joystick Input Device</param>
static void registerStick(InputDevice device)
{
// Find the correct handler for this type of stick
System.Action<InputDevice> registerCall = null;
bool isSupported = supportedSticks.TryGetValue(device.description.product, out registerCall);
// I do not know this stick
if (!isSupported)
{
#if UNITY_EDITOR
Debug.LogWarning("HOSASManager - Unsupported Flight Stick, probably just because of wonky load order");
#else
Debug.LogWarning("HOSASManager - Unsupported Flight Stick: " + device.description.product);
#endif
return;
}
// Now lets do the work
registerCall.Invoke(device);
}
}