diff options
| author | cashmere <cashmere@autistici.org> | 2026-07-28 20:14:51 +0200 |
|---|---|---|
| committer | cashmere <cashmere@autistici.org> | 2026-07-28 20:14:51 +0200 |
| commit | 32438ce25f69de99d2523b98237f62cfa90890d7 (patch) | |
| tree | 3a712c347dec0889bdea3328b4c817c2d9392b27 /rust/crates/animation/src/resolve.rs | |
| parent | f5a82152394fde4d8f740f664ab7737b82861618 (diff) | |
| download | opencut-32438ce25f69de99d2523b98237f62cfa90890d7.tar.gz | |
feat: port domain crates to Rust (session 1 complete)
All green: cargo test --workspace (~300 tests, 0 failures).
- timeline (45): placement, resize/ripple, split, snap, retime,
audio separation, scene helpers
- animation (73): bezier solver, channel sampling, keyframe ops,
split/clamp/clone, param definitions
- commands (9): Command trait, BatchCommand, history, 19 concrete
commands, freeze-frame port 1:1
- audio (22): audible collection, range mixing, mastering limiter,
rms/peak buckets
- text (19): cosmic-text layout/measure/raster, backgrounds,
decorations
- graphics (52): full CSS gradient parser, tiny-skia raster,
4 shape definitions
- effect-defs (16): blur definition, registry, pass resolution
- mask-defs (19): all 9 mask renderers via tiny-skia
- renderer (6): node model + scene builder port
- desktop: GPUI projects screen wired to ProjectStore
- scene: factory (web defaults: v32, Main Track, 1920x1080, 30fps)
Diffstat (limited to 'rust/crates/animation/src/resolve.rs')
| -rw-r--r-- | rust/crates/animation/src/resolve.rs | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/rust/crates/animation/src/resolve.rs b/rust/crates/animation/src/resolve.rs new file mode 100644 index 00000000..ccb9dbad --- /dev/null +++ b/rust/crates/animation/src/resolve.rs @@ -0,0 +1,182 @@ +use scene::{ChannelData, ElementAnimations, ParamValue}; +use time::MediaTime; + +use crate::color::{LinearRgba, format_linear_rgba, parse_color_to_linear_rgba}; +use crate::interpolation::channel_value_at_time; + +pub fn get_element_local_time( + timeline_time: MediaTime, + element_start_time: MediaTime, + element_duration: MediaTime, +) -> MediaTime { + let local_time = timeline_time - element_start_time; + if local_time <= MediaTime::ZERO { + return MediaTime::ZERO; + } + if local_time >= element_duration { + return element_duration; + } + local_time +} + +pub fn resolve_animation_path_value_at_time( + animations: Option<&ElementAnimations>, + property_path: &str, + local_time: MediaTime, + fallback_value: &ParamValue, +) -> ParamValue { + let Some(data) = animations.and_then(|a| a.get(property_path)) else { + return fallback_value.clone(); + }; + + match data { + ChannelData::Single(channel) => match channel_value_at_time( + Some(channel), + local_time, + fallback_value, + ) { + value => value, + }, + ChannelData::Composite(components) => { + let ParamValue::String(fallback_color) = fallback_value else { + return fallback_value.clone(); + }; + let (Some(r), Some(g), Some(b), Some(a)) = ( + components.get("r"), + components.get("g"), + components.get("b"), + components.get("a"), + ) else { + return fallback_value.clone(); + }; + let Some(fallback_components) = parse_color_to_linear_rgba(fallback_color) else { + return fallback_value.clone(); + }; + + let component_at = |channel: &scene::AnimationChannel, fallback: f64| { + match channel_value_at_time( + Some(channel), + local_time, + &ParamValue::Number(fallback), + ) { + ParamValue::Number(value) => value, + _ => fallback, + } + }; + ParamValue::String(format_linear_rgba(&LinearRgba { + r: component_at(r, fallback_components.r), + g: component_at(g, fallback_components.g), + b: component_at(b, fallback_components.b), + a: component_at(a, fallback_components.a), + })) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use scene::{AnimationChannel, ScalarAnimationKey, ScalarChannel, ScalarSegmentType, TangentMode}; + use std::collections::BTreeMap; + + fn at(time: i64) -> MediaTime { + MediaTime::from_ticks(time) + } + + #[test] + fn local_time_clamps_to_element_range() { + assert_eq!(get_element_local_time(at(50), at(100), at(200)), at(0)); + assert_eq!(get_element_local_time(at(150), at(100), at(200)), at(50)); + assert_eq!(get_element_local_time(at(400), at(100), at(200)), at(200)); + } + + #[test] + fn resolves_scalar_path_value() { + let channel = AnimationChannel::Scalar(ScalarChannel { + keys: vec![ + ScalarAnimationKey { + id: "a".to_string(), + time: at(0), + value: 0.0, + left_handle: None, + right_handle: None, + segment_to_next: ScalarSegmentType::Linear, + tangent_mode: TangentMode::Flat, + }, + ScalarAnimationKey { + id: "b".to_string(), + time: at(100), + value: 1.0, + left_handle: None, + right_handle: None, + segment_to_next: ScalarSegmentType::Linear, + tangent_mode: TangentMode::Flat, + }, + ], + extrapolation: None, + }); + let mut animations = BTreeMap::new(); + animations.insert("opacity".to_string(), ChannelData::Single(channel)); + assert_eq!( + resolve_animation_path_value_at_time( + Some(&animations), + "opacity", + at(50), + &ParamValue::Number(1.0), + ), + ParamValue::Number(0.5) + ); + assert_eq!( + resolve_animation_path_value_at_time( + Some(&animations), + "volume", + at(50), + &ParamValue::Number(1.0), + ), + ParamValue::Number(1.0) + ); + } + + #[test] + fn resolves_composite_color_path_value() { + let component = |value: f64| { + AnimationChannel::Scalar(ScalarChannel { + keys: vec![ScalarAnimationKey { + id: "k".to_string(), + time: at(0), + value, + left_handle: None, + right_handle: None, + segment_to_next: ScalarSegmentType::Linear, + tangent_mode: TangentMode::Flat, + }], + extrapolation: None, + }) + }; + let mut components = BTreeMap::new(); + components.insert("r".to_string(), component(1.0)); + components.insert("g".to_string(), component(0.0)); + components.insert("b".to_string(), component(0.0)); + components.insert("a".to_string(), component(1.0)); + let mut animations = BTreeMap::new(); + animations.insert("color".to_string(), ChannelData::Composite(components)); + assert_eq!( + resolve_animation_path_value_at_time( + Some(&animations), + "color", + at(0), + &ParamValue::String("#ffffff".to_string()), + ), + ParamValue::String("#ff0000".to_string()) + ); + assert_eq!( + resolve_animation_path_value_at_time( + Some(&animations), + "color", + at(0), + &ParamValue::Number(0.0), + ), + ParamValue::Number(0.0) + ); + } +} |
