-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathKeybindings.cs
More file actions
289 lines (270 loc) · 7.22 KB
/
Keybindings.cs
File metadata and controls
289 lines (270 loc) · 7.22 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
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
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace Wrj
{
public class Keybindings : MonoBehaviour
{
[SerializeField]
private bool quitOnEsc = true;
[SerializeField]
private bool fullScreenOnF11 = true;
[SerializeField]
private ButtonKeyCommand[] buttonKeys;
[SerializeField]
private ToggleKeyCommand[] toggleKeys;
[SerializeField]
private ActionKeyCommand[] actionKeys;
[SerializeField]
private HierarchyToggles[] objectEnableKeys;
public bool logKeys = false;
private List<KeyCommand> _keyCommands;
private List<ActionKeyCommand> _keyUps;
public void Add(KeyCommand keyCommand)
{
if (_keyCommands == null) _keyCommands = new List<KeyCommand>();
if (_keyUps == null) _keyUps = new List<ActionKeyCommand>();
if (keyCommand is ActionKeyCommand && ((ActionKeyCommand)keyCommand).onKeyUp)
{
_keyUps.Add((ActionKeyCommand)keyCommand);
}
else
{
_keyCommands.Add(keyCommand);
}
Prioritize();
}
private void Awake()
{
_keyCommands = new List<KeyCommand>();
_keyUps = new List<ActionKeyCommand>();
if (buttonKeys != null)
{
foreach (var item in buttonKeys)
{
_keyCommands.Add(item);
}
}
if (toggleKeys != null)
{
foreach (var item in toggleKeys)
{
_keyCommands.Add(item);
}
}
if (actionKeys != null)
{
foreach (var item in actionKeys)
{
if (item.onKeyUp)
{
_keyUps.Add(item);
}
else
{
_keyCommands.Add(item);
}
}
}
if (objectEnableKeys != null)
{
foreach (var item in objectEnableKeys)
{
_keyCommands.Add(item);
}
}
Prioritize();
}
private void Prioritize()
{
_keyCommands.Sort();
_keyUps.Sort();
}
void Update()
{
KeyCommand.NewFrame();
foreach (ActionKeyCommand keyUp in _keyUps)
{
if (Input.GetKeyUp(keyUp.key))
{
if (!keyUp.ModifierQualified()) continue;
if (logKeys)
{
Debug.Log(keyUp.ToString());
}
keyUp.Invoke();
}
}
if (!Input.anyKeyDown) return;
if (quitOnEsc && Input.GetKeyDown(KeyCode.Escape))
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
Application.Quit();
}
if (fullScreenOnF11 && Input.GetKeyDown(KeyCode.F11))
{
Screen.fullScreen = !Screen.fullScreen;
}
foreach (KeyCommand keyCommand in _keyCommands)
{
if (Input.GetKeyDown(keyCommand.key))
{
if (!keyCommand.ModifierQualified()) continue;
if (logKeys)
{
Debug.Log(keyCommand.ToString());
}
keyCommand.Invoke();
}
}
}
[Serializable]
public class KeyCommand : IComparable<KeyCommand>
{
public KeyCode key;
[Header("Modifier Keys")]
public bool ctrl;
public bool shift;
public bool alt;
public bool win;
private static List<KeyCode> rejections = new List<KeyCode>();
public bool ModifierQualified()
{
if (rejections.Contains(key)) return false;
// If no modifiers are required, ignore all modifier states
if (!ctrl && !shift && !alt && !win) return true;
bool shiftState = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool ctrlState = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
bool altState = Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt);
bool winState = Input.GetKey(KeyCode.LeftWindows) || Input.GetKey(KeyCode.RightWindows)
|| Input.GetKey(KeyCode.LeftApple) || Input.GetKey(KeyCode.RightApple);
// Return true if all states match requirements
return shiftState == shift &&
ctrlState == ctrl &&
altState == alt &&
winState == win;
}
public static void NewFrame()
{
if (rejections == null)
{
rejections = new List<KeyCode>();
}
rejections.Clear();
}
public virtual void Invoke()
{
rejections.Add(key);
}
public int ModifierCount()
{
int count = 0;
if (ctrl) count++;
if (shift) count++;
if (alt) count++;
if (win) count++;
return count;
}
public int CompareTo(KeyCommand other)
{
if (ModifierCount() == other.ModifierCount()) return 0;
return (ModifierCount() > other.ModifierCount()) ? -1 : 1;
}
public override string ToString()
{
string id = string.Empty;
if (ctrl) id += "CTRL+";
if (shift) id += "SHIFT+";
if (alt) id += "ALT+";
if (win) id += "WIN+";
id += Enum.GetName(typeof(KeyCode), key);
return id;
}
}
[Serializable]
public class ButtonKeyCommand : KeyCommand
{
[Header("Action")]
public UnityEngine.UI.Button button;
public override void Invoke()
{
if (button == null) return;
base.Invoke();
if (button != null && button.interactable)
{
button.onClick.Invoke();
}
}
public override string ToString()
{
return $"{base.ToString()}: [Button] {button.name}";
}
}
[Serializable]
public class ToggleKeyCommand : KeyCommand
{
[Header("Action")]
public UnityEngine.UI.Toggle toggle;
public override void Invoke()
{
if (toggle == null) return;
base.Invoke();
if (toggle != null && toggle.interactable)
{
toggle.isOn = !toggle.isOn;
}
}
public override string ToString()
{
return $"{base.ToString()}: [Toggle] {toggle.name}";
}
}
[Serializable]
public class ActionKeyCommand : KeyCommand
{
public bool onKeyUp = false;
[Header("Action")]
public UnityEvent action;
public override void Invoke()
{
if (action == null) return;
base.Invoke();
action.Invoke();
}
public override string ToString()
{
string result = $"{base.ToString()}: [Events";
if (onKeyUp) result += " (On Key Up)";
result += "]";
int eventCount = action.GetPersistentEventCount();
if (eventCount < 1) return result;
result += $"\n";
for (int i = 0; i < eventCount; i++)
{
result += $"{action.GetPersistentTarget(i)}.{action.GetPersistentMethodName(i)}";
if (i < eventCount - 1) result += "\n";
}
return result;
}
}
[Serializable]
public class HierarchyToggles : KeyCommand
{
[Header("Action")]
public GameObject gameObject;
public override void Invoke()
{
if (gameObject == null) return;
base.Invoke();
gameObject.ToggleActive();
}
public override string ToString()
{
return $"{base.ToString()}: [ToggleActive] {gameObject.name}";
}
}
}
}