From fa5bd75bb3d78fb1e8a2a9dde39d4dc736c7a753 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Tue, 25 Aug 2015 21:23:23 +0100 Subject: [PATCH] Fix formatting I don't know where that 3 spaces nonsense came from... :/ --- examples/test_window.rs | 2 +- imgui-sys/src/lib.rs | 195 +++++++++-------- src/lib.rs | 462 ++++++++++++++++++++-------------------- src/menus.rs | 136 ++++++------ src/sliders.rs | 134 ++++++------ src/widgets.rs | 90 ++++---- src/window.rs | 332 ++++++++++++++--------------- 7 files changed, 674 insertions(+), 677 deletions(-) diff --git a/examples/test_window.rs b/examples/test_window.rs index 071024a..d185dc0 100644 --- a/examples/test_window.rs +++ b/examples/test_window.rs @@ -13,7 +13,7 @@ const CLEAR_COLOR: (f32, f32, f32, f32) = (1.0, 1.0, 1.0, 1.0); fn main() { let mut support = Support::init(); - loop { + loop { let mut open = true; let active = support.render(CLEAR_COLOR, |ui| { ui.show_test_window(&mut open) diff --git a/imgui-sys/src/lib.rs b/imgui-sys/src/lib.rs index be5f731..dbedfa7 100644 --- a/imgui-sys/src/lib.rs +++ b/imgui-sys/src/lib.rs @@ -112,15 +112,15 @@ pub enum ImGuiKey { pub const ImGuiKey_COUNT: usize = 19; bitflags!( - #[repr(C)] - flags ImGuiAlign: c_int { - const ImGuiAlign_Left = 1 << 0, - const ImGuiAlign_Center = 1 << 1, - const ImGuiAlign_Right = 1 << 2, - const ImGuiAlign_Top = 1 << 3, - const ImGuiAlign_VCenter = 1 << 4, - const ImGuiAlign_Default = ImGuiAlign_Left.bits | ImGuiAlign_Top.bits - } + #[repr(C)] + flags ImGuiAlign: c_int { + const ImGuiAlign_Left = 1 << 0, + const ImGuiAlign_Center = 1 << 1, + const ImGuiAlign_Right = 1 << 2, + const ImGuiAlign_Top = 1 << 3, + const ImGuiAlign_VCenter = 1 << 4, + const ImGuiAlign_Default = ImGuiAlign_Left.bits | ImGuiAlign_Top.bits + } ); #[repr(C)] @@ -148,80 +148,80 @@ pub enum ImGuiMouseCursor { pub const ImGuiMouseCursor_COUNT: usize = 7; bitflags!( - #[repr(C)] - flags ImGuiWindowFlags: c_int { - const ImGuiWindowFlags_NoTitleBar = 1 << 0, - const ImGuiWindowFlags_NoResize = 1 << 1, - const ImGuiWindowFlags_NoMove = 1 << 2, - const ImGuiWindowFlags_NoScrollbar = 1 << 3, - const ImGuiWindowFlags_NoScrollWithMouse = 1 << 4, - const ImGuiWindowFlags_NoCollapse = 1 << 5, - const ImGuiWindowFlags_AlwaysAutoResize = 1 << 6, - const ImGuiWindowFlags_ShowBorders = 1 << 7, - const ImGuiWindowFlags_NoSavedSettings = 1 << 8, - const ImGuiWindowFlags_NoInputs = 1 << 9, - const ImGuiWindowFlags_MenuBar = 1 << 10, + #[repr(C)] + flags ImGuiWindowFlags: c_int { + const ImGuiWindowFlags_NoTitleBar = 1 << 0, + const ImGuiWindowFlags_NoResize = 1 << 1, + const ImGuiWindowFlags_NoMove = 1 << 2, + const ImGuiWindowFlags_NoScrollbar = 1 << 3, + const ImGuiWindowFlags_NoScrollWithMouse = 1 << 4, + const ImGuiWindowFlags_NoCollapse = 1 << 5, + const ImGuiWindowFlags_AlwaysAutoResize = 1 << 6, + const ImGuiWindowFlags_ShowBorders = 1 << 7, + const ImGuiWindowFlags_NoSavedSettings = 1 << 8, + const ImGuiWindowFlags_NoInputs = 1 << 9, + const ImGuiWindowFlags_MenuBar = 1 << 10, - const ImGuiWindowFlags_ChildWindow = 1 << 20, - const ImGuiWindowFlags_ChildWindowAutoFitX = 1 << 21, - const ImGuiWindowFlags_ChildWindowAutoFitY = 1 << 22, - const ImGuiWindowFlags_ComboBox = 1 << 23, - const ImGuiWindowFlags_Tooltip = 1 << 24, - const ImGuiWindowFlags_Popup = 1 << 25, - const ImGuiWindowFlags_Modal = 1 << 26, - const ImGuiWindowFlags_ChildMenu = 1 << 27 - } + const ImGuiWindowFlags_ChildWindow = 1 << 20, + const ImGuiWindowFlags_ChildWindowAutoFitX = 1 << 21, + const ImGuiWindowFlags_ChildWindowAutoFitY = 1 << 22, + const ImGuiWindowFlags_ComboBox = 1 << 23, + const ImGuiWindowFlags_Tooltip = 1 << 24, + const ImGuiWindowFlags_Popup = 1 << 25, + const ImGuiWindowFlags_Modal = 1 << 26, + const ImGuiWindowFlags_ChildMenu = 1 << 27 + } ); impl ImGuiWindowFlags { - #[inline] - pub fn with(self, mask: ImGuiWindowFlags, value: bool) -> ImGuiWindowFlags { - if value { self | mask } else { self - mask } - } + #[inline] + pub fn with(self, mask: ImGuiWindowFlags, value: bool) -> ImGuiWindowFlags { + if value { self | mask } else { self - mask } + } } bitflags!( - #[repr(C)] - flags ImGuiSetCond: c_int { - const ImGuiSetCond_Always = 1 << 0, - const ImGuiSetCond_Once = 1 << 1, - const ImGuiSetCond_FirstUseEver = 1 << 2, - const ImGuiSetCond_Appearing = 1 << 3 - } + #[repr(C)] + flags ImGuiSetCond: c_int { + const ImGuiSetCond_Always = 1 << 0, + const ImGuiSetCond_Once = 1 << 1, + const ImGuiSetCond_FirstUseEver = 1 << 2, + const ImGuiSetCond_Appearing = 1 << 3 + } ); bitflags!( - #[repr(C)] - flags ImGuiInputTextFlags: c_int { - const ImGuiInputTextFlags_CharsDecimal = 1 << 0, - const ImGuiInputTextFlags_CharsHexadecimal = 1 << 1, - const ImGuiInputTextFlags_CharsUppercase = 1 << 2, - const ImGuiInputTextFlags_CharsNoBlank = 1 << 3, - const ImGuiInputTextFlags_AutoSelectAll = 1 << 4, - const ImGuiInputTextFlags_EnterReturnsTrue = 1 << 5, - const ImGuiInputTextFlags_CallbackCompletion = 1 << 6, - const ImGuiInputTextFlags_CallbackHistory = 1 << 7, - const ImGuiInputTextFlags_CallbackAlways = 1 << 8, - const ImGuiInputTextFlags_CallbackCharFilter = 1 << 9, - const ImGuiInputTextFlags_AllowTabInput = 1 << 10, - const ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11, - const ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, - const ImGuiInputTextFlags_AlwaysInsertMode = 1 << 13, + #[repr(C)] + flags ImGuiInputTextFlags: c_int { + const ImGuiInputTextFlags_CharsDecimal = 1 << 0, + const ImGuiInputTextFlags_CharsHexadecimal = 1 << 1, + const ImGuiInputTextFlags_CharsUppercase = 1 << 2, + const ImGuiInputTextFlags_CharsNoBlank = 1 << 3, + const ImGuiInputTextFlags_AutoSelectAll = 1 << 4, + const ImGuiInputTextFlags_EnterReturnsTrue = 1 << 5, + const ImGuiInputTextFlags_CallbackCompletion = 1 << 6, + const ImGuiInputTextFlags_CallbackHistory = 1 << 7, + const ImGuiInputTextFlags_CallbackAlways = 1 << 8, + const ImGuiInputTextFlags_CallbackCharFilter = 1 << 9, + const ImGuiInputTextFlags_AllowTabInput = 1 << 10, + const ImGuiInputTextFlags_CtrlEnterForNewLine = 1 << 11, + const ImGuiInputTextFlags_NoHorizontalScroll = 1 << 12, + const ImGuiInputTextFlags_AlwaysInsertMode = 1 << 13, - const ImGuiInputTextFlags_Multiline = 1 << 20, - } + const ImGuiInputTextFlags_Multiline = 1 << 20, + } ); bitflags!( - #[repr(C)] - flags ImGuiSelectableFlags: c_int { - const ImGuiSelectableFlags_DontClosePopups = 1 << 0, - const ImGuiSelectableFlags_SpanAllColumns = 1 << 1 - } + #[repr(C)] + flags ImGuiSelectableFlags: c_int { + const ImGuiSelectableFlags_DontClosePopups = 1 << 0, + const ImGuiSelectableFlags_SpanAllColumns = 1 << 1 + } ); pub type ImGuiTextEditCallback = - Option c_int>; +Option c_int>; #[repr(C)] #[derive(Copy, Clone, Debug, Default)] @@ -231,12 +231,12 @@ pub struct ImVec2 { } impl ImVec2 { - pub fn new(x: f32, y: f32) -> ImVec2 { - ImVec2 { - x: x as c_float, - y: y as c_float - } - } + pub fn new(x: f32, y: f32) -> ImVec2 { + ImVec2 { + x: x as c_float, + y: y as c_float + } + } } #[cfg(feature = "glium")] @@ -254,14 +254,14 @@ pub struct ImVec4 { } impl ImVec4 { - pub fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 { - ImVec4 { - x: x as c_float, - y: y as c_float, - z: z as c_float, - w: w as c_float - } - } + pub fn new(x: f32, y: f32, z: f32, w: f32) -> ImVec4 { + ImVec4 { + x: x as c_float, + y: y as c_float, + z: z as c_float, + w: w as c_float + } + } } #[cfg(feature = "glium")] @@ -368,9 +368,9 @@ pub struct ImVector { } impl ImVector { - pub unsafe fn as_slice(&self) -> &[T] { - slice::from_raw_parts(self.data, self.size as usize) - } + pub unsafe fn as_slice(&self) -> &[T] { + slice::from_raw_parts(self.data, self.size as usize) + } } #[repr(C)] @@ -500,10 +500,10 @@ pub struct ImDrawData { } impl ImDrawData { - pub unsafe fn cmd_lists(&self) -> &[*const ImDrawList] { - let cmd_lists: *const *const ImDrawList = mem::transmute(self.cmd_lists); - slice::from_raw_parts(cmd_lists, self.cmd_lists_count as usize) - } + pub unsafe fn cmd_lists(&self) -> &[*const ImDrawList] { + let cmd_lists: *const *const ImDrawList = mem::transmute(self.cmd_lists); + slice::from_raw_parts(cmd_lists, self.cmd_lists_count as usize) + } } #[repr(C)] @@ -735,8 +735,7 @@ extern "C" { 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, items_getter: extern "C" fn(data: *mut c_void, - idx: c_int, - out_text: *mut *const c_char) -> bool, + idx: c_int, out_text: *mut *const c_char) -> bool, data: *mut c_void, items_count: c_int, height_in_items: c_int) -> 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, graph_size: ImVec2, stride: c_int); pub fn igPlotLines2(label: *const c_char, - values_getter: extern "C" fn(data: *mut c_void, - idx: c_int) -> c_float, + values_getter: extern "C" fn(data: *mut c_void, idx: c_int) -> c_float, data: *mut c_void, values_count: c_int, values_offset: c_int, overlay_text: *const c_char, @@ -761,8 +759,7 @@ extern "C" { scale_min: c_float, scale_max: c_float, graph_size: ImVec2, stride: c_int); pub fn igPlotHistogram2(label: *const c_char, - values_getter: extern "C" fn(data: *mut c_void, - idx: c_int) -> c_float, + values_getter: extern "C" fn(data: *mut c_void, idx: c_int) -> c_float, data: *mut c_void, values_count: c_int, values_offset: c_int, overlay_text: *const c_char, @@ -845,7 +842,7 @@ extern "C" { // Widgets: Input 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, callback: ImGuiTextEditCallback, user_data: *mut c_void) -> bool; pub fn igInputTextMultiline(label: *const c_char, @@ -1054,11 +1051,11 @@ extern "C" { font_cfg: *const ImFontConfig, glyph_ranges: *const ImWchar) -> *mut ImFont; pub fn ImFontAtlas_AddFontFromMemoryCompressedBase85TTF( - atlas: *mut ImFontAtlas, - compressed_ttf_data_base85: *const c_char, - size_pixels: c_float, - font_cfg: *const ImFontConfig, - glyph_ranges: *const ImWchar) -> *mut ImFont; + atlas: *mut ImFontAtlas, + compressed_ttf_data_base85: *const c_char, + size_pixels: c_float, + font_cfg: *const ImFontConfig, + glyph_ranges: *const ImWchar) -> *mut ImFont; pub fn ImFontAtlas_ClearTexData(atlas: *mut ImFontAtlas); pub fn ImFontAtlas_Clear(atlas: *mut ImFontAtlas); pub fn ImGuiIO_AddInputCharacter(c: c_ushort); diff --git a/src/lib.rs b/src/lib.rs index 283cdb5..c4245ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,16 +19,16 @@ use std::slice; use std::str; pub use imgui_sys::{ - ImDrawIdx, ImDrawVert, - ImGuiSetCond, - ImGuiSetCond_Always, ImGuiSetCond_Once, - ImGuiSetCond_FirstUseEver, ImGuiSetCond_Appearing, - ImGuiWindowFlags, - ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove, - ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse, - ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders, - ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar, - ImVec2, ImVec4 + ImDrawIdx, ImDrawVert, + ImGuiSetCond, + ImGuiSetCond_Always, ImGuiSetCond_Once, + ImGuiSetCond_FirstUseEver, ImGuiSetCond_Appearing, + ImGuiWindowFlags, + ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove, + ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse, + ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders, + ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar, + ImVec2, ImVec4 }; pub use menus::{Menu, MenuItem}; pub use sliders::{SliderFloat, SliderInt}; @@ -47,144 +47,144 @@ pub struct ImGui; #[macro_export] macro_rules! im_str { - ($e:tt) => ({ - let value = concat!($e, "\0"); - unsafe { ::imgui::ImStr::from_bytes(value.as_bytes()) } - }); - ($e:tt, $($arg:tt)*) => ({ - ::imgui::ImStr::from_fmt(format_args!($e, $($arg)*)) - }) + ($e:tt) => ({ + let value = concat!($e, "\0"); + unsafe { ::imgui::ImStr::from_bytes(value.as_bytes()) } + }); + ($e:tt, $($arg:tt)*) => ({ + ::imgui::ImStr::from_fmt(format_args!($e, $($arg)*)) + }) } pub struct ImStr<'a> { - bytes: Cow<'a, [u8]> + bytes: Cow<'a, [u8]> } impl<'a> ImStr<'a> { - pub unsafe fn from_bytes(bytes: &'a [u8]) -> ImStr<'a> { - ImStr { - bytes: Cow::Borrowed(bytes) - } - } - pub fn from_str(value: &str) -> ImStr<'a> { - let mut bytes: Vec = value.bytes().collect(); - bytes.push(0); - ImStr { - bytes: Cow::Owned(bytes) - } - } - pub fn from_fmt(args: fmt::Arguments) -> ImStr<'a> { - let mut bytes = fmt::format(args).into_bytes(); - bytes.push(0); - ImStr { - bytes: Cow::Owned(bytes) - } - } - fn as_ptr(&self) -> *const c_char { self.bytes.as_ptr() as *const c_char } + pub unsafe fn from_bytes(bytes: &'a [u8]) -> ImStr<'a> { + ImStr { + bytes: Cow::Borrowed(bytes) + } + } + pub fn from_str(value: &str) -> ImStr<'a> { + let mut bytes: Vec = value.bytes().collect(); + bytes.push(0); + ImStr { + bytes: Cow::Owned(bytes) + } + } + pub fn from_fmt(args: fmt::Arguments) -> ImStr<'a> { + let mut bytes = fmt::format(args).into_bytes(); + bytes.push(0); + ImStr { + bytes: Cow::Owned(bytes) + } + } + fn as_ptr(&self) -> *const c_char { self.bytes.as_ptr() as *const c_char } } pub struct TextureHandle<'a> { - pub width: u32, - pub height: u32, - pub pixels: &'a [c_uchar] + pub width: u32, + pub height: u32, + pub pixels: &'a [c_uchar] } pub fn get_version() -> &'static str { - unsafe { - let bytes = CStr::from_ptr(imgui_sys::igGetVersion()).to_bytes(); - str::from_utf8_unchecked(bytes) - } + unsafe { + let bytes = CStr::from_ptr(imgui_sys::igGetVersion()).to_bytes(); + str::from_utf8_unchecked(bytes) + } } impl ImGui { - pub fn init() -> ImGui { - let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; - io.render_draw_lists_fn = Some(render_draw_lists); + pub fn init() -> ImGui { + let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; + io.render_draw_lists_fn = Some(render_draw_lists); - ImGui - } - 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 mut pixels: *mut c_uchar = ptr::null_mut(); - let mut width: c_int = 0; - let mut height: c_int = 0; - let mut bytes_per_pixel: c_int = 0; - unsafe { - imgui_sys::ImFontAtlas_GetTexDataAsRGBA32(io.fonts, &mut pixels, &mut width, &mut height, &mut bytes_per_pixel); - f(TextureHandle { - width: width as u32, - height: height as u32, - pixels: slice::from_raw_parts(pixels, (width * height * bytes_per_pixel) as usize) - }) - } - } - pub fn draw_mouse_cursor(&mut self, value: bool) { - let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; - io.mouse_draw_cursor = value; - } - pub fn mouse_pos(&self) -> (f32, f32) { - let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; - (io.mouse_pos.x, io.mouse_pos.y) - } - pub fn set_mouse_pos(&mut self, x: f32, y: f32) { - let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; - io.mouse_pos.x = x; - io.mouse_pos.y = y; - } - pub fn set_mouse_down(&mut self, states: &[bool; 5]) { - let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; - io.mouse_down = *states; - } - pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } } - 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> { - unsafe { - let io: &mut imgui_sys::ImGuiIO = mem::transmute(imgui_sys::igGetIO()); - io.display_size.x = width as c_float; - io.display_size.y = height as c_float; - io.delta_time = delta_time; + ImGui + } + 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 mut pixels: *mut c_uchar = ptr::null_mut(); + let mut width: c_int = 0; + let mut height: c_int = 0; + let mut bytes_per_pixel: c_int = 0; + unsafe { + imgui_sys::ImFontAtlas_GetTexDataAsRGBA32(io.fonts, &mut pixels, &mut width, &mut height, &mut bytes_per_pixel); + f(TextureHandle { + width: width as u32, + height: height as u32, + pixels: slice::from_raw_parts(pixels, (width * height * bytes_per_pixel) as usize) + }) + } + } + pub fn draw_mouse_cursor(&mut self, value: bool) { + let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; + io.mouse_draw_cursor = value; + } + pub fn mouse_pos(&self) -> (f32, f32) { + let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; + (io.mouse_pos.x, io.mouse_pos.y) + } + pub fn set_mouse_pos(&mut self, x: f32, y: f32) { + let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; + io.mouse_pos.x = x; + io.mouse_pos.y = y; + } + pub fn set_mouse_down(&mut self, states: &[bool; 5]) { + let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; + io.mouse_down = *states; + } + pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } } + 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> { + unsafe { + let io: &mut imgui_sys::ImGuiIO = mem::transmute(imgui_sys::igGetIO()); + io.display_size.x = width as c_float; + io.display_size.y = height as c_float; + io.delta_time = delta_time; - imgui_sys::igNewFrame(); - } - Ui { - imgui: self - } - } + imgui_sys::igNewFrame(); + } + Ui { + imgui: self + } + } } impl Drop for ImGui { - fn drop(&mut self) { - unsafe { - imgui_sys::igShutdown(); - } - } + fn drop(&mut self) { + unsafe { + imgui_sys::igShutdown(); + } + } } #[cfg(feature = "sdl2")] impl ImGui { - pub fn update_mouse(&mut self, mouse: &::sdl2::mouse::MouseUtil) { - let (mouse_state, mouse_x, mouse_y) = mouse.get_mouse_state(); - let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; - io.mouse_pos.x = mouse_x as f32; - io.mouse_pos.y = mouse_y as f32; - io.mouse_down = [ - mouse_state.left(), - mouse_state.right(), - mouse_state.middle(), - mouse_state.x1(), - mouse_state.x2() - ]; - } + pub fn update_mouse(&mut self, mouse: &::sdl2::mouse::MouseUtil) { + let (mouse_state, mouse_x, mouse_y) = mouse.get_mouse_state(); + let io: &mut imgui_sys::ImGuiIO = unsafe { mem::transmute(imgui_sys::igGetIO()) }; + io.mouse_pos.x = mouse_x as f32; + io.mouse_pos.y = mouse_y as f32; + io.mouse_down = [ + mouse_state.left(), + mouse_state.right(), + mouse_state.middle(), + mouse_state.x1(), + mouse_state.x2() + ]; + } } pub struct DrawList<'a> { - pub cmd_buffer: &'a [imgui_sys::ImDrawCmd], - pub idx_buffer: &'a [imgui_sys::ImDrawIdx], - pub vtx_buffer: &'a [imgui_sys::ImDrawVert] + pub cmd_buffer: &'a [imgui_sys::ImDrawCmd], + pub idx_buffer: &'a [imgui_sys::ImDrawIdx], + pub vtx_buffer: &'a [imgui_sys::ImDrawVert] } pub struct Ui<'ui> { - imgui: &'ui ImGui + imgui: &'ui ImGui } 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 } impl<'ui> Ui<'ui> { - pub fn imgui(&self) -> &ImGui { self.imgui } - pub fn render(self, mut f: F) -> Result<(), E> - where F: FnMut(DrawList<'ui>) -> Result<(), E> { - unsafe { - let mut im_draw_data = mem::zeroed(); - RENDER_DRAW_LISTS_STATE.0 = &mut im_draw_data; - imgui_sys::igRender(); - RENDER_DRAW_LISTS_STATE.0 = 0 as *mut imgui_sys::ImDrawData; + pub fn imgui(&self) -> &ImGui { self.imgui } + pub fn render(self, mut f: F) -> Result<(), E> + where F: FnMut(DrawList<'ui>) -> Result<(), E> { + unsafe { + let mut im_draw_data = mem::zeroed(); + RENDER_DRAW_LISTS_STATE.0 = &mut im_draw_data; + imgui_sys::igRender(); + RENDER_DRAW_LISTS_STATE.0 = 0 as *mut imgui_sys::ImDrawData; - for &cmd_list in im_draw_data.cmd_lists() { - let draw_list = - DrawList { - cmd_buffer: (*cmd_list).cmd_buffer.as_slice(), - idx_buffer: (*cmd_list).idx_buffer.as_slice(), - vtx_buffer: (*cmd_list).vtx_buffer.as_slice() - }; - try!(f(draw_list)); - } - } - Ok(()) - } - pub fn show_user_guide(&self) { unsafe { imgui_sys::igShowUserGuide() }; } - pub fn show_test_window(&self, opened: &mut bool) { - unsafe { - imgui_sys::igShowTestWindow(opened); - } - } - pub fn show_metrics_window(&self, opened: &mut bool) { - unsafe { - imgui_sys::igShowMetricsWindow(opened); - } - } + for &cmd_list in im_draw_data.cmd_lists() { + let draw_list = + DrawList { + cmd_buffer: (*cmd_list).cmd_buffer.as_slice(), + idx_buffer: (*cmd_list).idx_buffer.as_slice(), + vtx_buffer: (*cmd_list).vtx_buffer.as_slice() + }; + try!(f(draw_list)); + } + } + Ok(()) + } + pub fn show_user_guide(&self) { unsafe { imgui_sys::igShowUserGuide() }; } + pub fn show_test_window(&self, opened: &mut bool) { + unsafe { + imgui_sys::igShowTestWindow(opened); + } + } + pub fn show_metrics_window(&self, opened: &mut bool) { + unsafe { + imgui_sys::igShowMetricsWindow(opened); + } + } } // Window 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 impl<'ui> Ui<'ui> { - pub fn separator(&self) { unsafe { imgui_sys::igSeparator() }; } - pub fn same_line(&self, pos_x: f32) { - unsafe { - imgui_sys::igSameLine(pos_x, -1.0f32) - } - } - pub fn same_line_spacing(&self, pos_x: f32, spacing_w: f32) { - unsafe { - imgui_sys::igSameLine(pos_x, spacing_w) - } - } - pub fn spacing(&self) { unsafe { imgui_sys::igSpacing() }; } + pub fn separator(&self) { unsafe { imgui_sys::igSeparator() }; } + pub fn same_line(&self, pos_x: f32) { + unsafe { + imgui_sys::igSameLine(pos_x, -1.0f32) + } + } + pub fn same_line_spacing(&self, pos_x: f32, spacing_w: f32) { + unsafe { + imgui_sys::igSameLine(pos_x, spacing_w) + } + } + pub fn spacing(&self) { unsafe { imgui_sys::igSpacing() }; } } // Widgets impl<'ui> Ui<'ui> { - pub fn text<'b>(&self, text: ImStr<'b>) { - // TODO: use igTextUnformatted - unsafe { - imgui_sys::igText(fmt_ptr(), text.as_ptr()); - } - } - pub fn text_colored<'b, A>(&self, col: A, text: ImStr<'b>) where A: Into { - unsafe { - imgui_sys::igTextColored(col.into(), fmt_ptr(), text.as_ptr()); - } - } - pub fn text_disabled<'b>(&self, text: ImStr<'b>) { - unsafe { - imgui_sys::igTextDisabled(fmt_ptr(), text.as_ptr()); - } - } - pub fn text_wrapped<'b>(&self, text: ImStr<'b>) { - unsafe { - imgui_sys::igTextWrapped(fmt_ptr(), text.as_ptr()); - } - } - pub fn label_text<'b>(&self, label: ImStr<'b>, text: ImStr<'b>) { - unsafe { - imgui_sys::igLabelText(label.as_ptr(), fmt_ptr(), text.as_ptr()); - } - } - pub fn bullet(&self) { - unsafe { - imgui_sys::igBullet(); - } - } - pub fn bullet_text<'b>(&self, text: ImStr<'b>) { - unsafe { - imgui_sys::igBulletText(fmt_ptr(), text.as_ptr()); - } - } - pub fn collapsing_header<'p>(&self, label: ImStr<'p>) -> CollapsingHeader<'ui, 'p> { - CollapsingHeader::new(label) - } - pub fn checkbox<'p>(&self, label: ImStr<'p>, value: &'p mut bool) -> bool { - unsafe { imgui_sys::igCheckbox(label.as_ptr(), value) } - } + pub fn text<'b>(&self, text: ImStr<'b>) { + // TODO: use igTextUnformatted + unsafe { + imgui_sys::igText(fmt_ptr(), text.as_ptr()); + } + } + pub fn text_colored<'b, A>(&self, col: A, text: ImStr<'b>) where A: Into { + unsafe { + imgui_sys::igTextColored(col.into(), fmt_ptr(), text.as_ptr()); + } + } + pub fn text_disabled<'b>(&self, text: ImStr<'b>) { + unsafe { + imgui_sys::igTextDisabled(fmt_ptr(), text.as_ptr()); + } + } + pub fn text_wrapped<'b>(&self, text: ImStr<'b>) { + unsafe { + imgui_sys::igTextWrapped(fmt_ptr(), text.as_ptr()); + } + } + pub fn label_text<'b>(&self, label: ImStr<'b>, text: ImStr<'b>) { + unsafe { + imgui_sys::igLabelText(label.as_ptr(), fmt_ptr(), text.as_ptr()); + } + } + pub fn bullet(&self) { + unsafe { + imgui_sys::igBullet(); + } + } + pub fn bullet_text<'b>(&self, text: ImStr<'b>) { + unsafe { + imgui_sys::igBulletText(fmt_ptr(), text.as_ptr()); + } + } + pub fn collapsing_header<'p>(&self, label: ImStr<'p>) -> CollapsingHeader<'ui, 'p> { + CollapsingHeader::new(label) + } + pub fn checkbox<'p>(&self, label: ImStr<'p>, value: &'p mut bool) -> bool { + unsafe { imgui_sys::igCheckbox(label.as_ptr(), value) } + } } // Widgets: Sliders impl<'ui> Ui<'ui> { - pub fn slider_f32<'p>(&self, label: ImStr<'p>, - value: &'p mut f32, min: f32, max: f32) -> SliderFloat<'ui, 'p> { - SliderFloat::new(label, value, min, max) - } - pub fn slider_i32<'p>(&self, label: ImStr<'p>, - value: &'p mut i32, min: i32, max: i32) -> SliderInt<'ui, 'p> { - SliderInt::new(label, value, min, max) - } + pub fn slider_f32<'p>(&self, label: ImStr<'p>, + value: &'p mut f32, min: f32, max: f32) -> SliderFloat<'ui, 'p> { + SliderFloat::new(label, value, min, max) + } + pub fn slider_i32<'p>(&self, label: ImStr<'p>, + value: &'p mut i32, min: i32, max: i32) -> SliderInt<'ui, 'p> { + SliderInt::new(label, value, min, max) + } } // Widgets: Menus impl<'ui> Ui<'ui> { - pub fn main_menu_bar(&self, f: F) where F: FnOnce() { - let render = unsafe { imgui_sys::igBeginMainMenuBar() }; - if render { - f(); - unsafe { imgui_sys::igEndMainMenuBar() }; - } - } - pub fn menu_bar(&self, f: F) where F: FnOnce() { - let render = unsafe { imgui_sys::igBeginMenuBar() }; - if render { - f(); - unsafe { imgui_sys::igEndMenuBar() }; - } - } - 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 main_menu_bar(&self, f: F) where F: FnOnce() { + let render = unsafe { imgui_sys::igBeginMainMenuBar() }; + if render { + f(); + unsafe { imgui_sys::igEndMainMenuBar() }; + } + } + pub fn menu_bar(&self, f: F) where F: FnOnce() { + let render = unsafe { imgui_sys::igBeginMenuBar() }; + if render { + f(); + unsafe { imgui_sys::igEndMenuBar() }; + } + } + 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) } } struct RenderDrawListsState(*mut imgui_sys::ImDrawData); @@ -332,7 +332,7 @@ static mut RENDER_DRAW_LISTS_STATE: RenderDrawListsState = RenderDrawListsState(0 as *mut imgui_sys::ImDrawData); extern "C" fn render_draw_lists(data: *mut imgui_sys::ImDrawData) { - unsafe { - ptr::copy_nonoverlapping(data, RENDER_DRAW_LISTS_STATE.0, 1); - } + unsafe { + ptr::copy_nonoverlapping(data, RENDER_DRAW_LISTS_STATE.0, 1); + } } diff --git a/src/menus.rs b/src/menus.rs index 35e32e1..88d3d93 100644 --- a/src/menus.rs +++ b/src/menus.rs @@ -5,81 +5,81 @@ use std::ptr; use super::{Ui, ImStr}; pub struct Menu<'ui, 'p> { - label: ImStr<'p>, - enabled: bool, - _phantom: PhantomData<&'ui Ui<'ui>> + label: ImStr<'p>, + enabled: bool, + _phantom: PhantomData<&'ui Ui<'ui>> } impl<'ui, 'p> Menu<'ui, 'p> { - pub fn new(label: ImStr<'p>) -> Self { - Menu { - label: label, - enabled: true, - _phantom: PhantomData - } - } - #[inline] - pub fn enabled(self, enabled: bool) -> Self { - Menu { - enabled: enabled, - .. self - } - } - pub fn build(self, f: F) { - let render = unsafe { imgui_sys::igBeginMenu(self.label.as_ptr(), self.enabled) }; - if render { - f(); - unsafe { imgui_sys::igEndMenu() }; - } - } + pub fn new(label: ImStr<'p>) -> Self { + Menu { + label: label, + enabled: true, + _phantom: PhantomData + } + } + #[inline] + pub fn enabled(self, enabled: bool) -> Self { + Menu { + enabled: enabled, + .. self + } + } + pub fn build(self, f: F) { + let render = unsafe { imgui_sys::igBeginMenu(self.label.as_ptr(), self.enabled) }; + if render { + f(); + unsafe { imgui_sys::igEndMenu() }; + } + } } pub struct MenuItem<'ui, 'p> { - label: ImStr<'p>, - shortcut: Option>, - selected: Option<&'p mut bool>, - enabled: bool, - _phantom: PhantomData<&'ui Ui<'ui>> + label: ImStr<'p>, + shortcut: Option>, + selected: Option<&'p mut bool>, + enabled: bool, + _phantom: PhantomData<&'ui Ui<'ui>> } impl<'ui, 'p> MenuItem<'ui, 'p> { - pub fn new(label: ImStr<'p>) -> Self { - MenuItem { - label: label, - shortcut: None, - selected: None, - enabled: true, - _phantom: PhantomData - } - } - #[inline] - pub fn shortcut(self, shortcut: ImStr<'p>) -> Self { - MenuItem { - shortcut: Some(shortcut), - .. self - } - } - #[inline] - pub fn selected(self, selected: &'p mut bool) -> Self { - MenuItem { - selected: Some(selected), - .. self - } - } - #[inline] - pub fn enabled(self, enabled: bool) -> Self { - MenuItem { - enabled: enabled, - .. self - } - } - pub fn build(self) -> bool { - let label = self.label.as_ptr(); - 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 enabled = self.enabled; - unsafe { - imgui_sys::igMenuItemPtr(label, shortcut, selected, enabled) - } - } + pub fn new(label: ImStr<'p>) -> Self { + MenuItem { + label: label, + shortcut: None, + selected: None, + enabled: true, + _phantom: PhantomData + } + } + #[inline] + pub fn shortcut(self, shortcut: ImStr<'p>) -> Self { + MenuItem { + shortcut: Some(shortcut), + .. self + } + } + #[inline] + pub fn selected(self, selected: &'p mut bool) -> Self { + MenuItem { + selected: Some(selected), + .. self + } + } + #[inline] + pub fn enabled(self, enabled: bool) -> Self { + MenuItem { + enabled: enabled, + .. self + } + } + pub fn build(self) -> bool { + let label = self.label.as_ptr(); + 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 enabled = self.enabled; + unsafe { + imgui_sys::igMenuItemPtr(label, shortcut, selected, enabled) + } + } } diff --git a/src/sliders.rs b/src/sliders.rs index add001e..cc361aa 100644 --- a/src/sliders.rs +++ b/src/sliders.rs @@ -6,84 +6,84 @@ use super::{Ui, ImStr}; // TODO: Consider using Range, even though it is half-open pub struct SliderInt<'ui, 'p> { - label: ImStr<'p>, - value: &'p mut i32, - min: i32, - max: i32, - display_format: ImStr<'p>, - _phantom: PhantomData<&'ui Ui<'ui>> + label: ImStr<'p>, + value: &'p mut i32, + min: i32, + max: i32, + display_format: ImStr<'p>, + _phantom: PhantomData<&'ui Ui<'ui>> } impl<'ui, 'p> SliderInt<'ui, 'p> { - pub fn new(label: ImStr<'p>, value: &'p mut i32, min: i32, max: i32) -> Self { - SliderInt { - label: label, - value: value, - min: min, - max: max, - display_format: unsafe { ImStr::from_bytes(b"%.0f\0") }, - _phantom: PhantomData - } - } - #[inline] - pub fn display_format(self, display_format: ImStr<'p>) -> Self { - SliderInt { - display_format: display_format, - .. self - } - } - pub fn build(self) -> bool { - unsafe { - imgui_sys::igSliderInt(self.label.as_ptr(), self.value, self.min, self.max, + pub fn new(label: ImStr<'p>, value: &'p mut i32, min: i32, max: i32) -> Self { + SliderInt { + label: label, + value: value, + min: min, + max: max, + display_format: unsafe { ImStr::from_bytes(b"%.0f\0") }, + _phantom: PhantomData + } + } + #[inline] + pub fn display_format(self, display_format: ImStr<'p>) -> Self { + SliderInt { + display_format: display_format, + .. self + } + } + pub fn build(self) -> bool { + unsafe { + imgui_sys::igSliderInt(self.label.as_ptr(), self.value, self.min, self.max, self.display_format.as_ptr() - ) - } - } + ) + } + } } pub struct SliderFloat<'ui, 'p> { - label: ImStr<'p>, - value: &'p mut f32, - min: f32, - max: f32, - display_format: ImStr<'p>, - power: f32, - _phantom: PhantomData<&'ui Ui<'ui>> + label: ImStr<'p>, + value: &'p mut f32, + min: f32, + max: f32, + display_format: ImStr<'p>, + power: f32, + _phantom: PhantomData<&'ui Ui<'ui>> } impl<'ui, 'p> SliderFloat<'ui, 'p> { - pub fn new(label: ImStr<'p>, value: &'p mut f32, min: f32, max: f32) -> Self { - SliderFloat { - label: label, - value: value, - min: min, - max: max, - display_format: unsafe { ImStr::from_bytes(b"%.3f\0") }, - power: 1.0, - _phantom: PhantomData - } - } - #[inline] - pub fn display_format(self, display_format: ImStr<'p>) -> Self { - SliderFloat { - display_format: display_format, - .. self - } - } - #[inline] - pub fn power(self, power: f32) -> Self { - SliderFloat { - power: power, - .. self - } - } - pub fn build(self) -> bool { - unsafe { - imgui_sys::igSliderFloat(self.label.as_ptr(), self.value, self.min, self.max, + pub fn new(label: ImStr<'p>, value: &'p mut f32, min: f32, max: f32) -> Self { + SliderFloat { + label: label, + value: value, + min: min, + max: max, + display_format: unsafe { ImStr::from_bytes(b"%.3f\0") }, + power: 1.0, + _phantom: PhantomData + } + } + #[inline] + pub fn display_format(self, display_format: ImStr<'p>) -> Self { + SliderFloat { + display_format: display_format, + .. self + } + } + #[inline] + pub fn power(self, power: f32) -> Self { + SliderFloat { + power: power, + .. self + } + } + pub fn build(self) -> bool { + unsafe { + imgui_sys::igSliderFloat(self.label.as_ptr(), self.value, self.min, self.max, self.display_format.as_ptr(), self.power - ) - } - } + ) + } + } } diff --git a/src/widgets.rs b/src/widgets.rs index 78a2f87..0ec8ed4 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -5,52 +5,52 @@ use std::ptr; use super::{Ui, ImStr}; pub struct CollapsingHeader<'ui, 'p> { - label: ImStr<'p>, - str_id: Option>, - display_frame: bool, - default_open: bool, - _phantom: PhantomData<&'ui Ui<'ui>> + label: ImStr<'p>, + str_id: Option>, + display_frame: bool, + default_open: bool, + _phantom: PhantomData<&'ui Ui<'ui>> } impl<'ui, 'p> CollapsingHeader<'ui, 'p> { - pub fn new(label: ImStr<'p>) -> Self { - CollapsingHeader { - label: label, - str_id: None, - display_frame: true, - default_open: false, - _phantom: PhantomData - } - } - #[inline] - pub fn str_id(self, str_id: ImStr<'p>) -> Self { - CollapsingHeader { - str_id: Some(str_id), - .. self - } - } - #[inline] - pub fn display_frame(self, display_frame: bool) -> Self { - CollapsingHeader { - display_frame: display_frame, - .. self - } - } - #[inline] - pub fn default_open(self, default_open: bool) -> Self { - CollapsingHeader { - default_open: default_open, - .. self - } - } - pub fn build(self) -> bool { - unsafe { - imgui_sys::igCollapsingHeader( - self.label.as_ptr(), - self.str_id.map(|x| x.as_ptr()).unwrap_or(ptr::null()), - self.display_frame, - self.default_open - ) - } - } + pub fn new(label: ImStr<'p>) -> Self { + CollapsingHeader { + label: label, + str_id: None, + display_frame: true, + default_open: false, + _phantom: PhantomData + } + } + #[inline] + pub fn str_id(self, str_id: ImStr<'p>) -> Self { + CollapsingHeader { + str_id: Some(str_id), + .. self + } + } + #[inline] + pub fn display_frame(self, display_frame: bool) -> Self { + CollapsingHeader { + display_frame: display_frame, + .. self + } + } + #[inline] + pub fn default_open(self, default_open: bool) -> Self { + CollapsingHeader { + default_open: default_open, + .. self + } + } + pub fn build(self) -> bool { + unsafe { + imgui_sys::igCollapsingHeader( + self.label.as_ptr(), + self.str_id.map(|x| x.as_ptr()).unwrap_or(ptr::null()), + self.display_frame, + self.default_open + ) + } + } } diff --git a/src/window.rs b/src/window.rs index 22915ac..30eb451 100644 --- a/src/window.rs +++ b/src/window.rs @@ -3,178 +3,178 @@ use std::marker::PhantomData; use std::ptr; use super::{ - Ui, - ImGuiSetCond, - ImGuiWindowFlags, - ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove, - ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse, - ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders, - ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar, - ImStr, ImVec2 + Ui, + ImGuiSetCond, + ImGuiWindowFlags, + ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove, + ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse, + ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders, + ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar, + ImStr, ImVec2 }; pub struct Window<'ui, 'p> { - pos: (f32, f32), - pos_cond: ImGuiSetCond, - size: (f32, f32), - size_cond: ImGuiSetCond, - name: ImStr<'p>, - opened: Option<&'p mut bool>, - bg_alpha: f32, - flags: ImGuiWindowFlags, - _phantom: PhantomData<&'ui Ui<'ui>> + pos: (f32, f32), + pos_cond: ImGuiSetCond, + size: (f32, f32), + size_cond: ImGuiSetCond, + name: ImStr<'p>, + opened: Option<&'p mut bool>, + bg_alpha: f32, + flags: ImGuiWindowFlags, + _phantom: PhantomData<&'ui Ui<'ui>> } impl<'ui, 'p> Window<'ui, 'p> { - pub fn new() -> Window<'ui, 'p> { - Window { - pos: (0.0, 0.0), - pos_cond: ImGuiSetCond::empty(), - size: (0.0, 0.0), - size_cond: ImGuiSetCond::empty(), - name: unsafe { ImStr::from_bytes(b"Debug\0") }, - opened: None, - bg_alpha: -1.0, - flags: ImGuiWindowFlags::empty(), - _phantom: PhantomData - } - } - #[inline] - pub fn position(self, pos: (f32, f32), cond: ImGuiSetCond) -> Self { - Window { - pos: pos, - pos_cond: cond, - .. self - } - } - #[inline] - pub fn size(self, size: (f32, f32), cond: ImGuiSetCond) -> Self { - Window { - size: size, - size_cond: cond, - .. self - } - } - #[inline] - pub fn name(self, name: ImStr<'p>) -> Self { - Window { - name: name, - .. self - } - } - #[inline] - pub fn opened(self, opened: &'p mut bool) -> Self { - Window { - opened: Some(opened), - .. self - } - } - #[inline] - pub fn bg_alpha(self, bg_alpha: f32) -> Self { - Window { - bg_alpha: bg_alpha, - .. self - } - } - pub fn flags(self, flags: ImGuiWindowFlags) -> Self { - Window { - flags: flags, - .. self - } - } - #[inline] - pub fn title_bar(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoTitleBar, !value), - .. self - } - } - #[inline] - pub fn resizable(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoResize, !value), - .. self - } - } - #[inline] - pub fn movable(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoMove, !value), - .. self - } - } - #[inline] - pub fn scroll_bar(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoScrollbar, !value), - .. self - } - } - #[inline] - pub fn scrollable(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoScrollWithMouse, !value), - .. self - } - } - #[inline] - pub fn collapsible(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoCollapse, !value), - .. self - } - } - #[inline] - pub fn always_auto_resize(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_AlwaysAutoResize, value), - .. self - } - } - #[inline] - pub fn show_borders(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_ShowBorders, value), - .. self - } - } - #[inline] - pub fn save_settings(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoSavedSettings, !value), - .. self - } - } - #[inline] - pub fn inputs(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_NoInputs, !value), - .. self - } - } - #[inline] - pub fn menu_bar(self, value: bool) -> Self { - Window { - flags: self.flags.with(ImGuiWindowFlags_MenuBar, value), - .. self - } - } - pub fn build(self, f: F) { - let render = unsafe { - if !self.pos_cond.is_empty() { - imgui_sys::igSetNextWindowPos(ImVec2::new(self.pos.0, self.pos.1), self.pos_cond); - } - if !self.size_cond.is_empty() { - imgui_sys::igSetNextWindowSize(ImVec2::new(self.size.0, self.size.1), self.size_cond); - } - imgui_sys::igBegin2(self.name.as_ptr(), + pub fn new() -> Window<'ui, 'p> { + Window { + pos: (0.0, 0.0), + pos_cond: ImGuiSetCond::empty(), + size: (0.0, 0.0), + size_cond: ImGuiSetCond::empty(), + name: unsafe { ImStr::from_bytes(b"Debug\0") }, + opened: None, + bg_alpha: -1.0, + flags: ImGuiWindowFlags::empty(), + _phantom: PhantomData + } + } + #[inline] + pub fn position(self, pos: (f32, f32), cond: ImGuiSetCond) -> Self { + Window { + pos: pos, + pos_cond: cond, + .. self + } + } + #[inline] + pub fn size(self, size: (f32, f32), cond: ImGuiSetCond) -> Self { + Window { + size: size, + size_cond: cond, + .. self + } + } + #[inline] + pub fn name(self, name: ImStr<'p>) -> Self { + Window { + name: name, + .. self + } + } + #[inline] + pub fn opened(self, opened: &'p mut bool) -> Self { + Window { + opened: Some(opened), + .. self + } + } + #[inline] + pub fn bg_alpha(self, bg_alpha: f32) -> Self { + Window { + bg_alpha: bg_alpha, + .. self + } + } + pub fn flags(self, flags: ImGuiWindowFlags) -> Self { + Window { + flags: flags, + .. self + } + } + #[inline] + pub fn title_bar(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoTitleBar, !value), + .. self + } + } + #[inline] + pub fn resizable(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoResize, !value), + .. self + } + } + #[inline] + pub fn movable(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoMove, !value), + .. self + } + } + #[inline] + pub fn scroll_bar(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoScrollbar, !value), + .. self + } + } + #[inline] + pub fn scrollable(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoScrollWithMouse, !value), + .. self + } + } + #[inline] + pub fn collapsible(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoCollapse, !value), + .. self + } + } + #[inline] + pub fn always_auto_resize(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_AlwaysAutoResize, value), + .. self + } + } + #[inline] + pub fn show_borders(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_ShowBorders, value), + .. self + } + } + #[inline] + pub fn save_settings(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoSavedSettings, !value), + .. self + } + } + #[inline] + pub fn inputs(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_NoInputs, !value), + .. self + } + } + #[inline] + pub fn menu_bar(self, value: bool) -> Self { + Window { + flags: self.flags.with(ImGuiWindowFlags_MenuBar, value), + .. self + } + } + pub fn build(self, f: F) { + let render = unsafe { + if !self.pos_cond.is_empty() { + imgui_sys::igSetNextWindowPos(ImVec2::new(self.pos.0, self.pos.1), self.pos_cond); + } + if !self.size_cond.is_empty() { + imgui_sys::igSetNextWindowSize(ImVec2::new(self.size.0, self.size.1), self.size_cond); + } + imgui_sys::igBegin2(self.name.as_ptr(), self.opened.map(|x| x as *mut bool).unwrap_or(ptr::null_mut()), ImVec2::new(0.0, 0.0), self.bg_alpha, self.flags - ) - }; - if render { - f(); - } - unsafe { imgui_sys::igEnd() }; - } + ) + }; + if render { + f(); + } + unsafe { imgui_sys::igEnd() }; + } }