feat: add flash countdown and responsive photobooth overlays
This commit is contained in:
119
src/capture.rs
Normal file
119
src/capture.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[derive(Default)]
|
||||
enum Phase {
|
||||
#[default]
|
||||
Tracking,
|
||||
Countdown(Instant),
|
||||
AwaitingRelease,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Sequence {
|
||||
phase: Phase,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Presentation {
|
||||
pub countdown: i32,
|
||||
pub flash_opacity: f32,
|
||||
pub awaiting_release: bool,
|
||||
}
|
||||
|
||||
impl Sequence {
|
||||
/// Returns true exactly once when a countdown finishes, even if frames skip.
|
||||
pub fn tick(&mut self, complete: bool, hand_present: bool, now: Instant) -> bool {
|
||||
match self.phase {
|
||||
Phase::Tracking if complete => self.phase = Phase::Countdown(now),
|
||||
Phase::Countdown(start) if now.duration_since(start) >= Duration::from_secs(3) => {
|
||||
self.phase = Phase::AwaitingRelease;
|
||||
return true;
|
||||
}
|
||||
Phase::AwaitingRelease if !hand_present => self.phase = Phase::Tracking,
|
||||
_ => {}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn tracking(&self) -> bool {
|
||||
matches!(self.phase, Phase::Tracking)
|
||||
}
|
||||
|
||||
pub fn presentation(&self, now: Instant) -> Presentation {
|
||||
match self.phase {
|
||||
Phase::Countdown(start) => {
|
||||
let elapsed = now.duration_since(start).as_secs_f32();
|
||||
Presentation {
|
||||
countdown: (3 - elapsed as i32).clamp(1, 3),
|
||||
// A brief translucent white flash when the palm circle fills.
|
||||
flash_opacity: 0.75 * (1.0 - elapsed / 0.4).clamp(0.0, 1.0),
|
||||
awaiting_release: false,
|
||||
}
|
||||
}
|
||||
Phase::AwaitingRelease => Presentation {
|
||||
awaiting_release: true,
|
||||
..Default::default()
|
||||
},
|
||||
Phase::Tracking => Presentation::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take_picture() {
|
||||
// TODO: Take a picture here after the countdown. No capture or file writes yet.
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn flash_countdown_and_single_capture_then_release() {
|
||||
let start = Instant::now();
|
||||
let mut sequence = Sequence::default();
|
||||
assert!(!sequence.tick(false, true, start));
|
||||
assert!(sequence.tracking());
|
||||
assert!(!sequence.tick(true, true, start));
|
||||
assert!(!sequence.tracking());
|
||||
assert_eq!(sequence.presentation(start).countdown, 3);
|
||||
assert_eq!(sequence.presentation(start).flash_opacity, 0.75);
|
||||
assert_eq!(
|
||||
sequence
|
||||
.presentation(start + Duration::from_millis(200))
|
||||
.flash_opacity,
|
||||
0.375
|
||||
);
|
||||
for (ms, number) in [(999, 3), (1000, 2), (1999, 2), (2000, 1), (2999, 1)] {
|
||||
let now = start + Duration::from_millis(ms);
|
||||
// Lowering the hand must not cancel an already-started countdown.
|
||||
assert!(!sequence.tick(false, false, now));
|
||||
assert_eq!(sequence.presentation(now).countdown, number);
|
||||
assert_eq!(sequence.presentation(now).flash_opacity, 0.0);
|
||||
}
|
||||
assert!(sequence.tick(true, true, start + Duration::from_secs(3)));
|
||||
assert!(
|
||||
sequence
|
||||
.presentation(start + Duration::from_secs(3))
|
||||
.awaiting_release
|
||||
);
|
||||
assert_eq!(
|
||||
sequence
|
||||
.presentation(start + Duration::from_secs(3))
|
||||
.countdown,
|
||||
0
|
||||
);
|
||||
assert!(!sequence.tick(true, true, start + Duration::from_secs(4)));
|
||||
assert!(!sequence.tracking());
|
||||
assert!(!sequence.tick(false, false, start + Duration::from_secs(5)));
|
||||
assert!(sequence.tracking());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delayed_frame_still_finishes_only_once() {
|
||||
let now = Instant::now();
|
||||
let mut sequence = Sequence::default();
|
||||
sequence.tick(true, true, now);
|
||||
assert!(sequence.tick(false, false, now + Duration::from_secs(10)));
|
||||
assert!(!sequence.tick(false, false, now + Duration::from_secs(11)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user