graphics/input/
data.rs

1use core::mem::transmute;
2
3use crate::{Point, lvgl};
4
5use super::{Key, State};
6
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8#[repr(C)]
9pub struct InputData {
10    pub r#continue: bool,
11    pub point: Point,
12    pub state: State,
13    pub key: Key,
14}
15
16impl Default for InputData {
17    fn default() -> Self {
18        Self {
19            point: Point::default(),
20            state: State::default(),
21            key: Key::Character(0),
22            r#continue: false,
23        }
24    }
25}
26
27impl InputData {
28    pub const fn new(point: Point, state: State, key: Key, r#continue: bool) -> Self {
29        Self {
30            point,
31            state,
32            key,
33            r#continue,
34        }
35    }
36
37    pub const fn default_constant() -> Self {
38        Self {
39            point: Point::new(0, 0),
40            state: State::Released,
41            key: Key::Character(0),
42            r#continue: false,
43        }
44    }
45
46    pub const fn get_continue(&self) -> bool {
47        self.r#continue
48    }
49
50    pub const fn get_point(&self) -> &Point {
51        &self.point
52    }
53
54    pub const fn get_touch(&self) -> &State {
55        &self.state
56    }
57
58    pub const fn get_key(&self) -> Key {
59        self.key
60    }
61
62    pub const fn set_continue(&mut self, r#continue: bool) {
63        self.r#continue = r#continue;
64    }
65
66    pub fn set_point(&mut self, point: Point) {
67        self.point = point;
68    }
69
70    pub fn set_state(&mut self, touch: State) {
71        self.state = touch;
72    }
73
74    pub fn set_key(&mut self, key: Key) {
75        self.key = key;
76    }
77
78    pub fn set(&mut self, point: Point, touch: State) {
79        self.point = point;
80        self.state = touch;
81    }
82}
83
84impl TryFrom<&mut [u8]> for &mut InputData {
85    type Error = ();
86
87    fn try_from(value: &mut [u8]) -> Result<Self, Self::Error> {
88        if value.len() != size_of::<InputData>() {
89            return Err(());
90        }
91        if !(value.as_ptr() as usize).is_multiple_of(core::mem::align_of::<InputData>()) {
92            return Err(());
93        }
94
95        #[allow(clippy::transmute_ptr_to_ref)]
96        Ok(unsafe { transmute::<*mut u8, Self>(value.as_mut_ptr()) })
97    }
98}
99
100impl AsMut<[u8]> for InputData {
101    fn as_mut(&mut self) -> &mut [u8] {
102        unsafe { core::slice::from_raw_parts_mut(self as *mut _ as *mut u8, size_of::<Self>()) }
103    }
104}
105
106impl From<InputData> for lvgl::lv_indev_data_t {
107    fn from(value: InputData) -> lvgl::lv_indev_data_t {
108        let mut input_device_data = lvgl::lv_indev_data_t::default();
109
110        let state = value.get_touch();
111
112        if *state == State::Pressed {
113            input_device_data.point = (*value.get_point()).into();
114        }
115
116        input_device_data.state = (*state).into();
117        input_device_data.key = value.key.into();
118
119        input_device_data
120    }
121}