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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use core::marker::PhantomData;
use embedded_graphics_core::geometry::{Point, Size};
use invert_axis::{InvertX, InvertXY, InvertY, NoInvert};
pub trait Layout {
fn map(&self, p: Point) -> Option<usize>;
fn size(&self) -> Size;
}
pub struct Rectangular<I> {
size: Size,
invert_axis: PhantomData<I>,
}
impl<I> Rectangular<I> {
const fn new_common(width: u32, height: u32) -> Self {
Self {
size: Size::new(width, height),
invert_axis: PhantomData,
}
}
}
impl Rectangular<NoInvert> {
pub const fn new(width: u32, height: u32) -> Self {
Self::new_common(width, height)
}
}
impl Rectangular<InvertX> {
pub const fn new_invert_x(width: u32, height: u32) -> Self {
Self::new_common(width, height)
}
}
impl Rectangular<InvertY> {
pub const fn new_invert_y(width: u32, height: u32) -> Self {
Self::new_common(width, height)
}
}
impl Rectangular<InvertXY> {
pub const fn new_invert_xy(width: u32, height: u32) -> Self {
Self::new_common(width, height)
}
}
macro_rules! impl_layout {
($invert_type:ty, $invert_x:expr, $invert_y:expr) => {
impl Layout for Rectangular<$invert_type> {
fn map(&self, mut p: Point) -> Option<usize> {
if $invert_x {
p.x = (self.size.width - 1) as i32 - p.x;
}
if $invert_y {
p.y = (self.size.height - 1) as i32 - p.y;
}
(p.x >= 0
&& p.y >= 0
&& p.x < self.size.width as i32
&& p.y < self.size.height as i32)
.then(|| p.y as usize * self.size.width as usize + p.x as usize)
}
fn size(&self) -> Size {
return self.size;
}
}
};
}
impl_layout!(NoInvert, false, false);
impl_layout!(InvertX, true, false);
impl_layout!(InvertY, false, true);
impl_layout!(InvertXY, true, true);
pub mod invert_axis {
pub enum NoInvert {}
pub enum InvertX {}
pub enum InvertY {}
pub enum InvertXY {}
}