Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display/Backend refactor #217

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
.ts.flag
.idea
.DS_Store
*.tsbuildinfo
35 changes: 30 additions & 5 deletions src/display/backend.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import { DisplayOptions, DisplayData } from "./types.js";
import { DEFAULT_HEIGHT, DEFAULT_WIDTH } from "../constants.js";
import { DisplayOptions, DisplayData, IDisplayBackend, DefaultsFor, Frozen, BaseDisplayOptions } from "./types.js";

/**
* @class Abstract display backend module
* @private
*/
export default abstract class Backend {
_options!: DisplayOptions;
export default abstract class Backend<TOptions extends BaseDisplayOptions = BaseDisplayOptions,
TChar = string, TFGColor = string, TBGColor = string,
TData extends DisplayData<TChar, TFGColor, TBGColor> = DisplayData<TChar, TFGColor, TBGColor>,
>
implements IDisplayBackend<TOptions, TData> {
protected get DEFAULTS() {
return {
width: DEFAULT_WIDTH,
height: DEFAULT_HEIGHT,
layout: "rect",
fg: "#ccc",
bg: "#000",
} satisfies DefaultsFor<BaseDisplayOptions>;

Check failure on line 20 in src/display/backend.ts

View workflow job for this annotation

GitHub Actions / build

';' expected.

Check failure on line 20 in src/display/backend.ts

View workflow job for this annotation

GitHub Actions / build

'(' expected.
}
_options!: Frozen<TOptions>;

getContainer(): HTMLElement | null { return null; }
setOptions(options: DisplayOptions) { this._options = options; }

abstract checkOptions(options: DisplayOptions): options is DisplayOptions & TOptions;
setOptions(options: TOptions) {
// return true if this change dirties the whole display
const { width, height, layout } = this._options ?? {};
this._options = this.defaultedOptions(options);
return width !== options.width || height !== options.height || layout !== options.layout;
}
protected abstract defaultedOptions(options: TOptions): Frozen<TOptions>;
getOptions(): Frozen<TOptions> {
return this._options as Frozen<TOptions>;
}

abstract schedule(cb: ()=>void): void;
abstract clear(): void;
abstract draw(data: DisplayData, clearBefore: boolean): void;
abstract draw(data: TData, clearBefore: boolean): void;
abstract computeSize(availWidth: number, availHeight: number): [number, number];
abstract computeFontSize(availWidth: number, availHeight: number): number;
abstract eventToPosition(x:number, y:number): [number, number];
Expand Down
73 changes: 58 additions & 15 deletions src/display/canvas.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import Backend from "./backend.js";
import { DisplayOptions } from "./types.js";
import { BaseDisplayOptions, DisplayData, TextDisplayOptions, UnknownBackend, DefaultsFor } from "./types.js";

export default abstract class Canvas extends Backend {
/**
* Base class for any backend that uses a `<canvas>` element as its display surface
*/
export abstract class BaseCanvas<TOptions extends BaseDisplayOptions,
TData extends DisplayData = DisplayData<string[], string, string>,
TChar = string[], TFGColor = string, TBGColor = string,
>
extends Backend<TOptions, TChar, TFGColor, TBGColor, TData extends DisplayData<TChar, TFGColor, TBGColor> ? TData : never> {
_ctx: CanvasRenderingContext2D;

constructor() {
constructor(oldBackend?: UnknownBackend) {
super();
this._ctx = document.createElement("canvas").getContext("2d") as CanvasRenderingContext2D;
this._ctx = oldBackend instanceof BaseCanvas ? oldBackend._ctx : document.createElement("canvas").getContext("2d")!;
}

schedule(cb: () => void) { requestAnimationFrame(cb); }
getContainer() { return this._ctx.canvas; }

setOptions(opts: DisplayOptions) {
super.setOptions(opts);
setOptions(opts: TOptions) {
let needsRepaint = super.setOptions(opts);

const style = (opts.fontStyle ? `${opts.fontStyle} ` : ``);
const font = `${style} ${opts.fontSize}px ${opts.fontFamily}`;
this._ctx.font = font;
this._updateSize();
if (needsRepaint) {
this._updateSize();
}

this._ctx.font = font;
this._ctx.textAlign = "center";
this._ctx.textBaseline = "middle";
return needsRepaint;
}

clear() {
const oldComposite = this._ctx.globalCompositeOperation;
this._ctx.save();
this._ctx.globalCompositeOperation = "copy"
this._ctx.fillStyle = this._options.bg;
this._ctx.fillRect(0, 0, this._ctx.canvas.width, this._ctx.canvas.height);
this._ctx.globalCompositeOperation = oldComposite;
this._ctx.restore();
}

eventToPosition(x: number, y: number): [number, number] {
Expand All @@ -50,3 +54,42 @@
abstract _normalizedEventToPosition(x: number, y: number): [number, number];
abstract _updateSize(): void;
}

export type CanvasDisplayData = DisplayData<string[], string, string>;
/**
* Base class for text canvases, which can display one or more text characters with a single foreground and a background color in each cell.
*/
export default abstract class Canvas<TOptions extends TextDisplayOptions,
TData extends DisplayData = CanvasDisplayData,
TChar = string[], TFGColor = string, TBGColor = string,
>
extends BaseCanvas<TOptions, TData, TChar, TFGColor, TBGColor> {
protected get DEFAULTS() {
return {
...super.DEFAULTS,
fontSize: 15,
spacing: 1,
border: 0,
fontFamily: "monospace",
fontStyle: "",
} satisfies DefaultsFor<TextDisplayOptions>;

Check failure on line 75 in src/display/canvas.ts

View workflow job for this annotation

GitHub Actions / build

';' expected.

Check failure on line 75 in src/display/canvas.ts

View workflow job for this annotation

GitHub Actions / build

'(' expected.
}
setOptions(opts: TOptions) {
const { fontSize, fontFamily, spacing } = this._options;
let needsRepaint = super.setOptions(opts) || fontSize !== opts.fontSize || fontFamily !== opts.fontFamily || spacing !== opts.spacing;

if (needsRepaint) {
opts = this._options;
const style = (opts.fontStyle ? `${opts.fontStyle} ` : ``);
const font = `${style} ${opts.fontSize}px ${opts.fontFamily}`;
this._ctx.font = font;
this._updateSize();

this._ctx.font = font;
this._ctx.textAlign = "center";
this._ctx.textBaseline = "middle";
}

return needsRepaint;
}
}
Loading
Loading