All files / player-browser/src/view/beatoraja/select skin-state.ts

89.74% Statements 35/39
90.47% Branches 38/42
71.42% Functions 5/7
87.87% Lines 29/33

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                                                    30x                 30x                                                         20x                   74x 21x 74x                     20x             55x     55x   55x 55x   54x 54x 54x     709x           53x 26x 26x 43x       43x     53x                   115x 62x 62x     115x         115x                                                             9x   9x 9x                      
/**
 * What the beatoraja select view's skin reads of the screen (`BeatorajaSelectView`), through
 * `@be-music/skin-beatoraja/scene`: the frame's select input (the focused bar and its chart's facts, the folder, the
 * search, the list's key-mode filter and sort, the player's LN MODE, the skin, the judge timing, the bar's best lamp, the
 * clock), the texts, numbers and image-set values resolved from it, and the active ops: the skin's options, the focused
 * bar's ops and the buttons pressed a moment ago.
 *
 * The part reads the view's state through {@link BeatorajaSelectSkinStateContext}; the select view passes it.
 */
 
import { buildBaseOpSet } from '@be-music/skin-beatoraja';
import {
  beatorajaWallClockOf,
  resolveBeatorajaConstantOps,
  resolveBeatorajaSelectBarOps,
  resolveBeatorajaSelectNumber,
  resolveBeatorajaSelectRefValue,
  resolveBeatorajaSelectText,
  resolveBeatorajaOptionIndex,
  resolveBeatorajaPlayOptionIndex,
  type BeatorajaSelectInput,
} from '@be-music/skin-beatoraja/scene';
import type { BrowseListOptions, LibraryEntry, PlayOptions } from '@be-music/player-scene-model';
import type { BeatorajaSceneKit } from '../../../skin/beatoraja/scene-kit.ts';
import { selectChartFacts } from './chart-facts.ts';
 
const LONG_NOTE_MODE_INDICES: Readonly<Record<PlayOptions['longNoteMode'], 0 | 1 | 2>> = { LN: 0, CN: 1, HCN: 2 };
 
/**
 * How long a `BUTTON_*` op stays active after a click fires (audit 2.1). 200ms is long
 * enough for the player to perceive the press-state highlight in skins that author short
 * "press feedback" tweens, short enough that subsequent clicks don't pile up if the user
 * mashes a button. Beatoraja itself gates press feedback on Java's input-frame state which
 * lasts a single frame; our 200ms window gives smooth animation under a 60fps tick.
 */
const BUTTON_PRESS_WINDOW_MS = 200;
 
/** What the skin state reads on the select view: its fields and methods of these names. */
export interface BeatorajaSelectSkinStateContext {
  /** The skin and the player's options for it. */
  readonly options: Pick<BeatorajaSceneKit, 'skin' | 'skinConfig'>;
  /** The play options, which the skin's option buttons show and step. */
  readonly playOptions?: Partial<PlayOptions>;
  readonly entries: ReadonlyArray<LibraryEntry>;
  /** The cursor: `entries[currentIndex]` is the focused bar. */
  readonly currentIndex: number;
  /** The search as typed. */
  readonly searchQuery: string;
  /** The list's key-mode filter and sort (the MODE and SORT buttons). */
  readonly list: Pick<BrowseListOptions, 'keysFilter' | 'sort'>;
  currentFolderLabel(): string;
  /** The best clear lamp of a chart or course bar, as its op (`beatorajaClearLampOp`); `undefined` for none. */
  lampOpOf(entry: LibraryEntry): number | undefined;
  /** The focused bar's replay slots and the one picked (`SelectFrame.replaySlots`), when the host keeps them. */
  readonly replaySlots?: { readonly exists: readonly boolean[]; readonly selected: number };
  /** The clock a button press is timed on, in ms. */
  nowMs(): number;
}
 
/** What the skin reads of the screen. See the module comment. */
export class BeatorajaSelectSkinState {
  private readonly ctx: BeatorajaSelectSkinStateContext;
 
  constructor(ctx: BeatorajaSelectSkinStateContext) {
    this.ctx = ctx;
  }
 
  /**
   * Stable per-skin op set — `skin_config.option` picks plus any always-on flags. Cached because
   * `buildBaseOpSet` walks the option map; recompute only when a new kit invalidates the
   * cache. Live per-frame ops (focused-bar kind, chart keymode) are added on top in
   * {@link computeActiveOps}.
   */
  baseOps(): ReadonlySet<number> {
    if (this.cachedBaseOps !== undefined) return this.cachedBaseOps;
    this.cachedBaseOps = buildBaseOpSet(this.ctx.options.skinConfig?.option);
    return this.cachedBaseOps;
  }
  cachedBaseOps: ReadonlySet<number> | undefined;
  /**
   * Transient BUTTON-press tracker (audit 2.1). Maps each clicked `act` code to the
   * time (`nowMs`) at which the press happened. `computeActiveOps` adds the
   * code to the active op set while it's still within {@link BUTTON_PRESS_WINDOW_MS}, so
   * skins gating press-state chrome on the matching op see the highlight in flight. Older
   * entries get garbage-collected lazily during op resolution; the map stays bounded since
   * the user can only click so many buttons per second.
   */
  readonly buttonPressedAt = new Map<number, number>();
 
  /**
   * Per-frame active op set: the stable base ops, the focused bar's ops and the buttons pressed a moment ago. Without
   * the bar's ops the info pane stays hidden: its destinations are gated on `op:[2]` (`SONGBAR`).
   */
  computeActiveOps(): ReadonlySet<number> {
    const base = this.baseOps();
    // The player's CONSTANT setting lights `OPTION_CONSTANT` on every bar: beatoraja reads the selected bar's play
    // config, and Be-Music keeps one for every key mode.
    const constantOps = resolveBeatorajaConstantOps(this.ctx.playOptions?.visualConstant);
    // Fast path: no active entry → just the base set (includes `skin_config.option` picks).
    const entry = this.ctx.entries[this.ctx.currentIndex];
    if (entry === undefined && constantOps.length === 0) return base;
 
    const ops = new Set(base);
    for (const op of constantOps) ops.add(op);
    if (entry === undefined) return ops;
    // The bar's ops: the screen's fixed state, and the folder, course or chart bar with its key mode, traits,
    // difficulty, judge rank and clear lamp (`resolveBeatorajaSelectBarOps`).
    for (const op of resolveBeatorajaSelectBarOps(this.selectInput())) ops.add(op);
 
    // Transient BUTTON-press feedback (audit 2.1). Each `act` code clicked within the last
    // `BUTTON_PRESS_WINDOW_MS` lights its matching op so destinations gating press-state
    // chrome on `op = {act}` show their pressed visual. GC stale entries lazily — the map's
    // size is bounded by user-click rate, which can never exceed ~10/sec.
    if (this.buttonPressedAt.size > 0) {
      const now = this.ctx.nowMs();
      for (const [act, pressedAtMs] of this.buttonPressedAt) {
        Iif (now - pressedAtMs >= BUTTON_PRESS_WINDOW_MS) {
          this.buttonPressedAt.delete(act);
          continue;
        }
        ops.add(act);
      }
    }
    return ops;
  }
 
  /**
   * What the skin reads of the screen (`@be-music/skin-beatoraja/scene`): the focused bar and its chart's facts, the
   * folder, the search, the list's key-mode filter and sort, the player's LN MODE, the skin, the judge timing, the bar's
   * best lamp, its replay slots and the clock. Built once per frame by the view's draw; read through it for every value, text and image
   * of the frame.
   */
  selectInput(): BeatorajaSelectInput {
    if (this.frameInput !== undefined) return this.frameInput;
    const entry = this.ctx.entries[this.ctx.currentIndex];
    const offset = this.ctx.playOptions?.judgeTimingMs ?? this.ctx.options.skinConfig?.offset;
    // A random course bar has no record.
    const lampOp =
      entry === undefined || (entry.kind === 'course' && entry.course.random !== undefined)
        ? undefined
        : entry.kind === 'folder'
          ? undefined
          : this.ctx.lampOpOf(entry);
    return {
      focused: entry,
      ...(entry?.kind === 'chart' ? { chart: selectChartFacts(entry.chart) } : {}),
      folderLabel: this.ctx.currentFolderLabel(),
      searchText: this.ctx.searchQuery,
      list: { keysFilter: this.ctx.list.keysFilter, sort: this.ctx.list.sort },
      ...(this.ctx.playOptions?.longNoteMode !== undefined
        ? { lnMode: LONG_NOTE_MODE_INDICES[this.ctx.playOptions.longNoteMode] }
        : {}),
      skin: { name: this.ctx.options.skin.name, author: this.ctx.options.skin.author },
      judgeTiming: typeof offset === 'number' && Number.isFinite(offset) ? offset : 0,
      ...(lampOp !== undefined ? { lampOp } : {}),
      ...(this.ctx.replaySlots !== undefined
        ? { replaySlots: this.ctx.replaySlots.exists, selectedReplaySlot: this.ctx.replaySlots.selected }
        : {}),
      wallClock: beatorajaWallClockOf(new Date()),
    };
  }
 
  /** The frame's {@link selectInput}, while the view draws it. */
  frameInput: BeatorajaSelectInput | undefined;
 
  resolveSelectionText(refOp: number): string | undefined {
    return resolveBeatorajaSelectText(refOp, this.selectInput());
  }
 
  resolveSelectionNumber(refOp: number): number | undefined {
    return resolveBeatorajaSelectNumber(refOp, this.selectInput());
  }
 
  resolveSelectionRefValue(refOp: number): number {
    const input = this.selectInput();
    // beatoraja's select screen shows the player's LN MODE on the LNMODE image, not the focused chart's.
    const playOptions = this.ctx.playOptions ?? {};
    return (
      resolveBeatorajaOptionIndex(refOp, {
        ...(playOptions.longNoteMode !== undefined ? { longNoteMode: playOptions.longNoteMode } : {}),
        hsFix: playOptions.hsFix,
        lampOp: input.lampOp,
      }) ??
      resolveBeatorajaPlayOptionIndex(refOp, playOptions) ??
      resolveBeatorajaSelectRefValue(refOp, input)
    );
  }
}