graphics/
area.rs

1use crate::{Point, lvgl};
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4pub struct Area(Point, Point);
5
6impl Area {
7    pub fn new(point_1: Point, point_2: Point) -> Self {
8        Self(point_1, point_2)
9    }
10
11    pub fn get_point_1(&self) -> Point {
12        self.0
13    }
14
15    pub fn get_point_2(&self) -> Point {
16        self.1
17    }
18
19    pub fn get_width(&self) -> u16 {
20        (self.1.get_x() - self.0.get_x()).unsigned_abs() + 1
21    }
22
23    pub fn get_height(&self) -> u16 {
24        (self.1.get_y() - self.0.get_y()).unsigned_abs() + 1
25    }
26
27    pub fn set_point_1(mut self, value: Point) -> Self {
28        self.0 = value;
29        self
30    }
31
32    pub fn set_point_2(mut self, value: Point) -> Self {
33        self.1 = value;
34        self
35    }
36}
37
38impl From<lvgl::lv_area_t> for Area {
39    fn from(value: lvgl::lv_area_t) -> Self {
40        Self::new(
41            Point::new(value.x1 as i16, value.y1 as i16),
42            Point::new(value.x2 as i16, value.y2 as i16),
43        )
44    }
45}
46
47impl AsRef<[u8]> for Area {
48    fn as_ref(&self) -> &[u8] {
49        unsafe {
50            core::slice::from_raw_parts(self as *const _ as *const u8, core::mem::size_of::<Self>())
51        }
52    }
53}
54
55impl AsRef<Area> for [u8; core::mem::size_of::<Area>()] {
56    fn as_ref(&self) -> &Area {
57        unsafe { &*(self as *const _ as *const Area) }
58    }
59}