use crate::keyed::{self, NumberSegment, Segment};
use crate::{EvaluatedVisual, SampleContext};
use alpha2_model::{
Animated, AnimatedPoint, EffectValue, Extrapolation, GradientStop, Insets, Interpolation,
Keyframe, LayoutLength, LayoutOffset, LayoutSize, MotionPath, Orient, Paint, Path,
PointKeyframe, Vec2, VisualProperties,
};
use alpha2_time::MediaTime;
#[must_use]
pub fn sample_visual(
properties: &VisualProperties,
time: MediaTime,
aspect: f64,
context: &SampleContext<'_>,
) -> EvaluatedVisual {
let rotation = evaluate_scalar(&properties.rotation_degrees, time, context);
EvaluatedVisual {
position: sample_point(&properties.position, time, aspect, context),
anchor: properties.anchor,
scale: evaluate_vec2(&properties.scale, time, context),
rotation_degrees: match properties.orient {
Orient::None => rotation,
Orient::Path => finite_f32(
f64::from(rotation) + path_angle(&properties.position, time, aspect, context),
),
},
opacity: evaluate_scalar(&properties.opacity, time, context).clamp(0.0, 1.0),
}
}
pub(crate) fn screen_aspect(aspect: f64) -> f64 {
if aspect.is_finite() && aspect > 0.0 {
aspect
} else {
1.0
}
}
pub(crate) fn motion_segments(
motion: &MotionPath,
aspect: f64,
) -> (Vec<alpha2_geometry::kurbo::PathSeg>, bool) {
use alpha2_geometry::kurbo::{CubicBez, PathSeg, Point, Vec2 as Vector};
let Some(subpath) = motion.path.subpaths.first() else {
return (Vec::new(), false);
};
let point = |value: Vec2| Point::new(f64::from(value.x) * aspect, f64::from(value.y));
let vector = |value: Vec2| Vector::new(f64::from(value.x) * aspect, f64::from(value.y));
let vertices = &subpath.vertices;
let joined = vertices
.iter()
.zip(vertices.iter().skip(1).chain(vertices.first()))
.take(if subpath.closed {
vertices.len()
} else {
vertices.len().saturating_sub(1)
});
let segments = joined
.map(|(from, to)| {
PathSeg::Cubic(CubicBez::new(
point(from.point),
point(from.point) + vector(from.out_tangent),
point(to.point) + vector(to.in_tangent),
point(to.point),
))
})
.collect();
(segments, subpath.closed)
}
fn degrees(direction: alpha2_geometry::kurbo::Vec2) -> f64 {
if direction.x == 0.0 && direction.y == 0.0 {
0.0
} else {
libm::atan2(direction.y, direction.x).to_degrees()
}
}
#[must_use]
pub fn path_angle(
animation: &AnimatedPoint,
time: MediaTime,
aspect: f64,
context: &SampleContext<'_>,
) -> f64 {
let aspect = screen_aspect(aspect);
match animation {
AnimatedPoint::Constant { .. } | AnimatedPoint::Data(_) | AnimatedPoint::Split { .. } => {
0.0
}
AnimatedPoint::Path(motion) => {
let (segments, closed) = motion_segments(motion, aspect);
let distance = f64::from(evaluate_scalar(&motion.distance, time, context));
degrees(alpha2_geometry::spatial::along_path(&segments, closed, distance).1)
}
AnimatedPoint::Keyframes {
keyframes,
before,
after,
} => {
let point = |value: Vec2| {
alpha2_geometry::kurbo::Point::new(f64::from(value.x) * aspect, f64::from(value.y))
};
let vector = |value: Vec2| {
alpha2_geometry::kurbo::Vec2::new(f64::from(value.x) * aspect, f64::from(value.y))
};
let direction = |index: usize, amount: f64| {
let (left, right) = (keyframes.get(index)?, keyframes.get(index + 1)?);
if matches!(left.interpolation, Interpolation::Hold) {
return None;
}
alpha2_geometry::spatial::direction_along(
point(left.value),
vector(left.out_tangent),
vector(right.in_tangent),
point(right.value),
amount,
)
};
let index_of =
|key: &PointKeyframe| keyframes.partition_point(|other| other.time < key.time);
let (segment, _) = keyed::read(keyframes, *before, *after, time);
let (at, own) = match segment.timed() {
Segment::Empty => return 0.0,
Segment::At(key) => (index_of(key), None),
Segment::Between(left, _, amount) => {
let index = index_of(left);
(index, direction(index, amount.clamp(0.0, 1.0)))
}
};
let found = own
.or_else(|| (at..keyframes.len()).find_map(|index| direction(index, 0.0)))
.or_else(|| (0..at).rev().find_map(|index| direction(index, 1.0)));
found.map_or(0.0, degrees)
}
}
}
fn evaluate_vec2(animation: &Animated<Vec2>, time: MediaTime, context: &SampleContext<'_>) -> Vec2 {
match animation {
Animated::Constant { value } => *value,
Animated::Data(binding) => {
crate::data::bound_vec2(binding, time, context).unwrap_or_default()
}
Animated::Split { channels } => {
let [x, y] = channel_values(channels, time, context);
Vec2::new(x, y)
}
Animated::Keyframes {
keyframes,
before,
after,
} => keyed_vec2(keyframes, *before, *after, time),
}
}
#[inline(never)]
fn keyed_vec2(
keyframes: &[Keyframe<Vec2>],
before: Extrapolation,
after: Extrapolation,
time: MediaTime,
) -> Vec2 {
let (segment, passes) = keyed::read(keyframes, before, after, time);
let (Some(first), Some(last)) = (keyframes.first(), keyframes.last()) else {
return Vec2::default();
};
match segment.timed() {
Segment::At(keyframe) if passes == 0 => keyframe.value,
segment => {
let component = |read: fn(&Vec2) -> f32| {
let wide = timed_number(&segment, read);
finite_f32(offset_by_passes(
wide,
passes,
read(&first.value),
read(&last.value),
))
};
Vec2::new(component(|value| value.x), component(|value| value.y))
}
}
}
#[must_use]
pub fn sample_vec2(
animation: &Animated<Vec2>,
time: MediaTime,
context: &SampleContext<'_>,
) -> Vec2 {
evaluate_vec2(animation, time, context)
}
#[must_use]
pub fn sample_scalar(
animation: &Animated<f32>,
time: MediaTime,
context: &SampleContext<'_>,
) -> f32 {
evaluate_scalar(animation, time, context)
}
fn evaluate_scalar(animation: &Animated<f32>, time: MediaTime, context: &SampleContext<'_>) -> f32 {
match animation {
Animated::Constant { value } => *value,
Animated::Data(binding) => crate::data::bound_scalar(binding, time, context).unwrap_or(0.0),
Animated::Split { .. } => 0.0,
Animated::Keyframes {
keyframes,
before,
after,
} => keyed_scalar(keyframes, *before, *after, time),
}
}
#[inline(never)]
fn keyed_scalar(
keyframes: &[Keyframe<f32>],
before: Extrapolation,
after: Extrapolation,
time: MediaTime,
) -> f32 {
let (segment, passes) = keyed::read(keyframes, before, after, time);
let (Some(first), Some(last)) = (keyframes.first(), keyframes.last()) else {
return 0.0;
};
match segment {
NumberSegment::Timed(Segment::At(keyframe)) if passes == 0 => keyframe.value,
segment => {
let wide = number(&segment, |value| *value);
finite_f32(offset_by_passes(wide, passes, first.value, last.value))
}
}
}
fn channel_values<const N: usize>(
channels: &[Animated<f32>],
time: MediaTime,
context: &SampleContext<'_>,
) -> [f32; N] {
std::array::from_fn(|index| {
channels
.get(index)
.map_or(0.0, |channel| evaluate_scalar(channel, time, context))
})
}
fn timed_number<T>(segment: &Segment<'_, Keyframe<T>>, read: impl Fn(&T) -> f32) -> f64 {
match segment {
Segment::Empty => 0.0,
Segment::At(keyframe) => f64::from(read(&keyframe.value)),
Segment::Between(left, right, amount) => {
lerp(read(&left.value), read(&right.value), *amount)
}
}
}
fn number<T>(segment: &NumberSegment<'_, Keyframe<T>>, read: impl Fn(&T) -> f32) -> f64 {
match segment {
NumberSegment::Timed(segment) => timed_number(segment, read),
NumberSegment::Curve(left, right, curve, progress) => {
curve.value(read(&left.value), read(&right.value), *progress)
}
NumberSegment::Line {
left,
right,
curve,
at_end,
outside,
} => {
let (from, to) = (read(&left.value), read(&right.value));
f64::from(if *at_end { to } else { from })
+ curve.end_slope(from, to, *at_end) * outside
}
}
}
fn lerp(start: f32, end: f32, amount: f64) -> f64 {
f64::from(start) + (f64::from(end) - f64::from(start)) * amount
}
fn offset_by_passes(value: f64, passes: i64, first: f32, last: f32) -> f64 {
if passes == 0 {
return value;
}
#[expect(clippy::cast_precision_loss, reason = "a count of passes")]
let passes = passes as f64;
value + passes * (f64::from(last) - f64::from(first))
}
#[must_use]
pub fn sample_point(
animation: &AnimatedPoint,
time: MediaTime,
aspect: f64,
context: &SampleContext<'_>,
) -> Vec2 {
match animation {
AnimatedPoint::Constant { value } => *value,
moving => moving_point(moving, time, aspect, context),
}
}
#[inline(never)]
fn moving_point(
animation: &AnimatedPoint,
time: MediaTime,
aspect: f64,
context: &SampleContext<'_>,
) -> Vec2 {
match animation {
AnimatedPoint::Constant { value } => *value,
AnimatedPoint::Data(binding) => {
crate::data::bound_vec2(binding, time, context).unwrap_or_default()
}
AnimatedPoint::Split { channels } => {
let [x, y] = channel_values(channels, time, context);
Vec2::new(x, y)
}
AnimatedPoint::Path(motion) => {
let aspect = screen_aspect(aspect);
let (segments, closed) = motion_segments(motion, aspect);
let distance = f64::from(evaluate_scalar(&motion.distance, time, context));
let (at, _) = alpha2_geometry::spatial::along_path(&segments, closed, distance);
Vec2::new(finite_f32(at.x / aspect), finite_f32(at.y))
}
AnimatedPoint::Keyframes {
keyframes,
before,
after,
} => keyed_point(keyframes, *before, *after, time, aspect),
}
}
fn keyed_point(
keyframes: &[PointKeyframe],
before: Extrapolation,
after: Extrapolation,
time: MediaTime,
aspect: f64,
) -> Vec2 {
let (segment, passes) = keyed::read(keyframes, before, after, time);
let (Some(first), Some(last)) = (keyframes.first(), keyframes.last()) else {
return Vec2::default();
};
let moved = |x: f64, y: f64| {
Vec2::new(
finite_f32(offset_by_passes(x, passes, first.value.x, last.value.x)),
finite_f32(offset_by_passes(y, passes, first.value.y, last.value.y)),
)
};
let (left, right, amount) = match segment.timed() {
Segment::Empty => return Vec2::default(),
Segment::At(keyframe) if passes == 0 => return keyframe.value,
Segment::At(keyframe) => {
return moved(f64::from(keyframe.value.x), f64::from(keyframe.value.y));
}
Segment::Between(left, right, amount) => (left, right, amount),
};
if left.out_tangent.is_zero() && right.in_tangent.is_zero() {
return moved(
lerp(left.value.x, right.value.x, amount),
lerp(left.value.y, right.value.y, amount),
);
}
let aspect = screen_aspect(aspect);
let point = |value: Vec2| {
alpha2_geometry::kurbo::Point::new(f64::from(value.x) * aspect, f64::from(value.y))
};
let vector = |value: Vec2| {
alpha2_geometry::kurbo::Vec2::new(f64::from(value.x) * aspect, f64::from(value.y))
};
let at = alpha2_geometry::spatial::point_along(
point(left.value),
vector(left.out_tangent),
vector(right.in_tangent),
point(right.value),
amount,
);
moved(at.x / aspect, at.y)
}
#[allow(clippy::cast_possible_truncation)]
fn finite_f32(value: f64) -> f32 {
if value.is_nan() {
0.0
} else {
value.clamp(f64::from(f32::MIN), f64::from(f32::MAX)) as f32
}
}
#[must_use]
pub fn sample_path(animation: &Animated<Path>, time: MediaTime) -> alpha2_geometry::EvaluatedPath {
use alpha2_geometry::kurbo::{Point, Vec2 as Vector};
use alpha2_geometry::{EvaluatedPath, EvaluatedSubpath, EvaluatedVertex};
match animation {
Animated::Constant { value } => EvaluatedPath::from(value),
Animated::Data(_) | Animated::Split { .. } => EvaluatedPath::default(),
Animated::Keyframes {
keyframes,
before,
after,
} => match segment(keyframes, *before, *after, time) {
Segment::At(keyframe) => EvaluatedPath::from(&keyframe.value),
Segment::Empty => EvaluatedPath::default(),
Segment::Between(left, right, amount) => {
let structure = |path: &Path| {
path.subpaths
.iter()
.map(|subpath| (subpath.closed, subpath.vertices.len()))
.collect::<Vec<_>>()
};
if structure(&left.value) != structure(&right.value) {
return EvaluatedPath::from(&left.value);
}
let lerp = |a: f32, b: f32| f64::from(a) + (f64::from(b) - f64::from(a)) * amount;
let point = |a: Vec2, b: Vec2| Point::new(lerp(a.x, b.x), lerp(a.y, b.y));
let vector = |a: Vec2, b: Vec2| Vector::new(lerp(a.x, b.x), lerp(a.y, b.y));
EvaluatedPath {
subpaths: left
.value
.subpaths
.iter()
.zip(&right.value.subpaths)
.map(|(from, to)| EvaluatedSubpath {
closed: from.closed,
vertices: from
.vertices
.iter()
.zip(&to.vertices)
.map(|(a, b)| EvaluatedVertex {
point: point(a.point, b.point),
in_tangent: vector(a.in_tangent, b.in_tangent),
out_tangent: vector(a.out_tangent, b.out_tangent),
})
.collect(),
})
.collect(),
}
}
},
}
}
#[must_use]
pub fn sample_paint(animation: &Animated<Paint>, time: MediaTime) -> Paint {
match animation {
Animated::Constant { value } => value.clone(),
Animated::Data(_) | Animated::Split { .. } => Paint::Solid {
color: alpha2_model::Rgba::new(0.0, 0.0, 0.0, 0.0),
},
Animated::Keyframes {
keyframes,
before,
after,
} => match segment(keyframes, *before, *after, time) {
Segment::At(keyframe) => keyframe.value.clone(),
Segment::Empty => Paint::Solid {
color: alpha2_model::Rgba::new(0.0, 0.0, 0.0, 0.0),
},
Segment::Between(left, right, amount) => {
interpolate_paint(&left.value, &right.value, amount)
.unwrap_or_else(|| left.value.clone())
}
},
}
}
fn interpolate_paint(from: &Paint, to: &Paint, amount: f64) -> Option<Paint> {
let lerp = |a: f32, b: f32| interpolate_f32(a, b, amount);
let point = |a: Vec2, b: Vec2| Vec2::new(lerp(a.x, b.x), lerp(a.y, b.y));
let color = |a: alpha2_model::Rgba, b: alpha2_model::Rgba| {
let [red, green, blue, alpha] = interpolate_color(
[a.red, a.green, a.blue, a.alpha],
[b.red, b.green, b.blue, b.alpha],
amount,
);
alpha2_model::Rgba::new(red, green, blue, alpha)
};
let stops = |a: &[GradientStop], b: &[GradientStop]| -> Option<Vec<GradientStop>> {
if a.len() != b.len() {
return None;
}
let mut previous = 0.0_f32;
Some(
a.iter()
.zip(b)
.map(|(a, b)| {
let offset = lerp(a.offset, b.offset).clamp(previous, 1.0);
previous = offset;
GradientStop {
offset,
color: color(a.color, b.color),
}
})
.collect(),
)
};
Some(match (from, to) {
(Paint::Solid { color: a }, Paint::Solid { color: b }) => Paint::Solid {
color: color(*a, *b),
},
(
Paint::LinearGradient {
start: s0,
end: e0,
stops: a,
spread,
},
Paint::LinearGradient {
start: s1,
end: e1,
stops: b,
spread: other,
},
) if spread == other => Paint::LinearGradient {
start: point(*s0, *s1),
end: point(*e0, *e1),
stops: stops(a, b)?,
spread: *spread,
},
(
Paint::RadialGradient {
center: c0,
radius: r0,
stops: a,
spread,
},
Paint::RadialGradient {
center: c1,
radius: r1,
stops: b,
spread: other,
},
) if spread == other => Paint::RadialGradient {
center: point(*c0, *c1),
radius: lerp(*r0, *r1).max(f32::MIN_POSITIVE),
stops: stops(a, b)?,
spread: *spread,
},
(
Paint::ConicGradient {
center: c0,
angle_degrees: a0,
stops: a,
},
Paint::ConicGradient {
center: c1,
angle_degrees: a1,
stops: b,
},
) => Paint::ConicGradient {
center: point(*c0, *c1),
angle_degrees: lerp(*a0, *a1),
stops: stops(a, b)?,
},
_ => return None,
})
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct EffectParamValue {
pub value: EffectValue,
pub interpolated: bool,
}
#[must_use]
pub fn sample_effect_param(
animation: &Animated<EffectValue>,
time: MediaTime,
context: &SampleContext<'_>,
) -> EffectParamValue {
let authored = |value: EffectValue| EffectParamValue {
value,
interpolated: false,
};
let interpolated = |value: EffectValue| EffectParamValue {
value,
interpolated: true,
};
let between = |left: EffectValue, right: EffectValue, amount: f64| {
interpolate_value(left, right, amount).map_or_else(|| authored(left), interpolated)
};
match animation {
Animated::Data(binding) => {
let value = match crate::data::bound_vec2(binding, time, context) {
Some(point) if binding.remap.is_none() && is_points(binding, context) => {
EffectValue::Vec2([point.x, point.y])
}
_ => EffectValue::Float(
crate::data::bound_scalar(binding, time, context).unwrap_or(0.0),
),
};
EffectParamValue {
value,
interpolated: true,
}
}
Animated::Constant { value } => authored(*value),
Animated::Split { channels } => interpolated(if channels.len() == 4 {
EffectValue::Vec4(channel_values(channels, time, context))
} else {
EffectValue::Vec2(channel_values(channels, time, context))
}),
Animated::Keyframes {
keyframes,
before,
after,
} => {
let (segment, passes) = keyed::read(keyframes, *before, *after, time);
if let Some(value) = repeat_offset_value(keyframes, &segment, passes) {
return interpolated(value);
}
match &segment {
NumberSegment::Curve(left, right, ..) | NumberSegment::Line { left, right, .. }
if matches!(
(left.value, right.value),
(EffectValue::Float(_), EffectValue::Float(_))
) =>
{
interpolated(EffectValue::Float(finite_f32(number(&segment, |value| {
float(*value)
}))))
}
_ => match segment.timed() {
Segment::Between(left, right, amount) => {
between(left.value, right.value, amount)
}
Segment::At(keyframe) => authored(keyframe.value),
Segment::Empty => authored(EffectValue::Float(0.0)),
},
}
}
}
}
const fn float(value: EffectValue) -> f32 {
match value {
EffectValue::Float(value) => value,
_ => 0.0,
}
}
fn repeat_offset_value(
keyframes: &[Keyframe<EffectValue>],
segment: &NumberSegment<'_, Keyframe<EffectValue>>,
passes: i64,
) -> Option<EffectValue> {
if passes == 0 {
return None;
}
let (first, last) = (keyframes.first()?.value, keyframes.last()?.value);
let component = |index: usize| {
let read = |value: &EffectValue| match value {
EffectValue::Float(value) => *value,
EffectValue::Vec2(values) => values.get(index).copied().unwrap_or_default(),
EffectValue::Vec4(values) => values.get(index).copied().unwrap_or_default(),
EffectValue::Color(_) | EffectValue::Enum(_) | EffectValue::Bool(_) => 0.0,
};
let wide = match first {
EffectValue::Float(_) => number(segment, read),
_ => timed_number(&segment.timed(), read),
};
finite_f32(offset_by_passes(wide, passes, read(&first), read(&last)))
};
match first {
EffectValue::Float(_) => Some(EffectValue::Float(component(0))),
EffectValue::Vec2(_) => Some(EffectValue::Vec2([component(0), component(1)])),
EffectValue::Vec4(_) => Some(EffectValue::Vec4(std::array::from_fn(component))),
EffectValue::Color(_) | EffectValue::Enum(_) | EffectValue::Bool(_) => None,
}
}
fn is_points(binding: &alpha2_model::DataBinding, context: &SampleContext<'_>) -> bool {
crate::data::bound_kind(binding, context) == Some(alpha2_model::DataKind::Points)
}
fn interpolate_value(start: EffectValue, end: EffectValue, amount: f64) -> Option<EffectValue> {
let lerp = |a: f32, b: f32| interpolate_f32(a, b, amount);
match (start, end) {
(EffectValue::Float(a), EffectValue::Float(b)) => Some(EffectValue::Float(lerp(a, b))),
(EffectValue::Vec2(a), EffectValue::Vec2(b)) => {
Some(EffectValue::Vec2([lerp(a[0], b[0]), lerp(a[1], b[1])]))
}
(EffectValue::Vec4(a), EffectValue::Vec4(b)) => {
Some(EffectValue::Vec4(std::array::from_fn(|index| {
lerp(a[index], b[index])
})))
}
(EffectValue::Color(a), EffectValue::Color(b)) => {
Some(EffectValue::Color(interpolate_color(a, b, amount)))
}
_ => None,
}
}
#[must_use]
pub fn sample_rgba(
animation: &Animated<alpha2_model::Rgba>,
time: MediaTime,
) -> alpha2_model::Rgba {
match animation {
Animated::Data(_) | Animated::Split { .. } => alpha2_model::Rgba::new(0.0, 0.0, 0.0, 0.0),
Animated::Constant { value } => *value,
Animated::Keyframes {
keyframes,
before,
after,
} => match segment(keyframes, *before, *after, time) {
Segment::At(keyframe) => keyframe.value,
Segment::Empty => alpha2_model::Rgba::new(0.0, 0.0, 0.0, 0.0),
Segment::Between(left, right, amount) => {
let (a, b) = (left.value, right.value);
let [red, green, blue, alpha] = interpolate_color(
[a.red, a.green, a.blue, a.alpha],
[b.red, b.green, b.blue, b.alpha],
amount,
);
alpha2_model::Rgba::new(red, green, blue, alpha)
}
},
}
}
#[allow(clippy::cast_possible_truncation)]
fn interpolate_color(start: [f32; 4], end: [f32; 4], amount: f64) -> [f32; 4] {
let (start_alpha, end_alpha) = (f64::from(start[3]), f64::from(end[3]));
let alpha = (start_alpha + (end_alpha - start_alpha) * amount).clamp(0.0, 1.0);
let mut color = [0.0; 4];
for index in 0..3 {
let from = f64::from(start[index]) * start_alpha;
let to = f64::from(end[index]) * end_alpha;
let premultiplied = from + (to - from) * amount;
let straight = if alpha > 0.0 {
premultiplied / alpha
} else {
f64::from(start[index]) + (f64::from(end[index]) - f64::from(start[index])) * amount
};
color[index] = straight.clamp(0.0, 1.0) as f32;
}
color[3] = alpha as f32;
color
}
fn segment<T>(
keyframes: &[Keyframe<T>],
before: Extrapolation,
after: Extrapolation,
time: MediaTime,
) -> Segment<'_, Keyframe<T>> {
keyed::read(keyframes, before, after, time).0.timed()
}
#[allow(clippy::cast_precision_loss)]
pub(crate) fn interpolation_amount(start: MediaTime, end: MediaTime, time: MediaTime) -> f64 {
let (start, end, time) = (start.ticks(), end.ticks(), time.ticks());
match (time.checked_sub(start), end.checked_sub(start)) {
(Some(elapsed), Some(length)) => elapsed as f64 / length as f64,
_ => {
(i128::from(time) - i128::from(start)) as f64
/ (i128::from(end) - i128::from(start)) as f64
}
}
}
#[allow(clippy::cast_possible_truncation)]
pub(crate) fn interpolate_f32(start: f32, end: f32, amount: f64) -> f32 {
(f64::from(start) + (f64::from(end) - f64::from(start)) * amount)
.clamp(f64::from(f32::MIN), f64::from(f32::MAX)) as f32
}
#[must_use]
pub fn sample_insets(animation: &Animated<Insets>, time: MediaTime) -> [f32; 4] {
match animation {
Animated::Constant { value } => value.sides().map(LayoutSize::value),
Animated::Data(_) | Animated::Split { .. } => [0.0; 4],
Animated::Keyframes {
keyframes,
before,
after,
} => match segment(keyframes, *before, *after, time) {
Segment::Between(left, right, amount) => {
let (from, to) = (left.value.sides(), right.value.sides());
std::array::from_fn(|side| {
interpolate_f32(from[side].value(), to[side].value(), amount)
})
}
Segment::At(keyframe) => keyframe.value.sides().map(LayoutSize::value),
Segment::Empty => [0.0; 4],
},
}
}
#[must_use]
pub fn sample_size(animation: &Animated<LayoutSize>, time: MediaTime) -> f32 {
match animation {
Animated::Constant { value } => value.value(),
Animated::Data(_) | Animated::Split { .. } => 0.0,
Animated::Keyframes {
keyframes,
before,
after,
} => match segment(keyframes, *before, *after, time) {
Segment::Between(left, right, amount) => {
interpolate_f32(left.value.value(), right.value.value(), amount)
}
Segment::At(keyframe) => keyframe.value.value(),
Segment::Empty => 0.0,
},
}
}
#[must_use]
pub fn sample_offset(animation: &Animated<LayoutOffset>, time: MediaTime) -> Vec2 {
let vector = |offset: &LayoutOffset| Vec2::new(offset.x.value(), offset.y.value());
match animation {
Animated::Constant { value } => vector(value),
Animated::Data(_) | Animated::Split { .. } => Vec2::default(),
Animated::Keyframes {
keyframes,
before,
after,
} => match segment(keyframes, *before, *after, time) {
Segment::Between(left, right, amount) => Vec2::new(
interpolate_f32(left.value.x.value(), right.value.x.value(), amount),
interpolate_f32(left.value.y.value(), right.value.y.value(), amount),
),
Segment::At(keyframe) => vector(&keyframe.value),
Segment::Empty => Vec2::default(),
},
}
}
#[must_use]
pub fn sample_length(animation: &Animated<LayoutLength>, time: MediaTime) -> LayoutLength {
match animation {
Animated::Constant { value } => *value,
Animated::Data(_) | Animated::Split { .. } => LayoutLength::Fraction(0.0),
Animated::Keyframes {
keyframes,
before,
after,
} => match segment(keyframes, *before, *after, time) {
Segment::Between(left, right, amount) => left.value.with_value(interpolate_f32(
left.value.value(),
right.value.value(),
amount,
)),
Segment::At(keyframe) => keyframe.value,
Segment::Empty => LayoutLength::Fraction(0.0),
},
}
}
#[must_use]
pub fn sample_hold<T: Clone + Default>(animation: &Animated<T>, time: MediaTime) -> T {
match animation {
Animated::Constant { value } => value.clone(),
Animated::Data(_) | Animated::Split { .. } => T::default(),
Animated::Keyframes {
keyframes,
before,
after,
} => keyed::held(keyframes, *before, *after, time)
.map(|keyframe| keyframe.value.clone())
.unwrap_or_default(),
}
}
#[cfg(test)]
#[path = "animation_tests.rs"]
mod tests;