-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisp.ts
128 lines (117 loc) · 2.91 KB
/
disp.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
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
import { Group, Node } from "aminogfx-gl";
import { Display, makeText, pfx } from "./gfx";
import { Color } from "./light";
import { time } from "./timer";
export type DisplayContent = {
hash: string;
images?: string[];
color?: Color;
inverted?: boolean;
text?: {
text: string;
vAlign: 'top'|'bottom'|'center'|'baseline';
x: number;
y: number;
size: number;
}[];
off?: true;
};
export function dText(text: string, x: number, y: number, vAlign: 'top'|'bottom'|'center'|'baseline', size: number): DisplayContent {
return {
hash: `text ${text}`,
text: [{
text,
vAlign,
x,
y,
size,
}],
};
}
export function dFitText(text: string, y: number = 64, vAlign: 'top'|'bottom'|'center'|'baseline' = 'center'): DisplayContent {
let size: number;
let x: number;
switch (text.length) {
case 1:
size = 72;
x = 60;
break;
case 2:
size = 72;
x = 40;
break;
case 3:
size = 78;
x = 22;
break;
case 4:
size = 70;
x = 13;
break;
case 5:
size = 62;
x = 5;
break;
case 6:
size = 55;
x = 7;
break;
case 7:
size = 55;
x = 2;
break;
default:
size = 30;
x = 2;
break;
}
return dText(text, x, y, vAlign, size);
}
export function dImage(name: string): DisplayContent {
return {
hash: `image ${name}`,
images: [name],
};
}
export function dClear(color: Color = Color.Black): DisplayContent {
return {
hash: `clear ${color}`,
color,
};
}
export function dOff(): DisplayContent {
return {
hash: 'off',
off: true,
};
}
export function dInvert(inverted: boolean, disp?: DisplayContent): DisplayContent {
return dHash({
...disp,
inverted,
});
}
export function dFlash(disp: DisplayContent, on = 200, off = 200) {
return dInvert(time()%(on+off)>off, disp);
}
export function dHash(d: Partial<DisplayContent>): DisplayContent {
return {
...d,
hash: JSON.stringify(d),
};
}
export function dMix(func: (() => DisplayContent|undefined)|DisplayContent|undefined): (state?: DisplayContent[]) => DisplayContent[] | undefined {
return prev => {
const l = typeof func==='function'? func() : func;
if (l)
return [l, ...(prev??[])];
else
return prev;
};
}
export function dMany(...content: DisplayContent[]): DisplayContent {
let obj: DisplayContent = { hash: ''};
for (const c of content)
obj = {...obj, ...c, images: [...obj.images ?? [], ...c.images ?? []]};
return dHash(obj);
}