use std::time::Duration;
use crate::geo::ScreenCoordinate;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MomentumEasing {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
}
impl MomentumEasing {
pub const DEFAULT: MomentumEasing = MomentumEasing { x1: 0.0, y1: 0.0, x2: 0.25, y2: 1.0 };
pub const ANDROID_FLING: MomentumEasing = MomentumEasing { x1: 0.175, y1: 0.5, x2: 0.35, y2: 1.0 };
}
impl Default for MomentumEasing {
fn default() -> Self {
MomentumEasing::DEFAULT
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MomentumDecayModel {
Friction { half_life: Duration, stop_speed: f64 },
Curve { reference_speed: f64, duration: Duration, exponent: f64, easing: MomentumEasing },
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MomentumDecay {
pub model: MomentumDecayModel,
pub min_speed: f64,
pub max_speed: f64,
pub max_distance: f64,
}
impl MomentumDecay {
pub const DEFAULT: MomentumDecay = MomentumDecay {
model: MomentumDecayModel::Friction { half_life: Duration::from_millis(140), stop_speed: 1.0 },
min_speed: 0.0,
max_speed: f64::INFINITY,
max_distance: f64::INFINITY,
};
pub const PAN: MomentumDecay = MomentumDecay {
model: MomentumDecayModel::Friction { half_life: Duration::from_millis(140), stop_speed: 10.0 },
min_speed: 200.0,
max_speed: 10_000.0,
..MomentumDecay::DEFAULT
};
pub const ZOOM: MomentumDecay = MomentumDecay {
model: MomentumDecayModel::Friction { half_life: Duration::from_millis(140), stop_speed: 0.01 },
max_distance: 2.5,
..MomentumDecay::DEFAULT
};
pub const BEARING: MomentumDecay = MomentumDecay { min_speed: 60.0, ..MomentumDecay::DEFAULT };
pub const PITCH: MomentumDecay = MomentumDecay { min_speed: 5.0, ..MomentumDecay::DEFAULT };
pub const ANDROID_FLING: MomentumDecay = MomentumDecay {
model: MomentumDecayModel::Curve {
reference_speed: 1000.0,
duration: Duration::from_micros(555_182),
exponent: 0.736_267_6,
easing: MomentumEasing::ANDROID_FLING,
},
min_speed: 50.0,
max_speed: 8000.0,
max_distance: f64::INFINITY,
};
pub fn validate(&self) -> Result<(), InvalidMomentum> {
let positive = |value: f64| value.is_finite() && value > 0.0;
let limits =
self.min_speed.is_finite() && self.min_speed >= 0.0 && self.max_speed > 0.0 && self.max_distance > 0.0;
let model = match self.model {
MomentumDecayModel::Friction { half_life, stop_speed } => !half_life.is_zero() && positive(stop_speed),
MomentumDecayModel::Curve { reference_speed, duration, exponent, easing } => {
let MomentumEasing { x1, y1, x2, y2 } = easing;
positive(reference_speed)
&& !duration.is_zero()
&& exponent.is_finite()
&& exponent > -1.0
&& [x1, y1, x2, y2].iter().all(|value| (0.0..=1.0).contains(value))
&& x1 > 0.0
&& y1 > 0.0
}
};
if limits && model {
Ok(())
} else {
Err(InvalidMomentum(
"a momentum decay needs positive speeds, distances and durations, a minimum speed that isn't \
negative, and a curve within [0, 1] that starts rising",
))
}
}
}
impl Default for MomentumDecay {
fn default() -> Self {
MomentumDecay::DEFAULT
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct InvalidMomentum(pub &'static str);
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MomentumSnap {
pub spacing: f64,
pub offset: f64,
pub tolerance: f64,
pub duration: Duration,
}
impl MomentumSnap {
pub const ZOOM: MomentumSnap =
MomentumSnap { spacing: 1.0, offset: 0.0, tolerance: 0.5, duration: Duration::from_millis(300) };
pub const BEARING: MomentumSnap =
MomentumSnap { spacing: 360.0, offset: 0.0, tolerance: 7.0, duration: Duration::from_millis(300) };
pub const PITCH: MomentumSnap =
MomentumSnap { spacing: 90.0, offset: 0.0, tolerance: 7.0, duration: Duration::from_millis(300) };
pub fn validate(&self) -> Result<(), InvalidMomentum> {
if self.spacing.is_finite()
&& self.spacing > 0.0
&& self.offset.is_finite()
&& self.tolerance.is_finite()
&& self.tolerance >= 0.0
{
Ok(())
} else {
Err(InvalidMomentum(
"a momentum snap needs a positive spacing, a finite offset, and a tolerance that isn't negative",
))
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MomentumGlide<T> {
pub velocity: T,
pub decay: MomentumDecay,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AnchoredMomentum {
pub glide: Option<MomentumGlide<f64>>,
pub anchor: Option<ScreenCoordinate>,
pub snap: Option<MomentumSnap>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct CameraMomentum {
pub pan: Option<MomentumGlide<ScreenCoordinate>>,
pub zoom: AnchoredMomentum,
pub bearing: AnchoredMomentum,
pub pitch: AnchoredMomentum,
}