88 lines
3.5 KiB
JavaScript
88 lines
3.5 KiB
JavaScript
// The rules behind the wall segments of a map (the wall editor: drag to draw a segment from grid
|
|
// intersection to grid intersection, drag an endpoint to retype it, drag the segment itself to move
|
|
// it). Like pins and tokens, walls are stored in image pixels — the very space Foundry draws a
|
|
// scene's walls in — so these helpers only translate and snap: they hold no state and talk to no
|
|
// API, and the editor saves the whole set the way the viewer ends up with it.
|
|
|
|
// Screen pixels (relative to the top left corner of the map viewport) → image pixels, clamped to
|
|
// the image but never refused: while a wall is being drawn the pointer does leave the image, and
|
|
// the endpoint then sticks to its edge instead of vanishing.
|
|
function ToDrawPoint(point, view) {
|
|
const zoom = Number(view?.zoom);
|
|
if (!Number.isFinite(zoom) || zoom <= 0) return null;
|
|
|
|
const x = (Number(point?.x) - Number(view?.pan?.x ?? 0)) / zoom;
|
|
const y = (Number(point?.y) - Number(view?.pan?.y ?? 0)) / zoom;
|
|
if (!Number.isFinite(x) || !Number.isFinite(y)) return null;
|
|
return { x: Math.max(0, x), y: Math.max(0, y) };
|
|
}
|
|
|
|
// The nearest intersection of the grid, in image pixels: a drawn wall's endpoints snap to these,
|
|
// so walls line up with the cells whatever offset the map has — the way Foundry snaps its walls
|
|
// to the grid too.
|
|
function SnapToGrid(point, grid) {
|
|
const size = Math.max(1, Number(grid?.gridSize) || 50);
|
|
const offsetX = Number(grid?.offsetX) || 0;
|
|
const offsetY = Number(grid?.offsetY) || 0;
|
|
|
|
const x = Number(point?.x);
|
|
const y = Number(point?.y);
|
|
if (!Number.isFinite(x) || !Number.isFinite(y)) return null;
|
|
|
|
return {
|
|
x: Math.max(0, Math.round(offsetX + Math.round((x - offsetX) / size) * size)),
|
|
y: Math.max(0, Math.round(offsetY + Math.round((y - offsetY) / size) * size))
|
|
};
|
|
}
|
|
|
|
// A wall (image pixels) → screen pixels of the map viewport. Walls are drawn in screen space like
|
|
// the pins, so their lines stay one pixel wide whatever the zoom is.
|
|
function ToScreenPoint(x, y, view) {
|
|
const zoom = Number(view?.zoom) || 0;
|
|
return {
|
|
x: Number(view?.pan?.x ?? 0) + Number(x ?? 0) * zoom,
|
|
y: Number(view?.pan?.y ?? 0) + Number(y ?? 0) * zoom
|
|
};
|
|
}
|
|
|
|
function ToScreenWall(wall, view) {
|
|
const from = ToScreenPoint(wall?.x1, wall?.y1, view);
|
|
const to = ToScreenPoint(wall?.x2, wall?.y2, view);
|
|
return { x1: from.x, y1: from.y, x2: to.x, y2: to.y };
|
|
}
|
|
|
|
// A whole wall slid over the map: both endpoints by the same delta, so a moved wall keeps its
|
|
// shape and its alignment to the grid it was drawn on. Never off the top left corner.
|
|
function MoveWall(wall, dx, dy) {
|
|
const x1 = Number(wall?.x1);
|
|
const y1 = Number(wall?.y1);
|
|
const x2 = Number(wall?.x2);
|
|
const y2 = Number(wall?.y2);
|
|
const stepX = Number(dx);
|
|
const stepY = Number(dy);
|
|
if (![x1, y1, x2, y2, stepX, stepY].every(Number.isFinite)) return null;
|
|
|
|
return {
|
|
x1: Math.max(0, Math.round(x1 + stepX)),
|
|
y1: Math.max(0, Math.round(y1 + stepY)),
|
|
x2: Math.max(0, Math.round(x2 + stepX)),
|
|
y2: Math.max(0, Math.round(y2 + stepY))
|
|
};
|
|
}
|
|
|
|
// Two endpoints of a segment, snapped or not, long enough to be a wall: a drag that ends where it
|
|
// started drew nothing.
|
|
function IsWall(wall) {
|
|
return Boolean(wall) && Number.isFinite(wall.x1) && Number.isFinite(wall.y1)
|
|
&& Number.isFinite(wall.x2) && Number.isFinite(wall.y2)
|
|
&& !(wall.x1 === wall.x2 && wall.y1 === wall.y2);
|
|
}
|
|
|
|
export {
|
|
IsWall,
|
|
MoveWall,
|
|
SnapToGrid,
|
|
ToDrawPoint,
|
|
ToScreenWall
|
|
};
|