All files / player-browser/src/view/built-in decide-view.ts

83.33% Statements 70/84
57.14% Branches 20/35
80% Functions 8/10
87.34% Lines 69/79

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                                                  31x   31x                         15x                 6x 6x   6x 6x 6x 6x 6x 6x 6x             6x 6x     6x 6x 6x                                   3x 3x 3x 3x 3x       9x 9x 9x       6x 6x 6x 6x 6x 6x 6x 6x 6x 6x             2x 2x 2x 2x 2x       2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x 2x   2x                           3x 3x 3x 3x 3x 3x 3x 3x                   3x 3x     1x 1x      
/**
 * The built-in family's decide screen: a Be-Music Skin's `decide` drawn on its stage, letterboxed into the window.
 * It draws the decide session's frames and turns a click into a `continue` intent; the session times the screen.
 */
 
import { resolveStageOrientation } from '@be-music/player-skin-sdk';
import { Color, Container, Graphics } from 'pixi.js';
import type { BrowserAssetSource } from '../../collection/types.ts';
import type { BuiltInKit } from '../../host/family-kit.ts';
import { logger } from '../../logger.ts';
import type { PixiSceneHost } from '../../host/scene-host.ts';
import {
  resolveDesignTextResolution,
  resolveScaledViewport,
  setDesignPixelRatio,
  setDesignTextResolution,
} from '../../stage/viewport.ts';
import {
  BeMusicDecideBinding,
  resolveBeMusicDecideDurationMs,
  resolveBeMusicSkinStage,
} from '../../skin/be-music/binding.ts';
import { ChartImageCache } from '../../skin/be-music/chart-images.ts';
import type { DecideFrame, DecideIntent, DecideView, IntentSink, SceneViewTiming } from '../types.ts';
 
const log = logger('decide');
 
const BG = new Color('#05070b');
 
export interface BuiltInDecideViewOptions {
  /** Where the chart's files are, for its images (`frame.chartImage`); every image reads as unavailable without one. */
  readonly source?: BrowserAssetSource;
}
 
/**
 * The skin's timing on the decide screen: input opens at once (a Be-Music Skin has no input delay), and the screen
 * moves on after the skin's `decide.durationMs`, at once for a skin without a decide screen. No fade-out: the skin's
 * own drawing covers it.
 */
export function resolveBuiltInDecideTiming(kit: BuiltInKit): SceneViewTiming {
  return {
    inputDelayMs: 0,
    autoAdvanceMs: kit.skin.decide !== undefined ? resolveBeMusicDecideDurationMs(kit.skin) : 0,
    fadeOutMs: 0,
  };
}
 
/** The built-in decide view. See the module comment. */
export class BuiltInDecideView implements DecideView {
  public readonly family = 'built-in' as const;
  public readonly root: Container = new Container();
  public readonly timing: SceneViewTiming;
  private readonly designRoot = new Container();
  private readonly viewportBackground = new Graphics();
  private readonly designClipMask = new Graphics();
  private cachedScreenWidth = -1;
  private cachedScreenHeight = -1;
  private cachedDesignWidth = -1;
  private cachedDesignHeight = -1;
  private kit: BuiltInKit;
  private readonly source: BrowserAssetSource | undefined;
  private binding: BeMusicDecideBinding | undefined;
  private chartImages: ChartImageCache | undefined;
  private host: PixiSceneHost | undefined;
  private emit: IntentSink<DecideIntent> | undefined;
  private built = false;
  private disposed = false;
 
  public constructor(kit: BuiltInKit, options: BuiltInDecideViewOptions = {}) {
    this.kit = kit;
    this.source = options.source;
    this.timing = resolveBuiltInDecideTiming(kit);
  }
 
  /**
   * Takes the player's new settings and effect level for the skin; the next frame draws with them. The skin itself
   * stays: another skin needs another view.
   */
  public updateKit(kit: BuiltInKit): void {
    if (kit.skin !== this.kit.skin) {
      log.warn('a decide view keeps its skin; ignoring a kit of another skin');
      return;
    }
    this.kit = kit;
    this.binding?.setSettings(kit.settings);
  }
 
  /** Builds the scene graph 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(host);
    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.binding?.dispose();
      this.binding = undefined;
      this.chartImages?.dispose();
      this.chartImages = undefined;
      this.root.destroy({ children: true, context: true });
    } catch (error) {
      log.warn('decide cleanup threw', error);
    }
  }
 
  public draw(frame: DecideFrame): void {
    const host = this.host;
    Iif (host === undefined || this.disposed) return;
    const skin = this.kit.skin;
    const screen = host.app.screen;
    const { width: designWidth, height: designHeight } = resolveBeMusicSkinStage(
      skin,
      screen.width > 0 && screen.height > 0 ? screen : undefined,
    );
    const screenWidth = screen.width || designWidth;
    const screenHeight = screen.height || designHeight;
    const viewport = resolveScaledViewport(screenWidth, screenHeight, designWidth, designHeight);
    setDesignTextResolution(resolveDesignTextResolution(viewport.scale, host.app.renderer.resolution));
    setDesignPixelRatio(viewport.scale * host.app.renderer.resolution);
    Eif (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.scale);
    host.setStageOrientation(resolveStageOrientation({ width: designWidth, height: designHeight }));
    Eif (this.cachedDesignWidth !== designWidth || this.cachedDesignHeight !== designHeight) {
      this.designClipMask.clear().rect(0, 0, designWidth, designHeight).fill(0xffffff);
      this.cachedDesignWidth = designWidth;
      this.cachedDesignHeight = designHeight;
    }
    this.binding?.render({
      designWidth,
      designHeight,
      nowMs: frame.clock.nowMs,
      elapsedMs: frame.clock.nowMs - frame.clock.enteredAtMs,
      chart: frame.chart,
      playOptions: frame.playOptions,
      effects: this.kit.effects ?? 'full',
      audio: undefined,
    });
  }
 
  /** Builds the scene graph under {@link root} and the skin's binding, once. */
  private build(host: PixiSceneHost): void {
    this.built = true;
    const skin = this.kit.skin;
    this.root.label = 'decide/scene';
    this.designRoot.label = 'decide/root';
    this.viewportBackground.label = 'decide/viewport-bg';
    this.designClipMask.label = 'decide/design-clip';
    this.root.addChild(this.viewportBackground, this.designRoot);
    Iif (skin.decide !== undefined) {
      const source = this.source;
      this.chartImages = new ChartImageCache({ resolveSource: () => source });
      this.binding = new BeMusicDecideBinding(skin, {
        chartImage: this.chartImages.lookup,
        settings: this.kit.settings,
        renderer: host.app.renderer,
      });
      this.designRoot.addChild(this.binding.view);
    }
    this.designRoot.addChild(this.designClipMask);
    this.designRoot.mask = this.designClipMask;
  }
 
  private readonly handlePointerDown = (): void => {
    this.emit?.({ type: 'continue' });
  };
}