-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayController.js
75 lines (69 loc) · 2.1 KB
/
displayController.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
export default class DisplayController {
constructor(displayElement, characterFactory) {
this.display = displayElement;
this.characterFactory = characterFactory;
this.cursorPosition = { x: 0, y: 0 };
this.displayContent = Array(2).fill().map(() => Array(16).fill(' '));
}
updateDisplay() {
this.display.innerHTML = '';
for (let row = 0; row < 2; row++) {
for (let col = 0; col < 16; col++) {
const charElement = this.characterFactory.createCharacterElement(this.displayContent[row][col]);
if (row === this.cursorPosition.y && col === this.cursorPosition.x) {
const cursorElement = document.createElement('div');
cursorElement.className = 'cursor'
charElement.appendChild(cursorElement);
}
this.display.appendChild(charElement);
}
}
}
moveCursor(direction) {
switch (direction) {
case 'up':
this.cursorPosition.y = Math.max(0, this.cursorPosition.y - 1);
break;
case 'down':
this.cursorPosition.y = Math.min(1, this.cursorPosition.y + 1);
break;
case 'left':
if (this.cursorPosition.x > 0) {
this.cursorPosition.x--;
} else if (this.cursorPosition.y > 0) {
this.cursorPosition.y--;
this.cursorPosition.x = 15;
}
break;
case 'right':
if (this.cursorPosition.x < 15) {
this.cursorPosition.x++;
} else if (this.cursorPosition.y < 1) {
this.cursorPosition.y++;
this.cursorPosition.x = 0;
}
break;
}
this.updateDisplay();
}
setText(text) {
let row = this.cursorPosition.y;
let col = this.cursorPosition.x;
for (let char of text) {
if (row >= 2) break;
this.displayContent[row][col] = char;
col++;
if (col >= 16) {
col = 0;
row++;
}
}
this.cursorPosition = { x: col, y: row };
this.updateDisplay();
}
clearDisplay() {
this.displayContent = Array(2).fill().map(() => Array(16).fill(' '));
this.cursorPosition = { x: 0, y: 0 };
this.updateDisplay();
}
}