From f98f11fabb13766274e18db667f1c74d75484479 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Sat, 18 Feb 2017 11:28:24 +0100 Subject: [PATCH 1/3] 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 From e6850373142a4680de45ee2d6d7047cb419992a9 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Sat, 18 Feb 2017 11:49:30 +0100 Subject: [PATCH 2/3] Formatted with rustfmt. --- src/progressbar.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/progressbar.rs b/src/progressbar.rs index 3820af0..dab6193 100644 --- a/src/progressbar.rs +++ b/src/progressbar.rs @@ -35,7 +35,7 @@ impl<'p> ProgressBar<'p> { } /// 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 + /// 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 { @@ -49,8 +49,7 @@ impl<'p> ProgressBar<'p> { unsafe { imgui_sys::igProgressBar(self.fraction, &self.size, - self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null()) - ); + self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null())); } } -} \ No newline at end of file +} From fa2ba7f69c15c36ce6f03816195fa8a540177239 Mon Sep 17 00:00:00 2001 From: Maurice Gilden Date: Sat, 18 Feb 2017 14:40:49 +0100 Subject: [PATCH 3/3] Fixed compile error in example. --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index b8620cb..8f008b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -675,7 +675,10 @@ impl<'ui> Ui<'ui> { /// Creates a progress bar. Fraction is the progress level with 0.0 = 0% and 1.0 = 100%. /// /// # Example - /// ```no_run + /// ```rust,no_run + /// # use imgui::*; + /// # let mut imgui = ImGui::init(); + /// # let ui = imgui.frame((0, 0), (0, 0), 0.1); /// ui.progress_bar(0.6) /// .size(imgui::ImVec2::new(100.0, 12.0)) /// .overlay_text(im_str!("Progress!"))