-
Notifications
You must be signed in to change notification settings - Fork 6
/
BindingsShared.lua
543 lines (460 loc) · 11.5 KB
/
BindingsShared.lua
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
--[[
Notes
When a bind has its key clear/unbound we do Main.SetOptionBoolean("ns2lua/UnboundBinds/"BindName) because some of engine keybinds resets to defaults
if we set there value to an empty string
Public functions:
string:KeyName GetBoundKey(BindName)
bool IsKeyBound(string KeyName)
void SetKeybind(string KeyName, string BindName)
void UnbindKey(string KeyName)
void ClearBind(string BindName)
]]--
InputKeyNames = {
"Escape",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"0",
"Minus",
"Equals",
"Back",
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"LeftBracket",
"RightBracket",
"Return",
"LeftControl",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
"Semicolon",
"Apostrophe",
"Grave",
"LeftShift",
"Backslash",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
"Comma",
"Period",
"Slash",
"RightShift",
"Multiply",
"LeftAlt",
"Space",
"Capital",
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"NumLock",
"Scroll",
"NumPad7",
"NumPad8",
"NumPad9",
"Subtract",
"NumPad4",
"NumPad5",
"NumPad6",
"Add",
"NumPad1",
"NumPad2",
"NumPad3",
"NumPad0",
"Decimal",
"F11",
"F12",
"F13",
"F14",
"F15",
"NumPadEquals",
"NumPadEnter",
"RightControl",
"Divide",
"PrintScreen",
"RightAlt",
"Pause",
"Home",
"Up",
"PageUp",
"Left",
"Right",
"End",
"Down",
"PageDown",
"Insert",
"Delete",
"LeftWindows",
"RightWindows",
"AppMenu",
"MouseX",
"MouseY",
"MouseZ", --Scroll Wheel
"MouseButton0",
"MouseButton1",
"MouseButton2",
"MouseButton3",
"MouseButton4",
}
InputEnum = {
"PrimaryAttack",
"SecondaryAttack",
"NextWeapon",
"PrevWeapon",
"Reload",
"Use",
"Jump",
"Crouch",
"MovementModifier",
"Minimap",
"Buy",
"ToggleFlashlight",
"Weapon1",
"Weapon2",
"Weapon3",
"Weapon4",
"Weapon5",
"ScrollBackward",
"ScrollRight",
"ScrollLeft",
"ScrollForward",
"Exit",
"Drop",
"Taunt",
"Scoreboard",
"ToggleSayings1",
"ToggleSayings2",
"TeamChat",
"TextChat",
}
KeyBindInfo = {
Loaded = false,
KeybindEntrys = {},
RegisteredKeybinds = {},
KeybindNameToKey = {},
BoundKeys = {}, --stores the maping of a key to a keybindname
KeybindGroups = {},
LogLevel = 1,
}
KeyBindInfo.MovementKeybinds = {
BindDefaultKeys = true,
Description = "Movement",
Keybinds = {
{"MoveForward", "Move forward", "W"},
{"MoveBackward", "Move Backward", "S"},
{"MoveLeft", "Move Left", "A"},
{"MoveRight", "Move Right", "D"},
{"Jump", "Jump", "Space"},
{"MovementModifier", "Movement special", "LeftShift"},
{"Crouch", "Crouch", "LeftControl"},
}
}
KeyBindInfo.ActionKeybinds = {
BindDefaultKeys = true,
Description = "Misc",
Keybinds = {
{"PrimaryFire", "Primary attack", "MouseButton0"},
{"SecondaryFire", "Secondary attack", "MouseButton1"},
{"Reload", "Reload", "R"},
{"Drop", "Drop weapon", "G"},
{"ToggleFlashlight", "Toggle Flashlight", "F"},
{"Buy", "Buy", "T"},
{"Use", "Use", "E"},
{"Taunt", "Taunt", "Z"},
{"VoiceChat", "Use microphone", "F9"},
{"ToggleSayings", "Voice menu", "X"},
{"TextChat", "Public chat", "Y"},
{"TeamChat", "Team chat", "U"},
{"Weapon1", "Weapon #1", "1"},
{"Weapon2", "Weapon #2", "2"},
{"Weapon3", "Weapon #3", "3"},
{"Weapon4", "Weapon #4", "4"},
{"Weapon5", "Weapon #5", "5"},
}
}
KeyBindInfo.MiscKeybinds = {
BindDefaultKeys = true,
Description = "Misc",
Keybinds = {
{"ToggleThirdPerson", "Toggle Third Person View", ""},
{"JoinMarines", "Join Marines", "F1"},
{"JoinAliens", "Join Aliens", "F2"},
{"JoinRandom", "Join Random Team", "F3"},
{"ReadyRoom" , "Return to Ready Room", "F4"},
{"ToggleConsole", "Open Console", "Grave"},
{"Exit", "Open Main Menu", "Escape"},
}
}
KeyBindInfo.HiddenKeybinds = {
Hidden = true,
Keybinds = {
{"ActivateSteamworksOverlay"},
{"LockViewFrustum"},
{"LockViewPoint"},
{"ToggleDebugging"},
}
}
KeyBindInfo.EngineProcessed = {
ToggleConsole = true,
ActivateSteamworksOverlay = true,
Voice = true,
LockViewFrustum = true,
LockViewPoint = true,
ToggleDebugging = true,
}
function KeyBindInfo:Init()
if(not self.Loaded) then
self:ReloadKeyBindInfo()
end
end
function KeyBindInfo:MainVMLazyLoad()
if(not self.Loaded and not self.LazyLoad) then
self:AddDefaultKeybindGroups()
self.LazyLoad = true
end
end
function KeyBindInfo:ReloadKeyBindInfo()
table.clear(self.KeybindEntrys)
table.clear(self.KeybindGroups)
table.clear(self.RegisteredKeybinds)
table.clear(self.KeybindNameToKey)
table.clear(self.BoundKeys)
self:AddDefaultKeybindGroups()
self:LoadAndValidateSavedKeyBinds()
self.Loaded = true
self.LazyLoad = nil
end
function KeyBindInfo:AddDefaultKeybindGroups()
self:AddKeybindGroup(self.MovementKeybinds)
self:AddKeybindGroup(self.ActionKeybinds)
self:AddKeybindGroup(self.MiscKeybinds)
self:AddKeybindGroup(self.HiddenKeybinds)
end
--
function KeyBindInfo:AddKeybindGroup(keybindGroup)
table.insert(self.KeybindGroups, keybindGroup)
for _,keybind in ipairs(keybindGroup.Keybinds) do
self.RegisteredKeybinds[keybind[1]] = keybind
self.KeybindEntrys[#self.KeybindEntrys+1] = keybind
end
end
function KeyBindInfo:LoadAndValidateSavedKeyBinds()
for _,bindinfo in ipairs(self.KeybindEntrys) do
local key = Main.GetOptionString("input/"..bindinfo[1], "")
if(key ~= "" and not Main.GetOptionBoolean("ns2lua/UnboundBinds/"..bindinfo[1], false)) then
if(self:IsKeyBound(key)) then
self:Log(1, string.format("ignoreing \"%s\" bind because \"%s\" is alreay bound to the same key which is \"%s\"", bindinfo[1], self.BoundKeys[key], key), 2 )
else
self:InternalBindKey(key, bindinfo[1])
end
end
end
local keybindversion = Main.GetOptionString("ns2lua/KeybindVersion", "")
if(keybindversion == "") then
self:FixBinds()
Main.SetOptionString("ns2lua/KeybindVersion", "1")
end
--add any keybinds where they default key is currently unbound and which have BindDefautKeys set to true for there keybind group
for _,bindgroup in ipairs(self.KeybindGroups) do
if(bindgroup.BindDefaultKeys) then
for _,bind in ipairs(bindgroup.Keybinds) do
if(not self:GetBoundKey(bind[1]) and not self:IsKeyBound(bind[3])) then
self:InternalBindKey(bind[3], bind[1])
end
end
end
end
end
--{BindName, Engine default key or false to always unbind this bind, new key or false to use the DefaultKey for the keybind}
local BindFixs = {
--do TeamChat before TextChat so we free up Y for TextChat
{"TeamChat", "Y", false},
{"TextChat", "Return", false},
--shift these 2 to diffent F keys so we can use the F keys for randomteam and readyroom
{"ActivateSteamworksOverlay","F3", "F10"},
{"LockViewFrustum", "F4", "F11"},
}
function KeyBindInfo:FixBinds()
for _,KeyInfo in ipairs(BindFixs) do
local bindName = KeyInfo[1]
local currentKey = self:GetBoundKey(bindName)
if(KeyInfo[2]) then
if(not currentKey or currentKey == KeyInfo[2]) then
local newkey = KeyInfo[3] or self.RegisteredKeybinds[bindName][3]
if(not self:IsKeyBound(newkey)) then
self:SetKeybind(newkey, bindName)
end
end
else
if(currentKey) then
self:UnbindKey(currentKey)
end
end
end
end
function KeyBindInfo:RegisterKeyBindGroup(label, keybinds)
if(self.Loaded) then
self:AddKeybindGroup(label, keybinds)
end
end
function KeyBindInfo:GetBindingDialogTable()
if(not self.Loaded and not self.LazyLoad) then
self:MainVMLazyLoad()
end
if(not self.BindingDialogTable) then
local bindTable = {}
local index = 1
for _,bindgroup in ipairs(self.KeybindGroups) do
if(not bindgroup.Hidden) then
bindTable[index] = bindgroup.Description
bindTable[index+1] = "title"
bindTable[index+2] = bindgroup.Description
bindTable[index+3] = ""
index = index+4
for _,bind in ipairs(bindgroup.Keybinds) do
bindTable[index] = bind[1]
bindTable[index+1] = "input"
bindTable[index+2] = bind[2]
bindTable[index+3] = bind[3]
index = index+4
end
end
end
self.BindingDialogTable = bindTable
end
return self.BindingDialogTable
end
function KeyBindInfo:GetBoundKey(keybindname)
if(self.RegisteredKeybinds[keybindname] == nil) then
error("GetBoundKey: keybind called \""..keybindname.."\" does not exist")
end
return self.KeybindNameToKey[keybindname]
end
function KeyBindInfo:IsKeyBound(key)
return self.BoundKeys[key] ~= nil
end
function KeyBindInfo:SetKeybind(key, bindname, dontSave)
if(self.RegisteredKeybinds[bindname] == nil and not self.EngineProcessed[bindname]) then
error("SetKeyBind: keybind called \""..bindname.."\" does not exist")
end
--if keybind had a key already set clear the record of it in our BoundKeys table
if(self.KeybindNameToKey[bindname]) then
self.BoundKeys[self.KeybindNameToKey[bindname]] = nil
end
if(self.BoundKeys[key]) then
self:UnbindKey(key)
end
self:InternalBindKey(key, bindname)
if(not dontSave) then
Main.SetOptionString("ns2lua/UnboundBinds/"..bindname, nil)
Main.SetOptionString("input/"..bindname, key)
end
end
function KeyBindInfo:UnbindKey(key, dontSave)
if(self.BoundKeys[key] == nil) then
self:Log(1, "\""..key.."\" is already unbound")
return
end
local bindName = self.BoundKeys[key]
if(not dontSave) then
Main.SetOptionString("input/"..bindName, "")
Main.SetOptionBoolean("ns2lua/UnboundBinds/"..bindName, true)
end
self.KeybindNameToKey[bindName] = nil
self.BoundKeys[key] = nil
end
function KeyBindInfo:ClearBind(bindname)
Main.SetOptionString("input/"..bindName, "")
Main.SetOptionBoolean("ns2lua/UnboundBinds/"..bindName, true)
if(self.KeybindNameToKey[bindName] == nil) then
self:Log(1, "\""..bindname.."\" is already unbound")
else
local key = self.KeybindNameToKey[bindName]
self.KeybindNameToKey[bindName] = nil
self.BoundKeys[key] = nil
end
end
function KeyBindInfo:CheckKeybindChange(bindName)
local newkey = Main.GetOptionString("input/"..bindName, "")
local oldkey = self.KeybindNameToKey[bindName]
if(newkey == "") then
newkey = nil
end
local ChangeType = false
if(newkey ~= oldkey) then
if(oldkey) then
if(newkey) then
ChangeType = "ReBind"
else
ChangeType = "ClearBind"
end
else
ChangeType = "SetBind"
end
end
self:SetKeybind(bindName, newkey, true)
return ChangeType, newkey, oldkey
end
function KeyBindInfo:CheckKeyBindsLoaded()
--check if we populated the table already
if(next(self.BoundKeys) ~= nil) then
return
else
self:LoadAndValidateSavedKeyBinds()
end
end
function KeyBindInfo:InternalBindKey(key, bindname)
self.BoundKeys[key] = bindname
self.KeybindNameToKey[bindname] = key
end
function KeyBindInfo:ResetGroupToDefaults()
end
function KeyBindInfo:ResetKeybindsToDefaults()
for _,bindgroup in ipairs(self.KeybindGroups) do
for _,bind in ipairs(bindgroup.Keybinds) do
Main.SetOptionString("input/"..bind[1], bind[3])
self:InternalBindKey(bind[3], bind[1])
end
end
end
function KeyBindInfo:Log(level, msg)
if(level > self.LogLevel) then
return
end
if(Shared) then
Shared.Message(msg)
else
print(msg)
end
end