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
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use crate::caches::TextureCache;
use crate::primitives::{draw_base, fill_box};
use crate::properties::{
WidgetProperties, PROPERTY_BORDER_WIDTH, PROPERTY_PROGRESS, PROPERTY_PROGRESS_COLOR,
};
use crate::texture_store::TextureStore;
use crate::widget::Widget;
use sdl2::pixels::Color;
#[derive(Default)]
pub struct ProgressWidget {
texture_store: TextureStore,
properties: WidgetProperties,
}
impl Widget for ProgressWidget {
widget_default_impl!();
fn draw(&mut self, c: &mut Canvas<Window>, _t: &mut TextureCache) -> Option<&Texture> {
if self.invalidated() {
let bounds = self.properties.get_bounds();
let border_width = self.properties.get_value(PROPERTY_BORDER_WIDTH);
self.texture_store
.create_or_resize_texture(c, bounds.0, bounds.1);
let cloned_properties = self.properties.clone();
c.with_texture_canvas(self.texture_store.get_mut_ref(), |texture| {
draw_base(texture, &cloned_properties, None);
let inside_color = cloned_properties.get_color(PROPERTY_PROGRESS_COLOR, Color::RED);
let progress = cloned_properties.get_value(PROPERTY_PROGRESS);
let start_x = border_width;
let start_y = border_width;
let progress_width = (f64::from(bounds.0) * (f64::from(progress) / 100.0)) as u32
- (border_width * 2) as u32;
let progress_height = bounds.1 - (border_width * 2) as u32;
fill_box(
texture,
start_x as u32,
start_y as u32,
progress_width,
progress_height,
inside_color,
);
})
.unwrap();
}
self.texture_store.get_optional_ref()
}
}