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
use super::{Buffer, Rect, Drawable, Pixel, Pixels, Coord, Style};
pub struct Canvas<'a, S: Style> {
buffer: &'a mut Buffer<S>,
pub frame: Rect,
}
impl<'a, S: Style> Canvas<'a, S> {
pub fn new(buffer: &'a mut Buffer<S>, frame: Rect) -> Self {
Self {
buffer,
frame,
}
}
}
impl<'a, S: Style> Drawable<S> for Canvas<'a, S> {
fn fill(&mut self, text: char, style: S) {
self.buffer.rect(self.frame, text, style);
}
fn rect(&mut self, rect: Rect, text: char, style: S) {
self.buffer.rect(Rect::new(rect.x + self.frame.x, rect.y + self.frame.y, rect.width, rect.height), text, style);
}
fn string(&mut self, x: u16, y: u16, text: String, style: S) {
let mut current_x = x;
let mut current_y = y;
for c in text.chars() {
self.pixel(
current_x,
current_y,
Pixel {
text: Some(c),
style,
},
);
current_x += 1;
if current_x >= self.frame.width {
current_x = 0;
current_y += 1;
}
}
}
fn pixels(&mut self, x: u16, y: u16, pixels: &Pixels<S>) {
let mut current_x = x;
let mut current_y = y;
for p in pixels.iter(){
self.pixel(
current_x,
current_y,
*p
);
current_x += 1;
if current_x >= self.frame.width {
current_x = 0;
current_y += 1;
}
}
}
fn pixel(&mut self, x: u16, y: u16, pixel: Pixel<S>) {
self.buffer.pixel(self.frame.x + x, self.frame.y + y, pixel);
}
fn pixel_at_index(&mut self, index: usize, pixel: Pixel<S>) {
let index = Coord::from_index(index, self.frame.width).as_index(self.buffer.width);
self.buffer.pixel_at_index(index, pixel);
}
}