Add rustfmt configuration and reformat

This commit is contained in:
Joonas Javanainen 2016-06-10 22:58:50 +03:00
parent 26799233e4
commit 56f1cb964d
11 changed files with 329 additions and 461 deletions

2
rustfmt.toml Normal file
View File

@ -0,0 +1,2 @@
fn_single_line = true
reorder_imports = true

View File

@ -1,8 +1,5 @@
use glium::{
index, program, texture, vertex,
Blend, DrawError, DrawParameters, GlObject, IndexBuffer, Program, Rect, Surface, Texture2d,
VertexBuffer
};
use glium::{Blend, DrawError, DrawParameters, GlObject, IndexBuffer, Program, Rect, Surface,
Texture2d, VertexBuffer, index, program, texture, vertex};
use glium::backend::{Context, Facade};
use glium::index::PrimitiveType;
use glium::texture::{ClientFormat, RawImage2d};
@ -11,7 +8,7 @@ use std::borrow::Cow;
use std::fmt;
use std::rc::Rc;
use super::{DrawList, Ui, ImDrawIdx, ImDrawVert, ImGui};
use super::{DrawList, ImDrawIdx, ImDrawVert, ImGui, Ui};
pub type RendererResult<T> = Result<T, RendererError>;
@ -21,7 +18,7 @@ pub enum RendererError {
Index(index::BufferCreationError),
Program(program::ProgramChooserCreationError),
Texture(texture::TextureCreationError),
Draw(DrawError)
Draw(DrawError),
}
impl fmt::Display for RendererError {
@ -32,44 +29,34 @@ impl fmt::Display for RendererError {
Index(_) => write!(f, "Index buffer creation failed"),
Program(ref e) => write!(f, "Program creation failed: {}", e),
Texture(_) => write!(f, "Texture creation failed"),
Draw(ref e) => write!(f, "Drawing failed: {}", e)
Draw(ref e) => write!(f, "Drawing failed: {}", e),
}
}
}
impl From<vertex::BufferCreationError> for RendererError {
fn from(e: vertex::BufferCreationError) -> RendererError {
RendererError::Vertex(e)
}
fn from(e: vertex::BufferCreationError) -> RendererError { RendererError::Vertex(e) }
}
impl From<index::BufferCreationError> for RendererError {
fn from(e: index::BufferCreationError) -> RendererError {
RendererError::Index(e)
}
fn from(e: index::BufferCreationError) -> RendererError { RendererError::Index(e) }
}
impl From<program::ProgramChooserCreationError> for RendererError {
fn from(e: program::ProgramChooserCreationError) -> RendererError {
RendererError::Program(e)
}
fn from(e: program::ProgramChooserCreationError) -> RendererError { RendererError::Program(e) }
}
impl From<texture::TextureCreationError> for RendererError {
fn from(e: texture::TextureCreationError) -> RendererError {
RendererError::Texture(e)
}
fn from(e: texture::TextureCreationError) -> RendererError { RendererError::Texture(e) }
}
impl From<DrawError> for RendererError {
fn from(e: DrawError) -> RendererError {
RendererError::Draw(e)
}
fn from(e: DrawError) -> RendererError { RendererError::Draw(e) }
}
pub struct Renderer {
ctx: Rc<Context>,
device_objects: DeviceObjects
device_objects: DeviceObjects,
}
impl Renderer {
@ -77,28 +64,27 @@ impl Renderer {
let device_objects = try!(DeviceObjects::init(imgui, ctx));
Ok(Renderer {
ctx: ctx.get_context().clone(),
device_objects: device_objects
device_objects: device_objects,
})
}
pub fn render<'a, S: Surface>(&mut self, surface: &mut S,
ui: Ui<'a>) -> RendererResult<()> {
pub fn render<'a, S: Surface>(&mut self, surface: &mut S, ui: Ui<'a>) -> RendererResult<()> {
let _ = self.ctx.insert_debug_marker("imgui-rs: starting rendering");
let result = ui.render(|draw_list| self.render_draw_list(surface, draw_list));
let _ = self.ctx.insert_debug_marker("imgui-rs: rendering finished");
result
}
fn render_draw_list<'a, S: Surface>(&mut self, surface: &mut S,
draw_list: DrawList<'a>) -> RendererResult<()> {
fn render_draw_list<'a, S: Surface>(&mut self,
surface: &mut S,
draw_list: DrawList<'a>)
-> RendererResult<()> {
try!(self.device_objects.upload_vertex_buffer(&self.ctx, draw_list.vtx_buffer));
try!(self.device_objects.upload_index_buffer(&self.ctx, draw_list.idx_buffer));
let (width, height) = surface.get_dimensions();
let matrix = [
[ 2.0 / (width as f32), 0.0, 0.0, 0.0 ],
[ 0.0, 2.0 / -(height as f32), 0.0, 0.0 ],
[ 0.0, 0.0, -1.0, 0.0 ],
[ -1.0, 1.0, 0.0, 1.0 ]
];
let matrix = [[2.0 / (width as f32), 0.0, 0.0, 0.0],
[0.0, 2.0 / -(height as f32), 0.0, 0.0],
[0.0, 0.0, -1.0, 0.0],
[-1.0, 1.0, 0.0, 1.0]];
let font_texture_id = self.device_objects.texture.get_id() as uintptr_t;
let mut idx_start = 0;
@ -115,13 +101,15 @@ impl Renderer {
left: cmd.clip_rect.x as u32,
bottom: (height as f32 - cmd.clip_rect.w) as u32,
width: (cmd.clip_rect.z - cmd.clip_rect.x) as u32,
height: (cmd.clip_rect.w - cmd.clip_rect.y) as u32
height: (cmd.clip_rect.w - cmd.clip_rect.y) as u32,
}),
.. Default::default()
..Default::default()
};
let idx_end = idx_start + cmd.elem_count as usize;
try!(surface.draw(&self.device_objects.vertex_buffer,
&self.device_objects.index_buffer.slice(idx_start ..idx_end)
&self.device_objects
.index_buffer
.slice(idx_start..idx_end)
.expect("Invalid index buffer range"),
&self.device_objects.program,
&uniforms,
@ -136,10 +124,11 @@ pub struct DeviceObjects {
vertex_buffer: VertexBuffer<ImDrawVert>,
index_buffer: IndexBuffer<ImDrawIdx>,
program: Program,
texture: Texture2d
texture: Texture2d,
}
fn compile_default_program<F: Facade>(ctx: &F) -> Result<Program, program::ProgramChooserCreationError> {
fn compile_default_program<F: Facade>(ctx: &F)
-> Result<Program, program::ProgramChooserCreationError> {
program!(
ctx,
140 => {
@ -166,7 +155,7 @@ impl DeviceObjects {
data: Cow::Borrowed(handle.pixels),
width: handle.width,
height: handle.height,
format: ClientFormat::U8U8U8U8
format: ClientFormat::U8U8U8U8,
};
Texture2d::new(ctx, data)
}));
@ -176,34 +165,38 @@ impl DeviceObjects {
vertex_buffer: vertex_buffer,
index_buffer: index_buffer,
program: program,
texture: texture
texture: texture,
})
}
pub fn upload_vertex_buffer<F: Facade>(&mut self, ctx: &F,
vtx_buffer: &[ImDrawVert]) -> RendererResult<()> {
pub fn upload_vertex_buffer<F: Facade>(&mut self,
ctx: &F,
vtx_buffer: &[ImDrawVert])
-> RendererResult<()> {
self.vertex_buffer.invalidate();
if let Some(slice) = self.vertex_buffer.slice_mut(0 .. vtx_buffer.len()) {
if let Some(slice) = self.vertex_buffer.slice_mut(0..vtx_buffer.len()) {
slice.write(vtx_buffer);
return Ok(());
}
self.vertex_buffer = try!(VertexBuffer::dynamic(ctx, vtx_buffer));
let _ = ctx.get_context().insert_debug_marker(
&format!("imgui-rs: resized vertex buffer to {} bytes",
self.vertex_buffer.get_size()));
let _ = ctx.get_context()
.insert_debug_marker(&format!("imgui-rs: resized vertex buffer to {} bytes",
self.vertex_buffer.get_size()));
Ok(())
}
pub fn upload_index_buffer<F: Facade>(&mut self, ctx: &F,
idx_buffer: &[ImDrawIdx]) -> RendererResult<()> {
pub fn upload_index_buffer<F: Facade>(&mut self,
ctx: &F,
idx_buffer: &[ImDrawIdx])
-> RendererResult<()> {
self.index_buffer.invalidate();
if let Some(slice) = self.index_buffer.slice_mut(0 .. idx_buffer.len()) {
if let Some(slice) = self.index_buffer.slice_mut(0..idx_buffer.len()) {
slice.write(idx_buffer);
return Ok(());
}
self.index_buffer =
try!(IndexBuffer::dynamic(ctx, PrimitiveType::TrianglesList, idx_buffer));
let _ = ctx.get_context().insert_debug_marker(
&format!("imgui-rs: resized index buffer to {} bytes",
self.index_buffer.get_size()));
let _ = ctx.get_context()
.insert_debug_marker(&format!("imgui-rs: resized index buffer to {} bytes",
self.index_buffer.get_size()));
Ok(())
}
}

View File

@ -3,17 +3,15 @@ use libc::size_t;
use std::marker::PhantomData;
use std::ptr;
use super::{
Ui,
ImGuiInputTextFlags, ImGuiInputTextFlags_CharsDecimal, ImGuiInputTextFlags_CharsHexadecimal,
ImGuiInputTextFlags_CharsUppercase, ImGuiInputTextFlags_CharsNoBlank,
ImGuiInputTextFlags_AutoSelectAll, ImGuiInputTextFlags_EnterReturnsTrue,
ImGuiInputTextFlags_CallbackCompletion, ImGuiInputTextFlags_CallbackHistory,
ImGuiInputTextFlags_CallbackAlways, ImGuiInputTextFlags_CallbackCharFilter,
ImGuiInputTextFlags_AllowTabInput, //ImGuiInputTextFlags_CtrlEnterForNewLine,
ImGuiInputTextFlags_NoHorizontalScroll, ImGuiInputTextFlags_AlwaysInsertMode,
ImStr
};
use super::{ImGuiInputTextFlags,
ImGuiInputTextFlags_AllowTabInput /* ImGuiInputTextFlags_CtrlEnterForNewLine, */,
ImGuiInputTextFlags_AlwaysInsertMode, ImGuiInputTextFlags_AutoSelectAll,
ImGuiInputTextFlags_CallbackAlways, ImGuiInputTextFlags_CallbackCharFilter,
ImGuiInputTextFlags_CallbackCompletion, ImGuiInputTextFlags_CallbackHistory,
ImGuiInputTextFlags_CharsDecimal, ImGuiInputTextFlags_CharsHexadecimal,
ImGuiInputTextFlags_CharsNoBlank, ImGuiInputTextFlags_CharsUppercase,
ImGuiInputTextFlags_EnterReturnsTrue, ImGuiInputTextFlags_NoHorizontalScroll, ImStr,
Ui};
macro_rules! impl_text_flags {
($InputType:ident) => {
@ -169,7 +167,7 @@ pub struct InputText<'ui, 'p> {
label: ImStr<'p>,
buf: &'p mut str,
flags: ImGuiInputTextFlags,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> InputText<'ui, 'p> {
@ -178,7 +176,7 @@ impl<'ui, 'p> InputText<'ui, 'p> {
label: label,
buf: buf,
flags: ImGuiInputTextFlags::empty(),
_phantom: PhantomData
_phantom: PhantomData,
}
}
@ -189,14 +187,14 @@ impl<'ui, 'p> InputText<'ui, 'p> {
pub fn build(self) -> bool {
unsafe {
imgui_sys::igInputText(
self.label.as_ptr(),
// TODO: this is evil. Perhaps something else than &mut str is better
self.buf.as_ptr() as *mut i8,
self.buf.len() as size_t,
self.flags,
None,
ptr::null_mut())
imgui_sys::igInputText(self.label.as_ptr(),
// TODO: this is evil.
// Perhaps something else than &mut str is better
self.buf.as_ptr() as *mut i8,
self.buf.len() as size_t,
self.flags,
None,
ptr::null_mut())
}
}
}
@ -208,7 +206,7 @@ pub struct InputInt<'ui, 'p> {
step: i32,
step_fast: i32,
flags: ImGuiInputTextFlags,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> InputInt<'ui, 'p> {
@ -219,18 +217,17 @@ impl<'ui, 'p> InputInt<'ui, 'p> {
step: 1,
step_fast: 100,
flags: ImGuiInputTextFlags::empty(),
_phantom: PhantomData
_phantom: PhantomData,
}
}
pub fn build(self) -> bool {
unsafe {
imgui_sys::igInputInt(
self.label.as_ptr(),
self.value as *mut i32,
self.step,
self.step_fast,
self.flags)
imgui_sys::igInputInt(self.label.as_ptr(),
self.value as *mut i32,
self.step,
self.step_fast,
self.flags)
}
}
@ -246,7 +243,7 @@ pub struct InputFloat<'ui, 'p> {
step_fast: f32,
decimal_precision: i32,
flags: ImGuiInputTextFlags,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> InputFloat<'ui, 'p> {
@ -258,19 +255,18 @@ impl<'ui, 'p> InputFloat<'ui, 'p> {
step_fast: 0.0,
decimal_precision: -1,
flags: ImGuiInputTextFlags::empty(),
_phantom: PhantomData
_phantom: PhantomData,
}
}
pub fn build(self) -> bool {
unsafe {
imgui_sys::igInputFloat(
self.label.as_ptr(),
self.value as *mut f32,
self.step,
self.step_fast,
self.decimal_precision,
self.flags)
imgui_sys::igInputFloat(self.label.as_ptr(),
self.value as *mut f32,
self.step,
self.step_fast,
self.decimal_precision,
self.flags)
}
}
@ -362,52 +358,47 @@ impl_input_intn!(InputInt4, 4, igInputInt4);
#[must_use]
pub struct ColorEdit3<'ui, 'p> {
label: ImStr<'p>,
value: &'p mut [f32;3],
_phantom: PhantomData<&'ui Ui<'ui>>
value: &'p mut [f32; 3],
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> ColorEdit3<'ui, 'p> {
pub fn new(label: ImStr<'p>, value: &'p mut [f32;3]) -> Self {
pub fn new(label: ImStr<'p>, value: &'p mut [f32; 3]) -> Self {
ColorEdit3 {
label: label,
value: value,
_phantom: PhantomData
_phantom: PhantomData,
}
}
pub fn build(self) -> bool {
unsafe {
imgui_sys::igColorEdit3(
self.label.as_ptr(),
self.value.as_mut_ptr())
}
unsafe { imgui_sys::igColorEdit3(self.label.as_ptr(), self.value.as_mut_ptr()) }
}
}
#[must_use]
pub struct ColorEdit4<'ui, 'p> {
label: ImStr<'p>,
value: &'p mut [f32;4],
value: &'p mut [f32; 4],
show_alpha: bool,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> ColorEdit4<'ui, 'p> {
pub fn new(label: ImStr<'p>, value: &'p mut [f32;4]) -> Self {
pub fn new(label: ImStr<'p>, value: &'p mut [f32; 4]) -> Self {
ColorEdit4 {
label: label,
value: value,
show_alpha: true,
_phantom: PhantomData
_phantom: PhantomData,
}
}
pub fn build(self) -> bool {
unsafe {
imgui_sys::igColorEdit4(
self.label.as_ptr(),
self.value.as_mut_ptr(),
self.show_alpha)
imgui_sys::igColorEdit4(self.label.as_ptr(),
self.value.as_mut_ptr(),
self.show_alpha)
}
}
}

View File

@ -15,46 +15,34 @@ use std::ptr;
use std::slice;
use std::str;
pub use imgui_sys::{
ImDrawIdx, ImDrawVert,
ImGuiInputTextFlags, ImGuiInputTextFlags_CharsDecimal, ImGuiInputTextFlags_CharsHexadecimal,
ImGuiInputTextFlags_CharsUppercase, ImGuiInputTextFlags_CharsNoBlank,
ImGuiInputTextFlags_AutoSelectAll, ImGuiInputTextFlags_EnterReturnsTrue,
ImGuiInputTextFlags_CallbackCompletion, ImGuiInputTextFlags_CallbackHistory,
ImGuiInputTextFlags_CallbackAlways, ImGuiInputTextFlags_CallbackCharFilter,
ImGuiInputTextFlags_AllowTabInput, ImGuiInputTextFlags_CtrlEnterForNewLine,
ImGuiInputTextFlags_NoHorizontalScroll, ImGuiInputTextFlags_AlwaysInsertMode,
ImGuiInputTextFlags_ReadOnly,
ImGuiInputTextFlags_Password,
ImGuiSelectableFlags,
ImGuiSelectableFlags_DontClosePopups, ImGuiSelectableFlags_SpanAllColumns,
ImGuiSetCond,
ImGuiSetCond_Always, ImGuiSetCond_Once,
ImGuiSetCond_FirstUseEver, ImGuiSetCond_Appearing,
ImGuiStyle,
ImGuiWindowFlags,
ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove,
ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoCollapse,
ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_ShowBorders,
ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar,
ImGuiWindowFlags_HorizontalScrollbar, ImGuiWindowFlags_NoFocusOnAppearing,
ImGuiWindowFlags_NoBringToFrontOnFocus,
ImVec2, ImVec4,
ImGuiKey
};
pub use input::{
ColorEdit3, ColorEdit4,
InputFloat, InputFloat2, InputFloat3, InputFloat4,
InputInt, InputInt2, InputInt3, InputInt4,
InputText
};
pub use imgui_sys::{ImDrawIdx, ImDrawVert, ImGuiInputTextFlags, ImGuiInputTextFlags_AllowTabInput,
ImGuiInputTextFlags_AlwaysInsertMode, ImGuiInputTextFlags_AutoSelectAll,
ImGuiInputTextFlags_CallbackAlways, ImGuiInputTextFlags_CallbackCharFilter,
ImGuiInputTextFlags_CallbackCompletion, ImGuiInputTextFlags_CallbackHistory,
ImGuiInputTextFlags_CharsDecimal, ImGuiInputTextFlags_CharsHexadecimal,
ImGuiInputTextFlags_CharsNoBlank, ImGuiInputTextFlags_CharsUppercase,
ImGuiInputTextFlags_CtrlEnterForNewLine, ImGuiInputTextFlags_EnterReturnsTrue,
ImGuiInputTextFlags_NoHorizontalScroll, ImGuiInputTextFlags_Password,
ImGuiInputTextFlags_ReadOnly, ImGuiKey, ImGuiSelectableFlags,
ImGuiSelectableFlags_DontClosePopups, ImGuiSelectableFlags_SpanAllColumns,
ImGuiSetCond, ImGuiSetCond_Always, ImGuiSetCond_Appearing,
ImGuiSetCond_FirstUseEver, ImGuiSetCond_Once, ImGuiStyle, ImGuiWindowFlags,
ImGuiWindowFlags_AlwaysAutoResize, ImGuiWindowFlags_HorizontalScrollbar,
ImGuiWindowFlags_MenuBar, ImGuiWindowFlags_NoBringToFrontOnFocus,
ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_NoFocusOnAppearing,
ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_NoMove, ImGuiWindowFlags_NoResize,
ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoScrollWithMouse,
ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoTitleBar,
ImGuiWindowFlags_ShowBorders, ImVec2, ImVec4};
pub use input::{ColorEdit3, ColorEdit4, InputFloat, InputFloat2, InputFloat3, InputFloat4,
InputInt, InputInt2, InputInt3, InputInt4, InputText};
pub use menus::{Menu, MenuItem};
pub use sliders::{SliderFloat, SliderInt};
pub use trees::{TreeNode};
pub use widgets::{CollapsingHeader};
pub use window::{Window};
pub use plotlines::{PlotLines};
pub use plothistogram::{PlotHistogram};
pub use trees::TreeNode;
pub use widgets::CollapsingHeader;
pub use window::Window;
pub use plotlines::PlotLines;
pub use plothistogram::PlotHistogram;
mod input;
mod menus;
@ -72,7 +60,7 @@ pub struct ImGui {
// We need to keep ownership of the ImStr values to ensure the *const char pointer
// lives long enough in case the ImStr contains a Cow::Owned
ini_filename: Option<ImStr<'static>>,
log_filename: Option<ImStr<'static>>
log_filename: Option<ImStr<'static>>,
}
#[macro_export]
@ -88,14 +76,12 @@ macro_rules! im_str {
#[derive(Clone)]
pub struct ImStr<'a> {
bytes: Cow<'a, [u8]>
bytes: Cow<'a, [u8]>,
}
impl<'a> ImStr<'a> {
pub unsafe fn from_bytes_unchecked(bytes: &'a [u8]) -> ImStr<'a> {
ImStr {
bytes: Cow::Borrowed(bytes)
}
ImStr { bytes: Cow::Borrowed(bytes) }
}
pub fn as_ptr(&self) -> *const c_char { self.bytes.as_ptr() as *const c_char }
}
@ -104,25 +90,21 @@ impl<'a> From<&'a str> for ImStr<'a> {
fn from(value: &'a str) -> ImStr<'a> {
let mut bytes: Vec<u8> = value.bytes().collect();
bytes.push(0);
ImStr {
bytes: Cow::Owned(bytes)
}
ImStr { bytes: Cow::Owned(bytes) }
}
}
impl From<String> for ImStr<'static> {
fn from(mut value: String) -> ImStr<'static> {
value.push('\0');
ImStr {
bytes: Cow::Owned(value.into_bytes())
}
ImStr { bytes: Cow::Owned(value.into_bytes()) }
}
}
pub struct TextureHandle<'a> {
pub width: u32,
pub height: u32,
pub pixels: &'a [c_uchar]
pub pixels: &'a [c_uchar],
}
pub fn get_version() -> &'static str {
@ -136,33 +118,35 @@ impl ImGui {
pub fn init() -> ImGui {
ImGui {
ini_filename: None,
log_filename: None
log_filename: None,
}
}
fn io(&self) -> &imgui_sys::ImGuiIO {
unsafe { mem::transmute(imgui_sys::igGetIO()) }
}
fn io(&self) -> &imgui_sys::ImGuiIO { unsafe { mem::transmute(imgui_sys::igGetIO()) } }
fn io_mut(&mut self) -> &mut imgui_sys::ImGuiIO {
unsafe { mem::transmute(imgui_sys::igGetIO()) }
}
pub fn style(&self) -> &ImGuiStyle {
unsafe { mem::transmute(imgui_sys::igGetStyle()) }
}
pub fn style(&self) -> &ImGuiStyle { unsafe { mem::transmute(imgui_sys::igGetStyle()) } }
pub fn style_mut(&self) -> &mut ImGuiStyle {
unsafe { mem::transmute(imgui_sys::igGetStyle()) }
}
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 = self.io();
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);
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)
pixels: slice::from_raw_parts(pixels, (width * height * bytes_per_pixel) as usize),
})
}
}
@ -175,8 +159,8 @@ impl ImGui {
{
let io = self.io_mut();
io.ini_filename = match value {
Some(ref x) => x.as_ptr(),
None => ptr::null()
Some(ref x) => x.as_ptr(),
None => ptr::null(),
}
}
self.ini_filename = value;
@ -185,8 +169,8 @@ impl ImGui {
{
let io = self.io_mut();
io.log_filename = match value {
Some(ref x) => x.as_ptr(),
None => ptr::null()
Some(ref x) => x.as_ptr(),
None => ptr::null(),
}
}
self.log_filename = value;
@ -279,13 +263,9 @@ impl ImGui {
}
unsafe {
imgui_sys::igNewFrame();
CURRENT_UI = Some(Ui {
imgui: mem::transmute(self as &'a ImGui)
});
}
Ui {
imgui: self
CURRENT_UI = Some(Ui { imgui: mem::transmute(self as &'a ImGui) });
}
Ui { imgui: self }
}
}
@ -303,11 +283,11 @@ static mut CURRENT_UI: Option<Ui<'static>> = None;
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 vtx_buffer: &'a [imgui_sys::ImDrawVert],
}
pub struct Ui<'ui> {
imgui: &'ui ImGui
imgui: &'ui ImGui,
}
static FMT: &'static [u8] = b"%s\0";
@ -345,24 +325,24 @@ impl<'ui> Ui<'ui> {
io.metrics_active_windows
}
pub fn render<F, E>(self, mut f: F) -> Result<(), E>
where F: FnMut(DrawList<'ui>) -> Result<(), E> {
unsafe {
imgui_sys::igRender();
where F: FnMut(DrawList<'ui>) -> Result<(), E>
{
unsafe {
imgui_sys::igRender();
let draw_data = imgui_sys::igGetDrawData();
for &cmd_list in (*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));
}
CURRENT_UI = None;
let draw_data = imgui_sys::igGetDrawData();
for &cmd_list in (*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(())
CURRENT_UI = None;
}
Ok(())
}
pub fn show_user_guide(&self) { unsafe { imgui_sys::igShowUserGuide() }; }
pub fn show_default_style_editor(&self) {
unsafe { imgui_sys::igShowStyleEditor(ptr::null_mut()) };
@ -385,9 +365,7 @@ impl<'ui> Ui<'ui> {
}
impl<'a> Ui<'a> {
pub unsafe fn current_ui() -> Option<&'a Ui<'a>> {
CURRENT_UI.as_ref()
}
pub unsafe fn current_ui() -> Option<&'a Ui<'a>> { CURRENT_UI.as_ref() }
}
// Window
@ -398,29 +376,19 @@ impl<'ui> Ui<'ui> {
// 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(&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)
}
unsafe { imgui_sys::igSameLine(pos_x, spacing_w) }
}
pub fn spacing(&self) { unsafe { imgui_sys::igSpacing() }; }
pub fn columns<'p>(&self, count: i32, id: ImStr<'p>, border: bool){
pub fn columns<'p>(&self, count: i32, id: ImStr<'p>, border: bool) {
unsafe { imgui_sys::igColumns(count, id.as_ptr(), border) }
}
pub fn next_column(&self) {
unsafe { imgui_sys::igNextColumn() }
}
pub fn next_column(&self) { unsafe { imgui_sys::igNextColumn() } }
pub fn get_column_index(&self) -> i32 {
unsafe { imgui_sys::igGetColumnIndex() }
}
pub fn get_column_index(&self) -> i32 { unsafe { imgui_sys::igGetColumnIndex() } }
pub fn get_column_offset(&self, column_index: i32) -> f32 {
unsafe { imgui_sys::igGetColumnOffset(column_index) }
@ -434,11 +402,7 @@ impl<'ui> Ui<'ui> {
unsafe { imgui_sys::igGetColumnWidth(column_index) }
}
pub fn get_columns_count(&self) -> i32 {
unsafe { imgui_sys::igGetColumnsCount() }
}
pub fn get_columns_count(&self) -> i32 { unsafe { imgui_sys::igGetColumnsCount() } }
}
// Widgets
@ -449,7 +413,9 @@ impl<'ui> Ui<'ui> {
imgui_sys::igText(fmt_ptr(), text.as_ptr());
}
}
pub fn text_colored<'p, A>(&self, col: A, text: ImStr<'p>) where A: Into<ImVec4> {
pub fn text_colored<'p, A>(&self, col: A, text: ImStr<'p>)
where A: Into<ImVec4>
{
unsafe {
imgui_sys::igTextColored(col.into(), fmt_ptr(), text.as_ptr());
}
@ -480,9 +446,7 @@ impl<'ui> Ui<'ui> {
}
}
pub fn small_button<'p>(&self, label: ImStr<'p>) -> bool {
unsafe {
imgui_sys::igSmallButton(label.as_ptr())
}
unsafe { imgui_sys::igSmallButton(label.as_ptr()) }
}
pub fn collapsing_header<'p>(&self, label: ImStr<'p>) -> CollapsingHeader<'ui, 'p> {
CollapsingHeader::new(label)
@ -494,10 +458,16 @@ impl<'ui> Ui<'ui> {
// Widgets: Input
impl<'ui> Ui<'ui> {
pub fn color_edit3<'p>(&self, label: ImStr<'p>, value: &'p mut [f32;3]) -> ColorEdit3<'ui, 'p> {
pub fn color_edit3<'p>(&self,
label: ImStr<'p>,
value: &'p mut [f32; 3])
-> ColorEdit3<'ui, 'p> {
ColorEdit3::new(label, value)
}
pub fn color_edit4<'p>(&self, label: ImStr<'p>, value: &'p mut [f32;4]) -> ColorEdit4<'ui, 'p> {
pub fn color_edit4<'p>(&self,
label: ImStr<'p>,
value: &'p mut [f32; 4])
-> ColorEdit4<'ui, 'p> {
ColorEdit4::new(label, value)
}
pub fn input_text<'p>(&self, label: ImStr<'p>, buf: &'p mut str) -> InputText<'ui, 'p> {
@ -506,66 +476,89 @@ impl<'ui> Ui<'ui> {
pub fn input_float<'p>(&self, label: ImStr<'p>, value: &'p mut f32) -> InputFloat<'ui, 'p> {
InputFloat::new(label, value)
}
pub fn input_float2<'p>(&self, label: ImStr<'p>, value: &'p mut [f32;2]) -> InputFloat2<'ui, 'p> {
pub fn input_float2<'p>(&self,
label: ImStr<'p>,
value: &'p mut [f32; 2])
-> InputFloat2<'ui, 'p> {
InputFloat2::new(label, value)
}
pub fn input_float3<'p>(&self, label: ImStr<'p>, value: &'p mut [f32;3]) -> InputFloat3<'ui, 'p> {
pub fn input_float3<'p>(&self,
label: ImStr<'p>,
value: &'p mut [f32; 3])
-> InputFloat3<'ui, 'p> {
InputFloat3::new(label, value)
}
pub fn input_float4<'p>(&self, label: ImStr<'p>, value: &'p mut [f32;4]) -> InputFloat4<'ui, 'p> {
pub fn input_float4<'p>(&self,
label: ImStr<'p>,
value: &'p mut [f32; 4])
-> InputFloat4<'ui, 'p> {
InputFloat4::new(label, value)
}
pub fn input_int<'p>(&self, label: ImStr<'p>, value: &'p mut i32) -> InputInt<'ui, 'p> {
InputInt::new(label, value)
}
pub fn input_int2<'p>(&self, label: ImStr<'p>, value: &'p mut [i32;2]) -> InputInt2<'ui, 'p> {
pub fn input_int2<'p>(&self, label: ImStr<'p>, value: &'p mut [i32; 2]) -> InputInt2<'ui, 'p> {
InputInt2::new(label, value)
}
pub fn input_int3<'p>(&self, label: ImStr<'p>, value: &'p mut [i32;3]) -> InputInt3<'ui, 'p> {
pub fn input_int3<'p>(&self, label: ImStr<'p>, value: &'p mut [i32; 3]) -> InputInt3<'ui, 'p> {
InputInt3::new(label, value)
}
pub fn input_int4<'p>(&self, label: ImStr<'p>, value: &'p mut [i32;4]) -> InputInt4<'ui, 'p> {
pub fn input_int4<'p>(&self, label: ImStr<'p>, value: &'p mut [i32; 4]) -> InputInt4<'ui, 'p> {
InputInt4::new(label, value)
}
}
// Widgets: Sliders
impl<'ui> Ui<'ui> {
pub fn slider_float<'p>(&self, label: ImStr<'p>,
value: &'p mut f32, min: f32, max: f32) -> SliderFloat<'ui, 'p> {
pub fn slider_float<'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_int<'p>(&self, label: ImStr<'p>,
value: &'p mut i32, min: i32, max: i32) -> SliderInt<'ui, 'p> {
pub fn slider_int<'p>(&self,
label: ImStr<'p>,
value: &'p mut i32,
min: i32,
max: i32)
-> SliderInt<'ui, 'p> {
SliderInt::new(label, value, min, max)
}
}
// Widgets: Trees
impl<'ui> Ui<'ui> {
pub fn tree_node<'p>(&self, id: ImStr<'p>) -> TreeNode<'ui, 'p> {
TreeNode::new(id)
}
pub fn tree_node<'p>(&self, id: ImStr<'p>) -> TreeNode<'ui, 'p> { TreeNode::new(id) }
}
// Widgets: Selectable / Lists
impl<'ui> Ui<'ui> {
pub fn selectable<'p>(&self, label: ImStr<'p>, selected: bool, flags: ImGuiSelectableFlags,
size: ImVec2) -> bool {
pub fn selectable<'p>(&self,
label: ImStr<'p>,
selected: bool,
flags: ImGuiSelectableFlags,
size: ImVec2)
-> bool {
unsafe { imgui_sys::igSelectable(label.as_ptr(), selected, flags, size) }
}
}
// Widgets: Menus
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() };
if render {
f();
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() };
if render {
f();
@ -581,7 +574,9 @@ 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() {
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();
@ -590,15 +585,15 @@ impl<'ui> Ui<'ui> {
}
}
//Widgets: Combos
// Widgets: Combos
impl<'ui> Ui<'ui> {
pub fn combo<'p>(&self,
pub fn combo<'p>(&self,
label: ImStr<'p>,
current_item: &mut i32,
items: &'p [ImStr<'p>],
height_in_items: i32)
height_in_items: i32)
-> bool {
let items_inner : Vec<*const c_char> = items.into_iter().map(|item| item.as_ptr()).collect();
let items_inner: Vec<*const c_char> = items.into_iter().map(|item| item.as_ptr()).collect();
unsafe {
imgui_sys::igCombo(label.as_ptr(),
current_item,
@ -609,37 +604,33 @@ impl<'ui> Ui<'ui> {
}
}
//Widgets: ListBox
// Widgets: ListBox
impl<'ui> Ui<'ui> {
pub fn list_box<'p>(&self,
label: ImStr<'p>,
current_item: &mut i32,
items: &'p [ImStr<'p>],
height_in_items: i32)
-> bool{
let items_inner : Vec<*const c_char> = items.into_iter().map(|item| item.as_ptr()).collect();
unsafe{
-> bool {
let items_inner: Vec<*const c_char> = items.into_iter().map(|item| item.as_ptr()).collect();
unsafe {
imgui_sys::igListBox(label.as_ptr(),
current_item,
items_inner.as_ptr() as *mut *const c_char,
items_inner.len() as i32,
height_in_items)
}
}
}
}
impl<'ui> Ui<'ui> {
pub fn plot_lines<'p>(&self,
label: ImStr<'p>,
values: &'p[f32])->PlotLines<'p>{
pub fn plot_lines<'p>(&self, label: ImStr<'p>, values: &'p [f32]) -> PlotLines<'p> {
PlotLines::new(label, values)
}
}
impl<'ui> Ui<'ui> {
pub fn plot_histogram<'p>(&self,
label: ImStr<'p>,
values: &'p[f32])->PlotHistogram<'p>{
pub fn plot_histogram<'p>(&self, label: ImStr<'p>, values: &'p [f32]) -> PlotHistogram<'p> {
PlotHistogram::new(label, values)
}
}

View File

@ -2,13 +2,13 @@ use imgui_sys;
use std::marker::PhantomData;
use std::ptr;
use super::{Ui, ImStr};
use super::{ImStr, Ui};
#[must_use]
pub struct Menu<'ui, 'p> {
label: ImStr<'p>,
enabled: bool,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> Menu<'ui, 'p> {
@ -16,16 +16,11 @@ impl<'ui, 'p> Menu<'ui, 'p> {
Menu {
label: label,
enabled: true,
_phantom: PhantomData
_phantom: PhantomData,
}
}
#[inline]
pub fn enabled(self, enabled: bool) -> Self {
Menu {
enabled: enabled,
.. self
}
}
pub fn enabled(self, enabled: bool) -> Self { Menu { enabled: enabled, ..self } }
pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe { imgui_sys::igBeginMenu(self.label.as_ptr(), self.enabled) };
if render {
@ -41,7 +36,7 @@ pub struct MenuItem<'ui, 'p> {
shortcut: Option<ImStr<'p>>,
selected: Option<&'p mut bool>,
enabled: bool,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> MenuItem<'ui, 'p> {
@ -51,37 +46,24 @@ impl<'ui, 'p> MenuItem<'ui, 'p> {
shortcut: None,
selected: None,
enabled: true,
_phantom: PhantomData
_phantom: PhantomData,
}
}
#[inline]
pub fn shortcut(self, shortcut: ImStr<'p>) -> Self {
MenuItem {
shortcut: Some(shortcut),
.. self
}
MenuItem { shortcut: Some(shortcut), ..self }
}
#[inline]
pub fn selected(self, selected: &'p mut bool) -> Self {
MenuItem {
selected: Some(selected),
.. self
}
MenuItem { selected: Some(selected), ..self }
}
#[inline]
pub fn enabled(self, enabled: bool) -> Self {
MenuItem {
enabled: enabled,
.. self
}
}
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)
}
unsafe { imgui_sys::igMenuItemPtr(label, shortcut, selected, enabled) }
}
}

View File

@ -1,5 +1,5 @@
use imgui_sys;
use super::{ImStr};
use super::ImStr;
use imgui_sys::ImVec2;
use std::{f32, mem, ptr};
use libc::c_float;
@ -55,14 +55,16 @@ impl<'p> PlotHistogram<'p> {
pub fn build(self) {
unsafe {
imgui_sys::igPlotHistogram(self.label.as_ptr(),
self.values.as_ptr() as *const c_float,
self.values.len() as i32,
self.values_offset as i32,
self.overlay_text.map(|x| x.as_ptr()).unwrap_or(ptr::null()),
self.scale_min,
self.scale_max,
self.graph_size,
mem::size_of::<f32>() as i32);
self.values.as_ptr() as *const c_float,
self.values.len() as i32,
self.values_offset as i32,
self.overlay_text
.map(|x| x.as_ptr())
.unwrap_or(ptr::null()),
self.scale_min,
self.scale_max,
self.graph_size,
mem::size_of::<f32>() as i32);
}
}
}

View File

@ -1,5 +1,5 @@
use imgui_sys;
use super::{ImStr};
use super::ImStr;
use imgui_sys::ImVec2;
use std::{f32, mem, ptr};
use libc::c_float;
@ -38,14 +38,10 @@ impl<'p> PlotLines<'p> {
}
#[inline]
pub fn scale_min(self, scale_min: f32) -> Self {
PlotLines { scale_min: scale_min, ..self }
}
pub fn scale_min(self, scale_min: f32) -> Self { PlotLines { scale_min: scale_min, ..self } }
#[inline]
pub fn scale_max(self, scale_max: f32) -> Self {
PlotLines { scale_max: scale_max, ..self }
}
pub fn scale_max(self, scale_max: f32) -> Self { PlotLines { scale_max: scale_max, ..self } }
#[inline]
pub fn graph_size(self, graph_size: ImVec2) -> Self {

View File

@ -1,7 +1,7 @@
use imgui_sys;
use std::marker::PhantomData;
use super::{Ui, ImStr};
use super::{ImStr, Ui};
// TODO: Consider using Range, even though it is half-open
@ -12,7 +12,7 @@ pub struct SliderInt<'ui, 'p> {
min: i32,
max: i32,
display_format: ImStr<'p>,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> SliderInt<'ui, 'p> {
@ -23,21 +23,20 @@ impl<'ui, 'p> SliderInt<'ui, 'p> {
min: min,
max: max,
display_format: unsafe { ImStr::from_bytes_unchecked(b"%.0f\0") },
_phantom: PhantomData
_phantom: PhantomData,
}
}
#[inline]
pub fn display_format(self, display_format: ImStr<'p>) -> Self {
SliderInt {
display_format: display_format,
.. 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()
)
imgui_sys::igSliderInt(self.label.as_ptr(),
self.value,
self.min,
self.max,
self.display_format.as_ptr())
}
}
}
@ -50,7 +49,7 @@ pub struct SliderFloat<'ui, 'p> {
max: f32,
display_format: ImStr<'p>,
power: f32,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> SliderFloat<'ui, 'p> {
@ -62,30 +61,23 @@ impl<'ui, 'p> SliderFloat<'ui, 'p> {
max: max,
display_format: unsafe { ImStr::from_bytes_unchecked(b"%.3f\0") },
power: 1.0,
_phantom: PhantomData
_phantom: PhantomData,
}
}
#[inline]
pub fn display_format(self, display_format: ImStr<'p>) -> Self {
SliderFloat {
display_format: display_format,
.. self
}
SliderFloat { display_format: display_format, ..self }
}
#[inline]
pub fn power(self, power: f32) -> Self {
SliderFloat {
power: power,
.. self
}
}
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
)
imgui_sys::igSliderFloat(self.label.as_ptr(),
self.value,
self.min,
self.max,
self.display_format.as_ptr(),
self.power)
}
}
}

View File

@ -1,7 +1,7 @@
use imgui_sys;
use std::marker::PhantomData;
use super::{Ui, ImGuiSetCond, ImStr};
use super::{ImGuiSetCond, ImStr, Ui};
#[must_use]
pub struct TreeNode<'ui, 'p> {
@ -9,7 +9,7 @@ pub struct TreeNode<'ui, 'p> {
label: Option<ImStr<'p>>,
opened: bool,
opened_cond: ImGuiSetCond,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> TreeNode<'ui, 'p> {
@ -19,22 +19,17 @@ impl<'ui, 'p> TreeNode<'ui, 'p> {
label: None,
opened: false,
opened_cond: ImGuiSetCond::empty(),
_phantom: PhantomData
_phantom: PhantomData,
}
}
#[inline]
pub fn label(self, label: ImStr<'p>) -> Self {
TreeNode {
label: Some(label),
.. self
}
}
pub fn label(self, label: ImStr<'p>) -> Self { TreeNode { label: Some(label), ..self } }
#[inline]
pub fn opened(self, opened: bool, cond: ImGuiSetCond) -> Self {
TreeNode {
opened: opened,
opened_cond: cond,
.. self
..self
}
}
pub fn build<F: FnOnce()>(self, f: F) {
@ -42,11 +37,9 @@ impl<'ui, 'p> TreeNode<'ui, 'p> {
if !self.opened_cond.is_empty() {
imgui_sys::igSetNextTreeNodeOpened(self.opened, self.opened_cond);
}
imgui_sys::igTreeNodeStr(
self.id.as_ptr(),
super::fmt_ptr(),
self.label.unwrap_or(self.id).as_ptr()
)
imgui_sys::igTreeNodeStr(self.id.as_ptr(),
super::fmt_ptr(),
self.label.unwrap_or(self.id).as_ptr())
};
if render {
f();

View File

@ -2,7 +2,7 @@ use imgui_sys;
use std::marker::PhantomData;
use std::ptr;
use super::{Ui, ImStr};
use super::{ImStr, Ui};
#[must_use]
pub struct CollapsingHeader<'ui, 'p> {
@ -10,7 +10,7 @@ pub struct CollapsingHeader<'ui, 'p> {
str_id: Option<ImStr<'p>>,
display_frame: bool,
default_open: bool,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> CollapsingHeader<'ui, 'p> {
@ -20,38 +20,27 @@ impl<'ui, 'p> CollapsingHeader<'ui, 'p> {
str_id: None,
display_frame: true,
default_open: false,
_phantom: PhantomData
_phantom: PhantomData,
}
}
#[inline]
pub fn str_id(self, str_id: ImStr<'p>) -> Self {
CollapsingHeader {
str_id: Some(str_id),
.. self
}
CollapsingHeader { str_id: Some(str_id), ..self }
}
#[inline]
pub fn display_frame(self, display_frame: bool) -> Self {
CollapsingHeader {
display_frame: display_frame,
.. self
}
CollapsingHeader { display_frame: display_frame, ..self }
}
#[inline]
pub fn default_open(self, default_open: bool) -> Self {
CollapsingHeader {
default_open: default_open,
.. 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
)
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)
}
}
}

View File

@ -2,18 +2,13 @@ use imgui_sys;
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,
ImGuiWindowFlags_HorizontalScrollbar, ImGuiWindowFlags_NoFocusOnAppearing,
ImGuiWindowFlags_NoBringToFrontOnFocus,
ImStr, ImVec2
};
use super::{ImGuiSetCond, ImGuiWindowFlags, ImGuiWindowFlags_AlwaysAutoResize,
ImGuiWindowFlags_HorizontalScrollbar, ImGuiWindowFlags_MenuBar,
ImGuiWindowFlags_NoBringToFrontOnFocus, ImGuiWindowFlags_NoCollapse,
ImGuiWindowFlags_NoFocusOnAppearing, ImGuiWindowFlags_NoInputs,
ImGuiWindowFlags_NoMove, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoSavedSettings,
ImGuiWindowFlags_NoScrollWithMouse, ImGuiWindowFlags_NoScrollbar,
ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_ShowBorders, ImStr, ImVec2, Ui};
#[must_use]
pub struct Window<'ui, 'p> {
@ -25,7 +20,7 @@ pub struct Window<'ui, 'p> {
opened: Option<&'p mut bool>,
bg_alpha: f32,
flags: ImGuiWindowFlags,
_phantom: PhantomData<&'ui Ui<'ui>>
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> Window<'ui, 'p> {
@ -39,7 +34,7 @@ impl<'ui, 'p> Window<'ui, 'p> {
opened: None,
bg_alpha: -1.0,
flags: ImGuiWindowFlags::empty(),
_phantom: PhantomData
_phantom: PhantomData,
}
}
#[inline]
@ -47,7 +42,7 @@ impl<'ui, 'p> Window<'ui, 'p> {
Window {
pos: pos,
pos_cond: cond,
.. self
..self
}
}
#[inline]
@ -55,127 +50,70 @@ impl<'ui, 'p> Window<'ui, 'p> {
Window {
size: size,
size_cond: cond,
.. self
..self
}
}
#[inline]
pub fn opened(self, opened: &'p mut bool) -> Self {
Window {
opened: Some(opened),
.. self
}
}
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 bg_alpha(self, bg_alpha: f32) -> Self { Window { bg_alpha: bg_alpha, ..self } }
#[inline]
pub fn flags(self, flags: ImGuiWindowFlags) -> Self {
Window {
flags: flags,
.. 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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
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
}
Window { flags: self.flags.with(ImGuiWindowFlags_MenuBar, value), ..self }
}
#[inline]
pub fn horizontal_scrollbar(self, value: bool) -> Self {
Window {
flags: self.flags.with(ImGuiWindowFlags_HorizontalScrollbar, value),
.. self
}
Window { flags: self.flags.with(ImGuiWindowFlags_HorizontalScrollbar, value), ..self }
}
#[inline]
pub fn no_focus_on_appearing(self, value: bool) -> Self {
Window {
flags: self.flags.with(ImGuiWindowFlags_NoFocusOnAppearing, value),
.. self
}
Window { flags: self.flags.with(ImGuiWindowFlags_NoFocusOnAppearing, value), ..self }
}
#[inline]
pub fn no_bring_to_front_on_focus(self, value: bool) -> Self {
Window {
flags: self.flags.with(ImGuiWindowFlags_NoBringToFrontOnFocus, value),
.. self
}
Window { flags: self.flags.with(ImGuiWindowFlags_NoBringToFrontOnFocus, value), ..self }
}
pub fn build<F: FnOnce()>(self, f: F) {
let render = unsafe {
@ -185,12 +123,11 @@ impl<'ui, 'p> Window<'ui, 'p> {
if !self.size_cond.is_empty() {
imgui_sys::igSetNextWindowSize(self.size.into(), 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)
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();