From 6ad52c517b24cfaa1b8eff0398d1ad7baf4a1367 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Fri, 12 Jul 2019 19:51:15 +0300 Subject: [PATCH] Pull new progress bar API from 0.1-dev --- imgui-examples/examples/progress_bar.rs | 27 ++++++++++ src/legacy.rs | 8 +++ src/lib.rs | 21 +------- src/progressbar.rs | 59 ---------------------- src/widget/mod.rs | 1 + src/widget/progress_bar.rs | 67 +++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 79 deletions(-) create mode 100644 imgui-examples/examples/progress_bar.rs delete mode 100644 src/progressbar.rs create mode 100644 src/widget/progress_bar.rs diff --git a/imgui-examples/examples/progress_bar.rs b/imgui-examples/examples/progress_bar.rs new file mode 100644 index 0000000..cc8ead1 --- /dev/null +++ b/imgui-examples/examples/progress_bar.rs @@ -0,0 +1,27 @@ +use imgui::*; + +mod support; + +fn main() { + let system = support::init(file!()); + system.main_loop(|run, ui| { + let w = Window::new(im_str!("Progress bar")) + .opened(run) + .position([20.0, 20.0], Condition::Appearing) + .size([700.0, 200.0], Condition::Appearing); + w.build(&ui, || { + ui.text("This is a simple progress bar:"); + ProgressBar::new(0.5).build(ui); + + ui.separator(); + ui.text("This progress bar has a custom size:"); + ProgressBar::new(0.3).size([200.0, 50.0]).build(ui); + + ui.separator(); + ui.text("This progress bar uses overlay text:"); + ProgressBar::new(0.8) + .overlay_text(im_str!("Lorem ipsum")) + .build(ui); + }); + }); +} diff --git a/src/legacy.rs b/src/legacy.rs index 272a5ee..5fe9f20 100644 --- a/src/legacy.rs +++ b/src/legacy.rs @@ -3,6 +3,7 @@ use bitflags::bitflags; use std::os::raw::c_int; use crate::string::ImStr; +use crate::widget::progress_bar::ProgressBar; use crate::window::{Window, WindowFlags}; use crate::Ui; @@ -352,3 +353,10 @@ impl<'ui> Ui<'ui> { unsafe { sys::igIsWindowFocused(ImGuiFocusedFlags::ChildWindows.bits()) } } } + +impl<'ui> Ui<'ui> { + #[deprecated(since = "0.2.0", note = "use imgui::ProgressBar::new(...) instead")] + pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'p> { + ProgressBar::new(fraction) + } +} diff --git a/src/lib.rs b/src/lib.rs index b8f48ba..d4e4304 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,6 @@ pub use self::menus::{Menu, MenuItem}; pub use self::plothistogram::PlotHistogram; pub use self::plotlines::PlotLines; pub use self::popup_modal::PopupModal; -pub use self::progressbar::ProgressBar; pub use self::render::draw_data::*; pub use self::render::renderer::*; pub use self::sliders::{ @@ -48,6 +47,7 @@ pub use self::stacks::*; pub use self::string::*; pub use self::style::*; pub use self::trees::{CollapsingHeader, TreeNode}; +pub use self::widget::progress_bar::*; pub use self::window::*; pub use self::window_draw_list::{ChannelsSplit, ImColor, WindowDrawList}; use internal::RawCast; @@ -69,7 +69,6 @@ mod menus; mod plothistogram; mod plotlines; mod popup_modal; -mod progressbar; mod render; mod sliders; mod stacks; @@ -808,24 +807,6 @@ impl<'ui> Ui<'ui> { } } -impl<'ui> Ui<'ui> { - /// Creates a progress bar. Fraction is the progress level with 0.0 = 0% and 1.0 = 100%. - /// - /// # Example - /// ```rust,no_run - /// # use imgui::*; - /// # let mut imgui = Context::create(); - /// # let ui = imgui.frame(); - /// ui.progress_bar(0.6) - /// .size([100.0, 12.0]) - /// .overlay_text(im_str!("Progress!")) - /// .build(); - /// ``` - pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'ui, 'p> { - ProgressBar::new(self, fraction) - } -} - impl<'ui> Ui<'ui> { /// Creates a child frame. Size is size of child_frame within parent window. /// diff --git a/src/progressbar.rs b/src/progressbar.rs deleted file mode 100644 index 12bcddd..0000000 --- a/src/progressbar.rs +++ /dev/null @@ -1,59 +0,0 @@ -#![warn(missing_docs)] -use std::marker::PhantomData; -use std::ptr; -use sys; - -use super::{ImStr, Ui}; - -/// Progress bar widget. -#[must_use] -pub struct ProgressBar<'ui, 'p> { - fraction: f32, - size: [f32; 2], - overlay_text: Option<&'p ImStr>, - _phantom: PhantomData<&'ui Ui<'ui>>, -} - -impl<'ui, 'p> ProgressBar<'ui, '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(_: &Ui<'ui>, fraction: f32) -> Self { - ProgressBar { - fraction, - size: [-1.0, 0.0], - overlay_text: None, - _phantom: PhantomData, - } - } - - /// Sets an optional text that will be drawn over the progress bar. - #[inline] - pub fn overlay_text(mut self, overlay_text: &'p ImStr) -> 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: [f32; 2]) -> 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 { - sys::igProgressBar( - self.fraction, - self.size.into(), - self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null()), - ); - } - } -} diff --git a/src/widget/mod.rs b/src/widget/mod.rs index c827b6a..cb7e7f4 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -1,2 +1,3 @@ pub mod misc; +pub mod progress_bar; pub mod text; diff --git a/src/widget/progress_bar.rs b/src/widget/progress_bar.rs new file mode 100644 index 0000000..97567f9 --- /dev/null +++ b/src/widget/progress_bar.rs @@ -0,0 +1,67 @@ +use std::ptr; + +use crate::string::ImStr; +use crate::sys; +use crate::Ui; + +/// Builder for a progress bar widget. +/// +/// # Examples +/// +/// ```no_run +/// # use imgui::*; +/// # let mut imgui = Context::create(); +/// # let ui = imgui.frame(); +/// ProgressBar::new(0.6) +/// .size([100.0, 12.0]) +/// .overlay_text(im_str!("Progress!")) +/// .build(&ui); +/// ``` +#[derive(Debug, Clone)] +#[must_use] +pub struct ProgressBar<'a> { + fraction: f32, + size: [f32; 2], + overlay_text: Option<&'a ImStr>, +} + +impl<'a> ProgressBar<'a> { + /// 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) -> ProgressBar<'a> { + ProgressBar { + fraction, + size: [-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: &'a ImStr) -> ProgressBar { + 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: [f32; 2]) -> Self { + self.size = size.into(); + self + } + /// Builds the progress bar. + pub fn build(self, _: &Ui) { + unsafe { + sys::igProgressBar( + self.fraction, + self.size.into(), + self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null()), + ); + } + } +}