dvd_render/
grid.rs

1#[derive(Copy, Clone)]
2pub struct GridCell {
3	character: char,
4	fg_color: image::Rgba<u8>,
5	bg_color: image::Rgba<u8>
6}
7
8impl GridCell {
9	#[inline]
10	pub fn new(character: char) -> Self {
11		Self {
12			character,
13			fg_color: image::Rgba([u8::MAX, u8::MAX, u8::MAX, u8::MAX]),
14			bg_color: image::Rgba([u8::MIN, u8::MIN, u8::MIN, u8::MAX])
15		}
16	}
17
18	#[inline]
19	pub fn new_fg_color(character: char, fg_color: image::Rgba<u8>) -> Self {
20		Self {
21			character,
22			fg_color,
23			bg_color: image::Rgba([u8::MIN, u8::MIN, u8::MIN, u8::MAX])
24		}
25	}
26
27	#[inline]
28	pub fn new_full_color(
29		character: char,
30		fg_color: image::Rgba<u8>,
31		bg_color: image::Rgba<u8>
32	) -> Self {
33		Self {
34			character,
35			fg_color,
36			bg_color
37		}
38	}
39
40	#[inline]
41	pub fn space() -> Self {
42		Self::new(' ')
43	}
44
45	#[inline]
46	pub fn set_fg_color(&mut self, fg_color: image::Rgba<u8>) {
47		self.fg_color = fg_color;
48	}
49
50	#[inline]
51	pub fn set_bg_color(&mut self, bg_color: image::Rgba<u8>) {
52		self.bg_color = bg_color;
53	}
54
55	#[inline]
56	pub fn character(&self) -> char {
57		self.character
58	}
59
60	#[inline]
61	pub fn fg_color(&self) -> image::Rgba<u8> {
62		self.fg_color
63	}
64
65	#[inline]
66	pub fn bg_color(&self) -> image::Rgba<u8> {
67		self.bg_color
68	}
69}
70
71#[derive(Clone)]
72pub struct Grid<const W: usize, const H: usize> {
73	cells: [[GridCell; W]; H]
74}
75
76impl<const W: usize, const H: usize> Default for Grid<W, H> {
77	fn default() -> Self {
78		Self {
79			cells: [[GridCell::space(); W]; H]
80		}
81	}
82}
83
84impl<const W: usize, const H: usize> Grid<W, H> {
85	/// panics if out of bounds
86	pub fn set(&mut self, x: usize, y: usize, c: GridCell) {
87		self.cells[y][x] = c;
88	}
89
90	pub fn get_cell(&self, x: usize, y: usize) -> &GridCell {
91		&self.cells[y][x]
92	}
93
94	pub fn get_cell_mut(&mut self, x: usize, y: usize) -> &mut GridCell {
95		&mut self.cells[y][x]
96	}
97
98	pub(crate) fn chars(&self) -> Vec<char> {
99		let mut chars = vec![];
100
101		for row in self.cells {
102			for cell in row {
103				if !chars.contains(&cell.character) {
104					chars.push(cell.character);
105				}
106			}
107		}
108
109		chars
110	}
111
112	#[inline]
113	pub(crate) fn cells(&self) -> &[[GridCell; W]; H] {
114		&self.cells
115	}
116}