-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controls.js
182 lines (163 loc) · 5.08 KB
/
Controls.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
// This file contains default Tetreml controls configuration.
// If you are looking to change the game controls, you can now do so in-game.
// Singleplayer controls.
const singleplayerControlsMapping = {
left: "KeyA",
right: "KeyD",
softDrop: "KeyS",
hardDrop: "KeyW",
rotateCounterClockwise: "KeyT",
rotateClockwise: "KeyY",
hold: "Space",
reset: "KeyR",
esc: "Escape",
quitModifier: "KeyP",
volumeDown: "Minus",
volumeUp: "Equal"
};
// Two-player controls.
// Player 1. This player's controls also contain pause, quit and volume keys for the whole game.
const twoPlayerControlsMappingPlayer1 = {
left: "KeyA",
right: "KeyD",
softDrop: "KeyS",
hardDrop: "KeyW",
rotateCounterClockwise: "KeyT",
rotateClockwise: "KeyY",
hold: "Space",
esc: "Escape",
quitModifier: "KeyP",
volumeDown: "Digit6",
volumeUp: "Digit7"
};
// Player 2.
// With numpad.
const twoPlayerControlsMappingPlayer2WithNumpad = {
left: "ArrowLeft",
right: "ArrowRight",
softDrop: "ArrowDown",
hardDrop: "ArrowUp",
rotateCounterClockwise: "Numpad7",
rotateClockwise: "Numpad8",
hold: "Numpad9"
};
// Without numpad.
const twoPlayerControlsMappingPlayer2WithoutNumpad = {
left: "KeyK",
right: "Semicolon",
softDrop: "KeyL",
hardDrop: "KeyO",
rotateCounterClockwise: "BracketLeft",
rotateClockwise: "BracketRight",
hold: "Backslash"
};
// ---------------
function copyControlsList(controlsList) {
let res = [];
for (let subList of controlsList) {
let newSubList = [subList[0]];
for (let i = 1; i < subList.length; i++) newSubList.push([...subList[i]]);
res.push(newSubList);
}
return res;
}
class ControlsEditScreen {
constructor(parent, controlsList, saveCallback) {
this.parent = parent;
/*
Each element of controlsList is a two-element array:
– If the first element is null, it is a header with the label as the second element.
– Otherwise, it is a control with label as the first element and the currently configured value as the second.
*/
this.originalControlsList = controlsList;
this.saveCallback = saveCallback;
}
init() {
this.controlsList = copyControlsList(this.originalControlsList);
this.selectedControl = 0;
this.selectedSection = 0;
this.editingTime = 0;
document.addEventListener("keydown", this.keyDownHandler = (key) => { this.onKeyDown(key.code); });
}
onKeyDown(keycode) {
if (this.editingTime) {
this.currentControlsSublist[this.selectedControl+1][1] = keycode;
this.editingTime = 0;
} else switch (keycode) {
case "ArrowRight":
this.selectedSection = (this.selectedSection + 1) % this.controlsList.length;
this.selectedControl = 0;
break;
case "ArrowLeft":
this.selectedSection = (this.selectedSection + this.controlsList.length - 1) % this.controlsList.length;
this.selectedControl = 0;
break;
case "ArrowDown":
this.selectedControl = (this.selectedControl + 1) % (this.currentControlsSublist.length - 1);
break;
case "ArrowUp":
this.selectedControl = (this.selectedControl + this.currentControlsSublist.length - 2) % (this.currentControlsSublist.length - 1);
break;
case "Enter":
this.editingTime = 1000;
break;
case "KeyR":
this.controlsList = copyControlsList(this.originalControlsList);
break;
case "Escape":
this.saveCallback(this.controlsList);
goBack();
break;
}
}
get currentControlsSublist() {
return this.controlsList[this.selectedSection];
}
render() {
let timePassed = 0;
if (this.oldTime == null) {
this.oldTime = new Date().getTime();
return;
} else {
let currentTime = new Date().getTime();
timePassed = currentTime - this.oldTime;
this.oldTime = currentTime;
}
if (this.editingTime) {
this.editingTime = Math.max(0, this.editingTime - timePassed);
if (!this.editingTime) this.currentControlsSublist[this.selectedControl+1][1] = null;
}
ctx.fillStyle = "#FFF";
ctx.font = "300 40px Segoe UI";
ctx.textAlign = "left";
ctx.fillText("Edit controls", 15, 50);
ctx.font = "12px Segoe UI";
if (this.controlsList.length > 1) {
ctx.textAlign = "center";
ctx.fillText(this.currentControlsSublist[0], 475, 50);
ctx.textAlign = "left";
ctx.fillText("\u25c4", 360, 50);
ctx.textAlign = "right";
ctx.fillText("\u25ba", 590, 50);
}
ctx.strokeStyle = "#FFF";
ctx.lineWidth = 1;
ctx.strokeRect(70.5, 74.5 + 25 * this.selectedControl, 499, 24);
if (this.editingTime) {
ctx.globalAlpha = 0.3;
ctx.fillRect(71, 75 + 25 * this.selectedControl, 0.498 * this.editingTime, 23);
ctx.globalAlpha = 1;
}
ctx.textAlign = "left";
for (let i = 1; i < this.currentControlsSublist.length; i++)
ctx.fillText(this.currentControlsSublist[i][0], 150, 65 + 25 * i);
ctx.textAlign = "right";
for (let i = 1; i < this.currentControlsSublist.length; i++)
ctx.fillText(formatKeycode(this.currentControlsSublist[i][1]), 490, 65 + 25 * i);
ctx.textAlign = "center";
ctx.fillText(this.editingTime ? "Press a key to set or wait to unset." : "Press Enter to configure this control; Esc to finish or R to revert all current changes.", 320, 340);
}
close() {
document.removeEventListener("keydown", this.keyDownHandler);
}
}