Repository: bevyengine/bevy
File: crates/bevy_ui/src/ui_transform.rs (Rust)

Code before:
}

impl UiGlobalTransform {
    /// If the transform is invertible returns its inverse.
    /// Otherwise returns `None`.
    #[inline]
    pub fn try_inverse(&self) -> Option<Affine2> {
        (self.matrix2.determinant() != 0.).then_some(self.inverse())
    }

    /// Creates a `UiGlobalTransform` from the given 2D translation.
    #[inline]
    pub fn from_translation(translation: Vec2) -> Self {
        Self(Affine2::from_translation(translation))
    }

    /// Creates a `UiGlobalTransform` from the given 2D translation.
    #[inline]
    pub fn from_xy(x: f32, y: f32) -> Self {
        Self::from_translation(Vec2::new(x, y))
    }

    /// Creates a `UiGlobalTransform` from the given rotation.
    #[inline]
    pub fn from_rotation(rotation: Rot2) -> Self {
        Self(Affine2::from_angle(rotation.as_radians()))
    }

    /// Creates a `UiGlobalTransform` from the given scaling.
    #[inline]
    pub fn from_scale(scale: Vec2) -> Self {
        Self(Affine2::from_scale(scale))
    }

    /// Extracts scale, angle and translation from self.
    /// The transform is expected to be non-degenerate and without shearing, or the output will be invalid.
    #[inline]
    pub fn to_scale_angle_translation(&self) -> (Vec2, f32, Vec2) {
        self.0.to_scale_angle_translation()
    }

    /// Returns the transform as an [`Affine2`]
    #[inline]
    pub fn affine(&self) -> Affine2 {
        self.0
    }
}

impl From<Affine2> for UiGlobalTransform {
    fn from(value: Affine2) -> Self {
        Self(value)

Review comment (suggestion):
Doing
```suggestion
        Self(Affine2::from_mat2(rotation.into()))
```
Skips all floating point operations (notably trig and inverse trig).
That said we probably should offer a `From<Rot2> for Affine2` like we do with Isometry2d and Mat2

Code after:
}

impl UiGlobalTransform {
    /// If the transform is invertible returns its inverse.
    /// Otherwise returns `None`.
    #[inline]
    pub fn try_inverse(&self) -> Option<Affine2> {
        (self.matrix2.determinant() != 0.).then_some(self.inverse())
    }

    /// Creates a `UiGlobalTransform` from the given 2D translation.
    #[inline]
    pub fn from_translation(translation: Vec2) -> Self {
        Self(Affine2::from_translation(translation))
    }

    /// Creates a `UiGlobalTransform` from the given 2D translation.
    #[inline]
    pub fn from_xy(x: f32, y: f32) -> Self {
        Self::from_translation(Vec2::new(x, y))
    }

    /// Creates a `UiGlobalTransform` from the given rotation.
    #[inline]
    pub fn from_rotation(rotation: Rot2) -> Self {
        Self(Affine2::from_mat2(rotation.into()))
    }

    /// Creates a `UiGlobalTransform` from the given scaling.
    #[inline]
    pub fn from_scale(scale: Vec2) -> Self {
        Self(Affine2::from_scale(scale))
    }

    /// Extracts scale, angle and translation from self.
    /// The transform is expected to be non-degenerate and without shearing, or the output will be invalid.
    #[inline]
    pub fn to_scale_angle_translation(&self) -> (Vec2, f32, Vec2) {
        self.0.to_scale_angle_translation()
    }

    /// Returns the transform as an [`Affine2`]
    #[inline]
    pub fn affine(&self) -> Affine2 {
        self.0
    }
}

impl From<Affine2> for UiGlobalTransform {
    fn from(value: Affine2) -> Self {
        Self(value)