forked from tao-yu/Alg-Trainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.html
193 lines (157 loc) · 4.51 KB
/
controls.html
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
<!DOCTYPE html>
<html>
<head>
<title>Algorithm trainer</title>
<meta charset="utf-8">
<link rel="icon" type="image/png" href="favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="favicon-16x16.png" sizes="16x16" />
<link rel="stylesheet" type="text/css" href="style.css">
<style>
td {
width: 8em;
padding: 0.4em;
}
tr {
border: 1px solid #888888;
}
td:nth-child(1) {
text-align: right;
border-right: 1px solid #888888;
}
input {
width: 100%;
box-sizing: border-box;
}
#changed {
visibility: hidden;
padding: 1em;
}
</style>
</head>
<body>
<h2> Control Config </h2>
<p>
Click a key binding to change it! The "Move" column specifies the move(s) to make upon that
key combo.
</p>
<div id="changed">
<button id="save">Save!</button>
<button id="undo">Go back!</button>
</div>
<table id="currentKeymap">
<tr>
<th>Key</th>
<th>Move</th>
<th></th>
</tr>
<tbody id="keymapsbody">
</tbody>
</table>
<br />
<button id="add-new"> Add another! </button>
<br />
<br />
<button id="reset"> Reset to default! </button>
</body>
<script src="js/controls.js"></script>
<script src="js/keymaps.js"></script>
<script>
let keymapsBody = document.getElementById("keymapsbody");
let changedDiv = document.getElementById("changed");
let currentKeymap = getKeyMaps();
let isChanging = false;
function notifyChanged() {
if (JSON.stringify(currentKeymap) === JSON.stringify(getKeyMaps())) {
changedDiv.style.visibility = "hidden";
} else {
changedDiv.style.visibility = "visible";
}
}
function editField(i) {
let [k, v] = currentKeymap[i];
let tr = keymapsBody.children[i];
let key = tr.children[0];
let move = tr.children[1];
let buttonsTd = tr.children[2];
let newKc = k;
if (isChanging) {
rerender(); return;
}
isChanging = true;
key.innerHTML = "";
let keyField = document.createElement("input");
keyField.value = k.toString();
key.appendChild(keyField);
keyField.addEventListener("keydown", e => {
e.preventDefault();
let kc = keyEventToKeyCombo(e, true);
newKc = keyEventToKeyCombo(e, false) || newKc;
keyField.value = kc.toString();
});
move.innerHTML = "";
let moveField = document.createElement("input");
moveField.value = v;
move.appendChild(moveField);
buttonsTd.innerHTML = "";
let escapeButton = document.createElement("button");
escapeButton.innerText = "❌";
escapeButton.onclick = _e => {
rerender();
};
buttonsTd.appendChild(escapeButton);
let acceptButton = document.createElement("button");
acceptButton.innerText = "✅";
acceptButton.onclick = _e => {
currentKeymap[i] = [newKc, moveField.value];
rerender();
};
buttonsTd.appendChild(acceptButton);
let removeButton = document.createElement("button");
removeButton.innerText = "🗑";
removeButton.onclick = _e => {
currentKeymap.splice(i, 1);
rerender();
};
buttonsTd.appendChild(removeButton);
tr.onclick = null;
}
function rerender(quiet=false) {
if (!quiet) {
notifyChanged();
}
keymapsBody.innerHTML = "";
isChanging = false;
for (let i = 0; i < currentKeymap.length; i++) {
let [k, v] = currentKeymap[i];
let key = document.createElement("td");
key.innerText = k.toString();
let move = document.createElement("td");
move.innerText = v;
let tr = document.createElement("tr");
tr.appendChild(key);
tr.appendChild(move);
tr.appendChild(document.createElement("td"));
keymapsBody.appendChild(tr);
tr.onclick = _e => editField(i);
}
}
document.getElementById("add-new").onclick = _ => {
currentKeymap.push([new KeyCombo(""), "alg"]);
rerender(true);
editField(currentKeymap.length - 1);
};
document.getElementById("reset").onclick = _ => {
currentKeymap = defaultKeymaps;
rerender(false);
};
document.getElementById("save").onclick = _ => {
localStorage.setItem("keymaps", JSON.stringify(currentKeymap));
rerender();
}
document.getElementById("undo").onclick = _ => {
currentKeymap = getKeyMaps();
rerender();
};
rerender();
</script>
</html>