import m, {MithrilComponent} from '@garage44/mithril' import {Alert, Badge, Button, ButtonGroup, Card, FieldNumber, FieldSegmented, FieldText, Spinner} from '@garage44/mithril/components' import type { BuildingDesign, BuildingResponse, OpeningKind, P2, Selection, Stair, Storey, Wall, } from '@/lib/design' import {$s, api, notifier, signupApi} from '@/app' import {GameLink} from '@/components/elements/buildings/buildings' import {ApiError} from '@/lib/api' import { addOpening, addStair, addStorey, addWall, alongWall, clampOpening, clone, copyWindowsUp, dist, flipStair, moveCorner, nearestOnSegment, norm, pointInRing, problems, removeSelected, removeTopStorey, restack, snap, stairRect, slideOpening, spreadWindows, sub, suggestedWindows, toLocal, wallLength, } from '@/lib/design' import {bind, local} from '@/lib/reactive' import {findRooms, labelPoint, nameRoomAt, relabel} from '@/lib/rooms' type Tool = 'door' | 'select' | 'stair' | 'wall' | 'window' /** * The tools, each with its keyboard key. There is no room tool: a room is * whatever the walls enclose, found by `lib/rooms`, and naming one is a tap on * it with Select. */ const TOOLS: {hint: string; id: Tool; key: string; label: string}[] = [ { hint: 'Tap a wall, a door, a window or the stairs to change them — drag a wall’s end or a door to move it. Tap a room to name it. Drag the plan to move around.', id: 'select', key: 'v', label: 'Select', }, {hint: 'Tap where a wall starts, then where it ends. Keep tapping to carry on; Done (or Esc) to stop. Rooms appear by themselves.', id: 'wall', key: 'w', label: 'Wall'}, {hint: 'Tap a wall where the door should be. A door in an outside wall is a front door in the street too.', id: 'door', key: 'd', label: 'Door'}, {hint: 'Tap a wall where the window should be. Windows in outside walls are the building’s windows in the street.', id: 'window', key: 'n', label: 'Window'}, {hint: 'Tap where the stairs start, then the direction they climb. They rise to the floor above.', id: 'stair', key: 's', label: 'Stairs'}, ] /** How far, in screen pixels, a tap may be from a thing and still hit it. */ const HIT_PX = 14 /** The session to edit with: the player's, or an operator's on the admin side. */ const client = () => ($s.signup.token ? signupApi : api) interface View { cx: number cy: number /** Metres across the plan. */ w: number } interface BuildingAttrs { pand?: string } /** * The floor-plan editor for one building (`/building/{pand}`). * * Opens with the building's real outline and whatever design it has — its * stored one, or the one the game would generate from the registers, which is * where every owner starts. The plan is one storey at a time, north up, metres * on a grid; the storey below shows faintly underneath, because a staircase has * to land somewhere. * * Everything here edits a draft. Nothing reaches the world until Save, and the * game picks the save up within half a minute (`systems::building_designs`). */ export class Building extends MithrilComponent { s = local({ busy: false, cursor: null as P2 | null, data: null as BuildingResponse | null, design: null as BuildingDesign | null, dirty: false, error: '', /** An enclosed area nobody has named yet, tapped with Select. */ face: null as P2[] | null, future: [] as BuildingDesign[], history: [] as BuildingDesign[], k: 0, pending: null as P2 | null, sel: null as Selection | null, tool: 'select' as Tool, view: {cx: 0, cy: 0, w: 30} as View, }) /** The `pand` the last load was for, so a new one in the route reloads. */ private pand: string | undefined = undefined private svg: SVGSVGElement | null = null /** Numbers being typed and not yet applied, by selection and field (see `num` in the view). */ private drafts = new Map() private drag: { /** A wall end or an opening being dragged, with the draft before it. */ edit?: {before: BuildingDesign; from: P2; target: {kind: 'corner'} | {id: string; kind: 'opening'; wallId: string}} moved: boolean start: [number, number] view: View } | null = null /** The outline and neighbours, recomputed only when the data or the origin moves. */ private memo: { data: BuildingResponse | null lat: number | undefined lng: number | undefined neighbours: P2[][] outline: P2[] } | null = null private load = async () => { const s = this.s const pand = this.pand ?? '' const query = new URLSearchParams(window.location.search) const hint = query.get('lat') && query.get('lng') ? `?lat=${query.get('lat')}&lng=${query.get('lng')}` : '' if (!$s.signup.token && !$s.profile.token) { s.error = 'Sign in to open the building editor.' return } try { const r = await client().get(`/api/pipeline/buildings/${pand}${hint}`) s.data = r s.design = r.design ? clone(r.design) : null s.history = [] s.future = [] s.dirty = false s.sel = null s.k = 0 if (r.design) { s.view = fit(r) } } catch (e) { s.error = e instanceof ApiError ? e.message : 'Could not open that building' } } private syncPand(vnode: m.Vnode) { const pand = vnode.attrs.pand ?? '' if (pand === this.pand) { return } this.pand = pand void this.load() } // The keyboard, for whoever has one. Registered once, for the life of the // page; the handler reads the current draft whenever a key arrives. private onKey = (e: KeyboardEvent) => this.keys(e) // Leaving with unsaved work is the one mistake this page can make for you. private warn = (e: BeforeUnloadEvent) => { if (this.s.dirty) { e.preventDefault() } } oninit(vnode: m.Vnode) { this.syncPand(vnode) } onupdate(vnode: m.VnodeDOM) { this.syncPand(vnode) } oncreate() { window.addEventListener('keydown', this.onKey) window.addEventListener('beforeunload', this.warn) } onremove() { window.removeEventListener('keydown', this.onKey) window.removeEventListener('beforeunload', this.warn) } // ── Derived values ───────────────────────────────────────────────────── private memoised() { const {data, design} = this.s const lat = design?.origin[0] const lng = design?.origin[1] const memo = this.memo if (memo && memo.data === data && memo.lat === lat && memo.lng === lng) { return memo } const outline: P2[] = data?.facts && design ? data.facts.outline.map(([la, lo]) => toLocal(design.origin, la, lo)) : [] const neighbours: P2[][] = data?.facts && design ? data.facts.neighbours.map((r) => r.map(([la, lo]) => toLocal(design.origin, la, lo))) : [] this.memo = {data, lat, lng, neighbours, outline} return this.memo } private get outline() { return this.memoised().outline } private get canEdit() { return !!this.s.data?.access.can_edit } private get storey(): Storey { const design = this.s.design! return design.storeys[Math.min(this.s.k, design.storeys.length - 1)] } private get pxPerM() { return this.svg ? this.svg.clientWidth / this.s.view.w : 20 } private get hitM() { return HIT_PX / this.pxPerM } // ── Selection details ────────────────────────────────────────────────── private get selWall() { const sel = this.s.sel return sel?.kind === 'wall' ? this.storey.walls.find((w) => w.id === sel.id) : undefined } private get selOpeningWall() { const sel = this.s.sel return sel?.kind === 'opening' ? this.storey.walls.find((w) => w.id === sel.wallId) : undefined } private get selOpening() { const sel = this.s.sel return this.selOpeningWall?.openings.find((o) => sel && o.id === sel.id) } /** Make `next` the draft, with an undo step back to the current one. */ private commit = (next: BuildingDesign, before: BuildingDesign = this.s.design!) => { const s = this.s // Rooms follow their walls: every change re-finds them, and a named room // keeps its name wherever its walls went. for (const st of next.storeys) { st.rooms = relabel(st) } s.history = [...s.history.slice(-60), before] s.future = [] s.design = next s.dirty = true } /** Change the draft through a function, with an undo step. */ private mutate = (fn: (d: BuildingDesign, s: Storey) => void) => { const next = clone(this.s.design!) fn(next, next.storeys[Math.min(this.s.k, next.storeys.length - 1)]) this.commit(next) } private undo = () => { const s = this.s const prev = s.history[s.history.length - 1] if (!prev) { return } const design = s.design! s.future = [design, ...s.future] s.history = s.history.slice(0, -1) s.design = prev s.sel = null s.dirty = true } private redo = () => { const s = this.s const [next, ...rest] = s.future if (!next) { return } s.history = [...s.history, s.design!] s.future = rest s.design = next s.sel = null s.dirty = true } private save = async () => { const s = this.s s.busy = true try { const r = await client().put<{design: BuildingDesign}>( `/api/pipeline/buildings/${this.pand ?? ''}/design`, s.design, ) s.design = r.design s.data = {...s.data!, design: r.design, stored: true} s.dirty = false notifier.success('Saved. The game shows it within half a minute.') } catch (e) { notifier.error(e instanceof ApiError ? e.message : 'Could not save the design') } s.busy = false } private reset = async () => { const s = this.s s.busy = true try { if (s.data!.stored) { await client().delete(`/api/pipeline/buildings/${this.pand ?? ''}/design`) } notifier.success('Back to the layout the game generates') await this.load() } catch (e) { notifier.error(e instanceof ApiError ? e.message : 'Could not reset the design') } s.busy = false } private keys(e: KeyboardEvent) { const s = this.s // Only once the editor is on screen, as under Preact, where the handler // was installed by the full render. if (s.error || !s.data || !s.design) { return } const canEdit = this.canEdit // The tool and storey pickers are radio inputs, and one keeps the focus // after a click; that is choosing, not typing, so the keys still work. const typing = (e.target instanceof HTMLInputElement && e.target.type !== 'radio') || e.target instanceof HTMLTextAreaElement if (typing) { return } const mod = e.ctrlKey || e.metaKey if (mod && e.key.toLowerCase() === 'z') { e.preventDefault() if (e.shiftKey) { this.redo() } else { this.undo() } } else if (mod && e.key.toLowerCase() === 'y') { e.preventDefault() this.redo() } else if (e.key === 'Escape') { s.pending = null s.sel = null s.face = null } else if ((e.key === 'Delete' || e.key === 'Backspace') && s.sel && canEdit) { e.preventDefault() const was = s.sel this.mutate((_, st) => removeSelected(st, was)) s.sel = null } else if (!mod && canEdit) { const t = TOOLS.find((x) => x.key === e.key.toLowerCase()) if (t) { this.pick(t.id) } } } private pick = (tid: Tool) => { const s = this.s s.tool = tid s.pending = null s.sel = null s.face = null } // ── Pointer handling ─────────────────────────────────────────────────── private toPlan = (e: PointerEvent | WheelEvent): P2 | null => { const el = this.svg const ctm = el?.getScreenCTM() if (!el || !ctm) { return null } const pt = new DOMPoint(e.clientX, e.clientY).matrixTransform(ctm.inverse()) // SVG y runs down; the plan's runs north. return [pt.x, -pt.y] } private snapped = (p: P2) => snap(p, this.storey, this.outline, this.hitM) private hitTest = (p: P2): Selection | null => { const storey = this.storey const hitM = this.hitM for (const w of storey.walls) { for (const o of w.openings) { const c = alongWall(w, o.at) if (dist(c, p) <= Math.max(hitM, o.width / 2)) { return {id: o.id, kind: 'opening', wallId: w.id} } } } for (const s of storey.stairs) { if (pointInRing(stairRect(s), p)) { return {id: s.id, kind: 'stair'} } } let best: {d: number; id: string} | null = null for (const w of storey.walls) { const d = nearestOnSegment(p, w.a, w.b).off if (d < hitM && (!best || d < best.d)) { best = {d, id: w.id} } } if (best) { return {id: best.id, kind: 'wall'} } const room = storey.rooms.find((r) => pointInRing(r.outline, p)) return room ? {id: room.id, kind: 'room'} : null } /** The unnamed enclosed area under `p`, if the tap found nothing else. */ private faceAt = (p: P2): P2[] | null => findRooms(this.storey).find((f) => pointInRing(f, p)) ?? null private tap = (raw: P2) => { const s = this.s const tool = s.tool const design = s.design! const k = s.k if (!this.canEdit && tool !== 'select') { return } const p = this.snapped(raw) switch (tool) { case 'select': { const hit = this.hitTest(raw) s.sel = hit s.face = hit ? null : this.faceAt(raw) return } case 'wall': if (!s.pending) { s.pending = p } else if (dist(s.pending, p) < 0.05) { s.pending = null } else { const from = s.pending this.mutate((_, st) => { addWall(st, from, p) }) s.pending = p } return case 'door': case 'window': { const kind: OpeningKind = tool // Tried on a copy first: a tap that misses every wall must not // leave an undo step that undoes nothing. const next = clone(design) if (addOpening(next.storeys[k], raw, kind, this.hitM * 2)) { this.commit(next) } else { notifier.error(`Tap on a wall to put a ${kind} in it`) } return } case 'stair': if (!s.pending) { s.pending = p } else { const foot = s.pending if (k === design.storeys.length - 1) { notifier.error('Stairs climb to the floor above — add a storey first') } else { this.mutate((_, st) => { addStair(st, foot, p) }) } s.pending = null } return } } private onPointerDown = (e: PointerEvent) => { const s = this.s ;(e.target as Element).setPointerCapture?.(e.pointerId) this.drag = {moved: false, start: [e.clientX, e.clientY], view: s.view} // With Select, a press on the selected wall's end or the selected // opening grabs it instead of the plan. const p = this.toPlan(e) if (!p || s.tool !== 'select' || !this.canEdit) { return } const design = s.design! const grab = this.hitM * 1.5 const selWall = this.selWall const selOpening = this.selOpening const selOpeningWall = this.selOpeningWall if (selWall) { const end = [selWall.a, selWall.b].find((c) => dist(c, p) < grab) if (end) { this.drag.edit = {before: design, from: end, target: {kind: 'corner'}} } } else if (selOpening && selOpeningWall) { if (dist(alongWall(selOpeningWall, selOpening.at), p) < Math.max(grab, selOpening.width / 2)) { this.drag.edit = { before: design, from: p, target: {id: selOpening.id, kind: 'opening', wallId: selOpeningWall.id}, } } } } private onPointerMove = (e: PointerEvent) => { const s = this.s const p = this.toPlan(e) if (p) { s.cursor = this.snapped(p) } const d = this.drag if (!d) { return } const dx = e.clientX - d.start[0] const dy = e.clientY - d.start[1] if (!d.moved && Math.hypot(dx, dy) < 6) { return } d.moved = true if (d.edit && p) { // Rebuilt from the draft as it was at the press, so the drag is one // move however many frames it took, and one undo step. const next = clone(d.edit.before) const st = next.storeys[Math.min(s.k, next.storeys.length - 1)] const target = d.edit.target if (target.kind === 'corner') { const others: Storey = {...st, walls: st.walls.filter((w) => dist(w.a, d.edit!.from) > 0.03 && dist(w.b, d.edit!.from) > 0.03)} moveCorner(st, d.edit.from, snap(p, others, this.outline, this.hitM)) } else { const wall = st.walls.find((w) => w.id === target.wallId) if (wall) { slideOpening(wall, target.id, p) } } s.design = next return } // Any tool pans on a drag: on a phone there is no other way to move. const pxPerM = this.pxPerM s.view = {...d.view, cx: d.view.cx - dx / pxPerM, cy: d.view.cy + dy / pxPerM} } private onPointerUp = (e: PointerEvent) => { const d = this.drag this.drag = null if (d?.edit && d.moved) { this.commit(clone(this.s.design!), d.edit.before) return } if (d && !d.moved) { const p = this.toPlan(e) if (p) { this.tap(p) } } } private onWheel = (e: WheelEvent) => { e.preventDefault() this.zoom(e.deltaY > 0 ? 1.15 : 1 / 1.15) } private zoom = (f: number) => { const view = this.s.view this.s.view = {...view, w: Math.max(4, Math.min(200, view.w * f))} } view() { const s = this.s const pand = this.pand ?? '' const {data, design, error, face, k, pending, cursor, sel, tool, view, history, future, busy, dirty} = s if (error) { return (

{error}

) } if (!data) { return (
Opening the building…
) } if (!design) { return (

This building is too small to have an inside, or the map data for it is not on this server yet.

) } const {outline, neighbours} = this.memoised() const canEdit = data.access.can_edit const storey = this.storey const below = k > 0 ? design.storeys[k - 1] : null const pxPerM = this.pxPerM const mutate = this.mutate const selWall = this.selWall const selOpeningWall = this.selOpeningWall const selOpening = this.selOpening const selStair = sel?.kind === 'stair' ? storey.stairs.find((st) => st.id === sel.id) : undefined const selRoom = sel?.kind === 'room' ? storey.rooms.find((r) => r.id === sel.id) : undefined const faces = findRooms(storey) const unnamed = faces.filter((f) => !storey.rooms.some((r) => pointInRing(f, labelPoint(r.outline)))) /** Edit the selected thing in a fresh draft, found again by id. */ const editSel = (fn: (x: {opening?: Wall['openings'][0]; room?: Storey['rooms'][0]; stair?: Stair; wall?: Wall}) => void) => mutate((_, st) => { if (!sel) { return } const wall = st.walls.find((w) => w.id === (sel.kind === 'opening' ? sel.wallId : sel.id)) fn({ opening: sel.kind === 'opening' ? wall?.openings.find((o) => o.id === sel.id) : undefined, room: st.rooms.find((r) => r.id === sel.id), stair: st.stairs.find((x) => x.id === sel.id), wall: sel.kind === 'wall' ? wall : undefined, }) }) /** * A number field for a property of the selection (or of the storey). * * `FieldNumber` writes its model on every keystroke, and a write here is * an edit with an undo step — typing "1.25" would be four of them, and * a minimum clamped mid-word ("0" of "0.3" against 0.02) would rewrite * the box under the cursor. So keystrokes land in a draft, and the * draft is applied, clamped, when the field commits: on `change` (blur * or Enter) and after a stepper press or an arrow key, which bubble up * to the wrapper after the field has written the draft. The old field * applied on `change` only; the steppers are new. */ const num = (key: string, label: string, value: number, step: number, set: (v: number) => void, min = 0, max = 100) => { const id = `${sel ? sel.id : `storey-${k}`}:${key}` const drafts = this.drafts const apply = () => { if (!drafts.has(id)) { return } const v = drafts.get(id) drafts.delete(id) if (v != null && Number.isFinite(v)) { set(Math.max(min, Math.min(max, v))) } } return (
{ if ((e.target as Element).closest('.m-input__step')) { apply() } }} onkeyup={(e: KeyboardEvent) => { if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { apply() } }} >
) } /** * A name field. It writes on `change` (`lazy`), as the old one did, so a * name is one undo step and not one per letter. `FieldText` has no * `maxlength`, so the 120-character cap the input carried is a slice. */ const nameField = (key: string, value: string, set: (v: string) => void, placeholder?: string) => ( ) const issues = problems(design, data.facts?.height ?? null) const toolHint = TOOLS.find((t) => t.id === tool)?.hint // ── The plan ─────────────────────────────────────────────────────────── const aspect = 0.75 const h = view.w * aspect const vb = `${view.cx - view.w / 2} ${-view.cy - h / 2} ${view.w} ${h}` const pts = (r: P2[]) => r.map((p) => `${p[0]},${-p[1]}`).join(' ') const stroke = (metres: number) => Math.max(metres, 1.5 / pxPerM) const grid: m.Vnode[] = [] { const step = view.w > 60 ? 5 : 1 const x0 = Math.floor((view.cx - view.w / 2) / step) * step const y0 = Math.floor((view.cy - h / 2) / step) * step for (let x = x0; x <= view.cx + view.w / 2; x += step) { grid.push() } for (let y = y0; y <= view.cy + h / 2; y += step) { grid.push() } } const drawWall = (w: Wall, faint: boolean) => { const selected = sel?.kind === 'wall' && sel.id === w.id const width = stroke(w.exterior ? 0.24 : Math.max(w.thickness, 0.08)) return ( {!faint && w.openings.map((o) => { const d = norm(sub(w.b, w.a)) ?? ([1, 0] as P2) const p = alongWall(w, o.at - o.width / 2) const q = alongWall(w, o.at + o.width / 2) const left: P2 = [-d[1], d[0]] const leaf: P2 = [p[0] + left[0] * o.width, p[1] + left[1] * o.width] const isSel = sel?.kind === 'opening' && sel.id === o.id // The swing, from the open leaf round to the far jamb about // the hinge — in screen coordinates (y down), where a // positive cross product is clockwise, which is SVG's // sweep flag 1. const u = [leaf[0] - p[0], -(leaf[1] - p[1])] const v = [q[0] - p[0], -(q[1] - p[1])] const sweep = u[0] * v[1] - u[1] * v[0] > 0 ? 1 : 0 return ( {o.kind === 'door' ? ( <> ) : ( )} ) })} ) } const drawStair = (st: Stair, faint: boolean) => { const r = stairRect(st) const d = norm(st.dir) ?? ([1, 0] as P2) const isSel = sel?.kind === 'stair' && sel.id === st.id const treads = [] for (let i = 1; i < 16; i++) { const t = (i / 16) * st.run const a: P2 = [r[0][0] + d[0] * t, r[0][1] + d[1] * t] const b: P2 = [r[3][0] + d[0] * t, r[3][1] + d[1] * t] treads.push() } const mid0: P2 = [(r[0][0] + r[3][0]) / 2, (r[0][1] + r[3][1]) / 2] const mid1: P2 = [(r[1][0] + r[2][0]) / 2, (r[1][1] + r[2][1]) / 2] return ( {treads} ) } const area = (r: P2[]) => Math.abs(r.reduce((a, p, i) => a + p[0] * r[(i + 1) % r.length][1] - r[(i + 1) % r.length][0] * p[1], 0) / 2) const owner = data.access.owned_by const [lat, lng] = design.origin // ── The properties panel: whatever is selected, or the storey ──────────── let panel: {actions?: m.Children; body: m.Children; title: string} if (selWall) { panel = { actions: canEdit && ( <> {k + 1 < design.storeys.length && selWall.openings.some((o) => o.kind === 'window') && ( )} ), body: ( <>

{wallLength(selWall).toFixed(2)} m long. {canEdit && ' Drag either end to move it; the walls meeting it come along.'}

{!selWall.exterior && (
{num('thickness', 'Thickness', selWall.thickness, 0.01, (v) => editSel(({wall}) => wall && (wall.thickness = v)), 0.02, 1.5)}
)} ), title: selWall.exterior ? 'Outer wall' : 'Wall', } } else if (selOpening && selOpeningWall) { panel = { body: ( <>
{num('width', 'Width', selOpening.width, 0.05, (v) => editSel(({opening}) => { if (opening) { opening.width = Math.min(v, wallLength(selOpeningWall)) opening.at = clampOpening(opening.at, opening.width, wallLength(selOpeningWall)) } }), 0.2, 6)} {num('height', 'Height', selOpening.height, 0.05, (v) => editSel(({opening}) => opening && (opening.height = v)), 0.2, 10)} {num('sill', 'Sill', selOpening.sill, 0.05, (v) => editSel(({opening}) => opening && (opening.sill = v)), 0, 5)} {num('at', 'From the wall end', selOpening.at, 0.05, (v) => editSel(({opening}) => opening && (opening.at = clampOpening(v, opening.width, wallLength(selOpeningWall)))), 0, 100)}

{selOpening.kind === 'door' ? 'A door with a sill above 30 cm cannot be walked through.' : 'A window is a hole you can see through, not walk through.'}

), title: selOpening.kind === 'door' ? 'Door' : 'Window', } } else if (selStair) { panel = { actions: canEdit && ( ), body: (
{num('width', 'Width', selStair.width, 0.05, (v) => editSel(({stair}) => stair && (stair.width = v)), 0.5, 5)} {num('run', 'Length', selStair.run, 0.1, (v) => editSel(({stair}) => stair && (stair.run = v)), 1, 15)}
), title: 'Stairs', } } else if (selRoom) { panel = { body: ( <> {nameField(`room-${selRoom.id}`, selRoom.name, (v) => editSel(({room}) => room && (room.name = v)))}

{area(selRoom.outline).toFixed(1)} m². The name shows in the game when somebody stands in it.

), title: 'Room', } } else if (face) { panel = { body: ( <> {nameField( 'face', '', (raw) => { const v = raw.trim() if (!v) { return } const at = labelPoint(face) let id: null | string = null mutate((_, st) => { id = nameRoomAt(st, at, v) }) if (id) { s.sel = {id, kind: 'room'} s.face = null } }, 'Keuken, Woonkamer…', )}

{area(face).toFixed(1)} m², not named yet.

), title: 'Room', } } else { panel = { actions: canEdit && k === design.storeys.length - 1 && design.storeys.length > 1 && ( ), body: ( <>
{nameField(`storey-${k}`, storey.name, (v) => mutate((_, st) => { st.name = v }))} {num('height', 'Floor to floor', storey.height, 0.1, (v) => mutate((d, st) => { st.height = v restack(d) }), 2, 12)}

{storey.walls.length} walls, {storey.walls.reduce((a, w) => a + w.openings.length, 0)} doors and windows, {storey.stairs.length} stairs. Floor at {storey.elevation.toFixed(2)} m.

), title: storey.name, } } const deleteSel = canEdit && sel && ( ) return (

{owner ? `${owner}'s building` : 'Building'}

{pand}
{!canEdit && ( You can look, not change: only the building's owner can.{' '} {data.stored ? 'This is the design its owner saved.' : 'Nobody has designed it yet; this is what the game generates.'} )} {/* * One storey is shown at a time, so the storeys are a one-of * choice. Picking one drops the selection and any wall or * flight half drawn, which belong to the storey they were on. */}
({label: st.name || `Storey ${i}`, value: i}))} size="sm" /> {canEdit && ( )}
{canEdit && (
this.pick(t)} options={TOOLS.map((t) => ({ label: {t.label}, value: t.id, }))} size="sm" /> {pending && ( )}
)} {canEdit && toolHint &&

{toolHint}

}
{ this.svg = v.dom as SVGSVGElement }} onpointerdown={this.onPointerDown} onpointerleave={() => { s.cursor = null }} onpointermove={this.onPointerMove} onpointerup={this.onPointerUp} onremove={() => { this.svg = null }} onwheel={this.onWheel} preserveAspectRatio="xMidYMid meet" viewBox={vb} > {grid} {neighbours.map((r, i) => ( ))} {outline.length > 2 && ( )} {storey.floor.length > 2 && } {/* Over the floor, or the floor hides it: the storey below, faint, because a staircase has to land somewhere. */} {below && {below.walls.map((w) => drawWall(w, true))}} {/* The flights arriving from below, as the hole they come up through — under this storey's own flights, which may rise out of the same well. */} {below?.stairs.map((st) => ( ))} {unnamed.map((f, i) => { const c = labelPoint(f) const isSel = face !== null && pointInRing(f, labelPoint(face)) return ( {isSel && } {area(f).toFixed(1)} m² ) })} {storey.rooms.map((r) => { const c = labelPoint(r.outline) const isSel = sel?.kind === 'room' && sel.id === r.id return ( {r.name} {area(r.outline).toFixed(1)} m² ) })} {storey.walls.map((w) => drawWall(w, false))} {storey.stairs.map((st) => drawStair(st, false))} {pending && cursor && ( {dist(pending, cursor).toFixed(2)} m )} {cursor && tool !== 'select' && } N ↑
{panel.body} {issues.length > 0 && ( {issues.map((p) => (

{p}

))}
)}

In the game, walk in — or tap the building and choose Look inside — and pick a floor on the right. The floor names are yours, and a room's name shows when somebody stands in it.

{/* * Pinned to the bottom of the window: on a phone the plan and the * panel are taller than the screen, and Save must not be a * scroll away from the edit it saves. */} {canEdit && (
{dirty ? 'Unsaved changes' : data.stored ? 'Saved' : 'Not saved yet'}
)}
) } } /** A view framing the building's outline (or its ground floor) with a margin. */ function fit(r: BuildingResponse): View { const d = r.design if (!d) { return {cx: 0, cy: 0, w: 30} } const pts: P2[] = r.facts?.outline.length ? r.facts.outline.map(([la, lo]) => toLocal(d.origin, la, lo)) : d.storeys[0]?.floor ?? [] if (!pts.length) { return {cx: 0, cy: 0, w: 30} } const xs = pts.map((p) => p[0]) const ys = pts.map((p) => p[1]) const [x0, x1, y0, y1] = [Math.min(...xs), Math.max(...xs), Math.min(...ys), Math.max(...ys)] return {cx: (x0 + x1) / 2, cy: (y0 + y1) / 2, w: Math.max(12, Math.max(x1 - x0, (y1 - y0) / 0.75) * 1.35)} }