From f98f11fabb13766274e18db667f1c74d75484479 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Sat, 18 Feb 2017 11:28:24 +0100 Subject: [PATCH] Added support for progress bar. --- CHANGELOG.markdown | 4 ++++ src/lib.rs | 17 ++++++++++++++ src/progressbar.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/progressbar.rs diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 584b27b..c8510db 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -2,6 +2,10 @@ ## [Unreleased] +### Added + + - Support for progress bar + ### Removed - `Window::always_vertical_scollbar` (typo) diff --git a/src/lib.rs b/src/lib.rs index 4392602..b8620cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,7 @@ pub use input::{ColorEdit3, ColorEdit4, InputFloat, InputFloat2, InputFloat3, In pub use menus::{Menu, MenuItem}; pub use plothistogram::PlotHistogram; pub use plotlines::PlotLines; +pub use progressbar::ProgressBar; pub use sliders::{SliderFloat, SliderInt}; pub use trees::{CollapsingHeader, TreeNode}; pub use window::Window; @@ -53,6 +54,7 @@ mod input; mod menus; mod plothistogram; mod plotlines; +mod progressbar; mod sliders; mod trees; mod window; @@ -668,3 +670,18 @@ impl<'ui> Ui<'ui> { PlotHistogram::new(label, values) } } + +impl<'ui> Ui<'ui> { + /// Creates a progress bar. Fraction is the progress level with 0.0 = 0% and 1.0 = 100%. + /// + /// # Example + /// ```no_run + /// ui.progress_bar(0.6) + /// .size(imgui::ImVec2::new(100.0, 12.0)) + /// .overlay_text(im_str!("Progress!")) + /// .build(); + /// ``` + pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'p> { + ProgressBar::new(fraction) + } +} diff --git a/src/progressbar.rs b/src/progressbar.rs new file mode 100644 index 0000000..3820af0 --- /dev/null +++ b/src/progressbar.rs @@ -0,0 +1,56 @@ +#![warn(missing_docs)] + +use imgui_sys; +use std::ptr; +use super::{ImStr, ImVec2}; + + +/// Progress bar widget. +#[must_use] +pub struct ProgressBar<'p> { + fraction: f32, + size: ImVec2, + overlay_text: Option>, +} + +impl<'p> ProgressBar<'p> { + /// Creates a progress bar with a given fraction showing + /// the progress (0.0 = 0%, 1.0 = 100%). + /// The progress bar will be automatically sized to fill + /// the entire width of the window if no custom size is + /// specified. + pub fn new(fraction: f32) -> Self { + ProgressBar { + fraction: fraction, + size: ImVec2::new(-1.0, 0.0), + overlay_text: None, + } + } + + /// Sets an optional text that will be drawn over the progress bar. + #[inline] + pub fn overlay_text(mut self, overlay_text: ImStr<'p>) -> Self { + self.overlay_text = Some(overlay_text); + self + } + + /// Sets the size of the progress bar. Negative values will automatically + /// align to the end of the axis, zero will let the progress bar choose a + /// size and positive values will use the given size. + #[inline] + pub fn size(mut self, size: ImVec2) -> Self { + self.size = size; + self + } + + /// Builds the progress bar. This has to be called after setting all parameters + /// of the progress bar, otherwise the it will not be shown. + pub fn build(self) { + unsafe { + imgui_sys::igProgressBar(self.fraction, + &self.size, + self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null()) + ); + } + } +} \ No newline at end of file