diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 283e10ef5..6b43420e2 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -35,6 +35,8 @@ pub mod media; pub mod shape; /// Borrowed views of native XFL shape-hint records. pub mod shape_hints; +/// Borrowed views of native XFL tween and custom-ease records. +pub mod tween; /// Largest single entry inflated (bytes). pub const MAX_ENTRY: usize = 256 * 1024 * 1024; diff --git a/crates/xfl/src/tween.rs b/crates/xfl/src/tween.rs new file mode 100644 index 000000000..753be73d1 --- /dev/null +++ b/crates/xfl/src/tween.rs @@ -0,0 +1,125 @@ +//! Borrowed views of native XFL frame-tween and custom-ease records. +//! +//! This parses the native attributes and point lists. Interpreting easing curves and applying +//! them to an application's timeline model remain consumer responsibilities. + +use crate::shape::XflElement; + +/// Native motion/shape tween settings on a `DOMFrame`. +#[derive(Clone, Copy, Debug)] +pub struct FrameTween<'a, E: XflElement> { + frame: &'a E, +} + +impl<'a, E: XflElement> FrameTween<'a, E> { + pub fn new(frame: &'a E) -> Self { + Self { frame } + } + + /// The classic slider value, when present and finite. + pub fn acceleration(&self) -> Option { + number(self.frame, "acceleration") + } + + pub fn has_custom_ease(&self) -> Option { + boolean(self.frame, "hasCustomEase") + } + + /// Animate's default is a single curve unless this is explicitly false. + pub fn use_single_ease_curve(&self) -> Option { + boolean(self.frame, "useSingleEaseCurve") + } + + pub fn orient_to_path(&self) -> Option { + boolean(self.frame, "motionTweenOrientToPath") + } + + /// Raw native rotation setting; interpretation stays in the consuming application. + pub fn rotate(&self) -> Option<&'a str> { + self.frame.attr("motionTweenRotate") + } + + /// Custom ease curves in source order; unknown tween entries are skipped. + pub fn custom_eases(&self) -> impl Iterator> + 'a { + self.frame.child("tweens").into_iter().flat_map(|wrapper| wrapper.elements_named("CustomEase")).map(|element| CustomEase { element }) + } +} + +/// A native custom-ease curve. +#[derive(Clone, Copy, Debug)] +pub struct CustomEase<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> CustomEase<'a, E> { + /// The property-group name, defaulting to `all` in XFL. + pub fn target(&self) -> &'a str { + self.element.attr("target").map(str::trim).unwrap_or("all") + } + + pub fn points(&self) -> impl Iterator> + 'a { + self.element.elements_named("Point").map(|element| EasePoint { element }) + } +} + +/// One native custom-ease point. +#[derive(Clone, Copy, Debug)] +pub struct EasePoint<'a, E: XflElement> { + element: &'a E, +} + +impl EasePoint<'_, E> { + pub fn x(&self) -> Option { + number(self.element, "x") + } + + pub fn y(&self) -> Option { + number(self.element, "y") + } +} + +fn number(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) +} + +fn boolean(element: &impl XflElement, name: &str) -> Option { + element.attr(name).map(|value| value.trim() == "true") +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(source: &[u8]) -> craft_xml::El { + craft_xml::parse(source).expect("well-formed test XML") + } + + #[test] + fn reads_frame_tween_settings_and_custom_eases_in_order() { + let frame = parse( + br#""#, + ); + let tween = FrameTween::new(&frame); + assert_eq!(tween.acceleration(), Some(-45.0)); + assert_eq!(tween.has_custom_ease(), Some(true)); + assert_eq!(tween.use_single_ease_curve(), Some(false)); + assert_eq!(tween.orient_to_path(), Some(true)); + assert_eq!(tween.rotate(), Some("clockwise")); + let curves: Vec<_> = tween.custom_eases().collect(); + assert_eq!(curves.len(), 2); + assert_eq!(curves[0].target(), "position"); + assert_eq!(curves[0].points().map(|point| (point.x(), point.y())).collect::>(), [(Some(0.0), Some(0.0)), (Some(0.5), Some(0.25))]); + assert_eq!(curves[1].target(), "all"); + assert_eq!(curves[1].points().next().map(|point| (point.x(), point.y())), Some((Some(1.0), Some(1.0)))); + } + + #[test] + fn absent_and_malformed_values_remain_optional() { + let frame = parse(br#""#); + let tween = FrameTween::new(&frame); + assert_eq!(tween.acceleration(), None); + assert_eq!(tween.has_custom_ease(), Some(false)); + assert_eq!(tween.use_single_ease_curve(), Some(false)); + assert!(tween.custom_eases().next().is_none()); + } +}