iced_core/
transformation.rs

1use crate::{Point, Rectangle, Size, Vector};
2
3use glam::{Mat4, Vec3, Vec4};
4use std::ops::Mul;
5
6/// A 2D transformation matrix.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct Transformation(Mat4);
9
10impl Transformation {
11    /// A [`Transformation`] that preserves whatever is transformed.
12    pub const IDENTITY: Self = Self(Mat4::IDENTITY);
13
14    /// Creates an orthographic projection.
15    #[rustfmt::skip]
16    pub fn orthographic(width: u32, height: u32) -> Transformation {
17        Transformation(Mat4::orthographic_rh_gl(
18            0.0, width as f32,
19            height as f32, 0.0,
20            -1.0, 1.0
21        ))
22    }
23
24    /// Creates a translate transformation.
25    pub fn translate(x: f32, y: f32) -> Transformation {
26        Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0)))
27    }
28
29    /// Creates a uniform scaling transformation.
30    pub fn scale(scaling: f32) -> Transformation {
31        Transformation(Mat4::from_scale(Vec3::new(scaling, scaling, 1.0)))
32    }
33
34    /// Returns the scale factor of the [`Transformation`].
35    pub fn scale_factor(&self) -> f32 {
36        self.0.x_axis.x
37    }
38
39    /// Returns the translation of the [`Transformation`].
40    pub fn translation(&self) -> Vector {
41        Vector::new(self.0.w_axis.x, self.0.w_axis.y)
42    }
43}
44
45impl Default for Transformation {
46    fn default() -> Self {
47        Transformation::IDENTITY
48    }
49}
50
51impl Mul for Transformation {
52    type Output = Self;
53
54    fn mul(self, rhs: Self) -> Self {
55        Transformation(self.0 * rhs.0)
56    }
57}
58
59impl Mul<Transformation> for Point {
60    type Output = Self;
61
62    fn mul(self, transformation: Transformation) -> Self {
63        let point = transformation
64            .0
65            .mul_vec4(Vec4::new(self.x, self.y, 1.0, 1.0));
66
67        Point::new(point.x, point.y)
68    }
69}
70
71impl Mul<Transformation> for Vector {
72    type Output = Self;
73
74    fn mul(self, transformation: Transformation) -> Self {
75        let new_vector = transformation
76            .0
77            .mul_vec4(Vec4::new(self.x, self.y, 1.0, 0.0));
78
79        Vector::new(new_vector.x, new_vector.y)
80    }
81}
82
83impl Mul<Transformation> for Size {
84    type Output = Self;
85
86    fn mul(self, transformation: Transformation) -> Self {
87        let new_size = transformation.0.mul_vec4(Vec4::new(
88            self.width,
89            self.height,
90            1.0,
91            0.0,
92        ));
93
94        Size::new(new_size.x, new_size.y)
95    }
96}
97
98impl Mul<Transformation> for Rectangle {
99    type Output = Self;
100
101    fn mul(self, transformation: Transformation) -> Self {
102        let position = self.position();
103        let size = self.size();
104
105        Self::new(position * transformation, size * transformation)
106    }
107}
108
109impl AsRef<[f32; 16]> for Transformation {
110    fn as_ref(&self) -> &[f32; 16] {
111        self.0.as_ref()
112    }
113}
114
115impl From<Transformation> for [f32; 16] {
116    fn from(t: Transformation) -> [f32; 16] {
117        *t.as_ref()
118    }
119}
120
121impl From<Transformation> for Mat4 {
122    fn from(transformation: Transformation) -> Self {
123        transformation.0
124    }
125}