All files / player-skin-sdk/src song-info.ts

100% Statements 12/12
100% Branches 8/8
100% Functions 1/1
100% Lines 10/10

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                                            7x         7x 7x 6x 7x 7x 3x 3x   6x 6x    
import type { BeMusicGameplayRuntime } from './types.ts';
 
/** The song info a skin draws in one layer: the title, artist and genre the runtime names it by. */
export interface BeMusicSongInfoText {
  chartTitle?: string;
  chartArtist?: string;
  chartGenre?: string;
}
 
/** One song info layer to draw this frame, at `alpha` (0-1). */
export interface BeMusicSongInfoLayer {
  info: BeMusicSongInfoText;
  alpha: number;
}
 
/**
 * The song info layers to draw this frame, back to front: the current song alone at full opacity, or, while a NONSTOP
 * play crossfades into the next stage's song (`runtime.songInfoFade`), the old song fading out under the new one fading
 * in. A skin draws its title block once per layer, multiplying each layer's opacity in, so it crossfades without
 * tracking anything itself; a layer at 0 is left out.
 */
export function resolveSongInfoLayers(runtime: BeMusicGameplayRuntime): BeMusicSongInfoLayer[] {
  const current: BeMusicSongInfoText = {
    chartTitle: runtime.chartTitle,
    chartArtist: runtime.chartArtist,
    chartGenre: runtime.chartGenre,
  };
  const fade = runtime.songInfoFade;
  if (fade === undefined) return [{ info: current, alpha: 1 }];
  const progress = Number.isFinite(fade.progress) ? Math.min(1, Math.max(0, fade.progress)) : 1;
  const layers: BeMusicSongInfoLayer[] = [];
  if (progress < 1) {
    const { progress: _progress, ...previous } = fade;
    layers.push({ info: previous, alpha: 1 - progress });
  }
  if (progress > 0) layers.push({ info: current, alpha: progress });
  return layers;
}