clutter/auto/
animatable.rs

1use crate::Interval;
2use glib::{object::IsA, translate::*};
3use std::fmt;
4
5glib_wrapper! {
6    pub struct Animatable(Interface<ffi::ClutterAnimatable>);
7
8    match fn {
9        get_type => || ffi::clutter_animatable_get_type(),
10    }
11}
12
13pub const NONE_ANIMATABLE: Option<&Animatable> = None;
14
15/// Trait containing all `Animatable` methods.
16///
17/// # Implementors
18///
19/// [`Actor`](struct.Actor.html), [`Animatable`](struct.Animatable.html), [`Box`](struct.Box.html), [`Clone`](struct.Clone.html), [`Group`](struct.Group.html), [`Rectangle`](struct.Rectangle.html), [`ScrollActor`](struct.ScrollActor.html), [`Stage`](struct.Stage.html), [`Text`](struct.Text.html), [`Texture`](struct.Texture.html)
20pub trait AnimatableExt: 'static {
21    /// Finds the `gobject::ParamSpec` for `property_name`
22    /// ## `property_name`
23    /// the name of the animatable property to find
24    ///
25    /// # Returns
26    ///
27    /// The `gobject::ParamSpec` for the given property
28    ///  or `None`
29    fn find_property(&self, property_name: &str) -> Option<glib::ParamSpec>;
30
31    /// Retrieves the current state of `property_name` and sets `value` with it
32    /// ## `property_name`
33    /// the name of the animatable property to retrieve
34    /// ## `value`
35    /// a `gobject::Value` initialized to the type of the property to retrieve
36    fn get_initial_state(&self, property_name: &str, value: &mut glib::Value);
37
38    /// Asks a `Animatable` implementation to interpolate a
39    /// a named property between the initial and final values of
40    /// a `Interval`, using `progress` as the interpolation
41    /// value, and store the result inside `value`.
42    ///
43    /// This function should be used for every property animation
44    /// involving `Animatable`<!-- -->s.
45    ///
46    /// This function replaces `Animatable::animate_property`.
47    /// ## `property_name`
48    /// the name of the property to interpolate
49    /// ## `interval`
50    /// a `Interval` with the animation range
51    /// ## `progress`
52    /// the progress to use to interpolate between the
53    ///  initial and final values of the `interval`
54    /// ## `value`
55    /// return location for an initialized `gobject::Value`
56    ///  using the same type of the `interval`
57    ///
58    /// # Returns
59    ///
60    /// `true` if the interpolation was successful,
61    ///  and `false` otherwise
62    fn interpolate_value<P: IsA<Interval>>(
63        &self,
64        property_name: &str,
65        interval: &P,
66        progress: f64,
67    ) -> Option<glib::Value>;
68
69    /// Sets the current state of `property_name` to `value`
70    /// ## `property_name`
71    /// the name of the animatable property to set
72    /// ## `value`
73    /// the value of the animatable property to set
74    fn set_final_state(&self, property_name: &str, value: &glib::Value);
75}
76
77impl<O: IsA<Animatable>> AnimatableExt for O {
78    fn find_property(&self, property_name: &str) -> Option<glib::ParamSpec> {
79        unsafe {
80            from_glib_none(ffi::clutter_animatable_find_property(
81                self.as_ref().to_glib_none().0,
82                property_name.to_glib_none().0,
83            ))
84        }
85    }
86
87    fn get_initial_state(&self, property_name: &str, value: &mut glib::Value) {
88        unsafe {
89            ffi::clutter_animatable_get_initial_state(
90                self.as_ref().to_glib_none().0,
91                property_name.to_glib_none().0,
92                value.to_glib_none_mut().0,
93            );
94        }
95    }
96
97    fn interpolate_value<P: IsA<Interval>>(
98        &self,
99        property_name: &str,
100        interval: &P,
101        progress: f64,
102    ) -> Option<glib::Value> {
103        unsafe {
104            let mut value = glib::Value::uninitialized();
105            let ret = from_glib(ffi::clutter_animatable_interpolate_value(
106                self.as_ref().to_glib_none().0,
107                property_name.to_glib_none().0,
108                interval.as_ref().to_glib_none().0,
109                progress,
110                value.to_glib_none_mut().0,
111            ));
112            if ret {
113                Some(value)
114            } else {
115                None
116            }
117        }
118    }
119
120    fn set_final_state(&self, property_name: &str, value: &glib::Value) {
121        unsafe {
122            ffi::clutter_animatable_set_final_state(
123                self.as_ref().to_glib_none().0,
124                property_name.to_glib_none().0,
125                value.to_glib_none().0,
126            );
127        }
128    }
129}
130
131impl fmt::Display for Animatable {
132    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
133        write!(f, "Animatable")
134    }
135}