Skip to content

Commit 8cff63d

Browse files
committed
Update the lib
1 parent 5b7bce5 commit 8cff63d

1 file changed

Lines changed: 57 additions & 16 deletions

File tree

src/lib.rs

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ enum PixelState {
1010
VLine,
1111
Cross,
1212
Pixel,
13+
Filled,
1314
Text(char),
1415
Circle(bool),
1516
}
@@ -29,14 +30,17 @@ impl PixelState {
2930
} else {
3031
'O'
3132
}
32-
}
33+
},
34+
Self::Filled => '█',
3335
}
3436
}
3537

3638
fn update(&mut self, new_state: PixelState) {
3739
let next_state = match (*self, new_state) {
3840
(Self::HLine, Self::VLine) => Self::Cross,
3941
(Self::VLine, Self::HLine) => Self::Cross,
42+
(Self::Filled, _) => Self::Filled,
43+
(_, Self::Filled) => Self::Filled,
4044
(_, Self::Circle(what)) => Self::Circle(what),
4145
(Self::Circle(what), _) => Self::Circle(what),
4246
(_, Self::Pixel) => Self::Pixel,
@@ -48,24 +52,36 @@ impl PixelState {
4852
}
4953
}
5054

51-
pub struct TextDrawingBackend(Vec<PixelState>);
55+
pub struct TextDrawingBackend {
56+
size: (u32, u32),
57+
data: Vec<PixelState>
58+
}
59+
60+
impl TextDrawingBackend {
61+
pub fn new(width: u32, height: u32) -> TextDrawingBackend {
62+
TextDrawingBackend {
63+
size: (width, height),
64+
data: vec![PixelState::Empty; (width * height) as usize],
65+
}
66+
}
67+
}
5268

5369
impl DrawingBackend for TextDrawingBackend {
5470
type ErrorType = std::io::Error;
5571

5672
fn get_size(&self) -> (u32, u32) {
57-
(100, 30)
73+
self.size
5874
}
5975

6076
fn ensure_prepared(&mut self) -> Result<(), DrawingErrorKind<std::io::Error>> {
6177
Ok(())
6278
}
6379

6480
fn present(&mut self) -> Result<(), DrawingErrorKind<std::io::Error>> {
65-
for r in 0..30 {
81+
for r in 0..self.size.1 {
6682
let mut buf = String::new();
67-
for c in 0..100 {
68-
buf.push(self.0[r * 100 + c].to_char());
83+
for c in 0..self.size.0 {
84+
buf.push(self.data[(r * self.size.0 + c) as usize].to_char());
6985
}
7086
println!("{}", buf);
7187
}
@@ -75,12 +91,35 @@ impl DrawingBackend for TextDrawingBackend {
7591

7692
fn draw_pixel(
7793
&mut self,
78-
pos: (i32, i32),
94+
mut pos: (i32, i32),
7995
color: BackendColor,
8096
) -> Result<(), DrawingErrorKind<std::io::Error>> {
97+
pos.0 = pos.0.max(0).min(self.size.0 as i32);
98+
pos.1 = pos.1.max(0).min(self.size.1 as i32);
8199
if color.alpha > 0.3 {
82-
self.0[(pos.1 * 100 + pos.0) as usize].update(PixelState::Pixel);
100+
self.data[(pos.1 * self.size.0 as i32 + pos.0) as usize].update(PixelState::Pixel);
101+
}
102+
Ok(())
103+
}
104+
105+
fn draw_rect<S: BackendStyle>(
106+
&mut self,
107+
mut upper_left: (i32, i32),
108+
mut bottom_right: (i32, i32),
109+
style: &S,
110+
fill: bool,
111+
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
112+
upper_left.0 = upper_left.0.max(0).min(self.size.0 as i32);
113+
upper_left.1 = upper_left.1.max(0).min(self.size.1 as i32);
114+
bottom_right.0 = bottom_right.0.max(0).min(self.size.0 as i32 - 1);
115+
bottom_right.1 = bottom_right.1.max(0).min(self.size.1 as i32 - 1);
116+
117+
for x in upper_left.0..=bottom_right.0 {
118+
for y in upper_left.1..=bottom_right.1 {
119+
self.data[(y * self.size.0 as i32 + x) as usize].update(PixelState::Filled);
120+
}
83121
}
122+
84123
Ok(())
85124
}
86125

@@ -92,20 +131,20 @@ impl DrawingBackend for TextDrawingBackend {
92131
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
93132
if from.0 == to.0 {
94133
let x = from.0;
95-
let y0 = from.1.min(to.1);
96-
let y1 = from.1.max(to.1);
134+
let y0 = from.1.min(to.1).max(0).min(self.size.1 as i32);
135+
let y1 = from.1.max(to.1).max(0).min(self.size.1 as i32);
97136
for y in y0..y1 {
98-
self.0[(y * 100 + x) as usize].update(PixelState::VLine);
137+
self.data[(y * self.size.0 as i32 + x) as usize].update(PixelState::VLine);
99138
}
100139
return Ok(());
101140
}
102141

103142
if from.1 == to.1 {
104143
let y = from.1;
105-
let x0 = from.0.min(to.0);
106-
let x1 = from.0.max(to.0);
144+
let x0 = from.0.min(to.0).max(0).min(self.size.0 as i32);
145+
let x1 = from.0.max(to.0).max(0).min(self.size.0 as i32);
107146
for x in x0..x1 {
108-
self.0[(y * 100 + x) as usize].update(PixelState::HLine);
147+
self.data[(y * self.size.0 as i32 + x) as usize].update(PixelState::HLine);
109148
}
110149
return Ok(());
111150
}
@@ -139,9 +178,11 @@ impl DrawingBackend for TextDrawingBackend {
139178
VPos::Center => -height / 2,
140179
VPos::Bottom => -height,
141180
};
142-
let offset = (pos.1 + dy).max(0) * 100 + (pos.0 + dx).max(0);
181+
let offset = (pos.1 + dy).max(0) * self.size.0 as i32 + (pos.0 + dx).max(0);
143182
for (idx, chr) in (offset..).zip(text.chars()) {
144-
self.0[idx as usize].update(PixelState::Text(chr));
183+
if (idx as usize) < self.data.len() {
184+
self.data[idx as usize].update(PixelState::Text(chr));
185+
}
145186
}
146187
Ok(())
147188
}

0 commit comments

Comments
 (0)