Controller: hold to show the secondary subtitle, dictionary entry navigation, standard button names (#312)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
YashaNoosandClaude Opus 5.5 authored and GitHub committed 2026-11-01 15:54:47 -08:00
1 parent af44735bb8
commit 655510cf2e
24 files changed
+609 -33

No files matched your search

@@ -16,6 +16,9 @@ const BINDINGS: ResolvedControllerConfig['bindings'] = {
nextAudio: { kind: 'button', buttonIndex: 5 },
playCurrentAudio: { kind: 'button', buttonIndex: 4 },
toggleMpvPause: { kind: 'button', buttonIndex: 9 },
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -86,7 +89,10 @@ function findOne(root: FakeElement, className: string, text?: string): FakeEleme
}
/** Renders the form into a fake container with `document` stubbed for the duration. */
function renderForm(learningActionId: ActionId | null) {
function renderForm(
learningActionId: ActionId | null,
options: { bindings?: typeof BINDINGS; mapping?: string } = {},
) {
const previousDocument = Object.getOwnPropertyDescriptor(globalThis, 'document');
Object.defineProperty(globalThis, 'document', {
configurable: true,
@@ -98,7 +104,8 @@ function renderForm(learningActionId: ActionId | null) {
const container = new FakeElement();
createControllerConfigForm({
container: container as unknown as HTMLElement,
getBindings: () => BINDINGS,
getBindings: () => options.bindings ?? BINDINGS,
getControllerMapping: () => options.mapping,
getLearningActionId: () => learningActionId,
getDpadLearningActionId: () => null,
onLearn: (actionId, bindingType) => calls.push(`learn:${actionId}:${bindingType}`),
@@ -141,9 +148,12 @@ test('controller config form renders grouped rows with friendly binding labels',
[
['Toggle Lookup', 'A / Cross'],
['Close Lookup', 'B / Circle'],
['Previous Entry', 'None'],
['Next Entry', 'None'],
['Mine Card', 'X / Square'],
['Toggle Keyboard-Only Mode', 'Y / Triangle'],
['Toggle MPV Pause', 'R3 / RS'],
['Show Secondary Subtitle (Hold)', 'None'],
['Quit MPV', 'Back / Select'],
['Previous Audio', 'None'],
['Next Audio', 'RB / R1'],
@@ -160,7 +170,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'],
);
assert.deepEqual(findAll(container, 'expanded'), []);
assert.deepEqual(findAll(container, 'controller-config-edit-panel'), []);
@@ -227,3 +237,29 @@ for (const c of [
assert.deepEqual(calls, [c.expected]);
});
}
test('controller config form names buttons by the W3C standard layout for standard pads', () => {
const bindings: typeof BINDINGS = {
...BINDINGS,
quitMpv: { kind: 'button', buttonIndex: 6 },
toggleMpvPause: { kind: 'button', buttonIndex: 10 },
holdSecondarySub: { kind: 'button', buttonIndex: 11 },
previousEntry: { kind: 'button', buttonIndex: 8 },
nextEntry: { kind: 'button', buttonIndex: 9 },
rightStickVertical: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
};
const badge = (row: FakeElement) => findOne(row, 'controller-config-badge').textContent;
const standard = renderForm(null, { bindings, mapping: 'standard' });
assert.equal(badge(standard.row('Quit MPV')), 'LT / L2');
assert.equal(badge(standard.row('Previous Entry')), 'Back / Select');
assert.equal(badge(standard.row('Next Entry')), 'Start / Options');
assert.equal(badge(standard.row('Toggle MPV Pause')), 'L3 / LS');
assert.equal(badge(standard.row('Show Secondary Subtitle (Hold)')), 'R3 / RS');
assert.equal(badge(standard.row('Popup Jump (Stick)')), 'Right Stick Y');
// Pads without the standard mapping keep the previous names.
const raw = renderForm(null, { bindings, mapping: '' });
assert.equal(badge(raw.row('Quit MPV')), 'Back / Select');
assert.equal(badge(raw.row('Previous Entry')), 'L3 / LS');
});
+76 -13
View File
@@ -30,6 +30,20 @@ export const CONTROLLER_BINDING_DEFINITIONS: ControllerBindingDefinition[] = [
bindingType: 'discrete',
defaultBinding: { kind: 'button', buttonIndex: 1 },
},
{
id: 'previousEntry',
label: 'Previous Entry',
group: 'Lookup',
bindingType: 'discrete',
defaultBinding: { kind: 'none' },
},
{
id: 'nextEntry',
label: 'Next Entry',
group: 'Lookup',
bindingType: 'discrete',
defaultBinding: { kind: 'none' },
},
{
id: 'mineCard',
label: 'Mine Card',
@@ -51,6 +65,13 @@ export const CONTROLLER_BINDING_DEFINITIONS: ControllerBindingDefinition[] = [
bindingType: 'discrete',
defaultBinding: { kind: 'button', buttonIndex: 9 },
},
{
id: 'holdSecondarySub',
label: 'Show Secondary Subtitle (Hold)',
group: 'Playback',
bindingType: 'discrete',
defaultBinding: { kind: 'none' },
},
{
id: 'quitMpv',
label: 'Quit MPV',
@@ -132,7 +153,9 @@ export function getDefaultDpadFallback(
return 'dpadFallback' in binding && binding.dpadFallback ? binding.dpadFallback : 'none';
}
const STANDARD_BUTTON_NAMES: Record<number, string> = {
// Names for pads that do not report the W3C "standard" mapping. Raw layouts
// vary by driver, so these are a best guess.
const RAW_LAYOUT_BUTTON_NAMES: Record<number, string> = {
0: 'A / Cross',
1: 'B / Circle',
2: 'X / Square',
@@ -152,7 +175,7 @@ const STANDARD_BUTTON_NAMES: Record<number, string> = {
16: 'Guide / Home',
};
const STANDARD_AXIS_NAMES: Record<number, string> = {
const RAW_LAYOUT_AXIS_NAMES: Record<number, string> = {
0: 'Left Stick X',
1: 'Left Stick Y',
2: 'Left Trigger',
@@ -161,18 +184,52 @@ const STANDARD_AXIS_NAMES: Record<number, string> = {
5: 'Right Trigger',
};
// Names for pads that report `mapping === 'standard'` (most Xbox, PlayStation,
// Switch and 8BitDo pads). Index meanings are fixed by the W3C Gamepad spec:
// https://w3c.github.io/gamepad/#remapping
const W3C_STANDARD_BUTTON_NAMES: Record<number, string> = {
0: 'A / Cross',
1: 'B / Circle',
2: 'X / Square',
3: 'Y / Triangle',
4: 'LB / L1',
5: 'RB / R1',
6: 'LT / L2',
7: 'RT / R2',
8: 'Back / Select',
9: 'Start / Options',
10: 'L3 / LS',
11: 'R3 / RS',
12: 'D-pad Up',
13: 'D-pad Down',
14: 'D-pad Left',
15: 'D-pad Right',
16: 'Guide / Home',
};
const W3C_STANDARD_AXIS_NAMES: Record<number, string> = {
0: 'Left Stick X',
1: 'Left Stick Y',
2: 'Right Stick X',
3: 'Right Stick Y',
};
type ControllerMapping = string | null | undefined;
const DPAD_FALLBACK_LABELS: Record<ControllerDpadFallback, string> = {
none: 'None',
horizontal: 'D-pad \u2194',
vertical: 'D-pad \u2195',
};
function getFriendlyButtonName(buttonIndex: number): string {
return STANDARD_BUTTON_NAMES[buttonIndex] ?? `Button ${buttonIndex}`;
export function getFriendlyButtonName(buttonIndex: number, mapping?: ControllerMapping): string {
const names = mapping === 'standard' ? W3C_STANDARD_BUTTON_NAMES : RAW_LAYOUT_BUTTON_NAMES;
return names[buttonIndex] ?? `Button ${buttonIndex}`;
}
function getFriendlyAxisName(axisIndex: number): string {
return STANDARD_AXIS_NAMES[axisIndex] ?? `Axis ${axisIndex}`;
export function getFriendlyAxisName(axisIndex: number, mapping?: ControllerMapping): string {
const names = mapping === 'standard' ? W3C_STANDARD_AXIS_NAMES : RAW_LAYOUT_AXIS_NAMES;
return names[axisIndex] ?? `Axis ${axisIndex}`;
}
export function formatControllerBindingSummary(
@@ -193,21 +250,25 @@ export function formatControllerBindingSummary(
return `Axis ${binding.axisIndex} + D-pad ${binding.dpadFallback}`;
}
function formatFriendlyStickLabel(binding: ResolvedControllerAxisBinding): string {
function formatFriendlyStickLabel(
binding: ResolvedControllerAxisBinding,
mapping: ControllerMapping,
): string {
if (binding.kind === 'none') return 'None';
return getFriendlyAxisName(binding.axisIndex);
return getFriendlyAxisName(binding.axisIndex, mapping);
}
function formatFriendlyBindingLabel(
binding: ResolvedControllerDiscreteBinding | ResolvedControllerAxisBinding,
mapping: ControllerMapping,
): string {
if (binding.kind === 'none') return 'None';
if ('direction' in binding) {
const name = getFriendlyAxisName(binding.axisIndex);
const name = getFriendlyAxisName(binding.axisIndex, mapping);
return `${name} ${binding.direction === 'positive' ? '+' : '\u2212'}`;
}
if ('buttonIndex' in binding) return getFriendlyButtonName(binding.buttonIndex);
return getFriendlyAxisName(binding.axisIndex);
if ('buttonIndex' in binding) return getFriendlyButtonName(binding.buttonIndex, mapping);
return getFriendlyAxisName(binding.axisIndex, mapping);
}
/** Unique key for expanded rows. Stick rows use the action id, dpad rows append ':dpad'. */
@@ -224,6 +285,8 @@ export function createControllerConfigForm(options: {
onDpadLearn: (actionId: ControllerBindingActionId) => void;
onDpadClear: (actionId: ControllerBindingActionId) => void;
onDpadReset: (actionId: ControllerBindingActionId) => void;
/** Gamepad API `mapping` of the controller being configured; picks the button names. */
getControllerMapping?: () => ControllerMapping;
}) {
let expandedRowKey: ExpandedRowKey | null = null;
@@ -275,7 +338,7 @@ export function createControllerConfigForm(options: {
const row = createRow(
definition.label,
formatFriendlyBindingLabel(binding),
formatFriendlyBindingLabel(binding, options.getControllerMapping?.()),
binding.kind === 'none',
isExpanded,
`Learn ${definition.label}`,
@@ -329,7 +392,7 @@ export function createControllerConfigForm(options: {
const row = createRow(
`${definition.label} (Stick)`,
formatFriendlyStickLabel(binding),
formatFriendlyStickLabel(binding, options.getControllerMapping?.()),
binding.kind === 'none',
isExpanded,
`Learn ${definition.label} stick`,
@@ -71,6 +71,9 @@ test('controller debug modal renders active controller axes, buttons, and config
nextAudio: { kind: 'button', buttonIndex: 5 },
playCurrentAudio: { kind: 'button', buttonIndex: 4 },
toggleMpvPause: { kind: 'button', buttonIndex: 9 },
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -186,6 +189,9 @@ test('controller debug modal copies buttonIndices config to clipboard', async ()
nextAudio: { kind: 'button', buttonIndex: 5 },
playCurrentAudio: { kind: 'button', buttonIndex: 4 },
toggleMpvPause: { kind: 'button', buttonIndex: 9 },
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -300,6 +306,9 @@ test('controller debug modal stays closed and notifies when controller support i
nextAudio: { kind: 'button', buttonIndex: 5 },
playCurrentAudio: { kind: 'button', buttonIndex: 4 },
toggleMpvPause: { kind: 'button', buttonIndex: 9 },
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
@@ -153,6 +153,9 @@ function buildContext() {
nextAudio: { kind: 'button', buttonIndex: 5 },
playCurrentAudio: { kind: 'button', buttonIndex: 4 },
toggleMpvPause: { kind: 'button', buttonIndex: 9 },
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },
+4
View File
@@ -56,6 +56,7 @@ export function createControllerSelectModal(
const controllerConfigForm = createControllerConfigForm({
container: ctx.dom.controllerConfigList,
getControllerMapping: () => getSelectedController()?.mapping ?? null,
getBindings: () =>
getSelectedControllerConfig()?.bindings ?? {
toggleLookup: { kind: 'button', buttonIndex: 0 },
@@ -67,6 +68,9 @@ export function createControllerSelectModal(
nextAudio: { kind: 'button', buttonIndex: 5 },
playCurrentAudio: { kind: 'button', buttonIndex: 4 },
toggleMpvPause: { kind: 'button', buttonIndex: 9 },
holdSecondarySub: { kind: 'none' },
previousEntry: { kind: 'none' },
nextEntry: { kind: 'none' },
leftStickHorizontal: { kind: 'axis', axisIndex: 0, dpadFallback: 'horizontal' },
leftStickVertical: { kind: 'axis', axisIndex: 1, dpadFallback: 'vertical' },
rightStickHorizontal: { kind: 'axis', axisIndex: 3, dpadFallback: 'none' },