iris_ui/
test.rs

1use crate::geom::{Bounds, Point};
2use crate::gfx::{DrawingContext, TextStyle};
3use crate::scene::Scene;
4use crate::{Theme, util};
5use embedded_graphics::Drawable;
6use embedded_graphics::geometry::Point as EPoint;
7use embedded_graphics::mock_display::MockDisplay;
8use embedded_graphics::mono_font::MonoTextStyle;
9use embedded_graphics::mono_font::ascii::FONT_7X13_BOLD;
10use embedded_graphics::mono_font::iso_8859_9::FONT_6X10;
11use embedded_graphics::pixelcolor::{Rgb565, RgbColor, WebColors};
12use embedded_graphics::primitives::{Line, Primitive, PrimitiveStyle};
13use embedded_graphics::text::Text;
14
15pub struct MockDrawingContext {
16    pub clip_rect: Bounds,
17    pub display: MockDisplay<Rgb565>,
18    offset: Point,
19}
20
21impl MockDrawingContext {
22    pub fn new(scene: &Scene) -> MockDrawingContext {
23        let mut ctx: MockDrawingContext = MockDrawingContext {
24            clip_rect: scene.dirty_rect,
25            display: MockDisplay::new(),
26            offset: Point::new(0, 0),
27        };
28        ctx.display.set_allow_out_of_bounds_drawing(true);
29        ctx.display.set_allow_overdraw(true);
30        ctx
31    }
32    pub fn make_mock_theme() -> Theme {
33        Theme {
34            bg: Rgb565::WHITE,
35            fg: Rgb565::BLACK,
36            selected_bg: Rgb565::WHITE,
37            selected_fg: Rgb565::BLACK,
38            panel_bg: Rgb565::CSS_GRAY,
39            font: FONT_6X10,
40            bold_font: FONT_7X13_BOLD,
41        }
42    }
43}
44impl DrawingContext for MockDrawingContext {
45    fn fill_rect(&mut self, bounds: &Bounds, color: &Rgb565) {
46        // info!("fill_rect {:?} {:?} {:?}", bounds, self.clip_rect, color);
47        util::bounds_to_rect(bounds)
48            .intersection(&util::bounds_to_rect(&self.clip_rect))
49            .into_styled(PrimitiveStyle::with_fill(*color))
50            .draw(&mut self.display)
51            .unwrap();
52    }
53
54    fn stroke_rect(&mut self, bounds: &Bounds, color: &Rgb565) {
55        util::bounds_to_rect(bounds)
56            .intersection(&util::bounds_to_rect(&self.clip_rect))
57            .into_styled(PrimitiveStyle::with_stroke(*color, 1))
58            .draw(&mut self.display)
59            .unwrap();
60    }
61
62    fn line(&mut self, start: &Point, end: &Point, color: &Rgb565) {
63        let line = Line::new(EPoint::new(start.x, start.y), EPoint::new(end.x, end.y));
64        line.into_styled(PrimitiveStyle::with_stroke(*color, 1))
65            .draw(&mut self.display)
66            .unwrap();
67    }
68
69    // fn fill_text(&mut self, bounds: &Bounds, text: &str, style: &TextStyle);
70    fn fill_text(&mut self, bounds: &Bounds, text: &str, style: &TextStyle) {
71        let style = MonoTextStyle::new(&style.font, *style.color);
72        let mut pt = embedded_graphics::geometry::Point::new(bounds.position.x, bounds.position.y);
73        pt.y += bounds.size.h / 2;
74        pt.y += (style.font.baseline as i32) / 2;
75        let w = (style.font.character_size.width as i32) * (text.len() as i32);
76        pt.x += (bounds.size.w - w) / 2;
77        Text::new(text, pt, style).draw(&mut self.display).unwrap();
78    }
79
80    fn text(&mut self, text: &str, _position: &Point, _style: &TextStyle) {}
81
82    fn translate(&mut self, offset: &Point) {
83        self.offset = self.offset + *offset;
84    }
85}