机器状态页面 样式优化 对接新的 设备状态数据 ,xbox和 方向盘 需要 互斥控制 中英文优化,方向盘加入 点火熄火等功能,等待对接状态和远程控制和倒车影像
This commit is contained in:
153
src/components/devices/Steerbutton.tsx
Normal file
153
src/components/devices/Steerbutton.tsx
Normal file
@@ -0,0 +1,153 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
interface SteerButtonProps {
|
||||
icon: React.ReactNode;
|
||||
label: string;
|
||||
color: string;
|
||||
onPressStart?: () => void;
|
||||
onPressEnd?: () => void;
|
||||
interval?: number;
|
||||
}
|
||||
|
||||
const SteerButton = ({
|
||||
icon,
|
||||
label,
|
||||
color,
|
||||
onPressStart,
|
||||
onPressEnd,
|
||||
interval = 100,
|
||||
}: SteerButtonProps) => {
|
||||
const [pressed, setPressed] = useState(false);
|
||||
|
||||
// 是否处于按下状态
|
||||
const holdingRef = useRef(false);
|
||||
|
||||
// 定时发送句柄
|
||||
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
|
||||
// 保存最新的回调,避免定时器使用过期闭包
|
||||
const onPressStartRef = useRef(onPressStart);
|
||||
const onPressEndRef = useRef(onPressEnd);
|
||||
|
||||
useEffect(() => {
|
||||
onPressStartRef.current = onPressStart;
|
||||
}, [onPressStart]);
|
||||
|
||||
useEffect(() => {
|
||||
onPressEndRef.current = onPressEnd;
|
||||
}, [onPressEnd]);
|
||||
|
||||
// 停止长按
|
||||
const stopHold = useCallback(() => {
|
||||
if (!holdingRef.current) return;
|
||||
|
||||
holdingRef.current = false;
|
||||
|
||||
if (timerRef.current !== null) {
|
||||
clearTimeout(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
|
||||
setPressed(false);
|
||||
|
||||
// 只在有效按压结束时发送一次释放数据
|
||||
onPressEndRef.current?.();
|
||||
}, []);
|
||||
|
||||
// 开始长按
|
||||
const startHold = useCallback(() => {
|
||||
// 防止重复触发
|
||||
if (holdingRef.current) return;
|
||||
|
||||
holdingRef.current = true;
|
||||
setPressed(true);
|
||||
|
||||
// 立即发送第一帧
|
||||
onPressStartRef.current?.();
|
||||
|
||||
// 持续发送
|
||||
const scheduleNext = () => {
|
||||
if (!holdingRef.current) return;
|
||||
|
||||
timerRef.current = setTimeout(() => {
|
||||
if (!holdingRef.current) return;
|
||||
|
||||
onPressStartRef.current?.();
|
||||
|
||||
scheduleNext();
|
||||
}, interval);
|
||||
};
|
||||
|
||||
scheduleNext();
|
||||
}, [interval]);
|
||||
|
||||
// 组件卸载时清理定时器
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
holdingRef.current = false;
|
||||
|
||||
if (timerRef.current !== null) {
|
||||
clearTimeout(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
aria-label={label}
|
||||
aria-pressed={pressed}
|
||||
onPointerDown={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
// 仅响应鼠标左键,触摸和笔不受影响
|
||||
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
||||
|
||||
// 捕获指针,确保在按钮外松开也能收到事件
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
|
||||
startHold();
|
||||
}}
|
||||
onPointerUp={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
stopHold();
|
||||
}}
|
||||
onPointerCancel={stopHold}
|
||||
onLostPointerCapture={stopHold}
|
||||
onContextMenu={(e) => e.preventDefault()}
|
||||
style={{
|
||||
flex: '1 1 0',
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
gap: 4,
|
||||
minHeight: 46,
|
||||
padding: '6px 4px',
|
||||
borderRadius: 8,
|
||||
background: pressed ? `${color}30` : '#1e293b',
|
||||
border: `1px solid ${pressed ? color : `${color}55`}`,
|
||||
color,
|
||||
cursor: 'pointer',
|
||||
userSelect: 'none',
|
||||
touchAction: 'none',
|
||||
transform: pressed ? 'scale(0.94)' : 'scale(1)',
|
||||
boxShadow: pressed ? `0 0 14px ${color}66` : 'none',
|
||||
transition:
|
||||
'transform .08s, background .12s, border-color .12s, box-shadow .12s',
|
||||
}}
|
||||
>
|
||||
<span style={{ display: 'inline-flex', fontSize: 16, lineHeight: 1 }}>
|
||||
{icon}
|
||||
</span>
|
||||
|
||||
<span style={{ fontSize: 11, fontWeight: 600, whiteSpace: 'nowrap' }}>
|
||||
{label}
|
||||
</span>
|
||||
</button>
|
||||
);
|
||||
};
|
||||
export default SteerButton;
|
||||
Reference in New Issue
Block a user