All files / player-browser/src/view/shared play-frame.ts

93.81% Statements 91/97
80.76% Branches 42/52
100% Functions 12/12
94.04% Lines 79/84

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                                                                  29x                                                                 15x 8x     8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x       8x                           8x                   64x       8x 15x                               15x 15x 15x               7x 6x                             6x 3x 3x 2x 2x 2x   3x 3x   3x 3x 3x           3x               6x 6x 6x 6x 6x             6x           6x           6x 3x 3x 3x     6x 6x 6x 6x 6x 6x 6x 6x 6x 6x       6x 6x   6x 6x 6x 6x 6x 6x 6x 6x 6x           6x 6x 6x 6x         14x 8x 8x 8x 8x    
/**
 * The frame of the built-in and LR2 play views: the layer tree under the view's root, its fit to the screen with the
 * design-rect clip and background, the fade in and out, and the per-frame pass that draws the view's chrome
 * (`PlayChrome`), the BGA and the playfield into it.
 *
 * The functions read and write the view's state through {@link PlayFrameContext}; the play view passes itself.
 */
 
import { resolveStageOrientation } from '@be-music/player-skin-sdk';
import { resolveChartPlayfieldVisual } from '@be-music/chart-model';
import type { Application, Container, Graphics, Text } from 'pixi.js';
import type { AudioFeatures } from '../../runtime/audio-analysis.ts';
import type { PlaySessionOptions } from '../../session/play/play-options.ts';
import {
  resolveDesignTextResolution,
  resolveScaledViewport,
  setDesignPixelRatio,
  setDesignTextResolution,
} from '../../stage/viewport.ts';
import { BG } from './play-constants.ts';
import type { PixiSceneHost } from '../../host/scene-host.ts';
import type { PerfTracker } from './perf.ts';
import { renderBGA, type BGARendererContext } from './bga-renderer.ts';
import { renderBombs } from './playfield/bombs.ts';
import type { PlayfieldContext } from './playfield/context.ts';
import { renderLanes } from './playfield/lanes.ts';
import { renderNotes } from './playfield/notes.ts';
import { applyChartReverse } from './playfield/chart-note-visual.ts';
import { renderShutter } from './playfield/shutter.ts';
import { renderText } from './playfield/text.ts';
import type { PlayfieldStage } from '../types.ts';
 
/** How long the play screen takes to fade up from black as it appears on a Be-Music Skin. */
const BE_MUSIC_ENTER_FADE_MS = 400;
 
/** The view's members the frame passes use: a play view's members of these names. */
export interface PlayFrameContext extends PlayfieldContext, BGARendererContext {
  readonly options: Readonly<
    Pick<PlaySessionOptions, 'beMusicEffects' | 'showInvisibleNotes' | 'bga' | 'autoPlay' | 'nonstop'>
  >;
  host: PixiSceneHost | undefined;
  readonly app: Application;
  readonly root: Container;
  readonly designRoot: Container;
  readonly designClipMask: Graphics;
  readonly viewportBackground: Graphics;
  readonly background: Graphics;
  readonly bgaLayer: Container;
  readonly skinLayer: Container;
  readonly noteLayer: Container;
  readonly bombLayer: Container;
  readonly overlayLayer: Container;
  readonly textLayer: Container;
  readonly overlay: Text;
  stampedDesignWidth: number;
  stampedDesignHeight: number;
  cachedScreenWidth: number;
  cachedScreenHeight: number;
  enteredAt: number | undefined;
  readonly paintsThroughBeMusicSkin: boolean;
  audioFrame: AudioFeatures | undefined;
  readonly perf: PerfTracker;
}
 
/** Builds the scene graph under {@link root} on the first {@link prepare} or {@link enter}; later calls no-op. */
export function build(ctx: PlayFrameContext, host: PixiSceneHost): void {
  if (ctx.host !== undefined) return;
  ctx.host = host;
  // Label every top-level node so the PixiJS Devtools "Scene Graph" panel reads as `gameplay > {bga,skin,lane,…}`
  // instead of a wall of `Container` rows. Layer ordering matches `addChild` below.
  ctx.root.label = 'gameplay/scene';
  ctx.designRoot.label = 'gameplay/root';
  ctx.viewportBackground.label = 'gameplay/viewport-bg';
  ctx.background.label = 'gameplay/background';
  ctx.bgaLayer.label = 'gameplay/bga';
  ctx.skinLayer.label = 'gameplay/skin';
  ctx.laneLayer.label = 'gameplay/lanes';
  ctx.noteLayer.label = 'gameplay/notes';
  ctx.bombLayer.label = 'gameplay/bombs';
  ctx.overlayLayer.label = 'gameplay/overlay';
  ctx.textLayer.label = 'gameplay/text';
  ctx.overlay.label = 'gameplay/pause-overlay';
  ctx.designClipMask.label = 'gameplay/design-clip';
  // The mask and design background only change shape with the stage (a rotation can switch a skin between its
  // landscape and portrait stages), so they are stamped here and restamped by `render` only when the stage changes,
  // skipping the per-frame rebuild that was contributing to the rAF handler's runtime.
  stampDesignRect(ctx, ctx.playfieldSkin.stage);
  // The gameplay scene owns its own pointerdown listener on the canvas itself (`this.focus`) and a window-level
  // keydown listener — none of the per-layer children need to participate in Pixi's interaction system. Marking
  // each render-only Container as `eventMode = 'none'` lets the interaction manager skip the entire subtree
  // during hit-testing every pointermove / pointerdown, which is otherwise an O(N) walk over hundreds of skin
  // children per event.
  //
  // CAUTION: `this.background` and `this.designClipMask` are intentionally NOT marked `'none'`. The mask is
  // referenced via `this.designRoot.mask = this.designClipMask` and Pixi v8's mask resolution path interacts with the
  // event-mode setting in subtle ways — clearing it on the mask Graphics has been observed to leave the root
  // unclipped (residual previous-frame content visible, e.g. the LR2 intro title strip stays painted across
  // subsequent frames) and has the side effect of dropping the rendered frame rate. The mask + the static
  // `background` Graphics don't carry interactive children either way, so leaving their `eventMode` at the
  // default is the safest choice.
  for (const layer of [
    ctx.bgaLayer,
    ctx.skinLayer,
    ctx.laneLayer,
    ctx.noteLayer,
    ctx.shutterLayer,
    ctx.bombLayer,
    ctx.overlayLayer,
    ctx.textLayer,
  ]) {
    layer.eventMode = 'none';
  }
  // The view's own sub-layers (e.g. the LR2 bitmap-text layer) must be parked inside `skinLayer` before the scene graph
  // is assembled so their z-order matches the skin's own layering.
  ctx.chrome.attachLayers?.();
  ctx.designRoot.addChild(
    ctx.background,
    ctx.bgaLayer,
    ctx.skinLayer,
    ctx.laneLayer,
    ctx.noteLayer,
    ctx.shutterLayer,
    ctx.bombLayer,
    ctx.overlayLayer,
    ctx.textLayer,
    ctx.overlay,
    ctx.designClipMask,
  );
  // Clip every child of `designRoot` to the design rectangle. The mask graphic itself is a child of `designRoot`, so
  // the same viewport scale / translate applies to both the mask and the masked content — Pixi clips against the
  // mask's world-space bounds, which matches the on-screen design rectangle.
  ctx.designRoot.mask = ctx.designClipMask;
  ctx.shutterLayer.label = 'gameplay/shutter';
  ctx.root.addChild(ctx.viewportBackground, ctx.designRoot);
}
 
/**
 * The picture's alpha `elapsedMs` into a play's fade-out of `fadeOutMs`: 1 → 0, and 1 when there is no fade. The
 * built-in and LR2 views fade their design root with it, and the beatoraja view a failed play's canvas.
 */
export function playEndingFadeAlpha(elapsedMs: number, fadeOutMs: number): number {
  if (!(fadeOutMs > 0)) return 1;
  return Math.max(0, Math.min(1, 1 - elapsedMs / fadeOutMs));
}
 
/**
 * Drives the global "fade everything to black" tween while {@link beginExitSequence} is in flight. The skin's
 * authored `#FADEOUT` overlay (if any) only covers whatever the skin artist drew — gameplay-side layers (notes, lane
 * chrome, BGA, shutters, bombs, overlay text) sit *above* `skinLayer` in `designRoot` and would otherwise stay
 * fully visible until the scene is torn down. Fading `designRoot` itself dims every layer uniformly;
 * `viewportBackground` is a sibling of `designRoot` (under `root`), so the page reveals the same `BG` (near-black)
 * the playfield sat against, giving a clean fade-to-black appearance regardless of which lanes are still painting.
 *
 * Called every frame from {@link render}; cheap when the exit sequence isn't running (`designRoot.alpha` is set
 * back to 1 only when it diverges from 1, so steady-state has no GPU cost).
 */
function applyExitFadeAlpha(ctx: PlayFrameContext): void {
  if (!ctx.play.exiting) {
    let alpha = 1;
    if (ctx.paintsThroughBeMusicSkin) {
      const now = performance.now();
      ctx.enteredAt ??= now;
      alpha = Math.min(1, (now - ctx.enteredAt) / BE_MUSIC_ENTER_FADE_MS);
    }
    if (ctx.designRoot.alpha !== alpha) ctx.designRoot.alpha = alpha;
    return;
  }
  const fadeOutMs = ctx.play.exitFadeOutMs;
  const fadeStart = ctx.play.exitFadeStartedAt;
  Iif (fadeStart === undefined || fadeOutMs <= 0) {
    // Fade hasn't been seeded (or the theme has no fade-out); leave designRoot at full alpha. Close-only themes keep
    // the scene visible until the host transitions us out.
    if (ctx.designRoot.alpha !== 1) ctx.designRoot.alpha = 1;
    return;
  }
  ctx.designRoot.alpha = playEndingFadeAlpha(ctx.play.playClock() - fadeStart, fadeOutMs);
}
 
/**
 * Draws one frame of the play at chart time `seconds`. `audioFrame` is the mix analysed for this frame, for an
 * audio-reactive skin; the view passes none when nothing it draws reacts to the audio, so the analysis is skipped.
 */
export function render(ctx: PlayFrameContext, seconds: number, audioFrame: AudioFeatures | undefined): void {
  const screenWidth = ctx.app.screen.width;
  const screenHeight = ctx.app.screen.height;
  const stage = ctx.playfieldSkin.stage;
  const { width: designWidth, height: designHeight } = stage;
  const viewport = resolveScaledViewport(
    screenWidth,
    screenHeight,
    designWidth,
    designHeight,
    stage.displayAspectRatio,
  );
  setDesignTextResolution(
    resolveDesignTextResolution(
      Math.max(viewport.scaleX ?? viewport.scale, viewport.scaleY ?? viewport.scale),
      ctx.app.renderer.resolution,
    ),
  );
  setDesignPixelRatio(
    Math.max(viewport.scaleX ?? viewport.scale, viewport.scaleY ?? viewport.scale) * ctx.app.renderer.resolution,
  );
  // Only rebuild the static rect graphics when their backing dimensions actually change. The previous unconditional
  // `.clear().rect().fill()` chain ran on every rAF tick and rebuilt the GraphicsContext for each — Pixi v8 has no
  // change-detection built in.
  if (ctx.cachedScreenWidth !== screenWidth || ctx.cachedScreenHeight !== screenHeight) {
    ctx.viewportBackground.clear().rect(0, 0, screenWidth, screenHeight).fill(BG);
    ctx.cachedScreenWidth = screenWidth;
    ctx.cachedScreenHeight = screenHeight;
  }
  // The player's own move and scale of the skin (LR2's 1 / 2 + the arrows), in design pixels.
  const adjust = ctx.play.skinAdjust;
  const scaleX = viewport.scaleX ?? viewport.scale;
  const scaleY = viewport.scaleY ?? viewport.scale;
  ctx.designRoot.position.set(viewport.x + (adjust?.x ?? 0) * scaleX, viewport.y + (adjust?.y ?? 0) * scaleY);
  ctx.designRoot.scale.set(scaleX * (adjust?.scale ?? 1), scaleY * (adjust?.scale ?? 1));
  stampDesignRect(ctx, stage);
  ctx.host?.setStageOrientation(resolveStageOrientation(stage));
  applyExitFadeAlpha(ctx);
  ctx.audioFrame = audioFrame;
  ctx.perf.time('renderSkin', () => ctx.chrome.draw(designWidth, designHeight));
  // LR2 negative-BPM reversal: the event pump stops firing BGA cues there, so the cue clock is clamped (freeze, not
  // rewind) while an already-playing movie keeps running on the true clock. All other `seconds` readers here
  // (progress ratio, time readouts, judge-until comparisons) stay on the true clock.
  const reversalSeconds = ctx.play.timingResolver?.reversal?.seconds;
  const bgaCueSeconds = reversalSeconds === undefined ? seconds : Math.min(seconds, reversalSeconds);
  // Chart-authored playfield options (#931): DARK hides the BGA, REVERSE mirrors the note stream in the lane area.
  const engineFrame = ctx.play.engineFrame;
  const chartPlayfield = resolveChartPlayfieldVisual(engineFrame?.chartOptions, engineFrame?.currentSeconds ?? seconds);
  ctx.bgaLayer.visible = !chartPlayfield.hideBGA;
  Eif (!chartPlayfield.hideBGA) ctx.perf.time('renderBGA', () => renderBGA(ctx, seconds, bgaCueSeconds));
  ctx.perf.time('renderLanes', () => renderLanes(ctx, designWidth, designHeight));
  ctx.perf.time('renderNotes', () => renderNotes(ctx, seconds, designHeight));
  let laneTop = Number.POSITIVE_INFINITY;
  let laneBottom = Number.NEGATIVE_INFINITY;
  Iif (chartPlayfield.reverse) {
    for (const lane of ctx.play.laneX.values()) {
      laneTop = Math.min(laneTop, lane.top);
      laneBottom = Math.max(laneBottom, lane.bottom);
    }
  }
  applyChartReverse(ctx.noteLayer, chartPlayfield.reverse, laneTop, laneBottom);
  ctx.perf.time('renderShutter', () => renderShutter(ctx));
  ctx.perf.time('renderBombs', () => renderBombs(ctx));
  ctx.perf.time('renderText', () => renderText(ctx, designWidth, designHeight, seconds));
}
 
/** Redraws the design clip mask and background for a stage of `size`, when it differs from the last one drawn. */
function stampDesignRect(ctx: PlayFrameContext, size: PlayfieldStage): void {
  if (size.width === ctx.stampedDesignWidth && size.height === ctx.stampedDesignHeight) return;
  ctx.stampedDesignWidth = size.width;
  ctx.stampedDesignHeight = size.height;
  ctx.designClipMask.clear().rect(0, 0, size.width, size.height).fill(0xffffff);
  ctx.background.clear().rect(0, 0, size.width, size.height).fill(BG);
}