Pull new progress bar API from 0.1-dev

This commit is contained in:
Joonas Javanainen 2019-07-12 19:51:15 +03:00
parent 7f10cb35b2
commit 6ad52c517b
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
6 changed files with 104 additions and 79 deletions

View File

@ -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);
});
});
}

View File

@ -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)
}
}

View File

@ -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.
///

View File

@ -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()),
);
}
}
}

View File

@ -1,2 +1,3 @@
pub mod misc;
pub mod progress_bar;
pub mod text;

View File

@ -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()),
);
}
}
}