forked from simonlc/tetr.js
-
Notifications
You must be signed in to change notification settings - Fork 27
/
menu.js
335 lines (323 loc) · 7.48 KB
/
menu.js
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
var version = '0.62';
var setLoop;
var arrowReleased = true;
var arrowDelay = 0;
var key = {
8: 'Backspace',
9: 'Tab',
13: 'Enter',
16: 'Shift',
17: 'Ctrl',
18: 'Alt',
19: 'Pause',
20: 'Caps Lock',
27: 'Esc',
32: 'Space',
33: 'PgUp',
34: 'PgDn',
35: 'End',
36: 'Home',
37: '←',
38: '↑',
39: '→',
40: '↓',
45: 'Insert',
46: 'Delete',
48: '0',
49: '1',
50: '2',
51: '3',
52: '4',
53: '5',
54: '6',
55: '7',
56: '8',
57: '9',
59: ';',
61: '=',
65: 'A',
66: 'B',
67: 'C',
68: 'D',
69: 'E',
70: 'F',
71: 'G',
72: 'H',
73: 'I',
74: 'J',
75: 'K',
76: 'L',
77: 'M',
78: 'N',
79: 'O',
80: 'P',
81: 'Q',
82: 'R',
83: 'S',
84: 'T',
85: 'U',
86: 'V',
87: 'W',
88: 'X',
89: 'Y',
90: 'Z',
96: '0kpad',
97: '1kpad',
98: '2kpad',
99: '3kpad',
100: '4kpad',
101: '5kpad',
102: '6kpad',
103: '7kpad',
104: '8kpad',
105: '9kpad',
106: '*',
107: '+',
109: '-',
110: '.',
111: '/',
112: 'F1',
113: 'F2',
114: 'F3',
115: 'F4',
116: 'F5',
117: 'F6',
118: 'F7',
119: 'F8',
120: 'F9',
121: 'F10',
122: 'F11',
123: 'F12',
173: '-',
186: ';',
187: '=',
188: ',',
189: '-',
190: '.',
191: '/',
192: '`',
219: '[',
220: '\\',
221: ']',
222: "'",
undefined: "---",
0: "---"
};
/**
* Show and hide menus.
*/
var menus = document.getElementsByClassName('menu');
var menuStack = [];
function menu(menuIndex, stackOper) {
var current = void 0;
for (var i = 0, len = menus.length; i < len; i++) {
if (menus[i].classList.contains('on')) {
current = i;
}
menus[i].classList.remove('on');
}
if (menuIndex !== void 0) {
menus[menuIndex].classList.add('on');
}
if (stackOper === 1) {
if (current !== void 0) {
menuStack.push(current);
}
} else if (stackOper === -1) {
current = menuStack.pop();
if ((current !== void 0) && (menuIndex === void 0)) {
menus[current].classList.add('on');
}
} else if (stackOper !== 0) {
menuStack = [];
}
}
/**
* Controls Menu
*/
var newKey, currCell, tempKey, controls = document.getElementById('controls'), controlCells = controls.getElementsByTagName('td');
// Give controls an event listener.
for (var i = 0, len = controlCells.length; i < len; i++) {
controlCells[i].onclick = function () {
// First check if we're already waiting for an input.
if (currCell) {
// TODO DRY
// Make this into a function and call it when we press Esc.
binds[currCell.id] = tempKey;
$setText(currCell, key[tempKey] || tempKey);
}
tempKey = binds[this.id];
$setText(this, 'Press key');
currCell = this;
}
}
// Listen for key input if a control has been clicked on.
addEventListener('keyup', function (e) {
// if click outside of cell or press esc clear currCell
// reset binds button.
if (currCell) {
var newKey=e.keyCode;
if(newKey===8){
newKey=void 0;
}
// Checks if key already in use, and unbinds it.
if(newKey){
for (var i in binds) {
if (newKey === binds[i]) {
binds[i] = void 0;
$setText($$(i), key[void 0]);
}
}
}
// Binds the key and saves the data.
binds[currCell.id] = newKey;
$setText(currCell, key[newKey] || newKey);
localStorage.setItem('binds', JSON.stringify(binds));
currCell = 0;
}
}, false);
/**
* Settings Menu
*/
function settingsLoop() {
if (arrowReleased || arrowDelay >= 6) {
if (settingsArrow) {
mySettings[s] = (mySettings[s] === 0) ? setting[s].length - 1 : mySettings[s] - 1;
} else {
mySettings[s] = (mySettings[s] === setting[s].length - 1) ? 0 : mySettings[s] + 1;
}
saveSetting(s);
arrowReleased = false;
} else {
arrowDelay++;
}
setLoop = setTimeout(settingsLoop, 50);
}
var s;
var settingsArrow;
// TODO DRY this.
function arrowRelease(e) {
//resize();
arrowReleased = true;
arrowDelay = 0;
clearTimeout(setLoop);
this.onmouseup = void 0;
this.onmouseout = void 0;
this.ontouchend = void 0;
this.ontouchcancel = void 0;
if (e && e.preventDefault) {
e.preventDefault();
} //avoid selection by touch
}
function left(e) {
settingsArrow = 1;
s = this.parentNode.id;
this.onmouseup = arrowRelease;
this.onmouseout = arrowRelease;
this.ontouchend = arrowRelease;
this.ontouchcancel = arrowRelease;
if (e && e.preventDefault) {
e.preventDefault();
} //avoid selection by touch
settingsLoop();
}
function right(e) {
settingsArrow = 0;
s = this.parentNode.id;
this.onmouseup = arrowRelease;
this.onmouseout = arrowRelease;
this.ontouchend = arrowRelease;
this.ontouchcancel = arrowRelease;
if (e && e.preventDefault) {
e.preventDefault();
} //avoid selection by touch
settingsLoop();
}
/**
* LocalStorage functions
*/
function saveSetting(s) {
if (localStorage === void 0) {
localStorage = {};
}
localStorage['version'] = version;
$setText($$(s).getElementsByTagName('span')[0], setting[s][mySettings[s]]);
localStorage['settings'] = JSON.stringify(mySettings);
}
function loadLocalData() {
if (localStorage === void 0) {
localStorage = {};
}
if (localStorage['binds']) {
binds = JSON.parse(localStorage.getItem('binds'));
for (var i = 0, len = controlCells.length; i < len; i++) {
var keycode=binds[controlCells[i].id];
$setText(controlCells[i], key[keycode] || keycode);
}
}else{
$$("btnbinds").classList.add("highlight");
}
// TODO When new version just update with new stuff, rest stays unchanged.
var storedSettings;
if (localStorage['settings']) {
storedSettings = JSON.parse(localStorage.getItem('settings'));
if (localStorage['version'] !== version) {
try{
tryUpgradeSetting(storedSetting);
localStorage['settings'] = JSON.stringify(mySettings);
}catch(e){
localStorage.removeItem('settings');
}
}
}
if (localStorage['settings']) {
for (var i in mySettings) {
if (i in storedSettings) {
mySettings[i] = storedSettings[i];
}
}
}
}
loadLocalData();
for (var s in settingName) {
var div = document.createElement('div');
var sname = document.createElement('b');
var iLeft = document.createElement('i');
var span = document.createElement('span');
var iRight = document.createElement('i');
div.id = s;
$setText(sname, settingName[s]);
$setText(span, setting[s][mySettings[s]]);
iLeft.className = 'material-icons left';
iRight.className = 'material-icons right';
$setText(iLeft, "\uE314");
$setText(iRight, "\uE315");
iLeft.onmousedown = left;
iLeft.ontouchstart = left;
iRight.onmousedown = right;
iRight.ontouchstart = right;
set.appendChild(div);
div.appendChild(sname);
div.appendChild(iLeft);
div.appendChild(span);
div.appendChild(iRight);
}
function renameTransfer(obj, oldName, newName, f){
if(obj[oldName] === void 0 && obj[newName] === void 0){
throw oldName+","+newName;
}
if(obj[oldName] !== void 0 && obj[newName] === void 0){
obj[newName] = f?f(obj[oldName]):obj[oldName];
delete obj[oldName];
}
}
function tryUpgradeSetting(sett){
renameTransfer(sett, 'Lock Delay', "LockDelay");
renameTransfer(sett, 'Soft Drop', "SoftDrop", function(oldv){
if(oldv === 7){
return gravityArr.length - 1;
}else{
return oldv + 1;
}
});
}