[Ui] Add popup_modal wrapper

This commit is contained in:
Malik Olivier Boussejra 2018-09-26 09:07:22 +09:00
parent b98e57d055
commit e68047efd1
2 changed files with 49 additions and 0 deletions

View File

@ -25,6 +25,7 @@ pub use input::{
pub use menus::{Menu, MenuItem};
pub use plothistogram::PlotHistogram;
pub use plotlines::PlotLines;
pub use popup_modal::PopupModal;
pub use progressbar::ProgressBar;
pub use sliders::{
SliderFloat, SliderFloat2, SliderFloat3, SliderFloat4, SliderInt, SliderInt2, SliderInt3,
@ -49,6 +50,7 @@ mod input;
mod menus;
mod plothistogram;
mod plotlines;
mod popup_modal;
mod progressbar;
mod sliders;
mod string;
@ -1204,6 +1206,9 @@ impl<'ui> Ui<'ui> {
unsafe { sys::igEndPopup() };
}
}
pub fn popup_modal<'p>(&self, str_id: &'p ImStr) -> PopupModal<'ui, 'p> {
PopupModal::new(self, str_id)
}
pub fn close_current_popup(&self) {
unsafe { sys::igCloseCurrentPopup() };
}

44
src/popup_modal.rs Normal file
View File

@ -0,0 +1,44 @@
use std::marker::PhantomData;
use std::ptr;
use super::{ImGuiWindowFlags, ImStr, Ui};
use sys;
#[must_use]
pub struct PopupModal<'ui, 'p> {
label: &'p ImStr,
opened: Option<&'p mut bool>,
flags: ImGuiWindowFlags,
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> PopupModal<'ui, 'p> {
pub fn new(_: &Ui<'ui>, label: &'p ImStr) -> Self {
PopupModal {
label,
opened: None,
flags: ImGuiWindowFlags::empty(),
_phantom: PhantomData,
}
}
pub fn opened(mut self, opened: &'p mut bool) -> Self {
self.opened = Some(opened);
self
}
pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe {
sys::igBeginPopupModal(
self.label.as_ptr(),
self.opened
.map(|x| x as *mut bool)
.unwrap_or(ptr::null_mut()),
self.flags,
)
};
if render {
f();
unsafe { sys::igEndMenu() };
}
}
}