All files / player-browser/src/view/shared/playfield shutter.ts

60.41% Statements 29/48
46.15% Branches 18/39
75% Functions 3/4
72.5% Lines 29/40

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                              6x   6x 6x 6x 6x                                         6x 6x 6x 6x 6x 6x 36x 36x 36x 36x 36x 36x 36x   6x 6x 6x                   6x 6x 6x 6x   6x   6x     6x                     29x                                                  
/** The lane covers of the shared playfield: each side's SUDDEN / HIDDEN masks over its lanes. */
 
import { isChannelOnSide } from '../../../chart/lane-sides.ts';
import type { BeMusicLaneCoverStyle } from '@be-music/player-skin-sdk';
import { resolveCoverStripes, type CoverRect } from './cover-pattern.ts';
import type { PlayfieldContext } from './context.ts';
 
/**
 * Draws the LR2 HIDDEN / SUDDEN / HID+SUD masks over the playfield, opaque and striped in the skin's
 * {@link BeMusicLaneCoverStyle}: each side's {@link resolveLaneCovers} cover (the
 * `shutter` fraction at the top, the bottom or both ends, nothing while LANE COVER is off, or what a replay shows),
 * over the union of that side's lanes' top/bottom from `laneX`, so the mask exactly covers the lane area however the
 * theme laid the lanes out (single-side, DP, etc.).
 */
export function renderShutter(ctx: PlayfieldContext): void {
  ctx.shutterLayer.clear();
  // A Be-Music Skin that draws its own covers reads them from `runtime.laneCovers` instead.
  Iif (ctx.skinBinding?.drawsLaneCover) return;
  Iif (ctx.play.laneX.size === 0) return;
  for (const cover of ctx.play.resolveLaneCovers()) {
    paintSideShutter(
      ctx,
      collectSideBounds(ctx, cover.side),
      cover.sudden,
      cover.hidden,
      ctx.drawsLift ? cover.lift : 0,
    );
  }
}
 
function collectSideBounds(
  ctx: PlayfieldContext,
  side: '1P' | '2P',
):
  | {
      left: number;
      right: number;
      top: number;
      bottom: number;
    }
  | undefined {
  let left = Number.POSITIVE_INFINITY;
  let right = Number.NEGATIVE_INFINITY;
  let top = Number.POSITIVE_INFINITY;
  let bottom = Number.NEGATIVE_INFINITY;
  let any = false;
  for (const [channel, lane] of ctx.play.laneX) {
    const onSide = isChannelOnSide(channel, side);
    Iif (!onSide) continue;
    any = true;
    left = Math.min(left, lane.x);
    right = Math.max(right, lane.x + lane.w);
    top = Math.min(top, lane.top);
    bottom = Math.max(bottom, lane.baseBottom ?? lane.bottom);
  }
  Iif (!any) return undefined;
  Iif (!Number.isFinite(left) || !Number.isFinite(right) || bottom - top <= 0) return undefined;
  return { left, right, top, bottom };
}
 
function paintSideShutter(
  ctx: PlayfieldContext,
  bounds: { left: number; right: number; top: number; bottom: number } | undefined,
  sudden: number,
  hidden: number,
  lift: number,
): void {
  Iif (!bounds) return;
  const style = ctx.skinBinding?.laneCoverStyle ?? DEFAULT_LANE_COVER_STYLE;
  const width = bounds.right - bounds.left;
  const height = bounds.bottom - bounds.top;
  // SUDDEN+ at the top of the lanes, its inner edge at its bottom; notes emerge below it.
  Iif (sudden > 0) paintCover(ctx, style, { x: bounds.left, y: bounds.top, w: width, h: height * sudden }, 'bottom');
  // LIFT at the bottom, its inner edge at its top: the raised judgement line.
  Iif (lift > 0)
    paintCover(ctx, style, { x: bounds.left, y: bounds.bottom - height * lift, w: width, h: height * lift }, 'top');
  // HIDDEN+ just above the LIFT (or the judgement line), its inner edge at its top.
  Iif (hidden > 0) {
    paintCover(
      ctx,
      style,
      { x: bounds.left, y: bounds.bottom - height * (lift + hidden), w: width, h: height * hidden },
      'top',
    );
  }
}
 
/** The covers of a skin that gives no {@link BeMusicLaneCoverStyle}: black with faint grey stripes. */
export const DEFAULT_LANE_COVER_STYLE: BeMusicLaneCoverStyle = {
  color: 0x000000,
  stripeColor: 0xffffff,
  stripeAlpha: 0.05,
};
 
/** One opaque cover over `rect`, striped from its `inner` edge (the one facing the notes). */
function paintCover(
  ctx: PlayfieldContext,
  style: BeMusicLaneCoverStyle,
  rect: CoverRect,
  inner: 'top' | 'bottom',
): void {
  const layer = ctx.shutterLayer;
  layer.rect(rect.x, rect.y, rect.w, rect.h).fill({ color: style.color, alpha: 1 });
  if (style.stripeAlpha > 0) {
    const anchorY = inner === 'top' ? rect.y : rect.y + rect.h;
    for (const stripe of resolveCoverStripes(rect, anchorY)) layer.poly(stripe);
    layer.fill({ color: style.stripeColor, alpha: style.stripeAlpha });
  }
  if (style.edgeColor !== undefined && rect.h >= 1) {
    const edgeY = inner === 'top' ? rect.y : rect.y + rect.h - 1;
    layer.rect(rect.x, edgeY, rect.w, 1).fill({ color: style.edgeColor, alpha: style.edgeAlpha ?? 1 });
  }
}