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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#![no_std]
#![deny(warnings)]

pub mod drivers;
pub mod layout;

mod display;
mod sprites;
mod widgets;

pub use display::*;
pub use sprites::*;
pub use widgets::*;

pub type Glyph = u8;
pub type SpriteId = u8;

pub trait Canvas {
    fn draw(&mut self, bounds: Rectangle, bitmap: &[u8]);
}

pub trait Display {
    fn render(&mut self, req: RenderRequest);
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RenderRequest {
    pub origin: Point,
    pub sprite_id: SpriteId,
    pub glyph: Glyph,
}

impl RenderRequest {
    pub fn new(origin: Point, sprite_id: SpriteId, glyph: Glyph) -> Self {
        Self {
            origin,
            sprite_id,
            glyph,
        }
    }

    pub fn from_bytes(bytes: &[u8]) -> Self {
        assert!(bytes.len() == 4);
        Self::new(Point::new(bytes[0], bytes[1]), bytes[2], bytes[3])
    }

    pub fn as_bytes(&self) -> [u8; 4] {
        [self.origin.x, self.origin.y, self.sprite_id, self.glyph]
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Point {
    pub x: u8,
    pub y: u8,
}

impl Point {
    pub const fn zero() -> Self {
        Self::new(0, 0)
    }

    pub const fn new(x: u8, y: u8) -> Self {
        Self { x, y }
    }
}

impl From<Point> for (u8, u8) {
    fn from(p: Point) -> Self {
        (p.x, p.y)
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Size {
    pub width: u8,
    pub height: u8,
}

impl Size {
    pub const fn new(width: u8, height: u8) -> Self {
        Self { width, height }
    }
}

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Rectangle {
    pub origin: Point,
    pub size: Size,
}

impl Rectangle {
    pub const fn new(origin: Point, size: Size) -> Self {
        Self { origin, size }
    }

    pub fn start(&self) -> Point {
        self.origin
    }

    pub fn end(&self) -> Point {
        let origin = self.origin;
        let size = self.size;
        Point::new(origin.x + size.width, origin.y + size.height)
    }
}

pub enum Glyphs {
    Single,
    Sequential(u8),
    Alphabet(&'static [Glyph]),
}

#[allow(clippy::len_without_is_empty)]
impl Glyphs {
    pub fn index(&self, glyph: Glyph) -> Option<usize> {
        match self {
            Glyphs::Single => Some(0),
            Glyphs::Sequential(len) if glyph < *len => Some(glyph as _),
            Glyphs::Alphabet(glyphs) => glyphs.iter().position(|g| *g == glyph),
            _ => None,
        }
    }

    pub const fn len(&self) -> usize {
        match self {
            Glyphs::Single => 1,
            Glyphs::Sequential(len) => *len as usize,
            Glyphs::Alphabet(glyphs) => glyphs.len(),
        }
    }
}