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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::hash::{Hash, Hasher};
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use crate::framebuffer::cgmath;
use crate::framebuffer::common;
use crate::framebuffer::common::{color, mxcfb_rect};
use crate::framebuffer::FramebufferDraw;
use crate::framebuffer::FramebufferRefresh;
use crate::framebuffer::PartialRefreshMode;
use crate::appctx;
pub type ActiveRegionFunction = fn(&mut appctx::ApplicationContext<'_>, UIElementHandle);
#[derive(Clone)]
pub struct ActiveRegionHandler {
pub handler: ActiveRegionFunction,
pub element: UIElementHandle,
}
impl std::fmt::Debug for ActiveRegionHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{0:p}", self)
}
}
#[derive(Clone, Copy)]
pub enum UIConstraintRefresh {
NoRefresh,
Refresh,
RefreshAndWait,
}
impl Default for UIConstraintRefresh {
fn default() -> UIConstraintRefresh {
UIConstraintRefresh::Refresh
}
}
#[derive(Clone)]
pub struct UIElementHandle(Arc<RwLock<UIElementWrapper>>);
#[derive(Clone)]
pub struct UIElementWrapper {
pub position: cgmath::Point2<i32>,
pub refresh: UIConstraintRefresh,
pub last_drawn_rect: Option<common::mxcfb_rect>,
pub onclick: Option<ActiveRegionFunction>,
pub inner: UIElement,
}
impl Default for UIElementWrapper {
fn default() -> UIElementWrapper {
UIElementWrapper {
position: cgmath::Point2 { x: 0, y: 0 },
refresh: UIConstraintRefresh::default(),
last_drawn_rect: Option::default(),
onclick: Option::default(),
inner: UIElement::default(),
}
}
}
impl Hash for UIElementWrapper {
fn hash<H: Hasher>(&self, state: &mut H) {
self.position.x.hash(state);
self.position.y.hash(state);
}
}
impl PartialEq for UIElementWrapper {
fn eq(&self, other: &UIElementWrapper) -> bool {
self.position == other.position
}
}
impl Eq for UIElementWrapper {}
#[derive(Clone)]
pub enum UIElement {
Text {
text: String,
scale: f32,
foreground: color,
border_px: u32,
},
Image {
img: image::DynamicImage,
},
Region {
size: cgmath::Vector2<u32>,
border_color: color,
border_px: u32,
},
Unspecified,
}
impl UIElementHandle {
pub fn read(&self) -> RwLockReadGuard<'_, UIElementWrapper> {
self.0.read().unwrap()
}
pub fn write(&self) -> RwLockWriteGuard<'_, UIElementWrapper> {
self.0.write().unwrap()
}
pub fn new(elem: UIElementWrapper) -> UIElementHandle {
UIElementHandle(Arc::new(RwLock::new(elem)))
}
}
impl UIElementWrapper {
pub fn draw(
&mut self,
app: &mut appctx::ApplicationContext<'_>,
handler: &Option<ActiveRegionHandler>,
) {
let refresh = self.refresh;
let framebuffer = app.get_framebuffer_ref();
let old_filled_rect = match self.last_drawn_rect {
Some(rect) => {
framebuffer.fill_rect(rect.top_left().cast().unwrap(), rect.size(), color::WHITE);
if rect.top_left() != self.position.cast().unwrap() {
framebuffer.partial_refresh(
&rect,
PartialRefreshMode::Wait,
common::waveform_mode::WAVEFORM_MODE_DU,
common::display_temp::TEMP_USE_REMARKABLE_DRAW,
common::dither_mode::EPDC_FLAG_USE_DITHERING_PASSTHROUGH,
0,
false,
);
}
rect
}
None => mxcfb_rect::invalid(),
};
let rect = match self.inner {
UIElement::Text {
ref text,
scale,
foreground,
border_px,
} => app.display_text(
self.position.cast().unwrap(),
foreground,
scale,
border_px as u32,
8,
text,
refresh,
),
UIElement::Image { ref img } => {
app.display_image(img, self.position.cast().unwrap(), refresh)
}
UIElement::Region {
size,
border_color,
border_px,
} => app.display_rect(
self.position.cast().unwrap(),
size.cast().unwrap(),
border_px,
border_color,
refresh,
),
UIElement::Unspecified => return,
};
if old_filled_rect != rect {
if let Some(ref h) = handler {
if old_filled_rect != mxcfb_rect::invalid() {
app.remove_active_region_at_point(
old_filled_rect.top as u16,
old_filled_rect.left as u16,
);
}
if app
.find_active_region(self.position.y as u16, self.position.x as u16)
.is_none()
{
app.create_active_region(
rect.top as u16,
rect.left as u16,
rect.height as u16,
rect.width as u16,
h.handler,
h.element.clone(),
);
}
}
}
if let Some(last_rect) = self.last_drawn_rect {
if last_rect != rect {
framebuffer.partial_refresh(
&last_rect,
PartialRefreshMode::Async,
common::waveform_mode::WAVEFORM_MODE_DU,
common::display_temp::TEMP_USE_REMARKABLE_DRAW,
common::dither_mode::EPDC_FLAG_USE_DITHERING_PASSTHROUGH,
0,
false,
);
}
}
self.last_drawn_rect = Some(rect);
}
}
impl Default for UIElement {
fn default() -> UIElement {
UIElement::Unspecified
}
}