feat(client): 同步每路镜像并精简操控入口与 HUD 映射
This commit is contained in:
28 files changed
+503
-62
No files matched your search
@@ -234,9 +234,7 @@ pub fn set_role(
|
||||
/// 左右镜像。摄像头装反了才需要,不是判罚相关的东西,所以不设密码。
|
||||
#[tauri::command]
|
||||
pub fn set_mirror(mirror: bool, state: Shared<'_>) -> Result<ConfigView, String> {
|
||||
state
|
||||
.update_config(move |config| config.mirror = mirror)
|
||||
.map_err(|err| format!("{err}"))?;
|
||||
state.set_camera_mirror(None, Some(mirror), None)?;
|
||||
|
||||
Ok(ConfigView::from(&state.config()))
|
||||
}
|
||||
@@ -244,13 +242,21 @@ pub fn set_mirror(mirror: bool, state: Shared<'_>) -> Result<ConfigView, String>
|
||||
/// 上下镜像,独立保存,不改动左右镜像。
|
||||
#[tauri::command]
|
||||
pub fn set_mirror_vertical(mirror_vertical: bool, state: Shared<'_>) -> Result<ConfigView, String> {
|
||||
state
|
||||
.update_config(move |config| config.mirror_vertical = mirror_vertical)
|
||||
.map_err(|err| format!("{err}"))?;
|
||||
state.set_camera_mirror(None, None, Some(mirror_vertical))?;
|
||||
|
||||
Ok(ConfigView::from(&state.config()))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn set_camera_mirror(
|
||||
camera: u32,
|
||||
mirror: bool,
|
||||
mirror_vertical: bool,
|
||||
state: Shared<'_>,
|
||||
) -> Result<(), String> {
|
||||
state.set_camera_mirror(Some(camera), Some(mirror), Some(mirror_vertical))
|
||||
}
|
||||
|
||||
/// 保存 HUD 设置(血条、瞄准器、网格、命中反馈)。
|
||||
#[tauri::command]
|
||||
pub fn save_hud(hud: HudConfig, state: Shared<'_>) -> Result<ConfigView, String> {
|
||||
|
||||
@@ -344,6 +344,7 @@ fn run() -> Result<(), Box<dyn Error>> {
|
||||
commands::set_team,
|
||||
commands::set_role,
|
||||
commands::set_mirror,
|
||||
commands::set_camera_mirror,
|
||||
commands::set_mirror_vertical,
|
||||
commands::save_hud,
|
||||
commands::set_team_name,
|
||||
|
||||
@@ -60,6 +60,8 @@ pub struct AppState {
|
||||
match_socket: Option<std::net::UdpSocket>,
|
||||
match_error: Mutex<Option<String>>,
|
||||
match_changed: tokio::sync::Notify,
|
||||
mirror_inbox: Mutex<cam_core::camera_mirror::CameraMirrors>,
|
||||
mirror_origin: u64,
|
||||
operator: Mutex<OperatorState>,
|
||||
operator_socket: Option<std::net::UdpSocket>,
|
||||
operator_focused: AtomicBool,
|
||||
@@ -90,6 +92,125 @@ pub struct AppState {
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
fn mirror_targets(&self) -> Vec<u32> {
|
||||
let config = self.config();
|
||||
if config.role == Role::Team {
|
||||
config.camera_id.into_iter().collect()
|
||||
} else {
|
||||
self.slots
|
||||
.lock()
|
||||
.expect("slots poisoned")
|
||||
.slots()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_camera_mirror(
|
||||
&self,
|
||||
camera: Option<u32>,
|
||||
horizontal: Option<bool>,
|
||||
vertical: Option<bool>,
|
||||
) -> Result<(), String> {
|
||||
let targets = self.mirror_targets();
|
||||
let ids = match camera {
|
||||
Some(id) if targets.contains(&id) => vec![id],
|
||||
Some(_) => return Err("只能修改已绑定或当前槽位的图传板".into()),
|
||||
None => targets,
|
||||
};
|
||||
let mut config = self.config.lock().expect("config poisoned");
|
||||
let revision = config
|
||||
.camera_mirrors
|
||||
.values()
|
||||
.map(|v| v.revision)
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
.checked_add(1)
|
||||
.ok_or("镜像修订号已用尽")?;
|
||||
for id in ids {
|
||||
let old = config.camera_mirrors.get(&id.to_string());
|
||||
let packet = protocol::CameraMirror {
|
||||
camera_id: id,
|
||||
revision,
|
||||
origin: self.mirror_origin,
|
||||
mirror: horizontal.unwrap_or(old.map_or(config.mirror, |v| v.mirror)),
|
||||
mirror_vertical: vertical
|
||||
.unwrap_or(old.map_or(config.mirror_vertical, |v| v.mirror_vertical)),
|
||||
};
|
||||
cam_core::camera_mirror::merge(&mut config.camera_mirrors, packet);
|
||||
}
|
||||
if camera.is_none() || config.role == Role::Team {
|
||||
if let Some(v) = horizontal {
|
||||
config.mirror = v;
|
||||
}
|
||||
if let Some(v) = vertical {
|
||||
config.mirror_vertical = v;
|
||||
}
|
||||
}
|
||||
config
|
||||
.save(&self.config_path)
|
||||
.map_err(|e| format!("镜像已在本次运行生效,但保存失败:{e}"))?;
|
||||
drop(config);
|
||||
self.tick_mirrors();
|
||||
self.push_snapshot();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn tick_mirrors(&self) {
|
||||
let targets = self.mirror_targets();
|
||||
let inbox = std::mem::take(&mut *self.mirror_inbox.lock().expect("mirror inbox poisoned"));
|
||||
let mut config = self.config.lock().expect("config poisoned");
|
||||
let mut changed = false;
|
||||
for (id, value) in inbox {
|
||||
if let Ok(id) = id.parse::<u32>() {
|
||||
if targets.contains(&id) {
|
||||
changed |= cam_core::camera_mirror::merge(
|
||||
&mut config.camera_mirrors,
|
||||
value.packet(id),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
if config.role == Role::Team {
|
||||
if let Some(value) = config
|
||||
.camera_id
|
||||
.and_then(|id| config.camera_mirrors.get(&id.to_string()))
|
||||
.cloned()
|
||||
{
|
||||
config.mirror = value.mirror;
|
||||
config.mirror_vertical = value.mirror_vertical;
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
if let Err(error) = config.save(&self.config_path) {
|
||||
log::warn!("镜像同步保存失败:{error}");
|
||||
}
|
||||
}
|
||||
let packets: Vec<_> = targets
|
||||
.into_iter()
|
||||
.filter_map(|id| {
|
||||
config
|
||||
.camera_mirrors
|
||||
.get(&id.to_string())
|
||||
.map(|v| v.packet(id))
|
||||
})
|
||||
.collect();
|
||||
drop(config);
|
||||
if !self.demo {
|
||||
if let Some(socket) = &self.match_socket {
|
||||
for packet in packets {
|
||||
if let Some(bytes) = packet.encode() {
|
||||
let _ = socket.send_to(
|
||||
&bytes,
|
||||
(std::net::Ipv4Addr::BROADCAST, protocol::ARMOR_PORT),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn operator_context(&self) -> OperatorContext {
|
||||
let config = self.config();
|
||||
let clock = self.match_view();
|
||||
@@ -432,6 +553,8 @@ impl AppState {
|
||||
match_socket,
|
||||
match_error: Mutex::new(match_error),
|
||||
match_changed: tokio::sync::Notify::new(),
|
||||
mirror_inbox: Mutex::new(Default::default()),
|
||||
mirror_origin: session.max(1),
|
||||
operator: Mutex::new(OperatorState::new(session)),
|
||||
operator_socket,
|
||||
operator_focused: AtomicBool::new(true),
|
||||
@@ -462,6 +585,17 @@ impl AppState {
|
||||
///
|
||||
/// 必须在 `Arc<AppState>` 建好之后调用(sink 里要持有 state 的通道)。
|
||||
pub fn install_sinks(self: &Arc<Self>) {
|
||||
let weak = Arc::downgrade(self);
|
||||
self.armor.set_mirror_sink(Arc::new(move |packet| {
|
||||
if let Some(state) = weak.upgrade() {
|
||||
if !state.demo && state.mirror_targets().contains(&packet.camera_id) {
|
||||
cam_core::camera_mirror::merge(
|
||||
&mut state.mirror_inbox.lock().expect("mirror inbox poisoned"),
|
||||
packet,
|
||||
);
|
||||
}
|
||||
}
|
||||
}));
|
||||
let weak = Arc::downgrade(self);
|
||||
self.armor.set_match_sink(Arc::new(move |packet, ip| {
|
||||
if let Some(state) = weak.upgrade() {
|
||||
@@ -848,6 +982,7 @@ impl AppState {
|
||||
}
|
||||
|
||||
pub fn tick(&self) {
|
||||
self.tick_mirrors();
|
||||
self.tick_match();
|
||||
let online: Vec<u32> = self
|
||||
.discovery
|
||||
@@ -958,12 +1093,19 @@ impl AppState {
|
||||
.apply_outcomes(&mut health);
|
||||
|
||||
Snapshot {
|
||||
camera_mirrors: config.camera_mirrors.clone(),
|
||||
operator_control: self.operator.lock().expect("operator poisoned").view(),
|
||||
match_clock: self.match_view(),
|
||||
role: config.role,
|
||||
team: config.team,
|
||||
mirrored: config.mirror,
|
||||
mirrored_vertical: config.mirror_vertical,
|
||||
mirrored: config
|
||||
.camera_id
|
||||
.and_then(|id| config.camera_mirrors.get(&id.to_string()))
|
||||
.map_or(config.mirror, |v| v.mirror),
|
||||
mirrored_vertical: config
|
||||
.camera_id
|
||||
.and_then(|id| config.camera_mirrors.get(&id.to_string()))
|
||||
.map_or(config.mirror_vertical, |v| v.mirror_vertical),
|
||||
demo: self.demo,
|
||||
diagnostics,
|
||||
unlocked: self.is_unlocked(),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://schema.tauri.app/config/2",
|
||||
"productName": "RMIC-2026",
|
||||
"version": "0.5.0",
|
||||
"version": "0.6.0",
|
||||
"identifier": "com.simbarobotics.rmic2026",
|
||||
"build": {
|
||||
"beforeDevCommand": "pnpm dev",
|
||||
|
||||
Reference in new issue
Block a user