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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 3x 3x 3x | import { html, render } from 'lit';
import { optionLabel } from './labels.ts';
// The Options dialog's key config (the Keys tab): the key map the built-in skins' screens read (LR2's or beatoraja's),
// then one page per key config group (5 / 7 / 10 / 14 KEY, 9 KEY), a row per lane with the key it takes, and on the
// 5 / 7 / 10 / 14 KEY page each side's START and SELECT. Clicking a row's key waits for the next key press and gives the
// row that key; Esc cancels. A lane cannot take START's or SELECT's key, nor they a lane's. Built with the dialog as
// `this` (`OptionsDialog`); the keys are `lane-keys.ts`' and `control-keys.ts`', and a play reads them as it starts, so
// the rows are disabled during one.
import {
CONTROL_KEY_CONFIG,
effectiveControlKey,
keyboardEventToTokens,
PLAY_SCENE_KEY_CODES,
resolveControlButtons,
type ControlKeyId,
type ControlScheme,
} from '@be-music/player-browser/host';
import { LANE_KEY_CONFIG, laneKeyLabel, type LaneKeyGroup } from '@be-music/player-engine/lanes';
import { assignLaneKey, effectiveLaneKey, resetLaneKeys } from '../../lane-keys.ts';
import { assignControlKey, overwriteActionKeysWithScheme } from '../../control-keys.ts';
import { buildActionKeyPage } from './action-keys.ts';
import { laneKeyConfigLabel } from './action-labels.ts';
export { laneKeyConfigLabel };
import { playerLang } from '../../player-copy.ts';
import { optionLabelContent, type OptionContainer } from '../options-controls.ts';
import type { OptionsDialog } from './index.ts';
/** The page of the screens' operations (`action-keys.ts`). */
export const SCREENS_PAGE_TITLE = 'Screens';
/** Each group's page title. */
const GROUP_TITLES: Readonly<Record<LaneKeyGroup, string>> = { beat: '5 / 7 / 10 / 14 KEY', popn: '9 KEY' };
/** START's or SELECT's name in the key config (`1P START`), which `labels.ts` translates. */
export function controlKeyConfigLabel(id: ControlKeyId): string {
const [side, button] = id.split('-');
return `${side} ${button!.toUpperCase()}`;
}
/** A row of the key config: a lane of a group, or START / SELECT. */
type KeyConfigRow = { readonly group: LaneKeyGroup; readonly id: string } | { readonly control: ControlKeyId };
function sameRow(left: KeyConfigRow | undefined, right: KeyConfigRow): boolean {
if (left === undefined) return false;
if ('control' in left || 'control' in right) {
return 'control' in left && 'control' in right && left.control === right.control;
}
return left.group === right.group && left.id === right.id;
}
const PROMPTS = {
en: { waiting: 'Press a key… (Esc cancels)', refused: 'That key cannot be used — press another' },
ja: { waiting: 'キーを押してください… (Esc で取り消し)', refused: 'そのキーは使えません。別のキーを押してください' },
} as const;
/** What the overwrite of the key config by `scheme`'s keys asks first, in the player's language. */
function overwritePrompt(scheme: ControlScheme): string {
const name = scheme === 'lr2' ? 'LR2' : 'beatoraja';
return playerLang() === 'ja'
? `画面の操作のキーを ${name} のキー設定で上書きします。自分で変えたキーは元に戻ります。よろしいですか?`
: `Overwrite the screens' keys with ${name}'s? Keys you changed go back to ${name}'s.`;
}
/** Builds the key config into `folder`. */
export function buildKeyConfigFolder(this: OptionsDialog): void {
let repaintActionKeys: (() => void) | undefined;
// Picking LR2's or beatoraja's controls overwrites the key config with that key map's keys (asking first when the
// player has keys of their own it would drop); it is not a mode the keys of their own survive.
let scheme = this.guiState.builtInControlScheme;
this.layout?.setGroupReset('keyConfigPages', () => {
if (schemeController.disabled) return;
this.guiState.actionKeys = {};
this.guiState.laneKeys = {};
this.guiState.controlKeys = {};
if (waiting !== undefined) stop();
else paint();
repaintActionKeys?.();
this.scheduleSettingsSave();
});
const schemeController = this.trackPlayOption(
this.place('builtInControlScheme').addBinding(this.guiState, 'builtInControlScheme', {
label: 'Built-in skin controls',
options: [
{ text: 'LR2', value: 'lr2' },
{ text: 'beatoraja', value: 'beatoraja' },
],
}),
);
schemeController.on('change', ({ value }) => {
if (value === scheme) return;
const next = overwriteActionKeysWithScheme(this.guiState.actionKeys, value);
if (next !== this.guiState.actionKeys && !window.confirm(overwritePrompt(value))) {
this.guiState.builtInControlScheme = scheme;
schemeController.refresh();
return;
}
scheme = value;
this.guiState.actionKeys = next;
this.scheduleSettingsSave();
repaintActionKeys?.();
});
const groups = Object.keys(GROUP_TITLES) as LaneKeyGroup[];
const pages = this.place('keyConfigPages').addTab({
// The screens' keys first, then each lane group's: 5 / 7 / 10 / 14 KEY, then 9 KEY.
pages: [{ title: SCREENS_PAGE_TITLE }, ...groups.map((group) => ({ title: GROUP_TITLES[group] }))],
}).pages;
const screensPage = pages[0];
if (screensPage !== undefined) repaintActionKeys = buildActionKeyPage.call(this, screensPage);
const views: (() => void)[] = [];
let waiting: (KeyConfigRow & { refused?: boolean }) | undefined;
const prompts = () => PROMPTS[playerLang() === 'ja' ? 'ja' : 'en'];
const keyOf = (row: KeyConfigRow): string =>
'control' in row
? effectiveControlKey(this.guiState.controlKeys, row.control)
: effectiveLaneKey(this.guiState.laneKeys, row.group, row.id);
const paint = (): void => {
for (const update of views) update();
};
const stop = (): void => {
document.removeEventListener('keydown', onKey, true);
waiting = undefined;
paint();
};
/**
* Whether `token` is another row's key it cannot take: START's or SELECT's, or a turntable's second key, for a 5 / 7 /
* 10 / 14 KEY lane; a lane's (its turntable keys too) for START or SELECT. The screens could not tell them apart.
*/
const takenByOtherKind = (row: KeyConfigRow, token: string): boolean => {
if (!('control' in row)) {
if (row.group !== 'beat') return false;
// A turntable's second key (Ctrl, and Space on the 1P side) stays the turntable's.
const buttons = resolveControlButtons({ laneKeys: this.guiState.laneKeys }).get(token) ?? [];
if (buttons.some((button) => button.endsWith('-scratch-ccw'))) return true;
return CONTROL_KEY_CONFIG.some((entry) => effectiveControlKey(this.guiState.controlKeys, entry.id) === token);
}
const buttons = resolveControlButtons({ laneKeys: this.guiState.laneKeys }).get(token) ?? [];
return buttons.some((button) => !button.endsWith('-start') && !button.endsWith('-select'));
};
// The next key press, ahead of the dialog (whose Esc would close it) and of the screens behind it.
const onKey = (event: KeyboardEvent): void => {
if (waiting === undefined) return;
event.preventDefault();
event.stopPropagation();
if (event.repeat) return;
if (event.code === 'Escape') {
stop();
return;
}
const token = keyboardEventToTokens(event)[0];
// A play keeps Esc, Pause, F5, Backspace and the arrows for itself; a key with no token cannot be a row's.
if (token === undefined || PLAY_SCENE_KEY_CODES.has(event.code) || takenByOtherKind(waiting, token)) {
waiting.refused = true;
paint();
return;
}
if ('control' in waiting) {
this.guiState.controlKeys = assignControlKey(this.guiState.controlKeys, waiting.control, token);
} else {
this.guiState.laneKeys = assignLaneKey(this.guiState.laneKeys, waiting.group, waiting.id, token);
}
this.scheduleSettingsSave();
stop();
};
const dialog = document.querySelector('#options-modal');
dialog?.addEventListener('close', () => {
if (waiting !== undefined) stop();
});
const addRow = (page: OptionContainer, label: string, row: KeyConfigRow): void => {
const content = document.createElement('div');
content.className = 'options-key-row';
let language = playerLang();
const update = (): void => {
const active = sameRow(waiting, row);
const text = active ? (waiting?.refused ? prompts().refused : prompts().waiting) : laneKeyLabel(keyOf(row));
render(
html`<span class="options-label-wrap"
><span class="label options-label">${optionLabelContent(optionLabel(label, language))}</span
><span data-option-help-host></span
></span>
<button
type="button"
class="btn options-key"
data-variant="outline"
data-size="sm"
aria-pressed=${String(active)}
@click=${() => {
if (sameRow(waiting, row)) {
stop();
return;
}
if (waiting === undefined) document.addEventListener('keydown', onKey, true);
waiting = { ...row };
paint();
}}
>
${text}
</button>`,
content,
);
};
const option = page.addElement(content, (nextLanguage) => {
language = nextLanguage === 'ja' ? 'ja' : 'en';
update();
});
option.element.dataset.optionLabel = label;
this.trackPlayOption(option);
views.push(update);
};
groups.forEach((group, groupIndex) => {
const page = pages[groupIndex + 1];
if (page === undefined) return;
if (group === 'beat') {
// Two panes side by side, 1P and 2P, each with its side's lanes, then its START and SELECT, which go with the
// 5 / 7 / 10 / 14 KEY lanes whose keys the screens read them with.
page.element.classList.add('options-key-panes');
const panes = { '1P': page.addFolder({ title: '1P' }), '2P': page.addFolder({ title: '2P' }) } as const;
for (const lane of LANE_KEY_CONFIG.beat) {
addRow(panes[lane.side], laneKeyConfigLabel(group, lane), { group, id: lane.id });
}
for (const entry of CONTROL_KEY_CONFIG) {
addRow(panes[entry.side], controlKeyConfigLabel(entry.id), { control: entry.id });
}
} else {
for (const lane of LANE_KEY_CONFIG[group]) addRow(page, laneKeyConfigLabel(group, lane), { group, id: lane.id });
}
this.trackPlayOption(
page.addButton({ title: 'Reset keys to defaults' }).on('click', () => {
if (waiting !== undefined) stop();
this.guiState.laneKeys = resetLaneKeys(this.guiState.laneKeys, group);
if (group === 'beat') this.guiState.controlKeys = {};
this.scheduleSettingsSave();
paint();
}),
);
});
paint();
}
|