Skip to content

Commit

Permalink
Added input_float, some change in macros and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bitshifter committed Jan 14, 2016
1 parent 1c13208 commit c4fdf85
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 41 deletions.
4 changes: 4 additions & 0 deletions examples/test_window_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct State {
buf: String,
text: String,
int: i32,
float: f32,
auto_resize_state: AutoResizeState,
file_menu: FileMenuState
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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();
}
})
}
Expand Down
119 changes: 82 additions & 37 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,114 +16,114 @@ use super::{
};

macro_rules! impl_text_flags {
($T:ident) => {
($InputType:ident) => {
#[inline]
pub fn flags(self, flags: ImGuiInputTextFlags) -> Self {
$T {
$InputType {
flags: flags,
.. self
}
}

#[inline]
pub fn chars_decimal(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsDecimal, value),
.. self
}
}

#[inline]
pub fn chars_hexadecimal(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsHexadecimal, value),
.. self
}
}

#[inline]
pub fn chars_uppercase(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsUppercase, value),
.. self
}
}

#[inline]
pub fn chars_noblank(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CharsNoBlank, value),
.. self
}
}

#[inline]
pub fn auto_select_all(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_AutoSelectAll, value),
.. self
}
}

#[inline]
pub fn enter_returns_true(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_EnterReturnsTrue, value),
.. self
}
}

#[inline]
pub fn callback_completion(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCompletion, value),
.. self
}
}

#[inline]
pub fn callback_history(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackHistory, value),
.. self
}
}

#[inline]
pub fn callback_always(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackAlways, value),
.. self
}
}

#[inline]
pub fn callback_char_filter(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_CallbackCharFilter, value),
.. self
}
}

#[inline]
pub fn allow_tab_input(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_AllowTabInput, value),
.. self
}
}

#[inline]
pub fn no_horizontal_scroll(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_NoHorizontalScroll, value),
.. self
}
}

#[inline]
pub fn always_insert_mode(self, value: bool) -> Self {
$T {
$InputType {
flags: self.flags.with(ImGuiInputTextFlags_AlwaysInsertMode, value),
.. self
}
Expand All @@ -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>,
Expand Down Expand Up @@ -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>>
}
Expand All @@ -184,38 +204,63 @@ 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 {
unsafe {
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)
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c4fdf85

Please sign in to comment.