feat(client): GM wall editor, round-1 fields (#73)

Draw, select, edit and delete walls through the GM panel: the four 5.8
presets, raw switches (preset XOR raw, open excluded from wall.update), a
private label, the one-way arrow + flip (sight_dir names the blind side),
the player-marker switch, door open/close through door.set, secret/locked
with the locked hint and one-click unlock, delete and single-step undo.
No thickness, height or position-flip control (ADR-0012).

The pure rules and payload builders live in src/wallEditor.ts; the GM wall
slice (src/slices/walls.ts) owns the server records, selection and undo;
the render module draws on the gm layer and defines the input precedence:
token drag (#72) > armed wall draw > camera pan (#69), with a click (no
movement) selecting the nearest wall.

Shared core edits: client/src/reducer.ts gains the wall-select and
wall-record-undo events (the only new core events); style.css and the
client README document the panel and rules. Unit tests cover the slice,
presets, derived kind, arrow side, snapping, payload XOR, door intent and
undo builders; the e2e draws every preset, edits the fields, flips the
one-way wall, opens/closes a door, deletes and undoes, proves a token drag
still wins over draw mode, and probes the player's fog pixels, with the
player WS leaking no wall data.

Ref #73
This commit is contained in:
2026-11-21 11:26:45 +01:00
parent 2d9548c941
commit 91a51c706d
9 changed files with 2425 additions and 0 deletions
+325
View File
@@ -0,0 +1,325 @@
// GM wall layer and editor input (T10, #73; DESIGN 5.8, 7.4, 7.2). Walls are GM-only, so this module
// draws into the `gm` layer and arms nothing for a player session. It is the Pixi half of the
// editor: the panel (`src/panels/walls.ts`) owns the armed preset and sends the intents, the slice
// (`src/slices/walls.ts`) owns the server's records, and `src/wallEditor.ts` owns the pure rules.
//
// Input precedence (the thing #73 must not break):
// 1. token drag (#72) wins first: its window-capture `pointerdown` runs before this module's and
// stops immediate propagation when the press lands on a token the session may move;
// 2. wall drawing wins next, but only while a preset is armed: the two clicks are consumed and the
// camera never starts a pan;
// 3. otherwise the camera (#69) keeps pan/pinch, and a click (no movement) selects the wall under
// the pointer. A drag that moved more than a few pixels is a pan, not a selection.
// The e2e arms a preset and starts a drag on a token to prove rule 1 still wins.
//
// A one-way wall's arrow points from the seeing side toward the blind side (`sight_dir` names the
// blind side, GLOSSARY "Blind side"): `arrowVector` is the one place that direction is computed.
import { Container, Graphics } from 'pixi.js'
import type { RenderContext, RenderModule } from '../stage.ts'
import { dragOf } from '../../slices/move.ts'
import type { WallView } from '../../slices/walls.ts'
import { selectedWallId, undoOf, wallsOf } from '../../slices/walls.ts'
import { arrowVector, distanceToSegment, wallMidpoint } from '../../wallEditor.ts'
import type { Vec2 } from '../../protocol.ts'
import { mountWallPanel } from '../../panels/walls.ts'
const COLORS: Record<string, number> = {
wall: 0xb8c4d0,
door: 0xd19a51,
window: 0x54c7c9,
one_way: 0xb989e0,
custom: 0xd8d8d8,
}
interface WallNode {
container: Container
graphics: Graphics
sig: string
}
function wallSignature(wall: WallView, selected: boolean): string {
const s = wall.switches
return [
wall.id,
wall.a.join(','),
wall.b.join(','),
wall.kind,
s.blocksSight ? 1 : 0,
s.blocksMovement ? 1 : 0,
s.sightDir,
s.playerMarker ? 1 : 0,
s.door ? 1 : 0,
s.open ? 1 : 0,
s.secret ? 1 : 0,
s.locked ? 1 : 0,
selected ? 1 : 0,
].join('|')
}
/** A dashed/dotted stroke, used for an open door (nothing is there) and a secret door (GM view). */
function dashedLine(
graphics: Graphics,
a: Vec2,
b: Vec2,
width: number,
color: number,
dash: number,
gap: number,
alpha: number,
): void {
const dx = b[0] - a[0]
const dy = b[1] - a[1]
const length = Math.hypot(dx, dy)
if (!(length > 0)) return
const ux = dx / length
const uy = dy / length
for (let start = 0; start < length; start += dash + gap) {
const end = Math.min(length, start + dash)
graphics
.moveTo(a[0] + ux * start, a[1] + uy * start)
.lineTo(a[0] + ux * end, a[1] + uy * end)
.stroke({ width, color, alpha })
}
}
function drawWall(graphics: Graphics, wall: WallView, selected: boolean): void {
graphics.clear()
const a = wall.a
const b = wall.b
const color = COLORS[wall.kind] ?? COLORS['custom']!
const width = selected ? 6 : 4
const alpha = selected ? 1 : 0.85
const open = wall.switches.door && wall.switches.open
if (selected) {
graphics.moveTo(a[0], a[1]).lineTo(b[0], b[1]).stroke({ width: width + 6, color: 0x7ee787, alpha: 0.25 })
}
if (open) {
// An open door leaves a gap; the schema keeps the record, the GM sees where it was.
dashedLine(graphics, a, b, width, color, 8, 8, 0.55)
} else if (wall.switches.secret) {
dashedLine(graphics, a, b, width, color, 3, 5, alpha)
} else {
graphics.moveTo(a[0], a[1]).lineTo(b[0], b[1]).stroke({ width, color, alpha })
}
// Endpoint handles while selected: the two authoring points, never moved in round 1.
if (selected) {
for (const point of [a, b]) {
graphics.circle(point[0], point[1], 5).fill(0x0b0b0d).stroke({ width: 2, color: 0x7ee787 })
}
}
// Story 50: the one-way arrow at the wall's middle, toward the blind side.
const direction = arrowVector(a, b, wall.switches.sightDir)
if (direction) {
const [mx, my] = wallMidpoint(a, b)
const tip: Vec2 = [mx + direction[0] * 22, my + direction[1] * 22]
const back: Vec2 = [mx - direction[0] * 8, my - direction[1] * 8]
const px = -direction[1]
const py = direction[0]
graphics
.moveTo(back[0], back[1])
.lineTo(tip[0], tip[1])
.stroke({ width: 3, color: 0xfff3c4, alpha: 0.95 })
graphics
.moveTo(tip[0], tip[1])
.lineTo(tip[0] - direction[0] * 9 + px * 6, tip[1] - direction[1] * 9 + py * 6)
.lineTo(tip[0] - direction[0] * 9 - px * 6, tip[1] - direction[1] * 9 - py * 6)
.closePath()
.fill(0xfff3c4)
}
// Story 48: the GM sees the marker switch. A marked window is the only wall a player learns about.
if (wall.switches.playerMarker) {
const [mx, my] = wallMidpoint(a, b)
if (wall.kind === 'window') {
graphics.circle(mx, my, 6).fill(0xffcc33).stroke({ width: 2, color: 0x2b1d00 })
} else {
graphics.circle(mx, my, 5).stroke({ width: 2, color: 0xffcc33, alpha: 0.7 })
}
}
}
export const renderModule: RenderModule = {
name: 'walls',
mount({ app, layers, store }: RenderContext): () => void {
const canvas = app.canvas
const host = document.querySelector<HTMLElement>('#panels')
const panel = host ? mountWallPanel(host, event => store.dispatch(event)) : null
const container = new Container()
layers.gm.addChild(container)
const preview = new Graphics()
container.addChild(preview)
const nodes = new Map<number, WallNode>()
let hover: Vec2 | null = null
let down: { x: number; y: number; pointerId: number } | null = null
function worldOf(event: PointerEvent): Vec2 {
const rect = canvas.getBoundingClientRect()
const local = layers.gm.toLocal({ x: event.clientX - rect.left, y: event.clientY - rect.top })
return [local.x, local.y]
}
function zoom(): number {
const scale = Math.abs(layers.gm.worldTransform.a)
return scale > 0 ? scale : 1
}
function drawPreview(): void {
preview.clear()
if (!panel?.armed || panel.first === null) return
const a = panel.first
const b = hover ?? a
preview
.moveTo(a[0], a[1])
.lineTo(b[0], b[1])
.stroke({ width: 2, color: 0x7ee787, alpha: 0.8 })
preview.circle(a[0], a[1], 4).fill(0x7ee787)
preview.circle(b[0], b[1], 3).stroke({ width: 2, color: 0x7ee787 })
}
function renderWalls(): void {
const state = store.state
const selected = selectedWallId(state)
const walls = wallsOf(state)
const seen = new Set<number>()
for (const wall of walls) {
seen.add(wall.id)
const sig = wallSignature(wall, wall.id === selected)
let node = nodes.get(wall.id)
if (!node) {
const graphics = new Graphics()
const wallContainer = new Container()
wallContainer.addChild(graphics)
container.addChild(wallContainer)
node = { container: wallContainer, graphics, sig: '' }
nodes.set(wall.id, node)
}
if (node.sig !== sig) {
drawWall(node.graphics, wall, wall.id === selected)
node.sig = sig
}
}
for (const [id, node] of nodes) {
if (seen.has(id)) continue
container.removeChild(node.container)
node.container.destroy({ children: true })
nodes.delete(id)
}
}
function publish(): void {
const state = store.state
const walls = wallsOf(state)
const selected = selectedWallId(state)
const dataset = canvas.dataset
dataset['walls'] = String(walls.length)
dataset['wallKinds'] = walls.map(wall => `${wall.id}:${wall.kind}`).join(' ')
dataset['wallSelected'] = selected === null ? '' : String(selected)
dataset['wallArrows'] = walls
.filter(wall => wall.switches.sightDir !== 'both')
.map(wall => `${wall.id}:${wall.switches.sightDir}`)
.join(' ')
dataset['wallMarkers'] = walls
.filter(wall => wall.switches.playerMarker)
.map(wall => String(wall.id))
.join(' ')
dataset['wallDraw'] = panel?.armed ? (panel.first === null ? 'first' : 'second') : 'off'
dataset['wallPreview'] = panel?.first ? panel.first.join(',') : ''
dataset['wallUndo'] = undoOf(state) === null ? 'no' : 'yes'
}
function sync(): void {
renderWalls()
drawPreview()
publish()
}
function onPointerDown(event: PointerEvent): void {
if (event.button !== 0 || !event.isPrimary || event.target !== canvas) return
const state = store.state
if (state.connection.phase !== 'live' || state.session.role !== 'gm' || !panel) return
if (panel.armed) {
// Drawing takes precedence over pan: consume the gesture and feed the point.
event.preventDefault()
event.stopImmediatePropagation()
hover = worldOf(event)
panel.drawPoint(hover)
sync()
return
}
down = { x: event.clientX, y: event.clientY, pointerId: event.pointerId }
}
function onPointerMove(event: PointerEvent): void {
if (!panel?.armed) return
// A token drag owns its gesture (#72); do not paint a wall preview under it.
if (dragOf(store.state) !== null) return
if (!event.isPrimary) return
hover = worldOf(event)
drawPreview()
}
function onPointerUp(event: PointerEvent): void {
const start = down
down = null
if (!panel || panel.armed) return
if (!start || start.pointerId !== event.pointerId) return
if (event.target !== canvas) return
// A moved pointer was a pan (#69), not a click on a wall.
if (Math.hypot(event.clientX - start.x, event.clientY - start.y) > 5) return
if (store.state.session.role !== 'gm') return
const point = worldOf(event)
const tolerance = 8 / zoom()
let hit: WallView | null = null
let best = tolerance
for (const wall of wallsOf(store.state)) {
const distance = distanceToSegment(point, wall.a, wall.b)
if (distance <= best) {
best = distance
hit = wall
}
}
store.dispatch({ type: 'wall-select', id: hit?.id ?? null })
}
function onCancel(): void {
down = null
}
function onKey(event: KeyboardEvent): void {
if (event.key !== 'Escape' || !panel?.armed) return
panel.cancelDraw()
hover = null
sync()
}
const unsubscribe = store.subscribe(() => {
panel?.render(store.state)
sync()
})
panel?.render(store.state)
sync()
window.addEventListener('pointerdown', onPointerDown, true)
window.addEventListener('pointermove', onPointerMove, true)
window.addEventListener('pointerup', onPointerUp, true)
window.addEventListener('pointercancel', onCancel, true)
window.addEventListener('blur', onCancel)
window.addEventListener('keydown', onKey)
return () => {
unsubscribe()
window.removeEventListener('pointerdown', onPointerDown, true)
window.removeEventListener('pointermove', onPointerMove, true)
window.removeEventListener('pointerup', onPointerUp, true)
window.removeEventListener('pointercancel', onCancel, true)
window.removeEventListener('blur', onCancel)
window.removeEventListener('keydown', onKey)
panel?.destroy()
}
},
}