use crate::camera::CameraOptions;
use crate::gestures::input::GestureEvent;
use crate::gestures::options::{Axis, GestureOptions, Notch};
const AXES: [Axis; 3] = [Axis::Zoom, Axis::Bearing, Axis::Pitch];
const MAX_CROSSED: i64 = 64;
const MIN_INTERVAL_MS: f64 = 50.0;
impl Axis {
fn capture_and_rearm(self) -> (f64, f64) {
match self {
Axis::Zoom => (0.02, 0.04),
Axis::Bearing | Axis::Pitch => (1.0, 2.0),
}
}
fn circular(self) -> bool {
self == Axis::Bearing
}
fn value(self, camera: &CameraOptions) -> Option<f64> {
match self {
Axis::Zoom => camera.zoom,
Axis::Bearing => camera.bearing,
Axis::Pitch => camera.pitch,
}
.filter(|value| value.is_finite())
}
}
type Key = (usize, i64);
#[derive(Debug, Default)]
struct AxisWatch {
last: Option<f64>,
disarmed: Vec<Key>,
last_event: Option<f64>,
}
#[derive(Debug, Default)]
pub(crate) struct NotchWatch {
axes: [AxisWatch; 3],
}
impl NotchWatch {
pub(crate) fn new(camera: &CameraOptions, options: &GestureOptions) -> NotchWatch {
let mut watch = NotchWatch::default();
for (axis, state) in AXES.into_iter().zip(&mut watch.axes) {
let Some(value) = axis.value(camera) else { continue };
let (capture, _) = axis.capture_and_rearm();
state.last = Some(value);
state.disarmed = reached(axis, &options.axis(axis).notches, value, 0.0, capture).collect();
}
watch
}
pub(crate) fn sample(
&mut self,
camera: &CameraOptions,
options: &GestureOptions,
now: f64,
events: &mut Vec<GestureEvent>,
) {
for (axis, state) in AXES.into_iter().zip(&mut self.axes) {
let axis_options = options.axis(axis);
let Some(to) = axis.value(camera) else { continue };
let Some(from) = state.last.replace(to) else { continue };
if !axis_options.enabled || axis_options.notches.is_empty() {
continue;
}
let travel = if axis.circular() { bearing_delta(from, to) } else { to - from };
if travel == 0.0 {
continue;
}
let (capture, rearm) = axis.capture_and_rearm();
let notches = &axis_options.notches;
let mut first = None;
for key in reached(axis, notches, from, travel, capture) {
if state.disarmed.contains(&key) {
continue;
}
state.disarmed.push(key);
first.get_or_insert(key);
}
state.disarmed.retain(|&key| {
notches.get(key.0).is_some_and(|notch| distance(axis, notch_value(axis, notch, key.1), to) <= rearm)
});
let Some((index, step)) = first else { continue };
if state.last_event.is_some_and(|last| now - last < MIN_INTERVAL_MS) {
continue;
}
state.last_event = Some(now);
let notch = notches[index];
events.push(GestureEvent::Notch { axis, value: notch_value(axis, ¬ch, step), kind: notch.kind });
}
}
}
fn reached(axis: Axis, notches: &[Notch], from: f64, travel: f64, capture: f64) -> impl Iterator<Item = Key> {
let (low, high) = (from + travel.min(0.0) - capture, from + travel.max(0.0) + capture);
notches
.iter()
.enumerate()
.flat_map(move |(index, notch)| steps(axis, notch, low, high).into_iter().map(move |step| (index, step)))
}
fn steps(axis: Axis, notch: &Notch, low: f64, high: f64) -> Vec<i64> {
let between = |low: f64, high: f64, min: f64, max: f64| {
let first = ((low - notch.offset) / notch.spacing).ceil().max(min) as i64;
let end = ((high - notch.offset) / notch.spacing).floor().min(max) as i64;
first..=end.min(first.saturating_add(MAX_CROSSED - 1))
};
if !axis.circular() {
return between(low, high, f64::MIN, f64::MAX).collect();
}
let last = ((360.0 / notch.spacing).ceil() - 1.0).max(0.0);
let first_turn = ((low - notch.offset) / 360.0).floor() as i64;
let last_turn = ((high - notch.offset) / 360.0).floor() as i64;
let mut steps = Vec::new();
for turn in first_turn..=last_turn {
let shift = 360.0 * turn as f64;
steps.extend(between(low - shift, high - shift, 0.0, last));
if steps.len() >= MAX_CROSSED as usize {
break;
}
}
steps
}
fn notch_value(axis: Axis, notch: &Notch, step: i64) -> f64 {
let value = notch.offset + step as f64 * notch.spacing;
if axis.circular() { normalize_bearing(value) } else { value }
}
fn distance(axis: Axis, a: f64, b: f64) -> f64 {
if axis.circular() { bearing_delta(a, b).abs() } else { (a - b).abs() }
}
fn normalize_bearing(bearing: f64) -> f64 {
bearing.rem_euclid(360.0)
}
fn bearing_delta(from: f64, to: f64) -> f64 {
(normalize_bearing(to) - normalize_bearing(from) + 180.0).rem_euclid(360.0) - 180.0
}
#[cfg(test)]
#[path = "notches_test.rs"]
mod notches_test;