Skip to main content

minifb_ui/ui/
progressbar.rs

1pub struct ProgressBar {
2    pub pos_x: usize,
3    pub pos_y: usize,
4    pub width: usize,
5    pub height: usize,
6
7    progress: f32,
8
9    pub track_color: crate::color::Color,
10    pub fill_color: crate::color::Color,
11    pub border_color: crate::color::Color,
12    pub border_size: usize,
13    pub radius: usize,
14
15    pub label: Option<crate::ui::text::Text>,
16    pub label_size: f32,
17    pub label_color: crate::color::Color,
18    pub show_percentage: bool,
19}
20
21impl Default for ProgressBar {
22    fn default() -> Self {
23        Self {
24            pos_x: 0,
25            pos_y: 0,
26            width: 200,
27            height: 16,
28            progress: 0.0,
29            track_color: crate::color::Color::new(40, 40, 50),
30            fill_color: crate::color::Color::new(108, 92, 231),
31            border_color: crate::color::Color::new(80, 80, 100),
32            border_size: 1,
33            radius: 4,
34            label: None,
35            label_size: 12.0,
36            label_color: crate::color::Color::new(200, 200, 210),
37            show_percentage: false,
38        }
39    }
40}
41
42impl ProgressBar {
43    pub fn position(mut self, x: usize, y: usize) -> Self {
44        self.pos_x = x;
45        self.pos_y = y;
46        self
47    }
48
49    pub fn size(mut self, width: usize, height: usize) -> Self {
50        self.width = width;
51        self.height = height;
52        self
53    }
54
55    pub fn default_progress(mut self, p: f32) -> Self {
56        self.progress = p.clamp(0.0, 1.0);
57        self
58    }
59
60    pub fn track_color(mut self, color: crate::color::Color) -> Self {
61        self.track_color = color;
62        self
63    }
64
65    pub fn fill_color(mut self, color: crate::color::Color) -> Self {
66        self.fill_color = color;
67        self
68    }
69
70    pub fn border_color(mut self, color: crate::color::Color) -> Self {
71        self.border_color = color;
72        self
73    }
74
75    pub fn border(mut self, size: usize) -> Self {
76        self.border_size = size;
77        self
78    }
79
80    pub fn radius(mut self, radius: usize) -> Self {
81        self.radius = radius;
82        self
83    }
84
85    pub fn show_percentage(mut self, show: bool) -> Self {
86        self.show_percentage = show;
87        self
88    }
89
90    pub fn label(mut self, text: &str, font: crate::ttf::Font, size: f32) -> Self {
91        self.label = Some(crate::ui::text::Text::new(text, font));
92        self.label_size = size;
93        self
94    }
95
96    pub fn label_color(mut self, color: crate::color::Color) -> Self {
97        self.label_color = color;
98        self
99    }
100
101    pub fn progress(&self) -> f32 {
102        self.progress
103    }
104
105    pub fn set_progress(&mut self, p: f32) {
106        self.progress = p.clamp(0.0, 1.0);
107    }
108
109    pub fn draw(&mut self, window: &mut crate::window::Window) {
110        // Border
111        window.draw_rect_f(
112            self.pos_x, self.pos_y, self.width, self.height,
113            self.radius, &self.border_color, 0,
114        );
115
116        // Track
117        let inner_x = self.pos_x + self.border_size;
118        let inner_y = self.pos_y + self.border_size;
119        let inner_w = self.width.saturating_sub(self.border_size * 2);
120        let inner_h = self.height.saturating_sub(self.border_size * 2);
121        let inner_r = self.radius.saturating_sub(self.border_size);
122        window.draw_rect_f(inner_x, inner_y, inner_w, inner_h, inner_r, &self.track_color, 0);
123
124        // Fill
125        let fill_w = (inner_w as f32 * self.progress) as usize;
126        if fill_w > 0 {
127            window.draw_rect_f(inner_x, inner_y, fill_w, inner_h, inner_r, &self.fill_color, 0);
128        }
129
130        // Percentage text
131        if self.show_percentage {
132            if let Some(ref label) = self.label {
133                let pct = format!("{}%", (self.progress * 100.0) as u32);
134                let pct_text = crate::ui::text::Text::new(&pct, label.font.clone());
135                window.draw_text_centered(
136                    self.pos_x, self.pos_y, self.width, self.height,
137                    &pct_text, self.label_size, &self.label_color,
138                );
139            }
140        }
141    }
142}