Fix formatting

I don't know where that 3 spaces nonsense came from... :/
This commit is contained in:
Joonas Javanainen 2015-08-25 21:23:23 +01:00
parent 1a42c45373
commit fa5bd75bb3
7 changed files with 674 additions and 677 deletions

View File

@ -13,7 +13,7 @@ const CLEAR_COLOR: (f32, f32, f32, f32) = (1.0, 1.0, 1.0, 1.0);
fn main() { fn main() {
let mut support = Support::init(); let mut support = Support::init();
loop { loop {
let mut open = true; let mut open = true;
let active = support.render(CLEAR_COLOR, |ui| { let active = support.render(CLEAR_COLOR, |ui| {
ui.show_test_window(&mut open) ui.show_test_window(&mut open)

View File

@ -112,15 +112,15 @@ pub enum ImGuiKey {
pub const ImGuiKey_COUNT: usize = 19; pub const ImGuiKey_COUNT: usize = 19;
bitflags!( bitflags!(
#[repr(C)] #[repr(C)]
flags ImGuiAlign: c_int { flags ImGuiAlign: c_int {
const ImGuiAlign_Left = 1 << 0, const ImGuiAlign_Left = 1 << 0,
const ImGuiAlign_Center = 1 << 1, const ImGuiAlign_Center = 1 << 1,
const ImGuiAlign_Right = 1 << 2, const ImGuiAlign_Right = 1 << 2,
const ImGuiAlign_Top = 1 << 3, const ImGuiAlign_Top = 1 << 3,
const ImGuiAlign_VCenter = 1 << 4, const ImGuiAlign_VCenter = 1 << 4,
const ImGuiAlign_Default = ImGuiAlign_Left.bits | ImGuiAlign_Top.bits const ImGuiAlign_Default = ImGuiAlign_Left.bits | ImGuiAlign_Top.bits
} }
); );
#[repr(C)] #[repr(C)]
@ -148,80 +148,80 @@ pub enum ImGuiMouseCursor {
pub const ImGuiMouseCursor_COUNT: usize = 7; pub const ImGuiMouseCursor_COUNT: usize = 7;
bitflags!( bitflags!(
#[repr(C)] #[repr(C)]
flags ImGuiWindowFlags: c_int { flags ImGuiWindowFlags: c_int {
const ImGuiWindowFlags_NoTitleBar = 1 << 0, const ImGuiWindowFlags_NoTitleBar = 1 << 0,
const ImGuiWindowFlags_NoResize = 1 << 1, const ImGuiWindowFlags_NoResize = 1 << 1,
const ImGuiWindowFlags_NoMove = 1 << 2, const ImGuiWindowFlags_NoMove = 1 << 2,
const ImGuiWindowFlags_NoScrollbar = 1 << 3, const ImGuiWindowFlags_NoScrollbar = 1 << 3,
const ImGuiWindowFlags_NoScrollWithMouse = 1 << 4, const ImGuiWindowFlags_NoScrollWithMouse = 1 << 4,
const ImGuiWindowFlags_NoCollapse = 1 << 5, const ImGuiWindowFlags_NoCollapse = 1 << 5,
const ImGuiWindowFlags_AlwaysAutoResize = 1 << 6, const ImGuiWindowFlags_AlwaysAutoResize = 1 << 6,
const ImGuiWindowFlags_ShowBorders = 1 << 7, const ImGuiWindowFlags_ShowBorders = 1 << 7,
const ImGuiWindowFlags_NoSavedSettings = 1 << 8, const ImGuiWindowFlags_NoSavedSettings = 1 << 8,
const ImGuiWindowFlags_NoInputs = 1 << 9, const ImGuiWindowFlags_NoInputs = 1 << 9,
const ImGuiWindowFlags_MenuBar = 1 << 10, const ImGuiWindowFlags_MenuBar = 1 << 10,
const ImGuiWindowFlags_ChildWindow = 1 << 20, const ImGuiWindowFlags_ChildWindow = 1 << 20,
const ImGuiWindowFlags_ChildWindowAutoFitX = 1 << 21, const ImGuiWindowFlags_ChildWindowAutoFitX = 1 << 21,
const ImGuiWindowFlags_ChildWindowAutoFitY = 1 << 22, const ImGuiWindowFlags_ChildWindowAutoFitY = 1 << 22,
const ImGuiWindowFlags_ComboBox = 1 << 23, const ImGuiWindowFlags_ComboBox = 1 << 23,
const ImGuiWindowFlags_Tooltip = 1 << 24, const ImGuiWindowFlags_Tooltip = 1 << 24,
const ImGuiWindowFlags_Popup = 1 << 25, const ImGuiWindowFlags_Popup = 1 << 25,
const ImGuiWindowFlags_Modal = 1 << 26, const ImGuiWindowFlags_Modal = 1 << 26,
const ImGuiWindowFlags_ChildMenu = 1 << 27 const ImGuiWindowFlags_ChildMenu = 1 << 27
} }
); );
impl ImGuiWindowFlags { impl ImGuiWindowFlags {
#[inline] #[inline]
pub fn with(self, mask: ImGuiWindowFlags, value: bool) -> ImGuiWindowFlags { pub fn with(self, mask: ImGuiWindowFlags, value: bool) -> ImGuiWindowFlags {
if value { self | mask } else { self - mask } if value { self | mask } else { self - mask }
} }
} }
bitflags!( bitflags!(
#[repr(C)] #[repr(C)]
flags ImGuiSetCond: c_int { flags ImGuiSetCond: c_int {
const ImGuiSetCond_Always = 1 << 0, const ImGuiSetCond_Always = 1 << 0,
const ImGuiSetCond_Once = 1 << 1, const ImGuiSetCond_Once = 1 << 1,
const ImGuiSetCond_FirstUseEver = 1 << 2, const ImGuiSetCond_FirstUseEver = 1 << 2,
const ImGuiSetCond_Appearing = 1 << 3 const ImGuiSetCond_Appearing = 1 << 3
} }
); );
bitflags!( bitflags!(
#[repr(C)] #[repr(C)]
flags ImGuiInputTextFlags: c_int { flags ImGuiInputTextFlags: c_int {
const ImGuiInputTextFlags_CharsDecimal = 1 << 0, const ImGuiInputTextFlags_CharsDecimal = 1 << 0,
const ImGuiInputTextFlags_CharsHexadecimal = 1 << 1, const ImGuiInputTextFlags_CharsHexadecimal = 1 << 1,
const ImGuiInputTextFlags_CharsUppercase = 1 << 2, const ImGuiInputTextFlags_CharsUppercase = 1 << 2,
const ImGuiInputTextFlags_CharsNoBlank = 1 << 3, const ImGuiInputTextFlags_CharsNoBlank = 1 << 3,
const ImGuiInputTextFlags_AutoSelectAll = 1 << 4, const ImGuiInputTextFlags_AutoSelectAll = 1 << 4,
const ImGuiInputTextFlags_EnterReturnsTrue = 1 << 5, const ImGuiInputTextFlags_EnterReturnsTrue = 1 << 5,
const ImGuiInputTextFlags_CallbackCompletion = 1 << 6, const ImGuiInputTextFlags_CallbackCompletion = 1 << 6,
const ImGuiInputTextFlags_CallbackHistory = 1 << 7, const ImGuiInputTextFlags_CallbackHistory = 1 << 7,
const ImGuiInputTextFlags_CallbackAlways = 1 << 8, const ImGuiInputTextFlags_CallbackAlways = 1 << 8,
const ImGuiInputTextFlags_CallbackCharFilter = 1 << 9, const ImGuiInputTextFlags_CallbackCharFilter = 1 << 9,
const ImGuiInputTextFlags_AllowTabInput = 1 << 10, const ImGuiInputTextFlags_AllowTabInput = 1 << 10,
const ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11, const ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11,
const ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, const ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12,
const ImGuiInputTextFlags_AlwaysInsertMode = 1 << 13, const ImGuiInputTextFlags_AlwaysInsertMode = 1 << 13,
const ImGuiInputTextFlags_Multiline = 1 << 20, const ImGuiInputTextFlags_Multiline = 1 << 20,
} }
); );
bitflags!( bitflags!(
#[repr(C)] #[repr(C)]
flags ImGuiSelectableFlags: c_int { flags ImGuiSelectableFlags: c_int {
const ImGuiSelectableFlags_DontClosePopups = 1 << 0, const ImGuiSelectableFlags_DontClosePopups = 1 << 0,
const ImGuiSelectableFlags_SpanAllColumns = 1 << 1 const ImGuiSelectableFlags_SpanAllColumns = 1 << 1
} }
); );
pub type ImGuiTextEditCallback = pub type ImGuiTextEditCallback =
Option<extern "C" fn(data: *mut ImGuiTextEditCallbackData) -> c_int>; Option<extern "C" fn(data: *mut ImGuiTextEditCallbackData) -> c_int>;
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, Debug, Default)] #[derive(Copy, Clone, Debug, Default)]
@ -231,12 +231,12 @@ pub struct ImVec2 {
} }
impl ImVec2 { impl ImVec2 {
pub fn new(x: f32, y: f32) -> ImVec2 { pub fn new(x: f32, y: f32) -> ImVec2 {
ImVec2 { ImVec2 {
x: x as c_float, x: x as c_float,
y: y as c_float y: y as c_float
} }
} }
} }
#[cfg(feature = "glium")] #[cfg(feature = "glium")]
@ -254,14 +254,14 @@ pub struct ImVec4 {
} }
impl ImVec4 { impl ImVec4 {
pub fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 { pub fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 {
ImVec4 { ImVec4 {
x: x as c_float, x: x as c_float,
y: y as c_float, y: y as c_float,
z: z as c_float, z: z as c_float,
w: w as c_float w: w as c_float
} }
} }
} }
#[cfg(feature = "glium")] #[cfg(feature = "glium")]
@ -368,9 +368,9 @@ pub struct ImVector<T> {
} }
impl<T> ImVector<T> { impl<T> ImVector<T> {
pub unsafe fn as_slice(&self) -> &[T] { pub unsafe fn as_slice(&self) -> &[T] {
slice::from_raw_parts(self.data, self.size as usize) slice::from_raw_parts(self.data, self.size as usize)
} }
} }
#[repr(C)] #[repr(C)]
@ -500,10 +500,10 @@ pub struct ImDrawData {
} }
impl ImDrawData { impl ImDrawData {
pub unsafe fn cmd_lists(&self) -> &[*const ImDrawList] { pub unsafe fn cmd_lists(&self) -> &[*const ImDrawList] {
let cmd_lists: *const *const ImDrawList = mem::transmute(self.cmd_lists); let cmd_lists: *const *const ImDrawList = mem::transmute(self.cmd_lists);
slice::from_raw_parts(cmd_lists, self.cmd_lists_count as usize) slice::from_raw_parts(cmd_lists, self.cmd_lists_count as usize)
} }
} }
#[repr(C)] #[repr(C)]
@ -735,8 +735,7 @@ extern "C" {
items_separated_by_zeros: *const c_char, height_in_items: c_int) -> bool; items_separated_by_zeros: *const c_char, height_in_items: c_int) -> bool;
pub fn igCombo3(label: *const c_char, current_item: *mut c_int, pub fn igCombo3(label: *const c_char, current_item: *mut c_int,
items_getter: extern "C" fn(data: *mut c_void, items_getter: extern "C" fn(data: *mut c_void,
idx: c_int, idx: c_int, out_text: *mut *const c_char) -> bool,
out_text: *mut *const c_char) -> bool,
data: *mut c_void, items_count: c_int, data: *mut c_void, items_count: c_int,
height_in_items: c_int) -> bool; height_in_items: c_int) -> bool;
pub fn igColorButton(col: ImVec4, small_height: bool, outline_border: bool) -> bool; pub fn igColorButton(col: ImVec4, small_height: bool, outline_border: bool) -> bool;
@ -749,8 +748,7 @@ extern "C" {
scale_min: c_float, scale_max: c_float, scale_min: c_float, scale_max: c_float,
graph_size: ImVec2, stride: c_int); graph_size: ImVec2, stride: c_int);
pub fn igPlotLines2(label: *const c_char, pub fn igPlotLines2(label: *const c_char,
values_getter: extern "C" fn(data: *mut c_void, values_getter: extern "C" fn(data: *mut c_void, idx: c_int) -> c_float,
idx: c_int) -> c_float,
data: *mut c_void, data: *mut c_void,
values_count: c_int, values_offset: c_int, values_count: c_int, values_offset: c_int,
overlay_text: *const c_char, overlay_text: *const c_char,
@ -761,8 +759,7 @@ extern "C" {
scale_min: c_float, scale_max: c_float, scale_min: c_float, scale_max: c_float,
graph_size: ImVec2, stride: c_int); graph_size: ImVec2, stride: c_int);
pub fn igPlotHistogram2(label: *const c_char, pub fn igPlotHistogram2(label: *const c_char,
values_getter: extern "C" fn(data: *mut c_void, values_getter: extern "C" fn(data: *mut c_void, idx: c_int) -> c_float,
idx: c_int) -> c_float,
data: *mut c_void, data: *mut c_void,
values_count: c_int, values_offset: c_int, values_count: c_int, values_offset: c_int,
overlay_text: *const c_char, overlay_text: *const c_char,
@ -845,7 +842,7 @@ extern "C" {
// Widgets: Input // Widgets: Input
extern "C" { extern "C" {
pub fn igInputText(label: *const c_char, buf: *mut c_char, pub fn igInputText(label: *const c_char, buf: *mut c_char,
buf_size: size_t, flags: ImGuiInputTextFlags, buf_size: size_t, flags: ImGuiInputTextFlags,
callback: ImGuiTextEditCallback, user_data: *mut c_void) -> bool; callback: ImGuiTextEditCallback, user_data: *mut c_void) -> bool;
pub fn igInputTextMultiline(label: *const c_char, pub fn igInputTextMultiline(label: *const c_char,
@ -1054,11 +1051,11 @@ extern "C" {
font_cfg: *const ImFontConfig, font_cfg: *const ImFontConfig,
glyph_ranges: *const ImWchar) -> *mut ImFont; glyph_ranges: *const ImWchar) -> *mut ImFont;
pub fn ImFontAtlas_AddFontFromMemoryCompressedBase85TTF( pub fn ImFontAtlas_AddFontFromMemoryCompressedBase85TTF(
atlas: *mut ImFontAtlas, atlas: *mut ImFontAtlas,
compressed_ttf_data_base85: *const c_char, compressed_ttf_data_base85: *const c_char,
size_pixels: c_float, size_pixels: c_float,
font_cfg: *const ImFontConfig, font_cfg: *const ImFontConfig,
glyph_ranges: *const ImWchar) -> *mut ImFont; glyph_ranges: *const ImWchar) -> *mut ImFont;
pub fn ImFontAtlas_ClearTexData(atlas: *mut ImFontAtlas); pub fn ImFontAtlas_ClearTexData(atlas: *mut ImFontAtlas);
pub fn ImFontAtlas_Clear(atlas: *mut ImFontAtlas); pub fn ImFontAtlas_Clear(atlas: *mut ImFontAtlas);
pub fn ImGuiIO_AddInputCharacter(c: c_ushort); pub fn ImGuiIO_AddInputCharacter(c: c_ushort);

View File

@ -19,16 +19,16 @@ use std::slice;
use std::str; use std::str;
pub use imgui_sys::{ pub use imgui_sys::{
ImDrawIdx, ImDrawVert, ImDrawIdx, ImDrawVert,
ImGuiSetCond, ImGuiSetCond,
ImGuiSetCond_Always, ImGuiSetCond_Once, ImGuiSetCond_Always, ImGuiSetCond_Once,
ImGuiSetCond_FirstUseEver, ImGuiSetCond_Appearing, ImGuiSetCond_FirstUseEver, ImGuiSetCond_Appearing,
ImGuiWindowFlags, ImGuiWindowFlags,
ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove, ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove,
ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse,
ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders, ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders,
ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar, ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar,
ImVec2, ImVec4 ImVec2, ImVec4
}; };
pub use menus::{Menu, MenuItem}; pub use menus::{Menu, MenuItem};
pub use sliders::{SliderFloat, SliderInt}; pub use sliders::{SliderFloat, SliderInt};
@ -47,144 +47,144 @@ pub struct ImGui;
#[macro_export] #[macro_export]
macro_rules! im_str { macro_rules! im_str {
($e:tt) => ({ ($e:tt) => ({
let value = concat!($e, "\0"); let value = concat!($e, "\0");
unsafe { ::imgui::ImStr::from_bytes(value.as_bytes()) } unsafe { ::imgui::ImStr::from_bytes(value.as_bytes()) }
}); });
($e:tt, $($arg:tt)*) => ({ ($e:tt, $($arg:tt)*) => ({
::imgui::ImStr::from_fmt(format_args!($e, $($arg)*)) ::imgui::ImStr::from_fmt(format_args!($e, $($arg)*))
}) })
} }
pub struct ImStr<'a> { pub struct ImStr<'a> {
bytes: Cow<'a, [u8]> bytes: Cow<'a, [u8]>
} }
impl<'a> ImStr<'a> { impl<'a> ImStr<'a> {
pub unsafe fn from_bytes(bytes: &'a [u8]) -> ImStr<'a> { pub unsafe fn from_bytes(bytes: &'a [u8]) -> ImStr<'a> {
ImStr { ImStr {
bytes: Cow::Borrowed(bytes) bytes: Cow::Borrowed(bytes)
} }
} }
pub fn from_str(value: &str) -> ImStr<'a> { pub fn from_str(value: &str) -> ImStr<'a> {
let mut bytes: Vec<u8> = value.bytes().collect(); let mut bytes: Vec<u8> = value.bytes().collect();
bytes.push(0); bytes.push(0);
ImStr { ImStr {
bytes: Cow::Owned(bytes) bytes: Cow::Owned(bytes)
} }
} }
pub fn from_fmt(args: fmt::Arguments) -> ImStr<'a> { pub fn from_fmt(args: fmt::Arguments) -> ImStr<'a> {
let mut bytes = fmt::format(args).into_bytes(); let mut bytes = fmt::format(args).into_bytes();
bytes.push(0); bytes.push(0);
ImStr { ImStr {
bytes: Cow::Owned(bytes) bytes: Cow::Owned(bytes)
} }
} }
fn as_ptr(&self) -> *const c_char { self.bytes.as_ptr() as *const c_char } fn as_ptr(&self) -> *const c_char { self.bytes.as_ptr() as *const c_char }
} }
pub struct TextureHandle<'a> { pub struct TextureHandle<'a> {
pub width: u32, pub width: u32,
pub height: u32, pub height: u32,
pub pixels: &'a [c_uchar] pub pixels: &'a [c_uchar]
} }
pub fn get_version() -> &'static str { pub fn get_version() -> &'static str {
unsafe { unsafe {
let bytes = CStr::from_ptr(imgui_sys::igGetVersion()).to_bytes(); let bytes = CStr::from_ptr(imgui_sys::igGetVersion()).to_bytes();
str::from_utf8_unchecked(bytes) str::from_utf8_unchecked(bytes)
} }
} }
impl ImGui { impl ImGui {
pub fn init() -> ImGui { pub fn init() -> ImGui {
let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) };
io.render_draw_lists_fn = Some(render_draw_lists); io.render_draw_lists_fn = Some(render_draw_lists);
ImGui ImGui
} }
pub fn prepare_texture<'a, F, T>(&mut self, f: F) -> T where F: FnOnce(TextureHandle<'a>) -> T { pub fn prepare_texture<'a, F, T>(&mut self, f: F) -> T where F: FnOnce(TextureHandle<'a>) -> T {
let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) };
let mut pixels: *mut c_uchar = ptr::null_mut(); let mut pixels: *mut c_uchar = ptr::null_mut();
let mut width: c_int = 0; let mut width: c_int = 0;
let mut height: c_int = 0; let mut height: c_int = 0;
let mut bytes_per_pixel: c_int = 0; let mut bytes_per_pixel: c_int = 0;
unsafe { unsafe {
imgui_sys::ImFontAtlas_GetTexDataAsRGBA32(io.fonts, &mut pixels, &mut width, &mut height, &mut bytes_per_pixel); imgui_sys::ImFontAtlas_GetTexDataAsRGBA32(io.fonts, &mut pixels, &mut width, &mut height, &mut bytes_per_pixel);
f(TextureHandle { f(TextureHandle {
width: width as u32, width: width as u32,
height: height as u32, height: height as u32,
pixels: slice::from_raw_parts(pixels, (width * height * bytes_per_pixel) as usize) pixels: slice::from_raw_parts(pixels, (width * height * bytes_per_pixel) as usize)
}) })
} }
} }
pub fn draw_mouse_cursor(&mut self, value: bool) { pub fn draw_mouse_cursor(&mut self, value: bool) {
let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) };
io.mouse_draw_cursor = value; io.mouse_draw_cursor = value;
} }
pub fn mouse_pos(&self) -> (f32, f32) { pub fn mouse_pos(&self) -> (f32, f32) {
let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) };
(io.mouse_pos.x, io.mouse_pos.y) (io.mouse_pos.x, io.mouse_pos.y)
} }
pub fn set_mouse_pos(&mut self, x: f32, y: f32) { pub fn set_mouse_pos(&mut self, x: f32, y: f32) {
let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) };
io.mouse_pos.x = x; io.mouse_pos.x = x;
io.mouse_pos.y = y; io.mouse_pos.y = y;
} }
pub fn set_mouse_down(&mut self, states: &[bool; 5]) { pub fn set_mouse_down(&mut self, states: &[bool; 5]) {
let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) };
io.mouse_down = *states; io.mouse_down = *states;
} }
pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } } pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } }
pub fn get_frame_count(&self) -> i32 { unsafe { imgui_sys::igGetFrameCount() } } pub fn get_frame_count(&self) -> i32 { unsafe { imgui_sys::igGetFrameCount() } }
pub fn frame<'ui, 'a: 'ui>(&'a mut self, width: u32, height: u32, delta_time: f32) -> Ui<'ui> { pub fn frame<'ui, 'a: 'ui>(&'a mut self, width: u32, height: u32, delta_time: f32) -> Ui<'ui> {
unsafe { unsafe {
let io: &mut imgui_sys::ImGuiIO = mem::transmute(imgui_sys::igGetIO()); let io: &mut imgui_sys::ImGuiIO = mem::transmute(imgui_sys::igGetIO());
io.display_size.x = width as c_float; io.display_size.x = width as c_float;
io.display_size.y = height as c_float; io.display_size.y = height as c_float;
io.delta_time = delta_time; io.delta_time = delta_time;
imgui_sys::igNewFrame(); imgui_sys::igNewFrame();
} }
Ui { Ui {
imgui: self imgui: self
} }
} }
} }
impl Drop for ImGui { impl Drop for ImGui {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
imgui_sys::igShutdown(); imgui_sys::igShutdown();
} }
} }
} }
#[cfg(feature = "sdl2")] #[cfg(feature = "sdl2")]
impl ImGui { impl ImGui {
pub fn update_mouse(&mut self, mouse: &::sdl2::mouse::MouseUtil) { pub fn update_mouse(&mut self, mouse: &::sdl2::mouse::MouseUtil) {
let (mouse_state, mouse_x, mouse_y) = mouse.get_mouse_state(); let (mouse_state, mouse_x, mouse_y) = mouse.get_mouse_state();
let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) };
io.mouse_pos.x = mouse_x as f32; io.mouse_pos.x = mouse_x as f32;
io.mouse_pos.y = mouse_y as f32; io.mouse_pos.y = mouse_y as f32;
io.mouse_down = [ io.mouse_down = [
mouse_state.left(), mouse_state.left(),
mouse_state.right(), mouse_state.right(),
mouse_state.middle(), mouse_state.middle(),
mouse_state.x1(), mouse_state.x1(),
mouse_state.x2() mouse_state.x2()
]; ];
} }
} }
pub struct DrawList<'a> { pub struct DrawList<'a> {
pub cmd_buffer: &'a [imgui_sys::ImDrawCmd], pub cmd_buffer: &'a [imgui_sys::ImDrawCmd],
pub idx_buffer: &'a [imgui_sys::ImDrawIdx], pub idx_buffer: &'a [imgui_sys::ImDrawIdx],
pub vtx_buffer: &'a [imgui_sys::ImDrawVert] pub vtx_buffer: &'a [imgui_sys::ImDrawVert]
} }
pub struct Ui<'ui> { pub struct Ui<'ui> {
imgui: &'ui ImGui imgui: &'ui ImGui
} }
static FMT: &'static [u8] = b"%s\0"; static FMT: &'static [u8] = b"%s\0";
@ -192,137 +192,137 @@ static FMT: &'static [u8] = b"%s\0";
fn fmt_ptr() -> *const c_char { FMT.as_ptr() as *const c_char } fn fmt_ptr() -> *const c_char { FMT.as_ptr() as *const c_char }
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn imgui(&self) -> &ImGui { self.imgui } pub fn imgui(&self) -> &ImGui { self.imgui }
pub fn render<F, E>(self, mut f: F) -> Result<(), E> pub fn render<F, E>(self, mut f: F) -> Result<(), E>
where F: FnMut(DrawList<'ui>) -> Result<(), E> { where F: FnMut(DrawList<'ui>) -> Result<(), E> {
unsafe { unsafe {
let mut im_draw_data = mem::zeroed(); let mut im_draw_data = mem::zeroed();
RENDER_DRAW_LISTS_STATE.0 = &mut im_draw_data; RENDER_DRAW_LISTS_STATE.0 = &mut im_draw_data;
imgui_sys::igRender(); imgui_sys::igRender();
RENDER_DRAW_LISTS_STATE.0 = 0 as *mut imgui_sys::ImDrawData; RENDER_DRAW_LISTS_STATE.0 = 0 as *mut imgui_sys::ImDrawData;
for &cmd_list in im_draw_data.cmd_lists() { for &cmd_list in im_draw_data.cmd_lists() {
let draw_list = let draw_list =
DrawList { DrawList {
cmd_buffer: (*cmd_list).cmd_buffer.as_slice(), cmd_buffer: (*cmd_list).cmd_buffer.as_slice(),
idx_buffer: (*cmd_list).idx_buffer.as_slice(), idx_buffer: (*cmd_list).idx_buffer.as_slice(),
vtx_buffer: (*cmd_list).vtx_buffer.as_slice() vtx_buffer: (*cmd_list).vtx_buffer.as_slice()
}; };
try!(f(draw_list)); try!(f(draw_list));
} }
} }
Ok(()) Ok(())
} }
pub fn show_user_guide(&self) { unsafe { imgui_sys::igShowUserGuide() }; } pub fn show_user_guide(&self) { unsafe { imgui_sys::igShowUserGuide() }; }
pub fn show_test_window(&self, opened: &mut bool) { pub fn show_test_window(&self, opened: &mut bool) {
unsafe { unsafe {
imgui_sys::igShowTestWindow(opened); imgui_sys::igShowTestWindow(opened);
} }
} }
pub fn show_metrics_window(&self, opened: &mut bool) { pub fn show_metrics_window(&self, opened: &mut bool) {
unsafe { unsafe {
imgui_sys::igShowMetricsWindow(opened); imgui_sys::igShowMetricsWindow(opened);
} }
} }
} }
// Window // Window
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn window<'p>(&self) -> Window<'ui, 'p> { Window::new() } pub fn window<'p>(&self) -> Window<'ui, 'p> { Window::new() }
} }
// Layout // Layout
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn separator(&self) { unsafe { imgui_sys::igSeparator() }; } pub fn separator(&self) { unsafe { imgui_sys::igSeparator() }; }
pub fn same_line(&self, pos_x: f32) { pub fn same_line(&self, pos_x: f32) {
unsafe { unsafe {
imgui_sys::igSameLine(pos_x, -1.0f32) imgui_sys::igSameLine(pos_x, -1.0f32)
} }
} }
pub fn same_line_spacing(&self, pos_x: f32, spacing_w: f32) { pub fn same_line_spacing(&self, pos_x: f32, spacing_w: f32) {
unsafe { unsafe {
imgui_sys::igSameLine(pos_x, spacing_w) imgui_sys::igSameLine(pos_x, spacing_w)
} }
} }
pub fn spacing(&self) { unsafe { imgui_sys::igSpacing() }; } pub fn spacing(&self) { unsafe { imgui_sys::igSpacing() }; }
} }
// Widgets // Widgets
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn text<'b>(&self, text: ImStr<'b>) { pub fn text<'b>(&self, text: ImStr<'b>) {
// TODO: use igTextUnformatted // TODO: use igTextUnformatted
unsafe { unsafe {
imgui_sys::igText(fmt_ptr(), text.as_ptr()); imgui_sys::igText(fmt_ptr(), text.as_ptr());
} }
} }
pub fn text_colored<'b, A>(&self, col: A, text: ImStr<'b>) where A: Into<ImVec4> { pub fn text_colored<'b, A>(&self, col: A, text: ImStr<'b>) where A: Into<ImVec4> {
unsafe { unsafe {
imgui_sys::igTextColored(col.into(), fmt_ptr(), text.as_ptr()); imgui_sys::igTextColored(col.into(), fmt_ptr(), text.as_ptr());
} }
} }
pub fn text_disabled<'b>(&self, text: ImStr<'b>) { pub fn text_disabled<'b>(&self, text: ImStr<'b>) {
unsafe { unsafe {
imgui_sys::igTextDisabled(fmt_ptr(), text.as_ptr()); imgui_sys::igTextDisabled(fmt_ptr(), text.as_ptr());
} }
} }
pub fn text_wrapped<'b>(&self, text: ImStr<'b>) { pub fn text_wrapped<'b>(&self, text: ImStr<'b>) {
unsafe { unsafe {
imgui_sys::igTextWrapped(fmt_ptr(), text.as_ptr()); imgui_sys::igTextWrapped(fmt_ptr(), text.as_ptr());
} }
} }
pub fn label_text<'b>(&self, label: ImStr<'b>, text: ImStr<'b>) { pub fn label_text<'b>(&self, label: ImStr<'b>, text: ImStr<'b>) {
unsafe { unsafe {
imgui_sys::igLabelText(label.as_ptr(), fmt_ptr(), text.as_ptr()); imgui_sys::igLabelText(label.as_ptr(), fmt_ptr(), text.as_ptr());
} }
} }
pub fn bullet(&self) { pub fn bullet(&self) {
unsafe { unsafe {
imgui_sys::igBullet(); imgui_sys::igBullet();
} }
} }
pub fn bullet_text<'b>(&self, text: ImStr<'b>) { pub fn bullet_text<'b>(&self, text: ImStr<'b>) {
unsafe { unsafe {
imgui_sys::igBulletText(fmt_ptr(), text.as_ptr()); imgui_sys::igBulletText(fmt_ptr(), text.as_ptr());
} }
} }
pub fn collapsing_header<'p>(&self, label: ImStr<'p>) -> CollapsingHeader<'ui, 'p> { pub fn collapsing_header<'p>(&self, label: ImStr<'p>) -> CollapsingHeader<'ui, 'p> {
CollapsingHeader::new(label) CollapsingHeader::new(label)
} }
pub fn checkbox<'p>(&self, label: ImStr<'p>, value: &'p mut bool) -> bool { pub fn checkbox<'p>(&self, label: ImStr<'p>, value: &'p mut bool) -> bool {
unsafe { imgui_sys::igCheckbox(label.as_ptr(), value) } unsafe { imgui_sys::igCheckbox(label.as_ptr(), value) }
} }
} }
// Widgets: Sliders // Widgets: Sliders
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn slider_f32<'p>(&self, label: ImStr<'p>, pub fn slider_f32<'p>(&self, label: ImStr<'p>,
value: &'p mut f32, min: f32, max: f32) -> SliderFloat<'ui, 'p> { value: &'p mut f32, min: f32, max: f32) -> SliderFloat<'ui, 'p> {
SliderFloat::new(label, value, min, max) SliderFloat::new(label, value, min, max)
} }
pub fn slider_i32<'p>(&self, label: ImStr<'p>, pub fn slider_i32<'p>(&self, label: ImStr<'p>,
value: &'p mut i32, min: i32, max: i32) -> SliderInt<'ui, 'p> { value: &'p mut i32, min: i32, max: i32) -> SliderInt<'ui, 'p> {
SliderInt::new(label, value, min, max) SliderInt::new(label, value, min, max)
} }
} }
// Widgets: Menus // Widgets: Menus
impl<'ui> Ui<'ui> { impl<'ui> Ui<'ui> {
pub fn main_menu_bar<F>(&self, f: F) where F: FnOnce() { pub fn main_menu_bar<F>(&self, f: F) where F: FnOnce() {
let render = unsafe { imgui_sys::igBeginMainMenuBar() }; let render = unsafe { imgui_sys::igBeginMainMenuBar() };
if render { if render {
f(); f();
unsafe { imgui_sys::igEndMainMenuBar() }; unsafe { imgui_sys::igEndMainMenuBar() };
} }
} }
pub fn menu_bar<F>(&self, f: F) where F: FnOnce() { pub fn menu_bar<F>(&self, f: F) where F: FnOnce() {
let render = unsafe { imgui_sys::igBeginMenuBar() }; let render = unsafe { imgui_sys::igBeginMenuBar() };
if render { if render {
f(); f();
unsafe { imgui_sys::igEndMenuBar() }; unsafe { imgui_sys::igEndMenuBar() };
} }
} }
pub fn menu<'p>(&self, label: ImStr<'p>) -> Menu<'ui, 'p> { Menu::new(label) } 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) } pub fn menu_item<'p>(&self, label: ImStr<'p>) -> MenuItem<'ui, 'p> { MenuItem::new(label) }
} }
struct RenderDrawListsState(*mut imgui_sys::ImDrawData); struct RenderDrawListsState(*mut imgui_sys::ImDrawData);
@ -332,7 +332,7 @@ static mut RENDER_DRAW_LISTS_STATE: RenderDrawListsState =
RenderDrawListsState(0 as *mut imgui_sys::ImDrawData); RenderDrawListsState(0 as *mut imgui_sys::ImDrawData);
extern "C" fn render_draw_lists(data: *mut imgui_sys::ImDrawData) { extern "C" fn render_draw_lists(data: *mut imgui_sys::ImDrawData) {
unsafe { unsafe {
ptr::copy_nonoverlapping(data, RENDER_DRAW_LISTS_STATE.0, 1); ptr::copy_nonoverlapping(data, RENDER_DRAW_LISTS_STATE.0, 1);
} }
} }

View File

@ -5,81 +5,81 @@ use std::ptr;
use super::{Ui, ImStr}; use super::{Ui, ImStr};
pub struct Menu<'ui, 'p> { pub struct Menu<'ui, 'p> {
label: ImStr<'p>, label: ImStr<'p>,
enabled: bool, enabled: bool,
_phantom: PhantomData<&'ui Ui<'ui>> _phantom: PhantomData<&'ui Ui<'ui>>
} }
impl<'ui, 'p> Menu<'ui, 'p> { impl<'ui, 'p> Menu<'ui, 'p> {
pub fn new(label: ImStr<'p>) -> Self { pub fn new(label: ImStr<'p>) -> Self {
Menu { Menu {
label: label, label: label,
enabled: true, enabled: true,
_phantom: PhantomData _phantom: PhantomData
} }
} }
#[inline] #[inline]
pub fn enabled(self, enabled: bool) -> Self { pub fn enabled(self, enabled: bool) -> Self {
Menu { Menu {
enabled: enabled, enabled: enabled,
.. self .. self
} }
} }
pub fn build<F: FnOnce()>(self, f: F) { pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe { imgui_sys::igBeginMenu(self.label.as_ptr(), self.enabled) }; let render = unsafe { imgui_sys::igBeginMenu(self.label.as_ptr(), self.enabled) };
if render { if render {
f(); f();
unsafe { imgui_sys::igEndMenu() }; unsafe { imgui_sys::igEndMenu() };
} }
} }
} }
pub struct MenuItem<'ui, 'p> { pub struct MenuItem<'ui, 'p> {
label: ImStr<'p>, label: ImStr<'p>,
shortcut: Option<ImStr<'p>>, shortcut: Option<ImStr<'p>>,
selected: Option<&'p mut bool>, selected: Option<&'p mut bool>,
enabled: bool, enabled: bool,
_phantom: PhantomData<&'ui Ui<'ui>> _phantom: PhantomData<&'ui Ui<'ui>>
} }
impl<'ui, 'p> MenuItem<'ui, 'p> { impl<'ui, 'p> MenuItem<'ui, 'p> {
pub fn new(label: ImStr<'p>) -> Self { pub fn new(label: ImStr<'p>) -> Self {
MenuItem { MenuItem {
label: label, label: label,
shortcut: None, shortcut: None,
selected: None, selected: None,
enabled: true, enabled: true,
_phantom: PhantomData _phantom: PhantomData
} }
} }
#[inline] #[inline]
pub fn shortcut(self, shortcut: ImStr<'p>) -> Self { pub fn shortcut(self, shortcut: ImStr<'p>) -> Self {
MenuItem { MenuItem {
shortcut: Some(shortcut), shortcut: Some(shortcut),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn selected(self, selected: &'p mut bool) -> Self { pub fn selected(self, selected: &'p mut bool) -> Self {
MenuItem { MenuItem {
selected: Some(selected), selected: Some(selected),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn enabled(self, enabled: bool) -> Self { pub fn enabled(self, enabled: bool) -> Self {
MenuItem { MenuItem {
enabled: enabled, enabled: enabled,
.. self .. self
} }
} }
pub fn build(self) -> bool { pub fn build(self) -> bool {
let label = self.label.as_ptr(); let label = self.label.as_ptr();
let shortcut = self.shortcut.map(|x| x.as_ptr()).unwrap_or(ptr::null()); let shortcut = self.shortcut.map(|x| x.as_ptr()).unwrap_or(ptr::null());
let selected = self.selected.map(|x| x as *mut bool).unwrap_or(ptr::null_mut()); let selected = self.selected.map(|x| x as *mut bool).unwrap_or(ptr::null_mut());
let enabled = self.enabled; let enabled = self.enabled;
unsafe { unsafe {
imgui_sys::igMenuItemPtr(label, shortcut, selected, enabled) imgui_sys::igMenuItemPtr(label, shortcut, selected, enabled)
} }
} }
} }

View File

@ -6,84 +6,84 @@ use super::{Ui, ImStr};
// TODO: Consider using Range, even though it is half-open // TODO: Consider using Range, even though it is half-open
pub struct SliderInt<'ui, 'p> { pub struct SliderInt<'ui, 'p> {
label: ImStr<'p>, label: ImStr<'p>,
value: &'p mut i32, value: &'p mut i32,
min: i32, min: i32,
max: i32, max: i32,
display_format: ImStr<'p>, display_format: ImStr<'p>,
_phantom: PhantomData<&'ui Ui<'ui>> _phantom: PhantomData<&'ui Ui<'ui>>
} }
impl<'ui, 'p> SliderInt<'ui, 'p> { impl<'ui, 'p> SliderInt<'ui, 'p> {
pub fn new(label: ImStr<'p>, value: &'p mut i32, min: i32, max: i32) -> Self { pub fn new(label: ImStr<'p>, value: &'p mut i32, min: i32, max: i32) -> Self {
SliderInt { SliderInt {
label: label, label: label,
value: value, value: value,
min: min, min: min,
max: max, max: max,
display_format: unsafe { ImStr::from_bytes(b"%.0f\0") }, display_format: unsafe { ImStr::from_bytes(b"%.0f\0") },
_phantom: PhantomData _phantom: PhantomData
} }
} }
#[inline] #[inline]
pub fn display_format(self, display_format: ImStr<'p>) -> Self { pub fn display_format(self, display_format: ImStr<'p>) -> Self {
SliderInt { SliderInt {
display_format: display_format, display_format: display_format,
.. self .. self
} }
} }
pub fn build(self) -> bool { pub fn build(self) -> bool {
unsafe { unsafe {
imgui_sys::igSliderInt(self.label.as_ptr(), self.value, self.min, self.max, imgui_sys::igSliderInt(self.label.as_ptr(), self.value, self.min, self.max,
self.display_format.as_ptr() self.display_format.as_ptr()
) )
} }
} }
} }
pub struct SliderFloat<'ui, 'p> { pub struct SliderFloat<'ui, 'p> {
label: ImStr<'p>, label: ImStr<'p>,
value: &'p mut f32, value: &'p mut f32,
min: f32, min: f32,
max: f32, max: f32,
display_format: ImStr<'p>, display_format: ImStr<'p>,
power: f32, power: f32,
_phantom: PhantomData<&'ui Ui<'ui>> _phantom: PhantomData<&'ui Ui<'ui>>
} }
impl<'ui, 'p> SliderFloat<'ui, 'p> { impl<'ui, 'p> SliderFloat<'ui, 'p> {
pub fn new(label: ImStr<'p>, value: &'p mut f32, min: f32, max: f32) -> Self { pub fn new(label: ImStr<'p>, value: &'p mut f32, min: f32, max: f32) -> Self {
SliderFloat { SliderFloat {
label: label, label: label,
value: value, value: value,
min: min, min: min,
max: max, max: max,
display_format: unsafe { ImStr::from_bytes(b"%.3f\0") }, display_format: unsafe { ImStr::from_bytes(b"%.3f\0") },
power: 1.0, power: 1.0,
_phantom: PhantomData _phantom: PhantomData
} }
} }
#[inline] #[inline]
pub fn display_format(self, display_format: ImStr<'p>) -> Self { pub fn display_format(self, display_format: ImStr<'p>) -> Self {
SliderFloat { SliderFloat {
display_format: display_format, display_format: display_format,
.. self .. self
} }
} }
#[inline] #[inline]
pub fn power(self, power: f32) -> Self { pub fn power(self, power: f32) -> Self {
SliderFloat { SliderFloat {
power: power, power: power,
.. self .. self
} }
} }
pub fn build(self) -> bool { pub fn build(self) -> bool {
unsafe { unsafe {
imgui_sys::igSliderFloat(self.label.as_ptr(), self.value, self.min, self.max, imgui_sys::igSliderFloat(self.label.as_ptr(), self.value, self.min, self.max,
self.display_format.as_ptr(), self.display_format.as_ptr(),
self.power self.power
) )
} }
} }
} }

View File

@ -5,52 +5,52 @@ use std::ptr;
use super::{Ui, ImStr}; use super::{Ui, ImStr};
pub struct CollapsingHeader<'ui, 'p> { pub struct CollapsingHeader<'ui, 'p> {
label: ImStr<'p>, label: ImStr<'p>,
str_id: Option<ImStr<'p>>, str_id: Option<ImStr<'p>>,
display_frame: bool, display_frame: bool,
default_open: bool, default_open: bool,
_phantom: PhantomData<&'ui Ui<'ui>> _phantom: PhantomData<&'ui Ui<'ui>>
} }
impl<'ui, 'p> CollapsingHeader<'ui, 'p> { impl<'ui, 'p> CollapsingHeader<'ui, 'p> {
pub fn new(label: ImStr<'p>) -> Self { pub fn new(label: ImStr<'p>) -> Self {
CollapsingHeader { CollapsingHeader {
label: label, label: label,
str_id: None, str_id: None,
display_frame: true, display_frame: true,
default_open: false, default_open: false,
_phantom: PhantomData _phantom: PhantomData
} }
} }
#[inline] #[inline]
pub fn str_id(self, str_id: ImStr<'p>) -> Self { pub fn str_id(self, str_id: ImStr<'p>) -> Self {
CollapsingHeader { CollapsingHeader {
str_id: Some(str_id), str_id: Some(str_id),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn display_frame(self, display_frame: bool) -> Self { pub fn display_frame(self, display_frame: bool) -> Self {
CollapsingHeader { CollapsingHeader {
display_frame: display_frame, display_frame: display_frame,
.. self .. self
} }
} }
#[inline] #[inline]
pub fn default_open(self, default_open: bool) -> Self { pub fn default_open(self, default_open: bool) -> Self {
CollapsingHeader { CollapsingHeader {
default_open: default_open, default_open: default_open,
.. self .. self
} }
} }
pub fn build(self) -> bool { pub fn build(self) -> bool {
unsafe { unsafe {
imgui_sys::igCollapsingHeader( imgui_sys::igCollapsingHeader(
self.label.as_ptr(), self.label.as_ptr(),
self.str_id.map(|x| x.as_ptr()).unwrap_or(ptr::null()), self.str_id.map(|x| x.as_ptr()).unwrap_or(ptr::null()),
self.display_frame, self.display_frame,
self.default_open self.default_open
) )
} }
} }
} }

View File

@ -3,178 +3,178 @@ use std::marker::PhantomData;
use std::ptr; use std::ptr;
use super::{ use super::{
Ui, Ui,
ImGuiSetCond, ImGuiSetCond,
ImGuiWindowFlags, ImGuiWindowFlags,
ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove, ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove,
ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse,
ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders, ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders,
ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar, ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar,
ImStr, ImVec2 ImStr, ImVec2
}; };
pub struct Window<'ui, 'p> { pub struct Window<'ui, 'p> {
pos: (f32, f32), pos: (f32, f32),
pos_cond: ImGuiSetCond, pos_cond: ImGuiSetCond,
size: (f32, f32), size: (f32, f32),
size_cond: ImGuiSetCond, size_cond: ImGuiSetCond,
name: ImStr<'p>, name: ImStr<'p>,
opened: Option<&'p mut bool>, opened: Option<&'p mut bool>,
bg_alpha: f32, bg_alpha: f32,
flags: ImGuiWindowFlags, flags: ImGuiWindowFlags,
_phantom: PhantomData<&'ui Ui<'ui>> _phantom: PhantomData<&'ui Ui<'ui>>
} }
impl<'ui, 'p> Window<'ui, 'p> { impl<'ui, 'p> Window<'ui, 'p> {
pub fn new() -> Window<'ui, 'p> { pub fn new() -> Window<'ui, 'p> {
Window { Window {
pos: (0.0, 0.0), pos: (0.0, 0.0),
pos_cond: ImGuiSetCond::empty(), pos_cond: ImGuiSetCond::empty(),
size: (0.0, 0.0), size: (0.0, 0.0),
size_cond: ImGuiSetCond::empty(), size_cond: ImGuiSetCond::empty(),
name: unsafe { ImStr::from_bytes(b"Debug\0") }, name: unsafe { ImStr::from_bytes(b"Debug\0") },
opened: None, opened: None,
bg_alpha: -1.0, bg_alpha: -1.0,
flags: ImGuiWindowFlags::empty(), flags: ImGuiWindowFlags::empty(),
_phantom: PhantomData _phantom: PhantomData
} }
} }
#[inline] #[inline]
pub fn position(self, pos: (f32, f32), cond: ImGuiSetCond) -> Self { pub fn position(self, pos: (f32, f32), cond: ImGuiSetCond) -> Self {
Window { Window {
pos: pos, pos: pos,
pos_cond: cond, pos_cond: cond,
.. self .. self
} }
} }
#[inline] #[inline]
pub fn size(self, size: (f32, f32), cond: ImGuiSetCond) -> Self { pub fn size(self, size: (f32, f32), cond: ImGuiSetCond) -> Self {
Window { Window {
size: size, size: size,
size_cond: cond, size_cond: cond,
.. self .. self
} }
} }
#[inline] #[inline]
pub fn name(self, name: ImStr<'p>) -> Self { pub fn name(self, name: ImStr<'p>) -> Self {
Window { Window {
name: name, name: name,
.. self .. self
} }
} }
#[inline] #[inline]
pub fn opened(self, opened: &'p mut bool) -> Self { pub fn opened(self, opened: &'p mut bool) -> Self {
Window { Window {
opened: Some(opened), opened: Some(opened),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn bg_alpha(self, bg_alpha: f32) -> Self { pub fn bg_alpha(self, bg_alpha: f32) -> Self {
Window { Window {
bg_alpha: bg_alpha, bg_alpha: bg_alpha,
.. self .. self
} }
} }
pub fn flags(self, flags: ImGuiWindowFlags) -> Self { pub fn flags(self, flags: ImGuiWindowFlags) -> Self {
Window { Window {
flags: flags, flags: flags,
.. self .. self
} }
} }
#[inline] #[inline]
pub fn title_bar(self, value: bool) -> Self { pub fn title_bar(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoTitleBar, !value), flags: self.flags.with(ImGuiWindowFlags_NoTitleBar, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn resizable(self, value: bool) -> Self { pub fn resizable(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoResize, !value), flags: self.flags.with(ImGuiWindowFlags_NoResize, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn movable(self, value: bool) -> Self { pub fn movable(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoMove, !value), flags: self.flags.with(ImGuiWindowFlags_NoMove, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn scroll_bar(self, value: bool) -> Self { pub fn scroll_bar(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoScrollbar, !value), flags: self.flags.with(ImGuiWindowFlags_NoScrollbar, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn scrollable(self, value: bool) -> Self { pub fn scrollable(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoScrollWithMouse, !value), flags: self.flags.with(ImGuiWindowFlags_NoScrollWithMouse, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn collapsible(self, value: bool) -> Self { pub fn collapsible(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoCollapse, !value), flags: self.flags.with(ImGuiWindowFlags_NoCollapse, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn always_auto_resize(self, value: bool) -> Self { pub fn always_auto_resize(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_AlwaysAutoResize, value), flags: self.flags.with(ImGuiWindowFlags_AlwaysAutoResize, value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn show_borders(self, value: bool) -> Self { pub fn show_borders(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_ShowBorders, value), flags: self.flags.with(ImGuiWindowFlags_ShowBorders, value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn save_settings(self, value: bool) -> Self { pub fn save_settings(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoSavedSettings, !value), flags: self.flags.with(ImGuiWindowFlags_NoSavedSettings, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn inputs(self, value: bool) -> Self { pub fn inputs(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_NoInputs, !value), flags: self.flags.with(ImGuiWindowFlags_NoInputs, !value),
.. self .. self
} }
} }
#[inline] #[inline]
pub fn menu_bar(self, value: bool) -> Self { pub fn menu_bar(self, value: bool) -> Self {
Window { Window {
flags: self.flags.with(ImGuiWindowFlags_MenuBar, value), flags: self.flags.with(ImGuiWindowFlags_MenuBar, value),
.. self .. self
} }
} }
pub fn build<F: FnOnce()>(self, f: F) { pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe { let render = unsafe {
if !self.pos_cond.is_empty() { if !self.pos_cond.is_empty() {
imgui_sys::igSetNextWindowPos(ImVec2::new(self.pos.0, self.pos.1), self.pos_cond); imgui_sys::igSetNextWindowPos(ImVec2::new(self.pos.0, self.pos.1), self.pos_cond);
} }
if !self.size_cond.is_empty() { if !self.size_cond.is_empty() {
imgui_sys::igSetNextWindowSize(ImVec2::new(self.size.0, self.size.1), self.size_cond); imgui_sys::igSetNextWindowSize(ImVec2::new(self.size.0, self.size.1), self.size_cond);
} }
imgui_sys::igBegin2(self.name.as_ptr(), imgui_sys::igBegin2(self.name.as_ptr(),
self.opened.map(|x| x as *mut bool).unwrap_or(ptr::null_mut()), self.opened.map(|x| x as *mut bool).unwrap_or(ptr::null_mut()),
ImVec2::new(0.0, 0.0), self.bg_alpha, self.flags ImVec2::new(0.0, 0.0), self.bg_alpha, self.flags
) )
}; };
if render { if render {
f(); f();
} }
unsafe { imgui_sys::igEnd() }; unsafe { imgui_sys::igEnd() };
} }
} }