import type { Graphics } from 'pixi.js'; import { Theme } from './Theme'; export interface ControlBoxOptions { fill: number; border: number; radius?: number; x?: number; y?: number; } /** * The shared look of every control. The stroke is inset by half a pixel so it lands on whole * device pixels and stays crisp instead of blurring over two. */ export function drawControlBox(g: Graphics, w: number, h: number, options: ControlBoxOptions): void { const { fill, border, radius = Theme.radius, x = 0, y = 0 } = options; g.roundRect(x, y, w, h, radius).fill(fill); g.roundRect(x + 0.5, y + 0.5, w - 1, h - 1, radius).stroke({ width: 1, color: border }); } export interface GridOptions { /** Where the origin of the drawn space sits on screen, in the same pixels as `w`/`h`. */ originX: number; originY: number; /** Spacing in screen pixels (world/graph step times zoom). */ step: number; w: number; h: number; alpha: number; /** Alpha of the two lines through the origin; omitted = no axes. */ axisAlpha?: number; } /** * White at a low alpha rather than a fixed colour, so the grid reads on whatever background the * scene sets. `g` is cleared by the caller. */ export function drawGrid(g: Graphics, options: GridOptions): void { const { originX, originY, step, w, h, alpha, axisAlpha } = options; const startX = ((originX % step) + step) % step; const startY = ((originY % step) + step) % step; for (let x = startX; x < w; x += step) g.moveTo(x, 0).lineTo(x, h); for (let y = startY; y < h; y += step) g.moveTo(0, y).lineTo(w, y); g.stroke({ width: 1, color: GRID_COLOR, alpha }); if (axisAlpha === undefined) return; const axis = { width: 1, color: GRID_COLOR, alpha: axisAlpha }; if (originX >= 0 && originX <= w) g.moveTo(originX, 0).lineTo(originX, h).stroke(axis); if (originY >= 0 && originY <= h) g.moveTo(0, originY).lineTo(w, originY).stroke(axis); } const GRID_COLOR = 0xffffff;