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 | 29x 29x 29x 29x 29x 29x 23x 23x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 2x 5x 5x 5x 5x 5x 5x 5x 5x 12x 12x 12x 7x 7x 7x 7x 7x 7x 7x 7x 7x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 5x 5x 5x 13x 13x 13x 5x 5x 5x 5x 13x 13x 13x 13x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 13x 13x 13x 13x 2x 2x | /**
* The LR2 family's decide screen: an LR2 decide skin (`Decide/decide.lr2skin`) drawn on its canvas over the chart's
* `#STAGEFILE`, letterboxed into the window. It draws the decide session's frames through the LR2 decide state
* (`resolveLR2DecideState`) and LR2's scene timers (`resolveLR2DecideTimers`), which it reads off each frame, and
* turns a click into a `continue` intent; the session times the screen from the skin's timing
* ({@link resolveLR2DecideTiming}).
*/
import {
LR2_SPECIAL_GRAPHIC,
type LR2DestinationRect,
type LR2ImageElement,
type LR2Skin,
type LR2SpecialGraphic,
} from '@be-music/skin-lr2';
import { LR2_TIMER } from '@be-music/skin-lr2/ids';
import { resolveLR2DecideState, resolveLR2DecideTimers, type LR2SceneState } from '@be-music/skin-lr2/state';
import type { LibraryChart } from '@be-music/player-scene-model';
import { Color, Container, Graphics, Sprite, 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 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 { evaluateElementDestination, makeLR2StaticImageSprite } from '../../skin/lr2/render.ts';
import { isDestinationVisible, makeLR2TextSprite, resolveScaledViewport } from '../../skin/lr2/scene-render.ts';
import {
LR2ChartGraphicTextureStore,
LR2SkinTextureStore,
collectDecideSkinTexturePaths,
resolveSolidSpecialGraphicTexture,
} from '../../skin/lr2/scene-textures.ts';
import type { DecideFrame, DecideIntent, DecideView, IntentSink, SceneViewTiming } from '../types.ts';
const log = logger('decide');
/** 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 decide skin without `#STARTINPUT`: the 1500 ms of LR2's stock decide skin. */
export const LR2_DECIDE_DEFAULT_INPUT_DELAY_MS = 1500;
/**
* How long the decide screen stays after input opens before it moves on to the play by itself: long enough to read the
* title and artist once they have faded in, short enough that a hands-off play starts promptly.
*/
export const LR2_DECIDE_AUTO_ADVANCE_AFTER_INPUT_MS = 1200;
/**
* The decide skin's timing: input opens after its `#STARTINPUT` (1500 ms without one), the screen moves on by itself
* at `#SCENETIME` when authored (zero skips the screen), otherwise {@link LR2_DECIDE_AUTO_ADVANCE_AFTER_INPUT_MS}
* later. It fades out over `#FADEOUT` as it leaves (at once without one).
*/
export function resolveLR2DecideTiming(skin: Pick<LR2Skin, 'timing'>): SceneViewTiming {
const inputDelayMs = Math.max(0, skin.timing.startInput ?? LR2_DECIDE_DEFAULT_INPUT_DELAY_MS);
return {
inputDelayMs,
autoAdvanceMs: Number.isFinite(skin.timing.sceneTime)
? Math.max(0, skin.timing.sceneTime!)
: inputDelayMs + LR2_DECIDE_AUTO_ADVANCE_AFTER_INPUT_MS,
fadeOutMs: Math.max(0, skin.timing.fadeOut ?? 0),
};
}
export interface LR2DecideViewOptions {
/** Where the chart's files are, for its `#STAGEFILE`, `*BANNER` and `*BACKBMP`; none load without one. */
readonly source?: BrowserAssetSource;
}
/** The LR2 decide view. See the module comment. */
export class LR2DecideView implements DecideView {
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();
/**
* The chart's `#STAGEFILE`, drawn over the whole canvas between the background and the skin, so the skin's chrome
* (title plates, difficulty stickers, slide-in lines) paints over it, as on LR2's decide screen. Empty for a chart
* without one.
*/
private readonly stageFileLayer = new Container();
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<DecideIntent> | undefined;
private built = false;
private disposed = false;
public constructor(kit: LR2Kit, options: LR2DecideViewOptions = {}) {
this.kit = kit;
this.source = options.source;
this.timing = resolveLR2DecideTiming(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 decide 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<DecideIntent>): 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: DecideFrame): void {
const host = this.host;
Iif (host === undefined || this.disposed) return;
this.nowMs = frame.clock.nowMs;
this.timers = resolveLR2DecideTimers(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);
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);
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(this.stageFileLayer);
disposeChildren(this.skinLayer);
this.drawStageFile(frame.chart, designWidth, designHeight);
// The decide skin's ops and texts: the chart's difficulty and key mode, its title, artist, …, and the course's.
this.drawSkin(
skin,
frame.chart,
resolveLR2DecideState({ chart: frame.chart, ...(frame.course !== undefined ? { course: frame.course } : {}) }),
);
}
/** Builds the scene graph under {@link root}, once. */
private build(): void {
this.built = true;
this.root.label = 'decide/scene';
this.designRoot.label = 'decide/root';
this.viewportBackground.label = 'decide/viewport-bg';
this.background.label = 'decide/background';
this.stageFileLayer.label = 'decide/stagefile';
this.skinLayer.label = 'decide/skin';
this.designClipMask.label = 'decide/design-clip';
this.root.addChild(this.viewportBackground, this.designRoot);
this.designRoot.addChild(this.background, this.stageFileLayer, this.skinLayer, this.designClipMask);
this.designRoot.mask = this.designClipMask;
}
private async loadSkinTextures(): Promise<void> {
const skin = this.kit.skin;
await this.skinTextures.preload(skin, collectDecideSkinTexturePaths(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 chart's `#STAGEFILE` (or a bmson's `info.back_image`) over the whole canvas, under whatever the skin
* draws, so a theme gets the artwork whether or not its skin references `*STAGEFILE`. The texture loads on the first
* frame and shows from a later one; without a stage file the background shows through.
*/
private drawStageFile(chart: LibraryChart, designWidth: number, designHeight: number): void {
Eif (!chart.chart.metadata.stageFile) return;
const texture = this.chartGraphicTexture(chart, LR2_SPECIAL_GRAPHIC.STAGEFILE);
if (texture === undefined) return;
const sprite = new Sprite(texture);
sprite.label = 'decide/stagefile-backdrop';
sprite.position.set(0, 0);
sprite.width = designWidth;
sprite.height = designHeight;
this.stageFileLayer.addChild(sprite);
}
private drawSkin(skin: LR2Skin, chart: LibraryChart, state: LR2SceneState): void {
const { ops } = state;
for (const image of skin.images) {
const dst = this.evaluateElementDst(image);
if (!isDestinationVisible(dst, ops, this.timerActive)) continue;
const sprite = this.makeStaticImageSprite(image, dst, chart);
if (sprite) this.skinLayer.addChild(sprite);
}
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;
this.skinLayer.addChild(
makeLR2TextSprite(value, text, dst, {
maxFontSize: 22,
bitmapFonts: this.bitmapFonts,
systemFontSizes: skin.systemFontSizes,
}),
);
}
}
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, chart: LibraryChart) {
return makeLR2StaticImageSprite(image, dst, {
textures: this.skinTextures.asReadonlyMap(),
elapsedSinceTimer: this.elapsedSinceTimer,
resolveSpecialGraphicTexture: (path) => this.chartGraphicTexture(chart, path),
});
}
/**
* The texture of one of LR2's chart-bound images (`*BANNER`, `*STAGEFILE`, `*BACKBMP`, or the solid `*BLACK` /
* `*WHITE`) for the chart; the first miss starts loading it, and a later frame draws it.
*/
private chartGraphicTexture(chart: LibraryChart, path: LR2SpecialGraphic): Texture | undefined {
return (
resolveSolidSpecialGraphicTexture(path) ??
this.chartGraphicTextures.resolveFromSource(this.source, chart, path, () => undefined)
);
}
private readonly handlePointerDown = (): void => {
this.emit?.({ type: 'continue' });
};
}
|