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
use bevy_app::prelude::Plugin;
use bevy_asset::{load_internal_asset, prelude::Assets, Asset, Handle};
use bevy_ecs::prelude::{Bundle, Component, Query, ResMut};
use bevy_reflect::{TypePath, TypeUuid};
use bevy_render::{
    prelude::Color,
    render_resource::{AsBindGroup, Shader},
};
use bevy_ui::{node_bundles::MaterialNodeBundle, Style, UiMaterial, UiMaterialPlugin};
use bevy_utils::default;

pub const PROGRESS_BAR_HANDLE: Handle<Shader> =
    Handle::weak_from_u128(8714649747086695632918559878778085427);
pub struct ProgressBarPlugin;

impl Plugin for ProgressBarPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        load_internal_asset!(
            app,
            PROGRESS_BAR_HANDLE,
            "progress_shader.wgsl",
            Shader::from_wgsl
        );
        app.add_systems(bevy_app::Update, update_progress_bar)
            .add_plugins(UiMaterialPlugin::<ProgressBarMaterial>::default());
    }
}

/// The Progress Bar.
/// Has Different Colored section with relative size to each other
/// and a Color for the empty space
#[derive(Component)]
pub struct ProgressBar {
    /// The Progress
    /// a f32 between 0.0 and 1.0
    progress: f32,
    /// The Different Sections
    /// The amount is the space relative to the other Sections.
    pub sections: Vec<(u32, Color)>,
    /// The Color of the space that is not progressed to
    pub empty_color: Color,
}

impl ProgressBar {
    /// Creates a new ProgressBar
    ///
    /// # Examples
    /// ```
    /// use bevy_progressbar::ProgressBar;
    /// use bevy_render::prelude::Color;
    /// let bar = ProgressBar::new(vec![(10, Color::RED), (9, Color::BLUE)]);
    /// ```
    pub fn new(sections: Vec<(u32, Color)>) -> Self {
        Self {
            progress: 0.0,
            sections,
            empty_color: Color::NONE,
        }
    }
    /// Creates a new ProgressBar with a single section
    pub fn single(color: Color) -> Self {
        Self {
            progress: 0.0,
            sections: vec![(1, color)],
            empty_color: Color::NONE,
        }
    }

    /// Sets the progress of the bar
    ///
    /// # Arguments
    ///
    /// * `amount` - The Progress. gets clamped between 0.0 and 1.0
    ///
    /// # Examples
    ///
    /// ```
    /// use bevy_progressbar::ProgressBar;
    ///
    /// let mut bar = ProgressBar::default();
    /// bar.set_progress(0.5);
    /// assert_eq!(bar.get_progress(), 0.5);
    /// bar.set_progress(10.0);
    /// assert_eq!(bar.get_progress(), 1.0);
    /// ```
    pub fn set_progress(&mut self, amount: f32) -> &mut Self {
        self.progress = amount.clamp(0.0, 1.0);
        self
    }

    /// Returns the current progress
    pub fn get_progress(&self) -> f32 {
        self.progress
    }

    /// Increases the progress
    /// the new progress is at most 1.0
    ///
    /// # Examples
    /// ```
    /// use bevy_progressbar::ProgressBar;
    /// let mut bar = ProgressBar::default();
    /// bar.increase_progress(0.5);
    /// assert_eq!(bar.get_progress(), 0.5);
    /// bar.increase_progress(4.2);
    /// assert_eq!(bar.get_progress(), 1.0);
    /// ```
    pub fn increase_progress(&mut self, amount: f32) -> &mut Self {
        self.progress += amount;
        self.progress = self.progress.clamp(0.0, 1.0);
        self
    }

    /// Resets the progress to 0.0
    pub fn reset(&mut self) -> &mut Self {
        self.progress = 0.0;
        self
    }

    /// Returns true if the ProgressBar is is_finished
    ///
    /// # Examples
    /// ```
    /// use bevy_progressbar::ProgressBar;
    /// let mut bar = ProgressBar::default();
    /// assert_eq!(bar.is_finished(), false);
    /// bar.increase_progress(1.0);
    /// assert_eq!(bar.is_finished(), true);
    /// ```
    pub fn is_finished(&self) -> bool {
        self.progress >= 1.0
    }

    pub fn clear_sections(&mut self) -> &mut Self {
        self.sections.clear();
        self
    }

    pub fn add_section(&mut self, amount: u32, color: Color) -> &mut Self {
        self.sections.push((amount, color));
        self
    }
}

impl Default for ProgressBar {
    fn default() -> Self {
        Self {
            progress: 0.0,
            sections: vec![],
            empty_color: Color::NONE,
        }
    }
}

#[derive(Bundle)]
pub struct ProgressBarBundle {
    progressbar: ProgressBar,
    material_node_bundle: MaterialNodeBundle<ProgressBarMaterial>,
}

impl ProgressBarBundle {
    pub fn new(
        style: Style,
        progressbar: ProgressBar,
        materials: &mut ResMut<Assets<ProgressBarMaterial>>,
    ) -> ProgressBarBundle {
        ProgressBarBundle {
            progressbar,
            material_node_bundle: MaterialNodeBundle {
                style,
                material: materials.add(ProgressBarMaterial::default()),
                ..default()
            },
        }
    }
}

/// The Material for the ProgressBar
/// uses a simple wgsl shader
#[derive(Asset, TypePath, AsBindGroup, TypeUuid, Debug, Clone)]
#[uuid = "7d4aa28a-c01f-4ac7-b6f5-9b64cc3b4214"]
pub struct ProgressBarMaterial {
    #[uniform(0)]
    empty_color: Color,
    #[uniform(1)]
    progress: f32,
    /// The color of each section
    #[storage(2)]
    sections_color: Vec<Color>,
    #[storage(3)]
    sections_start_percentage: Vec<f32>,
    /// the length of the `sections_color` / `sections_start_percentage` vec.
    /// needs to be set for the shader
    #[uniform(4)]
    sections_count: u32,
}

impl Default for ProgressBarMaterial {
    fn default() -> Self {
        Self {
            empty_color: Color::NONE,
            progress: 0.0,
            sections_color: vec![],
            sections_start_percentage: vec![],
            sections_count: 0,
        }
    }
}

impl ProgressBarMaterial {
    /// Updates the material to match the ProgressBar
    pub fn update(&mut self, bar: &ProgressBar) {
        self.empty_color = bar.empty_color;
        self.progress = bar.progress;
        self.sections_color = vec![];
        self.sections_start_percentage = vec![];
        let total_amount: u32 = bar.sections.iter().map(|(amount, _)| amount).sum();
        for (amount, color) in bar.sections.iter() {
            self.sections_start_percentage
                .push(1. / (total_amount as f32 / *amount as f32));
            self.sections_color.push(*color);
        }
        self.sections_count = bar.sections.len() as u32;
    }
}

impl UiMaterial for ProgressBarMaterial {
    fn fragment_shader() -> bevy_render::render_resource::ShaderRef {
        PROGRESS_BAR_HANDLE.into()
    }
}

fn update_progress_bar(
    bar_query: Query<(&ProgressBar, &Handle<ProgressBarMaterial>)>,
    mut materials: ResMut<Assets<ProgressBarMaterial>>,
) {
    for (bar, handle) in bar_query.iter() {
        let Some(material) = materials.get_mut(handle) else {
            continue;
        };

        material.update(bar);
    }
}