Add support for popups and selectable widgets.

This commit is contained in:
Cameron Hart 2016-01-17 20:05:30 +11:00
parent 05cb9ccc3f
commit 9e78e86000
2 changed files with 49 additions and 0 deletions

View File

@ -44,6 +44,7 @@ struct State {
vec3i: [i32;3],
col1: [f32;3],
col2: [f32;4],
selected_fish: Option<usize>,
auto_resize_state: AutoResizeState,
file_menu: FileMenuState
}
@ -90,6 +91,7 @@ impl Default for State {
vec3i: [10, 20, 30],
col1: [1.0, 0.0, 0.2],
col2: [0.4, 0.7, 0.0, 0.5],
selected_fish: None,
auto_resize_state: Default::default(),
file_menu: Default::default()
}
@ -335,6 +337,30 @@ fn show_test_window<'a>(ui: &Ui<'a>, state: &mut State, opened: &mut bool) {
ui.spacing();
});
}
if ui.collapsing_header(im_str!("Popups & Modal windows")).build() {
ui.tree_node(im_str!("Popups")).build(|| {
ui.text_wrapped(im_str!("When a popup is active, it inhibits interacting with windows that are behind the popup. Clicking outside the popup closes it."));
let names = [im_str!("Bream"), im_str!("Haddock"), im_str!("Mackerel"), im_str!("Pollock"), im_str!("Tilefish")];
if ui.small_button(im_str!("Select..")) {
ui.open_popup(im_str!("select"));
}
ui.same_line(0.0);
ui.text(
match state.selected_fish {
Some(index) => names[index].clone(),
None => im_str!("<None>")
});
ui.popup(im_str!("select"), || {
ui.text(im_str!("Aquarium"));
ui.separator();
for (index, name) in names.iter().enumerate() {
if ui.selectable(name.clone()) {
state.selected_fish = Some(index);
}
}
});
});
}
})
}

View File

@ -24,6 +24,8 @@ pub use imgui_sys::{
ImGuiInputTextFlags_AllowTabInput, ImGuiInputTextFlags_CtrlEnterForNewLine,
ImGuiInputTextFlags_NoHorizontalScroll, ImGuiInputTextFlags_AlwaysInsertMode,
ImGuiInputTextFlags_ReadOnly,
ImGuiSelectableFlags,
ImGuiSelectableFlags_DontClosePopups, ImGuiSelectableFlags_SpanAllColumns,
ImGuiSetCond,
ImGuiSetCond_Always, ImGuiSetCond_Once,
ImGuiSetCond_FirstUseEver, ImGuiSetCond_Appearing,
@ -518,6 +520,13 @@ impl<'ui> Ui<'ui> {
}
}
// Widgets: Selectable / Lists
impl<'ui> Ui<'ui> {
pub fn selectable<'p>(&self, label: ImStr<'p>) -> bool {
unsafe { imgui_sys::igSelectable(label.as_ptr(), false, ImGuiSelectableFlags::empty(), ImVec2::new(0.0,0.0)) }
}
}
// Widgets: Menus
impl<'ui> Ui<'ui> {
pub fn main_menu_bar<F>(&self, f: F) where F: FnOnce() {
@ -537,3 +546,17 @@ impl<'ui> Ui<'ui> {
pub fn menu<'p>(&self, label: ImStr<'p>) -> Menu<'ui, 'p> { Menu::new(label) }
pub fn menu_item<'p>(&self, label: ImStr<'p>) -> MenuItem<'ui, 'p> { MenuItem::new(label) }
}
// Widgets: Popups
impl<'ui> Ui<'ui> {
pub fn open_popup<'p>(&self, str_id: ImStr<'p>) {
unsafe { imgui_sys::igOpenPopup(str_id.as_ptr()) };
}
pub fn popup<'p, F>(&self, str_id: ImStr<'p>, f: F) where F: FnOnce() {
let render = unsafe { imgui_sys::igBeginPopup(str_id.as_ptr()) };
if render {
f();
unsafe { imgui_sys::igEndPopup() };
}
}
}