-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrols.js
More file actions
396 lines (360 loc) · 11.7 KB
/
controls.js
File metadata and controls
396 lines (360 loc) · 11.7 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
(() => {
// Global active keys set for momentary keys (arrow keys, A, B, Q, C)
window.activeKeys = new Set();
window.shapecount = 0;
// State for toggle keys to track when a key is held down
const toggleKeysDown = {};
window.isPaused = false;
// Emit custom controlInput event with provided details.
// The 'active' property indicates toggle status: true means on, false means off.
function emitControlInput({ direction = null, key = null, active = true }) {
document.dispatchEvent(
new CustomEvent("controlInput", { detail: { direction, key, active } })
);
}
window.emitControlInput = emitControlInput;
// -----------------------------------------------------------
// Momentary keys: arrow keys and A, B handled via pointer and keyboard.
function addPointerKeyHandlers(selector, keyCode, directionName) {
const activate = (e) => {
e.preventDefault();
if (!window.activeKeys.has(keyCode)) {
window.activeKeys.add(keyCode);
emitControlInput({
direction: directionName.toUpperCase(),
active: true,
});
}
$(selector).addClass("pressed").css("transform", "translate(0, 2px)");
$(`.${directionName}text`).text(directionName.toUpperCase());
};
const deactivate = (e) => {
e.preventDefault();
window.activeKeys.delete(keyCode);
emitControlInput({
direction: directionName.toUpperCase(),
active: false,
});
$(selector).removeClass("pressed").css("transform", "translate(0, 0)");
$(`.${directionName}text`).text("");
};
$(selector)
.on("pointerdown", activate)
.on("pointerup pointerleave", deactivate);
}
// Bind arrow key buttons
addPointerKeyHandlers(".up", 38, "up");
addPointerKeyHandlers(".down", 40, "down");
addPointerKeyHandlers(".left", 37, "left");
addPointerKeyHandlers(".right", 39, "right");
// Bind toggle control buttons: Q, F, and C
bindToggleButton(".shape-btn", 81, "Q");
bindToggleButtonWireframe(".wireframe-btn", 70, "F");
bindToggleButton(".color-btn", 67, "C");
// -----------------------------------------------------------
// Toggle buttons for control keys (Q, F, C) via pointer.
function bindToggleButton(selector, keyCode, keyChar) {
$(selector).on("pointerup", function (e) {
e.preventDefault();
// Toggle the button state
if ($(this).hasClass("active")) {
$(this).removeClass("active");
emitControlInput({ key: keyChar, active: false });
} else {
$(this).addClass("active");
emitControlInput({ key: keyChar, active: true });
$(this).removeClass("active");
}
});
}
function bindToggleButtonWireframe(selector, keyCode, keyChar) {
$(selector).on("pointerup", function (e) {
e.preventDefault();
// Toggle the button state
if ($(this).hasClass("active")) {
$(this).removeClass("active");
emitControlInput({ key: keyChar, active: false });
} else {
$(this).addClass("active");
emitControlInput({ key: keyChar, active: true });
// $(this).removeClass("active");
}
});
}
// -----------------------------------------------------------
// Shape selector dropdown change
$("#shape-list").on("change", (e) => {
const shape = e.target.value;
emitControlInput({ key: shape.toUpperCase() });
});
// -----------------------------------------------------------
// Keyboard support
function updatePauseButton() {
const $btn = $(".pause-toggle");
const $icon = $btn.find("ion-icon");
if (window.isPaused) {
$btn.addClass("green").removeClass("red");
$icon.attr("name", "play");
} else {
$btn.addClass("red").removeClass("green");
$icon.attr("name", "pause");
}
}
// Pointer click handler
$(".pause-toggle").on("pointerup", function (e) {
e.preventDefault();
window.isPaused = !window.isPaused;
emitControlInput({
key: "PAUSE",
active: window.isPaused,
});
updatePauseButton();
});
// Spacebar keyboard handler
$(document).on("keydown", function (e) {
if (e.code === "Space") {
e.preventDefault(); // Prevent page scrolling
window.isPaused = !window.isPaused;
emitControlInput({
key: "PAUSE",
active: window.isPaused,
});
updatePauseButton();
}
});
// Keyboard support
$(document)
.on("keydown", function (e) {
const keyCode = e.which;
// Handle momentary keys (arrow keys, A, B)
if ([37, 38, 39, 40, 65, 66].includes(keyCode)) {
if (!window.activeKeys.has(keyCode)) {
window.activeKeys.add(keyCode);
switch (keyCode) {
case 37:
emitControlInput({ direction: "LEFT", active: true });
$(".left")
.addClass("pressed")
.css("transform", "translate(0, 2px)");
$(".lefttext").text("LEFT");
break;
case 38:
emitControlInput({ direction: "UP", active: true });
$(".up")
.addClass("pressed")
.css("transform", "translate(0, 2px)");
$(".uptext").text("UP");
break;
case 39:
emitControlInput({ direction: "RIGHT", active: true });
$(".right")
.addClass("pressed")
.css("transform", "translate(0, 2px)");
$(".righttext").text("RIGHT");
break;
case 40:
emitControlInput({ direction: "DOWN", active: true });
$(".down")
.addClass("pressed")
.css("transform", "translate(0, 2px)");
$(".downtext").text("DOWN");
break;
case 65:
emitControlInput({ key: "A", active: true });
$(".a").text("A");
break;
case 66:
emitControlInput({ key: "B", active: true });
$(".b").text("B");
break;
}
}
}
// Handle Q and C as momentary keys (fixed for hold)
else if ([81, 67].includes(keyCode)) {
e.preventDefault();
if (!window.activeKeys.has(keyCode)) {
window.activeKeys.add(keyCode);
let selector, keyChar;
if (keyCode === 81) {
selector = ".shape-btn";
keyChar = "Q";
} else if (keyCode === 67) {
selector = ".color-btn";
keyChar = "C";
}
$(selector).addClass("active");
emitControlInput({ key: keyChar, active: true });
}
}
// Handle F as toggle
else if (keyCode === 70) {
e.preventDefault();
if (!toggleKeysDown[keyCode]) {
toggleKeysDown[keyCode] = true;
const selector = ".wireframe-btn";
const keyChar = "F";
if (!$(selector).hasClass("active")) {
$(selector).addClass("active");
emitControlInput({ key: keyChar, active: true });
} else {
$(selector).removeClass("active");
emitControlInput({ key: keyChar, active: false });
}
}
}
})
.on("keyup", function (e) {
const keyCode = e.which;
// Momentary keys release
if ([37, 38, 39, 40, 65, 66].includes(keyCode)) {
window.activeKeys.delete(keyCode);
switch (keyCode) {
case 37:
$(".left")
.removeClass("pressed")
.css("transform", "translate(0,0)");
$(".lefttext").text("");
emitControlInput({ direction: "LEFT", active: false });
break;
case 38:
$(".up").removeClass("pressed").css("transform", "translate(0,0)");
$(".uptext").text("");
emitControlInput({ direction: "UP", active: false });
break;
case 39:
$(".right")
.removeClass("pressed")
.css("transform", "translate(0,0)");
$(".righttext").text("");
emitControlInput({ direction: "RIGHT", active: false });
break;
case 40:
$(".down")
.removeClass("pressed")
.css("transform", "translate(0,0)");
$(".downtext").text("");
emitControlInput({ direction: "DOWN", active: false });
break;
case 65:
$(".a").text("");
emitControlInput({ key: "A", active: false });
break;
case 66:
$(".b").text("");
emitControlInput({ key: "B", active: false });
break;
}
}
// Clear toggle key state for F
if (keyCode === 70) {
toggleKeysDown[keyCode] = false;
}
// Q and C key release (momentary)
if ([81, 67].includes(keyCode)) {
window.activeKeys.delete(keyCode);
let selector, keyChar;
if (keyCode === 81) {
selector = ".shape-btn";
keyChar = "Q";
} else if (keyCode === 67) {
selector = ".color-btn";
keyChar = "C";
}
$(selector).removeClass("active");
emitControlInput({ key: keyChar, active: false });
}
});
})();
// Konami code with UI feedback
var Konami = function (callback) {
var konami = {
input: "",
pattern: "38384040373937396665", // Up, Up, Down, Down, Left, Right, Left, Right, B, A
load: function (callback) {
document.addEventListener("keydown", function (e) {
konami.input += e.keyCode;
if (konami.input.length > konami.pattern.length) {
konami.input = konami.input.slice(-konami.pattern.length);
}
if (konami.input === konami.pattern) {
konami.input = "";
callback();
}
});
},
};
konami.load(callback);
};
window.emitControlInput = emitControlInput;
new Konami(function () {
alert("Colors mode activated (Press A when you close this)!");
$(".up").css({
background: "orange",
borderRight: "10px solid #996300",
borderBottom: "10px solid #996300",
borderLeft: "10px solid #b37300",
borderTop: "10px solid #cc8400",
});
$(".uptext").css({
color: "orange",
textShadow: "0 0 10px orange, 0 0 10px orange",
});
$(".down").css({
background: "tomato",
borderRight: "10px solid #e02200",
borderBottom: "10px solid #e02200",
borderLeft: "10px solid #f92600",
borderTop: "10px solid #ff3814",
});
$(".downtext").css({
color: "tomato",
textShadow: "0 0 10px tomato, 0 0 10px tomato",
});
$(".left").css({
background: "skyblue",
borderRight: "10px solid #30aadc",
borderBottom: "10px solid #30aadc",
borderLeft: "10px solid #45b3e0",
borderTop: "10px solid #5bbce4",
});
$(".lefttext").css({
color: "skyblue",
textShadow: "0 0 10px skyblue, 0 0 10px skyblue",
});
$(".right").css({
background: "red",
borderRight: "10px solid #990000",
borderBottom: "10px solid #990000",
borderLeft: "10px solid #b30000",
borderTop: "10px solid #cc0000",
});
$(".righttext").css({
color: "red",
textShadow: "0 0 10px red, 0 0 10px red",
});
});
$(".up").on("click", function () {
$(".uptext").text("UP");
emitControlInput({ direction: "UP" });
});
$(".down").on("click", function () {
$(".downtext").text("DOWN");
emitControlInput({ direction: "DOWN" });
});
$(".left").on("click", function () {
$(".lefttext").text("LEFT");
emitControlInput({ direction: "LEFT" });
});
$(".right").on("click", function () {
$(".righttext").text("RIGHT");
emitControlInput({ direction: "RIGHT" });
});
$(".a").on("click", function () {
$(".a").text("A");
emitControlInput({ key: "A" });
});
$(".b").on("click", function () {
$(".b").text("B");
emitControlInput({ key: "B" });
});