RKTK API Docs RKTK Home Repo

rktk/task/display/
utils.rs

1use embedded_graphics::{prelude::*, primitives::Rectangle};
2
3#[derive(Debug, Clone, Copy)]
4pub enum Rotation {
5    Rotate0,
6    Rotate90,
7}
8
9#[derive(Debug)]
10pub struct RotatedDrawTarget<'a, T>
11where
12    T: DrawTarget,
13{
14    parent: &'a mut T,
15    rotation: Rotation,
16}
17
18impl<'a, T> RotatedDrawTarget<'a, T>
19where
20    T: DrawTarget,
21{
22    pub fn new(parent: &'a mut T, rotation: Rotation) -> Self {
23        Self { parent, rotation }
24    }
25}
26
27impl<T> DrawTarget for RotatedDrawTarget<'_, T>
28where
29    T: DrawTarget,
30{
31    type Color = T::Color;
32    type Error = T::Error;
33
34    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
35    where
36        I: IntoIterator<Item = Pixel<Self::Color>>,
37    {
38        match self.rotation {
39            Rotation::Rotate0 => self.draw_iter(pixels),
40            Rotation::Rotate90 => {
41                let parent_height = self.parent.bounding_box().size.height as i32;
42
43                self.parent.draw_iter(
44                    pixels
45                        .into_iter()
46                        .map(|Pixel(p, c)| Pixel(Point::new(p.y, parent_height - p.x), c)),
47                )
48            }
49        }
50    }
51}
52
53impl<T> Dimensions for RotatedDrawTarget<'_, T>
54where
55    T: DrawTarget,
56{
57    fn bounding_box(&self) -> Rectangle {
58        match self.rotation {
59            Rotation::Rotate0 => self.parent.bounding_box(),
60            Rotation::Rotate90 => {
61                let parent_bb = self.parent.bounding_box();
62                Rectangle::new(
63                    parent_bb.top_left,
64                    Size::new(parent_bb.size.height, parent_bb.size.width),
65                )
66            }
67        }
68    }
69}