pittore 0.2.4

Simple toolkit for 2D visualization based on wgpu.
Documentation
use glam::{Affine2, Mat2, Vec2};

#[derive(Debug, Clone, Copy)]
pub struct Transform2d {
    pub translation: Vec2,
    pub rotation: Mat2,
    pub scale: Vec2,
}

impl Default for Transform2d {
    fn default() -> Self {
        Transform2d::IDENTITY
    }
}

impl Transform2d {
    pub const IDENTITY: Self = Transform2d {
        translation: Vec2::ZERO,
        rotation: Mat2::IDENTITY,
        scale: Vec2::ONE,
    };

    pub const fn from_xy(x: f32, y: f32) -> Self {
        Transform2d {
            translation: Vec2::new(x, y),
            ..Self::IDENTITY
        }
    }

    pub const fn from_translation(translation: Vec2) -> Self {
        Transform2d {
            translation,
            ..Self::IDENTITY
        }
    }

    pub const fn from_rotation(rotation: Mat2) -> Self {
        Transform2d {
            rotation,
            ..Self::IDENTITY
        }
    }

    pub fn from_angle(angle: f32) -> Self {
        Transform2d {
            rotation: Mat2::from_angle(angle),
            ..Self::IDENTITY
        }
    }

    pub const fn from_scale(scale: Vec2) -> Self {
        Transform2d {
            scale,
            ..Self::IDENTITY
        }
    }

    pub const fn with_translation(mut self, translation: Vec2) -> Self {
        self.translation = translation;
        self
    }

    pub const fn with_rotation(mut self, rotation: Mat2) -> Self {
        self.rotation = rotation;
        self
    }

    pub fn with_angle(mut self, angle: f32) -> Self {
        self.rotation = Mat2::from_angle(angle);
        self
    }

    pub const fn with_scale(mut self, scale: Vec2) -> Self {
        self.scale = scale;
        self
    }

    pub fn compute_affine(&self) -> Affine2 {
        Affine2 {
            matrix2: self.rotation
                * Mat2::from_cols(Vec2::new(self.scale.x, 0.0), Vec2::new(0.0, self.scale.y)),
            translation: self.translation,
        }
    }
}