Skip to main content

ferroui_base/media/
scale_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/// Scale a visual.
9#[repr(C)]
10pub struct ScaleTransform {
11    base: Transform,
12}
13
14ferro_class!(ScaleTransform: Transform);
15crate::ferro_class_info!(ScaleTransform { new: ScaleTransform::new });
16
17impl FerroObjectImpl for ScaleTransform {
18    fn on_property_changed(this: &Self, change: &FerroPropertyChangedEventArgs<'_>) {
19        Self::parent_on_property_changed(this, change);
20
21        if change.property() == Self::scale_x_property().as_property() || change.property() == Self::scale_y_property().as_property() {
22            this.raise_changed();
23        }
24    }
25}
26
27impl TransformImpl for ScaleTransform {
28    fn value(this: &Self) -> Matrix {
29        Matrix::create_scale(this.scale_x(), this.scale_y())
30    }
31}
32
33crate::ferro_properties! { impl ScaleTransform {
34    ferro_property!(pub fn scale_x_property() -> StyledProperty<f64> {
35        FerroProperty::register::<ScaleTransform, _>("ScaleX", 1.0)
36    });
37
38    ferro_property!(pub fn scale_y_property() -> StyledProperty<f64> {
39        FerroProperty::register::<ScaleTransform, _>("ScaleY", 1.0)
40    });
41} }
42
43impl ScaleTransform {
44    /// Creates the class data.
45    pub fn construct() -> Self {
46        Self { base: Transform::construct() }
47    }
48
49    pub fn new() -> Ref<Self> {
50        instantiate(Self::construct())
51    }
52
53    pub fn with_scale(scale_x: f64, scale_y: f64) -> Ref<Self> {
54        let result = Self::new();
55        result.set_scale_x(scale_x);
56        result.set_scale_y(scale_y);
57        result
58    }
59
60    /// The horizontal scale.
61    pub fn scale_x(&self) -> f64 {
62        self.get_value(Self::scale_x_property())
63    }
64
65    pub fn set_scale_x(&self, value: f64) {
66        self.set_value(Self::scale_x_property(), value)
67    }
68
69    /// The vertical scale.
70    pub fn scale_y(&self) -> f64 {
71        self.get_value(Self::scale_y_property())
72    }
73
74    pub fn set_scale_y(&self, value: f64) {
75        self.set_value(Self::scale_y_property(), value)
76    }
77}