homemanager-fe/src/modules/house-planner/index.ts

53 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-01-16 19:37:39 +00:00
import { HousePlannerCanvas } from './canvas';
2023-01-20 16:43:32 +00:00
import { Layer, Vec2, Vec2Box } from './interfaces';
2023-01-18 21:20:06 +00:00
import { HousePlannerCanvasTools } from './tools';
2023-01-16 19:37:39 +00:00
export class HousePlanner {
public canvas!: HTMLCanvasElement;
public manager?: HousePlannerCanvas;
2023-01-18 18:11:54 +00:00
initialize(
canvas: HTMLCanvasElement,
initialData: Layer[],
canvasDim: Vec2,
2023-01-18 18:47:22 +00:00
canvasPos: Vec2,
2023-01-18 18:11:54 +00:00
canvasZoom = 1,
2023-01-20 16:43:32 +00:00
editable = true,
2023-01-24 19:04:01 +00:00
grid = true,
headless = false,
2023-01-20 16:43:32 +00:00
boundingBox?: Vec2Box
): [number, Vec2] {
2023-01-16 19:37:39 +00:00
this.canvas = canvas;
2023-01-18 18:11:54 +00:00
this.manager = new HousePlannerCanvas(
canvas,
canvasDim,
canvasPos,
2023-01-18 18:47:22 +00:00
canvasZoom,
2023-01-24 19:04:01 +00:00
editable,
grid,
headless
2023-01-18 18:11:54 +00:00
);
2023-01-16 19:37:39 +00:00
this.manager.layers = initialData;
2023-01-18 18:47:22 +00:00
if (editable && this.manager.tools) {
2023-01-18 21:20:06 +00:00
const stdToolkit = this.manager.tools as HousePlannerCanvasTools;
stdToolkit.selectLayer(
2023-01-18 18:47:22 +00:00
initialData[initialData.findIndex((layer) => layer.active)]
);
2023-01-18 21:20:06 +00:00
stdToolkit.setInitialSelection();
stdToolkit.setTool('move');
2023-01-18 18:47:22 +00:00
}
2023-01-16 19:37:39 +00:00
this.manager.draw();
2023-01-20 16:43:32 +00:00
if (boundingBox) {
2023-01-24 19:04:01 +00:00
this.manager.setViewRectangle(boundingBox);
2023-01-20 16:43:32 +00:00
}
return [this.manager.canvasZoom, this.manager.canvasPos];
2023-01-16 19:37:39 +00:00
}
cleanUp() {
this.manager?.cleanUp();
}
}