1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::ops::{Add, Mul, Neg, Sub};

use mat32;
use num_traits::{Float, One, Zero};
use specs::{Component, DenseVecStorage, FlaggedStorage};

#[derive(Debug, Serialize, Deserialize)]
pub struct Transform2D<T> {
    pub position: [T; 2],
    pub scale: [T; 2],
    pub rotation: T,
}

impl<T> Component for Transform2D<T>
where
    T: 'static + Sync + Send,
{
    type Storage = FlaggedStorage<Self, DenseVecStorage<Self>>;
}

impl<T> Default for Transform2D<T>
where
    T: Zero + One,
{
    #[inline(always)]
    fn default() -> Self {
        Self::new([T::zero(), T::zero()], [T::one(), T::one()], T::zero())
    }
}

impl<T> Transform2D<T>
where
    T: Zero + One,
{
    #[inline(always)]
    pub fn new(position: [T; 2], scale: [T; 2], rotation: T) -> Self {
        Transform2D {
            position: position,
            scale: scale,
            rotation: rotation,
        }
    }

    #[inline]
    pub fn with_position(mut self, position: [T; 2]) -> Self {
        self.position = position;
        self
    }
    #[inline]
    pub fn with_scale(mut self, scale: [T; 2]) -> Self {
        self.scale = scale;
        self
    }
    #[inline]
    pub fn with_rotation(mut self, rotation: T) -> Self {
        self.rotation = rotation;
        self
    }

    #[inline]
    pub fn set_position(&mut self, position: [T; 2]) -> &mut Self {
        self.position = position;
        self
    }
    #[inline]
    pub fn set_scale(&mut self, scale: [T; 2]) -> &mut Self {
        self.scale = scale;
        self
    }
    #[inline]
    pub fn set_rotation(&mut self, rotation: T) -> &mut Self {
        self.rotation = rotation;
        self
    }

    #[inline(always)]
    pub fn matrix(&self) -> [T; 6]
    where
        T: Float,
        for<'a, 'b> &'a T: Mul<&'b T, Output = T>
            + Neg<Output = T>
            + Add<&'b T, Output = T>
            + Sub<&'b T, Output = T>,
    {
        let mut matrix = mat32::new_identity();
        mat32::compose(&mut matrix, &self.position, &self.scale, &self.rotation);
        matrix
    }
}