-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconsole.ts
64 lines (56 loc) · 2.16 KB
/
console.ts
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
namespace scene.consoleOverlay {
let consoleColor = 1;
let consoleStrings: string[];
let tabSize = 8;
const marginx = 4;
const marginy = 2;
const consoleFont = bitmaps.font5;
console.addListener(listener);
export function isVisible() {
return !!consoleStrings;
}
export function clear() {
consoleStrings = [];
}
export function setVisible(value: boolean, col?: number) {
if (value != !!consoleStrings)
consoleStrings = value ? [] : undefined;
if (col !== undefined)
consoleColor = col;
}
function listener(priority: ConsolePriority, text: string) {
if (!theScreen || !consoleStrings || !text)
return;
const consoleColumns = Math.floor((theScreen.width - 2 * marginx) / consoleFont.charWidth);
const consoleLines = Math.floor(theScreen.height / (consoleFont.charHeight + marginy)) - 1;
// split text into lines
text.split("\n")
.filter(line => !!line)
.forEach(line => {
for (let j = 0; j < line.length; j += consoleColumns) {
consoleStrings.push(line.slice(j, j + consoleColumns));
}
});
if (consoleStrings.length > consoleLines) {
consoleStrings.splice(0, consoleStrings.length - consoleLines);
}
}
export function draw() {
if (!consoleStrings) return;
const height = consoleFont.charHeight + marginy;
const top = 2 + height;
for (let i = 0; i < consoleStrings.length; ++i) {
if (consoleStrings[i].indexOf("\t") >= 0) {
const t = consoleStrings[i].split("\t");
let tOff = 0;
for (let tab of t) {
let padding = tabSize - ((tOff + tab.length) % tabSize)
theScreen.print(tab, marginx + (tOff * consoleFont.charWidth), top + i * height, consoleColor, consoleFont);
tOff += tab.length + padding;
}
}
else
theScreen.print(consoleStrings[i], marginx, top + i * height, consoleColor, consoleFont);
}
}
}