use crate::{
TransformValue,
property::{self, PropertySnapshot, PropertyValue},
runtime::{AnimationTargetId, AnimationTick},
};
#[derive(Debug, Clone, PartialEq, Default)]
pub struct EffectSnapshot {
pub opacity: Option<f32>,
pub width: Option<f32>,
pub height: Option<f32>,
pub padding: Option<f32>,
pub translation: Option<iced::Vector>,
pub transform: Option<TransformValue>,
pub rotate: Option<f32>,
pub scale: Option<f32>,
pub radius: Option<f32>,
pub background: Option<iced::Color>,
pub border_color: Option<iced::Color>,
pub text_color: Option<iced::Color>,
pub shadow: Option<iced::Shadow>,
}
impl EffectSnapshot {
#[must_use]
pub fn from_properties(properties: &PropertySnapshot) -> Self {
let mut effects = Self::default();
for entry in properties.entries() {
match entry.value() {
PropertyValue::Scalar(value) if *entry.spec() == property::OPACITY.raw() => {
effects.opacity = Some(*value);
}
PropertyValue::Scalar(value) if *entry.spec() == property::WIDTH.raw() => {
effects.width = Some(*value);
}
PropertyValue::Scalar(value) if *entry.spec() == property::HEIGHT.raw() => {
effects.height = Some(*value);
}
PropertyValue::Scalar(value) if *entry.spec() == property::PADDING.raw() => {
effects.padding = Some(*value);
}
PropertyValue::Scalar(value) if *entry.spec() == property::ROTATE.raw() => {
effects.rotate = Some(*value);
}
PropertyValue::Transform(value) if *entry.spec() == property::TRANSFORM.raw() => {
effects.transform = Some(*value);
}
PropertyValue::Vector2(value) if *entry.spec() == property::TRANSLATE.raw() => {
effects.translation = Some(*value);
}
PropertyValue::Scalar(value) if *entry.spec() == property::SCALE.raw() => {
effects.scale = Some(*value);
}
PropertyValue::Scalar(value) if *entry.spec() == property::RADIUS.raw() => {
effects.radius = Some(*value);
}
PropertyValue::Color(value) if *entry.spec() == property::BACKGROUND.raw() => {
effects.background = Some(*value);
}
PropertyValue::Color(value) if *entry.spec() == property::BORDER_COLOR.raw() => {
effects.border_color = Some(*value);
}
PropertyValue::Color(value) if *entry.spec() == property::TEXT_COLOR.raw() => {
effects.text_color = Some(*value);
}
PropertyValue::Shadow(value) if *entry.spec() == property::SHADOW.raw() => {
effects.shadow = Some(*value);
}
_ => {}
}
}
effects
}
#[must_use]
pub fn from_tick_for(tick: &AnimationTick, target: AnimationTargetId) -> Self {
tick.properties_for(target)
.map(Self::from_properties)
.unwrap_or_default()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self == &Self::default()
}
}
#[must_use]
pub fn effect_snapshot(properties: &PropertySnapshot) -> EffectSnapshot {
EffectSnapshot::from_properties(properties)
}
#[must_use]
pub fn tick_effect_snapshot_for(tick: &AnimationTick, target: AnimationTargetId) -> EffectSnapshot {
EffectSnapshot::from_tick_for(tick, target)
}