diff --git a/examples/test_window_impl.rs b/examples/test_window_impl.rs index a4b83d2..c63d9c2 100644 --- a/examples/test_window_impl.rs +++ b/examples/test_window_impl.rs @@ -35,6 +35,7 @@ struct State { buf: String, text: String, int: i32, + float: f32, auto_resize_state: AutoResizeState, file_menu: FileMenuState } @@ -72,6 +73,7 @@ impl Default for State { buf: buf, text: text, int: 123, + float: 0.001, auto_resize_state: Default::default(), file_menu: Default::default() } @@ -293,6 +295,8 @@ fn show_test_window<'a>(ui: &Ui<'a>, state: &mut State, opened: &mut bool) { ui.label_text(im_str!("label"), im_str!("Value")); ui.input_text(im_str!("input text"), &mut state.text).build(); ui.input_int(im_str!("input int"), &mut state.int).build(); + ui.input_float(im_str!("input float"), &mut state.float) + .step(0.01).step_fast(1.0).build(); } }) } diff --git a/src/input.rs b/src/input.rs index aeb85fc..af3adec 100644 --- a/src/input.rs +++ b/src/input.rs @@ -16,10 +16,10 @@ use super::{ }; macro_rules! impl_text_flags { - ($T:ident) => { + ($InputType:ident) => { #[inline] pub fn flags(self, flags: ImGuiInputTextFlags) -> Self { - $T { + $InputType { flags: flags, .. self } @@ -27,7 +27,7 @@ macro_rules! impl_text_flags { #[inline] pub fn chars_decimal(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CharsDecimal, value), .. self } @@ -35,7 +35,7 @@ macro_rules! impl_text_flags { #[inline] pub fn chars_hexadecimal(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CharsHexadecimal, value), .. self } @@ -43,7 +43,7 @@ macro_rules! impl_text_flags { #[inline] pub fn chars_uppercase(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CharsUppercase, value), .. self } @@ -51,7 +51,7 @@ macro_rules! impl_text_flags { #[inline] pub fn chars_noblank(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CharsNoBlank, value), .. self } @@ -59,7 +59,7 @@ macro_rules! impl_text_flags { #[inline] pub fn auto_select_all(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_AutoSelectAll, value), .. self } @@ -67,7 +67,7 @@ macro_rules! impl_text_flags { #[inline] pub fn enter_returns_true(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_EnterReturnsTrue, value), .. self } @@ -75,7 +75,7 @@ macro_rules! impl_text_flags { #[inline] pub fn callback_completion(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CallbackCompletion, value), .. self } @@ -83,7 +83,7 @@ macro_rules! impl_text_flags { #[inline] pub fn callback_history(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CallbackHistory, value), .. self } @@ -91,7 +91,7 @@ macro_rules! impl_text_flags { #[inline] pub fn callback_always(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CallbackAlways, value), .. self } @@ -99,7 +99,7 @@ macro_rules! impl_text_flags { #[inline] pub fn callback_char_filter(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_CallbackCharFilter, value), .. self } @@ -107,7 +107,7 @@ macro_rules! impl_text_flags { #[inline] pub fn allow_tab_input(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_AllowTabInput, value), .. self } @@ -115,7 +115,7 @@ macro_rules! impl_text_flags { #[inline] pub fn no_horizontal_scroll(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_NoHorizontalScroll, value), .. self } @@ -123,7 +123,7 @@ macro_rules! impl_text_flags { #[inline] pub fn always_insert_mode(self, value: bool) -> Self { - $T { + $InputType { flags: self.flags.with(ImGuiInputTextFlags_AlwaysInsertMode, value), .. self } @@ -132,6 +132,26 @@ macro_rules! impl_text_flags { } } +macro_rules! impl_step_params { + ($InputType:ident, $Value:ty) => { + #[inline] + pub fn step(self, value: $Value) -> Self { + $InputType { + step: value, + .. self + } + } + + #[inline] + pub fn step_fast(self, value: $Value) -> Self { + $InputType { + step_fast: value, + .. self + } + } + } +} + #[must_use] pub struct InputText<'ui, 'p> { label: ImStr<'p>, @@ -173,8 +193,8 @@ impl<'ui, 'p> InputText<'ui, 'p> { pub struct InputInt<'ui, 'p> { label: ImStr<'p>, value: &'p mut i32, - step: i32, - step_fast: i32, + step: i32, + step_fast: i32, flags: ImGuiInputTextFlags, _phantom: PhantomData<&'ui Ui<'ui>> } @@ -184,29 +204,14 @@ impl<'ui, 'p> InputInt<'ui, 'p> { InputInt { label: label, value: value, - step: 1, - step_fast: 100, + step: 1, + step_fast: 100, flags: ImGuiInputTextFlags::empty(), _phantom: PhantomData } } - #[inline] - pub fn step(self, value: i32) -> Self { - InputInt { - step: value, - .. self - } - } - - #[inline] - pub fn step_fast(self, value: i32) -> Self { - InputInt { - step_fast: value, - .. self - } - } - + impl_step_params!(InputInt, i32); impl_text_flags!(InputInt); pub fn build(self) -> bool { @@ -214,8 +219,48 @@ impl<'ui, 'p> InputInt<'ui, 'p> { imgui_sys::igInputInt( self.label.as_ptr(), self.value as *mut i32, - self.step, - self.step_fast, + self.step, + self.step_fast, + self.flags) + } + } +} + +#[must_use] +pub struct InputFloat<'ui, 'p> { + label: ImStr<'p>, + value: &'p mut f32, + step: f32, + step_fast: f32, + decimal_precision: i32, + flags: ImGuiInputTextFlags, + _phantom: PhantomData<&'ui Ui<'ui>> +} + +impl<'ui, 'p> InputFloat<'ui, 'p> { + pub fn new(label: ImStr<'p>, value: &'p mut f32) -> Self { + InputFloat { + label: label, + value: value, + step: 0.0, + step_fast: 0.0, + decimal_precision: -1, + flags: ImGuiInputTextFlags::empty(), + _phantom: PhantomData + } + } + + impl_step_params!(InputFloat, f32); + impl_text_flags!(InputFloat); + + 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) } } diff --git a/src/lib.rs b/src/lib.rs index 73de401..c913fca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub use imgui_sys::{ ImVec2, ImVec4, ImGuiKey }; -pub use input::{InputInt, InputText}; +pub use input::{InputFloat, InputInt, InputText}; pub use menus::{Menu, MenuItem}; pub use sliders::{SliderFloat, SliderInt}; pub use trees::{TreeNode}; @@ -447,12 +447,15 @@ impl<'ui> Ui<'ui> { // Widgets: Input impl<'ui> Ui<'ui> { - pub fn input_int<'p>(&self, label: ImStr<'p>, value: &'p mut i32) -> InputInt<'ui, 'p> { - InputInt::new(label, value) - } pub fn input_text<'p>(&self, label: ImStr<'p>, buf: &'p mut str) -> InputText<'ui, 'p> { InputText::new(label, buf) } + pub fn input_float<'p>(&self, label: ImStr<'p>, value: &'p mut f32) -> InputFloat<'ui, 'p> { + InputFloat::new(label, value) + } + pub fn input_int<'p>(&self, label: ImStr<'p>, value: &'p mut i32) -> InputInt<'ui, 'p> { + InputInt::new(label, value) + } } // Widgets: Sliders