From b523fe9fb9e95c394720fc3fd2d0a9526f99760b Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Thu, 20 Aug 2015 23:25:57 +0300 Subject: [PATCH] Window options --- examples/test_window.rs | 32 +++++++++++++++++++++++ src/lib.rs | 25 ++++++++++++++++-- src/sliders.rs | 56 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 109 insertions(+), 4 deletions(-) diff --git a/examples/test_window.rs b/examples/test_window.rs index f83896f..c8d42a0 100644 --- a/examples/test_window.rs +++ b/examples/test_window.rs @@ -205,6 +205,38 @@ fn show_test_window<'a>(frame: &Frame<'a>, state: &mut State) -> bool { frame.text_wrapped(im_str!("This window is being created by the show_test_window() function. Please refer to the code for programming reference.\n\nUser Guide:")); show_user_guide(frame); } + + if frame.collapsing_header(im_str!("Window options")).build() { + if let Some(no_titlebar) = frame.checkbox(im_str!("no titlebar"), state.no_titlebar) { + state.no_titlebar = no_titlebar; + } + frame.same_line(150.0); + if let Some(no_border) = frame.checkbox(im_str!("no border"), state.no_border) { + state.no_border = no_border; + } + frame.same_line(300.0); + if let Some(no_resize) = frame.checkbox(im_str!("no resize"), state.no_resize) { + state.no_resize = no_resize; + } + if let Some(no_move) = frame.checkbox(im_str!("no move"), state.no_move) { + state.no_move = no_move; + } + frame.same_line(150.0); + if let Some(no_scrollbar) = frame.checkbox(im_str!("no scrollbar"), state.no_scrollbar) { + state.no_scrollbar = no_scrollbar; + } + frame.same_line(300.0); + if let Some(no_collapse) = frame.checkbox(im_str!("no collapse"), state.no_collapse) { + state.no_collapse = no_collapse; + } + if let Some(no_menu) = frame.checkbox(im_str!("no menu"), state.no_menu) { + state.no_menu = no_menu; + } + if let Some(bg_alpha) = frame.slider_f32(im_str!("bg alpha"), + state.bg_alpha, 0.0, 1.0).build() { + state.bg_alpha = bg_alpha; + } + } }) } diff --git a/src/lib.rs b/src/lib.rs index 4ced5d5..1f9214d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ pub use ffi::{ ImVec2, ImVec4 }; pub use menus::{Menu, MenuItem}; -pub use sliders::{SliderInt}; +pub use sliders::{SliderFloat, SliderInt}; pub use widgets::{CollapsingHeader}; pub use window::{Window}; @@ -231,7 +231,17 @@ impl<'fr> Frame<'fr> { // Layout impl<'fr> Frame<'fr> { - pub fn separator(&self) { unsafe { ffi:: igSeparator() }; } + pub fn separator(&self) { unsafe { ffi::igSeparator() }; } + pub fn same_line(&self, pos_x: f32) { + unsafe { + ffi::igSameLine(pos_x as c_float, -1.0f32 as c_float) + } + } + pub fn same_line_spacing(&self, pos_x: f32, spacing_w: f32) { + unsafe { + ffi::igSameLine(pos_x as c_float, spacing_w as c_float) + } + } pub fn spacing(&self) { unsafe { ffi::igSpacing() }; } } @@ -276,10 +286,21 @@ impl<'fr> Frame<'fr> { pub fn collapsing_header<'p>(&self, label: ImStr<'p>) -> CollapsingHeader<'fr, 'p> { CollapsingHeader::new(label) } + pub fn checkbox<'p>(&self, label: ImStr<'p>, value: bool) -> Option { + let mut result = value; + let changed = unsafe { + ffi::igCheckbox(label.as_ptr(), &mut result) + }; + if changed { Some(result) } else { None } + } } // Widgets: Sliders impl<'fr> Frame<'fr> { + pub fn slider_f32<'p>(&self, label: ImStr<'p>, + value: f32, min: f32, max: f32) -> SliderFloat<'fr, 'p> { + SliderFloat::new(label, value, min, max) + } pub fn slider_i32<'p>(&self, label: ImStr<'p>, value: i32, min: i32, max: i32) -> SliderInt<'fr, 'p> { SliderInt::new(label, value, min, max) diff --git a/src/sliders.rs b/src/sliders.rs index 396229c..23dedba 100644 --- a/src/sliders.rs +++ b/src/sliders.rs @@ -1,4 +1,4 @@ -use libc::c_int; +use libc::{c_float, c_int}; use std::marker::PhantomData; use super::ffi; @@ -22,7 +22,7 @@ impl<'fr, 'p> SliderInt<'fr, 'p> { value: value, min: min, max: max, - display_format: unsafe { ImStr::from_bytes(b"%.0f") }, + display_format: unsafe { ImStr::from_bytes(b"%.0f\0") }, _phantom: PhantomData } } @@ -46,3 +46,55 @@ impl<'fr, 'p> SliderInt<'fr, 'p> { if changed { Some(value as i32) } else { None } } } + +pub struct SliderFloat<'fr, 'p> { + label: ImStr<'p>, + value: f32, + min: f32, + max: f32, + display_format: ImStr<'p>, + power: f32, + _phantom: PhantomData<&'fr Frame<'fr>> +} + +impl<'fr, 'p> SliderFloat<'fr, 'p> { + pub fn new(label: ImStr<'p>, value: 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) -> Option { + let mut value = self.value as c_float; + let changed = unsafe { + ffi::igSliderFloat(self.label.as_ptr(), + &mut value, + self.min as c_float, + self.max as c_float, + self.display_format.as_ptr(), + self.power as c_float + ) + }; + if changed { Some(value as f32) } else { None } + } +} +