-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeypanel.js
More file actions
277 lines (229 loc) · 7.53 KB
/
keypanel.js
File metadata and controls
277 lines (229 loc) · 7.53 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
let g_current_selected = null;
let g_set_binds = {
"w": [ "+forward", "+forward" ],
"a": [ "+moveleft", "+moveleft" ],
"s": [ "+back", "+back" ],
"d": [ "+moveright", "+moveright" ],
"e": [ "+use", "+use" ],
"ctrl": [ "+duck", "+duck" ],
"`": [ "toggleconsole", "toggleconsole" ],
"mouse1": [ "+attack", "+attack" ],
"mouse2": [ "+attack2", "+attack2" ],
};
const g_default_binds = JSON.parse(JSON.stringify(g_set_binds));
const BindMode = {
ALL: 0,
CM: 1,
NON_CM: 2,
};
let g_bind_mode = BindMode.ALL; // Fuck you JS for not having proper enums
function getBinding(key) {
if (key === null) return [ null, null ];
if (g_set_binds.hasOwnProperty(key)) {
return g_set_binds[key];
} else {
return [ null, null ];
}
}
function setModeBinding(key, val) {
if (key === null) return;
if (!g_set_binds.hasOwnProperty(key)) g_set_binds[key] = [ null, null ];
if (g_bind_mode === BindMode.ALL || g_bind_mode === BindMode.CM) {
g_set_binds[key][0] = val;
}
if (g_bind_mode === BindMode.ALL || g_bind_mode === BindMode.NON_CM) {
g_set_binds[key][1] = val;
}
if (g_set_binds[key][0] === null && g_set_binds[key][1] === null) {
delete g_set_binds[key];
}
window.localStorage.setItem("keybinds", JSON.stringify(g_set_binds))
}
let keys = document.getElementsByClassName("key");
for (let i = 0; i < keys.length; ++i) {
let key_attr = keys[i].attributes.getNamedItem("p2key");
if (key_attr !== null) {
keys[i].classList.add("bindable");
// Add the key name to the title (hover text)
let title = keys[i].attributes.getNamedItem("title")?.value || "";
keys[i].setAttribute("title", `${title}\n${key_attr.value.toUpperCase()}`.trim());
keys[i].addEventListener("click", function() {
if (g_current_selected !== null) {
g_current_selected.classList.remove("selected");
}
g_current_selected = this;
this.classList.add("selected");
updateSelected();
});
}
}
updateBound();
g_bind_mode = BindMode.ALL;
updateSelected();
let actions = document.getElementsByName("key_action");
for (let i = 0; i < actions.length; ++i) {
actions[i].checked = false;
actions[i].addEventListener("change", function() {
if (this.checked) updateBind();
});
}
document.getElementById("bind_saveload_name").addEventListener("keydown", function(e) {
if (e.isComposing || e.keyCode == 229) return;
if (e.key == ' ' || e.key == '"' || e.key == ':' || e.key == '{' || e.key == '}' || e.key == ';') e.preventDefault();
});
document.getElementById("bind_saveload_name").addEventListener("input", function() {
updateBind();
});
document.getElementById("bindmode_all").addEventListener("change", function() {
if (this.checked) {
g_bind_mode = BindMode.ALL;
updateSelected();
}
});
document.getElementById("bindmode_cm").addEventListener("change", function() {
if (this.checked) {
g_bind_mode = BindMode.CM;
updateSelected();
}
});
document.getElementById("bindmode_non_cm").addEventListener("change", function() {
if (this.checked) {
g_bind_mode = BindMode.NON_CM;
updateSelected();
}
});
function updateSelected() {
document.getElementById("bindmode_all").checked = g_bind_mode === BindMode.ALL;
document.getElementById("bindmode_cm").checked = g_bind_mode === BindMode.CM;
document.getElementById("bindmode_non_cm").checked = g_bind_mode === BindMode.NON_CM;
let key_name =
g_current_selected === null ?
null :
g_current_selected.attributes.getNamedItem("p2key").value;
let active_bind_full = null;
switch (g_bind_mode) {
case BindMode.ALL:
if (getBinding(key_name)[0] === getBinding(key_name)[1]) {
active_bind_full = getBinding(key_name)[0];
}
break;
case BindMode.CM:
active_bind_full = getBinding(key_name)[0];
break;
case BindMode.NON_CM:
active_bind_full = getBinding(key_name)[1];
break;
}
let active_bind =
active_bind_full === null ?
null :
active_bind_full.split(" ")[0];
if (active_bind === "save" || active_bind === "load" || active_bind === "saveload") {
document.getElementById("bind_saveload_name").disabled = false
document.getElementById("bind_saveload_name").value = active_bind_full.split(" ")[1];
} else {
document.getElementById("bind_saveload_name").disabled = true;
document.getElementById("bind_saveload_name").value = "quick";
active_bind = active_bind_full;
}
let actions = document.getElementsByName("key_action");
for (let i = 0; i < actions.length; ++i) {
actions[i].disabled = key_name === null;
actions[i].checked = actions[i].value == active_bind;
}
}
function updateBind() {
let active_bind = null;
let actions = document.getElementsByName("key_action");
for (let i = 0; i < actions.length; ++i) {
if (actions[i].checked) {
active_bind = actions[i].value;
break;
}
}
let active_bind_full = null;
if (active_bind == "save" || active_bind == "load" || active_bind === "saveload") {
let save_name = document.getElementById("bind_saveload_name").value;
if (save_name === "") save_name = "quick";
active_bind_full = `${active_bind} ${save_name}`;
} else {
active_bind_full = active_bind;
}
if (active_bind === "save" || active_bind === "load" || active_bind === "saveload") {
document.getElementById("bind_saveload_name").disabled = false
} else {
document.getElementById("bind_saveload_name").disabled = true;
document.getElementById("bind_saveload_name").value = "quick";
}
let key_name =
g_current_selected === null ?
null :
g_current_selected.attributes.getNamedItem("p2key").value;
setModeBinding(key_name, active_bind_full);
updateBound();
}
function updateBound() {
for (let i = 0; i < keys.length; ++i) {
keys[i].classList.remove("bound");
let key_attr = keys[i].attributes.getNamedItem("p2key");
if (key_attr !== null) {
let binding = getBinding(key_attr.value);
if (binding[0] !== null || binding[1] !== null) keys[i].classList.add("bound");
}
}
}
document.getElementById("reset_bind").addEventListener("click", function() {
let key_name =
g_current_selected === null ?
null :
g_current_selected.attributes.getNamedItem("p2key").value;
setModeBinding(key_name, null);
updateBound();
updateSelected();
});
document.getElementById("reset_all").addEventListener("click", function() {
g_set_binds = JSON.parse(JSON.stringify(g_default_binds));
window.localStorage.clear();
updateBound();
updateSelected();
});
document.getElementById("gen_cfg").addEventListener("click", function() {
let file_str = 'bind "mwheelup" "+scrollup"\nbind "mwheeldown" "+scrolldown"\n';
for (const ent of Object.entries(g_set_binds)) {
const key = ent[0];
const cm = ent[1][0];
const non_cm = ent[1][1];
if (cm === non_cm && cm === null) continue; // backup to prevent null binds
let cmd =
cm === non_cm ?
cm :
cm !== null && non_cm !== null ?
`cm_only ${cm}; non_cm_only ${non_cm}` :
cm !== null ?
`cm_only ${cm}` :
non_cm !== null ?
`non_cm_only ${non_cm}` :
null;
file_str += `bind "${key}" "${cmd}"\n`;
}
file_str += "host_writeconfig\n";
file_str += "echo \"Binds configured! Have fun!\"\n";
const blob = new Blob([file_str]);
const data = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = data;
a.download = "init_binds.cfg";
a.click();
setTimeout(() => {
window.URL.revokeObjectURL(data);
a.remove();
}, 0);
});
document.addEventListener("DOMContentLoaded", function() {
let stored_binds = window.localStorage.getItem("keybinds");
if (stored_binds !== null) {
g_set_binds = JSON.parse(stored_binds);
}
updateBound();
updateSelected();
});