机器状态页面 样式优化 对接新的 设备状态数据 ,xbox和 方向盘 需要 互斥控制 中英文优化,方向盘加入 点火熄火等功能,等待对接状态和远程控制和倒车影像
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import React, { useEffect, useState, useRef } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
const XboxController = ({ onGamepadUpdate }) => {
|
||||
const XboxController = ({ onGamepadUpdate, onConnectionChange }) => {
|
||||
const { t } = useTranslation();
|
||||
const [gamepadState, setGamepadState] = useState<{
|
||||
buttons: boolean[];
|
||||
@@ -12,22 +11,20 @@ const XboxController = ({ onGamepadUpdate }) => {
|
||||
axes: [0, 0, 0, 0],
|
||||
connected: false,
|
||||
});
|
||||
|
||||
const requestRef = useRef<number>(null);
|
||||
// 用 ref 保存最新的回调,避免 RAF 循环闭包捕获旧 props 导致权限判断失效
|
||||
const onGamepadUpdateRef = useRef(onGamepadUpdate);
|
||||
onGamepadUpdateRef.current = onGamepadUpdate;
|
||||
const onConnectionChangeRef = useRef(onConnectionChange);
|
||||
onConnectionChangeRef.current = onConnectionChange;
|
||||
const DEAD_ZONE = 0.08;
|
||||
|
||||
const normalizeAxis = (v: number) =>
|
||||
Math.abs(v) < DEAD_ZONE ? 0 : Number(v.toFixed(2));
|
||||
const lastSendRef = useRef(0);
|
||||
const SEND_INTERVAL = 1000 / 10; // 20Hz
|
||||
const lastPayloadRef = useRef('');
|
||||
|
||||
const updateGamepad = () => {
|
||||
const gp = navigator.getGamepads()[0];
|
||||
|
||||
if (gp) {
|
||||
const state = {
|
||||
buttons: gp.buttons.map(b => b.pressed),
|
||||
@@ -35,16 +32,12 @@ const XboxController = ({ onGamepadUpdate }) => {
|
||||
connected: true,
|
||||
ts: Date.now(),
|
||||
};
|
||||
|
||||
setGamepadState(state);
|
||||
|
||||
const payload = JSON.stringify({
|
||||
buttons: state.buttons,
|
||||
axes: state.axes,
|
||||
});
|
||||
|
||||
const now = performance.now();
|
||||
|
||||
if (
|
||||
now - lastSendRef.current > SEND_INTERVAL
|
||||
// payload !== lastPayloadRef.current
|
||||
@@ -59,74 +52,54 @@ const XboxController = ({ onGamepadUpdate }) => {
|
||||
requestRef.current = requestAnimationFrame(updateGamepad);
|
||||
return;
|
||||
}
|
||||
|
||||
requestRef.current = requestAnimationFrame(updateGamepad);
|
||||
};
|
||||
useEffect(() => {
|
||||
requestRef.current = requestAnimationFrame(updateGamepad);
|
||||
|
||||
const onFocus = () => {
|
||||
// 页面重新获得焦点 → 立刻重拉一次
|
||||
requestRef.current = requestAnimationFrame(updateGamepad);
|
||||
};
|
||||
|
||||
window.addEventListener('focus', onFocus);
|
||||
|
||||
return () => {
|
||||
if (requestRef.current) cancelAnimationFrame(requestRef.current);
|
||||
window.removeEventListener('focus', onFocus);
|
||||
};
|
||||
}, []); // ✅ 空依赖
|
||||
|
||||
useEffect(() => {
|
||||
setTimeout(() => {
|
||||
window.focus();
|
||||
navigator.getGamepads(); // 🔥 强制触发一次
|
||||
}, 0);
|
||||
}, [/* 页面切换依赖 */]);
|
||||
|
||||
useEffect(() => {
|
||||
const handleConnect = (e: GamepadEvent) => {
|
||||
console.log('🎮 Gamepad connected:', e.gamepad.id);
|
||||
setGamepadState(prev => ({ ...prev, connected: true }));
|
||||
};
|
||||
|
||||
const handleDisconnect = (e: GamepadEvent) => {
|
||||
console.log('❌ Gamepad disconnected');
|
||||
//handleControlModal({ control: false });
|
||||
setGamepadState(prev => ({ ...prev, connected: false }));
|
||||
};
|
||||
|
||||
window.addEventListener('gamepadconnected', handleConnect);
|
||||
window.addEventListener('gamepaddisconnected', handleDisconnect);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('gamepadconnected', handleConnect);
|
||||
window.removeEventListener('gamepaddisconnected', handleDisconnect);
|
||||
};
|
||||
}, []);
|
||||
|
||||
// UI mapping for Xbox Gamepad
|
||||
|
||||
//点火熄火 上面俩 AB升降底盘 XY割刀加减速 button7急停
|
||||
// Buttons: 0:A, 1:B, 2:X, 3:Y, 4:LB, 5:RB, 6:LT, 7:RT, 8:Back, 9:Start, 10:LS, 11:RS, 12:Up, 13:Down, 14:Left, 15:Right
|
||||
//点火熄火 上面俩 AB升降底盘 XY割刀加减速 button8急停 BUTTON4点火 BUTTON5熄火
|
||||
const { buttons, axes, connected } = gamepadState;
|
||||
|
||||
const getAxisStyle = (axisX: number, axisY: number) => ({
|
||||
transform: `translate(${axisX * 6}px, ${axisY * 6}px)`,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
onConnectionChangeRef.current?.(gamepadState.connected);
|
||||
}, [gamepadState.connected]);
|
||||
return (
|
||||
<div className="relative h-[252px] aspect-[4/3] flex items-center justify-center">
|
||||
{/* 手柄硬件连接状态指示(仅表示物理手柄是否连接) */}
|
||||
<div
|
||||
className={`absolute top-0 right-0 px-2 py-0.5 rounded text-[8px] font-bold tracking-widest border ${connected
|
||||
? 'bg-cyan-500/20 text-cyan-400 border-cyan-500/30'
|
||||
: 'bg-slate-800 text-slate-500 border-white/5'
|
||||
}`}
|
||||
>
|
||||
{connected ? t('devices.controllerConnected') : t('devices.controllerNotConnected')}
|
||||
</div>
|
||||
|
||||
<svg viewBox="0 0 400 300" className="w-full h-full drop-shadow-[0_0_20px_rgba(34,211,238,0.15)]">
|
||||
{/* Main Body */}
|
||||
@@ -138,22 +111,23 @@ const XboxController = ({ onGamepadUpdate }) => {
|
||||
transform="scale(1, 1.08)"
|
||||
transformOrigin="200px 180px"
|
||||
/>
|
||||
|
||||
{/* Triggers & Bumpers */}
|
||||
|
||||
<rect x="70" y="50" width="50" height="25" rx="5" fill={buttons[4] ? "#22d3ee" : "#0f172a"} stroke="#22d3ee" strokeWidth="1" opacity={buttons[4] ? 1 : 0.3} />
|
||||
<text x="95" y="66" textAnchor="middle" fill={buttons[4] ? "#fff" : "#15414b"} fontSize="12" fontWeight="bold">LB</text>
|
||||
<rect x="280" y="50" width="50" height="25" rx="5" fill={buttons[5] ? "#22d3ee" : "#0f172a"} stroke="#22d3ee" strokeWidth="1" opacity={buttons[5] ? 1 : 0.3} />
|
||||
<text x="305" y="66" textAnchor="middle" fill={buttons[5] ? "#fff" : "#15414b"} fontSize="12" fontWeight="bold">RB</text>
|
||||
{/* 左右耳朵标识:左=点火 右=熄火 */}
|
||||
|
||||
{/* LT / RT Indicators */}
|
||||
<path d="M80 30 L110 30 Q115 45 95 50 L80 50 Z" fill={buttons[6] ? "#22d3ee" : "transparent"} stroke="#22d3ee" strokeWidth="1" opacity={buttons[6] ? 1 : 0.2} />
|
||||
<path d="M320 30 L290 30 Q285 45 305 50 L320 50 Z" fill={buttons[7] ? "#22d3ee" : "transparent"} stroke="#22d3ee" strokeWidth="1" opacity={buttons[7] ? 1 : 0.2} />
|
||||
|
||||
{/* Left Stick Area */}
|
||||
<circle cx="120" cy="140" r="35" fill="#0f172a" stroke="#22d3ee" strokeWidth="1" strokeDasharray="2 2" />
|
||||
<g style={getAxisStyle(axes[0] || 0, axes[1] || 0)}>
|
||||
<circle cx="120" cy="140" r="28" fill="#1e293b" stroke="#22d3ee" strokeWidth="2" />
|
||||
<circle cx="120" cy="140" r="12" fill={buttons[10] ? "#22d3ee" : "transparent"} stroke="#22d3ee" strokeWidth="1" />
|
||||
</g>
|
||||
|
||||
{/* Right Stick Area */}
|
||||
<g transform="translate(0, 20)">
|
||||
<circle
|
||||
@@ -165,7 +139,6 @@ const XboxController = ({ onGamepadUpdate }) => {
|
||||
strokeWidth="1"
|
||||
strokeDasharray="2 2"
|
||||
/>
|
||||
|
||||
<g style={getAxisStyle(axes[2] || 0, axes[3] || 0)}>
|
||||
<circle
|
||||
cx="280"
|
||||
@@ -185,7 +158,6 @@ const XboxController = ({ onGamepadUpdate }) => {
|
||||
/>
|
||||
</g>
|
||||
</g>
|
||||
|
||||
{/* D-Pad */}
|
||||
<g transform="translate(130, 220)">
|
||||
<path d="M0 -25 L0 25 M-25 0 L25 0" stroke="#334155" strokeWidth="15" strokeLinecap="round" />
|
||||
@@ -194,21 +166,28 @@ const XboxController = ({ onGamepadUpdate }) => {
|
||||
<path d="M-25 0 L-10 0" stroke={buttons[14] ? "#22d3ee" : "#64748b"} strokeWidth="12" strokeLinecap="round" />
|
||||
<path d="M25 0 L10 0" stroke={buttons[15] ? "#22d3ee" : "#64748b"} strokeWidth="12" strokeLinecap="round" />
|
||||
</g>
|
||||
|
||||
{/* Buttons (ABXY) */}
|
||||
{/* Buttons (ABXY) + Text Label */}
|
||||
<g transform="translate(280, 140)">
|
||||
<circle cx="0" cy="20" r="12" fill={buttons[0] ? "#22c55e" : "#0f172a"} stroke="#22c55e" strokeWidth="1" opacity={buttons[0] ? 1 : 0.6} /> {/* A */}
|
||||
<circle cx="20" cy="0" r="12" fill={buttons[1] ? "#ef4444" : "#0f172a"} stroke="#ef4444" strokeWidth="1" opacity={buttons[1] ? 1 : 0.6} /> {/* B */}
|
||||
<circle cx="-20" cy="0" r="12" fill={buttons[2] ? "#3b82f6" : "#0f172a"} stroke="#3b82f6" strokeWidth="1" opacity={buttons[2] ? 1 : 0.6} /> {/* X */}
|
||||
<circle cx="0" cy="-20" r="12" fill={buttons[3] ? "#eab308" : "#0f172a"} stroke="#eab308" strokeWidth="1" opacity={buttons[3] ? 1 : 0.6} /> {/* Y */}
|
||||
</g>
|
||||
<text x="0" y="24" textAnchor="middle" fill={buttons[0] ? "#fff" : "#22c55e"} fontSize="14" fontWeight="bold">A</text>
|
||||
|
||||
{/* Menu Buttons */}
|
||||
<circle cx="20" cy="0" r="12" fill={buttons[1] ? "#ef4444" : "#0f172a"} stroke="#ef4444" strokeWidth="1" opacity={buttons[1] ? 1 : 0.6} /> {/* B */}
|
||||
<text x="20" y="4" textAnchor="middle" fill={buttons[1] ? "#fff" : "#ef4444"} fontSize="14" fontWeight="bold">B</text>
|
||||
|
||||
<circle cx="-20" cy="0" r="12" fill={buttons[2] ? "#3b82f6" : "#0f172a"} stroke="#3b82f6" strokeWidth="1" opacity={buttons[2] ? 1 : 0.6} /> {/* X */}
|
||||
<text x="-20" y="4" textAnchor="middle" fill={buttons[2] ? "#fff" : "#3b82f6"} fontSize="14" fontWeight="bold">X</text>
|
||||
|
||||
<circle cx="0" cy="-20" r="12" fill={buttons[3] ? "#eab308" : "#0f172a"} stroke="#eab308" strokeWidth="1" opacity={buttons[3] ? 1 : 0.6} /> {/* Y */}
|
||||
<text x="0" y="-16" textAnchor="middle" fill={buttons[3] ? "#fff" : "#eab308"} fontSize="14" fontWeight="bold">Y</text>
|
||||
</g>
|
||||
{/* Menu Buttons + Text Label button8=急停, button9=Menu */}
|
||||
<circle cx="180" cy="140" r="6" fill={buttons[8] ? "#22d3ee" : "#334155"} />
|
||||
<text x="180" y="152" textAnchor="middle" fill={buttons[8] ? "#22d3ee" : "#64748b"} fontSize="9" fontWeight="bold">急停</text>
|
||||
|
||||
<circle cx="220" cy="140" r="6" fill={buttons[9] ? "#22d3ee" : "#334155"} />
|
||||
<text x="220" y="152" textAnchor="middle" fill={buttons[9] ? "#22d3ee" : "#64748b"} fontSize="9" fontWeight="bold">Menu</text>
|
||||
</svg>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default XboxController;
|
||||
|
||||
Reference in New Issue
Block a user