diff --git a/src/display/display.ts b/src/display/display.ts index 39b29434..bd6a91a7 100644 --- a/src/display/display.ts +++ b/src/display/display.ts @@ -365,8 +365,11 @@ export interface Display = TOptions extends {layout: infer L} ? NonNullable : typeof DisplayImpl["DEFAULT_LAYOUT"]; -type BackendFor = LayoutBackend>; -type TCharFromOptions = BackendChars>; +type BackendFor = LayoutBackend, TOptions>; +type TCharFromOptions = + TOptions extends {tileMap: Record} ? TKey[] + : TOptions extends {tileMap: readonly any[]} ? number[] + : BackendChars>; type TFGColorFromOptions = BackendFGColor>; type TBGColorFromOptions = BackendBGColor>; @@ -384,6 +387,6 @@ export interface DisplayConstructor { } export const Display = class Display extends DisplayImpl { -} as any; +} as unknown as DisplayConstructor; export default Display; \ No newline at end of file diff --git a/src/display/hex.ts b/src/display/hex.ts index d553ddfe..5e773f06 100644 --- a/src/display/hex.ts +++ b/src/display/hex.ts @@ -3,7 +3,7 @@ import { TextDisplayOptions, DefaultsFor, DisplayOptions } from "./types.js"; import { mod } from "../util.js"; declare module "./types.js" { - interface LayoutTypeBackendMap { + interface LayoutTypeBackendMap { hex: Hex; } } diff --git a/src/display/rect.ts b/src/display/rect.ts index 97cb2f36..e83e6aac 100644 --- a/src/display/rect.ts +++ b/src/display/rect.ts @@ -2,7 +2,7 @@ import Canvas, { CanvasDisplayData } from "./canvas.js"; import { DefaultsFor, DisplayOptions, TextDisplayOptions } from "./types.js"; declare module "./types.js" { - interface LayoutTypeBackendMap { + interface LayoutTypeBackendMap { rect: Rect; } } diff --git a/src/display/term.ts b/src/display/term.ts index e8fbbe84..bea31704 100644 --- a/src/display/term.ts +++ b/src/display/term.ts @@ -3,7 +3,7 @@ import { BaseDisplayOptions, DisplayData, DisplayOptions } from "./types.js"; import * as Color from "../color.js"; declare module "./types.js" { - interface LayoutTypeBackendMap { + interface LayoutTypeBackendMap { term: Term; } } diff --git a/src/display/tile-gl.ts b/src/display/tile-gl.ts index 862b081b..58d73292 100644 --- a/src/display/tile-gl.ts +++ b/src/display/tile-gl.ts @@ -1,24 +1,24 @@ import Backend from "./backend.js"; -import { TileDisplayOptions, DisplayData, DefaultsFor, DisplayOptions } from "./types.js"; +import { TileDisplayOptions, DisplayData, TileMapKey, DefaultsFor, DisplayOptions } from "./types.js"; import * as Color from "../color.js"; declare module "./types.js" { - interface LayoutTypeBackendMap { - "tile-gl": TileGL; + interface LayoutTypeBackendMap { + "tile-gl": TileGL} ? TKey : string>; } } -export interface TileGLOptions extends TileDisplayOptions { +export interface TileGLOptions extends TileDisplayOptions { layout: "tile-gl"; } -export interface TileGLData extends DisplayData { +export interface TileGLData extends DisplayData { } /** * @class Tile backend * @private */ -export default class TileGL extends Backend { +export default class TileGL extends Backend, TChar[], string[], string[], TileGLData> { _gl!: WebGLRenderingContext; _program!: WebGLProgram; _uniforms: {[key:string]: WebGLUniformLocation | null}; @@ -29,7 +29,7 @@ export default class TileGL extends Backend; + } satisfies DefaultsFor>; } static isSupported() { return !!document.createElement("canvas").getContext("webgl2", {preserveDrawingBuffer:true}); @@ -52,11 +52,11 @@ export default class TileGL extends Backend void) { requestAnimationFrame(cb); } getContainer() { return this._gl.canvas as HTMLCanvasElement; } - checkOptions(options: DisplayOptions): options is TileGLOptions { + checkOptions(options: DisplayOptions): options is TileGLOptions { return options.layout === "tile-gl"; } - setOptions(opts: TileGLOptions) { + setOptions(opts: TileGLOptions) { let needsRepaint = super.setOptions(opts); this._updateSize(); @@ -71,14 +71,14 @@ export default class TileGL extends Backend { + protected defaultedOptions(options: TileGLOptions): Required> { return { ...this.DEFAULTS, ...options, } } - draw(data: TileGLData, clearBefore: boolean) { + draw(data: TileGLData, clearBefore: boolean) { const gl = this._gl; const opts = this._options; const {x, y, chars, fgs, bgs} = data; @@ -101,7 +101,7 @@ export default class TileGL extends Backend { + "tile": Tile} ? TKey : string>; } } -export interface TileOptions extends TileDisplayOptions { +export interface TileOptions extends TileDisplayOptions { layout: "tile"; } -export interface TileData extends DisplayData { +export interface TileData extends DisplayData { } /** * @class Tile backend * @private */ -export default class Tile extends BaseCanvas { +export default class Tile extends BaseCanvas, TileData, TChar[], string[], string[]> { _colorCanvas: HTMLCanvasElement; protected get DEFAULTS() { @@ -26,7 +26,7 @@ export default class Tile extends BaseCanvas; + } satisfies DefaultsFor>; } constructor(oldBackend?: UnknownBackend) { @@ -34,18 +34,18 @@ export default class Tile extends BaseCanvas { return options.layout === "tile"; } - protected defaultedOptions(options: TileOptions): Required { + protected defaultedOptions(options: TileOptions): Required> { return { ...this.DEFAULTS, ...options, } } - draw(data: TileData, clearBefore: boolean) { + draw(data: TileData, clearBefore: boolean) { const {x, y, chars, fgs, bgs} = data; let tileWidth = this._options.tileWidth; @@ -70,7 +70,7 @@ export default class Tile extends BaseCanvas { // Entries in this map are added via declaration merging, see the top of e.g. rect.ts } -export type LayoutType = {[TLayout in keyof LayoutTypeBackendMap]: TLayout}[keyof LayoutTypeBackendMap]; +export type LayoutType = {[TLayout in keyof LayoutTypeBackendMap]: TLayout}[keyof LayoutTypeBackendMap]; export type AnyBackend = IDisplayBackend; export type UnknownBackend = IDisplayBackend; @@ -11,11 +11,11 @@ export type BackendChars = TBackend extends IDispla export type BackendFGColor = TBackend extends IDisplayBackend ? TData["fg"] : never; export type BackendBGColor = TBackend extends IDisplayBackend ? TData["bg"] : never; -export type LayoutBackend = LayoutTypeBackendMap[TLayout]; -export type LayoutOptions = BackendOptions> -export type LayoutChars = BackendChars> -export type LayoutFGColor = BackendFGColor> -export type LayoutBGColor = BackendBGColor> +export type LayoutBackend = LayoutTypeBackendMap[TLayout]; +export type LayoutOptions = BackendOptions> +export type LayoutChars = BackendChars> +export type LayoutFGColor = BackendFGColor> +export type LayoutBGColor = BackendBGColor> export interface BaseDisplayOptions { width?: number; @@ -33,10 +33,11 @@ export interface TextDisplayOptions extends BaseDisplayOptions { fontStyle?: string; } -export interface TileDisplayOptions extends BaseDisplayOptions { +export type TileMapKey = string | number | symbol; +export interface TileDisplayOptions extends BaseDisplayOptions { tileWidth?: number; tileHeight?: number; - tileMap: { [key: string]: [number, number] }; + tileMap: { [K in TChar]: [number, number] }; tileSet: null | HTMLCanvasElement | HTMLImageElement | HTMLVideoElement | ImageBitmap; tileColorize?: boolean; }