From 5d05a27cfc3930dfd5f4681faeb2bd5b317aa471 Mon Sep 17 00:00:00 2001 From: zyp <1480083907@qq.com> Date: Wed, 16 Sep 2026 20:02:46 +0800 Subject: [PATCH] =?UTF-8?q?refactor(ui):=20=E9=87=8D=E6=9E=84=E6=97=A0?= =?UTF-8?q?=E4=BA=BA=E6=9C=BA=E6=8E=A7=E5=88=B6=E5=99=A8=E7=95=8C=E9=9D=A2?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 HomeScreen 组件重命名为 PilotHomeScreen 并调整参数 - 移除大量内联的 UI 组件实现,改为独立的可复用组件 - 优化地图预览显示逻辑,更新为更简洁的定位信息展示 - 重构飞行控制面板布局,调整尺寸和间距参数 - 添加更多菜单选项,整合飞前检查等功能入口 - 更新遥测数据显示组件,优化仪表盘读数展示 - 调整按钮样式和交互效果,改进视觉反馈 - 优化响应式布局计算,提升不同屏幕尺寸适配效果 --- .../ui/DroneControllerScreen.kt | 877 ++++-------------- .../ui/FlightInstrumentObserver.kt | 89 ++ .../ui/FlightInstrumentReadings.kt | 56 ++ .../dronecontroller/ui/PilotHomeScreen.kt | 195 ++++ .../ui/PilotPreflightScreen.kt | 74 ++ .../ui/FlightInstrumentReadingsTest.kt | 50 + 6 files changed, 627 insertions(+), 714 deletions(-) create mode 100644 sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentObserver.kt create mode 100644 sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentReadings.kt create mode 100644 sample/src/main/java/com/zklh/dronecontroller/ui/PilotHomeScreen.kt create mode 100644 sample/src/main/java/com/zklh/dronecontroller/ui/PilotPreflightScreen.kt create mode 100644 sample/src/test/java/com/zklh/dronecontroller/ui/FlightInstrumentReadingsTest.kt diff --git a/sample/src/main/java/com/zklh/dronecontroller/ui/DroneControllerScreen.kt b/sample/src/main/java/com/zklh/dronecontroller/ui/DroneControllerScreen.kt index 85125cf..c28f241 100644 --- a/sample/src/main/java/com/zklh/dronecontroller/ui/DroneControllerScreen.kt +++ b/sample/src/main/java/com/zklh/dronecontroller/ui/DroneControllerScreen.kt @@ -123,6 +123,14 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.foundation.layout.requiredSize +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.window.Popup +import androidx.compose.ui.window.PopupProperties +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.geometry.CornerRadius import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Size @@ -857,11 +865,13 @@ fun DroneControllerScreen() { } when (page) { - PilotPage.Home -> HomeScreen( + PilotPage.Home -> PilotHomeScreen( sdkState = sdkState, telemetry = telemetry, commandMessage = displayMessage, cloudConnected = thirdCloudConnected || cloudState.connected, + cloudTenantName = thirdCloudTenantName, + cloudUsername = thirdCloudUsername, onOpenCloud = { page = PilotPage.ThirdPartyCloud }, onOpenPreflight = { page = PilotPage.Preflight }, onOpenMission = { @@ -1012,297 +1022,6 @@ fun DroneControllerScreen() { } } -@Composable -private fun HomeScreen( - sdkState: DroneSdkState, - telemetry: TelemetrySnapshot, - commandMessage: String, - cloudConnected: Boolean, - onOpenCloud: () -> Unit, - onOpenPreflight: () -> Unit, - onOpenMission: () -> Unit, - onEnterFlight: () -> Unit, - onRetryConnect: () -> Unit -) { - BoxWithConstraints( - modifier = Modifier - .fillMaxSize() - .background(PilotPanel) - ) { - val compact = maxHeight < 560.dp || maxWidth < 960.dp - val pad = if (compact) 12.dp else 22.dp - val cardHeight = if (compact) 70.dp else 88.dp - val titleSize = if (compact) 18.sp else 22.sp - - Row(Modifier.fillMaxSize()) { - Column( - modifier = Modifier - .weight(0.58f) - .fillMaxHeight() - .padding(pad), - verticalArrangement = Arrangement.spacedBy(if (compact) 10.dp else 14.dp) - ) { - Row(horizontalArrangement = Arrangement.spacedBy(18.dp), verticalAlignment = Alignment.CenterVertically) { - Icon(Icons.Filled.Person, null, tint = Color(0xFF687078), modifier = Modifier.size(if (compact) 26.dp else 32.dp)) - Icon(Icons.Filled.Shield, null, tint = Color(0xFF687078), modifier = Modifier.size(if (compact) 26.dp else 32.dp)) - Spacer(Modifier.weight(1f)) - StatusBadge(if (sdkState.productConnected) "无人机已连接" else "无人机未连接", sdkState.productConnected) - } - - Row(horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth()) { - HomeCard("限高500m", "utmiss RID", Icons.Filled.LocationOn, titleSize, cardHeight, Modifier.weight(1f), onOpenPreflight) - HomeCard( - if (cloudConnected) "中科联航(江苏)信息技..." else "第三方云", - if (cloudConnected) "已连接" else "URL 登录", - Icons.Filled.Cloud, - titleSize, - cardHeight, - Modifier.weight(1f), - onOpenCloud - ) - } - - Row(horizontalArrangement = Arrangement.spacedBy(12.dp), modifier = Modifier.fillMaxWidth()) { - HomeCard("航线", "KMZ / WPML", Icons.Filled.Route, titleSize, cardHeight, Modifier.weight(1.6f), onOpenMission) - HomeIconCard(Icons.Filled.Image, cardHeight, Modifier.weight(0.7f), onOpenCloud) - HomeIconCard(Icons.Filled.Map, cardHeight, Modifier.weight(0.7f), onEnterFlight) - } - - PromoPanel(modifier = Modifier.weight(1f)) - } - - Column( - modifier = Modifier - .weight(0.42f) - .fillMaxHeight() - .background(Color.White) - .padding(pad), - horizontalAlignment = Alignment.CenterHorizontally - ) { - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.SpaceBetween, - verticalAlignment = Alignment.CenterVertically - ) { - Text( - if (sdkState.productConnected) "142航时/365天 143天" else "无人机未连接", - color = Color(0xFF687078), - fontSize = if (compact) 11.sp else 15.sp, - fontWeight = FontWeight.Bold, - maxLines = 1, - overflow = TextOverflow.Ellipsis - ) - Button( - onClick = onOpenPreflight, - colors = ButtonDefaults.buttonColors(containerColor = PilotOrange), - shape = RoundedCornerShape(6.dp), - contentPadding = ButtonDefaults.ButtonWithIconContentPadding, - modifier = Modifier.height(if (compact) 32.dp else 42.dp) - ) { - Text("注意", fontWeight = FontWeight.Bold, fontSize = if (compact) 12.sp else 15.sp) - Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, null, modifier = Modifier.size(17.dp)) - } - } - - Spacer(Modifier.height(if (compact) 6.dp else 14.dp)) - DroneSilhouette( - modifier = Modifier - .fillMaxWidth(if (compact) 0.58f else 0.66f) - .aspectRatio(3.1f) - ) - Spacer(Modifier.height(if (compact) 5.dp else 8.dp)) - Text( - if (sdkState.productConnected) (sdkState.productId?.toString() ?: "MATRICE 4T") else "等待无人机", - fontSize = if (compact) 20.sp else 29.sp, - fontWeight = FontWeight.Black, - maxLines = 1, - overflow = TextOverflow.Ellipsis - ) - Spacer(Modifier.height(if (compact) 5.dp else 8.dp)) - Row(horizontalArrangement = Arrangement.spacedBy(10.dp)) { - Pill("A", Icons.Filled.Radio, sdkState.productConnected) - Pill(if (sdkState.productConnected) "无人机已连接" else "无人机未连接", Icons.Filled.Sync, sdkState.productConnected) - } - Spacer(Modifier.height(if (compact) 7.dp else 10.dp)) - ConnectionDiagnosticCard( - sdkState = sdkState, - message = commandMessage, - compact = compact, - onRetryConnect = onRetryConnect - ) - Spacer(Modifier.height(if (compact) 4.dp else 8.dp)) - Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(10.dp)) { - Text("CAM", fontSize = if (compact) 14.sp else 18.sp, fontWeight = FontWeight.Black) - Text(if (sdkState.productConnected) "负载 1" else "负载未连接", color = Color(0xFF8A8D91), fontSize = if (compact) 10.sp else 13.sp, fontWeight = FontWeight.Bold) - } - Spacer(Modifier.height(if (compact) 4.dp else 7.dp)) - Button( - onClick = { - if (!sdkState.productConnected) onRetryConnect() - onEnterFlight() - }, - colors = ButtonDefaults.buttonColors(containerColor = Color(0xFF4F5559)), - shape = RoundedCornerShape(7.dp), - modifier = Modifier - .fillMaxWidth() - .height(if (compact) 42.dp else 58.dp) - ) { - Text("进入飞行界面", fontSize = if (compact) 15.sp else 22.sp, fontWeight = FontWeight.Black) - } - Spacer(Modifier.height(5.dp)) - Text( - if (sdkState.productConnected) "坐标 ${formatLatLng(telemetry.latitude, telemetry.longitude)}" else "坐标 --", - color = PilotMuted, - fontSize = 10.sp, - maxLines = 1, - overflow = TextOverflow.Ellipsis - ) - } - } - } -} - -@Composable -private fun HomeCard( - title: String, - subtitle: String, - icon: ImageVector, - titleSize: androidx.compose.ui.unit.TextUnit, - height: Dp, - modifier: Modifier, - onClick: () -> Unit -) { - Surface( - color = Color.White, - shape = RoundedCornerShape(8.dp), - shadowElevation = 0.5.dp, - modifier = modifier - .height(height) - .clickable(onClick = onClick) - ) { - Row( - modifier = Modifier.padding(horizontal = 14.dp, vertical = 10.dp), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.SpaceBetween - ) { - Column(modifier = Modifier.weight(1f)) { - Text(title, color = PilotText, fontSize = titleSize, fontWeight = FontWeight.Black, maxLines = 1, overflow = TextOverflow.Ellipsis) - Text(subtitle, color = PilotMuted, fontSize = 12.sp, maxLines = 1, overflow = TextOverflow.Ellipsis) - } - RoundIcon(icon) - } - } -} - -@Composable -private fun HomeIconCard(icon: ImageVector, height: Dp, modifier: Modifier, onClick: () -> Unit) { - Surface( - color = Color.White, - shape = RoundedCornerShape(8.dp), - shadowElevation = 0.5.dp, - modifier = modifier - .height(height) - .clickable(onClick = onClick) - ) { - Box(contentAlignment = Alignment.Center) { - RoundIcon(icon) - } - } -} - -@Composable -private fun RoundIcon(icon: ImageVector) { - Surface(shape = CircleShape, color = Color(0xFFF0F2F4), modifier = Modifier.size(44.dp)) { - Box(contentAlignment = Alignment.Center) { - Icon(icon, null, tint = Color(0xFF4B5563), modifier = Modifier.size(24.dp)) - } - } -} - -@Composable -private fun PromoPanel(modifier: Modifier = Modifier) { - Surface( - color = Color(0xFF151821), - shape = RoundedCornerShape(8.dp), - modifier = modifier.fillMaxWidth() - ) { - Row( - Modifier - .fillMaxSize() - .padding(start = 26.dp, end = 22.dp), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(18.dp) - ) { - Column( - modifier = Modifier - .weight(1f), - verticalArrangement = Arrangement.spacedBy(10.dp) - ) { - Text("大疆智图权益", color = Color.White, fontSize = 24.sp, fontWeight = FontWeight.Black, maxLines = 1, overflow = TextOverflow.Ellipsis) - Text("购买 DJI Matrice 4 系列无人机\n赠送大疆智图权益", color = Color.White, fontSize = 13.sp, lineHeight = 18.sp, fontWeight = FontWeight.Bold, maxLines = 2, overflow = TextOverflow.Ellipsis) - OutlinedButton( - onClick = {}, - shape = RoundedCornerShape(999.dp), - border = BorderStroke(1.5.dp, Color.White), - colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.White), - modifier = Modifier.height(36.dp) - ) { - Text("了解更多", fontWeight = FontWeight.Bold, fontSize = 12.sp) - } - } - Box( - modifier = Modifier - .fillMaxHeight(0.56f) - .width(210.dp) - .background(Color(0xFF2A3038), RoundedCornerShape(10.dp)) - .border(1.dp, Color(0xFF59616D), RoundedCornerShape(10.dp)) - ) { - Text("TERRA", color = Color(0xFF9CA3AF), fontSize = 24.sp, fontWeight = FontWeight.Black, modifier = Modifier.align(Alignment.Center)) - } - } - } -} - -@Composable -private fun DroneSilhouette(modifier: Modifier = Modifier) { - Canvas(modifier = modifier) { - val w = size.width - val h = size.height - val body = Color(0xFFE7EAEC) - val dark = Color(0xFF24282C) - val arm = Color(0xFFB9C0C4) - drawRoundRect(body, topLeft = Offset(w * 0.42f, h * 0.28f), size = Size(w * 0.16f, h * 0.42f), cornerRadius = CornerRadius(12f, 12f)) - drawLine(arm, Offset(w * 0.48f, h * 0.42f), Offset(w * 0.18f, h * 0.15f), 8f, StrokeCap.Round) - drawLine(arm, Offset(w * 0.52f, h * 0.42f), Offset(w * 0.82f, h * 0.15f), 8f, StrokeCap.Round) - drawLine(arm, Offset(w * 0.48f, h * 0.58f), Offset(w * 0.18f, h * 0.84f), 8f, StrokeCap.Round) - drawLine(arm, Offset(w * 0.52f, h * 0.58f), Offset(w * 0.82f, h * 0.84f), 8f, StrokeCap.Round) - listOf( - Offset(w * 0.15f, h * 0.13f), - Offset(w * 0.85f, h * 0.13f), - Offset(w * 0.15f, h * 0.87f), - Offset(w * 0.85f, h * 0.87f) - ).forEachIndexed { index, center -> - drawCircle(dark, radius = h * 0.08f, center = center) - val sign = if (index % 2 == 0) 1f else -1f - drawLine(PilotOrange, center + Offset(-w * 0.08f * sign, -h * 0.02f), center + Offset(w * 0.11f * sign, -h * 0.05f), 5f, StrokeCap.Round) - } - } -} - -@Composable -private fun Pill(text: String, icon: ImageVector, active: Boolean) { - Surface(shape = RoundedCornerShape(999.dp), color = Color(0xFFF0F1F2)) { - Row( - modifier = Modifier.padding(horizontal = 12.dp, vertical = 6.dp), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(5.dp) - ) { - Icon(icon, null, tint = if (active) PilotText else PilotMuted, modifier = Modifier.size(17.dp)) - Text(text, color = PilotText, fontSize = 14.sp, fontWeight = FontWeight.Black) - } - } -} - @Composable private fun ConnectionDiagnosticCard( sdkState: DroneSdkState, @@ -2197,89 +1916,7 @@ private fun PreflightScreen( onFarLimitChange: (Boolean) -> Unit, onToast: (String) -> Unit ) { - Column(Modifier.fillMaxSize().background(Color.White)) { - SimpleTitleBar( - title = "飞前检查", - onBack = null, - right = { - IconButton(onClick = onClose) { - Icon(Icons.Filled.Close, null, modifier = Modifier.size(32.dp)) - } - } - ) - Row( - modifier = Modifier - .fillMaxWidth() - .height(52.dp) - .padding(horizontal = 12.dp, vertical = 6.dp), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(12.dp) - ) { - Button( - onClick = { onToast("健康管理后续接入") }, - colors = ButtonDefaults.buttonColors(containerColor = PilotOrange), - shape = RoundedCornerShape(8.dp), - contentPadding = ButtonDefaults.TextButtonContentPadding, - modifier = Modifier.width(185.dp).fillMaxHeight() - ) { - Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) { - Text("注意", fontSize = 15.sp, fontWeight = FontWeight.Bold) - Text("健康管理", fontSize = 14.sp, fontWeight = FontWeight.Bold) - } - } - StatusChip(Icons.Filled.Route, if (sdkState.productConnected) remoteControllerGearText(telemetry.gear) else "--", if (sdkState.productConnected) Color.Black else PilotMuted) - StatusChip(Icons.Filled.BatteryFull, batteryStatusText(sdkState.productConnected, telemetry), if (sdkState.productConnected) Color.Black else PilotMuted) - StatusChip(Icons.Filled.Radio, if (sdkState.productConnected) "100%" else "--", if (sdkState.productConnected) Color.Black else PilotMuted) - StatusChip(Icons.Filled.Home, if (sdkState.productConnected) "未设置" else "--", if (sdkState.productConnected) PilotOrange else PilotMuted) - StatusChip(Icons.Filled.Warning, rtkStatusText(sdkState.productConnected, telemetry), rtkStatusColor(sdkState.productConnected, telemetry)) - StatusChip(Icons.Filled.UploadFile, if (sdkState.productConnected) "39.8G" else "--", if (sdkState.productConnected) Color.Black else PilotMuted) - StatusChip(Icons.Filled.Person, "A控", Color.Black) - } - Surface( - color = Color(0xFFFFEBCD), - border = BorderStroke(1.5.dp, PilotOrange), - shape = RoundedCornerShape(7.dp), - modifier = Modifier - .fillMaxWidth() - .height(38.dp) - .padding(horizontal = 12.dp) - ) { - Row( - modifier = Modifier.padding(horizontal = 14.dp), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(10.dp) - ) { - Icon(Icons.Filled.Warning, null, tint = PilotOrange, modifier = Modifier.size(18.dp)) - Text("提示:请确认飞行器的机臂完全展开,如果已经展开请忽略该信息。", color = PilotOrange, fontSize = 13.sp, maxLines = 1, overflow = TextOverflow.Ellipsis) - } - } - Row(Modifier.fillMaxSize().padding(horizontal = 12.dp, vertical = 6.dp)) { - Column(Modifier.weight(1f).verticalScroll(rememberScrollState())) { - HeightSetting("最低返航高", "(20~1500m)", returnAltitude, onReturnAltitudeChange) - HeightSetting("限高", "(20~1500m)", maxAltitude, onMaxAltitudeChange) - PreflightSegment("返航点", listOf("飞机", "A控"), 0) - PreflightSegment("避障行为", listOf("刹停", "绕行", "关闭"), 0) - AvoidanceSetting("水平避障", "刹停:1.0m 告警:16.0m", 0.96f) - AvoidanceSetting("上避障", "刹停:1.0m 告警:10.0m", 0.96f) - AvoidanceSetting("下避障", "刹停:2.0m 告警:10.0m", 0.82f) - AvoidanceSetting("自定义电量报警", "严重低电量:10% 低电量:24%", 0.38f) - } - Column(Modifier.weight(1f).verticalScroll(rememberScrollState())) { - DropSetting("失联行为", "悬停") - SwitchSetting("限远", farLimitEnabled, onFarLimitChange) - DropSetting("摇杆模式", "美国手") - DropSetting("地形数据", "定位高度收敛中", orangeDot = true) - Spacer(Modifier.height(10.dp)) - Text( - "MSDK:${sdkInitDisplayText(sdkState)} / ${if (sdkState.productConnected) "无人机已连接" else "无人机未连接"}", - color = PilotMuted, - fontSize = 14.sp, - maxLines = 2, - overflow = TextOverflow.Ellipsis - ) - } - } - } + PilotPreflightScreen(sdkState, telemetry, onClose) } @Composable @@ -2443,15 +2080,16 @@ private fun FlightScreen( } BoxWithConstraints(Modifier.fillMaxSize().background(PilotBlack)) { val compact = maxHeight < 560.dp || maxWidth < 960.dp - val topBarHeight = if (compact) 54.dp else 64.dp - val railWidth = if (compact) 62.dp else 74.dp - val sideRailWidth = if (compact) 74.dp else 86.dp - val bottomInset = 10.dp - val wheelSize = if (compact) 112.dp else 140.dp + val topBarHeight = if (compact) 46.dp else 54.dp + val railWidth = if (compact) 50.dp else 58.dp + val sideRailWidth = if (compact) 56.dp else 64.dp + val bottomInset = if (compact) 4.dp else 8.dp + val wheelSize = if (compact) 100.dp else 120.dp if (flightMode == FlightMode.Map) { NativeMap( connected = sdkState.productConnected, + telemetry = telemetry, modifier = Modifier.fillMaxSize() ) } else { @@ -2508,38 +2146,26 @@ private fun FlightScreen( liveBusy = liveBusy, missionActive = showMissionPanel, onMission = onToggleMissionPanel, + onOpenPreflight = onOpenPreflight, onReturnHome = onGoHome, onSwitchMode = onToggleFlightMode, onToggleSimulator = onToggleSimulator, onToggleLiveStream = onToggleLiveStream, modifier = Modifier .align(Alignment.TopStart) - .offset(y = topBarHeight) - .height(maxHeight - topBarHeight) + .offset(x = 8.dp, y = topBarHeight + 12.dp) + .height(maxHeight - topBarHeight - wheelSize - 30.dp) ) - if (flightMode == FlightMode.Camera) { - if (sdkState.productConnected) { - CameraTopSettings( - compact = compact, - modifier = Modifier - .align(Alignment.TopEnd) - .offset(y = topBarHeight + 12.dp) - .padding(end = sideRailWidth + 12.dp) - ) - } - } - FlightTelemetryHud( telemetry = telemetry, connected = sdkState.productConnected, compact = compact, modifier = Modifier .align(Alignment.BottomCenter) - .offset(x = if (compact) 31.dp else 37.dp) - .padding(bottom = bottomInset + 2.dp) - .width(if (compact) 360.dp else 470.dp) - .height(if (compact) 118.dp else 150.dp) + .padding(bottom = bottomInset) + .width((maxWidth - (wheelSize + 18.dp) * 2 - 18.dp).coerceAtMost(if (compact) 430.dp else 560.dp)) + .height(if (compact) 122.dp else 168.dp) ) RightCameraRail( @@ -2552,8 +2178,9 @@ private fun FlightScreen( onOpenActionPicker = { showCameraActionPicker = true }, onShutter = { triggerCameraShutter() }, modifier = Modifier - .align(Alignment.CenterEnd) - .padding(end = 0.dp) + .align(Alignment.TopEnd) + .offset(y = topBarHeight + 16.dp) + .padding(end = 10.dp) .zIndex(4f) ) @@ -2589,7 +2216,7 @@ private fun FlightScreen( compact = compact, modifier = Modifier .align(Alignment.BottomStart) - .offset(x = railWidth + 18.dp, y = -bottomInset) + .offset(x = if (compact) 10.dp else 14.dp, y = -bottomInset) .size(wheelSize) .zIndex(3f), onValueChange = { value -> @@ -2616,7 +2243,7 @@ private fun FlightScreen( compact = compact, modifier = Modifier .align(Alignment.BottomEnd) - .padding(end = 18.dp, bottom = bottomInset) + .padding(end = if (compact) 10.dp else 14.dp, bottom = bottomInset) .size(wheelSize) .zIndex(3f), onValueChange = { value -> @@ -2668,7 +2295,7 @@ private fun FlightStickWheel( onPressedChange: (Boolean) -> Unit ) { val density = LocalDensity.current - val diameter = if (compact) 112.dp else 140.dp + val diameter = if (compact) 100.dp else 120.dp val diameterPx = with(density) { diameter.toPx() } val radiusPx = diameterPx * 0.34f var knobOffset by remember { mutableStateOf(Offset.Zero) } @@ -2698,7 +2325,8 @@ private fun FlightStickWheel( Box( modifier = modifier .clip(CircleShape) - .background(Color(0xB5000000)) + .background(Color(0x660B1119)) + .semantics { contentDescription = "$title,$statusText" } .border(1.dp, if (enabled || pressEnabled) Color(0x99FFFFFF) else Color(0x557A7A7A), CircleShape) .pointerInput(pressEnabled) { if (!pressEnabled) return@pointerInput @@ -2763,10 +2391,7 @@ private fun FlightStickWheel( ) drawCircle(Color.White.copy(alpha = if (enabled) 0.28f else 0.12f), radius = inner * 0.42f, center = center + knobOffset) } - Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.align(Alignment.TopCenter).padding(top = if (compact) 10.dp else 12.dp)) { - Text(title, color = Color.White, fontSize = if (compact) 11.sp else 13.sp, fontWeight = FontWeight.Black, maxLines = 1) - Text(statusText, color = if (enabled) Color(0xFFBDEBDD) else Color(0xFFB8BEC6), fontSize = if (compact) 8.sp else 9.sp, fontWeight = FontWeight.Bold, maxLines = 1) - } + } } @@ -3017,7 +2642,7 @@ private fun FlightTopBar( compact: Boolean, modifier: Modifier = Modifier ) { - val warnWidth = if (compact) 260.dp else 420.dp + val warnWidth = if (compact) 150.dp else 240.dp val topFont = if (compact) 16.sp else 19.sp val isWarning = alertState.active || !sdkState.productConnected || sdkState.registerError != null val alertColor = alertBannerColor(alertState) @@ -3027,7 +2652,7 @@ private fun FlightTopBar( val rtkColor = rtkStatusColor(sdkState.productConnected, telemetry) val rtkCount = rtkSatelliteText(sdkState.productConnected, telemetry) - Column(modifier.background(PilotBlack)) { + Column(modifier.background(Color(0xA80B1119))) { Row( modifier = Modifier .fillMaxWidth() @@ -3108,92 +2733,17 @@ private fun FlightTopBar( } @Composable -private fun NativeMap(connected: Boolean, modifier: Modifier = Modifier) { - Box(modifier.background(Color(0xFF5E7446))) { - Canvas(Modifier.fillMaxSize()) { - val w = size.width - val h = size.height - drawRect(Color(0xFF657B4D)) - repeat(72) { index -> - val x = ((index * 83) % w.toInt()).toFloat() - val y = ((index * 51) % h.toInt()).toFloat() - drawCircle(Color(0x226F8C53), radius = 48f, center = Offset(x, y)) - } - val road = Color(0xCCDEE2D7) - drawLine(road, Offset(w * 0.72f, h * 0.04f), Offset(w * 0.93f, h * 0.90f), 8f, StrokeCap.Round) - drawLine(road, Offset(w * 0.46f, h * 0.0f), Offset(w * 0.64f, h * 0.86f), 5f, StrokeCap.Round) - drawLine(road, Offset(w * 0.10f, h * 0.84f), Offset(w * 0.96f, h * 0.64f), 7f, StrokeCap.Round) - drawLine(Color(0xAA2873A5), Offset(w * 0.50f, h * 0.55f), Offset(w * 0.94f, h * 0.18f), 3f, StrokeCap.Round) - drawLine(PilotOrange.copy(alpha = 0.75f), Offset(0f, h * 0.91f), Offset(w * 0.16f, h), 16f, StrokeCap.Round) +private fun NativeMap(connected: Boolean, modifier: Modifier = Modifier, telemetry: TelemetrySnapshot = TelemetrySnapshot()) { + Box(modifier.background(Color(0xFF111922)), contentAlignment = Alignment.Center) { + Column(horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp)) { + Icon(Icons.Filled.MyLocation, null, tint = if (connected && telemetry.locationValid) PilotGreen else PilotMuted, modifier = Modifier.size(36.dp)) + Text("定位信息", color = Color.White, fontSize = 18.sp, fontWeight = FontWeight.Bold) + Text(if (connected && telemetry.locationValid) "%.6f, %.6f".format(Locale.US, telemetry.latitude, telemetry.longitude) else "等待有效飞机定位", color = Color(0xFFADB8C6), fontSize = 14.sp) + Text("地图尚未接入,可在更多中切回图传", color = Color(0xFF7D8C9E), fontSize = 12.sp) } - Box( - modifier = Modifier - .align(Alignment.Center) - .size(70.dp) - .background(Color(0xAAFF5CF4), RoundedCornerShape(4.dp)) - .border(3.dp, Color(0xFFFF33FF), RoundedCornerShape(4.dp)) - ) { - Text("监管所", color = Color.White, fontWeight = FontWeight.Black, fontSize = 16.sp, modifier = Modifier.align(Alignment.Center)) - } - if (connected) { - Icon( - Icons.Filled.Navigation, - null, - tint = Color(0xFF2FA8FF), - modifier = Modifier - .align(Alignment.Center) - .offset(x = (-22).dp, y = (-42).dp) - .size(58.dp) - ) - } else { - Surface( - color = Color(0xCC222831), - shape = RoundedCornerShape(8.dp), - modifier = Modifier.align(Alignment.Center) - ) { - Text( - "无飞机定位", - color = Color.White, - fontSize = 13.sp, - fontWeight = FontWeight.Bold, - modifier = Modifier.padding(horizontal = 12.dp, vertical = 7.dp) - ) - } - } - Text("车辆管理所\n-征税大厅", color = Color.White, fontSize = 17.sp, fontWeight = FontWeight.Black, modifier = Modifier.align(Alignment.CenterEnd).padding(end = 160.dp)) - Text("公安监管\n龙里县星源", color = Color.White, fontSize = 17.sp, fontWeight = FontWeight.Black, modifier = Modifier.align(Alignment.BottomCenter).padding(bottom = 140.dp)) } } -@Composable -private fun NativeCameraPreview(modifier: Modifier = Modifier) { - Box(modifier.background(Color(0xFFC9C4B9))) { - Canvas(Modifier.fillMaxSize()) { - val w = size.width - val h = size.height - drawRect(Color(0xFFD0D3CE), topLeft = Offset(0f, 0f), size = Size(w * 0.44f, h)) - drawRect(Color(0xFF6E7270), topLeft = Offset(w * 0.44f, 0f), size = Size(w * 0.12f, h)) - drawRect(Color(0xFF886858), topLeft = Offset(w * 0.64f, 0f), size = Size(w * 0.24f, h)) - drawRect(Color(0xFFE0C41A), topLeft = Offset(w * 0.64f, h * 0.12f), size = Size(w * 0.18f, h * 0.48f), style = Stroke(4f)) - drawLine(Color(0xFFEEE8D8), Offset(0f, h * 0.75f), Offset(w, h * 0.75f), 8f) - repeat(18) { i -> - val y = h * 0.77f + i * 18f - drawLine(Color(0x66A77E55), Offset(0f, y), Offset(w, y + 8f), 2f) - } - } - Text( - "广角 1.0X", - color = PilotGreen, - fontSize = 16.sp, - fontWeight = FontWeight.Black, - modifier = Modifier - .align(Alignment.TopCenter) - .padding(top = 86.dp) - .border(1.5.dp, PilotGreen, RoundedCornerShape(5.dp)) - .padding(horizontal = 10.dp, vertical = 4.dp) - ) - } -} @Composable private fun DjiCameraLivePreview(connected: Boolean, modifier: Modifier = Modifier) { @@ -3282,51 +2832,33 @@ private fun LeftFlightRail( liveBusy: Boolean, missionActive: Boolean, onMission: () -> Unit, + onOpenPreflight: () -> Unit, onReturnHome: () -> Unit, onSwitchMode: () -> Unit, onToggleSimulator: () -> Unit, onToggleLiveStream: () -> Unit, modifier: Modifier = Modifier ) { - Surface( - color = if (cameraStyle) Color(0xF0000000) else Color(0xF8FFFFFF), - shape = RoundedCornerShape(topEnd = 8.dp, bottomEnd = 8.dp), - modifier = modifier.width(width) - ) { - Column( - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.SpaceEvenly, - modifier = Modifier.padding(vertical = 6.dp) - ) { - RailButton(Icons.Filled.Route, "航线库", compact, cameraStyle, onMission, active = missionActive) - RailButton(Icons.Filled.Home, "返航", compact, cameraStyle, onReturnHome) - RailButton( - Icons.Filled.Videocam, - when { - liveBusy -> "直播中" - liveStreaming -> "关直播" - else -> "开直播" - }, - compact, - cameraStyle, - onToggleLiveStream, - active = liveStreaming || liveBusy - ) - RailButton( - if (cameraStyle) Icons.Filled.Map else Icons.Filled.Videocam, - if (cameraStyle) "地图" else "图传", - compact, - cameraStyle, - onSwitchMode - ) - RailButton( - Icons.Filled.PowerSettingsNew, - if (simulatorEnabled) "关模拟" else "开模拟", - compact, - cameraStyle, - onToggleSimulator, - active = simulatorEnabled - ) + var showMore by remember { mutableStateOf(false) } + val menuOffset = with(LocalDensity.current) { (width + 8.dp).roundToPx() } + Box(modifier.width(width)) { + Column(Modifier.fillMaxHeight().verticalScroll(rememberScrollState()), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(if (compact) 6.dp else 8.dp)) { + RailButton(Icons.Filled.Route, "任务", compact, true, onMission, active = missionActive) + RailButton(Icons.Filled.Videocam, if (liveStreaming) "关直播" else if (liveBusy) "直播中" else "开直播", compact, true, onToggleLiveStream, active = liveStreaming || liveBusy) + RailButton(Icons.Filled.MoreHoriz, "更多", compact, true, { showMore = !showMore }, active = showMore) + RailButton(Icons.Filled.Home, "返航", compact, true, onReturnHome) + } + if (showMore) { + Popup(alignment = Alignment.TopStart, offset = IntOffset(menuOffset, 0), onDismissRequest = { showMore = false }, properties = PopupProperties(focusable = true)) { + Surface(color = Color(0xE6121B27), shape = RoundedCornerShape(12.dp), modifier = Modifier.width(116.dp).heightIn(max = 240.dp)) { + Column(Modifier.padding(8.dp).verticalScroll(rememberScrollState()), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(8.dp)) { + RailButton(Icons.Filled.Shield, "飞前检查", compact, true, { showMore = false; onOpenPreflight() }) + RailButton(if (cameraStyle) Icons.Filled.Map else Icons.Filled.Videocam, if (cameraStyle) "定位信息" else "图传", compact, true, { showMore = false; onSwitchMode() }) + RailButton(Icons.Filled.PowerSettingsNew, if (simulatorEnabled) "关模拟" else "开模拟", compact, true, onToggleSimulator, active = simulatorEnabled) + RailButton(Icons.Filled.Close, "收起", compact, true, { showMore = false }) + } + } + } } } } @@ -3352,81 +2884,25 @@ private fun RailButton( } Column( modifier = Modifier - .width(if (compact) 52.dp else 62.dp) + .width(if (compact) 46.dp else 54.dp) .clip(RoundedCornerShape(7.dp)) - .background(if (active) Color(0x3320E59B) else Color.Transparent) + .background(if (active) Color(0xBB144841) else Color(0x7A0B1119)) + .border(1.dp, if (active) PilotGreen.copy(alpha = 0.55f) else Color.White.copy(alpha = 0.10f), RoundedCornerShape(7.dp)) .clickable(onClick = onClick) - .padding(vertical = 4.dp), + .padding(vertical = if (compact) 4.dp else 5.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(2.dp) ) { - Icon(icon, null, tint = contentColor, modifier = Modifier.size(if (compact) 24.dp else 29.dp)) + Icon(icon, null, tint = contentColor, modifier = Modifier.size(if (compact) 22.dp else 26.dp)) if (text.isNotBlank()) { - Text(text, color = labelColor, fontSize = if (compact) 10.sp else 12.sp, textAlign = TextAlign.Center) + Text(text, color = labelColor, fontSize = if (compact) 9.sp else 10.sp, textAlign = TextAlign.Center, maxLines = 1) } } } -@Composable -private fun MapDrawingTools(compact: Boolean, modifier: Modifier = Modifier) { - Surface(color = Color.White, shape = RoundedCornerShape(8.dp), shadowElevation = 2.dp, modifier = modifier.width(if (compact) 42.dp else 48.dp)) { - Column { - MapToolButton(Icons.Filled.Route, compact) - HorizontalDivider(color = PilotLine) - MapToolButton(Icons.Filled.Map, compact) - HorizontalDivider(color = PilotLine) - MapToolButton(Icons.Filled.Radio, compact) - } - } -} -@Composable -private fun MapLayerTools(compact: Boolean, modifier: Modifier = Modifier) { - Column(modifier, verticalArrangement = Arrangement.spacedBy(8.dp)) { - FloatingTool(Icons.Filled.Search, compact) - Column { - FloatingTool(Icons.Filled.Navigation, compact, selected = true) - FloatingTool(Icons.Filled.Layers, compact) - FloatingTool(Icons.Filled.Map, compact) - } - FloatingTool(Icons.Filled.MyLocation, compact) - } -} -@Composable -private fun FloatingTool(icon: ImageVector, compact: Boolean, selected: Boolean = false) { - Surface(color = if (selected) PilotBlue else Color.White, shape = RoundedCornerShape(6.dp), shadowElevation = 2.dp, modifier = Modifier.size(if (compact) 44.dp else 52.dp)) { - Box(contentAlignment = Alignment.Center) { - Icon(icon, null, tint = if (selected) Color.White else PilotText, modifier = Modifier.size(if (compact) 25.dp else 31.dp)) - } - } -} -@Composable -private fun MapToolButton(icon: ImageVector, compact: Boolean) { - Box(contentAlignment = Alignment.Center, modifier = Modifier.size(if (compact) 42.dp else 48.dp)) { - Icon(icon, null, tint = PilotText, modifier = Modifier.size(if (compact) 24.dp else 28.dp)) - } -} - -@Composable -private fun CameraTopSettings(compact: Boolean, modifier: Modifier = Modifier) { - Surface(color = Color(0xB02A2A2A), shape = RoundedCornerShape(8.dp), modifier = modifier.height(if (compact) 48.dp else 56.dp)) { - Row( - modifier = Modifier.padding(horizontal = 12.dp), - verticalAlignment = Alignment.CenterVertically, - horizontalArrangement = Arrangement.spacedBy(if (compact) 12.dp else 16.dp) - ) { - val fs = if (compact) 12.sp else 14.sp - Text("ISO\n200", color = Color.White, fontSize = fs, fontWeight = FontWeight.Bold) - Text("F\n1.7", color = Color(0xFF9E9E9E), fontSize = fs, fontWeight = FontWeight.Bold) - Text("Shutter\n1/30", color = Color.White, fontSize = fs, fontWeight = FontWeight.Bold) - Text("M.M\n+0.3", color = Color(0xFF9E9E9E), fontSize = fs, fontWeight = FontWeight.Bold) - Text("AFC", color = Color.White, fontSize = if (compact) 15.sp else 18.sp, fontWeight = FontWeight.Black) - Text("CVI\n6886", color = Color.White, fontSize = fs, fontWeight = FontWeight.Black) - } - } -} @Composable private fun FocusFrame(compact: Boolean, modifier: Modifier = Modifier) { @@ -3449,82 +2925,57 @@ private fun FocusFrame(compact: Boolean, modifier: Modifier = Modifier) { } } -@Composable -private fun MiniPreview(mode: FlightMode, connected: Boolean, compact: Boolean, onClick: () -> Unit, modifier: Modifier = Modifier) { - Surface( - color = Color(0xDD000000), - shape = RoundedCornerShape(8.dp), - modifier = modifier.clickable(onClick = onClick) - ) { - Box(Modifier.fillMaxSize()) { - if (mode == FlightMode.Map) { - if (connected) { - NativeCameraPreview(Modifier.fillMaxSize()) - } else { - Box(Modifier.fillMaxSize().background(Color(0xFF111827)), contentAlignment = Alignment.Center) { - Text("CAM --", color = Color(0xFFCBD5E1), fontSize = if (compact) 12.sp else 15.sp, fontWeight = FontWeight.Black) - } - } - Text("CAM", color = Color.White, fontSize = if (compact) 12.sp else 15.sp, fontWeight = FontWeight.Black, modifier = Modifier.align(Alignment.TopStart).padding(7.dp)) - } else { - NativeMap(connected = connected, modifier = Modifier.fillMaxSize()) - } - Box( - modifier = Modifier - .align(Alignment.BottomStart) - .padding(8.dp) - .size(if (compact) 20.dp else 24.dp) - .border(2.dp, Color.White) - ) - } - } -} @Composable private fun FlightTelemetryHud(telemetry: TelemetrySnapshot, connected: Boolean, compact: Boolean, modifier: Modifier = Modifier) { - val speedValue = if (connected) "%04.1f".format(Locale.US, horizontalSpeed(telemetry)) else "00.0" - val altitudeValue = if (connected) "%05.1f".format(Locale.US, telemetry.altitude.coerceAtLeast(0.0)) else "000.0" - val aslValue = if (connected) "%05.1f ASL".format(Locale.US, telemetry.altitude.coerceAtLeast(0.0)) else "0034.9 ASL" - val headingDegrees = if (connected) normalizeHeading(telemetry.heading) else 180.0 - val headingValue = "%03d".format(Locale.US, headingDegrees.roundToInt() % 360) + val readings = rememberFlightInstrumentReadings(connected, telemetry.cameraComponent) + FlightInstrumentDashboard(readings, compact, modifier) +} - Box(modifier = modifier, contentAlignment = Alignment.Center) { +@Composable +private fun FlightInstrumentDashboard(readings: FlightInstrumentReadings, compact: Boolean, modifier: Modifier = Modifier) { + val headingDegrees = readings.heading ?: 0.0 + val headingValue = readings.heading?.let { "%03d".format(Locale.US, it.roundToInt() % 360) } ?: "—" + BoxWithConstraints(modifier, contentAlignment = Alignment.Center) { + val designWidth = if (compact) 430.dp else 560.dp + val designHeight = if (compact) 122.dp else 168.dp + val scale = minOf(maxWidth / designWidth, maxHeight / designHeight, 1f) Row( - modifier = Modifier.fillMaxSize(), + modifier = Modifier.requiredSize(designWidth, designHeight).graphicsLayer(scaleX = scale, scaleY = scale), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.Center ) { - SpeedReadout(speedValue, compact, Modifier.width(if (compact) 86.dp else 104.dp).fillMaxHeight()) - PitchScale(compact, Modifier.width(if (compact) 28.dp else 34.dp).fillMaxHeight()) - CompassInstrument(headingValue, headingDegrees, compact, Modifier.size(if (compact) 108.dp else 136.dp)) - AltitudeScale(compact, Modifier.width(if (compact) 28.dp else 34.dp).fillMaxHeight()) - AltitudeReadout(altitudeValue, aslValue, compact, Modifier.width(if (compact) 110.dp else 128.dp).fillMaxHeight()) + SpeedReadout(instrumentNumber(readings.horizontalSpeed), instrumentNumber(readings.windSpeed), compact, Modifier.width(if (compact) 104.dp else 138.dp).fillMaxHeight()) + PitchScale(readings.gimbalPitch, compact, Modifier.width(if (compact) 38.dp else 48.dp).fillMaxHeight()) + CompassInstrument(headingValue, headingDegrees, readings.obstacles, compact, Modifier.size(if (compact) 122.dp else 160.dp)) + AltitudeScale(compact, Modifier.width(if (compact) 30.dp else 40.dp).fillMaxHeight()) + AltitudeReadout(instrumentNumber(readings.relativeAltitude), "${instrumentNumber(readings.seaLevelAltitude)} ASL", instrumentNumber(readings.climbSpeed), readings.obstacles, compact, Modifier.width(if (compact) 136.dp else 174.dp).fillMaxHeight()) } } } @Composable -private fun SpeedReadout(value: String, compact: Boolean, modifier: Modifier = Modifier) { - val valueSize = if (compact) 25.sp else 32.sp +private fun SpeedReadout(value: String, windSpeed: String, compact: Boolean, modifier: Modifier = Modifier) { + val valueSize = if (compact) 29.sp else 38.sp Box(modifier) { Text( - "WS 00.0", + "WS $windSpeed", color = Color.White, - fontSize = if (compact) 11.sp else 14.sp, + fontSize = if (compact) 12.sp else 15.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow), - modifier = Modifier.align(Alignment.TopStart).padding(top = if (compact) 13.dp else 22.dp) + modifier = Modifier.align(Alignment.TopStart).padding(top = if (compact) 14.dp else 24.dp) ) Row( verticalAlignment = Alignment.Bottom, horizontalArrangement = Arrangement.spacedBy(if (compact) 4.dp else 6.dp), - modifier = Modifier.align(Alignment.CenterStart).padding(top = if (compact) 22.dp else 28.dp) + modifier = Modifier.align(Alignment.CenterStart).padding(top = if (compact) 18.dp else 24.dp) ) { Column(horizontalAlignment = Alignment.Start) { Text( "SPD", color = PilotGreen, - fontSize = if (compact) 9.sp else 11.sp, + fontSize = if (compact) 10.sp else 12.sp, fontWeight = FontWeight.Black, lineHeight = if (compact) 9.sp else 11.sp, style = TextStyle(shadow = HudTextShadow) @@ -3532,7 +2983,7 @@ private fun SpeedReadout(value: String, compact: Boolean, modifier: Modifier = M Text( "m/s", color = PilotGreen, - fontSize = if (compact) 8.sp else 10.sp, + fontSize = if (compact) 9.sp else 11.sp, fontWeight = FontWeight.Black, lineHeight = if (compact) 8.sp else 10.sp, style = TextStyle(shadow = HudTextShadow) @@ -3551,15 +3002,15 @@ private fun SpeedReadout(value: String, compact: Boolean, modifier: Modifier = M } @Composable -private fun PitchScale(compact: Boolean, modifier: Modifier = Modifier) { - Box(modifier, contentAlignment = Alignment.Center) { +private fun PitchScale(pitch: Double?, compact: Boolean, modifier: Modifier = Modifier) { + Box(modifier.semantics { contentDescription = "云台俯仰角 ${instrumentNumber(pitch, 0)} 度" }, contentAlignment = Alignment.Center) { Text( - "0°", + "${instrumentNumber(pitch, 0)}°", color = Color(0xFFFFC400), - fontSize = if (compact) 12.sp else 15.sp, + fontSize = if (compact) 14.sp else 17.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow), - modifier = Modifier.align(Alignment.CenterStart).offset(y = if (compact) (-24).dp else (-30).dp) + modifier = Modifier.align(Alignment.CenterStart).offset(y = if (compact) (-24).dp else (-34).dp) ) Canvas(Modifier.fillMaxHeight(0.66f).width(if (compact) 8.dp else 10.dp).align(Alignment.CenterEnd)) { val railX = size.width / 2f @@ -3575,20 +3026,20 @@ private fun PitchScale(compact: Boolean, modifier: Modifier = Modifier) { drawLine(Color(0xAAFFFFFF), Offset(0f, y), Offset(size.width, y), 1.5f) } } - Canvas(Modifier.size(width = if (compact) 13.dp else 16.dp, height = if (compact) 8.dp else 9.dp).align(Alignment.CenterEnd).offset(x = if (compact) 7.dp else 8.dp)) { + Canvas(Modifier.size(width = if (compact) 13.dp else 16.dp, height = if (compact) 8.dp else 9.dp).align(Alignment.CenterEnd).offset(x = if (compact) 7.dp else 8.dp, y = (-(pitch ?: 0.0).coerceIn(-90.0, 90.0) / 90.0 * if (compact) 35 else 44).dp)) { val marker = Path().apply { moveTo(0f, 0f) lineTo(size.width, size.height / 2f) lineTo(0f, size.height) close() } - drawPath(marker, Color(0xFF13D8FF)) + if (pitch != null && pitch.isFinite()) drawPath(marker, Color(0xFF13D8FF)) } } } @Composable -private fun CompassInstrument(headingText: String, headingDegrees: Double, compact: Boolean, modifier: Modifier = Modifier) { +private fun CompassInstrument(headingText: String, headingDegrees: Double, obstacles: InstrumentObstacles, compact: Boolean, modifier: Modifier = Modifier) { val red = Color(0xFFFF3333) Box(modifier, contentAlignment = Alignment.Center) { Canvas(Modifier.fillMaxSize()) { @@ -3596,12 +3047,12 @@ private fun CompassInstrument(headingText: String, headingDegrees: Double, compa val outerRadius = size.minDimension * 0.47f val ringRadius = outerRadius * 0.78f val textRadius = outerRadius * 0.84f - val headingOffset = 180.0 - headingDegrees + val headingOffset = -headingDegrees - drawCircle(Color(0xF0181818), outerRadius, center) - drawCircle(Color(0xDD24211D), outerRadius * 0.72f, center) - drawCircle(Color(0x442D343A), outerRadius * 0.46f, center, style = Stroke(1.6f)) - drawCircle(Color(0x442D343A), outerRadius * 0.28f, center, style = Stroke(1.2f)) + drawCircle(Color(0xF0121212), outerRadius, center) + drawCircle(Color(0xDD28231F), outerRadius * 0.72f, center) + drawCircle(Color(0x55323A40), outerRadius * 0.46f, center, style = Stroke(1.6f)) + drawCircle(Color(0x55323A40), outerRadius * 0.28f, center, style = Stroke(1.2f)) drawLine(Color(0x662D343A), Offset(center.x - ringRadius, center.y), Offset(center.x + ringRadius, center.y), 1.4f) drawLine(Color(0x662D343A), Offset(center.x, center.y - ringRadius), Offset(center.x, center.y + ringRadius), 1.4f) @@ -3655,44 +3106,35 @@ private fun CompassInstrument(headingText: String, headingDegrees: Double, compa nativeCanvas.drawText(text, x, y, fillPaint) } - val smallText = if (compact) 7.8f else 9.8f - listOf( - 300.0 to "15", - 330.0 to "12", - 240.0 to "9", - 210.0 to "6", - 30.0 to "21", - 60.0 to "24", - 120.0 to "30", - 150.0 to "33" - ).forEach { (angle, text) -> - drawCompassLabel(text, angle, textRadius, smallText, Color.White) + val smallText = if (compact) 7.8.sp.toPx() else 9.8.sp.toPx() + (30..330 step 30).filter { it % 90 != 0 }.forEach { angle -> + drawCompassLabel((angle / 10).toString(), angle.toDouble(), textRadius, smallText, Color.White) + } + listOf("N" to 0.0, "E" to 90.0, "S" to 180.0, "W" to 270.0).forEach { (label, angle) -> + drawCompassLabel(label, angle, outerRadius * 0.84f, if (compact) 10.sp.toPx() else 13.sp.toPx(), Color.White) } - drawCompassLabel("E", 270.0, outerRadius * 0.74f, if (compact) 10f else 13f, Color.White) - drawCompassLabel("W", 90.0, outerRadius * 0.74f, if (compact) 10f else 13f, Color.White) - drawCompassLabel("N", 180.0, outerRadius * 0.86f, if (compact) 10f else 13f, Color.White) } } Text( headingText, color = PilotGreen, - fontSize = if (compact) 13.sp else 16.sp, + fontSize = if (compact) 16.sp else 20.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow), - modifier = Modifier.align(Alignment.TopCenter).offset(y = if (compact) (-4).dp else (-2).dp) + modifier = Modifier.align(Alignment.TopCenter).offset(y = if (compact) (-8).dp else (-10).dp) ) Box( modifier = Modifier .align(Alignment.TopCenter) - .offset(y = if (compact) 21.dp else 27.dp) - .size(width = if (compact) 7.dp else 9.dp, height = if (compact) 10.dp else 13.dp) + .offset(y = if (compact) 19.dp else 25.dp) + .size(width = if (compact) 8.dp else 10.dp, height = if (compact) 12.dp else 15.dp) .clip(RoundedCornerShape(2.dp)) .background(Color(0xFF10CFFF)) ) - Text("NA", color = red, fontSize = if (compact) 13.sp else 17.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow), modifier = Modifier.align(Alignment.TopCenter).offset(y = if (compact) 33.dp else 42.dp)) - Text("NA", color = red, fontSize = if (compact) 13.sp else 17.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow), modifier = Modifier.align(Alignment.CenterStart).offset(x = if (compact) 24.dp else 31.dp)) - Text("NA", color = red, fontSize = if (compact) 13.sp else 17.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow), modifier = Modifier.align(Alignment.CenterEnd).offset(x = if (compact) (-24).dp else (-31).dp)) - Text("NA", color = red, fontSize = if (compact) 13.sp else 17.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow), modifier = Modifier.align(Alignment.BottomCenter).offset(y = if (compact) (-25).dp else (-32).dp)) + ObstacleReadout(obstacles.front, "前方", compact, Modifier.align(Alignment.TopCenter).offset(y = if (compact) 31.dp else 40.dp)) + ObstacleReadout(obstacles.left, "左方", compact, Modifier.align(Alignment.CenterStart).offset(x = if (compact) 27.dp else 35.dp)) + ObstacleReadout(obstacles.right, "右方", compact, Modifier.align(Alignment.CenterEnd).offset(x = if (compact) (-27).dp else (-35).dp)) + ObstacleReadout(obstacles.back, "后方", compact, Modifier.align(Alignment.BottomCenter).offset(y = if (compact) (-28).dp else (-36).dp)) } } @@ -3713,30 +3155,12 @@ private fun AltitudeScale(compact: Boolean, modifier: Modifier = Modifier) { drawLine(Color(0xAAFFFFFF), Offset(0f, y), Offset(size.width, y), 1.5f) } } - Canvas(Modifier.size(width = if (compact) 16.dp else 20.dp, height = if (compact) 15.dp else 19.dp).align(Alignment.CenterStart).offset(x = if (compact) (-8).dp else (-10).dp)) { - repeat(3) { index -> - val y = index * size.height / 3f - drawLine( - color = PilotDanger, - start = Offset(size.width * 0.04f, y + size.height * 0.18f), - end = Offset(size.width * 0.45f, y), - strokeWidth = if (compact) 2.5f else 3.2f, - cap = StrokeCap.Square - ) - drawLine( - color = PilotOrange, - start = Offset(size.width * 0.50f, y + size.height * 0.18f), - end = Offset(size.width * 0.92f, y), - strokeWidth = if (compact) 2.5f else 3.2f, - cap = StrokeCap.Square - ) - } - } + Box(Modifier.width(if (compact) 12.dp else 16.dp).height(2.dp).align(Alignment.CenterStart).offset(x = (-4).dp).background(Color(0xFFADB8C6))) } } @Composable -private fun AltitudeReadout(value: String, aslValue: String, compact: Boolean, modifier: Modifier = Modifier) { +private fun AltitudeReadout(value: String, aslValue: String, climbSpeed: String, obstacles: InstrumentObstacles, compact: Boolean, modifier: Modifier = Modifier) { val red = Color(0xFFFF3333) Box(modifier) { Row( @@ -3744,25 +3168,25 @@ private fun AltitudeReadout(value: String, aslValue: String, compact: Boolean, m horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { - Text("NA", color = red, fontSize = if (compact) 11.sp else 14.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow)) - Text("0.0 VS", color = Color.White, fontSize = if (compact) 11.sp else 14.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow)) + ObstacleReadout(obstacles.up, "上方", compact) + Text("$climbSpeed VS", color = Color.White, fontSize = if (compact) 12.sp else 15.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow)) } Row( verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(if (compact) 4.dp else 6.dp), - modifier = Modifier.align(Alignment.CenterStart).padding(top = if (compact) 22.dp else 28.dp) + modifier = Modifier.align(Alignment.CenterStart).padding(top = if (compact) 18.dp else 24.dp) ) { Text( value, color = PilotGreen, - fontSize = if (compact) 25.sp else 32.sp, + fontSize = if (compact) 29.sp else 38.sp, fontWeight = FontWeight.Black, maxLines = 1, style = TextStyle(shadow = HudTextShadow) ) Column(horizontalAlignment = Alignment.Start) { - Text("ALT", color = PilotGreen, fontSize = if (compact) 9.sp else 11.sp, fontWeight = FontWeight.Black, lineHeight = if (compact) 9.sp else 11.sp, style = TextStyle(shadow = HudTextShadow)) - Text("m", color = PilotGreen, fontSize = if (compact) 8.sp else 10.sp, fontWeight = FontWeight.Black, lineHeight = if (compact) 8.sp else 10.sp, style = TextStyle(shadow = HudTextShadow)) + Text("ALT", color = PilotGreen, fontSize = if (compact) 10.sp else 12.sp, fontWeight = FontWeight.Black, lineHeight = if (compact) 10.sp else 12.sp, style = TextStyle(shadow = HudTextShadow)) + Text("m", color = PilotGreen, fontSize = if (compact) 9.sp else 11.sp, fontWeight = FontWeight.Black, lineHeight = if (compact) 9.sp else 11.sp, style = TextStyle(shadow = HudTextShadow)) } } Row( @@ -3770,8 +3194,8 @@ private fun AltitudeReadout(value: String, aslValue: String, compact: Boolean, m horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically ) { - Text("NA", color = red, fontSize = if (compact) 11.sp else 14.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow)) - Text(aslValue, color = Color.White, fontSize = if (compact) 11.sp else 14.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow)) + ObstacleReadout(obstacles.down, "下方", compact) + Text(aslValue, color = Color.White, fontSize = if (compact) 12.sp else 15.sp, fontWeight = FontWeight.Black, style = TextStyle(shadow = HudTextShadow)) } } } @@ -3867,14 +3291,14 @@ private fun RightCameraRail( modifier: Modifier = Modifier ) { Surface( - color = Color(0xE9000000), + color = Color.Transparent, shape = RoundedCornerShape(topStart = 10.dp, bottomStart = 10.dp), modifier = modifier .width(width) - .height(if (compact) 166.dp else 204.dp) + .height(if (compact) 148.dp else 174.dp) ) { Column( - modifier = Modifier.fillMaxSize().padding(vertical = if (compact) 6.dp else 10.dp), + modifier = Modifier.fillMaxSize().padding(vertical = if (compact) 4.dp else 6.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.SpaceEvenly ) { @@ -3897,7 +3321,7 @@ private fun RightCameraRail( Text( selectedAction.statusLabel(recording, laserFillLightEnabled), color = Color(0xFFBDBDBD), - fontSize = if (compact) 9.sp else 10.sp, + fontSize = if (compact) 8.sp else 9.sp, fontWeight = FontWeight.Bold, maxLines = 1, overflow = TextOverflow.Ellipsis, @@ -3995,15 +3419,18 @@ private fun CameraRailIcon( val content = if (enabled) tint else Color(0xFF7D8288) Column( modifier = Modifier - .size(width = if (compact) 54.dp else 64.dp, height = if (label == null) if (compact) 36.dp else 42.dp else if (compact) 56.dp else 68.dp) + .size(width = if (compact) 48.dp else 56.dp, height = if (label == null) if (compact) 34.dp else 40.dp else if (compact) 50.dp else 58.dp) + .clip(RoundedCornerShape(10.dp)) + .background(Color(0x7A0B1119)) + .border(1.dp, Color.White.copy(alpha = 0.10f), RoundedCornerShape(10.dp)) .clickable(enabled = enabled, onClick = onClick), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { - Icon(icon, null, tint = content, modifier = Modifier.size(if (compact) 24.dp else 30.dp)) + Icon(icon, null, tint = content, modifier = Modifier.size(if (compact) 22.dp else 26.dp)) if (label != null) { Spacer(Modifier.height(2.dp)) - Text(label, color = content, fontSize = if (compact) 10.sp else 12.sp, fontWeight = FontWeight.Black, maxLines = 1) + Text(label, color = content, fontSize = if (compact) 9.sp else 10.sp, fontWeight = FontWeight.Black, maxLines = 1) } } } @@ -4605,3 +4032,25 @@ private fun headingCardinal(heading: Double?): String { val index = ((normalized / 45.0).roundToInt()) % directions.size return directions[index] } + + +@Composable +private fun ObstacleReadout(distance: Double?, direction: String, compact: Boolean, modifier: Modifier = Modifier) { + val value = if (distance == null) "NA" else instrumentNumber(distance) + Text(value, color = if (distance == null) PilotDanger else Color.White, + fontSize = if (compact) 11.sp else 14.sp, fontWeight = FontWeight.Black, + style = TextStyle(shadow = HudTextShadow), maxLines = 1, + modifier = modifier.semantics { contentDescription = "$direction 障碍物距离 ${if (distance == null) "不可用" else "$value 米"}" }) +} + +@Preview(name = "Flight instrument · live sample", widthDp = 440, heightDp = 166) +@Composable +private fun FlightInstrumentPreview() { + Box(Modifier.fillMaxSize().background(Color(0xFF65716B)), contentAlignment = Alignment.Center) { + FlightInstrumentDashboard( + FlightInstrumentReadings(relativeAltitude = 30.0, seaLevelAltitude = 116.8, heading = 189.0, + horizontalSpeed = 0.0, climbSpeed = 0.0, windSpeed = 2.3, gimbalPitch = -12.0), + compact = true, modifier = Modifier.fillMaxSize().padding(12.dp) + ) + } +} diff --git a/sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentObserver.kt b/sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentObserver.kt new file mode 100644 index 0000000..5aa3b06 --- /dev/null +++ b/sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentObserver.kt @@ -0,0 +1,89 @@ +package com.zklh.dronecontroller.ui + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.remember +import dji.sdk.keyvalue.key.DJIKey +import dji.sdk.keyvalue.key.FlightControllerKey +import dji.sdk.keyvalue.key.GimbalKey +import dji.sdk.keyvalue.key.KeyTools +import dji.sdk.keyvalue.value.common.ComponentIndexType +import dji.v5.manager.KeyManager +import dji.v5.manager.aircraft.perception.PerceptionManager +import dji.v5.manager.aircraft.perception.data.ObstacleData +import dji.v5.manager.aircraft.perception.data.PerceptionInfo +import dji.v5.manager.aircraft.perception.listener.ObstacleDataListener +import dji.v5.manager.aircraft.perception.listener.PerceptionInformationListener +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.update + +/** Independent read-only UI subscriptions; never enables control or changes aircraft settings. */ +@Composable +internal fun rememberFlightInstrumentReadings(connected: Boolean, cameraComponent: String): FlightInstrumentReadings { + val readings = remember(connected, cameraComponent) { MutableStateFlow(FlightInstrumentReadings()) } + DisposableEffect(connected, cameraComponent) { + val owner = Any() + var active = true + val lock = Any() + var obstacleData: ObstacleData? = null + var perceptionInfo: PerceptionInfo? = null + fun updateObstacles() { + val info = perceptionInfo + val data = obstacleData + val sectors = instrumentObstacleSectors(data?.horizontalObstacleDistance?.toList().orEmpty(), data?.horizontalAngleInterval ?: 0) + val obstacles = InstrumentObstacles( + front = sectors[0].takeIf { info?.forwardObstacleAvoidanceWorking == true }, + right = sectors[1].takeIf { info?.rightSideObstacleAvoidanceWorking == true }, + back = sectors[2].takeIf { info?.backwardObstacleAvoidanceWorking == true }, + left = sectors[3].takeIf { info?.leftSideObstacleAvoidanceWorking == true }, + up = instrumentObstacleDistance(data?.upwardObstacleDistance).takeIf { info?.upwardObstacleAvoidanceWorking == true }, + down = instrumentObstacleDistance(data?.downwardObstacleDistance).takeIf { info?.downwardObstacleAvoidanceWorking == true } + ) + readings.update { it.copy(obstacles = obstacles) } + } + val perception = if (connected) runCatching { PerceptionManager.getInstance() }.getOrNull() else null + val obstacleListener = ObstacleDataListener { data -> + synchronized(lock) { + if (active) { obstacleData = data; updateObstacles() } + } + } + val perceptionListener = PerceptionInformationListener { info -> + synchronized(lock) { + if (active) { perceptionInfo = info; updateObstacles() } + } + } + fun observe(key: DJIKey, change: FlightInstrumentReadings.(T?) -> FlightInstrumentReadings) { + // Unsupported keys keep null instead of masquerading as zero. + runCatching { + KeyManager.getInstance().listen(key, owner, true) { _, value -> + synchronized(lock) { + if (active) readings.update { it.change(value) } + } + } + } + } + if (connected) { + runCatching { perception?.addPerceptionInformationListener(perceptionListener) } + runCatching { perception?.addObstacleDataListener(obstacleListener) } + observe(KeyTools.createKey(FlightControllerKey.KeyAltitude)) { copy(relativeAltitude = it) } + observe(KeyTools.createKey(FlightControllerKey.KeyHeightAboveSeaLevel)) { copy(seaLevelAltitude = it?.height) } + observe(KeyTools.createKey(FlightControllerKey.KeyCompassHeading)) { copy(heading = instrumentHeading(it)) } + observe(KeyTools.createKey(FlightControllerKey.KeyAircraftVelocity)) { + copy(horizontalSpeed = it?.let { v -> instrumentHorizontalSpeed(v.x, v.y) }, climbSpeed = instrumentClimbSpeed(it?.z)) + } + observe(KeyTools.createKey(FlightControllerKey.KeyWindSpeed)) { copy(windSpeed = instrumentWindSpeed(it)) } + val component = ComponentIndexType.values().firstOrNull { it.name == cameraComponent } ?: ComponentIndexType.LEFT_OR_MAIN + observe(KeyTools.createKey(GimbalKey.KeyGimbalAttitude, component)) { copy(gimbalPitch = it?.pitch) } + } + onDispose { + synchronized(lock) { active = false } + runCatching { KeyManager.getInstance().cancelListen(owner) } + runCatching { perception?.removeObstacleDataListener(obstacleListener) } + runCatching { perception?.removePerceptionInformationListener(perceptionListener) } + } + } + val current by readings.collectAsState() + return current +} diff --git a/sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentReadings.kt b/sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentReadings.kt new file mode 100644 index 0000000..483f05f --- /dev/null +++ b/sample/src/main/java/com/zklh/dronecontroller/ui/FlightInstrumentReadings.kt @@ -0,0 +1,56 @@ +package com.zklh.dronecontroller.ui + +import java.util.Locale +import kotlin.math.hypot + +/** Presentation values only. Null means the SDK has not supplied a usable reading. */ +internal data class FlightInstrumentReadings( + val relativeAltitude: Double? = null, + val seaLevelAltitude: Double? = null, + val heading: Double? = null, + val horizontalSpeed: Double? = null, + val climbSpeed: Double? = null, + val windSpeed: Double? = null, + val gimbalPitch: Double? = null, + val obstacles: InstrumentObstacles = InstrumentObstacles() +) + +internal data class InstrumentObstacles( + val front: Double? = null, + val right: Double? = null, + val back: Double? = null, + val left: Double? = null, + val up: Double? = null, + val down: Double? = null +) + +internal fun instrumentObstacleDistance(millimeters: Int?): Double? = + millimeters?.takeIf { it > 0 && it < Int.MAX_VALUE }?.div(1000.0) + +/** Body-relative quadrants: SDK/UXSDK start at the nose and proceed clockwise. */ +internal fun instrumentObstacleSectors(distances: List, interval: Int): List { + if (interval !in 1..90 || distances.size.toLong() * interval != 360L) return List(4) { null } + val sectors = MutableList(4) { null } + distances.forEachIndexed { index, distance -> + val sector = ((index * interval + 45) % 360) / 90 + val meters = instrumentObstacleDistance(distance) + if (meters != null && (sectors[sector] == null || meters < sectors[sector]!!)) sectors[sector] = meters + } + return sectors +} + +internal fun instrumentNumber(value: Double?, decimals: Int = 1): String = + value?.takeIf { it.isFinite() }?.let { + // Avoid displaying negative zero when the aircraft is stationary. + String.format(Locale.US, "%.${decimals}f", if (it == 0.0) 0.0 else it) + } ?: "—" + +internal fun instrumentHeading(value: Double?): Double? = + value?.takeIf { it.isFinite() }?.let { ((it % 360.0) + 360.0) % 360.0 } + +internal fun instrumentHorizontalSpeed(x: Double, y: Double): Double? = + hypot(x, y).takeIf { it.isFinite() } + +// DJI SpeedDisplayWidget uses dm/s; VerticalVelocityWidget uses negative Z for ascent. +internal fun instrumentWindSpeed(raw: Int?): Double? = raw?.takeIf { it >= 0 }?.div(10.0) +internal fun instrumentClimbSpeed(z: Double?): Double? = z?.takeIf { it.isFinite() }?.let { -it } diff --git a/sample/src/main/java/com/zklh/dronecontroller/ui/PilotHomeScreen.kt b/sample/src/main/java/com/zklh/dronecontroller/ui/PilotHomeScreen.kt new file mode 100644 index 0000000..b13eb69 --- /dev/null +++ b/sample/src/main/java/com/zklh/dronecontroller/ui/PilotHomeScreen.kt @@ -0,0 +1,195 @@ +package com.zklh.dronecontroller.ui + +import androidx.compose.foundation.BorderStroke +import androidx.compose.foundation.Canvas +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.KeyboardArrowRight +import androidx.compose.material.icons.filled.* +import androidx.compose.material3.* +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.StrokeCap +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.zklh.dronecontroller.core.msdk.DroneSdkState +import com.zklh.dronecontroller.core.telemetry.TelemetrySnapshot + +private val HomeBackground = Color(0xFF090D12) +private val HomeCardColor = Color(0xE6151C24) +private val HomeSecondary = Color(0xFFA2AFBF) +private val HomeAccent = Color(0xFF3C91FF) +private val HomeOnline = Color(0xFF42D6AC) + +@Composable +internal fun PilotHomeScreen( + sdkState: DroneSdkState, + telemetry: TelemetrySnapshot, + commandMessage: String, + cloudConnected: Boolean, + cloudTenantName: String, + cloudUsername: String, + onOpenCloud: () -> Unit, + onOpenPreflight: () -> Unit, + onOpenMission: () -> Unit, + onEnterFlight: () -> Unit, + onRetryConnect: () -> Unit +) { + BoxWithConstraints(Modifier.fillMaxSize().background(HomeBackground)) { + val compact = maxHeight < 560.dp + val pad = if (compact) 14.dp else 20.dp + val gap = if (compact) 10.dp else 14.dp + Column(Modifier.fillMaxSize().padding(pad), verticalArrangement = Arrangement.spacedBy(gap)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Filled.Navigation, null, tint = HomeAccent, modifier = Modifier.size(26.dp)) + Spacer(Modifier.width(10.dp)) + Text("飞行工作台", color = Color.White, fontWeight = FontWeight.Bold, fontSize = if (compact) 20.sp else 24.sp) + Spacer(Modifier.weight(1f)) + HomeConnectionBadge(if (sdkState.productLinkConnected) "遥控器已连接" else "等待连接遥控器", sdkState.productLinkConnected) + } + Row(Modifier.weight(1f).fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(gap)) { + Surface(color = HomeCardColor, shape = RoundedCornerShape(8.dp), border = BorderStroke(1.dp, Color.White.copy(alpha = 0.08f)), modifier = Modifier.weight(0.62f).fillMaxHeight()) { + Column(Modifier.padding(pad).verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(gap)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Text("当前设备", color = HomeSecondary, fontSize = 13.sp) + Spacer(Modifier.weight(1f)) + HomeConnectionBadge(if (sdkState.productConnected) "已连接" else "未连接", sdkState.productConnected) + } + Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxWidth()) { + AircraftIllustration(Modifier.weight(0.42f).height(if (compact) 96.dp else 142.dp)) + Column(Modifier.weight(0.54f).padding(start = 12.dp), verticalArrangement = Arrangement.spacedBy(7.dp)) { + Text( + if (sdkState.productConnected) telemetry.productType.replace('_', ' ').ifBlank { "无人机已连接" } else "等待无人机连接", + color = Color.White, fontSize = if (compact) 20.sp else 26.sp, fontWeight = FontWeight.Bold, + maxLines = 2, overflow = TextOverflow.Ellipsis + ) + Text("SN ${sdkState.aircraftSerialNumber.ifBlank { "—" }}", color = HomeSecondary, fontSize = 11.sp, maxLines = 1, overflow = TextOverflow.Ellipsis) + Text(telemetry.remoteControllerType.replace('_', ' ').ifBlank { "遥控器" }, color = HomeSecondary, fontSize = 12.sp, maxLines = 1, overflow = TextOverflow.Ellipsis) + } + } + HorizontalDivider(color = Color.White.copy(alpha = 0.07f)) + Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) { + HomeMetric(Icons.Filled.BatteryFull, "飞行器电量", if (sdkState.productConnected && telemetry.batteryPercent in 0..100) "${telemetry.batteryPercent}%" else "—") + HomeMetric(Icons.Filled.MyLocation, "卫星数", if (sdkState.productConnected && telemetry.gpsValid) telemetry.gpsSatelliteCount.toString() else "—") + HomeMetric(Icons.Filled.Radio, "遥控链路", if (sdkState.productLinkConnected) "已连接" else "未连接") + } + Row(horizontalArrangement = Arrangement.spacedBy(10.dp), modifier = Modifier.fillMaxWidth()) { + OutlinedButton(onClick = onOpenPreflight, shape = RoundedCornerShape(8.dp), border = BorderStroke(1.dp, Color(0xFF384659)), colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.White), modifier = Modifier.weight(1f).height(46.dp)) { + Icon(Icons.Filled.Shield, null, modifier = Modifier.size(18.dp)) + Spacer(Modifier.width(7.dp)) + Text("飞前检查", maxLines = 1) + } + Button(onClick = { if (!sdkState.productConnected) onRetryConnect(); onEnterFlight() }, shape = RoundedCornerShape(8.dp), colors = ButtonDefaults.buttonColors(containerColor = HomeAccent), modifier = Modifier.weight(1.35f).height(46.dp)) { + Text("进入飞行", fontWeight = FontWeight.Bold, fontSize = 16.sp) + Spacer(Modifier.width(12.dp)) + Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, null) + } + } + } + } + Column(Modifier.weight(0.38f).fillMaxHeight().verticalScroll(rememberScrollState()), verticalArrangement = Arrangement.spacedBy(gap)) { + Surface(color = HomeCardColor, shape = RoundedCornerShape(8.dp), border = BorderStroke(1.dp, Color.White.copy(alpha = 0.08f))) { + Column(Modifier.fillMaxWidth().padding(pad), verticalArrangement = Arrangement.spacedBy(if (compact) 12.dp else 20.dp)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Filled.Cloud, null, tint = HomeAccent) + Spacer(Modifier.width(9.dp)) + Text("云平台", color = Color.White, fontWeight = FontWeight.Bold, fontSize = 18.sp) + Spacer(Modifier.weight(1f)) + Text(if (cloudConnected) "已连接" else "未连接", color = if (cloudConnected) HomeOnline else HomeSecondary, fontSize = 12.sp) + } + HomeDetail("租户", cloudTenantName.ifBlank { "未配置" }) + HomeDetail("账号", cloudUsername.ifBlank { "未配置" }) + OutlinedButton(onClick = onOpenCloud, shape = RoundedCornerShape(8.dp), border = BorderStroke(1.dp, Color(0xFF384659)), colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.White), modifier = Modifier.fillMaxWidth().height(42.dp)) { + Text("管理云平台") + Spacer(Modifier.weight(1f)) + Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, null) + } + } + } + Surface(onClick = onOpenMission, color = HomeCardColor, shape = RoundedCornerShape(8.dp), border = BorderStroke(1.dp, Color.White.copy(alpha = 0.08f))) { + Row(Modifier.fillMaxWidth().padding(pad), verticalAlignment = Alignment.CenterVertically) { + Icon(Icons.Filled.Route, null, tint = HomeAccent, modifier = Modifier.size(30.dp)) + Column(Modifier.weight(1f).padding(horizontal = 12.dp), verticalArrangement = Arrangement.spacedBy(5.dp)) { + Text("航线任务", color = Color.White, fontWeight = FontWeight.Bold, fontSize = 17.sp) + Text("导入 · 上传 · 执行", color = HomeSecondary, fontSize = 11.sp) + } + Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, null, tint = HomeSecondary) + } + } + } + } + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(8.dp)) { + Icon(Icons.Filled.Radio, null, tint = HomeSecondary, modifier = Modifier.size(16.dp)) + Text(commandMessage.ifBlank { if (sdkState.isRegistered) "设备服务已就绪" else "正在初始化设备服务" }, color = HomeSecondary, fontSize = 11.sp, maxLines = 1, overflow = TextOverflow.Ellipsis, modifier = Modifier.weight(1f)) + if (!sdkState.productConnected) { + TextButton(onClick = onRetryConnect, contentPadding = PaddingValues(horizontal = 8.dp, vertical = 0.dp), modifier = Modifier.height(32.dp)) { Text("重试连接", color = HomeAccent, fontSize = 12.sp) } + } + } + } + } +} + +@Composable +private fun HomeConnectionBadge(text: String, online: Boolean) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(6.dp)) { + Box(Modifier.size(6.dp).background(if (online) HomeOnline else HomeSecondary, CircleShape)) + Text(text, color = if (online) HomeOnline else HomeSecondary, fontSize = 12.sp) + } +} + +@Composable +private fun HomeMetric(icon: ImageVector, label: String, value: String) { + Column(verticalArrangement = Arrangement.spacedBy(6.dp)) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(4.dp)) { + Icon(icon, null, tint = HomeSecondary, modifier = Modifier.size(14.dp)) + Text(label, color = HomeSecondary, fontSize = 11.sp) + } + Text(value, color = Color.White, fontSize = 20.sp, fontWeight = FontWeight.Medium) + } +} + +@Composable +private fun HomeDetail(label: String, value: String) { + Column(verticalArrangement = Arrangement.spacedBy(5.dp)) { + Text(label, color = HomeSecondary, fontSize = 11.sp) + Text(value, color = Color.White, fontSize = 15.sp, maxLines = 1, overflow = TextOverflow.Ellipsis) + } +} + +@Composable +private fun AircraftIllustration(modifier: Modifier) { + Canvas(modifier) { + val c = center + val radius = size.minDimension * 0.43f + drawCircle(HomeAccent.copy(alpha = 0.04f), radius, c) + drawCircle(HomeAccent.copy(alpha = 0.14f), radius, c, style = Stroke(1.dp.toPx())) + listOf(Offset(-0.7f, -0.6f), Offset(0.7f, -0.6f), Offset(-0.7f, 0.6f), Offset(0.7f, 0.6f)).forEach { direction -> + val rotor = c + direction * radius + drawLine(Color(0xFF7C8A9B), c, rotor, radius * 0.12f, StrokeCap.Round) + drawCircle(Color(0xFFACBAC9), radius * 0.14f, rotor) + drawLine(Color(0xFFDAE3ED), rotor - Offset(radius * 0.26f, radius * 0.08f), rotor + Offset(radius * 0.26f, radius * 0.08f), radius * 0.06f, StrokeCap.Round) + } + drawOval(Color(0xFFB6C2D0), topLeft = c - Offset(radius * 0.23f, radius * 0.40f), size = androidx.compose.ui.geometry.Size(radius * 0.46f, radius * 0.8f)) + drawCircle(Color(0xFF233449), radius * 0.13f, c + Offset(0f, radius * 0.35f)) + drawCircle(HomeAccent, radius * 0.055f, c + Offset(0f, radius * 0.35f)) + } +} + +@Preview(name = "Home · RC landscape", widthDp = 960, heightDp = 540) +@Composable +private fun PilotHomePreview() { + PilotHomeScreen(DroneSdkState(), TelemetrySnapshot(), "等待连接无人机", false, "", "", {}, {}, {}, {}, {}) +} diff --git a/sample/src/main/java/com/zklh/dronecontroller/ui/PilotPreflightScreen.kt b/sample/src/main/java/com/zklh/dronecontroller/ui/PilotPreflightScreen.kt new file mode 100644 index 0000000..7650815 --- /dev/null +++ b/sample/src/main/java/com/zklh/dronecontroller/ui/PilotPreflightScreen.kt @@ -0,0 +1,74 @@ +package com.zklh.dronecontroller.ui + +import androidx.compose.foundation.background +import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Close +import androidx.compose.material3.* +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.zklh.dronecontroller.core.msdk.DroneSdkState +import com.zklh.dronecontroller.core.telemetry.TelemetrySnapshot +import com.zklh.dronecontroller.core.telemetry.hasReliableRtkPosition + +/** Read-only checks. The former placeholder sliders never wrote settings to the aircraft. */ +@Composable +internal fun PilotPreflightScreen(sdkState: DroneSdkState, telemetry: TelemetrySnapshot, onClose: () -> Unit) { + val connected = sdkState.productConnected + fun reading(value: String) = if (connected) value else "—" + Column(Modifier.fillMaxSize().background(Color(0xFF0D1118)).padding(20.dp)) { + Row(verticalAlignment = Alignment.CenterVertically) { + Column(Modifier.weight(1f)) { + Text("飞前检查", color = Color.White, fontSize = 24.sp, fontWeight = FontWeight.Bold) + Text("设备状态", color = Color(0xFF9CABB9), fontSize = 12.sp) + } + IconButton(onClick = onClose) { Icon(Icons.Filled.Close, "关闭飞前检查", tint = Color.White) } + } + Spacer(Modifier.height(18.dp)) + Row(Modifier.weight(1f).verticalScroll(rememberScrollState()), horizontalArrangement = Arrangement.spacedBy(16.dp)) { + CheckCard("连接与定位", listOf( + "飞机连接" to if (connected) "已连接" else "未连接", + "遥控链路" to if (sdkState.productLinkConnected) "已连接" else "未连接", + "卫星数" to reading(if (telemetry.gpsValid) telemetry.gpsSatelliteCount.toString() else "定位不可用"), + "返航点" to reading(if (telemetry.homeLocationValid) "已记录" else "未取得"), + "RTK" to reading(if (telemetry.hasReliableRtkForCheck()) "定位可用" else if (telemetry.rtkEnabled) "等待定位" else "未启用") + ), Modifier.weight(1f)) + CheckCard("电池与设备", listOf( + "飞行器电量" to reading(if (telemetry.batteryPercent in 0..100) "${telemetry.batteryPercent}%" else "—"), + "遥控器电量" to if (sdkState.productLinkConnected && telemetry.rcBatteryPercent in 0..100) "${telemetry.rcBatteryPercent}%" else "—", + "电池温度" to reading(if (telemetry.batteryTemperatureKnown) "${instrumentNumber(telemetry.batteryTemperature)} °C" else "—"), + "预计剩余飞行时间" to reading(if (telemetry.remainingFlightTimeKnown) "${telemetry.remainingFlightTime / 60} 分钟" else "—"), + "固件版本" to reading(telemetry.firmwareVersion.ifBlank { "—" }) + ), Modifier.weight(1f)) + } + Spacer(Modifier.height(12.dp)) + Text(sdkState.registerError ?: sdkState.aircraftConnectionDetail, color = Color(0xFF9CABB9), fontSize = 12.sp) + Text("起飞前请检查机臂、桨叶及周围环境,并确认返航与避障设置。", color = Color(0xFF9CABB9), fontSize = 12.sp, modifier = Modifier.padding(top = 8.dp)) + } +} + +private fun TelemetrySnapshot.hasReliableRtkForCheck() = + hasReliableRtkPosition() + +@Composable +private fun CheckCard(title: String, rows: List>, modifier: Modifier) { + Surface(color = Color(0xFF171F2A), shape = RoundedCornerShape(16.dp), modifier = modifier) { + Column(Modifier.padding(18.dp), verticalArrangement = Arrangement.spacedBy(16.dp)) { + Text(title, color = Color.White, fontSize = 18.sp, fontWeight = FontWeight.Bold) + rows.forEach { (label, value) -> + Row(horizontalArrangement = Arrangement.spacedBy(12.dp), verticalAlignment = Alignment.CenterVertically) { + Text(label, color = Color(0xFF9CABB9), fontSize = 13.sp, modifier = Modifier.weight(1f)) + Text(value, color = Color.White, fontSize = 13.sp, modifier = Modifier.weight(1f)) + } + } + } + } +} diff --git a/sample/src/test/java/com/zklh/dronecontroller/ui/FlightInstrumentReadingsTest.kt b/sample/src/test/java/com/zklh/dronecontroller/ui/FlightInstrumentReadingsTest.kt new file mode 100644 index 0000000..4d753b0 --- /dev/null +++ b/sample/src/test/java/com/zklh/dronecontroller/ui/FlightInstrumentReadingsTest.kt @@ -0,0 +1,50 @@ +package com.zklh.dronecontroller.ui + +import org.junit.Assert.* +import org.junit.Test + +class FlightInstrumentReadingsTest { + @Test fun missingReadingsAreDifferentFromStationaryReadings() { + assertEquals("—", instrumentNumber(null)) + assertEquals("—", instrumentNumber(Double.NaN)) + assertEquals("—", instrumentNumber(Double.POSITIVE_INFINITY)) + assertEquals("0.0", instrumentNumber(0.0)) + assertEquals("0.0", instrumentNumber(-0.0)) + } + + @Test fun heightsKeepTheirOwnReferenceAndAllowFlightBelowTakeoffPoint() { + val readings = FlightInstrumentReadings(relativeAltitude = -12.3, seaLevelAltitude = 116.8) + assertEquals("-12.3", instrumentNumber(readings.relativeAltitude)) + assertEquals("116.8", instrumentNumber(readings.seaLevelAltitude)) + assertEquals("—", instrumentNumber(readings.copy(seaLevelAltitude = null).seaLevelAltitude)) + } + + @Test fun djiWindAndVelocityUnitsMatchTheDisplayedUnits() { + assertEquals(3.7, instrumentWindSpeed(37)!!, 0.001) + assertNull(instrumentWindSpeed(null)) + assertNull(instrumentWindSpeed(-1)) + assertEquals(5.0, instrumentHorizontalSpeed(3.0, -4.0)!!, 0.001) + assertEquals(2.4, instrumentClimbSpeed(-2.4)!!, 0.001) + assertEquals(-1.7, instrumentClimbSpeed(1.7)!!, 0.001) + assertNull(instrumentClimbSpeed(Double.NaN)) + } + + @Test fun headingWrapsAtNorth() { + assertEquals(359.0, instrumentHeading(-1.0)!!, 0.001) + assertEquals(0.0, instrumentHeading(360.0)!!, 0.001) + assertEquals(90.0, instrumentHeading(450.0)!!, 0.001) + assertNull(instrumentHeading(null)) + assertNull(instrumentHeading(Double.NaN)) + } + + @Test fun obstacleSectorsKeepNoseRightTailLeftAndConvertMillimeters() { + // 45-degree samples: the final sample wraps back into the forward quadrant. + val sectors = instrumentObstacleSectors(listOf(6000, 4000, 5000, 9000, 8000, 7000, 10000, 3000), 45) + assertEquals(listOf(3.0, 4.0, 8.0, 7.0), sectors) + assertEquals(List(4) { null }, instrumentObstacleSectors(listOf(1000), 1)) + assertEquals(List(4) { null }, instrumentObstacleSectors(emptyList(), 0)) + assertEquals(List(4) { null }, instrumentObstacleSectors(List(4) { Int.MAX_VALUE }, 90)) + assertNull(instrumentObstacleDistance(0)) + assertNull(instrumentObstacleDistance(-1)) + } +}