fantasy-console/viewsheets.ts

63 lines
1.6 KiB
TypeScript
Raw Permalink Normal View History

2023-05-06 14:49:46 -07:00
import { clearScreen, fillRect } from "./window.ts";
import { drawIcon, drawText } from "./builtins.ts";
import { COLOR } from "./colors.ts";
import { getSheet } from "./sheet.ts";
import { mouseClick, mousePos } from "./mouse.ts";
import { getCart } from "./cart.ts";
2023-05-08 21:39:08 -07:00
import { font } from "./font.ts";
2023-05-09 19:42:02 -07:00
import { codeIcon, spriteIcon, mapIcon } from "./icons.ts";
2023-05-06 14:49:46 -07:00
import { reGridWithGap } from "./util.ts";
2023-05-08 21:39:08 -07:00
const fontHeight = font.height;
2023-05-06 14:49:46 -07:00
export const page = {activeSheet: 0, tab: "sheet"};
const gridX = 8;
const gridY = 20;
const cellW = 22;
const cellH = 16;
const gapX = 8;
const gapY = 8;
const update = () => {
if (mouseClick()) {
const {x: mouseX, y: mouseY} = mousePos();
const g = reGridWithGap(mouseX, mouseY, gridX, gridY, cellW, cellH, gapX, gapY);
if (g) {
const {x, y} = g;
page.activeSheet = 4*y+x;
const sheet = getSheet(page.activeSheet);
if (!sheet) {
console.log(x, y, g);
}
2023-05-06 14:49:46 -07:00
page.tab = getSheet(page.activeSheet).sheet_type;
}
}
}
const draw = () => {
clearScreen();
2023-05-07 13:58:55 -07:00
fillRect(0, 8, 128, 112, COLOR.DARKGRAY);
2023-05-06 14:49:46 -07:00
// Draw the sheet grid
getCart().forEach((sheet, i) => {
const x = gridX+(cellW+gapX)*(i%4);
const y = gridY+(cellH+gapY)*Math.floor(i/4);
fillRect(x, y, cellW, cellH, i===page.activeSheet ? COLOR.PURPLE : COLOR.BLACK);
drawText(x+(cellW)/2, y+(cellH-fontHeight)/2, i.toString().padStart(2,"0"));
const icon = {
code: codeIcon,
spritesheet: spriteIcon,
2023-05-09 19:42:02 -07:00
map: mapIcon,
2023-05-06 14:49:46 -07:00
none: null,
}[sheet.sheet_type];
if (icon) {
drawIcon(x+2, y+4, icon, COLOR.WHITE);
}
});
}
export const viewsheets = {
update,
draw,
}