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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
use crate::properties::{
WidgetProperties, PROPERTY_BORDER_WIDTH, PROPERTY_FONT_COLOR, PROPERTY_FONT_NAME,
PROPERTY_FONT_SIZE, PROPERTY_FONT_STYLE, PROPERTY_HIDDEN, PROPERTY_ID, PROPERTY_INVALIDATED,
PROPERTY_TEXT,
};
use crate::system_widgets::base_widget::BaseWidget;
use crate::widget::Widget;
use sdl2::image::LoadTexture;
use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::render::{Canvas, Texture, TextureQuery};
use sdl2::ttf::{FontStyle, Sdl2TtfContext};
use sdl2::video::Window;
use std::cell::{RefCell, RefMut};
use std::collections::HashMap;
use std::path::Path;
struct WidgetCacheContainer {
widget: RefCell<Box<dyn Widget>>,
name: String,
parent: u32,
children: Vec<u32>,
}
impl WidgetCacheContainer {
pub fn new(widget: Box<dyn Widget>, name: String, parent: u32) -> Self {
Self {
widget: RefCell::new(widget),
name,
parent,
children: Vec::new(),
}
}
}
pub struct WidgetCache {
cache: Vec<WidgetCacheContainer>,
texture_cache: TextureCache,
}
impl WidgetCache {
pub fn new(w: u32, h: u32) -> Self {
let mut base_widget = BaseWidget::default();
base_widget
.properties()
.set_bounds(w, h)
.set_origin(0, 0)
.invalidate();
Self {
cache: vec![WidgetCacheContainer::new(
Box::new(base_widget),
String::from("root"),
0,
)],
texture_cache: TextureCache::default(),
}
}
#[inline]
pub fn id_at_point(&self, x: u32, y: u32) -> u32 {
let mut found_id: u32 = 0;
let mut hidden_widgets: Vec<u32> = vec![];
let cache_size = self.size();
for id in 0..cache_size {
let is_hidden = self.cache[id as usize]
.widget
.borrow_mut()
.properties()
.get_bool(PROPERTY_HIDDEN);
let widget_xy = self.cache[id as usize]
.widget
.borrow_mut()
.properties()
.get_origin();
let widget_wh = self.cache[id as usize]
.widget
.borrow_mut()
.properties()
.get_bounds();
if !is_hidden {
if x >= widget_xy.0
&& x <= widget_xy.0 + widget_wh.0
&& y >= widget_xy.1
&& y <= widget_xy.1 + widget_wh.1
&& !hidden_widgets.contains(&id)
{
found_id = id;
}
} else {
hidden_widgets.push(id);
for hidden_widget_id in self.get_children_of(id) {
hidden_widgets.push(hidden_widget_id);
}
}
}
found_id
}
#[inline]
pub fn get(&self, widget_id: u32) -> RefMut<Box<dyn Widget>> {
self.cache[widget_id as usize].widget.borrow_mut()
}
#[inline]
pub fn get_by_name(&self, name: String) -> RefMut<Box<dyn Widget>> {
let cache_size = self.size();
for i in 0..cache_size {
if self.cache[i as usize].name == name {
return self.get(i);
}
}
self.get(0)
}
#[inline]
pub fn get_parent_of(&self, widget_id: u32) -> u32 {
self.cache[widget_id as usize].parent
}
#[inline]
pub fn get_children_of(&self, widget_id: u32) -> Vec<u32> {
self.cache[widget_id as usize].children.clone()
}
#[inline]
pub fn add(&mut self, mut widget: Box<dyn Widget>, widget_name: String, parent_id: u32) -> u32 {
widget.invalidate();
self.cache
.push(WidgetCacheContainer::new(widget, widget_name, parent_id));
let widget_id = self.size() - 1;
self.cache[parent_id as usize].children.push(widget_id);
self.cache[widget_id as usize]
.widget
.borrow_mut()
.properties()
.set_value(PROPERTY_ID, widget_id as i32);
widget_id
}
#[inline]
pub fn size(&self) -> u32 {
self.cache.len() as u32
}
#[inline]
pub fn set_hidden(&mut self, widget_id: u32, state: bool) {
if widget_id == 0 {
return;
}
if !state {
self.get(widget_id).properties().delete(PROPERTY_HIDDEN);
} else {
self.get(widget_id).properties().set_bool(PROPERTY_HIDDEN);
}
let children = self.get_children_of(widget_id);
for child_widget_id in children {
self.set_hidden(child_widget_id, state);
}
}
pub fn invalidated(&self) -> bool {
let mut invalidated: bool = false;
for x in &self.cache {
if x.widget.borrow_mut().invalidated() {
invalidated = true;
break;
}
}
invalidated
}
fn paint_widget(&mut self, widget_id: u32, c: &mut Canvas<Window>) {
let paint_widget = &mut self.cache[widget_id as usize];
let is_hidden = paint_widget
.widget
.borrow_mut()
.properties()
.get_bool(PROPERTY_HIDDEN);
let widget_xy = paint_widget.widget.borrow_mut().properties().get_origin();
let widget_wh = paint_widget.widget.borrow_mut().properties().get_bounds();
if !is_hidden {
match paint_widget
.widget
.borrow_mut()
.draw(c, &mut self.texture_cache)
{
Some(texture) => {
c.copy(
texture,
None,
Rect::new(
widget_xy.0 as i32,
widget_xy.1 as i32,
widget_wh.0,
widget_wh.1,
),
)
.unwrap();
}
None => eprintln!("No texture presented: ID={}", widget_id),
};
paint_widget
.widget
.borrow_mut()
.properties()
.delete(PROPERTY_INVALIDATED);
}
}
fn draw(&mut self, widget_id: u32, c: &mut Canvas<Window>) {
let children_of_widget = self.get_children_of(widget_id);
if children_of_widget.is_empty() {
return;
}
for id in &children_of_widget {
self.paint_widget(*id, c);
if *id != widget_id {
self.draw(*id, c);
}
}
}
pub fn refresh(&mut self, c: &mut Canvas<Window>) {
c.set_draw_color(Color::RGBA(255, 255, 255, 255));
c.clear();
self.paint_widget(0, c);
self.draw(0, c);
c.present();
}
}
pub struct TextureCache {
images: HashMap<String, Texture>,
ttf_context: Sdl2TtfContext,
}
impl Default for TextureCache {
fn default() -> Self {
Self {
images: HashMap::new(),
ttf_context: sdl2::ttf::init().map_err(|e| e.to_string()).unwrap(),
}
}
}
impl TextureCache {
pub fn get_ttf_context(&self) -> &Sdl2TtfContext {
&self.ttf_context
}
pub fn get_image(&mut self, c: &mut Canvas<Window>, image_name: String) -> &Texture {
self.images.entry(image_name.clone()).or_insert({
c.texture_creator()
.load_texture(Path::new(&image_name))
.unwrap()
})
}
pub fn render_text(
&mut self,
c: &mut Canvas<Window>,
properties: &mut WidgetProperties,
alt_color: Option<Color>,
) -> (Texture, u32, u32) {
let ttf_context = self.get_ttf_context();
let texture_creator = c.texture_creator();
let bounds = properties.get_bounds();
let font_name = properties.get(PROPERTY_FONT_NAME);
let text_color =
alt_color.unwrap_or_else(|| properties.get_color(PROPERTY_FONT_COLOR, Color::BLACK));
let font_size = properties.get_value(PROPERTY_FONT_SIZE);
let border_width = properties.get_value(PROPERTY_BORDER_WIDTH);
let font_style: FontStyle =
FontStyle::from_bits_truncate(properties.get_value(PROPERTY_FONT_STYLE));
let text_message = properties.get(PROPERTY_TEXT);
let mut font = ttf_context
.load_font(Path::new(&font_name), font_size as u16)
.unwrap();
let text_max_width = bounds.0;
let surface = font
.render(&text_message)
.blended_wrapped(text_color, text_max_width - (border_width * 2) as u32)
.map_err(|e| e.to_string())
.unwrap();
let font_texture = texture_creator
.create_texture_from_surface(&surface)
.map_err(|e| e.to_string())
.unwrap();
let TextureQuery { width, height, .. } = font_texture.query();
font.set_style(font_style);
(font_texture, width, height)
}
}