All files / player-browser/src/session/play play-cover-keys.ts

96.55% Statements 28/29
96.15% Branches 25/26
100% Functions 4/4
100% Lines 26/26

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                                        79x     46x           162x 162x 221x   162x                             82x 82x 3x   79x 79x 79x 5x   74x 16x   58x 3x   55x 9x   46x 10x   36x 3x   33x 2x   31x    
/**
 * The lane cover keys of a play that takes them (`PlaySessionOptions.coverKeys`, the beatoraja play screen's): PageUp /
 * PageDown move SUDDEN+, Home / End the LIFT, H switches HIDDEN+ and J / K move it. A step is 1 % of the lane, 5 % with
 * Shift. They change the play state's covers like any cover command, so a course that locks the hi-speed locks them
 * too. A key one of the play's lanes takes (H, J and K are 2P lanes of a DP chart) plays the lane instead.
 */
 
import type { LaneCoverCommand } from '@be-music/player-scene-model';
import { keyboardEventToTokens } from '../../runtime/web-input-runtime.ts';
import { DEFAULT_CONTROL_ACTIONS, type ControlActionMap } from '../controls/control-actions.ts';
import type { PlayViewSetting } from '../../view/types.ts';
 
/** What a cover key does: the cover command, and the view setting it changes (for the view's feedback). */
export interface PlayCoverKey {
  readonly command: LaneCoverCommand;
  readonly setting: PlayViewSetting;
}
 
/** A cover key's step: 1 % of the lane, 5 % with Shift. */
function coverStep(shiftKey: boolean): number {
  return shiftKey ? 0.05 : 0.01;
}
 
const NO_LANE_TOKENS: ReadonlySet<string> = new Set();
 
/** The input tokens the play's lanes take (`LaneBinding.inputTokens`, lowercase), for {@link resolvePlayCoverKey}. */
export function resolveLaneInputTokens(
  bindings: readonly { readonly inputTokens: readonly string[] }[] | undefined,
): ReadonlySet<string> {
  const tokens = new Set<string>();
  for (const binding of bindings ?? []) {
    for (const token of binding.inputTokens) tokens.add(token.toLowerCase());
  }
  return tokens;
}
 
/**
 * What a key does to the lane covers by the player's keys (`actions`), or `undefined` for a key that is not a cover key:
 * PageUp raises SUDDEN+ (less of the lane covered) and PageDown lowers it; End raises the LIFT and Home lowers it; H
 * switches HIDDEN+ on (at a quarter of the lane without a height) or off; K raises HIDDEN+ and J lowers it, by default.
 * A key whose input token (from its `code`, as the lane input reads it) is one of `laneTokens`
 * ({@link resolveLaneInputTokens}) is a lane's, not a cover key.
 */
export function resolvePlayCoverKey(
  event: { readonly key: string; readonly code?: string; readonly shiftKey: boolean },
  laneTokens: ReadonlySet<string> = NO_LANE_TOKENS,
  actions: ControlActionMap = DEFAULT_CONTROL_ACTIONS.lr2,
): PlayCoverKey | undefined {
  Iif (event.code === undefined) return undefined;
  if (laneTokens.size > 0 && keyboardEventToTokens({ code: event.code }).some((token) => laneTokens.has(token))) {
    return undefined;
  }
  const keyEvent = { code: event.code, key: event.key, shiftKey: event.shiftKey };
  const step = coverStep(event.shiftKey);
  if (actions.is('play.sudden-up', keyEvent)) {
    return { command: { type: 'cover-adjust', part: 'sudden', delta: -step }, setting: 'sudden' };
  }
  if (actions.is('play.sudden-down', keyEvent)) {
    return { command: { type: 'cover-adjust', part: 'sudden', delta: step }, setting: 'sudden' };
  }
  if (actions.is('play.lift-down', keyEvent)) {
    return { command: { type: 'cover-adjust', part: 'lift', delta: -step }, setting: 'lift' };
  }
  if (actions.is('play.lift-up', keyEvent)) {
    return { command: { type: 'cover-adjust', part: 'lift', delta: step }, setting: 'lift' };
  }
  if (actions.is('play.hidden-toggle', keyEvent)) {
    return { command: { type: 'cover-toggle', part: 'hidden' }, setting: 'hidden-toggle' };
  }
  if (actions.is('play.hidden-down', keyEvent)) {
    return { command: { type: 'cover-adjust', part: 'hidden', delta: -step }, setting: 'hidden' };
  }
  if (actions.is('play.hidden-up', keyEvent)) {
    return { command: { type: 'cover-adjust', part: 'hidden', delta: step }, setting: 'hidden' };
  }
  return undefined;
}