-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrols.js
149 lines (140 loc) · 3.93 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
export default function Controls(arithmetic, canvas, properties) {
let keySequence = [];
let gradiusInterval;
setupEventListeners();
function registerKey(key) {
keySequence.push(key);
}
function resetKeySequence() {
keySequence = [];
}
function gradius() {
const r1 = Math.random();
const r2 = Math.random();
const r3 = Math.random();
const r4 = Math.random();
const r5 = Math.random();
const r6 = Math.random();
const x = properties.canvasWidth * r1;
const y = properties.canvasHeight * r2;
canvas.font = `${r3 * 60}px arial`;
canvas.fillStyle = `#${Math.round(r4 * 255).toString(16)}${Math.round(r5 * 255).toString(16)}${Math.round(r6 * 256).toString(16)}`;
canvas.fillText([78, 69, 82, 68].map((n) => String.fromCharCode(n)).join(""), x, y);
}
function setupEventListeners() {
const preventDefaultKeys = [
"Tab",
" ",
"ArrowUp",
"ArrowDown",
"ArrowLeft",
"ArrowRight",
];
window.addEventListener("keydown", (event) => {
if (preventDefaultKeys.includes(event.key)) {
event.preventDefault();
}
switch (event.key) {
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
arithmetic.setOperand(event.key);
registerKey("number");
break;
case "Tab":
arithmetic.changeOperand();
registerKey("tab");
break;
case "Enter":
arithmetic.swapOperands();
registerKey("enter");
break;
case " ":
arithmetic.switchOperation();
registerKey("space");
break;
case "+":
arithmetic.setOperation("+");
registerKey("operation");
break;
case "-":
arithmetic.setOperation("−");
registerKey("operation");
break;
case "*":
arithmetic.setOperation("×");
registerKey("operation");
break;
case "/":
arithmetic.setOperation("÷");
registerKey("operation");
break;
case "ArrowUp":
arithmetic.increaseActiveOperand();
registerKey("up");
break;
case "ArrowDown":
arithmetic.decreaseActiveOperand();
registerKey("down");
break;
case "ArrowLeft":
arithmetic.changeOperand("left");
registerKey("left");
break;
case "ArrowRight":
arithmetic.changeOperand("right");
registerKey("right");
break;
case "a":
registerKey("a");
break;
case "b":
registerKey("b");
break;
case "r":
arithmetic.randomize();
break;
case ":":
arithmetic.toggleVisualization();
break;
default:
console.log("Unused key: %s", event.key); // eslint-disable-line no-console
}
// Look for key sequence
const currentKeySequence = keySequence.join(">");
switch (currentKeySequence) {
case "number":
break;
case "number>operation":
if (arithmetic.activeOperand === "left") {
arithmetic.changeOperand();
} else {
resetKeySequence();
}
break;
case "number>operation>number":
if (arithmetic.activeOperand === "right") {
arithmetic.changeOperand();
}
resetKeySequence();
break;
case "up>up>down>down>left>right>left>right>b>a":
gradiusInterval = setInterval(gradius.bind(canvas), 0);
resetKeySequence();
break;
default:
clearInterval(gradiusInterval);
if (!"up>up>down>down>left>right>left>right>b>a".startsWith(currentKeySequence)) {
resetKeySequence();
}
}
});
}
}