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 | 28x 28x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 4x 4x 4x | import { SILENT_AUDIO } from '@be-music/player-skin-sdk';
/**
* The built-in family's select view: a Be-Music Skin's `select` screen (`BeMusicSelectBinding`), letterboxed into the
* window, with the README dialog over it. It draws the select session's frames and turns the player's pointer input
* (a tap or a click on a bar, a swipe and its glide, the wheel, the skin's PLAY / AUTO / search / play option / readme
* hit areas) into intents.
*/
import { cyclePlayOptionValue, normalizeSearchQuery, PLAY_OPTION_CHOICES } from '@be-music/player-scene-model';
import {
resolveBeMusicSelectLayout,
resolveStageOrientation,
type BeMusicSelectLayout,
} from '@be-music/player-skin-sdk';
import { Color, Container, Graphics } from 'pixi.js';
import { resolveChartSource } from '../../collection/collection.ts';
import { hasReadtextForChart } from '../../collection/readtext.ts';
import type { BuiltInKit } from '../../host/family-kit.ts';
import {
designRectToCanvas,
resolveDesignTextResolution,
resolveScaledViewport,
setDesignPixelRatio,
setDesignTextResolution,
type ScaledViewport,
} from '../../stage/viewport.ts';
import type { PixiSceneHost } from '../../host/scene-host.ts';
import { PerfTracker } from '../shared/perf.ts';
import { SelectReadmeDialog } from '../shared/readme-dialog.ts';
import { SelectPointer, type SelectDesignSize, type SelectPointerContext } from '../shared/select-pointer.ts';
import { BeMusicSelectBinding, resolveBeMusicSkinStage } from '../../skin/be-music/binding.ts';
import { resolveSelectListWindow } from '../../skin/be-music/registry.ts';
import { ChartImageCache } from '../../skin/be-music/chart-images.ts';
import type { BeMusicSelectActions } from '../../skin/be-music/types.ts';
import { logger } from '../../logger.ts';
import type { IntentSink, SelectFrame, SelectIntent, SelectView } from '../types.ts';
const log = logger('select');
const BG = new Color('#050912');
/** The built-in select view. See the module comment. */
export class BuiltInSelectView implements SelectView {
public readonly family = 'built-in' as const;
/** The viewport background and the letterboxed design canvas. */
public readonly root: Container = new Container();
private readonly designRoot = new Container();
/** Clips the design canvas, so an element sliding in from off-canvas is cut at its edge, not drawn in the bars. */
private readonly designClipMask = new Graphics();
private readonly viewportBackground = new Graphics();
private readonly background = new Graphics();
/** Where the Be-Music Skin's back and front layers mount; the skin owns what is inside. */
private readonly beMusicBackHolder: Container = new Container();
private readonly beMusicFrontHolder: Container = new Container();
private readonly readme: SelectReadmeDialog = new SelectReadmeDialog();
private readonly pointer: SelectPointer;
/** Per-frame timing, logged every second with `?perf` or `globalThis.__BE_MUSIC_PERF__ = true`. */
private readonly perf: PerfTracker = new PerfTracker('select');
private kit: BuiltInKit;
private host: PixiSceneHost | undefined;
private emit: IntentSink<SelectIntent> | undefined;
private attached = false;
private built = false;
private disposed = false;
private renderer: BeMusicSelectBinding | undefined;
private chartImages: ChartImageCache | undefined;
/** The last frame drawn: what the pointer reads (the list, the cursor) between frames. */
private frame: SelectFrame | undefined;
/** The frame revision the skin was last built from; it rebuilds when the session's state changes. */
private builtRevision = -1;
/** The skin reported an unfinished transition (a launch outro): build it again next frame. */
private needsFrame = false;
/** When the cursor last moved, on the host clock: the skin's focus transitions count from it. */
private cursorChangedAt = 0;
private cachedScreenWidth = -1;
private cachedScreenHeight = -1;
private cachedDesignWidth = -1;
private cachedDesignHeight = -1;
/** The viewport of the last build, to place the search box on the page. */
private lastViewport: ScaledViewport | undefined;
/** The pointer in design pixels, `-1` while it is off the canvas. */
private mouseX = -1;
private mouseY = -1;
public constructor(kit: BuiltInKit) {
this.kit = kit;
this.pointer = new SelectPointer(BuiltInSelectView.createPointerContext(this));
}
/** How long the skin's launch outro plays: none with the skin's effects off. */
public get launchOutroMs(): number {
return (this.kit.effects ?? 'full') === 'off' ? 0 : (this.kit.skin.select.outroMs ?? 0);
}
/** Takes the player's new settings for the skin (the skin keeps its family and identity). */
public updateKit(kit: BuiltInKit): void {
this.kit = { ...kit, skin: this.kit.skin };
this.renderer?.setSettings(kit.settings);
this.builtRevision = -1;
}
public attach(host: PixiSceneHost, emit: IntentSink<SelectIntent>): void {
if (this.disposed) return;
this.host = host;
this.emit = emit;
if (!this.built) this.build();
const canvas = host.app.canvas;
canvas.addEventListener('pointerdown', this.pointer.handlePointerDown);
canvas.addEventListener('pointermove', this.pointer.handlePointerMove);
canvas.addEventListener('pointerleave', this.pointer.handlePointerLeave);
canvas.addEventListener('pointerup', this.pointer.handlePointerUp);
canvas.addEventListener('pointercancel', this.pointer.handlePointerCancel);
// `passive: false`, so a wheel over the list does not also scroll the page under it.
canvas.addEventListener('wheel', this.pointer.handleWheel, { passive: false });
// A wheel throttle left over from before the screen last left would eat the first notch.
this.pointer.lastWheelMoveAt = Number.NEGATIVE_INFINITY;
this.readme.attach(
() => host.domRoot,
() => this.emit?.({ type: 'readtext' }),
(index) => this.emit?.({ type: 'readtext-tab', index }),
);
this.builtRevision = -1;
this.attached = true;
}
public detach(): void {
Eif (!this.attached) return;
this.attached = false;
this.pointer.fling = undefined;
const canvas = this.host?.app.canvas;
Iif (canvas !== undefined) {
canvas.removeEventListener('pointerdown', this.pointer.handlePointerDown);
canvas.removeEventListener('pointermove', this.pointer.handlePointerMove);
canvas.removeEventListener('pointerleave', this.pointer.handlePointerLeave);
canvas.removeEventListener('pointerup', this.pointer.handlePointerUp);
canvas.removeEventListener('pointercancel', this.pointer.handlePointerCancel);
canvas.removeEventListener('wheel', this.pointer.handleWheel);
}
this.readme.detach();
this.emit = undefined;
}
public dispose(): void {
Iif (this.disposed) return;
this.detach();
this.disposed = true;
this.renderer?.dispose();
this.renderer = undefined;
this.chartImages?.dispose();
this.chartImages = undefined;
this.root.parent?.removeChild(this.root);
try {
this.root.destroy({ children: true, context: true });
} catch (error) {
log.warn('root.destroy threw', error);
}
this.host = undefined;
}
public draw(frame: SelectFrame): void {
if (this.disposed || this.host === undefined) return;
this.frame = frame;
const now = frame.clock.nowMs;
for (const event of frame.events) {
if (event.kind === 'browse' && event.event.kind !== 'list-changed' && event.event.kind !== 'favorite') {
this.cursorChangedAt = event.atMs;
} else if (event.kind === 'readtext-scroll') {
this.readme.scrollBy(event.delta);
}
}
this.readme.show(frame.readText);
this.perf.beginTick();
this.pointer.advanceFling(now);
this.pointer.settleListPull(now);
const screen = this.host.app.screen;
// The skin is built again only when the session's state changed, while a transition is unfinished, or when the
// screen was resized (a rotation can switch the skin to its other stage); between, it draws itself every tick.
if (
frame.revision !== this.builtRevision ||
this.needsFrame ||
screen.width !== this.cachedScreenWidth ||
screen.height !== this.cachedScreenHeight
) {
this.perf.time('render', () => this.buildFrame(frame));
}
const renderer = this.renderer;
if (renderer !== undefined) {
const { width, height } = this.beMusicDesignSize;
const onStage = this.mouseX >= 0 && this.mouseY >= 0 && this.mouseX < width && this.mouseY < height;
renderer.setPointer(onStage ? this.mouseX : undefined, onStage ? this.mouseY : undefined);
renderer.setListScroll(this.pointer.shownListPull());
const focused = frame.entries[frame.cursor];
// With effects off the skins' audio drive is silent, so the frame's analysis is left unread (and unrun).
const audio = (this.kit.effects ?? 'full') === 'off' ? SILENT_AUDIO : frame.audio;
renderer.tick(now, focused?.kind === 'chart' ? focused.chart : undefined, frame.startedAtMs, audio);
}
const report = this.perf.endFrame(() => ({ entries: frame.entries.length, charts: frame.library.charts.length }));
if (report) log.debug('perf', report);
}
/**
* Where the skin's search box is on the canvas, in CSS pixels from its top-left corner, or `undefined` when the skin
* has none (or nothing is drawn yet).
*/
public searchFieldRect(): { x: number; y: number; width: number; height: number; fontSize?: number } | undefined {
const rect = this.renderer?.searchFieldRect;
const viewport = this.lastViewport;
Eif (rect === undefined || viewport === undefined) return undefined;
return {
...designRectToCanvas(rect, viewport),
...(rect.fontSize !== undefined ? { fontSize: rect.fontSize * (viewport.scaleY ?? viewport.scale) } : {}),
};
}
/** The skin's design canvas and its size, for screenshots. */
public getStageInfo(): { container: Container; width: number; height: number } {
const { width, height } = this.beMusicDesignSize;
return { container: this.designRoot, width, height };
}
/** Builds the scene graph under {@link root}, on the first attach. */
private build(): void {
this.built = true;
this.root.label = 'select/scene';
this.designRoot.label = 'select/root';
this.viewportBackground.label = 'select/viewport-bg';
this.background.label = 'select/background';
this.designClipMask.label = 'select/design-clip';
this.beMusicBackHolder.label = 'select/be-music-back';
this.beMusicFrontHolder.label = 'select/be-music-front';
this.root.addChild(this.viewportBackground, this.designRoot);
this.designRoot.addChild(this.background, this.beMusicBackHolder, this.beMusicFrontHolder, this.designClipMask);
this.designRoot.mask = this.designClipMask;
}
/** Lays the design canvas into the screen and has the skin build the frame. */
private buildFrame(frame: SelectFrame): void {
const host = this.host!;
const app = host.app;
const screenWidth = app.screen.width || app.canvas.parentElement?.clientWidth || 640;
const screenHeight = app.screen.height || app.canvas.parentElement?.clientHeight || 480;
const { width: designWidth, height: designHeight } = this.beMusicDesignSize;
const viewport = resolveScaledViewport(screenWidth, screenHeight, designWidth, designHeight);
setDesignTextResolution(resolveDesignTextResolution(viewport.scale, app.renderer.resolution));
setDesignPixelRatio(viewport.scale * app.renderer.resolution);
// The rects are drawn again only when their sizes change: a `Graphics` rebuild every frame is not free.
if (this.cachedScreenWidth !== app.screen.width || this.cachedScreenHeight !== app.screen.height) {
this.viewportBackground.clear().rect(0, 0, screenWidth, screenHeight).fill(BG);
this.cachedScreenWidth = app.screen.width;
this.cachedScreenHeight = app.screen.height;
}
this.designRoot.position.set(viewport.x, viewport.y);
this.designRoot.scale.set(viewport.scale);
this.lastViewport = viewport;
host.setStageOrientation(resolveStageOrientation({ width: designWidth, height: designHeight }));
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;
}
const renderer = this.ensureRenderer(frame);
const window = resolveSelectListWindow(this.beMusicSelectLayout, designHeight, frame.cursor, frame.entries.length);
const focused = frame.entries[frame.cursor];
const folder = frame.browse.path[frame.browse.path.length - 1];
renderer.render({
designWidth,
designHeight,
nowMs: frame.clock.nowMs,
sceneStartedAt: frame.clock.enteredAtMs,
cursorChangedAt: this.cursorChangedAt,
entries: frame.entries,
selectedIndex: frame.cursor,
firstVisibleIndex: window.firstVisibleIndex,
visibleRows: window.visibleRows,
focusedChart: focused?.kind === 'chart' ? focused.chart : undefined,
focusedCourse: focused?.kind === 'course' ? focused.course : undefined,
folderLabel: folder?.label,
folderPath: frame.browse.path.map((folder) => folder.label),
searchQuery: normalizeSearchQuery(frame.browse.searchText),
searchEditing: frame.searchEditing,
totalCharts: frame.library.charts.length,
hasReadme: focused?.kind === 'chart' && hasReadtextForChart(frame.library, focused.chart),
actions: this.actions,
effects: this.kit.effects ?? 'full',
launchAt: frame.startedAtMs,
playOptions: frame.playOptions,
playOptionChoices: PLAY_OPTION_CHOICES,
});
this.builtRevision = frame.revision;
// The skin draws itself every tick; only a launch outro needs it built again each frame.
this.needsFrame = frame.startedAtMs !== undefined;
}
private ensureRenderer(frame: SelectFrame): BeMusicSelectBinding {
if (this.renderer !== undefined) {
if (this.chartImages !== undefined && this.imagesLibrary !== frame.library) {
// Images cached for the previous library's charts may not match the new one's files.
this.chartImages.clear();
this.imagesLibrary = frame.library;
}
return this.renderer;
}
this.imagesLibrary = frame.library;
const chartImages = new ChartImageCache({
resolveSource: (chartEntry) =>
this.frame === undefined ? undefined : resolveChartSource(this.frame.library, chartEntry),
onLoaded: () => this.renderer?.invalidate(),
});
this.chartImages = chartImages;
this.renderer = new BeMusicSelectBinding(this.kit.skin, {
resolveChartBestRecord: (chartEntry) => this.frame?.records.chart(chartEntry),
resolveCourseBestRecord: (course) => this.frame?.records.course(course),
resolveDanRanks: () => this.frame?.records.danRanks?.() ?? [],
chartImage: chartImages.lookup,
settings: this.kit.settings,
renderer: this.host?.app.renderer,
});
this.beMusicBackHolder.addChild(this.renderer.backLayer);
this.beMusicFrontHolder.addChild(this.renderer.frontLayer);
return this.renderer;
}
/** The library the song images were cached for. */
private imagesLibrary: SelectFrame['library'] | undefined;
/** The skin's hit areas, as intents. */
private readonly actions: BeMusicSelectActions = {
play: (): void => this.emit?.({ type: 'start', autoPlay: false }),
autoPlay: (): void => this.emit?.({ type: 'start', autoPlay: true }),
practice: (): void => this.emit?.({ type: 'practice' }),
activateSearch: (): void => this.emit?.({ type: 'search' }),
setPlayOption: (key, value): void => this.emit?.({ type: 'set-play-option', key, value }),
cyclePlayOption: (key, step = 1): void => {
// Options without a fixed set of choices (the hi-speed, the shutter) do not cycle from a skin.
if (this.frame === undefined || cyclePlayOptionValue(key, this.frame.playOptions[key], step) === undefined)
return;
this.emit?.({ type: 'play-option', key, step });
},
toggleReadme: (): void => this.emit?.({ type: 'readtext' }),
};
/** The Be-Music Skin's stage for the screen: its portrait stage while the screen is upright and it has one. */
private get beMusicDesignSize(): SelectDesignSize {
const screen = this.host?.app.screen;
const stage = resolveBeMusicSkinStage(
this.kit.skin,
screen && screen.width > 0 && screen.height > 0 ? screen : undefined,
);
return { width: stage.width, height: stage.height };
}
/** Where the skin's song list sits on the stage. */
private get beMusicSelectLayout(): BeMusicSelectLayout {
return resolveBeMusicSelectLayout(this.kit.skin.select, this.beMusicDesignSize);
}
/** What the pointer input reads and drives: the last frame, and intents for what the player asks. */
private static createPointerContext(view: BuiltInSelectView): SelectPointerContext {
const emit = (intent: SelectIntent): void => view.emit?.(intent);
return {
get visible(): boolean {
return view.attached;
},
get launchAt(): number | undefined {
return view.frame?.startedAtMs;
},
get screen() {
return view.host!.app.screen;
},
themeDesignSize: undefined,
get beMusicDesignSize(): SelectDesignSize {
return view.beMusicDesignSize;
},
get beMusicSelectLayout(): BeMusicSelectLayout {
return view.beMusicSelectLayout;
},
get selectedIndex(): number {
return view.frame?.cursor ?? 0;
},
get mouseX(): number {
return view.mouseX;
},
set mouseX(value: number) {
view.mouseX = value;
},
get mouseY(): number {
return view.mouseY;
},
set mouseY(value: number) {
view.mouseY = value;
},
resolveDesignSize: () => view.beMusicDesignSize,
currentEntries: () => view.frame?.entries ?? [],
moveCursorBy: (steps) => {
const delta = steps > 0 ? 1 : -1;
for (let step = 0; step < Math.abs(steps); step += 1)
emit({ type: 'browse', command: { type: 'move', delta } });
},
stepCursor: (direction) => emit({ type: 'browse', command: { type: 'move', delta: direction } }),
jumpCursor: (index) => emit({ type: 'browse', command: { type: 'jump', index } }),
activateFocused: () => emit({ type: 'activate' }),
handleThemePointerDown: () => {},
startSelectBGM: async () => emit({ type: 'gesture' }),
};
}
}
|