Add finger mode UI and refine breakout/serial integration

This commit is contained in:
lenn
2026-07-02 15:22:13 +08:00
parent 0a9fea0f0a
commit 71d314ac44
4 changed files with 352 additions and 90 deletions

View File

@@ -20,6 +20,7 @@ enum BreakoutPhase {
Idle,
Running,
Paused,
Won,
Over,
}
@@ -46,7 +47,6 @@ pub struct BreakoutGame {
score: u32,
combo: u32,
lives: u32,
level: u32,
last_time: Option<f64>,
previous_pause_gesture: bool,
pause_locked_until: f64,
@@ -63,7 +63,6 @@ impl Default for BreakoutGame {
score: 0,
combo: 0,
lives: 3,
level: 1,
last_time: None,
previous_pause_gesture: false,
pause_locked_until: 0.0,
@@ -150,7 +149,6 @@ impl BreakoutGame {
status_chip(ui, "分数", self.score.to_string(), ACCENT_GREEN);
status_chip(ui, "连击", self.combo.to_string(), ACCENT_ORANGE);
status_chip(ui, "生命", self.lives.to_string(), ACCENT_RED);
status_chip(ui, "关卡", self.level.to_string(), ACCENT_BLUE);
});
ui.add_space(7.0);
@@ -191,12 +189,11 @@ impl BreakoutGame {
self.score = 0;
self.combo = 0;
self.lives = 3;
self.level = 1;
self.rebuild_bricks();
}
fn start(&mut self) {
if self.phase == BreakoutPhase::Over {
if self.phase == BreakoutPhase::Over || self.phase == BreakoutPhase::Won {
self.reset();
}
if self.phase != BreakoutPhase::Running {
@@ -219,7 +216,10 @@ impl BreakoutGame {
let threshold = pressure_pause_threshold();
let active = top_force >= threshold;
if active && !self.previous_pause_gesture && now >= self.pause_locked_until {
if self.phase == BreakoutPhase::Idle || self.phase == BreakoutPhase::Over {
if matches!(
self.phase,
BreakoutPhase::Idle | BreakoutPhase::Over | BreakoutPhase::Won
) {
self.start();
} else {
self.toggle_pause();
@@ -249,7 +249,7 @@ impl BreakoutGame {
}
fn launch_ball(&mut self) {
let direction = if self.level % 2 == 0 { -0.24 } else { 0.24 };
let direction = 0.24;
self.ball_pos = egui::pos2(self.paddle_x, PADDLE_Y - PADDLE_H - BALL_RADIUS);
self.ball_vel = egui::vec2(direction, -1.0).normalized() * BALL_SPEED;
}
@@ -324,9 +324,8 @@ impl BreakoutGame {
}
if self.bricks.iter().all(|brick| !brick.alive) {
self.level += 1;
self.rebuild_bricks();
self.launch_ball();
self.phase = BreakoutPhase::Won;
self.ball_vel = egui::Vec2::ZERO;
}
}
@@ -358,6 +357,7 @@ impl BreakoutGame {
BreakoutPhase::Idle => "待机",
BreakoutPhase::Running => "运行",
BreakoutPhase::Paused => "暂停",
BreakoutPhase::Won => "过关",
BreakoutPhase::Over => "结束",
}
}
@@ -387,6 +387,7 @@ impl BreakoutGame {
if self.phase == BreakoutPhase::Idle
|| self.phase == BreakoutPhase::Paused
|| self.phase == BreakoutPhase::Won
|| self.phase == BreakoutPhase::Over
{
paint_center_overlay(&painter, rect, self.phase);
@@ -403,6 +404,10 @@ pub fn control_from_matrix(raw: &[u32], rows: u32, cols: u32) -> BreakoutControl
let cols = cols.max(1) as usize;
let sample_rows = rows.min(2);
let sample_cols = cols.min(2);
let top_gesture_rows = rows.min(1);
let top_gesture_cols = (cols / 3).clamp(1, cols);
let top_gesture_col_start = (cols - top_gesture_cols) / 2;
let top_gesture_col_end = top_gesture_col_start + top_gesture_cols;
let avg = |row_start: usize, row_end: usize, col_start: usize, col_end: usize| -> f32 {
let mut sum = 0.0;
let mut count = 0.0;
@@ -430,7 +435,12 @@ pub fn control_from_matrix(raw: &[u32], rows: u32, cols: u32) -> BreakoutControl
let left_force = tl + bl;
let right_force = tr + br;
let top_force = tl + tr;
let top_force = avg(
0,
top_gesture_rows,
top_gesture_col_start,
top_gesture_col_end,
);
let span = 1200.0_f32.max((PRESSURE_RANGE_MAX - PRESSURE_RANGE_MIN) * 0.22);
let raw_axis = ((right_force - left_force) / span).clamp(-1.0, 1.0);
let axis = if raw_axis.abs() < 0.045 {
@@ -485,6 +495,7 @@ fn status_color(phase: BreakoutPhase) -> egui::Color32 {
BreakoutPhase::Idle => ONE_DARK_PRO.text_dim,
BreakoutPhase::Running => ACCENT_GREEN,
BreakoutPhase::Paused => ACCENT_ORANGE,
BreakoutPhase::Won => ACCENT_GREEN,
BreakoutPhase::Over => ACCENT_RED,
}
}
@@ -607,6 +618,7 @@ fn paint_center_overlay(painter: &egui::Painter, rect: egui::Rect, phase: Breako
let (title, detail) = match phase {
BreakoutPhase::Idle => ("按压顶部或点击开始", "左右分区压力控制挡板"),
BreakoutPhase::Paused => ("已暂停", "再次按压顶部、P 或点击暂停继续"),
BreakoutPhase::Won => ("恭喜过关", "按压顶部、点击或空格重新开始"),
BreakoutPhase::Over => ("游戏结束", "点击或空格重开"),
BreakoutPhase::Running => ("", ""),
};