Add animated force panels

This commit is contained in:
lenn
2026-06-30 11:10:29 +08:00
parent d4f160af75
commit 444c20f233
2 changed files with 257 additions and 88 deletions

View File

@@ -13,14 +13,16 @@ use crate::{
},
ui::{
ConfigPanelState, ConnectPanelState, FloatingPanelState, MatrixConfigState,
draw_config_panel, draw_export_panel, draw_matrix_config_panel, draw_stats_panel,
panel_restore_item,
draw_config_panel, draw_export_panel, draw_hand_force_panels, draw_matrix_config_panel,
draw_stats_panel, panel_restore_item,
},
};
use eframe::{egui, egui_wgpu};
use std::sync::Arc;
const SUMMARY_POINTS_PER_SERIES: usize = 42;
const HAND_FORCE_PANEL_COUNT: usize = 7;
const HAND_FORCE_SEGMENT_COUNTS: [usize; HAND_FORCE_PANEL_COUNT] = [84, 84, 84, 84, 84, 70, 44];
pub struct EskinDesktopApp {
connect_panel: FloatingPanelState,
@@ -39,6 +41,7 @@ pub struct EskinDesktopApp {
matrix_config_panel: FloatingPanelState,
matrix_config: MatrixConfigState,
signal_history: Vec<f32>,
hand_signal_histories: [Vec<f32>; HAND_FORCE_PANEL_COUNT],
force_estimator: ForceEstimatorState,
latest_spatial_force: Option<HudSpatialForce>,
latest_raw_matrix: Vec<u32>,
@@ -104,6 +107,7 @@ impl EskinDesktopApp {
),
matrix_config: MatrixConfigState::default(),
signal_history: Vec::with_capacity(128),
hand_signal_histories: std::array::from_fn(|_| Vec::with_capacity(128)),
force_estimator: ForceEstimatorState::new(),
latest_spatial_force: None,
latest_raw_matrix: Vec::new(),
@@ -243,6 +247,10 @@ impl EskinDesktopApp {
if self.signal_history.len() > SUMMARY_POINTS_PER_SERIES {
self.signal_history.remove(0);
}
if self.config_state.mode == SerialMode::Hand {
update_hand_signal_histories(&mut self.hand_signal_histories, &sample.matrix);
}
}
}
@@ -367,12 +375,19 @@ impl EskinDesktopApp {
) {
self.switch_mode(next_mode);
}
draw_stats_panel(
ctx,
&mut self.stats_panel,
&self.signal_history,
self.latest_spatial_force,
);
match self.config_state.mode {
SerialMode::Finger => {
draw_stats_panel(
ctx,
&mut self.stats_panel,
&self.signal_history,
self.latest_spatial_force,
);
}
SerialMode::Hand => {
draw_hand_force_panels(ctx, self.stats_panel.visible, &self.hand_signal_histories);
}
}
draw_export_panel(
ctx,
&mut self.export_panel,
@@ -416,14 +431,26 @@ impl EskinDesktopApp {
panel_restore_item(ui, "录制", &mut self.export_panel);
panel_restore_item(ui, "矩阵", &mut self.matrix_config_panel);
panel_restore_item(ui, "统计", &mut self.stats_panel);
if ui.button("打砖块").clicked() {
if ui
.add_sized(
egui::vec2(ui.available_width(), 0.0),
egui::Button::new("打砖块"),
)
.clicked()
{
self.breakout_visible = true;
close_menu = true;
}
ui.separator();
if ui.button("全部显示").clicked() {
if ui
.add_sized(
egui::vec2(ui.available_width(), 0.0),
egui::Button::new("全部显示"),
)
.clicked()
{
self.config_panel.visible = true;
self.export_panel.visible = true;
self.matrix_config_panel.visible = true;
@@ -456,6 +483,9 @@ impl EskinDesktopApp {
self.force_estimator.reset();
self.latest_spatial_force = None;
self.signal_history.clear();
self.hand_signal_histories
.iter_mut()
.for_each(|history| history.clear());
self.active_mode = match next {
SerialMode::Finger => ActiveMode::Finger(FingerMode {
@@ -469,6 +499,30 @@ impl EskinDesktopApp {
}
}
fn update_hand_signal_histories(
histories: &mut [Vec<f32>; HAND_FORCE_PANEL_COUNT],
raw_values: &[u32],
) {
let mut offset = 0usize;
for (history, sample_count) in histories.iter_mut().zip(HAND_FORCE_SEGMENT_COUNTS) {
let raw_total = raw_values
.get(offset..offset + sample_count)
.unwrap_or(&[])
.iter()
.fold(0_u64, |sum, value| sum + *value as u64)
.min(u32::MAX as u64) as u32;
let force = raw_to_g1(raw_total).min(25.6);
let force = if force <= 0.1 { 0.0 } else { force };
history.push(force);
if history.len() > SUMMARY_POINTS_PER_SERIES {
history.remove(0);
}
offset += sample_count;
}
}
fn raw_to_g1(raw: u32) -> f32 {
const RAW: [u32; 12] = [
0, 21382, 108507, 123183, 147405, 171105, 192395, 250443, 231423, 350560, 396616, 429444,