All files / skin-beatoraja/src/elements timing-visualizer.ts

100% Statements 12/12
100% Branches 6/6
100% Functions 2/2
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 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                                                                                                                                                  188x 188x 188x 6x 6x   188x       6x 6x 6x 5x                                        
// Strict-typed normalization for beatoraja `timingvisualizer[]` declarations.
//
// The timingvisualizer plots the player's recent judgement timing offsets on a horizontal scale
// (late on the left, early on the right, perfect at center: beatoraja's offsets are note time
// minus press time, drawn at `center + offset`). Each judgement adds one mark; older
// samples fade upward. Authors point this at a destination strip on the play screen so the
// player can read their timing pattern at a glance during the run.
//
// Beatoraja's `SkinTimingVisualizer` (java) takes:
//   - `width` — pixel width used for the px/ms rate (`judgeWidthRate = width / (judgeWidthMillis
//     * 2 + 1)`). Default `301` is engineered so that with the default `judgeWidthMillis = 150`
//     the rate is exactly 1 px/ms (`301 / 301 == 1`).
//   - `judgeWidthMillis` — horizontal half-range in ms (e.g. 100 = ±100ms span across the box)
//   - `lineWidth` — thickness of each sample line
//   - `lineColor` / `centerColor` / `PGColor` / `GRColor` / `GDColor` / `BDColor` / `PRColor` —
//     judgement-band colors (RRGGBB or RRGGBBAA hex strings)
//   - `transparent` — base alpha for sample lines
//   - `drawDecay` — speed at which older samples fade
//
// The reference theme (`play7main.lua`) authors the element as just `{id = "timing"}` and relies
// on engine-side defaults for everything else. We preserve every authored field verbatim so the
// renderer can either honor them when supplied or pick its own defaults — most skins won't
// supply them.
//
// Defaults match `JSONSkin.java:297-311` (`TimingVisualizer`):
//   width = 301, judgeWidthMillis = 150, lineWidth = 1, transparent = 0, drawDecay = 1.
 
import { flattenBeatorajaElements, type NormalizedElement } from './base.ts';
import { numberField, pickHex, stringField } from './fields.ts';
import type { BeatorajaImageId } from './image.ts';
 
export interface BeatorajaTimingVisualizerElement {
  /** Destination id this visualizer targets. Same id space as `image[]` / `graph[]` / `text[]`. */
  id: BeatorajaImageId;
  /**
   * Pixel width that drives the px/ms rate. Mirrors upstream
   * `SkinTimingVisualizer.java:63` `judgeWidthRate = width / (judgeWidthMillis * 2 + 1)`. The
   * authored `width` is INDEPENDENT of the destination's runtime `region.width` — the
   * background is stretched to `region.width` but sample-line offsets are computed against
   * this value so the px/ms scale stays constant if the dst rect animates. Default `301`
   * matches `JSONSkin.java:299`.
   */
  width: number;
  /**
   * Horizontal half-range in ms. The destination box represents `[-judgeWidthMillis,
   * +judgeWidthMillis]`. Default `150` matches `JSONSkin.java:300` (= the half-range of the
   * GOOD window in most judges, paired with `width = 301` to give exactly 1 px/ms).
   */
  judgeWidthMillis: number;
  /** Pixel thickness of each sample line. `0` falls back to the renderer's default (~1px). */
  lineWidth: number;
  /** Base alpha (0..255) for sample lines. `0` means "fully opaque" (fallback). */
  transparent: number;
  /**
   * Sample fade decay rate. Higher values fade older samples faster. `0` falls back to the
   * renderer's default. Beatoraja uses this to scale the alpha-step between consecutive lines.
   */
  drawDecay: number;
  /** Base color of the sample line strokes (RRGGBB or RRGGBBAA hex; no `#`). */
  lineColor: string;
  /** Center / "perfect timing" guide line color. */
  centerColor: string;
  /** Per-judgement-band colors (RRGGBB[AA] hex). Empty string = renderer fallback. */
  pgColor: string;
  grColor: string;
  gdColor: string;
  bdColor: string;
  prColor: string;
  /** `if` codes that gate visibility (AND-merged with the destination's `op[]`). */
  ifCodes: ReadonlyArray<number>;
}
 
export function normalizeBeatorajaTimingVisualizers(input: unknown): BeatorajaTimingVisualizerElement[] {
  const flattened = flattenBeatorajaElements(input);
  const out: BeatorajaTimingVisualizerElement[] = [];
  for (const entry of flattened) {
    const normalized = normalizeOne(entry);
    if (normalized !== undefined) out.push(normalized);
  }
  return out;
}
 
function normalizeOne(entry: NormalizedElement): BeatorajaTimingVisualizerElement | undefined {
  const f = entry.fields;
  const id = f.id;
  if (typeof id !== 'string' && typeof id !== 'number') return undefined;
  return {
    id,
    // Defaults follow `JSONSkin.java:297-311`. Most skins author only `id` and rely on these.
    width: numberField(f, 'width', 301),
    judgeWidthMillis: numberField(f, 'judgeWidthMillis', 150),
    lineWidth: numberField(f, 'lineWidth', 1),
    transparent: numberField(f, 'transparent', 0),
    drawDecay: numberField(f, 'drawDecay', 1),
    lineColor: stringField(f, 'lineColor', ''),
    centerColor: stringField(f, 'centerColor', ''),
    // Beatoraja's java field naming uses uppercase abbreviations (`PGColor` etc.); we
    // case-insensitively pick whichever the author supplied.
    pgColor: pickHex(f, ['pgColor', 'PGColor']),
    grColor: pickHex(f, ['grColor', 'GRColor']),
    gdColor: pickHex(f, ['gdColor', 'GDColor']),
    bdColor: pickHex(f, ['bdColor', 'BDColor']),
    prColor: pickHex(f, ['prColor', 'PRColor']),
    ifCodes: entry.ifCodes,
  };
}