1use std::ops::Mul;
2
3use crate::{geometry::FRect, DefaultMatrix, Float};
4
5pub trait MatrixBackend: Clone + Mul<Self, Output = Self> + Sized {
6 fn identity() -> Self
7 where
8 Self: Sized;
9 fn from_row(scx: Float, sky: Float, skx: Float, scy: Float, tx: Float, ty: Float) -> Self
10 where
11 Self: Sized;
12 fn from_translate(x: Float, y: Float) -> Self
13 where
14 Self: Sized;
15 fn from_scale(x: Float, y: Float) -> Self
16 where
17 Self: Sized;
18 fn from_rotate(angle: Float) -> Self
19 where
20 Self: Sized;
21 fn pre_rotate(&mut self, angle: Float);
22 fn pre_scale(&mut self, x: Float, y: Float);
23 fn pre_translate(&mut self, x: Float, y: Float);
24 fn post_rotate(&mut self, angle: Float);
25 fn post_scale(&mut self, x: Float, y: Float);
26 fn post_translate(&mut self, x: Float, y: Float);
27 fn map_rect(&self, rect: FRect) -> FRect;
28}
29
30impl<T: MatrixBackend> Default for Matrix<T> {
31 fn default() -> Self {
32 Self::identity()
33 }
34}
35
36pub struct Matrix<T: MatrixBackend> {
37 pub(crate) backend: T,
38}
39
40impl<T: MatrixBackend> Matrix<T> {
41 pub fn identity() -> Self {
42 Self {
43 backend: T::identity(),
44 }
45 }
46
47 pub fn from_row(scx: Float, sky: Float, skx: Float, scy: Float, tx: Float, ty: Float) -> Self {
48 Self {
49 backend: T::from_row(scx, sky, skx, scy, tx, ty),
50 }
51 }
52
53 pub fn from_translate(x: Float, y: Float) -> Self {
54 Self {
55 backend: T::from_translate(x, y),
56 }
57 }
58
59 pub fn from_scale(x: Float, y: Float) -> Self {
60 Self {
61 backend: T::from_scale(x, y),
62 }
63 }
64
65 pub fn from_rotate(angle: Float) -> Self {
66 Self {
67 backend: T::from_rotate(angle),
68 }
69 }
70
71 pub fn pre_rotate(&mut self, angle: Float) {
72 self.backend.pre_rotate(angle)
73 }
74
75 pub fn pre_scale(&mut self, x: Float, y: Float) {
76 self.backend.pre_scale(x, y)
77 }
78
79 pub fn pre_translate(&mut self, x: Float, y: Float) {
80 self.backend.pre_translate(x, y)
81 }
82
83 pub fn post_rotate(&mut self, angle: Float) {
84 self.backend.post_rotate(angle)
85 }
86
87 pub fn post_scale(&mut self, x: Float, y: Float) {
88 self.backend.post_scale(x, y)
89 }
90
91 pub fn post_translate(&mut self, x: Float, y: Float) {
92 self.backend.post_translate(x, y)
93 }
94
95 pub fn map_rect(&self, rect: FRect) -> FRect {
96 self.backend.map_rect(rect)
97 }
98}
99
100impl Matrix<DefaultMatrix> {
101 pub fn default_identity() -> Self {
102 Self::identity()
103 }
104
105 pub fn default_from_row(scx: Float, sky: Float, skx: Float, scy: Float, tx: Float, ty: Float) -> Self {
106 Self::from_row(scx, sky, skx, scy, tx, ty)
107 }
108
109 pub fn default_from_translate(x: Float, y: Float) -> Self {
110 Self::from_translate(x, y)
111 }
112
113 pub fn default_from_scale(x: Float, y: Float) -> Self {
114 Self::from_scale(x, y)
115 }
116
117 pub fn default_from_rotate(angle: Float) -> Self {
118 Self::from_rotate(angle)
119 }
120}