All files / app-play/src/ui/keyboard gamepad-guide.ts

97.43% Statements 38/39
89.28% Branches 25/28
90.9% Functions 10/11
97.22% Lines 35/36

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                                      1x                                 1x   7x   4x       7x             1x     1x                                       43x 43x                 4x 4x 38x 38x 38x   4x 5x   4x 60x 60x                   3x 2x 2x 16x 2x 2x 1x 1x 14x   1x 1x 9x   1x 8x       1x 14x        
// What Help's Keyboard tab lists for the gamepads: which button of a standard pad (the CS DualShock layout) and of an
// IIDX controller is which key, turntable key, START or SELECT. In a play a standard pad's D-pad is key 1; on the other
// screens it is the arrow keys, and START / SELECT are the pad's Start and Back.
import {
  IIDX_GAMEPAD_MENU_BUTTONS,
  STANDARD_GAMEPAD_MENU_BUTTONS,
  type ControlButton,
  type ControlScreen,
  type GamepadMenuButton,
} from '@be-music/player-browser/host';
import { controlButtonName, type KeyboardGuideLang, type KeyboardGuideRow } from './keyboard-guide.ts';
 
/** The two lists: a standard pad's buttons and an IIDX controller's. */
export interface GamepadGuide {
  readonly standard: readonly KeyboardGuideRow[];
  readonly iidx: readonly KeyboardGuideRow[];
}
 
/** A standard pad's buttons (Web Gamepad standard indices) by their Xbox and PlayStation names. */
const STANDARD_NAMES: Readonly<Record<number, string>> = {
  0: 'A / ✕',
  1: 'B / ○',
  2: 'X / □',
  3: 'Y / △',
  4: 'LB / L1',
  5: 'RB / R1',
  6: 'LT / L2',
  7: 'RT / R2',
  8: 'Back / Share',
  9: 'Start / Options',
  12: 'D-pad ↑',
  13: 'D-pad ↓',
  14: 'D-pad ←',
  15: 'D-pad →',
};
 
const COPY = {
  en: {
    iidxButton: (n: number) => `Button ${n}`,
    turntable: 'Turntable',
    arrow: (arrow: string) => `${arrow} key`,
    dpad: 'D-pad',
  },
  ja: {
    iidxButton: (n: number) => `ボタン ${n}`,
    turntable: 'ターンテーブル',
    arrow: (arrow: string) => `${arrow} キー`,
    dpad: '十字キー',
  },
} as const;
 
const ARROWS: Partial<Record<GamepadMenuButton, string>> = { up: '↑', down: '↓', left: '←', right: '→' };
 
/** The order the rows list what a button is in. */
const ORDER: readonly GamepadMenuButton[] = [
  'key1',
  'key2',
  'key3',
  'key4',
  'key5',
  'key6',
  'key7',
  'scratch-cw',
  'scratch-ccw',
  'start',
  'select',
  'up',
  'down',
  'left',
  'right',
];
 
/** How what a 1P pad button is reads: a control button by its name, a D-pad direction as its arrow key. */
function buttonName(button: GamepadMenuButton, lang: KeyboardGuideLang): string {
  const arrow = ARROWS[button];
  return arrow !== undefined ? COPY[lang].arrow(arrow) : controlButtonName(`1P-${button}` as ControlButton, lang);
}
 
function rows(
  layout: Readonly<Record<number, GamepadMenuButton>>,
  name: (index: number) => string,
  lang: KeyboardGuideLang,
  extra: Partial<Record<GamepadMenuButton, readonly string[]>> = {},
): KeyboardGuideRow[] {
  const byButton = new Map<GamepadMenuButton, string[]>();
  for (const [index, button] of Object.entries(layout)) {
    const list = byButton.get(button) ?? [];
    list.push(name(Number(index)));
    byButton.set(button, list);
  }
  for (const [button, names] of Object.entries(extra) as [GamepadMenuButton, readonly string[]][]) {
    byButton.set(button, [...(byButton.get(button) ?? []), ...names]);
  }
  return ORDER.flatMap((button) => {
    const keys = byButton.get(button);
    return keys === undefined ? [] : [{ id: `gamepad-${button}`, name: buttonName(button, lang), keys, codes: [] }];
  });
}
 
/**
 * The gamepad lists of `screen`: on the select, decide and result screens the layout `runtime/gamepad-menu.ts` reads; in
 * a play the one the play reads, where a standard pad's D-pad is key 1 and it has no START or SELECT. The second pad is
 * 2P's, with the same buttons.
 */
export function buildGamepadGuide(screen: ControlScreen, lang: KeyboardGuideLang = 'en'): GamepadGuide {
  if (screen === 'global') return { standard: [], iidx: [] };
  const copy = COPY[lang];
  const iidxName = (index: number): string =>
    index <= 6 ? copy.iidxButton(index + 1) : index === 8 ? 'E1' : index === 9 ? 'E2' : copy.iidxButton(index + 1);
  const turntable = { 'scratch-cw': [`${copy.turntable} ↻`], 'scratch-ccw': [`${copy.turntable} ↺`] } as const;
  if (screen === 'play') {
    const standard: Record<number, GamepadMenuButton> = {};
    for (const [index, button] of Object.entries(STANDARD_GAMEPAD_MENU_BUTTONS)) {
      if (Number(index) < 8) standard[Number(index)] = button;
    }
    const iidx: Record<number, GamepadMenuButton> = {};
    for (const [index, button] of Object.entries(IIDX_GAMEPAD_MENU_BUTTONS)) {
      if (Number(index) <= 6) iidx[Number(index)] = button;
    }
    return {
      standard: rows(standard, (index) => STANDARD_NAMES[index] ?? String(index), lang, { key1: [copy.dpad] }),
      iidx: rows(iidx, iidxName, lang, turntable),
    };
  }
  return {
    standard: rows(STANDARD_GAMEPAD_MENU_BUTTONS, (index) => STANDARD_NAMES[index] ?? String(index), lang),
    iidx: rows(IIDX_GAMEPAD_MENU_BUTTONS, iidxName, lang, turntable),
  };
}