远程控制模块的控制状态放在外面

This commit is contained in:
mmc
2026-10-13 09:20:03 +08:00
parent ae8187e662
commit 138d6fc53d
5 changed files with 93 additions and 42 deletions

View File

@@ -172,7 +172,7 @@ const DPadControl: React.FC<DPadControlProps> = ({ onGamepadUpdate, onSpeedChang
);
return (
<div className="flex flex-col items-center gap-5 w-full">
<div className="flex flex-col items-center gap-1 w-full">
{/* 十字方向键 */}
<div className="grid grid-cols-3 grid-rows-3 gap-2.5">
<div />

View File

@@ -34,6 +34,9 @@ import {
EnvironmentOutlined,
DeleteOutlined,
SafetyOutlined,
LockOutlined,
UnlockOutlined,
LoadingOutlined,
} from '@ant-design/icons';
import {
@@ -87,6 +90,8 @@ export default function DeviceStatusPage() {
const [isLoopMode, setIsLoopMode] = useState(true);
const controllerRef = useRef<DeviceController | null>(null);
const isFirstConnect = useRef(true);
// 标记本次会话是否已经发起过控制权限申请,避免手柄/方向盘高频更新时重复申请
const hasRequestedControlRef = useRef(false);
const [messageApi, contextHolder] = message.useMessage();
@@ -633,18 +638,34 @@ export default function DeviceStatusPage() {
}, [device, uavState, currentDroneData]);
const handleGamepadUpdate = useCallback((state) => {
// 无控制权限时:不发送消息,且只发起一次权限申请(避免手柄/方向盘高频更新刷屏)
if (!isControlled) {
if (!hasRequestedControlRef.current && !connecting) {
hasRequestedControlRef.current = true;
handleControlModal();
}
return;
}
console.log('Gamepad state updated:', state);
// 记录方向盘 xy 坐标,用于倒车影像叠加层
if (state?.axes) {
setSteerXY({ x: state.axes[0] || 0, y: state.axes[1] || 0 });
}
controllerRef.current?.sendGamepad(state);
}, []);
}, [isControlled, connecting]);
const handleControlModal = useCallback(() => {
console.log('请求控制权限');
controllerRef.current?.requestControl();
}, []);
// 获得控制权限后,重置"已申请"标记,以便后续失去权限时可再次提示
useEffect(() => {
if (isControlled) {
hasRequestedControlRef.current = false;
}
}, [isControlled]);
const handleOk = () => {
controllerRef.current?.sendPermissionRequest(
true,
@@ -1423,24 +1444,66 @@ export default function DeviceStatusPage() {
{t('devices.remoteControl')}
</>
}
extra={
<Space size={8}>
{/* 权限状态标签 */}
<Tag
icon={connecting ? <LoadingOutlined /> : (isControlled ? <UnlockOutlined /> : <LockOutlined />)}
color={connecting ? 'gold' : (isControlled ? 'green' : 'default')}
style={{ margin: 0, fontSize: 12 }}
>
{connecting
? t('devices.controlRequesting')
: isControlled
? t('devices.controlGranted')
: t('devices.noControlRight')}
</Tag>
{/* 申请/释放控制按钮 */}
{isControlled ? (
<Button
size="small"
danger
icon={<LockOutlined />}
onClick={() => {
controllerRef.current?.release?.();
setIsControlled(false);
}}
>
{t('devices.releaseControl')}
</Button>
) : (
<Button
size="small"
type="primary"
icon={<UnlockOutlined />}
loading={connecting}
onClick={handleControlModal}
>
{t('devices.requestControl')}
</Button>
)}
</Space>
}
variant="borderless"
className="ds-card"
style={{ borderRadius: 12, background: '#0f172a', flex: 1, minHeight: 0 }}
style={{ borderRadius: 12, background: '#0f172a', flex: 1, minHeight: 0, position: 'relative' }}
styles={{
header: { color: '#fff' },
body: {
padding: 12,
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap', // ✅ 新增:空间不足自动换行
flexWrap: 'wrap',
gap: 12,
height: 'calc(100% - 57px)',
overflow: 'auto', // ✅ 改成 auto,换行后内容不会被截断
alignItems: 'center', // 垂直居中,换行后美观
overflow: 'auto',
alignItems: 'center',
justifyContent: 'center',
},
}}
>
{/* Xbox 手柄 - 左侧,缩小显示 */}
<div
style={{
@@ -1448,17 +1511,13 @@ export default function DeviceStatusPage() {
alignItems: 'center',
justifyContent: 'center',
flex: '0 0 auto',
minWidth: 320, // ✅ 限制最小宽度,小于这个宽度就会换行
minWidth: 320,
transform: 'scale(0.82)',
transformOrigin: 'left center',
}}
>
<XboxController
onGamepadUpdate={handleGamepadUpdate}
handleControlModal={handleControlModal}
isControlled={isControlled}
hasControlRight={hasControlRight}
isConnecting={connecting}
/>
</div>
@@ -1466,7 +1525,6 @@ export default function DeviceStatusPage() {
<div
style={{
flex: '1 1 auto',
//minWidth: 300, // ✅ 最小宽度,触发换行阈值,可按需调整
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',

View File

@@ -1,7 +1,7 @@
import React, { useEffect, useState, useRef } from 'react';
import { useTranslation } from 'react-i18next';
const XboxController = ({ onGamepadUpdate, handleControlModal, isControlled, isConnecting, hasControlRight }) => {
const XboxController = ({ onGamepadUpdate }) => {
const { t } = useTranslation();
const [gamepadState, setGamepadState] = useState<{
buttons: boolean[];
@@ -114,36 +114,15 @@ const XboxController = ({ onGamepadUpdate, handleControlModal, isControlled, isC
});
return (
<div className="relative h-[252px] aspect-[4/3] flex items-center justify-center "
onClick={() => {
if (!isControlled) {
console.log("点击了无控制权限,发起权限请求");
// 示例:调用你代码中已有的权限请求弹窗逻辑
handleControlModal({ control: true, type: "switchPermission" });
}
}}>
{/* Gamepad Status Badge */}
<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
? isConnecting
? 'bg-yellow-500/20 text-yellow-400 border-yellow-500/30'
: isControlled
? 'bg-cyan-500/20 text-cyan-400 border-cyan-500/30'
: 'bg-red-500/20 text-red-400 border-red-500/30'
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.controllerNotConnected')
) : isConnecting ? (
t('devices.control2')
) : isControlled ? (
t('devices.controlling')
) : (
t('devices.clickToRequestControl')
)}
{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)]">