Skip to main content

ferroui_base/rendering/composition/animations/
composition_animation.rs

1use super::{PropertySetSnapshot, PropertySetSnapshotSource};
2use crate::media::Color;
3use crate::numerics::{Matrix3x2, Matrix4x4, Quaternion, Vector2, Vector3, Vector4};
4use crate::rendering::composition::expressions::ExpressionVariant;
5use crate::rendering::composition::{AsCompositionObject, CompositionObject, CompositionPropertySet, Compositor};
6use std::cell::RefCell;
7use std::rc::Rc;
8
9/// This is the base class for ExpressionAnimation and KeyFrameAnimation.
10///
11/// Use the `start_animation` method of a composition object to start the
12/// animation. Value parameters (as opposed to reference parameters which
13/// are set using [`set_reference_parameter`](Self::set_reference_parameter))
14/// are copied and "embedded" into an expression at the time
15/// `start_animation` is called. Changing the value of the variable after
16/// `start_animation` is called will not affect the value of the
17/// ExpressionAnimation. See the remarks of
18/// [`ExpressionAnimation`](super::ExpressionAnimation) for additional
19/// information.
20///
21/// A class derives from it by embedding it (`base`) and dereferencing to
22/// it; the class implements [`ICompositionAnimation`](super::ICompositionAnimation),
23/// whose `Rc<dyn ..>` is the handle of an animation of any class.
24pub struct CompositionAnimation {
25    object: CompositionObject,
26    property_set: Rc<CompositionPropertySet>,
27    target: RefCell<Option<String>>,
28}
29
30impl CompositionAnimation {
31    pub(crate) fn new(compositor: &Rc<Compositor>) -> Self {
32        Self {
33            object: CompositionObject::new(compositor, None),
34            property_set: CompositionPropertySet::new(compositor),
35            target: RefCell::new(None),
36        }
37    }
38
39    /// The embedded `CompositionObject`.
40    pub fn object(&self) -> &CompositionObject {
41        &self.object
42    }
43
44    /// The associated compositor.
45    pub fn compositor(&self) -> &Rc<Compositor> {
46        self.object.compositor()
47    }
48
49    /// Clears all of the parameters of the animation.
50    pub fn clear_all_parameters(&self) {
51        self.property_set.clear_all()
52    }
53
54    /// Clears a parameter from the animation.
55    pub fn clear_parameter(&self, key: &str) {
56        self.property_set.clear(key)
57    }
58
59    fn set_variant(&self, key: &str, value: ExpressionVariant) {
60        self.property_set.set(key, value)
61    }
62
63    pub fn set_color_parameter(&self, key: &str, value: Color) {
64        self.set_variant(key, value.into())
65    }
66
67    pub fn set_matrix3x2_parameter(&self, key: &str, value: Matrix3x2) {
68        self.set_variant(key, value.into())
69    }
70
71    pub fn set_matrix4x4_parameter(&self, key: &str, value: Matrix4x4) {
72        self.set_variant(key, value.into())
73    }
74
75    pub fn set_quaternion_parameter(&self, key: &str, value: Quaternion) {
76        self.set_variant(key, value.into())
77    }
78
79    pub fn set_reference_parameter(&self, key: &str, composition_object: Rc<dyn AsCompositionObject>) {
80        self.property_set.set_object(key, composition_object)
81    }
82
83    pub fn set_scalar_parameter(&self, key: &str, value: f32) {
84        self.set_variant(key, value.into())
85    }
86
87    pub fn set_vector2_parameter(&self, key: &str, value: Vector2) {
88        self.set_variant(key, value.into())
89    }
90
91    pub fn set_vector3_parameter(&self, key: &str, value: Vector3) {
92        self.set_variant(key, value.into())
93    }
94
95    pub fn set_vector4_parameter(&self, key: &str, value: Vector4) {
96        self.set_variant(key, value.into())
97    }
98
99    /// The name of the property the animation targets.
100    pub fn target(&self) -> Option<String> {
101        self.target.borrow().clone()
102    }
103
104    pub fn set_target(&self, value: Option<String>) {
105        *self.target.borrow_mut() = value;
106    }
107
108    pub(crate) fn create_snapshot(&self) -> Rc<PropertySetSnapshot> {
109        Rc::new(self.property_set.snapshot())
110    }
111
112    /// The parameters of the animation in the form that is sent to the
113    /// server.
114    pub(crate) fn create_snapshot_source(&self) -> PropertySetSnapshotSource {
115        self.property_set.snapshot_source()
116    }
117}