This commit is contained in:
@@ -5,6 +5,7 @@ import { ClearWindow, CreateWindow, GetFirstWindowId } from '@/services/Windows'
|
||||
import { HideContextMenu, ShowContextMenu } from '~/services/ContextMenu';
|
||||
import { BuildNoteOptions, OptionLabel, ToImagePoint, ToScreenPoint } from '~/services/MapPins';
|
||||
import { ActorDragId, CellCentre, IsActorDrag, MoveByCells, TokenSize } from '~/services/MapTokens';
|
||||
import { IsWall, MoveWall, SnapToGrid, ToDrawPoint, ToScreenWall } from '~/services/MapWalls';
|
||||
import { DisplayToast } from '~/services/Toaster';
|
||||
import Server, { getBaseUrl } from '~/services/Server';
|
||||
import { emitter } from '~/services/Emitter';
|
||||
@@ -114,6 +115,18 @@ let lastPointer = { x: 0, y: 0 };
|
||||
|
||||
function onPointerDown(event) {
|
||||
if (event.button !== 0 || !viewport.value) return;
|
||||
if (editingWalls.value && imageUrl.value) {
|
||||
// While the wall editor is open the left pointer draws instead of panning: the wheel and
|
||||
// the zoom buttons still move the view about.
|
||||
const spot = wallSpot(event);
|
||||
const first = spot && SnapToGrid(spot, grid.value);
|
||||
if (!first) return;
|
||||
pointerId = event.pointerId;
|
||||
viewport.value.setPointerCapture?.(event.pointerId);
|
||||
drawingWall.value = { x1: first.x, y1: first.y, x2: first.x, y2: first.y };
|
||||
selectedWall.value = null;
|
||||
return;
|
||||
}
|
||||
dragging.value = true;
|
||||
pointerId = event.pointerId;
|
||||
lastPointer = { x: event.clientX, y: event.clientY };
|
||||
@@ -121,13 +134,28 @@ function onPointerDown(event) {
|
||||
}
|
||||
|
||||
function onPointerMove(event) {
|
||||
if (drawingWall.value && event.pointerId === pointerId) {
|
||||
const spot = wallSpot(event);
|
||||
const snapped = spot && SnapToGrid(spot, grid.value);
|
||||
if (snapped) drawingWall.value = { ...drawingWall.value, x2: snapped.x, y2: snapped.y };
|
||||
return;
|
||||
}
|
||||
if (!dragging.value || event.pointerId !== pointerId) return;
|
||||
pan.value = { x: pan.value.x + (event.clientX - lastPointer.x), y: pan.value.y + (event.clientY - lastPointer.y) };
|
||||
lastPointer = { x: event.clientX, y: event.clientY };
|
||||
}
|
||||
|
||||
function onPointerUp(event) {
|
||||
async function onPointerUp(event) {
|
||||
if (event.pointerId !== pointerId) return;
|
||||
if (drawingWall.value) {
|
||||
const segment = drawingWall.value;
|
||||
pointerId = null;
|
||||
drawingWall.value = null;
|
||||
viewport.value?.releasePointerCapture?.(event.pointerId);
|
||||
// A drag that ended where it started drew no wall; anything with length becomes one.
|
||||
if (IsWall(segment)) await saveWalls([...walls.value, segment]);
|
||||
return;
|
||||
}
|
||||
dragging.value = false;
|
||||
pointerId = null;
|
||||
viewport.value?.releasePointerCapture?.(event.pointerId);
|
||||
@@ -503,6 +531,215 @@ function handleActorEntry({ entityType, entry }) {
|
||||
actors.value = [entry, ...actors.value.filter((current) => String(current._id) !== String(entry._id))];
|
||||
}
|
||||
|
||||
// --- Walls blocking the map ---
|
||||
// The wall editor: with walls on, dragging over the map draws a segment from one grid intersection
|
||||
// to the next, dragging an endpoint retypes it, dragging the segment itself moves it, and right
|
||||
// clicking one (or Delete on a focused one) opens its controls and takes it off. Walls are stored
|
||||
// in image pixels like the pins, and every edit saves the whole set — the same set a paired
|
||||
// Foundry scene takes over, so the two see the same walls.
|
||||
|
||||
const walls = computed(() => map.value?.walls ?? []);
|
||||
const editingWalls = ref(false);
|
||||
// The wall a pointer is holding: a new segment being dragged, the whole segment being slid, or one
|
||||
// endpoint being retyped. Like a token's move, the change stays local until the pointer lets go,
|
||||
// so a drag across the map is one save and not one per mouse move.
|
||||
const drawingWall = ref(null);
|
||||
const draggingWall = ref(null);
|
||||
const draggingEnd = ref(null);
|
||||
// The wall whose controls are asked for: selected before its menu opens, kept while it is dragged.
|
||||
const selectedWall = ref(null);
|
||||
|
||||
const END_KEYS = [['x1', 'y1'], ['x2', 'y2']];
|
||||
let wallDragPointerId = null;
|
||||
let lastWallDrag = { x: 0, y: 0 };
|
||||
|
||||
function toggleWallEditing() {
|
||||
editingWalls.value = !editingWalls.value;
|
||||
if (!editingWalls.value) {
|
||||
drawingWall.value = null;
|
||||
draggingWall.value = null;
|
||||
draggingEnd.value = null;
|
||||
selectedWall.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
// The spot of the viewport in image pixels, clamped to the image: while a wall is being held the
|
||||
// pointer may cross an edge, and the wall should stay on the map rather than vanish.
|
||||
function wallSpot(event) {
|
||||
const bounds = viewport.value?.getBoundingClientRect();
|
||||
if (!bounds) return null;
|
||||
return ToDrawPoint({ x: event.clientX - bounds.left, y: event.clientY - bounds.top }, viewState());
|
||||
}
|
||||
|
||||
// The wall as it should be drawn right now: with the drag's preview on top of what is stored.
|
||||
function wallGeometry(wall) {
|
||||
if (draggingWall.value && String(draggingWall.value.wall?._id) === String(wall?._id)) {
|
||||
const moved = MoveWall(wall, draggingWall.value.dx, draggingWall.value.dy);
|
||||
if (moved) return { ...wall, ...moved };
|
||||
}
|
||||
if (draggingEnd.value && String(draggingEnd.value.wall?._id) === String(wall?._id) && draggingEnd.value.point) {
|
||||
const [keyX, keyY] = END_KEYS[draggingEnd.value.end];
|
||||
return { ...wall, [keyX]: draggingEnd.value.point.x, [keyY]: draggingEnd.value.point.y };
|
||||
}
|
||||
return wall;
|
||||
}
|
||||
|
||||
function wallScreen(wall) {
|
||||
return ToScreenWall(wallGeometry(wall), viewState());
|
||||
}
|
||||
|
||||
function wallClasses(wall) {
|
||||
return {
|
||||
'wall-door': Number(wall?.door) > 0,
|
||||
'wall-move-only': !wall?.sight && Number(wall?.door) === 0,
|
||||
selected: Boolean(selectedWall.value) && String(selectedWall.value) === String(wall?._id)
|
||||
};
|
||||
}
|
||||
|
||||
// The wire shape of a wall: what the server keeps, with the `_id` of the ones it already has so a
|
||||
// save neither doubles them nor re-ids them under the viewer's feet.
|
||||
function plainWall(wall) {
|
||||
const stored = {
|
||||
x1: Number(wall.x1), y1: Number(wall.y1), x2: Number(wall.x2), y2: Number(wall.y2),
|
||||
move: wall.move !== false, sight: wall.sight !== false, door: Number(wall.door) || 0
|
||||
};
|
||||
return wall._id ? { _id: String(wall._id), ...stored } : stored;
|
||||
}
|
||||
|
||||
async function saveWalls(next) {
|
||||
return updateMap('/map/walls', { id: map.value?._id, walls: next.map(plainWall) }, t('campaigns.map.walls.error'));
|
||||
}
|
||||
|
||||
async function replaceWall(wall, patch) {
|
||||
const next = walls.value.map((current) => (
|
||||
String(current?._id) === String(wall?._id) ? { ...current, ...patch } : current
|
||||
));
|
||||
await saveWalls(next);
|
||||
}
|
||||
|
||||
async function removeWall(wall) {
|
||||
if (String(selectedWall.value) === String(wall?._id)) selectedWall.value = null;
|
||||
await saveWalls(walls.value.filter((current) => String(current?._id) !== String(wall?._id)));
|
||||
}
|
||||
|
||||
function onWallPointerDown(event, wall) {
|
||||
// The segment is what is being held here: the map must not start panning under the pointer.
|
||||
event.stopPropagation();
|
||||
if (event.button !== 0) return;
|
||||
|
||||
wallDragPointerId = event.pointerId;
|
||||
event.currentTarget.setPointerCapture?.(event.pointerId);
|
||||
lastWallDrag = { x: event.clientX, y: event.clientY };
|
||||
selectedWall.value = String(wall?._id ?? '');
|
||||
draggingWall.value = { wall, dx: 0, dy: 0 };
|
||||
}
|
||||
|
||||
function onWallPointerMove(event) {
|
||||
if (!draggingWall.value || event.pointerId !== wallDragPointerId) return;
|
||||
const zoomScale = zoom.value || 1;
|
||||
draggingWall.value = {
|
||||
...draggingWall.value,
|
||||
// The drag is measured in screen pixels and applied in image ones, and the whole segment
|
||||
// goes by the same delta: a moved wall keeps its shape and the grid it was drawn on.
|
||||
dx: draggingWall.value.dx + (event.clientX - lastWallDrag.x) / zoomScale,
|
||||
dy: draggingWall.value.dy + (event.clientY - lastWallDrag.y) / zoomScale
|
||||
};
|
||||
lastWallDrag = { x: event.clientX, y: event.clientY };
|
||||
}
|
||||
|
||||
async function onWallPointerUp(event) {
|
||||
if (!draggingWall.value || event.pointerId !== wallDragPointerId) return;
|
||||
|
||||
const { wall, dx, dy } = draggingWall.value;
|
||||
wallDragPointerId = null;
|
||||
draggingWall.value = null;
|
||||
event.currentTarget?.releasePointerCapture?.(event.pointerId);
|
||||
|
||||
const moved = MoveWall(wall, dx, dy);
|
||||
if (!moved || (moved.x1 === Number(wall.x1) && moved.y1 === Number(wall.y1)
|
||||
&& moved.x2 === Number(wall.x2) && moved.y2 === Number(wall.y2))) return;
|
||||
await replaceWall(wall, moved);
|
||||
}
|
||||
|
||||
function onWallEndPointerDown(event, wall, end) {
|
||||
event.stopPropagation();
|
||||
if (event.button !== 0) return;
|
||||
|
||||
wallDragPointerId = event.pointerId;
|
||||
event.currentTarget.setPointerCapture?.(event.pointerId);
|
||||
selectedWall.value = String(wall?._id ?? '');
|
||||
draggingEnd.value = { wall, end, point: { x: Number(wall[END_KEYS[end][0]]), y: Number(wall[END_KEYS[end][1]]) } };
|
||||
}
|
||||
|
||||
function onWallEndPointerMove(event) {
|
||||
if (!draggingEnd.value || event.pointerId !== wallDragPointerId) return;
|
||||
const spot = wallSpot(event);
|
||||
const snapped = spot && SnapToGrid(spot, grid.value);
|
||||
if (!snapped) return;
|
||||
draggingEnd.value = { ...draggingEnd.value, point: snapped };
|
||||
}
|
||||
|
||||
async function onWallEndPointerUp(event) {
|
||||
if (!draggingEnd.value || event.pointerId !== wallDragPointerId) return;
|
||||
|
||||
const { wall, end, point } = draggingEnd.value;
|
||||
wallDragPointerId = null;
|
||||
draggingEnd.value = null;
|
||||
event.currentTarget?.releasePointerCapture?.(event.pointerId);
|
||||
|
||||
const [keyX, keyY] = END_KEYS[end];
|
||||
if (point.x === Number(wall[keyX]) && point.y === Number(wall[keyY])) return;
|
||||
const retyped = { ...wall, [keyX]: point.x, [keyY]: point.y };
|
||||
// Retyped into its own end, the segment has no length left: the wall is gone.
|
||||
if (!IsWall(retyped)) await removeWall(wall);
|
||||
else await replaceWall(wall, { [keyX]: point.x, [keyY]: point.y });
|
||||
}
|
||||
|
||||
function onWallContextMenu(wall) {
|
||||
selectedWall.value = String(wall?._id ?? '');
|
||||
ShowContextMenu([
|
||||
{
|
||||
name: t(wall.sight ? 'campaigns.map.walls.sight-open' : 'campaigns.map.walls.sight-block'),
|
||||
icon: '/icons/iconoir/regular/eye.svg',
|
||||
action: () => { HideContextMenu(); replaceWall(wall, { sight: !wall.sight }); }
|
||||
},
|
||||
{
|
||||
name: t(wall.move ? 'campaigns.map.walls.move-open' : 'campaigns.map.walls.move-block'),
|
||||
icon: '/icons/iconoir/regular/lock.svg',
|
||||
action: () => { HideContextMenu(); replaceWall(wall, { move: !wall.move }); }
|
||||
},
|
||||
{
|
||||
name: t('campaigns.map.walls.door'),
|
||||
icon: '/icons/iconoir/regular/home-simple-door.svg',
|
||||
context: [
|
||||
{ name: t('campaigns.map.walls.door-none'), action: () => { HideContextMenu(); replaceWall(wall, { door: 0 }); } },
|
||||
{ name: t('campaigns.map.walls.door-closed'), action: () => { HideContextMenu(); replaceWall(wall, { door: 2 }); } },
|
||||
{ name: t('campaigns.map.walls.door-open'), action: () => { HideContextMenu(); replaceWall(wall, { door: 1 }); } }
|
||||
]
|
||||
},
|
||||
{ divider: true },
|
||||
{
|
||||
name: t('campaigns.map.walls.remove'),
|
||||
icon: '/icons/iconoir/regular/xmark-circle.svg',
|
||||
action: () => { HideContextMenu(); removeWall(wall); }
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
// A focused wall answers Delete too, the way a focused token does.
|
||||
async function onWallKeydown(event, wall) {
|
||||
if (event.key !== 'Delete' && event.key !== 'Backspace') return;
|
||||
event.preventDefault();
|
||||
await removeWall(wall);
|
||||
}
|
||||
|
||||
// Escape lets go of whatever the editor is holding: an unfinished segment first, the selection next.
|
||||
function onWindowKeydown(event) {
|
||||
if (event.key !== 'Escape') return;
|
||||
if (drawingWall.value) drawingWall.value = null;
|
||||
else if (selectedWall.value) selectedWall.value = null;
|
||||
}
|
||||
|
||||
// A different map was anchored while this pane was already mounted.
|
||||
watch(() => props.map, (incoming, previous) => {
|
||||
if (incoming === previous) return;
|
||||
@@ -510,6 +747,10 @@ watch(() => props.map, (incoming, previous) => {
|
||||
imageSize.value = { width: 0, height: 0 };
|
||||
movingToken.value = null;
|
||||
draggingActor.value = false;
|
||||
drawingWall.value = null;
|
||||
draggingWall.value = null;
|
||||
draggingEnd.value = null;
|
||||
selectedWall.value = null;
|
||||
});
|
||||
|
||||
watch(campaignId, () => {
|
||||
@@ -525,6 +766,7 @@ onMounted(() => {
|
||||
emitter.on('campaign-entry-updated', handleActorEntry);
|
||||
emitter.on('delete-note', handlePinnedNoteDeleted);
|
||||
emitter.on('title-updated', handlePinnedNoteRenamed);
|
||||
window.addEventListener('keydown', onWindowKeydown);
|
||||
fetchPinnableNotes();
|
||||
fetchTokenActors();
|
||||
});
|
||||
@@ -535,6 +777,7 @@ onBeforeUnmount(() => {
|
||||
emitter.off('campaign-entry-updated', handleActorEntry);
|
||||
emitter.off('delete-note', handlePinnedNoteDeleted);
|
||||
emitter.off('title-updated', handlePinnedNoteRenamed);
|
||||
window.removeEventListener('keydown', onWindowKeydown);
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -550,6 +793,10 @@ onBeforeUnmount(() => {
|
||||
:aria-label="$t('campaigns.map.toggle-grid')" :aria-pressed="showGrid.toString()" @click="showGrid = !showGrid">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/view-grid.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
<button class="viewer-tool" :class="{ selected: editingWalls }" type="button" :title="$t('campaigns.map.walls.edit')"
|
||||
:aria-label="$t('campaigns.map.walls.edit')" :aria-pressed="editingWalls.toString()" @click="toggleWallEditing">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/border-out.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
<button class="viewer-tool" type="button" :title="$t('campaigns.map.zoom-out')" :aria-label="$t('campaigns.map.zoom-out')" @click="zoomFromToolbar(false)">
|
||||
<img class="tool-icon" src="/icons/iconoir/regular/minus.svg" alt="" aria-hidden="true">
|
||||
</button>
|
||||
@@ -587,6 +834,42 @@ onBeforeUnmount(() => {
|
||||
<template v-if="imageUrl">
|
||||
<img class="map-image" :src="imageUrl" :style="stageStyle" :alt="map?.name" draggable="false" @load="onImageLoaded">
|
||||
<div class="map-grid" :style="gridStyle" aria-hidden="true"></div>
|
||||
<!-- Walls are drawn in screen space like the pins, so their lines stay thin at any
|
||||
zoom. They only catch the mouse while the editor is open, and never when there
|
||||
is no image to hold them. -->
|
||||
<svg v-if="imageUrl && (walls.length || drawingWall || editingWalls)" class="map-walls" :class="{ editing: editingWalls }">
|
||||
<g v-for="wall in walls" :key="wall._id">
|
||||
<line class="wall-line" :class="wallClasses(wall)" v-bind="wallScreen(wall)"></line>
|
||||
<template v-if="editingWalls">
|
||||
<line
|
||||
class="wall-hit"
|
||||
role="button"
|
||||
tabindex="0"
|
||||
:aria-label="$t('campaigns.map.walls.wall')"
|
||||
v-bind="wallScreen(wall)"
|
||||
@pointerdown="onWallPointerDown($event, wall)"
|
||||
@pointermove="onWallPointerMove"
|
||||
@pointerup="onWallPointerUp"
|
||||
@pointercancel="onWallPointerUp"
|
||||
@contextmenu.stop.prevent="onWallContextMenu(wall)"
|
||||
@keydown="onWallKeydown($event, wall)"
|
||||
></line>
|
||||
<circle
|
||||
v-for="end in [0, 1]"
|
||||
:key="end"
|
||||
class="wall-end"
|
||||
:cx="end === 0 ? wallScreen(wall).x1 : wallScreen(wall).x2"
|
||||
:cy="end === 0 ? wallScreen(wall).y1 : wallScreen(wall).y2"
|
||||
r="5"
|
||||
@pointerdown="onWallEndPointerDown($event, wall, end)"
|
||||
@pointermove="onWallEndPointerMove"
|
||||
@pointerup="onWallEndPointerUp"
|
||||
@pointercancel="onWallEndPointerUp"
|
||||
></circle>
|
||||
</template>
|
||||
</g>
|
||||
<line v-if="drawingWall" class="wall-draft" v-bind="ToScreenWall(drawingWall, viewState())"></line>
|
||||
</svg>
|
||||
<div class="map-pins">
|
||||
<button
|
||||
v-for="pin in pins"
|
||||
@@ -629,6 +912,7 @@ onBeforeUnmount(() => {
|
||||
</button>
|
||||
</div>
|
||||
<div v-if="draggingActor" class="map-drop-hint">{{ $t('campaigns.map.tokens.drop-hint') }}</div>
|
||||
<div v-if="editingWalls" class="map-walls-hint">{{ $t('campaigns.map.walls.hint') }}</div>
|
||||
</template>
|
||||
<div v-else class="map-empty">
|
||||
{{ $t('campaigns.map.no-image') }}
|
||||
@@ -699,6 +983,27 @@ onBeforeUnmount(() => {
|
||||
background: var(--color-background-soft); color: var(--color-text); font-size: 12px;
|
||||
pointer-events: none; box-shadow: 0px 0px 6px -1px rgba(0,0,0,0.55);
|
||||
}
|
||||
/* Walls are drawn in screen space like the pins, so their lines stay thin at any zoom. A sight
|
||||
wall is red, a wall that only stops movement grey, a door green — close to how Foundry paints
|
||||
them on its own scenes. Only the editor's hit lines and handles catch the mouse. */
|
||||
.map-walls { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; overflow: visible; }
|
||||
.wall-line { stroke: var(--color-red); stroke-width: 2; opacity: .85; pointer-events: none; }
|
||||
.wall-line.wall-move-only { stroke: var(--color-gray); }
|
||||
.wall-line.wall-door { stroke: var(--color-green); stroke-width: 3; }
|
||||
.wall-line.selected { stroke: var(--color-link); opacity: 1; }
|
||||
/* The grabbable line and the endpoint handles: each wall gives its own hit area, wide enough that
|
||||
the pointer finds it at any zoom. */
|
||||
.wall-hit { stroke: transparent; stroke-width: 16; stroke-linecap: round; pointer-events: stroke; cursor: move; }
|
||||
.wall-hit:focus-visible { stroke: var(--color-link); stroke-opacity: .35; outline: none; }
|
||||
.wall-end { fill: var(--color-background-soft); stroke: var(--color-link); stroke-width: 1.5; pointer-events: auto; cursor: crosshair; }
|
||||
.wall-draft { stroke: var(--color-link); stroke-width: 2; stroke-dasharray: 6 4; pointer-events: none; }
|
||||
/* Shown while the wall editor is open, so the gestures it answers to are at hand. */
|
||||
.map-walls-hint {
|
||||
position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);
|
||||
padding: 5px 10px; border: 1px dashed var(--color-link); border-radius: 7px;
|
||||
background: var(--color-background-soft); color: var(--color-text); font-size: 12px;
|
||||
pointer-events: none; box-shadow: 0px 0px 6px -1px rgba(0,0,0,0.55);
|
||||
}
|
||||
.map-empty { width: 100%; height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; font-size: 13px; opacity: .8; }
|
||||
.map-edit { border: 1px solid var(--color-border); border-radius: 7px; padding: 6px 11px; background: var(--color-background-soft); color: var(--color-text); font-size: 12px; cursor: pointer; }
|
||||
.map-edit:hover { background: var(--color-button-hover); }
|
||||
|
||||
87
frontend/app/services/MapWalls.js
Normal file
87
frontend/app/services/MapWalls.js
Normal file
@@ -0,0 +1,87 @@
|
||||
// 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
|
||||
};
|
||||
Reference in New Issue
Block a user