/
InputManager.cs
386 lines (341 loc) · 10.8 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
using System.Collections;
using System.Collections.Generic;
using NaughtyAttributes;
using UnityEngine;
using UnityEngine.Events;
public class InputManager : MonoBehaviour {
private static InputManager control;
public bool isRebinding;
public KeyCode cancelRebindKey = KeyCode.Escape;
[SerializeField]
private Keybinds keyBinds;
[SerializeField]
[ValidateInput("SetSingleton")]
private List<InputEvent> events = new List<InputEvent>();
private Dictionary<string, InputAction> lookup;
private int numKeyCodes;
private void Awake() {
control = this;
numKeyCodes = System.Enum.GetNames(typeof(KeyCode)).Length;
LoadKeyBinds();
BuildLookup();
}
private bool SetSingleton() {
if (control == this)
return true;
control = this;
return true;
}
private void Update() {
foreach (InputEvent e in events) {
switch (e.type) {
case InputEventType.OnKeyDown:
if (GetKeyDown(e.name, e.negative))
e.callback.Invoke();
break;
case InputEventType.OnKey:
if (GetKey(e.name, e.negative))
e.callback.Invoke();
break;
case InputEventType.OnKeyUp:
if (GetKeyUp(e.name, e.negative))
e.callback.Invoke();
break;
case InputEventType.OnAxisDown:
if (GetAxisDown(e.name))
e.callback.Invoke();
break;
case InputEventType.OnAxisUp:
if (GetAxisUp(e.name))
e.callback.Invoke();
break;
}
}
}
private string[] GetInputNames() {
string[] names = new string[keyBinds.actions.Length];
for (int i = 0; i < names.Length; i++)
names[i] = keyBinds.actions[i].name;
return names;
}
public bool AddListener(string name, bool negative, InputEventType type, UnityAction callback) {
if (lookup == null)
BuildLookup();
if (lookup.ContainsKey(name)) {
events.Add(new InputEvent(name, negative, type, callback));
return true;
} else
return false;
}
/// <summary>
/// Has the key been pressed this frame?
/// </summary>
/// <param name="name">The name of the input.</param>
/// <param name="negative">Should access the negative value? Default is positive.</param>
public static bool GetKeyDown(string name, bool negative = false) {
control.CheckNameValid(name);
InputAction action = control.lookup[name];
return Input.GetKeyDown(negative?action.modifiedNegative1 : action.modifiedPositive1) ||
Input.GetKeyDown(negative?action.modifiedNegative2 : action.modifiedPositive2);
}
/// <summary>
/// Is the key being pressed right now?
/// </summary>
/// <param name="name">The name of the input.</param>
/// <param name="negative">Should access the negative value? Default is positive.</param>
public static bool GetKey(string name, bool negative = false) {
control.CheckNameValid(name);
InputAction action = control.lookup[name];
return Input.GetKey(negative?action.modifiedNegative1 : action.modifiedPositive1) ||
Input.GetKey(negative?action.modifiedNegative2 : action.modifiedPositive2);
}
/// <summary>
/// Has the key been released this frame?
/// </summary>
/// <param name="name">The name of the input.</param>
/// <param name="negative">Should access the negative value? Default is positive.</param>
public static bool GetKeyUp(string name, bool negative = false) {
control.CheckNameValid(name);
InputAction action = control.lookup[name];
return Input.GetKeyUp(negative?action.modifiedNegative1 : action.modifiedPositive1) ||
Input.GetKeyUp(negative?action.modifiedNegative1 : action.modifiedPositive1);
}
/// <summary>
/// Get the keycode of the input.
/// </summary>
/// <param name="name">The name of the input.</param>
/// <param name="primary">Should query the primary bindings?</param>
/// <param name="negative">Should access the negative value? Default is positive.</param>
public static KeyCode GetBinding(string name, bool primary = true, bool negative = false) {
if (primary)
return negative ? control.lookup[name].modifiedNegative1 : control.lookup[name].modifiedPositive1;
else
return negative ? control.lookup[name].modifiedNegative2 : control.lookup[name].modifiedPositive2;
}
/// <summary>
/// Returns -1 if the negative key is pressed, 1 if the positive key is pressed, and 0 if both or neither.
/// </summary>
/// <param name="name">The name of the input.</param>
public static int GetAxis(string name) => (GetKey(name) ? 1 : 0) + (GetKey(name, true) ? -1 : 0);
/// <summary>
/// Has either of the keys on the axis been pressed this frame?
/// </summary>
/// <param name="name">The name of the input.</param>
public static bool GetAxisDown(string name) => GetKeyDown(name) || GetKeyDown(name, true);
/// <summary>
/// Has either of the keys on the axis been released this frame?
/// </summary>
/// <param name="name">The name of the input.</param>
public static bool GetAxisUp(string name) => GetKeyUp(name) || GetKeyUp(name, true);
/// <summary>
/// Sets the bindings of all keys back to their defaults.
/// </summary>
public static void ResetAllBindings() {
for (int i = 0; i < control.keyBinds.actions.Length; i++)
control.keyBinds.actions[i].ResetBindings();
SaveKeyBinds();
}
/// <summary>
/// Resets a single input binding.
/// </summary>
/// <param name="name">The name of the input.</param>
/// <param name="negative">Should access the negative value? Default is positive.</param>
public static void ResetBinding(string name, bool negative = false) {
control.CheckNameValid(name);
control.lookup[name].ResetBinding(negative);
SaveKeyBinds();
}
private void CheckNameValid(string name) {
if (!lookup.ContainsKey(name))
throw new System.Exception($"hmm fuck, dont seem to have any binding w the name {name}???");
}
// to b used w SaveSystem.cs on github:
// https://github.com/celechii/Unity-Tools/blob/7d1182ea0f34b9b517bdbd67f88cb275316f1c2e/SaveSystem.cs
// <3
/// <summary>
/// Uses the save system to save the keybind JSON to /Resources/Saves/Keybinds.txt
/// </summary>
public static void SaveKeyBinds() {
SaveSystem.SaveTxt("Keybinds", control.keyBinds);
}
/// <summary>
/// Uses the save system to load the keybind JSON from /Resources/Saves/Keybinds.txt
/// If nothing is found, it will load the defaults instead.
/// </summary>
public void LoadKeyBinds() {
if (SaveSystem.SaveExists("Keybinds.txt"))
keyBinds = SaveSystem.LoadTxt<Keybinds>("Keybinds");
else
ResetAllBindings();
BuildLookup();
}
private void BuildLookup() {
if (lookup == null)
lookup = new Dictionary<string, InputAction>();
else
lookup.Clear();
for (int i = 0; i < keyBinds.actions.Length; i++)
lookup.Add(keyBinds.actions[i].name, keyBinds.actions[i]);
}
/// <summary>
/// Start the rebind process for a single input.
/// </summary>
/// <param name="name">The name of the input.</param>
/// <param name="callback">Method to call when the rebinding is complete.</param>
/// <param name="negative">Should access the negative value? Default is positive.</param>
public static void RebindKey(string name, System.Action callback = null, bool negative = false) {
control.CheckNameValid(name);
Debug.Log(control);
control.StartCoroutine(control.RebindInput(name, callback, negative));
}
private IEnumerator RebindInput(string name, System.Action callback = null, bool negative = false) {
isRebinding = true;
yield return null;
while (isRebinding) {
if (Input.GetKeyDown(cancelRebindKey)) {
isRebinding = false;
break;
}
if (Input.anyKeyDown) {
for (int i = 0; i < numKeyCodes; i++) {
if (Input.GetKeyDown((KeyCode)i)) {
lookup[name].SetKey((KeyCode)i, negative);
isRebinding = false;
}
}
}
yield return null;
}
SaveKeyBinds();
callback?.Invoke();
}
public static string GetKeyCodeNiceName(KeyCode key) {
switch (key) {
case KeyCode.Mouse0:
return "Left Mouse Button";
case KeyCode.Mouse1:
return "Right Mouse Button";
case KeyCode.Mouse2:
return "Middle Mouse Button";
case KeyCode.Mouse3:
case KeyCode.Mouse4:
case KeyCode.Mouse5:
case KeyCode.Mouse6:
return "Mouse Button " + ((KeyCode)(int)key - 323);
case KeyCode.Alpha0:
case KeyCode.Alpha1:
case KeyCode.Alpha2:
case KeyCode.Alpha3:
case KeyCode.Alpha4:
case KeyCode.Alpha5:
case KeyCode.Alpha6:
case KeyCode.Alpha7:
case KeyCode.Alpha8:
case KeyCode.Alpha9:
return ((int)key - 48).ToString();
case KeyCode.Keypad0:
case KeyCode.Keypad1:
case KeyCode.Keypad2:
case KeyCode.Keypad3:
case KeyCode.Keypad4:
case KeyCode.Keypad5:
case KeyCode.Keypad6:
case KeyCode.Keypad7:
case KeyCode.Keypad8:
case KeyCode.Keypad9:
return "NUM " + ((KeyCode)(int)key - 256);
default:
return MakeKeyReadable(key);
}
}
public static string MakeKeyReadable(KeyCode key) {
string entry = key.ToString();
for (int i = 1; i < entry.Length; i++) {
if (entry[i] >= 'A' && entry[i] <= 'Z') {
entry = entry.Insert(i, " ");
i++;
}
}
return entry;
}
[System.Serializable]
private struct Keybinds {
public InputAction[] actions;
}
[System.Serializable]
public class InputAction {
public string name;
public KeyCode positive1;
public KeyCode negative1;
[Space]
public KeyCode positive2;
public KeyCode negative2;
[HideInInspector]
public KeyCode modifiedPositive1;
[HideInInspector]
public KeyCode modifiedNegative1;
[HideInInspector]
public KeyCode modifiedPositive2;
[HideInInspector]
public KeyCode modifiedNegative2;
public void SetKey(KeyCode key, bool primary = true, bool negative = false) {
if (primary) {
if (negative)
modifiedNegative1 = key;
else
modifiedPositive1 = key;
} else {
if (negative)
modifiedNegative2 = key;
else
modifiedPositive2 = key;
}
}
public void ResetBindings() {
modifiedPositive1 = positive1;
modifiedNegative1 = negative1;
modifiedPositive2 = positive2;
modifiedNegative2 = negative2;
}
public void ResetBinding(bool primary = true, bool negative = false) {
if (primary) {
if (negative)
modifiedNegative1 = this.negative1;
else
modifiedPositive1 = positive1;
} else {
if (negative)
modifiedNegative2 = this.negative2;
else
modifiedPositive2 = positive2;
}
}
}
[System.Serializable]
private class InputEvent {
[Dropdown("GetInputNames")]
public string name;
[AllowNesting]
[HideIf("hideNegative")]
public bool negative;
public InputEventType type;
[Space]
public UnityEvent callback;
private bool hideNegative => type == InputEventType.OnAxisDown || type == InputEventType.OnAxisUp;
public InputEvent(string name, bool negative, InputEventType type, UnityAction callback) {
this.name = name;
this.negative = negative;
this.type = type;
this.callback = new UnityEvent();
this.callback.AddListener(callback);
}
private string[] GetInputNames() => InputManager.control.GetInputNames();
}
public enum InputEventType {
OnKeyDown,
OnKey,
OnKeyUp,
OnAxisDown,
OnAxisUp
}
}