- 键盘控制:数字按名次选车、方向键移动光标、出/收字幕、圈数 +/-、计时器、 名次脚本、快捷键总表;在输入框里打字时不触发,每次按键都有 Toast 反馈 - 车手名单编辑器(E):姓名/缩写/车号/国籍/车队/性能/黄T摄/头像均可改, 支持调整发车顺位、单条重置、全部重置、JSON 导入导出,localStorage 持久化 - LAP 可手动 +/-,LAP 旁新增 mm:ss.mmm 计时器(零点=计时榜出现的瞬间) - 名次脚本(J):JSON 预设,以计时器为零点,支持 swap/move/retire/restore/lap 与 order * 改脚本即时生效并从零重播,修掉"改完之后过期事件被一次性爆发"的问题 * order 立即铺回赛道;写错会在状态条里报出具体原因,不再静默失败 * localStorage 持久化,刷新不再丢失 - 合并 feature/standalone-output:OBS 透明输出页 /output 与 WebSocket 中继 server/relayServer.js * 修复中继晚连接的输出页只能收到最后一片补丁(缓存改为合并累加) - 修复 react-spring 缺少 deps 导致进场动画被每次渲染重启 (chyron、百叶窗、wipe、计时塔名次描边) - 界面中文化(上屏的转播图形保持英文),F1 字体栈补 CJK 回退 - 新增「启动开发服务器.cmd」一键启动,.gitignore 排除本地工具目录
467 lines
14 KiB
TypeScript
467 lines
14 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
import { broadcastDirectorSelector } from "../broadcast/director/broadcastDirectorSelector";
|
|
import { broadcastGraphicsSelector } from "../broadcast/graphics/broadcastGraphicsSelector";
|
|
import { eventSelector } from "../event/eventSelector";
|
|
import { uiSelector } from "../ui/uiSelector";
|
|
import {
|
|
clearSelectedCars,
|
|
replaceSelectedCars,
|
|
setCursorGridSpot,
|
|
toggleSelectedCar,
|
|
updateChyronSettings,
|
|
} from "../broadcast/director/broadcastDirectorSlice";
|
|
import {
|
|
setTimingTowerDisplayMode,
|
|
setTimingTowerSplitsMode,
|
|
} from "../broadcast/graphics/broadcastGraphicsSlice";
|
|
import {
|
|
adjustLap,
|
|
awardFastestLap,
|
|
increaseDistance,
|
|
pauseShowClock,
|
|
refreshRunningOrder,
|
|
resetShowClock,
|
|
setCarStatus,
|
|
setEventStatus,
|
|
startShowClock,
|
|
} from "../event/eventSlice";
|
|
import {
|
|
pushToast,
|
|
setDriverEditorOpen,
|
|
setPositionScriptOpen,
|
|
setShortcutHelpOpen,
|
|
toggleDriverEditor,
|
|
togglePositionScript,
|
|
toggleShortcutHelp,
|
|
} from "../ui/uiSlice";
|
|
import { useChyronDirector } from "../broadcast/director/useChyronDirector";
|
|
import {
|
|
flagModeList,
|
|
getChyronModesList,
|
|
getChyronSubModesList,
|
|
} from "../../domain/data/broadcastGraphics";
|
|
import {
|
|
BGTimingTowerDisplayMode,
|
|
BGTimingTowerSplitsMode,
|
|
CarStatus,
|
|
EventStatus,
|
|
FlagMode,
|
|
GridSpot,
|
|
} from "../../types/state";
|
|
import {
|
|
chyronModeLabels,
|
|
chyronSubModeLabels,
|
|
eventStatusLabels,
|
|
flagModeLabels,
|
|
labelOf,
|
|
timingTowerDisplayModeLabels,
|
|
timingTowerSplitsModeLabels,
|
|
} from "../../i18n/labels";
|
|
|
|
const EDITABLE_TAGS = ["INPUT", "TEXTAREA", "SELECT"];
|
|
|
|
const DISPLAY_MODES: BGTimingTowerDisplayMode[] = [
|
|
BGTimingTowerDisplayMode.Hidden,
|
|
BGTimingTowerDisplayMode.LeftOnly,
|
|
BGTimingTowerDisplayMode.LeftAndRight,
|
|
BGTimingTowerDisplayMode.FullLeft,
|
|
];
|
|
|
|
/** Typing into a form control must never fire a broadcast shortcut. */
|
|
function isTypingTarget(target: EventTarget | null): boolean {
|
|
const element = target as HTMLElement | null;
|
|
if (element == null || typeof element.tagName !== "string") return false;
|
|
if (element.isContentEditable) return true;
|
|
return EDITABLE_TAGS.includes(element.tagName);
|
|
}
|
|
|
|
/**
|
|
* Digits address cars by running position.
|
|
*
|
|
* The physical key is what is read first, because it is the only thing that
|
|
* stays stable when Shift turns "1" into "!" - the typed key is kept as a
|
|
* fallback for environments that do not report a code.
|
|
*/
|
|
function digitFromKeyboard(event: KeyboardEvent): number | null {
|
|
const fromCode = /^(?:Digit|Numpad)([0-9])$/.exec(event.code);
|
|
const raw = fromCode != null ? fromCode[1] : (/^[0-9]$/.test(event.key) ? event.key : null);
|
|
if (raw == null) return null;
|
|
// "0" is wired to P10, matching how the positions read on screen.
|
|
return raw === "0" ? 10 : Number(raw);
|
|
}
|
|
|
|
function cycle<T>(list: T[], current: T, delta: number): T | null {
|
|
if (list.length === 0) return null;
|
|
const index = list.indexOf(current);
|
|
if (index === -1) return list[0];
|
|
return list[(index + delta + list.length) % list.length];
|
|
}
|
|
|
|
/**
|
|
* The whole keyboard control surface of the broadcast page.
|
|
*
|
|
* The listener is registered once and reads the latest state through a ref, so
|
|
* the race ticking along every 600ms never tears down and rebuilds it.
|
|
*/
|
|
export function useBroadcastShortcuts() {
|
|
const dispatch = useDispatch();
|
|
|
|
const { grid, sortedGridSpots, eventStatus, showClock, eventProgress } =
|
|
useSelector(eventSelector);
|
|
const { selectedCars, cursorGridSpot, chyronSettings } = useSelector(broadcastDirectorSelector);
|
|
const { timingBoard } = useSelector(broadcastGraphicsSelector);
|
|
const ui = useSelector(uiSelector);
|
|
const chyron = useChyronDirector();
|
|
|
|
const handlerRef = useRef<(event: KeyboardEvent) => void>(() => undefined);
|
|
|
|
handlerRef.current = (event: KeyboardEvent) => {
|
|
// Leave the browser's own shortcuts alone.
|
|
if (event.ctrlKey || event.metaKey || event.altKey) return;
|
|
if (isTypingTarget(event.target)) return;
|
|
|
|
const { key } = event;
|
|
const shift = event.shiftKey;
|
|
// Shift changes the reported key ("p" becomes "P"), so letters are folded
|
|
// to lower case and Shift is tested explicitly instead.
|
|
const press = key.length === 1 ? key.toLowerCase() : key;
|
|
|
|
const consume = () => {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
};
|
|
const say = (text: string) => dispatch(pushToast(text));
|
|
|
|
// A panel always wins. While one is open only Escape (plus "?" for the help
|
|
// list itself) does anything, so a stray keystroke cannot fire the show.
|
|
if (ui.shortcutHelpOpen) {
|
|
if (key === "Escape" || key === "?") {
|
|
consume();
|
|
dispatch(setShortcutHelpOpen(false));
|
|
}
|
|
return;
|
|
}
|
|
if (ui.driverEditorOpen) {
|
|
if (key === "Escape") {
|
|
consume();
|
|
dispatch(setDriverEditorOpen(false));
|
|
}
|
|
return;
|
|
}
|
|
if (ui.positionScriptOpen) {
|
|
if (key === "Escape") {
|
|
consume();
|
|
dispatch(setPositionScriptOpen(false));
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (key === "?") {
|
|
consume();
|
|
dispatch(toggleShortcutHelp());
|
|
return;
|
|
}
|
|
if (press === "e" && !shift) {
|
|
consume();
|
|
dispatch(toggleDriverEditor());
|
|
return;
|
|
}
|
|
if (press === "j" && !shift) {
|
|
consume();
|
|
dispatch(togglePositionScript());
|
|
return;
|
|
}
|
|
|
|
const gridSpotAtPosition = (position: number): GridSpot | null =>
|
|
sortedGridSpots[position - 1] ?? null;
|
|
const labelFor = (gridSpot: GridSpot | null | undefined): string =>
|
|
gridSpot == null ? "?" : (grid[gridSpot]?.driver.initials ?? "?");
|
|
const labelList = (spots: GridSpot[]) => spots.map(labelFor).join(" + ");
|
|
/** The cars the chyron and race commands act on: selection first, then cursor. */
|
|
const actionSpots = (): GridSpot[] => (
|
|
selectedCars.length > 0
|
|
? selectedCars
|
|
: (cursorGridSpot == null ? [] : [cursorGridSpot])
|
|
);
|
|
|
|
// Digits select a car by the position it is running in.
|
|
const digit = digitFromKeyboard(event);
|
|
if (digit != null) {
|
|
const spot = gridSpotAtPosition(digit);
|
|
if (spot == null) return;
|
|
consume();
|
|
dispatch(setCursorGridSpot(spot));
|
|
if (shift) {
|
|
const wasSelected = selectedCars.includes(spot);
|
|
dispatch(toggleSelectedCar(spot));
|
|
say(`已${wasSelected ? "移出" : "加入"} P${digit} ${labelFor(spot)}`);
|
|
} else {
|
|
dispatch(replaceSelectedCars([spot]));
|
|
say(`已选中 P${digit} ${labelFor(spot)}`);
|
|
}
|
|
return;
|
|
}
|
|
|
|
switch (press) {
|
|
// ---------------------------------------------------------------- cursor
|
|
case "ArrowUp":
|
|
case "ArrowDown": {
|
|
consume();
|
|
const order = sortedGridSpots;
|
|
if (order.length === 0) return;
|
|
const delta = press === "ArrowUp" ? -1 : 1;
|
|
const currentIndex = cursorGridSpot == null ? -1 : order.indexOf(cursorGridSpot);
|
|
const nextIndex = currentIndex === -1
|
|
? (delta > 0 ? 0 : order.length - 1)
|
|
: Math.min(order.length - 1, Math.max(0, currentIndex + delta));
|
|
dispatch(setCursorGridSpot(order[nextIndex]));
|
|
return;
|
|
}
|
|
case "Home":
|
|
case "End": {
|
|
consume();
|
|
if (sortedGridSpots.length === 0) return;
|
|
dispatch(setCursorGridSpot(
|
|
press === "Home" ? sortedGridSpots[0] : sortedGridSpots[sortedGridSpots.length - 1],
|
|
));
|
|
return;
|
|
}
|
|
case " ": {
|
|
consume();
|
|
const spot = cursorGridSpot ?? sortedGridSpots[0] ?? null;
|
|
if (spot == null) return;
|
|
const wasSelected = selectedCars.includes(spot);
|
|
dispatch(toggleSelectedCar(spot));
|
|
say(`已把 ${labelFor(spot)} ${wasSelected ? "移出" : "加入"}已选`);
|
|
return;
|
|
}
|
|
case "x": {
|
|
consume();
|
|
dispatch(clearSelectedCars(null));
|
|
say("已清空已选");
|
|
return;
|
|
}
|
|
|
|
// --------------------------------------------------------------- chyrons
|
|
case "Enter": {
|
|
consume();
|
|
if (shift) {
|
|
chyron.next();
|
|
say("字幕:下一步动画");
|
|
return;
|
|
}
|
|
if (chyron.targetSpots.length === 0) {
|
|
say("先选一辆车:数字 1-9、0,或上下方向键");
|
|
return;
|
|
}
|
|
chyron.open();
|
|
say(`字幕:${labelList(chyron.targetSpots)}`);
|
|
return;
|
|
}
|
|
case "Backspace": {
|
|
consume();
|
|
chyron.close();
|
|
say("字幕收起中");
|
|
return;
|
|
}
|
|
case "Escape": {
|
|
consume();
|
|
chyron.close();
|
|
return;
|
|
}
|
|
case "[":
|
|
case "]": {
|
|
consume();
|
|
const mode = cycle(getChyronModesList(), chyronSettings.mode, press === "]" ? 1 : -1);
|
|
if (mode == null) return;
|
|
const subModes = getChyronSubModesList(mode);
|
|
dispatch(updateChyronSettings({
|
|
mode,
|
|
subMode: subModes.includes(chyronSettings.subMode)
|
|
? chyronSettings.subMode
|
|
: (subModes[0] ?? chyronSettings.subMode),
|
|
}));
|
|
say(`字幕模式:${labelOf(chyronModeLabels, mode)}`);
|
|
return;
|
|
}
|
|
case ",":
|
|
case ".": {
|
|
consume();
|
|
const subMode = cycle(
|
|
getChyronSubModesList(chyronSettings.mode),
|
|
chyronSettings.subMode,
|
|
press === "." ? 1 : -1,
|
|
);
|
|
if (subMode == null) return;
|
|
dispatch(updateChyronSettings({ subMode }));
|
|
say(`字幕子模式:${labelOf(chyronSubModeLabels, subMode)}`);
|
|
return;
|
|
}
|
|
case "f": {
|
|
consume();
|
|
if (shift) {
|
|
// Shift+F - fastest lap.
|
|
const spot = actionSpots()[0] ?? null;
|
|
if (spot == null) {
|
|
say("先选一辆车");
|
|
return;
|
|
}
|
|
dispatch(awardFastestLap(spot));
|
|
say(`最快圈:${labelFor(spot)}`);
|
|
return;
|
|
}
|
|
const flagMode = cycle(flagModeList, chyronSettings.flagMode, 1);
|
|
if (flagMode == null) return;
|
|
dispatch(updateChyronSettings({ flagMode }));
|
|
say(`旗帜:${labelOf(flagModeLabels, flagMode as FlagMode)}`);
|
|
return;
|
|
}
|
|
case "p": {
|
|
consume();
|
|
if (shift) {
|
|
// Shift+P - nudge the selected cars up the road.
|
|
const spots = actionSpots();
|
|
if (spots.length === 0) {
|
|
say("先选一辆车");
|
|
return;
|
|
}
|
|
dispatch(increaseDistance({
|
|
gridSpots: spots,
|
|
distance: 200,
|
|
randomness: 0,
|
|
usePerformance: false,
|
|
}));
|
|
dispatch(refreshRunningOrder());
|
|
say(`${labelList(spots)} +200 米`);
|
|
return;
|
|
}
|
|
const showPosFlag = !chyronSettings.showPosFlag;
|
|
dispatch(updateChyronSettings({ showPosFlag }));
|
|
say(`名次牌:${showPosFlag ? "显示" : "隐藏"}`);
|
|
return;
|
|
}
|
|
case "o": {
|
|
consume();
|
|
if (shift) {
|
|
// Shift+O - nudge the whole field.
|
|
dispatch(increaseDistance({
|
|
gridSpots: Array.from(Array(grid.length).keys()),
|
|
distance: 100,
|
|
randomness: 0.05,
|
|
usePerformance: true,
|
|
}));
|
|
dispatch(refreshRunningOrder());
|
|
say("全场 +100 米");
|
|
return;
|
|
}
|
|
const showPortrait = !chyronSettings.showPortrait;
|
|
dispatch(updateChyronSettings({ showPortrait }));
|
|
say(`头像:${showPortrait ? "显示" : "隐藏"}`);
|
|
return;
|
|
}
|
|
case "n": {
|
|
consume();
|
|
const showDriverNumber = !chyronSettings.showDriverNumber;
|
|
dispatch(updateChyronSettings({ showDriverNumber }));
|
|
say(`车号:${showDriverNumber ? "显示" : "隐藏"}`);
|
|
return;
|
|
}
|
|
|
|
// -------------------------------------------------- laps and show clock
|
|
case "=":
|
|
case "+": {
|
|
consume();
|
|
dispatch(adjustLap(1));
|
|
say(`圈数 ${eventProgress.lapCount + 1}`);
|
|
return;
|
|
}
|
|
case "-":
|
|
case "_": {
|
|
consume();
|
|
dispatch(adjustLap(-1));
|
|
say(`圈数 ${Math.max(1, eventProgress.lapCount - 1)}`);
|
|
return;
|
|
}
|
|
case "c": {
|
|
consume();
|
|
if (shift) {
|
|
dispatch(resetShowClock());
|
|
say("计时器已归零,脚本从头重放");
|
|
return;
|
|
}
|
|
if (showClock.startedAt == null) {
|
|
dispatch(startShowClock());
|
|
say("计时器已启动");
|
|
} else {
|
|
dispatch(pauseShowClock());
|
|
say("计时器已暂停");
|
|
}
|
|
return;
|
|
}
|
|
|
|
// ------------------------------------------------------------ race state
|
|
case "s": {
|
|
consume();
|
|
const next = eventStatus === EventStatus.Running
|
|
? EventStatus.Stopped
|
|
: EventStatus.Running;
|
|
dispatch(setEventStatus(next));
|
|
say(`比赛${labelOf(eventStatusLabels, next)}`);
|
|
return;
|
|
}
|
|
case "r": {
|
|
consume();
|
|
dispatch(refreshRunningOrder());
|
|
say("名次已重算");
|
|
return;
|
|
}
|
|
case "d": {
|
|
consume();
|
|
const spots = actionSpots();
|
|
if (spots.length === 0) {
|
|
say("先选一辆车");
|
|
return;
|
|
}
|
|
const allRetired = spots.every(spot => grid[spot]?.status === CarStatus.Retired);
|
|
dispatch(setCarStatus({
|
|
gridSpots: spots,
|
|
status: allRetired ? CarStatus.Normal : CarStatus.Retired,
|
|
}));
|
|
dispatch(refreshRunningOrder());
|
|
say(allRetired ? "已取消退赛" : "已退赛");
|
|
return;
|
|
}
|
|
|
|
// --------------------------------------------------------- timing tower
|
|
case "t": {
|
|
consume();
|
|
const displayMode = cycle(DISPLAY_MODES, timingBoard.timingTower.displayMode, 1);
|
|
if (displayMode == null) return;
|
|
dispatch(setTimingTowerDisplayMode(displayMode));
|
|
say(`计时塔:${labelOf(timingTowerDisplayModeLabels, displayMode)}`);
|
|
return;
|
|
}
|
|
case "i": {
|
|
consume();
|
|
const splitsMode = timingBoard.timingTower.splitsMode === BGTimingTowerSplitsMode.Leader
|
|
? BGTimingTowerSplitsMode.Interval
|
|
: BGTimingTowerSplitsMode.Leader;
|
|
dispatch(setTimingTowerSplitsMode(splitsMode));
|
|
say(`计时模式:${labelOf(timingTowerSplitsModeLabels, splitsMode)}`);
|
|
return;
|
|
}
|
|
|
|
default:
|
|
return;
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
const listener = (event: KeyboardEvent) => handlerRef.current(event);
|
|
window.addEventListener("keydown", listener);
|
|
return () => window.removeEventListener("keydown", listener);
|
|
}, []);
|
|
}
|