遥感 按着的时候 一直 发送ws
This commit is contained in:
@@ -39,8 +39,16 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
|
|||||||
// 用 ref 存储最新状态,避免 setInterval 闭包捕获旧值
|
// 用 ref 存储最新状态,避免 setInterval 闭包捕获旧值
|
||||||
const pressedRef = useRef<Record<string, boolean>>({});
|
const pressedRef = useRef<Record<string, boolean>>({});
|
||||||
const speedRef = useRef(0.5);
|
const speedRef = useRef(0.5);
|
||||||
|
// 始终指向最新的 emit,保证「按住持续发送」不受旧闭包影响
|
||||||
|
const emitRef = useRef<((overrides?: Record<string, boolean>) => void) | null>(null);
|
||||||
// 记录当前按下的键盘按键,避免 keydown 重复触发
|
// 记录当前按下的键盘按键,避免 keydown 重复触发
|
||||||
const activeKeysRef = useRef<Set<string>>(new Set());
|
const activeKeysRef = useRef<Set<string>>(new Set());
|
||||||
|
// 用 ref 存储外部回调,避免 emit/startPress/endPress 因回调引用变化而重建,
|
||||||
|
// 进而触发 useEffect 清理把持续发送的定时器清掉
|
||||||
|
const onGamepadUpdateRef = useRef(onGamepadUpdate);
|
||||||
|
onGamepadUpdateRef.current = onGamepadUpdate;
|
||||||
|
const onSpeedChangeRef = useRef(onSpeedChange);
|
||||||
|
onSpeedChangeRef.current = onSpeedChange;
|
||||||
|
|
||||||
// 构建状态:只传输 xy 坐标(axes),buttons 全部为 false
|
// 构建状态:只传输 xy 坐标(axes),buttons 全部为 false
|
||||||
const buildState = useCallback(
|
const buildState = useCallback(
|
||||||
@@ -51,14 +59,15 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
|
|||||||
let axisX = 0;
|
let axisX = 0;
|
||||||
let axisY = 0;
|
let axisY = 0;
|
||||||
const spd = speedRef.current;
|
const spd = speedRef.current;
|
||||||
|
// 向右为正、向左为负;向上为正、向下为负
|
||||||
if (btnMap.left) axisX -= spd;
|
if (btnMap.left) axisX -= spd;
|
||||||
if (btnMap.right) axisX += spd;
|
if (btnMap.right) axisX += spd;
|
||||||
if (btnMap.up) axisY -= spd;
|
if (btnMap.up) axisY += spd;
|
||||||
if (btnMap.down) axisY += spd;
|
if (btnMap.down) axisY -= spd;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
buttons: new Array(17).fill(false),
|
buttons: new Array(17).fill(false),
|
||||||
axes: [axisX, axisY, 0, 0],
|
axes: [0, axisY, axisX, 0],
|
||||||
connected: true,
|
connected: true,
|
||||||
ts: Date.now(),
|
ts: Date.now(),
|
||||||
};
|
};
|
||||||
@@ -70,10 +79,11 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
|
|||||||
(overrides?: Record<string, boolean>) => {
|
(overrides?: Record<string, boolean>) => {
|
||||||
if (disabled) return;
|
if (disabled) return;
|
||||||
const state = overrides ? buildState(overrides) : buildState();
|
const state = overrides ? buildState(overrides) : buildState();
|
||||||
onGamepadUpdate?.(state);
|
onGamepadUpdateRef.current?.(state);
|
||||||
},
|
},
|
||||||
[buildState, disabled, onGamepadUpdate],
|
[buildState, disabled],
|
||||||
);
|
);
|
||||||
|
emitRef.current = emit;
|
||||||
|
|
||||||
const startPress = useCallback(
|
const startPress = useCallback(
|
||||||
(key: string) => {
|
(key: string) => {
|
||||||
@@ -84,7 +94,7 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
|
|||||||
// 持续发送(模拟手柄按住)
|
// 持续发送(模拟手柄按住)
|
||||||
if (pressTimer.current) window.clearInterval(pressTimer.current);
|
if (pressTimer.current) window.clearInterval(pressTimer.current);
|
||||||
pressTimer.current = window.setInterval(() => {
|
pressTimer.current = window.setInterval(() => {
|
||||||
emit();
|
emitRef.current?.();
|
||||||
}, 100);
|
}, 100);
|
||||||
},
|
},
|
||||||
[disabled, emit],
|
[disabled, emit],
|
||||||
@@ -202,7 +212,7 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
|
|||||||
const v = parseFloat(e.target.value);
|
const v = parseFloat(e.target.value);
|
||||||
setSpeed(v);
|
setSpeed(v);
|
||||||
speedRef.current = v;
|
speedRef.current = v;
|
||||||
onSpeedChange?.(v);
|
onSpeedChangeRef.current?.(v);
|
||||||
}}
|
}}
|
||||||
className="w-full h-2.5 rounded-full appearance-none cursor-pointer bg-slate-700 accent-cyan-400"
|
className="w-full h-2.5 rounded-full appearance-none cursor-pointer bg-slate-700 accent-cyan-400"
|
||||||
style={{
|
style={{
|
||||||
|
|||||||
Reference in New Issue
Block a user