forked from Blendman974/faceclaw
96 lines
2.7 KiB
TypeScript
96 lines
2.7 KiB
TypeScript
import { G2_LENS_HEIGHT, G2_LENS_WIDTH, GrayImage } from "../graphics/image";
|
|
import { BdfFont } from "../graphics/bdffont";
|
|
import { type ConfigSettingString } from "./dashboard-settings";
|
|
|
|
export type DashboardInputEvent =
|
|
| { type: "click"; source: "ring" | "left-arm" | "right-arm" }
|
|
| { type: "double-click"; source: "ring" | "left-arm" | "right-arm" }
|
|
| { type: "scroll-up" }
|
|
| { type: "scroll-down" }
|
|
| { type: "unknown"; kind: string; eventSource: number; eventType: number };
|
|
|
|
export type LayerActions = {
|
|
disconnect: () => Promise<void> | void;
|
|
startTextSettingEdit: (setting: ConfigSettingString) => Promise<void> | void;
|
|
endTextSettingEdit: () => Promise<void> | void;
|
|
setVoiceControlEnabled: (enabled: boolean) => Promise<void> | void;
|
|
setStopwatchRenderActive: (active: boolean) => Promise<void> | void;
|
|
setTranscribeRenderActive: (active: boolean) => Promise<void> | void;
|
|
startDedicatedVoiceInput: (mode: "wakeword" | "full") => Promise<void> | void;
|
|
stopDedicatedVoiceInput: () => Promise<void> | void;
|
|
};
|
|
|
|
export type PaintBelow = () => GrayImage;
|
|
|
|
export interface LayerContext {
|
|
readonly stack: LayerStack;
|
|
readonly actions: LayerActions;
|
|
}
|
|
|
|
export interface Layer {
|
|
readonly paintOverBase?: boolean;
|
|
paint(ctx: LayerContext, paintBelow: PaintBelow): GrayImage;
|
|
handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> | void;
|
|
}
|
|
|
|
export class LayerStack {
|
|
private readonly layers: Layer[];
|
|
private readonly ctx: LayerContext;
|
|
|
|
constructor(baseLayer: Layer, actions: LayerActions) {
|
|
this.layers = [baseLayer];
|
|
this.ctx = {
|
|
stack: this,
|
|
actions,
|
|
};
|
|
}
|
|
|
|
push(layer: Layer): void {
|
|
this.layers.push(layer);
|
|
}
|
|
|
|
pop(): void {
|
|
if (this.layers.length > 1) {
|
|
this.layers.pop();
|
|
}
|
|
}
|
|
|
|
clearToBase(): void {
|
|
this.layers.splice(1);
|
|
}
|
|
|
|
isAtBase(): boolean {
|
|
return this.layers.length === 1;
|
|
}
|
|
|
|
setActions(actions: Partial<LayerActions>): void {
|
|
Object.assign(this.ctx.actions, actions);
|
|
}
|
|
|
|
paint(): GrayImage {
|
|
return this.paintLayer(this.layers.length - 1);
|
|
}
|
|
|
|
async handleInput(event: DashboardInputEvent): Promise<void> {
|
|
await this.layers[this.layers.length - 1]!.handleInput(event, this.ctx);
|
|
}
|
|
|
|
private paintLayer(index: number): GrayImage {
|
|
const layer = this.layers[index]!;
|
|
let cachedBelow: GrayImage | null = null;
|
|
return layer.paint(this.ctx, () => {
|
|
if (cachedBelow) {
|
|
return cachedBelow;
|
|
}
|
|
if (index <= 0) {
|
|
cachedBelow = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
|
} else if (layer.paintOverBase) {
|
|
cachedBelow = this.paintLayer(0);
|
|
} else {
|
|
cachedBelow = this.paintLayer(index - 1);
|
|
}
|
|
return cachedBelow;
|
|
});
|
|
}
|
|
}
|