feat(client): tokens, BitmapText labels, statuses and lists (#71)
- slices/tokens.ts: token upsert, token_gone, hello reset; status names only. - slices/characters.ts + slices/encountered.ts: own characters, encountered list with grey entries; the GM's per-player copies are ignored. - render/tokenBoard.ts (pure): event -> entered/updated/left patches, so one changed token costs one container; seeds from the reducer on mount. - render/modules/tokens.ts: pooled world-coordinate nodes on the tokens layer, one shared unit-circle context per kind, BitmapText from one pre-installed BitmapFont assigned only on a name change, stuck/squeezed badges, canvas dataset instrumentation for the "only changed entities" criterion. - panels/lists.ts + render/modules/lists.ts: #lists under #panels, textContent only, greyed encountered rows; mounted by a render module so no shared bootstrap file changed.
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
// Mounts the lists panel under the shell's `#panels` host when the stage comes up (T8, #71). A
|
||||
// render module rather than a `main.ts` edit: the stage's lifetime is the live session's, so a
|
||||
// panel cannot outlive a sign-in screen, and parallel tickets add their own panels without touching
|
||||
// a shared registry.
|
||||
|
||||
import type { RenderModule } from '../stage.ts'
|
||||
import { mountListsPanel } from '../../panels/lists.ts'
|
||||
|
||||
export const renderModule: RenderModule = {
|
||||
name: 'lists',
|
||||
mount({ store }): (() => void) | void {
|
||||
const host = document.querySelector<HTMLElement>('#panels')
|
||||
if (!host) return
|
||||
return mountListsPanel(host, store)
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
// The token layer (T8, #71; DESIGN 7.1 layer 3, 6.1 `token`/`token_gone`).
|
||||
//
|
||||
// Pooled containers in world coordinates on the `tokens` layer: the camera (#69) transforms the
|
||||
// layer's parent, never the tokens, so pan/zoom composes without this module knowing about it.
|
||||
// A name is a `BitmapText` from one pre-installed `BitmapFont` (DESIGN 7.2, Q17): atlased once, and
|
||||
// the text is re-assigned only when the name actually changed, never per frame. Statuses are drawn
|
||||
// as badges (story 30): a barred ring for `stuck`, inward brackets for `squeezed`.
|
||||
//
|
||||
// Everything here is driven by TokenBoard patches, so one changed token touches one container and
|
||||
// the untouched ones are not visited (DESIGN 7.2). The counts are published on the canvas dataset
|
||||
// so the browser e2e can prove that rule against the real server (`data-token-updated` etc).
|
||||
|
||||
import { BitmapFont, BitmapText, Container, Graphics, GraphicsContext } from 'pixi.js'
|
||||
import type { RenderContext, RenderModule } from '../stage.ts'
|
||||
import { TokenBoard } from '../tokenBoard.ts'
|
||||
import type { TokenPatch } from '../tokenBoard.ts'
|
||||
import { tokensOf } from '../../slices/tokens.ts'
|
||||
import type { TokenView } from '../../slices/tokens.ts'
|
||||
|
||||
/** One font for every token label: Latin (incl. accented) and the ASCII the names use. */
|
||||
const TOKEN_FONT = 'fff-token-label'
|
||||
const LABEL_SIZE = 16
|
||||
const GLYPHS: Array<string | [string, string]> = [
|
||||
[' ', '~'],
|
||||
['\u00A0', '\u00FF'],
|
||||
['\u0100', '\u017F'],
|
||||
'×·—–’‘“”…€£¥°',
|
||||
]
|
||||
|
||||
let fontInstalled = false
|
||||
function installFont(): void {
|
||||
if (fontInstalled) return
|
||||
fontInstalled = true
|
||||
BitmapFont.install({
|
||||
name: TOKEN_FONT,
|
||||
style: {
|
||||
fontFamily: 'Arial, Helvetica, sans-serif',
|
||||
fontSize: LABEL_SIZE,
|
||||
fill: 0xffffff,
|
||||
stroke: { color: 0x000000, width: 3 },
|
||||
},
|
||||
chars: GLYPHS,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared unit-circle contexts, tessellated once and scaled to each token's radius (DESIGN 7.2:
|
||||
* one GraphicsContext per colour, never one Graphics per token per frame).
|
||||
*/
|
||||
const BODY_STYLES = {
|
||||
mine: new GraphicsContext()
|
||||
.circle(0, 0, 1)
|
||||
.fill(0xe8bf4f)
|
||||
.stroke({ width: 0.09, color: 0xfff3c4 }),
|
||||
other: new GraphicsContext()
|
||||
.circle(0, 0, 1)
|
||||
.fill(0x4f7fbf)
|
||||
.stroke({ width: 0.09, color: 0xc7dcff }),
|
||||
gmOnly: new GraphicsContext()
|
||||
.circle(0, 0, 1)
|
||||
.fill(0x6d4f86)
|
||||
.stroke({ width: 0.09, color: 0xc9a5e0 }),
|
||||
} as const
|
||||
|
||||
interface TokenNode {
|
||||
container: Container
|
||||
body: Graphics
|
||||
label: BitmapText
|
||||
badges: Graphics
|
||||
/** What this node currently shows, so a patch can skip work that would change nothing. */
|
||||
shape: 'mine' | 'other' | 'gmOnly' | null
|
||||
statusKey: string
|
||||
}
|
||||
|
||||
function createNode(radius: number): TokenNode {
|
||||
const container = new Container()
|
||||
const body = new Graphics()
|
||||
const label = new BitmapText({
|
||||
text: '',
|
||||
style: { fontFamily: TOKEN_FONT, fontSize: LABEL_SIZE },
|
||||
roundPixels: true,
|
||||
})
|
||||
label.anchor.set(0.5, 0)
|
||||
label.y = radius + 2
|
||||
const badges = new Graphics()
|
||||
badges.visible = false
|
||||
container.addChild(body, label, badges)
|
||||
return { container, body, label, badges, shape: null, statusKey: '' }
|
||||
}
|
||||
|
||||
function shapeOf(view: TokenView): 'mine' | 'other' | 'gmOnly' {
|
||||
if (view.gmOnly) return 'gmOnly'
|
||||
return view.mine ? 'mine' : 'other'
|
||||
}
|
||||
|
||||
/** The badge glyphs. Two statuses stack; a cause is never drawn (players do not know it, 5.5). */
|
||||
function drawBadges(graphics: Graphics, radius: number, statuses: readonly string[]): void {
|
||||
graphics.clear()
|
||||
if (statuses.length === 0) {
|
||||
graphics.visible = false
|
||||
return
|
||||
}
|
||||
graphics.visible = true
|
||||
const size = Math.max(6, Math.min(12, radius * 0.42))
|
||||
const x = radius * 0.62
|
||||
let y = -radius * 0.62
|
||||
for (const status of statuses) {
|
||||
if (status === 'stuck') {
|
||||
// A barred ring: the token is held in place.
|
||||
graphics.circle(x, y, size).fill(0x2a0505).stroke({ width: 2, color: 0xff4d4d })
|
||||
graphics
|
||||
.moveTo(x - size * 0.62, y + size * 0.62)
|
||||
.lineTo(x + size * 0.62, y - size * 0.62)
|
||||
.stroke({ width: 2, color: 0xff4d4d })
|
||||
} else if (status === 'squeezed') {
|
||||
// Two arrows pressing inward: the token has no room.
|
||||
const half = size * 0.55
|
||||
graphics.circle(x, y, size).fill(0x2a1a02).stroke({ width: 2, color: 0xffb02e })
|
||||
graphics
|
||||
.moveTo(x - half, y)
|
||||
.lineTo(x - half * 0.2, y)
|
||||
.moveTo(x + half, y)
|
||||
.lineTo(x + half * 0.2, y)
|
||||
.stroke({ width: 2, color: 0xffb02e })
|
||||
} else {
|
||||
// A status this client has no badge for still shows that something is wrong.
|
||||
graphics.circle(x, y, size).fill(0x222222).stroke({ width: 2, color: 0xcccccc })
|
||||
}
|
||||
y += size * 2.3
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw one token. `at` is the position to draw at; it is the server's `pos` today, and the drag
|
||||
* module (#72) passes its optimistic position here while a drag is in flight, so prediction and
|
||||
* rollback never touch the reducer's authoritative view.
|
||||
*/
|
||||
function applyView(
|
||||
node: TokenNode,
|
||||
view: TokenView,
|
||||
stats: { labels: number },
|
||||
at: readonly [number, number] = view.pos,
|
||||
): void {
|
||||
const container = node.container
|
||||
container.position.set(at[0], at[1])
|
||||
container.alpha = view.gmOnly ? 0.72 : 1
|
||||
|
||||
const shape = shapeOf(view)
|
||||
if (shape !== node.shape) {
|
||||
node.body.context = BODY_STYLES[shape]
|
||||
node.shape = shape
|
||||
}
|
||||
// A unit circle scaled to the effective radius: one shared context for every token of a kind.
|
||||
node.body.scale.set(view.radius)
|
||||
node.body.visible = true
|
||||
|
||||
// The label is built once per name: assigning `text` re-lays out quads, so only do it on a change.
|
||||
if (node.label.text !== view.name) {
|
||||
node.label.text = view.name
|
||||
stats.labels += 1
|
||||
}
|
||||
node.label.y = view.radius + 2
|
||||
|
||||
// Badge size and place follow the radius, so a resize redraws them too.
|
||||
const statusKey = `${view.radius}:${view.statuses.join(' ')}`
|
||||
if (statusKey !== node.statusKey) {
|
||||
drawBadges(node.badges, view.radius, view.statuses)
|
||||
node.statusKey = statusKey
|
||||
}
|
||||
}
|
||||
|
||||
/** Publish the renderer's own counts, so the e2e can observe "only changed entities touched". */
|
||||
function publish(canvas: HTMLCanvasElement, board: TokenBoard, patch: TokenPatch, labels: number): void {
|
||||
const datasets = canvas.dataset
|
||||
datasets['tokens'] = String(board.size)
|
||||
datasets['tokenEntered'] = String(patch.entered.length)
|
||||
datasets['tokenUpdated'] = String(patch.updated.length)
|
||||
datasets['tokenLeft'] = String(patch.left.length)
|
||||
datasets['tokenLabels'] = String(labels)
|
||||
datasets['tokenIds'] = [...board.views.keys()].join(' ')
|
||||
datasets['tokenStatuses'] = [...board.views.values()]
|
||||
.flatMap(view => view.statuses.map(status => `${view.id}:${status}`))
|
||||
.join(' ')
|
||||
}
|
||||
|
||||
export const renderModule: RenderModule = {
|
||||
name: 'tokens',
|
||||
mount(context: RenderContext): (() => void) | void {
|
||||
installFont()
|
||||
const { store, app, layers } = context
|
||||
const canvas = app.canvas
|
||||
const board = new TokenBoard()
|
||||
const pool: TokenNode[] = []
|
||||
const nodes = new Map<string, TokenNode>()
|
||||
const stats = { labels: 0 }
|
||||
|
||||
function removeNode(id: string): void {
|
||||
const node = nodes.get(id)
|
||||
if (!node) return
|
||||
layers.tokens.removeChild(node.container)
|
||||
node.container.visible = false
|
||||
nodes.delete(id)
|
||||
pool.push(node)
|
||||
}
|
||||
|
||||
function enterNode(view: TokenView): void {
|
||||
const node = pool.pop() ?? createNode(view.radius)
|
||||
node.container.visible = true
|
||||
applyView(node, view, stats)
|
||||
layers.tokens.addChild(node.container)
|
||||
nodes.set(view.id, node)
|
||||
}
|
||||
|
||||
// The stage is created only once the session is live, but `app.init` is async and the resync
|
||||
// can beat it: start from whatever the reducer already holds, then follow the event stream.
|
||||
board.seed(tokensOf(store.state))
|
||||
let patch = board.drain()
|
||||
for (const view of patch.entered) enterNode(view)
|
||||
publish(canvas, board, patch, stats.labels)
|
||||
|
||||
const unsubscribe = store.subscribe((_state, event) => {
|
||||
board.apply(event)
|
||||
patch = board.drain()
|
||||
if (patch.reset) {
|
||||
for (const id of [...nodes.keys()]) removeNode(id)
|
||||
}
|
||||
for (const id of patch.left) removeNode(id)
|
||||
for (const view of patch.updated) {
|
||||
const node = nodes.get(view.id)
|
||||
if (node) applyView(node, view, stats)
|
||||
else enterNode(view)
|
||||
}
|
||||
for (const view of patch.entered) enterNode(view)
|
||||
if (patch.reset || patch.entered.length > 0 || patch.updated.length > 0 || patch.left.length > 0) {
|
||||
publish(canvas, board, patch, stats.labels)
|
||||
}
|
||||
})
|
||||
|
||||
return () => {
|
||||
unsubscribe()
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user