Skip to main content

ferroui_base/media/
matrix_transform.rs

1use crate::media::{Transform, TransformImpl};
2use crate::{
3    ferro_class, ferro_property, instantiate, FerroObjectImpl, FerroObjectImplExt, FerroProperty,
4    FerroPropertyChangedEventArgs, Matrix,
5    Ref, StyledProperty,
6};
7
8/// Transforms a visual according to a [`Matrix`].
9#[repr(C)]
10pub struct MatrixTransform {
11    base: Transform,
12}
13
14ferro_class!(MatrixTransform: Transform);
15crate::ferro_class_info!(MatrixTransform { new: MatrixTransform::new });
16
17impl FerroObjectImpl for MatrixTransform {
18    fn on_property_changed(this: &Self, change: &FerroPropertyChangedEventArgs<'_>) {
19        Self::parent_on_property_changed(this, change);
20
21        if change.property() == Self::matrix_property().as_property() {
22            this.raise_changed();
23        }
24    }
25}
26
27impl TransformImpl for MatrixTransform {
28    fn value(this: &Self) -> Matrix {
29        this.matrix()
30    }
31}
32
33crate::ferro_properties! { impl MatrixTransform {
34    ferro_property!(pub fn matrix_property() -> StyledProperty<Matrix> {
35        FerroProperty::register::<MatrixTransform, _>("Matrix", Matrix::IDENTITY)
36    });
37} }
38
39impl MatrixTransform {
40    /// Creates the class data.
41    pub fn construct() -> Self {
42        Self { base: Transform::construct() }
43    }
44
45    pub fn new() -> Ref<Self> {
46        instantiate(Self::construct())
47    }
48
49    /// Creates a transform with the given matrix.
50    pub fn with_matrix(matrix: Matrix) -> Ref<Self> {
51        let result = Self::new();
52        result.set_matrix(matrix);
53        result
54    }
55
56    /// The matrix.
57    pub fn matrix(&self) -> Matrix {
58        self.get_value(Self::matrix_property())
59    }
60
61    pub fn set_matrix(&self, value: Matrix) {
62        self.set_value(Self::matrix_property(), value)
63    }
64}