Controller: subtitle line actions, Button Style, keyboard-only mode pause, Alt+C fixes (#316)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
YashaNoosandClaude Opus 5.5 authored and GitHub committed 2026-11-02 21:41:46 -08:00
1 parent 6d0f37ac21
commit b99409bbcd
60 files changed
+1900 -219

No files matched your search

@@ -0,0 +1,99 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { CORE_DEFAULT_CONFIG } from '../../config/definitions/defaults-core';
import type { ResolvedControllerConfig } from '../../types';
import { findControllerBindingIssues } from './controller-binding-issues.js';
import { CONTROLLER_BINDING_DEFINITIONS } from './controller-config-form.js';
type Bindings = ResolvedControllerConfig['bindings'];
const DEFAULTS = CORE_DEFAULT_CONFIG.controller.bindings as Bindings;
const issuesWith = (changes: Partial<Bindings>) =>
findControllerBindingIssues(CONTROLLER_BINDING_DEFINITIONS, { ...DEFAULTS, ...changes });
test('the default bindings share no input', () => {
assert.deepEqual(issuesWith({}), {});
});
test('two actions on the same button both warn', () => {
// Next Audio is on R1 (button 5) by default.
assert.deepEqual(issuesWith({ nextSubtitle: { kind: 'button', buttonIndex: 5 } }), {
nextSubtitle: 'Same as Next Audio',
nextAudio: 'Same as Next Subtitle',
});
});
test('a button on the D-pad clashes with a D-pad row that is still on', () => {
assert.deepEqual(issuesWith({ nextSubtitle: { kind: 'button', buttonIndex: 15 } }), {
nextSubtitle: 'Same as Token Move (D-pad)',
'leftStickHorizontal:dpad': 'Same as Next Subtitle',
});
// Clearing the D-pad row (as the docs advise) removes the warning.
assert.deepEqual(
issuesWith({
nextSubtitle: { kind: 'button', buttonIndex: 15 },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'none' },
}),
{},
);
});
test('a D-pad reported as a hat axis also counts as the D-pad', () => {
assert.deepEqual(
issuesWith({ previousEntry: { kind: 'axis', axisIndex: 7, direction: 'negative' } }),
{
previousEntry: 'Same as Popup Scroll (D-pad)',
'leftStickVertical:dpad': 'Same as Previous Entry',
},
);
});
test('a stick direction clashes with a stick action on that axis', () => {
const bindings = {
rightStickVertical: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
holdSecondarySub: { kind: 'axis', axisIndex: 3, direction: 'negative' },
} as const;
assert.deepEqual(issuesWith(bindings), {
holdSecondarySub: 'Same as Popup Jump (Stick)',
rightStickVertical: 'Same as Show Secondary Subtitle (Hold)',
});
});
test('opposite directions of one stick are different inputs', () => {
assert.deepEqual(
issuesWith({
previousSubtitle: { kind: 'axis', axisIndex: 2, direction: 'negative' },
nextSubtitle: { kind: 'axis', axisIndex: 2, direction: 'positive' },
}),
{},
);
});
test('two stick actions on the same stick both warn', () => {
assert.deepEqual(
issuesWith({ leftStickVertical: { kind: 'axis', axisIndex: 0, dpadFallback: 'vertical' } }),
{
leftStickHorizontal: 'Same as Popup Scroll (Stick)',
leftStickVertical: 'Same as Token Move (Stick)',
},
);
});
test('Alt Horizontal does nothing, so it never clashes', () => {
// On standard pads the right stick's vertical axis is 3, the default of Alt Horizontal.
assert.deepEqual(
issuesWith({ rightStickVertical: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' } }),
{},
);
});
test('a row shared by several actions lists them all', () => {
assert.equal(
issuesWith({
mineCard: { kind: 'button', buttonIndex: 5 },
nextSubtitle: { kind: 'button', buttonIndex: 5 },
}).nextAudio,
'Same as Mine Card, Next Subtitle',
);
});
@@ -0,0 +1,112 @@
import { DPAD_AXIS_INDEX, DPAD_BUTTON_INDEX } from '../handlers/gamepad-controller.js';
import type {
ControllerDpadFallback,
ResolvedControllerAxisBinding,
ResolvedControllerConfig,
ResolvedControllerDiscreteBinding,
} from '../../types';
type ControllerBindings = ResolvedControllerConfig['bindings'];
type ControllerBindingActionId = keyof ControllerBindings;
/** What the checker needs from an Alt+C row definition. */
export type ControllerBindingIssueDefinition = {
id: ControllerBindingActionId;
label: string;
bindingType: 'discrete' | 'axis';
};
/**
* Rows that do nothing in SubMiner: no action reads `rightStickHorizontal`, so it cannot
* clash with anything.
*/
const INACTIVE_BINDINGS: ReadonlySet<ControllerBindingActionId> = new Set(['rightStickHorizontal']);
/** Row key of a stick binding's D-pad row in Alt+C. */
export function getControllerDpadRowKey(actionId: ControllerBindingActionId): string {
return `${actionId}:dpad`;
}
const buttonKey = (buttonIndex: number) => `button:${buttonIndex}`;
const axisKey = (axisIndex: number, direction: 'negative' | 'positive') =>
`axis:${axisIndex}:${direction}`;
function discreteInputs(binding: ResolvedControllerDiscreteBinding): string[] {
if (binding.kind === 'none') return [];
if ('buttonIndex' in binding) return [buttonKey(binding.buttonIndex)];
return [axisKey(binding.axisIndex, binding.direction)];
}
function stickInputs(binding: ResolvedControllerAxisBinding): string[] {
if (binding.kind === 'none') return [];
return [axisKey(binding.axisIndex, 'negative'), axisKey(binding.axisIndex, 'positive')];
}
/** The D-pad is read from buttons 12-15 and, on some raw pads, from a hat axis. */
function dpadInputs(fallback: ControllerDpadFallback): string[] {
if (fallback === 'horizontal') {
return [
buttonKey(DPAD_BUTTON_INDEX.left),
buttonKey(DPAD_BUTTON_INDEX.right),
axisKey(DPAD_AXIS_INDEX.horizontal, 'negative'),
axisKey(DPAD_AXIS_INDEX.horizontal, 'positive'),
];
}
if (fallback === 'vertical') {
return [
buttonKey(DPAD_BUTTON_INDEX.up),
buttonKey(DPAD_BUTTON_INDEX.down),
axisKey(DPAD_AXIS_INDEX.vertical, 'negative'),
axisKey(DPAD_AXIS_INDEX.vertical, 'positive'),
];
}
return [];
}
/**
* Warnings for the main Alt+C list, keyed by row (the action id, or its D-pad row key):
* a row warns when another row uses one of its inputs. Every such pair fires together at
* some point, since the situations nest (always, then keyboard-only mode, then popup open),
* so a shared input is always worth pointing out. Dialog layouts are checked separately:
* they pause these actions while open.
*/
export function findControllerBindingIssues(
definitions: readonly ControllerBindingIssueDefinition[],
bindings: ControllerBindings,
): Partial<Record<string, string>> {
const rows: { key: string; label: string; inputs: string[] }[] = [];
for (const definition of definitions) {
if (INACTIVE_BINDINGS.has(definition.id)) continue;
const binding = bindings[definition.id];
if (!binding) continue;
if (definition.bindingType === 'axis') {
const axisBinding = binding as ResolvedControllerAxisBinding;
rows.push({
key: definition.id,
label: `${definition.label} (Stick)`,
inputs: stickInputs(axisBinding),
});
rows.push({
key: getControllerDpadRowKey(definition.id),
label: `${definition.label} (D-pad)`,
inputs: dpadInputs(axisBinding.kind === 'none' ? 'none' : axisBinding.dpadFallback),
});
} else {
rows.push({
key: definition.id,
label: definition.label,
inputs: discreteInputs(binding as ResolvedControllerDiscreteBinding),
});
}
}
const issues: Partial<Record<string, string>> = {};
for (const row of rows) {
const inputs = new Set(row.inputs);
const clashes = rows
.filter((other) => other !== row && other.inputs.some((input) => inputs.has(input)))
.map((other) => other.label);
if (clashes.length > 0) issues[row.key] = `Same as ${clashes.join(', ')}`;
}
return issues;
}
@@ -5,7 +5,7 @@ import { CORE_DEFAULT_CONFIG } from '../../config/definitions/defaults-core';
import { getDefaultControllerFieldGroupingBindings } from '../../shared/controller-field-grouping';
import { getDefaultControllerReviewBindings } from '../../shared/controller-review';
import { CONTROLLER_SCREENS } from '../../shared/controller-screens';
import type { ResolvedControllerConfig } from '../../types';
import type { ControllerFaceButtonLabels, ResolvedControllerConfig } from '../../types';
import type { ControllerScreenLearning } from './controller-config-form';
import { createControllerConfigForm } from './controller-config-form.js';
@@ -24,6 +24,10 @@ const BINDINGS: ResolvedControllerConfig['bindings'] = {
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
previousSubtitle: { kind: 'none' },
nextSubtitle: { kind: 'none' },
replaySubtitle: { kind: 'none' },
playNextSubtitle: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -45,6 +49,12 @@ class FakeElement {
this.className = [...new Set([...this.classes(), ...tokens])].join(' ');
},
contains: (token: string) => this.classes().includes(token),
toggle: (token: string, force?: boolean) => {
const on = force ?? !this.classes().includes(token);
const rest = this.classes().filter((existing) => existing !== token);
this.className = (on ? [...rest, token] : rest).join(' ');
return on;
},
};
set innerHTML(value: string) {
@@ -99,7 +109,7 @@ function renderForm(
options: {
bindings?: typeof BINDINGS;
mapping?: string;
faceButtonLabels?: 'xbox' | 'nintendo';
faceButtonLabels?: ControllerFaceButtonLabels;
} = {},
) {
const previousDocument = Object.getOwnPropertyDescriptor(globalThis, 'document');
@@ -148,7 +158,7 @@ test('controller config form renders grouped rows with friendly binding labels',
assert.deepEqual(
findAll(container, 'controller-config-group').map((el) => el.textContent),
['Lookup', 'Playback', 'Popup Audio', 'Navigation'],
['Lookup', 'Playback', 'Subtitles', 'Popup Audio', 'Navigation'],
);
assert.deepEqual(
findAll(container, 'controller-config-row').map((row) => [
@@ -165,6 +175,10 @@ test('controller config form renders grouped rows with friendly binding labels',
['Toggle MPV Pause', 'R3 / RS'],
['Show Secondary Subtitle (Hold)', 'None'],
['Quit MPV', 'Back / Select'],
['Previous Subtitle', 'None'],
['Next Subtitle', 'None'],
['Replay Subtitle', 'None'],
['Play Next Subtitle', 'None'],
['Previous Audio', 'None'],
['Next Audio', 'RB / R1'],
['Play Current Audio', 'LB / L1'],
@@ -180,7 +194,7 @@ test('controller config form renders grouped rows with friendly binding labels',
);
assert.deepEqual(
findAll(container, 'disabled').map((badge) => badge.textContent),
['None', 'None', 'None', 'None', 'None', 'None'],
['None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None', 'None'],
);
assert.deepEqual(findAll(container, 'expanded'), []);
assert.deepEqual(findAll(container, 'controller-config-edit-panel'), []);
@@ -297,6 +311,7 @@ type ReviewFormOptions = {
reviewBindings?: ResolvedControllerConfig['reviewBindings'];
fieldGroupingBindings?: ResolvedControllerConfig['fieldGroupingBindings'];
learning?: ControllerScreenLearning | null;
faceButtonLabels?: ControllerFaceButtonLabels;
};
/** Renders the form with the dialog groups; `act` runs while `document` is still stubbed. */
@@ -340,7 +355,7 @@ function withReviewForm(
},
getScreenInputContext: () => ({
mapping: 'standard',
faceButtonLabels: 'xbox',
faceButtonLabels: options.faceButtonLabels ?? 'xbox',
buttonIndices: CORE_DEFAULT_CONFIG.controller.buttonIndices,
}),
getScreenLearning: () => options.learning ?? null,
@@ -430,6 +445,43 @@ test('controller config form warns about review bindings that clash', () => {
});
});
test('controller config form warns when main actions share an input', () => {
const { container, row } = renderForm(null, {
bindings: { ...BINDINGS, nextSubtitle: { kind: 'button', buttonIndex: 15 } },
mapping: 'standard',
});
assert.deepEqual(
findAll(container, 'controller-config-warning').map((el) => el.textContent),
['\u26a0 Same as Token Move (D-pad)', '\u26a0 Same as Next Subtitle'],
);
assert.equal(row('Next Subtitle').classList.contains('has-warning'), true);
assert.equal(row('Token Move (D-pad)').classList.contains('has-warning'), true);
assert.equal(row('Next Audio').classList.contains('has-warning'), false);
});
test('an edit panel slides in when it opens, not each time the list is redrawn', () => {
withReviewForm({}, ({ container, reviewRow }) => {
const panel = () => findOne(container, 'controller-config-edit-panel');
const kept = () => panel().classList.contains('controller-config-edit-panel-kept');
reviewRow('Toggle Lookup').click();
assert.equal(kept(), false, 'opening animates');
// Any other click redraws the whole list; the open panel is recreated as it was.
findOne(container, 'controller-config-group-toggle').click();
assert.equal(kept(), true);
reviewRow('Toggle Lookup').click();
assert.deepEqual(findAll(container, 'controller-config-edit-panel'), []);
reviewRow('Toggle Lookup').click();
assert.equal(kept(), false, 'reopening animates again');
reviewRow('Close Lookup').click();
assert.equal(kept(), false, 'another row opening animates');
});
});
/** The dialog group headers, in order. */
const groupHeaders = (container: FakeElement) =>
findAll(container, 'controller-config-group-toggle');
@@ -483,3 +535,30 @@ test('learning a duplicate card action opens only that group', () => {
},
);
});
test('controller config form names face buttons with PlayStation symbols when configured', () => {
const badge = (row: FakeElement) => findOne(row, 'controller-config-badge').textContent;
const bindings: typeof BINDINGS = {
...BINDINGS,
toggleLookup: { kind: 'button', buttonIndex: 0 },
closeLookup: { kind: 'button', buttonIndex: 1 },
mineCard: { kind: 'button', buttonIndex: 2 },
toggleKeyboardOnlyMode: { kind: 'button', buttonIndex: 3 },
};
const form = renderForm(null, { bindings, mapping: 'standard', faceButtonLabels: 'playstation' });
assert.equal(badge(form.row('Toggle Lookup')), '╳');
assert.equal(badge(form.row('Close Lookup')), '○');
assert.equal(badge(form.row('Mine Card')), '□');
assert.equal(badge(form.row('Toggle Keyboard-Only Mode')), '△');
assert.equal(badge(form.row('Next Audio')), 'RB / R1');
// The dialog groups print the same symbols.
withReviewForm({ faceButtonLabels: 'playstation' }, ({ container, reviewRow }) => {
findOne(container, 'controller-config-group-toggle').click();
assert.equal(badgeOf(reviewRow('Use this timing')), '╳');
assert.equal(badgeOf(reviewRow('Keep editing')), '○');
assert.equal(badgeOf(reviewRow('Play selection')), '□');
assert.equal(badgeOf(reviewRow('Reset timing')), '△');
});
});
+86 -14
View File
@@ -1,5 +1,6 @@
import {
findControllerScreenBindingIssues,
getControllerFaceButtonName,
formatControllerReviewBinding,
type ControllerReviewInputContext,
type ControllerScreenActionDefinition,
@@ -7,6 +8,10 @@ import {
type ControllerScreenDefinition,
} from '../../shared/controller-review';
import type { ControllerScreenId } from '../../shared/controller-screens';
import {
findControllerBindingIssues,
getControllerDpadRowKey,
} from './controller-binding-issues.js';
import type { ResolvedControllerReviewBinding } from '../../types';
/** A dialog layout shown as a collapsible group in Alt+C. */
@@ -102,6 +107,34 @@ export const CONTROLLER_BINDING_DEFINITIONS: ControllerBindingDefinition[] = [
bindingType: 'discrete',
defaultBinding: { kind: 'button', buttonIndex: 6 },
},
{
id: 'previousSubtitle',
label: 'Previous Subtitle',
group: 'Subtitles',
bindingType: 'discrete',
defaultBinding: { kind: 'none' },
},
{
id: 'nextSubtitle',
label: 'Next Subtitle',
group: 'Subtitles',
bindingType: 'discrete',
defaultBinding: { kind: 'none' },
},
{
id: 'replaySubtitle',
label: 'Replay Subtitle',
group: 'Subtitles',
bindingType: 'discrete',
defaultBinding: { kind: 'none' },
},
{
id: 'playNextSubtitle',
label: 'Play Next Subtitle',
group: 'Subtitles',
bindingType: 'discrete',
defaultBinding: { kind: 'none' },
},
{
id: 'previousAudio',
label: 'Previous Audio',
@@ -240,9 +273,6 @@ const W3C_STANDARD_AXIS_NAMES: Record<number, string> = {
type ControllerMapping = string | null | undefined;
type FaceButtonLabels = ResolvedControllerConfig['faceButtonLabels'] | undefined;
/** Nintendo-style pads print B at the bottom and A on the right; positions 0-3 are the same. */
const NINTENDO_FACE_BUTTON_NAMES: Record<number, string> = { 0: 'B', 1: 'A', 2: 'Y', 3: 'X' };
const DPAD_FALLBACK_LABELS: Record<ControllerDpadFallback, string> = {
none: 'None',
horizontal: 'D-pad \u2194',
@@ -254,9 +284,13 @@ export function getFriendlyButtonName(
mapping?: ControllerMapping,
faceButtonLabels?: FaceButtonLabels,
): string {
const nintendoName =
faceButtonLabels === 'nintendo' ? NINTENDO_FACE_BUTTON_NAMES[buttonIndex] : undefined;
if (nintendoName) return nintendoName;
// The default Xbox style keeps the shared "A / Cross" names; the others print their own.
// Face positions 0-3 are the same on raw and standard layouts.
const styledName =
faceButtonLabels && faceButtonLabels !== 'xbox'
? getControllerFaceButtonName(buttonIndex, faceButtonLabels)
: null;
if (styledName) return styledName;
const names = mapping === 'standard' ? W3C_STANDARD_BUTTON_NAMES : RAW_LAYOUT_BUTTON_NAMES;
return names[buttonIndex] ?? `Button ${buttonIndex}`;
}
@@ -326,6 +360,8 @@ export function createControllerConfigForm(options: {
getControllerMapping?: () => ControllerMapping;
/** `controller.faceButtonLabels`; names the face buttons as printed on the pad. */
getFaceButtonLabels?: () => FaceButtonLabels;
/** True until the window sees a controller: rows are a preview and actions are dimmed. */
isEditingLocked?: () => boolean;
/** Dialog layouts (review, field grouping) of the controller being configured. */
getScreenGroups?: () => ControllerScreenGroup[];
getScreenInputContext?: () => ControllerReviewInputContext;
@@ -336,14 +372,35 @@ export function createControllerConfigForm(options: {
onScreenResetAll?: (screenId: ControllerScreenId) => void;
}) {
let expandedRowKey: ExpandedRowKey | null = null;
/**
* Row whose edit panel the previous render showed. Every change rebuilds the list, so a
* panel that stays open is recreated; it only slides in when it was not open before.
*/
let previousPanelRowKey: ExpandedRowKey | null = null;
let panelRowKey: ExpandedRowKey | null = null;
/** Dialog groups start collapsed; this holds the opened ones. */
const expandedScreens = new Set<ControllerScreenId>();
function render(): void {
panelRowKey = null;
try {
renderRows();
} finally {
previousPanelRowKey = panelRowKey;
}
}
function renderRows(): void {
options.container.innerHTML = '';
options.container.classList?.toggle(
'controller-config-locked',
options.isEditingLocked?.() ?? false,
);
let lastGroup = '';
const learningActionId = options.getLearningActionId();
const dpadLearningActionId = options.getDpadLearningActionId();
const bindings = options.getBindings();
const issues = findControllerBindingIssues(CONTROLLER_BINDING_DEFINITIONS, bindings);
// Auto-expand when learning starts
if (learningActionId) {
@@ -361,17 +418,23 @@ export function createControllerConfigForm(options: {
lastGroup = definition.group;
}
const binding = options.getBindings()[definition.id];
const binding = bindings[definition.id];
if (definition.bindingType === 'axis') {
renderAxisStickRow(definition, binding as ResolvedControllerAxisBinding, learningActionId);
renderAxisStickRow(
definition,
binding as ResolvedControllerAxisBinding,
learningActionId,
issues[definition.id],
);
renderAxisDpadRow(
definition,
binding as ResolvedControllerAxisBinding,
dpadLearningActionId,
issues[getControllerDpadRowKey(definition.id)],
);
} else {
renderDiscreteRow(definition, binding, learningActionId);
renderDiscreteRow(definition, binding, learningActionId, issues[definition.id]);
}
}
@@ -485,7 +548,7 @@ export function createControllerConfigForm(options: {
: holdLabel
? `Holding ${holdLabel}: press another button for a combo, or let go to use ${holdLabel} alone\u2026`
: 'Press a button, or hold one and press another for a combo\u2026';
const panel = createEditPanel(hint, isLearning, {
const panel = createEditPanel(rowKey, hint, isLearning, {
onLearn: learn,
onClear: (e) => {
e.stopPropagation();
@@ -501,6 +564,7 @@ export function createControllerConfigForm(options: {
definition: ControllerBindingDefinition,
binding: ResolvedControllerConfig['bindings'][ControllerBindingActionId],
learningActionId: ControllerBindingActionId | null,
issue: string | undefined,
): void {
const rowKey = definition.id as string;
const isExpanded = expandedRowKey === rowKey;
@@ -526,6 +590,7 @@ export function createControllerConfigForm(options: {
e.stopPropagation();
options.onReset(definition.id);
},
issue,
);
row.addEventListener('click', () => {
expandedRowKey = expandedRowKey === rowKey ? null : rowKey;
@@ -537,7 +602,7 @@ export function createControllerConfigForm(options: {
const hint = isLearning
? 'Press a button, trigger, or move a stick\u2026'
: `Currently: ${formatControllerBindingSummary(binding)}`;
const panel = createEditPanel(hint, isLearning, {
const panel = createEditPanel(rowKey, hint, isLearning, {
onLearn: (e) => {
e.stopPropagation();
options.onLearn(definition.id, definition.bindingType);
@@ -559,6 +624,7 @@ export function createControllerConfigForm(options: {
definition: ControllerBindingDefinition,
binding: ResolvedControllerAxisBinding,
learningActionId: ControllerBindingActionId | null,
issue: string | undefined,
): void {
const rowKey = definition.id as string;
const isExpanded = expandedRowKey === rowKey;
@@ -580,6 +646,7 @@ export function createControllerConfigForm(options: {
e.stopPropagation();
options.onReset(definition.id);
},
issue,
);
row.addEventListener('click', () => {
expandedRowKey = expandedRowKey === rowKey ? null : rowKey;
@@ -590,7 +657,7 @@ export function createControllerConfigForm(options: {
if (isExpanded) {
const summary = binding.kind === 'none' ? 'Disabled' : `Axis ${binding.axisIndex}`;
const hint = isLearning ? 'Move a stick or trigger\u2026' : `Currently: ${summary}`;
const panel = createEditPanel(hint, isLearning, {
const panel = createEditPanel(rowKey, hint, isLearning, {
onLearn: (e) => {
e.stopPropagation();
options.onLearn(definition.id, 'axis');
@@ -612,8 +679,9 @@ export function createControllerConfigForm(options: {
definition: ControllerBindingDefinition,
binding: ResolvedControllerAxisBinding,
dpadLearningActionId: ControllerBindingActionId | null,
issue: string | undefined,
): void {
const rowKey = `${definition.id as string}:dpad`;
const rowKey = getControllerDpadRowKey(definition.id);
const isExpanded = expandedRowKey === rowKey;
const isLearning = dpadLearningActionId === definition.id;
@@ -636,6 +704,7 @@ export function createControllerConfigForm(options: {
e.stopPropagation();
options.onDpadReset(definition.id);
},
issue,
);
row.addEventListener('click', () => {
expandedRowKey = expandedRowKey === rowKey ? null : rowKey;
@@ -647,7 +716,7 @@ export function createControllerConfigForm(options: {
const hint = isLearning
? 'Press a D-pad direction\u2026'
: `Currently: ${DPAD_FALLBACK_LABELS[dpadFallback]}`;
const panel = createEditPanel(hint, isLearning, {
const panel = createEditPanel(rowKey, hint, isLearning, {
onLearn: (e) => {
e.stopPropagation();
options.onDpadLearn(definition.id);
@@ -729,6 +798,7 @@ export function createControllerConfigForm(options: {
}
function createEditPanel(
rowKey: ExpandedRowKey,
hintText: string,
isLearning: boolean,
callbacks: {
@@ -739,6 +809,8 @@ export function createControllerConfigForm(options: {
): HTMLDivElement {
const panel = document.createElement('div');
panel.className = 'controller-config-edit-panel';
if (rowKey === previousPanelRowKey) panel.classList.add('controller-config-edit-panel-kept');
panelRowKey = rowKey;
const inner = document.createElement('div');
inner.className = 'controller-config-edit-inner';
@@ -77,6 +77,10 @@ test('controller debug modal renders active controller axes, buttons, and config
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
previousSubtitle: { kind: 'none' },
nextSubtitle: { kind: 'none' },
replaySubtitle: { kind: 'none' },
playNextSubtitle: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -198,6 +202,10 @@ test('controller debug modal copies buttonIndices config to clipboard', async ()
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
previousSubtitle: { kind: 'none' },
nextSubtitle: { kind: 'none' },
replaySubtitle: { kind: 'none' },
playNextSubtitle: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -318,6 +326,10 @@ test('controller debug modal stays closed and notifies when controller support i
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
previousSubtitle: { kind: 'none' },
nextSubtitle: { kind: 'none' },
replaySubtitle: { kind: 'none' },
playNextSubtitle: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
+236 -41
View File
@@ -2,7 +2,11 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import { createRendererState } from '../state.js';
import { createControllerSelectModal } from './controller-select.js';
import {
createControllerSelectModal,
findMostRecentlyUsedIndex,
shortControllerName,
} from './controller-select.js';
import { getDefaultControllerReviewBindings } from '../../shared/controller-review';
import { getDefaultControllerFieldGroupingBindings } from '../../shared/controller-field-grouping';
@@ -159,6 +163,10 @@ function buildContext() {
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
previousSubtitle: { kind: 'none' },
nextSubtitle: { kind: 'none' },
replaySubtitle: { kind: 'none' },
playNextSubtitle: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -179,6 +187,7 @@ function buildContext() {
controllerSelectModal: { classList: createClassList(['hidden']), setAttribute: () => {} },
controllerSelectClose: createFakeElement(),
controllerSelectPicker: createFakeElement(),
controllerSelectFaceButtonStyle: createFakeElement(),
controllerSelectSummary: createFakeElement(),
controllerConfigList: createFakeElement(),
controllerSelectStatus: { textContent: '', classList: createClassList() },
@@ -328,6 +337,7 @@ test('controller select modal reset control stores the default binding in the se
state.controllerConfig.profiles = {
'pad-1': {
label: 'pad-1',
faceButtonLabels: state.controllerConfig.faceButtonLabels,
buttonIndices: state.controllerConfig.buttonIndices,
bindings: {
...state.controllerConfig.bindings,
@@ -429,7 +439,7 @@ test('controller select modal binding badge starts learn mode and persists bindi
}
});
test('controller select modal learn mode falls back to global bindings without a controller', async () => {
test('controller select modal locks editing until this window sees a controller', async () => {
const domHandle = installFakeDom();
const saved: unknown[] = [];
@@ -448,44 +458,45 @@ test('controller select modal learn mode falls back to global bindings without a
try {
const { state, dom } = buildContext();
// A freshly opened window sees no pad until a button is pressed in it.
state.connectedGamepads = [];
// No controller visible and none saved: edits go to the global bindings.
state.controllerConfig!.preferredGamepadId = '';
state.activeGamepadId = null;
const modal = createControllerSelectModal({ state, dom } as never, {
modalStateReader: { isAnyModalOpen: () => false },
syncSettingsModalSubtitleSuppression: () => {},
notifyControllerDisabled: () => {},
});
modal.wireDomEvents();
modal.openControllerSelectModal();
const firstRow = dom.controllerConfigList.children[1];
firstRow.dispatch('click');
const editPanel = dom.controllerConfigList.children[2];
const learnButton = editPanel.children[0].children[1].children[0];
learnButton.dispatch('click');
const press = 'Press any button on your controller to edit its bindings.';
assert.equal(dom.controllerSelectStatus.textContent, press);
assert.equal(dom.controllerConfigList.classList.contains('controller-config-locked'), true);
assert.equal(dom.controllerSelectFaceButtonStyle.disabled, true);
state.controllerRawButtons = Array.from({ length: 12 }, () => ({
value: 0,
pressed: false,
touched: false,
// Learn and Clear do nothing yet.
const row = findReviewRow(dom.controllerConfigList, 'Toggle Lookup');
row.children[1].children[0].dispatch('click');
state.controllerRawButtons = Array.from({ length: 12 }, (_, index) => ({
value: index === 11 ? 1 : 0,
pressed: index === 11,
touched: index === 11,
}));
state.controllerRawButtons[11] = { value: 1, pressed: true, touched: true };
modal.updateDevices();
await Promise.resolve();
assert.deepEqual(saved, []);
assert.equal(dom.controllerSelectStatus.textContent, press, 'kept across frames');
assert.deepEqual(saved.at(-1), {
bindings: {
toggleLookup: { kind: 'button', buttonIndex: 11 },
},
});
assert.deepEqual(state.controllerConfig?.bindings.toggleLookup, {
kind: 'button',
buttonIndex: 11,
});
// The press makes the pad visible: editing unlocks for it.
state.connectedGamepads = [{ id: 'pad-1', index: 0, mapping: 'standard', connected: true }];
state.activeGamepadId = 'pad-1';
modal.updateDevices();
assert.equal(dom.controllerConfigList.classList.contains('controller-config-locked'), false);
assert.equal(dom.controllerSelectFaceButtonStyle.disabled, false);
assert.equal(
dom.controllerSelectStatus.textContent,
'Choose a controller or click Learn to remap an action.',
);
} finally {
domHandle.restore();
}
@@ -799,7 +810,7 @@ test('controller select modal learns a duplicate card button and saves it to the
}
});
test('controller select modal shows and edits the saved controller before this window sees it', async () => {
test('controller select modal previews the saved controller, then edits it once seen', async () => {
const domHandle = installFakeDom();
const saved: unknown[] = [];
@@ -825,6 +836,7 @@ test('controller select modal shows and edits the saved controller before this w
config.profiles = {
'pad-1': {
label: 'pad-1',
faceButtonLabels: config.faceButtonLabels,
buttonIndices: config.buttonIndices,
bindings: config.bindings,
fieldGroupingBindings: config.fieldGroupingBindings,
@@ -846,18 +858,31 @@ test('controller select modal shows and edits the saved controller before this w
modal.wireDomEvents();
modal.openControllerSelectModal();
// The preview is the saved controller's layout, labelled as such.
assert.equal(dom.controllerSelectSummary.textContent, 'Showing pad-1 (saved).');
findByClass(dom.controllerConfigList, 'controller-config-group-toggle').dispatch('click');
const row = findReviewRow(dom.controllerConfigList, 'Start \u22120.1s');
assert.equal(row.children[1].children[0].textContent, 'L2 + D-pad \u25b6');
const waiting = 'Press any button on your controller so this window can see it.';
assert.equal(dom.controllerSelectStatus.textContent, waiting);
const startBack = () => findReviewRow(dom.controllerConfigList, 'Start \u22120.1s');
assert.equal(startBack().children[1].children[0].textContent, 'L2 + D-pad \u25b6');
const press = 'Press any button on your controller to edit its bindings.';
assert.equal(dom.controllerSelectStatus.textContent, press);
// Controller polling keeps publishing snapshots while no pad is visible.
modal.updateDevices();
modal.updateDevices();
assert.equal(dom.controllerSelectStatus.textContent, waiting, 'the prompt stays');
assert.equal(dom.controllerSelectStatus.textContent, press, 'the prompt stays');
// Reset goes to the saved controller's profile, not the global layout.
row.children[1].children[1].dispatch('click');
// Reset is locked until the window sees the pad.
startBack().children[1].children[1].dispatch('click');
await Promise.resolve();
assert.deepEqual(saved, []);
state.connectedGamepads = [{ id: 'pad-1', index: 0, mapping: 'standard', connected: true }];
state.activeGamepadId = 'pad-1';
modal.updateDevices();
assert.equal(
dom.controllerSelectStatus.textContent,
'Choose a controller or click Learn to remap an action.',
);
startBack().children[1].children[1].dispatch('click');
await Promise.resolve();
assert.deepEqual(saved.at(-1), {
profiles: {
@@ -873,19 +898,19 @@ test('controller select modal shows and edits the saved controller before this w
},
});
await new Promise((resolve) => setTimeout(resolve, 0));
const afterReset = dom.controllerSelectStatus.textContent;
assert.equal(afterReset, 'Start \u22120.1s reset.');
assert.equal(dom.controllerSelectStatus.textContent, 'Start \u22120.1s reset.');
modal.updateDevices();
assert.equal(dom.controllerSelectStatus.textContent, afterReset, 'the reset message stays');
assert.equal(
dom.controllerSelectStatus.textContent,
'Start \u22120.1s reset.',
'the reset message stays',
);
// The pad appears, then disappears again.
state.connectedGamepads = [{ id: 'pad-1', index: 0, mapping: 'standard', connected: true }];
state.activeGamepadId = 'pad-1';
modal.updateDevices();
// The pad disappears, then comes back.
state.connectedGamepads = [];
state.activeGamepadId = null;
modal.updateDevices();
assert.equal(dom.controllerSelectStatus.textContent, waiting);
assert.equal(dom.controllerSelectStatus.textContent, press);
state.connectedGamepads = [{ id: 'pad-1', index: 0, mapping: 'standard', connected: true }];
state.activeGamepadId = 'pad-1';
modal.updateDevices();
@@ -897,3 +922,173 @@ test('controller select modal shows and edits the saved controller before this w
domHandle.restore();
}
});
test('controller select modal previews the controller the overlay reports in use', async () => {
const domHandle = installFakeDom();
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
focus: () => {},
electronAPI: {
saveControllerConfig: async () => {},
notifyOverlayModalClosed: () => {},
getActiveControllerId: async () => 'DualSense Wireless Controller (STANDARD GAMEPAD)',
},
},
});
try {
const { state, dom } = buildContext();
state.connectedGamepads = [];
state.activeGamepadId = null;
const config = state.controllerConfig!;
// The saved controller is pad-1, but the overlay is reading the DualSense.
config.profiles = {
'DualSense Wireless Controller (STANDARD GAMEPAD)': {
label: 'DualSense',
faceButtonLabels: 'playstation',
buttonIndices: config.buttonIndices,
bindings: { ...config.bindings, mineCard: { kind: 'button', buttonIndex: 3 } },
reviewBindings: config.reviewBindings,
fieldGroupingBindings: config.fieldGroupingBindings,
},
};
const modal = createControllerSelectModal({ state, dom } as never, {
modalStateReader: { isAnyModalOpen: () => false },
syncSettingsModalSubtitleSuppression: () => {},
notifyControllerDisabled: () => {},
});
modal.wireDomEvents();
modal.openControllerSelectModal();
await new Promise((resolve) => setTimeout(resolve, 0));
assert.equal(
dom.controllerSelectSummary.textContent,
'Showing DualSense Wireless Controller (in use).',
);
assert.equal(
findReviewRow(dom.controllerConfigList, 'Mine Card').children[1].children[0].textContent,
'\u25b3',
);
assert.equal(dom.controllerSelectFaceButtonStyle.value, 'playstation');
} finally {
domHandle.restore();
}
});
test('controller select modal selects the controller you pressed when several appear', () => {
const domHandle = installFakeDom();
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
focus: () => {},
electronAPI: { saveControllerConfig: async () => {}, notifyOverlayModalClosed: () => {} },
},
});
try {
const { state, dom } = buildContext();
state.connectedGamepads = [];
state.activeGamepadId = null;
const modal = createControllerSelectModal({ state, dom } as never, {
modalStateReader: { isAnyModalOpen: () => false },
syncSettingsModalSubtitleSuppression: () => {},
notifyControllerDisabled: () => {},
});
modal.wireDomEvents();
modal.openControllerSelectModal();
// One press reveals every pad; pad-2 has the newest input, pad-1 is the saved one.
state.connectedGamepads = [
{ id: 'pad-1', index: 0, mapping: 'standard', connected: true, timestamp: 100 },
{ id: 'pad-2', index: 1, mapping: 'standard', connected: true, timestamp: 500 },
];
state.activeGamepadId = 'pad-1';
modal.updateDevices();
assert.equal(state.controllerDeviceSelectedIndex, 1);
assert.equal(state.controllerSelectGamepadId, 'pad-2', 'this window then reads pad-2');
modal.closeControllerSelectModal();
assert.equal(state.controllerSelectGamepadId, null);
} finally {
domHandle.restore();
}
});
test('controller select modal shows and saves the button style per controller', async () => {
const domHandle = installFakeDom();
const saved: unknown[] = [];
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
focus: () => {},
electronAPI: {
saveControllerConfig: async (update: unknown) => {
saved.push(update);
},
notifyOverlayModalClosed: () => {},
},
},
});
try {
const { state, dom } = buildContext();
const modal = createControllerSelectModal({ state, dom } as never, {
modalStateReader: { isAnyModalOpen: () => false },
syncSettingsModalSubtitleSuppression: () => {},
notifyControllerDisabled: () => {},
});
modal.wireDomEvents();
modal.openControllerSelectModal();
const stylePicker = dom.controllerSelectFaceButtonStyle;
assert.deepEqual(
stylePicker.children.map((option: any) => option.value),
['xbox', 'nintendo', 'playstation'],
);
assert.equal(stylePicker.value, 'xbox');
stylePicker.value = 'playstation';
stylePicker.dispatch('change');
await new Promise((resolve) => setTimeout(resolve, 0));
assert.deepEqual(saved.at(-1), {
profiles: { 'pad-1': { faceButtonLabels: 'playstation' } },
});
assert.equal(state.controllerConfig?.profiles['pad-1']?.faceButtonLabels, 'playstation');
assert.equal(state.controllerConfig?.faceButtonLabels, 'xbox', 'other controllers keep theirs');
assert.equal(dom.controllerSelectStatus.textContent, 'Button style: PlayStation.');
// Toggle Lookup is on the bottom face button.
assert.equal(
findReviewRow(dom.controllerConfigList, 'Toggle Lookup').children[1].children[0].textContent,
'╳',
);
// The other controller still uses the default style.
dom.controllerSelectPicker.value = 'pad-2:1';
dom.controllerSelectPicker.dispatch('change');
assert.equal(stylePicker.value, 'xbox');
} finally {
domHandle.restore();
}
});
test('shortControllerName drops the driver details in brackets', () => {
assert.equal(
shortControllerName('Xbox Wireless Controller (STANDARD GAMEPAD Vendor: 045e Product: 0b13)'),
'Xbox Wireless Controller',
);
assert.equal(shortControllerName('pad-1'), 'pad-1');
assert.equal(shortControllerName('(only details)'), '(only details)');
});
test('findMostRecentlyUsedIndex picks the newest input and ignores missing timestamps', () => {
assert.equal(findMostRecentlyUsedIndex([{ timestamp: 100 }, { timestamp: 500 }, {}]), 1);
assert.equal(findMostRecentlyUsedIndex([{ timestamp: 900 }, { timestamp: Number.NaN }]), 0);
assert.equal(findMostRecentlyUsedIndex([{}, { timestamp: 0 }]), -1);
assert.equal(findMostRecentlyUsedIndex([]), -1);
});
+160 -26
View File
@@ -3,6 +3,7 @@ import { resolveControllerConfigForGamepad } from '../controller-profile-config.
import { createControllerBindingCapture } from '../handlers/controller-binding-capture.js';
import { createControllerComboCapture } from '../handlers/controller-combo-capture.js';
import {
CONTROLLER_FACE_BUTTON_STYLES,
formatControllerReviewInput,
getControllerScreenAction,
getDefaultControllerScreenBindings,
@@ -22,6 +23,27 @@ import {
getDefaultDpadFallback,
} from './controller-config-form.js';
const PRESS_TO_EDIT_STATUS = 'Press any button on your controller to edit its bindings.';
/** `8BitDo Ultimate Wireless Controller (STANDARD GAMEPAD Vendor: …)` → the name part. */
export function shortControllerName(gamepadId: string): string {
return gamepadId.replace(/\s*\(.*\)\s*$/, '').trim() || gamepadId;
}
/** Index of the pad with the newest input (Gamepad API timestamp), or -1 if none report one. */
export function findMostRecentlyUsedIndex(devices: ReadonlyArray<{ timestamp?: number }>): number {
let best = -1;
let bestTimestamp = 0;
devices.forEach((device, index) => {
const timestamp = device.timestamp ?? 0;
if (Number.isFinite(timestamp) && timestamp > bestTimestamp) {
best = index;
bestTimestamp = timestamp;
}
});
return best;
}
function clampSelectedIndex(ctx: RendererContext): void {
if (ctx.state.connectedGamepads.length === 0) {
ctx.state.controllerDeviceSelectedIndex = 0;
@@ -34,11 +56,6 @@ function clampSelectedIndex(ctx: RendererContext): void {
);
}
const NO_CONTROLLER_STATUS = 'No controllers detected.';
const WAITING_FOR_SAVED_CONTROLLER_STATUS =
'Press any button on your controller so this window can see it.';
const NO_CONTROLLER_STATUSES = new Set([NO_CONTROLLER_STATUS, WAITING_FOR_SAVED_CONTROLLER_STATUS]);
export function createControllerSelectModal(
ctx: RendererContext,
options: {
@@ -65,17 +82,52 @@ export function createControllerSelectModal(
holdLabel: string | null;
} | null = null;
/** The controller the overlay reports in use, fetched when the window opens. */
let overlayActiveControllerId: string | null = null;
async function loadOverlayActiveController(): Promise<void> {
let gamepadId: string | null = null;
try {
gamepadId = (await window.electronAPI.getActiveControllerId?.()) ?? null;
} catch {
return;
}
if (!ctx.state.controllerSelectModalOpen || gamepadId === overlayActiveControllerId) return;
overlayActiveControllerId = gamepadId;
renderPicker();
controllerConfigForm.render();
}
function getSelectedController() {
return ctx.state.connectedGamepads[ctx.state.controllerDeviceSelectedIndex] ?? null;
}
/**
* The controller whose profile Alt+C shows and edits. A freshly opened window cannot see
* any pad until a button is pressed in it, so until then use the saved (preferred)
* controller rather than the global defaults it does not actually use.
* any pad until a button is pressed in it; until then it previews the controller the
* overlay reports in use, else the saved (preferred) one, and editing stays locked.
*/
function getSelectedControllerId(): string | null {
return getSelectedController()?.id ?? (ctx.state.controllerConfig?.preferredGamepadId || null);
return (
getSelectedController()?.id ??
overlayActiveControllerId ??
(ctx.state.controllerConfig?.preferredGamepadId || null)
);
}
/**
* Nothing can be changed until this window sees a controller: before that it could only
* guess which controller you mean, and a change could land on the wrong one.
*/
function isEditingLocked(): boolean {
return ctx.state.connectedGamepads.length === 0;
}
/** True when editing may go ahead; otherwise says why not. */
function canEdit(): boolean {
if (!isEditingLocked()) return true;
setStatus(PRESS_TO_EDIT_STATUS);
return false;
}
function getSelectedControllerConfig() {
@@ -115,8 +167,9 @@ export function createControllerSelectModal(
const controllerConfigForm = createControllerConfigForm({
container: ctx.dom.controllerConfigList,
isEditingLocked,
getControllerMapping: () => getSelectedController()?.mapping ?? null,
getFaceButtonLabels: () => ctx.state.controllerConfig?.faceButtonLabels,
getFaceButtonLabels: () => getSelectedControllerConfig()?.faceButtonLabels,
getBindings: () =>
getSelectedControllerConfig()?.bindings ?? {
toggleLookup: { kind: 'button', buttonIndex: 0 },
@@ -131,6 +184,10 @@ export function createControllerSelectModal(
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
previousSubtitle: { kind: 'none' },
nextSubtitle: { kind: 'none' },
replaySubtitle: { kind: 'none' },
playNextSubtitle: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -154,6 +211,7 @@ export function createControllerSelectModal(
}
: null,
onScreenLearn: (screenId, actionId) => {
if (!canEdit()) return;
learningActionId = null;
dpadLearningActionId = null;
bindingCapture = null;
@@ -171,6 +229,7 @@ export function createControllerSelectModal(
);
},
onScreenClear: (screenId, actionId) => {
if (!canEdit()) return;
const screen = getControllerScreen(screenId);
void saveScreenBindings(
screen,
@@ -179,6 +238,7 @@ export function createControllerSelectModal(
);
},
onScreenReset: (screenId, actionId) => {
if (!canEdit()) return;
const screen = getControllerScreen(screenId);
void saveScreenBindings(
screen,
@@ -187,6 +247,7 @@ export function createControllerSelectModal(
);
},
onScreenResetAll: (screenId) => {
if (!canEdit()) return;
const screen = getControllerScreen(screenId);
void saveScreenBindings(
screen,
@@ -197,6 +258,7 @@ export function createControllerSelectModal(
getLearningActionId: () => learningActionId,
getDpadLearningActionId: () => dpadLearningActionId,
onLearn: (actionId, bindingType) => {
if (!canEdit()) return;
const definition = getControllerBindingDefinition(actionId);
if (!definition) return;
dpadLearningActionId = null;
@@ -232,12 +294,15 @@ export function createControllerSelectModal(
setStatus(`Waiting for input for ${definition.label}.`);
},
onClear: (actionId) => {
if (!canEdit()) return;
void saveBinding(actionId, { kind: 'none' });
},
onReset: (actionId) => {
if (!canEdit()) return;
void saveBinding(actionId, getDefaultControllerBinding(actionId));
},
onDpadLearn: (actionId) => {
if (!canEdit()) return;
const definition = getControllerBindingDefinition(actionId);
if (!definition) return;
learningActionId = null;
@@ -259,9 +324,11 @@ export function createControllerSelectModal(
setStatus(`Press a D-pad direction for ${definition.label}.`);
},
onDpadClear: (actionId) => {
if (!canEdit()) return;
void saveDpadFallback(actionId, 'none');
},
onDpadReset: (actionId) => {
if (!canEdit()) return;
void saveDpadFallback(actionId, getDefaultDpadFallback(actionId));
},
});
@@ -279,9 +346,18 @@ export function createControllerSelectModal(
function syncSelectedControllerId(): void {
const selected = ctx.state.connectedGamepads[ctx.state.controllerDeviceSelectedIndex];
selectedControllerKey = selected ? getDeviceSelectionKey(selected) : null;
// This window then reads that controller, so Learn hears the pad being configured.
ctx.state.controllerSelectGamepadId = selected?.id ?? null;
}
function syncSelectedIndexToCurrentController(): void {
// The pad you just pressed (newest input) is the one in your hands.
const recentIndex = findMostRecentlyUsedIndex(ctx.state.connectedGamepads);
if (recentIndex >= 0) {
ctx.state.controllerDeviceSelectedIndex = recentIndex;
syncSelectedControllerId();
return;
}
const preferredId = ctx.state.controllerConfig?.preferredGamepadId ?? '';
const activeIndex = ctx.state.connectedGamepads.findIndex(
(device) => device.id === ctx.state.activeGamepadId,
@@ -331,12 +407,58 @@ export function createControllerSelectModal(
ctx.dom.controllerSelectPicker.disabled = ctx.state.connectedGamepads.length === 0;
ctx.dom.controllerSelectSummary.textContent =
ctx.state.connectedGamepads.length === 0
? 'No controller detected.'
? describePreview()
: `Active: ${ctx.state.activeGamepadId ?? 'none'} · Preferred: ${preferredId || 'none'}`;
lastRenderedDevicesKey = getDevicesKey();
lastRenderedActiveGamepadId = ctx.state.activeGamepadId;
lastRenderedPreferredId = preferredId;
renderFaceButtonStylePicker();
}
/** Which controller the locked preview shows, before this window sees one. */
function describePreview(): string {
const previewId = getSelectedControllerId();
if (!previewId) return 'No controller detected.';
const source = previewId === overlayActiveControllerId ? 'in use' : 'saved';
return `Showing ${shortControllerName(previewId)} (${source}).`;
}
/** Shows the button style of the controller being configured (its own, else the default). */
function renderFaceButtonStylePicker(): void {
const picker = ctx.dom.controllerSelectFaceButtonStyle;
picker.disabled = isEditingLocked();
const current = getSelectedControllerConfig()?.faceButtonLabels ?? 'xbox';
picker.innerHTML = '';
for (const style of CONTROLLER_FACE_BUTTON_STYLES) {
const option = document.createElement('option');
option.value = style.id;
option.textContent = `${style.name} (${style.hint})`;
option.selected = style.id === current;
picker.appendChild(option);
}
picker.value = current;
}
/** Saved for the controller being configured, like its remaps. */
async function saveFaceButtonStyle(
style: (typeof CONTROLLER_FACE_BUTTON_STYLES)[number],
): Promise<void> {
const profileId = getSelectedControllerId();
try {
await saveControllerConfig(
profileId
? { profiles: { [profileId]: { faceButtonLabels: style.id } } }
: { faceButtonLabels: style.id },
);
renderFaceButtonStylePicker();
controllerConfigForm.render();
setStatus(`Button style: ${style.name}.`);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
setStatus(`Failed to save button style: ${message}`, true);
renderFaceButtonStylePicker();
}
}
async function saveControllerConfig(
@@ -350,6 +472,9 @@ export function createControllerSelectModal(
if (update.preferredGamepadLabel !== undefined) {
ctx.state.controllerConfig.preferredGamepadLabel = update.preferredGamepadLabel;
}
if (update.faceButtonLabels) {
ctx.state.controllerConfig.faceButtonLabels = update.faceButtonLabels;
}
if (update.bindings) {
ctx.state.controllerConfig.bindings = {
...ctx.state.controllerConfig.bindings,
@@ -371,6 +496,7 @@ export function createControllerSelectModal(
const currentProfile = ctx.state.controllerConfig.profiles[profileId];
const baseProfile = currentProfile ?? {
label: profileUpdate.label ?? profileId,
faceButtonLabels: ctx.state.controllerConfig.faceButtonLabels,
buttonIndices: ctx.state.controllerConfig.buttonIndices,
bindings: ctx.state.controllerConfig.bindings,
reviewBindings: ctx.state.controllerConfig.reviewBindings,
@@ -378,6 +504,10 @@ export function createControllerSelectModal(
};
ctx.state.controllerConfig.profiles[profileId] = {
label: profileUpdate.label ?? baseProfile.label,
faceButtonLabels:
profileUpdate.faceButtonLabels ??
baseProfile.faceButtonLabels ??
ctx.state.controllerConfig.faceButtonLabels,
buttonIndices: {
...baseProfile.buttonIndices,
...(profileUpdate.buttonIndices ?? {}),
@@ -568,33 +698,27 @@ export function createControllerSelectModal(
controllerConfigForm.render();
}
// Polling publishes a snapshot every frame, pad or not: only a change in the devices
// sets the no-pad status, so it neither replaces the prompt nor a message like "Reset".
// Polling publishes a snapshot every frame, pad or not. Until the window sees a pad,
// editing is locked, so the only status is the prompt; it is set once, not every frame.
const idle = !learningActionId && !dpadLearningActionId && !screenLearning;
if (ctx.state.connectedGamepads.length === 0) {
if (idle && shouldRender) setStatus(getNoControllerStatus());
} else if (
idle &&
NO_CONTROLLER_STATUSES.has(ctx.dom.controllerSelectStatus.textContent ?? '')
) {
if (ctx.state.connectedGamepads.length === 0 && idle) {
if (ctx.dom.controllerSelectStatus.textContent !== PRESS_TO_EDIT_STATUS) {
setStatus(PRESS_TO_EDIT_STATUS);
}
} else if (idle && ctx.dom.controllerSelectStatus.textContent === PRESS_TO_EDIT_STATUS) {
setStatus('Choose a controller or click Learn to remap an action.');
}
}
/** A saved controller is only waiting for a press; without one there is nothing to show. */
function getNoControllerStatus(): string {
return ctx.state.controllerConfig?.preferredGamepadId
? WAITING_FOR_SAVED_CONTROLLER_STATUS
: NO_CONTROLLER_STATUS;
}
function openControllerSelectModal(): boolean {
if (ctx.state.controllerConfig?.enabled !== true) {
options.notifyControllerDisabled();
return false;
}
ctx.state.controllerSelectModalOpen = true;
overlayActiveControllerId = null;
syncSelectedIndexToCurrentController();
void loadOverlayActiveController();
options.syncSettingsModalSubtitleSuppression();
ctx.dom.overlay.classList.add('interactive');
ctx.dom.controllerSelectModal.classList.remove('hidden');
@@ -604,7 +728,7 @@ export function createControllerSelectModal(
renderPicker();
controllerConfigForm.render();
if (ctx.state.connectedGamepads.length === 0) {
setStatus(getNoControllerStatus());
setStatus(PRESS_TO_EDIT_STATUS);
} else {
setStatus('Choose a controller or click Learn to remap an action.');
}
@@ -618,6 +742,7 @@ export function createControllerSelectModal(
bindingCapture = null;
cancelScreenLearning();
ctx.state.controllerSelectModalOpen = false;
ctx.state.controllerSelectGamepadId = null;
options.syncSettingsModalSubtitleSuppression();
ctx.dom.controllerSelectModal.classList.add('hidden');
ctx.dom.controllerSelectModal.setAttribute('aria-hidden', 'true');
@@ -687,6 +812,15 @@ export function createControllerSelectModal(
ctx.dom.controllerSelectSave.addEventListener('click', () => {
void saveSelectedController();
});
ctx.dom.controllerSelectFaceButtonStyle.addEventListener('change', () => {
if (!canEdit()) {
renderFaceButtonStylePicker();
return;
}
const value = ctx.dom.controllerSelectFaceButtonStyle.value;
const style = CONTROLLER_FACE_BUTTON_STYLES.find((candidate) => candidate.id === value);
if (style) void saveFaceButtonStyle(style);
});
ctx.dom.controllerSelectPicker.addEventListener('change', () => {
const selectedKey = ctx.dom.controllerSelectPicker.value;
const selectedIndex = ctx.state.connectedGamepads.findIndex(