Skip to main content

aura_anim_iced/keyframes/
frame.rs

1use crate::property::PropertySnapshot;
2
3#[cfg(test)]
4use crate::{prelude::RawPropertySpec, property::PropertyEntry};
5
6/// A property snapshot stored at a normalized keyframe offset.
7#[derive(Debug, Clone, PartialEq)]
8pub struct Keyframe {
9    offset: f32,
10    snapshot: PropertySnapshot,
11}
12
13impl Keyframe {
14    /// Creates a keyframe with a normalized offset and composition-sorted snapshot.
15    #[must_use]
16    pub fn new(offset: f32, mut snapshot: PropertySnapshot) -> Self {
17        snapshot.sort_by_composition_key();
18
19        Self {
20            offset: normalize_offset(offset),
21            snapshot,
22        }
23    }
24
25    /// Returns the normalized offset of this keyframe.
26    #[must_use]
27    pub const fn offset(&self) -> f32 {
28        self.offset
29    }
30
31    /// Returns a reference to the property snapshot at this keyframe.
32    #[must_use]
33    pub const fn snapshot(&self) -> &PropertySnapshot {
34        &self.snapshot
35    }
36
37    pub(crate) fn merge_snapshot(&mut self, snapshot: PropertySnapshot) {
38        self.snapshot.merge(snapshot);
39    }
40
41    #[cfg(test)]
42    pub(crate) fn find_property(&self, property: &RawPropertySpec) -> Option<&PropertyEntry> {
43        self.snapshot.find_property(property)
44    }
45}
46
47/// Normalizes a keyframe offset to the inclusive range `[0.0, 1.0]`.
48#[must_use]
49pub fn normalize_offset(offset: f32) -> f32 {
50    if offset.is_finite() {
51        offset.clamp(0.0, 1.0)
52    } else {
53        0.0
54    }
55}