Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | 28x 28x 28x 28x 28x 28x 19x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 2x 6x 6x 6x 6x 6x 6x 6x 6x 14x 14x 14x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 6x 6x 6x 8x 8x 8x 8x 6x 6x 6x 6x 8x 8x 8x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x | import { clamp } from '@be-music/shared-utils/core';
/**
* The LR2 family's result screen: an LR2 result skin (`Result/*.lr2skin`, or the course-result skin for a course run)
* drawn on its canvas, letterboxed into the window. It draws the result session's frames through the LR2 result state
* (`resolveLR2ResultState`) and LR2's result timers, which it reads off each frame (`resolveLR2ResultTimers`), and
* turns a click into a `continue` intent (a press on, as Enter); the session plays the jingle and steps the screen.
*/
import type {
LR2DestinationRect,
LR2GaugeChartElement,
LR2ImageElement,
LR2ScoreChartElement,
LR2Skin,
LR2SpecialGraphic,
} from '@be-music/skin-lr2';
import { LR2_TIMER } from '@be-music/skin-lr2/ids';
import {
resolveLR2GraphReveal,
resolveLR2ResultState,
resolveLR2ResultTimers,
type LR2ResultState,
} from '@be-music/skin-lr2/state';
import { scrollCourseStages, type PlayResult } from '@be-music/player-scene-model';
import { Color, Container, Graphics, type Texture } from 'pixi.js';
import type { BrowserAssetSource } from '../../collection/types.ts';
import type { LR2Kit } from '../../host/family-kit.ts';
import { logger } from '../../logger.ts';
import {
resolveDesignTextResolution,
resolveScaledViewport,
setDesignPixelRatio,
setDesignTextResolution,
} from '../../stage/viewport.ts';
import type { PixiSceneHost } from '../../host/scene-host.ts';
import { disposeChildren } from '../../stage/pixi-utils.ts';
import type { LR2LoadedFont } from '../../skin/lr2/bitmap-text.ts';
import { loadSkinBitmapFonts } from '../../skin/lr2/font-loader.ts';
import {
containerSpriteSink,
evaluateElementDestination,
makeLR2BargraphSprite,
makeLR2SliderSprite,
makeLR2StaticImageSprite,
renderNumberElement,
} from '../../skin/lr2/render.ts';
import { isDestinationVisible, makeLR2TextSprite } from '../../skin/lr2/scene-render.ts';
import {
LR2ChartGraphicTextureStore,
LR2SkinTextureStore,
collectResultSkinTexturePaths,
resolveSolidSpecialGraphicTexture,
} from '../../skin/lr2/scene-textures.ts';
import type { IntentSink, ResultFrame, ResultIntent, ResultView, SceneViewTiming } from '../types.ts';
/** The course stages an LR2 skin lists at once: texts 150-154 and 160-164 name stages 1-5. */
const LR2_COURSE_STAGE_ROWS = 5;
const log = logger('result');
/** The window size assumed while the host's screen reads 0: LR2's 640x480. */
const FALLBACK_SCREEN_WIDTH = 640;
const FALLBACK_SCREEN_HEIGHT = 480;
const BG = new Color('#05070b');
/** The input delay of a result skin without `#STARTINPUT`: the 1500 ms of LR2's stock result skin. */
export const LR2_RESULT_DEFAULT_INPUT_DELAY_MS = 1500;
/** The result skin's timing: input opens after its `#STARTINPUT` (1500 ms without one); no auto advance or fade-out. */
export function resolveLR2ResultTiming(skin: Pick<LR2Skin, 'timing'>): SceneViewTiming {
return {
inputDelayMs: Math.max(0, skin.timing.startInput ?? LR2_RESULT_DEFAULT_INPUT_DELAY_MS),
fadeOutMs: 0,
};
}
export interface LR2ResultViewOptions {
/** Where the result's chart files are, for its `*STAGEFILE`, `*BANNER` and `*BACKBMP`; none load without one. */
readonly source?: BrowserAssetSource;
}
/** A point of a result graph, both axes 0..1. */
interface GraphPoint {
readonly x: number;
readonly y: number;
}
/** The LR2 result view. See the module comment. */
export class LR2ResultView implements ResultView {
public readonly family = 'lr2' as const;
public readonly root: Container = new Container();
public readonly timing: SceneViewTiming;
private readonly designRoot = new Container();
private readonly viewportBackground = new Graphics();
private readonly background = new Graphics();
private readonly skinLayer = new Container();
/** Keeps the skin's artwork out of the letterbox bars. */
private readonly designClipMask = new Graphics();
/** The sizes the static rects were last drawn at; Pixi rebuilds a `Graphics` on every redraw. */
private cachedScreenWidth = -1;
private cachedScreenHeight = -1;
private cachedDesignWidth = -1;
private cachedDesignHeight = -1;
private readonly kit: LR2Kit;
private readonly source: BrowserAssetSource | undefined;
private readonly skinTextures = new LR2SkinTextureStore();
private readonly chartGraphicTextures = new LR2ChartGraphicTextureStore();
/** The skin's `#LR2FONT` bitmap fonts, once loaded. */
private bitmapFonts: ReadonlyMap<number, LR2LoadedFont> = new Map();
/** The frame being drawn: its clock and its running timers. */
private nowMs = 0;
private timers: ReadonlyMap<number, number> = new Map();
private host: PixiSceneHost | undefined;
private emit: IntentSink<ResultIntent> | undefined;
private built = false;
private disposed = false;
public constructor(kit: LR2Kit, options: LR2ResultViewOptions = {}) {
this.kit = kit;
this.source = options.source;
this.timing = resolveLR2ResultTiming(kit.skin);
}
/** An LR2 kit carries nothing but its skin, which the view keeps: another skin needs another view. */
public updateKit(kit: LR2Kit): void {
if (kit.skin !== this.kit.skin) log.warn('a result view keeps its skin; ignoring a kit of another skin');
}
/**
* Builds the scene graph and starts loading the skin's images and bitmap fonts on the first attach, then listens for
* clicks on the canvas.
*/
public attach(host: PixiSceneHost, emit: IntentSink<ResultIntent>): void {
Iif (this.disposed) return;
Eif (!this.built) {
this.build();
void this.loadSkinTextures();
void this.loadBitmapFonts();
}
this.host = host;
this.emit = emit;
host.app.canvas.addEventListener('pointerdown', this.handlePointerDown);
}
public detach(): void {
this.host?.app.canvas.removeEventListener('pointerdown', this.handlePointerDown);
this.host = undefined;
this.emit = undefined;
}
public dispose(): void {
Iif (this.disposed) return;
this.detach();
this.disposed = true;
this.root.parent?.removeChild(this.root);
try {
this.skinTextures.dispose();
this.chartGraphicTextures.dispose();
} catch (error) {
log.warn('texture cleanup threw', error);
}
try {
this.root.destroy({ children: true, context: true });
} catch (error) {
log.warn('root.destroy threw', error);
}
}
public draw(frame: ResultFrame): void {
const host = this.host;
Iif (host === undefined || this.disposed) return;
this.nowMs = frame.clock.nowMs;
this.timers = resolveLR2ResultTimers(frame);
const skin = this.kit.skin;
const screenWidth = host.app.screen.width || FALLBACK_SCREEN_WIDTH;
const screenHeight = host.app.screen.height || FALLBACK_SCREEN_HEIGHT;
const designWidth = skin.width;
const designHeight = skin.height;
const viewport = resolveScaledViewport(screenWidth, screenHeight, designWidth, designHeight);
setDesignTextResolution(
resolveDesignTextResolution(
Math.max(viewport.scaleX ?? viewport.scale, viewport.scaleY ?? viewport.scale),
host.app.renderer.resolution,
),
);
setDesignPixelRatio(
Math.max(viewport.scaleX ?? viewport.scale, viewport.scaleY ?? viewport.scale) * host.app.renderer.resolution,
);
if (this.cachedScreenWidth !== screenWidth || this.cachedScreenHeight !== screenHeight) {
this.viewportBackground.clear().rect(0, 0, screenWidth, screenHeight).fill(BG);
this.cachedScreenWidth = screenWidth;
this.cachedScreenHeight = screenHeight;
}
this.designRoot.position.set(viewport.x, viewport.y);
this.designRoot.scale.set(viewport.scaleX ?? viewport.scale, viewport.scaleY ?? viewport.scale);
host.setStageOrientation('landscape');
if (this.cachedDesignWidth !== designWidth || this.cachedDesignHeight !== designHeight) {
this.designClipMask.clear().rect(0, 0, designWidth, designHeight).fill(0xffffff);
this.background.clear().rect(0, 0, designWidth, designHeight).fill(BG);
this.cachedDesignWidth = designWidth;
this.cachedDesignHeight = designHeight;
}
// `disposeChildren`, not a bare `removeChildren`: each frame builds fresh sprites, texts and graph polylines, and
// detached ones would keep their renderer-side state alive (see `pixi-utils.ts`).
disposeChildren(this.skinLayer);
const { result } = frame;
this.drawSkin(
skin,
result,
resolveLR2ResultState({
result,
// A course run longer than LR2's five stage rows (texts 150-154) scrolls its stage list to show every stage.
...(result.course !== undefined
? {
course: scrollCourseStages(
result.course,
frame.clock.nowMs - frame.clock.enteredAtMs,
LR2_COURSE_STAGE_ROWS,
),
}
: {}),
// The `#FLIPRESULT` of the skin the result is shown on (op 351).
flipResult: skin.scratchFlip?.flipResult === true,
// The skin's CUSTOMOPTION defaults: LITONE4-style result skins gate most of their chrome on `#IF,<default>`.
customOps: (skin.customOptions ?? []).map((option) => option.defaultOp),
}),
);
}
/** Builds the scene graph under {@link root}, once. */
private build(): void {
this.built = true;
this.root.label = 'result/scene';
this.designRoot.label = 'result/root';
this.viewportBackground.label = 'result/viewport-bg';
this.background.label = 'result/background';
this.skinLayer.label = 'result/skin';
this.designClipMask.label = 'result/design-clip';
this.root.addChild(this.viewportBackground, this.designRoot);
this.designRoot.addChild(this.background, this.skinLayer, this.designClipMask);
this.designRoot.mask = this.designClipMask;
}
private async loadSkinTextures(): Promise<void> {
const skin = this.kit.skin;
await this.skinTextures.preload(skin, collectResultSkinTexturePaths(skin), () => !this.disposed);
}
private async loadBitmapFonts(): Promise<void> {
const skin = this.kit.skin;
Eif (skin.lr2FontPaths.length === 0) return;
const loaded = await loadSkinBitmapFonts(skin.lr2FontPaths, skin.files);
if (!this.disposed) this.bitmapFonts = loaded;
}
/**
* Draws the skin's images, numbers, texts, bar graphs, sliders and graphs in their declaration order, as LR2 paints
* its CSV stream (a later line paints on top): the stock result skin declares its high-score panel after the score
* numbers and the graphs, and the panel has to cover them once timer 152 shows it.
*/
private drawSkin(skin: LR2Skin, result: PlayResult, state: LR2ResultState): void {
const { ops } = state;
const work: { readonly order: number; readonly paint: () => void }[] = [];
const textures = this.skinTextures.asReadonlyMap();
for (const image of skin.images) {
const dst = this.evaluateElementDst(image);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
work.push({
order: image.declarationOrder,
paint: () => {
const sprite = this.makeStaticImageSprite(image, dst, result);
if (sprite) this.skinLayer.addChild(sprite);
},
});
}
for (const number of skin.numbers) {
const dst = this.evaluateElementDst(number);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
const value = state.number(number.source.num);
if (value === undefined) continue;
work.push({
order: number.declarationOrder,
paint: () => renderNumberElement(containerSpriteSink(this.skinLayer), number, value, textures, dst),
});
}
for (const text of skin.texts) {
const dst = this.evaluateElementDst(text);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
const value = state.text(text.st);
if (value === undefined || value.length === 0) continue;
work.push({
order: text.declarationOrder,
paint: () => {
this.skinLayer.addChild(
makeLR2TextSprite(value, text, dst, {
bitmapFonts: this.bitmapFonts,
systemFontSizes: skin.systemFontSizes,
}),
);
},
});
}
// The EX-score and rate bars next to the numbers: the value is the filled share, 0..1.
for (const bargraph of skin.bargraphs) {
const dst = this.evaluateElementDst(bargraph);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
const value = state.bargraph(bargraph.type);
if (value === undefined) continue;
work.push({
order: bargraph.declarationOrder,
paint: () => {
const sprite = makeLR2BargraphSprite(bargraph, dst, value, textures);
if (sprite) this.skinLayer.addChild(sprite);
},
});
}
for (const slider of skin.sliders) {
const dst = this.evaluateElementDst(slider);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
const value = state.slider(slider.type);
if (value === undefined) continue;
work.push({
order: slider.declarationOrder,
paint: () => {
const sprite = makeLR2SliderSprite(slider, dst, value, textures);
if (sprite) this.skinLayer.addChild(sprite);
},
});
}
for (const chart of skin.gaugeCharts) {
const dst = this.evaluateElementDst(chart);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
work.push({
order: chart.declarationOrder,
paint: () => {
const graphic = this.makeGaugeChartGraphic(chart, dst, result);
if (graphic) this.skinLayer.addChild(graphic);
},
});
}
for (const chart of skin.scoreCharts) {
const dst = this.evaluateElementDst(chart);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
work.push({
order: chart.declarationOrder,
paint: () => {
const graphic = this.makeScoreChartGraphic(chart, dst, result);
if (graphic) this.skinLayer.addChild(graphic);
},
});
}
// A stable sort, so elements on the same line keep the order they were collected in.
work.sort((a, b) => a.order - b.order);
for (const item of work) item.paint();
}
/**
* One `#SRC_GAUGECHART_*` line: the gauge over the play, left to right, from the bottom-left corner the destination
* names (`y` grows upward). Only the 1P line draws: a play has one gauge.
*/
private makeGaugeChartGraphic(
chart: LR2GaugeChartElement,
dst: LR2DestinationRect,
result: PlayResult,
): Graphics | undefined {
const { fieldWidth, fieldHeight, start, end } = chart.source;
if (fieldWidth <= 0 || fieldHeight <= 0) return undefined;
const series = chart.side === '2P' ? [] : result.gaugeHistory;
if (series.length === 0) return undefined;
const revealRatio = this.graphReveal(chart.destination.timer, start, end);
if (revealRatio <= 0) return undefined;
return drawGraphLine({
points: series.map((sample) => ({
x: clamp01(sample.progress),
// The gauge is already 0..100.
y: clamp01(sample.value / 100),
})),
revealRatio,
dst,
fieldWidth,
fieldHeight,
label: `gaugechart[${chart.side}/idx=${chart.source.index}]`,
});
}
/**
* One `#SRC_SCORECHART` line: the EX score over its maximum, laid out as a gauge line. Only line 0, this play,
* draws: the best record (1) and the rival (2) do not reach the result.
*/
private makeScoreChartGraphic(
chart: LR2ScoreChartElement,
dst: LR2DestinationRect,
result: PlayResult,
): Graphics | undefined {
if (chart.source.index !== 0) return undefined;
const { fieldWidth, fieldHeight, start, end } = chart.source;
if (fieldWidth <= 0 || fieldHeight <= 0) return undefined;
const exMax = result.score.total * 2;
if (exMax <= 0) return undefined;
const series = result.scoreHistory;
if (series.length === 0) return undefined;
const revealRatio = this.graphReveal(chart.destination.timer, start, end);
if (revealRatio <= 0) return undefined;
return drawGraphLine({
points: series.map((sample) => ({ x: clamp01(sample.progress), y: clamp01(sample.exScore / exMax) })),
revealRatio,
dst,
fieldWidth,
fieldHeight,
label: `scorechart[idx=${chart.source.index}]`,
});
}
/** How much of a graph on `timer` shows this frame (see {@link resolveLR2GraphReveal}). */
private graphReveal(timer: number, start: number, end: number): number {
const elapsedMs =
this.timers.has(timer) || timer === LR2_TIMER.SCENE_START ? this.elapsedSinceTimer(timer) : undefined;
return resolveLR2GraphReveal(elapsedMs, start, end);
}
private evaluateElementDst(element: {
destination: LR2DestinationRect;
keyframes: LR2DestinationRect[];
}): LR2DestinationRect {
return evaluateElementDestination(element, this.elapsedSinceTimer);
}
/** How long `timer` has run on the frame's clock; 0 before it starts, which holds an element at its first keyframe. */
private readonly elapsedSinceTimer = (timer: number): number => {
const startedAt = this.timers.get(timer);
return startedAt === undefined ? 0 : Math.max(0, this.nowMs - startedAt);
};
/** Whether `timer` runs on the frame: timer 0 always does; an element on a timer that has not started stays hidden. */
private readonly timerActive = (timer: number): boolean => timer === LR2_TIMER.SCENE_START || this.timers.has(timer);
private makeStaticImageSprite(image: LR2ImageElement, dst: LR2DestinationRect, result: PlayResult) {
return makeLR2StaticImageSprite(image, dst, {
textures: this.skinTextures.asReadonlyMap(),
elapsedSinceTimer: this.elapsedSinceTimer,
resolveSpecialGraphicTexture: (path) => this.chartGraphicTexture(result, path),
});
}
/**
* The texture of one of LR2's chart-bound images (`*BANNER`, `*STAGEFILE`, `*BACKBMP`, or the solid `*BLACK` /
* `*WHITE`) for the chart just played; the first miss starts loading it, and a later frame draws it.
*/
private chartGraphicTexture(result: PlayResult, path: LR2SpecialGraphic): Texture | undefined {
return (
resolveSolidSpecialGraphicTexture(path) ??
this.chartGraphicTextures.resolveFromSource(this.source, result.chart, path, () => undefined)
);
}
private readonly handlePointerDown = (): void => {
this.emit?.({ type: 'continue' });
};
}
function clamp01(value: number): number {
return clamp(value, 0, 1);
}
/**
* Draws a result graph's line through `points` (both axes 0..1) on the field whose bottom-left corner is the
* destination's `(x, y)`: x runs to `x + fieldWidth`, y up to `y - fieldHeight`. The line stops at `revealRatio` along
* x, partway through the segment it falls in. Its width is the destination's `w` (at least 1), its colour the
* destination's tint and its alpha the destination's.
*/
function drawGraphLine(input: {
readonly points: readonly GraphPoint[];
readonly revealRatio: number;
readonly dst: LR2DestinationRect;
readonly fieldWidth: number;
readonly fieldHeight: number;
readonly label: string;
}): Graphics | undefined {
const { points, dst, fieldWidth, fieldHeight } = input;
if (points.length === 0) return undefined;
const cutoff = clamp01(input.revealRatio);
if (cutoff <= 0) return undefined;
const toScreen = (point: GraphPoint): { sx: number; sy: number } => ({
sx: dst.x + point.x * fieldWidth,
sy: dst.y - point.y * fieldHeight,
});
const graphic = new Graphics();
graphic.label = input.label;
graphic.alpha = dst.alpha;
const first = points[0]!;
const start = toScreen(first);
graphic.moveTo(start.sx, start.sy);
let drewAny = false;
for (let index = 1; index < points.length; index += 1) {
const previous = points[index - 1]!;
const current = points[index]!;
if (current.x <= cutoff) {
const screen = toScreen(current);
graphic.lineTo(screen.sx, screen.sy);
drewAny = true;
continue;
}
// The cutoff falls inside this segment: end the line where it crosses it.
const span = current.x - previous.x;
if (span <= 0) break;
const t = clamp01((cutoff - previous.x) / span);
const screen = toScreen({
x: previous.x + (current.x - previous.x) * t,
y: previous.y + (current.y - previous.y) * t,
});
graphic.lineTo(screen.sx, screen.sy);
drewAny = true;
break;
}
if (!drewAny && points.length === 1) {
// A single sample (a chart that ended before any judgement): a dot half a pixel wide, since a `lineTo` to the same
// point draws nothing.
graphic.lineTo(start.sx + 0.5, start.sy);
}
const color = ((dst.r << 16) | (dst.g << 8) | dst.b) >>> 0;
graphic.stroke({ color, width: Math.max(1, Math.abs(dst.w || 2)), alpha: 1, alignment: 0.5 });
return graphic;
}
|